Unverified Commit 730aaafd authored by St. Elmo's avatar St. Elmo
Browse files

fixed model maniplation tests

parent 9045ae71
......@@ -32,33 +32,33 @@ function Base.:+(
end
function mkrxn(substrates, products)
metdict = Dict{Metabolite,Float64}()
metdict = Dict{String,Float64}()
if typeof(substrates) == Metabolite
substrates != && (metdict[substrates] = get(metdict, substrates, 0.0) - 1.0)
substrates != && (metdict[substrates.id] = get(metdict, substrates.id, 0.0) - 1.0)
elseif typeof(substrates) == MetaboliteWithCoefficient
substrates.metabolite != && (
metdict[substrates.metabolite] =
get(metdict, substrates.metabolite, 0.0) - 1.0 * abs(substrates.coeff)
metdict[substrates.metabolite.id] =
get(metdict, substrates.metabolite.id, 0.0) - 1.0 * abs(substrates.coeff)
)
else
for mwc in substrates
metdict[mwc.metabolite] =
get(metdict, mwc.metabolite, 0.0) - 1.0 * abs(mwc.coeff)
metdict[mwc.metabolite.id] =
get(metdict, mwc.metabolite.id, 0.0) - 1.0 * abs(mwc.coeff)
end
end
if typeof(products) == Metabolite
products != && (metdict[products] = get(metdict, products, 0.0) + 1.0)
products != && (metdict[products.id] = get(metdict, products.id, 0.0) + 1.0)
elseif typeof(products) == MetaboliteWithCoefficient
products.metabolite != && (
metdict[products.metabolite] =
get(metdict, products.metabolite, 0.0) + abs(products.coeff)
metdict[products.metabolite.id] =
get(metdict, products.metabolite.id, 0.0) + abs(products.coeff)
)
else
for mwc in products
metdict[mwc.metabolite] =
get(metdict, mwc.metabolite, 0.0) + 1.0 * abs(mwc.coeff)
metdict[mwc.metabolite.id] =
get(metdict, mwc.metabolite.id, 0.0) + 1.0 * abs(mwc.coeff)
end
end
......
......@@ -10,12 +10,7 @@ function add!(model::StandardModel, rxns::Vector{Reaction})
end
function add!(model::StandardModel, rxn::Reaction)
if model[rxn] == -1
push!(model.reactions, rxn)
else
@warn "$(rxn.id) already present in model."
end
return nothing
model.reactions[rxn.id] = rxn
end
"""
......@@ -30,12 +25,7 @@ function add!(model::StandardModel, mets::Vector{Metabolite})
end
function add!(model::StandardModel, met::Metabolite)
if model[met] == -1
push!(model.metabolites, met)
else
@warn "$(met.id) already present in model."
end
return nothing
model.metabolites[met.id] = met
end
"""
......@@ -50,12 +40,7 @@ function add!(model::StandardModel, genes::Vector{Gene})
end
function add!(model::StandardModel, gene::Gene)
if model[gene] == -1
push!(model.genes, gene)
else
@warn "$(gene.id) already present in model."
end
return nothing
model.genes[gene.id] = gene
end
"""
......@@ -119,21 +104,14 @@ end
Remove all `rxn(s)` from `model` if the `id`s match those in `rxns`.
"""
function rm!(model::StandardModel, rxns::Union{Vector{Reaction},Reaction})
new_rxn_list = Reaction[]
for r in model.reactions
if typeof(rxns) == Reaction
if rxns.id != r.id
push!(new_rxn_list, r)
end
else
if !(r.id in [rr.id for rr in rxns])
push!(new_rxn_list, r)
end
end
function rm!(model::StandardModel, rxns::Vector{Reaction})
for rxn in rxns
rm!(model, rxn)
end
model.reactions = new_rxn_list
return nothing
end
function rm!(model::StandardModel, rxn::Reaction)
delete!(model.reactions, rxn.id)
end
"""
......@@ -141,21 +119,14 @@ end
Remove `met(s)` from `model` based on metabolite `id`.
"""
function rm!(model::StandardModel, mets::Union{Vector{Metabolite},Metabolite})
new_met_list = Metabolite[]
for m in model.metabolites
if typeof(mets) == Metabolite
if mets.id != m.id
push!(new_met_list, m)
end
else
if !(m.id in [mm.id for mm in mets])
push!(new_met_list, m)
end
end
function rm!(model::StandardModel, mets::Vector{Metabolite})
for m in mets
rm!(model, m)
end
model.metabolites = new_met_list
return nothing
end
function rm!(model::StandardModel, met::Metabolite)
delete!(model.metabolites, met.id)
end
"""
......@@ -163,74 +134,12 @@ end
Remove `gene(s)` from `model` based on gene `id`.
"""
function rm!(model::StandardModel, genes::Union{Vector{Gene},Gene})
new_gene_list = Gene[]
for g in model.genes
if typeof(genes) == Gene
if genes.id != g.id
push!(new_gene_list, g)
end
else
if !(g.id in [gg.id for gg in genes])
push!(new_gene_list, g)
end
end
function rm!(model::StandardModel, genes::Vector{Gene})
for gene in genes
rm!(model, gene)
end
model.genes = new_gene_list
return nothing
end
"""
fix_model!(model::StandardModel)
Inspect metabolites and genes of `model` relative to the reactions of `model`.
Remove genes or metabolites that are not used in the reactions.
Add genes or metabolites that are not present in `model.genes` or `model.metabolites` but are used in `model.reactions`.
Everything is based on the `id` of metabolites and genes, thus is it possible for a metabolite or gene to be duplicated but using
a different `id`. Other functions are provided to help identify these cases.
See also: [`check_duplicate_annotations`](@ref), [`check_duplicate_reaction`](@ref), [`check_same_formula`](@ref).
"""
function fix_model!(model::StandardModel)
rxn_mets = Metabolite[] # list of metabolites used in reactions
for rxn in model.reactions
for met in keys(rxn.metabolites)
if rxn_mets[met] == -1
push!(rxn_mets, met)
end
end
end
rxn_genes = Gene[] # list of genes used in reactions
for rxn in model.reactions
for gene_list in rxn.grr # for [] in [[]]
for gene in gene_list
if rxn_genes[gene] == -1
push!(rxn_genes, gene)
end
end
end
end
model_gene_ids = [x.id for x in model.genes]
model_mets_ids = [x.id for x in model.metabolites]
rxn_gene_ids = [x.id for x in rxn_genes]
rxn_mets_ids = [x.id for x in rxn_mets]
extra_genes = setdiff(model_gene_ids, rxn_gene_ids)
!isempty(extra_genes) && rm!(model, [findfirst(model.genes, x) for x in extra_genes])
extra_mets = setdiff(model_mets_ids, rxn_mets_ids)
!isempty(extra_mets) &&
rm!(model, [findfirst(model.metabolites, x) for x in extra_mets])
missing_genes = setdiff(rxn_gene_ids, model_gene_ids)
for mg in missing_genes
g = findfirst(rxn_genes, mg)
add!(model, g)
end
missing_mets = setdiff(rxn_mets_ids, model_mets_ids)
for mm in missing_mets
m = findfirst(rxn_mets, mm)
add!(model, m)
end
function rm!(model::StandardModel, gene::Gene)
delete!(model.genes, gene.id)
end
@testset "Construction overloading" begin
model_path = download_data_file(
"http://bigg.ucsd.edu/static/models/iJO1366.json",
"http://bigg.ucsd.edu/static/models/.json",
joinpath("data", "iJO1366.json"),
"9376a93f62ad430719f23e612154dd94c67e0d7c9545ed9d17a4d0c347672313",
)
model = read_model(model_path, StandardModel)
rxn_original = findfirst(model.reactions, "NADH16pp")
nadh = findfirst(model.metabolites, "nadh_c")
h_c = findfirst(model.metabolites, "h_c")
q8 = findfirst(model.metabolites, "q8_c")
q8h2 = findfirst(model.metabolites, "q8h2_c")
nad = findfirst(model.metabolites, "nad_c")
h_p = findfirst(model.metabolites, "h_p")
rxn_original = model.reactions["NADH16pp"]
nadh = model.metabolites["nadh_c"]
h_c = model.metabolites["h_c"]
q8 = model.metabolites["q8_c"]
q8h2 = model.metabolites["q8h2_c"]
nad = model.metabolites["nad_c"]
h_p = model.metabolites["h_p"]
rxn = nadh + 4.0 * h_c + 1.0 * q8 1.0 * q8h2 + 1.0 * nad + 3.0 * h_p
@test rxn.lb == 0.0 && rxn.ub > 0.0
......@@ -35,7 +35,7 @@
rxn = 1.0 * nadh + 4.0 * h_c + 1.0 * q8 1.0 * q8h2 + 1.0 * nad + 3.0 * h_p
@test prod(values(rxn.metabolites)) == -12
@test ("q8h2_c" in [x.id for x in keys(rxn.metabolites)])
@test ("q8h2_c" in [x for x in keys(rxn.metabolites)])
rxn = nadh + 4.0 * h_c + 1.0 * q8 1.0 * q8h2 + 1.0 * nad + 3.0 * h_p
@test rxn.lb == 0.0 && rxn.ub > 0.0
......
......@@ -17,20 +17,20 @@
g6 = Gene("g6")
g7 = Gene("g7")
r1 = Reaction("r1", Dict(m1 => -1.0, m2 => 1.0), :forward)
r2 = Reaction("r2", Dict(m2 => -2.0, m3 => 1.0), :bidirectional)
r2.grr = [[g2], [g1, g3]]
r3 = Reaction("r3", Dict(m1 => -1.0, m4 => 2.0), :reverse)
r4 = Reaction("r4", Dict(m1 => -5.0, m4 => 2.0), :reverse)
r5 = Reaction("r5", Dict(m1 => -11.0, m4 => 2.0, m3 => 2.0), :reverse)
r1 = Reaction("r1", Dict(m1.id => -1.0, m2.id => 1.0), :forward)
r2 = Reaction("r2", Dict(m2.id => -2.0, m3.id => 1.0), :bidirectional)
r2.grr = [["g2"], ["g1", "g3"]]
r3 = Reaction("r3", Dict(m1.id => -1.0, m4.id => 2.0), :reverse)
r4 = Reaction("r4", Dict(m1.id => -5.0, m4.id => 2.0), :reverse)
r5 = Reaction("r5", Dict(m1.id => -11.0, m4.id => 2.0, m3.id => 2.0), :reverse)
rxns = [r1, r2]
model = StandardModel()
model.id = "model"
model.reactions = rxns
model.metabolites = mets
model.genes = genes
model.reactions = OrderedDict(r.id=>r for r in rxns)
model.metabolites = OrderedDict(m.id=>m for m in mets)
model.genes = OrderedDict(g.id => g for g in genes)
### reactions
add!(model, [r3, r4])
......@@ -71,10 +71,4 @@
rm!(model, g1)
@test length(model.genes) == 4
fix_model!(model)
@test (
length(model.reactions) == 2 &&
length(model.metabolites) == 4 &&
length(model.genes) == 3
)
end
......@@ -13,15 +13,15 @@
v3, C, -500, 500
end
rxn = mod.reactions[1]
rxn = mod.reactions["v1"]
@test rxn.lb == -1000.0
@test rxn.ub == 1000.0
rxn = mod.reactions[2]
rxn = mod.reactions["v2"]
@test rxn.lb == -500
@test rxn.ub == 1000.0
rxn = mod.reactions[3]
rxn = mod.reactions["v3"]
@test rxn.lb == -500
@test rxn.ub == 500
end
Supports Markdown
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