[Tutor] triple-nested for loop not working
Spyros Charonis
s.charonis at gmail.com
Wed May 4 20:31:07 CEST 2011
Hello everyone,
I have written a program, as part of a bioinformatics project, that extracts
motif sequences (programmatically just strings of letters) from a database
and writes them to a file.
I have written another script to annotate the database file (in plaintext
ASCII format) by replacing every match of a motif with a sequence of tildes
(~). Primitive I know, but not much more can be done with ASCII files. The
code goes as follows:
motif_file = open('myfolder/pythonfiles/final motifs_11SGLOBULIN', 'r') #
=> final motifs_11sglobulin contains the output of my first program
align_file = open('myfolder/pythonfiles/11sglobulin.seqs', 'a+') #
=> 11sglobulin.seqs is the ASCII sequence alignment file which I want to
"annotate" (modify)
finalmotif_seqs = []
finalmotif_length = [] # store length of each motif
finalmotif_annot = []
for line in finalmotifs:
finalmotif_seqs.append(line)
mot_length = len(line)
finalmotif_length.append(mot_length)
for item in finalmotif_length:
annotation = '~' * item
finalmotif_annot.append(annotation)
finalmotifs = motif_file.readlines()
seqalign = align_file.readlines()
for line in seqalign:
for i in len(finalmotif_seqs): # for item in finalmotif_seqs:
for i in len(finalmotif_annot): # for item in finalmotif_annot:
if finalmotif_seqs[i] in line: # if item in line:
newline = line.replace(finalmotif_seqs[i],
finalmotif_annot[i])
#sys.stdout.write(newline) # => print the lines out on
the shell
align_file.writelines(newline)
motif_file.close()
align_file.close()
My coding issue is that although the script runs, there is a logic error
somewhere in the triple-nested for loop as I when I check my file I'm
supposedly modifying there is no change. All three lists are built correctly
(I've confirmed this on the Python shell). Any help would be much
appreciated!
I am running Python 2.6.5
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110504/62823f15/attachment.html>
More information about the Tutor
mailing list