473,795 Members | 2,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reversing string in python

90 New Member
I intend use string and reverse function to build a simple application in python for DNA (presented by A,G,G, and T) mutation when one of its substring is reversed during the replication process. The reversal happens what are termed inverted pairs. For instance, if the pattern TGAA is later followed byinverted pattern AAGT, the slice of DNA delimited by those patterns could be inverted and reattached. Something's like

TGAACATTAAGT
will be inversed to
TGAATTACAAGT

---------------------------------
The program is simple but I don't know how to manipulate the string to make it be reversed in the way I would like to. Here is my incomplete design:
Expand|Select|Wrap|Line Numbers
  1. DNAsequence = raw_input('Please enter a DNA sequence :')  #First, people have  to enter a DNA sequence (A,C,G,T only).
  2. pattern= raw_input('please enter the pattern :') # Second, people have to enter the pattern (also A,C,G,T only)  this limited to 4 characters.
  3. MutatedDNA ='......'  #this is the output I would like to have, a mutated sequence of DNA
  4.  
Sep 14 '07 #1
16 3594
ilikepython
844 Recognized Expert Contributor
I intend use string and reverse function to build a simple application in python for DNA (presented by A,G,G, and T) mutation when one of its substring is reversed during the replication process. The reversal happens what are termed inverted pairs. For instance, if the pattern TGAA is later followed byinverted pattern AAGT, the slice of DNA delimited by those patterns could be inverted and reattached. Something's like

TGAACATTAAGT
will be inversed to
TGAATTACAAGT

---------------------------------
The program is simple but I don't know how to manipulate the string to make it be reversed in the way I would like to. Here is my incomplete design:
Expand|Select|Wrap|Line Numbers
  1. DNAsequence = raw_input('Please enter a DNA sequence :')  #First people have  to enter a DNA sequence (A,C,G,T only).
  2. pattern= raw_input('please enter the pattern :') # Second people have to enter the pattern (also A,C,G,T only)  
  3. MutatedDNA ='......'  #this is the output I would like to have, a mutated sequence of DNA
  4.  
To reverse:
Expand|Select|Wrap|Line Numbers
  1. >>> s = "CATT"
  2. >>> s[::-1]
  3. 'TTAC'
  4. >>>
  5. >>> ls = list(s)
  6. >>> ls.reverse()
  7. >>> "".join(ls)
  8. 'TTAC'
  9.  
Sep 14 '07 #2
python101
90 New Member
thank you I got the principle, I will try to see how far I can go.

Anyway, what command(s) should I use if there is another letter rather than A,C,G,T used in the first and second input (if there is a error of inputing, there will be a message appear so user can re-input)?
Sep 14 '07 #3
ilikepython
844 Recognized Expert Contributor
thank you I got the principle, I will try to see how far I can go.

Anyway, what command(s) should I use if there is another letter rather than A,C,G,T used in the first and second input (if there is a error of inputing, there will be a message appear so user can re-input)?
Expand|Select|Wrap|Line Numbers
  1. import string
  2. letts = string.lowercase
  3. letts.remove("a")
  4. letts.remove("c")
  5. letts.remove("g")
  6. letts.remove("t")
  7.  
  8. bad = 0
  9. for let in user_input.lower():
  10.     if let in letts:
  11.         bad = 1
  12.  
  13. ... or ...
  14.  
  15. us = user_input.lower()
  16. if us.count("a") + us.count("c") + us.count("g") + us.count("t") < len(us):
  17.     bad = 1
  18. else:
  19.     bad = 0
  20.  
Sep 14 '07 #4
python101
90 New Member
Fished the basic, however, I would like to have something advanced:
- I want to inverse ALL occurrences of the input pattern (if there is more than one) in the DNAsequence. Display the new inversed sequence (other none-inversed in DNA sequence + inversed pattern(s) in proper index as example above, not only the inversed pattern). How can I do so?
Sep 14 '07 #5
ilikepython
844 Recognized Expert Contributor
Fished the basic, however, I would like to have something advanced:
- I want to inverse ALL occurrences of the input pattern (if there is more than one) in the DNAsequence. Display the new inversed sequence (other none-inversed in DNA sequence + inversed pattern(s) in proper index as example above, not only the inversed pattern). How can I do so?
Like this?
Expand|Select|Wrap|Line Numbers
  1. seq = "TCGA"
  2. dna = "TCGAGATCTAGTCATCTAGCTCGATCGAAAGTCTATCGATCGGAT"
  3. print dna.replace(seq, seq[::-1])
  4.  
