Detect Mutation using Python Last Updated : 12 Nov, 2020 Comments Improve Suggest changes Like Article Like Report Prerequisite: Random Numbers in Python The following article depicts how Python can be used to detect a mutated DNA strand. Functions UsedgenerateDNASequence(): 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 baseGetting Started The following steps are followed in order to achieve our required functionality: Import random libraryThe 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. Python3 # import random library import random # function to generate dna strands def generateDNASequence(): # list of available DNA bases l = ['C', 'A', 'G', 'T'] res = "" for i in range(0, 40): # creating the DNA strand by appending # random characters from the list res = res + random.choice(l) return res # function to alter dna strands def applyGammaRadiation(dna): # possibility of mutation is generated randomly pos = random.randint(1, 100) cdna = '' # list of available DNA bases l = ['C', 'A', 'G', 'T'] # if the possibility of mutation generated randomly # is >50 then mutation happens if(pos > 50): # the position where mutation will take place # is chosen randomly changepos = random.randint(0, 39) dl = [] # the characters in DNA strand is converted to list dl[:0] = dna # the character at the determined mutation position # is fetched. ch = "" + dl[changepos] # 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 l.remove(ch) # the new character or DNA base is chosen from the list ms = random.choice(l) cl = [] # DNA strand characters are again appended to a new list cl[:0] = dna # the new base in the mutated position is set cl[changepos] = ms # the characters in the cl list is converted to string again # this is the new mutated DNA string cdna = ''.join([str(e) for e in cl]) # if possibility of mutation is less than 50% then no # mutation happens else: # if no mutation occurs original dna is same as mutated dna cdna = dna return cdna # function to detect mutation def detectMutation(dna, cdna): count = 0 # x and y take each character in dna and cdna # for character by character comparison for x, y in zip(dna, cdna): # if the character at the same index match # then the count is increased if x == y: count = count + 1 # incase of mismatch the loop is broken else: break # the count value points to the index before the # position of mutation return count dna = generateDNASequence() print(dna+" (Original DNA)") cdna = applyGammaRadiation(dna) print(cdna+" (DNA after radiation)") count = detectMutation(dna, cdna) # if count=40 it means all the characters of the 2 strands match # hence no mutation if count == 40: print("No Mutation detected") # if count is less than 40 # it means mutation has occurred else: # ^ denotes the position of mutation pos = "^" print(pos.rjust(count+1)) print("Mutation detected at pos = ", (count+1)) Output Comment More infoAdvertise with us Next Article Detect Mutation using Python S Shreyasi_Chakraborty Follow Improve Article Tags : Python python-utility Python-random Practice Tags : python Similar Reads Detect script exit in Python Python is a scripting language. This means that a Python code is executed line by line with the help of a Python interpreter. When a python interpreter encounters an end-of-file character, it is unable to retrieve any data from the script. This EOF(end-of-file) character is the same as the EOF that 2 min read Detect an Unknown Language using Python The idea behind language detection is based on the detection of the character among the expression and words in the text. The main principle is to detect commonly used words like to, of in English. Python provides various modules for language detection. In this article, the modules covered are: lang 2 min read Python String isidentifier() Method The isidentifier() method in Python is used to check whether a given string qualifies as a valid identifier according to the Python language rules. Identifiers are names used to identify variables, functions, classes, and other objects. A valid identifier must begin with a letter (A-Z or a-z) or an 3 min read Determining file format using Python The general way of recognizing the type of file is by looking at its extension. But this isn't generally the case. This type of standard for recognizing file by associating an extension with a file type is enforced by some operating system families (predominantly Windows). Other OS's such as Linux ( 3 min read Python String isdigit() Method The isdigit() method is a built-in Python function that checks if all characters in a string are digits. This method returns True if each character in the string is a numeric digit (0-9) and False otherwise. Example:Pythona = "12345" print(a.isdigit()) b = "1234a5" print(b.isdigit())OutputTrue False 3 min read Detect an object with OpenCV-Python Object detection refers to identifying and locating objects within images or videos. OpenCV provides a simple way to implement object detection using Haar Cascades a classifier trained to detect objects based on positive and negative images. In this article we will focus on detecting objects using i 4 min read Python String isprintable() Method Python String isprintable() is a built-in method used for string handling. The isprintable() method returns "True" if all characters in the string are printable or the string is empty, Otherwise, It returns "False". This function is used to check if the argument contains any printable characters suc 3 min read Simple Plagiarism Detector in Python In this article, we are going to make a simple plagiarism detector in Python. What is Plagiarism? Plagiarism is simply called cheating. When one person copies the work or idea of another person and uses that in their work by their name it is called plagiarism. For example, if someone writing an arti 3 min read Check if the image is empty using Python - OpenCV Prerequisite: Basics of OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library i 2 min read Python | platform.uname() Method In this article, we will learn how to detect the operating system that is currently being used in your system using Python. Detect OS Using the platform Module in Python The platform.uname() method in python is used to get information about the current operating system. This method returns informati 1 min read Like