Unverified Commit 0d629d65 authored by St. Elmo's avatar St. Elmo
Browse files

added metabolite removal function

parent 161baf07
......@@ -271,12 +271,62 @@ function verify_consistency(
end
"""
remove_reactions(m::CoreModel, rxns::Vector{Int})
remove_metabolites(model::CoreModel, metabolites)
Removes a set of `metabolites` from the `model` of type `CoreModel` and returns
a new `CoreModel` without those metabolites. Here, `metabolites` can be either a
string, a vector of strings, an index or a vector of indices. Also removes any
reactions that have no associated metabolites after the metabolites have been
removed.
# Example
```
model = load_model(CoreModel, "e_coli_core.json")
m1 = remove_metabolites(model, ["glc__D_e", "for_c"])
m2 = remove_metabolites(model, "glc__D_e")
m3 = remove_metabolites(model, indexin(["glc__D_e", "for_c"], metabolites(model)))
m4 = remove_metabolites(model, first(indexin(["glc__D_e"], metabolites(model))))
```
"""
function remove_metabolites(model::CoreModel, mets)
mets_to_keep = filter(x -> x mets, 1:n_metabolites(model))
temp_S = model.S[mets_to_keep, :]
(I, rxns_to_keep, val) = findnz(temp_S)
sort!(rxns_to_keep)
unique!(rxns_to_keep)
new_S = model.S[mets_to_keep, rxns_to_keep]
new_b = model.b[mets_to_keep]
new_c = model.c[rxns_to_keep]
new_lbs = model.xl[rxns_to_keep]
new_ubs = model.xu[rxns_to_keep]
new_rxns = model.rxns[rxns_to_keep]
new_mets = model.mets[mets_to_keep]
return CoreModel(new_S, new_b, new_c, new_lbs, new_ubs, new_rxns, new_mets)
end
Remove reaction(s) from a `CoreModel`.
function remove_metabolites(model::CoreModel, met::Integer)
return remove_metabolites(model, [met])
end
Also removes any metabolites not involved in any reaction after the deletion.
function remove_metabolites(model::CoreModel, met::String)
return remove_metabolites(model, [met])
end
function remove_metabolites(model::CoreModel, mets::Vector{String})
met_indices = filter(!isnothing, indexin(mets, metabolites(model)))
if isempty(met_indices)
return model
else
return remove_metabolites(model, met_indices)
end
end
"""
Removes a set of reactions from a CoreModel.
Also removes the metabolites not involved in any reaction.
"""
function remove_reactions(m::CoreModel, rxns::Vector{Int})
rxns_to_keep = filter(e -> e rxns, 1:n_reactions(m))
......
......@@ -192,3 +192,27 @@ end
@test reactions(modLp) == reactions(lp)[2:3]
@test metabolites(modLp) == metabolites(lp)[1:3]
end
@testset "Remove metabolites" begin
core_json = download_data_file(
"http://bigg.ucsd.edu/static/models/e_coli_core.json",
joinpath("data", "e_coli_core.json"),
"7bedec10576cfe935b19218dc881f3fb14f890a1871448fc19a9b4ee15b448d8",
)
model = load_model(CoreModel, core_json)
m1 = remove_metabolites(model, ["glc__D_e", "for_c"])
m2 = remove_metabolites(model, "glc__D_e")
m3 = remove_metabolites(model, indexin(["glc__D_e", "for_c"], metabolites(model)))
m4 = remove_metabolites(model, first(indexin(["glc__D_e"], metabolites(model))))
@test size(stoichiometry(m1)) == (70, 94)
@test size(stoichiometry(m2)) == (71, 94)
@test size(stoichiometry(m3)) == (70, 94)
@test size(stoichiometry(m4)) == (71, 94)
@test any(["glc__D_e", "for_c"] . Ref(metabolites(m1)))
@test any(["glc__D_e"] . Ref(metabolites(m2)))
@test any(["glc__D_e", "for_c"] . Ref(metabolites(m3)))
@test any(["glc__D_e"] . Ref(metabolites(m4)))
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment