I'm given the following DNA string:
mydna = 'AGUGGCUAUUACUACAUGCCGAAGUUCCUUAAAUUUAACUUACCAGGCUUAACCGGAUGAUGAUUAUUAUUACCUUAAUUUUA'
How can I get the DNA string's complement?
CodePudding user response:
Here's a quick one liner that does the job:
''.join([{'A': 'U', 'U': 'A', 'C': 'G', 'G': 'C'}[i.upper()] for i in mydna])
It iterates through every character in the original string and then replaces the values.
CodePudding user response:
You can use str.maketrans
:
translation = str.maketrans("AUCG", "UAGC")
print(mydna.translate(translation))
This outputs:
UCACCGAUAAUGAUGUACGGCUUCAAGGAAUUUAAAUUGAAUGGUCCGAAUUGGCCUACUACUAAUAAUAAUGGAAUUAAAAU
CodePudding user response:
No fancy stuff:
original_strand = 'AGUGGCUAUUACUACAUGCCGAAGUUCCUUAAAUUUAACUUACCAGGCUUAACCGGAUGAUGAUUAUUAUUACCUUAAUUUUA'
new_strand = ''
for char in original_strand: # for every character in the string...
if char == 'A':
replace_with = 'U'
elif char == 'U':
replace_with = 'A'
elif char == 'C':
replace_with = 'G'
elif char == 'G':
replace_with = 'C'
new_strand = replace_with
print(new_strand)