2009-10-15 2 views
0

두 개의 시퀀스가 ​​들어있는 파일이 있습니다. 나는 모든 서열을 읽고, 함께 결합하고, 두 서열의 길이를 함께 표시 할 수있는 프로그램을 가지고있다. 이제 길이를 개별적으로 표시하려고합니다. 두 시퀀스는 기호 >으로 구분됩니다.파일에 개별 시퀀스의 길이 표시

예 :

SEQ1 >ATGGGACTAGCAGT 

SEQ2 >AGGATGATGAGTGA 

프로그램 : 내가 제대로 이해하면

#!usr/bin/python 
import re 
fh=open('clostp1.fa','r') 
count=0 
content=fh.readlines() 
fh.close() 
seq='' 
patt=re.compile('>(.*?)') 
for item in content: 
    m=patt.match(item) 
    if not m: 
     s=item.replace('\n','') 
     seq=seq+s 
seq=seq.replace('\s','')  
print seq 
print 'The length of the coding sequence of the bacillus' 
print len(seq) 
+1

비 탐욕스러운 패턴 부분 (코드에서'..? ')은 항상 빈 문자열과 일치하므로 패턴 끝에는 의미가 없습니다. –

답변

4
for line in open("clostp1.fa"): 
    name, sequence = map(str.strip,line.split('>')) 
    print "The length of %s is %s"%(name, len(sequence)) 
+0

실제로 나는 이것을 선호합니다 : P – mandel

+0

각 행에 '>'가 하나만있는 경우 작동합니다.) – user149513

+0

예제에는 '>'가 하나만 있습니다. 하나 이상이있을 수 있다면 우리는 그 라인들로 무엇을해야 하는지를 알아야합니다. –

1

, 당신이 바로 그 길이에 따라 각각의 순서를 인쇄하려면? 난 당신이 단지 시퀀스를 반환하고 나중에 그들과 함께 원하는 것을 할 수있는 기능을 가지고 있다고 생각합니다.

#!usr/bin/python 
import re 

def get_content(file): 
    """ 
    Returns a dict with the name of the seq and its value 
    """ 
    result = {} 
    for current_line in open(file): 
     name, value = line.strip().split(">") 
     result[name] = value 
    return result 

당신은 dict을 얻은 다음 인쇄해야 할 내용을 인쇄합니다.

0
for line in open("clostp1.fa"): 
    name, _, seq = line.partition('>') 
    name, seq = name.rstrip(), seq.rstrip() 
    print("The length of {} is {}".format(name, len(seq))) 

partition 여기에 다음 split 더 적합합니다. 당신은 rstrip에 각각의 부분을해야하고, 형식 구문, py3.1에서 작동이 py2.6에서 작동하도록

print("The length of {0} is {1}".format(name, len(seq))) 

을 사용합니다.

+1

파티션은 3-tuple을 반환하지 않을까? –

+0

oops, yes, thanks gnibbler – SilentGhost

0
import re 
pattern = re.compile('(?P<seqname>\w*)\s*>\s*(?P<seqval>\w*)') 
for item in open('clostp1.fa','r').readlines(): 
    m = pattern.match(item) 
    if m: 
     print "sequence name: %s - %s length" % (m.groupdict()['seqname'],len(m.groupdict()['seqval'])) 
+0

.readlines()가 없어도 파일을 반복 할 수 있습니다. readlines()는 전체 파일을 한 번에 메모리로 읽습니다. 파일이 너무 크면 좋지 않을 수 있습니다. –

관련 문제