2

So I want to write a program that checks if the DNA is mutated or not, i'm not quite sure how to explain this, so this is what it says:
Write a program to determine if a patient has a mutation in their DNA sequence which results in a change of amino acid sequence. Your program should work like this:

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.

And this is part of the text file I use:

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

So this is my code so far:

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

So my code works OK with the first example, where there are only 3 letters being used, but the next two have more than 3, and I was wondering how I could manipulate my code to work with that too? I hope someone understands this question. Thank you in advance!

samir
  • 759
  • 2
  • 8
  • 13
  • Is this the same as this assignment? http://stackoverflow.com/questions/18541215/how-do-you-access-a-list-in-group-of-3-in-python - looks like you have your answer... – Jon Clements Sep 01 '13 at 11:28
  • You have to split the input string into bits of three, and check all bits. – Lennart Regebro Sep 01 '13 at 11:29
  • Essentially what you need is [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – Lennart Regebro Sep 01 '13 at 11:31

3 Answers3

1

You need to iterate over the codons one by one (strings slices are your friend here), and check the condition for all of them.

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

This assumes that both org and pat are of the same length, divisible by three. You might want to insert a check beforehand to make sure this is the case.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

something like this:

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."
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
  • This works! except when I attempt the longest example, It gives me `The patient's amino acid sequence is mutated. The patient's amino acid sequence is mutated. The patient's amino acid sequence is not mutated.` Instead of just the first line, how do I change that? – samir Sep 01 '13 at 11:35
  • That would happen if you used `print` instead of `return` (which requires wrapping the above code in a function). – Tim Pietzcker Sep 01 '13 at 11:37
0

Shorter version:

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.")
Gill Bates
  • 14,330
  • 23
  • 70
  • 138