straindesign.strainDesignSolutions
Container for strain design solutions (SDSolutions)
Module Contents
- class straindesign.strainDesignSolutions.SDSolutions(model, sd, status, sd_setup, *, _lazy_init=None)[source]
Bases:
objectContainer for strain design solutions
Objects of this class are returned by strain design computations. This class contains the metabolic interventions on the gene, reaction or regulation level alongside with information about the strain design setup, including the model used and the strain design modules. Strain design solutions can be accessed either through the fields or through specific functions that preprocess or reformat strain designs for different purposes.
Instances of this class are not meant to be created by StrainDesign users.
- Parameters:
model (cobra.Model) – A metabolic model that is an instance of the cobra.Model class.
sd (list of dict) – A list of dicts every dict represents an intervention set. Keys in each dict are reaction/gene identifiers and the associated value determines if it is added (1), not added (0) or knocked out (-1). For regulatory interventions, (1) means active regulation and (0) means regulatory intervention not added. These will be translated to True and False.
status (str) – Status string of the computation (e.g.: ‘optimal’)
sd_setup (dict) –
A dictionary containing information about the problem setup. This dict can/should contain the keys MODEL_ID, MODULES, MAX_SOLUTIONS, MAX_COST, TIME_LIMIT, SOLVER, KOCOST, KICOST, REGCOST, GKICOST, GKOCOST
These entries can be set like this: sd_setup[straindesign.MODEL_ID] = model.id
- Returns
(SDSolutions): Strain design solutions
- __iadd__(other)[source]
In-place merge of two SDSolutions objects (deduplicates at compressed level).
- attach_model(model)[source]
Attach the full cobra model so a lazily-loaded result can be expanded (
expand_group()/expand_all()). Pass the original model that was given tocompute_strain_designs. Returnsselffor chaining.
- expand_all(n_per_group=None)[source]
Expand all compressed groups.
- Parameters:
n_per_group – None means all, int means keep up to n per group.
- expand_group(grp_idx)[source]
Expand one compressed group on demand.
Returns list of expanded solution dicts. Also updates reaction_sd, sd_cost, itv_bounds, and group_map in place.
- get_compressed_model()[source]
Return the compressed (GPR-extended) cobra model the MILP was solved on, or
Noneif not restored. It is much smaller than the full model, so validating/analysing the compressed solutions (compressed_sd) in it is fast. Restore it withload(..., cmp_model=True)(rebuilt from the embedded rational snapshot) or supply your own viaload(..., cmp_model= my_model). Its solver backend is built lazily on first optimize.
- get_gene_reac_sd_assoc(i=None)[source]
Get reaction and gene-based strain design solutions, and show which reaction-based solution corresponds to which gene-based.
Often the association is not 1:1 but n:1.
- get_gene_reac_sd_assoc_mark_no_ki(i=None)[source]
Get reaction and gene-based strain design solutions, but also tag knock-ins that were not made with a 0
Often the association is not 1:1 but n:1.
- get_gene_sd_mark_no_ki(i=None)[source]
Get gene-based strain design solutions, but also tag knock-ins that were not made with a 0
- get_group(i)[source]
Get all expanded solution indices that belong to the same compressed group as solution i.
Returns a list of indices into reaction_sd that share the same compressed solution origin. Requires that compute_strain_designs was called with compression enabled.
- get_model()[source]
Return the full cobra model (or
Noneif not attached/restored). Restore an embedded one withload(..., model=True)or attach your own withattach_model().
- get_reaction_sd(i=None)[source]
Get reaction-based strain design solutions
Gene-based intervention sets are translated to the reaction level. This can be helpful to understand the impact of gene interventions. GPR-rules are accounted for automatically.
- get_reaction_sd_bnds(i=None)[source]
Get reaction-based strain design solutions represented by upper and lower bounds
Knocked-out reactions will show as upper and lower bounds of zero.
- get_reaction_sd_mark_no_ki(i=None)[source]
Get reaction-based strain design solutions, but also tag knock-ins that were not made with a 0
This can be helpful to analyze gene intervention sets in original metabolic models. GPR-rules are accounted for automatically.
- get_representative_sd()[source]
Get one representative expanded solution per compressed group.
Returns a list of dicts, one per unique compressed solution.
- get_strain_designs(i=None)[source]
Get i-th strain design (intervention set) or all in original format
- classmethod load(filename, model=None, cmp_model=None)[source]
Load strain design solutions from a file.
The already-materialised solutions,
get_representative_sd(),compressed_sdandcompression_mapare always available immediately and cheaply. The (possibly large) embedded models are rebuilt only if you ask for them, independently for the full and the compressed model.- Parameters:
model (None | True | cobra.Model, default None) –
Controls the full model and the compressed model respectively:
None(default) – do not attach that model.True– rebuild and attach the embedded snapshot (via StrainDesign’s rational-safenetworktools.model_from_dict; its solver backend is built lazily on first optimize). If nothing was embedded, the model staysNone.a
cobra.Model– attach this exact model (e.g. one you still have in memory). You pass the model OBJECT, not a name.
The (re)built/attached models are written ONTO the returned object and retrieved with
get_model()/get_compressed_model()– you do not pass a variable to be filled. The full model is needed to expand compressed groups (expand_all()); the compressed model is the fast vehicle for analysingcompressed_sd.cmp_model (None | True | cobra.Model, default None) –
Controls the full model and the compressed model respectively:
None(default) – do not attach that model.True– rebuild and attach the embedded snapshot (via StrainDesign’s rational-safenetworktools.model_from_dict; its solver backend is built lazily on first optimize). If nothing was embedded, the model staysNone.a
cobra.Model– attach this exact model (e.g. one you still have in memory). You pass the model OBJECT, not a name.
The (re)built/attached models are written ONTO the returned object and retrieved with
get_model()/get_compressed_model()– you do not pass a variable to be filled. The full model is needed to expand compressed groups (expand_all()); the compressed model is the fast vehicle for analysingcompressed_sd.
Examples
>>> sols = SDSolutions.load('res.sd') # no models >>> sols = SDSolutions.load('res.sd', model=True) # full only >>> sols = SDSolutions.load('res.sd', cmp_model=True) # compressed only >>> sols = SDSolutions.load('res.sd', model=True, cmp_model=True) # both >>> sols = SDSolutions.load('res.sd', model=my_model) # attach my own
- save(filename, embed_model=True)[source]
Save strain design solutions to a file (pickle).
The saved file is designed to be a self-contained, portable and reproducible record of a strain-design computation. Alongside the solutions it retains the compressed solution set, the compression map between compressed and full model, and
sd_setup(enough to rerun the exact same computation), and – withembed_model=True(default) – a portable snapshot of the full original model with the exact edits used. This makes it a convenient vessel for publishing, exchanging and reproducing results, or rerunning them on a different machine.Lazy/compressed results are saved as is, WITHOUT forcing a full expansion of every compressed group – full expansion can materialise an enormous number of decompressed designs and, on large problems, makes saving hang or run out of memory even though the search itself already finished (issue #47). To persist fully-expanded solutions instead, call
expand_all()(orexpand_group()) first: expansion materialises the solutions in-place, andsavethen simply pickles them.- Parameters:
filename (str) – Output path.
embed_model (bool, default True) – If True, embed portable, solver-less snapshots of BOTH the full model AND the compressed (GPR-extended) model the MILP was solved on, so the file is fully self-contained and either model can be restored on load (see
load()) without ambiguity, on any machine and across cobra/optlang/solver versions. Snapshots use StrainDesign’s rational-safenetworktools.model_to_dict, so the compressed model’s exact rational bounds/coefficients survive losslessly (no float rounding). Set False to save a leaner file when the reader already has the models.
Notes
The live cobra models (which hold un-picklable solver interfaces) are never pickled; only the embedded dict snapshots are. See
load(),attach_model(),get_model()andget_compressed_model()for restoring/accessing models afterwards.