jbrowse-plugin-tview 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +349 -69
  2. package/dist/AddHighlightModel/index.js +2 -13
  3. package/dist/AddHighlightModel/index.js.map +1 -1
  4. package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.d.ts +44 -64
  5. package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js +57 -81
  6. package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js.map +1 -1
  7. package/dist/LaunchTView/TviewGetPlanRpc/index.d.ts +1 -1
  8. package/dist/LaunchTView/align.d.ts +18 -17
  9. package/dist/LaunchTView/align.js +198 -151
  10. package/dist/LaunchTView/align.js.map +1 -1
  11. package/dist/LaunchTView/alleles.d.ts +14 -0
  12. package/dist/LaunchTView/alleles.js +51 -3
  13. package/dist/LaunchTView/alleles.js.map +1 -1
  14. package/dist/LaunchTView/fetchTviewPlan.d.ts +5 -37
  15. package/dist/LaunchTView/fetchTviewPlan.js +7 -28
  16. package/dist/LaunchTView/fetchTviewPlan.js.map +1 -1
  17. package/dist/LaunchTView/index.js +45 -16
  18. package/dist/LaunchTView/index.js.map +1 -1
  19. package/dist/LaunchTView/launch.d.ts +50 -0
  20. package/dist/LaunchTView/launch.js +48 -0
  21. package/dist/LaunchTView/launch.js.map +1 -0
  22. package/dist/LaunchTView/launchView.d.ts +3 -8
  23. package/dist/LaunchTView/launchView.js +18 -12
  24. package/dist/LaunchTView/launchView.js.map +1 -1
  25. package/dist/LaunchTView/modifications.d.ts +64 -0
  26. package/dist/LaunchTView/modifications.js +131 -0
  27. package/dist/LaunchTView/modifications.js.map +1 -0
  28. package/dist/LaunchTView/readLayout.d.ts +35 -39
  29. package/dist/LaunchTView/readLayout.js +44 -24
  30. package/dist/LaunchTView/readLayout.js.map +1 -1
  31. package/dist/LaunchTView/repeatStats.d.ts +1 -1
  32. package/dist/LaunchTView/repeatStats.js +6 -6
  33. package/dist/LaunchTView/repeatStats.js.map +1 -1
  34. package/dist/LaunchTView/repeats.js +6 -4
  35. package/dist/LaunchTView/repeats.js.map +1 -1
  36. package/dist/LaunchTView/tview.d.ts +47 -24
  37. package/dist/LaunchTView/tview.js +188 -99
  38. package/dist/LaunchTView/tview.js.map +1 -1
  39. package/dist/TViewPanel/components/TViewPanel.js +9 -4
  40. package/dist/TViewPanel/components/TViewPanel.js.map +1 -1
  41. package/dist/TViewPanel/genome.d.ts +11 -0
  42. package/dist/TViewPanel/genome.js +32 -0
  43. package/dist/TViewPanel/genome.js.map +1 -0
  44. package/dist/TViewPanel/init.d.ts +38 -19
  45. package/dist/TViewPanel/init.js +66 -39
  46. package/dist/TViewPanel/init.js.map +1 -1
  47. package/dist/TViewPanel/initMenu.d.ts +28 -0
  48. package/dist/TViewPanel/initMenu.js +79 -0
  49. package/dist/TViewPanel/initMenu.js.map +1 -0
  50. package/dist/TViewPanel/model.d.ts +1178 -383
  51. package/dist/TViewPanel/model.js +298 -87
  52. package/dist/TViewPanel/model.js.map +1 -1
  53. package/dist/TViewPanel/modifications.d.ts +25 -0
  54. package/dist/TViewPanel/modifications.js +89 -0
  55. package/dist/TViewPanel/modifications.js.map +1 -0
  56. package/dist/index.d.ts +0 -1
  57. package/dist/index.js +0 -1
  58. package/dist/index.js.map +1 -1
  59. package/dist/jbrowse-plugin-tview.umd.production.min.js +125 -89
  60. package/dist/jbrowse-plugin-tview.umd.production.min.js.map +4 -4
  61. package/dist/version.d.ts +1 -1
  62. package/dist/version.js +1 -1
  63. package/package.json +21 -18
  64. package/src/AddHighlightModel/index.tsx +8 -14
  65. package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +119 -157
  66. package/src/LaunchTView/TviewGetPlanRpc/index.ts +0 -1
  67. package/src/LaunchTView/align.ts +233 -177
  68. package/src/LaunchTView/alleles.ts +70 -4
  69. package/src/LaunchTView/fetchTviewPlan.ts +11 -54
  70. package/src/LaunchTView/index.ts +74 -16
  71. package/src/LaunchTView/launch.ts +98 -0
  72. package/src/LaunchTView/launchView.ts +28 -20
  73. package/src/LaunchTView/modifications.ts +200 -0
  74. package/src/LaunchTView/readLayout.ts +65 -41
  75. package/src/LaunchTView/repeatStats.ts +7 -7
  76. package/src/LaunchTView/repeats.ts +6 -4
  77. package/src/LaunchTView/tview.ts +228 -112
  78. package/src/TViewPanel/components/TViewPanel.tsx +33 -4
  79. package/src/TViewPanel/genome.ts +41 -0
  80. package/src/TViewPanel/init.ts +104 -51
  81. package/src/TViewPanel/initMenu.ts +126 -0
  82. package/src/TViewPanel/model.ts +350 -97
  83. package/src/TViewPanel/modifications.ts +127 -0
  84. package/src/index.ts +0 -2
  85. package/src/version.ts +1 -1
  86. package/dist/AddHighlightModel/GenomeMouseoverHighlight.d.ts +0 -6
  87. package/dist/AddHighlightModel/GenomeMouseoverHighlight.js +0 -23
  88. package/dist/AddHighlightModel/GenomeMouseoverHighlight.js.map +0 -1
  89. package/dist/AddHighlightModel/HighlightComponents.d.ts +0 -7
  90. package/dist/AddHighlightModel/HighlightComponents.js +0 -11
  91. package/dist/AddHighlightModel/HighlightComponents.js.map +0 -1
  92. package/dist/LaunchTView/components/ArrayReport.d.ts +0 -10
  93. package/dist/LaunchTView/components/ArrayReport.js +0 -59
  94. package/dist/LaunchTView/components/ArrayReport.js.map +0 -1
  95. package/dist/LaunchTView/components/LaunchTViewDialog.d.ts +0 -6
  96. package/dist/LaunchTView/components/LaunchTViewDialog.js +0 -124
  97. package/dist/LaunchTView/components/LaunchTViewDialog.js.map +0 -1
  98. package/dist/LaunchTView/sampleTree.d.ts +0 -37
  99. package/dist/LaunchTView/sampleTree.js +0 -65
  100. package/dist/LaunchTView/sampleTree.js.map +0 -1
  101. package/dist/LaunchTView/useTviewMsa.d.ts +0 -18
  102. package/dist/LaunchTView/useTviewMsa.js +0 -24
  103. package/dist/LaunchTView/useTviewMsa.js.map +0 -1
  104. package/dist/TViewPanel/coords.d.ts +0 -25
  105. package/dist/TViewPanel/coords.js +0 -56
  106. package/dist/TViewPanel/coords.js.map +0 -1
  107. package/src/AddHighlightModel/GenomeMouseoverHighlight.tsx +0 -46
  108. package/src/AddHighlightModel/HighlightComponents.tsx +0 -25
  109. package/src/LaunchTView/components/ArrayReport.tsx +0 -64
  110. package/src/LaunchTView/components/LaunchTViewDialog.tsx +0 -212
  111. package/src/LaunchTView/sampleTree.ts +0 -73
  112. package/src/LaunchTView/useTviewMsa.ts +0 -39
  113. package/src/TViewPanel/coords.ts +0 -65
