/
main.html_part3
206 lines (170 loc) · 10.1 KB
/
main.html_part3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
for(var hostIdx=0; hostIdx<hostNameArr.length; hostIdx++) hostNameIdxMap[hostNameArr[hostIdx]] = hostIdx;
for(var virusIdx=0; virusIdx<virusNameArr.length; virusIdx++) virusNameIdxMap[virusNameArr[virusIdx]] = virusIdx;
console.log("phageToHost finish loading!");
topHostNum = Math.min(25, hostNameArr.length);
$(function() { $( "#topSlider" ).slider({ value:25, min: 1, max: 100, step: 1, slide: function( event, ui ) { $( "#topAmount" ).val( ui.value ); topHostNum = Math.min(ui.value, hostNameArr.length); } }); });
$(function() { $( "#cutoffSlider" ).slider({ value:1, min: 0, max: 1, step: 0.01, slide: function( event, ui ) { $( "#cutoffAmount" ).val( ui.value ); cutoff = ui.value; } }); });
$("#listbox").jqxListBox({width: 290, height: 300, source: virusNameArr, checkboxes: true, filterable: true});
$("#listbox").on('checkChange', function (event) {
var items = $("#listbox").jqxListBox('getCheckedItems');
checkedVirusIdxArr = []; checkedVirusArr = [];
$.each(items, function (index) { checkedVirusIdxArr.push(this.index); checkedVirusArr.push(this.label); });
if(checkedVirusIdxArr.length > 0) $("#okBtn").prop('disabled', false);
else $("#okBtn").prop('disabled', true);
});
var host2organism = [], host2superkingdom = [], host2phylum = [], host2class = [],
host2order = [], host2family = [], host2genus = [], host2species = [];
for(var t=0; t<hostNameArr.length; t++){
host2organism[hostNameArr[t]] = organismArr[t];
host2superkingdom[hostNameArr[t]] = superkingdomArr[t];
host2phylum[hostNameArr[t]] = phylumArr[t];
host2class[hostNameArr[t]] = classArr[t];
host2order[hostNameArr[t]] = orderArr[t];
host2family[hostNameArr[t]] = familyArr[t];
host2genus[hostNameArr[t]] = genusArr[t];
host2species[hostNameArr[t]] = speciesArr[t];
}
console.log("hostTaxonomy finish loading!");
var minDist = 1, maxDist = 0, gap=0;
for(var rowIdx=0; rowIdx<virusHostMat.length; rowIdx++)
{
for(var colIdx=0; colIdx<virusHostMat[0].length; colIdx++)
{
if(virusHostMat[rowIdx][colIdx]<minDist) minDist = virusHostMat[rowIdx][colIdx];
if(virusHostMat[rowIdx][colIdx]>maxDist) maxDist = virusHostMat[rowIdx][colIdx];
}
}
minDist = Math.max(0,minDist-0.1); maxDist = Math.min(1,maxDist+0.1); gap = maxDist-minDist;
var colorScale = d3.scale.quantile().domain([minDist,maxDist]).range(colors);
function updateData() {
topHostNum = Math.min(topHostNum, hostNameArr.length);
var combinedArr = new Array(virusHostMat[checkedVirusIdxArr[0]].length).fill(0);
for(var rowIdx=0; rowIdx<checkedVirusIdxArr.length; rowIdx++) {
for(var colIdx=0; colIdx<virusHostMat[checkedVirusIdxArr[rowIdx]].length; colIdx++) {
combinedArr[colIdx] += virusHostMat[checkedVirusIdxArr[rowIdx]][colIdx];
}
}
var tmpArr = combinedArr.slice();
var thres = tmpArr.sort().slice(topHostNum-1,topHostNum);
indiceArr = [];
for(var colIdx=0; colIdx<combinedArr.length; colIdx++) {
if(indiceArr.length >= topHostNum) break;
if(combinedArr[colIdx] <= thres) { indiceArr.push({"idx":colIdx, "dist":combinedArr[colIdx] }); }
}
hostArr = []; edgeData =[];
for(var colIdx=0; colIdx<indiceArr.length; colIdx++) {
var realColIdx = indiceArr[colIdx].idx;
hostArr.push(hostNameArr[realColIdx]);
for(var rowIdx=0; rowIdx<checkedVirusIdxArr.length; rowIdx++) { edgeData.push({"row":rowIdx, "col":hostArr.length-1,
"value":virusHostMat[checkedVirusIdxArr[rowIdx]][realColIdx]<=cutoff?virusHostMat[checkedVirusIdxArr[rowIdx]][realColIdx]:1}); }
}
pathArr = [];
for(var colIdx=0; colIdx<hostArr.length; colIdx++) { pathArr.push([host2superkingdom[hostArr[colIdx]],host2phylum[hostArr[colIdx]],host2class[hostArr[colIdx]],host2order[hostArr[colIdx]],host2family[hostArr[colIdx]],host2genus[hostArr[colIdx]],host2species[hostArr[colIdx]]]); }
//rootNode = convertToHierarchy(pathArr);
//consensusArr = getConsensusTaxonomy(rootNode);
$("#consensusSuperkingdom").text("Superkingdom: "+getConsensusItem(pathArr,0));
$("#consensusPhylum").text("Phylum: "+getConsensusItem(pathArr,1));
$("#consensusClass").text("Class: "+getConsensusItem(pathArr,2));
$("#consensusOrder").text("Order: "+getConsensusItem(pathArr,3));
$("#consensusFamily").text("Family: "+getConsensusItem(pathArr,4));
$("#consensusGenus").text("Genus: "+getConsensusItem(pathArr,5));
$("#consensusSpecies").text("Species: "+getConsensusItem(pathArr,6));
var cellSize = 25, legendElementWidth = cellSize*2,
width = Math.max(hostArr.length*cellSize, 1000 ), height = Math.max(checkedVirusIdxArr.length*cellSize, 700 );
console.log();
d3.selectAll(".canvasSvg").remove();
var svg = d3.select("#heatCanvas").style("float", "left").attr("width", width).attr("height", height)
.append("g").attr('class', "canvasSvg").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var rowLabels = svg.append("g").selectAll(".rowLabelg").data(checkedVirusArr).enter().append("text")
.text(function (d) { return d; }).attr("x", 0).attr("y", function (d, i) { return i * cellSize; })
.style("text-anchor", "end").attr("transform", "translate(-6," + cellSize / 1.5 + ")")
.attr("class", function (d,i) { return "rowLabel mono r"+i;} ) ;
var colLabels = svg.append("g").selectAll(".colLabelg").data(hostArr).enter().append("text")
.text(function (d) { return d; }).attr("x", 0).attr("y", function (d, i) { return i * cellSize; })
.style("text-anchor", "left").attr("transform", "translate("+cellSize/2 + ",-6) rotate (-90)")
.attr("class", function (d,i) { return "colLabel mono c"+i;} );
var heatMap = svg.append("g").attr("class","g3").selectAll(".cellg").data(edgeData).enter().append("rect")
.attr("x", function(d) { return d.col * cellSize; }).attr("y", function(d) { return d.row * cellSize; })
.attr("class", function(d){return "cell cell-border cr"+(d.row)+" cc"+(d.col);})
.attr("width", cellSize).attr("height", cellSize).style("fill", function(d) { return colorScale(d.value); })
.on("mouseover", function(d){
d3.selectAll(".cell").filter(function(el) {return d.row == el.row || d.col == el.col; }).classed("cell-hover",true);
d3.selectAll(".rowLabel").classed("text-highlight",function(r,ri){ return ri==d.row;});
d3.selectAll(".colLabel").classed("text-highlight",function(c,ci){ return ci==d.col;});
d3.select("#tooltip1").style("left", (d3.event.pageX+10) + "px").style("top", (d3.event.pageY-10) + "px")
.html("Host Information: "+
"<br> Superkingdom: "+ host2superkingdom[hostArr[d.col]] +
"<br> Phylum: "+ host2phylum[hostArr[d.col]] +
"<br> Class: "+ host2class[hostArr[d.col]] +
"<br> Order: "+ host2order[hostArr[d.col]] +
"<br> Family: "+ host2family[hostArr[d.col]] +
"<br> Genus: "+ host2genus[hostArr[d.col]] +
"<br> Species: "+ host2species[hostArr[d.col]] +
"<br> Organism: "+ host2organism[hostArr[d.col]] +
"<br> distance:"+d.value);
d3.select("#tooltip1").classed("hidden", false);
})
.on("mouseout", function(d){
d3.selectAll(".cell").filter(function(el) {return d.row == el.row || d.col == el.col; }).classed("cell-hover",false);
d3.selectAll(".rowLabel").classed("text-highlight",false);
d3.selectAll(".colLabel").classed("text-highlight",false);
d3.select("#tooltip1").classed("hidden", true);
});
var legend = svg.selectAll(".legend").data([minDist,minDist+0.1*gap,minDist+0.2*gap,minDist+0.3*gap,minDist+0.4*gap,minDist+0.5*gap,minDist+0.6*gap,minDist+0.7*gap,minDist+0.8*gap,minDist+0.9*gap,maxDist]).enter().append("g").attr("class", "legend");
legend.append("rect").attr("x", function(d, i) { return legendElementWidth * i-margin.left; }).attr("y", (cellSize*2) - margin.top)
.attr("width", legendElementWidth).attr("height", cellSize)
.style("fill", function(d, i) { return colors[i]; });
legend.append("text").text(function(d) { return d3.format(".2f")(d); }).style("text-anchor", "left")
.attr("x", function(d, i) { return legendElementWidth * i-margin.left; }).attr("y", (cellSize*4)- margin.top);
}
function getConsensusItem(pathArr, idx)
{
var node = {id:"root", cnt:0, children:{}};
for (var i = 0; i < pathArr.length; i++)
{
var path = pathArr[i];
node.cnt ++;
item = path[idx];
if (!node.children[item]) node.children[item] = {id:item, cnt:0, children:{}};
node.children[item].cnt ++;
}
var arr = Object.keys( node.children ).map(function ( key ) { return node.children[key].cnt; });
var ratio = 100;
if(math.sum(arr) > 0) ratio = Math.max.apply(Math,arr)*100/math.sum(arr);
maxItem = Object.keys(node.children)[arr.indexOf(Math.max.apply(Math,arr))];
return node.children[maxItem].id+" ("+String(Math.round(ratio * 100) / 100)+"%)";
}
function convertToHierarchy(pathArr) {
var rootNode = {id:"root", cnt:0, children:{}};
for (var i = 0; i < pathArr.length; i++) {
var path = pathArr[i];
buildNodeRecursive(rootNode, path, 0);
}
return rootNode;
}
function buildNodeRecursive(node, path, idx) {
if (idx < path.length) {
node.cnt ++;
item = path[idx];
if (!node.children[item]) node.children[item] = {id:item, cnt:0, children:{}};
buildNodeRecursive(node.children[item], path, idx + 1);
}
}
function getConsensusTaxonomy(currNode) {
if(currNode.children && Object.keys(currNode.children).length > 0) {
var arr = Object.keys( currNode.children ).map(function ( key ) { return currNode.children[key].cnt; });
//console.log(arr + " : " +arr.length);
//console.log(arr + " : " +Math.max.apply(Math,arr) +" : " + arr.indexOf(Math.max.apply(Math,arr)));
//console.log(Math.max.apply(Math,arr) + " / " +math.sum(arr));
var ratio = 100;
if(math.sum(arr) > 0) ratio = Math.max.apply(Math,arr)*100/math.sum(arr);
maxItem = Object.keys(currNode.children)[arr.indexOf(Math.max.apply(Math,arr))];
returnArr = [currNode.children[maxItem].id+" ("+String(Math.round(ratio * 100) / 100)+"%)"];
returnArr = returnArr.concat(getConsensusTaxonomy(currNode.children[maxItem]));
return returnArr;
}
return [currNode.id+" (100%)"];
}
</script>
</body>
</html>