// Reverse translation

function split(x,n)
{
	var y = new Array();
	var str = x.length/n;
	for (var i = 0; i < str; i++) {
		y[i] = x.substring(i*n,n+i*n) + "<br>";
	}
	return y;
}



function atransl()
{
treelett = new Array("arg","leu","ser","thr","pro","ala","gly","val","lys","asn","gln","his","glu","asp","tyr","cys","phe","ile","met","trp","***","end","???");

onelett = new Array("r","l","s","t","p","a","g","v","k","n","q","h","e","d","y","c","f","i","m","w","*","*","?");

treelett_out = new Array("Arg","Leu","Ser","Thr","Pro","Ala","Gly","Val","Lys","Asn","Gln","His","Glu","Asp","Tyr","Cys","Phe","Ile","Met","Trp","***","End","???");

nucl = new Array("mng","yun","wsn","acn","ccn","gcn","ggn","gun","aar","aay","car","cay","gar","gay","uay","ugy","uuy","auh","aug","ugg","urr","urr","???");

ncode = "r=a,g    k=g,u    h=a,c,u    d=a,g,u<br>y=c,t    s=c,g    b=c,g,u    n=a,g,c,u<br>m=a,c    w=a,u    v=a,c,g";
if(document.rtransl.u_t[1].checked)  ncode = ncode.replace(/u/g,"t");
if(document.rtransl.ftnt[1].checked) ncode = ncode.toUpperCase();


switch ( document.rtransl.org.selectedIndex )
{
	case 1:
		organism = "Mus musculus";
		freq_a = new Array(43,0,39,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
							0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 52,52, 
							33,14,14,29,28, 23,26,12,39,0, 25,0,40,0,0, 0,0,16,0,0, 76,76);
			
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
							100,0,39,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 48,48,
							41,53,5,11,10, 10,23,46,61,0, 75,0,60,0,0, 0,0,0,100,100, 24,24);
			
		freq_c = new Array(57,80,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
							0,0,61,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
							17,20,46,35,31, 38,33,25,0,57, 0,60,0,56,57, 52,56,50,0,0, 0,0);
			
		freq_u = new Array(0,20,61,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
							0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
							9,13,35,25,31, 29,18,17,0,43, 0,40,0,44,43, 48,44,34,0,0, 0,0);
		break;
	case 2:
		organism = "Drosophila melanogaster";
		freq_a = new Array(21,0,39,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
							0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 74,74, 
							25,14,9,20,25, 17,28,11,30,0, 30,0,33,0,0, 0,0,19,0,0, 68,68);
		
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
							100,0,39,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 26,26,
							26,61,20,26,29, 19,8,47,70,0, 70,0,67,0,0, 0,0,0,100,100, 32,32);
		
		freq_c = new Array(79,77,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
							0,0,61,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
							33,15,48,38,33, 45,43,24,0,55, 0,60,0,47,63, 71,62,47,0,0, 0,0);
		
		freq_u = new Array(0,23,61,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
							0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
							16,10,22,17,13, 19,21,18,0,45, 0,40,0,53,37, 29,38,34,0,0, 0,0);
		break;
	case 3:
		organism = "Arabidopsis thaliana";
		freq_a = new Array(55,0,28,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
						0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 56,56, 
						47,24,20,31,33, 27,37,15,49,0, 56,0,52,0,0, 0,0,24,0,0, 80,80);
		
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
						100,0,28,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 44,44,
						29,33,10,15,18, 14,15,26,51,0, 44,0,48,0,0, 0,0,0,100,100, 20,20);
		
		freq_c = new Array(45,64,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
						0,0,72,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
						7,17,25,20,11, 16,14,19,0,48, 0,39,0,32,48, 40,49,35,0,0, 0,0);
		
		freq_u = new Array(0,36,72,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
						0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
						17,26,44,34,38, 43,34,41,0,52, 0,61,0,68,52, 60,51,41,0,0, 0,0);
		break;
	case 4:
		organism = "Saccharomyces cerevisiae";
		freq_a = new Array(69,0,27,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
						0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 68,68, 
						55,42,21,30,41, 29,22,21,58,0, 69,0,70,0,0, 0,0,27,0,0, 77,77);
		
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
						100,0,27,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 32,32,
						25,40,10,14,12, 11,12,19,42,0, 31,0,30,0,0, 0,0,0,100,100, 23,23);
		
		freq_c = new Array(31,44,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
						0,0,73,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
						6,6,27,22,16, 22,19,21,0,41, 0,36,0,35,44, 37,41,26,0,0, 0,0);
		
		freq_u = new Array(0,56,73,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
						0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
						14,13,42,34,31, 38,47,39,0,59, 0,64,0,65,56, 63,59,46,0,0, 0,0);
		break;
	case 5:
		organism = "Pichia pastoris";
		freq_a = new Array(64,0,25,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
						0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 82,82, 
						58,28,19,24,41, 23,32,15,47,0, 61,0,57,0,0, 0,0,19,0,0, 71,71);
		
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
						100,0,25,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 18,18,
						20,48,9,11,9, 6,10,19,53,0, 39,0,43,0,0, 0,0,0,100,100, 29,29);
		
		freq_c = new Array(36,51,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
						0,0,75,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
						5,8,29,25,15, 26,14,23,0,51, 0,43,0,42,54, 35,46,32,0,0, 0,0);
		
		freq_u = new Array(0,49,75,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
						0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
						16,16,44,40,35, 45,44,42,0,49, 0,57,0,58,46, 65,54,50,0,0, 0,0);
		break;
	case 6:
		organism = "Escherichia coli";
		freq_a = new Array(13,0,40,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
						0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 70,70, 
						16,19,15,19,21, 24,15,17,73,0, 34,0,67,0,0, 0,0,14,0,0, 91,91);
		
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
						100,0,40,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 30,30,
						17,58,13,24,64, 31,16,34,27,0, 66,0,33,0,0, 0,0,0,100,100, 9,9);
		
		freq_c = new Array(87,72,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
						0,0,60,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
						33,10,38,37,13, 26,35,20,0,48, 0,41,0,36,39, 53,41,37,0,0, 0,0);
		
		freq_u = new Array(0,28,60,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
						0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
						34,12,34,19,19, 19,34,29,0,52, 0,59,0,64,61, 47,59,49,0,0, 0,0);
		break;
	default:
		organism = "Homo sapiens"
		freq_a = new Array(42,0,39,100,0, 0,0,0,100,100, 0,0,0,0,0, 0,0,100,100,0, 0,0, 
						0,0,0,0,0, 0,0,0,100,100, 100,100,100,100,100, 0,0,0,0,0, 53,53, 
						32,15,15,28,28, 23,25,12,43,0, 26,0,42,0,0, 0,0,17,0,0, 76,76);
		
		freq_g = new Array(0,0,0,0,0, 100,100,100,0,0, 0,0,100,100,0, 0,0,0,0,0, 0,0,
						100,0,39,0,0, 0,100,0,0,0, 0,0,0,0,0, 100,0,0,0,100, 47,47,
						41,53,6,11,11, 11,25,46,57,0, 74,0,58,0,0, 0,0,0,100,100, 24,24);
		
		freq_c = new Array(58,80,0,0,100, 0,0,0,0,0, 100,100,0,0,0, 0,0,0,0,0, 0,0,
						0,0,61,100,100, 100,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,
						19,20,46,36,32, 40,34,24,0,53, 0,58,0,54,56, 55,54,47,0,0, 0,0);
		
		freq_u = new Array(0,20,61,0,0, 0,0,0,0,0, 0,0,0,0,100, 100,100,0,0,100, 100,100,
						0,100,0,0,0, 0,0,100,0,0, 0,0,0,0,0, 0,100,100,100,0, 0,0,
						8,13,34,24,29, 26,16,18,0,47, 0,42,0,46,44, 45,46,36,0,0, 0,0);
}


