@biolibs/bionet 1.8.3 → 1.8.5

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/src/merge.ts CHANGED
@@ -101,17 +101,36 @@ export interface StrainState {
101
101
  added: string;
102
102
  }
103
103
 
104
- /** One physical item: a tube of a molecule, or a culture of a strain. */
104
+ /** Something the lab physically has and cannot yet name. */
105
+ export interface SpecimenState {
106
+ /** Its ID. */
107
+ specimen: string;
108
+ /** What the lab would write on the tube. Never a claim about what is in it. */
109
+ name: string;
110
+ /** Anything said about where it came from. */
111
+ note: string | null;
112
+ /** The checksum it turned out to be, once an identification said so, or `null` while unknown. */
113
+ is: string | null;
114
+ /** Anything else set on it. */
115
+ fields: Record<string, string | number | null>;
116
+ /** The entry that added it. */
117
+ added: string;
118
+ }
119
+
120
+ /** One physical item: a tube of a molecule, a culture of a strain, or a tube of a specimen. */
105
121
  export interface ItemState {
106
122
  /** Its ID. */
107
123
  item: string;
108
124
  /**
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.
125
+ * The checksum of the part it is an item of, or `null`. Exactly one of this, `strain` and
126
+ * `specimen` is set when it is added; a specimen that has since been identified reads with both
127
+ * `specimen` and `part`, because the tube is the same tube and is now known.
111
128
  */
112
129
  part: string | null;
113
- /** The strain it is a culture of, or `null` if it is a tube of a molecule. */
130
+ /** The strain it is a culture of, or `null` if it is not a culture. */
114
131
  strain: string | null;
132
+ /** The specimen it is a tube of, or `null`. Set whether or not it has been identified. */
133
+ specimen: string | null;
115
134
  /** What one of its amount is, if anybody said: a tube, a microlitre. */
116
135
  unit: string | null;
117
136
  /** How much there is, by the log. Always a whole number of the unit. */
@@ -204,6 +223,13 @@ export interface PlanState {
204
223
  * would use. The plan is kept and shown; somebody has to look before carrying it out.
205
224
  */
206
225
  wanting: string[];
226
+ /**
227
+ * For a plan still open: the molecules it would use that only a plan the lab has set aside would
228
+ * have made. Refiling a plan leaves the ones downstream of it naming what the old plan would have
229
+ * made, which no entry is wrong for saying and no reader of one entry can see. A molecule still to
230
+ * be ordered is not listed: nothing was ever going to make it here.
231
+ */
232
+ orphaned: string[];
207
233
  /** Anything else set on it, such as which project it is part of. */
208
234
  fields: Record<string, string | number | null>;
209
235
  /** The entry that proposed it. */
@@ -265,6 +291,8 @@ export interface LabState {
265
291
  parts: PartState[];
266
292
  /** Every strain. */
267
293
  strains: StrainState[];
294
+ /** Everything the lab has and cannot yet name. */
295
+ specimens: SpecimenState[];
268
296
  /** Every item. */
269
297
  items: ItemState[];
270
298
  /** Every place. */
@@ -314,6 +342,7 @@ const text = (value: unknown, most: number): value is string =>
314
342
  const PLACE_RESERVED = new Set(["place", "kind", "parent", "grid", "standing", "retired", "adrift"]);
315
343
  /** The same, for a strain: what it is and came from is fixed when it is added. */
316
344
  const STRAIN_RESERVED = new Set(["strain", "host", "carries", "parent", "from"]);
345
+ const SPECIMEN_RESERVED = new Set(["specimen", "note", "is", "added"]);
317
346
 
318
347
  /**
319
348
  * What a plan's fields may not be called.
@@ -323,7 +352,7 @@ const STRAIN_RESERVED = new Set(["strain", "host", "carries", "parent", "from"])
323
352
  * something nobody proposed. A field says what a plan is *about* — which project it is part of,
324
353
  * say — and nothing about what carrying it out would do.
325
354
  */
326
- const PLAN_RESERVED = new Set(["plan", "kind", "instrument", "inputs", "expects", "settings", "note", "by", "settled", "wanting", "added"]);
355
+ const PLAN_RESERVED = new Set(["plan", "kind", "instrument", "inputs", "expects", "settings", "note", "by", "settled", "wanting", "orphaned", "added"]);
327
356
 
328
357
  /** The most positions a box may have along one side, and the farthest a place may stand or reach, in millimetres. */
329
358
  const MOST_SIDE = 100;
@@ -450,8 +479,10 @@ const KNOWN: Readonly<Record<string, Readonly<Record<string, Known>>>> = {
450
479
  "part.add": { sequence: true, circular: true, name: true, kind: true, ends: { left: true, right: true } },
451
480
  "part.annotate": { part: true, features: [{ type: true, label: true, strand: true, parts: [{ start: true, end: true }] }] },
452
481
  "part.offer": { part: true, available: true },
453
- "item.add": { item: true, part: true, strain: true, amount: true, unit: true },
482
+ "item.add": { item: true, part: true, strain: true, specimen: true, amount: true, unit: true },
454
483
  "strain.add": { strain: true, name: true, host: true, carries: true, parent: true, from: true },
484
+ "specimen.add": { specimen: true, name: true, note: true },
485
+ "specimen.identify": { specimen: true, part: true },
455
486
  "item.adjust": { item: true, by: true },
456
487
  "item.recount": { item: true, amount: true },
457
488
  "item.retire": { item: true },
@@ -633,7 +664,7 @@ const hadSeen = (later: Node, earlier: Node): boolean => (later.vc.get(earlier.a
633
664
  * ```
634
665
  */
635
666
  export async function mergeLog(entries: readonly unknown[], options: { lab?: string } = {}): Promise<LabState> {
636
- const state: LabState = { lab: options.lab ?? null, name: null, roster: [], parts: [], items: [], places: [], crowded: [], runs: [], plans: [], strains: [], entries: [],
667
+ const state: LabState = { lab: options.lab ?? null, name: null, roster: [], parts: [], items: [], places: [], crowded: [], runs: [], plans: [], strains: [], specimens: [], entries: [],
637
668
  waiting: [], discarded: [], overlaps: [], heads: {}, knows: {} };
638
669
 
639
670
  // Read and check every entry on its own, before any of them is believed about the others.
@@ -898,6 +929,7 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
898
929
 
899
930
  const places = new Map<string, { place: PlaceState; set: Map<string, Register> }>();
900
931
  const strains = new Map<string, { strain: StrainState; set: Map<string, Register> }>();
932
+ const specimens = new Map<string, { specimen: SpecimenState; set: Map<string, Register> }>();
901
933
  /** Each plan, with the entry that settled it, if one has, and anything set on it. */
902
934
  const plans = new Map<string, { plan: PlanState; settled: Node | null; set: Map<string, Register> }>();
903
935
  /** Where each item was last put. */
@@ -957,15 +989,18 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
957
989
  refuse("It does not carry an item ID, a whole starting amount and a unit.");
958
990
  } else if (items.has(op["item"])) {
959
991
  refuse("An item with that ID already exists.");
960
- } else if ((op["part"] === undefined) === (op["strain"] === undefined)) {
961
- refuse("An item is of a part or a culture of a strain, and not both.");
992
+ } else if ([op["part"], op["strain"], op["specimen"]].filter((one) => one !== undefined).length !== 1) {
993
+ refuse("An item is of a part, a culture of a strain, or a tube of a specimen, and only one of those.");
962
994
  } else if (op["part"] !== undefined && (typeof op["part"] !== "string" || !parts.has(op["part"]))) {
963
995
  refuse("The part it is an item of is not one the lab has.");
964
996
  } else if (op["strain"] !== undefined && (typeof op["strain"] !== "string" || !strains.has(op["strain"]))) {
965
997
  refuse("The strain it is a culture of is not one the lab has.");
998
+ } else if (op["specimen"] !== undefined && (typeof op["specimen"] !== "string" || !specimens.has(op["specimen"]))) {
999
+ refuse("The specimen it is a tube of is not one the lab has.");
966
1000
  } else {
967
1001
  items.set(op["item"], {
968
1002
  item: { item: op["item"], part: (op["part"] as string | undefined) ?? null, strain: (op["strain"] as string | undefined) ?? null,
1003
+ specimen: (op["specimen"] as string | undefined) ?? null,
969
1004
  unit: op["unit"], amount: 0, retired: false, needsRecount: false, from: null, where: null, fields: {}, added: node.id },
970
1005
  set: new Map(), base: { node, amount: op["amount"], recount: false }, adjusted: [], overlapped: false,
971
1006
  });
@@ -1066,7 +1101,7 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1066
1101
  });
1067
1102
  }
1068
1103
  items.set(output.item, {
1069
- item: { item: output.item, part: checksum, strain: null, unit: output.unit, amount: 0, retired: false, needsRecount: false, from: op["run"] as string,
1104
+ item: { item: output.item, part: checksum, strain: null, specimen: null, unit: output.unit, amount: 0, retired: false, needsRecount: false, from: op["run"] as string,
1070
1105
  where: null, fields: {}, added: node.id },
1071
1106
  set: new Map(), base: { node, amount: output.amount, recount: false }, adjusted: [], overlapped: false,
1072
1107
  });
@@ -1097,6 +1132,29 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1097
1132
  set: new Map([["name", { node, value: op["name"] }]]),
1098
1133
  });
1099
1134
  }
1135
+ } else if (op.type === "specimen.add") {
1136
+ if (!(typeof op["specimen"] === "string" && ITEM_ID.test(op["specimen"])) || !text(op["name"], 200)
1137
+ || !(op["note"] === null || text(op["note"], 2_000))) {
1138
+ refuse("It does not carry a specimen ID, what to call it and a note or nothing.");
1139
+ } else if (specimens.has(op["specimen"])) {
1140
+ refuse("A specimen with that ID already exists.");
1141
+ } else {
1142
+ specimens.set(op["specimen"], {
1143
+ specimen: { specimen: op["specimen"], name: op["name"], note: op["note"] as string | null, is: null, fields: {}, added: node.id },
1144
+ set: new Map([["name", { node, value: op["name"] }]]),
1145
+ });
1146
+ }
1147
+ } else if (op.type === "specimen.identify") {
1148
+ const held = typeof op["specimen"] === "string" ? specimens.get(op["specimen"]) : undefined;
1149
+ if (held === undefined) {
1150
+ refuse("The specimen it identifies is not one the lab has.");
1151
+ } else if (!(typeof op["part"] === "string" && parts.has(op["part"]))) {
1152
+ refuse("What it says the specimen is is not a part the lab has.");
1153
+ } else {
1154
+ // Two people may identify the same tube without knowing of each other. This is a register
1155
+ // like any other, so the same rule settles it and every lab reads the same answer.
1156
+ write(held.set, "is", node, op["part"], `what specimen ${op["specimen"]} is`);
1157
+ }
1100
1158
  } else if (op.type === "plan.add") {
1101
1159
  const problem = planProblem(op, items, parts, places, plans);
1102
1160
  if (problem !== null) { refuse(problem); continue; }
@@ -1116,7 +1174,7 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1116
1174
  plan: op["plan"] as string, kind: op["kind"] as string, instrument: op["instrument"] as string | null,
1117
1175
  inputs: (op["inputs"] as PlanInput[]).map((input) => ({ ...(input.item === undefined ? { part: input.part! } : { item: input.item }), amount: input.amount, role: input.role })),
1118
1176
  expects, settings: { ...(op["settings"] as Record<string, string | number>) }, note: op["note"] as string | null,
1119
- by: node.author, settled: null, wanting: [], fields: {}, added: node.id,
1177
+ by: node.author, settled: null, wanting: [], orphaned: [], fields: {}, added: node.id,
1120
1178
  },
1121
1179
  });
1122
1180
  } else if (op.type === "plan.settle") {
@@ -1142,10 +1200,12 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1142
1200
  : op["on"] === "item" && typeof op["id"] === "string" ? items.get(op["id"])
1143
1201
  : op["on"] === "place" && typeof op["id"] === "string" ? places.get(op["id"])
1144
1202
  : op["on"] === "strain" && typeof op["id"] === "string" ? strains.get(op["id"])
1203
+ : op["on"] === "specimen" && typeof op["id"] === "string" ? specimens.get(op["id"])
1145
1204
  : op["on"] === "plan" && typeof op["id"] === "string" ? plans.get(op["id"]) : undefined;
1146
- if (target === undefined) { refuse("It does not name a part, an item, a place, a strain or a plan the lab has."); continue; }
1205
+ if (target === undefined) { refuse("It does not name a part, an item, a place, a strain, a specimen or a plan the lab has."); continue; }
1147
1206
  if (op["on"] === "place" && typeof field === "string" && PLACE_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
1148
1207
  if (op["on"] === "strain" && typeof field === "string" && STRAIN_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
1208
+ if (op["on"] === "specimen" && typeof field === "string" && SPECIMEN_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
1149
1209
  if (op["on"] === "plan" && typeof field === "string" && PLAN_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
1150
1210
  // An adviser may file its own proposals and nothing else. It proposes the work, so leaving it
1151
1211
  // unable to say which project the work is for would have a person filing every step by hand;
@@ -1155,7 +1215,7 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1155
1215
  refuse("An adviser may only set a field on a plan of its own.");
1156
1216
  continue;
1157
1217
  }
1158
- if (field === "name" && (op["on"] === "place" || op["on"] === "strain") && !text(value, 200)) { refuse("A name has to be some text."); continue; }
1218
+ if (field === "name" && (op["on"] === "place" || op["on"] === "strain" || op["on"] === "specimen") && !text(value, 200)) { refuse("A name has to be some text."); continue; }
1159
1219
  if (typeof field !== "string" || !FIELD.test(field) || RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
1160
1220
  if (!(value === null || amountOf(value) || (typeof value === "string" && value.length <= 2_000))) { refuse("Its value is not text, a whole number or nothing."); continue; }
1161
1221
  if (field === "name" && op["on"] === "part" && !text(value, 200)) { refuse("A part's name has to be some text."); continue; }
@@ -1182,15 +1242,34 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1182
1242
  }
1183
1243
  state.strains.push(strain);
1184
1244
  }
1245
+ for (const { specimen, set } of specimens.values()) {
1246
+ for (const [field, held] of set) {
1247
+ if (field === "name") specimen.name = held.value as string;
1248
+ else if (field === "is") specimen.is = held.value as string;
1249
+ else specimen.fields[field] = held.value as string | number | null;
1250
+ }
1251
+ state.specimens.push(specimen);
1252
+ }
1185
1253
  for (const { item, set, base, adjusted, overlapped } of items.values()) {
1186
1254
  // A recount replaces the adjustments its author had seen. Everything else still counts.
1187
1255
  const since = adjusted.filter((one) => !hadSeen(base.node, one.node)).reduce((sum, one) => sum + one.by, 0);
1188
1256
  item.amount = base.amount + since;
1189
1257
  item.needsRecount = item.amount < 0 || overlapped;
1258
+ // Once a specimen has been identified, its tubes read as tubes of that molecule. The tube is
1259
+ // the same tube: it keeps its place, its amount and its history, and says where it came from.
1260
+ if (item.specimen !== null && item.part === null) {
1261
+ item.part = (specimens.get(item.specimen)?.set.get("is")?.value as string | undefined) ?? null;
1262
+ }
1190
1263
  for (const [field, held] of set) item.fields[field] = held.value as string | number | null;
1191
1264
  item.where = (put.get(item.item)?.get("where")?.value ?? null) as ItemState["where"];
1192
1265
  state.items.push(item);
1193
1266
  }
1267
+ // Which molecules the lab could actually put into a plan: one a live tube holds, or one a plan
1268
+ // still open is expected to make. And, separately, the ones whose only maker has been set aside.
1269
+ const heldNow = new Set(state.items.filter((one) => !one.retired && one.amount > 0 && one.part !== null).map((one) => one.part!));
1270
+ const willBeMade = new Set<string>(), wasToBeMade = new Set<string>();
1271
+ for (const { plan } of plans.values())
1272
+ for (const expect of plan.expects) (plan.settled === null ? willBeMade : wasToBeMade).add(expect.checksum);
1194
1273
  // A plan counts on tubes that go on being used after it is written. It is not refused for that:
1195
1274
  // it is kept, and says which of them are no longer what it needs.
1196
1275
  for (const { plan, set } of plans.values()) {
@@ -1200,6 +1279,14 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
1200
1279
  const held = input.item === undefined ? undefined : items.get(input.item)?.item;
1201
1280
  return held !== undefined && (held.retired || held.amount < input.amount) ? [held.item] : [];
1202
1281
  });
1282
+ // Refiling a plan leaves the plans downstream of it pointing at what the old one would have
1283
+ // made. Nothing about either plan is wrong on its own, so neither is refused; what is wrong is
1284
+ // the pair, and only the lab as a whole can see it. A molecule still to be ordered is not this:
1285
+ // it was never any plan's output, so it is left alone. This is the molecule a plan the lab has
1286
+ // set aside would have made, and that nothing open makes now.
1287
+ plan.orphaned = plan.inputs.flatMap((input) =>
1288
+ input.part !== undefined && !heldNow.has(input.part) && !willBeMade.has(input.part) && wasToBeMade.has(input.part)
1289
+ ? [input.part] : []);
1203
1290
  }
1204
1291
  state.plans.push(plan);
1205
1292
  }
package/src/method.ts CHANGED
@@ -106,6 +106,13 @@ export function publicMethod(state: LabState, lab: LabModel, options: MethodOpti
106
106
  const part = state.parts.find((one) => one.checksum === held.part);
107
107
  return { part: held.part, name: part?.name ?? "a part", unit: held.unit };
108
108
  }
109
+ // A tube of something nobody has identified. It has no checksum for the same reason a culture
110
+ // has none — there is no sequence to take one of — so it cannot travel either, and saying what
111
+ // this lab calls it is the most that can honestly be said.
112
+ // What a lab writes on a tube it cannot name is a private note to itself — "the one from the
113
+ // back of the -80" — and says nothing another lab could use. So the step says only that there
114
+ // was something unidentified here, which is the whole of what is honestly known about it.
115
+ if (held.specimen !== null) return { part: null, name: "something the publishing lab had not identified", unit: held.unit };
109
116
  // A culture. It has no checksum, so what travels is what it would have to be a culture of.
110
117
  const strain = state.strains.find((one) => one.strain === held.strain);
111
118
  return { part: null, name: strain?.name ?? "a culture", unit: held.unit,