我有一个包含两个序列的文件。我有一个程序可以读取所有序列,将它们组合在一起,并显示两个序列的长度。现在我想单独显示长度。这两个序列由符号>
分隔。
示例:
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)
答案 0 :(得分:4)
for line in open("clostp1.fa"):
name, sequence = map(str.strip,line.split('>'))
print "The length of %s is %s"%(name, len(sequence))
答案 1 :(得分: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
你得到了字典,然后打印你需要打印的东西。
答案 2 :(得分: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中使用,使用
print("The length of {0} is {1}".format(name, len(seq)))
使它在py2.6中工作。
答案 3 :(得分: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']))