@biolibs/bionet 1.5.0 → 1.8.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/README.md +54 -4
- package/dist/log.d.ts +49 -5
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +17 -4
- package/dist/log.js.map +1 -1
- package/dist/merge.d.ts +35 -6
- package/dist/merge.d.ts.map +1 -1
- package/dist/merge.js +103 -20
- package/dist/merge.js.map +1 -1
- package/package.json +3 -2
- package/src/log.ts +55 -7
- package/src/merge.ts +120 -25
package/src/log.ts
CHANGED
|
@@ -309,12 +309,24 @@ export interface PartEnds {
|
|
|
309
309
|
}
|
|
310
310
|
|
|
311
311
|
/**
|
|
312
|
-
*
|
|
312
|
+
* What a part is made of.
|
|
313
|
+
*
|
|
314
|
+
* A lab keeps protein as well as DNA, and the two cannot be told apart by looking: `ACGT` is a
|
|
315
|
+
* perfectly good peptide — alanine, cysteine, glycine, threonine — as well as a perfectly good
|
|
316
|
+
* piece of DNA. So which it is has to be said, and the two are counted by different checksums, as
|
|
317
|
+
* they should be: a gene and the protein it codes for are not the same thing.
|
|
318
|
+
*/
|
|
319
|
+
export type PartKind = "dna" | "protein";
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @param part - Its sequence, its name, whether it is a circle, its ends if they are not blunt, and
|
|
323
|
+
* what it is made of if it is not DNA.
|
|
313
324
|
* @returns The change that adds a part, by its sequence. Its checksum is computed when applied, and
|
|
314
325
|
* takes the ends in: the same bases with different ends are a different molecule.
|
|
315
326
|
*/
|
|
316
|
-
export const addPart = (part: { sequence: string; circular?: boolean; name: string; ends?: PartEnds }): Operation =>
|
|
327
|
+
export const addPart = (part: { sequence: string; circular?: boolean; name: string; ends?: PartEnds; kind?: PartKind }): Operation =>
|
|
317
328
|
({ type: "part.add", sequence: part.sequence, circular: part.circular === true, name: part.name,
|
|
329
|
+
...(part.kind === undefined || part.kind === "dna" ? {} : { kind: part.kind }),
|
|
318
330
|
...(part.ends === undefined || (part.ends.left === 0 && part.ends.right === 0) ? {} : { ends: { left: part.ends.left, right: part.ends.right } }) });
|
|
319
331
|
|
|
320
332
|
/** Something marked on a part's sequence: a gene, a promoter, an origin, a primer's binding site. */
|
|
@@ -335,9 +347,13 @@ export const annotatePart = (part: string, features: PartFeature[]): Operation =
|
|
|
335
347
|
/** @returns The change that says whether a part is on offer to other labs. */
|
|
336
348
|
export const offerPart = (part: string, available: boolean): Operation => ({ type: "part.offer", part, available });
|
|
337
349
|
|
|
338
|
-
/**
|
|
339
|
-
|
|
340
|
-
|
|
350
|
+
/**
|
|
351
|
+
* @returns The change that adds one physical lot: a tube of a part, or a culture of a strain.
|
|
352
|
+
* Exactly one of `part` and `strain` is given, and `amount` is a whole number of `unit`.
|
|
353
|
+
*/
|
|
354
|
+
export const addItem = (item: { item: string; part?: string; strain?: string; amount: number; unit?: string | null }): Operation =>
|
|
355
|
+
({ type: "item.add", item: item.item, ...(item.part === undefined ? {} : { part: item.part }),
|
|
356
|
+
...(item.strain === undefined ? {} : { strain: item.strain }), amount: item.amount, unit: item.unit ?? null });
|
|
341
357
|
|
|
342
358
|
/** @returns The change that adds to or takes from an item. `by` is a whole-number difference, never a total. */
|
|
343
359
|
export const adjustItem = (item: string, by: number): Operation => ({ type: "item.adjust", item, by });
|
|
@@ -504,8 +520,40 @@ export const settlePlan = (plan: string, run: string | null): Operation => ({ ty
|
|
|
504
520
|
/** @returns A new ID for a plan, made the way an item's is. */
|
|
505
521
|
export const newPlanId = (): string => newItemId();
|
|
506
522
|
|
|
507
|
-
/**
|
|
508
|
-
|
|
523
|
+
/**
|
|
524
|
+
* A living thing the lab keeps: an organism, and what has been put into it.
|
|
525
|
+
*
|
|
526
|
+
* A part is known by its sequence, so two labs that made the same molecule call it the same thing
|
|
527
|
+
* without ever having spoken. A strain is not like that. Two labs that put the same plasmid into
|
|
528
|
+
* the same published host have two strains, which may behave differently and did not come from each
|
|
529
|
+
* other, so a strain is known by an ID the way a tube or a place is, and says what it came from.
|
|
530
|
+
*/
|
|
531
|
+
export interface StrainRecord {
|
|
532
|
+
/** The strain's ID. */
|
|
533
|
+
strain: string;
|
|
534
|
+
/** What the lab calls it. */
|
|
535
|
+
name: string;
|
|
536
|
+
/** The organism it is, as a person would say it: `Saccharomyces cerevisiae W303`. */
|
|
537
|
+
host: string;
|
|
538
|
+
/** The checksums of the parts it carries. None, for a host nothing has been put into. */
|
|
539
|
+
carries: string[];
|
|
540
|
+
/** The strain it was made from, if the lab has that one. */
|
|
541
|
+
parent: string | null;
|
|
542
|
+
/** The run that made it, if one did. */
|
|
543
|
+
from: string | null;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* @returns The change that says the lab has a strain. It makes no culture of it: `addItem` with the
|
|
548
|
+
* strain does that, the way a tube of a part is added after the part.
|
|
549
|
+
*/
|
|
550
|
+
export const addStrain = (strain: StrainRecord): Operation => ({ type: "strain.add", ...strain });
|
|
551
|
+
|
|
552
|
+
/** @returns A new ID for a strain, made the way an item's is. */
|
|
553
|
+
export const newStrainId = (): string => newItemId();
|
|
554
|
+
|
|
555
|
+
/** @returns The change that sets one field on a part (by checksum), or an item, a place or a strain (by ID). */
|
|
556
|
+
export const setField = (on: "part" | "item" | "place" | "strain", id: string, field: string, value: string | number | null): Operation =>
|
|
509
557
|
({ type: "field.set", on, id, field, value });
|
|
510
558
|
|
|
511
559
|
/** @returns A new ID for a place, made the way an item's is. */
|
package/src/merge.ts
CHANGED
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
* the lab accepts, so somebody removed cannot add to the lab afterwards by claiming not to have
|
|
19
19
|
* heard. What they wrote past the cutoff is kept, set aside, and shown.
|
|
20
20
|
* - **When two changes truly overlap, the lower entry ID wins**, and the overlap is reported. It is
|
|
21
|
-
* the same choice on every machine and nobody can arrange it in advance.
|
|
21
|
+
* the same choice on every machine and nobody can arrange it in advance. The one exception is an
|
|
22
|
+
* adviser: where its change overlaps somebody else's, theirs stands whichever ID is lower, so that
|
|
23
|
+
* a key that cannot change what a person wrote cannot do it by writing at the same moment either.
|
|
22
24
|
* - **Between keepers, the one who has been a keeper longer is heard first.**
|
|
23
25
|
*
|
|
24
26
|
* It refuses quietly. An entry that is not allowed is still placed, with its status and the reason,
|
|
@@ -26,9 +28,11 @@
|
|
|
26
28
|
*/
|
|
27
29
|
import { Dseq } from "@biolibs/dseq";
|
|
28
30
|
import { Dseqrecord } from "@biolibs/dseqrecord";
|
|
31
|
+
import { seguid } from "@biolibs/seguid";
|
|
29
32
|
import type { SignOptions, SigningKey } from "@biolibs/opencloning";
|
|
30
33
|
import { entryId, readEntry, verifyEntry, writeEntry, type Operation, type PartEnds, type PartFeature, type PlanInput, type Role, type RunInput, type StandingMm,
|
|
31
|
-
type SignedEntry } from "./log.js";
|
|
34
|
+
type PartKind, type SignedEntry } from "./log.js";
|
|
35
|
+
export type { PartKind, StrainRecord } from "./log.js";
|
|
32
36
|
import { INVENTORY_VERSION, type InventoryDocument, type InventoryPart, type LabModel,
|
|
33
37
|
type PeerModel } from "./model.js";
|
|
34
38
|
|
|
@@ -51,10 +55,12 @@ export interface RosterKey {
|
|
|
51
55
|
removed: { by: string; cutoff: number } | null;
|
|
52
56
|
}
|
|
53
57
|
|
|
54
|
-
/** One part the lab knows of. */
|
|
58
|
+
/** One part the lab knows of: a piece of DNA, or a protein. */
|
|
55
59
|
export interface PartState {
|
|
56
|
-
/** Its checksum, which is its identity. */
|
|
60
|
+
/** Its checksum, which is its identity. A protein's is counted differently from DNA's. */
|
|
57
61
|
checksum: string;
|
|
62
|
+
/** What it is made of. */
|
|
63
|
+
kind: PartKind;
|
|
58
64
|
/** Its bases. */
|
|
59
65
|
sequence: string;
|
|
60
66
|
/** Whether it is a circle. */
|
|
@@ -75,12 +81,37 @@ export interface PartState {
|
|
|
75
81
|
added: string;
|
|
76
82
|
}
|
|
77
83
|
|
|
78
|
-
/** One
|
|
84
|
+
/** One strain the lab keeps. */
|
|
85
|
+
export interface StrainState {
|
|
86
|
+
/** Its ID. */
|
|
87
|
+
strain: string;
|
|
88
|
+
/** What the lab calls it. */
|
|
89
|
+
name: string;
|
|
90
|
+
/** The organism it is. */
|
|
91
|
+
host: string;
|
|
92
|
+
/** The checksums of the parts it carries. */
|
|
93
|
+
carries: string[];
|
|
94
|
+
/** The strain it was made from, if the lab has that one. */
|
|
95
|
+
parent: string | null;
|
|
96
|
+
/** The run that made it, if one did. */
|
|
97
|
+
from: string | null;
|
|
98
|
+
/** Anything else set on it. */
|
|
99
|
+
fields: Record<string, string | number | null>;
|
|
100
|
+
/** The entry that added it. */
|
|
101
|
+
added: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** One physical item: a tube of a molecule, or a culture of a strain. */
|
|
79
105
|
export interface ItemState {
|
|
80
106
|
/** Its ID. */
|
|
81
107
|
item: string;
|
|
82
|
-
/**
|
|
83
|
-
|
|
108
|
+
/**
|
|
109
|
+
* The checksum of the part it is an item of, or `null` if it is a culture. Exactly one of this
|
|
110
|
+
* and `strain` is set.
|
|
111
|
+
*/
|
|
112
|
+
part: string | null;
|
|
113
|
+
/** The strain it is a culture of, or `null` if it is a tube of a molecule. */
|
|
114
|
+
strain: string | null;
|
|
84
115
|
/** What one of its amount is, if anybody said: a tube, a microlitre. */
|
|
85
116
|
unit: string | null;
|
|
86
117
|
/** How much there is, by the log. Always a whole number of the unit. */
|
|
@@ -230,6 +261,8 @@ export interface LabState {
|
|
|
230
261
|
roster: RosterKey[];
|
|
231
262
|
/** Every part. */
|
|
232
263
|
parts: PartState[];
|
|
264
|
+
/** Every strain. */
|
|
265
|
+
strains: StrainState[];
|
|
233
266
|
/** Every item. */
|
|
234
267
|
items: ItemState[];
|
|
235
268
|
/** Every place. */
|
|
@@ -277,6 +310,8 @@ const text = (value: unknown, most: number): value is string =>
|
|
|
277
310
|
/** An amount is a whole number of the item's unit: only whole numbers have one agreed spelling to sign. */
|
|
278
311
|
/** Fields of a place that are changed by their own operations, not by `field.set`. */
|
|
279
312
|
const PLACE_RESERVED = new Set(["place", "kind", "parent", "grid", "standing", "retired", "adrift"]);
|
|
313
|
+
/** The same, for a strain: what it is and came from is fixed when it is added. */
|
|
314
|
+
const STRAIN_RESERVED = new Set(["strain", "host", "carries", "parent", "from"]);
|
|
280
315
|
|
|
281
316
|
/** The most positions a box may have along one side, and the farthest a place may stand or reach, in millimetres. */
|
|
282
317
|
const MOST_SIDE = 100;
|
|
@@ -316,8 +351,15 @@ const MOST_PIECES = 100;
|
|
|
316
351
|
* @returns The molecule's length, checksum and ends.
|
|
317
352
|
* @throws If that is not a sequence, or the ends are not ones a linear molecule of that length can have.
|
|
318
353
|
*/
|
|
319
|
-
function moleculeOf(sequence: string, circular: boolean, ends: unknown): { length: number; checksum: string; ends: PartEnds } {
|
|
354
|
+
function moleculeOf(sequence: string, circular: boolean, ends: unknown, kind: PartKind = "dna"): { length: number; checksum: string; ends: PartEnds } {
|
|
320
355
|
const blunt = { left: 0, right: 0 };
|
|
356
|
+
if (kind === "protein") {
|
|
357
|
+
// A protein has no strands, so no ends and no circle, and its checksum is counted over the
|
|
358
|
+
// amino acids. The same letters as DNA must never give the same checksum.
|
|
359
|
+
if (circular || (ends !== undefined && ends !== null && (ends as PartEnds).left !== 0)) throw new Error("protein");
|
|
360
|
+
if (!/^[ACDEFGHIKLMNPQRSTVWYBXZJUO*]+$/i.test(sequence)) throw new Error("protein");
|
|
361
|
+
return { length: sequence.length, checksum: String(seguid(sequence.toUpperCase(), "{protein}")), ends: blunt };
|
|
362
|
+
}
|
|
321
363
|
if (ends === undefined) { const record = new Dseqrecord(sequence, circular); return { length: record.length, checksum: record.seguid(), ends: blunt }; }
|
|
322
364
|
const given = ends as Partial<PartEnds> | null;
|
|
323
365
|
if (typeof given !== "object" || given === null || !amountOf(given.left) || !amountOf(given.right)) throw new Error("ends");
|
|
@@ -363,10 +405,11 @@ const KNOWN: Readonly<Record<string, Readonly<Record<string, Known>>>> = {
|
|
|
363
405
|
"roster.add": { key: true, person: true, role: true },
|
|
364
406
|
"roster.remove": { key: true, cutoff: true },
|
|
365
407
|
"roster.role": { key: true, role: true },
|
|
366
|
-
"part.add": { sequence: true, circular: true, name: true, ends: { left: true, right: true } },
|
|
408
|
+
"part.add": { sequence: true, circular: true, name: true, kind: true, ends: { left: true, right: true } },
|
|
367
409
|
"part.annotate": { part: true, features: [{ type: true, label: true, strand: true, parts: [{ start: true, end: true }] }] },
|
|
368
410
|
"part.offer": { part: true, available: true },
|
|
369
|
-
"item.add": { item: true, part: true, amount: true, unit: true },
|
|
411
|
+
"item.add": { item: true, part: true, strain: true, amount: true, unit: true },
|
|
412
|
+
"strain.add": { strain: true, name: true, host: true, carries: true, parent: true, from: true },
|
|
370
413
|
"item.adjust": { item: true, by: true },
|
|
371
414
|
"item.recount": { item: true, amount: true },
|
|
372
415
|
"item.retire": { item: true },
|
|
@@ -389,8 +432,14 @@ const KNOWN: Readonly<Record<string, Readonly<Record<string, Known>>>> = {
|
|
|
389
432
|
"field.set": { on: true, id: true, field: true, value: true },
|
|
390
433
|
};
|
|
391
434
|
|
|
392
|
-
/**
|
|
393
|
-
|
|
435
|
+
/**
|
|
436
|
+
* What an adviser may write. Everything else from one is refused.
|
|
437
|
+
*
|
|
438
|
+
* `part.annotate` is here with a condition the roster cannot answer, so it is checked again where
|
|
439
|
+
* the parts are known: an adviser may say what is on a part that nothing is marked on, and may not
|
|
440
|
+
* change what somebody has already marked.
|
|
441
|
+
*/
|
|
442
|
+
const ADVISER_MAY = new Set(["part.add", "part.annotate", "plan.add", "plan.settle"]);
|
|
394
443
|
|
|
395
444
|
/**
|
|
396
445
|
* The first field in a change that this version does not know, if there is one.
|
|
@@ -542,7 +591,7 @@ const hadSeen = (later: Node, earlier: Node): boolean => (later.vc.get(earlier.a
|
|
|
542
591
|
* ```
|
|
543
592
|
*/
|
|
544
593
|
export async function mergeLog(entries: readonly unknown[], options: { lab?: string } = {}): Promise<LabState> {
|
|
545
|
-
const state: LabState = { lab: options.lab ?? null, name: null, roster: [], parts: [], items: [], places: [], crowded: [], runs: [], plans: [], entries: [],
|
|
594
|
+
const state: LabState = { lab: options.lab ?? null, name: null, roster: [], parts: [], items: [], places: [], crowded: [], runs: [], plans: [], strains: [], entries: [],
|
|
546
595
|
waiting: [], discarded: [], overlaps: [], heads: {}, knows: {} };
|
|
547
596
|
|
|
548
597
|
// Read and check every entry on its own, before any of them is believed about the others.
|
|
@@ -730,7 +779,7 @@ function walk(founding: Node, chains: Map<string, Node[]>, chained: Map<string,
|
|
|
730
779
|
// An adviser proposes and describes. It is turned away here, where the roster is known as it
|
|
731
780
|
// stood when the entry was written, from anything that would change what the lab holds.
|
|
732
781
|
if (author.role === "adviser") {
|
|
733
|
-
if (!ADVISER_MAY.has(op.type)) { say("refused", "Its key is an adviser, which may add parts and propose runs and nothing else."); continue; }
|
|
782
|
+
if (!ADVISER_MAY.has(op.type)) { say("refused", "Its key is an adviser, which may add and describe parts and propose runs, and nothing else."); continue; }
|
|
734
783
|
node.adviser = true;
|
|
735
784
|
}
|
|
736
785
|
say("applied"); // whether the change itself makes sense is settled when the inventory is folded
|
|
@@ -798,12 +847,15 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
798
847
|
const write = (set: Map<string, Register>, field: string, node: Node, value: unknown, what: string): void => {
|
|
799
848
|
const held = set.get(field);
|
|
800
849
|
if (held === undefined || hadSeen(node, held.node)) { set.set(field, { node, value }); return; }
|
|
801
|
-
|
|
850
|
+
// Between two that overlap, the lower ID. Between a person's and an adviser's, the person's:
|
|
851
|
+
// an adviser may not change what somebody wrote, and writing at the same moment is not a way round it.
|
|
852
|
+
const wins = (node.adviser === true) !== (held.node.adviser === true) ? node.adviser !== true : node.id < held.node.id;
|
|
802
853
|
state.overlaps.push({ what, kept: wins ? node.id : held.node.id, dropped: wins ? held.node.id : node.id });
|
|
803
854
|
if (wins) set.set(field, { node, value });
|
|
804
855
|
};
|
|
805
856
|
|
|
806
857
|
const places = new Map<string, { place: PlaceState; set: Map<string, Register> }>();
|
|
858
|
+
const strains = new Map<string, { strain: StrainState; set: Map<string, Register> }>();
|
|
807
859
|
/** Each plan, with the entry that settled it, if one has. */
|
|
808
860
|
const plans = new Map<string, { plan: PlanState; settled: Node | null }>();
|
|
809
861
|
/** Where each item was last put. */
|
|
@@ -823,17 +875,20 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
823
875
|
}
|
|
824
876
|
// Both the molecule and its checksum are worked out inside the guard: an entry from another
|
|
825
877
|
// machine must never be able to stop the merge, only to be refused by it.
|
|
878
|
+
const kind = op["kind"] === undefined ? "dna" : op["kind"];
|
|
879
|
+
if (kind !== "dna" && kind !== "protein") { refuse("A part is made of dna or of protein."); continue; }
|
|
826
880
|
let molecule: ReturnType<typeof moleculeOf>;
|
|
827
881
|
try {
|
|
828
|
-
molecule = moleculeOf(op["sequence"], op["circular"], op["ends"]);
|
|
882
|
+
molecule = moleculeOf(op["sequence"], op["circular"], op["ends"], kind);
|
|
829
883
|
} catch {
|
|
830
|
-
refuse("What it carries is not a
|
|
884
|
+
refuse(kind === "protein" ? "What it carries is not a protein: a protein is written in amino acids, is not a circle and has no ends."
|
|
885
|
+
: "What it carries is not a sequence, with ends a molecule of that length can have.");
|
|
831
886
|
continue;
|
|
832
887
|
}
|
|
833
888
|
const checksum = molecule.checksum;
|
|
834
889
|
if (!parts.has(checksum)) {
|
|
835
890
|
parts.set(checksum, {
|
|
836
|
-
part: { checksum, sequence: op["sequence"], circular: op["circular"], length: molecule.length,
|
|
891
|
+
part: { checksum, kind, sequence: op["sequence"], circular: op["circular"], length: molecule.length,
|
|
837
892
|
name: op["name"], offered: false, features: [], ends: molecule.ends, fields: {}, added: node.id },
|
|
838
893
|
set: new Map([["name", { node, value: op["name"] }]]),
|
|
839
894
|
});
|
|
@@ -846,6 +901,13 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
846
901
|
const held = typeof op["part"] === "string" ? parts.get(op["part"]) : undefined;
|
|
847
902
|
if (held === undefined) { refuse("It does not name a part the lab has."); continue; }
|
|
848
903
|
if (!areFeatures(op["features"], held.part.length)) { refuse("What it marks is not a list of features that lie within the part."); continue; }
|
|
904
|
+
// An adviser may describe a part nobody has described, and may not change what somebody has
|
|
905
|
+
// marked. Whether anything is marked yet is read here, where the entries are in the order
|
|
906
|
+
// every machine settled on, so each of them answers it the same way.
|
|
907
|
+
if (node.adviser === true && held.set.has("features")) {
|
|
908
|
+
refuse("Its key is an adviser, which may mark a part only while nothing is marked on it. Somebody has marked this one.");
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
849
911
|
write(held.set, "features", node, op["features"], `what is marked on ${held.part.name}`);
|
|
850
912
|
} else if (op.type === "item.add") {
|
|
851
913
|
if (!(typeof op["item"] === "string" && ITEM_ID.test(op["item"])) || !amountOf(op["amount"]) || op["amount"] < 0
|
|
@@ -853,11 +915,16 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
853
915
|
refuse("It does not carry an item ID, a whole starting amount and a unit.");
|
|
854
916
|
} else if (items.has(op["item"])) {
|
|
855
917
|
refuse("An item with that ID already exists.");
|
|
856
|
-
} else if (
|
|
918
|
+
} else if ((op["part"] === undefined) === (op["strain"] === undefined)) {
|
|
919
|
+
refuse("An item is of a part or a culture of a strain, and not both.");
|
|
920
|
+
} else if (op["part"] !== undefined && (typeof op["part"] !== "string" || !parts.has(op["part"]))) {
|
|
857
921
|
refuse("The part it is an item of is not one the lab has.");
|
|
922
|
+
} else if (op["strain"] !== undefined && (typeof op["strain"] !== "string" || !strains.has(op["strain"]))) {
|
|
923
|
+
refuse("The strain it is a culture of is not one the lab has.");
|
|
858
924
|
} else {
|
|
859
925
|
items.set(op["item"], {
|
|
860
|
-
item: { item: op["item"], part: op["part"],
|
|
926
|
+
item: { item: op["item"], part: (op["part"] as string | undefined) ?? null, strain: (op["strain"] as string | undefined) ?? null,
|
|
927
|
+
unit: op["unit"], amount: 0, retired: false, needsRecount: false, from: null, where: null, fields: {}, added: node.id },
|
|
861
928
|
set: new Map(), base: { node, amount: op["amount"], recount: false }, adjusted: [], overlapped: false,
|
|
862
929
|
});
|
|
863
930
|
}
|
|
@@ -952,12 +1019,12 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
952
1019
|
for (const { output, record, checksum, ends } of made) {
|
|
953
1020
|
if (!parts.has(checksum)) {
|
|
954
1021
|
parts.set(checksum, {
|
|
955
|
-
part: { checksum, sequence: output.sequence, circular: output.circular, length: record.length, name: output.name, offered: false, features: [], ends, fields: {}, added: node.id },
|
|
1022
|
+
part: { checksum, kind: "dna", sequence: output.sequence, circular: output.circular, length: record.length, name: output.name, offered: false, features: [], ends, fields: {}, added: node.id },
|
|
956
1023
|
set: new Map([["name", { node, value: output.name }]]),
|
|
957
1024
|
});
|
|
958
1025
|
}
|
|
959
1026
|
items.set(output.item, {
|
|
960
|
-
item: { item: output.item, part: checksum, unit: output.unit, amount: 0, retired: false, needsRecount: false, from: op["run"] as string,
|
|
1027
|
+
item: { item: output.item, part: checksum, strain: null, unit: output.unit, amount: 0, retired: false, needsRecount: false, from: op["run"] as string,
|
|
961
1028
|
where: null, fields: {}, added: node.id },
|
|
962
1029
|
set: new Map(), base: { node, amount: output.amount, recount: false }, adjusted: [], overlapped: false,
|
|
963
1030
|
});
|
|
@@ -969,6 +1036,25 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
969
1036
|
settings: { ...(op["settings"] as Record<string, string | number>) }, outcome: op["outcome"] as "worked" | "failed",
|
|
970
1037
|
note: op["note"] as string | null, added: node.id,
|
|
971
1038
|
});
|
|
1039
|
+
} else if (op.type === "strain.add") {
|
|
1040
|
+
const carries = op["carries"];
|
|
1041
|
+
if (!(typeof op["strain"] === "string" && ITEM_ID.test(op["strain"])) || !text(op["name"], 200) || !text(op["host"], 200)) {
|
|
1042
|
+
refuse("It does not carry a strain ID, what to call it and what organism it is.");
|
|
1043
|
+
} else if (strains.has(op["strain"])) {
|
|
1044
|
+
refuse("A strain with that ID already exists.");
|
|
1045
|
+
} else if (!Array.isArray(carries) || carries.length > MOST_IN_A_RUN || !carries.every((one) => typeof one === "string" && parts.has(one))) {
|
|
1046
|
+
refuse("What it says the strain carries is not a list of parts the lab has.");
|
|
1047
|
+
} else if (!(op["parent"] === null || (typeof op["parent"] === "string" && strains.has(op["parent"])))) {
|
|
1048
|
+
refuse("The strain it says this one came from is not one the lab has.");
|
|
1049
|
+
} else if (!(op["from"] === null || (typeof op["from"] === "string" && state.runs.some((one) => one.run === op["from"])))) {
|
|
1050
|
+
refuse("The run it says made this strain is not one the lab has.");
|
|
1051
|
+
} else {
|
|
1052
|
+
strains.set(op["strain"], {
|
|
1053
|
+
strain: { strain: op["strain"], name: op["name"], host: op["host"], carries: [...new Set(carries as string[])],
|
|
1054
|
+
parent: op["parent"] as string | null, from: op["from"] as string | null, fields: {}, added: node.id },
|
|
1055
|
+
set: new Map([["name", { node, value: op["name"] }]]),
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
972
1058
|
} else if (op.type === "plan.add") {
|
|
973
1059
|
const problem = planProblem(op, items, parts, places, plans);
|
|
974
1060
|
if (problem !== null) { refuse(problem); continue; }
|
|
@@ -1012,10 +1098,12 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
1012
1098
|
const value = op["value"];
|
|
1013
1099
|
const target = op["on"] === "part" && typeof op["id"] === "string" ? parts.get(op["id"])
|
|
1014
1100
|
: op["on"] === "item" && typeof op["id"] === "string" ? items.get(op["id"])
|
|
1015
|
-
: op["on"] === "place" && typeof op["id"] === "string" ? places.get(op["id"])
|
|
1016
|
-
|
|
1101
|
+
: op["on"] === "place" && typeof op["id"] === "string" ? places.get(op["id"])
|
|
1102
|
+
: op["on"] === "strain" && typeof op["id"] === "string" ? strains.get(op["id"]) : undefined;
|
|
1103
|
+
if (target === undefined) { refuse("It does not name a part, an item, a place or a strain the lab has."); continue; }
|
|
1017
1104
|
if (op["on"] === "place" && typeof field === "string" && PLACE_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
|
|
1018
|
-
if (
|
|
1105
|
+
if (op["on"] === "strain" && typeof field === "string" && STRAIN_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
|
|
1106
|
+
if (field === "name" && (op["on"] === "place" || op["on"] === "strain") && !text(value, 200)) { refuse("A name has to be some text."); continue; }
|
|
1019
1107
|
if (typeof field !== "string" || !FIELD.test(field) || RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
|
|
1020
1108
|
if (!(value === null || amountOf(value) || (typeof value === "string" && value.length <= 2_000))) { refuse("Its value is not text, a whole number or nothing."); continue; }
|
|
1021
1109
|
if (field === "name" && op["on"] === "part" && !text(value, 200)) { refuse("A part's name has to be some text."); continue; }
|
|
@@ -1035,6 +1123,13 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
|
|
|
1035
1123
|
}
|
|
1036
1124
|
state.parts.push(part);
|
|
1037
1125
|
}
|
|
1126
|
+
for (const { strain, set } of strains.values()) {
|
|
1127
|
+
for (const [field, held] of set) {
|
|
1128
|
+
if (field === "name") strain.name = held.value as string;
|
|
1129
|
+
else strain.fields[field] = held.value as string | number | null;
|
|
1130
|
+
}
|
|
1131
|
+
state.strains.push(strain);
|
|
1132
|
+
}
|
|
1038
1133
|
for (const { item, set, base, adjusted, overlapped } of items.values()) {
|
|
1039
1134
|
// A recount replaces the adjustments its author had seen. Everything else still counts.
|
|
1040
1135
|
const since = adjusted.filter((one) => !hadSeen(base.node, one.node)).reduce((sum, one) => sum + one.by, 0);
|