User:Michael S. Serfas/Sandbox
<html> <script> //<![CDATA[
function ntsequence(id) {
/* Class prototype for sequence elements made from (presently) "branchseqdata" and "rootseqdata" textarea values */
this.id = id;
this.set = function(data) {
this.elem.value = data || "";
}
this.elem = document.getElementById(id);
this.scramble = function() {
/* Create a scrambled-nucleotide control for various measurements */
var pull, newarray = [],
seqarray = this.elem.value.split("");
while (seqarray[1]) {
pull = seqarray.splice(Math.floor(seqarray.length * Math.random()), 1);
newarray.push(pull);
}
newarray.push(seqarray[0]);
this.elem.value = newarray.join();
seqarray = null;
newarray = null;
}
this.reverse = function() {
var seqarray = this.elem.value.split("");
seqarray = seqarray.reverse();
this.elem.value = seqarray.join();
seqarray = null;
}
this.complement = function() {
// Nucleotide code opposites
// (meant to improve this structure...) var complement = {a: "T", c: "G", g: "C", t: "A", u: "A", r: "Y", y: "R", k: "M", m: "K", b: "V", v: "B", d: "H", h: "D", w: "W", s: "S"},
this.toDNA(); //temporarily cast as DNA for consistent handling
this.elem.value = this.elem.value.toLowerCase(); //this function uses uppercase to denote already replaced nts
this.elem.value = this.elem.value.replace(/a/g, "T");
this.elem.value = this.elem.value.replace(/t/g, "A");
this.elem.value = this.elem.value.replace(/c/g, "G");
this.elem.value = this.elem.value.replace(/g/g, "C");
this.elem.value = this.elem.value.replace(/y/g, "R");
this.elem.value = this.elem.value.replace(/r/g, "Y");
this.elem.value = this.elem.value.replace(/k/g, "M");
this.elem.value = this.elem.value.replace(/m/g, "K");
this.elem.value = this.elem.value.replace(/b/g, "V");
this.elem.value = this.elem.value.replace(/v/g, "B");
this.elem.value = this.elem.value.replace(/d/g, "H");
this.elem.value = this.elem.value.replace(/h/g, "D");
this.elem.value = this.elem.value.toLowerCase(); //back to lowercase
rnadna(); //recover original RNA/DNA status for all sequences from the checkbox
}
this.cleanup = function() {
var fasta = document.getElementById(this.id + "-fasta");
fasta.innerHTML = this.elem.value.match(/>.*\n/) || "";
this.elem.value = this.elem.value.replace(/>.*\n/, "");
this.elem.value = this.elem.value.toLowerCase(); //all one case for these things
this.elem.value = this.elem.value.replace(/x/g, "n"); // occasionally x's used to be encountered for N's, though this may not happen nowadays
this.elem.value = this.elem.value.replace(/[^acgtunrykmbdhvws\.\-]/g, ""); //remove non-genome letters. Both t,u allowed here
rnadna(); //restoring proper RNA/DNA status for all sequences as part of every cleanup (many bugs start from t/u mixups!)
}
this.toDNA = function() {
this.elem.value = this.elem.value.replace(/u/g, "t");
}
this.toRNA = function() {
this.elem.value = this.elem.value.replace(/t/g, "u");
}
this.translate = function() {
// The degenerate nucleotide codes decomposed to arrays
var outarray = ['
seqarray,
c,
x1,
x2,
x3,
aareturn,
aareturnhash,
aareturnarr,
codon,
i,
elem,
b1,
b2,
b3,
nt,
output = document.getElementById("output"),
SeC = document.getElementById("SeC"),
PyL = document.getElementById("PyL"),
degen = setdegen(),
altcode = {SeC: ["uga", "U"], PyL: ["uag", "O"], mitoW: ["uga", "W"], mitoM: ["aua", "M"], mitoS: ["agr", "S"], mitoX: ["agr", "*"], mitoN: ["aaa", "N"], mitoT: ["cun", "T"], mitoY: ["uaa", "Y"]},
tcodon = {uuu: "F", uuc: "F", uug: "L", uua: "L", cuu: "L", cua: "L", cuc: "L", cug: "L", auu: "I", auc: "I", aua: "I", aug: "M", guu: "V", guc: "V", gua: "V", gug: "V", ucu: "S", ucc: "S", uca: "S", ucg: "S", ccu: "P", ccc: "P", cca: "P", ccg: "P", acu: "T", acc: "T", aca: "T", acg: "T", gcu: "A", gcc: "A", gca: "A", gcg: "A", uau: "Y", uac: "Y", uaa: "*", uag: "*", cau: "H", cac: "H", caa: "Q", cag: "Q", aau: "N", aac: "N", aaa: "K", aag: "K", gau: "D", gac: "D", gaa: "E", gag: "E", ugu: "C", ugc: "C", uga: "*", ugg: "W", cgu: "R", cgc: "R", cga: "R", cgg: "R", agu: "S", agc: "S", aga: "R", agg: "R", ggu: "G", ggc: "G", gga: "G", ggg: "G"};
output.innerHTML = ""; //retake the memory of what may be a very bulky output from dotplot, etc.
for (i in altcode) {
elem = document.getElementById(i);
if (elem) {
if (elem.checked) {
for (j in degen[altcode[i][0].slice(2,3)]) {
tcodon[altcode[i][0].slice(0,2)+degen[altcode[i][0].slice(2,3)][j]] = altcode[i][1];
}
}
}
}
this.cleanup();
rnadna("RNA"); //all the rules here are written to translate RNA, and the checkbox is updated
// not needed to auto-reference sequence, output id's by getElementById?
seqarray = this.elem.value.split("");
seqarray = seqarray.reverse(); // .pop will pull out the first nucleotide
c = this.elem.value.length / 3;
if (!(frame3.checked || frame2.checked)) {
frame1.checked = true;
}
if (frame3.checked) {
seqarray.pop();
seqarray.pop();
c = c - 2 / 3;
} else {
if (frame2.checked) {
seqarray.pop();
c = c - 1 / 3;
}
}
if (c > 0) {
do {
c -= 1;
aareturnhash = {};
aareturnarr = [];
nt = [];
for (i = 1; i < 4; i++) {
nt.push(seqarray.pop());
}
if (tcodon[nt.join("")]) {
outarray.push(tcodon[nt.join("")]);
} else {
b1 = degen[nt[0]];
b2 = degen[nt[1]];
b3 = degen[nt[2]];
if (b3) {
for (x1 = 0; b1[x1]; x1++) {
for (x2 = 0; b2[x2]; x2++) {
for (x3 = 0; b3[x3]; x3++) {
codon = b1[x1] + b2[x2] + b3[x3];
aareturn = tcodon[codon.toLowerCase()];
if (!aareturnhash[aareturn]) {
aareturnhash[aareturn] = true;
aareturnarr.push(aareturn);
}
}
}
}
}
tcodon[nt.join("")] = "[" + aareturnarr.join("") + "]";
if (aareturnarr[1]) {
outarray.push("[");
}
outarray.push(aareturnarr.join("") || "");
if (aareturnarr[1]) {
outarray.push("]");
}
}
} while (c > 0);
}
outarray.push('<button onclick="newtranslateproteinwindow(output.innerHTML);">New window with this protein</button>');
output.innerHTML = outarray.join("");
outarray = null;
rnadna(); //restore proper RNA/DNA status display per user preference
window.setTimeout(saveargs, 0); // extra fork of a process to display all the args as a URL
}
}
function rnadna(force) {
// this isn't very efficient nor object oriented, but to help limit bugs EVERY sequence is set to RNA/DNA according to the checkbox EVERY time it is read
var seq = new ntsequence("branchseqdata"),
seq2 = new ntsequence("rootseqdata"),
DNA = document.getElementById("DNA"),
RNA = document.getElementById("RNA");
if (force === "RNA") {
RNA.checked = true;
}
if (force === "DNA") {
DNA.checked = true;
}
if (DNA.checked) {
seq.toDNA();
seq2.toDNA();
}
if (RNA.checked) {
seq.toRNA();
seq2.toRNA();
}
}
function importbranchseq() {
var myserver = document.getElementById("branchseqserver").value,
gi = document.getElementById("branchseqgi").value,
yyy = document.createElement("script");
yyy.id = "jsonp"
yyy.type = "text/javascript";
yyy.onload = function(){document.getElementsByTagName("head")[0].removeChild(yyy)};
yyy.src = (myserver + "ncbi_to_jsonp/?db=nuccore&rettype=fasta&id="+gi);
document.getElementsByTagName("head")[0].appendChild(yyy)
}
function callback(dict,seqname) {
var seq = new ntsequence(seqname || "branchseqdata"); seq.set(dict.data); seq.cleanup();
} //]]> </script> </head> <body>
<textarea type="text" name="branchseqdata" class="sequence" cols="100%" rows="20" id="branchseqdata" autofocus spellcheck="false" onblur="var seq = new ntsequence('branchseqdata'); seq.cleanup();"></textarea>
Sequence type: <input type="radio" name="seqtype" value="DNA" id="DNA" checked="checked" onclick="rnadna('DNA');">DNA <input type="radio" name="seqtype" value="RNA" id="RNA" onclick="rnadna('RNA');">RNA <button id="button" name="reverse complement" onclick="var seq = new ntsequence('branchseqdata'); seq.cleanup; seq.reverse(); seq.complement();">Reverse complement</button><button id="button" name="scramble" onclick="var seq = new ntsequence('branchseqdata'); seq.cleanup(); seq.scramble();">Scramble this sequence</button>
<button onclick="importbranchseq();">Import</button> the above sequence: gi <input type="text" id="branchseqgi" size="15" value=""></input> from <a href="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi">EFetch</a> (requires a very simple JSONP server at <input type="text" size="30" value="http://msserfas.appspot.com/" id="branchseqserver"></input>