@gmod/gfa-tabix 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +85 -2
- package/esm/abridgedWalks.d.ts +7 -0
- package/esm/abridgedWalks.js +34 -0
- package/esm/formsCheck.d.ts +50 -0
- package/esm/formsCheck.js +251 -0
- package/esm/gfaTabixFile.d.ts +27 -0
- package/esm/gfaTabixFile.js +168 -0
- package/esm/index.d.ts +30 -0
- package/esm/index.js +16 -0
- package/esm/refNames.d.ts +25 -0
- package/esm/refNames.js +65 -0
- package/esm/rgfaBed.d.ts +67 -0
- package/esm/rgfaBed.js +301 -0
- package/esm/structuralForms.d.ts +225 -0
- package/esm/structuralForms.js +813 -0
- package/esm/tabixSource.d.ts +25 -0
- package/esm/tabixSource.js +7 -0
- package/esm/util.d.ts +95 -0
- package/esm/util.js +59 -0
- package/esm/walkChunk.d.ts +246 -0
- package/esm/walkChunk.js +1314 -0
- package/esm/walkContainer.d.ts +54 -0
- package/esm/walkContainer.js +358 -0
- package/esm/walkCounts.d.ts +158 -0
- package/esm/walkCounts.js +580 -0
- package/esm/walkDeparture.d.ts +58 -0
- package/esm/walkDeparture.js +122 -0
- package/esm/walkDepartures.d.ts +80 -0
- package/esm/walkDepartures.js +301 -0
- package/esm/walkFile.d.ts +259 -0
- package/esm/walkFile.js +1534 -0
- package/esm/walkGaps.d.ts +56 -0
- package/esm/walkGaps.js +345 -0
- package/esm/walkRows.d.ts +315 -0
- package/esm/walkRows.js +1607 -0
- package/esm/walkRuns.d.ts +229 -0
- package/esm/walkRuns.js +989 -0
- package/package.json +63 -3
package/esm/walkChunk.js
ADDED
|
@@ -0,0 +1,1314 @@
|
|
|
1
|
+
// A tile of gfa-to-tabix's walk format 8, whose rows from `kind` on are
|
|
2
|
+
// format 6's (that repo's docs/walks-format.md). A tile lists its `ref` rows,
|
|
3
|
+
// which joined in hapOffset order are the tile's reference path R; a `nodes` row with the
|
|
4
|
+
// lengths of the nodes its routes visit; `route` rows, each a way off R; a
|
|
5
|
+
// `walk` row per piece of every other path, naming the routes it takes in
|
|
6
|
+
// order and, where R holds a node more than once, the occurrence a step
|
|
7
|
+
// takes; and an `abridged` row per piece the writer moved to a detail
|
|
8
|
+
// record. A `ref` row's `passes` say how many times the reference passes each
|
|
9
|
+
// of its steps' nodes. A handle is 2 * id + 1 when reversed.
|
|
10
|
+
export const NONE = -1;
|
|
11
|
+
export const idOf = (handle) => (handle - (handle & 1)) / 2;
|
|
12
|
+
// so an id fits an Int32Array, clear of the negative ids placeholders take
|
|
13
|
+
const ID_LIMIT = 2 ** 31;
|
|
14
|
+
export function checkedId(id) {
|
|
15
|
+
if (id < 0 || id >= ID_LIMIT) {
|
|
16
|
+
throw new WalkRowError(`a row holds node id ${id}, where walk format 8 allows ids from 0 to below 2^31`);
|
|
17
|
+
}
|
|
18
|
+
return id;
|
|
19
|
+
}
|
|
20
|
+
// the other strand's handle; arithmetic, which holds past 2^31
|
|
21
|
+
const otherStrand = (handle) => (handle & 1 ? handle - 1 : handle + 1);
|
|
22
|
+
// The handles a column of delta tokens names, each 2 * (id - previous id) +
|
|
23
|
+
// reverse, from `previous.id`, which ends on the last id read
|
|
24
|
+
export function tokens(field, previous, out) {
|
|
25
|
+
if (field === '.') {
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
let v = 0;
|
|
29
|
+
let neg = false;
|
|
30
|
+
for (let p = 0; p <= field.length; p++) {
|
|
31
|
+
const ch = p < field.length ? field.charCodeAt(p) : 44;
|
|
32
|
+
if (ch === 44) {
|
|
33
|
+
const d = neg ? -v : v;
|
|
34
|
+
const r = d & 1;
|
|
35
|
+
previous.id = checkedId(previous.id + (d - r) / 2);
|
|
36
|
+
out.push(previous.id * 2 + r);
|
|
37
|
+
v = 0;
|
|
38
|
+
neg = false;
|
|
39
|
+
}
|
|
40
|
+
else if (ch === 45) {
|
|
41
|
+
neg = true;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
v = v * 10 + (ch - 48);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
export function numbers(field) {
|
|
50
|
+
return field === '.' ? [] : field.split(',').map(Number);
|
|
51
|
+
}
|
|
52
|
+
// A ref row's `passes`: how many times the reference's paths pass each
|
|
53
|
+
// step's node, `.` when every one is 1
|
|
54
|
+
export function passCounts(cols, n) {
|
|
55
|
+
const field = cols[11];
|
|
56
|
+
const counts = field === '.' ? new Array(n).fill(1) : numbers(field !== null && field !== void 0 ? field : '');
|
|
57
|
+
if (counts.length !== n || counts.some(c => !Number.isInteger(c) || c < 1)) {
|
|
58
|
+
throw new WalkRowError(`a ref row's passes do not count its ${n} steps`);
|
|
59
|
+
}
|
|
60
|
+
return counts;
|
|
61
|
+
}
|
|
62
|
+
const forwardEnds = (a, b) => 2 - (a & 1) - (b & 1);
|
|
63
|
+
// A link one way round, so a link and its reverse are one: the way with more
|
|
64
|
+
// forward ends, as a walk along the reference takes it, else the smaller of a
|
|
65
|
+
// and the flipped b first
|
|
66
|
+
export function canonicalLink(a, b) {
|
|
67
|
+
const forward = forwardEnds(a, b);
|
|
68
|
+
return forward > 1 || (forward === 1 && a <= otherStrand(b))
|
|
69
|
+
? [a, b]
|
|
70
|
+
: [otherStrand(b), otherStrand(a)];
|
|
71
|
+
}
|
|
72
|
+
// A handle token and the occurrence in R its `@k` names, NONE for the default
|
|
73
|
+
export function parseCopy(token) {
|
|
74
|
+
const at = token.indexOf('@');
|
|
75
|
+
return at < 0 ? [+token, NONE] : [+token.slice(0, at), +token.slice(at + 1)];
|
|
76
|
+
}
|
|
77
|
+
// The chunk's reference path R: its handles, the bp of its steps before each
|
|
78
|
+
// position, where each position starts on the reference, and each node's
|
|
79
|
+
// occurrences in R
|
|
80
|
+
export class RefPath {
|
|
81
|
+
constructor() {
|
|
82
|
+
this.handles = [];
|
|
83
|
+
this.before = [0];
|
|
84
|
+
this.starts = [];
|
|
85
|
+
this.occurrences = new Map();
|
|
86
|
+
}
|
|
87
|
+
typed() {
|
|
88
|
+
const n = this.handles.length;
|
|
89
|
+
const ids = new Int32Array(n);
|
|
90
|
+
const rev = new Uint8Array(n);
|
|
91
|
+
const bp = new Int32Array(n);
|
|
92
|
+
for (let i = 0; i < n; i++) {
|
|
93
|
+
const h = this.handles[i];
|
|
94
|
+
ids[i] = idOf(h);
|
|
95
|
+
rev[i] = h & 1;
|
|
96
|
+
bp[i] = this.before[i + 1] - this.before[i];
|
|
97
|
+
}
|
|
98
|
+
return { ids, rev, bp, starts: Float64Array.from(this.starts) };
|
|
99
|
+
}
|
|
100
|
+
// R's node ids, strands, step lengths and starts by position
|
|
101
|
+
columns() {
|
|
102
|
+
var _a;
|
|
103
|
+
if (((_a = this.held) === null || _a === void 0 ? void 0 : _a.ids.length) !== this.handles.length) {
|
|
104
|
+
this.held = this.typed();
|
|
105
|
+
}
|
|
106
|
+
return this.held;
|
|
107
|
+
}
|
|
108
|
+
add(handles, lengths, start) {
|
|
109
|
+
for (let k = 0; k < handles.length; k++) {
|
|
110
|
+
const h = handles[k];
|
|
111
|
+
const i = this.handles.length;
|
|
112
|
+
this.handles.push(h);
|
|
113
|
+
this.before.push(this.before[i] + lengths[k]);
|
|
114
|
+
this.starts.push(start);
|
|
115
|
+
start += lengths[k];
|
|
116
|
+
const id = idOf(h);
|
|
117
|
+
const list = this.occurrences.get(id);
|
|
118
|
+
if (list) {
|
|
119
|
+
list.push(i);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
this.occurrences.set(id, [i]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// bp of R's steps before position i
|
|
127
|
+
bp(i) {
|
|
128
|
+
return this.before[i];
|
|
129
|
+
}
|
|
130
|
+
first(handle) {
|
|
131
|
+
var _a;
|
|
132
|
+
return (_a = this.occurrences.get(idOf(handle))) === null || _a === void 0 ? void 0 : _a[0];
|
|
133
|
+
}
|
|
134
|
+
// occurrence k of the handle's node, counting R's positions holding it from 0
|
|
135
|
+
copy(handle, k) {
|
|
136
|
+
var _a;
|
|
137
|
+
return (_a = this.occurrences.get(idOf(handle))) === null || _a === void 0 ? void 0 : _a[k];
|
|
138
|
+
}
|
|
139
|
+
nearest(handle, i) {
|
|
140
|
+
const at = this.occurrences.get(idOf(handle));
|
|
141
|
+
if (!at) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
let lo = 0;
|
|
145
|
+
let hi = at.length;
|
|
146
|
+
while (lo < hi) {
|
|
147
|
+
const mid = (lo + hi) >> 1;
|
|
148
|
+
if (at[mid] <= i) {
|
|
149
|
+
lo = mid + 1;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
hi = mid;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const before = at[lo - 1];
|
|
156
|
+
const after = at[lo];
|
|
157
|
+
if (before === undefined || after === undefined) {
|
|
158
|
+
return before !== null && before !== void 0 ? before : after;
|
|
159
|
+
}
|
|
160
|
+
return after - i <= i - before ? after : before;
|
|
161
|
+
}
|
|
162
|
+
// The position a stretch along R from position i, reading R the way `h`
|
|
163
|
+
// does there, first holds `target`, i itself included
|
|
164
|
+
reach(i, h, target) {
|
|
165
|
+
var _a;
|
|
166
|
+
const at = (_a = this.occurrences.get(idOf(target))) !== null && _a !== void 0 ? _a : [];
|
|
167
|
+
let k = firstAtLeast(at, i);
|
|
168
|
+
if (this.handles[i] === h) {
|
|
169
|
+
while (k < at.length && this.handles[at[k]] !== target) {
|
|
170
|
+
k++;
|
|
171
|
+
}
|
|
172
|
+
return at[k];
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
k = at[k] === i ? k : k - 1;
|
|
176
|
+
while (k >= 0 && otherStrand(this.handles[at[k]]) !== target) {
|
|
177
|
+
k--;
|
|
178
|
+
}
|
|
179
|
+
return at[k];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
handleAt(i, forward) {
|
|
183
|
+
return forward ? this.handles[i] : otherStrand(this.handles[i]);
|
|
184
|
+
}
|
|
185
|
+
// the position that starts at `start` on the reference
|
|
186
|
+
startingAt(start) {
|
|
187
|
+
const i = firstAtLeast(this.starts, start);
|
|
188
|
+
return this.starts[i] === start ? i : undefined;
|
|
189
|
+
}
|
|
190
|
+
endingAt(end) {
|
|
191
|
+
const i = firstAtLeast(this.starts, end) - 1;
|
|
192
|
+
return i >= 0 && this.end(i) === end ? i : undefined;
|
|
193
|
+
}
|
|
194
|
+
// the position `bp` of R's steps past position i
|
|
195
|
+
past(i, bp) {
|
|
196
|
+
const target = this.before[i] + bp;
|
|
197
|
+
const j = firstAtLeast(this.before, target);
|
|
198
|
+
return this.before[j] === target ? j : undefined;
|
|
199
|
+
}
|
|
200
|
+
end(i) {
|
|
201
|
+
return this.starts[i] + this.before[i + 1] - this.before[i];
|
|
202
|
+
}
|
|
203
|
+
// from the first base to the last of the positions over `span`
|
|
204
|
+
hull(span) {
|
|
205
|
+
const at = firstAtLeast(this.starts, span.start);
|
|
206
|
+
const first = at > 0 && this.end(at - 1) > span.start ? at - 1 : at;
|
|
207
|
+
const past = firstAtLeast(this.starts, span.end);
|
|
208
|
+
return past > first
|
|
209
|
+
? { start: this.starts[first], end: this.end(past - 1) }
|
|
210
|
+
: undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function firstAtLeast(sorted, value) {
|
|
214
|
+
let lo = 0;
|
|
215
|
+
let hi = sorted.length;
|
|
216
|
+
while (lo < hi) {
|
|
217
|
+
const mid = (lo + hi) >> 1;
|
|
218
|
+
if (sorted[mid] < value) {
|
|
219
|
+
lo = mid + 1;
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
hi = mid;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return lo;
|
|
226
|
+
}
|
|
227
|
+
export class WalkRowError extends Error {
|
|
228
|
+
}
|
|
229
|
+
const orientedEntry = (route, flip) => flip ? (route.exit === NONE ? NONE : otherStrand(route.exit)) : route.entry;
|
|
230
|
+
const orientedExit = (route, flip) => flip ? (route.entry === NONE ? NONE : otherStrand(route.entry)) : route.exit;
|
|
231
|
+
// A route's interior handle k, taken the way round `flip` says
|
|
232
|
+
export const interiorAt = (route, flip, k) => flip
|
|
233
|
+
? otherStrand(route.interior[route.interior.length - 1 - k])
|
|
234
|
+
: route.interior[k];
|
|
235
|
+
// A walk row's choices, four numbers each: route index, flip, and the
|
|
236
|
+
// occurrences its `:k` and `@k` name, NONE for the defaults
|
|
237
|
+
export function parseChoices(field) {
|
|
238
|
+
const choices = [];
|
|
239
|
+
if (field !== '.') {
|
|
240
|
+
let previous = 0;
|
|
241
|
+
const parts = [0, NONE, NONE];
|
|
242
|
+
let part = 0;
|
|
243
|
+
let v = 0;
|
|
244
|
+
let neg = false;
|
|
245
|
+
let any = false;
|
|
246
|
+
for (let p = 0; p <= field.length; p++) {
|
|
247
|
+
const ch = p < field.length ? field.charCodeAt(p) : 44;
|
|
248
|
+
if (ch === 44 || ch === 58 || ch === 64) {
|
|
249
|
+
if (any) {
|
|
250
|
+
parts[part] = neg ? -v : v;
|
|
251
|
+
}
|
|
252
|
+
v = 0;
|
|
253
|
+
neg = false;
|
|
254
|
+
any = false;
|
|
255
|
+
if (ch === 44) {
|
|
256
|
+
const d = parts[0];
|
|
257
|
+
const r = d & 1;
|
|
258
|
+
previous += (d - r) / 2;
|
|
259
|
+
choices.push(previous, r, parts[1], parts[2]);
|
|
260
|
+
parts[0] = 0;
|
|
261
|
+
parts[1] = NONE;
|
|
262
|
+
parts[2] = NONE;
|
|
263
|
+
part = 0;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
part = ch === 58 ? 1 : 2;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else if (ch === 45) {
|
|
270
|
+
neg = true;
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
v = v * 10 + (ch - 48);
|
|
274
|
+
any = true;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return choices;
|
|
279
|
+
}
|
|
280
|
+
// Follows a walk row of `n` steps as docs/walks-format.md decodes it
|
|
281
|
+
export function traverse(path, routes, start, startCopy, choices, n, visit) {
|
|
282
|
+
const routeOf = (index) => {
|
|
283
|
+
const route = routes[index];
|
|
284
|
+
if (!route) {
|
|
285
|
+
throw new WalkRowError(`route ${index} is not in the chunk`);
|
|
286
|
+
}
|
|
287
|
+
return route;
|
|
288
|
+
};
|
|
289
|
+
let emitted = 0;
|
|
290
|
+
let k = 0;
|
|
291
|
+
if (choices.length > 0 &&
|
|
292
|
+
orientedEntry(routeOf(choices[0]), choices[1] === 1) === NONE) {
|
|
293
|
+
if (!visit.route(choices[0], choices[1] === 1)) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
emitted += routeOf(choices[0]).interior.length;
|
|
297
|
+
k = 4;
|
|
298
|
+
}
|
|
299
|
+
let h = start;
|
|
300
|
+
let i = startCopy === NONE ? path.first(h) : path.copy(h, startCopy);
|
|
301
|
+
if (i === undefined) {
|
|
302
|
+
throw new WalkRowError('a walk row starts off its chunk');
|
|
303
|
+
}
|
|
304
|
+
visit.step(h, i);
|
|
305
|
+
emitted++;
|
|
306
|
+
// along R to the occurrence `copy` names, or to the first position holding
|
|
307
|
+
// `target`
|
|
308
|
+
const follow = (target, copy) => {
|
|
309
|
+
const forward = path.handles[i] === h;
|
|
310
|
+
const j = copy === NONE ? path.reach(i, h, target) : path.copy(target, copy);
|
|
311
|
+
if (j === undefined ||
|
|
312
|
+
(forward ? j < i : j > i) ||
|
|
313
|
+
path.handleAt(j, forward) !== target) {
|
|
314
|
+
throw new WalkRowError('a walk row runs off its chunk');
|
|
315
|
+
}
|
|
316
|
+
if (j !== i) {
|
|
317
|
+
visit.stretch(i, j, forward);
|
|
318
|
+
emitted += Math.abs(j - i);
|
|
319
|
+
}
|
|
320
|
+
i = j;
|
|
321
|
+
h = target;
|
|
322
|
+
};
|
|
323
|
+
for (; k < choices.length; k += 4) {
|
|
324
|
+
const index = choices[k];
|
|
325
|
+
const flip = choices[k + 1] === 1;
|
|
326
|
+
const route = routeOf(index);
|
|
327
|
+
const entry = orientedEntry(route, flip);
|
|
328
|
+
if (entry === NONE) {
|
|
329
|
+
throw new WalkRowError('an open route after the first choice');
|
|
330
|
+
}
|
|
331
|
+
follow(entry, choices[k + 2]);
|
|
332
|
+
if (!visit.route(index, flip)) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
emitted += route.interior.length;
|
|
336
|
+
const exit = orientedExit(route, flip);
|
|
337
|
+
if (exit === NONE) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
h = exit;
|
|
341
|
+
const copy = choices[k + 3];
|
|
342
|
+
i = copy === NONE ? path.nearest(h, i) : path.copy(h, copy);
|
|
343
|
+
if (i === undefined) {
|
|
344
|
+
throw new WalkRowError('a route lands off its chunk');
|
|
345
|
+
}
|
|
346
|
+
visit.step(h, i);
|
|
347
|
+
emitted++;
|
|
348
|
+
}
|
|
349
|
+
const tail = n - emitted;
|
|
350
|
+
if (tail < 0) {
|
|
351
|
+
throw new WalkRowError(`a walk row decodes to more than its ${n} steps`);
|
|
352
|
+
}
|
|
353
|
+
if (tail > 0) {
|
|
354
|
+
const forward = path.handles[i] === h;
|
|
355
|
+
const j = i + (forward ? tail : -tail);
|
|
356
|
+
if (j < 0 || j >= path.handles.length) {
|
|
357
|
+
throw new WalkRowError('a walk row runs off its chunk');
|
|
358
|
+
}
|
|
359
|
+
visit.stretch(i, j, forward);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
// A walk row's steps as node ids, reverse flags, where each lies on the
|
|
363
|
+
// reference, NaN off R, whether each runs against R at its pass, and its bp,
|
|
364
|
+
// -1 where `lengthOf` has none
|
|
365
|
+
function decodeWalk(path, routes, cols, choices, lengthOf) {
|
|
366
|
+
const n = +cols[8];
|
|
367
|
+
const ids = new Int32Array(n);
|
|
368
|
+
const rev = new Uint8Array(n);
|
|
369
|
+
const refStarts = new Float64Array(n).fill(Number.NaN);
|
|
370
|
+
const against = new Uint8Array(n);
|
|
371
|
+
const bp = new Int32Array(n);
|
|
372
|
+
let at = 0;
|
|
373
|
+
const push = (h, i) => {
|
|
374
|
+
if (at >= n) {
|
|
375
|
+
throw new WalkRowError('a walk row decodes to more steps than it states');
|
|
376
|
+
}
|
|
377
|
+
if (i === undefined) {
|
|
378
|
+
bp[at] = lengthOf(idOf(h));
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
refStarts[at] = path.starts[i];
|
|
382
|
+
against[at] = path.handles[i] === h ? 0 : 1;
|
|
383
|
+
bp[at] = path.before[i + 1] - path.before[i];
|
|
384
|
+
}
|
|
385
|
+
ids[at] = idOf(h);
|
|
386
|
+
rev[at++] = h & 1;
|
|
387
|
+
};
|
|
388
|
+
const [start, startCopy] = parseCopy(cols[9]);
|
|
389
|
+
const along = path.columns();
|
|
390
|
+
traverse(path, routes, start, startCopy, choices, n, {
|
|
391
|
+
step: push,
|
|
392
|
+
stretch(from, to, forward) {
|
|
393
|
+
if (at + Math.abs(to - from) > n) {
|
|
394
|
+
throw new WalkRowError('a walk row decodes to more steps than it states');
|
|
395
|
+
}
|
|
396
|
+
if (forward) {
|
|
397
|
+
for (let j = from + 1; j <= to; j++) {
|
|
398
|
+
ids[at] = along.ids[j];
|
|
399
|
+
rev[at] = along.rev[j];
|
|
400
|
+
refStarts[at] = along.starts[j];
|
|
401
|
+
bp[at++] = along.bp[j];
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
for (let j = from - 1; j >= to; j--) {
|
|
406
|
+
ids[at] = along.ids[j];
|
|
407
|
+
rev[at] = along.rev[j] ^ 1;
|
|
408
|
+
refStarts[at] = along.starts[j];
|
|
409
|
+
against[at] = 1;
|
|
410
|
+
bp[at++] = along.bp[j];
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
route(index, flip) {
|
|
415
|
+
const route = routes[index];
|
|
416
|
+
for (let k = 0; k < route.interior.length; k++) {
|
|
417
|
+
push(interiorAt(route, flip, k));
|
|
418
|
+
}
|
|
419
|
+
return true;
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
if (at !== n) {
|
|
423
|
+
throw new WalkRowError(`a walk row decodes to ${at} steps, not ${n}`);
|
|
424
|
+
}
|
|
425
|
+
return { ids, rev, refStarts, against, bp };
|
|
426
|
+
}
|
|
427
|
+
// The bases an SQ:Z: tag at or after column `from` joins, one node's per
|
|
428
|
+
// length, as `ids` names them
|
|
429
|
+
function sliceBases(cols, from, ids, lengths, out) {
|
|
430
|
+
const tag = cols.slice(from).find(c => c.startsWith('SQ:Z:'));
|
|
431
|
+
if (tag === undefined) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
let at = 5;
|
|
435
|
+
for (let k = 0; k < ids.length; k++) {
|
|
436
|
+
const length = lengths[k];
|
|
437
|
+
out.set(ids[k], tag.slice(at, at + length));
|
|
438
|
+
at += length;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const isCount = (field, least = 0) => field !== undefined &&
|
|
442
|
+
field !== '' &&
|
|
443
|
+
Number.isSafeInteger(Number(field)) &&
|
|
444
|
+
Number(field) >= least;
|
|
445
|
+
// Whether abridgedPiece takes an `abridged` line's numbers
|
|
446
|
+
const abridgedCounts = (cols) => [1, 2, 5, 6, 7, 8, 10].every(k => isCount(cols[k])) && isCount(cols[9], 1);
|
|
447
|
+
// An `abridged` line, `path fragStart hapOffset piece nsteps hapBp detail`: a
|
|
448
|
+
// piece the writer moved to a detail record, with its span on R and its bp
|
|
449
|
+
// on its contig
|
|
450
|
+
export function abridgedPiece(cols, tileStart) {
|
|
451
|
+
const count = (k, least = 0) => {
|
|
452
|
+
if (!isCount(cols[k], least)) {
|
|
453
|
+
throw new WalkRowError(`not an abridged row: ${cols.join('\t')}`);
|
|
454
|
+
}
|
|
455
|
+
return Number(cols[k]);
|
|
456
|
+
};
|
|
457
|
+
const abridged = {
|
|
458
|
+
steps: count(8),
|
|
459
|
+
detail: {
|
|
460
|
+
refName: cols[0],
|
|
461
|
+
start: tileStart,
|
|
462
|
+
name: `${tileStart}:${count(10)}`,
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
return {
|
|
466
|
+
chunkStart: tileStart,
|
|
467
|
+
name: cols[4],
|
|
468
|
+
fragStart: count(5),
|
|
469
|
+
hapOffset: count(6),
|
|
470
|
+
piece: count(7),
|
|
471
|
+
hapBp: count(9, 1),
|
|
472
|
+
span: { start: count(1), end: count(2) },
|
|
473
|
+
abridged,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
// A step that stands for steps of a piece the cut did not build, on R or
|
|
477
|
+
// off it: it has their bp and is in no fragment
|
|
478
|
+
export const SKIPPED = -(2 ** 31);
|
|
479
|
+
const DIGIT_0 = 48;
|
|
480
|
+
// A `blocks` row's list, the piece in path order: each block as where it
|
|
481
|
+
// begins on R, its bp and whether it runs against R, the bp off R before
|
|
482
|
+
// each, the bp off R before the first and after the last, and the bp from
|
|
483
|
+
// the first block to the last
|
|
484
|
+
export function parseBlocks(cols) {
|
|
485
|
+
var _a;
|
|
486
|
+
const list = (_a = cols[7]) !== null && _a !== void 0 ? _a : '';
|
|
487
|
+
const begins = [];
|
|
488
|
+
const lengths = [];
|
|
489
|
+
const against = [];
|
|
490
|
+
const off = [];
|
|
491
|
+
let at = 0;
|
|
492
|
+
let bp = 0;
|
|
493
|
+
let before = 0;
|
|
494
|
+
let run = 0;
|
|
495
|
+
let value = 0;
|
|
496
|
+
let digits = 0;
|
|
497
|
+
let negative = false;
|
|
498
|
+
let inBlock = false;
|
|
499
|
+
let begin = 0;
|
|
500
|
+
let reversed = false;
|
|
501
|
+
for (let p = 0; p <= list.length; p++) {
|
|
502
|
+
const ch = p < list.length ? list.charCodeAt(p) : 44;
|
|
503
|
+
const digit = ch - DIGIT_0;
|
|
504
|
+
if (digit >= 0 && digit <= 9) {
|
|
505
|
+
value = value * 10 + digit;
|
|
506
|
+
digits++;
|
|
507
|
+
}
|
|
508
|
+
else if (ch === 45 && !inBlock && digits === 0 && !negative) {
|
|
509
|
+
negative = true;
|
|
510
|
+
}
|
|
511
|
+
else if ((ch === 43 || ch === 45) && !inBlock && digits > 0) {
|
|
512
|
+
begin = at + (negative ? -value : value);
|
|
513
|
+
reversed = ch === 45;
|
|
514
|
+
inBlock = true;
|
|
515
|
+
value = 0;
|
|
516
|
+
digits = 0;
|
|
517
|
+
}
|
|
518
|
+
else if (ch === 44 && inBlock && digits > 0) {
|
|
519
|
+
before = begins.length === 0 ? run : before;
|
|
520
|
+
bp += value + (begins.length === 0 ? 0 : run);
|
|
521
|
+
off.push(begins.length === 0 ? 0 : run);
|
|
522
|
+
begins.push(begin);
|
|
523
|
+
lengths.push(value);
|
|
524
|
+
against.push(reversed);
|
|
525
|
+
at = reversed ? begin - value : begin + value;
|
|
526
|
+
run = 0;
|
|
527
|
+
inBlock = false;
|
|
528
|
+
value = 0;
|
|
529
|
+
digits = 0;
|
|
530
|
+
negative = false;
|
|
531
|
+
}
|
|
532
|
+
else if (ch === 44 && digits > 0 && !negative && run === 0 && value > 0) {
|
|
533
|
+
run = value;
|
|
534
|
+
value = 0;
|
|
535
|
+
digits = 0;
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
throw new WalkRowError(`not a blocks row's list: ${list.slice(0, 80)}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
if (begins.length === 0) {
|
|
542
|
+
throw new WalkRowError(`a blocks row with no block: ${cols.join('\t').slice(0, 120)}`);
|
|
543
|
+
}
|
|
544
|
+
return { before, after: run, begins, lengths, against, off, end: at, bp };
|
|
545
|
+
}
|
|
546
|
+
const blockLow = (blocks, k) => blocks.against[k]
|
|
547
|
+
? blocks.begins[k] - blocks.lengths[k]
|
|
548
|
+
: blocks.begins[k];
|
|
549
|
+
const blockEnd = (blocks, k) => blocks.against[k]
|
|
550
|
+
? blocks.begins[k] - blocks.lengths[k]
|
|
551
|
+
: blocks.begins[k] + blocks.lengths[k];
|
|
552
|
+
// Whether the departure before block k, from the block before it, can lie
|
|
553
|
+
// over `span`
|
|
554
|
+
const jumpOver = (blocks, k, span) => {
|
|
555
|
+
const leave = blockEnd(blocks, k - 1);
|
|
556
|
+
const rejoin = blocks.begins[k];
|
|
557
|
+
return (Math.min(leave, rejoin) - 1 < span.end &&
|
|
558
|
+
Math.max(leave, rejoin) + 1 > span.start);
|
|
559
|
+
};
|
|
560
|
+
// Where a record's own steps lie in an abridged piece of its path, on their
|
|
561
|
+
// contig: nowhere, or in the piece's whole run off R at one end
|
|
562
|
+
export function ownStretch(piece, record) {
|
|
563
|
+
const pieceEnd = piece.hapOffset + piece.hapBp;
|
|
564
|
+
const start = Math.max(piece.hapOffset, record.hapStart);
|
|
565
|
+
const end = Math.min(pieceEnd, record.hapStart + record.ownBp);
|
|
566
|
+
const trailing = start > piece.hapOffset;
|
|
567
|
+
if (start < end && trailing === end < pieceEnd) {
|
|
568
|
+
throw new WalkRowError(`the departure record of ${piece.name} with own steps over [${record.hapStart}, ${record.hapStart + record.ownBp}) of its contig lies ${trailing ? 'inside' : 'over all of'} piece ${piece.piece}, which covers [${piece.hapOffset}, ${pieceEnd}), where a record's own steps are a run at one end of a piece`);
|
|
569
|
+
}
|
|
570
|
+
return start < end ? { start, end, trailing } : undefined;
|
|
571
|
+
}
|
|
572
|
+
const pieceRow = (piece, ids, bp) => ({
|
|
573
|
+
chunkStart: piece.chunkStart,
|
|
574
|
+
name: piece.name,
|
|
575
|
+
fragStart: piece.fragStart,
|
|
576
|
+
hapOffset: piece.hapOffset,
|
|
577
|
+
piece: piece.piece,
|
|
578
|
+
n: ids.length,
|
|
579
|
+
steps: {
|
|
580
|
+
ids: Int32Array.from(ids),
|
|
581
|
+
rev: new Uint8Array(ids.length),
|
|
582
|
+
refStarts: new Float64Array(ids.length).fill(Number.NaN),
|
|
583
|
+
against: new Uint8Array(ids.length),
|
|
584
|
+
bp: Int32Array.from(bp),
|
|
585
|
+
},
|
|
586
|
+
});
|
|
587
|
+
// The walk row of an abridged piece the cut builds no step of: its bp
|
|
588
|
+
export const bpRow = (piece) => pieceRow(piece, [SKIPPED], [piece.hapBp]);
|
|
589
|
+
// The walk row of an abridged piece as its records' own runs, each a
|
|
590
|
+
// placeholder beside the step on R its record names, from its abridged row,
|
|
591
|
+
// the records and its tile's R alone
|
|
592
|
+
export function ownRunsRow(piece, runs, path, placeholder) {
|
|
593
|
+
var _a, _b;
|
|
594
|
+
const ids = [];
|
|
595
|
+
const bp = [];
|
|
596
|
+
const own = [];
|
|
597
|
+
const nodes = new Map();
|
|
598
|
+
const add = (start, end, run) => {
|
|
599
|
+
const id = run ? placeholder(start, end) : SKIPPED;
|
|
600
|
+
if (run) {
|
|
601
|
+
const atNodeEnd = run.trailing !== run.reversed;
|
|
602
|
+
const i = atNodeEnd ? path.endingAt(run.pos) : path.startingAt(run.pos);
|
|
603
|
+
if (i === undefined) {
|
|
604
|
+
throw new WalkRowError(`a departure record of ${piece.name} ${run.trailing ? 'leaves' : 'rejoins'} at ${run.pos}, where the reference path of the tile holding its own steps has no node ${atNodeEnd ? 'ending' : 'starting'}`);
|
|
605
|
+
}
|
|
606
|
+
nodes.set(id, {
|
|
607
|
+
refName: piece.name,
|
|
608
|
+
start,
|
|
609
|
+
end,
|
|
610
|
+
rank: 1,
|
|
611
|
+
passes: 0,
|
|
612
|
+
placeholder: true,
|
|
613
|
+
});
|
|
614
|
+
own.push({
|
|
615
|
+
key: run.key,
|
|
616
|
+
leave: run.trailing,
|
|
617
|
+
from: ids.length,
|
|
618
|
+
to: ids.length + 1,
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
ids.push(id);
|
|
622
|
+
bp.push(end - start);
|
|
623
|
+
};
|
|
624
|
+
const leading = runs.find(run => !run.trailing);
|
|
625
|
+
const trailing = runs.find(run => run.trailing);
|
|
626
|
+
if (leading) {
|
|
627
|
+
add(leading.start, leading.end, leading);
|
|
628
|
+
}
|
|
629
|
+
add((_a = leading === null || leading === void 0 ? void 0 : leading.end) !== null && _a !== void 0 ? _a : piece.hapOffset, (_b = trailing === null || trailing === void 0 ? void 0 : trailing.start) !== null && _b !== void 0 ? _b : piece.hapOffset + piece.hapBp);
|
|
630
|
+
if (trailing) {
|
|
631
|
+
add(trailing.start, trailing.end, trailing);
|
|
632
|
+
}
|
|
633
|
+
const row = {
|
|
634
|
+
...pieceRow(piece, ids, bp),
|
|
635
|
+
abridged: piece.abridged,
|
|
636
|
+
own,
|
|
637
|
+
};
|
|
638
|
+
return { row, nodes };
|
|
639
|
+
}
|
|
640
|
+
// The first and last blocks over `span`
|
|
641
|
+
export function blocksOver(blocks, span) {
|
|
642
|
+
let first = -1;
|
|
643
|
+
let last = -1;
|
|
644
|
+
for (let k = 0; k < blocks.begins.length; k++) {
|
|
645
|
+
const lo = blockLow(blocks, k);
|
|
646
|
+
if (lo < span.end && lo + blocks.lengths[k] > span.start) {
|
|
647
|
+
first = first < 0 ? k : first;
|
|
648
|
+
last = k;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return { first, last };
|
|
652
|
+
}
|
|
653
|
+
// The positions of R each block of a blocks row covers, from[k] up to to[k],
|
|
654
|
+
// whole steps over exactly the block's stretch
|
|
655
|
+
function blockRanges(blocks, path, row) {
|
|
656
|
+
const count = blocks.begins.length;
|
|
657
|
+
const from = new Int32Array(count);
|
|
658
|
+
const to = new Int32Array(count);
|
|
659
|
+
let low = Infinity;
|
|
660
|
+
let high = -Infinity;
|
|
661
|
+
for (let k = 0; k < count; k++) {
|
|
662
|
+
const length = blocks.lengths[k];
|
|
663
|
+
const lo = blockLow(blocks, k);
|
|
664
|
+
const i = path.startingAt(lo);
|
|
665
|
+
const j = i === undefined ? undefined : path.past(i, length);
|
|
666
|
+
if (i === undefined ||
|
|
667
|
+
j === undefined ||
|
|
668
|
+
j === i ||
|
|
669
|
+
path.end(j - 1) !== lo + length) {
|
|
670
|
+
throw new WalkRowError(`block ${k} of ${row} covers no whole steps of its tile's reference path over [${lo}, ${lo + length})`);
|
|
671
|
+
}
|
|
672
|
+
from[k] = i;
|
|
673
|
+
to[k] = j;
|
|
674
|
+
low = Math.min(low, lo);
|
|
675
|
+
high = Math.max(high, lo + length);
|
|
676
|
+
}
|
|
677
|
+
return { from, to, low, high };
|
|
678
|
+
}
|
|
679
|
+
// The positions of R each block of a piece's blocks row covers, the blocks
|
|
680
|
+
// together spanning what the piece's abridged row states
|
|
681
|
+
export function blockPositions(piece, blocks, path) {
|
|
682
|
+
const { name } = piece;
|
|
683
|
+
const { from, to, low, high } = blockRanges(blocks, path, `${name}'s blocks row, piece ${piece.piece},`);
|
|
684
|
+
if (low !== piece.span.start || high !== piece.span.end) {
|
|
685
|
+
throw new WalkRowError(`${name}'s blocks row, piece ${piece.piece}, spans [${low}, ${high}) and its abridged row [${piece.span.start}, ${piece.span.end})`);
|
|
686
|
+
}
|
|
687
|
+
return { from, to };
|
|
688
|
+
}
|
|
689
|
+
// The links a blocks row's piece takes from one block straight to the next,
|
|
690
|
+
// with no run off R between them, as pairs of handles, and its ends
|
|
691
|
+
// The links a stretch of R's positions `from` to `to` takes from one ref row
|
|
692
|
+
// to the next, as pairs of handles
|
|
693
|
+
function acrossRows(rowStarts, path, from, to, links) {
|
|
694
|
+
for (const b of rowStarts) {
|
|
695
|
+
if (Math.min(from, to) < b && b <= Math.max(from, to)) {
|
|
696
|
+
links.push(path.handles[b - 1], path.handles[b]);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
export function blockLinks(cols, path, rowStarts) {
|
|
701
|
+
const blocks = parseBlocks(cols);
|
|
702
|
+
const { from, to } = blockRanges(blocks, path, `${cols[4]}'s blocks row, piece ${cols[6]},`);
|
|
703
|
+
const firstOf = (k) => blocks.against[k] === true
|
|
704
|
+
? path.handleAt(to[k] - 1, false)
|
|
705
|
+
: path.handleAt(from[k], true);
|
|
706
|
+
const lastOf = (k) => blocks.against[k] === true
|
|
707
|
+
? path.handleAt(from[k], false)
|
|
708
|
+
: path.handleAt(to[k] - 1, true);
|
|
709
|
+
const links = [];
|
|
710
|
+
for (let k = 0; k < from.length; k++) {
|
|
711
|
+
acrossRows(rowStarts, path, from[k], to[k] - 1, links);
|
|
712
|
+
if (k > 0 && blocks.off[k] === 0) {
|
|
713
|
+
links.push(lastOf(k - 1), firstOf(k));
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
const ends = {
|
|
717
|
+
name: cols[4],
|
|
718
|
+
fragStart: +cols[5],
|
|
719
|
+
piece: +cols[6],
|
|
720
|
+
first: blocks.before > 0 ? -1 : firstOf(0),
|
|
721
|
+
last: blocks.after > 0 ? -1 : lastOf(from.length - 1),
|
|
722
|
+
};
|
|
723
|
+
return { links, ends };
|
|
724
|
+
}
|
|
725
|
+
// Every link a tile's ref rows and routes take, whichever walks a cut keeps,
|
|
726
|
+
// as pairs of handles, the ends of each of its ref and walk rows, and the
|
|
727
|
+
// positions of R that begin a ref row after its first
|
|
728
|
+
export function tileLinks(rows, path) {
|
|
729
|
+
var _a, _b, _c, _d;
|
|
730
|
+
const links = [];
|
|
731
|
+
const chain = (handles) => {
|
|
732
|
+
for (let k = 1; k < handles.length; k++) {
|
|
733
|
+
links.push(handles[k - 1], handles[k]);
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
// the positions of R that begin a ref row after its first
|
|
737
|
+
const rowStarts = [];
|
|
738
|
+
let at = 0;
|
|
739
|
+
const ends = [];
|
|
740
|
+
for (const cols of refRows(rows)) {
|
|
741
|
+
const handles = tokens(cols[9], { id: 0 }, []);
|
|
742
|
+
chain(handles);
|
|
743
|
+
rowStarts.push(at);
|
|
744
|
+
at += handles.length;
|
|
745
|
+
ends.push({
|
|
746
|
+
name: cols[4],
|
|
747
|
+
fragStart: +cols[5],
|
|
748
|
+
piece: +cols[7],
|
|
749
|
+
first: (_a = handles[0]) !== null && _a !== void 0 ? _a : -1,
|
|
750
|
+
last: (_b = handles.at(-1)) !== null && _b !== void 0 ? _b : -1,
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
rowStarts.shift();
|
|
754
|
+
const routes = [];
|
|
755
|
+
const previous = { id: 0 };
|
|
756
|
+
for (const cols of rows) {
|
|
757
|
+
if (cols[3] === 'route') {
|
|
758
|
+
const entry = tokens(cols[4], previous, []);
|
|
759
|
+
const interior = tokens(cols[5], previous, []);
|
|
760
|
+
const exit = tokens(cols[6], previous, []);
|
|
761
|
+
routes.push({ entry: (_c = entry[0]) !== null && _c !== void 0 ? _c : NONE, interior, exit: (_d = exit[0]) !== null && _d !== void 0 ? _d : NONE });
|
|
762
|
+
chain([...entry, ...interior, ...exit]);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
for (const cols of rows) {
|
|
766
|
+
if (cols[3] === 'walk') {
|
|
767
|
+
const [start, startCopy] = parseCopy(cols[9]);
|
|
768
|
+
let first = -1;
|
|
769
|
+
let last = -1;
|
|
770
|
+
traverse(path, routes, start, startCopy, parseChoices(cols[10]), +cols[8], {
|
|
771
|
+
step: handle => {
|
|
772
|
+
first = first < 0 ? handle : first;
|
|
773
|
+
last = handle;
|
|
774
|
+
},
|
|
775
|
+
stretch: (from, to, forward) => {
|
|
776
|
+
last = path.handleAt(to, forward);
|
|
777
|
+
acrossRows(rowStarts, path, from, to, links);
|
|
778
|
+
},
|
|
779
|
+
route: (index, flip) => {
|
|
780
|
+
const route = routes[index];
|
|
781
|
+
const n = route.interior.length;
|
|
782
|
+
if (n > 0) {
|
|
783
|
+
first = first < 0 ? interiorAt(route, flip, 0) : first;
|
|
784
|
+
last = interiorAt(route, flip, n - 1);
|
|
785
|
+
}
|
|
786
|
+
return true;
|
|
787
|
+
},
|
|
788
|
+
});
|
|
789
|
+
ends.push({
|
|
790
|
+
name: cols[4],
|
|
791
|
+
fragStart: +cols[5],
|
|
792
|
+
piece: +cols[7],
|
|
793
|
+
first,
|
|
794
|
+
last,
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return { links, ends, rowStarts };
|
|
799
|
+
}
|
|
800
|
+
// The walk row of an abridged piece the cut does not load: blocks `core[0]`
|
|
801
|
+
// to `core[1]` whole, and of the rest its end steps and each departure that
|
|
802
|
+
// can lie over `over`, a SKIPPED step for what lies between. No departure
|
|
803
|
+
// across a SKIPPED step lies over `over`, so none a cut computes there is one
|
|
804
|
+
// it returns
|
|
805
|
+
export function unloadedRow(piece, blocks, path, over, core, placeholder) {
|
|
806
|
+
const { name, abridged } = piece;
|
|
807
|
+
const count = blocks.begins.length;
|
|
808
|
+
const { from, to } = blockPositions(piece, blocks, path);
|
|
809
|
+
const inCore = (k) => core !== undefined && k >= core[0] && k <= core[1];
|
|
810
|
+
const joined = (k) => inCore(k - 1) || inCore(k) || jumpOver(blocks, k, over);
|
|
811
|
+
const ids = [];
|
|
812
|
+
const rev = [];
|
|
813
|
+
const refStarts = [];
|
|
814
|
+
const against = [];
|
|
815
|
+
const bp = [];
|
|
816
|
+
const nodes = new Map();
|
|
817
|
+
const { hapOffset } = piece;
|
|
818
|
+
let hap = hapOffset;
|
|
819
|
+
let skipped = 0;
|
|
820
|
+
let omitted = false;
|
|
821
|
+
const push = (id, reversed, refStart, opposed, length) => {
|
|
822
|
+
ids.push(id);
|
|
823
|
+
rev.push(reversed);
|
|
824
|
+
refStarts.push(refStart);
|
|
825
|
+
against.push(opposed);
|
|
826
|
+
bp.push(length);
|
|
827
|
+
hap += length;
|
|
828
|
+
};
|
|
829
|
+
const flush = () => {
|
|
830
|
+
if (skipped > 0 || omitted) {
|
|
831
|
+
const length = skipped;
|
|
832
|
+
skipped = 0;
|
|
833
|
+
omitted = false;
|
|
834
|
+
push(SKIPPED, 0, Number.NaN, 0, length);
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
const offR = (length) => {
|
|
838
|
+
flush();
|
|
839
|
+
const id = placeholder(hap, hap + length);
|
|
840
|
+
nodes.set(id, {
|
|
841
|
+
refName: name,
|
|
842
|
+
start: hap,
|
|
843
|
+
end: hap + length,
|
|
844
|
+
rank: 1,
|
|
845
|
+
passes: 0,
|
|
846
|
+
placeholder: true,
|
|
847
|
+
});
|
|
848
|
+
push(id, 0, Number.NaN, 0, length);
|
|
849
|
+
};
|
|
850
|
+
const step = (i, reversed) => {
|
|
851
|
+
flush();
|
|
852
|
+
const h = path.handleAt(i, !reversed);
|
|
853
|
+
push(idOf(h), h & 1, path.starts[i], reversed ? 1 : 0, path.end(i) - path.starts[i]);
|
|
854
|
+
};
|
|
855
|
+
const firstOf = (k) => (blocks.against[k] ? to[k] - 1 : from[k]);
|
|
856
|
+
const lastOf = (k) => (blocks.against[k] ? from[k] : to[k] - 1);
|
|
857
|
+
if (blocks.before > 0) {
|
|
858
|
+
offR(blocks.before);
|
|
859
|
+
}
|
|
860
|
+
for (let k = 0; k < count; k++) {
|
|
861
|
+
const reversed = blocks.against[k];
|
|
862
|
+
const run = blocks.off[k];
|
|
863
|
+
if (k > 0 && run > 0 && joined(k)) {
|
|
864
|
+
offR(run);
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
skipped += run;
|
|
868
|
+
}
|
|
869
|
+
const size = to[k] - from[k];
|
|
870
|
+
const needFirst = k === 0 || joined(k);
|
|
871
|
+
const needLast = k === count - 1 || joined(k + 1);
|
|
872
|
+
if (inCore(k)) {
|
|
873
|
+
for (let i = firstOf(k); reversed ? i >= from[k] : i < to[k]; i += reversed ? -1 : 1) {
|
|
874
|
+
step(i, reversed);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
else {
|
|
878
|
+
const firstBp = path.end(firstOf(k)) - path.starts[firstOf(k)];
|
|
879
|
+
const lastBp = path.end(lastOf(k)) - path.starts[lastOf(k)];
|
|
880
|
+
if (needFirst || (size === 1 && needLast)) {
|
|
881
|
+
step(firstOf(k), reversed);
|
|
882
|
+
}
|
|
883
|
+
else {
|
|
884
|
+
skipped += firstBp;
|
|
885
|
+
omitted = true;
|
|
886
|
+
}
|
|
887
|
+
if (size > 1) {
|
|
888
|
+
skipped += blocks.lengths[k] - firstBp - lastBp;
|
|
889
|
+
omitted || (omitted = size > 2);
|
|
890
|
+
if (needLast) {
|
|
891
|
+
step(lastOf(k), reversed);
|
|
892
|
+
}
|
|
893
|
+
else {
|
|
894
|
+
skipped += lastBp;
|
|
895
|
+
omitted = true;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
flush();
|
|
901
|
+
if (blocks.after > 0) {
|
|
902
|
+
offR(blocks.after);
|
|
903
|
+
}
|
|
904
|
+
const n = ids.length;
|
|
905
|
+
const row = {
|
|
906
|
+
chunkStart: piece.chunkStart,
|
|
907
|
+
name,
|
|
908
|
+
fragStart: piece.fragStart,
|
|
909
|
+
hapOffset,
|
|
910
|
+
piece: piece.piece,
|
|
911
|
+
n,
|
|
912
|
+
steps: {
|
|
913
|
+
ids: Int32Array.from(ids),
|
|
914
|
+
rev: Uint8Array.from(rev),
|
|
915
|
+
refStarts: Float64Array.from(refStarts),
|
|
916
|
+
against: Uint8Array.from(against),
|
|
917
|
+
bp: Int32Array.from(bp),
|
|
918
|
+
},
|
|
919
|
+
abridged,
|
|
920
|
+
};
|
|
921
|
+
return { row, nodes };
|
|
922
|
+
}
|
|
923
|
+
export const refRows = (rows) => rows.filter(c => c[3] === 'ref').sort((a, b) => +a[6] - +b[6]);
|
|
924
|
+
const tokenCount = (field) => field === '.' ? 0 : field.split(',').length;
|
|
925
|
+
// A walk row's steps off R, and of them those before its first step on R
|
|
926
|
+
// and after its last; undefined where its decode would refuse the row, for
|
|
927
|
+
// a route the tile lacks or a route without an entry or exit inside it
|
|
928
|
+
function stepsOffR(choices, routes) {
|
|
929
|
+
let interior = 0;
|
|
930
|
+
let ends = 0;
|
|
931
|
+
let sound = true;
|
|
932
|
+
for (let k = 0; k < choices.length; k += 4) {
|
|
933
|
+
const route = routes[choices[k]];
|
|
934
|
+
if (route) {
|
|
935
|
+
const flip = choices[k + 1] === 1;
|
|
936
|
+
const noEntry = flip ? route.noExit : route.noEntry;
|
|
937
|
+
const noExit = flip ? route.noEntry : route.noExit;
|
|
938
|
+
const leading = k === 0 && noEntry;
|
|
939
|
+
const trailing = !leading && noExit;
|
|
940
|
+
sound && (sound = (leading || !noEntry) && (!trailing || k + 4 === choices.length));
|
|
941
|
+
interior += route.interior;
|
|
942
|
+
ends += leading || trailing ? route.interior : 0;
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
sound = false;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return sound ? { interior, ends } : undefined;
|
|
949
|
+
}
|
|
950
|
+
const COUNTED_KINDS = new Set(['ref', 'walk', 'abridged']);
|
|
951
|
+
/**
|
|
952
|
+
* What a cut's decode of a tile reads, and what it surely keeps of the tile
|
|
953
|
+
* where its extent lies strictly inside the stretch the cut draws, from the
|
|
954
|
+
* tile's lines before any is decoded; undefined where the decode would
|
|
955
|
+
* refuse a row of a walk `keep` takes.
|
|
956
|
+
*
|
|
957
|
+
* `kept` is the steps of each `ref` and `walk` row of a walk `keep` takes,
|
|
958
|
+
* less the steps off R before a walk row's first step on R and after its
|
|
959
|
+
* last. Every step a row places on R lies within its tile's extent, so a cut
|
|
960
|
+
* over the extent keeps each row from its first step on R to its last, and
|
|
961
|
+
* every step between, as one stretch of a fragment, whether it is contained
|
|
962
|
+
* or not. The steps off R at a row's ends are kept only if a neighbouring
|
|
963
|
+
* piece reaches the window, so they are not counted. An `abridged` row
|
|
964
|
+
* counts nothing here, since a cut builds the piece from its blocks row with
|
|
965
|
+
* a placeholder for each run off R, in fewer steps than its `nsteps`
|
|
966
|
+
* (blocksSteps counts those from the blocks row), and a departure's own
|
|
967
|
+
* steps are not counted either. So `kept` never exceeds the steps of the
|
|
968
|
+
* cut's fragments, which its step budget counts with the departures' own
|
|
969
|
+
* steps.
|
|
970
|
+
*
|
|
971
|
+
* `n` and `interior` bound the steps the decode's rows give the read budget,
|
|
972
|
+
* and those of them off R: each `ref` and `walk` row's own, and an abridged
|
|
973
|
+
* piece's `nsteps`, all off R where `expand` reads it from its detail
|
|
974
|
+
* record, and else as many on R, no fewer than the row built from its blocks
|
|
975
|
+
* row or its bp holds. `blocks` says the cut reads a blocks row for one of
|
|
976
|
+
* the tile's pieces, and `expanded` a detail record.
|
|
977
|
+
*/
|
|
978
|
+
export function tileSteps(lines, keep, expand) {
|
|
979
|
+
var _a, _b, _c, _d, _e;
|
|
980
|
+
const rows = lines.map(line => line.split('\t'));
|
|
981
|
+
const routes = rows
|
|
982
|
+
.filter(cols => cols[3] === 'route')
|
|
983
|
+
.map(cols => {
|
|
984
|
+
var _a;
|
|
985
|
+
return ({
|
|
986
|
+
noEntry: cols[4] === '.',
|
|
987
|
+
interior: tokenCount((_a = cols[5]) !== null && _a !== void 0 ? _a : '.'),
|
|
988
|
+
noExit: cols[6] === '.',
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
const tally = { kept: 0, n: 0, interior: 0, blocks: false, expanded: false };
|
|
992
|
+
let sound = true;
|
|
993
|
+
for (const cols of rows) {
|
|
994
|
+
const kind = cols[3];
|
|
995
|
+
const name = cols[4];
|
|
996
|
+
if (COUNTED_KINDS.has(kind) && ((_a = keep === null || keep === void 0 ? void 0 : keep(name)) !== null && _a !== void 0 ? _a : true)) {
|
|
997
|
+
if (kind === 'ref') {
|
|
998
|
+
const n = tokenCount((_b = cols[9]) !== null && _b !== void 0 ? _b : '.');
|
|
999
|
+
tally.kept += n;
|
|
1000
|
+
tally.n += n;
|
|
1001
|
+
}
|
|
1002
|
+
else if (kind === 'walk') {
|
|
1003
|
+
const n = Number(cols[8]);
|
|
1004
|
+
const off = stepsOffR(parseChoices((_c = cols[10]) !== null && _c !== void 0 ? _c : '.'), routes);
|
|
1005
|
+
sound && (sound = off !== undefined && n > off.interior);
|
|
1006
|
+
tally.kept += n - ((_d = off === null || off === void 0 ? void 0 : off.ends) !== null && _d !== void 0 ? _d : 0);
|
|
1007
|
+
tally.n += n;
|
|
1008
|
+
tally.interior += (_e = off === null || off === void 0 ? void 0 : off.interior) !== null && _e !== void 0 ? _e : 0;
|
|
1009
|
+
}
|
|
1010
|
+
else {
|
|
1011
|
+
const n = Number(cols[8]);
|
|
1012
|
+
const expanded = (expand === null || expand === void 0 ? void 0 : expand(name)) === true;
|
|
1013
|
+
sound && (sound = abridgedCounts(cols));
|
|
1014
|
+
tally.n += expanded ? n : Math.max(n, 1);
|
|
1015
|
+
tally.interior += expanded ? n : 0;
|
|
1016
|
+
tally.expanded || (tally.expanded = expanded);
|
|
1017
|
+
tally.blocks || (tally.blocks = !expanded);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
return sound ? tally : undefined;
|
|
1022
|
+
}
|
|
1023
|
+
const takes = (cols, keep, expand) => {
|
|
1024
|
+
var _a;
|
|
1025
|
+
return cols[3] === 'abridged' &&
|
|
1026
|
+
((_a = keep === null || keep === void 0 ? void 0 : keep(cols[4])) !== null && _a !== void 0 ? _a : true) &&
|
|
1027
|
+
(expand === null || expand === void 0 ? void 0 : expand(cols[4])) !== true;
|
|
1028
|
+
};
|
|
1029
|
+
// The steps a tile's `abridged` rows state for the pieces a cut builds from
|
|
1030
|
+
// their blocks rows, which is no fewer than it builds of them
|
|
1031
|
+
export function statedAbridgedSteps(lines, keep, expand) {
|
|
1032
|
+
let steps = 0;
|
|
1033
|
+
for (const line of lines) {
|
|
1034
|
+
const cols = line.split('\t', 9);
|
|
1035
|
+
const stated = takes(cols, keep, expand) ? Number(cols[8]) : 0;
|
|
1036
|
+
steps += Number.isFinite(stated) ? stated : 0;
|
|
1037
|
+
}
|
|
1038
|
+
return steps;
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* The steps a cut over `drawn` surely keeps of the pieces a tile abridges
|
|
1042
|
+
* and the cut builds from their blocks rows, from the tile's lines and its
|
|
1043
|
+
* blocks unit's before any is decoded; undefined where a piece has no
|
|
1044
|
+
* blocks row, or one the cut refuses.
|
|
1045
|
+
*
|
|
1046
|
+
* A cut builds whole each block of such a piece that lies over the stretch
|
|
1047
|
+
* it draws, a step for each step of R under the block, and a placeholder
|
|
1048
|
+
* for the run off R between two blocks it builds whole. It keeps every step
|
|
1049
|
+
* a row places on that stretch, and every step between two of them with no
|
|
1050
|
+
* step onto R off the stretch between, contained or not. So the count is
|
|
1051
|
+
* the steps of R under each block wholly inside `drawn`, and one for each
|
|
1052
|
+
* run off R between two such blocks, one after the other. A run off R at a
|
|
1053
|
+
* piece's end is kept only where a neighbouring piece reaches the window,
|
|
1054
|
+
* so it is not counted.
|
|
1055
|
+
*/
|
|
1056
|
+
export function blocksSteps(lines, blocksLines, drawn, keep, expand) {
|
|
1057
|
+
const kinds = new Set(['ref', 'abridged']);
|
|
1058
|
+
const rows = lines
|
|
1059
|
+
.filter(line => { var _a; return kinds.has((_a = line.split('\t', 4)[3]) !== null && _a !== void 0 ? _a : ''); })
|
|
1060
|
+
.map(line => line.split('\t'));
|
|
1061
|
+
const listed = new Map(blocksLines.map(line => {
|
|
1062
|
+
const cols = line.split('\t');
|
|
1063
|
+
return [`${cols[4]}\t${cols[5]}\t${cols[6]}`, cols];
|
|
1064
|
+
}));
|
|
1065
|
+
const inside = (blocks, k) => blockLow(blocks, k) >= drawn.start &&
|
|
1066
|
+
blockLow(blocks, k) + blocks.lengths[k] <= drawn.end;
|
|
1067
|
+
let steps = 0;
|
|
1068
|
+
let sound = true;
|
|
1069
|
+
try {
|
|
1070
|
+
const { path } = chunkRef(rows);
|
|
1071
|
+
for (const cols of rows.filter(row => takes(row, keep, expand))) {
|
|
1072
|
+
const piece = abridgedPiece(cols, 0);
|
|
1073
|
+
const row = listed.get(`${piece.name}\t${piece.fragStart}\t${piece.piece}`);
|
|
1074
|
+
const blocks = row ? parseBlocks(row) : undefined;
|
|
1075
|
+
if (blocks && blocks.before + blocks.bp + blocks.after === piece.hapBp) {
|
|
1076
|
+
const { from, to } = blockPositions(piece, blocks, path);
|
|
1077
|
+
for (let k = 0; k < from.length; k++) {
|
|
1078
|
+
if (inside(blocks, k)) {
|
|
1079
|
+
steps += to[k] - from[k];
|
|
1080
|
+
steps +=
|
|
1081
|
+
k > 0 && blocks.off[k] > 0 && inside(blocks, k - 1) ? 1 : 0;
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
else {
|
|
1086
|
+
sound = false;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
catch (error) {
|
|
1091
|
+
if (!(error instanceof WalkRowError)) {
|
|
1092
|
+
throw error;
|
|
1093
|
+
}
|
|
1094
|
+
sound = false;
|
|
1095
|
+
}
|
|
1096
|
+
return sound ? steps : undefined;
|
|
1097
|
+
}
|
|
1098
|
+
// The ids of a column of delta tokens, from `previous.id`, which ends on the
|
|
1099
|
+
// last id read
|
|
1100
|
+
const deltaIds = (field, previous) => field === '.'
|
|
1101
|
+
? []
|
|
1102
|
+
: field.split(',').map(token => {
|
|
1103
|
+
const delta = Number.parseInt(token, 10);
|
|
1104
|
+
previous.id += (delta - (delta & 1)) / 2;
|
|
1105
|
+
return previous.id;
|
|
1106
|
+
});
|
|
1107
|
+
// Every node id a tile's ref rows and routes name, which holds every node
|
|
1108
|
+
// its decode and the pieces built from its blocks rows place
|
|
1109
|
+
export function tileNodeIds(lines, into) {
|
|
1110
|
+
const previous = { id: 0 };
|
|
1111
|
+
for (const line of lines) {
|
|
1112
|
+
const cols = line.split('\t');
|
|
1113
|
+
const columns = cols[3] === 'ref'
|
|
1114
|
+
? [cols[9]]
|
|
1115
|
+
: cols[3] === 'route'
|
|
1116
|
+
? cols.slice(4, 7)
|
|
1117
|
+
: [];
|
|
1118
|
+
const from = cols[3] === 'ref' ? { id: 0 } : previous;
|
|
1119
|
+
for (const field of columns) {
|
|
1120
|
+
for (const id of deltaIds(field !== null && field !== void 0 ? field : '.', from)) {
|
|
1121
|
+
into.add(id);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
return into;
|
|
1126
|
+
}
|
|
1127
|
+
export function chunkRef(rows) {
|
|
1128
|
+
const path = new RefPath();
|
|
1129
|
+
const lengths = new Map();
|
|
1130
|
+
for (const cols of refRows(rows)) {
|
|
1131
|
+
const handles = tokens(cols[9], { id: 0 }, []);
|
|
1132
|
+
const nodeLengths = numbers(cols[10]);
|
|
1133
|
+
path.add(handles, nodeLengths, +cols[6]);
|
|
1134
|
+
for (let k = 0; k < handles.length; k++) {
|
|
1135
|
+
lengths.set(idOf(handles[k]), nodeLengths[k]);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return { path, lengths };
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Decodes one tile's rows, each split on tabs, the tile starting at
|
|
1142
|
+
* `tileStart` nominally. `keep` names the walk rows whose steps a cut wants,
|
|
1143
|
+
* undefined for every one. Given `ref`, the rows are a detail record decoded
|
|
1144
|
+
* against that tile's R, and the result leaves out R's rows, nodes, ends and
|
|
1145
|
+
* bases, which the tile's own decode gave.
|
|
1146
|
+
*/
|
|
1147
|
+
export function decodeChunk(rows, tileStart, keep, withSequences = false, ref) {
|
|
1148
|
+
var _a, _b, _c, _d;
|
|
1149
|
+
const path = (_a = ref === null || ref === void 0 ? void 0 : ref.path) !== null && _a !== void 0 ? _a : new RefPath();
|
|
1150
|
+
const lengths = new Map();
|
|
1151
|
+
const routes = [];
|
|
1152
|
+
const out = {
|
|
1153
|
+
sequences: new Map(),
|
|
1154
|
+
rows: [],
|
|
1155
|
+
abridged: [],
|
|
1156
|
+
nodes: new Map(),
|
|
1157
|
+
revisits: [],
|
|
1158
|
+
path,
|
|
1159
|
+
};
|
|
1160
|
+
const placed = (id) => out.nodes.has(id) || (ref === null || ref === void 0 ? void 0 : ref.lengths.has(id)) === true;
|
|
1161
|
+
const previous = { id: 0 };
|
|
1162
|
+
for (const cols of ref ? [] : refRows(rows)) {
|
|
1163
|
+
const handles = tokens(cols[9], { id: 0 }, []);
|
|
1164
|
+
const nodeLengths = numbers(cols[10]);
|
|
1165
|
+
const passes = passCounts(cols, handles.length);
|
|
1166
|
+
const hapOffset = +cols[6];
|
|
1167
|
+
const first = path.starts.length;
|
|
1168
|
+
path.add(handles, nodeLengths, hapOffset);
|
|
1169
|
+
if (withSequences) {
|
|
1170
|
+
sliceBases(cols, 11, handles.map(idOf), nodeLengths, out.sequences);
|
|
1171
|
+
}
|
|
1172
|
+
let start = hapOffset;
|
|
1173
|
+
for (let k = 0; k < handles.length; k++) {
|
|
1174
|
+
const id = idOf(handles[k]);
|
|
1175
|
+
const length = nodeLengths[k];
|
|
1176
|
+
lengths.set(id, length);
|
|
1177
|
+
const node = {
|
|
1178
|
+
refName: cols[4],
|
|
1179
|
+
start,
|
|
1180
|
+
end: start + length,
|
|
1181
|
+
rank: 0,
|
|
1182
|
+
passes: passes[k],
|
|
1183
|
+
reversed: (handles[k] & 1) === 1,
|
|
1184
|
+
};
|
|
1185
|
+
if (out.nodes.has(id)) {
|
|
1186
|
+
out.revisits.push({ id, ...node });
|
|
1187
|
+
}
|
|
1188
|
+
else {
|
|
1189
|
+
out.nodes.set(id, node);
|
|
1190
|
+
}
|
|
1191
|
+
start += length;
|
|
1192
|
+
}
|
|
1193
|
+
if (keep === undefined || keep(cols[4])) {
|
|
1194
|
+
out.rows.push({
|
|
1195
|
+
chunkStart: tileStart,
|
|
1196
|
+
name: cols[4],
|
|
1197
|
+
fragStart: +cols[5],
|
|
1198
|
+
hapOffset,
|
|
1199
|
+
piece: +cols[7],
|
|
1200
|
+
n: handles.length,
|
|
1201
|
+
steps: {
|
|
1202
|
+
ids: Int32Array.from(handles, idOf),
|
|
1203
|
+
rev: Uint8Array.from(handles, h => h & 1),
|
|
1204
|
+
refStarts: Float64Array.from(path.starts.slice(first)),
|
|
1205
|
+
against: new Uint8Array(handles.length),
|
|
1206
|
+
bp: Int32Array.from(nodeLengths),
|
|
1207
|
+
},
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
for (const cols of rows) {
|
|
1212
|
+
if (cols[3] === 'nodes') {
|
|
1213
|
+
const ids = numbers(cols[4]);
|
|
1214
|
+
const nodeLengths = numbers(cols[5]);
|
|
1215
|
+
let id = 0;
|
|
1216
|
+
for (let k = 0; k < ids.length; k++) {
|
|
1217
|
+
id = checkedId(id + ids[k]);
|
|
1218
|
+
ids[k] = id;
|
|
1219
|
+
lengths.set(id, nodeLengths[k]);
|
|
1220
|
+
}
|
|
1221
|
+
if (withSequences) {
|
|
1222
|
+
sliceBases(cols, 6, ids, nodeLengths, out.sequences);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
else if (cols[3] === 'route') {
|
|
1226
|
+
const entry = tokens(cols[4], previous, []);
|
|
1227
|
+
const interior = tokens(cols[5], previous, []);
|
|
1228
|
+
const exit = tokens(cols[6], previous, []);
|
|
1229
|
+
routes.push({ entry: (_b = entry[0]) !== null && _b !== void 0 ? _b : NONE, interior, exit: (_c = exit[0]) !== null && _c !== void 0 ? _c : NONE });
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
const routeOf = (index) => {
|
|
1233
|
+
const route = routes[index];
|
|
1234
|
+
if (!route) {
|
|
1235
|
+
throw new WalkRowError(`route ${index} is not in the chunk`);
|
|
1236
|
+
}
|
|
1237
|
+
return route;
|
|
1238
|
+
};
|
|
1239
|
+
for (const cols of rows) {
|
|
1240
|
+
if (cols[3] === 'abridged') {
|
|
1241
|
+
const name = cols[4];
|
|
1242
|
+
if (keep === undefined || keep(name)) {
|
|
1243
|
+
out.abridged.push(abridgedPiece(cols, tileStart));
|
|
1244
|
+
}
|
|
1245
|
+
continue;
|
|
1246
|
+
}
|
|
1247
|
+
if (cols[3] !== 'walk') {
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
const name = cols[4];
|
|
1251
|
+
if (keep === undefined || keep(name)) {
|
|
1252
|
+
const choices = parseChoices(cols[10]);
|
|
1253
|
+
let interior = 0;
|
|
1254
|
+
// the walk's nodes off R, by length; the cut's tables place them
|
|
1255
|
+
for (let k = 0; k < choices.length; k += 4) {
|
|
1256
|
+
interior += routeOf(choices[k]).interior.length;
|
|
1257
|
+
for (const h of routeOf(choices[k]).interior) {
|
|
1258
|
+
const id = idOf(h);
|
|
1259
|
+
if (!placed(id)) {
|
|
1260
|
+
const length = (_d = lengths.get(id)) !== null && _d !== void 0 ? _d : 0;
|
|
1261
|
+
out.nodes.set(id, {
|
|
1262
|
+
refName: name,
|
|
1263
|
+
start: 0,
|
|
1264
|
+
end: length,
|
|
1265
|
+
rank: 1,
|
|
1266
|
+
passes: 0,
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
const steps = decodeWalk(path, routes, cols, choices, id => { var _a, _b; return (_b = (_a = lengths.get(id)) !== null && _a !== void 0 ? _a : ref === null || ref === void 0 ? void 0 : ref.lengths.get(id)) !== null && _b !== void 0 ? _b : -1; });
|
|
1272
|
+
const n = steps.ids.length;
|
|
1273
|
+
out.rows.push({
|
|
1274
|
+
chunkStart: tileStart,
|
|
1275
|
+
name,
|
|
1276
|
+
fragStart: +cols[5],
|
|
1277
|
+
hapOffset: +cols[6],
|
|
1278
|
+
piece: +cols[7],
|
|
1279
|
+
n,
|
|
1280
|
+
interior,
|
|
1281
|
+
steps,
|
|
1282
|
+
span: [+cols[1], +cols[2]],
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
return out;
|
|
1287
|
+
}
|
|
1288
|
+
// The reference's nodes in a chunk's ref rows, with their positions
|
|
1289
|
+
export function referenceNodes(rows) {
|
|
1290
|
+
const nodes = [];
|
|
1291
|
+
for (const cols of rows) {
|
|
1292
|
+
if (cols[3] !== 'ref') {
|
|
1293
|
+
continue;
|
|
1294
|
+
}
|
|
1295
|
+
const handles = tokens(cols[9], { id: 0 }, []);
|
|
1296
|
+
const nodeLengths = numbers(cols[10]);
|
|
1297
|
+
const passes = passCounts(cols, handles.length);
|
|
1298
|
+
let start = +cols[6];
|
|
1299
|
+
for (let k = 0; k < handles.length; k++) {
|
|
1300
|
+
const length = nodeLengths[k];
|
|
1301
|
+
nodes.push({
|
|
1302
|
+
id: idOf(handles[k]),
|
|
1303
|
+
refName: cols[4],
|
|
1304
|
+
start,
|
|
1305
|
+
end: start + length,
|
|
1306
|
+
rank: 0,
|
|
1307
|
+
passes: passes[k],
|
|
1308
|
+
reversed: (handles[k] & 1) === 1,
|
|
1309
|
+
});
|
|
1310
|
+
start += length;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
return nodes;
|
|
1314
|
+
}
|