@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
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { panSNHaplotypeOrSample } from "./util.js";
|
|
2
|
+
import { NONE, RefPath, abridgedPiece, blockPositions, canonicalLink, checkedId, idOf, interiorAt, numbers, parseChoices, parseCopy, passCounts, refRows, tokens, traverse, } from "./walkChunk.js";
|
|
3
|
+
import { WalkFormatError } from "./walkContainer.js";
|
|
4
|
+
/**
|
|
5
|
+
* Why a count may fall short, one bit each; a count with none is exact.
|
|
6
|
+
* `unloaded`: a haplotype over the window not counted there has a run off the
|
|
7
|
+
* reference the call did not load. `unattached`: one has own sequence of a
|
|
8
|
+
* departure over the window the call did not read. `revisited`: the reference
|
|
9
|
+
* passes the node more than once, so a step on it can lie at another pass.
|
|
10
|
+
* `elsewhere`: steps on it can lie in rows filed away from the window, as
|
|
11
|
+
* every step can in a file that does not state `revisited:i:0`.
|
|
12
|
+
*/
|
|
13
|
+
export const COUNT_REASONS = Object.freeze({
|
|
14
|
+
unloaded: 1,
|
|
15
|
+
unattached: 2,
|
|
16
|
+
revisited: 4,
|
|
17
|
+
elsewhere: 8,
|
|
18
|
+
});
|
|
19
|
+
const REASONS = [
|
|
20
|
+
'unloaded',
|
|
21
|
+
'unattached',
|
|
22
|
+
'revisited',
|
|
23
|
+
'elsewhere',
|
|
24
|
+
];
|
|
25
|
+
export const countReasons = (bits) => REASONS.filter(reason => (bits & COUNT_REASONS[reason]) !== 0);
|
|
26
|
+
function popcount(word) {
|
|
27
|
+
const pairs = word - ((word >>> 1) & 0x55555555);
|
|
28
|
+
const nibbles = (pairs & 0x33333333) + ((pairs >>> 2) & 0x33333333);
|
|
29
|
+
return Math.imul((nibbles + (nibbles >>> 4)) & 0x0f0f0f0f, 0x01010101) >>> 24;
|
|
30
|
+
}
|
|
31
|
+
// A growing table of counted things, each with the haplotypes stepping it as
|
|
32
|
+
// a bitset and the steps on it
|
|
33
|
+
class Tally {
|
|
34
|
+
constructor(words) {
|
|
35
|
+
this.size = 0;
|
|
36
|
+
this.words = words;
|
|
37
|
+
this.bits = new Uint32Array(256 * words);
|
|
38
|
+
this.passes = new Float64Array(256);
|
|
39
|
+
}
|
|
40
|
+
add() {
|
|
41
|
+
if (this.size === this.passes.length) {
|
|
42
|
+
const bits = new Uint32Array(this.bits.length * 2);
|
|
43
|
+
bits.set(this.bits);
|
|
44
|
+
this.bits = bits;
|
|
45
|
+
const passes = new Float64Array(this.passes.length * 2);
|
|
46
|
+
passes.set(this.passes);
|
|
47
|
+
this.passes = passes;
|
|
48
|
+
}
|
|
49
|
+
return this.size++;
|
|
50
|
+
}
|
|
51
|
+
mark(k, haplotype) {
|
|
52
|
+
const at = k * this.words + (haplotype >>> 5);
|
|
53
|
+
this.bits[at] = this.bits[at] | (1 << (haplotype & 31));
|
|
54
|
+
this.passes[k] = this.passes[k] + 1;
|
|
55
|
+
}
|
|
56
|
+
count(k) {
|
|
57
|
+
let n = 0;
|
|
58
|
+
for (let w = 0; w < this.words; w++) {
|
|
59
|
+
n += popcount(this.bits[k * this.words + w]);
|
|
60
|
+
}
|
|
61
|
+
return n;
|
|
62
|
+
}
|
|
63
|
+
// whether a haplotype of `set` does not step k
|
|
64
|
+
lacks(k, set) {
|
|
65
|
+
let lacking = false;
|
|
66
|
+
for (let w = 0; w < this.words; w++) {
|
|
67
|
+
lacking || (lacking = (set[w] & ~this.bits[k * this.words + w]) !== 0);
|
|
68
|
+
}
|
|
69
|
+
return lacking;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const anyOf = (set) => set.some(word => word !== 0);
|
|
73
|
+
const lineKey = (name, fragStart) => `${name}\t${fragStart}`;
|
|
74
|
+
/**
|
|
75
|
+
* Counts, as it reads rows, the haplotypes stepping each node and link and
|
|
76
|
+
* the steps on each, keeping no step: per node and link a bitset of
|
|
77
|
+
* haplotypes and a pass count, and per tile its R's nodes and links once
|
|
78
|
+
*/
|
|
79
|
+
export class WalkTally {
|
|
80
|
+
// `stepsFiledAtPass`: the file states `revisited:i:0`, of which walk format
|
|
81
|
+
// 8 promises that every step on a reference node is filed at its pass
|
|
82
|
+
constructor(haplotypes, stepsFiledAtPass) {
|
|
83
|
+
this.nodeAt = new Map();
|
|
84
|
+
this.nodeIds = [];
|
|
85
|
+
// how many times the reference passes each node by a window tile's ref
|
|
86
|
+
// row, 0 where none holds it, and whether such a pass lies on the drawn
|
|
87
|
+
// stretch
|
|
88
|
+
this.refPasses = [];
|
|
89
|
+
this.drawn = [];
|
|
90
|
+
// each link under the end it leaves, as an end's node index twice plus its
|
|
91
|
+
// strand: the ends it reaches and its index, in turn
|
|
92
|
+
this.linkAt = [];
|
|
93
|
+
this.linkEnds = [];
|
|
94
|
+
this.pieces = new Map();
|
|
95
|
+
// the stretches of each contig the counted pieces cover, and whether the
|
|
96
|
+
// call loaded every step of each
|
|
97
|
+
this.stretches = new Map();
|
|
98
|
+
this.names = haplotypes;
|
|
99
|
+
this.stepsFiledAtPass = stepsFiledAtPass;
|
|
100
|
+
this.haplotypeIndex = new Map(haplotypes.map((name, k) => [name, k]));
|
|
101
|
+
const words = Math.max(1, Math.ceil(haplotypes.length / 32));
|
|
102
|
+
this.nodes = new Tally(words);
|
|
103
|
+
this.links = new Tally(words);
|
|
104
|
+
this.window = new Uint32Array(words);
|
|
105
|
+
this.unloaded = new Uint32Array(words);
|
|
106
|
+
this.unattached = new Uint32Array(words);
|
|
107
|
+
}
|
|
108
|
+
haplotype(name) {
|
|
109
|
+
const listed = panSNHaplotypeOrSample(name);
|
|
110
|
+
const k = this.haplotypeIndex.get(listed);
|
|
111
|
+
if (k === undefined) {
|
|
112
|
+
throw new WalkFormatError(`The walk file has rows of ${name}, and its header lists no haplotype ${listed}`);
|
|
113
|
+
}
|
|
114
|
+
return k;
|
|
115
|
+
}
|
|
116
|
+
node(id) {
|
|
117
|
+
let k = this.nodeAt.get(id);
|
|
118
|
+
if (k === undefined) {
|
|
119
|
+
k = this.nodes.add();
|
|
120
|
+
this.nodeAt.set(id, k);
|
|
121
|
+
this.nodeIds.push(id);
|
|
122
|
+
this.refPasses.push(0);
|
|
123
|
+
this.drawn.push(false);
|
|
124
|
+
}
|
|
125
|
+
return k;
|
|
126
|
+
}
|
|
127
|
+
// The link from handle a to handle b, which is the link from b flipped to
|
|
128
|
+
// a flipped, held under whichever way round leaves the smaller end
|
|
129
|
+
link(a, b, nodeA = this.node(idOf(a)), nodeB = this.node(idOf(b))) {
|
|
130
|
+
var _a;
|
|
131
|
+
const endA = 2 * nodeA + (a & 1);
|
|
132
|
+
const endB = 2 * nodeB + (b & 1);
|
|
133
|
+
const forward = endA < (endB ^ 1) || (endA === (endB ^ 1) && endB <= (endA ^ 1));
|
|
134
|
+
const from = forward ? endA : endB ^ 1;
|
|
135
|
+
const to = forward ? endB : endA ^ 1;
|
|
136
|
+
const held = (_a = this.linkAt[from]) !== null && _a !== void 0 ? _a : [];
|
|
137
|
+
this.linkAt[from] = held;
|
|
138
|
+
let k = -1;
|
|
139
|
+
for (let i = 0; i < held.length && k < 0; i += 2) {
|
|
140
|
+
k = held[i] === to ? held[i + 1] : -1;
|
|
141
|
+
}
|
|
142
|
+
if (k < 0) {
|
|
143
|
+
k = this.links.add();
|
|
144
|
+
held.push(to, k);
|
|
145
|
+
const [first, second] = canonicalLink(a, b);
|
|
146
|
+
this.linkEnds.push(first, second);
|
|
147
|
+
}
|
|
148
|
+
return k;
|
|
149
|
+
}
|
|
150
|
+
pathLink(r, i) {
|
|
151
|
+
let k = r.links[i];
|
|
152
|
+
if (k < 0) {
|
|
153
|
+
k = this.link(r.path.handles[i], r.path.handles[i + 1], r.nodes[i], r.nodes[i + 1]);
|
|
154
|
+
r.links[i] = k;
|
|
155
|
+
}
|
|
156
|
+
return k;
|
|
157
|
+
}
|
|
158
|
+
routesOf(rows, lengths) {
|
|
159
|
+
var _a, _b;
|
|
160
|
+
const previous = { id: 0 };
|
|
161
|
+
const routes = [];
|
|
162
|
+
const counted = [];
|
|
163
|
+
for (const cols of rows) {
|
|
164
|
+
if (cols[3] === 'route') {
|
|
165
|
+
const entry = (_a = tokens(cols[4], previous, [])[0]) !== null && _a !== void 0 ? _a : NONE;
|
|
166
|
+
const interior = tokens(cols[5], previous, []);
|
|
167
|
+
const exit = (_b = tokens(cols[6], previous, [])[0]) !== null && _b !== void 0 ? _b : NONE;
|
|
168
|
+
const n = interior.length;
|
|
169
|
+
const nodes = new Int32Array(n);
|
|
170
|
+
const links = new Int32Array(n - 1 + (entry === NONE ? 0 : 1) + (exit === NONE ? 0 : 1));
|
|
171
|
+
let at = 0;
|
|
172
|
+
let bp = 0;
|
|
173
|
+
let before = entry;
|
|
174
|
+
let beforeNode = entry === NONE ? -1 : this.node(idOf(entry));
|
|
175
|
+
for (let k = 0; k < n; k++) {
|
|
176
|
+
const h = interior[k];
|
|
177
|
+
const length = lengths.get(idOf(h));
|
|
178
|
+
if (length === undefined) {
|
|
179
|
+
throw new WalkFormatError(`a route steps on node ${idOf(h)}, which its unit's nodes row lacks`);
|
|
180
|
+
}
|
|
181
|
+
bp += length;
|
|
182
|
+
const node = this.node(idOf(h));
|
|
183
|
+
nodes[k] = node;
|
|
184
|
+
if (before !== NONE) {
|
|
185
|
+
links[at++] = this.link(before, h, beforeNode, node);
|
|
186
|
+
}
|
|
187
|
+
before = h;
|
|
188
|
+
beforeNode = node;
|
|
189
|
+
}
|
|
190
|
+
if (before !== NONE && exit !== NONE) {
|
|
191
|
+
links[at++] = this.link(before, exit, beforeNode);
|
|
192
|
+
}
|
|
193
|
+
routes.push({ entry, interior, exit });
|
|
194
|
+
counted.push({ nodes, links, bp });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { routes, counted };
|
|
198
|
+
}
|
|
199
|
+
walksOf(rows, routes) {
|
|
200
|
+
var _a, _b;
|
|
201
|
+
const walks = [];
|
|
202
|
+
for (const cols of rows) {
|
|
203
|
+
if (cols[3] === 'walk') {
|
|
204
|
+
const choices = parseChoices(cols[10]);
|
|
205
|
+
let interior = 0;
|
|
206
|
+
for (let k = 0; k < choices.length; k += 4) {
|
|
207
|
+
interior += (_b = (_a = routes[choices[k]]) === null || _a === void 0 ? void 0 : _a.interior.length) !== null && _b !== void 0 ? _b : 0;
|
|
208
|
+
}
|
|
209
|
+
const [start, startCopy] = parseCopy(cols[9]);
|
|
210
|
+
walks.push({
|
|
211
|
+
name: cols[4],
|
|
212
|
+
fragStart: +cols[5],
|
|
213
|
+
hapOffset: +cols[6],
|
|
214
|
+
piece: +cols[7],
|
|
215
|
+
n: +cols[8],
|
|
216
|
+
interior,
|
|
217
|
+
span: { start: +cols[1], end: +cols[2] },
|
|
218
|
+
start,
|
|
219
|
+
startCopy,
|
|
220
|
+
choices,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return walks;
|
|
225
|
+
}
|
|
226
|
+
lengthsOf(rows, into) {
|
|
227
|
+
for (const cols of rows) {
|
|
228
|
+
if (cols[3] === 'nodes') {
|
|
229
|
+
const deltas = numbers(cols[4]);
|
|
230
|
+
const lengths = numbers(cols[5]);
|
|
231
|
+
let id = 0;
|
|
232
|
+
for (let k = 0; k < deltas.length; k++) {
|
|
233
|
+
id = checkedId(id + deltas[k]);
|
|
234
|
+
into.set(id, lengths[k]);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return into;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* A tile's rows, split on tabs, ready to count. A tile of the window
|
|
242
|
+
* states how many times the reference passes each node of its R, which
|
|
243
|
+
* makes a count on that node exact where its pass lies on the drawn
|
|
244
|
+
* stretch and the file states `revisited:i:0`; a tile fetched to attach
|
|
245
|
+
* states nothing of the kind
|
|
246
|
+
*/
|
|
247
|
+
tile(rows, tileStart, ofWindow) {
|
|
248
|
+
const path = new RefPath();
|
|
249
|
+
const lengths = new Map();
|
|
250
|
+
const references = [];
|
|
251
|
+
for (const cols of refRows(rows)) {
|
|
252
|
+
const handles = tokens(cols[9], { id: 0 }, []);
|
|
253
|
+
const nodeLengths = numbers(cols[10]);
|
|
254
|
+
const passes = passCounts(cols, handles.length);
|
|
255
|
+
const from = path.handles.length;
|
|
256
|
+
path.add(handles, nodeLengths, +cols[6]);
|
|
257
|
+
for (let k = 0; k < handles.length; k++) {
|
|
258
|
+
const id = idOf(handles[k]);
|
|
259
|
+
lengths.set(id, nodeLengths[k]);
|
|
260
|
+
const node = this.node(id);
|
|
261
|
+
if (ofWindow) {
|
|
262
|
+
this.refPasses[node] = Math.max(this.refPasses[node], passes[k]);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
references.push({
|
|
266
|
+
name: cols[4],
|
|
267
|
+
fragStart: +cols[5],
|
|
268
|
+
piece: +cols[7],
|
|
269
|
+
hapOffset: +cols[6],
|
|
270
|
+
from,
|
|
271
|
+
to: path.handles.length,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
const r = {
|
|
275
|
+
path,
|
|
276
|
+
nodes: Int32Array.from(path.handles, h => this.node(idOf(h))),
|
|
277
|
+
links: new Int32Array(path.handles.length).fill(-1),
|
|
278
|
+
lengths: this.lengthsOf(rows, lengths),
|
|
279
|
+
};
|
|
280
|
+
const { routes, counted } = this.routesOf(rows, r.lengths);
|
|
281
|
+
return {
|
|
282
|
+
start: tileStart,
|
|
283
|
+
r,
|
|
284
|
+
references,
|
|
285
|
+
routes,
|
|
286
|
+
counted,
|
|
287
|
+
walks: this.walksOf(rows, routes),
|
|
288
|
+
abridged: rows
|
|
289
|
+
.filter(cols => cols[3] === 'abridged')
|
|
290
|
+
.map(cols => abridgedPiece(cols, tileStart)),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
// A detail unit's rows, decoded against its tile's R
|
|
294
|
+
detail(rows, tile) {
|
|
295
|
+
const lengths = this.lengthsOf(rows, new Map(tile.r.lengths));
|
|
296
|
+
const { routes, counted } = this.routesOf(rows, lengths);
|
|
297
|
+
return {
|
|
298
|
+
start: tile.start,
|
|
299
|
+
r: tile.r,
|
|
300
|
+
references: [],
|
|
301
|
+
routes,
|
|
302
|
+
counted,
|
|
303
|
+
walks: this.walksOf(rows, routes),
|
|
304
|
+
abridged: [],
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
// Marks the nodes of a window tile's R whose pass lies on `drawn`
|
|
308
|
+
placeReference(unit, drawn) {
|
|
309
|
+
var _a;
|
|
310
|
+
const { path, nodes } = unit.r;
|
|
311
|
+
for (let i = 0; i < path.handles.length; i++) {
|
|
312
|
+
const node = nodes[i];
|
|
313
|
+
(_a = this.drawn)[node] || (_a[node] = path.starts[i] < drawn.end && path.end(i) > drawn.start);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
piece(name, fragStart, piece, ends) {
|
|
317
|
+
var _a;
|
|
318
|
+
const key = lineKey(name, fragStart);
|
|
319
|
+
const line = (_a = this.pieces.get(key)) !== null && _a !== void 0 ? _a : new Map();
|
|
320
|
+
this.pieces.set(key, line);
|
|
321
|
+
line.set(piece, ends);
|
|
322
|
+
this.mark(this.window, ends.haplotype);
|
|
323
|
+
}
|
|
324
|
+
stretch(name, start, end, loaded) {
|
|
325
|
+
var _a;
|
|
326
|
+
const held = (_a = this.stretches.get(name)) !== null && _a !== void 0 ? _a : [];
|
|
327
|
+
this.stretches.set(name, held);
|
|
328
|
+
held.push({ start, end, loaded });
|
|
329
|
+
}
|
|
330
|
+
mark(set, haplotype) {
|
|
331
|
+
set[haplotype >>> 5] = set[haplotype >>> 5] | (1 << (haplotype & 31));
|
|
332
|
+
}
|
|
333
|
+
// The reference's own piece in a window tile, counted on its haplotype
|
|
334
|
+
countReference(unit, { name, fragStart, piece, from, to }) {
|
|
335
|
+
const haplotype = this.haplotype(name);
|
|
336
|
+
const { r } = unit;
|
|
337
|
+
for (let i = from; i < to; i++) {
|
|
338
|
+
this.nodes.mark(r.nodes[i], haplotype);
|
|
339
|
+
if (i + 1 < to) {
|
|
340
|
+
this.links.mark(this.pathLink(r, i), haplotype);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
this.piece(name, fragStart, piece, {
|
|
344
|
+
haplotype,
|
|
345
|
+
first: to > from ? r.path.handles[from] : -1,
|
|
346
|
+
last: to > from ? r.path.handles[to - 1] : -1,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* A walk row's steps as the row decodes them, counted on its haplotype
|
|
351
|
+
* when `counting`, and its bp on its contig either way
|
|
352
|
+
*/
|
|
353
|
+
walk(unit, walk, counting) {
|
|
354
|
+
const haplotype = this.haplotype(walk.name);
|
|
355
|
+
const { r, routes, counted } = unit;
|
|
356
|
+
const { path } = r;
|
|
357
|
+
let first = -1;
|
|
358
|
+
let last = -1;
|
|
359
|
+
let bp = 0;
|
|
360
|
+
let emitted = 0;
|
|
361
|
+
traverse(path, routes, walk.start, walk.startCopy, walk.choices, walk.n, {
|
|
362
|
+
step: (handle, i) => {
|
|
363
|
+
first = first < 0 ? handle : first;
|
|
364
|
+
last = handle;
|
|
365
|
+
bp += path.before[i + 1] - path.before[i];
|
|
366
|
+
emitted++;
|
|
367
|
+
if (counting) {
|
|
368
|
+
this.nodes.mark(r.nodes[i], haplotype);
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
stretch: (from, to, forward) => {
|
|
372
|
+
last = path.handleAt(to, forward);
|
|
373
|
+
bp += forward
|
|
374
|
+
? path.before[to + 1] - path.before[from + 1]
|
|
375
|
+
: path.before[from] - path.before[to];
|
|
376
|
+
emitted += Math.abs(to - from);
|
|
377
|
+
const low = Math.min(from, to);
|
|
378
|
+
const high = counting ? Math.max(from, to) : low - 1;
|
|
379
|
+
for (let j = low; j <= high; j++) {
|
|
380
|
+
if (j !== from) {
|
|
381
|
+
this.nodes.mark(r.nodes[j], haplotype);
|
|
382
|
+
}
|
|
383
|
+
if (j < high) {
|
|
384
|
+
this.links.mark(this.pathLink(r, j), haplotype);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
route: (index, flip) => {
|
|
389
|
+
const route = routes[index];
|
|
390
|
+
const { nodes, links, bp: routeBp } = counted[index];
|
|
391
|
+
const n = route.interior.length;
|
|
392
|
+
first = first < 0 && n > 0 ? interiorAt(route, flip, 0) : first;
|
|
393
|
+
last = n > 0 ? interiorAt(route, flip, n - 1) : last;
|
|
394
|
+
bp += routeBp;
|
|
395
|
+
emitted += n;
|
|
396
|
+
if (counting) {
|
|
397
|
+
for (const node of nodes) {
|
|
398
|
+
this.nodes.mark(node, haplotype);
|
|
399
|
+
}
|
|
400
|
+
for (const link of links) {
|
|
401
|
+
this.links.mark(link, haplotype);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
if (emitted !== walk.n) {
|
|
408
|
+
throw new WalkFormatError(`a walk row of ${walk.name} decodes to ${emitted} steps, not its ${walk.n}`);
|
|
409
|
+
}
|
|
410
|
+
if (counting) {
|
|
411
|
+
this.piece(walk.name, walk.fragStart, walk.piece, {
|
|
412
|
+
haplotype,
|
|
413
|
+
first,
|
|
414
|
+
last,
|
|
415
|
+
});
|
|
416
|
+
this.stretch(walk.name, walk.hapOffset, walk.hapOffset + bp, true);
|
|
417
|
+
}
|
|
418
|
+
return bp;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* An abridged piece the call does not load, from its blocks row: its steps
|
|
422
|
+
* on R exactly, counted on its haplotype, and its runs off R as bp, which
|
|
423
|
+
* leave the haplotype's counts off R lower bounds
|
|
424
|
+
*/
|
|
425
|
+
blocks(unit, piece, blocks) {
|
|
426
|
+
const haplotype = this.haplotype(piece.name);
|
|
427
|
+
const { r } = unit;
|
|
428
|
+
const { path } = r;
|
|
429
|
+
const { from, to } = blockPositions(piece, blocks, path);
|
|
430
|
+
const count = from.length;
|
|
431
|
+
const firstOf = (k) => blocks.against[k]
|
|
432
|
+
? path.handleAt(to[k] - 1, false)
|
|
433
|
+
: path.handleAt(from[k], true);
|
|
434
|
+
const lastOf = (k) => blocks.against[k]
|
|
435
|
+
? path.handleAt(from[k], false)
|
|
436
|
+
: path.handleAt(to[k] - 1, true);
|
|
437
|
+
let offR = blocks.before + blocks.after;
|
|
438
|
+
for (let k = 0; k < count; k++) {
|
|
439
|
+
for (let i = from[k]; i < to[k]; i++) {
|
|
440
|
+
this.nodes.mark(r.nodes[i], haplotype);
|
|
441
|
+
if (i + 1 < to[k]) {
|
|
442
|
+
this.links.mark(this.pathLink(r, i), haplotype);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (k > 0 && blocks.off[k] === 0) {
|
|
446
|
+
this.links.mark(this.link(lastOf(k - 1), firstOf(k)), haplotype);
|
|
447
|
+
}
|
|
448
|
+
offR += blocks.off[k];
|
|
449
|
+
}
|
|
450
|
+
if (offR > 0) {
|
|
451
|
+
this.mark(this.unloaded, haplotype);
|
|
452
|
+
}
|
|
453
|
+
this.piece(piece.name, piece.fragStart, piece.piece, {
|
|
454
|
+
haplotype,
|
|
455
|
+
first: blocks.before > 0 ? -1 : firstOf(0),
|
|
456
|
+
last: blocks.after > 0 ? -1 : lastOf(count - 1),
|
|
457
|
+
});
|
|
458
|
+
this.stretch(piece.name, piece.hapOffset, piece.hapOffset + piece.hapBp, offR === 0);
|
|
459
|
+
}
|
|
460
|
+
// An abridged piece the call holds nothing of but its bp, which leaves its
|
|
461
|
+
// haplotype's counts off R lower bounds
|
|
462
|
+
unread(piece) {
|
|
463
|
+
const haplotype = this.haplotype(piece.name);
|
|
464
|
+
this.mark(this.unloaded, haplotype);
|
|
465
|
+
this.piece(piece.name, piece.fragStart, piece.piece, {
|
|
466
|
+
haplotype,
|
|
467
|
+
first: -1,
|
|
468
|
+
last: -1,
|
|
469
|
+
});
|
|
470
|
+
this.stretch(piece.name, piece.hapOffset, piece.hapOffset + piece.hapBp, false);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Every departure record over the window: its haplotype is over the
|
|
474
|
+
* window, and one whose own sequence, less what lies between W lines, the
|
|
475
|
+
* counted pieces do not cover leaves that haplotype's counts off R lower
|
|
476
|
+
* bounds
|
|
477
|
+
*/
|
|
478
|
+
departures(records) {
|
|
479
|
+
var _a;
|
|
480
|
+
for (const d of records) {
|
|
481
|
+
const haplotype = this.haplotype(d.name);
|
|
482
|
+
this.mark(this.window, haplotype);
|
|
483
|
+
const need = d.ownBp - d.missingBp;
|
|
484
|
+
let loaded = 0;
|
|
485
|
+
let held = 0;
|
|
486
|
+
for (const { start, end, loaded: whole } of (_a = this.stretches.get(d.name)) !== null && _a !== void 0 ? _a : []) {
|
|
487
|
+
const overlap = Math.max(0, Math.min(end, d.hapStart + d.ownBp) - Math.max(start, d.hapStart));
|
|
488
|
+
loaded += whole ? overlap : 0;
|
|
489
|
+
held += overlap;
|
|
490
|
+
}
|
|
491
|
+
if (need > 0 && loaded < need && held < need) {
|
|
492
|
+
this.mark(this.unattached, haplotype);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
// The links between consecutive pieces of a line, where both are counted
|
|
497
|
+
// and the call loaded the steps either side
|
|
498
|
+
joinPieces() {
|
|
499
|
+
for (const line of this.pieces.values()) {
|
|
500
|
+
for (const [piece, ends] of line) {
|
|
501
|
+
const next = line.get(piece + 1);
|
|
502
|
+
if (next && ends.last >= 0 && next.first >= 0) {
|
|
503
|
+
this.links.mark(this.link(ends.last, next.first), ends.haplotype);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
// Why the count of node k, or of a link with an end there, may fall short
|
|
509
|
+
// where one of `missing` does not step it: 0 on a node of a window tile's
|
|
510
|
+
// R passed once on the drawn stretch, in a file that states `revisited:i:0`
|
|
511
|
+
nodeReasons(k, missing) {
|
|
512
|
+
const passes = this.refPasses[k];
|
|
513
|
+
return this.stepsFiledAtPass && passes === 1
|
|
514
|
+
? this.drawn[k]
|
|
515
|
+
? 0
|
|
516
|
+
: COUNT_REASONS.elsewhere
|
|
517
|
+
: COUNT_REASONS.elsewhere |
|
|
518
|
+
(passes > 1 ? COUNT_REASONS.revisited : 0) |
|
|
519
|
+
(missing(this.unloaded) ? COUNT_REASONS.unloaded : 0) |
|
|
520
|
+
(missing(this.unattached) ? COUNT_REASONS.unattached : 0);
|
|
521
|
+
}
|
|
522
|
+
columns(tally, order, reasons) {
|
|
523
|
+
const file = this.names.length;
|
|
524
|
+
const held = new Map([
|
|
525
|
+
[this.unloaded, anyOf(this.unloaded)],
|
|
526
|
+
[this.unattached, anyOf(this.unattached)],
|
|
527
|
+
]);
|
|
528
|
+
const haplotypes = Int32Array.from(order, k => tally.count(k));
|
|
529
|
+
return {
|
|
530
|
+
haplotypes,
|
|
531
|
+
passes: Float64Array.from(order, k => tally.passes[k]),
|
|
532
|
+
haplotypeReasons: Uint8Array.from(order, (k, i) => haplotypes[i] === file
|
|
533
|
+
? 0
|
|
534
|
+
: reasons(k, set => held.get(set) === true && tally.lacks(k, set))),
|
|
535
|
+
passReasons: Uint8Array.from(order, k => reasons(k, set => held.get(set) === true)),
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
namesIn(set) {
|
|
539
|
+
return this.names.filter((_, k) => (set[k >>> 5] & (1 << (k & 31))) !== 0);
|
|
540
|
+
}
|
|
541
|
+
result() {
|
|
542
|
+
this.joinPieces();
|
|
543
|
+
const nodeOrder = [];
|
|
544
|
+
for (let k = 0; k < this.nodes.size; k++) {
|
|
545
|
+
if (this.nodes.passes[k] > 0) {
|
|
546
|
+
nodeOrder.push(k);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
nodeOrder.sort((a, b) => this.nodeIds[a] - this.nodeIds[b]);
|
|
550
|
+
const linkOrder = [];
|
|
551
|
+
for (let k = 0; k < this.links.size; k++) {
|
|
552
|
+
if (this.links.passes[k] > 0) {
|
|
553
|
+
linkOrder.push(k);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
const fromOf = (k) => this.linkEnds[2 * k];
|
|
557
|
+
const toOf = (k) => this.linkEnds[2 * k + 1];
|
|
558
|
+
linkOrder.sort((a, b) => fromOf(a) - fromOf(b) || toOf(a) - toOf(b));
|
|
559
|
+
const endNode = (handle) => this.nodeAt.get(idOf(handle));
|
|
560
|
+
const linkReasons = (k, missing) => this.nodeReasons(endNode(fromOf(k)), missing) |
|
|
561
|
+
this.nodeReasons(endNode(toOf(k)), missing);
|
|
562
|
+
return {
|
|
563
|
+
haplotypes: {
|
|
564
|
+
file: this.names.length,
|
|
565
|
+
window: this.namesIn(this.window).length,
|
|
566
|
+
unloaded: this.namesIn(this.unloaded),
|
|
567
|
+
unattached: this.namesIn(this.unattached),
|
|
568
|
+
},
|
|
569
|
+
nodes: {
|
|
570
|
+
ids: Int32Array.from(nodeOrder, k => this.nodeIds[k]),
|
|
571
|
+
...this.columns(this.nodes, nodeOrder, (k, missing) => this.nodeReasons(k, missing)),
|
|
572
|
+
},
|
|
573
|
+
links: {
|
|
574
|
+
from: Uint32Array.from(linkOrder, fromOf),
|
|
575
|
+
to: Uint32Array.from(linkOrder, toOf),
|
|
576
|
+
...this.columns(this.links, linkOrder, linkReasons),
|
|
577
|
+
},
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Abridged } from './walkRows.ts';
|
|
2
|
+
export interface WalkDeparture {
|
|
3
|
+
name: string;
|
|
4
|
+
hapStart: number;
|
|
5
|
+
ownBp: number;
|
|
6
|
+
missingBp: number;
|
|
7
|
+
leavePos: number;
|
|
8
|
+
leaveReversed: boolean;
|
|
9
|
+
leavePasses: number;
|
|
10
|
+
rejoinPos: number;
|
|
11
|
+
rejoinReversed: boolean;
|
|
12
|
+
rejoinPasses: number;
|
|
13
|
+
fromSteps: boolean;
|
|
14
|
+
steps?: {
|
|
15
|
+
ids: Int32Array;
|
|
16
|
+
rev: Uint8Array;
|
|
17
|
+
};
|
|
18
|
+
breakAt?: number;
|
|
19
|
+
abridged?: Abridged[];
|
|
20
|
+
}
|
|
21
|
+
export type DepartureRecord = Omit<WalkDeparture, 'steps' | 'breakAt' | 'abridged'>;
|
|
22
|
+
export interface Hole {
|
|
23
|
+
refName: string;
|
|
24
|
+
start: number;
|
|
25
|
+
end: number;
|
|
26
|
+
}
|
|
27
|
+
export declare const ATTACH_BP = 65536;
|
|
28
|
+
export declare function squeezer(holes: Hole[], refName: string): (bp: number) => number;
|
|
29
|
+
export type DepartureEnds = Pick<WalkDeparture, 'ownBp' | 'leavePos' | 'leaveReversed' | 'rejoinPos' | 'rejoinReversed'>;
|
|
30
|
+
export declare function adjacentSteps(d: DepartureEnds, squeeze: (bp: number) => number): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* What a departure does on S, derived from its ends: `switch`, the strands
|
|
33
|
+
* differ; `back`, the path jumps back over reference it walks again;
|
|
34
|
+
* `adjacent`, sequence of its own between adjacent steps; else `forward`, a
|
|
35
|
+
* jump forward over reference it leaves out. Positions compare with the
|
|
36
|
+
* holes before them removed.
|
|
37
|
+
*/
|
|
38
|
+
export type DepartureKind = 'switch' | 'back' | 'adjacent' | 'forward';
|
|
39
|
+
export declare function departureKind(d: DepartureEnds, squeeze?: (bp: number) => number): DepartureKind;
|
|
40
|
+
export declare function departureStretch(d: DepartureEnds): [number, number];
|
|
41
|
+
export declare function leavesOrRejoinsOn(d: DepartureEnds, span: {
|
|
42
|
+
start: number;
|
|
43
|
+
end: number;
|
|
44
|
+
}): boolean;
|
|
45
|
+
export declare const attachesOwnSteps: (d: DepartureEnds, drawn: {
|
|
46
|
+
start: number;
|
|
47
|
+
end: number;
|
|
48
|
+
}, attachBp: number) => boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Whether a departure's place on the reference is the writer's choice: an
|
|
51
|
+
* end lies on a node the reference passes more than once, so another pass
|
|
52
|
+
* would put it elsewhere or make it none, as where a path follows one copy
|
|
53
|
+
* of a collapsed repeat and the placement hops between copies
|
|
54
|
+
*/
|
|
55
|
+
export declare function placementChoice(d: Pick<WalkDeparture, 'leavePasses' | 'rejoinPasses'>): boolean;
|
|
56
|
+
export declare function parseDepartureRow(cols: string[], from?: number): WalkDeparture;
|
|
57
|
+
export declare function departureOwnTiles(cols: string[], from?: number): number[];
|
|
58
|
+
export declare function departureRowKey(cols: string[], from?: number): string;
|