473,804 Members | 3,123 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
16 3597
python101
90 New Member
I modified a little bit the source you gave me earlier, it worked quite well (only inversed the pattern we enter, not the next pattern after the pattern we entered). The new source code makes more sense. Thank U very much.
Sep 15 '07 #11
python101
90 New Member
The program seems to reverse only the first next pattern but not all in the sequence.

For example

dna = 'AACCTTGGAATTCATTAACCACGGAATTCATT'
pat ='AACC'
will only reversed to
dna = AACCGGTTAATTCATTAACCACGGAATTCATT'
Sep 16 '07 #12
ilikepython
844 Recognized Expert Contributor
The program seems to reverse only the first next pattern but not all in the sequence.

For example

dna = 'AACCTTGGAATTCATTAACCACGGAATTCATT'
pat ='AACC'
will only reversed to
dna = AACCGGTTAATTCATTAACCACGGAATTCATT'
I'm pretty sure it works:
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.  
  13. def getNew(dna, pat):
  14.     for i in indexList(dna, pat):
  15.         nex = dna[i + 4:i + 8]  # pattern after; to be reserved
  16.         here = dna[i:i + 4]    # pattern (pat)
  17.         dna = dna.replace(here + nex, here + nex[::-1]) # replace the combination with the last pattern reversed
  18.     return dna
  19.  
  20.  
  21. dna = 'AACCTTGGAATTCATTAACCACGGAATTCATT'
  22. pat ='AACC'
  23.  
  24. print "OLD: %s" % dna
  25. dna = getNew(dna, pat)
  26. print "NEW: %s" % dna
  27.  
Sep 16 '07 #13
python101
90 New Member
Thank you very mich, I have a problem when running the source code.

For example,
Expand|Select|Wrap|Line Numbers
  1. dna ='AGGTGGTTAGGTGGTT'
  2. pa='AGGT'
  3.  
  4. #the output is fine
  5. result='AGGTTTGGAGGTTTGG
  6.  
however, if it changes the last pattern of the dna
Expand|Select|Wrap|Line Numbers
  1. dna ='AGGTGGTTAGGTTGGT'
  2. pa='AGGT'
  3.  
  4. #the output is not good
  5. result='AGGTTTGGAGGTTGGT
  6.  
I'm also looking for a code without using while. I want something very basic since I'm just a beginner.
Sep 17 '07 #14
bvdet
2,851 Recognized Expert Moderator Specialist
Thank you very mich, I have a problem when running the source code.

For example,
Expand|Select|Wrap|Line Numbers
  1. dna ='AGGTGGTTAGGTGGTT'
  2. pa='AGGT'
  3.  
  4. #the output is fine
  5. result='AGGTTTGGAGGTTTGG
  6.  
however, if it changes the last pattern of the dna
Expand|Select|Wrap|Line Numbers
  1. dna ='AGGTGGTTAGGTTGGT'
  2. pa='AGGT'
  3.  
  4. #the output is not good
  5. result='AGGTTTGGAGGTTGGT
  6.  
I'm also looking for a code without using while. I want something very basic since I'm just a beginner.
The while and for statements are the two basic loop constructs in Python and are good for a beginner to learn. I made some changes to ilikepython's code:
Expand|Select|Wrap|Line Numbers
  1. # Reverse the sequence(s) in 'dna' following the substring defined by 'pat'def getNew(dna, pat):
  2. def getNew(dna, pat):
  3.     for i in indexList(dna, pat):
  4.         j = len(pat)
  5.         revstr = dna[i + j:i + j*2]
  6.         dna = revstr[::-1].join([dna[:i+j], dna[i+j*2:]])
  7.     return dna
  8.  
You still need function indexList().
Sep 17 '07 #15
python101
90 New Member
How can I print the output out(the mutated DNA)? I got error message.

How can I make the program run without being interrupted (after input the dna and pat, it outputs the result, then it appears the input again,...) when I want it to stop I type 'exit' and 'quit' to make it stop running?
Sep 18 '07 #16
bartonc
6,596 Recognized Expert Expert
How can I print the output out(the mutated DNA)? I got error message.

How can I make the program run without being interrupted (after input the dna and pat, it outputs the result, then it appears the input again,...) when I want it to stop I type 'exit' and 'quit' to make it stop running?
Expand|Select|Wrap|Line Numbers
  1. # Use lots of comments
  2. # to describe your program
  3.  
  4. # put imports at the top
  5. import sys
  6.  
  7. def CheckDNASequence(sequence):
  8.     # just a stub
  9.     return True
  10.  
  11.  
  12. # "encapsulate" using functions
  13. def GetDNASequence():  # use descriptive names
  14.     while 1:  # alway loop
  15.         seq = raw_input("Enter a sequence ('q' to quit): ")
  16.         if seq.lower() == "q":
  17.             return  # break out of the loop, returning None
  18.         if CheckDNASequence(seq):  # break out of the loop
  19.             break
  20.     return seq # good practice to put the valid return here
  21.  
  22. def GetPattern():
  23.     pat = raw_input("Enter a pattern: ")
  24.     return pat
  25.  
  26.  
  27. def main():
  28.     while True:  # always loop
  29.         seq = GetDNASequence()
  30.         if seq is None:
  31.             sys.exit()
  32.         print seq
  33.         pat = GetPattern()
  34.         print pat
  35.  
  36. if __name__ == "__main__":
  37.     main()
Sep 18 '07 #17

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
4765
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
2095
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
3317
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
9708
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
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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...
1
7623
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
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.