Skip to content
Snippets Groups Projects
Unverified Commit 0d2ed376 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil :bicyclist:
Browse files

add a reference-to-a-disk-stored-model wrapper

parent de0d158b
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SBML = "e5567a89-2604-4b09-9718-f5f78e97c3bb"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
......
......@@ -10,6 +10,7 @@ using MAT
using MacroTools
using OrderedCollections
using Random
using Serialization
using SparseArrays
using Statistics
......
"""
mutable struct Serialized{M <: MetabolicModel}
m::Maybe{M}
filename::String
end
A meta-model that represents a model that is serialized on the disk. The
internal model will be loaded on-demand by using any accessor, or by calling
[`precache!`](@ref) directly.
"""
mutable struct Serialized{M} <: MetabolicModel where {M<:MetabolicModel}
m::Maybe{M}
filename::String
end
function _on_precached(m::Serialized, f)
precache!(m)
f(m.m)
end
reactions(m::Serialized) = _on_precached(m, reactions)
n_reactions(m::Serialized) = _on_precached(m, n_reactions)
metabolites(m::Serialized) = _on_precached(m, metabolites)
n_metabolites(m::Serialized) = _on_precached(m, n_metabolites)
stoichiometry(m::Serialized) = _on_precached(m, stoichiometry)
bounds(m::Serialized) = _on_precached(m, bounds)
balance(m::Serialized) = _on_precached(m, balance)
objective(m::Serialized) = _on_precached(m, objective)
coupling(m::Serialized) = _on_precached(m, coupling)
n_coupling_constraints(m::Serialized) = _on_precached(m, n_coupling_constraints)
coupling_bounds(m::Serialized) = _on_precached(m, coupling_bounds)
genes(m::Serialized) = _on_precached(m, genes)
n_genes(m::Serialized) = _on_precached(m, n_genes)
metabolite_formula(m::Serialized) = _on_precached(m, metabolite_formula)
metabolite_charge(m::Serialized) = _on_precached(m, metabolite_charge)
reaction_annotations(m::Serialized) = _on_precached(m, reaction_annotations)
metabolite_annotations(m::Serialized) = _on_precached(m, metabolite_annotations)
gene_annotations(m::Serialized) = _on_precached(m, gene_annotations)
reaction_nodes(m::Serialized) = _on_precached(m, reaction_nodes)
metabolite_nodes(m::Serialized) = _on_precached(m, metabolite_nodes)
gene_notes(m::Serialized) = _on_precached(m, gene_notes)
metabolite_compartment(m::Serialized) = _on_precached(m, metabolite_compartment)
reaction_subsystem(m::Serialized) = _on_precached(m, reaction_subsystem)
"""
precache!(model::Serialized{MetabolicModel})::Nothing
Load the `Serialized` model from disk in case it's not alreadly loaded.
"""
function precache!(model::Serialized)::Nothing
if isnothing(model.m)
model.m = deserialize(model.filename)
end
nothing
end
......@@ -305,3 +305,21 @@ return `nothing`.
function reaction_subsystem(model::MetabolicModel, reaction_id::String)::Maybe{String}
return nothing
end
"""
precache!(a::MetabolicModel)::Nothing
Do whatever is feasible to get the model into a state that can be read from
as-quickly-as-possible. This may include e.g. generating helper index
structures and loading delayed parts of the model from disk. The model should
be modified "transparently" in-place. Analysis functions call this right before
applying modifications or converting the model to the optimization model using
[`make_optimization_model`](@ref); usually on the same machine where the
optimizers (and, generally, the core analysis algorithms) will run. The calls
are done in a good hope that the performance will be improved.
By default, it should be safe to do nothing.
"""
function precache!(a::MetabolicModel)::Nothing
nothing
end
"""
serialize_model(model::MM, filename::String)::Serialized{MM} where {MM<:MetabolicModel}
Serialize the `model` to file `filename`, returning a [`Serialized`](@ref)
model that is able to load itself back automatically upon precaching by
[`precache!`](@ref).
"""
function serialize_model(
model::MM,
filename::String,
)::Serialized{MM} where {MM<:MetabolicModel}
open(f -> serialize(f, model), filename, "w")
Serialized{MM}(nothing, filename)
end
"""
serialize_model(model::Serialized, filename::String)::Serialized
Specialization of [`serialize_model`](@ref) that prevents nested serialization
of already-serialized models.
"""
function serialize_model(model::Serialized, filename::String)
precache!(model)
serialize_model(model.m, filename)
end
File moved
"""
Base.show(io::IO, ::MIME"text/plain", m::Serialized{M}) where {M}
Show the [`Serialized`](@ref) model without unnecessarily loading it.
"""
function Base.show(io::IO, ::MIME"text/plain", m::Serialized{M}) where {M}
print(
io,
"Serialized{$M} saved in \"$(m.filename)\" ($(isnothing(m.m) ? "not loaded" : "loaded"))",
)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment