Created
January 14, 2014 21:49
-
-
Save drueck/8426483 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
var DNA_NUCLEOTIDES = ['A', 'T', 'C', 'G']; | |
var ALL_NUCLEOTIDES = DNA_NUCLEOTIDES.concat('U'); | |
function initialCounts() { | |
return DNA_NUCLEOTIDES.reduce(function(counts, nucleotide) { | |
counts[nucleotide] = 0; | |
return counts; | |
}, {}); | |
} | |
function countNucleotides(sequence) { | |
return sequence.split("").reduce(function(nucleotideCounts, nucleotide){ | |
if (nucleotideCounts[nucleotide] >= 0) { | |
nucleotideCounts[nucleotide]++; | |
} | |
return nucleotideCounts; | |
}, initialCounts()); | |
} | |
function validateNucleotide(nucleotide) { | |
if (ALL_NUCLEOTIDES.indexOf(nucleotide) === -1) { | |
throw new Error("Invalid Nucleotide"); | |
} | |
} | |
function DNA(sequence) { | |
this.nucleotideCounts = countNucleotides(sequence); | |
} | |
DNA.prototype.count = function(nucleotide) { | |
validateNucleotide(nucleotide); | |
return this.nucleotideCounts[nucleotide] || 0; | |
}; | |
module.exports = DNA; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment