jbrowse-plugin-tview 2.2.3 → 3.1.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 (110) hide show
  1. package/README.md +325 -106
  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 +11 -17
  9. package/dist/LaunchTView/align.js +190 -150
  10. package/dist/LaunchTView/align.js.map +1 -1
  11. package/dist/LaunchTView/alleles.d.ts +51 -0
  12. package/dist/LaunchTView/alleles.js +213 -0
  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 -21
  33. package/dist/LaunchTView/repeatStats.js.map +1 -1
  34. package/dist/LaunchTView/tview.d.ts +34 -23
  35. package/dist/LaunchTView/tview.js +133 -78
  36. package/dist/LaunchTView/tview.js.map +1 -1
  37. package/dist/TViewPanel/components/TViewPanel.js +9 -4
  38. package/dist/TViewPanel/components/TViewPanel.js.map +1 -1
  39. package/dist/TViewPanel/genome.d.ts +11 -0
  40. package/dist/TViewPanel/genome.js +32 -0
  41. package/dist/TViewPanel/genome.js.map +1 -0
  42. package/dist/TViewPanel/init.d.ts +38 -19
  43. package/dist/TViewPanel/init.js +66 -39
  44. package/dist/TViewPanel/init.js.map +1 -1
  45. package/dist/TViewPanel/initMenu.d.ts +28 -0
  46. package/dist/TViewPanel/initMenu.js +79 -0
  47. package/dist/TViewPanel/initMenu.js.map +1 -0
  48. package/dist/TViewPanel/model.d.ts +1090 -334
  49. package/dist/TViewPanel/model.js +295 -89
  50. package/dist/TViewPanel/model.js.map +1 -1
  51. package/dist/TViewPanel/modifications.d.ts +25 -0
  52. package/dist/TViewPanel/modifications.js +94 -0
  53. package/dist/TViewPanel/modifications.js.map +1 -0
  54. package/dist/index.d.ts +0 -1
  55. package/dist/index.js +0 -1
  56. package/dist/index.js.map +1 -1
  57. package/dist/jbrowse-plugin-tview.umd.production.min.js +125 -88
  58. package/dist/jbrowse-plugin-tview.umd.production.min.js.map +4 -4
  59. package/dist/version.d.ts +1 -1
  60. package/dist/version.js +1 -1
  61. package/package.json +8 -8
  62. package/src/AddHighlightModel/index.tsx +8 -14
  63. package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +119 -157
  64. package/src/LaunchTView/TviewGetPlanRpc/index.ts +0 -1
  65. package/src/LaunchTView/align.ts +225 -176
  66. package/src/LaunchTView/alleles.ts +272 -0
  67. package/src/LaunchTView/fetchTviewPlan.ts +11 -54
  68. package/src/LaunchTView/index.ts +74 -16
  69. package/src/LaunchTView/launch.ts +98 -0
  70. package/src/LaunchTView/launchView.ts +28 -20
  71. package/src/LaunchTView/modifications.ts +200 -0
  72. package/src/LaunchTView/readLayout.ts +65 -41
  73. package/src/LaunchTView/repeatStats.ts +7 -23
  74. package/src/LaunchTView/tview.ts +165 -91
  75. package/src/TViewPanel/components/TViewPanel.tsx +30 -5
  76. package/src/TViewPanel/genome.ts +41 -0
  77. package/src/TViewPanel/init.ts +104 -51
  78. package/src/TViewPanel/initMenu.ts +126 -0
  79. package/src/TViewPanel/model.ts +346 -97
  80. package/src/TViewPanel/modifications.ts +132 -0
  81. package/src/index.ts +0 -2
  82. package/src/version.ts +1 -1
  83. package/dist/AddHighlightModel/GenomeMouseoverHighlight.d.ts +0 -6
  84. package/dist/AddHighlightModel/GenomeMouseoverHighlight.js +0 -23
  85. package/dist/AddHighlightModel/GenomeMouseoverHighlight.js.map +0 -1
  86. package/dist/AddHighlightModel/HighlightComponents.d.ts +0 -7
  87. package/dist/AddHighlightModel/HighlightComponents.js +0 -11
  88. package/dist/AddHighlightModel/HighlightComponents.js.map +0 -1
  89. package/dist/LaunchTView/components/ArrayReport.d.ts +0 -10
  90. package/dist/LaunchTView/components/ArrayReport.js +0 -59
  91. package/dist/LaunchTView/components/ArrayReport.js.map +0 -1
  92. package/dist/LaunchTView/components/LaunchTViewDialog.d.ts +0 -6
  93. package/dist/LaunchTView/components/LaunchTViewDialog.js +0 -124
  94. package/dist/LaunchTView/components/LaunchTViewDialog.js.map +0 -1
  95. package/dist/LaunchTView/sampleTree.d.ts +0 -37
  96. package/dist/LaunchTView/sampleTree.js +0 -65
  97. package/dist/LaunchTView/sampleTree.js.map +0 -1
  98. package/dist/LaunchTView/useTviewMsa.d.ts +0 -18
  99. package/dist/LaunchTView/useTviewMsa.js +0 -24
  100. package/dist/LaunchTView/useTviewMsa.js.map +0 -1
  101. package/dist/TViewPanel/coords.d.ts +0 -25
  102. package/dist/TViewPanel/coords.js +0 -56
  103. package/dist/TViewPanel/coords.js.map +0 -1
  104. package/src/AddHighlightModel/GenomeMouseoverHighlight.tsx +0 -46
  105. package/src/AddHighlightModel/HighlightComponents.tsx +0 -25
  106. package/src/LaunchTView/components/ArrayReport.tsx +0 -64
  107. package/src/LaunchTView/components/LaunchTViewDialog.tsx +0 -212
  108. package/src/LaunchTView/sampleTree.ts +0 -73
  109. package/src/LaunchTView/useTviewMsa.ts +0 -39
  110. package/src/TViewPanel/coords.ts +0 -65