Sep 15 '07 #6
python101
90 New Member
I appreciate your help. Now I'd like to extend the program, instead of inversing the pattern we enter, we inverse the next pattern after the entered pattern. For example

dna = 'TACAAATCGGAC'
pat = 'AATC'

result will be 'TACAAATCACGG'?
Sep 15 '07 #7
ilikepython
844 Recognized Expert Contributor
I appreciate your help. Now I'd like to extend the program, instead of inversing the pattern we enter, we inverse the next pattern after the entered pattern. For example

dna = 'TACAAATCGGAC'
pat = 'AATC'

result will be 'TACAAATCACGG'?
You mean the last part should be 'CAGG'?
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i = 0):    # Thanks to bvdet for code
  2.     i_list = []
  3.     while 1:
  4.         try:
  5.             i = s.index(item, i)
  6.             i_list.append(i)
  7.             i += 1
  8.         except:
  9.             break
  10.     return i_list
  11.  
  12. dna = 'TACAAATCGGAC'
  13. pat  = 'AATC'
  14.  
  15. for i in indexList(dna, pat):
  16.     nex = dna[i + 4:i + 8]
  17.     dna = dna.replace(nex, nex[::-1])
  18.  
See if that works.
Sep 15 '07 #8
python101
90 New Member
I have just begun learning python in a few days. Your code looked so complicated for me to understand. Can you explain in more details or can you make the code less complicated?

Like the simple code of ilikepython
Expand|Select|Wrap|Line Numbers
  1. >>> s = "CATT"
  2. >>> s[::-1]
  3. 'TTAC'
  4. >>> ls = list(s)
  5. >>> ls.reverse()
  6. >>> "".join(ls)
  7. 'TTAC'
  8.  
it works well. From this source code, can we extend it to achievement my goal?
Sep 15 '07 #9
ilikepython
844 Recognized Expert Contributor
I have just begun learning python in a few days. Your code looked so complicated for me to understand. Can you explain in more details or can you make the code less complicated?

Like the simple code of ilikepython
Expand|Select|Wrap|Line Numbers
  1. >>> s = "CATT"
  2. >>> s[::-1]
  3. 'TTAC'
  4. >>> ls = list(s)
  5. >>> ls.reverse()
  6. >>> "".join(ls)
  7. 'TTAC'
  8.  
it works well. From this source code, can we extend it to achievement my goal?
Well, the code I gave you doesn't quite work right. Sorry. Let's try this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. dna = 'TACAAATCGGAC'
  3. pat  = 'AATC'
  4.  
  5. for i in indexList(dna, pat):
  6.     nex = dna[i + 4:i + 8]  # pattern after; to be reserved
  7.     here = dna[i:i + 4]    # pattern (pat)
  8.     dna = dna.replace(here + nex, here + nex[::-1]) # replace the combination with the last pattern reversed
  9.  
Don't worry about the code in indexList, just know what it does. It returns a list of the indices of the item in the list s. So:
Expand|Select|Wrap|Line Numbers
  1. ls = [1, 2, 2, 4, 5, 2, 4]
  2. indexList(ls, 2) will return [1, 2, 5]
  3. indexList(ls, 4) will return [3, 6]
  4. indexList(ls, 1) will return [0]
  5. indexList(ls, 7) will return []
  6.  
Does that make sense?
Sep 15 '07 #10

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

Similar topics

2
1481
by: Aki Niimura | last post by:
Hello everyone, I need to reverse an iterator in my program. There are many posting to related to this. But most of them are talking about how to expand the language to support such. In fact reversed() built-in function is added in Python 2.4 to do such. However, I need to use Python 2.2.x because of a module I'm using.
8
4764
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
16
2091
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it should except that every traversal thru the string I've gotten it to repeat the "list" again. This is what it looks like: for char in x: mylist.append(char)
1
3313
by: rajkumarbathula | last post by:
Hi Could any one help me out in reversing rows/elements of DataTable or String or DataList by using any simple statement? Thanks
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.