//Global variables
var WinOut, WinCount=0;
var n, s,d,c,s_count,d_count,c_count,c_lim;	//arrays for synthesis, decay and mRNA concentrations 
var s_min, s_max, d_min, d_max;				//physiological limits (see SimChip1)
var DisS,DisD;								//Distribution of s and d

function simchip2_input(){
//Creates a user form to enter simchip2 input data
var Input="<table><tr><td style=\'padding-left:20px;padding-right:20px;vertical-align:top;width:580px\'>"
+"<p><h3>Gene expression profiles in normal cells</h3></p>"
+"<form name=\'f_inp2\' action=\'\'>"
+"<table style=\'font-size:9pt\';  border=\'1\'; text-align: left; cellpadding=\'2\' cellspacing=\'2\'>"
+"<tr bgcolor=\'080880\' style=\'color: white\'>"
+"<td style=\'width: 180px\'><input name=\'Genes\' value=\'1000\' type=\'text\' title=\'200-32768\' size=\'4\' onblur=\'check_Genes(this.value)\'>&nbsp;&nbsp;Genes&nbsp;&nbsp;"
+"<input name=\'SortData\' type=checkbox>&nbsp;&nbsp;Sorted</td>"
+"<td style=\'text-align: center; width: 135px;\'><b>Synthesis</b></td>"
+"<td style=\'text-align: center; width: 135px;\'><b>Decay</b></td>"
+"</tr><tr>"
+"<td>Distribution type</td>"
+"<td>"
+"<input name=\'DistributionS\' value=\'uniform\' type=radio>&nbsp;uniform<br>"
+"<input name=\'DistributionS\' value=\'normal\' type=radio>&nbsp;normal<br>"
+"<input name=\'DistributionS\' checked value=\'skewed\' type=radio>&nbsp;skewed<br></td>"+"<td style=\'text-align: left\'>"
+"<input name=\'DistributionD\' checked value=\'uniform\' type=radio>&nbsp;uniform<br>"
+"<input name=\'DistributionD\' value=\'normal\' type=radio>&nbsp;normal<br>"
+"<input name=\'DistributionD\' value=\'skewed\' type=radio>&nbsp;skewed<br></td>"
+"</tr><tr bgcolor=\'CCCCCC\'><td>Minimum</td><td>"
+"<input name=\'S_min\' value=\'0\' type=\'text\' titel=\'0-10\' size=\'5\' onblur=\'check_Smin(this.value)\'>&nbsp;&nbsp;(molecules/min)</td><td>"
+"<input name=\'D_min\' value=\'0.05\' type=\'text\' titel=\'0.01-1\' size=\'5\' onblur=\'check_Dmin(this.value)\'>&nbsp;&nbsp;(percent/min)</td>"
+"</tr><tr bgcolor=\'CCCCCC\'>"
+"<td>Maximum</td>"
+"<td style=\'text-align: left\'>"
+"<input name=\'S_max\' value=\'50\' type=\'text\' titel=\'10-100\' size=\'5\' onblur=\'check_Smax(this.value)\'>&nbsp;&nbsp;(molecules/min)</td><td>"
+"<input name=\'D_max\' value=\'25\' type=\'text\' titel=\'1-50\' size=\'5\' onblur=\'check_Dmax(this.value)\'>&nbsp;&nbsp;(percent/min)</td>"
+"</tr></table>"
+"<table style=\'font-size:9pt\';  border=\'0\'; text-align: left; cellpadding=\'2\' cellspacing=\'2\'>"
+"<tr>"
+"<td style=\'width: 50px\'><input type=\'button\' value=\'GO\' onClick=\'simchip2_main()\'></td>"
+"<td style=\'text-align: center; width: 350px;\'></td>"
+"<td style=\'width: 50px\'><input type=\'button\' value=\'HELP\' onClick=\'simchip2_help()\'></td>"
+"</tr></table>"
+"</form><br>"
+"<a href=\"./downloads/bitterlich_and_hoffmann_2007.pdf\" target=\"poster\">Preliminary publication (in German)</a><br>"
+"<a href=\"./downloads/simchip2_shortcode.html\" target=\"SimChip2\">Simplified source code example</a><br>"
+"<a href=\"http://www.ncbi.nlm.nih.gov/Taxonomy/taxonomyhome.html/index.cgi\" target=\"NCBI\">Experimental data from the NCBI taxonomy browser</a>";
document.getElementById("divTxt").innerHTML=Input;
}//end of simchip2_input

