Your problem is most likely here.
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:


instead of
for line in seqalign:
    for i in xrange(len(finalmotif_seqs)):      # for item in finalmotif_seqs:
        for i in xrange(len(finalmotif_annot)):     # for item in 
finalmotif_annot:


len just returns one number. So for each loop, you are running through the loop 
only once where i= whatever len returned.
>>> for x in (5,6,7,8):
...    print x
...
5
6
7
8
Honestly, I am not sure why you are not getting the following error
>>> for x in len((5,6,7,8)):
...    print x
...
TypeError: 'int' object is not iterable


I would also avoid using "i" as your nested loop counter. It might work in this 
instance, but gives you more confusion and less flexibility. See the following 
example

>>> for x in xrange(3):
...     print "level 1=", x
...     for x in xrange(4,5):
...         print "level 2=", x
...         for x in xrange(6,9):
...             print "level 3=", x
...
level 1= 0
level 2= 4
level 3= 6
level 3= 7
level 3= 8
level 1= 1
level 2= 4
level 3= 6
level 3= 7
level 3= 8
level 1= 2
level 2= 4
level 3= 6
level 3= 7
level 3= 8

Instead use separate variables. This allows for easier understandability and 
for accessing them from nested loop levels.
>>> for x in xrange(3):
...     print "level 1=", x
...     for y in xrange(4,5):
...         print "level 2=", y
...         for z in xrange(6,9):
...             print "level 3=", z, ' | level 2=', y , ' | level 1=', x
...
level 1= 0
level 2= 4
level 3= 6  | level 2= 4  | level 1= 0
level 3= 7  | level 2= 4  | level 1= 0
level 3= 8  | level 2= 4  | level 1= 0
level 1= 1
level 2= 4
level 3= 6  | level 2= 4  | level 1= 1
level 3= 7  | level 2= 4  | level 1= 1
level 3= 8  | level 2= 4  | level 1= 1
level 1= 2
level 2= 4
level 3= 6  | level 2= 4  | level 1= 2
level 3= 7  | level 2= 4  | level 1= 2
level 3= 8  | level 2= 4  | level 1= 2



Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

From: tutor-bounces+ramit.prasad=jpmchase....@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmchase....@python.org] On Behalf Of Spyros 
Charonis
Sent: Wednesday, May 04, 2011 1:31 PM
To: tutor
Subject: [Tutor] triple-nested for loop not working

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



This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to