@gmod/gfa-tabix 10.0.0 → 10.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.
- package/README.md +4 -1
- package/esm/index.d.ts +3 -1
- package/esm/index.js +1 -0
- package/esm/walkDepartures.d.ts +31 -0
- package/esm/walkDepartures.js +49 -20
- package/esm/walkFile.d.ts +19 -0
- package/esm/walkFile.js +145 -0
- package/esm/walkRows.d.ts +10 -0
- package/esm/walkRows.js +16 -9
- package/esm/walkRuns.d.ts +229 -0
- package/esm/walkRuns.js +989 -0
- package/package.json +4 -2
package/esm/walkRuns.js
ADDED
|
@@ -0,0 +1,989 @@
|
|
|
1
|
+
import { RefPath, abridgedPiece, blockPositions, checkedId, idOf, interiorAt, numbers, parseBlocks, parseChoices, parseCopy, refRows, tokens, traverse, NONE, WalkRowError, } from "./walkChunk.js";
|
|
2
|
+
import { adjacentSteps, departureStretch, squeezer } from "./walkDeparture.js";
|
|
3
|
+
import { chainCrossings } from "./walkDepartures.js";
|
|
4
|
+
import { consecutivePieces, edgeKey } from "./walkRows.js";
|
|
5
|
+
/**
|
|
6
|
+
* What a walk does past one end of a fragment, read from the departure that
|
|
7
|
+
* leaves its last step on the reference, or rejoins its first: `beyond`, it
|
|
8
|
+
* is next on the reference past the drawn stretch on that side, the same way
|
|
9
|
+
* round; `elsewhere`, any other departure, reversed, back on the other side
|
|
10
|
+
* or with its far end inside the drawn stretch; `edge`, no departure, and the
|
|
11
|
+
* end lies on the drawn stretch's edge, so whatever the walk does next lies
|
|
12
|
+
* off it; `nowhere`, no departure and the end lies inside the drawn stretch:
|
|
13
|
+
* the walk has no step on the reference sequence past it.
|
|
14
|
+
*/
|
|
15
|
+
export const END_KINDS = ['beyond', 'elsewhere', 'edge', 'nowhere'];
|
|
16
|
+
/**
|
|
17
|
+
* A file whose header does not state revisited:i:0: a run off the reference
|
|
18
|
+
* may hold a node the reference passes elsewhere, so the runs would not say
|
|
19
|
+
* what the steps do. A host cuts the window instead.
|
|
20
|
+
*/
|
|
21
|
+
export class RunsRefusedError extends Error {
|
|
22
|
+
constructor() {
|
|
23
|
+
super(...arguments);
|
|
24
|
+
this.name = 'RunsRefusedError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export const refuseRevisited = (revisited) => revisited === 0
|
|
28
|
+
? undefined
|
|
29
|
+
: new RunsRefusedError(`The walk file's header states ${revisited === undefined ? 'no revisited:i:' : `revisited:i:${revisited}`}, and runs are read only from a file stating revisited:i:0; cut the window instead`);
|
|
30
|
+
const breaksHeld = new WeakMap();
|
|
31
|
+
// The positions of R that start past a hole, where the one before ends
|
|
32
|
+
function breaksOf(path) {
|
|
33
|
+
const n = path.starts.length;
|
|
34
|
+
let held = breaksHeld.get(path);
|
|
35
|
+
if ((held === null || held === void 0 ? void 0 : held.n) !== n) {
|
|
36
|
+
const breaks = [];
|
|
37
|
+
for (let i = 1; i < n; i++) {
|
|
38
|
+
if (path.starts[i] !== path.end(i - 1)) {
|
|
39
|
+
breaks.push(i);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
held = { n, breaks };
|
|
43
|
+
breaksHeld.set(path, held);
|
|
44
|
+
}
|
|
45
|
+
return held.breaks;
|
|
46
|
+
}
|
|
47
|
+
function firstAbove(sorted, value) {
|
|
48
|
+
let lo = 0;
|
|
49
|
+
let hi = sorted.length;
|
|
50
|
+
while (lo < hi) {
|
|
51
|
+
const mid = (lo + hi) >> 1;
|
|
52
|
+
if (sorted[mid] <= value) {
|
|
53
|
+
lo = mid + 1;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
hi = mid;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return lo;
|
|
60
|
+
}
|
|
61
|
+
// Whether R holds a hole between positions `a` and `b`, one apart
|
|
62
|
+
const holeBetween = (breaks, a, b) => breaks[firstAbove(breaks, Math.max(a, b) - 1)] === Math.max(a, b);
|
|
63
|
+
// A piece's items as its steps come, each stretch of R split at holes and
|
|
64
|
+
// joined to the stretch before where it carries on from it
|
|
65
|
+
class PieceItems {
|
|
66
|
+
constructor(path, piece) {
|
|
67
|
+
this.items = [];
|
|
68
|
+
this.path = path;
|
|
69
|
+
this.piece = piece;
|
|
70
|
+
}
|
|
71
|
+
// R's positions `from` to `to`, in the walk's order
|
|
72
|
+
along(from, to, against) {
|
|
73
|
+
const breaks = breaksOf(this.path);
|
|
74
|
+
const lo = Math.min(from, to);
|
|
75
|
+
const hi = Math.max(from, to);
|
|
76
|
+
const cuts = [];
|
|
77
|
+
for (let k = firstAbove(breaks, lo); k < breaks.length && breaks[k] <= hi; k++) {
|
|
78
|
+
cuts.push(breaks[k]);
|
|
79
|
+
}
|
|
80
|
+
let at = from;
|
|
81
|
+
for (const cut of against ? cuts.toReversed() : cuts) {
|
|
82
|
+
const end = against ? cut : cut - 1;
|
|
83
|
+
this.add(at, end, against);
|
|
84
|
+
at = against ? end - 1 : end + 1;
|
|
85
|
+
}
|
|
86
|
+
this.add(at, to, against);
|
|
87
|
+
}
|
|
88
|
+
add(from, to, against) {
|
|
89
|
+
const last = this.items.at(-1);
|
|
90
|
+
if ((last === null || last === void 0 ? void 0 : last.kind) === 'on' &&
|
|
91
|
+
last.path === this.path &&
|
|
92
|
+
last.against === against &&
|
|
93
|
+
last.b + (against ? -1 : 1) === from &&
|
|
94
|
+
!holeBetween(breaksOf(this.path), last.b, from)) {
|
|
95
|
+
last.b = to;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.items.push({
|
|
99
|
+
kind: 'on',
|
|
100
|
+
piece: this.piece,
|
|
101
|
+
path: this.path,
|
|
102
|
+
a: from,
|
|
103
|
+
b: to,
|
|
104
|
+
against,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
off(bp, loaded) {
|
|
109
|
+
const last = this.items.at(-1);
|
|
110
|
+
if ((last === null || last === void 0 ? void 0 : last.kind) === 'off' && last.loaded === loaded) {
|
|
111
|
+
last.bp.push(bp);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
this.items.push({ kind: 'off', piece: this.piece, bp: [bp], loaded });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A tile's R, its routes and its node lengths, its kept pieces as runs
|
|
120
|
+
* items, the `ref` and `walk` rows decoded and the `abridged` rows listed
|
|
121
|
+
* for blocksPiece, each walk row's span
|
|
122
|
+
*/
|
|
123
|
+
export function runsTile(rows, tileStart, keep) {
|
|
124
|
+
var _a, _b;
|
|
125
|
+
const path = new RefPath();
|
|
126
|
+
const lengths = new Map();
|
|
127
|
+
const routes = [];
|
|
128
|
+
const pieces = [];
|
|
129
|
+
const abridged = [];
|
|
130
|
+
const kept = (name) => keep === undefined || keep(name);
|
|
131
|
+
const references = refRows(rows).map(cols => {
|
|
132
|
+
const handles = tokens(cols[9], { id: 0 }, []);
|
|
133
|
+
const first = path.handles.length;
|
|
134
|
+
path.add(handles, numbers(cols[10]), +cols[6]);
|
|
135
|
+
return { cols, first, n: handles.length };
|
|
136
|
+
});
|
|
137
|
+
for (const { cols, first, n } of references) {
|
|
138
|
+
if (kept(cols[4]) && n > 0) {
|
|
139
|
+
const items = new PieceItems(path, +cols[7]);
|
|
140
|
+
items.along(first, first + n - 1, false);
|
|
141
|
+
pieces.push({
|
|
142
|
+
name: cols[4],
|
|
143
|
+
fragStart: +cols[5],
|
|
144
|
+
piece: +cols[7],
|
|
145
|
+
hapOffset: +cols[6],
|
|
146
|
+
items: items.items,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const previous = { id: 0 };
|
|
151
|
+
for (const cols of rows) {
|
|
152
|
+
if (cols[3] === 'nodes') {
|
|
153
|
+
const ids = numbers(cols[4]);
|
|
154
|
+
const nodeLengths = numbers(cols[5]);
|
|
155
|
+
let id = 0;
|
|
156
|
+
for (let k = 0; k < ids.length; k++) {
|
|
157
|
+
id = checkedId(id + ids[k]);
|
|
158
|
+
lengths.set(id, nodeLengths[k]);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (cols[3] === 'route') {
|
|
162
|
+
const entry = tokens(cols[4], previous, []);
|
|
163
|
+
const interior = tokens(cols[5], previous, []);
|
|
164
|
+
const exit = tokens(cols[6], previous, []);
|
|
165
|
+
routes.push({ entry: (_a = entry[0]) !== null && _a !== void 0 ? _a : NONE, interior, exit: (_b = exit[0]) !== null && _b !== void 0 ? _b : NONE });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
for (const cols of rows) {
|
|
169
|
+
const name = cols[4];
|
|
170
|
+
if (cols[3] === 'abridged' && kept(name)) {
|
|
171
|
+
abridged.push(abridgedPiece(cols, tileStart));
|
|
172
|
+
}
|
|
173
|
+
else if (cols[3] === 'walk' && kept(name)) {
|
|
174
|
+
const piece = +cols[7];
|
|
175
|
+
const items = new PieceItems(path, piece);
|
|
176
|
+
const [start, startCopy] = parseCopy(cols[9]);
|
|
177
|
+
const n = +cols[8];
|
|
178
|
+
let steps = 0;
|
|
179
|
+
traverse(path, routes, start, startCopy, parseChoices(cols[10]), n, {
|
|
180
|
+
step(h, i) {
|
|
181
|
+
items.along(i, i, path.handles[i] !== h);
|
|
182
|
+
steps++;
|
|
183
|
+
},
|
|
184
|
+
stretch(from, to, forward) {
|
|
185
|
+
items.along(forward ? from + 1 : from - 1, to, !forward);
|
|
186
|
+
steps += Math.abs(to - from);
|
|
187
|
+
},
|
|
188
|
+
route(index, flip) {
|
|
189
|
+
var _a;
|
|
190
|
+
const route = routes[index];
|
|
191
|
+
for (let k = 0; k < route.interior.length; k++) {
|
|
192
|
+
items.off((_a = lengths.get(idOf(interiorAt(route, flip, k)))) !== null && _a !== void 0 ? _a : 0, true);
|
|
193
|
+
}
|
|
194
|
+
steps += route.interior.length;
|
|
195
|
+
return true;
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
if (steps !== n) {
|
|
199
|
+
throw new WalkRowError(`a walk row decodes to ${steps} steps, not ${n}`);
|
|
200
|
+
}
|
|
201
|
+
pieces.push({
|
|
202
|
+
name,
|
|
203
|
+
fragStart: +cols[5],
|
|
204
|
+
piece,
|
|
205
|
+
hapOffset: +cols[6],
|
|
206
|
+
items: items.items,
|
|
207
|
+
span: { start: +cols[1], end: +cols[2] },
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return { path, pieces, abridged };
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* An abridged piece as runs items from its `blocks` row: each block a
|
|
215
|
+
* stretch of the tile's R and each run off R one placeholder
|
|
216
|
+
*/
|
|
217
|
+
export function blocksPiece(piece, cols, path) {
|
|
218
|
+
const blocks = parseBlocks(cols);
|
|
219
|
+
if (blocks.before + blocks.bp + blocks.after !== piece.hapBp) {
|
|
220
|
+
throw new WalkRowError(`the blocks row of ${piece.name}, piece ${piece.piece}, adds up to ${blocks.before + blocks.bp + blocks.after} bp, and its abridged row states a hapBp of ${piece.hapBp}`);
|
|
221
|
+
}
|
|
222
|
+
const { from, to } = blockPositions(piece, blocks, path);
|
|
223
|
+
const items = new PieceItems(path, piece.piece);
|
|
224
|
+
if (blocks.before > 0) {
|
|
225
|
+
items.off(blocks.before, false);
|
|
226
|
+
}
|
|
227
|
+
for (let k = 0; k < from.length; k++) {
|
|
228
|
+
if (blocks.off[k] > 0) {
|
|
229
|
+
items.off(blocks.off[k], false);
|
|
230
|
+
}
|
|
231
|
+
const against = blocks.against[k];
|
|
232
|
+
items.along(against ? to[k] - 1 : from[k], against ? from[k] : to[k] - 1, against);
|
|
233
|
+
}
|
|
234
|
+
if (blocks.after > 0) {
|
|
235
|
+
items.off(blocks.after, false);
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
name: piece.name,
|
|
239
|
+
fragStart: piece.fragStart,
|
|
240
|
+
piece: piece.piece,
|
|
241
|
+
hapOffset: piece.hapOffset,
|
|
242
|
+
items: items.items,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
// An abridged piece a read does not build: its bp, as a cut holds it
|
|
246
|
+
export const skippedPiece = (piece) => ({
|
|
247
|
+
name: piece.name,
|
|
248
|
+
fragStart: piece.fragStart,
|
|
249
|
+
piece: piece.piece,
|
|
250
|
+
hapOffset: piece.hapOffset,
|
|
251
|
+
items: [{ kind: 'skip', piece: piece.piece, bp: piece.hapBp }],
|
|
252
|
+
});
|
|
253
|
+
const stepsIn = (item) => item.kind === 'on'
|
|
254
|
+
? Math.abs(item.b - item.a) + 1
|
|
255
|
+
: item.kind === 'off'
|
|
256
|
+
? item.bp.length
|
|
257
|
+
: 1;
|
|
258
|
+
// The position of R step k of a stretch takes
|
|
259
|
+
const positionAt = (item, k) => item.against ? item.a - k : item.a + k;
|
|
260
|
+
const stepBp = (item, k) => item.kind === 'on'
|
|
261
|
+
? item.path.end(positionAt(item, k)) -
|
|
262
|
+
item.path.starts[positionAt(item, k)]
|
|
263
|
+
: item.kind === 'off'
|
|
264
|
+
? item.bp[k]
|
|
265
|
+
: item.bp;
|
|
266
|
+
// bp of an item's first k steps
|
|
267
|
+
const bpBefore = (item, k) => item.kind === 'on'
|
|
268
|
+
? item.against
|
|
269
|
+
? item.path.before[item.a + 1] - item.path.before[item.a + 1 - k]
|
|
270
|
+
: item.path.before[item.a + k] - item.path.before[item.a]
|
|
271
|
+
: item.kind === 'off'
|
|
272
|
+
? item.bp.slice(0, k).reduce((sum, bp) => sum + bp, 0)
|
|
273
|
+
: k > 0
|
|
274
|
+
? item.bp
|
|
275
|
+
: 0;
|
|
276
|
+
const itemBp = (item) => bpBefore(item, stepsIn(item));
|
|
277
|
+
// The steps of a stretch whose pass lies over [start, end), as the first and
|
|
278
|
+
// last of them in the walk's order; none where `first` > `last`
|
|
279
|
+
function stepsOver(item, start, end) {
|
|
280
|
+
const { path } = item;
|
|
281
|
+
const lo = Math.min(item.a, item.b);
|
|
282
|
+
const hi = Math.max(item.a, item.b);
|
|
283
|
+
let from = lo;
|
|
284
|
+
let past = hi + 1;
|
|
285
|
+
while (from < past) {
|
|
286
|
+
const mid = (from + past) >> 1;
|
|
287
|
+
if (path.end(mid) > start) {
|
|
288
|
+
past = mid;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
from = mid + 1;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
let low = from;
|
|
295
|
+
let high = hi + 1;
|
|
296
|
+
while (low < high) {
|
|
297
|
+
const mid = (low + high) >> 1;
|
|
298
|
+
if (path.starts[mid] < end) {
|
|
299
|
+
low = mid + 1;
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
high = mid;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const p1 = from;
|
|
306
|
+
const p2 = low - 1;
|
|
307
|
+
return item.against
|
|
308
|
+
? { first: item.a - p2, last: item.a - p1 }
|
|
309
|
+
: { first: p1 - item.a, last: p2 - item.a };
|
|
310
|
+
}
|
|
311
|
+
// The step after `at` in a run, or before it
|
|
312
|
+
function stepPast(items, at, backward) {
|
|
313
|
+
let { i, k } = at;
|
|
314
|
+
if (backward) {
|
|
315
|
+
k--;
|
|
316
|
+
while (k < 0 && i > 0) {
|
|
317
|
+
i--;
|
|
318
|
+
k = stepsIn(items[i]) - 1;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
k++;
|
|
323
|
+
while (i < items.length && k >= stepsIn(items[i])) {
|
|
324
|
+
i++;
|
|
325
|
+
k = 0;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return { i, k };
|
|
329
|
+
}
|
|
330
|
+
// A run's off-R steps past `from` back to `to`, or on to it, kept up to
|
|
331
|
+
// `context` bp: whole steps, but of a placeholder only the bp left
|
|
332
|
+
function contextBp(items, from, to, context, backward) {
|
|
333
|
+
let left = context;
|
|
334
|
+
let bp = 0;
|
|
335
|
+
let loaded = true;
|
|
336
|
+
const before = (a, b) => a.i < b.i || (a.i === b.i && a.k < b.k);
|
|
337
|
+
let at = stepPast(items, from, backward);
|
|
338
|
+
while (left > 0 &&
|
|
339
|
+
at.i >= 0 &&
|
|
340
|
+
at.i < items.length &&
|
|
341
|
+
at.k >= 0 &&
|
|
342
|
+
(backward ? !before(at, to) : before(at, to))) {
|
|
343
|
+
const item = items[at.i];
|
|
344
|
+
const size = stepBp(item, at.k);
|
|
345
|
+
const placeholder = item.kind === 'off' && !item.loaded;
|
|
346
|
+
bp += placeholder && size > left ? left : size;
|
|
347
|
+
loaded && (loaded = item.kind === 'off' && item.loaded);
|
|
348
|
+
left -= size;
|
|
349
|
+
at = stepPast(items, at, backward);
|
|
350
|
+
}
|
|
351
|
+
return { bp, loaded };
|
|
352
|
+
}
|
|
353
|
+
// The spans of a run a cut keeps, as walkSpans in walkRows.ts finds them
|
|
354
|
+
function runSpans(items, window, contained, context) {
|
|
355
|
+
const over = items.map(item => item.kind === 'on'
|
|
356
|
+
? { item, ...stepsOver(item, window.start, window.end) }
|
|
357
|
+
: undefined);
|
|
358
|
+
const spans = [];
|
|
359
|
+
let from = { i: 0, k: 0 };
|
|
360
|
+
let first;
|
|
361
|
+
let last;
|
|
362
|
+
const close = (at) => {
|
|
363
|
+
if (first && last) {
|
|
364
|
+
const lead = contained
|
|
365
|
+
? contextBp(items, first, from, context, true)
|
|
366
|
+
: { bp: 0, loaded: true };
|
|
367
|
+
const tail = contained
|
|
368
|
+
? contextBp(items, last, at, context, false)
|
|
369
|
+
: { bp: 0, loaded: true };
|
|
370
|
+
spans.push({
|
|
371
|
+
first,
|
|
372
|
+
last,
|
|
373
|
+
firstItem: first.item,
|
|
374
|
+
lastItem: last.item,
|
|
375
|
+
leadBp: lead.bp,
|
|
376
|
+
leadLoaded: lead.loaded,
|
|
377
|
+
tailBp: tail.bp,
|
|
378
|
+
tailLoaded: tail.loaded,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
first = undefined;
|
|
382
|
+
last = undefined;
|
|
383
|
+
};
|
|
384
|
+
items.forEach((item, i) => {
|
|
385
|
+
const on = over[i];
|
|
386
|
+
const n = stepsIn(item);
|
|
387
|
+
if (!contained) {
|
|
388
|
+
if (on && on.first <= on.last) {
|
|
389
|
+
first !== null && first !== void 0 ? first : (first = { i, k: on.first, item: on.item });
|
|
390
|
+
last = { i, k: on.last, item: on.item };
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
else if (item.kind === 'skip') {
|
|
394
|
+
close({ i, k: 0 });
|
|
395
|
+
from = { i: i + 1, k: 0 };
|
|
396
|
+
}
|
|
397
|
+
else if (on) {
|
|
398
|
+
if (on.first > on.last) {
|
|
399
|
+
close({ i, k: 0 });
|
|
400
|
+
from = { i: i + 1, k: 0 };
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
if (on.first > 0) {
|
|
404
|
+
close({ i, k: 0 });
|
|
405
|
+
from = { i, k: on.first };
|
|
406
|
+
}
|
|
407
|
+
first !== null && first !== void 0 ? first : (first = { i, k: on.first, item: on.item });
|
|
408
|
+
last = { i, k: on.last, item: on.item };
|
|
409
|
+
if (on.last < n - 1) {
|
|
410
|
+
close({ i, k: on.last + 1 });
|
|
411
|
+
from = { i: i + 1, k: 0 };
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
close({ i: items.length, k: 0 });
|
|
417
|
+
return spans;
|
|
418
|
+
}
|
|
419
|
+
// Departing departures as the cut sorts its departure table: by walk name,
|
|
420
|
+
// then contig position, then leave, computed ones before records
|
|
421
|
+
function departingTable(departing) {
|
|
422
|
+
const sorted = departing.toSorted((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0) ||
|
|
423
|
+
a.hapStart - b.hapStart ||
|
|
424
|
+
a.leavePos - b.leavePos ||
|
|
425
|
+
Number(a.record) - Number(b.record));
|
|
426
|
+
return {
|
|
427
|
+
names: sorted.map(d => d.name),
|
|
428
|
+
hapStarts: Float64Array.from(sorted, d => d.hapStart),
|
|
429
|
+
ownBp: Float64Array.from(sorted, d => d.ownBp),
|
|
430
|
+
missingBp: Float64Array.from(sorted, d => d.missingBp),
|
|
431
|
+
leavePos: Float64Array.from(sorted, d => d.leavePos),
|
|
432
|
+
leaveReversed: Uint8Array.from(sorted, d => (d.leaveReversed ? 1 : 0)),
|
|
433
|
+
rejoinPos: Float64Array.from(sorted, d => d.rejoinPos),
|
|
434
|
+
rejoinReversed: Uint8Array.from(sorted, d => (d.rejoinReversed ? 1 : 0)),
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
// A record as a fragment's end: its rejoin past a `leave`, else its leave
|
|
438
|
+
const departed = (d, leave) => d
|
|
439
|
+
? {
|
|
440
|
+
pos: leave ? d.rejoinPos : d.leavePos,
|
|
441
|
+
reversed: leave ? d.rejoinReversed : d.leaveReversed,
|
|
442
|
+
ownBp: d.ownBp,
|
|
443
|
+
missingBp: d.missingBp,
|
|
444
|
+
}
|
|
445
|
+
: undefined;
|
|
446
|
+
// The breakpoint of a step on R where the walk leaves it, or rejoins it
|
|
447
|
+
const breakpoint = (lo, hi, against, leave) => against === leave ? lo : hi;
|
|
448
|
+
/**
|
|
449
|
+
* The runs of a window's pieces, as a cut over the same window keeps them:
|
|
450
|
+
* each line's pieces in a row of piece numbers joined, each kept from its
|
|
451
|
+
* first step on `window` to its last, split where a contained cut splits
|
|
452
|
+
* it, and each fragment's ends from the departure at its first and last
|
|
453
|
+
* steps on the reference, computed in their piece or read from `records`
|
|
454
|
+
*/
|
|
455
|
+
export function windowRuns({ refName, pieces, records, window, contained, context, holes, revisited, }) {
|
|
456
|
+
const squeeze = squeezer(holes, refName);
|
|
457
|
+
const leaves = new Map();
|
|
458
|
+
const rejoins = new Map();
|
|
459
|
+
for (const d of records) {
|
|
460
|
+
leaves.set(edgeKey(d.name, d.leavePos, d.leaveReversed, d.hapStart), d);
|
|
461
|
+
rejoins.set(edgeKey(d.name, d.rejoinPos, d.rejoinReversed, d.hapStart + d.ownBp), d);
|
|
462
|
+
}
|
|
463
|
+
const fragments = [];
|
|
464
|
+
const jumping = [];
|
|
465
|
+
for (const run of consecutivePieces(pieces)) {
|
|
466
|
+
const items = run.flatMap(piece => piece.items);
|
|
467
|
+
const { name } = run[0];
|
|
468
|
+
const runStart = run[0].hapOffset;
|
|
469
|
+
const haps = new Float64Array(items.length + 1);
|
|
470
|
+
haps[0] = runStart;
|
|
471
|
+
items.forEach((item, i) => {
|
|
472
|
+
haps[i + 1] = haps[i] + itemBp(item);
|
|
473
|
+
});
|
|
474
|
+
const runEnd = haps[items.length];
|
|
475
|
+
const hapAt = ({ i, k }) => haps[i] + bpBefore(items[i], k);
|
|
476
|
+
const coordinates = (item, k) => {
|
|
477
|
+
const p = positionAt(item, k);
|
|
478
|
+
return { lo: item.path.starts[p], hi: item.path.end(p) };
|
|
479
|
+
};
|
|
480
|
+
const spans = runSpans(items, window, contained, context);
|
|
481
|
+
if (spans.length === 0 && run[0].name !== refName) {
|
|
482
|
+
for (const d of pieceDepartures(items, haps, window, squeeze)) {
|
|
483
|
+
jumping.push({ ...d, name: run[0].name });
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
for (const span of spans) {
|
|
487
|
+
const { first, last, firstItem, lastItem } = span;
|
|
488
|
+
const start = hapAt(first) - span.leadBp;
|
|
489
|
+
const lastEnd = hapAt({ i: last.i, k: last.k + 1 });
|
|
490
|
+
const end = lastEnd + span.tailBp;
|
|
491
|
+
const stretches = [];
|
|
492
|
+
let off = 0;
|
|
493
|
+
let offLoaded = true;
|
|
494
|
+
let offSteps = false;
|
|
495
|
+
for (let i = first.i; i <= last.i; i++) {
|
|
496
|
+
const item = items[i];
|
|
497
|
+
if (item.kind === 'on') {
|
|
498
|
+
const from = i === first.i ? first.k : 0;
|
|
499
|
+
const to = i === last.i ? last.k : stepsIn(item) - 1;
|
|
500
|
+
const a = coordinates(item, from);
|
|
501
|
+
const b = coordinates(item, to);
|
|
502
|
+
const lo = Math.min(a.lo, b.lo);
|
|
503
|
+
const hi = Math.max(a.hi, b.hi);
|
|
504
|
+
const held = stretches.at(-1);
|
|
505
|
+
if (held &&
|
|
506
|
+
!offSteps &&
|
|
507
|
+
held.against === item.against &&
|
|
508
|
+
(item.against ? hi === held.lo : lo === held.hi)) {
|
|
509
|
+
held.lo = Math.min(held.lo, lo);
|
|
510
|
+
held.hi = Math.max(held.hi, hi);
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
stretches.push({
|
|
514
|
+
lo,
|
|
515
|
+
hi,
|
|
516
|
+
against: item.against,
|
|
517
|
+
offBefore: off,
|
|
518
|
+
loaded: offLoaded,
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
off = 0;
|
|
522
|
+
offLoaded = true;
|
|
523
|
+
offSteps = false;
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
off += itemBp(item);
|
|
527
|
+
offLoaded && (offLoaded = item.kind === 'off' && item.loaded);
|
|
528
|
+
offSteps = true;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const head = coordinates(firstItem, first.k);
|
|
532
|
+
const tail = coordinates(lastItem, last.k);
|
|
533
|
+
const firstBreak = breakpoint(head.lo, head.hi, firstItem.against, false);
|
|
534
|
+
const lastBreak = breakpoint(tail.lo, tail.hi, lastItem.against, true);
|
|
535
|
+
const computed = (own, item, k, leave, at, against) => {
|
|
536
|
+
const c = coordinates(item, k);
|
|
537
|
+
const pos = breakpoint(c.lo, c.hi, item.against, !leave);
|
|
538
|
+
return own === 0 &&
|
|
539
|
+
item.against === against &&
|
|
540
|
+
squeeze(pos) === squeeze(at)
|
|
541
|
+
? undefined
|
|
542
|
+
: { pos, reversed: item.against, ownBp: own, missingBp: 0 };
|
|
543
|
+
};
|
|
544
|
+
// the next step on R past item i in its piece, `by` 1 or -1, and the
|
|
545
|
+
// bp off R before it
|
|
546
|
+
const nearestOn = (i, by) => {
|
|
547
|
+
var _a, _b;
|
|
548
|
+
const { piece } = items[i];
|
|
549
|
+
let j = i + by;
|
|
550
|
+
let own = 0;
|
|
551
|
+
while (((_a = items[j]) === null || _a === void 0 ? void 0 : _a.piece) === piece && ((_b = items[j]) === null || _b === void 0 ? void 0 : _b.kind) !== 'on') {
|
|
552
|
+
own += itemBp(items[j]);
|
|
553
|
+
j += by;
|
|
554
|
+
}
|
|
555
|
+
const found = items[j];
|
|
556
|
+
return (found === null || found === void 0 ? void 0 : found.kind) === 'on' && found.piece === piece
|
|
557
|
+
? { item: found, own }
|
|
558
|
+
: undefined;
|
|
559
|
+
};
|
|
560
|
+
const next = nearestOn(last.i, 1);
|
|
561
|
+
const previous = nearestOn(first.i, -1);
|
|
562
|
+
const after = last.k < stepsIn(lastItem) - 1
|
|
563
|
+
? undefined
|
|
564
|
+
: next
|
|
565
|
+
? computed(next.own, next.item, 0, true, lastBreak, lastItem.against)
|
|
566
|
+
: departed(leaves.get(edgeKey(name, lastBreak, lastItem.against, lastEnd)), true);
|
|
567
|
+
const before = first.k > 0
|
|
568
|
+
? undefined
|
|
569
|
+
: previous
|
|
570
|
+
? computed(previous.own, previous.item, stepsIn(previous.item) - 1, false, firstBreak, firstItem.against)
|
|
571
|
+
: departed(rejoins.get(edgeKey(name, firstBreak, firstItem.against, hapAt(first))), false);
|
|
572
|
+
const reference = name === refName ? referenceSteps(items, first, last) : undefined;
|
|
573
|
+
fragments.push({
|
|
574
|
+
name,
|
|
575
|
+
start,
|
|
576
|
+
end,
|
|
577
|
+
trimmedBefore: start - runStart,
|
|
578
|
+
trimmedAfter: runEnd - end,
|
|
579
|
+
leadBp: span.leadBp,
|
|
580
|
+
leadLoaded: span.leadLoaded,
|
|
581
|
+
tailBp: span.tailBp,
|
|
582
|
+
tailLoaded: span.tailLoaded,
|
|
583
|
+
stretches,
|
|
584
|
+
firstBreak,
|
|
585
|
+
lastBreak,
|
|
586
|
+
before,
|
|
587
|
+
after,
|
|
588
|
+
readBefore: hapAt(first) - runStart,
|
|
589
|
+
readAfter: runEnd - lastEnd,
|
|
590
|
+
reference,
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
const held = new Set(fragments.map(f => f.name));
|
|
595
|
+
const recorded = records.map(d => ({ ...d, record: true }));
|
|
596
|
+
return runsTables(fragments, refName, revisited, holes, [...jumping, ...recorded].filter(d => !held.has(d.name)));
|
|
597
|
+
}
|
|
598
|
+
// The departures between two steps on R of one piece of a run with no
|
|
599
|
+
// fragment that lie over `window`, as walkCut computes them
|
|
600
|
+
function pieceDepartures(items, haps, window, squeeze) {
|
|
601
|
+
const out = [];
|
|
602
|
+
let leave;
|
|
603
|
+
let own = 0;
|
|
604
|
+
items.forEach((item, i) => {
|
|
605
|
+
if (item.kind === 'on') {
|
|
606
|
+
if ((leave === null || leave === void 0 ? void 0 : leave.item.piece) === item.piece) {
|
|
607
|
+
const from = leave.item;
|
|
608
|
+
const p = positionAt(from, stepsIn(from) - 1);
|
|
609
|
+
const leavePos = from.against ? from.path.starts[p] : from.path.end(p);
|
|
610
|
+
const q = positionAt(item, 0);
|
|
611
|
+
const rejoinPos = item.against ? item.path.end(q) : item.path.starts[q];
|
|
612
|
+
const ends = {
|
|
613
|
+
ownBp: own,
|
|
614
|
+
leavePos,
|
|
615
|
+
leaveReversed: from.against,
|
|
616
|
+
rejoinPos,
|
|
617
|
+
rejoinReversed: item.against,
|
|
618
|
+
};
|
|
619
|
+
const [lo, hi] = departureStretch(ends);
|
|
620
|
+
if (!adjacentSteps(ends, squeeze) &&
|
|
621
|
+
lo < window.end &&
|
|
622
|
+
hi > window.start) {
|
|
623
|
+
out.push({
|
|
624
|
+
...ends,
|
|
625
|
+
hapStart: haps[leave.i + 1],
|
|
626
|
+
missingBp: 0,
|
|
627
|
+
record: false,
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
leave = { item, i };
|
|
632
|
+
own = 0;
|
|
633
|
+
}
|
|
634
|
+
else {
|
|
635
|
+
own += itemBp(item);
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
return out;
|
|
639
|
+
}
|
|
640
|
+
// The reference's nodes a fragment of its own steps on, in order
|
|
641
|
+
function referenceSteps(items, first, last) {
|
|
642
|
+
const names = [];
|
|
643
|
+
const starts = [];
|
|
644
|
+
const lengths = [];
|
|
645
|
+
for (let i = first.i; i <= last.i; i++) {
|
|
646
|
+
const item = items[i];
|
|
647
|
+
if (item.kind === 'on') {
|
|
648
|
+
const from = i === first.i ? first.k : 0;
|
|
649
|
+
const to = i === last.i ? last.k : stepsIn(item) - 1;
|
|
650
|
+
for (let k = from; k <= to; k++) {
|
|
651
|
+
const p = positionAt(item, k);
|
|
652
|
+
names.push(String(idOf(item.path.handles[p])));
|
|
653
|
+
starts.push(item.path.starts[p]);
|
|
654
|
+
lengths.push(item.path.end(p) - item.path.starts[p]);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return { names, starts, lengths };
|
|
659
|
+
}
|
|
660
|
+
const byWalk = (refName) => (a, b) => Number(b.name === refName) - Number(a.name === refName) ||
|
|
661
|
+
(a.name < b.name ? -1 : a.name > b.name ? 1 : a.start - b.start);
|
|
662
|
+
function endsTable(n) {
|
|
663
|
+
return {
|
|
664
|
+
kinds: new Uint8Array(n),
|
|
665
|
+
pos: new Float64Array(n),
|
|
666
|
+
reversed: new Uint8Array(n),
|
|
667
|
+
ownBp: new Float64Array(n),
|
|
668
|
+
missingBp: new Float64Array(n),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
// A fragment's tables, its ends told apart by the drawn stretch
|
|
672
|
+
function runsTables(unsorted, refName, revisited, holes, departing) {
|
|
673
|
+
const fragments = unsorted.toSorted(byWalk(refName));
|
|
674
|
+
const names = [];
|
|
675
|
+
const starts = [];
|
|
676
|
+
const lengths = [];
|
|
677
|
+
let drawnStart = Infinity;
|
|
678
|
+
let drawnEnd = -Infinity;
|
|
679
|
+
for (const f of fragments) {
|
|
680
|
+
if (f.reference) {
|
|
681
|
+
for (let k = 0; k < f.reference.names.length; k++) {
|
|
682
|
+
names.push(f.reference.names[k]);
|
|
683
|
+
starts.push(f.reference.starts[k]);
|
|
684
|
+
lengths.push(f.reference.lengths[k]);
|
|
685
|
+
}
|
|
686
|
+
for (const s of f.stretches) {
|
|
687
|
+
drawnStart = Math.min(drawnStart, s.lo);
|
|
688
|
+
drawnEnd = Math.max(drawnEnd, s.hi);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
const drawn = {
|
|
693
|
+
refName,
|
|
694
|
+
start: Number.isFinite(drawnStart) ? drawnStart : Number.NaN,
|
|
695
|
+
end: Number.isFinite(drawnEnd) ? drawnEnd : Number.NaN,
|
|
696
|
+
};
|
|
697
|
+
const n = fragments.length;
|
|
698
|
+
const offsets = new Int32Array(n + 1);
|
|
699
|
+
fragments.forEach((f, p) => {
|
|
700
|
+
offsets[p + 1] = offsets[p] + f.stretches.length;
|
|
701
|
+
});
|
|
702
|
+
const total = offsets[n];
|
|
703
|
+
const stretches = {
|
|
704
|
+
starts: new Float64Array(total),
|
|
705
|
+
bp: new Float64Array(total),
|
|
706
|
+
against: new Uint8Array(total),
|
|
707
|
+
offBefore: new Float64Array(total),
|
|
708
|
+
loaded: new Uint8Array(total),
|
|
709
|
+
};
|
|
710
|
+
const before = endsTable(n);
|
|
711
|
+
const after = endsTable(n);
|
|
712
|
+
const setEnd = (ends, p, d, at, against, leading, readBp) => {
|
|
713
|
+
const highSide = against === leading;
|
|
714
|
+
const pastDrawn = (pos) => highSide ? pos >= drawn.end : pos <= drawn.start;
|
|
715
|
+
const kind = d
|
|
716
|
+
? d.reversed === against && pastDrawn(d.pos)
|
|
717
|
+
? 'beyond'
|
|
718
|
+
: 'elsewhere'
|
|
719
|
+
: pastDrawn(at)
|
|
720
|
+
? 'edge'
|
|
721
|
+
: 'nowhere';
|
|
722
|
+
ends.kinds[p] = END_KINDS.indexOf(kind);
|
|
723
|
+
ends.pos[p] = d ? d.pos : Number.NaN;
|
|
724
|
+
ends.reversed[p] = (d === null || d === void 0 ? void 0 : d.reversed) === true ? 1 : 0;
|
|
725
|
+
ends.ownBp[p] = d ? d.ownBp : kind === 'nowhere' ? readBp : Number.NaN;
|
|
726
|
+
ends.missingBp[p] = d ? d.missingBp : 0;
|
|
727
|
+
};
|
|
728
|
+
fragments.forEach((f, p) => {
|
|
729
|
+
f.stretches.forEach((s, k) => {
|
|
730
|
+
const at = offsets[p] + k;
|
|
731
|
+
stretches.starts[at] = s.lo;
|
|
732
|
+
stretches.bp[at] = s.hi - s.lo;
|
|
733
|
+
stretches.against[at] = s.against ? 1 : 0;
|
|
734
|
+
stretches.offBefore[at] = s.offBefore;
|
|
735
|
+
stretches.loaded[at] = s.loaded ? 1 : 0;
|
|
736
|
+
});
|
|
737
|
+
const head = f.stretches[0];
|
|
738
|
+
const tail = f.stretches.at(-1);
|
|
739
|
+
setEnd(before, p, f.before, f.firstBreak, head.against, true, f.readBefore);
|
|
740
|
+
setEnd(after, p, f.after, f.lastBreak, tail.against, false, f.readAfter);
|
|
741
|
+
});
|
|
742
|
+
return {
|
|
743
|
+
refName,
|
|
744
|
+
drawn,
|
|
745
|
+
revisited,
|
|
746
|
+
holes: holes
|
|
747
|
+
.filter(h => h.refName === refName)
|
|
748
|
+
.map(({ start, end }) => ({ start, end }))
|
|
749
|
+
.sort((a, b) => a.start - b.start),
|
|
750
|
+
reference: {
|
|
751
|
+
names,
|
|
752
|
+
starts: Float64Array.from(starts),
|
|
753
|
+
lengths: Int32Array.from(lengths),
|
|
754
|
+
},
|
|
755
|
+
walks: {
|
|
756
|
+
names: fragments.map(f => f.name),
|
|
757
|
+
starts: Float64Array.from(fragments, f => f.start),
|
|
758
|
+
ends: Float64Array.from(fragments, f => f.end),
|
|
759
|
+
trimmedBefore: Float64Array.from(fragments, f => f.trimmedBefore),
|
|
760
|
+
trimmedAfter: Float64Array.from(fragments, f => f.trimmedAfter),
|
|
761
|
+
offsets,
|
|
762
|
+
leadBp: Float64Array.from(fragments, f => f.leadBp),
|
|
763
|
+
leadLoaded: Uint8Array.from(fragments, f => (f.leadLoaded ? 1 : 0)),
|
|
764
|
+
tailBp: Float64Array.from(fragments, f => f.tailBp),
|
|
765
|
+
tailLoaded: Uint8Array.from(fragments, f => (f.tailLoaded ? 1 : 0)),
|
|
766
|
+
before,
|
|
767
|
+
after,
|
|
768
|
+
},
|
|
769
|
+
stretches,
|
|
770
|
+
departing: departingTable(departing),
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* A cut's tables read back as runs: each walk-table walk a fragment, its
|
|
775
|
+
* steps on the reference grouped into stretches and those off it into runs,
|
|
776
|
+
* and its ends from the departure table's departure leaving its last step on
|
|
777
|
+
* the reference, or rejoining its first. `holes` are the walk file's
|
|
778
|
+
* header's, which the tables do not state. It does not refuse: it copies the
|
|
779
|
+
* tables' `revisited`, which a caller checks is 0. It is what
|
|
780
|
+
* WalkFile.getRunsForRange returns for a cut that expands nothing.
|
|
781
|
+
*/
|
|
782
|
+
export function fragmentRuns({ nodes, walks, departures, revisited }, refName, holes) {
|
|
783
|
+
var _a;
|
|
784
|
+
const refStarts = (_a = walks.refStarts) !== null && _a !== void 0 ? _a : new Float64Array(walks.steps.length);
|
|
785
|
+
const leaving = new Map();
|
|
786
|
+
const rejoining = new Map();
|
|
787
|
+
departures.names.forEach((name, k) => {
|
|
788
|
+
leaving.set(edgeKey(name, departures.leavePos[k], departures.leaveReversed[k] === 1, departures.hapStarts[k]), k);
|
|
789
|
+
rejoining.set(edgeKey(name, departures.rejoinPos[k], departures.rejoinReversed[k] === 1, departures.hapStarts[k] + departures.ownBp[k]), k);
|
|
790
|
+
});
|
|
791
|
+
const recorded = (k, leave) => k === undefined
|
|
792
|
+
? undefined
|
|
793
|
+
: {
|
|
794
|
+
pos: leave ? departures.rejoinPos[k] : departures.leavePos[k],
|
|
795
|
+
reversed: (leave
|
|
796
|
+
? departures.rejoinReversed[k]
|
|
797
|
+
: departures.leaveReversed[k]) === 1,
|
|
798
|
+
ownBp: departures.ownBp[k],
|
|
799
|
+
missingBp: departures.missingBp[k],
|
|
800
|
+
};
|
|
801
|
+
const fragments = walks.names.map((name, p) => {
|
|
802
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
803
|
+
const from = walks.offsets[p];
|
|
804
|
+
const to = walks.offsets[p + 1];
|
|
805
|
+
const stretches = [];
|
|
806
|
+
let off = 0;
|
|
807
|
+
let offLoaded = true;
|
|
808
|
+
let offSteps = false;
|
|
809
|
+
let lead = 0;
|
|
810
|
+
let leadLoaded = true;
|
|
811
|
+
let firstOn = -1;
|
|
812
|
+
let lastOn = -1;
|
|
813
|
+
for (let s = from; s < to; s++) {
|
|
814
|
+
const node = walks.steps[s];
|
|
815
|
+
const bp = nodes.lengths[node];
|
|
816
|
+
const placeholder = ((_a = nodes.placeholders) === null || _a === void 0 ? void 0 : _a[node]) === 1;
|
|
817
|
+
const at = refStarts[s];
|
|
818
|
+
if (Number.isNaN(at)) {
|
|
819
|
+
if (firstOn < 0) {
|
|
820
|
+
lead += bp;
|
|
821
|
+
leadLoaded && (leadLoaded = !placeholder);
|
|
822
|
+
}
|
|
823
|
+
else {
|
|
824
|
+
off += bp;
|
|
825
|
+
offLoaded && (offLoaded = !placeholder);
|
|
826
|
+
offSteps = true;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
const against = (walks.reversed[s] ^ ((_c = (_b = nodes.reversed) === null || _b === void 0 ? void 0 : _b[node]) !== null && _c !== void 0 ? _c : 0)) === 1;
|
|
831
|
+
const lo = at;
|
|
832
|
+
const hi = at + bp;
|
|
833
|
+
const held = stretches.at(-1);
|
|
834
|
+
if (held &&
|
|
835
|
+
!offSteps &&
|
|
836
|
+
held.against === against &&
|
|
837
|
+
(against ? hi === held.lo : lo === held.hi)) {
|
|
838
|
+
held.lo = Math.min(held.lo, lo);
|
|
839
|
+
held.hi = Math.max(held.hi, hi);
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
stretches.push({
|
|
843
|
+
lo,
|
|
844
|
+
hi,
|
|
845
|
+
against,
|
|
846
|
+
offBefore: firstOn < 0 ? 0 : off,
|
|
847
|
+
loaded: firstOn < 0 ? true : offLoaded,
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
firstOn = firstOn < 0 ? s : firstOn;
|
|
851
|
+
lastOn = s;
|
|
852
|
+
off = 0;
|
|
853
|
+
offLoaded = true;
|
|
854
|
+
offSteps = false;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
const stepAt = (s) => {
|
|
858
|
+
var _a, _b;
|
|
859
|
+
const node = walks.steps[s];
|
|
860
|
+
const lo = refStarts[s];
|
|
861
|
+
return {
|
|
862
|
+
lo,
|
|
863
|
+
hi: lo + nodes.lengths[node],
|
|
864
|
+
against: (walks.reversed[s] ^ ((_b = (_a = nodes.reversed) === null || _a === void 0 ? void 0 : _a[node]) !== null && _b !== void 0 ? _b : 0)) === 1,
|
|
865
|
+
};
|
|
866
|
+
};
|
|
867
|
+
const head = stepAt(firstOn);
|
|
868
|
+
const tail = stepAt(lastOn);
|
|
869
|
+
const firstBreak = breakpoint(head.lo, head.hi, head.against, false);
|
|
870
|
+
const lastBreak = breakpoint(tail.lo, tail.hi, tail.against, true);
|
|
871
|
+
const start = walks.starts[p];
|
|
872
|
+
const end = walks.ends[p];
|
|
873
|
+
const trimmedBefore = (_e = (_d = walks.trimmedBefore) === null || _d === void 0 ? void 0 : _d[p]) !== null && _e !== void 0 ? _e : 0;
|
|
874
|
+
const trimmedAfter = (_g = (_f = walks.trimmedAfter) === null || _f === void 0 ? void 0 : _f[p]) !== null && _g !== void 0 ? _g : 0;
|
|
875
|
+
const tailBp = off;
|
|
876
|
+
const reference = name === refName
|
|
877
|
+
? {
|
|
878
|
+
names: Array.from(walks.steps.subarray(from, to), node => nodes.names[node]),
|
|
879
|
+
starts: Array.from(refStarts.subarray(from, to)),
|
|
880
|
+
lengths: Array.from(walks.steps.subarray(from, to), node => nodes.lengths[node]),
|
|
881
|
+
}
|
|
882
|
+
: undefined;
|
|
883
|
+
return {
|
|
884
|
+
name,
|
|
885
|
+
start,
|
|
886
|
+
end,
|
|
887
|
+
trimmedBefore,
|
|
888
|
+
trimmedAfter,
|
|
889
|
+
leadBp: lead,
|
|
890
|
+
leadLoaded,
|
|
891
|
+
tailBp,
|
|
892
|
+
tailLoaded: offLoaded,
|
|
893
|
+
stretches,
|
|
894
|
+
firstBreak,
|
|
895
|
+
lastBreak,
|
|
896
|
+
before: recorded(rejoining.get(edgeKey(name, firstBreak, head.against, start + lead)), false),
|
|
897
|
+
after: recorded(leaving.get(edgeKey(name, lastBreak, tail.against, end - tailBp)), true),
|
|
898
|
+
readBefore: trimmedBefore + lead,
|
|
899
|
+
readAfter: trimmedAfter + tailBp,
|
|
900
|
+
reference,
|
|
901
|
+
};
|
|
902
|
+
});
|
|
903
|
+
const held = new Set(walks.names);
|
|
904
|
+
const departing = departures.names.flatMap((name, k) => held.has(name)
|
|
905
|
+
? []
|
|
906
|
+
: [
|
|
907
|
+
{
|
|
908
|
+
name,
|
|
909
|
+
hapStart: departures.hapStarts[k],
|
|
910
|
+
ownBp: departures.ownBp[k],
|
|
911
|
+
missingBp: departures.missingBp[k],
|
|
912
|
+
leavePos: departures.leavePos[k],
|
|
913
|
+
leaveReversed: departures.leaveReversed[k] === 1,
|
|
914
|
+
rejoinPos: departures.rejoinPos[k],
|
|
915
|
+
rejoinReversed: departures.rejoinReversed[k] === 1,
|
|
916
|
+
record: departures.fromSteps[k] === 0,
|
|
917
|
+
},
|
|
918
|
+
]);
|
|
919
|
+
return runsTables(fragments, refName, revisited, holes, departing);
|
|
920
|
+
}
|
|
921
|
+
// Whether an end of END_KINDS index `kind` rejoins the reference
|
|
922
|
+
const endRejoins = (kind) => kind <= END_KINDS.indexOf('elsewhere');
|
|
923
|
+
/**
|
|
924
|
+
* A fragment's stretches and runs as the events walkDepartures reads, two
|
|
925
|
+
* stretches with no own bp between them that follow on, holes removed, as
|
|
926
|
+
* one; with `ends`, a departure at either end that rejoins the reference
|
|
927
|
+
* too, as a cut lists it
|
|
928
|
+
*/
|
|
929
|
+
export function runEvents(runs, p, ends = false) {
|
|
930
|
+
const { stretches, walks } = runs;
|
|
931
|
+
const squeeze = squeezer(runs.holes.map(h => ({ ...h, refName: runs.refName })), runs.refName);
|
|
932
|
+
const events = [];
|
|
933
|
+
if (ends && endRejoins(walks.before.kinds[p])) {
|
|
934
|
+
const pos = walks.before.pos[p];
|
|
935
|
+
events.push({
|
|
936
|
+
kind: 'on',
|
|
937
|
+
lo: pos,
|
|
938
|
+
hi: pos,
|
|
939
|
+
reversed: walks.before.reversed[p] === 1,
|
|
940
|
+
}, { kind: 'own', bp: walks.before.ownBp[p], loaded: false });
|
|
941
|
+
}
|
|
942
|
+
let held;
|
|
943
|
+
for (let k = walks.offsets[p]; k < walks.offsets[p + 1]; k++) {
|
|
944
|
+
const lo = stretches.starts[k];
|
|
945
|
+
const hi = lo + stretches.bp[k];
|
|
946
|
+
const reversed = stretches.against[k] === 1;
|
|
947
|
+
const own = stretches.offBefore[k];
|
|
948
|
+
if (held &&
|
|
949
|
+
own === 0 &&
|
|
950
|
+
held.reversed === reversed &&
|
|
951
|
+
squeeze(reversed ? held.lo : held.hi) === squeeze(reversed ? hi : lo)) {
|
|
952
|
+
held.lo = Math.min(held.lo, lo);
|
|
953
|
+
held.hi = Math.max(held.hi, hi);
|
|
954
|
+
}
|
|
955
|
+
else {
|
|
956
|
+
if (held) {
|
|
957
|
+
events.push({
|
|
958
|
+
kind: 'own',
|
|
959
|
+
bp: own,
|
|
960
|
+
loaded: stretches.loaded[k] === 1,
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
held = { kind: 'on', lo, hi, reversed };
|
|
964
|
+
events.push(held);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
if (ends && endRejoins(walks.after.kinds[p])) {
|
|
968
|
+
const pos = walks.after.pos[p];
|
|
969
|
+
events.push({ kind: 'own', bp: walks.after.ownBp[p], loaded: false }, { kind: 'on', lo: pos, hi: pos, reversed: walks.after.reversed[p] === 1 });
|
|
970
|
+
}
|
|
971
|
+
return events;
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* A fragment's departures in its own order, as walkDepartures reads a chain:
|
|
975
|
+
* where it leaves a stretch along its way and rejoins the next, with the bp
|
|
976
|
+
* of its own runs between and of the stretches it takes the other way,
|
|
977
|
+
* `loaded` false where a run between holds a placeholder. Its way is the
|
|
978
|
+
* one walkDepartures reads it in.
|
|
979
|
+
*/
|
|
980
|
+
export function runDepartures(runs, p) {
|
|
981
|
+
const { flip, crossings } = chainCrossings(runEvents(runs, p));
|
|
982
|
+
return (flip ? crossings.toReversed() : crossings).map(c => ({
|
|
983
|
+
leavePos: flip ? c.to : c.from,
|
|
984
|
+
rejoinPos: flip ? c.from : c.to,
|
|
985
|
+
ownBp: c.ownBp,
|
|
986
|
+
invertedBp: c.invertedBp,
|
|
987
|
+
loaded: c.loaded,
|
|
988
|
+
}));
|
|
989
|
+
}
|