MediaWiki:GlycosphingolipidMassCalculator.js: Difference between revisions

No edit summary
No edit summary
Line 79: Line 79:
function switchCharge()
function switchCharge()
{
{
var disabled = document.getElementById("cs_polarity").selectedIndex == 0;
var display = document.getElementById("cs_polarity").selectedIndex == 0 ? 'none' : 'block';
document.getElementById("cs_charge").disabled = disabled;
document.getElementById("div_charge").style.cssText = 'display: ' + display;
}
}
function checkCharge()
function checkCharge()
Line 179: Line 179:


// 質量計算
// 質量計算
var mass = calculateMassFromFormula(formula, document.getElementById("cs_average").selectedIndex==0);
var mass = calculateMassFromFormula(formula, document.getElementById("cs_average").value === 'true');
var nominal = formulaNums[0]*12 +formulaNums[1] +formulaNums[2]*16
var nominal = formulaNums[0]*12 +formulaNums[1] +formulaNums[2]*16
+formulaNums[3]*14 +formulaNums[4]*32 +formulaNums[5]*31
+formulaNums[3]*14 +formulaNums[4]*32 +formulaNums[5]*31
+formulaNums[6]*23 +formulaNums[7]*39 +formulaNums[8]*40;
+formulaNums[6]*23 +formulaNums[7]*39 +formulaNums[8]*40;
if(charge != 0){
if(charge != 0 && pol != 0){
mass    /= charge;
mass    /= charge;
nominal /= charge;
nominal /= charge;

Revision as of 03:51, 5 June 2017

addOnloadHook(GlycosphingolipidMassCalculator_init); 

function GlycosphingolipidMassCalculator_init()
{
	// recalculate
	document.getElementById("cs_average").onchange = function(){
		calculateGlycosphingolipid();
	}
	// setLactoneMax
	document.getElementById("cs_neuac").onchange = function(){
		setLactoneMax();
	}
	document.getElementById("cs_neugc").onchange = function(){
		setLactoneMax();
	}
	document.getElementById("cs_hexua").onchange = function(){
		setLactoneMax();
	}
	document.getElementById("cs_lactone").onchange = function(){
		setLactoneMax();
	}
	// setFattyAcidMax
	document.getElementById("cs_fa_carbonno").onchange = function(){
		setFattyAcidMax();
	}
	document.getElementById("cs_fa_unsaturation").onchange = function(){
		setFattyAcidMax();
	}
	document.getElementById("cs_fa_hydroxygroup").onchange = function(){
		setFattyAcidMax();
	}
	// Polarity
	document.getElementById("cs_polarity").onchange = function(){
		switchCharge();
	}
	document.getElementById("cs_charge").onchange = function(){
		checkCharge();
	}
	// calculate button
	document.getElementById("calculate_glycosphingolipid_button").onclick = function(){
		calculateGlycosphingolipid();
	}
}
function setLactoneMax()
{
	var neuac = getGlycosphingolipidNum("cs_neuac", 0);
	var hexua = getGlycosphingolipidNum("cs_hexua", 0);
	var neugc = getGlycosphingolipidNum("cs_neugc", 0);
	var lactoneMax = neuac + hexua + neugc;

	var lactone = document.getElementById("cs_lactone");
	lactone.max = lactoneMax;
	if(lactoneMax < lactone.value)
		lactone.value = lactoneMax;
}
function setFattyAcidMax()
{
	var saturationDOM = document.getElementById("cs_fa_unsaturation");
	var hydroxyDOM    = document.getElementById("cs_fa_hydroxygroup");
	var carbonno = getGlycosphingolipidNum("cs_fa_carbonno", 0);
	if(carbonno < 3){
		saturationDOM.max = 0;
		saturationDOM.value = '0';
	} else {
		saturationDOM.max = carbonno-2;
		var saturation = getGlycosphingolipidNum("cs_fa_unsaturation", 0);
		if(carbonno-2 < saturation)
			saturationDOM.value = carbonno-2;
	}
	if(carbonno < 2)
		hydroxyDOM.value = '0';
	else {
		hydroxyDOM.max = carbonno-1;
		var hydroxy = getGlycosphingolipidNum("cs_fa_hydroxygroup", 0);
		if(carbonno-1 < hydroxy)
			hydroxyDOM.value = carbonno-1;
	}
}
function switchCharge()
{
	var display = document.getElementById("cs_polarity").selectedIndex == 0 ? 'none' : 'block';
	document.getElementById("div_charge").style.cssText = 'display: ' + display;
}
function checkCharge()
{
	var charge = Number(document.getElementById("cs_charge").value);
	if(charge == 0)
		charge = 1;
	else if(charge < 0)
		charge = -charge;
	document.getElementById("cs_charge").value = charge;
}
function getGlycosphingolipidNum(id, min)
{
	var dom = document.getElementById(id);
	var num = dom.value;
	if(isFinite(num) && num >= min)
		return Number(num);
	dom.value = min;
	return 0;
}
function checkFormulaNums(atom, count)
{
	var str = "";
	if(count > 0){
		str = atom;
		if(count > 1)
			str += count;
	}
	return str;
}
function calculateGlycosphingolipid()
{
	var hexose  = getGlycosphingolipidNum("cs_hexose", 0);
	var fucose  = getGlycosphingolipidNum("cs_fucose", 0);
	var hexua   = getGlycosphingolipidNum("cs_hexua", 0);
	var hexnac  = getGlycosphingolipidNum("cs_hexnac", 0);
	var neuac   = getGlycosphingolipidNum("cs_neuac", 0);
	var neugc   = getGlycosphingolipidNum("cs_neugc", 0);
	var methyl  = getGlycosphingolipidNum("cs_methyl", -99);
	var acetyl  = getGlycosphingolipidNum("cs_acetyl", -99);
	var nma     = getGlycosphingolipidNum("cs_nmethylamide", 0);
	var fac     = getGlycosphingolipidNum("cs_fa_carbonno", 0);
	var faus    = getGlycosphingolipidNum("cs_fa_unsaturation", 0);
	var faoh    = getGlycosphingolipidNum("cs_fa_hydroxygroup", 0);
	var lcbc    = getGlycosphingolipidNum("cs_lcb_carbonno", 0);
	var lcbus   = getGlycosphingolipidNum("cs_lcb_unsaturation", 0);
	var lcboh   = getGlycosphingolipidNum("cs_lcb_hydroxygroup", 0);
	var hpo3    = getGlycosphingolipidNum("cs_phosphate", 0);
	var cal     = getGlycosphingolipidNum("cs_ca", 0);
	var sod     = getGlycosphingolipidNum("cs_na", 0);
	var pot     = getGlycosphingolipidNum("cs_k", 0);
	var so3     = getGlycosphingolipidNum("cs_sulfate", 0);
	var lactone = getGlycosphingolipidNum("cs_lactone", 0);
	var charge  = getGlycosphingolipidNum("cs_charge", 0);

	var pol     = document.getElementById("cs_polarity").value;
	switch(pol){
		case 'NON': pol =  0; break;
		case 'NEG': pol =  1; break;
		case 'POS': pol = -1; break;
	}

	// struct formula
	var formulaNums = [];
	formulaNums[0] = (hexose + fucose + hexua)*6 + hexnac*8 + (neuac + neugc)*11 +
				methyl + acetyl*2 + nma + fac + lcbc; // C
	formulaNums[1] = 2 + (hexose + fucose)*10 + hexnac*13 + (neuac + neugc)*17 +
				hexua*8 + hpo3 + (methyl + acetyl - cal - lcbus)*2 +
				nma*3 + (lcbc*2+1) - sod - pot - (pol * charge); // H
	if(fac != 0)
		formulaNums[1] = formulaNums[1] + (fac-1)*2 - faus*2;
	formulaNums[2] = 1 + (hexose + hexnac)*5 + fucose*4 + neuac*8 + neugc*9 +
				hexua*6 + (so3 + hpo3)*3 + acetyl - nma + (lcboh-1); // O
	if(fac != 0)
		formulaNums[2] = formulaNums[2] + 1 + faoh;
	formulaNums[3] = 1 + hexnac + neuac + neugc + nma; // N
	formulaNums[4] = so3;  // S
	formulaNums[5] = hpo3; // P
	formulaNums[6] = sod;  // Na
	formulaNums[7] = pot;  // K
	formulaNums[8] = cal;  // Ca
	formulaNums[1] = formulaNums[1] - lactone*2;
	formulaNums[2] = formulaNums[2] - lactone;

	var formula = 'C' + formulaNums[0];
	formula += 'H' + formulaNums[1];
	formula += 'N';
	if(formulaNums[3] > 1)
		formula += formulaNums[3];
	formula += checkFormulaNums('O',  formulaNums[2]);
	formula += checkFormulaNums('P',  formulaNums[5]);
	formula += checkFormulaNums('S',  formulaNums[4]);
	formula += checkFormulaNums('Ca', formulaNums[8]);
	formula += checkFormulaNums('K',  formulaNums[7]);
	formula += checkFormulaNums('Na', formulaNums[6]);

	document.getElementById("cs_formula").value = formula;

	// 質量計算
	var mass = calculateMassFromFormula(formula, document.getElementById("cs_average").value === 'true');
	var nominal = formulaNums[0]*12 +formulaNums[1] +formulaNums[2]*16
		+formulaNums[3]*14 +formulaNums[4]*32 +formulaNums[5]*31
		+formulaNums[6]*23 +formulaNums[7]*39 +formulaNums[8]*40;
	if(charge != 0 && pol != 0){
		mass    /= charge;
		nominal /= charge;
	}
	if(Math.floor(nominal) == nominal)
		nominal = nominal.toFixed(0);
	else
		nominal = nominal.toFixed(2);
	document.getElementById("cs_mass").value = mass.toFixed(6);
	document.getElementById("cs_nominalmass").value = nominal;
}