Reputation:
I am trying to make a basic program which translates a string ATGTACATGGGCATAGCCATATA to its RNA sequence, which is UACAUGUACCCGUAUCGGUAUAU.
But instead the output is: CCCCCUUUUUUUUUUUUUUUUUUUUUUU
But it's not quite working. I think the problem is it goes through the entire string with each letter individually.
I am very new to bioinformatics programming so any suggestions would be welcomed.
DNA = "ATGTACATGGGCATAGCCATATA"
dna_length = len(DNA)
print("DNA: " + DNA)
print()
print("Length of DNA in base pairs: "+ str(dna_length))
RNA = []
for char in DNA:
if char == "G":
RNA.append("C")
for line in DNA:
if char == "C":
RNA.append("G")
for line in DNA:
if char == "A":
RNA.append("U")
for line in DNA:
if char == "T":
RNA.append("A")
print("".join(RNA))
Upvotes: 1
Views: 63
Reputation: 250921
The problem is that you're looping over the same list four times and also modifying the changes that were made in the previous loops. So, use a single loop with multiple if-else conditions:
RNA = []
for char in DNA:
if char == "G":
RNA.append("C")
elif char == "C":
RNA.append("G")
elif char == "A":
RNA.append("U")
elif char == "T":
RNA.append("A")
print("".join(RNA))
Best solution will be to use str.translate
:
>>> from string import maketrans
>>> s = "ATGTACATGGGCATAGCCATATA"
>>> tab = maketrans('GCAT', 'CGUA')
>>> s.translate(tab)
'UACAUGUACCCGUAUCGGUAUAU'
In Python 3 we can do it without any imports:
>>> s = "ATGTACATGGGCATAGCCATATA"
>>> s.translate({ord(k): v for k, v in zip('GCAT', 'CGUA')})
'UACAUGUACCCGUAUCGGUAUAU'
Upvotes: 3
Reputation: 133899
There is str.maketrans
which makes a translation table from 1 set of characters to another, and str.translate
which applies such mapping to a string; this should be the fastest way to do this, thus
>>> DNA_2_RNA = str.maketrans('CGAT', 'GCUA')
>>> DNA = 'ATGTACATGGGCATAGCCATATA'
>>> RNA = DNA.translate(DNA_2_RNA)
>>> RNA
'UACAUGUACCCGUAUCGGUAUAU'
Upvotes: 2
Reputation: 1090
Corrected Code:
DNA = "ATGTACATGGGCATAGCCATATA"
dna_length = len(DNA)
print("DNA: " + DNA)
print()
print("Length of DNA in base pairs: "+ str(dna_length))
RNA = []
for char in DNA:
if char == "G":
RNA.append("C")
elif char == "C":
RNA.append("G")
elif char == "A":
RNA.append("U")
elif char == "T":
RNA.append("A")
print("".join(RNA))
Result:
DNA: ATGTACATGGGCATAGCCATATA
()
Length of DNA in base pairs: 23
UACAUGUACCCGUAUCGGUAUAU
Upvotes: 0
Reputation: 117856
I would use a dict
to perform your substitutions, then use a generator expression within join
to perform your translation.
>>> RNA = {'G':'C', 'C':'G', 'A':'U', 'T':'A'}
>>> DNA = 'ATGTACATGGGCATAGCCATATA'
>>> translated = ''.join(RNA[i] for i in DNA)
>>> translated
'UACAUGUACCCGUAUCGGUAUAU'
Upvotes: 3