

function Calculate(form) {
	var windsize = form.windsize.value //parseInt(form.windsize.value)
	thePept = new Pept(form.PeptBox.value);
	form.PeptBox.value = thePept.Sequence;
	form.mwBox.value = thePept.MW();
	form.ExCoBox.value = thePept.ExCo();
	form.lBox.value = thePept.Sequence.length;
	form.VolBox.value = thePept.Vol(form.mwBox.value);
	document.ProtCalc.threeletterbox.value=thePept.ConvertCode(form.PeptBox.value);
	document.ProtCalc.hydrobox.value=thePept.Hydro(windsize,form.PeptBox.value);
	document.ProtCalc.resnumbox.value=thePept.ResNum(windsize,form.PeptBox.value);
	document.ProtCalc.items.value=MakeItemsList(windsize,form.PeptBox.value);
	
}

function MakeHydrograph() {
	if (document.ProtCalc.hydrobox.value.length>parseInt(document.ProtCalc.windsize.value)/2){
	var graphWindow=window.open("","Hydrograph","menubar=no,scrollbars=no,status=no,width=400,height=270")
	graphWindow.document.writeln("<HTML><HEAD><TITLE>Hydropathy Graph</TITLE></HEAD><BODY>");
	graphWindow.document.writeln('<APPLET CODE="LineChart.class" CODEBASE="http://www.basic.nwu.edu/classes/CFGraphs/" WIDTH="450" HEIGHT="250">');
	graphWindow.document.writeln('<PARAM NAME="ChartData.Columns" VALUE="Groups,Items,Values"> ');
	graphWindow.document.writeln('<PARAM NAME="ChartData.Groups" VALUE="'+document.ProtCalc.resnumbox.value+'">');
	graphWindow.document.writeln('<PARAM NAME="ChartData.Items" VALUE="'+document.ProtCalc.items.value+'">');
	graphWindow.document.writeln('<PARAM NAME="ChartData.Values"  VALUE="'+document.ProtCalc.hydrobox.value+'">');
	graphWindow.document.writeln('<PARAM NAME="Title" VALUE="Hydropathy"> ');
	graphWindow.document.writeln('<PARAM NAME="TitleFontName" VALUE="Arial">');
	graphWindow.document.writeln('<PARAM NAME="TitleFontHeight" VALUE="12">');
	graphWindow.document.writeln('<PARAM NAME="DebugInfoEnabled" VALUE="yes">');
	graphWindow.document.writeln('JAVA Must be installed for this to work properly!');
	graphWindow.document.writeln('</APPLET>');
	graphWindow.document.writeln('</BODY></HTML>');
	graphWindow.document.close(graphWindow);
}
	else{
	window.alert("The length of the peptide must be at least twice the window size to graph properly")
	}
}

function Blast(){
	var graphwindow=window.open("","BLAST");
	graphwindow.document.writeln("<HTML>");
	graphwindow.document.writeln('<FORM NAME="blastme" ACTION="http://www.ncbi.nlm.nih.gov/cgi-bin/BLAST/nph-blast" METHOD=POST>');
	graphwindow.document.writeln('<input type="hidden" name = "PROGRAM" value="blastp">');
	graphwindow.document.writeln('<input type="hidden" name = "DATALIB" value="pdb">');
	graphwindow.document.writeln('<input type="hidden" name = "SEQUENCE" value="'+document.ProtCalc.PeptBox.value+'">');
	graphwindow.document.writeln('<INPUT TYPE="submit">');
	graphwindow.document.writeln('</FORM>');
	graphwindow.document.writeln("\<SCRIPT LANGUAGE='JavaScript'>");
	graphwindow.document.writeln("document.forms[0].submit();");

	graphwindow.document.writeln("\<\/SCRIPT>");
	graphwindow.document.writeln("\</HTML>");
}

