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,5 +1,9 @@
1
1
  import { alignInsertionSite } from './align'
2
- import { buildArrayBlocks, reanchorInsertions } from './alleles'
2
+ import {
3
+ buildArrayBlocks,
4
+ normalizeInsertions,
5
+ reanchorInsertions,
6
+ } from './alleles'
3
7
  import {
4
8
  ABSENT,
5
9
  SPANNED_GAP,
@@ -9,10 +13,10 @@ import {
9
13
  parseRead,
10
14
  } from './readLayout'
11
15
  import { findReferenceArrays, mergeArrays } from './repeats'
12
- import { newickSafe } from './sampleTree'
13
16
 
14
17
  import type { ArrayBlock } from './alleles'
15
18
  import type { AlignmentFeature, ReadLayout } from './readLayout'
19
+ import type { Highlight, RowDataInput } from 'react-msaview'
16
20
 
17
21
  export type { AlignmentFeature, ReadLayout } from './readLayout'
18
22
  export { clipInsertions, parseCigar, parseRead } from './readLayout'
@@ -85,10 +89,16 @@ export interface ColumnLayout {
85
89
  */
86
90
  offsets: number[]
87
91
  totalColumns: number
88
- }
89
-
90
- function arrayStartIndex(arrays: ArrayBlock[]) {
91
- return new Map(arrays.map(a => [a.start, a]))
92
+ /** the array beginning at a position, for the positions one begins at */
93
+ arrayByStart: Map<number, ArrayBlock>
94
+ /**
95
+ * Ascending, the positions a row cannot be laid out in bulk over: an array
96
+ * block starts here, or some read inserts here. Between two of them every
97
+ * position is one reference column, so that stretch of a row is a slice of
98
+ * the read's own bases rather than a walk over them — which is what keeps
99
+ * rendering proportional to the indels in a region rather than to its width.
100
+ */
101
+ breaks: number[]
92
102
  }
93
103
 
94
104
  /**
@@ -103,15 +113,17 @@ export function buildColumnLayout(
103
113
  insWidths: Map<number, number>,
104
114
  arrays: ArrayBlock[] = [],
105
115
  ): ColumnLayout {
106
- const byStartPos = arrayStartIndex(arrays)
116
+ const arrayByStart = new Map(arrays.map(a => [a.start, a]))
107
117
  const offsets: number[] = []
118
+ const breaks: number[] = []
108
119
  let col = 0
109
120
  let pos = start
110
121
  while (pos < end) {
111
- const array = byStartPos.get(pos)
122
+ const array = arrayByStart.get(pos)
112
123
  if (array) {
113
124
  const stop = Math.min(array.end, end)
114
125
  offsets.push(col)
126
+ breaks.push(pos)
115
127
  col += array.width
116
128
  for (let p = pos + 1; p < stop; p++) {
117
129
  offsets.push(col)
@@ -119,12 +131,40 @@ export function buildColumnLayout(
119
131
  pos = stop
120
132
  } else {
121
133
  offsets.push(col)
122
- col += (insWidths.get(pos) ?? 0) + 1
134
+ const width = insWidths.get(pos) ?? 0
135
+ if (width) {
136
+ breaks.push(pos)
137
+ }
138
+ col += width + 1
123
139
  pos++
124
140
  }
125
141
  }
126
142
  offsets.push(col)
127
- return { start, end, insWidths, arrays, offsets, totalColumns: col }
143
+ return {
144
+ start,
145
+ end,
146
+ insWidths,
147
+ arrays,
148
+ offsets,
149
+ totalColumns: col,
150
+ arrayByStart,
151
+ breaks,
152
+ }
153
+ }
154
+
155
+ /** index of the first break at or after `pos` */
156
+ function breakIndex(breaks: number[], pos: number) {
157
+ let lo = 0
158
+ let hi = breaks.length
159
+ while (lo < hi) {
160
+ const mid = (lo + hi) >> 1
161
+ if (breaks[mid]! < pos) {
162
+ lo = mid + 1
163
+ } else {
164
+ hi = mid
165
+ }
166
+ }
167
+ return lo
128
168
  }
129
169
 
130
170
  /** the array covering `pos`, when `pos` is strictly inside one */
@@ -133,123 +173,103 @@ function arrayContaining(arrays: ArrayBlock[], pos: number) {
133
173
  }
134
174
 
135
175
  /**
136
- * Lays a single row out across the whole region. Rows are typically a tiny
137
- * fraction of the region, so the columns on either side are filled in one shot
138
- * from the offsets rather than walked position by position — which works over
139
- * array blocks too, since the offsets already carry their width.
176
+ * The reference positions a row is walked over, from inclusive to exclusive. A
177
+ * trailing insertion is keyed one past the last position the read spans, so
178
+ * that slot is in it.
140
179
  */
141
- export function renderRow(read: ReadLayout, layout: ColumnLayout) {
142
- const { start, end, insWidths, arrays, offsets, totalColumns } = layout
143
- const byStartPos = arrayStartIndex(arrays)
144
- // a trailing insertion is keyed one past the last position the read spans
145
- let from = Math.max(start, read.start)
146
- let to = Math.min(end, read.start + read.refChars.length + 1)
180
+ export function rowSpan(read: ReadLayout, layout: ColumnLayout) {
181
+ const { start, end, arrays } = layout
182
+ const lastPos = read.start + read.refChars.length
183
+ const from = Math.max(start, read.start)
184
+ const to = Math.min(end, lastPos + 1)
147
185
  // a row that begins or ends inside an array does not span it, so it gets no
148
186
  // block; snapping past the block keeps the walk from emitting part of one
149
187
  const startsInside = arrayContaining(arrays, from)
150
- if (startsInside) {
151
- from = Math.min(end, startsInside.end)
152
- }
153
188
  const endsInside = arrayContaining(arrays, to)
154
- if (endsInside) {
155
- to = endsInside.start
189
+ return {
190
+ from: startsInside ? Math.min(end, startsInside.end) : from,
191
+ to: endsInside ? endsInside.start : to,
156
192
  }
193
+ }
194
+
195
+ /**
196
+ * Lays a single row out across the whole region. Rows are typically a tiny
197
+ * fraction of the region, so the columns on either side are filled in one shot
198
+ * from the offsets rather than walked position by position — which works over
199
+ * array blocks too, since the offsets already carry their width. Inside its own
200
+ * span a row is walked only at the layout's breaks, and copied straight out of
201
+ * `refChars` between them.
202
+ */
203
+ export function renderRow(read: ReadLayout, layout: ColumnLayout) {
204
+ const { start, insWidths, offsets, totalColumns } = layout
205
+ const { arrayByStart, breaks } = layout
206
+ const lastPos = read.start + read.refChars.length
207
+ const { from, to } = rowSpan(read, layout)
157
208
  if (to <= from) {
158
209
  return ABSENT.repeat(totalColumns)
159
210
  }
160
211
 
161
212
  let ret = ABSENT.repeat(offsets[from - start]!)
162
213
  let pos = from
214
+ let next = breakIndex(breaks, from)
163
215
  while (pos < to) {
164
- const array = byStartPos.get(pos)
216
+ while (next < breaks.length && breaks[next]! < pos) {
217
+ next++
218
+ }
219
+ const stop = Math.min(next < breaks.length ? breaks[next]! : to, to)
220
+ if (stop > pos) {
221
+ // the walk never leaves the read's own bases except at its very last
222
+ // position, which is the slot a trailing insertion is keyed in
223
+ const cut = Math.min(stop, lastPos)
224
+ ret += read.refChars.slice(pos - read.start, cut - read.start)
225
+ ret += ABSENT.repeat(stop - cut)
226
+ pos = stop
227
+ continue
228
+ }
229
+ const array = arrayByStart.get(pos)
165
230
  if (array) {
166
- const row = array.rowByName.get(read.name)
167
- ret += row ?? SPANNED_GAP.repeat(array.width)
231
+ // a row with no allele here did not reach both edges of the array, or
232
+ // skipped it through an N — either way it is not here, and a block of
233
+ // deletion gaps would read as an allele it does not have
234
+ ret += array.rowByName.get(read.name) ?? ABSENT.repeat(array.width)
168
235
  pos = Math.min(array.end, to)
169
236
  continue
170
237
  }
171
- const idx = pos - read.start
172
- const spanned = idx >= 0 && idx < read.refChars.length
173
- const refChar = spanned ? read.refChars[idx]! : ABSENT
174
- const width = insWidths.get(pos) ?? 0
175
- if (width) {
176
- // a read that is not really here pads the insertion the same way it
177
- // pads the reference column, so an intron reads as one absence
178
- const pad = refChar === ABSENT ? ABSENT : SPANNED_GAP
179
- const ins = read.insertions.get(pos) ?? ''
180
- ret += ins + pad.repeat(width - ins.length)
181
- }
182
- ret += refChar
238
+ const refChar = pos < lastPos ? read.refChars[pos - read.start]! : ABSENT
239
+ const ins = read.insertions.get(pos) ?? ''
240
+ // insertion columns sit between pos - 1 and pos, so a read that is absent
241
+ // on either side, an intron or its own first base, does not span them
242
+ const spans =
243
+ ins !== '' ||
244
+ (refChar !== ABSENT &&
245
+ pos > read.start &&
246
+ read.refChars[pos - read.start - 1] !== ABSENT)
247
+ const pad = spans ? SPANNED_GAP : ABSENT
248
+ ret += ins + pad.repeat(insWidths.get(pos)! - ins.length) + refChar
183
249
  pos++
184
250
  }
185
251
  return ret + ABSENT.repeat(totalColumns - offsets[to - start]!)
186
252
  }
187
253
 
188
- // A defline is truncated at its first space (msa-parsers' FastaMSA), so the
189
- // count is joined on. `labelsAlignRight` puts it flush against the sequence.
190
- const COPY_COUNT_SEPARATOR = '|'
191
-
192
- /**
193
- * What a row is called on screen. The one place `label` falls back to `name`,
194
- * so the defline and the tree leaf cannot disagree about it — react-msaview
195
- * joins them by string, and a row the tree names differently renders blank.
196
- */
197
- export function rowLabel(read: ReadLayout) {
198
- return read.label ?? read.name
199
- }
200
-
201
254
  /**
202
- * Rows ordered longest-allele-first and labelled with their copy count.
203
- *
204
- * Copy number is the measurement at a tandem array, and reading it off where a
205
- * row runs out is the thing the unit-per-block layout exists to avoid, so it is
206
- * stated in the label. Ordering by it puts the alleles in a ladder, which is
207
- * what makes one row's array comparable to the next at a glance. Rows that do
208
- * not span the array keep genomic order, below the ones that do; the reference
209
- * stays on top whatever its allele, since it is what the columns are named
210
- * after rather than one of the observations.
255
+ * The reference row's name, and the sample its row table entry gives it. Not
256
+ * the sequence's own name: react-msaview prints a row's residues as
257
+ * `<row>:<index>`, and `chr4:25` would read as a genome coordinate when it is an
258
+ * index into the window.
211
259
  */
212
- export function labelAndOrderByCopies(
213
- reads: ReadLayout[],
214
- array: ArrayBlock,
215
- referenceName?: string,
216
- ) {
217
- const rank = (read: ReadLayout) => (read.name === referenceName ? 1 : 0)
218
- return reads
219
- .map(read => ({ read, copies: array.copiesByName.get(read.name) }))
220
- .sort((a, b) => {
221
- const byRef = rank(b.read) - rank(a.read)
222
- if (byRef) {
223
- return byRef
224
- }
225
- if (a.copies === undefined || b.copies === undefined) {
226
- return a.copies === b.copies
227
- ? byStart(a.read, b.read)
228
- : a.copies === undefined
229
- ? 1
230
- : -1
231
- }
232
- return b.copies - a.copies || byStart(a.read, b.read)
233
- })
234
- .map(({ read, copies }) =>
235
- copies === undefined
236
- ? read
237
- : { ...read, label: `${read.name}${COPY_COUNT_SEPARATOR}n=${copies}` },
238
- )
239
- }
260
+ export const REFERENCE = 'reference'
240
261
 
241
262
  export interface TviewPlan {
242
263
  reads: ReadLayout[]
264
+ /** the row each feature became, parallel to the features planned */
265
+ featureNames: string[]
243
266
  layout: ColumnLayout
244
- insertionWidths: [number, number][]
245
- /** [start, end, width] per array block, to rebuild the column mapping */
246
- arraySpans: [number, number, number][]
247
267
  region: { refName: string; start: number; end: number }
248
268
  /** rows x columns, i.e. how big the alignment renderTviewMsa builds will be */
249
269
  cellCount: number
250
270
  /** tandem arrays the reference carries here, left to right */
251
271
  arrays: ArrayBlock[]
252
- /** the array the rows are ordered and labelled by, when there is one */
272
+ /** the array the rows are ordered by, when there is one */
253
273
  subject?: ArrayBlock
254
274
  /** name of the reference row, when the reference sequence was available */
255
275
  referenceName?: string
@@ -287,7 +307,7 @@ export function planTviewMsa({
287
307
  sequence,
288
308
  sampleOf,
289
309
  }: PlanArgs): TviewPlan {
290
- const names = getReadNames(features, sampleOf)
310
+ const names = getReadNames(features, [REFERENCE])
291
311
  const parsed = features
292
312
  .map((f, i) => parseRead(f, names[i]!, sampleOf?.(i)))
293
313
  .sort(byStart)
@@ -295,10 +315,11 @@ export function planTviewMsa({
295
315
  // The reference is laid out as a row like any other, which is what lets it be
296
316
  // one of the alleles an array block squares up rather than a thing the block
297
317
  // has to be compared against afterwards.
298
- const referenceName = sequence ? newickSafe(refName) : undefined
318
+ const referenceName = sequence ? REFERENCE : undefined
299
319
  const referenceRead: ReadLayout | undefined = sequence
300
320
  ? {
301
- name: referenceName!,
321
+ name: REFERENCE,
322
+ sample: REFERENCE,
302
323
  start,
303
324
  refChars: sequence.toUpperCase(),
304
325
  insertions: new Map(),
@@ -319,7 +340,11 @@ export function planTviewMsa({
319
340
  const intervals = sequence
320
341
  ? mergeArrays(findReferenceArrays(sequence, start))
321
342
  : []
322
- const anchored = reanchorInsertions(clipped, intervals)
343
+ const anchored = normalizeInsertions(
344
+ reanchorInsertions(clipped, intervals),
345
+ intervals,
346
+ start,
347
+ )
323
348
  const arrays = buildArrayBlocks(anchored, intervals)
324
349
  const ownedByArray = new Set<number>()
325
350
  for (const array of arrays) {
@@ -338,39 +363,130 @@ export function planTviewMsa({
338
363
  best && alleleSpread(best) >= alleleSpread(array) ? best : array,
339
364
  undefined,
340
365
  )
341
- const reads = subject
342
- ? labelAndOrderByCopies(aligned, subject, referenceName)
343
- : aligned
344
-
345
- const insWidths = maxInsertionWidths(reads, ownedByArray)
366
+ const insWidths = maxInsertionWidths(aligned, ownedByArray)
346
367
  const layout = buildColumnLayout(start, end, insWidths, arrays)
347
368
 
348
369
  return {
349
- reads,
370
+ reads: aligned,
371
+ featureNames: names,
350
372
  layout,
351
- insertionWidths: [...insWidths.entries()].sort((a, b) => a[0] - b[0]),
352
- arraySpans: arrays.map(a => [a.start, a.end, a.width]),
353
373
  region: { refName, start, end },
354
- cellCount: reads.length * layout.totalColumns,
374
+ cellCount: aligned.length * layout.totalColumns,
355
375
  arrays,
356
376
  subject,
357
377
  referenceName,
358
378
  samples: [
359
- ...new Set(parsed.map(r => r.sample).filter(s => s !== undefined)),
379
+ ...new Set(
380
+ features.map((_, i) => sampleOf?.(i)).filter(s => s !== undefined),
381
+ ),
360
382
  ],
361
383
  }
362
384
  }
363
385
 
364
- export function renderTviewMsa({ reads, layout }: TviewPlan) {
365
- return reads.map(r => `>${rowLabel(r)}\n${renderRow(r, layout)}\n`).join('')
386
+ export function renderRows({ reads, layout }: TviewPlan) {
387
+ return reads.map(r => renderRow(r, layout))
388
+ }
389
+
390
+ export function toFasta({ reads }: TviewPlan, rows: string[]) {
391
+ return reads.map((r, i) => `>${r.name}\n${rows[i]}\n`).join('')
392
+ }
393
+
394
+ export function renderTviewMsa(plan: TviewPlan) {
395
+ return toFasta(plan, renderRows(plan))
396
+ }
397
+
398
+ /**
399
+ * What each row is, keyed by its name: the sample it came from and, where there
400
+ * is a subject array, the copies it carries there and their length in bp.
401
+ */
402
+ export function planRowData({ reads, subject }: TviewPlan): RowDataInput {
403
+ return Object.fromEntries(
404
+ reads.map(({ name, sample }) => {
405
+ const copies = subject?.copiesByName.get(name)
406
+ const alleleBp = subject?.lengthByName.get(name)
407
+ return [
408
+ name,
409
+ {
410
+ ...(sample === undefined ? {} : { sample }),
411
+ ...(copies === undefined ? {} : { copies }),
412
+ ...(alleleBp === undefined ? {} : { alleleBp }),
413
+ },
414
+ ]
415
+ }),
416
+ )
417
+ }
418
+
419
+ /** the value most of `values` share, the larger of two equally common */
420
+ function commonest(values: number[]) {
421
+ const counts = new Map<number, number>()
422
+ for (const value of values) {
423
+ counts.set(value, (counts.get(value) ?? 0) + 1)
424
+ }
425
+ return [...counts].sort((a, b) => b[1] - a[1] || b[0] - a[0])[0]![0]
426
+ }
427
+
428
+ // A bracket is kept in the session and drawn in one strip, and a noisy window
429
+ // has an insertion site every few bases, so only the sites carrying the most
430
+ // inserted sequence are marked.
431
+ const MAX_INSERTION_BRACKETS = 100
432
+
433
+ /**
434
+ * A bracket over the runs of columns that are not one reference base: the
435
+ * insertion sites, and each array block. Columns are the file's, 1-based.
436
+ */
437
+ export function planHighlights({
438
+ reads,
439
+ layout,
440
+ region,
441
+ referenceName,
442
+ }: TviewPlan): Highlight[] {
443
+ const { start, offsets, insWidths, arrays } = layout
444
+ const n = (x: number) => x.toLocaleString('en-US')
445
+ const column = (pos: number) => offsets[pos - start]! + 1
446
+ const lengthsAt = new Map<number, number[]>()
447
+ for (const read of reads) {
448
+ for (const [pos, ins] of read.insertions) {
449
+ const lengths = lengthsAt.get(pos) ?? []
450
+ lengths.push(ins.replaceAll(SPANNED_GAP, '').length)
451
+ lengthsAt.set(pos, lengths)
452
+ }
453
+ }
454
+ const insertedBases = (pos: number) =>
455
+ (lengthsAt.get(pos) ?? []).reduce((a, b) => a + b, 0)
456
+ const insertions = [...insWidths]
457
+ .filter(([, width]) => width > 0)
458
+ .sort((a, b) => insertedBases(b[0]) - insertedBases(a[0]) || a[0] - b[0])
459
+ .slice(0, MAX_INSERTION_BRACKETS)
460
+ .map(([pos, width]) => ({
461
+ start: column(pos),
462
+ end: column(pos) + width - 1,
463
+ label: `+${commonest(lengthsAt.get(pos) ?? [width])}bp after ${region.refName}:${n(pos)}`,
464
+ }))
465
+ const blocks = arrays.map(array => {
466
+ const copies = referenceName
467
+ ? array.copiesByName.get(referenceName)
468
+ : undefined
469
+ return {
470
+ start: column(array.start),
471
+ end: column(array.start) + array.width - 1,
472
+ label:
473
+ copies === undefined
474
+ ? `(${array.unit})n`
475
+ : `(${array.unit})x${copies} in the reference`,
476
+ }
477
+ })
478
+ return [...insertions, ...blocks]
479
+ .sort((a, b) => a.start - b.start)
480
+ .map((h): Highlight => ({ ...h, mark: 'bracket' }))
366
481
  }
367
482
 
368
483
  export function buildTviewMsa(args: PlanArgs) {
369
484
  const plan = planTviewMsa(args)
370
485
  return {
371
486
  msa: renderTviewMsa(plan),
372
- insertionWidths: plan.insertionWidths,
373
- arraySpans: plan.arraySpans,
487
+ rowData: planRowData(plan),
488
+ highlights: planHighlights(plan),
489
+ insertionWidths: [...plan.layout.insWidths].sort((a, b) => a[0] - b[0]),
374
490
  region: plan.region,
375
491
  arrays: plan.arrays,
376
492
  subject: plan.subject,
@@ -1,20 +1,49 @@
1
1
  import React from 'react'
2
2
 
3
+ import { ErrorMessage, LoadingEllipses } from '@jbrowse/core/ui'
4
+ import { Button, Typography } from '@mui/material'
3
5
  import { observer } from 'mobx-react'
4
6
  import { MSAView } from 'react-msaview'
5
7
 
6
8
  import type { JBrowsePluginTViewModel } from '../model'
7
9
  import type { MsaViewModel } from 'react-msaview'
8
10
 
11
+ const padded = { padding: 20 }
12
+
9
13
  const TViewPanel = observer(function TViewPanel2({
10
14
  model,
11
15
  }: {
12
16
  model: JBrowsePluginTViewModel
13
17
  }) {
14
- // react-msaview types its model prop with type: 'MsaView' baked in, but a
15
- // JBrowse view model's `type` must equal its registered view type name. That
16
- // literal is the only difference, and the MSA renderer never reads it.
17
- return <MSAView model={model as unknown as MsaViewModel} />
18
+ const state = model.viewState
19
+ return state.kind === 'alignment' ? (
20
+ // react-msaview types its model prop with type: 'MsaView' baked in, but a
21
+ // JBrowse view model's `type` must equal its registered view type name.
22
+ // That literal is the only difference, and the MSA renderer never reads it.
23
+ <MSAView model={model as unknown as MsaViewModel} />
24
+ ) : (
25
+ <div style={padded}>
26
+ {state.kind === 'loading' ? (
27
+ <LoadingEllipses variant="h6" message={state.message} />
28
+ ) : state.kind === 'notice' ? (
29
+ <Typography>{state.message}</Typography>
30
+ ) : (
31
+ <>
32
+ <ErrorMessage error={state.error} />
33
+ {state.retry ? (
34
+ <Button
35
+ variant="contained"
36
+ onClick={() => {
37
+ model.retryLoad()
38
+ }}
39
+ >
40
+ Retry
41
+ </Button>
42
+ ) : null}
43
+ </>
44
+ )}
45
+ </div>
46
+ )
18
47
  })
19
48
 
20
49
  export default TViewPanel
@@ -0,0 +1,41 @@
1
+ export interface HoverPosition {
2
+ /** 1-based */
3
+ coord: number
4
+ refName: string
5
+ /** the genome view the cursor is in, where the host says */
6
+ viewId?: string
7
+ }
8
+
9
+ /** the base a genome view's cursor is over, from `session.hovered` */
10
+ export function hoverPosition(hovered: unknown): HoverPosition | undefined {
11
+ const hover = !!hovered && typeof hovered === 'object' ? hovered : undefined
12
+ const pos =
13
+ hover && 'hoverPosition' in hover ? hover.hoverPosition : undefined
14
+ const viewId =
15
+ hover && 'viewId' in hover && typeof hover.viewId === 'string'
16
+ ? hover.viewId
17
+ : undefined
18
+ return !!pos &&
19
+ typeof pos === 'object' &&
20
+ 'coord' in pos &&
21
+ typeof pos.coord === 'number' &&
22
+ 'refName' in pos &&
23
+ typeof pos.refName === 'string'
24
+ ? { coord: pos.coord, refName: pos.refName, viewId }
25
+ : undefined
26
+ }
27
+
28
+ /** how many entries of an ascending index are below `value` */
29
+ export function lowerBound(index: ArrayLike<number>, value: number) {
30
+ let lo = 0
31
+ let hi = index.length
32
+ while (lo < hi) {
33
+ const mid = (lo + hi) >>> 1
34
+ if (index[mid]! < value) {
35
+ lo = mid + 1
36
+ } else {
37
+ hi = mid
38
+ }
39
+ }
40
+ return lo
41
+ }