-
Notifications
You must be signed in to change notification settings - Fork 63
/
GeneTrack.js
202 lines (169 loc) · 6.76 KB
/
GeneTrack.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
/**
* Visualization of genes, including exons and coding regions.
* @flow
*/
'use strict';
import type {Strand} from '../Alignment';
import Gene from '../data/gene';
import type {DataSource} from '../sources/DataSource';
import type {VizProps} from '../VisualizationWrapper';
import type {State} from '../types';
import GenericFeature from '../data/genericFeature';
import {GenericFeatureCache} from './GenericFeatureCache';
import React from 'react';
import _ from 'underscore';
import shallowEquals from 'shallow-equals';
import bedtools from '../data/bedtools';
import Interval from '../Interval';
import d3utils from './d3utils';
import scale from '../scale';
import ContigInterval from '../ContigInterval';
import canvasUtils from './canvas-utils';
import utils from '../utils';
import dataCanvas from 'data-canvas';
import style from '../style';
// Draw an arrow in the middle of the visible portion of range.
function drawArrow(ctx: CanvasRenderingContext2D,
clampedScale: (x: number)=>number,
range: Interval,
tipY: number,
strand: Strand) {
var x1 = clampedScale(1 + range.start),
x2 = clampedScale(1 + range.stop);
// it's off-screen or there's not enough room to draw it legibly.
if (x2 - x1 <= 2 * style.GENE_ARROW_SIZE) return;
var cx = (x1 + x2) / 2;
ctx.beginPath();
if (strand == '-') {
ctx.moveTo(cx + style.GENE_ARROW_SIZE, tipY - style.GENE_ARROW_SIZE);
ctx.lineTo(cx, tipY);
ctx.lineTo(cx + style.GENE_ARROW_SIZE, tipY + style.GENE_ARROW_SIZE);
} else {
ctx.moveTo(cx - style.GENE_ARROW_SIZE, tipY - style.GENE_ARROW_SIZE);
ctx.lineTo(cx, tipY);
ctx.lineTo(cx - style.GENE_ARROW_SIZE, tipY + style.GENE_ARROW_SIZE);
}
ctx.stroke();
}
function drawGeneName(ctx: CanvasRenderingContext2D,
clampedScale: (x: number)=>number,
geneLineY: number,
gene: Gene,
textIntervals: Interval[]) {
var p = gene.position,
centerX = 0.5 * (clampedScale(1 + p.start()) + clampedScale(1 + p.stop()));
// do not use gene name if it is null or empty
var name = !_.isEmpty(utils.stringToLiteral(gene.name)) ? gene.name : gene.id;
var textWidth = ctx.measureText(name).width;
var textInterval = new Interval(centerX - 0.5 * textWidth,
centerX + 0.5 * textWidth);
if (!_.any(textIntervals, iv => textInterval.intersects(iv))) {
textIntervals.push(textInterval);
var baselineY = geneLineY + style.GENE_FONT_SIZE + style.GENE_TEXT_PADDING;
ctx.fillText(name, centerX, baselineY);
}
}
class GeneTrack extends React.Component<VizProps<DataSource<Gene>>, State> {
props: VizProps<DataSource<Gene>>;
state: State;
cache: GenericFeatureCache;
ref: Object;
constructor(props: VizProps<DataSource<Gene>>) {
super(props);
this.ref = React.createRef();
this.state = {
networkStatus: null
};
}
render(): any {
return <canvas ref={this.ref} />;
}
componentDidMount() {
this.cache = new GenericFeatureCache(this.props.referenceSource);
// Visualize new reference data as it comes in from the network.
this.props.source.on('newdata', (range) => {
// add genes to generic cache
var genes = this.props.source.getFeaturesInRange(range);
genes.forEach(f => this.cache.addFeature(new GenericFeature(f.id, f.position, f)));
this.updateVisualization();
});
this.props.referenceSource.on('newdata', range => {
this.updateVisualization();
});
this.props.source.on('networkprogress', e => {
this.setState({networkStatus: e});
});
this.props.source.on('networkdone', e => {
this.setState({networkStatus: null});
});
this.updateVisualization();
}
getScale(): any {
return d3utils.getTrackScale(this.props.range, this.props.width);
}
componentDidUpdate(prevProps: any, prevState: any) {
if (!shallowEquals(this.props, prevProps) ||
!shallowEquals(this.state, prevState)) {
this.updateVisualization();
}
}
updateVisualization() {
const canvas = this.ref.current;
var {width, height} = this.props,
genomeRange = this.props.range;
var range = new ContigInterval(genomeRange.contig, genomeRange.start, genomeRange.stop);
// Hold off until height & width are known.
if (width === 0) return;
var sc = this.getScale(),
// We can't clamp scale directly because of offsetPx.
clampedScale = scale.linear()
.domain([sc.invert(0), sc.invert(width)])
.range([0, width])
.clamp(true);
if (canvas && canvas instanceof Element) { // check for getContext
if (canvas instanceof HTMLCanvasElement) { // check for sizeCanvas
d3utils.sizeCanvas(canvas, width, height);
}
var ctx = dataCanvas.getDataContext(canvasUtils.getContext(canvas));
ctx.reset();
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
var geneLineY = Math.round(height / 4);
var textIntervals = []; // x-intervals with rendered gene names, to avoid over-drawing.
ctx.font = `${style.GENE_FONT_SIZE}px ${style.GENE_FONT}`;
ctx.textAlign = 'center';
var vFeatures = _.flatten(_.map(this.cache.getGroupsOverlapping(range),
g => g.items));
vFeatures.forEach(vFeature => {
var gene = vFeature.gFeature;
if (!gene.position.intersects(range)) return;
ctx.pushObject(gene);
ctx.lineWidth = 1;
ctx.strokeStyle = style.GENE_COLOR;
ctx.fillStyle = style.GENE_COLOR;
canvasUtils.drawLine(ctx, clampedScale(1 + gene.position.start()), geneLineY + 0.5,
clampedScale(1 + gene.position.stop()), geneLineY + 0.5);
// TODO: only compute all these intervals when data becomes available.
var exons = bedtools.splitCodingExons(gene.exons, gene.codingRegion);
exons.forEach(exon => {
ctx.fillRect(sc(1 + exon.start),
geneLineY - 3 * (exon.isCoding ? 2 : 1),
sc(exon.stop + 2) - sc(1 + exon.start),
6 * (exon.isCoding ? 2 : 1));
});
var introns = gene.position.interval.complementIntervals(gene.exons);
introns.forEach(range => {
drawArrow(ctx, clampedScale, range, geneLineY + 0.5, gene.strand);
});
ctx.strokeStyle = style.GENE_COMPLEMENT_COLOR;
ctx.lineWidth = 2;
gene.exons.forEach(range => {
drawArrow(ctx, clampedScale, range, geneLineY + 0.5, gene.strand);
});
drawGeneName(ctx, clampedScale, geneLineY, gene, textIntervals);
ctx.popObject();
});
} // end typecheck for canvas
}
}
GeneTrack.displayName = 'genes';
module.exports = GeneTrack;