@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,122 @@
|
|
|
1
|
+
import { WalkRowError } from "./walkChunk.js";
|
|
2
|
+
// The most bp of own sequence a departure gets its steps attached for
|
|
3
|
+
export const ATTACH_BP = 65536;
|
|
4
|
+
// A position on `refName` with the holes before it removed, so that steps
|
|
5
|
+
// either side of a hole compare as adjacent
|
|
6
|
+
export function squeezer(holes, refName) {
|
|
7
|
+
const spans = holes
|
|
8
|
+
.filter(h => h.refName === refName)
|
|
9
|
+
.sort((a, b) => a.start - b.start);
|
|
10
|
+
return spans.length === 0
|
|
11
|
+
? (bp) => bp
|
|
12
|
+
: (bp) => {
|
|
13
|
+
let out = bp;
|
|
14
|
+
for (const { start, end } of spans) {
|
|
15
|
+
if (end > bp) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
out -= end - start;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
// Whether two consecutive steps on S follow S at their passes, and so depart
|
|
24
|
+
// nowhere: no sequence of the path's own between them, the same strand and
|
|
25
|
+
// one's breakpoint the other's, holes removed
|
|
26
|
+
export function adjacentSteps(d, squeeze) {
|
|
27
|
+
return (d.ownBp === 0 &&
|
|
28
|
+
d.leaveReversed === d.rejoinReversed &&
|
|
29
|
+
squeeze(d.leavePos) === squeeze(d.rejoinPos));
|
|
30
|
+
}
|
|
31
|
+
export function departureKind(d, squeeze = bp => bp) {
|
|
32
|
+
if (d.leaveReversed !== d.rejoinReversed) {
|
|
33
|
+
return 'switch';
|
|
34
|
+
}
|
|
35
|
+
const leave = squeeze(d.leavePos);
|
|
36
|
+
const rejoin = squeeze(d.rejoinPos);
|
|
37
|
+
return leave === rejoin
|
|
38
|
+
? 'adjacent'
|
|
39
|
+
: (d.leaveReversed ? rejoin > leave : rejoin < leave)
|
|
40
|
+
? 'back'
|
|
41
|
+
: 'forward';
|
|
42
|
+
}
|
|
43
|
+
// The bases of S a departure covers, from the base beside one breakpoint to
|
|
44
|
+
// the base beside the other, [lo, hi), as its record is filed
|
|
45
|
+
export function departureStretch(d) {
|
|
46
|
+
const leaveBase = d.leaveReversed ? d.leavePos : d.leavePos - 1;
|
|
47
|
+
const rejoinBase = d.rejoinReversed ? d.rejoinPos - 1 : d.rejoinPos;
|
|
48
|
+
const lo = Math.max(0, Math.min(leaveBase, rejoinBase));
|
|
49
|
+
return [lo, Math.max(lo, leaveBase, rejoinBase) + 1];
|
|
50
|
+
}
|
|
51
|
+
// Whether a departure leaves or rejoins on `span`: the base of its leave step
|
|
52
|
+
// or of its rejoin step beside the breakpoint lies there
|
|
53
|
+
export function leavesOrRejoinsOn(d, span) {
|
|
54
|
+
const [lo, hi] = departureStretch(d);
|
|
55
|
+
const on = (base) => base >= span.start && base < span.end;
|
|
56
|
+
return on(lo) || on(hi - 1);
|
|
57
|
+
}
|
|
58
|
+
// Whether a cut over `drawn` attaches a departure's own steps
|
|
59
|
+
export const attachesOwnSteps = (d, drawn, attachBp) => d.ownBp <= attachBp && leavesOrRejoinsOn(d, drawn);
|
|
60
|
+
/**
|
|
61
|
+
* Whether a departure's place on the reference is the writer's choice: an
|
|
62
|
+
* end lies on a node the reference passes more than once, so another pass
|
|
63
|
+
* would put it elsewhere or make it none, as where a path follows one copy
|
|
64
|
+
* of a collapsed repeat and the placement hops between copies
|
|
65
|
+
*/
|
|
66
|
+
export function placementChoice(d) {
|
|
67
|
+
return d.leavePasses > 1 || d.rejoinPasses > 1;
|
|
68
|
+
}
|
|
69
|
+
const count = (field, row, least = 0) => {
|
|
70
|
+
const n = Number(field);
|
|
71
|
+
if (field === undefined ||
|
|
72
|
+
field === '' ||
|
|
73
|
+
!Number.isInteger(n) ||
|
|
74
|
+
n < least) {
|
|
75
|
+
throw new WalkRowError(`not a departure row: ${row.join('\t')}`);
|
|
76
|
+
}
|
|
77
|
+
return n;
|
|
78
|
+
};
|
|
79
|
+
const strand = (field, row) => {
|
|
80
|
+
if (field !== '+' && field !== '-') {
|
|
81
|
+
throw new WalkRowError(`not a departure row: ${row.join('\t')}`);
|
|
82
|
+
}
|
|
83
|
+
return field === '-';
|
|
84
|
+
};
|
|
85
|
+
// A record row's columns, `departure path hapStart ownBp missingBp leavePos
|
|
86
|
+
// leaveStrand leavePasses rejoinPos rejoinStrand rejoinPasses`, from its kind
|
|
87
|
+
// at `from`; a row that is not one throws
|
|
88
|
+
export function parseDepartureRow(cols, from = 3) {
|
|
89
|
+
const n = (k) => count(cols[from + k], cols);
|
|
90
|
+
const passes = (k) => count(cols[from + k], cols, 1);
|
|
91
|
+
return {
|
|
92
|
+
name: cols[from + 1],
|
|
93
|
+
hapStart: n(2),
|
|
94
|
+
ownBp: n(3),
|
|
95
|
+
missingBp: n(4),
|
|
96
|
+
leavePos: n(5),
|
|
97
|
+
leaveReversed: strand(cols[from + 6], cols),
|
|
98
|
+
leavePasses: passes(7),
|
|
99
|
+
rejoinPos: n(8),
|
|
100
|
+
rejoinReversed: strand(cols[from + 9], cols),
|
|
101
|
+
rejoinPasses: passes(10),
|
|
102
|
+
fromSteps: false,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// A record row's `ownTiles`, after `rejoinPasses`: the nominal starts of
|
|
106
|
+
// the tiles holding its own steps, `.` for none
|
|
107
|
+
export function departureOwnTiles(cols, from = 3) {
|
|
108
|
+
const field = cols[from + 11];
|
|
109
|
+
const starts = field === '.' || field === undefined ? [] : field.split(',');
|
|
110
|
+
return starts.map(start => {
|
|
111
|
+
const n = Number(start);
|
|
112
|
+
if (start === '' || !Number.isSafeInteger(n) || n < 0) {
|
|
113
|
+
throw new WalkRowError(`a departure record's ownTiles is not a list of tile starts: ${cols.join('\t')}`);
|
|
114
|
+
}
|
|
115
|
+
return n;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// Every record's rows repeat the same columns after the block, so this keys
|
|
119
|
+
// a row's record
|
|
120
|
+
export function departureRowKey(cols, from = 3) {
|
|
121
|
+
return cols.slice(from).join('\t');
|
|
122
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { WalkDeparture } from './walkDeparture.ts';
|
|
2
|
+
import type { WalkFragment, WalkNode } from './walkRows.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Where a haplotype leaves the reference path R and rejoins it: R's bases
|
|
5
|
+
* `refStart`–`refEnd` against `altLength` of its own. `invertedBp` counts the
|
|
6
|
+
* steps along R against its orientation. A step back along R walks a tandem
|
|
7
|
+
* copy again: the departure is then a point at the junction, `repeatedBp`
|
|
8
|
+
* counts the bp walked again, and `copied` spans them on R.
|
|
9
|
+
*/
|
|
10
|
+
export interface Departure {
|
|
11
|
+
refStart: number;
|
|
12
|
+
refEnd: number;
|
|
13
|
+
altLength: number;
|
|
14
|
+
invertedBp: number;
|
|
15
|
+
repeatedBp: number;
|
|
16
|
+
copied?: [number, number];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A `sample#haplotype`'s departures, or a sample's where its paths' names
|
|
20
|
+
* have no haplotype field, by refStart. `covered` spans the R
|
|
21
|
+
* exactly one of its paths reaches, each span within one path.
|
|
22
|
+
*/
|
|
23
|
+
export interface HaplotypeDepartures {
|
|
24
|
+
haplotype: string;
|
|
25
|
+
covered: [number, number][];
|
|
26
|
+
departures: Departure[];
|
|
27
|
+
}
|
|
28
|
+
export interface DepartureSource {
|
|
29
|
+
fragments: WalkFragment[];
|
|
30
|
+
departures: WalkDeparture[];
|
|
31
|
+
nodes: Map<number, WalkNode>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Each haplotype's departures from R, `refName`, read off a cut: its
|
|
35
|
+
* departures, every one over the drawn stretch, give where each path leaves
|
|
36
|
+
* R and rejoins it and how much of its own it runs between, and its
|
|
37
|
+
* fragments' first and last steps give where it is on R otherwise. A
|
|
38
|
+
* fragment with the departures at its ends is a chain, as is a departure
|
|
39
|
+
* touching no fragment, such as one jumping over the window; each reads in
|
|
40
|
+
* its own orientation, turned round where it leaves R behind where it
|
|
41
|
+
* entered, and a stretch of R walked against it reads as inverted, a jump
|
|
42
|
+
* back as a tandem copy, and sequence between a contig's W lines as the
|
|
43
|
+
* path's own. R that two chains of a haplotype reach, such as a contig's
|
|
44
|
+
* second visit to the window, has no call. A piece the cut did not load
|
|
45
|
+
* reads as any other, from its steps on R and the bp of its own between
|
|
46
|
+
* them. The walks on R's own haplotype are left out.
|
|
47
|
+
*/
|
|
48
|
+
export declare function walkDepartures({ fragments, departures, nodes }: DepartureSource, refName: string): HaplotypeDepartures[];
|
|
49
|
+
export declare const FORMS_STEP_BUDGET = 16000000;
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { panSNHaplotypeOrSample } from "./util.js";
|
|
2
|
+
const onR = (start) => !Number.isNaN(start);
|
|
3
|
+
/**
|
|
4
|
+
* Each haplotype's departures from R, `refName`, read off a cut: its
|
|
5
|
+
* departures, every one over the drawn stretch, give where each path leaves
|
|
6
|
+
* R and rejoins it and how much of its own it runs between, and its
|
|
7
|
+
* fragments' first and last steps give where it is on R otherwise. A
|
|
8
|
+
* fragment with the departures at its ends is a chain, as is a departure
|
|
9
|
+
* touching no fragment, such as one jumping over the window; each reads in
|
|
10
|
+
* its own orientation, turned round where it leaves R behind where it
|
|
11
|
+
* entered, and a stretch of R walked against it reads as inverted, a jump
|
|
12
|
+
* back as a tandem copy, and sequence between a contig's W lines as the
|
|
13
|
+
* path's own. R that two chains of a haplotype reach, such as a contig's
|
|
14
|
+
* second visit to the window, has no call. A piece the cut did not load
|
|
15
|
+
* reads as any other, from its steps on R and the bp of its own between
|
|
16
|
+
* them. The walks on R's own haplotype are left out.
|
|
17
|
+
*/
|
|
18
|
+
export function walkDepartures({ fragments, departures, nodes }, refName) {
|
|
19
|
+
const length = (id) => {
|
|
20
|
+
const node = nodes.get(id);
|
|
21
|
+
return node ? node.end - node.start : 0;
|
|
22
|
+
};
|
|
23
|
+
const reference = panSNHaplotypeOrSample(refName);
|
|
24
|
+
const byPath = new Map();
|
|
25
|
+
const add = (name, item) => {
|
|
26
|
+
const items = byPath.get(name);
|
|
27
|
+
if (items) {
|
|
28
|
+
items.push(item);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
byPath.set(name, [item]);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
for (const f of fragments) {
|
|
35
|
+
add(f.name, {
|
|
36
|
+
hap: [f.hapStart - f.trimmedBefore, f.hapEnd + f.trimmedAfter],
|
|
37
|
+
fragment: f,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
for (const d of departures) {
|
|
41
|
+
add(d.name, { hap: [d.hapStart, d.hapStart + d.ownBp], departure: d });
|
|
42
|
+
}
|
|
43
|
+
const byHaplotype = new Map();
|
|
44
|
+
for (const [name, items] of byPath) {
|
|
45
|
+
const haplotype = panSNHaplotypeOrSample(name);
|
|
46
|
+
if (haplotype === reference || name === refName) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
let h = byHaplotype.get(haplotype);
|
|
50
|
+
if (!h) {
|
|
51
|
+
h = { haplotype, covered: [], departures: [] };
|
|
52
|
+
byHaplotype.set(haplotype, h);
|
|
53
|
+
}
|
|
54
|
+
for (const chain of pathChains(items, length)) {
|
|
55
|
+
readPath(chain, h);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const out = [...byHaplotype.values()];
|
|
59
|
+
for (const h of out) {
|
|
60
|
+
h.departures.sort((a, b) => a.refStart - b.refStart || a.refEnd - b.refEnd);
|
|
61
|
+
h.covered = coveredOnce(h.covered);
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A path's chains, each its events in the path's order: a fragment with the
|
|
67
|
+
* departures leaving its last step, rejoining its first or lying inside it,
|
|
68
|
+
* fragments that abut, and a departure touching no fragment on its own. What
|
|
69
|
+
* the read did not see lies between chains, so each reads on its own, and R
|
|
70
|
+
* that two chains reach has no call.
|
|
71
|
+
*/
|
|
72
|
+
function pathChains(items, length) {
|
|
73
|
+
// items chain where one's contig stretch touches another's: a departure's
|
|
74
|
+
// leave at a fragment's last step or inside it, its rejoin at the first,
|
|
75
|
+
// or two fragments abutting. In start order, an item touches the chain so
|
|
76
|
+
// far when it starts no later than the chain's furthest end. A fragment's
|
|
77
|
+
// stretch holds the steps the cut trimmed, so a record's rejoin chains to
|
|
78
|
+
// a piece whose first steps lie outside the drawn stretch
|
|
79
|
+
const all = items
|
|
80
|
+
.map(item => ({ item, edges: item.hap }))
|
|
81
|
+
.sort((a, b) => a.edges[0] - b.edges[0]);
|
|
82
|
+
const chains = [];
|
|
83
|
+
let chainEnd = -Infinity;
|
|
84
|
+
for (const { item, edges: [start, end], } of all) {
|
|
85
|
+
const chain = chains.at(-1);
|
|
86
|
+
if (chain && start <= chainEnd) {
|
|
87
|
+
chain.push(item);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
chains.push([item]);
|
|
91
|
+
}
|
|
92
|
+
chainEnd = Math.max(chainEnd, end);
|
|
93
|
+
}
|
|
94
|
+
return chains.map(chain => chainEvents(chain, length));
|
|
95
|
+
}
|
|
96
|
+
const on = (from, to, reversed) => ({
|
|
97
|
+
kind: 'on',
|
|
98
|
+
lo: Math.min(from, to),
|
|
99
|
+
hi: Math.max(from, to),
|
|
100
|
+
reversed,
|
|
101
|
+
});
|
|
102
|
+
function chainEvents(items, length) {
|
|
103
|
+
var _a;
|
|
104
|
+
const anchors = [];
|
|
105
|
+
for (const item of items) {
|
|
106
|
+
if ('fragment' in item) {
|
|
107
|
+
const f = item.fragment;
|
|
108
|
+
const n = f.ids.length;
|
|
109
|
+
const sa = f.refStarts[0];
|
|
110
|
+
const sb = f.refStarts[n - 1];
|
|
111
|
+
const ra = f.against[0] === 1;
|
|
112
|
+
const rb = f.against[n - 1] === 1;
|
|
113
|
+
if (onR(sa)) {
|
|
114
|
+
const pos = ra ? sa + length(f.ids[0]) : sa;
|
|
115
|
+
anchors.push({ hap: f.hapStart, order: 1, pos, reversed: ra });
|
|
116
|
+
}
|
|
117
|
+
if (onR(sb)) {
|
|
118
|
+
const pos = rb ? sb : sb + length(f.ids[n - 1]);
|
|
119
|
+
anchors.push({ hap: f.hapEnd, order: 0, pos, reversed: rb, exit: true });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const d = item.departure;
|
|
124
|
+
anchors.push({
|
|
125
|
+
hap: d.hapStart,
|
|
126
|
+
order: 0,
|
|
127
|
+
pos: d.leavePos,
|
|
128
|
+
reversed: d.leaveReversed,
|
|
129
|
+
own: d.ownBp,
|
|
130
|
+
}, {
|
|
131
|
+
hap: d.hapStart + d.ownBp,
|
|
132
|
+
order: 1,
|
|
133
|
+
pos: d.rejoinPos,
|
|
134
|
+
reversed: d.rejoinReversed,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
anchors.sort((a, b) => a.hap - b.hap || a.order - b.order);
|
|
139
|
+
const events = [];
|
|
140
|
+
// the open stretch on R: where it began, and the furthest fragment end seen
|
|
141
|
+
// along it
|
|
142
|
+
let cur;
|
|
143
|
+
let end;
|
|
144
|
+
const close = () => {
|
|
145
|
+
if (cur) {
|
|
146
|
+
events.push(on(cur.pos, end !== null && end !== void 0 ? end : cur.pos, cur.reversed));
|
|
147
|
+
}
|
|
148
|
+
cur = undefined;
|
|
149
|
+
end = undefined;
|
|
150
|
+
};
|
|
151
|
+
for (const a of anchors) {
|
|
152
|
+
if (cur && cur.reversed !== a.reversed) {
|
|
153
|
+
close();
|
|
154
|
+
}
|
|
155
|
+
if (a.order === 1) {
|
|
156
|
+
cur !== null && cur !== void 0 ? cur : (cur = { pos: a.pos, reversed: a.reversed });
|
|
157
|
+
}
|
|
158
|
+
else if (a.exit) {
|
|
159
|
+
if (cur) {
|
|
160
|
+
end = a.pos;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
cur !== null && cur !== void 0 ? cur : (cur = { pos: a.pos, reversed: a.reversed });
|
|
165
|
+
end = a.pos;
|
|
166
|
+
close();
|
|
167
|
+
events.push({ kind: 'own', bp: (_a = a.own) !== null && _a !== void 0 ? _a : 0 });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
close();
|
|
171
|
+
return events;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Whether a chain reads R back to front. One that walks R only along it, or
|
|
175
|
+
* only against it, reads that way. One that does both is forward where it
|
|
176
|
+
* leaves R further along than it entered, so an inversion filling most of
|
|
177
|
+
* the window does not turn round a walk whose flanks run forward; the bp
|
|
178
|
+
* walked each way decide only for a chain that leaves R where it entered.
|
|
179
|
+
*/
|
|
180
|
+
function readsBackToFront(events) {
|
|
181
|
+
const stretches = events.filter(e => e.kind === 'on');
|
|
182
|
+
const first = stretches[0];
|
|
183
|
+
const last = stretches.at(-1);
|
|
184
|
+
const bp = (reversed) => stretches
|
|
185
|
+
.filter(e => e.reversed === reversed)
|
|
186
|
+
.reduce((sum, e) => sum + (e.hi - e.lo || 1), 0);
|
|
187
|
+
const along = bp(false);
|
|
188
|
+
const against = bp(true);
|
|
189
|
+
const progress = first && last
|
|
190
|
+
? (last.reversed ? last.lo : last.hi) -
|
|
191
|
+
(first.reversed ? first.hi : first.lo)
|
|
192
|
+
: 0;
|
|
193
|
+
return along === 0 || against === 0 || progress === 0
|
|
194
|
+
? against > along
|
|
195
|
+
: progress < 0;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A path's departures in the forward view: its stretches along R in order,
|
|
199
|
+
* turned round where it reads R back to front, each stretch against it or of
|
|
200
|
+
* its own between two along it making one departure.
|
|
201
|
+
*/
|
|
202
|
+
function readPath(events, h) {
|
|
203
|
+
const flip = readsBackToFront(events);
|
|
204
|
+
let first = NaN;
|
|
205
|
+
let prevEnd = NaN;
|
|
206
|
+
let reach = -Infinity;
|
|
207
|
+
let alt = 0;
|
|
208
|
+
let inverted = 0;
|
|
209
|
+
for (const e of flip ? events.toReversed() : events) {
|
|
210
|
+
if (e.kind === 'own') {
|
|
211
|
+
alt += e.bp;
|
|
212
|
+
}
|
|
213
|
+
else if (e.reversed !== flip) {
|
|
214
|
+
alt += e.hi - e.lo;
|
|
215
|
+
inverted += e.hi - e.lo;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
const start = e.lo;
|
|
219
|
+
if (Number.isNaN(prevEnd)) {
|
|
220
|
+
first = start;
|
|
221
|
+
}
|
|
222
|
+
else if (start !== prevEnd || alt > 0) {
|
|
223
|
+
const repeated = start < prevEnd ? prevEnd - start : 0;
|
|
224
|
+
h.departures.push({
|
|
225
|
+
refStart: prevEnd,
|
|
226
|
+
refEnd: repeated > 0 ? prevEnd : start,
|
|
227
|
+
altLength: alt + repeated,
|
|
228
|
+
invertedBp: inverted,
|
|
229
|
+
repeatedBp: repeated,
|
|
230
|
+
...(repeated > 0 ? { copied: [start, prevEnd] } : {}),
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
prevEnd = e.hi;
|
|
234
|
+
reach = Math.max(reach, e.hi);
|
|
235
|
+
alt = 0;
|
|
236
|
+
inverted = 0;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!Number.isNaN(first)) {
|
|
240
|
+
h.covered.push([first, reach]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* The stretches of R that exactly one of a haplotype's paths covers, each
|
|
245
|
+
* within one path. Where two paths overlap, such as a second contig over the
|
|
246
|
+
* same locus, the haplotype holds two copies of that stretch and no call;
|
|
247
|
+
* where one path ends and the next starts, no call crosses the junction.
|
|
248
|
+
*/
|
|
249
|
+
function coveredOnce(spans) {
|
|
250
|
+
const points = [...new Set(spans.flat())].sort((a, b) => a - b);
|
|
251
|
+
const out = [];
|
|
252
|
+
let lastPath = -1;
|
|
253
|
+
for (let k = 0; k + 1 < points.length; k++) {
|
|
254
|
+
const a = points[k];
|
|
255
|
+
const b = points[k + 1];
|
|
256
|
+
const paths = spans.flatMap(([s, e], i) => (s <= a && e >= b ? [i] : []));
|
|
257
|
+
const last = out.at(-1);
|
|
258
|
+
if (paths.length !== 1) {
|
|
259
|
+
lastPath = -1;
|
|
260
|
+
}
|
|
261
|
+
else if (last && paths[0] === lastPath && last[1] === a) {
|
|
262
|
+
last[1] = b;
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
out.push([a, b]);
|
|
266
|
+
lastPath = paths[0];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return out;
|
|
270
|
+
}
|
|
271
|
+
// The most steps a forms cut keeps
|
|
272
|
+
export const FORMS_STEP_BUDGET = 16000000;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { WalkGraph } from './walkRows.ts';
|
|
2
|
+
import type { PlacedNode } from './walkChunk.ts';
|
|
3
|
+
import type { WalkFilehandle, WalkUnit } from './walkContainer.ts';
|
|
4
|
+
import type { WalkDeparture } from './walkDeparture.ts';
|
|
5
|
+
import type { BudgetNames, WalkHeader } from './walkRows.ts';
|
|
6
|
+
export declare const WALK_CONTEXT = 1000;
|
|
7
|
+
export declare const WALK_BUDGETS: Readonly<{
|
|
8
|
+
stepBudget: 4000000;
|
|
9
|
+
byteBudget: 8000000;
|
|
10
|
+
departureByteBudget: 2000000;
|
|
11
|
+
attachBp: 65536;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const CUT_PRESETS: Readonly<{
|
|
14
|
+
browser: Readonly<{
|
|
15
|
+
context: 1000;
|
|
16
|
+
contained: false;
|
|
17
|
+
attachBp: 65536;
|
|
18
|
+
stepBudget: 4000000;
|
|
19
|
+
byteBudget: 8000000;
|
|
20
|
+
departureByteBudget: 2000000;
|
|
21
|
+
}>;
|
|
22
|
+
figure: Readonly<{
|
|
23
|
+
context: 1000;
|
|
24
|
+
contained: false;
|
|
25
|
+
attachBp: 65536;
|
|
26
|
+
stepBudget: number;
|
|
27
|
+
byteBudget: number;
|
|
28
|
+
departureByteBudget: number;
|
|
29
|
+
}>;
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* A window whose departure read would fetch more than its budget. Zooming in
|
|
33
|
+
* does not lower the read, since a record is filed over its whole stretch,
|
|
34
|
+
* so this is not a RegionTooLargeError and shows no zoom-in notice.
|
|
35
|
+
*/
|
|
36
|
+
export declare class DepartureReadError extends Error {
|
|
37
|
+
name: string;
|
|
38
|
+
readonly bytes: number;
|
|
39
|
+
readonly limit: number;
|
|
40
|
+
constructor(bytes: number, limit: number);
|
|
41
|
+
}
|
|
42
|
+
export declare function readInOrder<T, R>(items: T[], limit: number, signal: AbortSignal, read: (item: T) => Promise<R>, use: (item: T, result: R) => void): Promise<void>;
|
|
43
|
+
export declare function touchingRuns<T extends {
|
|
44
|
+
unit: WalkUnit;
|
|
45
|
+
}>(items: T[], maxBytes: number): T[][];
|
|
46
|
+
export interface WalkCutOptions {
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
keep?: (name: string) => boolean;
|
|
49
|
+
stepBudget?: number;
|
|
50
|
+
byteBudget?: number;
|
|
51
|
+
departureByteBudget?: number;
|
|
52
|
+
attachBp?: number;
|
|
53
|
+
context?: number;
|
|
54
|
+
links?: 'walked' | 'between' | 'none';
|
|
55
|
+
sequences?: boolean;
|
|
56
|
+
budgetNames?: BudgetNames;
|
|
57
|
+
nodeLimit?: number;
|
|
58
|
+
placeholderLimit?: number;
|
|
59
|
+
contained?: boolean;
|
|
60
|
+
expand?: string[] | ((name: string) => boolean);
|
|
61
|
+
}
|
|
62
|
+
export type WalkCountOptions = Pick<WalkCutOptions, 'signal' | 'context' | 'contained' | 'attachBp' | 'stepBudget' | 'byteBudget' | 'departureByteBudget' | 'expand' | 'budgetNames'>;
|
|
63
|
+
interface OnReference {
|
|
64
|
+
referenceSample?: string;
|
|
65
|
+
}
|
|
66
|
+
export interface CutForRangeOptions extends WalkCutOptions, OnReference {
|
|
67
|
+
}
|
|
68
|
+
export interface GraphForRangeOptions extends Omit<WalkCutOptions, 'keep'>, OnReference {
|
|
69
|
+
haplotypes?: string[];
|
|
70
|
+
}
|
|
71
|
+
export interface CountsForRangeOptions extends WalkCountOptions, OnReference {
|
|
72
|
+
}
|
|
73
|
+
export interface BytesForRangeOptions extends OnReference {
|
|
74
|
+
signal?: AbortSignal;
|
|
75
|
+
context?: number;
|
|
76
|
+
attachBp?: number;
|
|
77
|
+
departureByteBudget?: number;
|
|
78
|
+
}
|
|
79
|
+
export interface FormsForRangeOptions extends OnReference {
|
|
80
|
+
stepBudget?: number;
|
|
81
|
+
byteBudget?: number;
|
|
82
|
+
departureByteBudget?: number;
|
|
83
|
+
signal?: AbortSignal;
|
|
84
|
+
}
|
|
85
|
+
export type WalkFileHeader = WalkHeader & {
|
|
86
|
+
tile: number;
|
|
87
|
+
};
|
|
88
|
+
export declare function attachTiles(units: WalkUnit[]): (ownTiles: number[]) => WalkUnit[];
|
|
89
|
+
type OneOf<T> = {
|
|
90
|
+
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>;
|
|
91
|
+
}[keyof T];
|
|
92
|
+
type WalksSource = OneOf<{
|
|
93
|
+
walksUrl: string;
|
|
94
|
+
walksPath: string;
|
|
95
|
+
walksFilehandle: WalkFilehandle;
|
|
96
|
+
}>;
|
|
97
|
+
type IdxSource = OneOf<{
|
|
98
|
+
idxUrl: string;
|
|
99
|
+
idxPath: string;
|
|
100
|
+
idxFilehandle: WalkFilehandle;
|
|
101
|
+
}>;
|
|
102
|
+
type NoIdx = Partial<Record<keyof IdxSource, never>>;
|
|
103
|
+
/**
|
|
104
|
+
* Where the walk file and its directory are: exactly one of `walksUrl`,
|
|
105
|
+
* `walksPath` or `walksFilehandle`, and one of `idxUrl`, `idxPath` or
|
|
106
|
+
* `idxFilehandle`, which may be left out beside a URL or a path: the
|
|
107
|
+
* directory is then `<walks>.idx`, before any query string or fragment of
|
|
108
|
+
* a URL
|
|
109
|
+
*/
|
|
110
|
+
export type WalkFileOptions = (Exclude<WalksSource, {
|
|
111
|
+
walksFilehandle: WalkFilehandle;
|
|
112
|
+
}> & (IdxSource | NoIdx)) | (Extract<WalksSource, {
|
|
113
|
+
walksFilehandle: WalkFilehandle;
|
|
114
|
+
}> & IdxSource);
|
|
115
|
+
/**
|
|
116
|
+
* A `gfa-to-tabix --walks` file of walk format 8, `<prefix>.<sample>.walks.gz`
|
|
117
|
+
* and its directory `<prefix>.<sample>.walks.gz.idx`, read a window at a
|
|
118
|
+
* time. At open the directory, the header and the sequence's unit table; for
|
|
119
|
+
* a window, the tiles and departure blocks overlapping the window and its
|
|
120
|
+
* context, in one read wherever their bytes touch, each tile decoded once;
|
|
121
|
+
* the `blocks` unit of each of those tiles that abridges a walk kept and not
|
|
122
|
+
* expanded; the detail records of the pieces of the walks `expand` names
|
|
123
|
+
* that the cut builds; then every tile holding own steps the read lacks of
|
|
124
|
+
* a record within `attachBp` that leaves or rejoins on the window or its
|
|
125
|
+
* context, which give those steps alone, from the tile's rows and the
|
|
126
|
+
* records, with no `blocks` unit read. A record that only jumps over them
|
|
127
|
+
* costs no read past its line. The table gives
|
|
128
|
+
* each read's bytes before it is made; a window whose tiles are past the
|
|
129
|
+
* byte budget reads no row, and one whose attach tiles take it past reads
|
|
130
|
+
* none of them. Only the walks `keep` accepts are decoded. The step budget
|
|
131
|
+
* bounds the steps the cut keeps, and rows of the window holding
|
|
132
|
+
* READ_STEPS_PER_KEPT times as many stop the read.
|
|
133
|
+
*
|
|
134
|
+
* Every range method takes 0-based half-open coordinates and a `refName`
|
|
135
|
+
* that is a contig of the reference sample (`chr6`, on `referenceSample`,
|
|
136
|
+
* the file's first reference by default) or the file's own name for it
|
|
137
|
+
* (`GRCh38#0#chr6`), and throws MissingSequenceError for a sequence the
|
|
138
|
+
* file lacks.
|
|
139
|
+
*/
|
|
140
|
+
export declare class WalkFile {
|
|
141
|
+
private readonly container;
|
|
142
|
+
constructor(opts: WalkFileOptions);
|
|
143
|
+
private readonly readHeader;
|
|
144
|
+
private readonly readLookup;
|
|
145
|
+
getHeader(opts?: {
|
|
146
|
+
signal?: AbortSignal;
|
|
147
|
+
}): Promise<{
|
|
148
|
+
tile: number;
|
|
149
|
+
format?: number;
|
|
150
|
+
references: string[];
|
|
151
|
+
haplotypes: string[];
|
|
152
|
+
holes: import("./walkDeparture.ts").Hole[];
|
|
153
|
+
revisited?: number;
|
|
154
|
+
graph?: string;
|
|
155
|
+
leftout?: number;
|
|
156
|
+
}>;
|
|
157
|
+
getReferenceSequences(opts?: {
|
|
158
|
+
signal?: AbortSignal;
|
|
159
|
+
}): Promise<string[]>;
|
|
160
|
+
/**
|
|
161
|
+
* The walk file's sequence for `contig` on `sample`, or for `contig` as
|
|
162
|
+
* the file spells it: `chr6` on `GRCh38` is `GRCh38#0#chr6`. Undefined when
|
|
163
|
+
* the file holds neither.
|
|
164
|
+
*/
|
|
165
|
+
getSequenceName(sample: string, contig: string, opts?: {
|
|
166
|
+
signal?: AbortSignal;
|
|
167
|
+
}): Promise<string | undefined>;
|
|
168
|
+
getUnits(refName: string, opts?: {
|
|
169
|
+
signal?: AbortSignal;
|
|
170
|
+
}): Promise<WalkUnit[]>;
|
|
171
|
+
getSequenceLength(refName: string, opts?: {
|
|
172
|
+
signal?: AbortSignal;
|
|
173
|
+
}): Promise<number>;
|
|
174
|
+
private resolve;
|
|
175
|
+
/**
|
|
176
|
+
* The compressed bytes a cut of the range reads when it keeps every walk
|
|
177
|
+
* and expands none: its tiles, their blocks units, its departure blocks,
|
|
178
|
+
* which this reads as the cut does, and the tiles named for their own
|
|
179
|
+
* steps by its records within `attachBp` that leave or rejoin on the
|
|
180
|
+
* window or its context. A cut that keeps fewer walks reads no more, and
|
|
181
|
+
* one that expands walks no more but for their detail units
|
|
182
|
+
*/
|
|
183
|
+
getBytesForRange(refName: string, start: number, end: number, opts?: BytesForRangeOptions): Promise<number>;
|
|
184
|
+
private bytesFor;
|
|
185
|
+
getReferenceNodes(refName: string, start: number, end: number, opts?: {
|
|
186
|
+
referenceSample?: string;
|
|
187
|
+
signal?: AbortSignal;
|
|
188
|
+
}): Promise<(PlacedNode & {
|
|
189
|
+
id: number;
|
|
190
|
+
})[]>;
|
|
191
|
+
private referenceNodes;
|
|
192
|
+
/**
|
|
193
|
+
* The cut of the range: its graph, the nodes it keeps, each walk's
|
|
194
|
+
* fragments and departures over it and the gaps between them, and
|
|
195
|
+
* `tables()`, which makes the graph's tables of all of them
|
|
196
|
+
*/
|
|
197
|
+
getCutForRange(refName: string, start: number, end: number, opts?: CutForRangeOptions): Promise<{
|
|
198
|
+
refName: string;
|
|
199
|
+
graph: WalkGraph;
|
|
200
|
+
kept: Set<number>;
|
|
201
|
+
fragments: import("./walkRows.ts").WalkFragment[];
|
|
202
|
+
departures: WalkDeparture[];
|
|
203
|
+
readonly gaps: import("./walkGaps.ts").WalkGap[];
|
|
204
|
+
placeholders: number;
|
|
205
|
+
tables: () => import("./util.ts").GraphTables;
|
|
206
|
+
}>;
|
|
207
|
+
private cut;
|
|
208
|
+
/**
|
|
209
|
+
* How many of the file's haplotypes step each node and link over the range,
|
|
210
|
+
* and how often, read as a cut that keeps every walk reads, with the same
|
|
211
|
+
* `context`, `contained`, `attachBp`, budgets and `expand`. It counts each
|
|
212
|
+
* piece the cut builds whole, and each piece holding the own steps of a
|
|
213
|
+
* record the cut attaches, while it decodes the rows, keeping no step and
|
|
214
|
+
* building no graph. Every count states why it may fall short, or that it
|
|
215
|
+
* is exact (docs/counts.md)
|
|
216
|
+
*/
|
|
217
|
+
getCountsForRange(refName: string, start: number, end: number, opts?: CountsForRangeOptions): Promise<import("./walkCounts.ts").WalkCounts>;
|
|
218
|
+
private counts;
|
|
219
|
+
/**
|
|
220
|
+
* The graph under the range as tables: the reference sample's sequence
|
|
221
|
+
* over it, with `context` bp either side, and the walks of the haplotypes
|
|
222
|
+
* asked for, every one by default; `refName` names the sequence as the
|
|
223
|
+
* file does, and `sequences` holds the nodes' bases when asked for.
|
|
224
|
+
*/
|
|
225
|
+
getGraphForRange(refName: string, start: number, end: number, opts?: GraphForRangeOptions): Promise<{
|
|
226
|
+
tables: import("./util.ts").GraphTables;
|
|
227
|
+
refName: string;
|
|
228
|
+
sample: string;
|
|
229
|
+
sequences: Map<number, string> | undefined;
|
|
230
|
+
}>;
|
|
231
|
+
/**
|
|
232
|
+
* Every haplotype's departures over the range, for structuralForms, from
|
|
233
|
+
* one cut without links that expands no walk. A haplotype the header names
|
|
234
|
+
* with no walk in the cut is listed with nothing covered.
|
|
235
|
+
*/
|
|
236
|
+
getFormsForRange(refName: string, start: number, end: number, opts?: FormsForRangeOptions): Promise<import("./walkDepartures.ts").HaplotypeDepartures[]>;
|
|
237
|
+
}
|
|
238
|
+
export type CutForRange = Awaited<ReturnType<WalkFile['getCutForRange']>>;
|
|
239
|
+
export type GraphForRange = Awaited<ReturnType<WalkFile['getGraphForRange']>>;
|
|
240
|
+
export {};
|