Detect Mutation using Python
Prerequisite: Random Numbers in Python
The following article depicts how Python can be used to detect a mutated DNA strand.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- generateDNASequence(): This method generates a random DNA strand of length 40 characters using the list of DNA bases A,C,G,T. This method returns the generated DNA strand.
- applyGammaRadiation(): This method takes the DNA strand generated by the above method as input parameter and then alters the strand at a random position only if the probability of mutation, which is also generated randomly is above 50%. The DNA base at the chosen position must be different from the DNA base with which it is replaced. This method returns the altered DNA strand.
- detectMutation(): This method takes the original and altered DNA strand as input and checks if the two strings are altered. If the strings are altered it returns the position of the altered DNA base
The following steps are followed in order to achieve our required functionality:
- Import random library
- The generateDNASequence() function is used to generate DNA strands. DNA strand is generated by randomly choosing characters from the list of available options. When the string length becomes 40 the loop is completed and the DNA strand is returned.
- The applyGammaRadiation() function to alter DNA strands. The possibility of mutation is generated randomly. If the possibility of mutation generated randomly is greater than 50 then mutation occurs else mutation does not occur. If mutation were to occur, the position of mutation is chosen randomly.
- Next, the characters in DNA strand is converted to list.
- The character at the position of mutation is fetched. Since the fetched character should be different from the one replacing it, we remove the fetched character from the list of available choices for choosing another character in its place. The new character or DNA base is chosen from the list.
- Original DNA strand characters are again appended to a new list. The new base/character is set in the mutated position.
- The characters in the cl list is converted to string again using join(). This is the new mutated DNA string.
- If no mutation occurs, original dna is same as mutated DNA.
- Finally the mutated/unmutated DNA is returned.
- Then detectMutation() function is used to detect mutation. In this function, x and y take each character in dna and cdna for character by character comparison. If the character at the same index match, then the count is increased. Incase of mismatch the loop is broken.
- The count value points to the index before the position of mutation. If count=40 it means all the characters of the 2 strands match, hence no mutation If count is less than 40, it means mutation has occurred.
Below is the Implementation.