Rosalind 共识和简介 python
Rosalind consensus and profile python
我正在 Rosalind Bioinformatics 网站 (http://rosalind.info/problems/cons/) 上解决问题 "Consensus nd Profile"。我使用网站上的样本输入尝试了我的代码,我的输出与样本输出相匹配。但是当我尝试更大的数据集时,网站说我的输出是错误的。有人可以帮我确定我的问题出在哪里吗?谢谢!
示例输入:
>Rosalind_1
ATCCAGCT
>Rosalind_2
GGGCAACT
>Rosalind_3
ATGGATCT
>Rosalind_4
AAGCAACC
>Rosalind_5
TTGGAACT
>Rosalind_6
ATGCCATT
>Rosalind_7
ATGGCACT
我已经提取了 dna 字符串并将它们存储在一个名为 strings 的列表中(我在这一步对更大数据集的试验是正确的,所以我在这里省略了我的代码):
['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
但是当我尝试更大的数据集时,网站说我的答案是错误的...有人能指出我错在哪里吗? (我是初学者,感谢您的耐心等待!)
你的算法完全没问题。正如@C_Z_ 所指出的 "make sure your format matches the sample output exactly" 不幸的是事实并非如此。
print k + ":" + " ".join(str(x) for x in v)
应该是
print k + ": " + " ".join(str(x) for x in v)
在共识序列之后,而不是之前。
如果您更改顺序并添加 space,您的答案将被 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
,其中包含所有碱基的所有计数。字典的键是当前基数。
我正在 Rosalind Bioinformatics 网站 (http://rosalind.info/problems/cons/) 上解决问题 "Consensus nd Profile"。我使用网站上的样本输入尝试了我的代码,我的输出与样本输出相匹配。但是当我尝试更大的数据集时,网站说我的输出是错误的。有人可以帮我确定我的问题出在哪里吗?谢谢!
示例输入:
>Rosalind_1
ATCCAGCT
>Rosalind_2
GGGCAACT
>Rosalind_3
ATGGATCT
>Rosalind_4
AAGCAACC
>Rosalind_5
TTGGAACT
>Rosalind_6
ATGCCATT
>Rosalind_7
ATGGCACT
我已经提取了 dna 字符串并将它们存储在一个名为 strings 的列表中(我在这一步对更大数据集的试验是正确的,所以我在这里省略了我的代码):
['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
但是当我尝试更大的数据集时,网站说我的答案是错误的...有人能指出我错在哪里吗? (我是初学者,感谢您的耐心等待!)
你的算法完全没问题。正如@C_Z_ 所指出的 "make sure your format matches the sample output exactly" 不幸的是事实并非如此。
print k + ":" + " ".join(str(x) for x in v)
应该是
print k + ": " + " ".join(str(x) for x in v)
在共识序列之后,而不是之前。 如果您更改顺序并添加 space,您的答案将被 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
,其中包含所有碱基的所有计数。字典的键是当前基数。