473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Easier method to reverse a given pattern?

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
3 2238
dwblas
626 Expert 512MB
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
Fuugie
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
dwblas
626 Expert 512MB
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

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

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
3
by: engsolnom | last post by:
I need to use the built-in method 'reverse', but notice strange behavior. Foe example: print my_list.reverse() doesn't work. new_list = my_list.reverse() doesn't work. my_list.reverse()...
3
by: Apple | last post by:
Hi I am a bit new to python. I was wondering if there is a way to determine whether or not a given string is a member method of a given object: def is_a_method(self, attr_name): 'returns True...
2
by: SomeDude | last post by:
Lo group, I would like to know if it is possible to a (string) replace on existing records based on a given pattern. Let's say I have a table containing the following records (strings):...
1
by: mirandacascade | last post by:
I do not understand how to use the find() method in ElementTree. The file 'sample.xml' is: <?xml version="1.0"?> <SampleRoot> <Header> <Product>FindMystery</Product> </Header>...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
8
by: alexander.stippler | last post by:
Hello, I need on the one hand static polymorphism for efficiency concerns, e.g. template <typename P> class Problem { public: const P &
1
by: ltruett | last post by:
Last week I continued my series of design patterns examples using PHP 5 with the Bridge Pattern, Flyweight Pattern, and Proxy Pattern. Here now is my 20th PHP 5 design pattern example, the...
7
by: cbmeeks | last post by:
Hope I'm using the right terminology. Anyway, say I have a class like: class Animal { public double GetValues() {......} public void FilterBy(string text); {......}
4
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.