@biolibs/gel 0.0.0-stage → 1.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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Fragment sizes from cut positions, so a digest can be loaded onto a gel without pulling in an enzyme library.
3
+ *
4
+ * Positions are 0-based offsets into the sequence, counted as boundaries between bases: a cut at `n` separates the
5
+ * base at index `n - 1` from the base at index `n`. This is the half-open convention used across biolibs, so a cut
6
+ * list is directly usable as slice boundaries. (Biopython's `RestrictionType.search` returns 1-based positions;
7
+ * subtract one from each before passing them here.)
8
+ */
9
+ import { ValueError } from "@biolibs/core";
10
+ /**
11
+ * The lengths of the pieces a set of cuts divides a sequence into.
12
+ *
13
+ * A linear sequence with `n` cuts falls into `n + 1` pieces, including the two uncut ends. A circular sequence with
14
+ * `n` cuts falls into `n` pieces, one of which spans the origin; an uncut circle stays whole.
15
+ *
16
+ * @param length - Sequence length in bases.
17
+ * @param topology - `"linear"` or `"circular"`.
18
+ * @param cuts - 0-based cut positions, in any order. Duplicates count once, since two enzymes cutting the same
19
+ * bond produce one break.
20
+ * @returns The piece lengths, largest first. Cuts at 0 or at `length` on a linear sequence are the existing ends and
21
+ * add no piece; zero-length pieces are never returned.
22
+ * @throws {@link ValueError} for a negative or non-integer length, or a cut outside `[0, length]`.
23
+ * @example
24
+ * ```ts
25
+ * fragmentSizes(1000, "linear", [100, 400]); // [600, 300, 100]
26
+ * fragmentSizes(1000, "circular", [100, 400]); // [700, 300]
27
+ * fragmentSizes(1000, "circular", []); // [1000]
28
+ * ```
29
+ */
30
+ export function fragmentSizes(length, topology, cuts) {
31
+ if (!Number.isInteger(length) || length < 0)
32
+ throw new ValueError(`sequence length must be a non-negative integer, got ${length}`);
33
+ for (const cut of cuts) {
34
+ if (!Number.isInteger(cut) || cut < 0 || cut > length)
35
+ throw new ValueError(`cut position ${cut} is outside a sequence of ${length} bases`);
36
+ }
37
+ const ordered = [...new Set(cuts)].sort((a, b) => a - b);
38
+ if (length === 0)
39
+ return [];
40
+ if (ordered.length === 0)
41
+ return [length];
42
+ const sizes = [];
43
+ if (topology === "linear") {
44
+ // The two ends are pieces too: from the start to the first cut, and from the last cut to the end.
45
+ let previous = 0;
46
+ for (const cut of ordered) {
47
+ sizes.push(cut - previous);
48
+ previous = cut;
49
+ }
50
+ sizes.push(length - previous);
51
+ }
52
+ else {
53
+ // On a circle every piece runs from one cut to the next, and the last wraps through the origin.
54
+ for (let i = 0; i < ordered.length - 1; i++)
55
+ sizes.push(ordered[i + 1] - ordered[i]);
56
+ sizes.push(length - ordered[ordered.length - 1] + ordered[0]);
57
+ }
58
+ return sizes.filter((size) => size > 0).sort((a, b) => b - a);
59
+ }
60
+ //# sourceMappingURL=fragments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fragments.js","sourceRoot":"","sources":["../src/fragments.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAK3C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,QAAkB,EAAE,IAAuB;IACvF,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,uDAAuD,MAAM,EAAE,CAAC,CAAC;IACnI,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,IAAI,GAAG,GAAG,MAAM;YAAE,MAAM,IAAI,UAAU,CAAC,gBAAgB,GAAG,6BAA6B,MAAM,QAAQ,CAAC,CAAC;IAC9I,CAAC;IAED,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACzD,IAAI,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAC5B,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAE1C,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC1B,kGAAkG;QAClG,IAAI,QAAQ,GAAG,CAAC,CAAC;QACjB,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;YAC1B,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,QAAQ,CAAC,CAAC;YAC3B,QAAQ,GAAG,GAAG,CAAC;QACjB,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC,CAAC;IAChC,CAAC;SAAM,CAAC;QACN,gGAAgG;QAChG,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE;YAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAE,GAAG,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC;QACvF,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAE,GAAG,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAChE,CAAC"}
package/dist/gels.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Agarose gel types and the size range each one separates.
3
+ *
4
+ * The percentages and ranges are the conventional working figures for agarose in TAE buffer, as printed in supplier
5
+ * guides: a low-percentage gel spreads large fragments out and runs small ones off the bottom together, a
6
+ * high-percentage gel does the reverse. They describe which fragments a gel would *tell apart*, and are not
7
+ * measurements of migration distance — a real gel depends on buffer, voltage, run time and the dye used.
8
+ */
9
+ /**
10
+ * One agarose concentration and the fragment sizes it resolves.
11
+ *
12
+ * `largest` and `smallest` bracket the range over which the log-linear model in {@link bandPosition} holds. Outside
13
+ * it, fragments still migrate but are not separated: above `largest` they stay bunched near the wells, below
14
+ * `smallest` they run off together at the front.
15
+ */
16
+ export interface GelType {
17
+ /** Short identifier, e.g. `"1.0"`. */
18
+ id: string;
19
+ /** Agarose concentration as a percentage (w/v). */
20
+ percent: number;
21
+ /** Largest fragment, in base pairs, the gel still separates. */
22
+ largest: number;
23
+ /** Smallest fragment, in base pairs, the gel still separates. */
24
+ smallest: number;
25
+ }
26
+ /**
27
+ * The four agarose concentrations in common use, from the one that resolves the largest fragments to the one that
28
+ * resolves the smallest.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * GELS.find((g) => g.percent === 1)?.largest; // 10000
33
+ * ```
34
+ */
35
+ export declare const GELS: readonly GelType[];
36
+ /**
37
+ * Look a gel up by its {@link GelType.id}.
38
+ *
39
+ * @param id - An id from {@link GELS}, e.g. `"1.5"`.
40
+ * @returns The matching gel.
41
+ * @throws {@link ValueError} if no gel has that id.
42
+ * @example
43
+ * ```ts
44
+ * gelById("2.0").smallest; // 50
45
+ * ```
46
+ */
47
+ export declare function gelById(id: string): GelType;
48
+ //# sourceMappingURL=gels.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gels.d.ts","sourceRoot":"","sources":["../src/gels.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;;;;;GAMG;AACH,MAAM,WAAW,OAAO;IACtB,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,mDAAmD;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,OAAO,EAAE,MAAM,CAAC;IAChB,iEAAiE;IACjE,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,IAAI,EAAE,SAAS,OAAO,EAKlC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAI3C"}
package/dist/gels.js ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Agarose gel types and the size range each one separates.
3
+ *
4
+ * The percentages and ranges are the conventional working figures for agarose in TAE buffer, as printed in supplier
5
+ * guides: a low-percentage gel spreads large fragments out and runs small ones off the bottom together, a
6
+ * high-percentage gel does the reverse. They describe which fragments a gel would *tell apart*, and are not
7
+ * measurements of migration distance — a real gel depends on buffer, voltage, run time and the dye used.
8
+ */
9
+ import { ValueError } from "@biolibs/core";
10
+ /**
11
+ * The four agarose concentrations in common use, from the one that resolves the largest fragments to the one that
12
+ * resolves the smallest.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * GELS.find((g) => g.percent === 1)?.largest; // 10000
17
+ * ```
18
+ */
19
+ export const GELS = [
20
+ { id: "0.8", percent: 0.8, largest: 15000, smallest: 500 },
21
+ { id: "1.0", percent: 1.0, largest: 10000, smallest: 250 },
22
+ { id: "1.5", percent: 1.5, largest: 4000, smallest: 100 },
23
+ { id: "2.0", percent: 2.0, largest: 2000, smallest: 50 },
24
+ ];
25
+ /**
26
+ * Look a gel up by its {@link GelType.id}.
27
+ *
28
+ * @param id - An id from {@link GELS}, e.g. `"1.5"`.
29
+ * @returns The matching gel.
30
+ * @throws {@link ValueError} if no gel has that id.
31
+ * @example
32
+ * ```ts
33
+ * gelById("2.0").smallest; // 50
34
+ * ```
35
+ */
36
+ export function gelById(id) {
37
+ const gel = GELS.find((g) => g.id === id);
38
+ if (!gel)
39
+ throw new ValueError(`unknown gel '${id}'; known gels are ${GELS.map((g) => g.id).join(", ")}`);
40
+ return gel;
41
+ }
42
+ //# sourceMappingURL=gels.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gels.js","sourceRoot":"","sources":["../src/gels.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAoB3C;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,IAAI,GAAuB;IACtC,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE;IAC1D,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE;IAC1D,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE;IACzD,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE;CACzD,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,OAAO,CAAC,EAAU;IAChC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IAC1C,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,UAAU,CAAC,gBAAgB,EAAE,qBAAqB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1G,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * `@biolibs/gel`: a virtual agarose gel — where DNA fragments of a given size would run, and which of them a gel
4
+ * would tell apart.
5
+ *
6
+ * There is no Biopython counterpart; this is the data behind the gel photograph that tools such as SnapGene and
7
+ * Benchling draw beside a digest. Migration follows the standard log-linear approximation over the range a gel
8
+ * resolves, described in `bands.ts`. The figures are conventional working values for agarose in TAE, enough to show
9
+ * which fragments would be distinguishable, not measurements of a particular rig.
10
+ *
11
+ * - `gels.ts`: `GelType`, `GELS`, `gelById` — agarose concentrations and their resolving ranges
12
+ * - `ladders.ts`: `Ladder`, `LADDERS`, `ladderById` — size markers
13
+ * - `bands.ts`: `bandPosition`, `resolves`, `Band`, `bandsOf`, `ladderBands` — fragment sizes to visible bands
14
+ * - `fragments.ts`: `Topology`, `fragmentSizes` — cut positions to fragment lengths
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { bandsOf, fragmentSizes, gelById } from "@biolibs/gel";
19
+ *
20
+ * const gel = gelById("1.0");
21
+ * const sizes = fragmentSizes(2686, "circular", [395, 1230]); // pUC19 cut twice
22
+ * for (const band of bandsOf(sizes, gel)) {
23
+ * console.log(`${band.size} bp at ${(band.position * 100).toFixed(0)}% down the gel`);
24
+ * }
25
+ * ```
26
+ */
27
+ export * from "./gels.js";
28
+ export * from "./ladders.js";
29
+ export * from "./bands.js";
30
+ export * from "./fragments.js";
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * `@biolibs/gel`: a virtual agarose gel — where DNA fragments of a given size would run, and which of them a gel
4
+ * would tell apart.
5
+ *
6
+ * There is no Biopython counterpart; this is the data behind the gel photograph that tools such as SnapGene and
7
+ * Benchling draw beside a digest. Migration follows the standard log-linear approximation over the range a gel
8
+ * resolves, described in `bands.ts`. The figures are conventional working values for agarose in TAE, enough to show
9
+ * which fragments would be distinguishable, not measurements of a particular rig.
10
+ *
11
+ * - `gels.ts`: `GelType`, `GELS`, `gelById` — agarose concentrations and their resolving ranges
12
+ * - `ladders.ts`: `Ladder`, `LADDERS`, `ladderById` — size markers
13
+ * - `bands.ts`: `bandPosition`, `resolves`, `Band`, `bandsOf`, `ladderBands` — fragment sizes to visible bands
14
+ * - `fragments.ts`: `Topology`, `fragmentSizes` — cut positions to fragment lengths
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { bandsOf, fragmentSizes, gelById } from "@biolibs/gel";
19
+ *
20
+ * const gel = gelById("1.0");
21
+ * const sizes = fragmentSizes(2686, "circular", [395, 1230]); // pUC19 cut twice
22
+ * for (const band of bandsOf(sizes, gel)) {
23
+ * console.log(`${band.size} bp at ${(band.position * 100).toFixed(0)}% down the gel`);
24
+ * }
25
+ * ```
26
+ */
27
+ export * from "./gels.js";
28
+ export * from "./ladders.js";
29
+ export * from "./bands.js";
30
+ export * from "./fragments.js";
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Size markers (ladders) run alongside a sample so band sizes can be read off.
3
+ *
4
+ * The two ladders here are the generic 1 kb and 100 bp sets sold by every supplier. Their band sizes are the
5
+ * nominal ones printed on the tube.
6
+ */
7
+ /** A set of marker bands of known size. */
8
+ export interface Ladder {
9
+ /** Short identifier, e.g. `"1kb"`. */
10
+ id: string;
11
+ /** Display name. */
12
+ name: string;
13
+ /** Band sizes in base pairs, largest first. */
14
+ sizes: readonly number[];
15
+ }
16
+ /**
17
+ * The generic 1 kb and 100 bp ladders.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * LADDERS[0]!.sizes[0]; // 10000
22
+ * ```
23
+ */
24
+ export declare const LADDERS: readonly Ladder[];
25
+ /**
26
+ * Look a ladder up by its {@link Ladder.id}.
27
+ *
28
+ * @param id - An id from {@link LADDERS}, e.g. `"100bp"`.
29
+ * @returns The matching ladder.
30
+ * @throws {@link ValueError} if no ladder has that id.
31
+ */
32
+ export declare function ladderById(id: string): Ladder;
33
+ //# sourceMappingURL=ladders.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ladders.d.ts","sourceRoot":"","sources":["../src/ladders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,2CAA2C;AAC3C,MAAM,WAAW,MAAM;IACrB,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,oBAAoB;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,+CAA+C;IAC/C,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CAC1B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,EAAE,SAAS,MAAM,EAGpC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAI7C"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Size markers (ladders) run alongside a sample so band sizes can be read off.
3
+ *
4
+ * The two ladders here are the generic 1 kb and 100 bp sets sold by every supplier. Their band sizes are the
5
+ * nominal ones printed on the tube.
6
+ */
7
+ import { ValueError } from "@biolibs/core";
8
+ /**
9
+ * The generic 1 kb and 100 bp ladders.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * LADDERS[0]!.sizes[0]; // 10000
14
+ * ```
15
+ */
16
+ export const LADDERS = [
17
+ { id: "1kb", name: "1 kb ladder", sizes: [10000, 8000, 6000, 5000, 4000, 3000, 2000, 1500, 1000, 500] },
18
+ { id: "100bp", name: "100 bp ladder", sizes: [1500, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100] },
19
+ ];
20
+ /**
21
+ * Look a ladder up by its {@link Ladder.id}.
22
+ *
23
+ * @param id - An id from {@link LADDERS}, e.g. `"100bp"`.
24
+ * @returns The matching ladder.
25
+ * @throws {@link ValueError} if no ladder has that id.
26
+ */
27
+ export function ladderById(id) {
28
+ const ladder = LADDERS.find((l) => l.id === id);
29
+ if (!ladder)
30
+ throw new ValueError(`unknown ladder '${id}'; known ladders are ${LADDERS.map((l) => l.id).join(", ")}`);
31
+ return ladder;
32
+ }
33
+ //# sourceMappingURL=ladders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ladders.js","sourceRoot":"","sources":["../src/ladders.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAY3C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,OAAO,GAAsB;IACxC,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,EAAE;IACvG,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE;CACzG,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,EAAU;IACnC,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IAChD,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,UAAU,CAAC,mBAAmB,EAAE,wBAAwB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACtH,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
+ "bugs": {
3
+ "url": "https://github.com/matthewlovestocode/biolibs/issues"
4
+ },
5
+ "dependencies": {
6
+ "@biolibs/core": "^1.0.0"
7
+ },
8
+ "description": "Virtual agarose gel electrophoresis: where DNA fragments of a given size run, ladders, and which bands a gel resolves",
9
+ "engines": {
10
+ "node": ">=20"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "src"
21
+ ],
22
+ "homepage": "https://github.com/matthewlovestocode/biolibs/tree/main/packages/gel#readme",
23
+ "keywords": [
24
+ "bioinformatics",
25
+ "biology",
26
+ "dna",
27
+ "typescript",
28
+ "agarose",
29
+ "gel-electrophoresis",
30
+ "ladder",
31
+ "fragment",
32
+ "virtual-gel"
33
+ ],
34
+ "license": "MIT",
35
+ "main": "dist/index.js",
2
36
  "name": "@biolibs/gel",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/matthewlovestocode/biolibs.git",
