@gmod/gfa-tabix 0.0.0-stage → 10.0.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 +82 -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 +28 -0
- package/esm/index.js +15 -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 +49 -0
- package/esm/walkDepartures.js +272 -0
- package/esm/walkFile.d.ts +240 -0
- package/esm/walkFile.js +1389 -0
- package/esm/walkGaps.d.ts +56 -0
- package/esm/walkGaps.js +345 -0
- package/esm/walkRows.d.ts +305 -0
- package/esm/walkRows.js +1600 -0
- package/package.json +61 -3
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { GenericFilehandle } from 'generic-filehandle2';
|
|
2
|
+
/**
|
|
3
|
+
* Walk format 8 (gfa-to-tabix's ADRs 0010 and 0012): `<prefix>.<sample>.walks.gz`, a
|
|
4
|
+
* series of gzip units, and its directory `<prefix>.<sample>.walks.gz.idx`.
|
|
5
|
+
* The directory's first line is the data's `#walks` line plus
|
|
6
|
+
* `bytes:i:<data length>`, then `#header 0 <length>` and one
|
|
7
|
+
* `seq <offset> <length>` line per reference sequence, giving the unit that
|
|
8
|
+
* lists the sequence's units: `#entry seq start end kind name offset length`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const WALK_FORMAT = 8;
|
|
11
|
+
export type WalkFilehandle = Pick<GenericFilehandle, 'read' | 'readFile' | 'stat' | 'source'>;
|
|
12
|
+
export declare class WalkFormatError extends Error {
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* One unit of a sequence's table: `start end` is a tile's true extent, a
|
|
17
|
+
* departure block's span, a detail record's pieces' span on the reference,
|
|
18
|
+
* or the span of a tile's abridged pieces' blocks; `name` a tile's nominal
|
|
19
|
+
* start, a detail's `tile:detail`, a `blocks` unit's tile, or `.` for a
|
|
20
|
+
* departure block; `offset length` its compressed bytes in the data file
|
|
21
|
+
*/
|
|
22
|
+
export interface WalkUnit {
|
|
23
|
+
refName: string;
|
|
24
|
+
start: number;
|
|
25
|
+
end: number;
|
|
26
|
+
kind: string;
|
|
27
|
+
name: string;
|
|
28
|
+
offset: number;
|
|
29
|
+
length: number;
|
|
30
|
+
}
|
|
31
|
+
export declare function once<T>(read: () => Promise<T>): () => Promise<T>;
|
|
32
|
+
export declare function raceAbort<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
33
|
+
/**
|
|
34
|
+
* A format 8 walk file read by byte ranges: the directory and the header
|
|
35
|
+
* once, each sequence's table once, and units as asked, the ranges of units
|
|
36
|
+
* that touch merged into one single-range read
|
|
37
|
+
*/
|
|
38
|
+
export declare class WalkContainer {
|
|
39
|
+
private readonly data;
|
|
40
|
+
private readonly directory;
|
|
41
|
+
private readonly tableReads;
|
|
42
|
+
constructor(data: WalkFilehandle, directory: WalkFilehandle);
|
|
43
|
+
private readonly readDirectory;
|
|
44
|
+
readonly header: () => Promise<string[]>;
|
|
45
|
+
sequences(): Promise<string[]>;
|
|
46
|
+
units(refName: string): Promise<WalkUnit[]>;
|
|
47
|
+
private table;
|
|
48
|
+
/**
|
|
49
|
+
* The lines of each unit, without its `#unit` line, in the order given.
|
|
50
|
+
* Units whose byte ranges touch are fetched by one read, and each is
|
|
51
|
+
* gunzipped on its own.
|
|
52
|
+
*/
|
|
53
|
+
read(units: WalkUnit[], signal?: AbortSignal): Promise<string[][]>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { unzip } from '@gmod/bgzf-filehandle';
|
|
2
|
+
import { WalkRowError } from "./walkChunk.js";
|
|
3
|
+
/**
|
|
4
|
+
* Walk format 8 (gfa-to-tabix's ADRs 0010 and 0012): `<prefix>.<sample>.walks.gz`, a
|
|
5
|
+
* series of gzip units, and its directory `<prefix>.<sample>.walks.gz.idx`.
|
|
6
|
+
* The directory's first line is the data's `#walks` line plus
|
|
7
|
+
* `bytes:i:<data length>`, then `#header 0 <length>` and one
|
|
8
|
+
* `seq <offset> <length>` line per reference sequence, giving the unit that
|
|
9
|
+
* lists the sequence's units: `#entry seq start end kind name offset length`.
|
|
10
|
+
*/
|
|
11
|
+
export const WALK_FORMAT = 8;
|
|
12
|
+
// A walk file the reader refuses: one in a format other than 8, or one whose
|
|
13
|
+
// units do not hold what its directory and tables say
|
|
14
|
+
export class WalkFormatError extends Error {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this.name = 'WalkFormatError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const decoder = new TextDecoder();
|
|
21
|
+
// The row kinds each unit kind holds; a unit kind not here is a later
|
|
22
|
+
// addition, whose rows a reader never reads
|
|
23
|
+
const ROW_KINDS = new Map([
|
|
24
|
+
['tile', ['ref', 'nodes', 'route', 'walk', 'abridged']],
|
|
25
|
+
['departures', ['departure']],
|
|
26
|
+
['detail', ['nodes', 'route', 'walk']],
|
|
27
|
+
['blocks', ['blocks']],
|
|
28
|
+
]);
|
|
29
|
+
async function unitLines(bytes, unit) {
|
|
30
|
+
const text = await unzip(bytes).then(unzipped => decoder.decode(unzipped), (error) => {
|
|
31
|
+
throw new WalkFormatError(`${unit} is not whole gzip members`, {
|
|
32
|
+
cause: error,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
if (!text.endsWith('\n')) {
|
|
36
|
+
throw new WalkFormatError(`${unit} ends partway through a line: ${JSON.stringify(text.slice(-40))}`);
|
|
37
|
+
}
|
|
38
|
+
return text.split('\n').filter(line => line !== '');
|
|
39
|
+
}
|
|
40
|
+
function unitName(unit) {
|
|
41
|
+
return `The unit at byte ${unit.offset} of the walk file, ${unit.refName}'s ${unit.kind} ${unit.name} over [${unit.start}, ${unit.end}) as its table lists it,`;
|
|
42
|
+
}
|
|
43
|
+
function endOfTableBefore({ header, tables }, table) {
|
|
44
|
+
let start = header.offset + header.length;
|
|
45
|
+
for (const { offset, length } of tables.values()) {
|
|
46
|
+
if (offset < table.offset) {
|
|
47
|
+
start = Math.max(start, offset + length);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return start;
|
|
51
|
+
}
|
|
52
|
+
function isTableHead(head, refName) {
|
|
53
|
+
const [tag, seq, start, , kind, name, ...more] = head.split('\t');
|
|
54
|
+
return (more.length === 0 &&
|
|
55
|
+
[tag, seq, start, kind, name].join('\t') ===
|
|
56
|
+
['#unit', refName, '0', 'table', '.'].join('\t'));
|
|
57
|
+
}
|
|
58
|
+
// The columns each row kind holds; any after them are tags, `NAME:T:value`
|
|
59
|
+
const FIXED_COLUMNS = new Map([
|
|
60
|
+
['ref', 12],
|
|
61
|
+
['nodes', 6],
|
|
62
|
+
['route', 7],
|
|
63
|
+
['walk', 11],
|
|
64
|
+
['abridged', 11],
|
|
65
|
+
['blocks', 8],
|
|
66
|
+
['departure', 15],
|
|
67
|
+
]);
|
|
68
|
+
const TAG = /^[A-Za-z][A-Za-z0-9]*:[A-Za-z]:/;
|
|
69
|
+
// A column after a row's fixed ones that is no tag
|
|
70
|
+
function untagged(row) {
|
|
71
|
+
var _a, _b;
|
|
72
|
+
const fixed = (_b = FIXED_COLUMNS.get((_a = row.split('\t', 4)[3]) !== null && _a !== void 0 ? _a : '')) !== null && _b !== void 0 ? _b : Infinity;
|
|
73
|
+
let at = -1;
|
|
74
|
+
let found = 0;
|
|
75
|
+
while (found < fixed) {
|
|
76
|
+
at = row.indexOf('\t', at + 1);
|
|
77
|
+
found = at < 0 ? Infinity : found + 1;
|
|
78
|
+
}
|
|
79
|
+
return found === fixed
|
|
80
|
+
? row
|
|
81
|
+
.slice(at + 1)
|
|
82
|
+
.split('\t')
|
|
83
|
+
.find(col => !TAG.test(col))
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
// The fixed columns a row's kind holds, where the row holds fewer
|
|
87
|
+
function lacking(row) {
|
|
88
|
+
var _a;
|
|
89
|
+
const fixed = FIXED_COLUMNS.get((_a = row.split('\t', 4)[3]) !== null && _a !== void 0 ? _a : '');
|
|
90
|
+
return fixed !== undefined && row.split('\t', fixed).length < fixed
|
|
91
|
+
? fixed
|
|
92
|
+
: undefined;
|
|
93
|
+
}
|
|
94
|
+
function checkUnit(unit, head, rows) {
|
|
95
|
+
var _a, _b, _c;
|
|
96
|
+
const stated = [
|
|
97
|
+
'#unit',
|
|
98
|
+
unit.refName,
|
|
99
|
+
unit.start,
|
|
100
|
+
unit.end,
|
|
101
|
+
unit.kind,
|
|
102
|
+
unit.name,
|
|
103
|
+
].join('\t');
|
|
104
|
+
const kinds = ROW_KINDS.get(unit.kind);
|
|
105
|
+
let stray;
|
|
106
|
+
let extra;
|
|
107
|
+
let short;
|
|
108
|
+
if (kinds !== undefined) {
|
|
109
|
+
for (const row of rows) {
|
|
110
|
+
const cols = row.split('\t');
|
|
111
|
+
const kind = (_a = cols[3]) !== null && _a !== void 0 ? _a : '';
|
|
112
|
+
const fixed = FIXED_COLUMNS.get(kind);
|
|
113
|
+
if (stray === undefined &&
|
|
114
|
+
(cols[0] !== unit.refName || !kinds.includes(kind))) {
|
|
115
|
+
stray = row;
|
|
116
|
+
}
|
|
117
|
+
if (fixed !== undefined && cols.length !== fixed) {
|
|
118
|
+
if (cols.length < fixed) {
|
|
119
|
+
short !== null && short !== void 0 ? short : (short = row);
|
|
120
|
+
}
|
|
121
|
+
else if (extra === undefined) {
|
|
122
|
+
for (let k = fixed; k < cols.length; k++) {
|
|
123
|
+
if (!TAG.test(cols[k])) {
|
|
124
|
+
extra = row;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (head !== stated) {
|
|
132
|
+
throw new WalkFormatError(`${unitName(unit)} starts ${JSON.stringify((_b = head === null || head === void 0 ? void 0 : head.slice(0, 80)) !== null && _b !== void 0 ? _b : '')}`);
|
|
133
|
+
}
|
|
134
|
+
else if (stray !== undefined) {
|
|
135
|
+
throw new WalkFormatError(`${unitName(unit)} holds a row that is not one of ${unit.refName}'s ${kinds === null || kinds === void 0 ? void 0 : kinds.join(', ')} rows: ${JSON.stringify(stray.slice(0, 80))}`);
|
|
136
|
+
}
|
|
137
|
+
else if (extra !== undefined) {
|
|
138
|
+
throw new WalkFormatError(`${unitName(unit)} holds a row with a column after its fixed ones that is no tag, ${JSON.stringify((_c = untagged(extra)) === null || _c === void 0 ? void 0 : _c.slice(0, 40))}: ${JSON.stringify(extra.slice(0, 80))}`);
|
|
139
|
+
}
|
|
140
|
+
else if (short !== undefined) {
|
|
141
|
+
throw new WalkRowError(`${unitName(unit)} holds a row of fewer columns than the ${lacking(short)} its kind holds: ${JSON.stringify(short.slice(0, 80))}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function integerTag(fields, tag) {
|
|
145
|
+
const field = fields.find(f => f.startsWith(`${tag}:i:`));
|
|
146
|
+
return field === undefined ? undefined : +field.slice(tag.length + 3);
|
|
147
|
+
}
|
|
148
|
+
function formatError(format) {
|
|
149
|
+
return new WalkFormatError(format > WALK_FORMAT
|
|
150
|
+
? `This walk file is format ${format}; this version of @gmod/gfa-tabix reads format ${WALK_FORMAT}, so update the viewer, or @gmod/gfa-tabix, to one that reads format ${format}`
|
|
151
|
+
: `This walk file is format ${format}; @gmod/gfa-tabix reads format ${WALK_FORMAT}, so rebuild it with gfa-to-tabix 5.0 or later`);
|
|
152
|
+
}
|
|
153
|
+
function parseDirectory(text) {
|
|
154
|
+
var _a;
|
|
155
|
+
const [first = '', ...rest] = text.split('\n').filter(line => line !== '');
|
|
156
|
+
const fields = first.split('\t');
|
|
157
|
+
if (fields[0] !== '#walks') {
|
|
158
|
+
throw new WalkFormatError(`This is not a walk file's directory: its first line starts ${JSON.stringify(first.slice(0, 40))}, not #walks`);
|
|
159
|
+
}
|
|
160
|
+
const format = (_a = integerTag(fields, 'format')) !== null && _a !== void 0 ? _a : 1;
|
|
161
|
+
if (format !== WALK_FORMAT) {
|
|
162
|
+
throw formatError(format);
|
|
163
|
+
}
|
|
164
|
+
let header;
|
|
165
|
+
const tables = new Map();
|
|
166
|
+
// a line whose first word starts with # and is not #header is a later
|
|
167
|
+
// addition
|
|
168
|
+
for (const line of rest) {
|
|
169
|
+
const [name = '', offset, length, ...more] = line.split('\t');
|
|
170
|
+
const range = { offset: Number(offset), length: Number(length) };
|
|
171
|
+
const known = name === '#header' || !name.startsWith('#');
|
|
172
|
+
if (known &&
|
|
173
|
+
(more.length > 0 ||
|
|
174
|
+
!Number.isSafeInteger(range.offset) ||
|
|
175
|
+
!Number.isSafeInteger(range.length))) {
|
|
176
|
+
throw new WalkFormatError(`A walk file directory line holds no byte range, or more than one: ${line}`);
|
|
177
|
+
}
|
|
178
|
+
if (name === '#header') {
|
|
179
|
+
header = range;
|
|
180
|
+
}
|
|
181
|
+
else if (known) {
|
|
182
|
+
tables.set(name, range);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (!header) {
|
|
186
|
+
throw new WalkFormatError('The walk file directory has no #header line');
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
walks: fields.filter(f => !f.startsWith('bytes:i:')),
|
|
190
|
+
bytes: integerTag(fields, 'bytes'),
|
|
191
|
+
header,
|
|
192
|
+
tables,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
// A read shared by every caller, tried again by the next caller once it fails
|
|
196
|
+
export function once(read) {
|
|
197
|
+
let held;
|
|
198
|
+
return () => {
|
|
199
|
+
if (!held) {
|
|
200
|
+
const started = read();
|
|
201
|
+
started.catch(() => {
|
|
202
|
+
if (held === started) {
|
|
203
|
+
held = undefined;
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
held = started;
|
|
207
|
+
}
|
|
208
|
+
return held;
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
// Settles like `promise` unless `signal` aborts first
|
|
212
|
+
export function raceAbort(promise, signal) {
|
|
213
|
+
return signal
|
|
214
|
+
? new Promise((resolve, reject) => {
|
|
215
|
+
signal.throwIfAborted();
|
|
216
|
+
const onAbort = () => {
|
|
217
|
+
reject(signal.reason instanceof Error
|
|
218
|
+
? signal.reason
|
|
219
|
+
: new Error(String(signal.reason)));
|
|
220
|
+
};
|
|
221
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
222
|
+
promise.then(resolve, reject).finally(() => {
|
|
223
|
+
signal.removeEventListener('abort', onAbort);
|
|
224
|
+
});
|
|
225
|
+
})
|
|
226
|
+
: promise;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* A format 8 walk file read by byte ranges: the directory and the header
|
|
230
|
+
* once, each sequence's table once, and units as asked, the ranges of units
|
|
231
|
+
* that touch merged into one single-range read
|
|
232
|
+
*/
|
|
233
|
+
export class WalkContainer {
|
|
234
|
+
constructor(data, directory) {
|
|
235
|
+
this.tableReads = new Map();
|
|
236
|
+
this.readDirectory = once(async () => parseDirectory(await this.directory.readFile('utf8')));
|
|
237
|
+
// The header unit's lines, once the directory and the data agree
|
|
238
|
+
this.header = once(async () => {
|
|
239
|
+
var _a, _b;
|
|
240
|
+
const directory = await this.readDirectory();
|
|
241
|
+
const { offset, length } = directory.header;
|
|
242
|
+
const header = await unitLines(await this.data.read(length, offset), 'The header unit');
|
|
243
|
+
const { size } = await this.data.stat();
|
|
244
|
+
if (header[0] !== directory.walks.join('\t')) {
|
|
245
|
+
throw new WalkFormatError(`The walk file's directory does not match its data: the directory states ${directory.walks.join(' ')}, the data ${(_a = header[0]) !== null && _a !== void 0 ? _a : 'no #walks line'}`);
|
|
246
|
+
}
|
|
247
|
+
else if (size > 0 && directory.bytes !== size) {
|
|
248
|
+
throw new WalkFormatError(`The walk file's directory does not match its data: the directory states ${(_b = directory.bytes) !== null && _b !== void 0 ? _b : 'no'} bytes, the data holds ${size}`);
|
|
249
|
+
}
|
|
250
|
+
return header;
|
|
251
|
+
});
|
|
252
|
+
this.data = data;
|
|
253
|
+
this.directory = directory;
|
|
254
|
+
}
|
|
255
|
+
// The reference sequences, in the directory's order, once the directory
|
|
256
|
+
// and the data agree
|
|
257
|
+
async sequences() {
|
|
258
|
+
await this.header();
|
|
259
|
+
return [...(await this.readDirectory()).tables.keys()];
|
|
260
|
+
}
|
|
261
|
+
// A sequence's units in file order, once the directory and the data
|
|
262
|
+
// agree; none for a sequence the file lacks
|
|
263
|
+
async units(refName) {
|
|
264
|
+
let read = this.tableReads.get(refName);
|
|
265
|
+
if (!read) {
|
|
266
|
+
read = once(async () => {
|
|
267
|
+
const directory = await this.readDirectory();
|
|
268
|
+
const range = directory.tables.get(refName);
|
|
269
|
+
const [checked, table] = await Promise.allSettled([
|
|
270
|
+
this.header(),
|
|
271
|
+
range
|
|
272
|
+
? this.table(refName, range, endOfTableBefore(directory, range))
|
|
273
|
+
: [],
|
|
274
|
+
]);
|
|
275
|
+
if (checked.status === 'rejected') {
|
|
276
|
+
throw checked.reason;
|
|
277
|
+
}
|
|
278
|
+
else if (table.status === 'rejected') {
|
|
279
|
+
throw table.reason;
|
|
280
|
+
}
|
|
281
|
+
return table.value;
|
|
282
|
+
});
|
|
283
|
+
this.tableReads.set(refName, read);
|
|
284
|
+
}
|
|
285
|
+
return read();
|
|
286
|
+
}
|
|
287
|
+
async table(refName, { offset, length }, unitsStart) {
|
|
288
|
+
const [head = '', ...entries] = await unitLines(await this.data.read(length, offset), `${refName}'s table`);
|
|
289
|
+
if (!isTableHead(head, refName)) {
|
|
290
|
+
throw new WalkFormatError(`${refName}'s table, at byte ${offset} of the walk file, starts ${JSON.stringify(head.slice(0, 80))}`);
|
|
291
|
+
}
|
|
292
|
+
const units = entries.map(line => {
|
|
293
|
+
const [tag, seq = '', start, end, kind = '', name = '', at, size, ...more] = line.split('\t');
|
|
294
|
+
const unit = {
|
|
295
|
+
refName: seq,
|
|
296
|
+
start: Number(start),
|
|
297
|
+
end: Number(end),
|
|
298
|
+
kind,
|
|
299
|
+
name,
|
|
300
|
+
offset: Number(at),
|
|
301
|
+
length: Number(size),
|
|
302
|
+
};
|
|
303
|
+
if (tag !== '#entry' ||
|
|
304
|
+
more.length > 0 ||
|
|
305
|
+
seq !== refName ||
|
|
306
|
+
![unit.start, unit.end, unit.offset, unit.length].every(Number.isSafeInteger)) {
|
|
307
|
+
throw new WalkFormatError(`${refName}'s unit table holds ${line}`);
|
|
308
|
+
}
|
|
309
|
+
return unit;
|
|
310
|
+
});
|
|
311
|
+
let covered = unitsStart;
|
|
312
|
+
for (const unit of units) {
|
|
313
|
+
if (unit.offset !== covered) {
|
|
314
|
+
throw new WalkFormatError(`${refName}'s unit table lists its ${unit.kind} ${unit.name} at byte ${unit.offset} of the walk file, where the bytes its entries cover so far end at byte ${covered}: a table's entries leave no gap and do not overlap`);
|
|
315
|
+
}
|
|
316
|
+
covered = unit.offset + unit.length;
|
|
317
|
+
}
|
|
318
|
+
if (covered !== offset) {
|
|
319
|
+
throw new WalkFormatError(`${refName}'s unit table is at byte ${offset} of the walk file, where the bytes its entries cover end at byte ${covered}: a table's entries leave no gap and do not overlap`);
|
|
320
|
+
}
|
|
321
|
+
return units;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The lines of each unit, without its `#unit` line, in the order given.
|
|
325
|
+
* Units whose byte ranges touch are fetched by one read, and each is
|
|
326
|
+
* gunzipped on its own.
|
|
327
|
+
*/
|
|
328
|
+
async read(units, signal) {
|
|
329
|
+
const byOffset = [...new Map(units.map(u => [u.offset, u])).values()]
|
|
330
|
+
.filter(u => u.length > 0)
|
|
331
|
+
.sort((a, b) => a.offset - b.offset);
|
|
332
|
+
const runs = [];
|
|
333
|
+
for (const unit of byOffset) {
|
|
334
|
+
const run = runs.at(-1);
|
|
335
|
+
const last = run === null || run === void 0 ? void 0 : run.at(-1);
|
|
336
|
+
if (run && last && last.offset + last.length === unit.offset) {
|
|
337
|
+
run.push(unit);
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
runs.push([unit]);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const held = new Map();
|
|
344
|
+
await Promise.all(runs.map(async (run) => {
|
|
345
|
+
const first = run[0];
|
|
346
|
+
const last = run.at(-1);
|
|
347
|
+
const length = last.offset + last.length - first.offset;
|
|
348
|
+
const bytes = await this.data.read(length, first.offset, { signal });
|
|
349
|
+
for (const unit of run) {
|
|
350
|
+
const at = unit.offset - first.offset;
|
|
351
|
+
const [head, ...rest] = await unitLines(bytes.subarray(at, at + unit.length), unitName(unit));
|
|
352
|
+
checkUnit(unit, head, rest);
|
|
353
|
+
held.set(unit.offset, rest);
|
|
354
|
+
}
|
|
355
|
+
}));
|
|
356
|
+
return units.map(unit => { var _a; return (_a = held.get(unit.offset)) !== null && _a !== void 0 ? _a : []; });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { RefPath } from './walkChunk.ts';
|
|
2
|
+
import type { AbridgedPiece, PieceBlocks, Route } from './walkChunk.ts';
|
|
3
|
+
import type { DepartureRecord } from './walkDeparture.ts';
|
|
4
|
+
export type CountReason = 'unloaded' | 'unattached' | 'revisited' | 'elsewhere';
|
|
5
|
+
/**
|
|
6
|
+
* Why a count may fall short, one bit each; a count with none is exact.
|
|
7
|
+
* `unloaded`: a haplotype over the window not counted there has a run off the
|
|
8
|
+
* reference the call did not load. `unattached`: one has own sequence of a
|
|
9
|
+
* departure over the window the call did not read. `revisited`: the reference
|
|
10
|
+
* passes the node more than once, so a step on it can lie at another pass.
|
|
11
|
+
* `elsewhere`: steps on it can lie in rows filed away from the window, as
|
|
12
|
+
* every step can in a file that does not state `revisited:i:0`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const COUNT_REASONS: Readonly<Record<CountReason, number>>;
|
|
15
|
+
export declare const countReasons: (bits: number) => CountReason[];
|
|
16
|
+
export interface CountColumns {
|
|
17
|
+
haplotypes: Int32Array;
|
|
18
|
+
passes: Float64Array;
|
|
19
|
+
haplotypeReasons: Uint8Array;
|
|
20
|
+
passReasons: Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* How many of a walk file's haplotypes step each node and link over a
|
|
24
|
+
* window, and how often. `nodes.ids` ascend; links are handle pairs, each
|
|
25
|
+
* `2 * id + reversed`, written the one way round a cut writes them, and
|
|
26
|
+
* ascend by `from`, then `to`. Each count states in its reasons column why it
|
|
27
|
+
* may fall short, 0 where it is exact
|
|
28
|
+
*/
|
|
29
|
+
export interface WalkCounts {
|
|
30
|
+
haplotypes: {
|
|
31
|
+
file: number;
|
|
32
|
+
window: number;
|
|
33
|
+
unloaded: string[];
|
|
34
|
+
unattached: string[];
|
|
35
|
+
};
|
|
36
|
+
nodes: CountColumns & {
|
|
37
|
+
ids: Int32Array;
|
|
38
|
+
};
|
|
39
|
+
links: CountColumns & {
|
|
40
|
+
from: Uint32Array;
|
|
41
|
+
to: Uint32Array;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
interface RouteCounts {
|
|
45
|
+
nodes: Int32Array;
|
|
46
|
+
links: Int32Array;
|
|
47
|
+
bp: number;
|
|
48
|
+
}
|
|
49
|
+
export interface CountWalk {
|
|
50
|
+
name: string;
|
|
51
|
+
fragStart: number;
|
|
52
|
+
piece: number;
|
|
53
|
+
hapOffset: number;
|
|
54
|
+
n: number;
|
|
55
|
+
interior: number;
|
|
56
|
+
span: {
|
|
57
|
+
start: number;
|
|
58
|
+
end: number;
|
|
59
|
+
};
|
|
60
|
+
start: number;
|
|
61
|
+
startCopy: number;
|
|
62
|
+
choices: number[];
|
|
63
|
+
}
|
|
64
|
+
interface CountPath {
|
|
65
|
+
path: RefPath;
|
|
66
|
+
nodes: Int32Array;
|
|
67
|
+
links: Int32Array;
|
|
68
|
+
lengths: Map<number, number>;
|
|
69
|
+
}
|
|
70
|
+
export interface CountUnit {
|
|
71
|
+
start: number;
|
|
72
|
+
r: CountPath;
|
|
73
|
+
references: {
|
|
74
|
+
name: string;
|
|
75
|
+
fragStart: number;
|
|
76
|
+
piece: number;
|
|
77
|
+
hapOffset: number;
|
|
78
|
+
from: number;
|
|
79
|
+
to: number;
|
|
80
|
+
}[];
|
|
81
|
+
routes: Route[];
|
|
82
|
+
counted: RouteCounts[];
|
|
83
|
+
walks: CountWalk[];
|
|
84
|
+
abridged: AbridgedPiece[];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Counts, as it reads rows, the haplotypes stepping each node and link and
|
|
88
|
+
* the steps on each, keeping no step: per node and link a bitset of
|
|
89
|
+
* haplotypes and a pass count, and per tile its R's nodes and links once
|
|
90
|
+
*/
|
|
91
|
+
export declare class WalkTally {
|
|
92
|
+
private readonly haplotypeIndex;
|
|
93
|
+
private readonly names;
|
|
94
|
+
private readonly stepsFiledAtPass;
|
|
95
|
+
private readonly nodes;
|
|
96
|
+
private readonly links;
|
|
97
|
+
private readonly nodeAt;
|
|
98
|
+
private readonly nodeIds;
|
|
99
|
+
private readonly refPasses;
|
|
100
|
+
private readonly drawn;
|
|
101
|
+
private readonly linkAt;
|
|
102
|
+
private readonly linkEnds;
|
|
103
|
+
private readonly window;
|
|
104
|
+
private readonly unloaded;
|
|
105
|
+
private readonly unattached;
|
|
106
|
+
private readonly pieces;
|
|
107
|
+
private readonly stretches;
|
|
108
|
+
constructor(haplotypes: string[], stepsFiledAtPass: boolean);
|
|
109
|
+
haplotype(name: string): number;
|
|
110
|
+
private node;
|
|
111
|
+
private link;
|
|
112
|
+
private pathLink;
|
|
113
|
+
private routesOf;
|
|
114
|
+
private walksOf;
|
|
115
|
+
private lengthsOf;
|
|
116
|
+
/**
|
|
117
|
+
* A tile's rows, split on tabs, ready to count. A tile of the window
|
|
118
|
+
* states how many times the reference passes each node of its R, which
|
|
119
|
+
* makes a count on that node exact where its pass lies on the drawn
|
|
120
|
+
* stretch and the file states `revisited:i:0`; a tile fetched to attach
|
|
121
|
+
* states nothing of the kind
|
|
122
|
+
*/
|
|
123
|
+
tile(rows: string[][], tileStart: number, ofWindow: boolean): CountUnit;
|
|
124
|
+
detail(rows: string[][], tile: CountUnit): CountUnit;
|
|
125
|
+
placeReference(unit: CountUnit, drawn: {
|
|
126
|
+
start: number;
|
|
127
|
+
end: number;
|
|
128
|
+
}): void;
|
|
129
|
+
private piece;
|
|
130
|
+
private stretch;
|
|
131
|
+
private mark;
|
|
132
|
+
countReference(unit: CountUnit, { name, fragStart, piece, from, to }: CountUnit['references'][number]): void;
|
|
133
|
+
/**
|
|
134
|
+
* A walk row's steps as the row decodes them, counted on its haplotype
|
|
135
|
+
* when `counting`, and its bp on its contig either way
|
|
136
|
+
*/
|
|
137
|
+
walk(unit: CountUnit, walk: CountWalk, counting: boolean): number;
|
|
138
|
+
/**
|
|
139
|
+
* An abridged piece the call does not load, from its blocks row: its steps
|
|
140
|
+
* on R exactly, counted on its haplotype, and its runs off R as bp, which
|
|
141
|
+
* leave the haplotype's counts off R lower bounds
|
|
142
|
+
*/
|
|
143
|
+
blocks(unit: CountUnit, piece: AbridgedPiece, blocks: PieceBlocks): void;
|
|
144
|
+
unread(piece: AbridgedPiece): void;
|
|
145
|
+
/**
|
|
146
|
+
* Every departure record over the window: its haplotype is over the
|
|
147
|
+
* window, and one whose own sequence, less what lies between W lines, the
|
|
148
|
+
* counted pieces do not cover leaves that haplotype's counts off R lower
|
|
149
|
+
* bounds
|
|
150
|
+
*/
|
|
151
|
+
departures(records: readonly Readonly<DepartureRecord>[]): void;
|
|
152
|
+
joinPieces(): void;
|
|
153
|
+
private nodeReasons;
|
|
154
|
+
private columns;
|
|
155
|
+
private namesIn;
|
|
156
|
+
result(): WalkCounts;
|
|
157
|
+
}
|
|
158
|
+
export {};
|