【发布时间】:2013-01-18 10:59:34
【问题描述】:
我找到了解析 fasta frmated 文件的代码。我需要统计每个序列中有多少个A、T、G等,例如:
>gi|7290019|gb|AAF45486.1| (AE003417) EG:BACR37P7.1 gene product [Drosophila melanogaster]
MRMRGRRLLPIIL
在这个序列中有:
M - 2
R - 4
G - 1
L - 3
I - 2
P - 1
代码很简单:
def FASTA(filename):
try:
f = file(filename)
except IOError:
print "The file, %s, does not exist" % filename
return
order = []
sequences = {}
for line in f:
if line.startswith('>'):
name = line[1:].rstrip('\n')
name = name.replace('_', ' ')
order.append(name)
sequences[name] = ''
else:
sequences[name] += line.rstrip('\n').rstrip('*')
print "%d sequences found" % len(order)
return order, sequences
x, y = FASTA("drosoph_b.fasta")
但是我如何计算这些氨基酸呢?我不想使用 BioPython,我想知道如何使用,例如 count...
【问题讨论】:
-
您的代码只是将
>行和序列行放在两个列表中。要计算序列行中的字符,您需要collections.Counter(line)。 -
“我不想使用 BioPython”——为什么?如果您只是想学习 Python,那很好,否则有充分的理由使用现有的库。
-
你也可以问biostars:biostar.stackexchange.com
标签: python bioinformatics biopython fasta