forked from opentargets-archive/webapp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
related-diseases-directive.js
253 lines (224 loc) · 12.8 KB
/
related-diseases-directive.js
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
angular.module('otPlugins')
.directive('otRelatedDiseases2', ['otUtils', 'otConsts', 'otDictionary', 'otApi', '$timeout', function (otUtils, otConsts, otDictionary, otApi, $timeout) {
'use strict';
// Details table --
// var colorScale = otUtils.colorScales.BLUE_0_1; //blue orig
var colorScale = otUtils.colorScales.BLUE_RED; // blue <-> red
var currTable;
var spDiv;
var getColorStyleString = function (value1, value2, hrefObj, hrefSbj) {
var str = '';
var width = 28;
var barScale1 = d3.scale.linear()
.domain([0, 1])
.range([0, width]); // Each cell is 48px
str = '<div style=\'background:#EEEEEE; color:#EEEEEE\'><a href=' + hrefObj + '><div style=\'display:inline-block; height:15px; width:' + barScale1(value1) + 'px; background:#582A72; margin-left:' + (width - barScale1(value1)) + 'px;\'></div></a><a href=' + hrefSbj + '><div style=\'display:inline-block; height:15px; width:' + barScale1(value2) + 'px; background:#AAAA39;\'></div></a>' + otUtils.floatPrettyPrint(value1) + '</div>';
return str;
};
var cols = [
{name: '', title: otDictionary.TARGET_SYMBOL},
{name: '', title: otDictionary.ENSEMBL_ID},
// Datatypes for the OBJECT
{name: '', title: otDictionary.ASSOCIATION_SCORE},
{name: otConsts.datatypes.GENETIC_ASSOCIATION.id, title: otDictionary[otConsts.datatypes.GENETIC_ASSOCIATION.id.toUpperCase()]},
{name: otConsts.datatypes.SOMATIC_MUTATION.id, title: otDictionary[otConsts.datatypes.SOMATIC_MUTATION.id.toUpperCase()]},
{name: otConsts.datatypes.KNOWN_DRUG.id, title: otDictionary[otConsts.datatypes.KNOWN_DRUG.id.toUpperCase()]},
{name: otConsts.datatypes.AFFECTED_PATHWAY.id, title: otDictionary[otConsts.datatypes.AFFECTED_PATHWAY.id.toUpperCase()]},
{name: otConsts.datatypes.RNA_EXPRESSION.id, title: otDictionary[otConsts.datatypes.RNA_EXPRESSION.id.toUpperCase()]},
{name: otConsts.datatypes.LITERATURE.id, title: otDictionary[otConsts.datatypes.LITERATURE.id.toUpperCase()]},
{name: otConsts.datatypes.ANIMAL_MODEL.id, title: otDictionary[otConsts.datatypes.ANIMAL_MODEL.id.toUpperCase()]},
{name: '', title: 'total score'},
// empty col for the gene name
{name: '', title: otDictionary.TARGET_NAME}
];
// setup the table
var setupTable = function (table, data, filename) {
currTable = $(table).DataTable({
'dom': '<"clearfix" <"clear small" i><"pull-left small" f><"pull-right"<"#cttvTableDownloadIcon">>rt<"pull-left small" l><"pull-right small" p>>',
'processing': false,
'data': data,
'columns': (function () {
var a = [];
for (var i = 0; i < cols.length; i++) {
a.push({'title': '<div><span title=\'' + cols[i].title + '\'>' + cols[i].title + '</span></div>', 'name': cols[i].name});
}
return a;
})(),
'columnDefs': [
{
'targets': [1, 10],
'visible': false
},
{
'targets': [1, 11],
'orderable': false
},
{'orderSequence': ['desc', 'asc'], 'targets': [2, 3, 4, 5, 6, 7, 8, 9, 10]},
{'orderSequence': ['asc', 'desc'], 'targets': [0]}
],
'order': [],
'orderMulti': true,
'autoWidth': false,
'ordering': true,
'lengthMenu': [[20, 100, 500], [20, 100, 500]],
'pageLength': 20,
'language': {
// "lengthMenu": "Display _MENU_ records per page",
// "zeroRecords": "Nothing found - sorry",
'info': 'Showing _START_ to _END_ of _TOTAL_ shared targets'
// "infoEmpty": "No records available",
// "infoFiltered": "(filtered from _MAX_ total records)"
}
}, filename);
return t;
};
function getBestSharedTargets (data, max) {
var targets = {};
var n = 0;
for (var i = 0; i < data.length; i++) {
var t = data[i].target.id;
if (!targets[t]) {
targets[t] = 1;
n++;
if (n === max) {
break;
}
}
}
return Object.keys(targets);
}
function splitDataIntoDiseases (data) {
var resp = {};
for (var i = 0; i < data.length; i++) {
var d = data[i];
if (!resp[d.disease.id]) {
resp[d.disease.id] = {};
}
resp[d.disease.id][d.target.id] = d;
}
return resp;
}
function getData (object, subject, targets, table, filename) {
var paramsObj = {};
paramsObj.disease = [object, subject];
paramsObj.target = targets.slice(0, 100);
paramsObj.size = 200;
paramsObj.direct = true;
paramsObj.facets = false;
// TODO: Include POST in otApi
var cttvApi = otApi.getSelf(); // No POST in the service yet
var url = cttvApi.url.associations();
cttvApi.call(url, paramsObj)
.then(function (resp) {
var splitData = splitDataIntoDiseases(resp.body.data);
var dtData = parseData(splitData, object, subject, targets.slice(0, 100));
spDiv.parentNode.removeChild(spDiv);
setupTable(table, dtData, filename);
});
}
function parseData (data, object, subject, targets) {
var newData = new Array(data.length);
for (var i = 0; i < targets.length; i++) {
var target = targets[i];
var objAssoc = data[object][target];
var sbjAssoc = data[subject][target];
var objDts = objAssoc.association_score.datatypes;
var sbjDts = sbjAssoc.association_score.datatypes;
var row = [];
var geneLoc = '/target/' + objAssoc.target.id + '/associations';
var geneObjDiseaseLoc = '/evidence/' + objAssoc.target.id + '/' + objAssoc.disease.id;
var geneSbjDiseaseLoc = '/evidence/' + sbjAssoc.target.id + '/' + sbjAssoc.disease.id;
// Name
row.push('<a href=' + geneLoc + ' title=' + objAssoc.target.gene_info.symbol + '>' + objAssoc.target.gene_info.symbol + '</a>');
// Ensembl ID
row.push(objAssoc.target.id);
// OBJECT...
// Association score
row.push(getColorStyleString(objAssoc.association_score.overall, sbjAssoc.association_score.overall, geneObjDiseaseLoc, geneSbjDiseaseLoc));
// Genetic association
row.push(getColorStyleString(objDts.genetic_association, sbjDts.genetic_association, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=genetic_associations', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=genetic_associations'));
// Somatic mutation
row.push(getColorStyleString(objDts.somatic_mutation, sbjDts.somatic_mutation, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=somatic_mutations', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=somatic_mutations'));
// Known drug
row.push(getColorStyleString(objDts.known_drug, sbjDts.known_drug, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=known_drugs', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=known_drugs'));
// Affected pathway
row.push(getColorStyleString(objDts.affected_pathway, sbjDts.affected_pathway, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=affected_pathways', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=affected_pathways'));
// Expression atlas
row.push(getColorStyleString(objDts.rna_expression, sbjDts.rna_expression, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=rna_expression', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=rna_expression'));
// Literature
row.push(getColorStyleString(objDts.literature, sbjDts.literature, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=literature', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=literature'));
// Animal model
row.push(getColorStyleString(objDts.animal_model, sbjDts.animal_model, geneObjDiseaseLoc + (geneObjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=animal_models', geneSbjDiseaseLoc + (geneSbjDiseaseLoc.indexOf('?') === -1 ? '?' : '&') + 'sec=animal_models'));
// Total score
row.push(objDts.genetic_association +
objDts.somatic_mutation +
objDts.known_drug +
objDts.rna_expression +
objDts.affected_pathway +
objDts.animal_model);
row.push('<a href=\'' + geneLoc + '\' title=\'' + objAssoc.target.gene_info.name + '\'>' + objAssoc.target.gene_info.name + '</a>');
newData[i] = row;
}
return newData;
}
return {
restrict: 'E',
templateUrl: 'plugins/related-diseases/related-diseases.html',
scope: {
disease: '=',
width: '='
},
link: function (scope, element) {
// Populate overview
$timeout(function () {
var v = vis()
.disease(scope.disease.efo)
.skip(1)
.size(600)
.score('jackard_weighted')
.cttvApi(otApi.getSelf());
// Populate details on node click:
v.on('click', function (d) {
// Set the spinner...
spDiv = document.createElement('div');
var sp = spinner()
.size(30)
.stroke(3);
element[0].appendChild(spDiv);
sp(spDiv);
// Set the header...
var container = document.getElementById('relatedDiseasesDetailsHeader');
container.innerHTML = '';
var h4 = document.createElement('h4');
h4.innerText = 'Details';
// Disease names with color
var divHeader = document.createElement('div');
var pD1 = document.createElement('span');
pD1.style['font-weight'] = 'bold';
pD1.style.color = '#582A72';
pD1.innerText = d.object.label;
var pVs = document.createElement('text');
pVs.innerText = ' vs ';
var pD2 = document.createElement('span');
pD2.style['font-weight'] = 'bold';
pD2.style.color = '#AAAA39';
pD2.innerText = d.subject.label;
container.appendChild(h4);
container.appendChild(pD1);
container.appendChild(pVs);
container.appendChild(pD2);
// Destroy prev table if exists
if (currTable) {
currTable.destroy();
}
var filename = 'shared_targets_' + d.subject.id + '-' + d.object.id;
scope.subject = d.subject.label;
var table = document.getElementById('relatedDiseasesDetailsTable');
table.innerHTML = '';
getData(d.object.id, d.subject.id, d.shared_targets, table, filename);
});
v(document.getElementById('relatedDiseasesOverview'));
}, 0);
}
};
}]);