Easier method to reverse a given pattern?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuugie
    New Member
    • Sep 2010
    • 32

    Easier method to reverse a given pattern?

    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: CGATTGAATTACAAG TCCAATT
    Enter the pattern: TGAA
    Mutated DNA sequence: CGATTGAACATTAAG TCCAATT

    ***DNA.txt reads: "CGATTGAATTACAA GTCCAATT" ***
    Code:
    source = file('DNA.txt')
    DNA = source.read()
    print DNA
    
    while not found:
        pattern = raw_input('Enter a pattern: ')
        pattern = pattern.upper()
        reverse = pattern[::-1]
        if pattern in DNA and reverse in DNA:
            found = True
    
    # print pattern, reverse
    first = DNA.index(pattern) + len(pattern) # find the end of the pattern index in our DNA string
    last = DNA.index(reverse)                 # find the beginning index of reverse pattern in DNA string
    beginning = DNA[:first]                   # beginning is the string of DNA including the pattern
    middle = DNA[first:last]                  # middle is the string between the pattern and the reverse of the pattern
    middle = middle[::-1]                     # reverse the order of the middle characters (this is the mutated portion)
    ending = DNA[last:]                       # ending is the DNA string from the start of the pattern reverse to end
    
    mutated = beginning + middle + ending     #join the DNA pieces with mutated middle together
    print mutated
    
    source.close()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.
    Code:
    original = "CGATTGAATTACAAGTCCAATT"
    enter_pattern = "TGAA"
    reverse = enter_pattern[::-1]
    print reverse
    divisor = len(enter_pattern)
    
    ## break into sections of length len(pattern)
    split_list = []
    junk_list = []
    for ctr, ch in enumerate(original):
        if (ctr) and (ctr%divisor == 0):
            split_list.append("".join(junk_list))
            junk_list = []
        junk_list.append(ch)
    split_list.append("".join(junk_list))
    print split_list
    
    ## find pattern and reverse next sequence unless this
    ## is the final sequence
    start = split_list.index(enter_pattern)
    end = split_list.index(reverse)
    if (start > -1) and (end > -1):
        for ctr in range(start+1, end):
            split_list[ctr] = split_list[ctr][::-1]
    
    print "CGATTGAACATTAAGTCCAATT = desired output"
    print "".join(split_list)

    Comment

    • Fuugie
      New Member
      • Sep 2010
      • 32

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

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

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

        Comment

        Working...