-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathortholog.js
More file actions
588 lines (520 loc) · 17.6 KB
/
ortholog.js
File metadata and controls
588 lines (520 loc) · 17.6 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
let comparedTaxa = [];
let dbConfig = {
orthodb: {
query: 'sparql/orthodb.rq',
endpoint: 'https://sparql.orthodb.org/sparql/'
},
oma: {
query: 'sparql/oma.rq',
endpoint: 'https://sparql.omabrowser.org/sparql/'
}
};
let srcDB = sessionStorage.getItem('srcDB') || localStorage.getItem('srcDB') || 'orthodb';
let showCellNumber = localStorage.getItem('showCellNumber') === 'true';
let horizontalOrder = localStorage.getItem('horizontalOrder') || 'tree';
let verticalOrder = localStorage.getItem('verticalOrder') || 'tree';
const urlParams = new URLSearchParams(window.location.search);
let taxaParam = [];
let proteinsParam = [];
let paramCount = 0;
for(let entry of urlParams.entries()) {
if(entry[0].startsWith('taxaUPIds')) {
taxaParam.push(entry[1]);
} else if(entry[0].startsWith('proteinUPIds')) {
proteinsParam.push(entry[1]);
}
if(entry[0] === 'srcDB') {
sessionStorage.setItem('srcDB', entry[1]);
}
for(let name of ['srcDB', 'showCellNumber', 'horizontalOrder', 'verticalOrder']) {
if(entry[0] === name) {
localStorage.setItem(name, entry[1]);
break;
}
}
++paramCount;
}
if(paramCount > 0) {
if(taxaParam.length > 0) {
selectedTaxa = {};
for (let taxonUPId of taxaParam) {
selectedTaxa[taxonUPId] = null;
}
localStorage.setObject('selectedTaxa', selectedTaxa);
}
if(proteinsParam.length > 0) {
selectedProteins = {};
for (let proteinUPId of proteinsParam) {
selectedProteins[proteinUPId] = null;
}
localStorage.setObject('selectedProteins', selectedProteins);
}
window.location.href = window.location.href.split('?')[0]; // Jump to URL without query parameter
}
selectedTaxa = localStorage.getObject('selectedTaxa') || {};
selectedProteins = localStorage.getObject('selectedProteins') || {};
let baseTaxon = {
genome_taxid: "9606",
up_id_url: "http://purl.uniprot.org/proteomes/UP000005640",
up_id: "UP000005640",
types: "http://purl.uniprot.org/core/Proteome, http://purl.uniprot.org/core/Reference_Proteome, http://purl.uniprot.org/core/Representative_Proteome",
organism_name: "Homo sapiens (Human)",
displayedName: "Human",
n_genes: 20596,
n_isoforms: 76225,
cpd_label: "Outlier",
busco_complete: "6163",
busco_single: "2364",
busco_multi: "3799",
busco_fragmented: "12",
busco_missing: "17",
assembly: "GCA_000001405.27"
};
let proteins = [];
let tooltips = {};
let tooltipMap = {};
let series = null;
let taxonTree = null;
let humanNode = null;
let paraNumMap = null;
let maxParalogNum = 0;
let mapNameToTaxa = {}, mapTaxIdToTaxa = {},
mapMnemonicToProteins = {};
mapDisplayedNameToProtein = {};
mapNameToTaxa[baseTaxon.displayedName] = baseTaxon;
mapTaxIdToTaxa[baseTaxon.genome_taxid] = baseTaxon;
Storage.prototype.getObject = function (key) {
let val = this.getItem(key);
return val && JSON.parse(val) || {};
}
function UpdateChart() {
tooltips = {};
series = null;
updateSelectedCount();
mapNameToTaxa = {};
mapTaxIdToTaxa = {};
comparedTaxa = Object.values(selectedTaxa);
for(let taxon of comparedTaxa) {
if(taxon.organism_name.includes("(")) {
let matched = taxon.organism_name.match(/^[^\(]+\(([^\)]+)\)/);
if(matched)
taxon.displayedName = matched[1];
}
taxon.displayedName = taxon.displayedName || taxon.organism_name;
mapNameToTaxa[taxon.displayedName] = taxon;
mapTaxIdToTaxa[taxon.genome_taxid] = taxon;
}
proteins = Object.values(selectedProteins);
mapMnemonicToProteins = {};
for(let protein of proteins) {
if (!mapMnemonicToProteins[protein.mnemonic])
mapMnemonicToProteins[protein.mnemonic] = [];
mapMnemonicToProteins[protein.mnemonic].push(protein);
}
maxParalogNum = 0;
mapDisplayedNameToProtein = {};
mapNameToTaxa[baseTaxon.displayedName] = baseTaxon;
mapTaxIdToTaxa[baseTaxon.genome_taxid] = baseTaxon;
$('#heatmap').hide();
$('#loader-container').show();
proteins.sort((protein1, protein2) => protein1.mnemonic < protein2.mnemonic ? -1 : 1);
show_proteins(proteins);
if(proteins.length === 0 || comparedTaxa.length === 0) {
$('#loader-container').hide();
$('#heatmap').show();
$('#heatmap')[0].innerText = "Select more than one proteomes and proteins to see orthologs.";
return;
}
// Assign unique displayed name to avoid conflict
for (let [mnemonic, proteins] of Object.entries(mapMnemonicToProteins)) {
if (proteins.length === 1) {
proteins[0].displayedName = mnemonic;
mapDisplayedNameToProtein[mnemonic] = proteins[0];
} else {
for (let protein of proteins) {
protein.displayedName = `${mnemonic} (${protein.up_id})`;
mapDisplayedNameToProtein[protein.displayedName] = protein;
}
}
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
queryBySpang(dbConfig[srcDB].query, dbConfig[srcDB].endpoint, {
taxa: comparedTaxa.map((taxon) => 'upTax:' + taxon.genome_taxid).join(' '),
proteins: proteins.map((protein) => 'uniprot:' + protein.up_id).join(' ')
}, (result) => {
maxParalogNum = 0;
let taxIdList = [baseTaxon.genome_taxid].concat(comparedTaxa.map((taxon) => taxon.genome_taxid));
let taxonProtMap = {};
for (let taxon of taxIdList)
taxonProtMap[taxon] = {};
for (let prot of proteins.map((prot) => prot.up_id))
taxonProtMap[baseTaxon.genome_taxid][prot] = [prot];
for (let binding of result.results.bindings) {
let taxon = binding.taxid.value.replace(/.*\//, '');
let baseProt = binding.uniprot_human.value.replace(/.*\//, '');
let taxProt = binding.uniprot.value.replace(/.*\//, '');
if (!taxonProtMap[taxon][baseProt])
taxonProtMap[taxon][baseProt] = [taxProt];
else
taxonProtMap[taxon][baseProt].push(taxProt);
maxParalogNum = Math.max(maxParalogNum, taxonProtMap[taxon][baseProt].length);
}
series = taxIdList.map((taxId, i) => {
let protMap = taxonProtMap[taxId];
let data = proteins.map((protein, j) => {
let up_id = protein.up_id;
let paralogNum = protMap[up_id]?.length || 0;
return {
id: up_id,
x: protein.displayedName,
y: paralogNum,
tooltip: `${paralogNum}: ` + (protMap[up_id] ?? '')
};
});
return {
id: taxId,
name: mapTaxIdToTaxa[taxId].displayedName,
data,
cellNum: data.filter(datum => datum.y > 0).length,
sum: data.reduce((num, datum) => num + datum.y, 0),
};
});
paraNumMap = {};
tooltipMap = {};
for(let tax of series) {
paraNumMap[tax.id] = {};
tooltipMap[tax.id] = {};
for(let prot of tax.data) {
paraNumMap[tax.id][prot.id] = prot.y;
tooltipMap[tax.id][prot.id] = prot.tooltip;
}
}
series.sort((row1, row2) => row1.cellNum < row2.cellNum || (row1.cellNum == row2.cellNum) && row1.sum < row2.sum ? 1 : -1);
queryBySpang("sparql/taxonomy_tree.rq", endpoint, { taxids: taxIdList.join(" ") },(res) => {
[taxonTree, humanNode] = constructTree(res.results);
raiseNode(humanNode);
taxonTree = simplifyTree(taxonTree);
renderChart();
});
});
}
function constructDummyTree(elemList) {
if(elemList.length === 0)
return {};
let rootLeaf = {
id: elemList[0].id,
name: elemList[0].name,
children: [],
}
let rootNode = {
children: [rootLeaf],
};
let currentNode = rootNode;
for(let elem of elemList.slice(1)) {
let newLeaf = {
id: elem.id,
name: elem.name,
children: [],
}
let newNode = {
children: [newLeaf],
}
currentNode.children.push(newNode);
currentNode = newNode;
}
return rootNode;
}
/// Construct tree from result of taxonomy_tree.rq
function constructTree(result) {
let nodeMap = {};
let humanNode_ = null;
for (let row of result.bindings) {
let childId = row.taxon_int.value;
let parentId = row.parent_int.value;
if (!nodeMap[parentId]) {
nodeMap[parentId] = {
id: parentId,
name: row.parent_label.value,
children: [],
parent: null
};
}
if (!nodeMap[childId]) {
nodeMap[childId] = {
id: childId,
name: mapTaxIdToTaxa[childId]?.displayedName,
children: [],
parent: null
};
}
nodeMap[parentId].children.push(nodeMap[childId]);
nodeMap[childId].parent = nodeMap[parentId];
if (childId === '9606') {
humanNode_ = nodeMap[childId];
}
}
if (nodeMap.length === 0) {
return null;
}
let root = nodeMap[Object.keys(nodeMap)[0]];
while (root.parent) {
root = root.parent;
}
return [root, humanNode_];
}
/// Create simple tree for dendrogram that includes only branches and leaves
function simplifyTree(node) {
if(node.children.length === 1)
return simplifyTree(node.children[0]);
else {
let simplifiedChildren = [];
for(let child of node.children) {
simplifiedChildren.push(simplifyTree(child));
}
node.children = simplifiedChildren;
return node;
}
}
// Raise subtree including the specified node to be the first child for each parent node
function raiseNode(node) {
let parent = node.parent;
if(parent) {
if(parent.children.length > 0 && parent.children[0] !== node) {
let index = parent.children.indexOf(node);
if(index >= 0) {
parent.children[index] = parent.children[0];
parent.children[0] = node;
}
}
raiseNode(parent);
}
}
function orderedLeaves(tree) {
if(!tree.children || tree.children.length === 0)
return [tree];
else {
let nodes = [];
for(let child of tree.children) {
nodes = nodes.concat(orderedLeaves(child));
}
return nodes;
}
}
function renderChart() {
$('#loader-container').hide();
$('#heatmap').show();
let columnVectors = [];
for(let i = 0; i < series[0].data.length; i++) {
let values = series.map(elem => elem.data[i].y);
columnVectors.push({
val: values,
name: series[0].data[i].x,
id: series[0].data[i].id,
cellNum: values.filter((y) => y > 0).length,
sum: values.reduce((a, b) => a + b),
});
}
let dataForD3 = {};
if(horizontalOrder === 'cell') {
columnVectors.sort((col1, col2) => col1.cellNum < col2.cellNum || (col1.cellNum == col2.cellNum) && col1.sum < col2.sum ? 1 : -1);
}
else if(horizontalOrder === 'alpha') {
columnVectors.sort((col1, col2) => col1.name > col2.name ? 1 : -1);
}
if(horizontalOrder !== 'tree') {
dataForD3.colJSON = constructDummyTree(columnVectors);
} else {
let cluster = hcluster().distance('euclidean').linkage('avg').posKey('val').data(columnVectors);
dataForD3.colJSON = cluster.tree();
}
let orderedProteins = orderedLeaves(dataForD3.colJSON);
if(verticalOrder !== 'tree') {
dataForD3.rowJSON = constructDummyTree(series);
} else {
dataForD3.rowJSON = taxonTree;
}
let orderedTaxa = orderedLeaves(dataForD3.rowJSON);
let taxaForTable = [baseTaxon];
for(let taxon of orderedTaxa) {
if(taxon.id !== baseTaxon.genome_taxid && mapTaxIdToTaxa[taxon.id]) {
taxaForTable.push(mapTaxIdToTaxa[taxon.id])
}
}
showProteomeTable(taxaForTable);
matrix = [];
tooltips = {};
orderedTaxa.forEach((tax, j) => {
let row = [];
tooltips[j] = {};
orderedProteins.forEach((prot, i) => {
row.push( paraNumMap[tax.id][prot.id] );
tooltips[j][i] = tooltipMap[tax.id][prot.id];
});
matrix.push(row);
});
dataForD3.matrix = matrix;
d3.heatmapDendro(dataForD3, "#heatmap", horizontalOrder === 'tree', verticalOrder === 'tree', showCellNumber);
showDbpediaImage(comparedTaxa);
showDbpediaImage([baseTaxon]);
}
$(() => {
window.addEventListener('resize', (event) => {
renderChart();
}, true);
$('#h-order-select').val(horizontalOrder);
$('#v-order-select').val(verticalOrder);
$('#h-order-select').change((e) => {
horizontalOrder = e.target.value;
localStorage.setItem('horizontalOrder', horizontalOrder);
UpdateChart();
});
$('#v-order-select').change((e) => {
verticalOrder = e.target.value;
localStorage.setItem('verticalOrder', verticalOrder);
UpdateChart();
});
$('#database-select').on('change', (e) => {
srcDB = e.target.value;
sessionStorage.setItem('srcDB', srcDB);
localStorage.setItem('srcDB', srcDB);
UpdateChart();
}).val(srcDB);
$('#cell-label-checkbox').on('change', (e) => {
showCellNumber = e.target.checked;
localStorage.setItem('showCellNumber', showCellNumber);
renderChart();
}).prop('checked', showCellNumber);
$('#share-btn').click((e) => {
let url = window.location.href.split('?')[0];
url += '?';
if(Object.keys(selectedProteins).length > 0 && Object.keys(selectedTaxa).length > 0) {
url += Object.keys(selectedTaxa).map(upId => `taxaUPIds[]=${upId}`).join('&');
url += '&';
url += Object.keys(selectedProteins).map(upId => `proteinUPIds[]=${upId}`).join('&');
url += `&srcDB=${srcDB}`;
url += `&showCellNumber=${showCellNumber}`;
url += `&horizontalOrder=${horizontalOrder}`;
url += `&verticalOrder=${verticalOrder}`;
if(url.length > 7333) {
alert("The content is too large for sharing via URI (Current: " + url.length + " / Max: 7333).");
} else {
navigator.clipboard.writeText(url).then(function() {
alert("Current selection is now on clipboard!");
});
}
} else {
alert("Select taxons and proteins first!");
}
});
// TODO: No query is needed if all taxa and proteins are cached
queryBySpang(`sparql/get_proteome_statistics.rq`, null,
{ values: Object.keys(selectedTaxa).filter(up_id => !selectedTaxa[up_id]).map((id) => `(proteome:${id})`).join(' ') },
function (data) {
let data_p = data['results']['bindings'];
for (let i = 0; i < data_p.length; i++) {
let row = data_p[i];
row['taxid']['value'].match(/(\d+)$/);
const genome_taxid = RegExp.$1;
const up_id_url = row['proteome']['value'];
const up_id = row['proteome']['value'].replace(/.*\//, '');
const types = row['types']['value'];
const organism_name = row['organism']['value'];
const n_genes = parseInt(row['proteins']['value']);
const n_isoforms = parseInt(row['isoforms']['value']);
const cpd_label = row['cpd_label']['value'];
const busco_complete = row['busco_complete'] ? row['busco_complete']['value'] : '';
const busco_single = row['busco_single'] ? row['busco_single']['value'] : '';
const busco_multi = row['busco_multi'] ? row['busco_multi']['value'] : '';
const busco_fragmented = row['busco_fragmented'] ? row['busco_fragmented']['value'] : '';
const busco_missing = row['busco_missing'] ? row['busco_missing']['value'] : '';
const assembly = row['assembly'] ? row['assembly']['value'] : '';
let entry = {
genome_taxid,
up_id_url,
up_id,
types,
organism_name,
n_genes,
n_isoforms,
cpd_label,
busco_complete,
busco_single,
busco_multi,
busco_fragmented,
busco_missing,
assembly
};
selectedTaxa[entry.up_id] = entry;
}
localStorage.setObject('selectedTaxa', selectedTaxa);
queryBySpang(`sparql/goid_to_search_proteins.rq`, null,
{ values: Object.keys(selectedProteins).filter(up_id => !selectedProteins[up_id]).map((id) => `(uniprot:${id})`).join(' ') },
function (data) {
let bindings = data['results']['bindings'];
for (let i = 0; i < bindings.length; i++) {
let row = bindings[i];
row['protein']['value'].match(/(\d+)$/);
const up_id_url = row['protein']['value'];
const up_id = row['protein']['value'].replace(/.*\//, '');
const mnemonic = row['mnemonic']['value'];
const full_name = row['full_name']['value'];
const map = row['map']['value'];
let entry = {
up_id_url,
up_id,
mnemonic,
full_name,
map
};
selectedProteins[up_id] = entry;
}
localStorage.setObject('selectedProteins', selectedProteins);
UpdateChart();
}
);
});
});
$(function() {
$(document).on('click', '.add_genome', function() {
let codename = $(this).data('codename');
delete selectedTaxa[codename];
localStorage.setObject('selectedTaxa', selectedTaxa);
UpdateChart();
});
$(document).on('click', '.add_genome_all', function() {
// Swith the icon
let selected = $(this).prop('checked');
for (let i=0; i<$('.add_genome').length; i++) {
let codename = $('.add_genome').eq(i).data('codename');
delete selectedTaxa[codename];
}
localStorage.setObject('selectedTaxa', selectedTaxa);
UpdateChart();
});
});
$(function() {
$(document).on('click', '.add_protein', function() {
let this_row = $(this).closest('tr');
// Selected item
let codename = this_row.find('td:nth-child(2)').text();
delete selectedProteins[codename];
localStorage.setObject('selectedProteins', selectedProteins);
UpdateChart();
});
$(document).on('click', '.add_protein_all', function() {
// Swith the icon
let selected = $(this).prop('checked');
for (let i=0; i<$('.add_protein').length; i++) {
let each_icon = $('.add_protein').eq(i);
let each_row = each_icon.closest('tr');
// Eech item
let codename = each_row.find('td:nth-child(2)').text();
// Delete the item
delete selectedProteins[codename];
}
localStorage.setObject('selectedProteins', selectedProteins);
UpdateChart();
});
});