[Tutor] triple-nested for loop not working
bob gailer
bgailer at gmail.com
Wed May 4 22:58:56 CEST 2011
On 5/4/2011 2:31 PM, Spyros Charonis wrote:
> 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 = []
>
Following line will fail, as finalmotifs is not defined. It appears to
be defined 10 lines later.
> 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:
Next 2 for statements both use i as the loop index. Even though the
program will run it is not a good idea.
> 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()
>
You need 2 loops, not 3. That is way overkill.You accomplish the same
thing with:
# open the 3 files
finalmotifs = motif_file.readlines()
for line in seqalign:
for item in finalmotifs:
if item in line:
align_file.write('~' * len(line) + '\n')
# close the 3 files
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list