@biolibs/gel 0.0.0-stage → 1.0.1
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 +365 -2
- package/dist/bands.d.ts +87 -0
- package/dist/bands.d.ts.map +1 -0
- package/dist/bands.js +128 -0
- package/dist/bands.js.map +1 -0
- package/dist/fragments.d.ts +32 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +60 -0
- package/dist/fragments.js.map +1 -0
- package/dist/gels.d.ts +62 -0
- package/dist/gels.d.ts.map +1 -0
- package/dist/gels.js +42 -0
- package/dist/gels.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/ladders.d.ts +33 -0
- package/dist/ladders.d.ts.map +1 -0
- package/dist/ladders.js +33 -0
- package/dist/ladders.js.map +1 -0
- package/dist/protein.d.ts +104 -0
- package/dist/protein.d.ts.map +1 -0
- package/dist/protein.js +103 -0
- package/dist/protein.js.map +1 -0
- package/package.json +46 -4
- package/src/bands.ts +149 -0
- package/src/fragments.ts +60 -0
- package/src/gels.ts +76 -0
- package/src/index.ts +33 -0
- package/src/ladders.ts +44 -0
- package/src/protein.ts +131 -0
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 { Resolving } 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, or a protein's mass in kilodaltons.
|
|
25
|
+
* @param gel - The range the gel separates.
|
|
26
|
+
* @returns 0 at the wells, 1 at the front. Fragments larger than {@link Resolving.largest} clamp to 0 and ones
|
|
27
|
+
* smaller than {@link Resolving.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: Resolving): 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: Resolving): 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 — the largest in the band, in whatever the gel sorts by: base pairs for DNA, kilodaltons for protein. */
|
|
60
|
+
size: number;
|
|
61
|
+
/** Every 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
|
+
/** The sizes in the band added up; a band holding more of what is stained shows more brightly. */
|
|
66
|
+
totalBases: number;
|
|
67
|
+
/** Brightness from 0 to 1, the band's share of the lane's total, 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: Resolving, 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: Resolving): 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
|
+
}
|
package/src/fragments.ts
ADDED
|
@@ -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,76 @@
|
|
|
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
|
+
* The range of sizes a gel separates, whatever is being run on it.
|
|
14
|
+
*
|
|
15
|
+
* It is this and not a kind of gel because the arithmetic of a band's position is the same
|
|
16
|
+
* arithmetic either way: the two ends bracket the range over which the log-linear model in
|
|
17
|
+
* {@link bandPosition} holds, and the unit they are in is whatever the gel sorts by — base pairs for
|
|
18
|
+
* DNA on agarose, kilodaltons for protein on acrylamide.
|
|
19
|
+
*/
|
|
20
|
+
export interface Resolving {
|
|
21
|
+
/** The largest the gel still separates. */
|
|
22
|
+
largest: number;
|
|
23
|
+
/** The smallest the gel still separates. */
|
|
24
|
+
smallest: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One agarose concentration and the fragment sizes it resolves.
|
|
29
|
+
*
|
|
30
|
+
* `largest` and `smallest` bracket the range over which the log-linear model in {@link bandPosition} holds. Outside
|
|
31
|
+
* it, fragments still migrate but are not separated: above `largest` they stay bunched near the wells, below
|
|
32
|
+
* `smallest` they run off together at the front.
|
|
33
|
+
*/
|
|
34
|
+
export interface GelType extends Resolving {
|
|
35
|
+
/** Short identifier, e.g. `"1.0"`. */
|
|
36
|
+
id: string;
|
|
37
|
+
/** Agarose concentration as a percentage (w/v). */
|
|
38
|
+
percent: number;
|
|
39
|
+
/** Largest fragment, in base pairs, the gel still separates. */
|
|
40
|
+
largest: number;
|
|
41
|
+
/** Smallest fragment, in base pairs, the gel still separates. */
|
|
42
|
+
smallest: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The four agarose concentrations in common use, from the one that resolves the largest fragments to the one that
|
|
47
|
+
* resolves the smallest.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* GELS.find((g) => g.percent === 1)?.largest; // 10000
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export const GELS: readonly GelType[] = [
|
|
55
|
+
{ id: "0.8", percent: 0.8, largest: 15000, smallest: 500 },
|
|
56
|
+
{ id: "1.0", percent: 1.0, largest: 10000, smallest: 250 },
|
|
57
|
+
{ id: "1.5", percent: 1.5, largest: 4000, smallest: 100 },
|
|
58
|
+
{ id: "2.0", percent: 2.0, largest: 2000, smallest: 50 },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Look a gel up by its {@link GelType.id}.
|
|
63
|
+
*
|
|
64
|
+
* @param id - An id from {@link GELS}, e.g. `"1.5"`.
|
|
65
|
+
* @returns The matching gel.
|
|
66
|
+
* @throws {@link ValueError} if no gel has that id.
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* gelById("2.0").smallest; // 50
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export function gelById(id: string): GelType {
|
|
73
|
+
const gel = GELS.find((g) => g.id === id);
|
|
74
|
+
if (!gel) throw new ValueError(`unknown gel '${id}'; known gels are ${GELS.map((g) => g.id).join(", ")}`);
|
|
75
|
+
return gel;
|
|
76
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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`: `Resolving`, `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
|
+
* - `protein.ts`: `ProteinGel`, `PROTEIN_GELS`, `PROTEIN_LADDERS`, `proteinBands` — the same, for protein on acrylamide
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { bandsOf, fragmentSizes, gelById } from "@biolibs/gel";
|
|
20
|
+
*
|
|
21
|
+
* const gel = gelById("1.0");
|
|
22
|
+
* const sizes = fragmentSizes(2686, "circular", [395, 1230]); // pUC19 cut twice
|
|
23
|
+
* for (const band of bandsOf(sizes, gel)) {
|
|
24
|
+
* console.log(`${band.size} bp at ${(band.position * 100).toFixed(0)}% down the gel`);
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export * from "./gels.js";
|
|
30
|
+
export * from "./ladders.js";
|
|
31
|
+
export * from "./bands.js";
|
|
32
|
+
export * from "./fragments.js";
|
|
33
|
+
export * from "./protein.js";
|
package/src/ladders.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
|
|
8
|
+
import { ValueError } from "@biolibs/core";
|
|
9
|
+
|
|
10
|
+
/** A set of marker bands of known size. */
|
|
11
|
+
export interface Ladder {
|
|
12
|
+
/** Short identifier, e.g. `"1kb"`. */
|
|
13
|
+
id: string;
|
|
14
|
+
/** Display name. */
|
|
15
|
+
name: string;
|
|
16
|
+
/** Band sizes in base pairs, largest first. */
|
|
17
|
+
sizes: readonly number[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The generic 1 kb and 100 bp ladders.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* LADDERS[0]!.sizes[0]; // 10000
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export const LADDERS: readonly Ladder[] = [
|
|
29
|
+
{ id: "1kb", name: "1 kb ladder", sizes: [10000, 8000, 6000, 5000, 4000, 3000, 2000, 1500, 1000, 500] },
|
|
30
|
+
{ id: "100bp", name: "100 bp ladder", sizes: [1500, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100] },
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Look a ladder up by its {@link Ladder.id}.
|
|
35
|
+
*
|
|
36
|
+
* @param id - An id from {@link LADDERS}, e.g. `"100bp"`.
|
|
37
|
+
* @returns The matching ladder.
|
|
38
|
+
* @throws {@link ValueError} if no ladder has that id.
|
|
39
|
+
*/
|
|
40
|
+
export function ladderById(id: string): Ladder {
|
|
41
|
+
const ladder = LADDERS.find((l) => l.id === id);
|
|
42
|
+
if (!ladder) throw new ValueError(`unknown ladder '${id}'; known ladders are ${LADDERS.map((l) => l.id).join(", ")}`);
|
|
43
|
+
return ladder;
|
|
44
|
+
}
|
package/src/protein.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polyacrylamide gels for protein, and the markers run beside them.
|
|
3
|
+
*
|
|
4
|
+
* A protein gel is read the same way an agarose one is, and for the same reason: a mesh holds large
|
|
5
|
+
* molecules back more than small ones, so over the range a gel resolves the distance run is close to
|
|
6
|
+
* a straight line against the logarithm of size. The one difference is what is being measured.
|
|
7
|
+
* Agarose sorts DNA by length in base pairs; SDS-PAGE sorts protein by mass in kilodaltons, because
|
|
8
|
+
* the detergent coats every chain with much the same charge per unit mass and undoes its shape, so
|
|
9
|
+
* what is left to tell two proteins apart is how big they are. That is why {@link bandPosition} and
|
|
10
|
+
* {@link bandsOf} are the functions used here too: they take a resolving range, not a kind of gel.
|
|
11
|
+
*
|
|
12
|
+
* The percentages and ranges are the conventional working figures printed in supplier guides for
|
|
13
|
+
* SDS-PAGE in Tris-glycine. They say which proteins a gel would *tell apart*, and are not
|
|
14
|
+
* measurements. Two things to keep in mind when reading a real gel against them: a prestained
|
|
15
|
+
* marker's bands are calibrated against unstained standards and run a little off their printed
|
|
16
|
+
* weights, and a protein that is glycosylated, or that has not been fully reduced, runs at a size
|
|
17
|
+
* that is not the mass its sequence comes to.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { ValueError } from "@biolibs/core";
|
|
21
|
+
import { bandPosition, bandsOf, ladderBands, type Band } from "./bands.js";
|
|
22
|
+
|
|
23
|
+
/** One acrylamide concentration and the protein sizes it resolves, in kilodaltons. */
|
|
24
|
+
export interface ProteinGel {
|
|
25
|
+
/** Short identifier, e.g. `"12"`. */
|
|
26
|
+
id: string;
|
|
27
|
+
/** Acrylamide concentration as a percentage (w/v). */
|
|
28
|
+
percent: number;
|
|
29
|
+
/** Largest protein, in kilodaltons, the gel still separates. */
|
|
30
|
+
largest: number;
|
|
31
|
+
/** Smallest protein, in kilodaltons, the gel still separates. */
|
|
32
|
+
smallest: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The four acrylamide concentrations in common use, from the one that resolves the largest proteins
|
|
37
|
+
* to the one that resolves the smallest.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* PROTEIN_GELS.find((g) => g.percent === 12)?.largest; // 100
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export const PROTEIN_GELS: readonly ProteinGel[] = [
|
|
45
|
+
{ id: "7.5", percent: 7.5, largest: 200, smallest: 40 },
|
|
46
|
+
{ id: "10", percent: 10, largest: 150, smallest: 20 },
|
|
47
|
+
{ id: "12", percent: 12, largest: 100, smallest: 10 },
|
|
48
|
+
{ id: "15", percent: 15, largest: 60, smallest: 5 },
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Look a protein gel up by its {@link ProteinGel.id}.
|
|
53
|
+
*
|
|
54
|
+
* @param id - An id from {@link PROTEIN_GELS}, e.g. `"12"`.
|
|
55
|
+
* @returns The matching gel.
|
|
56
|
+
* @throws {@link ValueError} if no protein gel has that id.
|
|
57
|
+
*/
|
|
58
|
+
export function proteinGelById(id: string): ProteinGel {
|
|
59
|
+
const gel = PROTEIN_GELS.find((one) => one.id === id);
|
|
60
|
+
if (!gel) throw new ValueError(`unknown protein gel '${id}'; known protein gels are ${PROTEIN_GELS.map((one) => one.id).join(", ")}`);
|
|
61
|
+
return gel;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A set of protein marker bands of known mass. */
|
|
65
|
+
export interface ProteinLadder {
|
|
66
|
+
/** Short identifier, e.g. `"broad"`. */
|
|
67
|
+
id: string;
|
|
68
|
+
/** Display name. */
|
|
69
|
+
name: string;
|
|
70
|
+
/** Band masses in kilodaltons, largest first. */
|
|
71
|
+
sizes: readonly number[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The generic broad-range and low-range protein markers.
|
|
76
|
+
*
|
|
77
|
+
* These are the nominal weights printed on the tube. A prestained marker runs a little off them,
|
|
78
|
+
* which is why a band is read against the marker in the lane beside it rather than off a distance.
|
|
79
|
+
*/
|
|
80
|
+
export const PROTEIN_LADDERS: readonly ProteinLadder[] = [
|
|
81
|
+
{ id: "broad", name: "Broad-range marker", sizes: [250, 150, 100, 75, 50, 37, 25, 20, 15, 10] },
|
|
82
|
+
{ id: "low", name: "Low-range marker", sizes: [100, 75, 50, 37, 25, 20, 15, 10] },
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Look a protein marker up by its {@link ProteinLadder.id}.
|
|
87
|
+
*
|
|
88
|
+
* @param id - An id from {@link PROTEIN_LADDERS}, e.g. `"broad"`.
|
|
89
|
+
* @returns The matching marker.
|
|
90
|
+
* @throws {@link ValueError} if no marker has that id.
|
|
91
|
+
*/
|
|
92
|
+
export function proteinLadderById(id: string): ProteinLadder {
|
|
93
|
+
const ladder = PROTEIN_LADDERS.find((one) => one.id === id);
|
|
94
|
+
if (!ladder) throw new ValueError(`unknown protein ladder '${id}'; known protein ladders are ${PROTEIN_LADDERS.map((one) => one.id).join(", ")}`);
|
|
95
|
+
return ladder;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* How far down a protein gel a band of a given mass runs, as a fraction of the way from the wells to
|
|
100
|
+
* the front.
|
|
101
|
+
*
|
|
102
|
+
* @param kilodaltons - The protein's mass.
|
|
103
|
+
* @param gel - The gel being run.
|
|
104
|
+
* @returns 0 at the wells, 1 at the front, clamped outside the range the gel separates.
|
|
105
|
+
* @throws {@link ValueError} for a mass that is not a positive finite number.
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* proteinBandPosition(100, proteinGelById("12")); // 0 — at the top of what 12% separates
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export const proteinBandPosition = (kilodaltons: number, gel: ProteinGel): number => bandPosition(kilodaltons, gel);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The bands a lane of protein would show.
|
|
115
|
+
*
|
|
116
|
+
* @param kilodaltons - The masses in the lane, in any order.
|
|
117
|
+
* @param gel - The gel being run.
|
|
118
|
+
* @param tolerance - Relative difference below which two proteins run as one band.
|
|
119
|
+
* @returns The bands, heaviest first. {@link Band.size} and {@link Band.sizes} are in kilodaltons.
|
|
120
|
+
*/
|
|
121
|
+
export const proteinBands = (kilodaltons: readonly number[], gel: ProteinGel, tolerance?: number): Band[] =>
|
|
122
|
+
bandsOf(kilodaltons, gel, tolerance);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The bands a protein marker would show, as a lane of single known masses.
|
|
126
|
+
*
|
|
127
|
+
* @param sizes - The marker's band masses, in kilodaltons.
|
|
128
|
+
* @param gel - The gel being run.
|
|
129
|
+
* @returns One band per marker, heaviest first.
|
|
130
|
+
*/
|
|
131
|
+
export const proteinLadderBands = (sizes: readonly number[], gel: ProteinGel): Band[] => ladderBands(sizes, gel);
|