@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.
@@ -0,0 +1,56 @@
1
+ import type { WalkDeparture } from './walkDeparture.ts';
2
+ import type { WalkFragment, WalkRun } from './walkRows.ts';
3
+ export type GapKind = 'steps' | 'read' | 'own' | 'unread';
4
+ export declare const GAP_KINDS: GapKind[];
5
+ /**
6
+ * One segment of a walk's stretch, by ADR 0001: `steps` the cut attached as
7
+ * own sequence, with their ids; `read`, steps the cut read and kept out;
8
+ * `own`, a record's unread middle, with its `missingBp`; `unread`, pieces
9
+ * the cut did not read, between the breakpoints on the reference it knows
10
+ * `before` and `after` them
11
+ */
12
+ export interface GapSegment {
13
+ kind: GapKind;
14
+ bp: number;
15
+ missingBp: number;
16
+ before: number;
17
+ after: number;
18
+ ids: number[];
19
+ rev: number[];
20
+ }
21
+ /**
22
+ * A stretch of a walk's contig and its segments: `edge` 0 between two
23
+ * fragments, 1 a row's start, 2 a row's end, 3 a departure outside the row;
24
+ * `fragment` the one it follows, or precedes for a row's start
25
+ */
26
+ export interface WalkGap {
27
+ name: string;
28
+ edge: 0 | 1 | 2 | 3;
29
+ start: number;
30
+ end: number;
31
+ fragment?: WalkFragment;
32
+ segments: GapSegment[];
33
+ }
34
+ export interface GapInputs {
35
+ runs: WalkRun[];
36
+ attach: WalkRun[];
37
+ fragments: WalkFragment[];
38
+ departures: WalkDeparture[];
39
+ refName: string;
40
+ before: (run: WalkRun) => Float64Array;
41
+ attached: Map<WalkRun, Set<number>>;
42
+ spans: {
43
+ run: WalkRun;
44
+ first: number;
45
+ last: number;
46
+ }[];
47
+ breakpoint: (run: WalkRun, i: number, leave: boolean) => {
48
+ pos: number;
49
+ };
50
+ }
51
+ /**
52
+ * Each walk's stretches between and past its fragments, and its departures
53
+ * outside them, split into segments that partition each stretch, as
54
+ * docs/adr/0001-a-cut-states-what-lies-between-fragments.md decides
55
+ */
56
+ export declare function walkGaps({ runs, attach, fragments, departures, refName, before, attached, breakpoint, spans, }: GapInputs): WalkGap[];
@@ -0,0 +1,345 @@
1
+ import { SKIPPED } from "./walkChunk.js";
2
+ export const GAP_KINDS = ['steps', 'read', 'own', 'unread'];
3
+ function groupByName(items, refName) {
4
+ const out = new Map();
5
+ for (const item of items) {
6
+ if (item.name !== refName) {
7
+ const list = out.get(item.name);
8
+ if (list) {
9
+ list.push(item);
10
+ }
11
+ else {
12
+ out.set(item.name, [item]);
13
+ }
14
+ }
15
+ }
16
+ return out;
17
+ }
18
+ // the first index in sorted `spans` whose end passes `at`, or touches it
19
+ function firstEnding(spans, at, touching = false) {
20
+ let lo = 0;
21
+ let hi = spans.length;
22
+ while (lo < hi) {
23
+ const mid = (lo + hi) >> 1;
24
+ const end = spans[mid].end;
25
+ if (touching ? end >= at : end > at) {
26
+ hi = mid;
27
+ }
28
+ else {
29
+ lo = mid + 1;
30
+ }
31
+ }
32
+ return lo;
33
+ }
34
+ // the first index in `spans`, sorted by start, that starts at or past `at`
35
+ function firstStarting(spans, at) {
36
+ let lo = 0;
37
+ let hi = spans.length;
38
+ while (lo < hi) {
39
+ const mid = (lo + hi) >> 1;
40
+ if (spans[mid].start >= at) {
41
+ hi = mid;
42
+ }
43
+ else {
44
+ lo = mid + 1;
45
+ }
46
+ }
47
+ return lo;
48
+ }
49
+ const runEnd = (run, bp) => run.hapOffset + bp[run.ids.length];
50
+ /**
51
+ * Each walk's stretches between and past its fragments, and its departures
52
+ * outside them, split into segments that partition each stretch, as
53
+ * docs/adr/0001-a-cut-states-what-lies-between-fragments.md decides
54
+ */
55
+ export function walkGaps({ runs, attach, fragments, departures, refName, before, attached, breakpoint, spans, }) {
56
+ var _a, _b, _c, _d, _e;
57
+ const inFragment = new Map();
58
+ for (const { run, first, last } of spans) {
59
+ inFragment.set(run, [...((_a = inFragment.get(run)) !== null && _a !== void 0 ? _a : []), [first, last]]);
60
+ }
61
+ const fragmentStep = (run, i) => {
62
+ var _a, _b;
63
+ return (_b = (_a = inFragment.get(run)) === null || _a === void 0 ? void 0 : _a.some(([first, last]) => i >= first && i <= last)) !== null && _b !== void 0 ? _b : false;
64
+ };
65
+ const runsBy = groupByName(runs, refName);
66
+ const attachBy = groupByName(attach, refName);
67
+ const fragmentsBy = groupByName(fragments, refName);
68
+ const departuresBy = groupByName(departures, refName);
69
+ const names = [
70
+ ...new Set([...fragmentsBy.keys(), ...departuresBy.keys()]),
71
+ ].sort();
72
+ const gaps = [];
73
+ for (const name of names) {
74
+ const walkRuns = [...((_b = runsBy.get(name)) !== null && _b !== void 0 ? _b : [])].sort((a, b) => a.hapOffset - b.hapOffset);
75
+ const attachSteps = [];
76
+ for (const run of (_c = attachBy.get(name)) !== null && _c !== void 0 ? _c : []) {
77
+ const marks = attached.get(run);
78
+ if (marks) {
79
+ const bp = before(run);
80
+ for (const i of marks) {
81
+ attachSteps.push({
82
+ start: run.hapOffset + bp[i],
83
+ end: run.hapOffset + bp[i + 1],
84
+ id: run.ids[i],
85
+ rev: run.rev[i],
86
+ attached: true,
87
+ });
88
+ }
89
+ }
90
+ }
91
+ const placed = ((_d = departuresBy.get(name)) !== null && _d !== void 0 ? _d : [])
92
+ .map(d => ({ d, start: d.hapStart, end: d.hapStart + d.ownBp }))
93
+ .sort((a, b) => a.start - b.start || a.end - b.end);
94
+ const walkFragments = [...((_e = fragmentsBy.get(name)) !== null && _e !== void 0 ? _e : [])].sort((a, b) => a.hapStart - b.hapStart);
95
+ // the reference steps of the walk's runs, built once a stretch needs a
96
+ // breakpoint
97
+ let anchors;
98
+ const anchorsOf = () => {
99
+ anchors !== null && anchors !== void 0 ? anchors : (anchors = walkRuns
100
+ .flatMap(run => {
101
+ const bp = before(run);
102
+ return [...run.ids.keys()]
103
+ .filter(i => !Number.isNaN(run.refStarts[i]) || run.ids[i] === SKIPPED)
104
+ .map(i => ({
105
+ run,
106
+ i,
107
+ start: run.hapOffset + bp[i],
108
+ end: run.hapOffset + bp[i + 1],
109
+ }));
110
+ })
111
+ .sort((a, b) => a.start - b.start));
112
+ return anchors;
113
+ };
114
+ const breakpointsAround = (start, end) => {
115
+ const all = anchorsOf();
116
+ const back = firstEnding(all, start) - 1;
117
+ const lastAnchor = back >= 0 ? all[back] : undefined;
118
+ const nextAnchor = all[firstStarting(all, end)];
119
+ const backPlaced = firstEnding(placed, start) - 1;
120
+ const lastPlaced = backPlaced >= 0 ? placed[backPlaced] : undefined;
121
+ const nextPlaced = placed[firstStarting(placed, end)];
122
+ const beforeFromPlaced = lastPlaced !== undefined &&
123
+ (!lastAnchor || lastPlaced.end > lastAnchor.end);
124
+ const afterFromPlaced = nextPlaced !== undefined &&
125
+ (!nextAnchor || nextPlaced.start < nextAnchor.start);
126
+ return {
127
+ before: beforeFromPlaced
128
+ ? lastPlaced.d.rejoinPos
129
+ : lastAnchor
130
+ ? breakpoint(lastAnchor.run, lastAnchor.i, true).pos
131
+ : Number.NaN,
132
+ after: afterFromPlaced
133
+ ? nextPlaced.d.leavePos
134
+ : nextAnchor
135
+ ? breakpoint(nextAnchor.run, nextAnchor.i, false).pos
136
+ : Number.NaN,
137
+ };
138
+ };
139
+ // a stretch's read steps, a 0 bp one included where it lies on either
140
+ // edge, and consecutive steps the cut did not attach as one
141
+ const stepsIn = (start, end) => {
142
+ var _a;
143
+ const out = [];
144
+ for (const run of walkRuns) {
145
+ const bp = before(run);
146
+ if (run.hapOffset <= end && runEnd(run, bp) >= start) {
147
+ const marks = attached.get(run);
148
+ let read;
149
+ let lo = 0;
150
+ let hi = run.ids.length;
151
+ while (lo < hi) {
152
+ const mid = (lo + hi) >> 1;
153
+ if (run.hapOffset + bp[mid + 1] >= start) {
154
+ hi = mid;
155
+ }
156
+ else {
157
+ lo = mid + 1;
158
+ }
159
+ }
160
+ for (let i = lo; i < run.ids.length && run.hapOffset + bp[i] <= end; i++) {
161
+ const s = run.hapOffset + bp[i];
162
+ const e = run.hapOffset + bp[i + 1];
163
+ const isAttached = (_a = marks === null || marks === void 0 ? void 0 : marks.has(i)) !== null && _a !== void 0 ? _a : false;
164
+ const inStretch = !fragmentStep(run, i) &&
165
+ ((e > start && s < end) || (s === e && isAttached));
166
+ if (inStretch && !isAttached && (read === null || read === void 0 ? void 0 : read.end) === s) {
167
+ read.end = e;
168
+ }
169
+ else if (inStretch) {
170
+ const step = {
171
+ start: s,
172
+ end: e,
173
+ id: run.ids[i],
174
+ rev: run.rev[i],
175
+ attached: isAttached,
176
+ };
177
+ out.push(step);
178
+ read = isAttached ? undefined : step;
179
+ }
180
+ else {
181
+ read = undefined;
182
+ }
183
+ }
184
+ }
185
+ }
186
+ for (const x of attachSteps) {
187
+ if ((x.end > start && x.start < end) ||
188
+ (x.start === x.end && x.start >= start && x.start <= end)) {
189
+ out.push(x);
190
+ }
191
+ }
192
+ return out.sort((a, b) => a.start - b.start || a.end - b.end);
193
+ };
194
+ // how far a row's end reaches over the departures that touch it
195
+ const reach = (start, end, forward) => {
196
+ let at = forward ? end : start;
197
+ for (const x of forward ? placed : [...placed].reverse()) {
198
+ if (forward && x.end > start && x.start <= at && x.end > at) {
199
+ at = x.end;
200
+ }
201
+ else if (!forward && x.start < end && x.end >= at && x.start < at) {
202
+ at = x.start;
203
+ }
204
+ }
205
+ return at;
206
+ };
207
+ const stretches = [];
208
+ const first = walkFragments[0];
209
+ const last = walkFragments.at(-1);
210
+ const rowStart = first
211
+ ? reach(first.hapStart - first.trimmedBefore, first.hapStart, false)
212
+ : Infinity;
213
+ const rowEnd = last
214
+ ? reach(last.hapEnd, last.hapEnd + last.trimmedAfter, true)
215
+ : -Infinity;
216
+ if (first && rowStart < first.hapStart) {
217
+ stretches.push({
218
+ name,
219
+ edge: 1,
220
+ start: rowStart,
221
+ end: first.hapStart,
222
+ fragment: first,
223
+ });
224
+ }
225
+ walkFragments.forEach((f, k) => {
226
+ const next = walkFragments[k + 1];
227
+ if (next) {
228
+ stretches.push({
229
+ name,
230
+ edge: 0,
231
+ start: f.hapEnd,
232
+ end: next.hapStart,
233
+ fragment: f,
234
+ });
235
+ }
236
+ });
237
+ if (last && rowEnd > last.hapEnd) {
238
+ stretches.push({
239
+ name,
240
+ edge: 2,
241
+ start: last.hapEnd,
242
+ end: rowEnd,
243
+ fragment: last,
244
+ });
245
+ }
246
+ for (const x of placed) {
247
+ const outside = x.end > x.start && (!first || x.end <= rowStart || x.start >= rowEnd);
248
+ if (outside) {
249
+ stretches.push({ name, edge: 3, start: x.start, end: x.end });
250
+ }
251
+ }
252
+ stretches.sort((a, b) => a.start - b.start || a.edge - b.edge);
253
+ for (const stretch of stretches) {
254
+ const { start, end } = stretch;
255
+ const inSteps = stepsIn(start, end);
256
+ const sized = inSteps.filter(x => x.end > x.start);
257
+ const zeroSteps = inSteps.filter(x => x.end === x.start && x.attached);
258
+ const from = firstEnding(placed, start, true);
259
+ const inPlaced = [];
260
+ for (let k = from; k < placed.length && placed[k].start <= end; k++) {
261
+ inPlaced.push(placed[k]);
262
+ }
263
+ const sizedPlaced = inPlaced.filter(x => x.end > start && x.start < end);
264
+ const zeroPlaced = inPlaced.filter(x => x.end === x.start && x.start > start && x.start < end);
265
+ const points = new Set([start, end]);
266
+ for (const x of [...sized, ...sizedPlaced, ...zeroPlaced]) {
267
+ for (const p of [x.start, x.end]) {
268
+ if (p > start && p < end) {
269
+ points.add(p);
270
+ }
271
+ }
272
+ }
273
+ const sorted = [...points].sort((a, b) => a - b);
274
+ const segments = [];
275
+ let lastOwner;
276
+ const add = (kind, bp, more = {}, owner) => {
277
+ var _a, _b;
278
+ const prev = segments.at(-1);
279
+ if (bp > 0 &&
280
+ (prev === null || prev === void 0 ? void 0 : prev.kind) === kind &&
281
+ prev.bp > 0 &&
282
+ (kind !== 'own' || lastOwner === owner)) {
283
+ prev.bp += bp;
284
+ prev.after = (_a = more.after) !== null && _a !== void 0 ? _a : prev.after;
285
+ for (const [j, id] of ((_b = more.ids) !== null && _b !== void 0 ? _b : []).entries()) {
286
+ prev.ids.push(id);
287
+ prev.rev.push(more.rev[j]);
288
+ }
289
+ }
290
+ else {
291
+ segments.push({
292
+ kind,
293
+ bp,
294
+ missingBp: 0,
295
+ before: Number.NaN,
296
+ after: Number.NaN,
297
+ ids: [],
298
+ rev: [],
299
+ ...more,
300
+ });
301
+ lastOwner = owner;
302
+ }
303
+ };
304
+ let p = 0;
305
+ let z = 0;
306
+ const flushZeros = (at) => {
307
+ for (; z < zeroSteps.length && zeroSteps[z].start <= at; z++) {
308
+ const x = zeroSteps[z];
309
+ add('steps', 0, { ids: [x.id], rev: [x.rev] });
310
+ }
311
+ for (const x of zeroPlaced) {
312
+ if (x.start === at) {
313
+ add('own', 0, { missingBp: x.d.missingBp }, x.d);
314
+ }
315
+ }
316
+ };
317
+ for (let k = 0; k + 1 < sorted.length; k++) {
318
+ const a = sorted[k];
319
+ const b = sorted[k + 1];
320
+ flushZeros(a);
321
+ while (p < sized.length && sized[p].end <= a) {
322
+ p++;
323
+ }
324
+ const candidate = sized[p];
325
+ const step = candidate && candidate.start <= a && candidate.end >= b
326
+ ? candidate
327
+ : undefined;
328
+ const own = sizedPlaced.find(x => x.start <= a && x.end >= b);
329
+ const bp = b - a;
330
+ if (step) {
331
+ add(step.attached ? 'steps' : 'read', bp, step.attached ? { ids: [step.id], rev: [step.rev] } : {});
332
+ }
333
+ else if (own) {
334
+ add('own', bp, { missingBp: own.d.missingBp }, own.d);
335
+ }
336
+ else {
337
+ add('unread', bp, breakpointsAround(a, b));
338
+ }
339
+ }
340
+ flushZeros(end);
341
+ gaps.push({ ...stretch, segments });
342
+ }
343
+ }
344
+ return gaps;
345
+ }