[Tutor] second if

David Palao dpalao.python at gmail.com
Mon Feb 10 17:11:08 CET 2014


I guess the replies by Alan and Peter precisely answer to your question?

Best

2014-02-10 12:46 GMT+01:00 rahmad akbar <matbioinfo at gmail.com>:
> David,
>
> thanks for your reply. i cant figure out why the if at that point and what
> is the 'if' try to accompolish
>
>
> On Mon, Feb 10, 2014 at 11:52 AM, David Palao <dpalao.python at gmail.com>
> wrote:
>>
>> Also, could you explain better what is your doubt? You don't
>> understand what "index >= 1" means, or why this "if" at this point, or
>> anything else?
>>
>> Best
>>
>> 2014-02-07 17:14 GMT+01:00 rahmad akbar <matbioinfo at gmail.com>:
>> > he guys, i am trying to understand this code: i understand the first if
>> > statement (if line.startswith..) in read_fasta function but couldnt
>> > understand the next one(if index >=...). thanks in advance!!
>> >
>> > import sys
>> > #class declaration with both attributes we need
>> > class Fasta:
>> >     def __init__(self, name, sequence):
>> >         #this will store the sequence name
>> >         self.name = name
>> >         #this  will store the sequence itself
>> >         self.sequence = sequence
>> >
>> > #this function will receive the list with the file
>> > #contents, create instances of the Fasta class as
>> > #it scans the list, putting the sequence name on the
>> > #first attribute and the sequence itself on the second
>> > #attribute
>> > def read_fasta(file):
>> >     #we declare an empty list that will store all
>> >     #Fasta class instances generated
>> >     items = []
>> >     index = 0
>> >     for line in file:
>> >     #we check to see if the line starts with a > sign
>> >         if line.startswith(">"):
>> >            #if so and our counter is large than 1
>> >            #we add the created class instance to our list
>> >            #a counter larger than 1 means we are reading
>> >            #from sequences 2 and above
>> >            if index >= 1:
>> >                items.append(aninstance)
>> >            index+=1
>> >            #we add the line contents to a string
>> >            name = line[:-1]
>> >            #and initialize the string to store the sequence
>> >            seq = ''
>> >            #this creates a class instance and we add the attributes
>> >            #which are the strings name and seq
>> >            aninstance = Fasta(name, seq)
>> >         else:
>> >            #the line does not start with > so it has to be
>> >            #a sequence line, so we increment the string and
>> >            #add it to the created instance
>> >             seq += line[:-1]
>> >             aninstance = Fasta(name, seq)
>> >
>> >     #the loop before reads everything but the penultimate
>> >     #sequence is added at the end, so we need to add it
>> >     #after the loop ends
>> >     items.append(aninstance)
>> >     #a list with all read sequences is returned
>> >     return items
>> >
>> > fastafile = open(sys.argv[1], 'r').readlines()
>> > mysequences = read_fasta(fastafile)
>> >
>> > print mysequences
>> >
>> > for i in mysequences:
>> >     print i.name
>> >
>> > --
>> > many thanks
>> > mat
>> >
>> > _______________________________________________
>> > Tutor maillist  -  Tutor at python.org
>> > To unsubscribe or change subscription options:
>> > https://mail.python.org/mailman/listinfo/tutor
>> >
>
>
>
>
> --
> many thanks
> mat


More information about the Tutor mailing list