开发者

dividing sequence in a file into 3s and numbering them

I have a file containing the sequence: ATGAAGCTAGGCATATTC. I want a simple python code that will divide this sequence into 3s: ATG AAG GCT.... and then divide their indices to 3s too: 123 456 789........

The final print output should look like this:

ATG 123
AAG 456
CTA 789
GGC 101112
ATA 131415开发者_如何学编程
TTC 161718

This is what I have done so far:

#!/usr/bin/python
import string
import sys
fileName1=sys.argv[1];
o=open(fileName1);
I=o.readlines();
I=map(string.strip,I);
I=''.join(I);
for i in range(0,len(I),3):
    print I[i:i+3], i+1; 

and I am getting this output:

ATG 1
AAG 4
CTA 7
GGC 10
ATA 13
TTC 16

The first column is correct but the second column is not what I want.


You can tell the read instruction to get three chars at a time:

i=1
while True :
    v = o.read(3)
    print v, " ", i, i+1, i+2
    i+=3
    if len(v) < 3:
        break


Instead of

print I[i:i+3], i+1; 

Do

print I[i:i+3], i+1, i+2, i+3; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