User:Michael S. Serfas/Sandbox

From OpenWetWare

(Difference between revisions)
Jump to: navigation, search
(Basic dump: what's in this version of Mediawiki? (e.g. use of legacy Javascript globals, no mw. object))
(Sequence import and trivial functions (a piece of DNAScript) - test - not sure how to break up the functions if imported here (css missing))
Line 2: Line 2:
<script>
<script>
//[CDATA[
//[CDATA[
-
function explore(something, level) {
+
 
-
     if (level > 0) {
+
function ntsequence(id) {
-
         document.write(level+":"+something);
+
     /* Class prototype for sequence elements made from (presently) "branchseqdata" and "rootseqdata" textarea values */
-
         document.write("<br />");
+
 
-
         if (typeof something == "object") {
+
    this.id = id;
-
             for (i in something) {
+
 
-
                 explore(something[i], level - 1);
+
    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 = ['<div id="translateoutput">'],
 +
             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 && 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];
 +
                }
             }
             }
         }
         }
-
    } else {
+
        this.cleanup();
-
         document.write("...");
+
        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('</div><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
     }
     }
}
}
-
explore(window, 10);
+
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>
</script>
 +
</head>
 +
<body>
 +
 +
<p><div name="branchseqdata-fasta" id="branchseqdata-fasta"></div></p>
 +
 +
<p><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></p>
 +
 +
<p>Sequence type:&nbsp;<input type="radio" name="seqtype" value="DNA" id="DNA" checked="checked" onclick="rnadna('DNA');">DNA&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="seqtype" value="RNA" id="RNA" onclick="rnadna('RNA');">RNA&nbsp;&nbsp;&nbsp;&nbsp;<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></p>
 +
<p><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>
 +
</div><div name="ss" style="display:none;" id="ss-1seq">
 +
<h3>Translation settings</h3>
 +
 +
<p><em>This function accepts degenerate nucleotide codes.  Positions which may contain one of several amino acids list all possibilities in brackets.</p>
 +
 +
<p>Some alternative genetic code options are listed below.</em></p>
 +
 +
<p><button class="outputbutton" id="test" name="translate" onclick="//lastfunction.innerHTML='translate'; var seq = new ntsequence('branchseqdata'); seq.translate();">Translate</button> frame <input type="radio" name="frame" value="1" id="frame1" checked="checked" onclick="var seq = new ntsequence('branchseqdata'); seq.translate();">1&nbsp;&nbsp;&nbsp;&nbsp;</input><input type="radio" name="frame" value="2" id="frame2" onclick="var seq = new ntsequence('branchseqdata'); seq.translate();">2&nbsp;&nbsp;&nbsp;&nbsp;</input>
 +
<input type="radio" name="frame" value="3" id="frame3" onclick="var seq = new ntsequence('branchseqdata'); seq.translate();">3&nbsp;&nbsp;&nbsp;&nbsp;</input>&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="SeC" id="SeC" value="OFF" onclick="mitoW.checked = false;">UGA = Selenocysteine (U)</input>&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="PyL" id="PyL" value="OFF">UAG = Pyrrolysine (O)</input></p>
 +
 +
<p><a href="http://link.springer.com/article/10.1007%2FBF01936921">Mitochondrial codes:</a> <input type="checkbox" name="mitoW" id="mitoW" value="OFF" onclick="SeC.checked = false;">UGA = W</input>&nbsp;&nbsp;<input type="checkbox" name="mitoM" id="mitoM" value="OFF">AUA = M</input>&nbsp;&nbsp;<input type="checkbox" name="mitoS" id="mitoS" value="OFF" onclick="mitoX.checked = false;">AGR = S</input>&nbsp;&nbsp;<input type="checkbox" name="mitoX" id="mitoX" value="OFF" onclick="mitoS.checked = false;">AGR = *</input>&nbsp;&nbsp;<input type="checkbox" name="mitoN" id="mitoN" value="OFF">AAA = N</input>&nbsp;&nbsp;<input type="checkbox" name="mitoT" id="mitoT" value="OFF">CUN = T</input>&nbsp;&nbsp;<input type="checkbox" name="mitoY" id="mitoY" value="OFF">UAA = Y</input>&nbsp;&nbsp;</p>
 +
</body>
</html>
</html>

Revision as of 19:54, 24 July 2013

Sequence type: DNA    RNA    

the above sequence: gi from EFetch (requires a very simple JSONP server at

Personal tools