In: Computer Science
Code for count "CAG" in DNA sequence is as below. In case of any query, do comment.
#function to count CAG in DNA sequence
def count_CAG_In_DNA(dnaSequence):
countOfCAG = 0
#do-while loop to parse the while DNA sequence
while True:
#find the position of CAG in DNA sequence
positionOfCAG = dnaSequence.find("CAG")
#if no CAG found then break the loop
if (positionOfCAG == -1):
break
#increment the count of CAG
countOfCAG += 1
#extract the new array,slicing array from the character till CAG is found
dnaSequence = dnaSequence[positionOfCAG+3:]
return countOfCAG
# main driver function
#output should be 2
dnaSequence = "TACAGCAGC"
print("Count of CAG in DNA Squence :" + dnaSequence)
print(count_CAG_In_DNA(dnaSequence))
#output should be 0
dnaSequence ="TACATCATC"
print("Count of CAG in DNA Squence :" + dnaSequence)
print(count_CAG_In_DNA(dnaSequence))
#output should be 6
dnaSequence = "TACAGCAGCTACAGCAGCTACAGCAGC"
print("Count of CAG in DNA Squence :" + dnaSequence)
print(count_CAG_In_DNA(dnaSequence))
#output should be 9
dnaSequence = "TACAGCAGCTACAGCAGCTACAGCAGCTACAGCAGCTACAGC"
print("Count of CAG in DNA Squence :" + dnaSequence)
print(count_CAG_In_DNA(dnaSequence))
#output should be 7
dnaSequence ="TACAGCAGCTACAGCTACAGCAGCTACAGCAGC"
print("Count of CAG in DNA Squence :" + dnaSequence)
print(count_CAG_In_DNA(dnaSequence))
#===========================================Screen shot of code ==============
Screen shot of the code for indentation:
Output:
Please rate your answer. Thanks