Skip to content
Snippets Groups Projects
Commit 6e713c42 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil :bicyclist:
Browse files

start the grand unification of remove_whatever

parent f20e41f1
No related branches found
No related tags found
No related merge requests found
macro _remove_fn(objname, model_type, idx_type, args...)
body = last(args)
typeof(body) == Expr || throw(DomainError(body, "missing function body"))
plural = :plural in args
plural_s = plural ? "s" : ""
inplace = :inplace in args
fname = Symbol(:remove_, objname, plural_s, inplace ? "!" : "")
idx_var = Symbol(
objname,
idx_type == :Int ? "_idx" :
idx_type == :String ? "_id" :
throw(DomainError(idx_type, "unsupported index type for _remove_fn macro")),
plural_s,
)
if plural
idx_type = AbstractVector{eval(idx_type)}
end
docstring = """
$fname(model::$model_type, $idx_var::$idx_type)
Remove $objname$plural_s from the model of type `$model_type`
$(inplace ? "in-place" : "and return the modified model").
"""
return :(@doc $docstring $fname(model::$model_type, $idx_var::$idx_type) = $body)
end
......@@ -106,62 +106,6 @@ macro add_reactions!(model::Symbol, ex::Expr)
return all_reactions
end
"""
remove_reactions!(model::StandardModel, ids::Vector{String})
Remove all reactions with `ids` from `model`. Note, may result in orphan metabolites.
# Example
```
remove_reactions!(model, ["EX_glc__D_e", "fba"])
```
"""
function remove_reactions!(model::StandardModel, ids::Vector{String})
pop!.(Ref(model.reactions), ids)
end
"""
remove_reaction!(model::StandardModel, id::String)
Remove reaction with `id` from `model`. Note, may result in orphan metabolites.
# Example
```
remove_reaction!(model, "EX_glc__D_e")
```
"""
remove_reaction!(model::StandardModel, id::String) = remove_reactions!(model, [id])
"""
remove_metabolites!(model::StandardModel, ids::Vector{String})
Remove all metabolites with `ids` from `model`.
Warning, this could leave the model inconsistent, e.g. a reaction might
require the deleted metabolite, in which case analysis functions will error.
# Example
```
remove_metabolites!(model, ["atp_c", "adp_c"])
```
"""
function remove_metabolites!(model::StandardModel, ids::Vector{String})
pop!.(Ref(model.metabolites), ids)
end
"""
remove_metabolite!(model::StandardModel, id::String)
Remove metabolite with `id` from `model`.
Warning, this could leave the model inconsistent, e.g. a reaction might
require the deleted metabolite, in which case analysis functions will error.
# Example
```
remove_metabolite!(model, "atp_c")
```
"""
remove_metabolite!(model::StandardModel, id::String) = remove_metabolites!(model, [id])
"""
remove_genes!(
model::StandardModel,
......@@ -239,3 +183,49 @@ end
change_bounds!(n, rxn_ids, lower = lower, upper = upper)
n
end
@_remove_fn reaction StandardModel String inplace begin
delete!(model.reactions, reaction_id)
end
@_remove_fn reaction StandardModel String inplace plural begin
remove_reaction!.(Ref(model), reaction_ids)
end
@_remove_fn reaction StandardModel String begin
remove_reactions(model, [reaction_id])
end
@_remove_fn reaction StandardModel String plural begin
n = copy(model)
n.reactions = copy(model.reactions)
remove_reactions!(n, reaction_ids)
n
end
@_remove_fn metabolite StandardModel String inplace begin
remove_metabolites!(model, [metabolite_id])
end
@_remove_fn metabolite StandardModel String inplace plural begin
remove_reactions!(
model,
[
rid for (rid, rn) in model.reactions if
any(haskey.(Ref(rn.metabolites), metabolite_ids))
],
)
delete!.(Ref(model.metabolites), metabolite_ids)
end
@_remove_fn metabolite StandardModel String plural begin
n = copy(model)
n.reactions = copy(model.reactions)
n.metabolites = copy(model.metabolites)
remove_metabolites!(n, metabolite_ids)
n
end
@_remove_fn metabolite StandardModel String begin
remove_metabolites(model, [metabolite_id])
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