gemsmap 0.3.0__py3-none-any.whl

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,97 @@
1
+ """Normalized multi-field search index, built once when a model is loaded.
2
+
3
+ Every entity gets one lowercase haystack combining all meaningful fields (ids, names, formula,
4
+ equation, participant names, genes, subsystem, EC, annotation values). A query matches when all of
5
+ its tokens occur in the haystack; ranking prefers id/name hits. The frontend keeps an equivalent
6
+ index for instant typing; this endpoint serves scripts and API users.
7
+ """
8
+ import re
9
+ import unicodedata
10
+ from dataclasses import dataclass
11
+
12
+ _SEP = re.compile(r"[\s_\-,;:()\[\]{}|/]+")
13
+
14
+
15
+ def normalize(text: str) -> str:
16
+ t = unicodedata.normalize("NFKD", str(text)).encode("ascii", "ignore").decode().lower()
17
+ return t
18
+
19
+
20
+ def collapsed(text: str) -> str:
21
+ """'glc__D_e' -> 'glc d e' so both 'glc__D' and 'glc d' match."""
22
+ return _SEP.sub(" ", normalize(text)).strip()
23
+
24
+
25
+ def annotation_text(ann) -> str:
26
+ out = []
27
+ for k, v in (ann or {}).items():
28
+ vals = v if isinstance(v, (list, tuple)) else [v]
29
+ out.append(f"{k} " + " ".join(str(x) for x in vals))
30
+ return " ".join(out)
31
+
32
+
33
+ @dataclass
34
+ class Item:
35
+ kind: str
36
+ id: str
37
+ name: str
38
+ detail: str
39
+ id_n: str
40
+ name_n: str
41
+ hay: str
42
+
43
+
44
+ class SearchIndex:
45
+ def __init__(self, model):
46
+ items: list[Item] = []
47
+ met_name = {m.id: m.name or "" for m in model.metabolites}
48
+ for m in model.metabolites:
49
+ fields = [m.id, m.name, m.formula, m.compartment, str(m.charge), annotation_text(m.annotation)]
50
+ items.append(self._item("metabolite", m.id, m.name or m.id, f"{m.formula or ''} · {m.compartment}", fields))
51
+ for r in model.reactions:
52
+ ec = r.annotation.get("ec-code", []) if r.annotation else []
53
+ ec = ec if isinstance(ec, list) else [ec]
54
+ fields = [r.id, r.name, r.reaction, r.subsystem, r.gene_reaction_rule, " ".join(ec),
55
+ " ".join(met_name.get(m.id, "") for m in r.metabolites),
56
+ " ".join(g.name or "" for g in r.genes), annotation_text(r.annotation)]
57
+ items.append(self._item("reaction", r.id, r.name or r.id, r.subsystem or "", fields))
58
+ for g in model.genes:
59
+ fields = [g.id, g.name, " ".join(r.id for r in g.reactions), annotation_text(g.annotation)]
60
+ items.append(self._item("gene", g.id, g.name or g.id, f"{len(g.reactions)} reactions", fields))
61
+ self.items = items
62
+
63
+ @staticmethod
64
+ def _item(kind, id_, name, detail, fields) -> Item:
65
+ raw = " ".join(normalize(f) for f in fields if f)
66
+ hay = raw + " " + collapsed(raw)
67
+ return Item(kind, id_, name, detail, normalize(id_), normalize(name or ""), hay)
68
+
69
+ def search(self, query: str, kinds=None, limit: int = 50) -> list[dict]:
70
+ q = normalize(query).strip()
71
+ if not q:
72
+ return []
73
+ tokens = [t for t in collapsed(q).split(" ") if t] or [q]
74
+ scored = []
75
+ for it in self.items:
76
+ if kinds and it.kind not in kinds:
77
+ continue
78
+ if q not in it.hay and not all(t in it.hay for t in tokens):
79
+ continue
80
+ if it.id_n == q:
81
+ s = 100
82
+ elif it.name_n == q:
83
+ s = 92
84
+ elif it.id_n.startswith(q):
85
+ s = 80
86
+ elif it.name_n.startswith(q):
87
+ s = 72
88
+ elif q in it.id_n:
89
+ s = 55
90
+ elif q in it.name_n:
91
+ s = 48
92
+ else:
93
+ s = 20
94
+ scored.append((s - len(it.id_n) * 0.01, it))
95
+ scored.sort(key=lambda x: -x[0])
96
+ return [{"kind": it.kind, "id": it.id, "name": it.name, "detail": it.detail, "score": round(s, 2)}
97
+ for s, it in scored[:limit]]
@@ -0,0 +1,427 @@
1
+ """FBA, pFBA, loopless FBA and FVA with COBRApy, returned as self-describing SimulationResult dicts.
2
+
3
+ Scientific rules enforced here:
4
+ - fluxes are returned only when the solver status is optimal (never zeros for a failed solve);
5
+ - objectiveValue is always the value of the biological objective (Σ c·v); for pFBA the minimized
6
+ total flux is reported separately in metadata.totalFlux;
7
+ - FVA returns intervals only. No midpoint is computed or treated as a flux distribution;
8
+ - flux sampling (COBRApy OptGP / ACHR) returns the sampled flux distributions and their per-reaction
9
+ statistics; samples that fail COBRApy's validation (bounds, mass balance) are counted and dropped.
10
+ Exchanges are derived from the mean flux vector (a convex combination of feasible samples, hence
11
+ feasible itself); metabolite turnover is not, because |S·v| is not linear in v. The mean is never
12
+ presented as an FBA flux distribution (reactionFluxes stays empty);
13
+ - metabolite turnover = 0.5 · Σ_j |S_ij · v_j|, a derived proxy computed from one flux vector.
14
+
15
+ Runs can be cancelled. FVA is computed by COBRApy in chunks of reactions (each chunk is a regular
16
+ flux_variability_analysis call with the same constraints), so it reports progress and stops between
17
+ chunks; an FVA stopped with "keep" returns only the intervals that were fully computed. A single
18
+ FBA/pFBA/loopless solve cannot be interrupted inside the solver: a cancelled run is discarded when
19
+ the solver returns.
20
+ """
21
+ import math
22
+ import threading
23
+ import time
24
+ import uuid
25
+ from datetime import datetime, timezone
26
+ from typing import Optional
27
+
28
+ import cobra
29
+ from cobra.exceptions import OptimizationError
30
+ from cobra.flux_analysis import flux_variability_analysis, pfba
31
+ from cobra.flux_analysis.loopless import loopless_solution
32
+
33
+ from ..errors import ScienceError, num
34
+ from ..schemas import SimulationRequest
35
+ from . import model_service as ms
36
+
37
+ METHOD_LABEL = {"fba": "FBA", "pfba": "pFBA", "loopless": "Loopless FBA", "fva": "FVA", "sampling": "Flux sampling"}
38
+
39
+
40
+ # ----------------------------------------------------------------------------- jobs (progress + cancel)
41
+ _jobs: dict[str, dict] = {}
42
+ _jobs_lock = threading.Lock()
43
+
44
+
45
+ def job_register(job_id: Optional[str]) -> None:
46
+ if job_id:
47
+ with _jobs_lock:
48
+ _jobs.setdefault(job_id, {"done": 0, "total": 0, "phase": "queued", "cancel": False, "keep": False})
49
+
50
+
51
+ def job_finish(job_id: Optional[str]) -> None:
52
+ if job_id:
53
+ with _jobs_lock:
54
+ _jobs.pop(job_id, None)
55
+
56
+
57
+ def job_status(job_id: str) -> Optional[dict]:
58
+ with _jobs_lock:
59
+ j = _jobs.get(job_id)
60
+ return {k: j[k] for k in ("done", "total", "phase", "cancel")} if j else None
61
+
62
+
63
+ def job_cancel(job_id: str, keep: bool) -> bool:
64
+ with _jobs_lock:
65
+ j = _jobs.get(job_id)
66
+ if not j:
67
+ return False
68
+ j["cancel"], j["keep"] = True, keep
69
+ return True
70
+
71
+
72
+ def _job(job_id: Optional[str]) -> Optional[dict]:
73
+ with _jobs_lock:
74
+ return _jobs.get(job_id) if job_id else None
75
+
76
+
77
+ def _cancelled_error() -> ScienceError:
78
+ return ScienceError("Simulation cancelled", "The run was stopped before it finished; nothing was saved.",
79
+ status=409, detail={"cancelled": True})
80
+
81
+
82
+ def _fva_chunk_size(n: int, processes: int, pfba_factor: Optional[float]) -> int:
83
+ """Reactions per COBRApy FVA call. Every call re-solves the optimum (and pFBA with a pFBA factor)
84
+ and, with processes > 1, starts a worker pool, so chunks are larger in those cases."""
85
+ parts = 6 if processes > 1 else 40 if pfba_factor else 100
86
+ return max(10, math.ceil(n / parts))
87
+
88
+
89
+ def normalize_status(raw: Optional[str]) -> str:
90
+ s = str(raw or "undefined").lower()
91
+ if s == "optimal":
92
+ return "optimal"
93
+ if "infeasible" in s and "unbounded" in s:
94
+ return "infeasible_or_unbounded"
95
+ if "infeasible" in s:
96
+ return "infeasible"
97
+ if "unbounded" in s:
98
+ return "unbounded"
99
+ if s == "error":
100
+ return "error"
101
+ return "undefined"
102
+
103
+
104
+ def metabolite_turnover(model: cobra.Model, fluxes: dict[str, float]) -> dict[str, float]:
105
+ acc = {m.id: 0.0 for m in model.metabolites}
106
+ for r in model.reactions:
107
+ v = fluxes.get(r.id) or 0.0
108
+ if v == 0.0:
109
+ continue
110
+ for m, c in r.metabolites.items():
111
+ acc[m.id] += abs(c * v)
112
+ return {k: 0.5 * v for k, v in acc.items()}
113
+
114
+
115
+ def exchange_fluxes(model: cobra.Model, fluxes: dict[str, float], kinds: dict[str, str]) -> dict[str, dict]:
116
+ out = {}
117
+ for r in model.boundary:
118
+ met, coef = ms.boundary_metabolite(r)
119
+ v = fluxes.get(r.id) or 0.0
120
+ imported = coef * v # > 0: the metabolite enters the system
121
+ out[r.id] = {"metabolite": met, "kind": kinds.get(r.id, "exchange"), "flux": v,
122
+ "uptake": max(0.0, imported), "secretion": max(0.0, -imported)}
123
+ return out
124
+
125
+
126
+ def exchange_intervals(model: cobra.Model, fva: dict[str, dict], kinds: dict[str, str]) -> dict[str, dict]:
127
+ out = {}
128
+ for r in model.boundary:
129
+ iv = fva.get(r.id)
130
+ if not iv or iv["minimum"] is None or iv["maximum"] is None:
131
+ continue
132
+ met, coef = ms.boundary_metabolite(r)
133
+ a, b = coef * iv["minimum"], coef * iv["maximum"]
134
+ out[r.id] = {"metabolite": met, "kind": kinds.get(r.id, "exchange"),
135
+ "fluxMin": iv["minimum"], "fluxMax": iv["maximum"],
136
+ "uptakeMax": max(0.0, a, b), "secretionMax": max(0.0, -min(a, b))}
137
+ return out
138
+
139
+
140
+ def derive_from_fluxes(entry, fluxes: dict) -> dict:
141
+ """Turnover and exchanges for an imported flux vector, using the loaded model's stoichiometry.
142
+
143
+ Only reaction ids present in the model are used; the caller decides whether the vector belongs
144
+ to this model (the UI only offers this when nearly all ids match).
145
+ """
146
+ model = entry.model
147
+ known = {k: float(v) for k, v in fluxes.items() if v is not None and k in model.reactions and math.isfinite(v)}
148
+ kinds = ms.boundary_kinds(model)
149
+ return {
150
+ "metaboliteTurnover": {k: num(v) for k, v in metabolite_turnover(model, known).items()},
151
+ "exchanges": exchange_fluxes(model, known, kinds),
152
+ "matched": len(known), "total": len(fluxes),
153
+ "model": {"filename": entry.filename, "checksum": entry.checksum},
154
+ }
155
+
156
+
157
+ def _diagnostics(status: str, model: cobra.Model, entry, req: SimulationRequest) -> list[str]:
158
+ if status == "optimal":
159
+ return []
160
+ changed = ms.bound_changes(entry, req.state)
161
+ if status in ("infeasible", "infeasible_or_unbounded"):
162
+ try:
163
+ open_uptakes = len(model.medium)
164
+ except Exception: # noqa: BLE001
165
+ open_uptakes = None
166
+ forced = [r.id for r in model.reactions if r.lower_bound > 0 or r.upper_bound < 0]
167
+ hints = [
168
+ "Medium: are the essential substrates available?"
169
+ + (f" ({open_uptakes} exchange reactions currently allow uptake)" if open_uptakes is not None else ""),
170
+ "Objective: can the objective reaction carry flux with these constraints?",
171
+ ]
172
+ if changed:
173
+ hints.append(f"Bounds: {len(changed)} reaction(s) differ from the model file — "
174
+ + ", ".join(c["reaction"] for c in changed[:6]) + ("…" if len(changed) > 6 else ""))
175
+ if forced:
176
+ hints.append(f"Forced fluxes: {len(forced)} reaction(s) must carry non-zero flux (e.g. maintenance) — "
177
+ + ", ".join(forced[:6]) + ("…" if len(forced) > 6 else ""))
178
+ return hints
179
+ if status == "unbounded":
180
+ return ["The objective can grow without limit: look for objective-path reactions with infinite (∞) bounds,",
181
+ "or an uptake left unbounded in the medium."]
182
+ return ["The solver did not return an optimal solution. See the technical details."]
183
+
184
+
185
+ def _objective_value(objective: dict[str, float], fluxes: dict[str, float]) -> float:
186
+ return sum(c * fluxes.get(rid, 0.0) for rid, c in objective.items())
187
+
188
+
189
+ def _objective_label(objective: dict[str, float]) -> str:
190
+ if not objective:
191
+ return "no objective"
192
+ if len(objective) == 1:
193
+ return next(iter(objective))
194
+ return f"{len(objective)}-term objective"
195
+
196
+
197
+ def simulate(entry, method: str, req: SimulationRequest) -> dict:
198
+ if method not in METHOD_LABEL:
199
+ raise ScienceError("Unknown method", method, status=404, hints=list(METHOD_LABEL.values()))
200
+ state, params = req.state, req.params
201
+ job = _job(req.job_id)
202
+ if job and job["cancel"]:
203
+ raise _cancelled_error()
204
+ if job:
205
+ job["phase"] = "solving"
206
+ kinds = ms.boundary_kinds(entry.model)
207
+ started = datetime.now(timezone.utc)
208
+ t0 = time.perf_counter()
209
+ with ms.applied(entry, state) as model:
210
+ objective = ms.objective_coefficients(model)
211
+ direction = model.objective_direction
212
+ if not objective:
213
+ raise ScienceError("No objective defined", "The model state has an empty objective.", status=422,
214
+ hints=["Open the objective editor and choose at least one reaction."])
215
+ try:
216
+ medium = {k: num(v) for k, v in model.medium.items()}
217
+ except Exception: # noqa: BLE001
218
+ medium = {}
219
+ fluxes: Optional[dict[str, float]] = None
220
+ fva: Optional[dict[str, dict]] = None
221
+ objective_value: Optional[float] = None
222
+ status_raw: Optional[str] = None
223
+ message: Optional[str] = None
224
+ extra: dict = {}
225
+ partial: Optional[dict] = None
226
+ sampling: Optional[dict] = None
227
+ try:
228
+ if method == "fba":
229
+ sol = model.optimize(raise_error=False)
230
+ status_raw = sol.status
231
+ if sol.status == "optimal":
232
+ fluxes = sol.fluxes.to_dict()
233
+ else:
234
+ # Establish feasibility first so a failed solve is reported with its real status.
235
+ opt = model.slim_optimize(error_value=None)
236
+ status_raw = "optimal"
237
+ if method == "pfba":
238
+ sol = pfba(model, fraction_of_optimum=params.fraction_of_optimum)
239
+ status_raw = sol.status
240
+ if sol.status == "optimal":
241
+ fluxes = sol.fluxes.to_dict()
242
+ extra["totalFlux"] = num(sol.objective_value)
243
+ elif method == "loopless":
244
+ sol = loopless_solution(model)
245
+ status_raw = sol.status
246
+ if sol.status == "optimal":
247
+ fluxes = sol.fluxes.to_dict()
248
+ elif method == "fva":
249
+ rxns = params.reactions
250
+ if rxns:
251
+ missing = [r for r in rxns if r not in model.reactions]
252
+ if missing:
253
+ raise ScienceError("Unknown reactions in FVA subset", ", ".join(missing[:8]), status=422)
254
+ ids = list(rxns) if rxns else [r.id for r in model.reactions]
255
+ size = _fva_chunk_size(len(ids), params.processes, params.pfba_factor)
256
+ if job:
257
+ job["total"], job["phase"] = len(ids), "fva"
258
+ fva = {}
259
+ for i in range(0, len(ids), size):
260
+ if job and job["cancel"]:
261
+ if job["keep"] and fva:
262
+ partial = {"done": len(fva), "total": len(ids)}
263
+ break
264
+ raise _cancelled_error()
265
+ df = flux_variability_analysis(
266
+ model, reaction_list=ids[i:i + size], loopless=params.loopless,
267
+ fraction_of_optimum=params.fraction_of_optimum, pfba_factor=params.pfba_factor,
268
+ processes=min(params.processes, size))
269
+ fva.update({rid: {"minimum": num(row["minimum"]), "maximum": num(row["maximum"])}
270
+ for rid, row in df.iterrows()})
271
+ if job:
272
+ job["done"] = len(fva)
273
+ objective_value = num(opt)
274
+ elif method == "sampling":
275
+ sampling, partial = _sample(model, params, job)
276
+ extra["sampling"] = sampling["meta"]
277
+ except ScienceError:
278
+ raise
279
+ except OptimizationError as e:
280
+ status_raw = model.solver.status
281
+ message = str(e)
282
+ except Exception as e: # noqa: BLE001 - reported as an explicit 'error' status, never as fluxes
283
+ status_raw = "error"
284
+ message = f"{type(e).__name__}: {e}"
285
+ if job and job["cancel"] and partial is None:
286
+ raise _cancelled_error() # a single solve cannot be interrupted: its answer is discarded
287
+ if fluxes is not None and any(v is None or not math.isfinite(v) for v in fluxes.values()):
288
+ fluxes, status_raw, message = None, "undefined", "The solver returned non-finite fluxes."
289
+ status = normalize_status(status_raw)
290
+ if status != "optimal":
291
+ fluxes, fva, objective_value, sampling = None, None, None, None
292
+ diagnostics = _diagnostics(status, model, entry, req)
293
+ turnover = exchanges = None
294
+ if fluxes is not None:
295
+ objective_value = num(_objective_value(objective, fluxes))
296
+ turnover = {k: num(v) for k, v in metabolite_turnover(model, fluxes).items()}
297
+ exchanges = exchange_fluxes(model, fluxes, kinds)
298
+ fluxes = {k: num(v) for k, v in fluxes.items()}
299
+ elif fva is not None:
300
+ exchanges = exchange_intervals(model, fva, kinds)
301
+ elif sampling is not None:
302
+ mean = {k: v["mean"] for k, v in sampling["summary"].items()}
303
+ objective_value = num(_objective_value(objective, mean)) # = mean objective over the samples (linear)
304
+ exchanges = exchange_fluxes(model, mean, kinds)
305
+ elapsed = time.perf_counter() - t0
306
+ label = METHOD_LABEL[method]
307
+ metadata = {
308
+ "model": {"id": entry.id, "modelId": model.id or "", "name": model.name or "", "filename": entry.filename,
309
+ "checksum": entry.checksum, "format": entry.fmt},
310
+ "state": {"id": state.id, "name": state.name or "Base model"},
311
+ "method": method, "methodLabel": label,
312
+ "solver": ms.solver_name(model), "cobraVersion": cobra.__version__,
313
+ "objective": {"coefficients": objective, "direction": direction},
314
+ "objectiveIsModelDefault": state.objective is None,
315
+ "medium": medium,
316
+ "boundChanges": ms.bound_changes(entry, state),
317
+ "parameters": _params_for(method, params),
318
+ "timestamp": started.isoformat(),
319
+ "elapsedSeconds": round(elapsed, 4),
320
+ "solverStatusRaw": str(status_raw),
321
+ "message": message,
322
+ "tolerance": num(getattr(model, "tolerance", None)),
323
+ **_edit_provenance(entry),
324
+ **extra,
325
+ }
326
+ if partial:
327
+ metadata["partial"] = partial
328
+ if method == "fva":
329
+ metadata["parameters"]["reactionSubset"] = partial["done"]
330
+ return {
331
+ "id": uuid.uuid4().hex[:12],
332
+ "name": (req.name or f"{label} · {state.name or 'Base model'} · {_objective_label(objective)}")
333
+ + (f" (stopped at {partial['done']}/{partial['total']}{' samples' if method == 'sampling' else ''})" if partial else ""),
334
+ "modelId": entry.id,
335
+ "modelStateId": state.id,
336
+ "method": method,
337
+ "solverStatus": status,
338
+ "objectiveValue": objective_value,
339
+ "reactionFluxes": fluxes,
340
+ "fva": fva,
341
+ "metaboliteTurnover": turnover,
342
+ "exchanges": exchanges,
343
+ "samples": {k: sampling[k] for k in ("reactions", "constant", "summary")} if sampling else None,
344
+ "diagnostics": diagnostics,
345
+ "metadata": metadata,
346
+ }
347
+
348
+
349
+ def _edit_provenance(entry) -> dict:
350
+ """Results computed on a structurally edited model record it (the file checksum alone would hide it)."""
351
+ from . import edit_service
352
+ log = edit_service.log_of(entry)
353
+ if not log["applied"]:
354
+ return {}
355
+ return {"modelEdits": {"count": len(log["applied"]), "hash": edit_service.log_hash(log),
356
+ "labels": [t["label"] for t in log["applied"][-20:]]}}
357
+
358
+
359
+ def _params_for(method: str, p) -> dict:
360
+ if method == "pfba":
361
+ return {"fractionOfOptimum": p.fraction_of_optimum}
362
+ if method == "fva":
363
+ return {"fractionOfOptimum": p.fraction_of_optimum, "loopless": p.loopless, "pfbaFactor": p.pfba_factor,
364
+ "reactionSubset": len(p.reactions) if p.reactions else None, "processes": p.processes}
365
+ if method == "sampling":
366
+ return {"samples": p.samples, "sampler": "OptGP" if p.sampler == "optgp" else "ACHR", "thinning": p.thinning,
367
+ "processes": p.processes if p.sampler == "optgp" else 1, "seed": p.seed,
368
+ "objectiveFraction": p.objective_fraction}
369
+ return {}
370
+
371
+
372
+ def _sig(x: float) -> float:
373
+ """Sample values rounded to 6 significant digits (keeps result files small; far below solver tolerance)."""
374
+ return float(f"{x:.6g}")
375
+
376
+
377
+ def _sample(model: cobra.Model, p, job) -> tuple[dict, Optional[dict]]:
378
+ """Hit-and-run sampling with progress and cancel: one sampler (one warmup), samples drawn in batches."""
379
+ import pandas as pd
380
+ from cobra.sampling import ACHRSampler, OptGPSampler
381
+ from cobra.util.solver import fix_objective_as_constraint
382
+ if p.objective_fraction is not None:
383
+ fix_objective_as_constraint(model, fraction=p.objective_fraction) # reverted with the state context
384
+ if job:
385
+ job["phase"], job["total"], job["done"] = "warmup", p.samples, 0
386
+ t0 = time.perf_counter()
387
+ if p.sampler == "achr":
388
+ sampler = ACHRSampler(model, thinning=p.thinning, seed=p.seed)
389
+ else:
390
+ sampler = OptGPSampler(model, thinning=p.thinning, processes=p.processes, seed=p.seed)
391
+ warmup = time.perf_counter() - t0
392
+ if job:
393
+ job["phase"] = "sampling"
394
+ batch = max(1, min(p.samples, max(50, math.ceil(p.samples / 20))))
395
+ frames, drawn, partial = [], 0, None
396
+ while drawn < p.samples:
397
+ if job and job["cancel"]:
398
+ if job["keep"] and drawn:
399
+ partial = {"done": drawn, "total": p.samples}
400
+ break
401
+ raise _cancelled_error()
402
+ df = sampler.sample(min(batch, p.samples - drawn))
403
+ frames.append(df)
404
+ drawn += len(df)
405
+ if job:
406
+ job["done"] = min(drawn, p.samples)
407
+ allf = pd.concat(frames, ignore_index=True).iloc[:p.samples]
408
+ codes = list(sampler.validate(allf))
409
+ valid = allf[[c == "v" for c in codes]]
410
+ if not len(valid):
411
+ raise ScienceError("No valid flux sample", f"All {len(allf)} samples failed COBRApy's validation.", status=422,
412
+ hints=["Check that the model state is feasible (run FBA first).", "Try the OptGP sampler or a larger thinning."])
413
+ reactions, constant, summary = {}, {}, {}
414
+ for rid in valid.columns:
415
+ col = valid[rid].to_numpy(dtype=float)
416
+ lo, hi = float(col.min()), float(col.max())
417
+ q = [float(x) for x in pd.Series(col).quantile([0.025, 0.25, 0.5, 0.75, 0.975])]
418
+ summary[rid] = {"mean": num(float(col.mean())), "sd": num(float(col.std(ddof=1)) if len(col) > 1 else 0.0),
419
+ "median": num(q[2]), "q025": num(q[0]), "q25": num(q[1]), "q75": num(q[3]), "q975": num(q[4]),
420
+ "min": num(lo), "max": num(hi)}
421
+ if hi - lo <= 1e-9 * max(1.0, abs(hi)):
422
+ constant[rid] = num(float(col.mean())) # the same value in every sample: stored once
423
+ else:
424
+ reactions[rid] = [_sig(x) for x in col]
425
+ meta = {"n": int(len(valid)), "drawn": int(len(allf)), "invalid": int(len(allf) - len(valid)),
426
+ "warmupSeconds": round(warmup, 3), "variable": len(reactions), "constant": len(constant)}
427
+ return {"reactions": reactions, "constant": constant, "summary": summary, "meta": meta}, partial
gemsmap/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ """GEMsMAP: integrating constraint-based modeling, metabolic visualization and identity-aware pathway templates.
2
+
3
+ The installed package runs the COBRAGUI backend (package ``cobragui``) and serves the built web interface on the
4
+ same address; the ``gemsmap`` command starts it and opens the browser. Version scheme: PEP 440 (0.3.0b1 = beta 1).
5
+ """
6
+ __version__ = "0.3.0"
gemsmap/__main__.py ADDED
@@ -0,0 +1,6 @@
1
+ """python -m gemsmap — same as the gemsmap command."""
2
+ import sys
3
+
4
+ from .cli import main
5
+
6
+ sys.exit(main())