Skip to content

Instantly share code, notes, and snippets.

@workmad3
Created March 20, 2015 11:44
Show Gist options
  • Save workmad3/3e70f2bfa952821f1a83 to your computer and use it in GitHub Desktop.
Save workmad3/3e70f2bfa952821f1a83 to your computer and use it in GitHub Desktop.
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
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