reversing string in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    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:
    [code=python]
    DNAsequence = raw_input('Plea se enter a DNA sequence :') #First, people have to enter a DNA sequence (A,C,G,T only).
    pattern= raw_input('plea se enter the pattern :') # Second, people have to enter the pattern (also A,C,G,T only) this limited to 4 characters.
    MutatedDNA ='......' #this is the output I would like to have, a mutated sequence of DNA
    [/code]
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by python101
    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:
    [code=python]
    DNAsequence = raw_input('Plea se enter a DNA sequence :') #First people have to enter a DNA sequence (A,C,G,T only).
    pattern= raw_input('plea se enter the pattern :') # Second people have to enter the pattern (also A,C,G,T only)
    MutatedDNA ='......' #this is the output I would like to have, a mutated sequence of DNA
    [/code]
    To reverse:
    [code=python]
    >>> s = "CATT"
    >>> s[::-1]
    'TTAC'
    >>>
    >>> ls = list(s)
    >>> ls.reverse()
    >>> "".join(ls)
    'TTAC'
    [/code]

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      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)?

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by python101
        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)?
        [code=python]
        import string
        letts = string.lowercas e
        letts.remove("a ")
        letts.remove("c ")
        letts.remove("g ")
        letts.remove("t ")

        bad = 0
        for let in user_input.lowe r():
        if let in letts:
        bad = 1

        ... or ...

        us = user_input.lowe r()
        if us.count("a") + us.count("c") + us.count("g") + us.count("t") < len(us):
        bad = 1
        else:
        bad = 0
        [/code]

        Comment

        • python101
          New Member
          • Sep 2007
          • 90

          #5
          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?

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by python101
            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?
            [code=python]
            seq = "TCGA"
            dna = "TCGAGATCTAGTCA TCTAGCTCGATCGAA AGTCTATCGATCGGA T"
            print dna.replace(seq , seq[::-1])
            [/code]

            Comment

            • python101
              New Member
              • Sep 2007
              • 90

              #7
              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'?

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by python101
                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'?
                [code=python]
                def indexList(s, item, i = 0): # Thanks to bvdet for code
                i_list = []
                while 1:
                try:
                i = s.index(item, i)
                i_list.append(i )
                i += 1
                except:
                break
                return i_list

                dna = 'TACAAATCGGAC'
                pat = 'AATC'

                for i in indexList(dna, pat):
                nex = dna[i + 4:i + 8]
                dna = dna.replace(nex , nex[::-1])
                [/code]
                See if that works.

                Comment

                • python101
                  New Member
                  • Sep 2007
                  • 90

                  #9
                  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
                  [code=python]
                  >>> s = "CATT"
                  >>> s[::-1]
                  'TTAC'
                  >>> ls = list(s)
                  >>> ls.reverse()
                  >>> "".join(ls)
                  'TTAC'
                  [/code]
                  it works well. From this source code, can we extend it to achievement my goal?

                  Comment

                  • ilikepython
                    Recognized Expert Contributor
                    • Feb 2007
                    • 844

                    #10
                    Originally posted by python101
                    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
                    [code=python]
                    >>> s = "CATT"
                    >>> s[::-1]
                    'TTAC'
                    >>> ls = list(s)
                    >>> ls.reverse()
                    >>> "".join(ls)
                    'TTAC'
                    [/code]
                    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:
                    [code=python]

                    dna = 'TACAAATCGGAC'
                    pat = 'AATC'

                    for i in indexList(dna, pat):
                    nex = dna[i + 4:i + 8] # pattern after; to be reserved
                    here = dna[i:i + 4] # pattern (pat)
                    dna = dna.replace(her e + nex, here + nex[::-1]) # replace the combination with the last pattern reversed
                    [/code]
                    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:
                    Code:
                    ls = [1, 2, 2, 4, 5, 2, 4]
                    indexList(ls, 2) will return [1, 2, 5]
                    indexList(ls, 4) will return [3, 6]
                    indexList(ls, 1) will return [0]
                    indexList(ls, 7) will return []
                    Does that make sense?

                    Comment

                    • python101
                      New Member
                      • Sep 2007
                      • 90

                      #11
                      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.

                      Comment

                      • python101
                        New Member
                        • Sep 2007
                        • 90

                        #12
                        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'

                        Comment

                        • ilikepython
                          Recognized Expert Contributor
                          • Feb 2007
                          • 844

                          #13
                          Originally posted by python101
                          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:
                          [code=python]
                          def indexList(s, item, i = 0): # Thanks to bvdet for code
                          i_list = []
                          while 1:
                          try:
                          i = s.index(item, i)
                          i_list.append(i )
                          i += 1
                          except:
                          break
                          return i_list


                          def getNew(dna, pat):
                          for i in indexList(dna, pat):
                          nex = dna[i + 4:i + 8] # pattern after; to be reserved
                          here = dna[i:i + 4] # pattern (pat)
                          dna = dna.replace(her e + nex, here + nex[::-1]) # replace the combination with the last pattern reversed
                          return dna


                          dna = 'AACCTTGGAATTCA TTAACCACGGAATTC ATT'
                          pat ='AACC'

                          print "OLD: %s" % dna
                          dna = getNew(dna, pat)
                          print "NEW: %s" % dna
                          [/code]

                          Comment

                          • python101
                            New Member
                            • Sep 2007
                            • 90

                            #14
                            Thank you very mich, I have a problem when running the source code.

                            For example,
                            [code=python]
                            dna ='AGGTGGTTAGGTGGTT'
                            pa='AGGT'

                            #the output is fine
                            result='AGGTTTGGAGGTTTGG
                            [/code]
                            however, if it changes the last pattern of the dna
                            [code=python]
                            dna ='AGGTGGTTAGGTTGGT'
                            pa='AGGT'

                            #the output is not good
                            result='AGGTTTGGAGGTTGGT
                            [/code]

                            I'm also looking for a code without using while. I want something very basic since I'm just a beginner.

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Originally posted by python101
                              Thank you very mich, I have a problem when running the source code.

                              For example,
                              [code=python]
                              dna ='AGGTGGTTAGGTGGTT'
                              pa='AGGT'

                              #the output is fine
                              result='AGGTTTGGAGGTTTGG
                              [/code]
                              however, if it changes the last pattern of the dna
                              [code=python]
                              dna ='AGGTGGTTAGGTTGGT'
                              pa='AGGT'

                              #the output is not good
                              result='AGGTTTGGAGGTTGGT
                              [/code]

                              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:[code=Python]# Reverse the sequence(s) in 'dna' following the substring defined by 'pat'def getNew(dna, pat):
                              def getNew(dna, pat):
                              for i in indexList(dna, pat):
                              j = len(pat)
                              revstr = dna[i + j:i + j*2]
                              dna = revstr[::-1].join([dna[:i+j], dna[i+j*2:]])
                              return dna
                              [/code]You still need function indexList().

                              Comment

                              Working...