-4

So I understand how to use str.replace() to replace single letters in a string, and I also know how to use the following replace_all function:

def replace_all(text, dic):
for i, j in dic.iteritems():
    text = text.replace(i,j)
return text

But I am trying to replace letters with each other. For example replace each A with T and each T with A, each C with G and each G with C, but I end up getting a string composed of only two letters, either A and G or C and T, for example, and I know the output should be composed of four letters. Here is the code I have tried (I'd rather avoid built in functions):

d={'A': 'T', 'C': 'G', 'A': 'T', 'G': 'C'}
DNA_String = open('rosalind_rna.txt', 'r')
DNA_String = DNA_String.read()


reverse = str(DNA_String[::-1])


def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i,j)
    return text


complement = replace_all(reverse, d)
print complement 

I also tried using:

complement = str.replace(reverse, 'A', 'T')
complement = str.replace(reverse, 'T', 'A')
complement = str.replace(reverse, 'G', 'C')
complement = str.replace(reverse, 'C', 'G')

But I end up getting a string that is four times as long as it should be.

I've also tried:

complement = str.replace(reverse, 'A', 'T').replace(reverse, 'T', 'A').replace(reverse, 'G', 'C')str.replace(reverse, 'C', 'G')

But I get an error message that an integer input is needed.

0

3 Answers 3

1

You can map each letter to another letter.

>>> M = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}
>>> STR = 'CGAATT'
>>> S = "".join([M.get(c,c) for c in STR])
>>> S
'GCTTAA'
Sign up to request clarification or add additional context in comments.

Comments

0

You should probably use str.translate for this. Use string.maketrans to create an according transition table.

>>> import string
>>> d ={'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
>>> s = "ACTG"
>>> _from, to = map(lambda t: ''.join(t), zip(*d.items()))
>>> t = string.maketrans(_from, to)
>>> s.translate(t)
'TGAC'

By the way, the error you get with this line

complement = str.replace(reverse, 'A', 'T').replace(reverse, 'T', 'A')...

is that you are explicitly passing the self keyword when it is passed implicitly. Doing str.replace(reverse, 'A', 'T') is equivalent to reverse.replace('A', 'T'). Accordingly, when you do str.replace(...).replace(reverse, 'T', 'A'), this is equivalent to str.replace(str.replace(...), reverse, 'T', 'A'), i.e. the result of the first replace is inserted as self in the other replace, and the other parameters are shifted and the 'A' is interpreted as the count parameter, which has to be an int.

Comments

-1

I think this is happening because you're replacing all the As with Ts and then replacing all those Ts (as well as those in the original string) with As. Try replacing with lower-case letters and then converting the whole string with upper():

dic = {'A': 't', 'T': 'a', 'C': 'g', 'G': 'c'}
text = 'GATTCCACCGT'
for i, j in dic.iteritems():
            text = text.replace(i,j)
text = text.upper()

gives:

'CTAAGGTGGCA'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.