2013-09-01 23 views
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個以上,我想知道如何操作我的代碼來處理也是?我希望有人能夠理解這個問題。先謝謝你!

+0

這是一樣的這項任務? http://stackoverflow.com/questions/18541215/how-do-you-access-a-list-in-group-of-3-in-python - 看起來像你有你的答案... –

+0

你必須拆分將輸入字符串轉換爲三位,並檢查所有位。 –

+0

基本上你需要的是[你如何將一個列表分割成大小均勻的Python塊?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-蟒蛇大小) –

回答

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是相同長度的,由三個整除。您可能需要事先插入檢查以確保是這種情況。

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." 
+0

這個工程!除非我嘗試最長的例子,它給了我'患者的氨基酸序列發生了突變。 患者的氨基酸序列發生了突變。 患者的氨基酸序列沒有發生突變。而不是隻是第一行,我該如何改變它? – samir

+0

如果您使用'print'而不是'return'(這需要將上面的代碼封裝在一個函數中),就會發生這種情況。 –

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.")