1

我有这种格式的数据:

>abc12
ATCGACAG

>def34
ACCGACG

等等

我已将每个基因存储到字典中,其中以 > 开头的行作为值。所以字典类似于{'abc12':'ATCGACAG'等}

现在我希望能够比较每个基因,以便计算每个位点的 A、T、C 或 G 的数量。

我唯一能想到的是将字典分解为每个核苷酸位点的列表,并使用带有计数器的 zip()。这是最好的方法吗?如果是这样,我如何将字典分解为每个站点的列表?

4

3 回答 3

4

使用collections.Counter

>>> from collections import Counter
>>> Counter('ATCGACAG')
Counter({'A': 3, 'C': 2, 'G': 2, 'T': 1})
>>> Counter('ACCGACG')
Counter({'C': 3, 'A': 2, 'G': 2})
于 2012-09-27T22:39:56.437 回答
1

有理由不使用 Biopython 吗?

from Bio import AlignIO
alignment =AlignIO.read("alignment.fas", "fasta")
n=0
while n<len(alignment[0]):
    A=alignment[:,n].count('A')
    C=alignment[:,n].count('C')
    G=alignment[:,n].count('G')
    T=alignment[:,n].count('T')
    gap=alignment[:,n].count('-')

    print "at position %s there are %s A's, %s C's, %s G's, %s T's and %s gaps" % (n, A, C, G, T, gap)
    n=n+1

确保你有一个真正的对齐(即序列是相同的长度)。
ps 对于打印语句的丑陋格式,我深表歉意...

这返回

at position 0 there are 2 A's, 0 C's, 0 G's, 0 T's and 0 gaps
at position 1 there are 0 A's, 1 C's, 0 G's, 1 T's and 0 gaps
at position 2 there are 0 A's, 2 C's, 0 G's, 0 T's and 0 gaps
at position 3 there are 0 A's, 0 C's, 2 G's, 0 T's and 0 gaps
at position 4 there are 2 A's, 0 C's, 0 G's, 0 T's and 0 gaps
at position 5 there are 0 A's, 2 C's, 0 G's, 0 T's and 0 gaps
at position 6 there are 1 A's, 0 C's, 0 G's, 0 T's and 1 gaps
at position 7 there are 0 A's, 0 C's, 2 G's, 0 T's and 0 gaps
于 2012-09-28T01:51:55.343 回答
0
s1 = "ATCGACAG"
s2 = "ACCGACG"   
alignment = [s1[i] if s1[i] == s2[i] else "-" for i in range(len(min([s1,s2],key=len)))]
print "".join(alignment)
A-CGAC-
于 2012-09-27T23:44:59.433 回答