jbrowse-plugin-tview 2.0.6 → 2.2.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.
- package/README.md +222 -4
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.d.ts +132 -0
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js +152 -0
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js.map +1 -0
- package/dist/LaunchTView/TviewGetPlanRpc/index.d.ts +3 -0
- package/dist/LaunchTView/TviewGetPlanRpc/index.js +5 -0
- package/dist/LaunchTView/TviewGetPlanRpc/index.js.map +1 -0
- package/dist/LaunchTView/align.d.ts +106 -9
- package/dist/LaunchTView/align.js +349 -15
- package/dist/LaunchTView/align.js.map +1 -1
- package/dist/LaunchTView/alleles.d.ts +83 -0
- package/dist/LaunchTView/alleles.js +180 -0
- package/dist/LaunchTView/alleles.js.map +1 -0
- package/dist/LaunchTView/components/ArrayReport.d.ts +10 -0
- package/dist/LaunchTView/components/ArrayReport.js +59 -0
- package/dist/LaunchTView/components/ArrayReport.js.map +1 -0
- package/dist/LaunchTView/components/LaunchTViewDialog.js +74 -37
- package/dist/LaunchTView/components/LaunchTViewDialog.js.map +1 -1
- package/dist/LaunchTView/fetchTviewPlan.d.ts +20 -13
- package/dist/LaunchTView/fetchTviewPlan.js +27 -27
- package/dist/LaunchTView/fetchTviewPlan.js.map +1 -1
- package/dist/LaunchTView/launchView.d.ts +25 -0
- package/dist/LaunchTView/launchView.js +20 -0
- package/dist/LaunchTView/launchView.js.map +1 -0
- package/dist/LaunchTView/readLayout.d.ts +85 -0
- package/dist/LaunchTView/readLayout.js +96 -0
- package/dist/LaunchTView/readLayout.js.map +1 -0
- package/dist/LaunchTView/repeatStats.d.ts +56 -0
- package/dist/LaunchTView/repeatStats.js +109 -0
- package/dist/LaunchTView/repeatStats.js.map +1 -0
- package/dist/LaunchTView/repeats.d.ts +68 -0
- package/dist/LaunchTView/repeats.js +200 -0
- package/dist/LaunchTView/repeats.js.map +1 -0
- package/dist/LaunchTView/sampleTree.d.ts +37 -0
- package/dist/LaunchTView/sampleTree.js +65 -0
- package/dist/LaunchTView/sampleTree.js.map +1 -0
- package/dist/LaunchTView/tview.d.ts +67 -44
- package/dist/LaunchTView/tview.js +193 -121
- package/dist/LaunchTView/tview.js.map +1 -1
- package/dist/LaunchTView/useTviewMsa.d.ts +13 -9
- package/dist/LaunchTView/useTviewMsa.js +15 -11
- package/dist/LaunchTView/useTviewMsa.js.map +1 -1
- package/dist/TViewPanel/coords.d.ts +9 -1
- package/dist/TViewPanel/coords.js +22 -2
- package/dist/TViewPanel/coords.js.map +1 -1
- package/dist/TViewPanel/init.d.ts +56 -0
- package/dist/TViewPanel/init.js +59 -0
- package/dist/TViewPanel/init.js.map +1 -0
- package/dist/TViewPanel/model.d.ts +109 -93
- package/dist/TViewPanel/model.js +73 -51
- package/dist/TViewPanel/model.js.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/jbrowse-plugin-tview.umd.production.min.js +25 -25
- package/dist/jbrowse-plugin-tview.umd.production.min.js.map +4 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +9 -3
- package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +264 -0
- package/src/LaunchTView/TviewGetPlanRpc/index.ts +14 -0
- package/src/LaunchTView/align.ts +406 -17
- package/src/LaunchTView/alleles.ts +229 -0
- package/src/LaunchTView/components/ArrayReport.tsx +64 -0
- package/src/LaunchTView/components/LaunchTViewDialog.tsx +122 -39
- package/src/LaunchTView/fetchTviewPlan.ts +42 -42
- package/src/LaunchTView/launchView.ts +50 -0
- package/src/LaunchTView/readLayout.ts +145 -0
- package/src/LaunchTView/repeatStats.ts +170 -0
- package/src/LaunchTView/repeats.ts +218 -0
- package/src/LaunchTView/sampleTree.ts +73 -0
- package/src/LaunchTView/tview.ts +265 -157
- package/src/LaunchTView/useTviewMsa.ts +20 -17
- package/src/TViewPanel/coords.ts +23 -1
- package/src/TViewPanel/init.ts +127 -0
- package/src/TViewPanel/model.ts +82 -61
- package/src/index.ts +5 -1
- package/src/version.ts +1 -1
package/src/LaunchTView/align.ts
CHANGED
|
@@ -5,10 +5,24 @@
|
|
|
5
5
|
* shared event stops being readable. Aligning the inserted sequences against
|
|
6
6
|
* each other fixes that.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* There are two ways to do it, and which one applies is a property of the
|
|
9
|
+
* inserted sequence rather than a setting:
|
|
10
|
+
*
|
|
11
|
+
* - **Aperiodic** (`alignInsertions`): one center-star alignment over the whole
|
|
12
|
+
* inserted sequence. Right for a novel insertion, a mobile element, anything
|
|
13
|
+
* whose bases only make sense read straight through.
|
|
14
|
+
* - **Periodic** (`alignPeriodicInsertions`): a tandem array, where the whole
|
|
15
|
+
* inserted sequence is n copies of one unit. Aligning those base-to-base is
|
|
16
|
+
* both unaffordable and wrong — it is free to align copy 5 of one read to
|
|
17
|
+
* copy 3 of another, and the difference that matters (how many copies) ends
|
|
18
|
+
* up expressed as where the row stops. Aligning unit-to-unit instead puts
|
|
19
|
+
* copy k of every read in the same columns, so copy number reads as a count
|
|
20
|
+
* of blocks and a divergent copy stands out as a column inside its block.
|
|
21
|
+
*
|
|
22
|
+
* Both are local to one reference position: the insertion still occupies "as
|
|
23
|
+
* many columns as the widest gapped sequence", which is the only thing
|
|
24
|
+
* buildColumnToRefPos knows about, so the column <-> reference mapping is
|
|
25
|
+
* untouched either way.
|
|
12
26
|
*/
|
|
13
27
|
|
|
14
28
|
const MATCH = 2
|
|
@@ -20,6 +34,36 @@ const GAP = -3
|
|
|
20
34
|
const MAX_LEN = 200
|
|
21
35
|
const MAX_DISTINCT = 30
|
|
22
36
|
|
|
37
|
+
// Shortest unit that earns a unit-per-block layout. The scan below starts at 1
|
|
38
|
+
// so it reports the TRUE minimal period, which is what makes a homopolymer come
|
|
39
|
+
// back as 1 and get declined here: `AAAA...` is periodic at every lag, and
|
|
40
|
+
// laying it out as 2bp blocks would be an arbitrary reading of one run of A.
|
|
41
|
+
const MIN_UNIT_PERIOD = 2
|
|
42
|
+
|
|
43
|
+
// Longest unit the scan looks for. Covers STRs and the common VNTR range; the
|
|
44
|
+
// megabase-unit arrays (LPA KIV-2 at 5.5kb) need more copies than a read holds
|
|
45
|
+
// before they are periodic at all, so they are out of reach here regardless.
|
|
46
|
+
const MAX_PERIOD = 300
|
|
47
|
+
|
|
48
|
+
// Copies needed before a sequence counts as an array. Two copies of anything is
|
|
49
|
+
// a duplication, not a tandem repeat, and at two the period is unidentifiable
|
|
50
|
+
// from the sequence alone.
|
|
51
|
+
const MIN_COPIES = 3
|
|
52
|
+
|
|
53
|
+
// Fraction of positions that must agree at lag p for p to be the period. Loose
|
|
54
|
+
// enough for the ~5-15% divergence between copies of a real VNTR unit and for
|
|
55
|
+
// long-read error on top of it.
|
|
56
|
+
const PERIODIC_MATCH = 0.8
|
|
57
|
+
|
|
58
|
+
// The period scan is O(len * maxPeriod), so it runs on a prefix rather than the
|
|
59
|
+
// whole insert. An array that is periodic in its first 3kb is periodic.
|
|
60
|
+
const PROBE_LEN = 3000
|
|
61
|
+
|
|
62
|
+
// Backstops on the unit-level work, in the same spirit as MAX_LEN/MAX_DISTINCT:
|
|
63
|
+
// decline and leave the site to the aperiodic path rather than stall on it.
|
|
64
|
+
const MAX_UNITS = 1000
|
|
65
|
+
const MAX_PERIODIC_CELLS = 20_000_000
|
|
66
|
+
|
|
23
67
|
function subst(a: string | undefined, b: string | undefined) {
|
|
24
68
|
return a === b ? MATCH : MISMATCH
|
|
25
69
|
}
|
|
@@ -115,21 +159,355 @@ function emit({ slots, atCenter }: CenterSplit, slotWidths: number[]) {
|
|
|
115
159
|
}
|
|
116
160
|
|
|
117
161
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
162
|
+
* The smallest lag at which a sequence repeats, or undefined when it does not.
|
|
163
|
+
*
|
|
164
|
+
* Scanning up from 1 returns the true minimal period, which is what separates a
|
|
165
|
+
* tandem array from a homopolymer: `AAAA...` matches at every lag and comes back
|
|
166
|
+
* as 1, and a 30bp unit comes back as 30 rather than as 60 or 90.
|
|
123
167
|
*/
|
|
124
|
-
export function
|
|
168
|
+
export function detectPeriod(seq: string): number | undefined {
|
|
169
|
+
const probe = seq.length > PROBE_LEN ? seq.slice(0, PROBE_LEN) : seq
|
|
170
|
+
const maxPeriod = Math.min(MAX_PERIOD, Math.floor(probe.length / MIN_COPIES))
|
|
171
|
+
for (let p = 1; p <= maxPeriod; p++) {
|
|
172
|
+
const n = probe.length - p
|
|
173
|
+
let matches = 0
|
|
174
|
+
for (let i = 0; i < n; i++) {
|
|
175
|
+
if (probe[i] === probe[i + p]) {
|
|
176
|
+
matches++
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (matches / n >= PERIODIC_MATCH) {
|
|
180
|
+
return p
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return undefined
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Majority base at each offset within the unit, over the whole-length copies of
|
|
188
|
+
* `seq`. The consensus is what every copy of every read is aligned to, so it has
|
|
189
|
+
* to be one fixed string for the site — deriving it per read would let two reads
|
|
190
|
+
* disagree about where a unit starts.
|
|
191
|
+
*/
|
|
192
|
+
export function consensusUnit(seq: string, period: number) {
|
|
193
|
+
const copies = Math.floor(seq.length / period)
|
|
194
|
+
let ret = ''
|
|
195
|
+
for (let j = 0; j < period; j++) {
|
|
196
|
+
const counts = new Map<string, number>()
|
|
197
|
+
for (let k = 0; k < copies; k++) {
|
|
198
|
+
const c = seq[k * period + j]!
|
|
199
|
+
counts.set(c, (counts.get(c) ?? 0) + 1)
|
|
200
|
+
}
|
|
201
|
+
// ties break lexicographically so the consensus is a function of the input
|
|
202
|
+
// and not of Map iteration order
|
|
203
|
+
ret += [...counts].sort(
|
|
204
|
+
(a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1),
|
|
205
|
+
)[0]![0]
|
|
206
|
+
}
|
|
207
|
+
return ret
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Align one copy of the consensus unit against `seq` starting at `from`: global
|
|
212
|
+
* on the consensus, since a copy is a whole copy, and free at the window's right
|
|
213
|
+
* edge, since the copy ends wherever it ends. `consumed` is where the next copy
|
|
214
|
+
* starts, which is what lets an indel inside one copy stay inside it rather than
|
|
215
|
+
* shifting every copy after it.
|
|
216
|
+
*/
|
|
217
|
+
function alignUnitAt(consensus: string, seq: string, from: number) {
|
|
218
|
+
const n = consensus.length
|
|
219
|
+
const m = Math.min(2 * n, seq.length - from)
|
|
220
|
+
const stride = m + 1
|
|
221
|
+
const score = new Int32Array((n + 1) * stride)
|
|
222
|
+
for (let i = 1; i <= n; i++) {
|
|
223
|
+
score[i * stride] = i * GAP
|
|
224
|
+
}
|
|
225
|
+
for (let j = 1; j <= m; j++) {
|
|
226
|
+
score[j] = j * GAP
|
|
227
|
+
}
|
|
228
|
+
for (let i = 1; i <= n; i++) {
|
|
229
|
+
for (let j = 1; j <= m; j++) {
|
|
230
|
+
score[i * stride + j] = Math.max(
|
|
231
|
+
score[(i - 1) * stride + j - 1]! +
|
|
232
|
+
subst(consensus[i - 1], seq[from + j - 1]),
|
|
233
|
+
score[(i - 1) * stride + j]! + GAP,
|
|
234
|
+
score[i * stride + j - 1]! + GAP,
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// the copy ends at whichever window position scores best once the consensus
|
|
240
|
+
// is spent
|
|
241
|
+
let consumed = 0
|
|
242
|
+
let best = score[n * stride]!
|
|
243
|
+
for (let j = 1; j <= m; j++) {
|
|
244
|
+
const s = score[n * stride + j]!
|
|
245
|
+
if (s > best) {
|
|
246
|
+
best = s
|
|
247
|
+
consumed = j
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// a copy that consumed nothing would not advance the cursor
|
|
251
|
+
if (consumed === 0) {
|
|
252
|
+
return undefined
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const outCenter: string[] = []
|
|
256
|
+
const outOther: string[] = []
|
|
257
|
+
let i = n
|
|
258
|
+
let j = consumed
|
|
259
|
+
while (i > 0 || j > 0) {
|
|
260
|
+
const cur = score[i * stride + j]!
|
|
261
|
+
const diagonal =
|
|
262
|
+
i > 0 &&
|
|
263
|
+
j > 0 &&
|
|
264
|
+
cur ===
|
|
265
|
+
score[(i - 1) * stride + j - 1]! +
|
|
266
|
+
subst(consensus[i - 1], seq[from + j - 1])
|
|
267
|
+
if (diagonal) {
|
|
268
|
+
outCenter.push(consensus[i - 1]!)
|
|
269
|
+
outOther.push(seq[from + j - 1]!)
|
|
270
|
+
i--
|
|
271
|
+
j--
|
|
272
|
+
} else if (i > 0 && cur === score[(i - 1) * stride + j]! + GAP) {
|
|
273
|
+
outCenter.push(consensus[i - 1]!)
|
|
274
|
+
outOther.push('-')
|
|
275
|
+
i--
|
|
276
|
+
} else {
|
|
277
|
+
outCenter.push('-')
|
|
278
|
+
outOther.push(seq[from + j - 1]!)
|
|
279
|
+
j--
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
center: outCenter.reverse().join(''),
|
|
284
|
+
other: outOther.reverse().join(''),
|
|
285
|
+
consumed,
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Every copy in `seq`, each as a pairwise alignment against the consensus. */
|
|
290
|
+
function splitIntoUnits(seq: string, consensus: string) {
|
|
291
|
+
const units: { center: string; other: string }[] = []
|
|
292
|
+
let cursor = 0
|
|
293
|
+
while (cursor < seq.length) {
|
|
294
|
+
const unit = alignUnitAt(consensus, seq, cursor)
|
|
295
|
+
if (!unit || units.length >= MAX_UNITS) {
|
|
296
|
+
return undefined
|
|
297
|
+
}
|
|
298
|
+
units.push(unit)
|
|
299
|
+
cursor += unit.consumed
|
|
300
|
+
}
|
|
301
|
+
return units
|
|
302
|
+
}
|
|
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.
|
|
307
|
+
const IDENTITY_PROBE_LEN = 300
|
|
308
|
+
|
|
309
|
+
/**
|
|
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.
|
|
312
|
+
*
|
|
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.
|
|
320
|
+
*/
|
|
321
|
+
export function unitIdentity(seq: string, unit: string) {
|
|
322
|
+
const probe =
|
|
323
|
+
seq.length > IDENTITY_PROBE_LEN ? seq.slice(0, IDENTITY_PROBE_LEN) : seq
|
|
324
|
+
const n = probe.length
|
|
325
|
+
if (!n || !unit.length) {
|
|
326
|
+
return 0
|
|
327
|
+
}
|
|
328
|
+
// two spare copies so the alignment can start mid-copy and still run off the
|
|
329
|
+
// end without paying for it
|
|
330
|
+
const tile = unit.repeat(Math.ceil(n / unit.length) + 2)
|
|
331
|
+
const m = tile.length
|
|
332
|
+
const stride = m + 1
|
|
333
|
+
const score = new Int32Array((n + 1) * stride)
|
|
334
|
+
const matches = new Int32Array((n + 1) * stride)
|
|
335
|
+
// row 0 stays zero: starting anywhere in the tiling is free
|
|
336
|
+
for (let i = 1; i <= n; i++) {
|
|
337
|
+
score[i * stride] = i * GAP
|
|
338
|
+
}
|
|
339
|
+
for (let i = 1; i <= n; i++) {
|
|
340
|
+
for (let j = 1; j <= m; j++) {
|
|
341
|
+
const hit = probe[i - 1] === tile[j - 1]
|
|
342
|
+
const diagonal =
|
|
343
|
+
score[(i - 1) * stride + j - 1]! + (hit ? MATCH : MISMATCH)
|
|
344
|
+
const up = score[(i - 1) * stride + j]! + GAP
|
|
345
|
+
const left = score[i * stride + j - 1]! + GAP
|
|
346
|
+
const best = Math.max(diagonal, up, left)
|
|
347
|
+
score[i * stride + j] = best
|
|
348
|
+
matches[i * stride + j] =
|
|
349
|
+
best === diagonal
|
|
350
|
+
? matches[(i - 1) * stride + j - 1]! + (hit ? 1 : 0)
|
|
351
|
+
: best === up
|
|
352
|
+
? matches[(i - 1) * stride + j]!
|
|
353
|
+
: matches[i * stride + j - 1]!
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
let best = score[n * stride]!
|
|
357
|
+
let bestMatches = matches[n * stride]!
|
|
358
|
+
for (let j = 1; j <= m; j++) {
|
|
359
|
+
if (score[n * stride + j]! > best) {
|
|
360
|
+
best = score[n * stride + j]!
|
|
361
|
+
bestMatches = matches[n * stride + j]!
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return bestMatches / n
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export interface PeriodicAlignment {
|
|
368
|
+
/** repeat unit length in bp */
|
|
369
|
+
period: number
|
|
370
|
+
/** each distinct input mapped to a gapped version, all the same length */
|
|
371
|
+
gapped: Map<string, string>
|
|
372
|
+
/** each distinct input mapped to its copy count */
|
|
373
|
+
copies: Map<string, number>
|
|
374
|
+
/** columns each copy occupies, left to right; sums to the gapped length */
|
|
375
|
+
unitWidths: number[]
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Unit-per-block alignment of the sequences inserted at one reference position.
|
|
380
|
+
*
|
|
381
|
+
* Copy k of every sequence lands in the same block of columns, and a block is
|
|
382
|
+
* as wide as the widest version of that copy across the site, so a sequence with
|
|
383
|
+
* fewer copies runs out of blocks rather than drifting out of register. Returns
|
|
384
|
+
* undefined when the site is not a tandem array, or is too big to lay out this
|
|
385
|
+
* way; the caller falls back to `alignInsertions`.
|
|
386
|
+
*
|
|
387
|
+
* **Blocks are array order, not homology.** Copies are anchored at the left edge
|
|
388
|
+
* of the insertion and counted rightward, so block k is "the kth copy along",
|
|
389
|
+
* and arrays grow and shrink by unequal crossing over anywhere inside them —
|
|
390
|
+
* block 9 of a 12-copy allele and block 9 of a 20-copy allele need not be the
|
|
391
|
+
* same copy. What the layout does buy is that copies stay in register, so a
|
|
392
|
+
* single divergent copy shows up as a column instead of shifting every copy
|
|
393
|
+
* after it. Reading down one row is sound; reading across two is a hypothesis.
|
|
394
|
+
*/
|
|
395
|
+
export function alignPeriodicInsertions(
|
|
396
|
+
seqs: string[],
|
|
397
|
+
): PeriodicAlignment | undefined {
|
|
125
398
|
const distinct = [...new Set(seqs)]
|
|
126
399
|
const longest = distinct.reduce((a, b) => (b.length > a.length ? b : a), '')
|
|
400
|
+
const period = detectPeriod(longest)
|
|
401
|
+
if (period === undefined || period < MIN_UNIT_PERIOD) {
|
|
402
|
+
return undefined
|
|
403
|
+
}
|
|
404
|
+
return alignToUnit(distinct, consensusUnit(longest, period))
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Unit-per-block alignment against a unit the caller already knows.
|
|
409
|
+
*
|
|
410
|
+
* Separated from `alignPeriodicInsertions` because where the unit comes from is
|
|
411
|
+
* the whole difference between the two ways of finding an array. Measured off
|
|
412
|
+
* the inserted sequences, it can only be derived from one of them — the longest,
|
|
413
|
+
* which is a guess that one chimeric read overturns for the whole site. Measured
|
|
414
|
+
* off the reference, it is a property of the locus, so every read is laid out
|
|
415
|
+
* against the same unit whatever the reads happen to be.
|
|
416
|
+
*/
|
|
417
|
+
export function alignToUnit(
|
|
418
|
+
seqs: string[],
|
|
419
|
+
consensus: string,
|
|
420
|
+
): PeriodicAlignment | undefined {
|
|
421
|
+
const distinct = [...new Set(seqs)].filter(s => s.length > 0)
|
|
422
|
+
const period = consensus.length
|
|
423
|
+
if (!distinct.length || period < MIN_UNIT_PERIOD) {
|
|
424
|
+
return undefined
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const totalUnits = distinct.reduce(
|
|
428
|
+
(a, s) => a + Math.ceil(s.length / period),
|
|
429
|
+
0,
|
|
430
|
+
)
|
|
431
|
+
if (totalUnits * (period + 1) * (2 * period + 1) > MAX_PERIODIC_CELLS) {
|
|
432
|
+
return undefined
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const splits = new Map<string, CenterSplit[]>()
|
|
436
|
+
for (const seq of distinct) {
|
|
437
|
+
const units = splitIntoUnits(seq, consensus)
|
|
438
|
+
if (!units) {
|
|
439
|
+
return undefined
|
|
440
|
+
}
|
|
441
|
+
splits.set(
|
|
442
|
+
seq,
|
|
443
|
+
units.map(u => splitAgainstCenter(u, period)),
|
|
444
|
+
)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const maxUnits = Math.max(...[...splits.values()].map(u => u.length))
|
|
448
|
+
const slotWidths: number[][] = []
|
|
449
|
+
for (let k = 0; k < maxUnits; k++) {
|
|
450
|
+
const widths = Array.from({ length: period + 1 }, () => 0)
|
|
451
|
+
for (const unitSplits of splits.values()) {
|
|
452
|
+
const split = unitSplits[k]
|
|
453
|
+
if (split) {
|
|
454
|
+
for (let j = 0; j <= period; j++) {
|
|
455
|
+
widths[j] = Math.max(widths[j]!, split.slots[j]!.length)
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
slotWidths.push(widths)
|
|
460
|
+
}
|
|
461
|
+
const unitWidths = slotWidths.map(w => period + w.reduce((a, b) => a + b, 0))
|
|
462
|
+
|
|
463
|
+
const gapped = new Map<string, string>()
|
|
464
|
+
const copies = new Map<string, number>()
|
|
465
|
+
for (const [seq, unitSplits] of splits) {
|
|
466
|
+
let row = ''
|
|
467
|
+
for (let k = 0; k < maxUnits; k++) {
|
|
468
|
+
const split = unitSplits[k]
|
|
469
|
+
row += split ? emit(split, slotWidths[k]!) : '-'.repeat(unitWidths[k]!)
|
|
470
|
+
}
|
|
471
|
+
gapped.set(seq, row)
|
|
472
|
+
copies.set(seq, unitSplits.length)
|
|
473
|
+
}
|
|
474
|
+
return { period, gapped, copies, unitWidths }
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export interface InsertionAlignment {
|
|
478
|
+
/** each distinct input mapped to a gapped version, all of the same length */
|
|
479
|
+
gapped: Map<string, string>
|
|
480
|
+
/** present when the site is a tandem array */
|
|
481
|
+
periodic?: PeriodicAlignment
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Lay out the sequences inserted at one reference position, by whichever of the
|
|
486
|
+
* two alignments the sequence itself calls for. The single place that decision
|
|
487
|
+
* is made, so a caller reading the period cannot disagree with the layout it
|
|
488
|
+
* gets.
|
|
489
|
+
*
|
|
490
|
+
* A tandem array goes unit-per-block, which is both the readable answer and the
|
|
491
|
+
* affordable one — the size caps here are what the base-to-base alignment costs,
|
|
492
|
+
* and an expansion is always past them. Everything else gets the center-star
|
|
493
|
+
* alignment, or is left alone (`gapped` empty) when there is nothing to gain
|
|
494
|
+
* from a single distinct sequence, or the site is too large to be worth it.
|
|
495
|
+
*/
|
|
496
|
+
export function alignInsertionSite(seqs: string[]): InsertionAlignment {
|
|
497
|
+
const distinct = [...new Set(seqs)]
|
|
498
|
+
// an array is worth unrolling even when every read agrees on it: the copies
|
|
499
|
+
// still have to be squared up with each other before they can be counted
|
|
500
|
+
const periodic = alignPeriodicInsertions(distinct)
|
|
501
|
+
if (periodic) {
|
|
502
|
+
return { gapped: periodic.gapped, periodic }
|
|
503
|
+
}
|
|
504
|
+
const longest = distinct.reduce((a, b) => (b.length > a.length ? b : a), '')
|
|
127
505
|
if (
|
|
128
506
|
distinct.length < 2 ||
|
|
129
507
|
distinct.length > MAX_DISTINCT ||
|
|
130
508
|
longest.length > MAX_LEN
|
|
131
509
|
) {
|
|
132
|
-
return new Map<string, string>()
|
|
510
|
+
return { gapped: new Map<string, string>() }
|
|
133
511
|
}
|
|
134
512
|
|
|
135
513
|
const centerLen = longest.length
|
|
@@ -145,10 +523,21 @@ export function alignInsertions(seqs: string[]) {
|
|
|
145
523
|
const slotWidths = Array.from({ length: centerLen + 1 }, (_, j) =>
|
|
146
524
|
splits.reduce((max, s) => Math.max(max, s.slots[j]!.length), 0),
|
|
147
525
|
)
|
|
148
|
-
return
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
(seq, i): [string, string] => [
|
|
152
|
-
|
|
153
|
-
|
|
526
|
+
return {
|
|
527
|
+
gapped: new Map([
|
|
528
|
+
[longest, emit(centerSplit, slotWidths)],
|
|
529
|
+
...others.map((seq, i): [string, string] => [
|
|
530
|
+
seq,
|
|
531
|
+
emit(splits[i]!, slotWidths),
|
|
532
|
+
]),
|
|
533
|
+
]),
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* The gapped sequences alone, for callers with no use for the period. Every
|
|
539
|
+
* decision lives in `alignInsertionSite`.
|
|
540
|
+
*/
|
|
541
|
+
export function alignInsertions(seqs: string[]) {
|
|
542
|
+
return alignInsertionSite(seqs).gapped
|
|
154
543
|
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { alignToUnit, unitIdentity } from './align'
|
|
2
|
+
|
|
3
|
+
import type { ReadLayout } from './readLayout'
|
|
4
|
+
import type { ReferenceArray } from './repeats'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A tandem array laid out as one block of columns per copy, replacing the
|
|
8
|
+
* reference columns the array covers.
|
|
9
|
+
*
|
|
10
|
+
* The interval comes from the reference and the unit comes with it, so every
|
|
11
|
+
* row is measured over the same span against the same unit. That is what makes
|
|
12
|
+
* the counts comparable: a read's allele is what it has between here and here,
|
|
13
|
+
* whatever the aligner did with the gap, and the reference is just the row whose
|
|
14
|
+
* allele the coordinates are named after.
|
|
15
|
+
*/
|
|
16
|
+
export interface ArrayBlock {
|
|
17
|
+
/** reference position the array starts at, inclusive */
|
|
18
|
+
start: number
|
|
19
|
+
/** reference position the array ends at, exclusive */
|
|
20
|
+
end: number
|
|
21
|
+
period: number
|
|
22
|
+
unit: string
|
|
23
|
+
/** columns the block occupies */
|
|
24
|
+
width: number
|
|
25
|
+
/** columns each copy occupies, left to right; sums to `width` */
|
|
26
|
+
unitWidths: number[]
|
|
27
|
+
/** gapped block, by row name; only rows whose allele could be measured */
|
|
28
|
+
rowByName: Map<string, string>
|
|
29
|
+
/** copies carried, by row name */
|
|
30
|
+
copiesByName: Map<string, number>
|
|
31
|
+
/** allele length in bp, by row name */
|
|
32
|
+
lengthByName: Map<string, number>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const SPANNED_GAP = '-'
|
|
36
|
+
const ABSENT = '.'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What one row has between two reference positions: its matched bases, plus
|
|
40
|
+
* whatever it inserted inside, minus whatever it deleted.
|
|
41
|
+
*
|
|
42
|
+
* This is the measurement the insertion-anchored reading cannot make. An indel
|
|
43
|
+
* inside an array has no unique placement, so two reads carrying the same allele
|
|
44
|
+
* can disagree about which reference position it is inserted at and by how much;
|
|
45
|
+
* summed over the interval, those choices cancel and both come back the same
|
|
46
|
+
* length. Undefined when the row does not reach both edges — copies cannot be
|
|
47
|
+
* counted from a read that stops inside the array, and guessing from a partial
|
|
48
|
+
* one is how a truncated read reads as a contracted allele.
|
|
49
|
+
*/
|
|
50
|
+
export function extractAllele(read: ReadLayout, start: number, end: number) {
|
|
51
|
+
const last = read.start + read.refChars.length
|
|
52
|
+
if (read.start > start || last < end) {
|
|
53
|
+
return undefined
|
|
54
|
+
}
|
|
55
|
+
let ret = ''
|
|
56
|
+
for (let pos = start; pos <= end; pos++) {
|
|
57
|
+
ret += read.insertions.get(pos) ?? ''
|
|
58
|
+
if (pos < end) {
|
|
59
|
+
const char = read.refChars[pos - read.start]!
|
|
60
|
+
if (char === ABSENT) {
|
|
61
|
+
// an N op skips the position rather than deleting it, so the row has no
|
|
62
|
+
// allele here at all rather than a shorter one
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
if (char !== SPANNED_GAP) {
|
|
66
|
+
ret += char
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return ret
|
|
71
|
+
}
|
|
72
|
+
|
|
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
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
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.
|
|
90
|
+
*/
|
|
91
|
+
const ABSORB_COPIES = 1
|
|
92
|
+
|
|
93
|
+
/**
|
|
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.
|
|
99
|
+
*/
|
|
100
|
+
const ABSORB_IDENTITY = 0.8
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Widen each array to cover the insertions the aligner anchored just outside it
|
|
104
|
+
* that are made of the array's own unit.
|
|
105
|
+
*
|
|
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.
|
|
115
|
+
*
|
|
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.
|
|
121
|
+
*/
|
|
122
|
+
export function absorbAdjacentInsertions(
|
|
123
|
+
reads: ReadLayout[],
|
|
124
|
+
arrays: ReferenceArray[],
|
|
125
|
+
) {
|
|
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
|
|
153
|
+
}
|
|
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
|
+
}
|
|
173
|
+
}
|
|
174
|
+
previousEnd = end
|
|
175
|
+
return { ...array, start, end }
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Lay every row's allele out unit-per-block, one block per array.
|
|
181
|
+
*
|
|
182
|
+
* Arrays whose alleles are all one length still get a block: squaring the copies
|
|
183
|
+
* up against each other is what turns "this row is 63 bases long" into "this row
|
|
184
|
+
* has 21 copies and the 12th one differs", and that is true whether or not the
|
|
185
|
+
* rows disagree.
|
|
186
|
+
*/
|
|
187
|
+
export function buildArrayBlocks(
|
|
188
|
+
reads: ReadLayout[],
|
|
189
|
+
arrays: ReferenceArray[],
|
|
190
|
+
) {
|
|
191
|
+
const ret: ArrayBlock[] = []
|
|
192
|
+
for (const array of arrays) {
|
|
193
|
+
const alleleByName = new Map<string, string>()
|
|
194
|
+
for (const read of reads) {
|
|
195
|
+
const allele = extractAllele(read, array.start, array.end)
|
|
196
|
+
if (allele !== undefined) {
|
|
197
|
+
alleleByName.set(read.name, allele)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const aligned = alignToUnit([...alleleByName.values()], array.unit)
|
|
201
|
+
if (!aligned) {
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
const rowByName = new Map<string, string>()
|
|
205
|
+
const copiesByName = new Map<string, number>()
|
|
206
|
+
const lengthByName = new Map<string, number>()
|
|
207
|
+
for (const [name, allele] of alleleByName) {
|
|
208
|
+
const row = aligned.gapped.get(allele)
|
|
209
|
+
const copies = aligned.copies.get(allele)
|
|
210
|
+
if (row !== undefined && copies !== undefined) {
|
|
211
|
+
rowByName.set(name, row)
|
|
212
|
+
copiesByName.set(name, copies)
|
|
213
|
+
lengthByName.set(name, allele.length)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
ret.push({
|
|
217
|
+
start: array.start,
|
|
218
|
+
end: array.end,
|
|
219
|
+
period: aligned.period,
|
|
220
|
+
unit: array.unit,
|
|
221
|
+
width: aligned.unitWidths.reduce((a, b) => a + b, 0),
|
|
222
|
+
unitWidths: aligned.unitWidths,
|
|
223
|
+
rowByName,
|
|
224
|
+
copiesByName,
|
|
225
|
+
lengthByName,
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
return ret
|
|
229
|
+
}
|