Python str mapping permutation with some limitations and special conditions using recursion

Viewed 109

Given a string as input I need to change the a's to t's, c's to g's, u's to a's and g's to c's [ a-t, c-g, g-c, u-a ]. And another special condition is that there can be at most of two instances where u's convert to g's and/or g's convert to t's [ u-g, g-t].

As as example -

input : 
    auugca
output : 
    taacgt
    tgacgt
    taatgt
    tggcgt
    tgatgt
    tagtgt

In the output,

  1. No special changes
  2. One special change 2nd character u to g
  3. One special change 3rd character u to g
  4. One special change 4th character g to t
  5. Two special changes 2nd and 3rd character
  6. Two special changes 2nd and 4th character
  7. Two special changes 3rd and 4th character

I thought of it like this,

  • start from the beginning of the string
  • convert a-t and c-g.
  • If u or g is found, check how many specials have been previously encountered, if less than two, then treat as special, increment previously encountered counter, create two new searches recursively from the next location of the string one with special considered and other not

The code that I came up with is(in Python) -

 # mirna is the string to be converted
 # wobblecount is the number of specials coneverted
 # location is from which location of the string the convertion will start
 # compliment is the end string to be created


 def createonewobble(mirna, location, wobblecount, compliment):

    for counter in range(location, len(mirna)):
        if (mirna[counter] == 'A' or mirna[counter] == 'a'):
            compliment = compliment + "t"
        elif (mirna[counter] == 'C' or mirna[counter] == 'c'):
            compliment = compliment + "g"
        elif ((mirna[counter] == 'U' or mirna[counter] == 'u') and (wobblecount < 2)):
            compliment = compliment + "g"
            createonewobble(mirna, counter+1 , wobblecount+1, compliment)
            compliment = compliment + "a"
            createonewobble(mirna, counter+1 , wobblecount, compliment)
        elif ((mirna[counter] == 'G' or mirna[counter] == 'g') and (wobblecount < 2)):
            compliment = compliment + "t"
            createonewobble(mirna, counter+1 , wobblecount+1, compliment)
            compliment = compliment + "a"
            createonewobble(mirna, counter+1 , wobblecount, compliment)
        elif ((mirna[counter] == 'U' or mirna[counter] == 'u') and (wobblecount == 2)):
            compliment = compliment + "a"
        elif ((mirna[counter] == 'G' or mirna[counter] == 'g') and (wobblecount == 2)):
            compliment = compliment + "c"

    print compliment


mirna = "auugca"
createonewobble(mirna, 0, 0, "")

Output

tggcgt
tggatgt
tggatagt
tggatagt
tggatgt
tggatagt
tggatagt
tgagtgt
tgagtagt
tgagtagt
tgagatgt
tgagatagt
tgagatagt
tgagatgt
tgagatagt
tgagatagt
tgagtgt
tgagtagt
tgagtagt
tgagatgt
tgagatagt
tgagatagt
tgagatgt
tgagatagt
tgagatagt

This is giving me 25 outputs, with not a single correct one and also some are of length a lot more than the size of the string. Where am I going wrong ?

1 Answers
Related