43
+ "directory": "packages/gel"
44
+ },
45
+ "type": "module",
46
+ "types": "dist/index.d.ts",
47
+ "version": "1.0.0"
48
+ }
package/src/bands.ts ADDED
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Turning a list of fragment sizes into the bands a gel would show.
3
+ *
4
+ * Over the range a gel separates, the distance a fragment migrates is very nearly a straight line in the logarithm
5
+ * of its length — the standard first approximation, and the reason a gel photograph is read against a log scale.
6
+ * So {@link bandPosition} interpolates in `log10(size)` between the gel's largest and smallest resolved fragment,
7
+ * giving 0 at the wells and 1 at the running front.
8
+ *
9
+ * Two fragments of nearly the same size cannot be told apart by eye, so {@link bandsOf} merges sizes that fall
10
+ * within a tolerance of each other into a single band and records how many fragments it holds. Fragments outside
11
+ * the gel's range are not separated at all, so they are merged regardless of tolerance: the small ones pile up
12
+ * together at the front, and the large ones stay bunched at the wells.
13
+ */
14
+
15
+ import { ValueError } from "@biolibs/core";
16
+ import type { GelType } from "./gels.js";
17
+
18
+ /** Default band-merging tolerance: fragments within 3% of each other run as one band. */
19
+ export const DEFAULT_TOLERANCE = 0.03;
20
+
21
+ /**
22
+ * How far down the gel a fragment of `size` runs, as a fraction of the distance from the wells to the front.
23
+ *
24
+ * @param size - Fragment length in base pairs.
25
+ * @param gel - The gel being run.
26
+ * @returns 0 at the wells, 1 at the front. Fragments larger than {@link GelType.largest} clamp to 0 and ones
27
+ * smaller than {@link GelType.smallest} clamp to 1, since the gel does not separate them.
28
+ * @throws {@link ValueError} for a size that is not a positive finite number.
29
+ * @example
30
+ * ```ts
31
+ * const gel = { id: "1.0", percent: 1, largest: 10000, smallest: 250 };
32
+ * bandPosition(10000, gel); // 0
33
+ * bandPosition(250, gel); // 1
34
+ * bandPosition(1581, gel); // ~0.5 (the geometric mean of the two ends)
35
+ * ```
36
+ */
37
+ export function bandPosition(size: number, gel: GelType): number {
38
+ if (!Number.isFinite(size) || size <= 0) throw new ValueError(`fragment size must be a positive number, got ${size}`);
39
+ const high = Math.log10(gel.largest);
40
+ const low = Math.log10(gel.smallest);
41
+ // A gel whose ends coincide separates nothing; put everything at the front rather than dividing by zero.
42
+ if (high <= low) return 1;
43
+ const fraction = (high - Math.log10(size)) / (high - low);
44
+ return Math.min(1, Math.max(0, fraction));
45
+ }
46
+
47
+ /**
48
+ * Whether a gel separates a fragment of this size from its neighbours, i.e. whether it falls inside the gel's
49
+ * resolving range.
50
+ *
51
+ * @param size - Fragment length in base pairs.
52
+ * @param gel - The gel being run.
53
+ * @returns `true` when `gel.smallest <= size <= gel.largest`.
54
+ */
55
+ export const resolves = (size: number, gel: GelType): boolean => size <= gel.largest && size >= gel.smallest;
56
+
57
+ /** One visible band: the fragments that run together at a single place on the gel. */
58
+ export interface Band {
59
+ /** Representative size in base pairs — the largest fragment in the band. */
60
+ size: number;
61
+ /** Every fragment size merged into this band, largest first. */
62
+ sizes: readonly number[];
63
+ /** Distance from the wells, 0 to 1, as {@link bandPosition} gives it. */
64
+ position: number;
65
+ /** Total base pairs in the band; a band holding more DNA stains more brightly. */
66
+ totalBases: number;
67
+ /** Brightness from 0 to 1, the band's share of the lane's base pairs, scaled so the heaviest band is 1. */
68
+ intensity: number;
69
+ /**
70
+ * True when every fragment in the band is outside the gel's resolving range, so the gel does not separate them:
71
+ * below it they sit together at the running front, above it they stay bunched at the wells.
72
+ */
73
+ unresolved: boolean;
74
+ }
75
+
76
+ /**
77
+ * The bands a lane would show, given the sizes of the fragments loaded into it.
78
+ *
79
+ * @param sizes - Fragment lengths in base pairs, in any order. Sizes of zero or less are rejected.
80
+ * @param gel - The gel being run.
81
+ * @param tolerance - Relative size difference below which two fragments run as one band. Defaults to
82
+ * {@link DEFAULT_TOLERANCE}; pass 0 to keep every distinct size apart.
83
+ * @returns The bands, largest fragment first. An empty lane gives an empty array.
84
+ * @throws {@link ValueError} for a non-positive size or a negative tolerance.
85
+ * @example
86
+ * ```ts
87
+ * const gel = gelById("1.0");
88
+ * bandsOf([2000, 2010, 500], gel).length; // 2 — 2000 and 2010 run together
89
+ * ```
90
+ */
91
+ export function bandsOf(sizes: readonly number[], gel: GelType, tolerance = DEFAULT_TOLERANCE): Band[] {
92
+ if (!Number.isFinite(tolerance) || tolerance < 0) throw new ValueError(`tolerance must be zero or more, got ${tolerance}`);
93
+ for (const size of sizes) {
94
+ if (!Number.isFinite(size) || size <= 0) throw new ValueError(`fragment size must be a positive number, got ${size}`);
95
+ }
96
+ if (sizes.length === 0) return [];
97
+
98
+ const descending = [...sizes].sort((a, b) => b - a);
99
+
100
+ // Walk down the sizes, starting a new group whenever the next fragment is further than `tolerance` from the
101
+ // group's representative size. Comparing against the group's representative rather than the previous fragment is
102
+ // what stops a run of near-neighbours chaining into one unbounded band.
103
+ const groups: number[][] = [];
104
+ for (const size of descending) {
105
+ const current = groups[groups.length - 1];
106
+ const representative = current?.[0];
107
+ // Fragments the gel does not separate belong in one band however far apart they are: everything above the
108
+ // range stays bunched at the wells, and everything below it runs off together at the front.
109
+ const bothUnresolved = representative !== undefined
110
+ && ((representative < gel.smallest && size < gel.smallest)
111
+ || (representative > gel.largest && size > gel.largest));
112
+ const closeEnough = representative !== undefined && representative - size <= representative * tolerance;
113
+ if (current && (bothUnresolved || closeEnough)) current.push(size);
114
+ else groups.push([size]);
115
+ }
116
+
117
+ const weights = groups.map((group) => group.reduce((sum, size) => sum + size, 0));
118
+ const heaviest = Math.max(...weights);
119
+ return groups.map((group, i) => ({
120
+ size: group[0]!,
121
+ sizes: group,
122
+ position: bandPosition(group[0]!, gel),
123
+ totalBases: weights[i]!,
124
+ intensity: heaviest > 0 ? weights[i]! / heaviest : 0,
125
+ unresolved: group.every((size) => !resolves(size, gel)),
126
+ }));
127
+ }
128
+
129
+ /**
130
+ * The bands a ladder would show on a gel, as a lane of single fragments.
131
+ *
132
+ * @param ladderSizes - The ladder's band sizes.
133
+ * @param gel - The gel being run.
134
+ * @returns One band per marker size, largest first, each of equal intensity. Markers the gel does not resolve are
135
+ * marked `unresolved` but, unlike a sample lane, are never merged: a ladder's bands are known sizes and worth
136
+ * showing individually even where the gel would not separate them.
137
+ */
138
+ export function ladderBands(ladderSizes: readonly number[], gel: GelType): Band[] {
139
+ return [...ladderSizes]
140
+ .sort((a, b) => b - a)
141
+ .map((size) => ({
142
+ size,
143
+ sizes: [size],
144
+ position: bandPosition(size, gel),
145
+ totalBases: size,
146
+ intensity: 1,
147
+ unresolved: !resolves(size, gel),
148
+ }));
149
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Fragment sizes from cut positions, so a digest can be loaded onto a gel without pulling in an enzyme library.
3
+ *
4
+ * Positions are 0-based offsets into the sequence, counted as boundaries between bases: a cut at `n` separates the
5
+ * base at index `n - 1` from the base at index `n`. This is the half-open convention used across biolibs, so a cut
6
+ * list is directly usable as slice boundaries. (Biopython's `RestrictionType.search` returns 1-based positions;
7
+ * subtract one from each before passing them here.)
8
+ */
9
+
10
+ import { ValueError } from "@biolibs/core";
11
+
12
+ /** Whether a sequence is a line with two ends or a closed circle. */
13
+ export type Topology = "linear" | "circular";
14
+
15
+ /**
16
+ * The lengths of the pieces a set of cuts divides a sequence into.
17
+ *
18
+ * A linear sequence with `n` cuts falls into `n + 1` pieces, including the two uncut ends. A circular sequence with
19
+ * `n` cuts falls into `n` pieces, one of which spans the origin; an uncut circle stays whole.
20
+ *
21
+ * @param length - Sequence length in bases.
22
+ * @param topology - `"linear"` or `"circular"`.
23
+ * @param cuts - 0-based cut positions, in any order. Duplicates count once, since two enzymes cutting the same
24
+ * bond produce one break.
25
+ * @returns The piece lengths, largest first. Cuts at 0 or at `length` on a linear sequence are the existing ends and
26
+ * add no piece; zero-length pieces are never returned.
27
+ * @throws {@link ValueError} for a negative or non-integer length, or a cut outside `[0, length]`.
28
+ * @example
29
+ * ```ts
30
+ * fragmentSizes(1000, "linear", [100, 400]); // [600, 300, 100]
31
+ * fragmentSizes(1000, "circular", [100, 400]); // [700, 300]
32
+ * fragmentSizes(1000, "circular", []); // [1000]
33
+ * ```
34
+ */
35
+ export function fragmentSizes(length: number, topology: Topology, cuts: readonly number[]): number[] {
36
+ if (!Number.isInteger(length) || length < 0) throw new ValueError(`sequence length must be a non-negative integer, got ${length}`);
37
+ for (const cut of cuts) {
38
+ if (!Number.isInteger(cut) || cut < 0 || cut > length) throw new ValueError(`cut position ${cut} is outside a sequence of ${length} bases`);
39
+ }
40
+
41
+ const ordered = [...new Set(cuts)].sort((a, b) => a - b);
42
+ if (length === 0) return [];
43
+ if (ordered.length === 0) return [length];
44
+
45
+ const sizes: number[] = [];
46
+ if (topology === "linear") {
47
+ // The two ends are pieces too: from the start to the first cut, and from the last cut to the end.
48
+ let previous = 0;
49
+ for (const cut of ordered) {
50
+ sizes.push(cut - previous);
51
+ previous = cut;
52
+ }
53
+ sizes.push(length - previous);
54
+ } else {
55
+ // On a circle every piece runs from one cut to the next, and the last wraps through the origin.
56
+ for (let i = 0; i < ordered.length - 1; i++) sizes.push(ordered[i + 1]! - ordered[i]!);
57
+ sizes.push(length - ordered[ordered.length - 1]! + ordered[0]!);
58
+ }
59
+ return sizes.filter((size) => size > 0).sort((a, b) => b - a);
60
+ }
package/src/gels.ts ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Agarose gel types and the size range each one separates.
3
+ *
4
+ * The percentages and ranges are the conventional working figures for agarose in TAE buffer, as printed in supplier
5
+ * guides: a low-percentage gel spreads large fragments out and runs small ones off the bottom together, a
6
+ * high-percentage gel does the reverse. They describe which fragments a gel would *tell apart*, and are not
7
+ * measurements of migration distance — a real gel depends on buffer, voltage, run time and the dye used.
8
+ */
9
+
10
+ import { ValueError } from "@biolibs/core";
11
+
12
+ /**
13
+ * One agarose concentration and the fragment sizes it resolves.
14
+ *
15
+ * `largest` and `smallest` bracket the range over which the log-linear model in {@link bandPosition} holds. Outside
16
+ * it, fragments still migrate but are not separated: above `largest` they stay bunched near the wells, below
17
+ * `smallest` they run off together at the front.
18
+ */
19
+ export interface GelType {
20
+ /** Short identifier, e.g. `"1.0"`. */
21
+ id: string;
22
+ /** Agarose concentration as a percentage (w/v). */
23
+ percent: number;
24
+ /** Largest fragment, in base pairs, the gel still separates. */
25
+ largest: number;
26
+ /** Smallest fragment, in base pairs, the gel still separates. */
27
+ smallest: number;
28
+ }
29
+
30
+ /**
31
+ * The four agarose concentrations in common use, from the one that resolves the largest fragments to the one that
32
+ * resolves the smallest.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * GELS.find((g) => g.percent === 1)?.largest; // 10000
37
+ * ```
38
+ */
39
+ export const GELS: readonly GelType[] = [
40
+ { id: "0.8", percent: 0.8, largest: 15000, smallest: 500 },
41
+ { id: "1.0", percent: 1.0, largest: 10000, smallest: 250 },
42
+ { id: "1.5", percent: 1.5, largest: 4000, smallest: 100 },
43
+ { id: "2.0", percent: 2.0, largest: 2000, smallest: 50 },
44
+ ];
45
+
46
+ /**
47
+ * Look a gel up by its {@link GelType.id}.
48
+ *
49
+ * @param id - An id from {@link GELS}, e.g. `"1.5"`.
50
+ * @returns The matching gel.
51
+ * @throws {@link ValueError} if no gel has that id.
52
+ * @example
53
+ * ```ts
54
+ * gelById("2.0").smallest; // 50
55
+ * ```
56
+ */
57
+ export function gelById(id: string): GelType {
58
+ const gel = GELS.find((g) => g.id === id);
59
+ if (!gel) throw new ValueError(`unknown gel '${id}'; known gels are ${GELS.map((g) => g.id).join(", ")}`);
60
+ return gel;
61
+ }