/
model.tsx
355 lines (333 loc) · 9.08 KB
/
model.tsx
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
import React from 'react'
import { autorun, when } from 'mobx'
import {
addDisposer,
getSnapshot,
isAlive,
types,
Instance,
IStateTreeNode,
} from 'mobx-state-tree'
import deepEqual from 'fast-deep-equal'
// jbrowse
import {
AnyConfigurationModel,
AnyConfigurationSchemaType,
getConf,
} from '@jbrowse/core/configuration'
import { BaseDisplay } from '@jbrowse/core/pluggableElementTypes/models'
import PluginManager from '@jbrowse/core/PluginManager'
import { MenuItem } from '@jbrowse/core/ui'
import { FeatureDensityStats } from '@jbrowse/core/data_adapters/BaseAdapter'
// locals
import { LinearAlignmentsDisplayMixin } from './alignmentsModel'
import { getLowerPanelDisplays } from './util'
const minDisplayHeight = 20
function deepSnap<T extends IStateTreeNode, U extends IStateTreeNode>(
x1: T,
x2: U,
) {
return deepEqual(
x1 ? getSnapshot(x1) : undefined,
x2 ? getSnapshot(x2) : undefined,
)
}
function preCheck(self: AlignmentsDisplayModel) {
const { PileupDisplay, SNPCoverageDisplay } = self
return (
PileupDisplay ||
isAlive(PileupDisplay) ||
SNPCoverageDisplay ||
isAlive(SNPCoverageDisplay)
)
}
function propagateColorBy(self: AlignmentsDisplayModel) {
const { PileupDisplay, SNPCoverageDisplay } = self
if (!preCheck(self) || !PileupDisplay.colorBy) {
return
}
if (!deepSnap(PileupDisplay.colorBy, SNPCoverageDisplay.colorBy)) {
SNPCoverageDisplay.setColorBy(getSnapshot(PileupDisplay.colorBy))
}
}
function propagateFilterBy(self: AlignmentsDisplayModel) {
const { PileupDisplay, SNPCoverageDisplay } = self
if (!preCheck(self) || !PileupDisplay.filterBy) {
return
}
if (!deepSnap(PileupDisplay.filterBy, SNPCoverageDisplay.filterBy)) {
SNPCoverageDisplay.setFilterBy(getSnapshot(PileupDisplay.filterBy))
}
}
/**
* #stateModel LinearAlignmentsDisplay
* extends
* - [BaseDisplay](../basedisplay)
* - [LinearAlignmentsDisplayMixin](../linearalignmentsdisplaymixin)
*/
function stateModelFactory(
pluginManager: PluginManager,
configSchema: AnyConfigurationSchemaType,
) {
return types
.compose(
'LinearAlignmentsDisplay',
BaseDisplay,
LinearAlignmentsDisplayMixin(pluginManager, configSchema),
)
.volatile(() => ({
scrollTop: 0,
}))
.actions(self => ({
/**
* #action
*/
setScrollTop(scrollTop: number) {
self.scrollTop = scrollTop
},
/**
* #action
*/
setSNPCoverageHeight(n: number) {
self.snpCovHeight = n
},
}))
.views(self => ({
/**
* #getter
*/
get height() {
return self.heightPreConfig ?? getConf(self, 'height')
},
/**
* #getter
*/
get featureIdUnderMouse() {
return (
self.PileupDisplay.featureIdUnderMouse ||
self.SNPCoverageDisplay.featureIdUnderMouse
)
},
}))
.views(self => ({
/**
* #getter
*/
get pileupConf() {
const conf = getConf(self, 'pileupDisplay')
return {
...conf,
type: self.lowerPanelType,
displayId: `${self.configuration.displayId}_${self.lowerPanelType}_xyz`, // xyz to avoid someone accidentally naming the displayId similar to this
}
},
/**
* #method
*/
getFeatureByID(blockKey: string, id: string) {
return self.PileupDisplay.getFeatureByID(blockKey, id)
},
/**
* #method
*/
searchFeatureByID(id: string) {
return self.PileupDisplay.searchFeatureByID?.(id)
},
/**
* #getter
*/
get features() {
return self.PileupDisplay.features
},
/**
* #getter
*/
get DisplayBlurb() {
return self.PileupDisplay?.DisplayBlurb
},
/**
* #getter
*/
get sortedBy() {
return self.PileupDisplay.sortedBy
},
/**
* #getter
*/
get coverageConf() {
const conf = getConf(self, 'snpCoverageDisplay')
return {
...conf,
displayId: `${self.configuration.displayId}_snpcoverage_xyz`, // xyz to avoid someone accidentally naming the displayId similar to this
}
},
}))
.actions(self => ({
/**
* #action
*/
setSNPCoverageDisplay(configuration: AnyConfigurationModel) {
self.SNPCoverageDisplay = {
type: 'LinearSNPCoverageDisplay',
configuration,
height: self.snpCovHeight,
}
},
/**
* #action
*/
setFeatureDensityStatsLimit(stats?: FeatureDensityStats) {
self.PileupDisplay.setFeatureDensityStatsLimit(stats)
self.SNPCoverageDisplay.setFeatureDensityStatsLimit(stats)
},
/**
* #action
*/
setPileupDisplay(configuration: AnyConfigurationModel) {
self.PileupDisplay = {
type: configuration.type || 'LinearPileupDisplay',
configuration,
}
},
/**
* #action
*/
setHeight(n: number) {
self.heightPreConfig = Math.max(n, minDisplayHeight)
return self.heightPreConfig
},
/**
* #action
*/
setLowerPanelType(type: string) {
self.lowerPanelType = type
},
/**
* #action
*/
resizeHeight(distance: number) {
const oldHeight = self.height
const newHeight = this.setHeight(self.height + distance)
return newHeight - oldHeight
},
}))
.actions(self => ({
afterAttach() {
addDisposer(
self,
autorun(() => {
const {
SNPCoverageDisplay,
PileupDisplay,
coverageConf,
pileupConf,
} = self
if (!SNPCoverageDisplay) {
self.setSNPCoverageDisplay(coverageConf)
} else if (
!deepEqual(
coverageConf,
getSnapshot(SNPCoverageDisplay.configuration),
)
) {
SNPCoverageDisplay.setHeight(self.snpCovHeight)
SNPCoverageDisplay.setConfig(self.coverageConf)
}
if (!PileupDisplay || self.lowerPanelType !== PileupDisplay.type) {
self.setPileupDisplay(pileupConf)
} else if (
!deepEqual(pileupConf, getSnapshot(PileupDisplay.configuration))
) {
PileupDisplay.setConfig(self.pileupConf)
}
propagateColorBy(self as AlignmentsDisplayModel)
propagateFilterBy(self as AlignmentsDisplayModel)
}),
)
addDisposer(
self,
autorun(() => {
self.setSNPCoverageHeight(self.SNPCoverageDisplay.height)
}),
)
addDisposer(
self,
autorun(() => {
self.PileupDisplay.setHeight(
self.height - self.SNPCoverageDisplay.height,
)
}),
)
},
/**
* #action
*/
async renderSvg(opts: { rasterizeLayers?: boolean }) {
const pileupHeight = self.height - self.SNPCoverageDisplay.height
await when(
() =>
!self.PileupDisplay.renderProps().notReady &&
!self.SNPCoverageDisplay.renderProps().notReady,
)
return (
<>
<g>{await self.SNPCoverageDisplay.renderSvg(opts)}</g>
<g transform={`translate(0 ${self.SNPCoverageDisplay.height})`}>
{await self.PileupDisplay.renderSvg({
...opts,
overrideHeight: pileupHeight,
})}
</g>
</>
)
},
}))
.views(self => {
const { trackMenuItems: superTrackMenuItems } = self
return {
/**
* #method
*/
trackMenuItems(): MenuItem[] {
if (!self.PileupDisplay) {
return []
}
const extra = getLowerPanelDisplays(pluginManager).map(d => ({
type: 'radio',
label: d.displayName,
checked: d.name === self.PileupDisplay.type,
onClick: () => self.setLowerPanelType(d.name),
}))
return [
...superTrackMenuItems(),
{
type: 'subMenu',
label: 'Pileup settings',
subMenu: self.PileupDisplay.trackMenuItems(),
},
{
type: 'subMenu',
label: 'SNPCoverage settings',
subMenu: self.SNPCoverageDisplay.trackMenuItems(),
},
{
type: 'subMenu',
label: `Replace lower panel with...`,
subMenu: extra,
},
]
},
}
})
.preProcessSnapshot(snap => {
if (!snap) {
return snap
}
// @ts-expect-error
const { height, ...rest } = snap
return { heightPreConfig: height, ...rest }
})
}
export default stateModelFactory
export type AlignmentsDisplayStateModel = ReturnType<typeof stateModelFactory>
export type AlignmentsDisplayModel = Instance<AlignmentsDisplayStateModel>