Is there an easier way to reverse a given pattern than the way I have coded it?
Example of the program:
Enter a DNA sequence: CGATTGAATTACAAG TCCAATT
Enter the pattern: TGAA
Mutated DNA sequence: CGATTGAACATTAAG TCCAATT
***DNA.txt reads: "CGATTGAATTACAA GTCCAATT" ***
Example of the program:
Enter a DNA sequence: CGATTGAATTACAAG TCCAATT
Enter the pattern: TGAA
Mutated DNA sequence: CGATTGAACATTAAG TCCAATT
***DNA.txt reads: "CGATTGAATTACAA GTCCAATT" ***
Code:
source = file('DNA.txt')
DNA = source.read()
print DNA
while not found:
pattern = raw_input('Enter a pattern: ')
pattern = pattern.upper()
reverse = pattern[::-1]
if pattern in DNA and reverse in DNA:
found = True
# print pattern, reverse
first = DNA.index(pattern) + len(pattern) # find the end of the pattern index in our DNA string
last = DNA.index(reverse) # find the beginning index of reverse pattern in DNA string
beginning = DNA[:first] # beginning is the string of DNA including the pattern
middle = DNA[first:last] # middle is the string between the pattern and the reverse of the pattern
middle = middle[::-1] # reverse the order of the middle characters (this is the mutated portion)
ending = DNA[last:] # ending is the DNA string from the start of the pattern reverse to end
mutated = beginning + middle + ending #join the DNA pieces with mutated middle together
print mutated
source.close()
Comment