By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Easier method to reverse a given pattern?

P: 32
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: CGATTGAATTACAAGTCCAATT
Enter the pattern: TGAA
Mutated DNA sequence: CGATTGAACATTAAGTCCAATT

***DNA.txt reads: "CGATTGAATTACAAGTCCAATT" ***
Expand|Select|Wrap|Line Numbers
  1. source = file('DNA.txt')
  2. DNA = source.read()
  3. print DNA
  4.  
  5. while not found:
  6.     pattern = raw_input('Enter a pattern: ')
  7.     pattern = pattern.upper()
  8.     reverse = pattern[::-1]
  9.     if pattern in DNA and reverse in DNA:
  10.         found = True
  11.  
  12. # print pattern, reverse
  13. first = DNA.index(pattern) + len(pattern) # find the end of the pattern index in our DNA string
  14. last = DNA.index(reverse)                 # find the beginning index of reverse pattern in DNA string
  15. beginning = DNA[:first]                   # beginning is the string of DNA including the pattern
  16. middle = DNA[first:last]                  # middle is the string between the pattern and the reverse of the pattern
  17. middle = middle[::-1]                     # reverse the order of the middle characters (this is the mutated portion)
  18. ending = DNA[last:]                       # ending is the DNA string from the start of the pattern reverse to end
  19.  
  20. mutated = beginning + middle + ending     #join the DNA pieces with mutated middle together
  21. print mutated
  22.  
  23. source.close()
Nov 11 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 624
That is probably the best way to go but it depends of course on the definition of "easier". It's just personal preference to split into a list of sequences the length of "pattern entered" and work with that.
Expand|Select|Wrap|Line Numbers
  1. original = "CGATTGAATTACAAGTCCAATT"
  2. enter_pattern = "TGAA"
  3. reverse = enter_pattern[::-1]
  4. print reverse
  5. divisor = len(enter_pattern)
  6.  
  7. ## break into sections of length len(pattern)
  8. split_list = []
  9. junk_list = []
  10. for ctr, ch in enumerate(original):
  11.     if (ctr) and (ctr%divisor == 0):
  12.         split_list.append("".join(junk_list))
  13.         junk_list = []
  14.     junk_list.append(ch)
  15. split_list.append("".join(junk_list))
  16. print split_list
  17.  
  18. ## find pattern and reverse next sequence unless this
  19. ## is the final sequence
  20. start = split_list.index(enter_pattern)
  21. end = split_list.index(reverse)
  22. if (start > -1) and (end > -1):
  23.     for ctr in range(start+1, end):
  24.         split_list[ctr] = split_list[ctr][::-1]
  25.  
  26. print "CGATTGAACATTAAGTCCAATT = desired output"
  27. print "".join(split_list)
Nov 11 '10 #2

P: 32
For this, it is suppose to pull the DNA sequence from the txt file and then the user is asked to enter a pattern to search for (example: tgaa) either in lowercase or uppercase and have it reverse the pattern in the new mutated DNA sequence.
Nov 12 '10 #3

Expert 100+
P: 624
You are reversing the letters between the sequence entered and it's reverse. Is this correct? Note also that your code does the reversing whether or not the sequence and it's reverse are found, which would yield an error for "first", "last", etc. if one of them is not in the file (for the code as originally posted). If you want to do it this way, consider using find() instead of index. Find returns -1 if not found, so you can eliminate
"if pattern in DNA and reverse in DNA:"
and check if "first" and "last" are both greater than -1.
Nov 12 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.