-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdrawSequenceOverlay.ts
More file actions
181 lines (173 loc) · 4.47 KB
/
drawSequenceOverlay.ts
File metadata and controls
181 lines (173 loc) · 4.47 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
import { type AnnotationFeature } from '@apollo-annotation/mst'
import { type Frame, getFrame } from '@jbrowse/core/util'
import { type BlockSet, type ContentBlock } from '@jbrowse/core/util/blockTypes'
import { type Theme } from '@mui/material'
import { type ApolloSessionModel, type HoveredFeature } from '../session'
function getSeqRow(
strand: 1 | -1 | undefined,
bpPerPx: number,
): number | undefined {
if (bpPerPx > 1 || strand === undefined) {
return
}
return strand === 1 ? 3 : 4
}
function getTranslationRow(frame: Frame, bpPerPx: number) {
const offset = bpPerPx <= 1 ? 2 : 0
switch (frame) {
case 3: {
return 0
}
case 2: {
return 1
}
case 1: {
return 2
}
case -1: {
return 3 + offset
}
case -2: {
return 4 + offset
}
case -3: {
return 5 + offset
}
}
}
function getLeftPx(
feature: { min: number; max: number },
bpPerPx: number,
offsetPx: number,
block: ContentBlock,
) {
const blockLeftPx = block.offsetPx - offsetPx
const featureLeftBpDistanceFromBlockLeftBp = block.reversed
? block.end - feature.max
: feature.min - block.start
const featureLeftPxDistanceFromBlockLeftPx =
featureLeftBpDistanceFromBlockLeftBp / bpPerPx
return blockLeftPx + featureLeftPxDistanceFromBlockLeftPx
}
function fillAndStrokeRect(
ctx: CanvasRenderingContext2D,
left: number,
top: number,
width: number,
height: number,
theme: Theme,
selected = false,
) {
ctx.fillStyle = selected
? theme.palette.action.disabled
: theme.palette.action.focus
ctx.fillRect(left, top, width, height)
ctx.strokeStyle = selected
? theme.palette.text.secondary
: theme.palette.text.primary
ctx.strokeStyle = theme.palette.text.primary
ctx.strokeRect(left, top, width, height)
}
function drawHighlight(
ctx: CanvasRenderingContext2D,
feature: AnnotationFeature,
bpPerPx: number,
offsetPx: number,
rowHeight: number,
block: ContentBlock,
theme: Theme,
selected = false,
) {
const row = getSeqRow(feature.strand, bpPerPx)
if (!row) {
return
}
const left = getLeftPx(feature, bpPerPx, offsetPx, block)
const width = feature.length / bpPerPx
const top = row * rowHeight
fillAndStrokeRect(ctx, left, top, width, rowHeight, theme, selected)
}
function drawCDSHighlight(
ctx: CanvasRenderingContext2D,
feature: AnnotationFeature,
bpPerPx: number,
offsetPx: number,
rowHeight: number,
block: ContentBlock,
theme: Theme,
selected = false,
) {
const parentFeature = feature.parent
if (!parentFeature) {
return
}
const cdsLocs = parentFeature.cdsLocations.find((loc) => {
const min = loc.at(feature.strand === 1 ? 0 : -1)?.min
const max = loc.at(feature.strand === 1 ? -1 : 0)?.max
return feature.min === min && feature.max === max
})
if (!cdsLocs) {
return
}
for (const loc of cdsLocs) {
const frame = getFrame(loc.min, loc.max, feature.strand ?? 1, loc.phase)
const row = getTranslationRow(frame, bpPerPx)
const left = getLeftPx(loc, bpPerPx, offsetPx, block)
const top = row * rowHeight
const width = (loc.max - loc.min) / bpPerPx
fillAndStrokeRect(ctx, left, top, width, rowHeight, theme, selected)
}
}
export function drawSequenceOverlay(
canvas: HTMLCanvasElement,
ctx: CanvasRenderingContext2D,
hoveredFeature: HoveredFeature | undefined,
selectedFeature: AnnotationFeature | undefined,
rowHeight: number,
theme: Theme,
session: ApolloSessionModel,
bpPerPx: number,
offsetPx: number,
dynamicBlocks: BlockSet,
) {
const { featureTypeOntology } = session.apolloDataStore.ontologyManager
if (!featureTypeOntology) {
throw new Error('featureTypeOntology is undefined')
}
for (const block of dynamicBlocks.contentBlocks) {
ctx.save()
ctx.beginPath()
const blockLeftPx = block.offsetPx - offsetPx
ctx.rect(blockLeftPx, 0, block.widthPx, canvas.height)
ctx.clip()
for (const feature of [
selectedFeature,
hoveredFeature?.feature,
].filter<AnnotationFeature>((f) => f !== undefined)) {
if (featureTypeOntology.isTypeOf(feature.type, 'CDS')) {
drawCDSHighlight(
ctx,
feature,
bpPerPx,
offsetPx,
rowHeight,
block,
theme,
true,
)
} else {
drawHighlight(
ctx,
feature,
bpPerPx,
offsetPx,
rowHeight,
block,
theme,
true,
)
}
}
ctx.restore()
}
}