<commit_before>//		a spectrum viewer
//
//      Copyright  2015 Rappsilber Laboratory, Edinburgh University
//
// 		Licensed under the Apache License, Version 2.0 (the "License");
// 		you may not use this file except in compliance with the License.
// 		You may obtain a copy of the License at
//
// 		http://www.apache.org/licenses/LICENSE-2.0
//
//   	Unless required by applicable law or agreed to in writing, software
//   	distributed under the License is distributed on an "AS IS" BASIS,
//   	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   	See the License for the specific language governing permissions and
//   	limitations under the License.
//
//		authors: <NAME>, <NAME>
//
//		graph/Peak.js

function Peak (id, graph){
	var peak = graph.model.JSONdata.peaks[id];

	this.x = peak.mz;
	this.y = peak.intensity;
	this.IsotopeClusters = [];
	this.labels = [];
	for (i=0; i<peak.clusterIds.length; i++)
		this.IsotopeClusters.push(graph.model.JSONdata.clusters[peak.clusterIds[i]]);
	if (this.IsotopeClusters.length > 0)
		this.charge = this.IsotopeClusters[0].charge;	//TODO charge multiple clusters?
	this.graph = graph;

	//make fragments
	var notLossyFragments = [];
	var lossyFragments = [];
	this.isMonoisotopic = false;
	for (var i = 0; i < this.IsotopeClusters.length; i++) {
		this.isotope = id - this.IsotopeClusters[i].firstPeakId;
		if (this.isotope == 0)
			this.isMonoisotopic = true;
	};

	var fragments = graph.model.fragments;

	for (var f = 0; f < fragments.length; f++) {
		if(_.intersection(fragments[f].clusterIds, peak.clusterIds).length != 0){
			if(fragments[f].class == "lossy")
				lossyFragments.push(fragments[f]);
			else
				notLossyFragments.push(fragments[f]);
		}				
	};

	this.fragments = notLossyFragments.concat(lossyFragments); //merge arrays
/*
	if (this.IsotopeClusters.length > 0){
		var fragments = graph.model.JSONdata.fragments;
		for(i=0; i<fragments.length; i++){
			for(j=0; j<this.IsotopeClusters.length; j++){
				if(_.contains(fragments[i].clusterIds, this.IsotopeClusters[j].id)){
					//var frag = new Fragment (fragments[i]);
					if(fragments[i].class == "lossy")
						lossyFragments.push(fragments[i]);
					else
						notLossyFragments.push(fragments[i]);
				}
			}
		}
	};

	this.fragments = notLossyFragments.concat(lossyFragments); //merge arrays*/

	//error
	for (var i = 0; i < this.fragments.length; i++) {
		this.error = this.fragments[i].clusterInfo[0].error.toFixed(2)+" "+this.fragments[i].clusterInfo[0].errorUnit;
	};

	//make tooltip
	var tooltip = "";
	var fragCount = this.fragments.length;
	for (var f = 0; f < fragCount; f++){
		if (f > 0) {
			tooltip += ", ";
		};
		tooltip += this.fragments[f].sequence;
	};

	this.tooltip = tooltip + " m/z: " + this.x + ", i: " + this.y + ", charge: " + this.charge + ", error: "+ this.error;

	//svg elements
	this.g = this.graph.peaks.append('g');
	this.g.append("svg:title").text(this.tooltip);	// easy tooltip
	if (this.fragments.length > 0) {
		this.highlightLine = this.g.append('line')
							.attr("stroke", this.graph.model.highlightColour)
							.attr("stroke-width", this.graph.model.highlightWidth)
							.attr("opacity","0");
		this.highlightLine.attr("x1", 0);
		this.highlightLine.attr("x2", 0);
				
		//set the dom events for it
		var self = this;
		var group = this.g[0][0];
		group.onmouseover = function(evt) {
			startHighlight();
		};
		group.onmouseout = function(evt) {
			endHighlight();
		};
		group.ontouchstart = function(evt) {
			startHighlight();
		};
		group.ontouchend = function(evt) {
			endHighlight();
		};
		group.onclick = function(evt){
			stickyHighlight(evt.ctrlKey);
		}

		function startHighlight(fragId){
			var fragments = [];
			if(fragId){
				for (var i = 0; i < self.fragments.length; i++) {
					if(self.fragments[i].id == parseInt(fragId))
						fragments.push(self.fragments[i]);	
				};
			}
			else	
				fragments = self.fragments;
			self.graph.model.addHighlight(fragments);	
		}
		function endHighlight(){
			self.graph.model.clearHighlight(self.fragments);	
		}
		function stickyHighlight(ctrl, fragId){

			var fragments = [];
			if(fragId){
				for (var i = 0; i < self.fragments.length; i++) {
					if(self.fragments[i].id == parseInt(fragId))
						fragments.push(self.fragments[i]);	
				};
			}
			else	
				fragments = self.fragments;
			self.graph.model.updateStickyHighlight(fragments, ctrl);
		}

		if (this.isMonoisotopic){
		  	//create frag labels
		  	//labeldrag	
			this.labelDrag = d3.behavior.drag();
			this.labelDrag.on("dragstart", function(){<commit_msg>new tooltip<commit_after>//		a spectrum viewer
//
//      Copyright  2015 Rappsilber Laboratory, Edinburgh University
//
// 		Licensed under the Apache License, Version 2.0 (the "License");
// 		you may not use this file except in compliance with the License.
// 		You may obtain a copy of the License at
//
// 		http://www.apache.org/licenses/LICENSE-2.0
//
//   	Unless required by applicable law or agreed to in writing, software
//   	distributed under the License is distributed on an "AS IS" BASIS,
//   	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   	See the License for the specific language governing permissions and
//   	limitations under the License.
//
//		authors: <NAME>, <NAME>
//
//		graph/Peak.js

function Peak (id, graph){
	var peak = graph.model.JSONdata.peaks[id];

	this.x = peak.mz;
	this.y = peak.intensity;
	this.IsotopeClusters = [];
	this.labels = [];
	for (i=0; i<peak.clusterIds.length; i++)
		this.IsotopeClusters.push(graph.model.JSONdata.clusters[peak.clusterIds[i]]);
	if (this.IsotopeClusters.length > 0)
		this.charge = this.IsotopeClusters[0].charge;	//TODO charge multiple clusters?
	this.graph = graph;

	//make fragments
	var notLossyFragments = [];
	var lossyFragments = [];
	this.isMonoisotopic = false;
	for (var i = 0; i < this.IsotopeClusters.length; i++) {
		this.isotope = id - this.IsotopeClusters[i].firstPeakId;
		if (this.isotope == 0)
			this.isMonoisotopic = true;
	};

	var fragments = graph.model.fragments;

	for (var f = 0; f < fragments.length; f++) {
		if(_.intersection(fragments[f].clusterIds, peak.clusterIds).length != 0){
			if(fragments[f].class == "lossy")
				lossyFragments.push(fragments[f]);
			else
				notLossyFragments.push(fragments[f]);
		}				
	};

	this.fragments = notLossyFragments.concat(lossyFragments); //merge arrays
/*
	if (this.IsotopeClusters.length > 0){
		var fragments = graph.model.JSONdata.fragments;
		for(i=0; i<fragments.length; i++){
			for(j=0; j<this.IsotopeClusters.length; j++){
				if(_.contains(fragments[i].clusterIds, this.IsotopeClusters[j].id)){
					//var frag = new Fragment (fragments[i]);
					if(fragments[i].class == "lossy")
						lossyFragments.push(fragments[i]);
					else
						notLossyFragments.push(fragments[i]);
				}
			}
		}
	};

	this.fragments = notLossyFragments.concat(lossyFragments); //merge arrays*/


	//make tooltip
	var tooltip = "";
	var fragCount = this.fragments.length;
	for (var f = 0; f < fragCount; f++){
		if (f > 0) {
			tooltip += "<br/>";
		};
		charge = graph.model.JSONdata.clusters[this.fragments[f].clusterIds[0]].charge;
		error = this.fragments[f].clusterInfo[0].error.toFixed(2)+" "+this.fragments[f].clusterInfo[0].errorUnit;
		tooltip += this.fragments[f].name + " (" + this.fragments[f].sequence + ") - " + "charge: " + charge + ", error: " + error;
	};

	this.tooltip = " m/z: " + this.x + ", i: " + this.y + "<p>" + tooltip + "</p>";

	//svg elements
	this.g = this.graph.peaks.append('g');
	//this.g.append("svg:title").text(this.tooltip);	// easy tooltip

	//new tooltip
	this.tip = d3.select("body").append("div")   
	    .attr("class", "tooltip")
	    .style("position", "absolute")               
	    .style("opacity", 0)
	    .style("font-size", "0.8em")
	    .html(this.tooltip);
	 //end

	if (this.fragments.length > 0) {
		this.highlightLine = this.g.append('line')
							.attr("stroke", this.graph.model.highlightColour)
							.attr("stroke-width", this.graph.model.highlightWidth)
							.attr("opacity","0");
		this.highlightLine.attr("x1", 0);
		this.highlightLine.attr("x2", 0);
				
		//set the dom events for it
		var self = this;
		var group = this.g[0][0];
		group.onmouseover = function(evt) {
			showTooltip(evt.clientX, evt.clientY);
			startHighlight();
		};
		group.onmouseout = function(evt) {
			hideTooltip();
			endHighlight();
		};
		group.ontouchstart = function(evt) {
			showTooltip(evt.clientX, evt.clientY);
			startHighlight();
		};
		group.ontouchend = function(evt) {
			hideTooltip();
			endHighlight();
		};
		group.onclick = function(evt){
			stickyHighlight(evt.ctrlKey);
		}

		function showTooltip(x,y){
			self.tip.style("opacity", 1)
					.style("left", (x+10) + "px")
					.style("top", (y - 28) + "px"); 
		}
		function hideTooltip(){
			self.tip.style("opacity", 0);
		}
		function startHighlight(fragId){
			var fragments = [];
			if(fragId){
				for (var i = 0; i < self.fragments.length; i++) {
					if(self.fragments[i].id == parseInt(fragId))
						fragments.push(self.fragments[i]);	
				};
			}
			else	
				fragments = self.fragments;
			self.graph.model.addHighlight(fragments);	
		}
		function endHighlight(){
			self.tip.style("opacity", 0)
			self.graph.model.clearHighlight(self.fragments);	
		}
		function stickyHighlight(ctrl, fragId){

			var fragments = [];
			if(fragId){
				for (var i = 0; i < self.fragments.length; i++) {
					if(self.fragments[i].id == parseInt(fragId))
						fragments.push(self.fragments[i]);	
				};
			}
			else	
				fragments = self.fragments;
			self.graph.model.updateStickyHighlight(fragments, ctrl);
		}

		if (this.isMonoisotopic){
		  	//create frag labels
		  	//labeldrag	
			this.labelDrag = d3.behavior.drag();
			this.labelDrag.on("dragstart", function(){
{'fraction_non_alphanumeric': 0.1641260162601626, 'fraction_numerical': 0.008231707317073171, 'mean_word_length': 3.3162280701754385, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7084608', 'n_tokens_mistral': 4007, 'n_tokens_neox': 3481, 'n_words': 707}