<br><br><div class="gmail_quote">On Wed, May 4, 2011 at 13:31, Spyros Charonis <span dir="ltr"><<a href="mailto:s.charonis@gmail.com" target="_blank">s.charonis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div>Hello everyone,</div><div><br></div><div>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.</div>
<div>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: </div>
<div><br></div><div><br></div><div>motif_file = open('myfolder/pythonfiles/final motifs_11SGLOBULIN', 'r') # => final motifs_11sglobulin contains the output of my first program</div><div>align_file = open('myfolder/pythonfiles/11sglobulin.seqs', 'a+') # => 11sglobulin.seqs is the ASCII sequence alignment file which I want to "annotate" (modify) </div>
<div><br></div><div><div>finalmotif_seqs = [] </div><div>finalmotif_length = [] # store length of each motif</div><div>finalmotif_annot = [] </div><div><br></div><div>for line in finalmotifs:</div><div> finalmotif_seqs.append(line)</div>
<div> mot_length = len(line)</div><div> finalmotif_length.append(mot_length)</div><div><br></div><div>for item in finalmotif_length:</div><div> annotation = '~' * item </div><div> finalmotif_annot.append(annotation) </div>
</div><div><br></div><div>finalmotifs = motif_file.readlines()</div><div>seqalign = align_file.readlines() </div></div><div><br></div><div>for line in seqalign: </div><div> for i in len(finalmotif_seqs): # for item in finalmotif_seqs:</div>
<div> for i in len(finalmotif_annot): # for item in finalmotif_annot:</div><div> if finalmotif_seqs[i] in line: # if item in line:</div><div> newline = line.replace(finalmotif_seqs[i], finalmotif_annot[i])</div>
<div> #sys.stdout.write(newline) # => print the lines out on the shell</div><div> align_file.writelines(newline) </div></blockquote><div><br></div><div>Pay attention to scope with the elements of your iteration loops. If you call everything 'item' you can confuse yourself, others, and the interpreter as to which 'item' you're talking about.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br></div><div>motif_file.close()</div>
<div>align_file.close()</div>
<div><br></div><div> </div><div>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! </div>
<div>I am running Python 2.6.5 </div><div><br></div>
<br>_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br>