-
-
Save workmad3/3e70f2bfa952821f1a83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Complement | |
def of_dna(sequence) | |
sequence = sequence.upcase | |
sequence_error 'DNA', sequence unless NucleicAcids.dna? sequence | |
sequence.tr NucleicAcids::DNA_NUCLEOTIDES, NucleicAcids::RNA_NUCLEOTIDES | |
end | |
def of_rna(sequence) | |
sequence = sequence.upcase | |
sequence_error 'RNA', sequence unless NucleicAcids.rna? sequence | |
sequence.tr NucleicAcids::RNA_NUCLEOTIDES, NucleicAcids::DNA_NUCLEOTIDES | |
end | |
def sequence_error(label, sequence) | |
fail ArgumentError.new "#{sequence} is not a valid #{label} sequence" | |
end | |
private_class_method :sequence_error | |
extend self | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
module NucleicAcids | |
DNA_NUCLEOTIDES = 'GCTA' | |
RNA_NUCLEOTIDES = 'CGAU' | |
def self.dna?(sequence) | |
subset? DNA_NUCLEOTIDES, sequence.upcase | |
end | |
def self.rna?(sequence) | |
subset? RNA_NUCLEOTIDES, sequence.upcase | |
end | |
def self.subset?(nucleotides, sequence) | |
Set.new(sequence.chars).subset? Set.new(nucleotides.chars) | |
end | |
private_class_method :subset? | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment