jbrowse-plugin-tview 2.2.0 → 2.2.3

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.
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.2.0";
1
+ export declare const version = "2.2.3";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.2.0';
1
+ export const version = '2.2.3';
2
2
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.2.0",
2
+ "version": "2.2.3",
3
3
  "license": "MIT",
4
4
  "name": "jbrowse-plugin-tview",
5
5
  "repository": {
@@ -23,6 +23,7 @@
23
23
  "format": "pnpm prettier --write .",
24
24
  "prebuild": "pnpm clean",
25
25
  "build": "tsc && NODE_ENV=production node esbuild.mjs && cp distconfig.json dist/config.json",
26
+ "typecheck": "tsc --noEmit",
26
27
  "lint": "eslint --report-unused-disable-directives --max-warnings 0",
27
28
  "test": "vitest run",
28
29
  "test:watch": "vitest",
@@ -36,48 +37,51 @@
36
37
  "postversion": "git push --follow-tags",
37
38
  "figures:repeats": "node scripts/repeat-figures.mjs",
38
39
  "prefigures:repeats": "pnpm pretest:e2e",
39
- "qc:repeats": "tsx test/liveRepeats.qc.ts",
40
40
  "report:repeats": "tsx scripts/repeat-report.mjs",
41
- "readme:repeats": "tsx scripts/repeat-readme.mjs"
41
+ "readme:repeats": "tsx scripts/repeat-readme.mjs",
42
+ "demos": "node scripts/demo-links.mjs",
43
+ "demos:upload": "node scripts/demo-upload.mjs",
44
+ "predemos:upload": "pnpm build && pnpm demos"
42
45
  },
43
46
  "jbrowse-plugin": {
44
47
  "name": "TView"
45
48
  },
46
49
  "dependencies": {
47
50
  "@emotion/styled": "^11.14.1",
48
- "swr": "^2.4.2"
51
+ "swr": "^2.5.1"
49
52
  },
50
53
  "devDependencies": {
51
54
  "@emotion/react": "^11.14.0",
52
55
  "@eslint/js": "^10.0.1",
53
56
  "@fal-works/esbuild-plugin-global-externals": "^2.1.2",
54
- "@gmod/bam": "^7.3.4",
57
+ "@gmod/bam": "^8.11.1",
58
+ "@gmod/indexedfasta": "^5.0.11",
55
59
  "@jbrowse/core": "^4.3.0",
56
- "@jbrowse/mobx-state-tree": "^5.13.0",
60
+ "@jbrowse/mobx-state-tree": "^6.1.0",
57
61
  "@jbrowse/plugin-linear-genome-view": "^4.3.0",
58
- "@mui/icons-material": "^9.2.0",
59
- "@mui/material": "^9.2.0",
60
- "@mui/system": "^9.2.0",
61
- "@types/node": "^26.1.1",
62
- "@types/react": "^19.2.17",
63
- "esbuild": "^0.28.1",
64
- "eslint": "^10.7.0",
62
+ "@mui/icons-material": "^9.3.1",
63
+ "@mui/material": "^9.3.1",
64
+ "@mui/system": "^9.3.0",
65
+ "@types/node": "^26.3.0",
66
+ "@types/react": "^19.2.18",
67
+ "esbuild": "^0.28.2",
68
+ "eslint": "^10.9.1",
65
69
  "eslint-plugin-import-x": "^4.17.1",
66
70
  "eslint-plugin-react": "^7.37.5",
67
71
  "eslint-plugin-react-hooks": "^7.1.1",
68
- "mobx": "^6.16.1",
69
- "mobx-react": "^9.2.2",
72
+ "mobx": "^7.0.0",
73
+ "mobx-react": "^10.0.0",
70
74
  "prettier": "^3.9.6",
71
75
  "pretty-bytes": "^7.1.1",
72
- "puppeteer": "^25.3.0",
76
+ "puppeteer": "^25.9.0",
73
77
  "react": "^19.2.8",
74
78
  "react-dom": "^19.2.8",
75
- "react-msaview": "^5.7.1",
79
+ "react-msaview": "^6.4.2",
76
80
  "rimraf": "^6.1.3",
77
81
  "tss-react": "^4.9.21",
78
- "tsx": "^4.20.6",
82
+ "tsx": "^4.23.12",
79
83
  "typescript": "^6.0.2",
80
- "typescript-eslint": "^8.65.0",
81
- "vitest": "^4.1.10"
84
+ "typescript-eslint": "^8.68.0",
85
+ "vitest": "^4.1.11"
82
86
  }
83
87
  }
@@ -301,26 +301,34 @@ function splitIntoUnits(seq: string, consensus: string) {
301
301
  return units
302
302
  }
303
303
 
304
- // The tiled alignment below is O(len * len), so it scores a prefix rather than
305
- // the whole insert. A sequence that is copies of the unit for its first 300
306
- // bases is copies of the unit.
304
+ // The tiled alignment below is O(len^2), so it scores windows of the insert
305
+ // rather than all of it.
307
306
  const IDENTITY_PROBE_LEN = 300
308
307
 
309
308
  /**
310
- * How much of `seq` is explained by `unit` repeated end to end: the fraction of
311
- * `seq`'s bases that align to a matching base of the tiling.
309
+ * Head, middle and tail of `seq`, or the whole of it when it is short enough to
310
+ * score outright.
312
311
  *
313
- * Free at both ends of the tiling, since an inserted run of copies can start
314
- * and stop mid-copy, and global on `seq`, since the question is about all of it.
315
- * That is what separates "more copies of this array" from "something else that
316
- * landed next to it" without asking the sequence to be in phase with the unit:
317
- * measured against the trio, the repeat alleles the aligner anchored just
318
- * outside an array score 0.94-1.00 and unrelated sequence of the same length
319
- * scores 0.67 or below.
312
+ * Scoring only the head is how a prefix probe lies: `unit x 12` followed by a
313
+ * kilobase of something else scores 1.00 on its first 300 bases and is not the
314
+ * array's. Three windows cost the same and cannot be fooled that way, because
315
+ * the sequence has to still be the unit where it ends as well as where it
316
+ * starts.
320
317
  */
321
- export function unitIdentity(seq: string, unit: string) {
322
- const probe =
323
- seq.length > IDENTITY_PROBE_LEN ? seq.slice(0, IDENTITY_PROBE_LEN) : seq
318
+ function probeWindows(seq: string) {
319
+ if (seq.length <= IDENTITY_PROBE_LEN) {
320
+ return [seq]
321
+ }
322
+ const middle = Math.floor((seq.length - IDENTITY_PROBE_LEN) / 2)
323
+ return [
324
+ seq.slice(0, IDENTITY_PROBE_LEN),
325
+ seq.slice(middle, middle + IDENTITY_PROBE_LEN),
326
+ seq.slice(-IDENTITY_PROBE_LEN),
327
+ ]
328
+ }
329
+
330
+ /** the fraction of `probe`'s bases that match `unit` tiled end to end */
331
+ function tiledIdentity(probe: string, unit: string) {
324
332
  const n = probe.length
325
333
  if (!n || !unit.length) {
326
334
  return 0
@@ -364,6 +372,27 @@ export function unitIdentity(seq: string, unit: string) {
364
372
  return bestMatches / n
365
373
  }
366
374
 
375
+ /**
376
+ * How much of `seq` is explained by `unit` repeated end to end, scored at three
377
+ * places along it and reported as the worst of them.
378
+ *
379
+ * Each window is aligned free at both ends of the tiling, since an inserted run
380
+ * of copies can start and stop mid-copy, and global on the window, since the
381
+ * question is about all of it. That is what separates "more copies of this
382
+ * array" from "something else that landed next to it" without asking the
383
+ * sequence to be in phase with the unit — the ABCA7 read that carries 1207bp of
384
+ * VNTR anchored outside the array begins six bases into a copy, and the FMR1
385
+ * one begins on the wrong base of the unit and carries the locus's own AGG
386
+ * interruptions.
387
+ *
388
+ * Measured against the trio: the repeat alleles the aligner anchored just
389
+ * outside an array score 0.9 and up, and unrelated sequence of the same length
390
+ * scores 0.67 or below.
391
+ */
392
+ export function unitIdentity(seq: string, unit: string) {
393
+ return Math.min(...probeWindows(seq).map(w => tiledIdentity(w, unit)))
394
+ }
395
+
367
396
  export interface PeriodicAlignment {
368
397
  /** repeat unit length in bp */
369
398
  period: number
@@ -413,14 +442,21 @@ export function alignPeriodicInsertions(
413
442
  * which is a guess that one chimeric read overturns for the whole site. Measured
414
443
  * off the reference, it is a property of the locus, so every read is laid out
415
444
  * against the same unit whatever the reads happen to be.
445
+ *
446
+ * An empty sequence is one of the alleles, not a missing one: a row that
447
+ * deletes the whole array carries nought copies of it, which is a measurement
448
+ * and the most extreme contraction there is. It splits into no units and so
449
+ * lays out as a row of gaps the width of the block, which is what a deletion
450
+ * should look like. Dropping it instead left that row indistinguishable from
451
+ * one that never reached the array at all.
416
452
  */
417
453
  export function alignToUnit(
418
454
  seqs: string[],
419
455
  consensus: string,
420
456
  ): PeriodicAlignment | undefined {
421
- const distinct = [...new Set(seqs)].filter(s => s.length > 0)
457
+ const distinct = [...new Set(seqs)]
422
458
  const period = consensus.length
423
- if (!distinct.length || period < MIN_UNIT_PERIOD) {
459
+ if (!distinct.some(s => s.length) || period < MIN_UNIT_PERIOD) {
424
460
  return undefined
425
461
  }
426
462
 
@@ -1,4 +1,5 @@
1
1
  import { alignToUnit, unitIdentity } from './align'
2
+ import { ABSENT, SPANNED_GAP } from './readLayout'
2
3
 
3
4
  import type { ReadLayout } from './readLayout'
4
5
  import type { ReferenceArray } from './repeats'
@@ -32,9 +33,6 @@ export interface ArrayBlock {
32
33
  lengthByName: Map<string, number>
33
34
  }
34
35
 
35
- const SPANNED_GAP = '-'
36
- const ABSENT = '.'
37
-
38
36
  /**
39
37
  * What one row has between two reference positions: its matched bases, plus
40
38
  * whatever it inserted inside, minus whatever it deleted.
@@ -70,109 +68,219 @@ export function extractAllele(read: ReadLayout, start: number, end: number) {
70
68
  return ret
71
69
  }
72
70
 
73
- /** Reference positions an array owns, including the insertion slot at its right edge. */
74
- export function arrayInsertionKeys(array: { start: number; end: number }) {
75
- const ret: number[] = []
76
- for (let pos = array.start; pos <= array.end; pos++) {
77
- ret.push(pos)
78
- }
79
- return ret
71
+ /**
72
+ * How far outside an array an insertion is looked for, in reference bases.
73
+ *
74
+ * GIAB's adotto tandem repeat catalog buffers every interval by +/-25bp before
75
+ * merging, to leave room for the alignment ambiguity that comes with capturing
76
+ * variation inside a repeat, and measured what the buffer misses: re-expanding
77
+ * every region by a further 10bp would move only 0.2% of its boundaries
78
+ * (English et al., Nat Biotechnol 42, 2024, doi:10.1038/s41587-024-02225-z). A
79
+ * whole copy is the floor for a unit longer than that, since a copy is the
80
+ * aligner's room to slide a repeat indel. Both cover the misplacements measured
81
+ * in this trio: ATXN3 at 1bp, FMR1 at 2bp of a 3bp unit, ABCA7 at 19bp of a
82
+ * 25bp one.
83
+ *
84
+ * It bounds the search, not the answer. Whether an insertion actually moves is
85
+ * decided exactly, by `shiftInsertion`, so this being generous costs nothing.
86
+ */
87
+ const FLANK_BP = 25
88
+
89
+ /** the read's aligned base at a reference position, if it has one there */
90
+ function readBase(read: ReadLayout, pos: number) {
91
+ const i = pos - read.start
92
+ return i >= 0 && i < read.refChars.length ? read.refChars[i] : undefined
80
93
  }
81
94
 
82
95
  /**
83
- * How far outside an array an insertion of its own unit can be anchored, in
84
- * copies. An indel made of repeat units has no unique placement inside the
85
- * array, and the flanking bases let the aligner carry it a little further still
86
- * — but only about a copy, because past that the sequence either side stops
87
- * matching. Every misplaced repeat allele in the trio sits within one copy of
88
- * the edge: ATXN3's at 1bp, FMR1's at 2bp of a 3bp unit, ABCA7's at 19bp of a
89
- * 25bp one.
96
+ * How much of an insertion the array's unit has to explain before it counts as
97
+ * the array's. The repeat alleles the aligner anchored just outside an array in
98
+ * the trio score 0.9 and up, and unrelated sequence scores 0.67 or below, so
99
+ * this sits in open space rather than on a boundary.
100
+ */
101
+ const BELONGS_IDENTITY = 0.8
102
+
103
+ /**
104
+ * `ins`, inserted before reference position `from`, rewritten as an insertion
105
+ * before `to` — or undefined when the read's own bases do not allow it.
106
+ *
107
+ * An insertion has no unique position. `S` before a base `b` and `rotate(S)`
108
+ * after it spell the same read, so an aligner is free to anchor an expansion
109
+ * outside the array it belongs to, and at ATXN3 it did exactly that for most of
110
+ * the reads carrying one. Walking an indel through that equivalence class is
111
+ * the standard normalization for it (Tan, Abecasis & Kang, "Unified
112
+ * representation of genetic variants", Bioinformatics 31, 2015,
113
+ * doi:10.1093/bioinformatics/btv112, which walks left; this walks toward the
114
+ * array, the same operation with the array as the canonical anchor).
115
+ *
116
+ * The step is tested against the **read's** aligned base rather than the
117
+ * reference's, which is what makes the rewrite exact: the row still renders
118
+ * base for base as it did, and the only thing that changes is the reference
119
+ * position the bases are filed under. A step onto a deletion has no base to
120
+ * swap with and stops.
121
+ *
122
+ * Being exact, it is also incomplete, and measurably so. An aligner does not
123
+ * only choose among equivalent placements — it chooses the highest-scoring one,
124
+ * which for a repeat allele carrying an interruption is often not equivalent to
125
+ * any placement inside the array. FMR1's misplaced read is exactly that: 32bp
126
+ * of the locus's own CGG anchored two bases early, starting on a `C` where the
127
+ * read has a `G`, so no rewrite of it exists. Hence `reanchorInsertions` treats
128
+ * this as the preferred spelling and not as the test of whether to move.
90
129
  */
91
- const ABSORB_COPIES = 1
130
+ export function shiftInsertion(
131
+ read: ReadLayout,
132
+ from: number,
133
+ to: number,
134
+ ins: string,
135
+ ) {
136
+ let seq = ins
137
+ for (let pos = from; pos < to; pos++) {
138
+ const head = seq[0]!
139
+ if (readBase(read, pos) !== head) {
140
+ return undefined
141
+ }
142
+ seq = seq.slice(1) + head
143
+ }
144
+ for (let pos = from - 1; pos >= to; pos--) {
145
+ const tail = seq.at(-1)!
146
+ if (readBase(read, pos) !== tail) {
147
+ return undefined
148
+ }
149
+ seq = tail + seq.slice(0, -1)
150
+ }
151
+ return seq
152
+ }
153
+
154
+ /** an array with the span of reference positions whose insertions it may claim */
155
+ interface FlankWindow {
156
+ array: ReferenceArray
157
+ from: number
158
+ to: number
159
+ }
92
160
 
93
161
  /**
94
- * How much of the inserted sequence the array's unit has to explain before the
95
- * insertion counts as part of the array. The repeat alleles the aligner
96
- * anchored just outside an array in the trio score 0.94-1.00 here and unrelated
97
- * sequence scores 0.67 or below, so this sits in open space rather than on a
98
- * boundary.
162
+ * The window around each array that its insertions may be re-anchored from.
163
+ *
164
+ * Two arrays may not claim one position: `extractAllele` reads the slot at an
165
+ * array's `end`, so a shared slot would be counted into two alleles. Neighbours
166
+ * split the reference between them down the middle — a property of the
167
+ * reference alone, so unlike the interval it replaces, no read can move it.
99
168
  */
100
- const ABSORB_IDENTITY = 0.8
169
+ export function flankWindows(arrays: ReferenceArray[]): FlankWindow[] {
170
+ const ordered = [...arrays].sort((a, b) => a.start - b.start)
171
+ return ordered.map((array, i) => {
172
+ const pad = Math.max(FLANK_BP, array.period)
173
+ const previous = ordered[i - 1]
174
+ const next = ordered[i + 1]
175
+ return {
176
+ array,
177
+ from: Math.max(
178
+ array.start - pad,
179
+ previous ? Math.floor((previous.end + array.start) / 2) + 1 : -Infinity,
180
+ ),
181
+ to: Math.min(
182
+ array.end + pad,
183
+ next ? Math.floor((array.end + next.start) / 2) : Infinity,
184
+ ),
185
+ }
186
+ })
187
+ }
188
+
189
+ /** the array whose window `pos` falls in, if any */
190
+ function windowAt(pos: number, windows: FlankWindow[]) {
191
+ for (const window of windows) {
192
+ if (pos < window.from) {
193
+ return undefined
194
+ }
195
+ if (pos <= window.to) {
196
+ return window
197
+ }
198
+ }
199
+ return undefined
200
+ }
101
201
 
102
202
  /**
103
- * Widen each array to cover the insertions the aligner anchored just outside it
104
- * that are made of the array's own unit.
203
+ * Re-file each read's insertions under the array they belong to, one read at a
204
+ * time.
205
+ *
206
+ * Measuring an allele over a reference interval fixes the "one array reports as
207
+ * several sites" problem for indels the aligner placed *inside* the interval.
208
+ * It does not fix the ones placed just outside: there the insertion is part of
209
+ * no allele — it becomes its own run of insertion columns — and the read
210
+ * carrying it is measured as though it had the reference allele. Both halves of
211
+ * that are wrong, and neither is rare: at ATXN3 the aligner anchored 60 of 162
212
+ * reads' expansions one base outside the array.
105
213
  *
106
- * An allele is measured over a reference interval, which fixes the "one array
107
- * reports as several sites" problem for indels the aligner placed *inside* the
108
- * interval. It does not fix the ones it placed just outside: an aligner is free
109
- * to anchor an expansion at the base before the array starts, and there the
110
- * insertion is not part of any allele — it is left to become its own run of
111
- * insertion columns, and the read it belongs to is measured as if it carried
112
- * the reference allele. Both halves of that are wrong, and they are not rare:
113
- * at ATXN3 the aligner anchored 60 of 162 reads' expansions one base outside
114
- * the array, and at ABCA7 a 1207bp allele 19 bases outside.
214
+ * Doing this per read is the whole point. Widening the shared interval instead
215
+ * — which is what this replaces — made one read's misplacement everyone's: a
216
+ * single spurious copy anchored two bases early moved the array's left edge for
217
+ * every row, and the reference's own copy count with it. Re-anchoring touches
218
+ * only the read that carries the insertion, so a read can be wrong on its own.
115
219
  *
116
- * What is absorbed is decided by the inserted sequence, not by the count of
117
- * reads carrying it — a single read's expansion is as real as fifty, and the
118
- * whole point of measuring over an interval is that it does not need a vote.
119
- * Widening the interval costs every row the few reference bases in between,
120
- * which they all pay equally, so the counts stay comparable.
220
+ * Nothing here consults the other reads, or how many of them agree. A single
221
+ * read's expansion is as real as fifty, which is what measuring over an
222
+ * interval buys and what a vote would give back. What decides an insertion is
223
+ * its own sequence: `unitIdentity` against the array's unit, since the aligner
224
+ * is free to place a repeat allele outside the array but is not free to make
225
+ * unrelated sequence out of the array's unit.
121
226
  */
122
- export function absorbAdjacentInsertions(
227
+ export function reanchorInsertions(
123
228
  reads: ReadLayout[],
124
229
  arrays: ReferenceArray[],
125
230
  ) {
126
- const ordered = [...arrays].sort((a, b) => a.start - b.start)
127
- // the widened end of the array to the left, which the next one may not reach
128
- let previousEnd = -Infinity
129
- return ordered.map((array, i) => {
130
- const margin = ABSORB_COPIES * array.period
131
- // Two arrays that met at a position would read the same insertion slot
132
- // twice — once as each one's allele — so they are held apart on both
133
- // sides: leftwards off whatever the previous array grew to, rightwards off
134
- // where the next one starts, which is as far left as that one can be
135
- // pushed in turn. Neither bound can cross the array's own edges, since
136
- // mergeArrays has already left a gap between every pair.
137
- const floor = Math.max(array.start - margin, previousEnd + 1)
138
- const ceiling = Math.min(
139
- array.end + margin,
140
- (ordered[i + 1]?.start ?? Infinity) - 1,
141
- )
142
- let { start, end } = array
143
- // memoized per distinct insert: one locus routinely has dozens of reads
144
- // carrying the same allele at the same site
145
- const belongs = new Map<string, boolean>()
146
- const isUnit = (ins: string) => {
147
- let ret = belongs.get(ins)
148
- if (ret === undefined) {
149
- ret = unitIdentity(ins, array.unit) >= ABSORB_IDENTITY
150
- belongs.set(ins, ret)
151
- }
152
- return ret
231
+ const windows = flankWindows(arrays)
232
+ if (!windows.length) {
233
+ return reads
234
+ }
235
+ // memoized per array and distinct insert: one locus routinely has dozens of
236
+ // reads carrying the same allele at the same site
237
+ const belongs = new Map<string, boolean>()
238
+ const isUnit = (window: FlankWindow, ins: string) => {
239
+ const key = `${window.array.start}:${ins}`
240
+ let ret = belongs.get(key)
241
+ if (ret === undefined) {
242
+ ret =
243
+ // a whole copy at least: a base or two of read error beside an array
244
+ // says nothing about where its allele ends, and the identity of a
245
+ // fragment shorter than the unit is not a measurement of anything
246
+ ins.length >= window.array.period &&
247
+ unitIdentity(ins, window.array.unit) >= BELONGS_IDENTITY
248
+ belongs.set(key, ret)
153
249
  }
154
- for (const read of reads) {
155
- for (const [pos, ins] of read.insertions) {
156
- // a whole copy at least: a base or two of read error next to an array
157
- // says nothing about where the array ends, and moving the interval for
158
- // it would lay flanking sequence out as repeat copies
159
- if (
160
- ins.length < array.period ||
161
- pos < floor ||
162
- pos > ceiling ||
163
- (pos >= array.start && pos <= array.end) ||
164
- !isUnit(ins)
165
- ) {
166
- continue
167
- }
168
- start = Math.min(start, pos)
169
- // an insertion is keyed to the position it precedes, and extractAllele
170
- // reads the slot at `end`, so covering it means reaching that position
171
- end = Math.max(end, pos)
172
- }
250
+ return ret
251
+ }
252
+ return reads.map(read => {
253
+ if (!read.insertions.size) {
254
+ return read
255
+ }
256
+ let moved = false
257
+ const next = new Map<number, string>()
258
+ // Insertions are re-filed in genomic order of where the aligner put them,
259
+ // so two events landing in one slot keep their order along the read.
260
+ for (const [pos, ins] of [...read.insertions].sort((a, b) => a[0] - b[0])) {
261
+ const window = windowAt(pos, windows)
262
+ const { start, end } = window?.array ?? {}
263
+ const target =
264
+ window === undefined || (pos >= start! && pos <= end!)
265
+ ? undefined
266
+ : isUnit(window, ins)
267
+ ? pos < start!
268
+ ? start!
269
+ : end!
270
+ : undefined
271
+ // an exact rewrite is preferred where one exists, so a row that can be
272
+ // re-spelled reads base for base as the aligner wrote it; where none
273
+ // exists the bases move as written, which preserves the length the
274
+ // measurement is made of
275
+ const at = target ?? pos
276
+ moved ||= target !== undefined
277
+ const seq =
278
+ target === undefined
279
+ ? ins
280
+ : (shiftInsertion(read, pos, target, ins) ?? ins)
281
+ next.set(at, (next.get(at) ?? '') + seq)
173
282
  }
174
- previousEnd = end
175
- return { ...array, start, end }
283
+ return moved ? { ...read, insertions: next } : read
176
284
  })
177
285
  }
178
286