@@ -0,0 +1,126 @@
1
+ import { getSession } from '@jbrowse/core/util'
2
+
3
+ import { entryTrackId, trackLabel } from './init'
4
+ import {
5
+ alignmentsTrackIds,
6
+ isGenomeView,
7
+ regionLaunch,
8
+ } from '../LaunchTView/launch'
9
+
10
+ import type { TviewInit } from './init'
11
+ import type { RegionLaunch } from '../LaunchTView/launch'
12
+ import type { MenuItem } from '@jbrowse/core/ui'
13
+ import type { IAnyStateTreeNode } from '@jbrowse/mobx-state-tree'
14
+
15
+ export const TRACKS_LABEL = 'Tracks'
16
+ export const CURRENT_REGION_LABEL = "Show the connected view's current region"
17
+
18
+ export interface TrackChoice {
19
+ trackId: string
20
+ label: string
21
+ }
22
+
23
+ /** the alignments tracks open in the genome view, then any others `init` reads */
24
+ export function trackChoiceIds(init: TviewInit, openTrackIds: string[]) {
25
+ const open = new Set(openTrackIds)
26
+ return [
27
+ ...openTrackIds,
28
+ ...init.tracks.map(entryTrackId).filter(trackId => !open.has(trackId)),
29
+ ]
30
+ }
31
+
32
+ export function withTrack(
33
+ init: TviewInit,
34
+ trackId: string,
35
+ included: boolean,
36
+ ): TviewInit {
37
+ const others = init.tracks.filter(entry => entryTrackId(entry) !== trackId)
38
+ return {
39
+ ...init,
40
+ tracks: included
41
+ ? others.length === init.tracks.length
42
+ ? [...init.tracks, trackId]
43
+ : init.tracks
44
+ : others,
45
+ }
46
+ }
47
+
48
+ export function atRegion(
49
+ init: TviewInit,
50
+ { assembly, loc }: { assembly: string; loc: string },
51
+ ): TviewInit {
52
+ return { ...init, assembly, loc }
53
+ }
54
+
55
+ export function initMenuItems({
56
+ init,
57
+ choices,
58
+ region,
59
+ setInit,
60
+ }: {
61
+ init: TviewInit
62
+ choices: TrackChoice[]
63
+ region: RegionLaunch
64
+ setInit: (init: TviewInit) => void
65
+ }): MenuItem[] {
66
+ const included = new Set(init.tracks.map(entryTrackId))
67
+ return [
68
+ {
69
+ label: TRACKS_LABEL,
70
+ type: 'subMenu',
71
+ subMenu: choices.map(({ trackId, label }) => {
72
+ const checked = included.has(trackId)
73
+ return {
74
+ label,
75
+ type: 'checkbox',
76
+ checked,
77
+ disabled: checked && included.size === 1,
78
+ disabledHelpText: 'A tview reads at least one track',
79
+ onClick: () => {
80
+ setInit(withTrack(init, trackId, !checked))
81
+ },
82
+ }
83
+ }),
84
+ },
85
+ {
86
+ label: CURRENT_REGION_LABEL,
87
+ disabled: region.reason !== undefined,
88
+ subLabel: region.reason,
89
+ onClick: () => {
90
+ if (region.reason === undefined) {
91
+ setInit(atRegion(init, region))
92
+ }
93
+ },
94
+ },
95
+ ]
96
+ }
97
+
98
+ export function tviewInitMenuItems(
99
+ view: IAnyStateTreeNode & {
100
+ init?: TviewInit
101
+ connectedView?: object
102
+ setInit: (init: TviewInit) => void
103
+ },
104
+ ) {
105
+ const { init } = view
106
+ const connectedView =
107
+ view.connectedView && isGenomeView(view.connectedView)
108
+ ? view.connectedView
109
+ : undefined
110
+ const session = getSession(view)
111
+ return init
112
+ ? initMenuItems({
113
+ init,
114
+ choices: trackChoiceIds(
115
+ init,
116
+ connectedView ? alignmentsTrackIds(connectedView.tracks) : [],
117
+ ).map(trackId => ({ trackId, label: trackLabel(session, trackId) })),
118
+ region: connectedView
119
+ ? regionLaunch(session, connectedView.visibleWholeBaseRegions)
120
+ : { reason: 'No connected genome view' },
121
+ setInit: next => {
122
+ view.setInit(next)
123
+ },
124
+ })
125
+ : []
126
+ }
@@ -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_MARK,
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
+ ResidueColorDefault,
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,6 +45,70 @@ 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
+ const LOAD_BRACKETS = 'tview-load'
67
+
68
+ const ROW_HEIGHT = 12
69
+
70
+ const DNA_COLORS: ResidueColorDefault = {
71
+ field: 'residue',
72
+ scale: { scheme: 'jbrowse_dna' },
73
+ }
74
+
75
+ const COPY_PANELS: RowPanelSpec[] = [
76
+ {
77
+ kind: 'bar',
78
+ header: 'copies',
79
+ width: 60,
80
+ encoding: { x: { field: 'copies', scale: { domainMin: 0 } } },
81
+ },
82
+ {
83
+ kind: 'text',
84
+ width: 40,
85
+ encoding: { text: { field: 'copies' } },
86
+ },
87
+ ]
88
+
89
+ function rowTransform(samples: string[], bySubject: boolean): RowTransform {
90
+ return {
91
+ facet: { field: 'sample', sort: [REFERENCE, ...samples] },
92
+ ...(bySubject ? { sort: [{ field: 'copies', order: 'descending' }] } : {}),
93
+ }
94
+ }
95
+
96
+ /**
97
+ * 10px is comfortable for an ordinary pileup, but an unrolled tandem array runs
98
+ * to hundreds of columns and at 10 almost none of it is on screen at once, so
99
+ * the width shrinks to fit roughly two screen-widths.
100
+ */
101
+ const MAX_COL_WIDTH = 10
102
+
103
+ function fittedColWidth(columns: number) {
104
+ return Math.max(
105
+ 1,
106
+ Math.min(MAX_COL_WIDTH, Math.floor(FIT_WIDTH_PX / columns)),
107
+ )
108
+ }
109
+
110
+ const highlightKey = (h: Highlight) => JSON.stringify(h)
111
+
32
112
  /**
33
113
  * #stateModel TViewPlugin
34
114
  * extends
@@ -39,7 +119,16 @@ export default function stateModelFactory() {
39
119
  .compose(
40
120
  'TView',
41
121
  BaseViewModel,
42
- MSAModelF(),
122
+ MSAModelF({
123
+ maxInlineSnapshotBytes: 0,
124
+ defaults: {
125
+ colWidth: MAX_COL_WIDTH,
126
+ rowHeight: ROW_HEIGHT,
127
+ labelsAlignRight: true,
128
+ residueColor: DNA_COLORS,
129
+ turnedOffTracks: { 'position-ruler': false },
130
+ },
131
+ }),
43
132
  types.model({
44
133
  type: types.literal('TView'),
45
134
  /**
@@ -54,37 +143,25 @@ export default function stateModelFactory() {
54
143
  * `./init.ts` for why this one is kept rather than cleared
55
144
  */
