2

所以我想写一个程序来检查 DNA 是否突变,我不太清楚如何解释,所以这就是它所说的:
写一个程序来确定患者的 DNA 序列是否有突变从而导致氨基酸序列发生变化。你的程序应该像这样工作:

Enter original DNA: AAT
Enter patient DNA: AAC
The patient's amino acid sequence is not mutated.

Enter original DNA: AATTGTTCTTCT
Enter patient DNA: AACTGCAGCTCA
The patient's amino acid sequence is not mutated.

Enter original DNA: TGTCATGCCTTATTAGAAAACGGTGAG
Enter patient DNA: TGTCATGTCTTATTAGAAAAAGGTGAG
The patient's amino acid sequence is mutated.

这是我使用的文本文件的一部分:

The mappings from codon to amino acid are available in a file, called codon_amino.txt, an excerpt of which is shown here:


AAC N
AAG K
AAT N
GCG A
GCT A
GGA G
GGC G
GGG G
GGT G

所以这是我到目前为止的代码:

n = {}
for line in open('codons.txt'):
    codon, codons = line.split()
    n[codon] = codons


org = input('Enter original DNA: ')
pat = input('Enter patient DNA: ')

if n[org] == n[pat]:
    print("The patient's amino acid sequence is not mutated.")
else:
    print("The patient's amino acid sequence is mutated.")

所以我的代码在第一个例子中运行良好,只使用了 3 个字母,但接下来的两个字母超过 3 个,我想知道如何操纵我的代码来处理它?我希望有人能理解这个问题。先感谢您!

4

3 回答 3

1

您需要逐个迭代密码子(字符串切片是您的朋友),并检查所有密码子的条件。

mutated = False           # First, assume no mutation is present
for index in range(0, len(org), 3):
    if n[org[index:index+3]] != n[pat[index:index+3]]:
        mutated = True    # Set mutation flag to True
        break             # Abort the loop - no point in checking any further
if mutated:
    print("The patient's amino acid sequence is mutated.")
else:
    print("The patient's amino acid sequence is not mutated.")

这假设两者orgpat具有相同的长度,可以被三整除。您可能需要事先插入一张支票以确保是这种情况。

于 2013-09-01T11:30:54.663 回答
1

像这样的东西:

for i in range(0, len(org), 3):
    if n[org[i:i + 3]] != n[pat[i:i + 3]]:
        return "The patient's amino acid sequence is mutated."

return "The patient's amino acid sequence is not mutated."
于 2013-09-01T11:31:06.510 回答
0

较短的版本:

n = {}
for line in open('codons.txt'):
    codon, codons = line.split()
    n[codon] = codons


org = input('Enter original DNA: ')
pat = input('Enter patient DNA: ')

if len(org) + len(pat) % 3 or len(org) != len(pat):
    raise ValueError

if any((n[org[i: i+3]] != n[pat[i: i+3]] for i in xrange(len(org)))):
    print("The patient's amino acid sequence is mutated.")
else:
    print("The patient's amino acid sequence is not mutated.")
于 2013-09-01T11:48:23.190 回答