Suppose I've selected a run of DNA on-screen and I want to know the base content (the amounts of G, C, T, and A).
text = getSelection().toString(); // get the data as a string text = text.toUpperCase(); // optionally convert it to upper case bases = new Object; // create a place to store the base counts bases.G = bases.C = bases.T = bases.A = 0; // initialize // now loop over the string contents: for (var i = 0; i < text.length; i++) bases[ text[i] ]++; // bump the count for that base
// format the data for viewing
msg = "G: " + bases.G/text.length + "\n";
msg += "C: " + bases.C/text.length + "\n";
msg += "A: " + bases.A/text.length + "\n";
msg += "T: " + bases.T/text.length + "\n";
msg += "GC Content: " + (bases.G + bases.C)/text.length;
// view it:
alert( msg );
If I run this script against a web page where I've highlighted some DNA text, I get:
Bookmarklets of this sort have proven to be a major productivity boon for me in various situations as I cruise the web. When I see data I want to analyze, I don't have to copy and paste it to Excel (or whatever). With a bookmarklet, I can analyze it instantly, sur la vitre.