56
145
  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
146
  /**
73
147
  * #property
74
148
  */
75
149
  zoomToBaseLevel: types.optional(types.boolean, false),
76
150
  }),
77
151
  )
78
- .views(self => ({
152
+ .volatile(() => ({
79
153
  /**
80
- * #getter
154
+ * #volatile
155
+ * the reference region the loaded alignment spans
81
156
  */
82
- get columnToRefPos() {
83
- const { msaRegion, insertionWidths, arraySpans } = self
84
- return msaRegion
85
- ? buildColumnToRefPos({ ...msaRegion, insertionWidths, arraySpans })
86
- : undefined
87
- },
157
+ loadedRegion: undefined as IRegion | undefined,
158
+ /**
159
+ * #volatile
160
+ * the base modification calls the last load placed, when its reads had any
161
+ */
162
+ loadedModifications: undefined as ModificationSummary | undefined,
163
+ }))
164
+ .views(self => ({
88
165
  /**
89
166
  * #getter
90
167
  */
@@ -108,16 +185,42 @@ export default function stateModelFactory() {
108
185
  get initSources() {
109
186
  return self.init ? initSources(getSession(self), self.init) : undefined
110
187
  },
188
+ /**
189
+ * #getter
190
+ * what a load is keyed on: two inits that say the same thing are one
191
+ */
192
+ get initKey() {
193
+ return self.init ? JSON.stringify(self.init) : undefined
194
+ },
195
+ /**
196
+ * #getter
197
+ * overrides base: a view with an `init` rebuilds what its snapshot drops
198
+ */
199
+ get hostRestoresData() {
200
+ return self.init !== undefined
201
+ },
111
202
  }))
112
203
  .views(self => ({
113
204
  /**
114
205
  * #method
206
+ * the reference base a visible column is read against: the reference
207
+ * row's residue there, or the next one where the reference has a gap
115
208
  */
116
209
  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 }
210
+ const { loadedRegion } = self
211
+ const index = self.seqPosIndex(REFERENCE)
212
+ const residue = index
213
+ ? lowerBound(index, self.visibleColToGlobalCol(col))
214
+ : undefined
215
+ return loadedRegion &&
216
+ index &&
217
+ residue !== undefined &&
218
+ residue < index.length
219
+ ? {
220
+ refName: loadedRegion.refName,
221
+ start: loadedRegion.start + residue,
222
+ end: loadedRegion.start + residue + 1,
223
+ }
121
224
  : undefined
122
225
  },
123
226
  }))
@@ -141,46 +244,149 @@ export default function stateModelFactory() {
141
244
  },
142
245
  }))
