USE RUBY PROGRAMMING LANGUAGE The reverse-complement of a DNA string is a new st
ID: 3585638 • Letter: U
Question
USE RUBY PROGRAMMING LANGUAGE
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 = DNA.new('ATTGCC')
=> ATTGCC
>> dna2 = dna1.reverse_complement
=> GGCAAT
>> dna2
=> GGCAAT
>> dna1
=> ATTGCC
>> dna2.reverse_complement
=> ATTGCC
>> dna1.reverse_complement.reverse_complement == dna1
=> true
Explanation / Answer
#!/usr/bin/ruby
class DNA
def initialize(seq)
@sequence = seq
end
def reverse_complement
rC = ''
@sequence.split('').each do |i|
if i == 'A'
rC += 'T'
else if i == 'C'
rC += 'G'
else if i == 'G'
rC += 'C'
else
rC += 'A'
end
end
return rC
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.