In: Computer Science
A DNA strand is represented by a string of the characters A, C, G, and T, each of which represents a nucleotide. Each nucleotide has its complement as indicated by this Ruby hash:
NUCLEOTIDE_COMPLEMENT = { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C'
}
The reverse-complement of a DNA string is a new string in which each nucleotide is replaced by its complement and the string is reversed. Reverse-complements are important in bioinformatics since a strand of DNA can be read in forward direction or, equivalently, its complementary strand can be read in its forward direction, which is reverse of the original string’s direction. Add the reverse_complement method to your DNA class. In response to reverse_complement, it returns a new DNA instance representing its reverse-complement. Note also that a == b if and only if DNA instances a and b represent the same DNA strand.
>> dna1 =
=> ATTGCC
>> dna2 =
=> GGCAAT
>> dna2
=> GGCAAT
>> dna1
=> ATTGCC
>> dna2.reverse_complement => ATTGCC
DNA.new('ATTGCC') dna1.reverse_complement
>> dna1.reverse_complement.reverse_complement == dna1 => true
Python code for above problem
class DNA:
def __init__(self,dna):
self.dna=dna
def reverse_complement(self):
new_dna=""
for i in
range(len(self.dna)):
ch=self.dna[i]
if(ch=='A'):
new_dna+="T"
elif(ch=='T'):
new_dna+="A"
elif(ch=='G'):
new_dna+="C"
else:
new_dna+="G"
return DNA(new_dna)
def __str__(self):
return self.dna
dna1=DNA("ATTGCC")
print(dna1.reverse_complement().__str__()=="TAACGG") #
prints True
print(dna1.reverse_complement().reverse_complement().__str__()=="ATTGCC")
# prints True
Mention in comments if any mistakes or errors are found. Thank you.