MediaWiki:GlycosphingolipidMassCalculator.js

From Jcbl.jp
Revision as of 13:43, 7 March 2018 by Jcblmaster (Talk | contribs)

Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
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();
	}
	// setLongChainBases
	document.getElementById("cs_lcb_carbonno").onchange = function(){
		setLongChainBasesMax("cs_lcb_carbonno", document.getElementById("cs_lcb_carbonno").min);
	}
	document.getElementById("cs_lcb_hydroxygroup").onchange = function(){
		setLongChainBasesMax("cs_lcb_hydroxygroup", document.getElementById("cs_lcb_hydroxygroup").min);
	}
	// 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, 0);
	var hexua = getGlycosphingolipidNum("cs_hexua", 0, 0);
	var neugc = getGlycosphingolipidNum("cs_neugc", 0, 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, 0);
	if(carbonno < 3){
		saturationDOM.max = 0;
		saturationDOM.value = '0';
	} else {
		saturationDOM.max = carbonno-2;
		var saturation = getGlycosphingolipidNum("cs_fa_unsaturation", 0, 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, 0);
		if(carbonno-1 < hydroxy)
			hydroxyDOM.value = carbonno-1;
	}
}
function setLongChainBasesMax(id, min)
{
	getGlycosphingolipidNum(id, min, 0);
}
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, max)
{
	var dom = document.getElementById(id);
	var num = dom.value;
	if(num == null || !isFinite(num)){
		dom.value = 0;
		return 0;
	}
	if(max != 0 && max < num){
		dom.value = max;
		return max;
	}
	if(num < min){
		dom.value = min;
		return min;
	}
	return Number(num);
}
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, 0);
	var fucose  = getGlycosphingolipidNum("cs_fucose", 0, 0);
	var hexua   = getGlycosphingolipidNum("cs_hexua", 0, 0);
	var hexnac  = getGlycosphingolipidNum("cs_hexnac", 0, 0);
	var neuac   = getGlycosphingolipidNum("cs_neuac", 0, 0);
	var neugc   = getGlycosphingolipidNum("cs_neugc", 0, 0);
	var methyl  = getGlycosphingolipidNum("cs_methyl", -99, 0);
	var acetyl  = getGlycosphingolipidNum("cs_acetyl", -99, 0);
	var nma     = getGlycosphingolipidNum("cs_nmethylamide", 0, 0);
	var fac     = getGlycosphingolipidNum("cs_fa_carbonno", 0, 0);
	var faus    = getGlycosphingolipidNum("cs_fa_unsaturation", 0, fac-2 < 0 ? 0 : fac-2);
	var faoh    = getGlycosphingolipidNum("cs_fa_hydroxygroup", 0, fac-1 < 0 ? 0 : fac-1);
	var lcbc    = getGlycosphingolipidNum("cs_lcb_carbonno", 5, 0);
	var lcbus   = getGlycosphingolipidNum("cs_lcb_unsaturation", 0, 0);
	var lcboh   = getGlycosphingolipidNum("cs_lcb_hydroxygroup", 2, 0);
	var hpo3    = getGlycosphingolipidNum("cs_phosphate", 0, 0);
	var nh4     = getGlycosphingolipidNum("cs_nh4", 0, 0);
	var sod     = getGlycosphingolipidNum("cs_na", 0, 0);
	var pot     = getGlycosphingolipidNum("cs_k", 0, 0);
	var so3     = getGlycosphingolipidNum("cs_sulfate", 0, 0);
	var lactone = getGlycosphingolipidNum("cs_lactone", 0, neuac+neugc+hexua);
	var charge  = getGlycosphingolipidNum("cs_charge", 0, 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 - lcbus)*2 +
				nma*3 + (lcbc*2+1) + nh4*3 - 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 + nh4 + 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 EXACT = {
		"C":  12.0,
		"H":   1.0078250322,
		"Li":  7.01600344,
		"O":  15.994914620,
		"N":  14.003074004,
		"S":  31.972071174,
		"P":  30.973761998,
		"Na": 22.98976928,
		"K":  38.96370649,
		"Ca": 39.9625909
	}
	var electron = 0.00054857990;
	// 質量計算
//	var mass = calculateMassFromFormula(formula, document.getElementById("cs_average").value === 'true');
	var mass = formulaNums[0] * EXACT["C"]  + formulaNums[1] * EXACT["H"] + formulaNums[2] * EXACT["O"] +
	           formulaNums[3] * EXACT["N"]  + formulaNums[4] * EXACT["S"] + formulaNums[5] * EXACT["P"] +
	           formulaNums[6] * EXACT["Na"] + formulaNums[7] * EXACT["K"];
	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;

	if(charge != 0 && pol != 0){
		if(pol > 0)
			mass = mass / charge - electron;
		else if(pol < 0)
			mass = mass / charge + electron;
		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;
}
Personal tools