/
vis_defines.js
494 lines (442 loc) · 18.1 KB
/
vis_defines.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
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
/**
* Functions and components for both dataset visualizations and batch visualizations.
*/
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import url from 'url';
import * as encoding from '../libs/query_encoding';
import { Modal, ModalHeader, ModalBody, ModalFooter } from '../libs/ui/modal';
import * as globals from './globals';
/**
* Dataset visualization section.
**************************************************************************************************
*/
/**
* Maximum number of hic files allowed to be selected at once.
*/
const MAX_HIC_FILES_SELECTED = 8;
/**
* If you modify ASSEMBLY_DETAILS in vis_defines.py, this object might need corresponding
* modifications.
*/
const ASSEMBLY_DETAILS = {
GRCh38: {
species: 'Homo sapiens',
ucsc_assembly: 'hg38',
ensembl_host: 'www.ensembl.org',
},
'GRCh38-minimal': {
species: 'Homo sapiens',
ucsc_assembly: 'hg38',
ensembl_host: 'www.ensembl.org',
},
hg19: {
species: 'Homo sapiens',
ucsc_assembly: 'hg19',
ensembl_host: 'grch37.ensembl.org',
},
mm10: {
species: 'Mus musculus',
ucsc_assembly: 'mm10',
ensembl_host: 'www.ensembl.org',
},
'mm10-minimal': {
species: 'Mus musculus',
ucsc_assembly: 'mm10',
ensembl_host: 'www.ensembl.org',
},
mm9: {
species: 'Mus musculus',
ucsc_assembly: 'mm9',
},
dm6: {
species: 'Drosophila melanogaster',
ucsc_assembly: 'dm6',
},
dm3: {
species: 'Drosophila melanogaster',
ucsc_assembly: 'dm3',
},
ce11: {
species: 'Caenorhabditis elegans',
ucsc_assembly: 'ce11',
},
ce10: {
species: 'Caenorhabditis elegans',
ucsc_assembly: 'ce10',
},
ce6: {
species: 'Caenorhabditis elegans',
ucsc_assembly: 'ce6',
},
};
/**
* File types allowed for each browser.
*/
const browserFileTypes = {
IGV: [],
UCSC: [],
Ensembl: ['bigWig', 'bigBed'],
hic: ['hic'],
};
/**
* Open a browser visualization in a new tab. If called from a React component, that component must
* be mounted.
* @param {object} dataset Dataset object whose files we're visualizing
* @param {string} browser Specifies browser to use: UCSC, Ensembl, hic currently
* acceptable
* @param {string} assembly Assembly to use with visualizer
* @param {array} files Array of files to visualize if applicable
* @param {string} datasetUrl URL of `dataset` parameter
*/
export const visOpenBrowser = (dataset, browser, assembly, files, datasetUrl) => {
let href;
switch (browser) {
case 'IGV': {
const ucscAssembly = (ASSEMBLY_DETAILS[assembly] || {}).ucsc_assembly || assembly;
const sessionURL = `${datasetUrl}@@igv.json?assembly=${encodeURIComponent(ucscAssembly)}`;
href=`https://igv.org/app/?sessionURL=${encodeURIComponent(sessionURL)}`;
break;
}
case 'UCSC': {
// UCSC does not use `files` under any circumstances.
const ucscAssembly = ASSEMBLY_DETAILS[assembly].ucsc_assembly;
href = `http://genome.ucsc.edu/cgi-bin/hgTracks?hubClear=${datasetUrl}@@hub/hub.txt&db=${ucscAssembly}`;
break;
}
case 'hic': {
const parsedUrl = url.parse(datasetUrl);
delete parsedUrl.path;
delete parsedUrl.search;
delete parsedUrl.query;
const fileQueries = files.map((file) => {
parsedUrl.pathname = file.href;
const name = file.biological_replicates && file.biological_replicates.length > 0 ? `&name=${dataset.accession} / ${file.title}, Replicate ${file.biological_replicates.join(',')}` : '';
return encoding.encodedURIComponentOLD(`{hicUrl=${url.format(parsedUrl)}${name}}`, { encodeEquals: true });
});
href = `http://aidenlab.org/juicebox/?juicebox=${fileQueries.join(',')}`;
break;
}
case 'Ensembl': {
if (ASSEMBLY_DETAILS[assembly].ensembl_host) {
href = `http://${ASSEMBLY_DETAILS[assembly].ensembl_host}/Trackhub?url=${datasetUrl}@@hub/hub.txt;species=${ASSEMBLY_DETAILS[assembly].species.replace(/ /g, '_')}`;
}
break;
}
default:
break;
}
if (href) {
window.open(href, '_blank');
}
};
/**
* Filter files down to the ones that should be selected by default for the given browser.
* @param {array} files Files to filter according to what a browser uses
* @param {string} browser Browser to filter files against; see `browserFileTypes`
* @param {boolean} limits True to place browser-specific limits on resulting files, if any
* @return {array} `files` that can be browsed with `browser`.
*/
export const visFilterBrowserFiles = (files, browser, limits = false) => {
// Filter the given files to ones qualified for the given browser, and only those files with
// "released" status.
const qualifiedFileTypes = browserFileTypes[browser];
let qualifiedFiles = files.filter(file => qualifiedFileTypes.indexOf(file.file_format) !== -1 && file.status === 'released');
// For the hic browser, sort to prioritize "mapping quality thresholded chromatin interactions"
// output_types.
if (browser === 'hic') {
qualifiedFiles = qualifiedFiles.sort(file => (file.output_type === 'mapping quality thresholded chromatin interactions' ? -1 : 0));
if (limits) {
qualifiedFiles = qualifiedFiles.slice(0, MAX_HIC_FILES_SELECTED);
}
}
return qualifiedFiles;
};
/**
* Determine whether a file is selectable for the currently selected genome browser.
* @param {object} file File to check against its browser selectable state
* @param {array} selectedFiles Files that are currently selected
* @param {string} browser Currently selected browser
*/
export const visFileSelectable = (file, selectedFiles, browser) => {
let selectable = false;
switch (browser) {
case 'UCSC':
// Always not selectable.
break;
case 'hic':
selectable = (browserFileTypes[browser].indexOf(file.file_format) !== -1) && (selectedFiles.length < MAX_HIC_FILES_SELECTED);
break;
case 'Ensembl':
// Always not selectable.
break;
default:
// Should never happen, but not selectable if it does.
break;
}
return selectable;
};
/**
* The order that browsers should appear when sorted.
*/
const browserOrder = [
'UCSC',
'hic',
'Ensembl',
'IGV',
];
/**
* Given an array of browsers, sort them according to the order in the `browserOrder` global above.
* @param {array} browsers Array of browsers to sort
* @return {array} Same contents as `browsers` but sorted according to `browserOrder`
*/
export const visSortBrowsers = browsers => (
_.sortBy(browsers, browser => browserOrder.indexOf(browser))
);
/**
* Map of browser to display name.
*/
const browserNameMap = {
UCSC: 'UCSC',
hic: 'Juicebox',
Ensembl: 'Ensembl',
};
/**
* Map a browser to its display name.
* @param {string} browser Browser whose display name is desired
*/
export const visMapBrowserName = browser => browserNameMap[browser];
/**
* Batch visualization section.
**************************************************************************************************
*/
/**
* Generate a batch hub URL from the search results. This URL gets inserted into visualization
* URLs.
* @param {object} results Search results
* @param {string} hostName Domain name of host
*
* @return {string} hub URL used in visualization URLs
*/
const generateBatchHubUrl = (results, hostName) => {
const parsedUrl = url.parse(results['@id']).search;
if (parsedUrl) {
return `${hostName}/batch_hub/${encodeURIComponent(parsedUrl.substr(1).replace('&', ',,'))}/hub.txt`;
}
return null;
};
/**
* Generate a list of relevant assemblies from the given search results. This includes any
* assemblies included in the search results, filtered by any specified in the query string that
* generated these search results, or all included assemblies if no assemblies were given in the
* query string.
* @param {object} results Search results
*
* @return {array} Assemblies in search results, filtered by query string assemblies if any
*/
const getRelevantAssemblies = (results) => {
let relevantAssemblies = [];
const assemblyFacet = results.facets.find(facet => facet.field === 'assembly');
if (assemblyFacet) {
// Get array of assemblies specified in the search query; empty array if none.
const specifiedAssemblies = results.filters.filter(filter => filter.field === 'assembly').map(filter => filter.term);
relevantAssemblies = assemblyFacet.terms.filter(term => term.doc_count > 0 && (specifiedAssemblies.length === 0 || specifiedAssemblies.includes(term.key)));
}
return relevantAssemblies.map(assembly => assembly.key).sort((a, b) => globals.assemblyPriority.indexOf(a) - globals.assemblyPriority.indexOf(b));
};
/**
* Currently this maps an assembly as stored in encode to an assembly that UCSC uses in the URL.
* Each mapping results in an object to allow for future expansion.
*/
const ucscAssemblyDetails = {
GRCh38: { mappedAssembly: 'hg38' },
'GRCh38-minimal': { mappedAssembly: 'hg38' },
hg19: { mappedAssembly: 'hg19' },
mm10: { mappedAssembly: 'mm10' },
'mm10-minimal': { mappedAssembly: 'mm10' },
mm9: { mappedAssembly: 'mm9' },
dm6: { mappedAssembly: 'dm6' },
dm3: { mappedAssembly: 'dm3' },
ce11: { mappedAssembly: 'ce11' },
ce10: { mappedAssembly: 'ce10' },
ce6: { mappedAssembly: 'ce6' },
};
/**
* Generate a batch hub URL for UCSC based on the given assembly.
* @param {string} assembly Assembly to use for the generated URL
* @param {string} batchHubUrl Batch hub URL
* @param {string} position Optional region search position string
*/
const ucscUrlGenerator = (assembly, batchHubUrl, position) => {
const details = ucscAssemblyDetails[assembly];
if (details) {
return {
visualizer: 'UCSC',
url: `http://genome.ucsc.edu/cgi-bin/hgTracks?hubClear=${batchHubUrl}&db=${details.mappedAssembly}${position ? `&position=${position}` : ''}`,
};
}
return null;
};
/**
* Currently this maps an assembly as stored in encode to a species that ENSEMBL uses in the URL.
* Each mapping results in an object to allow for future expansion.
*/
const ensembleAssemblyDetails = {
GRCh38: { species: 'Homo_sapiens' },
'GRCh38-minimal': { species: 'Homo_sapiens' },
mm10: { species: 'Mus_musculus' },
'mm10-minimal': { species: 'Mus_musculus' },
};
/**
* Generate a batch hub URL for ENSEMBL based on the given assembly.
* @param {string} assembly Assembly to use for the generated URL
* @param {string} batchHubUrl Batch hub URL
*/
const ensemblUrlGenerator = (assembly, batchHubUrl) => {
const details = ensembleAssemblyDetails[assembly];
if (details) {
return {
visualizer: 'Ensembl',
url: `http://www.ensembl.org/Trackhub?url=${batchHubUrl};species=${details.species}`,
};
}
return null;
};
/**
* Used for the loop through each external visualization we support. Add new generator functions to
* this array to support new visualizers.
*/
const urlGenerators = [ucscUrlGenerator, ensemblUrlGenerator];
/**
* Display a Visualize button that brings up a modal that lets you choose an assembly and a browser
* in which to display the visualization.
*/
export class BrowserSelector extends React.Component {
constructor() {
super();
// Set initial React state.
this.state = { selectorOpen: false };
this.openModal = this.openModal.bind(this);
this.closeModal = this.closeModal.bind(this);
this.handleClick = this.handleClick.bind(this);
}
// Called to open the browser-selection modal.
openModal() {
this.setState({ selectorOpen: true });
}
// Called to close the browser-seletino modal.
closeModal() {
this.setState({ selectorOpen: false });
}
// When the link to open a browser gets clicked, this gets called to close the modal in
// addition to going to the link.
handleClick() {
this.closeModal();
}
render() {
const { results, disabledTitle } = this.props;
// Only consider Visualize button if exactly one type= of Experiment or Annotation exists
// in query string.
const docTypes = results.filters.filter(filter => filter.field === 'type').map(filter => filter.term);
if (docTypes.length > 1 || (docTypes.length === 1 && docTypes[0] !== 'Experiment' && docTypes[0] !== 'Annotation')) {
return null;
}
// Generate the batch hub URL used in batch visualization query strings.
const parsedLocationHref = url.parse(this.context.location_href);
const hostName = `${parsedLocationHref.protocol}//${parsedLocationHref.host}`;
const batchHubUrl = generateBatchHubUrl(results, hostName);
if (!batchHubUrl) {
return null;
}
// If coordinates are given, construct a "position" query string parameter expanded by 200
// bp in either direction.
let position = '';
if (results.coordinates) {
const matches = results.coordinates.match(/(.+):(\d+)-(\d+)/);
if (matches) {
const lowCoordinate = Number(matches[2]);
const highCoordinate = Number(matches[3]);
position = `${matches[1]}:${lowCoordinate - 200}-${highCoordinate + 200}`;
}
}
const relevantAssemblies = getRelevantAssemblies(results);
const visualizeCfg = {};
relevantAssemblies.forEach((assembly) => {
visualizeCfg[assembly] = {};
urlGenerators.forEach((urlGenerator) => {
const visualizationMechanism = urlGenerator(assembly, batchHubUrl, position);
if (visualizationMechanism) {
visualizeCfg[assembly][visualizationMechanism.visualizer] = visualizationMechanism.url;
}
});
});
if (relevantAssemblies.length > 0) {
return (
<React.Fragment>
<button onClick={this.openModal} className="btn btn-info btn-sm" data-test="visualize" id="visualize-control">
Visualize
</button>
{this.state.selectorOpen ?
<Modal closeModal={this.closeModal} addClasses="browser-selector__modal" focusId="visualize-limit-close">
<ModalHeader title="Open visualization browser" closeModal={this.closeModal} />
<ModalBody>
{disabledTitle ?
<div>{disabledTitle}</div>
:
<div className="browser-selector">
<div className="browser-selector__inner">
<div className="browser-selector__title">
<div className="browser-selector__assembly-title">
Assembly
</div>
<div className="browser-selector__browsers-title">
Visualize with browser…
</div>
</div>
<hr />
{relevantAssemblies.map((assembly) => {
const assemblyBrowsers = visualizeCfg[assembly];
const browserList = _(Object.keys(assemblyBrowsers)).sortBy(browser => _(globals.browserPriority).indexOf(browser));
return (
<div key={assembly} className="browser-selector__assembly-option">
<div className="browser-selector__assembly">
{assembly}:
</div>
<div className="browser-selector__browsers">
{browserList.map(browser =>
<div key={browser} className="browser-selector__browser">
<a href={assemblyBrowsers[browser]} onClick={this.handleClick} rel="noopener noreferrer" target="_blank">
{browser}
</a>
</div>
)}
</div>
</div>
);
})}
</div>
</div>
}
</ModalBody>
<ModalFooter closeModal={<button id="visualize-limit-close" className="btn btn-info" onClick={this.closeModal}>Close</button>} />
</Modal>
: null}
</React.Fragment>
);
}
return null;
}
}
BrowserSelector.propTypes = {
/** Search results that might include visualizations */
results: PropTypes.object.isRequired,
/** Title of accessible text for disabled title; also flag for disabling */
disabledTitle: PropTypes.string,
};
BrowserSelector.defaultProps = {
disabledTitle: '',
};
BrowserSelector.contextTypes = {
location_href: PropTypes.string,
};