-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathNeedlePlot.react.js
448 lines (416 loc) · 14.8 KB
/
NeedlePlot.react.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
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
import React, {Component} from 'react';
import Plot from 'react-plotly.js';
import {reduce, max, range, repeat, mergeDeepRight, omit} from 'ramda';
import {propTypes, defaultProps} from '../components/NeedlePlot.react';
/**
* Checks if a variable is representation of a number or not
* https://stackoverflow.com/questions/9716468/pure-javascript-a-function-like-jquerys-isnumeric
* @param {String/FLoat} n A variable to test.
* @return {Bool} True if n is a number, false otherwise.
*/
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
/**
* Converts an array elements to numbers and ignore the elements which are not
* a representation of numbers
* @param {Array} test_array An array
* @return {Array} An array with only numbers.
*/
function filterNanArray(test_array) {
return test_array.filter(el => Number(isNumeric(el)));
}
/**
* Search the protein position array for small protein domains (typically 1->5 sites)
* and bogus entries (i.e. "?-123" or "320-?"), the protein domains are indicated
* by the presence of a '-' character in the element of the array.
* @param {Array} protein_pos_array An array containing protein domains
* @return {Array} positions_array An array with only single site
protein mutations.
* @return {Array} domains_array An array with only small domains
protein mutations.
* @return {Array} idx_old_positions_array An array with the indexes of the
single site protein mutations
relative to protein_pos_array
* @return {Array} idx_bogus_entry An array with the indexes of the
bogus entries (containing '?')
relative to protein_pos_array
*/
function extractSmallDomains(protein_pos_array) {
const positions_array = [];
const domains_array = [];
const idx_old_positions_array = [];
const idx_bogus_entry = [];
protein_pos_array.forEach((dx, i) => {
if (dx.indexOf('-') > -1) {
const domains_limits = dx.split('-');
if (isNumeric(domains_limits[0]) || isNumeric(domains_limits[1])) {
idx_bogus_entry.push(i);
} else {
domains_array.push(dx);
}
} else {
idx_old_positions_array.push(i);
positions_array.push(dx);
}
});
return [
positions_array,
domains_array,
idx_old_positions_array,
idx_bogus_entry,
];
}
/**
* Creates two arrays to plot horizontal lines with many markers
*
* @param {number} xi start x coordinate of the line
* @param {number} xf stop x coordinate of the line
* @param {number} y y coordinate of the line
* @param {int} n number of markers
* @return {array} x x coordinates of the horizontal ine
* @return {array} y y coordinates of the horizontal ine
*/
function createHorizontalLine(xi, xf, y, n) {
const dx = (xf - xi) / n;
const N = Math.max(2, n);
const x = range(0, N).map(i => xi + i * dx);
return [x, repeat(y, N)];
}
/**
* Finds the max of an array while ignoring the NaN values
*
* @param {array} test_array an array with numbers as entries
* @return {number} max value of the array
*/
function nanMax(test_array) {
return reduce(max, -Infinity, filterNanArray(test_array));
}
/**
* The Needle Plot component is used to visualize large datasets
* containing categorical or numerical data. The lines and markers in
* the plot correspond to bars in a histogram.
**/
export default class NeedlePlot extends Component {
constructor() {
super();
this.state = {
xStart: null,
xEnd: null,
};
this.handleChange = this.handleChange.bind(this);
}
UNSAFE_componentWillMount() {
// For default argument of taken from defaultProps deeply nested
this.props = mergeDeepRight(NeedlePlot.defaultProps, this.props);
}
// Handle plot events
handleChange(event) {
// Zoom
if (event['xaxis.range[0]'] || event['xaxis.range']) {
this.setState({
xStart: event['xaxis.range[0]'] || event['xaxis.range'][0],
xEnd: event['xaxis.range[1]'] || event['xaxis.range'][1],
});
}
// Autozoom
else if (event['xaxis.autorange'] === true) {
this.setState({
xStart: null,
xEnd: null,
});
}
}
render() {
const {id} = this.props;
const {
data,
globalAnnotation,
domainAnnotations,
} = this.prepareTraces();
const layout = this.prepareLayout({
data,
globalAnnotation,
domainAnnotations,
});
return (
<div id={id}>
<Plot
data={data}
layout={layout}
onRelayout={this.handleChange}
{...omit(['setProps'], this.props)}
/>
</div>
);
}
// Fetch data
prepareTraces() {
const {
mutationData: {x, y, mutationGroups, domains},
domainStyle: {domainColor, displayMinorDomains},
needleStyle: {
stemColor,
stemThickness,
stemConstHeight,
headSize,
headColor,
headSymbol,
},
} = mergeDeepRight(NeedlePlot.defaultProps, this.props);
// Apply filtering on protein positions
const [
x_single_site,
small_domains,
idx_old_positions_array,
] = extractSmallDomains(x);
// manage whether headColor is an array or a string
const fixed_mutation_colors = Array.isArray(headColor)
? headColor
: mutationGroups.map(() => headColor);
const fixed_mutation_symbols = Array.isArray(headSymbol)
? headSymbol
: mutationGroups.map(() => headSymbol);
const fixed_domain_colors = domainColor;
const X_DATA_MIN = Math.min.apply(null, x_single_site);
const X_DATA_MAX = Math.max.apply(null, x_single_site);
const Y_DATA_MAX = stemConstHeight === true ? 1 : nanMax(y);
const X_RANGE_MIN = this.state.xStart || X_DATA_MIN;
const X_RANGE_MAX = this.state.xEnd || X_DATA_MAX;
const XSPAN = X_RANGE_MAX - X_RANGE_MIN;
// this is used to trigger a change of display inside annotations
const XSPAN_RATIO = 0.2;
const Y_BUFFER = stemConstHeight === true ? 0.5 : Y_DATA_MAX / 10;
// this is used to scale the position for the annotations
const Y_BUFFER_DIVIDER = 2;
const Y_TOP = stemConstHeight === true ? 2 : Y_DATA_MAX + Y_BUFFER;
const DOMAIN_WIDTH = 33;
const sequenceDomains = [];
const domainAnnotations = [];
let hoverlabels = [];
// contains the height of each stem
let stemsY = [];
idx_old_positions_array.forEach(idx => {
if (stemConstHeight) {
stemsY = stemsY.concat([1]);
} else {
hoverlabels = hoverlabels.concat([
'(' + x[idx] + ',' + y[idx] + ')',
]);
stemsY = stemsY.concat([y[idx]]);
}
});
const hoverinfo =
stemConstHeight === true ? 'x+name+text' : 'name+text';
// build the different protein large domains
domains.forEach((dom, i) => {
const domainLimits = dom.coord.split('-');
const x0 = Number(domainLimits[0]);
const x1 = Number(domainLimits[1]);
const domainLength = x1 - x0;
// Highlight of the protein domain
sequenceDomains.push({
x: [x1, x0],
y: [Y_TOP, Y_TOP],
xaxis: 'x1',
name: dom.name,
fill: 'tozeroy',
mode: 'lines',
opacity: 0.5,
visible: 'legendonly',
legendgroup: dom.name,
marker: {color: fixed_domain_colors[i]},
});
const [line_x, line_y] = createHorizontalLine(
x0,
x1,
-Y_BUFFER,
x1 - x0
);
sequenceDomains.push({
type: 'scatter',
mode: 'lines',
fill: 'tozeroy',
fillcolor: fixed_domain_colors[i],
hoveron: 'points+fills',
x: line_x,
y: line_y,
xaxis: 'x2',
showlegend: false,
hoverinfo: 'name',
name: `[${x0}->${x1}] ${dom.name}`,
marker: {color: fixed_domain_colors[i]},
line: {width: 2},
});
// Name of the protein domain
domainAnnotations.push({
x: (x0 + x1) / Y_BUFFER_DIVIDER,
y: -Y_BUFFER / Y_BUFFER_DIVIDER,
showarrow: false,
text: dom.name,
width: domainLength,
align: domainLength < XSPAN_RATIO * XSPAN ? 'right' : 'center',
});
});
if (displayMinorDomains === true) {
// build the different protein small domains
small_domains.forEach(dom => {
const x0 = Number(dom.split('-')[0]);
const x1 = Number(dom.split('-')[1]);
const gname = mutationGroups[x.indexOf(dom)];
const [line_x, line_y] = createHorizontalLine(
x0,
x1,
-Y_BUFFER / Y_BUFFER_DIVIDER,
x1 - x0
);
// Range of the protein domain on the xaxis
sequenceDomains.push({
type: 'scatter',
mode: 'lines',
x: line_x,
y: line_y,
fill: 'tozeroy',
fillcolor:
fixed_mutation_colors[
[...new Set(mutationGroups)].indexOf(gname)
],
hoveron: 'points+fills',
xaxis: 'x2',
hoverinfo: 'name+text',
name: gname,
text: `[${x0}->${x1}] `,
showlegend: false,
marker: {
color:
fixed_mutation_colors[
[...new Set(mutationGroups)].indexOf(gname)
],
},
line: {width: DOMAIN_WIDTH},
});
});
}
const globalAnnotation = [
{
text: `<b>${x_single_site.length +
small_domains.length} Mutations</b>`,
x: 0.01,
xref: 'paper',
y: 1.1,
yref: 'paper',
showarrow: false,
align: 'left',
},
];
const data = [
{
type: 'scatter',
mode: 'markers',
x: x_single_site,
y: stemsY,
xaxis: 'x1',
hoverinfo: hoverinfo,
text: hoverlabels,
error_y: {
type: 'data',
symmetric: false,
array: 0,
arrayminus: stemsY,
thickness: stemThickness,
width: 0,
color: stemColor,
},
transforms: [
{
type: 'groupby',
groups: mutationGroups,
nameformat: `%{group}`,
styles: [...new Set(mutationGroups)].map(
(target, i) => {
return {
target: target,
value: {
marker: {
size: headSize,
symbol: fixed_mutation_symbols[i],
color: fixed_mutation_colors[i],
},
},
};
}
),
},
],
},
].concat(sequenceDomains);
return {data, globalAnnotation, domainAnnotations};
}
// Fetch layout
prepareLayout(vars) {
const {data, globalAnnotation, domainAnnotations} = vars;
const {xlabel, ylabel, rangeSlider} = mergeDeepRight(
NeedlePlot.defaultProps,
this.props
);
let {xStart, xEnd} = this.state;
let first_init = false;
// initialize the range based on input data
if (Boolean(!xStart) || Boolean(!xEnd)) {
first_init = true;
data.forEach(trace => {
const X_DATA_MIN = Math.min.apply(null, trace.x);
const X_DATA_MAX = Math.max.apply(null, trace.x);
if (xStart > X_DATA_MIN || Boolean(!xStart)) {
xStart = X_DATA_MIN;
}
if (xEnd < X_DATA_MAX || Boolean(!xEnd)) {
xEnd = X_DATA_MAX;
}
});
}
// this is used to zoom in the axis range initially
const XSTART_RATIO = 0.98;
const XEND_RATIO = 1.02;
const layout = {
legend: {
orientation: 'v',
x: 1,
y: 1.05,
bgcolor: 'rgba(255, 255, 255, 0)',
},
hovermode: 'closest',
xaxis: {
title: xlabel,
showgrid: false,
zeroline: false,
autorange: Boolean(!xStart),
range: [xStart, xEnd],
anchor: 'y',
},
xaxis2: {
scaleanchor: 'x',
autorange: Boolean(!xStart),
range: [xStart, xEnd],
anchor: 'y',
overlaying: 'x',
},
yaxis: {
title: ylabel,
showgrid: false,
ticks: 'inside',
},
margin: {t: 100, l: 40, r: 0, b: 40},
annotations: domainAnnotations.concat(globalAnnotation),
};
if (rangeSlider === true) {
layout.xaxis.rangeslider =
first_init === true
? {range: [xStart * XSTART_RATIO, xEnd * XEND_RATIO]}
: {};
}
return layout;
}
}
NeedlePlot.propTypes = propTypes;
NeedlePlot.defaultProps = defaultProps;