function simchip2_main(){
//reads user-defined variables from input form f_inp2
var i,j,m=0,sort;
  n=parseInt(document.f_inp2.Genes.value);		//total number of probes (rows)
  sort=document.f_inp2.SortData.checked;
  for (i=2;i<=4;i++){
	if (document.f_inp2.elements[i].checked)
      DisS=i-2;
  }
  for (i=5;i<=7;i++){
	if (document.f_inp2.elements[i].checked)
      DisD=i-5;
  }
  s_min=parseFloat(document.f_inp2.S_min.value);
  s_max=parseFloat(document.f_inp2.S_max.value);
  d_min=parseFloat(document.f_inp2.D_min.value)/100;
  d_max=parseFloat(document.f_inp2.D_max.value)/100;
  
  //Simulation
  s=new Array(n);		//synthesis array
  d=new Array(n);		//decay array
  c=new Array(n);		//concentration array
  for (i=1;i<=n;i++){
	s[i]=Math.round(Distribution(DisS,s_min,s_max)*100)/100;
	d[i]=Math.round(Distribution(DisD,d_min,d_max)*10000)/10000;
	c[i]=Math.round(s[i]/d[i]);
  }
  
  //Frequency counting
  s_count=new Array(20);		//synthesis counter
  d_count=new Array(20);		//decay counter
  c_count=new Array(20);		//concentration counter
  for (j=1;j<=20;j++){
	s_count[j]=0;
	d_count[j]=0;
	c_count[j]=0;
  }
  //splits total range of s and d into 20 equal categories
  for(i=1;i<=n;i++){
	for(j=1;j<=20;j++){
	  if(s[i] >= s_max/20*(j-1) && s[i] < s_max/20*j)
	    s_count[j]++;
	  if(d[i] >= d_max/20*(j-1) && d[i] < d_max/20*j)
	    d_count[j]++;
	}
  }
  //splits 1% of the total range of c into 19 categories and puts the remaining upper 99% into category 20
  c_lim = s_max/d_min*0.01;  
  for(i=1;i<=n;i++){
	for(j=1;j<=19;j++){
	  if(c[i] >= c_lim/20*(j-1) && c[i] < c_lim/20*j){
	    m++;
	    c_count[j]++;
	  }
	}
  }
  c_count[20]=n-m;  //number of probes falling into upper class

  
  //Bubble sort
  if(sort==true){
	for(i=1;i<=n;i++){
		for(j=i+1;j<=n;j++){
		  if(c[i]<c[j]){
			s[0]=s[i];
			d[0]=d[i];
			c[0]=c[i];
			s[i]=s[j];
			d[i]=d[j];
			c[i]=c[j];
			s[j]=s[0];
			d[j]=d[0];
			c[j]=c[0];
		  }
		}
	}
  }
  Write_data();
}//end of simchip2_main

