[Tutor] please help

Cameron Simpson cs at zip.com.au
Fri Mar 21 22:39:16 CET 2014


On 21Mar2014 20:31, Mustafa Musameh <jmmy71 at yahoo.com> wrote:
> Please help. I have been search the internet to understand how to write a simple program/script with python, and I did not do anything.
> I have a file that look like this
> >ID 1
> agtcgtacgt…
> >ID 2
> attttaaaaggggcccttcc
> .
> .
> .
> in other words, it contains several IDs each one has a sequence of 'acgt' letters
> I need to write a script in python where the output will be, for example, like this 
> > ID 1
> a = 10%, c = 40%,  g=40%, t = 10%
> >ID 2
> a = 15%, c = 35%,  g=35%, t = 15%
> .
> .
> .
> (i mean the first line is the ID and the second line is the frequency of each letter )
> How I can tell python to print the first line as it is and count characters starting from the second line till the beginning of the next '>' and so on

You want a loop that reads lines in pairs. Example:

  while True:
    line1 = fp.readline()
    print line1,
    line2 = fp.readline()
    ... process the line and report ...

Then to process the line, iterate over the line. Because a line is
string, and a string is a sequence of characters, you can write:

  for c in line2:
    ... collect statistics about c ...
  ... print report ...

I would collect the statistics using a dictionary to keep count of
the characters. See the dict.setdefault method; it should be helpful.

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

... in '59 I ran out of brakes *four times* -- and I don't mean they didn't
work very well, I mean I had none.  Like the main oil line had sheared.  You
know, so that oil, you know, when you put your foot on the floor, the oil
just went squirting out into the atmosphere.  Things like that.  You know,
I'd always believed that Colin was close to genius in his design ability
and everything -- if he could just get over this failing of his of making
things too bloody light.  I mean, Colin's idea of a Grand Prix car was
it should win the race and, as it crossed the finishing line, it should
collapse in a heap of bits.  If it didn't do that, it was built too strongly.
        - Innes Ireland


More information about the Tutor mailing list