MediaWiki:LipoqualitySearch.js

From Metabolomics.JP
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
// Last-update: 2018/10/11
+
// Last-update: 2018/10/30
 
// 対象項目からデータを検索する ////////////////////////////////////////////////////////
 
// 対象項目からデータを検索する ////////////////////////////////////////////////////////
 
// param sampleSource - sample source
 
// param sampleSource - sample source
Line 284: Line 284:
 
else if(gAllData[key][j]["Sample source"] == "Cell")
 
else if(gAllData[key][j]["Sample source"] == "Cell")
 
legend = gAllData[key][j]["Sample type"];
 
legend = gAllData[key][j]["Sample type"];
 +
else
 +
legend = key + "/" + gAllData[key][j]["Sample name"];
  
 
if(!legends[legend] && lipidType == chain){ // legend名が複数回被る可能性があるので、すでに格納済みのlegendは処理しない
 
if(!legends[legend] && lipidType == chain){ // legend名が複数回被る可能性があるので、すでに格納済みのlegendは処理しない

Revision as of 09:39, 30 October 2018

// Last-update: 2018/10/30
// 対象項目からデータを検索する ////////////////////////////////////////////////////////
// param sampleSource - sample source
// param sampleType - sample type
// param lipidClass - lipid class
// return 検索結果
function lipoqualitySearch(sampleSource, sampleType, lipidClass)
{
	// sample sourceとsample typeはAllがあるため、正規表現を用いてマッチングを行う
	var ssReg = new RegExp(translateForReg(sampleSource));
	var stReg = new RegExp(translateForReg(sampleType));

	// 検索
	var result = [];
	var i = 0;
	for(key in gAllData){
		if(!ssReg.test(key)) // sample sourceが合致するか
			continue;

		for(var j = 0; j < gAllData[key].length; j ++){
//console.log("length:" + j + "," + gAllData[key].length);
			// 対象sample source, sample typeかチェック
//console.log("st:" + key + ":" + gAllData[key][j]["Sample name"] + ":" + stReg.test(gAllData[key][j]["Sample name"]));
			if(!stReg.test(gAllData[key][j]["Sample name"])) // sample typeが合致するか
				continue;

//console.log("search:" + key + ":" + gAllData[key][j]["Sample name"] + ":" + gAllData[key][j]["data"].length);
			var found = false;
			for(var k = 0; k < gAllData[key][j]["data"].length; k ++){
				// 対象lipid classかチェック
//console.log("hit:" + key + ":" + gAllData[key][j]["data"][k]["Lipid class"] + "==" + lipidClass);
				if(gAllData[key][j]["data"][k]["Lipid class"] == lipidClass){
					found = true;
					break;
				}
			}
			// 対象だったので結果に格納する
			if(found){
				result[i] = [];
				result[i]["title"]       = key + "/" + gAllData[key][j]["Sample name"] + "/" + lipidClass;
				result[i]["Method link"] = gAllData[key][j]["Method link"];
				i ++;
			}
		}
	}

	return result;
}
// 指定されたID群をもとに、グラフ全般用のデータを取得する ///////////////////////////////
// param ids       - 対象データのID。「SampleSource/SampleType/LipidClass」で構成される。
// param chartType - 未使用
// return 対象データ
function retrieveChartData(ids, chartType)
{
	// '#7fbfff'がbarchartのデフォルト色と似ているので排除する
	var pieColors = ['#ff7f7f','#7f7fff','#7fff7f','#ff7fbf','#bfff7f','#ff7fff','#7fffff','#000000','#bf7fff','#7fffbf','#ffbf7f','#808080'];

	var result     = {};
	var data       = {};
	var colorKeys  = {};
	var colorIndex = 0;
	// id毎にデータを取得
	result.x   = []; // Xラベル
	result.map = []; // チャートデータ
	for(var index = 0; index < ids.length; index ++){
		var w = ids[index].split("/");
		var samplesource = w[0];
		var sampletype   = w[1];
		var lipidclass   = w[2];

		result.map[index]     = {}; // 通常チャート用
		result.map[index].pie = []; // パイチャート用
		var pieKeys = {};
		var pieIndex = 0;
		for(key in gAllData){ // sample source
			if(samplesource != key)
				continue;

			for(var j = 0; j < gAllData[key].length; j ++){ // sample name
				if(sampletype != gAllData[key][j]["Sample name"])
					continue;

				// 同一のLipidSuperClassのみ対象とする
				// 通常チャートは同一LipidClassのみだが、パイチャートにおいて意味を持つ
				var targetLipidSuperClass = undefined;
				for(var k = 0; k < gAllData[key][j]["data"].length; k ++){
					if(gAllData[key][j]["data"][k]["Lipid class"] == lipidclass){
						targetLipidSuperClass = gAllData[key][j]["data"][k]["Lipid super class"];
						break;
					}
				}

				// 各lipid classのチェック
				for(var k = 0; k < gAllData[key][j]["data"].length; k ++){
					if(gAllData[key][j]["data"][k]["Lipid super class"] != targetLipidSuperClass) // superクラスが違うものは対象外
						continue;

					var lc = gAllData[key][j]["data"][k]["Lipid class"];
					// lipid classごとに色を変える
					if(colorKeys[lc] == undefined){
						colorKeys[lc] = colorIndex++;
					}
					// パイチャート用データ
					if(pieKeys[lc] == undefined){
						pieKeys[lc] = pieIndex++;
						result.map[index].pie[pieKeys[lc]] = {};
						result.map[index].pie[pieKeys[lc]].name = lc; // type
						result.map[index].pie[pieKeys[lc]].color = pieColors[colorKeys[lc]%pieColors.length];
						result.map[index].pie[pieKeys[lc]].y = 0;
					}
					result.map[index].pie[pieKeys[lc]].y += parseFloat(gAllData[key][j]["data"][k]["Intensity"]); // intensity

					if(lc == lipidclass){ // lipid classが対象のものなら、通常チャート用にデータを格納する
						result.map[index].title = samplesource + "/" + sampletype + "/" + lipidclass;
						result.map[index].meta  = {};
						for(meta in gAllData[key][j]){// 全メタ情報
							if(meta != "data")
								result.map[index].meta[meta] = gAllData[key][j][meta];
						}

						var lipidType = getLipidType(gAllData[key][j]["data"][k]); // chain文字列を取得する
						// すべてのデータでX軸を統一するため、いったんdata配列にデータを格納する
						if(data[lipidType] == undefined){ // まだ出てきていないchainならばX軸ラベルに追加する
							data[lipidType] = [];
							result.x.push(lipidType);
						}
						data[lipidType][index] = gAllData[key][j]["data"][k];

						//Logger.log(filename + "/" + tabname + "/" + type);
					}
				}
			}
		}
	}

	// chainの小さい順に並べ替える
	result.x.sort(function(a1, a2){
		// chainの数による比較
		var lipidClass1 = a1.split("/");
		var lipidClass2 = a2.split("/");
		if(lipidClass1.length < lipidClass2.length) return -1;
		if(lipidClass1.length > lipidClass2.length) return 1;
		// chainの長さと二重結合数による比較
		for(var i = 0; i < lipidClass1.length; i ++){
			var lc1 = parseInt(lipidClass1[i].replace(/:/, ""));
			var lc2 = parseInt(lipidClass2[i].replace(/:/, ""));
			if(lc1 < lc2) return -1;
			if(lc1 > lc2) return 1;
		}
		return 0;
	});

	// データを整形
	var maxOfAll = 0;
	for(var index = 0; index < ids.length; index ++){
		result.map[index].data = [];
		var max = 0;
		for(var j = 0; j < result.x.length; j ++){
			result.map[index].data[j] = [];

			if(data[result.x[j]][index] == undefined){ // このデータには、対象chainの情報がないため、ダミーを格納しておく
				data[result.x[j]][index] = {
					"Intensity":"0",
					"Formula"  :"",
					"InChIKey" :"",
					"SMILES"   :"",
					"RT[min]"  :"",
					"m/z"      :"",
					"Adduct"   :"",
					"TI"       :"",
					"MI"       :"",
					"MSMS"     :""
				};
			}

			// Intensityの処理
			var inte = parseFloat(data[result.x[j]][index]["Intensity"]);
			if(inte > max)
				max = inte;
			result.map[index].data[j].push(inte);

			// メタ情報の格納
			result.map[index].data[j].meta = {};
			result.map[index].data[j].meta["Formula"]  = data[result.x[j]][index]["Formula"];
			result.map[index].data[j].meta["InChIKey"] = data[result.x[j]][index]["InChIKey"];
			result.map[index].data[j].meta["SMILES"]   = data[result.x[j]][index]["SMILES"];
			result.map[index].data[j].meta["RT[min]"]  = data[result.x[j]][index]["RT[min]"];
			result.map[index].data[j].meta["m/z"]      = data[result.x[j]][index]["m/z"];
			result.map[index].data[j].meta["Adduct"]   = data[result.x[j]][index]["Adduct"];
			result.map[index].data[j].meta["TI"]       = data[result.x[j]][index]["TI"];
			result.map[index].data[j].meta["MI"]       = data[result.x[j]][index]["MI"];
			result.map[index].data[j].meta["MSMS"]     = data[result.x[j]][index]["MSMS"];
		}
		for(var j = 0; j < result.x.length; j ++) // 意味ある?
			result.map[index].data[j][0] = result.map[index].data[j][0];

		// 全体での最大Intensityを見つけ出す
		result.map[index].max = max;
		if(maxOfAll < max)
			maxOfAll = max;
	}

	result.maxOfAll = maxOfAll;

	result.title = lipidclass;
	// アルキル、ジアシル用に配列にしておく
	// -> 完全に別々になったので、無意味に
	var array = [];
	array.push(result);

	return array;
}
// 文字列を正規表現文字列に変換する ////////////////////////////////////////
// param str 対象文字列
// return 正規表現用文字列
function translateForReg(str)
{
	// ()はエスケープする
	str = str.replace("(","\\(","g");
	str = str.replace(")","\\)","g");

	// Allはすべてが対象
	if(str == "All" || str.length == 0)
		str = ".*";
	else
		str = "^" + str + "$";

	return str;
}
// chainグラフ用データの取得 ///////////////////////////////////////////////
// param samplename - Sample Name
// param samplesource - Sample Source
// param sampletype - Sample Type
// param lipidclass - Lipid class
// param chain - Lipid chain
// return 対象データ
function retrieveChainData(samplename, samplesource, sampletype, lipidclass, chain)
{
	// sample sourceとsample typeはAllがあるため、正規表現を用いてマッチングを行う
//	var ssReg = new RegExp(translateForReg(samplesource));
//	var stReg = new RegExp(translateForReg(sampletype));
	var snReg = new RegExp(translateForReg(samplename), "i");

	var result       = {};
	var maxOfAll     = 0;
	var index        = 0;
	var legends      = {};
	if(chain.indexOf("/") > 0)
		result.title     = lipidclass + "(" + chain + ")";
	else
		result.title     = lipidclass + " " + chain;
	result.lipidclass = lipidclass;
	result.chain      = chain;
	result.x         = [];
	result.ionmode   = [];
	result.intensity = [];
	result.adduct    = [];
	quantification   = "";
	for(key in gAllData){
//		if(!ssReg.test(key)) // sample sourceが合致するか
//			continue;

		for(var j = 0; j < gAllData[key].length; j ++){
//			if(!stReg.test(gAllData[key][j]["Sample name"])) // sample typeが合致するか
//				continue;
			if(!snReg.test(gAllData[key][j]["Sample source"])) // samplenameが合致するか
				continue;

			// 特定のlipid class, lipid chainのみを対象とする
			for(var k = 0; k < gAllData[key][j]["data"].length; k ++){
				if(gAllData[key][j]["data"][k]["Lipid class"] != lipidclass)
					continue;

				if(!gAllData[key][j]["data"][k]["Lipid super class"]) // 空データ
					continue;

				var lipidType = getLipidType(gAllData[key][j]["data"][k]);
//				var legend = key + "/" + gAllData[key][j]["Sample name"];
				var legend;
				if(gAllData[key][j]["Sample source"] == "Human")
					legend = "Human plasma";
				else if(gAllData[key][j]["Sample source"] == "Mouse")
					legend = gAllData[key][j]["Tissue"];// + "_" + gAllData[key][j]["Sample type"] + "_" + gAllData[key][j]["Treatment"];
				else if(gAllData[key][j]["Sample source"] == "Cell")
					legend = gAllData[key][j]["Sample type"];
				else
					legend = key + "/" + gAllData[key][j]["Sample name"];

				if(!legends[legend] && lipidType == chain){ // legend名が複数回被る可能性があるので、すでに格納済みのlegendは処理しない
					// 対象,,m
					result.x[index]         = legend;
					result.ionmode[index]   = gAllData[key][j]["Ion mode"];
					result.adduct[index]    = gAllData[key][j]["data"][k]["Adduct"];
					result.intensity[index] = parseFloat(gAllData[key][j]["data"][k]["Intensity"]); // intensity

					if(quantification.length == 0)
						quantification = gAllData[key][j]["Quantification"];
					else if(quantification != gAllData[key][j]["Quantification"])
						quantification = "?";

					if(maxOfAll < result.intensity[index]) // 全体での最大Intensityを調べる
						maxOfAll = result.intensity[index];
					index ++;
					legends[legend] = true;
				}
			}
		}
	}

	result.maxOfAll       = maxOfAll;
	result.quantification = quantification;

	return result;
}
// lipid chainを構築して返す。chain情報はSN1~SN4まで分かれており、さらにSN1が空の場合はTotal chainを使用する ///
// param array - 列データ
// return lipid chain文字列。16:0, 16:0/16:0など
function getLipidType(array)
{
	// SN1からSN4までのchainを連結させる
	var lipidType = array["SN1 chain"];
	for(var l = 2; l <= 4; l ++){
		if(array["SN" + l + " chain"].length > 0)
			lipidType += "/" + array["SN" + l + " chain"];
		else
			break;
	}
	// SN1が空だった場合はTotalChainを使用する
	if(lipidType.length == 0)
		lipidType = array["Total chain"];
	return lipidType;
}
Personal tools
Namespaces

Variants
Actions
Navigation
metabolites
Toolbox