On Wed, May 4, 2011 at 13:31, Spyros Charonis <s.charo...@gmail.com> 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 = []
>
> 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)
>

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.


>
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to