2

This is what I have so far, it is not replacing the C with a G for some reason. Why might this be?

DNASeq=raw_input("Enter the DNA sequence here: ")
DNASeq=DNASeq.upper().replace(" ","")

reverse=DNASeq[::-1]

print reverse.replace('A','U').replace('T','A').replace('C','G').replace('G','C')
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Danny C
  • 31
  • 2
  • 1
    You're replacing the C with a G, and then replacing (all) Gs with a C. What do you expect? There'll be lots of C in your output, but no G. –  Jun 23 '16 at 02:38

3 Answers3

3

The problem is that you replace C with G then G with C. One simple way to prevent you going from one to the other would be to replace C with g, so it wouldn't then go back to C, then uppercase the result:

gattaca="GATTACA"
rev = gattaca[::-1]
print rev.replace('A','u').replace('T','a').replace('C','g').replace('G','c').upper()

This correctly outputs UGUAAUC instead of UCUAAUC as your example did.

UPDATE

The more Pythonic way, though, and avoiding the case-based hack, and being more efficient as the string doesn't need to be scanned five times, and being more obvious as to the purpose, would be:

from string import maketrans
transtab = maketrans("ATCG", "UAGC")
print rev.translate(transtab)
Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 1
    Yes, this should have been obvious to me from the beginning! It is also much faster than my dictionary-based method, on python 3.5 http://stackoverflow.com/questions/34287893/why-is-str-translate-faster-in-python-3-5-compared-to-python-3-4 – juanpa.arrivillaga Jun 24 '16 at 03:33
2

As pointed out already, the problem is that your are replacing ALL Cs with Gs first. I wanted to throw in this approach because I think it would be the most efficient:

>>> complement = {'A':'U', 'G':'C', 'C':'G','T':'A'}
>>> seq = "GATTACA"
>>> "".join(complement[c] for c in seq)[::-1]
'UGUAAUC'
>>>
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
0

You have done almost correct code. but here one thing you need to understand is First you replace C with G and again you replace G with C.

.replace('C','G').replace('G','C')

just remove .replace('G','C') from your code and everything works fine.

here is the correct code:

DNASeq=raw_input("Enter the DNA sequence here: ")
DNASeq=DNASeq.upper().replace(" ","")

reverse=DNASeq[::-1]

print reverse.replace('A','U').replace('T','A').replace('C','G').replace('G','C')
Rajiv Sharma
  • 6,746
  • 1
  • 52
  • 54