function simchip2_help(){
var WinHelp;
WinHelp=window.open("","","width=500,height=250,left=10,top=10,resizable=yes,scrollbars=yes,menubar=no");
	WinHelp.document.writeln("<html><head><title>SimChip 2</title></head>\n<body>");
	WinHelp.document.writeln("<h3>How to create a typical gene expression profile<\/h3>"); 
	WinHelp.document.writeln("Click on GO, and you will get an expression profile of 1000 genes. ");
	WinHelp.document.writeln("Then choose any other number from 200 to 32,768 genes and eventually check the <i>Sorted</i> box. ");
	WinHelp.document.writeln("<p>In accordance with <i>SimChip1<\/i>, each signal is calculated as a ratio:");
	WinHelp.document.writeln("<b>c = S / D</b>. Both constants will vary randomly within physiological limits (see below).<\/p>"); 
	WinHelp.document.writeln("<p>The default settings produce a right-skewed histogram, which mirrors real experimental conditions. "); 
	WinHelp.document.writeln("Therefore, the underlying individual values can be used for calibration of microarray data against a virtual standard.<\/p>"); 
	WinHelp.document.writeln("Furthermore, when you play with the distribution types of S and D, you will find that the typical extreme skewness is only obtained, ");  
	WinHelp.document.writeln("if D is uniformely distributed. The distribution of S seems to be of lesser importance, ");
	WinHelp.document.writeln("but as is shown below, the skewed distribution makes physiological sense.</p>"); 
	WinHelp.document.writeln("<h4>Mathematical Background<\/h4>");
	WinHelp.document.writeln("The overall distribution pattern is a combined probability density function of the two constants S and D.");
	WinHelp.document.writeln("<br>In our computer model, S varies from 0 to 50 molecules and D from 0.05 to 25 percent per minute.");
	WinHelp.document.writeln("Uniform distributions are generated using the javascript function Math.random(). ");
	WinHelp.document.writeln("For a \"pseudo-normal\" distribution, <i>SimChip</i> calculates the <u>average</u> of four such random numbers. ");
	WinHelp.document.writeln("The \"skewed\" distribution for S is derived from work of Konishi: This Japanese computer scientist postulated ");
	WinHelp.document.writeln("from thermodynamic considerations that the mRNA synthesis rate can be described as a <u>product</u> of random numbers ");
	WinHelp.document.writeln("(POL II x activation x inhibition).");
	WinHelp.document.writeln("\n<\/body><\/html>");
WinHelp.document.close();
}//end of simchip2_help

function Distribution(type, min, max){
  switch (type) {
  case 0:	//uniform
    return min+(max-min)*Math.random();
    break;
  case 1:	//normal
    return min+(max-min)*((Math.random()+Math.random()+Math.random()+Math.random())/4);
    break;
  case 2:	//skewed
    return min+(max-min)*(Math.random()*Math.random());
    break;
  }
}

function check_Genes(tx){
with (document.f_inp2.Genes) {
	value = parseInt(tx);
	if(isNaN(value)) value="1000";
	if(value<200) value="200";
	if(value>32786) value="32786";
   }
}
function check_Smin(tx){
with (document.f_inp2.S_min) {
	value = parseFloat(tx);
	if(isNaN(value)) value="0";
	if(value<0) value="0";
	if(value>10) value="10";
   }
}
function check_Smax(tx){
with (document.f_inp2.S_max) {
	value = parseFloat(tx);
	if(isNaN(value)) value="50";
	if(value<10) value="10";
	if(value>100) value="100";
   }
}
function check_Dmin(tx){
with (document.f_inp2.D_min) {
	value = parseFloat(tx);
	if(isNaN(value)) value="0.05";
	if(value<0.01) value="0.01";
	if(value>1) value="1";
   }
}
function check_Dmax(tx){
with (document.f_inp2.D_max) {
	value = parseFloat(tx);
	if(isNaN(value)) value="25";
	if(value<1) value="1";
	if(value>50) value="50";
   }
}

