471,344 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

reversing string in python

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 3354
ilikepython
844 Expert 512MB
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
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 Expert 512MB
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
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 Expert 512MB
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
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 Expert 512MB
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
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 Expert 512MB
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
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
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 Expert 512MB
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
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 Expert Mod 2GB
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
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 Expert 4TB
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

Post your reply

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

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.