Rosalind共识和配置文件python

时间:2016-07-26 15:22:39

标签: python python-2.7 bioinformatics rosalind

我正在处理这个问题" Consensus nd Profile"罗莎琳德生物信息学网站(http://rosalind.info/problems/cons/)。我使用网站上的示例输入尝试了我的代码,我的输出与示例输出匹配。但当我尝试更大的数据集时,网站说我的输出是错误的。有人可以帮我确定我的问题所在吗?谢谢!

示例输入:

>Rosalind_1
ATCCAGCT
>Rosalind_2
GGGCAACT
>Rosalind_3
ATGGATCT
>Rosalind_4
AAGCAACC
>Rosalind_5
TTGGAACT
>Rosalind_6
ATGCCATT
>Rosalind_7
ATGGCACT

我已经提取了dna字符串并将它们存储在一个名为字符串的列表中(我在此步骤中使用较大数据集的试验是正确的,所以我在这里省略了我的代码):

['ATCCAGCT', 'GGGCAACT', 'ATGGATCT', 'AAGCAACC', 'TTGGAACT', 'ATGCCATT', 'ATGGCACT']

之后我的代码:

#convert strings into matrix
matrix = []
for i in strings:
    matrix.append([j for j in i])
M = np.array(matrix).reshape(len(matrix),len(matrix[0]))

对于样本输入,M看起来像这样:

[['A' 'T' 'C' 'C' 'A' 'G' 'C' 'T']
 ['G' 'G' 'G' 'C' 'A' 'A' 'C' 'T']
 ['A' 'T' 'G' 'G' 'A' 'T' 'C' 'T']
 ['A' 'A' 'G' 'C' 'A' 'A' 'C' 'C']
 ['T' 'T' 'G' 'G' 'A' 'A' 'C' 'T']
 ['A' 'T' 'G' 'C' 'C' 'A' 'T' 'T']
 ['A' 'T' 'G' 'G' 'C' 'A' 'C' 'T']]

之后我的代码:

#convert string matrix into profile matrix
A = []
C = []
G = []
T = []
for i in range(len(matrix[0])):
    A_count = 0
    C_count = 0
    G_count = 0
    T_count = 0
    for j in M[:,i]:
        if j == "A":
            A_count += 1
        elif j == "C":
            C_count += 1
        elif j == "G":
            G_count += 1
        elif j == "T":
            T_count += 1
    A.append(A_count)
    C.append(C_count)
    G.append(G_count)
    T.append(T_count)

profile_matrix = {"A": A, "C": C, "G": G, "T": T}
for k, v in profile_matrix.items():
    print k + ":" + " ".join(str(x) for x in v)

#get consensus string
P = []
P.append(A)
P.append(C)
P.append(G)
P.append(T)
profile = np.array(P).reshape(4, len(A))
consensus = []
for i in range(len(A)):
    if max(profile[:,i]) == profile[0,i]:
        consensus.append("A")
    elif max(profile[:,i]) == profile[1,i]:
        consensus.append("C")
    elif max(profile[:,i]) == profile[2,i]:
        consensus.append("G")
    elif max(profile[:,i]) == profile[3,i]:
        consensus.append("T")
print "".join(consensus)

这些代码提供正确的样本输出:

A:5 1 0 0 5 5 0 0
C:0 0 1 4 2 0 6 1
T:1 5 0 0 0 1 1 6
G:1 1 6 3 0 1 0 0
ATGCAACT

但是当我尝试使用更大的数据集时,网站上说我的答案是错误的...有人能指出我错在哪里吗? (我是初学者,谢谢你的耐心等待!)

1 个答案:

答案 0 :(得分:1)

你的算法完全没问题。正如@C_Z_指出“确保您的格式与样本输出完全匹配”,遗憾的是并非如此。

print k + ":" + " ".join(str(x) for x in v)

应该是

print k + ": " + " ".join(str(x) for x in v)

之后,而不是之前的共识序列。 如果您更改订单并添加空间,您的答案将被rosalind接受。

由于这是对您的问题的一个简单回答,这里是针对同一问题的替代解决方案,而不使用numpy: 不使用每个核苷酸的变量,而是使用字典。用23种氨基酸做同样的事情并不好玩,例如

from collections import defaultdict
for i in range(len(strings[0])):
    counter.append(defaultdict(int))
    for seq in seqs:
        counter[i][seq[i]] += 1
    consensus += max(counter[i], key=counter[i].get)

counter为每个职位存储dictionary,其中包含所有基数的所有计数。字典的关键是当前的基础。