function Pept(theString)
{
	theString = theString.toUpperCase()
	this.Sequence = RemoveNonRes(theString)
	this.aCount = CountChar(theString,"A")
	this.bCount = CountChar(theString,"B")
	this.cCount = CountChar(theString,"C")
	this.dCount = CountChar(theString,"D")
	this.eCount = CountChar(theString,"E")
	this.fCount = CountChar(theString,"F")
	this.gCount = CountChar(theString,"G")
	this.hCount = CountChar(theString,"H")
	this.iCount = CountChar(theString,"I")
	this.kCount = CountChar(theString,"K")
	this.lCount = CountChar(theString,"L")
	this.mCount = CountChar(theString,"M")
	this.nCount = CountChar(theString,"N")
	this.pCount = CountChar(theString,"P")
	this.qCount = CountChar(theString,"Q")
	this.rCount = CountChar(theString,"R")
	this.sCount = CountChar(theString,"S")
	this.tCount = CountChar(theString,"T")
	this.vCount = CountChar(theString,"V")
	this.wCount = CountChar(theString,"W")
	this.yCount = CountChar(theString,"Y")
	this.zCount = CountChar(theString,"Z")
	this.MW = MW
	this.ExCo = ExCo
	this.Vol = Vol
	this.ConvertCode = ConvertCode
	this.ResTo = ResTo
	this.Hydro = Hydro
	this.AssignH = AssignH
	this.ResNum = ResNum
}

function AddRes(form, res){
	form.PeptBox.value += res
//	Calculate(form)
}

//calculates molecular weight of peptide sequence after editing
function MW()
{
	if (this.Sequence.length > 0) {
		return Math.round(71.1 * this.aCount + 156.2 * this.rCount
+ 114.1 * this.nCount + 115.1 * this.dCount  + 103.1 * this.cCount + 128.1
* this.qCount + 129.1 * this.eCount  + 57.1 * this.gCount  + 137.1 *
this.hCount  + 113.1 * this.iCount  + 113.1 * this.lCount  + 128.2 *
this.kCount + 131.2 * this.mCount + 147.2 * this.fCount  + 97.1 *
this.pCount + 87.1 * this.sCount + 101.1 * this.tCount + 186.2 *
this.wCount + 163.2 * this.yCount + 99.1 * this.vCount + 132.61 * this.bCount + 146.64 * this.zCount + 18 )
	}
	else
	{
		return "0"
	}
}

/* ExCo funtion calculates the extinction coefficient of a denatured
protein in 6M guanidine/
HCl  at 280 nm based on the wok of Gill and von Hipple, Anal Biochem  (1989)*/
function ExCo()
{
	if (this.cCount || this.yCount|| this.wCount) {
		return Math.round(120 * this.cCount + 1280 * this.yCount +
5690 *  this.wCount)
	}
	else{
		return "<==See Assumptions"
	}
}


function Vol(molwt)
{

	if (this.Sequence.length > 0) {
		return Math.round( 1.21 * molwt)
	}
	else{
		return "0"
	}
}


function RemoveNonRes(theString) {
	var returnString = "";
	for ( var i = 0; i < theString.length; i++) {
		if (IsRes(theString.charAt(i))) {
			returnString += theString.charAt(i);
		}
	}
	return returnString
}

function CountChar(theString,theChar) {
	var returnValue = 0;
	for ( var i = 0; i < theString.length; i++) {
		if (theString.charAt(i) == theChar) {
			returnValue ++;
		}
	}
	return returnValue
}

function DoCheck(PeptBox) {
	PeptBox.value = CheckRes(PeptBox.value)
}

function CheckRes(theString) {
	var returnString = "";
	var cnt = 0;
	var rcnt = 0;
	theString = theString.toUpperCase()
	for ( var i = 0; i < theString.length; i++) {
		if (IsRes(theString.charAt(i))) {
			returnString += theString.charAt(i)
		}
	}
	theString=returnString
	returnString=""
	for ( var i = 0; i < theString.length; i++) {
		if (cnt>2) {
			returnString += " "
			cnt=0
		}
		cnt++
		if (rcnt>20) {
			returnString += "\r\n"
			rcnt=0
		}
		rcnt++
		returnString += theString.charAt(i)
	}
	return returnString
}