@@ -1,19 +1,35 @@
1
1
  import { BaseViewModel } from '@jbrowse/core/pluggableElementTypes'
2
2
  import { getSession } from '@jbrowse/core/util'
3
- import { addDisposer, types } from '@jbrowse/mobx-state-tree'
3
+ import { addDisposer, isAlive, types } from '@jbrowse/mobx-state-tree'
4
4
  import { autorun } from 'mobx'
5
5
  import { MSAModelF } from 'react-msaview'
6
6
 
7
- import { buildColumnToRefPos, renderedColToMsaCol } from './coords'
8
- import { initRegion, initSources } from './init'
7
+ import { hoverPosition, lowerBound } from './genome'
8
+ import { initRegion, initSources, missingTrackIds } from './init'
9
+ import { tviewInitMenuItems } from './initMenu'
10
+ import {
11
+ MODIFICATION_FILL,
12
+ modificationAnnotations,
13
+ modificationMenu,
14
+ } from './modifications'
9
15
  import { fetchTviewPlan } from '../LaunchTView/fetchTviewPlan'
10
16
  import { MAX_CELLS } from '../LaunchTView/limits'
17
+ import { REFERENCE } from '../LaunchTView/tview'
11
18
 
12
19
  import type { TviewInit } from './init'
20
+ import type { ModificationSummary } from './modifications'
21
+ import type { TviewPlanResult } from '../LaunchTView/TviewGetPlanRpc/TviewGetPlan'
13
22
  import type { FetchRegion, TviewSource } from '../LaunchTView/fetchTviewPlan'