function Write_data(){
var i,x,y,h;
var graph;
graph='<img src=\"./images/sim2ordinate50.gif\" align=\"bottom\">';
for(i=1;i<=20;i++){
  h=Math.round(c_count[i]/n*375*2)
  if(h>380)h=380;
  graph+='<img src=\"./images/blue_square.gif\" align=\"bottom\" style=\"width:20px; height:'+h+'px\">';	//Math.round(c_count[i]/n*375*2)px>';
}
for(i=1;i<=20;i++){
  x=(i+2)*20+4;
  y=385-(s_count[i]/n)*385*2;
  graph+='<img src=\"./images/blue_dot.gif\" align=\"top\" style=\"position:absolute;top:'+y+'px;left:'+x+'px\">';
}
for(i=1;i<=20;i++){
  x=(i+2)*20+6;
  y=385-(d_count[i]/n)*385*2;
  graph+='<img src=\"./images/red_dot.gif\" align=\"top\" style=\"position:absolute;top:'+y+'px;left:'+x+'px\">';
}
WinCount++;
  WinOut=window.open("",WinCount,"width=600,height=600,left=10,top=10,resizable=yes,scrollbars=yes,menubar=yes");
  WinOut.document.writeln("<html><head><title>SimChip 2 Histogram</title></head>\n<body>");
  WinOut.document.writeln(graph+"<br><br>");
  WinOut.document.writeln("Frequency distribution:&nbsp;&nbsp;&nbsp;<img src=\"./images/blue_square.gif\">&nbsp;&nbsp;=&nbsp;c&nbsp;&nbsp;&nbsp;<img src=\"./images/blue_dot.gif\">&nbsp;&nbsp;=&nbsp;s&nbsp;&nbsp;&nbsp;<img src=\"./images/red_dot.gif\">&nbsp;&nbsp;=&nbsp;d<br>");
  WinOut.document.writeln("Synthesis: " + document.f_inp2.elements[DisS+2].value + " distribution between " + document.f_inp2.S_min.value + " to " + document.f_inp2.S_max.value + " molecules per min<br>");
  WinOut.document.writeln("Decay: " + document.f_inp2.elements[DisD+5].value + " distribution between " + document.f_inp2.D_min.value + " to " + document.f_inp2.D_max.value + " percent per min<br>");
  WinOut.document.writeln("For range limits see below table<br><br>");
  WinOut.document.writeln("<table style=\'font-size:9pt\';  border=\'1\'>");
  WinOut.document.writeln("<tr  bgcolor=\'080880\' style=\'color: white\' align='center'><td>mRNA<br>(molecules/cell)</td><td>frequency<br>(%)</td><td>synthesis<br>(molecules/min)</td><td>frequency<br>(%)</td><td>decay<br>(percent/min)</td><td>frequency<br>(%)</td></tr>");
  for(i=1;i<=19;i++){
	WinOut.document.writeln("<tr align='center'><td>\<"+Math.round(c_lim/20*i)+"</td><td>"+Math.round(c_count[i]/n*1000)/10+"</td><td>\<"+Math.round(s_max/20*i*100)/100+"</td><td>"+Math.round(s_count[i]/n*1000)/10+"</td><td>\<"+Math.round(d_max/20*i*10000)/100+"</td><td>"+Math.round(d_count[i]/n*1000)/10+"</td></tr>");	
  }
  WinOut.document.writeln("<tr align='center'><td>\<"+Math.round(s_max/d_min)+"</td><td>"+Math.round(c_count[20]/n*1000)/10+"</td><td>\<"+Math.round(s_max*100)/100+"</td><td>"+Math.round(s_count[20]/n*1000)/10+"</td><td>\<"+Math.round(d_max*10000)/100+"</td><td>"+Math.round(d_count[20]/n*1000)/10+"</td></tr>");
  WinOut.document.writeln("</table><br><br>");
  WinOut.document.writeln("<table style=\'font-size:9pt\';  border=\'1\'>");
  WinOut.document.writeln("<tr bgcolor=\'080880\' style=\'color: white\' align='center'><td>probe<br>number</td>");
  WinOut.document.writeln("<td>mRNA<br>(molecules/cell)</td><td>synthesis<br>(molecules/min)</td><td>decay<br>(percent/min)</td></tr>");
  for(i=1;i<=n;i++)
	WinOut.document.writeln("<tr align='center'><td>P"+i+"</td><td>"+c[i]+"</td><td>"+s[i]+"</td><td>"+Math.round(d[i]*10000)/100+"</td></tr>");
  WinOut.document.writeln("</table></body></html>");
  WinOut.document.close();
}