// забираем значения, заданные в форме
var aseq     = document.rtransl.str.value;
var name_seq = document.rtransl.name.value;
var al       = parseFloat(document.rtransl.al.value); 
if(isNaN(al)){alert(alt19_01); document.rtransl.al.focus(); return true;};
al = Math.floor(Math.abs(al));
document.rtransl.al.value = al;


// приводим "str" к стандартному виду который будет называться "aseq"
aseq = aseq.toLowerCase();
if(document.rtransl.code[0].checked) 
{
	aseq = aseq.replace(/[^acdefghiklmnpqrstvwy*]/g,"");
	aalength = aseq.length;
	if(aalength<1){alert(alt19_02); document.rtransl.str.focus(); return true;};
} else {
	aseq = aseq.replace(/[^acdeghilmnoprstuvy*]/g,"");
	aalength = aseq.length;
	if(aalength<3){alert(alt19_03); document.rtransl.str.focus(); return true;};
	aalength = Math.floor(aalength/3);
}


// переводим аминокислотную последовательность в номера, соответствующие индивидуальным аминокислотам
var aa  = new Array();

for(var i1 = 0; i1 < aalength; i1++)
{
	for(var i2 = 0; i2 < 23; i2++)
	{
		if(document.rtransl.code[0].checked) 
		{
			if(aseq.substring(i1,i1+1) == onelett[i2]) {aa[i1] = i2; break};
		} else {
			if(i2 == 22) {aa[i1] = 22; break};
			if(aseq.substring(i1*3,i1*3+3) == treelett[i2]) {aa[i1] = i2; break};
		}
	}
}