23
+ import type { TviewModifications } from '../LaunchTView/modifications'
14
24
  import type { MenuItem } from '@jbrowse/core/ui'
15
25
  import type { Instance } from '@jbrowse/mobx-state-tree'
16
26
  import type { LinearGenomeViewModel } from '@jbrowse/plugin-linear-genome-view'
27
+ import type {
28
+ Highlight,
29
+ ResidueColor,
30
+ RowPanelSpec,
31
+ RowTransform,
32
+ } from 'react-msaview'
17
33
 
18
34
  // re-exported so the inferred (composed) state-model type can name mobx's
19
35
  // IKeyValueMap when emitting declarations (avoids TS2883 portability error)
@@ -29,17 +45,82 @@ export interface IRegion {
29
45
 
30
46
  export type { TviewInit } from './init'
31
47
 
48
+ /** the load the view last started, and the `init` it was started for */
49
+ export type LoadState = { key: string } & (
50
+ | { status: 'loading' }
51
+ | { status: 'loaded' }
52
+ | { status: 'failed'; error: unknown }
53
+ )
54
+
55
+ /** what the panel shows in place of the alignment, or that it shows it */
56
+ export type TViewState =
57
+ | { kind: 'alignment' }
58
+ | { kind: 'loading'; message: string }
59
+ | { kind: 'notice'; message: string }
60
+ | { kind: 'error'; error: unknown; retry: boolean }
61
+
62
+ // how much of the alignment the fitted column width aims to put on screen
63
+ const FIT_WIDTH_PX = 3000
64
+
65
+ const GENOME_HOVER = 'genome-hover'
66
+
67
+ const ROW_HEIGHT = 12
68
+
69
+ const DNA_COLORS: ResidueColor = {
70
+ field: 'residue',
71
+ scale: { scheme: 'jbrowse_dna' },
72
+ }
73
+
74
+ const COPY_PANELS: RowPanelSpec[] = [
75
+ {
76
+ kind: 'bar',
77
+ header: 'copies',
78
+ width: 60,
79
+ encoding: { x: { field: 'copies', scale: { domainMin: 0 } } },
80
+ },
81
+ {
82
+ kind: 'text',
83
+ width: 40,
84
+ encoding: { text: { field: 'copies' } },
85
+ },
86
+ ]
87
+
88
+ function rowTransform(samples: string[], bySubject: boolean): RowTransform {
89
+ return {
90
+ facet: { field: 'sample', sort: [REFERENCE, ...samples] },
91
+ ...(bySubject ? { sort: [{ field: 'copies', order: 'descending' }] } : {}),
92
+ }
93
+ }
94
+
95
+ /**
96
+ * 10px is comfortable for an ordinary pileup, but an unrolled tandem array runs
97
+ * to hundreds of columns and at 10 almost none of it is on screen at once, so
98
+ * the width shrinks to fit roughly two screen-widths.
99
+ */
100
+ function fittedColWidth(columns: number) {
101
+ return Math.max(1, Math.min(10, Math.floor(FIT_WIDTH_PX / columns)))
102
+ }
103
+
104
+ const highlightKey = (h: Highlight) => JSON.stringify(h)
105
+
32
106
  /**
33
107
  * #stateModel TViewPlugin
34
108
  * extends
35
109
  * - MSAModel from https://github.com/GMOD/react-msaview
36
110
  */
37
111
  export default function stateModelFactory() {
112
+ const msaModel = MSAModelF({ maxInlineSnapshotBytes: 0 })
113
+ const msaDefaults = {
114
+ colWidth: msaModel.properties.colWidth.getDefaultInstanceOrSnapshot(),
115
+ rowHeight: msaModel.properties.rowHeight.getDefaultInstanceOrSnapshot(),
116
+ labelsAlignRight:
117
+ msaModel.properties.labelsAlignRight.getDefaultInstanceOrSnapshot(),
118
+ }
38
119
  return types
39
120
  .compose(
40
121
  'TView',
41
122
  BaseViewModel,
42
- MSAModelF(),
123
+ msaModel,
43
124
  types.model({
44
125
  type: types.literal('TView'),
45
126
  /**
@@ -54,37 +135,30 @@ export default function stateModelFactory() {
54
135
  * `./init.ts` for why this one is kept rather than cleared
55
136
  */
56
137
  init: types.frozen<TviewInit | undefined>(),
57
- /**
58
- * #property
59
- * reference region the alignment columns span, filled in by the load
60
- */
61
- msaRegion: types.frozen<IRegion | undefined>(),
62
- /**
63
- * #property
64
- * [refPos, width] for every position where some read has an insertion
65
- */
66
- insertionWidths: types.frozen<[number, number][]>([]),
67
- /**
68
- * #property
69
- * [start, end, columns] for every tandem array laid out per copy
70
- */
71
- arraySpans: types.frozen<[number, number, number][]>([]),
72
138
  /**
73
139
  * #property
74
140
  */
75
141
  zoomToBaseLevel: types.optional(types.boolean, false),
76
142
  }),
77
143
  )
78
- .views(self => ({
144
+ .volatile(() => ({
79
145
  /**
80
- * #getter
146
+ * #volatile
147
+ * the reference region the loaded alignment spans
81
148
  */
82
- get columnToRefPos() {
83
- const { msaRegion, insertionWidths, arraySpans } = self
84
- return msaRegion
85
- ? buildColumnToRefPos({ ...msaRegion, insertionWidths, arraySpans })
86
- : undefined
87
- },
149
+ loadedRegion: undefined as IRegion | undefined,
150
+ /**
151
+ * #volatile
152
+ * the highlights the last load added, which the next one replaces
153
+ */
154
+ loadedHighlights: [] as Highlight[],
155
+ /**
156
+ * #volatile
157
+ * the base modification calls the last load placed, when its reads had any
158
+ */
159
+ loadedModifications: undefined as ModificationSummary | undefined,
160
+ }))
161
+ .views(self => ({
88
162
  /**
89
163
  * #getter
90
164
  */
@@ -108,16 +182,42 @@ export default function stateModelFactory() {
108
182
  get initSources() {
109
183
  return self.init ? initSources(getSession(self), self.init) : undefined
110
184
  },
185
+ /**
186
+ * #getter
187
+ * what a load is keyed on: two inits that say the same thing are one
188
+ */
189
+ get initKey() {
190
+ return self.init ? JSON.stringify(self.init) : undefined
191
+ },
192
+ /**
193
+ * #getter
194
+ * overrides base: a view with an `init` rebuilds what its snapshot drops
195
+ */
196
+ get hostRestoresData() {
197
+ return self.init !== undefined
198
+ },
111
199
  }))
112
200
  .views(self => ({
113
201
  /**
114
202
  * #method
203
+ * the reference base a visible column is read against: the reference
204
+ * row's residue there, or the next one where the reference has a gap
115
205
  */
116
206
  colToGenomeRegion(col: number): IRegion | undefined {
117
- const { columnToRefPos, msaRegion, blanks } = self
118
- const pos = columnToRefPos?.[renderedColToMsaCol(blanks, col)]
119
- return msaRegion && pos !== undefined
120
- ? { refName: msaRegion.refName, start: pos, end: pos + 1 }
207
+ const { loadedRegion } = self
208
+ const index = self.seqPosIndex(REFERENCE)
209
+ const residue = index
210
+ ? lowerBound(index, self.visibleColToGlobalCol(col))
211
+ : undefined
212
+ return loadedRegion &&
213
+ index &&
214
+ residue !== undefined &&
215
+ residue < index.length
216
+ ? {
217
+ refName: loadedRegion.refName,
218
+ start: loadedRegion.start + residue,
219
+ end: loadedRegion.start + residue + 1,
220
+ }
121
221
  : undefined
122
222
  },
123
223
  }))
@@ -141,46 +241,156 @@ export default function stateModelFactory() {
141
241
  },
142
242
  }))
143
243
  .volatile(() => ({
144
- /** a load is in flight, or has failed and should not be retried */
145
- loading: false,
146
- loadFailed: false,
244
+ loadState: undefined as LoadState | undefined,
147
245
  }))
148
- .actions(self => ({
246
+ .views(self => ({
149
247
  /**
150
- * #action
248
+ * #getter
249
+ * the load state of the current `init`, not of one it replaced
151
250
  */
152
- setLoading(arg: boolean) {
153
- self.loading = arg
251
+ get currentLoad() {
252
+ const { loadState, initKey } = self
253
+ return loadState?.key === initKey ? loadState : undefined
154
254
  },
255
+ }))
256
+ .views(self => ({
257
+ /**
258
+ * #getter
259
+ * why `init` cannot be loaded yet, or undefined once it can
260
+ */
261
+ get initBlocker(): TViewState | undefined {
262
+ const { init, initRegion } = self
263
+ const session = getSession(self)
264
+ const assembly = init
265
+ ? session.assemblyManager.get(init.assembly)
266
+ : undefined
267
+ const missing = init ? missingTrackIds(session, init) : []
268
+ return !init
269
+ ? { kind: 'notice', message: 'This view names no region to load' }
270
+ : assembly?.error
271
+ ? { kind: 'error', error: assembly.error, retry: false }
272
+ : !initRegion
273
+ ? {
274
+ kind: 'loading',
275
+ message: `Waiting for assembly ${init.assembly}`,
276
+ }
277
+ : initRegion.error !== undefined
278
+ ? { kind: 'error', error: initRegion.error, retry: false }
279
+ : missing.length
280
+ ? {
281
+ kind: 'error',
282
+ error: `No track in this session has the id ${missing.join(', ')}`,
283
+ retry: false,
284
+ }
285
+ : undefined
286
+ },
287
+ }))
288
+ .views(self => ({
289
+ /**
290
+ * #getter
291
+ */
292
+ get viewState(): TViewState {
293
+ const { init, data, currentLoad, initBlocker } = self
294
+ const loadState: TViewState =
295
+ currentLoad?.status === 'failed'
296
+ ? { kind: 'error', error: currentLoad.error, retry: true }
297
+ : currentLoad?.status === 'loaded'
298
+ ? data.msa
299
+ ? { kind: 'alignment' }
300
+ : {
301
+ kind: 'notice',
302
+ message: 'The region has no reference sequence or reads',
303
+ }
304
+ : {
305
+ kind: 'loading',
306
+ message: 'Building alignment from track data',
307
+ }
308
+ return !init && data.msa
309
+ ? { kind: 'alignment' }
310
+ : (initBlocker ?? loadState)
311
+ },
312
+ }))
313
+ .actions(self => ({
155
314
  /**
156
315
  * #action
157
316
  */
158
- setLoadFailed(arg: boolean) {
159
- self.loadFailed = arg
317
+ setLoadState(arg?: LoadState) {
318
+ self.loadState = arg
160
319
  },
161
320
  /**
162
321
  * #action
163
322
  */
164
323
  setInit(arg?: TviewInit) {
165
324
  self.init = arg
166
- self.loadFailed = false
167
325
  },
168
326
  /**
169
327
  * #action
328
+ * forget a failed load, so the view tries its `init` again
329
+ */
330
+ retryLoad() {
331
+ self.loadState = undefined
332
+ },
333
+ /**
334
+ * #action
335
+ * draws a load's base modification calls as annotations, which are
336
+ * volatile: as many as the cap allows would bloat the session snapshot
170
337
  */
171
- setMsaData(result: {
172
- msa: string
173
- tree?: string
174
- insertionWidths: [number, number][]
175
- arraySpans: [number, number, number][]
176
- region: IRegion
177
- }) {
178
- self.msaRegion = result.region
179
- self.insertionWidths = result.insertionWidths
180
- self.arraySpans = result.arraySpans
181
- self.data.setMSA(result.msa)
182
- if (result.tree) {
183
- self.data.setTree(result.tree)
338
+ setModifications(modifications?: TviewModifications) {
339
+ if (modifications || self.loadedModifications) {
340
+ self.setAnnotations(
341
+ modifications ? modificationAnnotations(modifications) : [],
342
+ )
343
+ }
344
+ if (
345
+ modifications &&
346
+ !self.encodings.some(e => e.channel === 'featureFill')
347
+ ) {
348
+ self.setEncodings([...self.encodings, MODIFICATION_FILL])
349
+ }
350
+ self.loadedModifications = modifications
351
+ ? {
352
+ shown: modifications.row.length,
353
+ dropped: modifications.dropped,
354
+ unplaced: modifications.unplaced,
355
+ }
356
+ : undefined
357
+ },
358
+ /**
359
+ * #action
360
+ * shows a built alignment: its rows, how they are grouped, ordered and
361
+ * marked, and the defaults a view that has not set its own takes
362
+ */
363
+ setAlignment(result: TviewPlanResult) {
364
+ const bySubject = result.hasSubject
365
+ const replaced = new Set(
366
+ [...self.loadedHighlights, ...result.highlights].map(highlightKey),
367
+ )
368
+ self.loadedRegion = result.region
369
+ self.loadedHighlights = result.highlights
370
+ self.setMSA(result.msa)
371
+ this.setModifications(result.modifications)
372
+ self.data.setTree(undefined)
373
+ self.setRowData(result.rowData)
374
+ self.setRowTransform(rowTransform(result.samples, bySubject))
375
+ self.setRowPanels(bySubject ? COPY_PANELS : [])
376
+ self.setHighlights([
377
+ ...self.highlights.filter(h => !replaced.has(highlightKey(h))),
378
+ ...result.highlights,
379
+ ])
380
+ if (self.relativeTo === undefined) {
381
+ self.drawRelativeTo(REFERENCE)
382
+ }
383
+ if (Object.keys(self.residueColor).length === 0) {
384
+ self.setResidueColor(DNA_COLORS)
385
+ }
386
+ if (self.colWidth === msaDefaults.colWidth) {
387
+ self.setColWidth(fittedColWidth(result.columnCount))
388
+ }
389
+ if (self.rowHeight === msaDefaults.rowHeight) {
390
+ self.setRowHeight(ROW_HEIGHT)
391
+ }
392
+ if (self.labelsAlignRight === msaDefaults.labelsAlignRight) {
393
+ self.setLabelsAlignRight(true)
184
394
  }
185
395
  },
186
396
  /**
@@ -204,57 +414,95 @@ export default function stateModelFactory() {
204
414
  }
205
415
  },
206
416
  }))
207
- .actions(self => ({
208
- /**
209
- * #action
210
- * builds the alignment `init` describes, from whatever `init` describes
211
- * it over. The one way an alignment ever gets here.
212
- */
213
- async load(region: FetchRegion, sources: TviewSource[]) {
214
- const session = getSession(self)
215
- self.setLoading(true)
216
- self.setLoadingMSA(true)
217
- self.setStatus({ msg: 'Building alignment from track data' })
218
- try {
219
- const result = await fetchTviewPlan({ session, sources, region })
220
- if (result.tooLarge) {
221
- throw new Error(
222
- `alignment is ${result.cellCount.toLocaleString('en-US')} cells, above the ${MAX_CELLS.toLocaleString('en-US')} limit`,
223
- )
417
+ .actions(self => {
418
+ let controller: AbortController | undefined
419
+ return {
420
+ /**
421
+ * #action
422
+ * builds the alignment `init` describes, from whatever `init`
423
+ * describes it over. The one way an alignment ever gets here. A result
424
+ * for an `init` that is no longer current, or for a view that has been
425
+ * closed, is dropped.
426
+ */
427
+ async load(key: string, region: FetchRegion, sources: TviewSource[]) {
428
+ controller?.abort()
429
+ const own = new AbortController()
430
+ controller = own
431
+ const current = () =>
432
+ controller === own && isAlive(self) && self.initKey === key
433
+ self.setLoadState({ key, status: 'loading' })
434
+ try {
435
+ const result = await fetchTviewPlan({
436
+ session: getSession(self),
437
+ sources,
438
+ region,
439
+ signal: own.signal,
440
+ })
441
+ if (current()) {
442
+ if (result.tooLarge) {
443
+ self.setLoadState({
444
+ key,
445
+ status: 'failed',
446
+ error: `The alignment is ${result.cellCount.toLocaleString('en-US')} cells, above the ${MAX_CELLS.toLocaleString('en-US')} limit`,
447
+ })
448
+ } else {
449
+ self.setAlignment(result)
450
+ self.setLoadState({ key, status: 'loaded' })
451
+ }
452
+ }
453
+ } catch (error) {
454
+ if (current()) {
455
+ console.error(error)
456
+ self.setLoadState({ key, status: 'failed', error })
457
+ }
224
458
  }
225
- self.setMsaData(result)
226
- } catch (e) {
227
- console.error(e)
228
- self.setLoadFailed(true)
229
- session.notify(`Could not build tview alignment: ${e}`, 'error')
230
- } finally {
231
- self.setLoadingMSA(false)
232
- self.setStatus(undefined)
233
- self.setLoading(false)
234
- }
235
- },
236
- }))
459
+ },
460
+ beforeDestroy() {
461
+ controller?.abort()
462
+ },
463
+ }
464
+ })
237
465
  .actions(self => ({
238
466
  afterCreate() {
467
+ self.setImportForm(false)
468
+ addDisposer(
469
+ self,
470
+ autorun(() => {
471
+ const { initKey, initRegion, initSources, loadState } = self
472
+ if (
473
+ initKey &&
474
+ loadState?.key !== initKey &&
475
+ initRegion?.region &&
476
+ initSources
477
+ ) {
478
+ void self.load(initKey, initRegion.region, initSources)
479
+ }
480
+ }),
481
+ )
239
482
  addDisposer(
240
483
  self,
241
484
  autorun(() => {
242
- // Everything that decides whether to load is a getter over `init`,
243
- // so this reads as the one sentence it is: an alignment the view
244
- // does not have, over a region and files it can now resolve. It
245
- // stays armed across the assembly loading and the track configs
246
- // arriving, which is what a restored session needs and what a
247
- // session-authored view needs, without either being a special case.
248
- const { data, initRegion, initSources, loading, loadFailed } = self
485
+ const { loadedRegion, connectedView } = self
486
+ const hover = hoverPosition(getSession(self).hovered)
487
+ const inConnectedView =
488
+ !!connectedView &&
489
+ (hover?.viewId === undefined || hover.viewId === connectedView.id)
490
+ const residue =
491
+ inConnectedView &&
492
+ loadedRegion &&
493
+ hover?.refName === loadedRegion.refName
494
+ ? hover.coord - loadedRegion.start
495
+ : 0
249
496
  if (
250
- !data.msa &&
251
- initRegion &&
252
- initSources &&
253
- !loading &&
254
- !loadFailed
497
+ loadedRegion &&
498
+ residue >= 1 &&
499
+ residue <= loadedRegion.end - loadedRegion.start
255
500
  ) {
256
- // load handles its own failures, so it never rejects
257
- void self.load(initRegion, initSources)
501
+ self.applyHighlight(GENOME_HOVER, [
502
+ { row: REFERENCE, start: residue, end: residue },
503
+ ])
504
+ } else {
505
+ self.clearHighlight(GENOME_HOVER)
258
506
  }
259
507
  }),
260
508
  )
@@ -284,8 +532,13 @@ export default function stateModelFactory() {
284
532
  * overrides base
285
533
  */
286
534
  menuItems(): MenuItem[] {
535
+ const { loadedModifications } = self
287
536
  return [
288
537
  ...superMenuItems(),
538
+ ...tviewInitMenuItems(self),
539
+ ...(loadedModifications
540
+ ? [modificationMenu(loadedModifications, self)]
541
+ : []),
289
542
  {
290
543
  label: 'Zoom to base level on click?',
291
544
  type: 'checkbox',
@@ -0,0 +1,127 @@
1
+ import { MAX_MODIFICATION_CALLS } from '../LaunchTView/modifications'
2
+
3
+ import type { TviewModifications } from '../LaunchTView/modifications'
4
+ import type { MenuItem } from '@jbrowse/core/ui'
5
+ import type { Annotation, ColorEncoding, ResidueColor } from 'react-msaview'
6
+
7
+ /** the SAMtags codes, and the ChEBI ids JBrowse names */
8
+ const NAMES: Record<string, string> = {
9
+ m: '5mC',
10
+ h: '5hmC',
11
+ f: '5fC',
12
+ c: '5caC',
13
+ g: '5hmU',
14
+ e: '5fU',
15
+ b: '5caU',
16
+ a: '6mA',
17
+ o: '8oxoG',
18
+ n: 'xao',
19
+ 17082: 'pseU',
20
+ 17596: 'inosine',
21
+ 21839: '4mC',
22
+ }
23
+
24
+ /** the calls a load placed, and the ones it could not show */
25
+ export interface ModificationSummary {
26
+ shown: number
27
+ dropped: number
28
+ unplaced: number
29
+ }
30
+
31
+ export const MODIFICATION_FILL: ColorEncoding = {
32
+ channel: 'featureFill',
33
+ field: 'probability',
34
+ scale: {
35
+ type: 'linear',
36
+ domainMin: 0,
37
+ domainMax: 100,
38
+ range: ['#0000ff', '#ff0000'],
39
+ },
40
+ legend: { title: 'modification %' },
41
+ }
42
+
43
+ export function modificationName(type: string) {
44
+ return NAMES[type] ?? type
45
+ }
46
+
47
+ export function modificationAnnotations({
48
+ rows,
49
+ types,
50
+ row,
51
+ residue,
52
+ type,
53
+ prob,
54
+ }: TviewModifications): Annotation[] {
55
+ return Array.from(row, (r, i) => {
56
+ const name = modificationName(types[type[i]!]!)
57
+ const percent = Math.round(((prob[i]! + 0.5) / 256) * 100)
58
+ return {
59
+ id: rows[r]!,
60
+ accession: name,
61
+ name,
62
+ description: `${name} ${percent}%`,
63
+ featureType: 'modified_base',
64
+ start: residue[i]!,
65
+ end: residue[i]!,
66
+ attributes: { probability: String(percent) },
67
+ }
68
+ })
69
+ }
70
+
71
+ const n = (x: number) => x.toLocaleString('en-US')
72
+
73
+ export function modificationSummaryLine({
74
+ shown,
75
+ dropped,
76
+ unplaced,
77
+ }: ModificationSummary) {
78
+ return [
79
+ `${n(shown)} calls shown`,
80
+ ...(dropped
81
+ ? [
82
+ `${n(dropped)} less probable over the ${n(MAX_MODIFICATION_CALLS)} cap`,
83
+ ]
84
+ : []),
85
+ ...(unplaced
86
+ ? [`${n(unplaced)} ${unplaced === 1 ? 'row' : 'rows'} not annotated`]
87
+ : []),
88
+ ].join(', ')
89
+ }
90
+
91
+ interface ColoredView {
92
+ residueColor: ResidueColor
93
+ resolvedResidueColor: { field: string }
94
+ setResidueColor: (color: ResidueColor) => void
95
+ }
96
+
97
+ export function modificationMenu(
98
+ summary: ModificationSummary,
99
+ view: ColoredView,
100
+ ) {
101
+ const byModification = view.resolvedResidueColor.field === 'feature'
102
+ const colorBy = (field: 'residue' | 'feature') => () => {
103
+ view.setResidueColor({ ...view.residueColor, field })
104
+ }
105
+ const summaryLine: MenuItem[] =
106
+ summary.dropped || summary.unplaced
107
+ ? [{ type: 'subHeader', label: modificationSummaryLine(summary) }]
108
+ : []
109
+ return {
110
+ label: 'Base modifications',
111
+ subMenu: [
112
+ {
113
+ label: 'Color by bases',
114
+ type: 'radio',
115
+ checked: !byModification,
116
+ onClick: colorBy('residue'),
117
+ },
118
+ {
119
+ label: 'Color by modification',
120
+ type: 'radio',
121
+ checked: byModification,
122
+ onClick: colorBy('feature'),
123
+ },
124
+ ...summaryLine,
125
+ ],
126
+ } satisfies MenuItem
127
+ }
package/src/index.ts CHANGED
@@ -20,6 +20,4 @@ export default class TViewPlugin extends Plugin {
20
20
  TviewGetPlanRpcF(pluginManager)
21
21
  AddHighlightModelF(pluginManager)
22
22
  }
23
-
24
- configure(_pluginManager: PluginManager) {}
25
23
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '2.2.1'
1
+ export const version = '3.0.0'