143
246
  .volatile(() => ({
144
- /** a load is in flight, or has failed and should not be retried */
145
- loading: false,
146
- loadFailed: false,
247
+ loadState: undefined as LoadState | undefined,
147
248
  }))
148
- .actions(self => ({
249
+ .views(self => ({
149
250
  /**
150
- * #action
251
+ * #getter
252
+ * the load state of the current `init`, not of one it replaced
151
253
  */
152
- setLoading(arg: boolean) {
153
- self.loading = arg
254
+ get currentLoad() {
255
+ const { loadState, initKey } = self
256
+ return loadState?.key === initKey ? loadState : undefined
154
257
  },
258
+ }))
259
+ .views(self => ({
260
+ /**
261
+ * #getter
262
+ * why `init` cannot be loaded yet, or undefined once it can
263
+ */
264
+ get initBlocker(): TViewState | undefined {
265
+ const { init, initRegion } = self
266
+ const session = getSession(self)
267
+ const assembly = init
268
+ ? session.assemblyManager.get(init.assembly)
269
+ : undefined
270
+ const missing = init ? missingTrackIds(session, init) : []
271
+ return !init
272
+ ? { kind: 'notice', message: 'This view names no region to load' }
273
+ : assembly?.error
274
+ ? { kind: 'error', error: assembly.error, retry: false }
275
+ : !initRegion
276
+ ? {
277
+ kind: 'loading',
278
+ message: `Waiting for assembly ${init.assembly}`,
279
+ }
280
+ : initRegion.error !== undefined
281
+ ? { kind: 'error', error: initRegion.error, retry: false }
282
+ : missing.length
283
+ ? {
284
+ kind: 'error',
285
+ error: `No track in this session has the id ${missing.join(', ')}`,
286
+ retry: false,
287
+ }
288
+ : undefined
289
+ },
290
+ }))
291
+ .views(self => ({
292
+ /**
293
+ * #getter
294
+ */
295
+ get viewState(): TViewState {
296
+ const { init, data, currentLoad, initBlocker } = self
297
+ const loadState: TViewState =
298
+ currentLoad?.status === 'failed'
299
+ ? { kind: 'error', error: currentLoad.error, retry: true }
300
+ : currentLoad?.status === 'loaded'
301
+ ? data.msa
302
+ ? { kind: 'alignment' }
303
+ : {
304
+ kind: 'notice',
305
+ message: 'The region has no reference sequence or reads',
306
+ }
307
+ : {
308
+ kind: 'loading',
309
+ message: 'Building alignment from track data',
310
+ }
311
+ return !init && data.msa
312
+ ? { kind: 'alignment' }
313
+ : (initBlocker ?? loadState)
314
+ },
315
+ }))
316
+ .actions(self => ({
155
317
  /**
156
318
  * #action
157
319
  */
158
- setLoadFailed(arg: boolean) {
159
- self.loadFailed = arg
320
+ setLoadState(arg?: LoadState) {
321
+ self.loadState = arg
160
322
  },
161
323
  /**
162
324
  * #action
163
325
  */
164
326
  setInit(arg?: TviewInit) {
165
327
  self.init = arg
166
- self.loadFailed = false
167
328
  },
168
329
  /**
169
330
  * #action
331
+ * forget a failed load, so the view tries its `init` again
170
332
  */
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)
333
+ retryLoad() {
334
+ self.loadState = undefined
335
+ },
336
+ /**
337
+ * #action
338
+ * draws a load's base modification calls as annotations, which are
339
+ * volatile: as many as the cap allows would bloat the session snapshot
340
+ */
341
+ setModifications(modifications?: TviewModifications) {
342
+ if (modifications || self.loadedModifications) {
343
+ self.setAnnotations(
344
+ modifications ? modificationAnnotations(modifications) : [],
345
+ )
346
+ }
347
+ if (modifications && self.marks === undefined) {
348
+ self.setMarks([MODIFICATION_MARK])
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 numbered in the genome's coordinates. The brackets are
362
+ * transient, since every load rebuilds them, and a session saved by 3.0.0
363
+ * carries its own copies of them, which are dropped here
364
+ */
365
+ setAlignment(result: TviewPlanResult) {
366
+ const { region, highlights } = result
367
+ const bySubject = result.hasSubject
368
+ const rebuilt = new Set(highlights.map(highlightKey))
369
+ self.loadedRegion = region
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.setRowCoordinates([
377
+ { row: REFERENCE, start: region.start + 1, name: region.refName },
378
+ ])
379
+ self.applyHighlight(LOAD_BRACKETS, highlights)
380
+ if (self.highlights.some(h => rebuilt.has(highlightKey(h)))) {
381
+ self.setHighlights(
382
+ self.highlights.filter(h => !rebuilt.has(highlightKey(h))),
383
+ )
384
+ }
385
+ if (self.relativeTo === undefined) {
386
+ self.drawRelativeTo(REFERENCE)
387
+ }
388
+ if (self.colWidth === MAX_COL_WIDTH) {
389
+ self.setColWidth(fittedColWidth(result.columnCount))
184
390
  }
185
391
  },
186
392
  /**
@@ -204,57 +410,95 @@ export default function stateModelFactory() {
204
410
  }
205
411
  },
206
412
  }))
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
- )
413
+ .actions(self => {
414
+ let controller: AbortController | undefined
415
+ return {
416
+ /**
417
+ * #action
418
+ * builds the alignment `init` describes, from whatever `init`
419
+ * describes it over. The one way an alignment ever gets here. A result
420
+ * for an `init` that is no longer current, or for a view that has been
421
+ * closed, is dropped.
422
+ */
423
+ async load(key: string, region: FetchRegion, sources: TviewSource[]) {
424
+ controller?.abort()
425
+ const own = new AbortController()
426
+ controller = own
427
+ const current = () =>
428
+ controller === own && isAlive(self) && self.initKey === key
429
+ self.setLoadState({ key, status: 'loading' })
430
+ try {
431
+ const result = await fetchTviewPlan({
432
+ session: getSession(self),
433
+ sources,
434
+ region,
435
+ signal: own.signal,
436
+ })
437
+ if (current()) {
438
+ if (result.tooLarge) {
439
+ self.setLoadState({
440
+ key,
441
+ status: 'failed',
442
+ error: `The alignment is ${result.cellCount.toLocaleString('en-US')} cells, above the ${MAX_CELLS.toLocaleString('en-US')} limit`,
443
+ })
444
+ } else {
445
+ self.setAlignment(result)
446
+ self.setLoadState({ key, status: 'loaded' })
447
+ }
448
+ }
449
+ } catch (error) {
450
+ if (current()) {
451
+ console.error(error)
452
+ self.setLoadState({ key, status: 'failed', error })
453
+ }
224
454
  }
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
- }))
455
+ },
456
+ beforeDestroy() {
457
+ controller?.abort()
458
+ },
459
+ }
460
+ })
237
461
  .actions(self => ({
238
462
  afterCreate() {
463
+ self.setImportForm(false)
464
+ addDisposer(
465
+ self,
466
+ autorun(() => {
467
+ const { initKey, initRegion, initSources, loadState } = self
468
+ if (
469
+ initKey &&
470
+ loadState?.key !== initKey &&
471
+ initRegion?.region &&
472
+ initSources
473
+ ) {
474
+ void self.load(initKey, initRegion.region, initSources)
475
+ }
476
+ }),
477
+ )
239
478
  addDisposer(
240
479
  self,
241
480
  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
481
+ const { loadedRegion, connectedView } = self
482
+ const hover = hoverPosition(getSession(self).hovered)
483
+ const inConnectedView =
484
+ !!connectedView &&
485
+ (hover?.viewId === undefined || hover.viewId === connectedView.id)
486
+ const residue =
487
+ inConnectedView &&
488
+ loadedRegion &&
489
+ hover?.refName === loadedRegion.refName
490
+ ? hover.coord - loadedRegion.start
491
+ : 0
249
492
  if (
250
- !data.msa &&
251
- initRegion &&
252
- initSources &&
253
- !loading &&
254
- !loadFailed
493
+ loadedRegion &&
494
+ residue >= 1 &&
495
+ residue <= loadedRegion.end - loadedRegion.start
255
496
  ) {
256
- // load handles its own failures, so it never rejects
257
- void self.load(initRegion, initSources)
497
+ self.applyHighlight(GENOME_HOVER, [
498
+ { row: REFERENCE, start: residue, end: residue },
499
+ ])
500
+ } else {
501
+ self.clearHighlight(GENOME_HOVER)
258
502
  }
259
503
  }),
260
504
  )
@@ -284,8 +528,13 @@ export default function stateModelFactory() {
284
528
  * overrides base
285
529
  */
286
530
  menuItems(): MenuItem[] {
531
+ const { loadedModifications } = self
287
532
  return [
288
533
  ...superMenuItems(),
534
+ ...tviewInitMenuItems(self),
535
+ ...(loadedModifications
536
+ ? [modificationMenu(loadedModifications, self)]
537
+ : []),
289
538
  {
290
539
  label: 'Zoom to base level on click?',
291
540
  type: 'checkbox',