[Tutor] character counting

Mustafa Musameh jmmy71 at yahoo.com
Sun Mar 23 07:28:26 CET 2014


Hi;
I have a file that looks like this:
>title 1
AAATTTGGGCCCATA...
TTAACAAGTTAAAT…
>title 2
AAATTTAAACCCGGGG…
ATATATATA…
…

I wrote the following to count the As, Cs, Gs anTs for each title I wrote the following
import sys

file = open('file.fna')

data=file.readlines()

for line in data:

    line = line.rstrip()

    if line.startswith('>') :

        print line

    if not line.startswith('>') :

        seq = line.rstrip()

        counters={}

        for char in seq:

            counters[char] = counters.get(char,0) + 1

        Ks = counters.keys()

        Ks.sort()

        for k in Ks:

            print sum(counters.itervalues())        





I want to get the following out put:

>title
234
>title 1
3453
….
but what i get
>title 1
60
60
60
60
…
it seems it do counting for each line and print it out.

Can you help me please
Thanks

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140323/c9d7f80f/attachment-0001.html>


More information about the Tutor mailing list