codseq = "";
oseq   = "";
aseq1  = "";
nseq   = new Array("","","","");


function compare(a, b)
{
	a1 = Math.floor(a.slice(0,-1));
	b1 = Math.floor(b.slice(0,-1));
	
	if ( a1 > b1 ) return -1;
	if ( a1 < b1 ) return 1;
	return 0;
}

for(var i3 = 0; i3 < aalength; i3++)
{
	codseq += (i3 + 1) + "-";
	oseq += nucl[aa[i3]];
	
	
	if(aa[i3] == 22)
	{
		aseq1   += aseq.substring(i3*3,i3*3+3);
		codseq  += aseq.substring(i3*3,i3*3+3) + " -- Undefined amino acid\n";
		nseq[0] += "???"; nseq[1] += "   "; nseq[2] += "   "; nseq[3] += "   ";
		continue;
	}
	
	if(document.rtransl.code[0].checked) 
	{
		codseq += onelett[aa[i3]] + "\n";
		aseq1  += " " + onelett[aa[i3]] + " ";
	} else {
		codseq += treelett_out[aa[i3]] + "\n";
		aseq1  += treelett_out[aa[i3]];
	}
	
	for(var i4 = 0; i4 < 3; i4++)
	{
		codseq += "     ";
		
		fourlett = new Array(freq_a[i4*22 + aa[i3]]+"a", freq_g[i4*22 + aa[i3]]+"g", freq_c[i4*22 + aa[i3]]+"c", freq_u[i4*22 + aa[i3]]+"u" );
		
		fourlett.sort(compare);
		for(var i5 = 0; i5 < 4; i5++)
		{
			proc = fourlett[i5].slice(0,-1);
			if(proc > 0)
			{
				nucleotid = fourlett[i5].slice(-1);
				if(document.rtransl.u_t[1].checked)  nucleotid = nucleotid.replace(/u/g,"t");
				if(document.rtransl.ftnt[1].checked) nucleotid = nucleotid.toUpperCase();
				
				nseq[i5] += nucleotid;
				if(proc == 100)
				{
					codseq += nucleotid;
				} else {
					codseq += nucleotid + "(" + proc + "%) ";
				}
			} else {
				nseq[i5] += " ";
			}
		}
		codseq += "\n";
	}
	codseq += "\n";
}
if(document.rtransl.ftnt[1].checked) oseq = oseq.toUpperCase();

document.rtransl.codseq.value = codseq;

Aoseq  = split(oseq, al*3);
Aaseq1 = split(aseq1, al*3);
Anseq0 = split(nseq[0], al*3);
Anseq1 = split(nseq[1], al*3);
Anseq2 = split(nseq[2], al*3);
Anseq3 = split(nseq[3], al*3);

aseq2  = "";
aseq3  = "";


for(var i6 = 0; i6 < Aaseq1.length; i6++)
{
	aseq2 += "<font color='#800000#'>" + Aaseq1[i6] + "</font><br>" + Anseq0[i6] + "<br>" + Anseq1[i6] + "<br>" + Anseq2[i6] + "<br>" + Anseq3[i6] + "<br>&nbsp;<br>";
	aseq3 += "<font color='#800000#'>" + Aaseq1[i6] + "</font><br>" + Aoseq[i6] + "<br>&nbsp;<br>";
}


id = window.open('','result', config='height=500,width=600,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes,location=no,status=no');
id.document.write("<title>&nbsp;&nbsp;&nbsp;Reverse Translation&nbsp;&nbsp;&nbsp;</title><BODY BGCOLOR='white'>");
id.focus();
id.document.write("<h3>Reverse translation</h3><PRE>&gt; ");
id.document.write(name_seq);
id.document.write(" R-translated in organism: ");
id.document.write(organism);
id.document.write("<font color='Gray'><br>");
id.document.write(aseq2);


id.document.write("</PRE></font><h4>One-letter code:</h4><PRE>&gt; ");
id.document.write(name_seq);
id.document.write("<font color='Gray'><br>");
id.document.write(aseq3);
id.document.write("<br>&nbsp;<br>&nbsp;<br>");
id.document.write(ncode);
id.document.write("</font></PRE><br><br><br><br><br>");

// закрываем окно 
id.document.write("</body></html>");
id.document.close();

}
