Team:TUDelft/js/targetfinder

function find(params) {

   var fasta = params.DNA.value.trim();
   var width = 24;
   var DNA = fasta2DNA(fasta);
   DNA = DNA.toUpperCase();
   if (!correctDNA(DNA)) {
       return error("The input is not a correct DNA string and/or fasta format.\n It might contain N's or U's.\n");
   } else {
       console.log("Fasta parsed. Sequence alphabet correct.");
   }
   var grouped = grouper(DNA, width);
   passed = grouped.map(pass);
   console.log(passed)
   for (var i = 0; i < passed.length; i++) {
       if (passed[i]) {
           print(grouped[i] + '\n');
       }
   }
   function count(str, char) {
       return str.split(char).length - 1;
   }
   function correctDNA(str) {
       return str.length === count(str, 'A') + count(str, 'T') + count(str, 'G') + count(str, 'C');
   }
   function GC(str) {
       return (count(str, 'G') + count(str, 'C')) / str.length;
   }
   function pass(dna) {
       var gc = GC(dna);
       if (0.4 > gc || gc > 0.6) {
           console.log("Wrong GC content.");
           console.log(gc);
           return false;
       }
       if (dna[0] === 'G') {
           return false;
       }
       return true;
   }
   function splitlines(str) {
       return str.match(/[^\r\n]+/g)
   }
   function fasta2DNA(str) {
       var strs = splitlines(str);
       strs = strs.map(function (str) {
           str = str.trim();
           if (!(str[0] === '>' || str[0] === ';')) {
               return str;
           }
       });
       return strs.join();
   }
   function grouper(str, n) {
       var len = str.length;
       var res = new Array(len - n);
       for (var i = 0; i < len - n; i++) {
           res[i] = str.slice(i, i + n);
       }
       return res;
   }
   function error(str) {
       console.log("ERROR: " + str);
   }
   function print(str) {
       $('.DNA').append(document.createTextNode(str))
   }

}