[Tutor] second if

rahmad akbar matbioinfo at gmail.com
Fri Feb 7 17:14:12 CET 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140207/1ea960ae/attachment-0001.html>


More information about the Tutor mailing list