Reputation: 21
Write a function ReverseComplement to solve the Reverse Complement Problem, which is reproduced below. (Hint: use the function reverse that you wrote on the last step as a subroutine.)
Need a little help here! I need to change a given text not only backwards but also making some letters equal to others in order to generate a whole new result:
def reverse(text):
reversed_text = ""
# Note: Indices are starting with zero and the last number in ranges aren't included
for i in range(len(text)):
reversed_text += text[len(text)-1-i]
return reversed_text
text = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
text = text.replace('A', 'T')
text = text.replace('G', 'C')
So my output would print:
CCTCCTTTCTCTCTCTCTTTTTTTTTTCCTCTCTCTCTCTTTTTCCCCCTTCCTCTCTCTTTCTTTTCCT
But the real thing that I need to do whit my function is:
GCTGCTATCAGACACTCTTTTTTTAATCCACACAGAGACATATTGCCCGTTGCAGTCAGAATGAAAAGCT
Not entirely replacing the A for T and G for C.... I have 0 programming experience and I`m stuck! please Help
Upvotes: 1
Views: 93
Reputation: 1856
First, you can reverse the whole string by using:
def reverse(string):
return string[::-1]
or
''.join(reversed("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"))
Then, you'll need to replace the letters
text.replace('A', 'T') will replace all 'A' with 'Ts. So TAT -> TTT. What you need to do is text.replace('A', '1'), text.replace('T', 'A'), text.replace('1', 'T').
example:
text = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
# For swapping A and T
text.replace("A", "1")
text.replace("T", "A")
text.replace("1", "T")
# For swapping G and C
text.replace("G", "1")
text.replace("C", "G")
text.replace("1", "C")
Upvotes: 2
Reputation: 53
In Python, the easiest way to reverse a string would be
text = "ACGT"
reversed_text = text[::-1]
# Now, reversed_text is "TGCA"
Once you've done that, you can replace A with T and C with G (and vice-versa)
# For swapping A and T
reversed_text.replace("A", "1")
reversed_text.replace("T", "A")
reversed_text.replace("1", "T")
# For swapping G and C
reversed_text.replace("G", "1")
reversed_text.replace("C", "G")
reversed_text.replace("1", "C")
Upvotes: 0
Reputation: 97247
Here's one way to do it:
text[::-1].translate(maketrans('ACGT', 'TGCA'))
This uses:
text[::-1]
to reverse the stringmaketrans
to create a translation tabletranslate
to perform the actual translationSnippet for Python 2:
from string import maketrans
text = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
translated = text[::-1].translate(maketrans('ACGT', 'TGCA'))
print(translated)
Snippet for Python 3:
text = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
translated = text[::-1].translate(str.maketrans('ACGT', 'TGCA'))
print(translated)
Upvotes: 4