(python) Write a program in python that computes the reverse complement of a DNA
ID: 3689103 • Letter: #
Question
(python)
Write a program in python that computes the reverse complement of a DNA sequence. In other words, your program should convert all A's to T's, C's to G's, G's to C's, and T's to A's and reverses the sequence. The input is one sequence in FASTA format in a file called 'dna.txt'. For example, if the file contains:
>human
ACCGT
Then the output of the program should be 'ACGGT'. Your program should work for any sequence and not just the one in the example.
This is what I have so far but it does not work:
f = open ("dna.txt")
mylist = f.read().splitlines()
for ch in reversed(mylist):
if ch == 'A':
mylist=mylist+'T'
elif ch == 'G':
mylist=mylist+'C'
elif ch == 'C':
mylist=mylist+'G'
elif ch =='T':
mylist=mylist+'A'
print(mylist[1])
Explanation / Answer
f = open ("dna.txt")
mylist = f.read().splitlines()
ans = ''
for ch in reversed(mylist[1]):
if ch == 'A':
ans=ans+'T'
elif ch == 'G':
ans=ans+'C'
elif ch == 'C':
ans=ans+'G'
elif ch =='T':
ans=ans+'A'
print(ans)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.