[Tutor] character counting

Cameron Simpson cs at zip.com.au
Sun Mar 23 22:51:12 CET 2014


On 23Mar2014 17:28, Mustafa Musameh <jmmy71 at yahoo.com> wrote:
> 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('>') :

You could just say "else" here instead of "if not".

>         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())

This prints the same sum as many times as there are keys.
Notice that your print statement has no mention of "k"?

You either want just the "print" with no loop over Ks or you want
the loop, with some expression inside which changes depending on
the value of "k". You call, of course, depending on your desired
result.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

"Don't you know the speed limit is 55 miles per hour???"
"Yeah, but I wasn't going to be out that long."
        - Steven Wright


More information about the Tutor mailing list