function IsRes(theRes) {
 if ((theRes == "A") ||
	(theRes == "B") ||
	(theRes == "C") ||
	(theRes == "D") ||
	(theRes == "E") ||
	(theRes == "F") ||
	(theRes == "G") ||
	(theRes == "H") ||
	(theRes == "I") ||
	(theRes == "K") ||
	(theRes == "L") ||
	(theRes == "M") ||
	(theRes == "N") ||
	(theRes == "P") ||
	(theRes == "Q") ||
	(theRes == "R") ||
	(theRes == "S") ||
	(theRes == "T") ||
	(theRes == "V") ||
	(theRes == "W") ||
	(theRes == "Y") ||
	(theRes == "Z") ) {
		return 1
	}
	return 0  //put in alert box
}

function ConvertCode(data) {
	if (data.length<1)
		return "";
	var returnstring=ResTo(data.charAt(0));
	for (var i=1; i<data.length;i++) {
		returnstring+=" - "+ResTo(data.charAt(i));
	}
    return returnstring;
}



function ResTo(theRes) {
	if (theRes== "A") return "Ala";
	if (theRes == "B") return "Asx";
	if (theRes == "C") return "Cys";
	if (theRes == "D") return "Asp";
	if (theRes == "E") return "Glu";
	if (theRes == "F") return "Phe";
	if (theRes == "G") return "Gly";
	if (theRes == "H") return "His";
	if (theRes == "I") return "Ile";
	if (theRes == "K") return "Lys";
	if (theRes == "L") return "Leu";
	if (theRes == "M") return "Met";
	if (theRes == "N") return "Asn";
	if (theRes == "P") return "Pro";
	if (theRes == "Q") return "Gln";
	if (theRes == "R") return "Arg";
	if (theRes == "S") return "Ser";
	if (theRes == "T") return "Thr";
	if (theRes == "V") return "Val";
	if (theRes == "W") return "Trp";
	if (theRes == "Y") return "Tyr";
	if (theRes == "Z") return "Glx";
	return "???";
}


function Hydro(wind, data)
{
	var i = 0;
	var sum,j;
	var blockCnt=parseInt(wind);
	document.ProtCalc.windsize.value=wind;
	returnstring ="";
	while (i < (data.length - blockCnt)) //228
	{
		sum = 0;
		for ( j=i; j<i+blockCnt && j<data.length; j++)
		{
			sum += AssignH(data.charAt(j));
		}
		if (returnstring.length>0)
			returnstring+=",";
		returnstring+=Math.round(sum);
		i++;
	}
	return returnstring;
 }

/*sums hydropathy using moving window approach of 11 residues.
this is to be plotted against the sixth residue in the middle of the
 moving window */

function ResNum(wind,peptide)
{
	var i=0
	document.ProtCalc.windsize.value=parseInt(wind);
	returnstring = ""
	for (i=Math.round(1+wind/2)-1; i < peptide.length - Math.round(1+wind/2); i++)
	{
		if (returnstring.length>0)
			returnstring+=",";
		returnstring+=i;
	}
	return returnstring

}

function MakeItemsList(peptide)
{
	var i=0
	returnstring = ""
	for (i=5; i < peptide.length - 6; i++)
	{
		if (returnstring.length>0)
			returnstring+=",";
		returnstring+="hydropathy";
	}
	return returnstring

}

//returns the residue # in middle of window



function AssignH(theRes) {

	if (theRes == "A") return 6.3;
	if (theRes == "B") return 1.0;
	if (theRes == "C") return 7.0;
	if (theRes == "D") return 1.0;
	if (theRes == "E") return 1.0;
	if (theRes == "F") return 7.2;
	if (theRes == "G") return 4.1;
	if (theRes == "H") return 1.3;
	if (theRes == "I") return 9.0;
	if (theRes == "K") return 0.6;
	if (theRes == "L") return 8.2;
	if (theRes == "M") return 6.4;
	if (theRes == "N") return 1.0;
	if (theRes == "P") return 2.9;
	if (theRes == "Q") return 1.0;
	if (theRes == "R") return 0.0;
	if (theRes == "S") return 3.6;
	if (theRes == "T") return 3.8;
	if (theRes == "V") return 8.7;
	if (theRes == "W") return 3.6;
	if (theRes == "Y") return 3.2;
	if (theRes == "Z") return 1.0
	return 0
}

//assigns hydropathy values as per Kyte and Dolittle j. mol.bio. (1982)

function Disallow(form) {
	form.PeptBox.focus()
}





