Unverified Commit 97ba5bea authored by Miroslav Kratochvil's avatar Miroslav Kratochvil
Browse files

reformat all stuff that passed unnoticed

parent 4e503890
Pipeline #42913 passed with stages
in 8 minutes and 47 seconds
......@@ -99,8 +99,11 @@ end
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::CoreModel, rxn_id::String)::Dict{String, Float64}
Dict(m.mets[k]=>v for (k, v) in zip(findnz(m.S[:, first(indexin([rxn_id], m.rxns))])...))
function reaction_stoichiometry(m::CoreModel, rxn_id::String)::Dict{String,Float64}
Dict(
m.mets[k] => v for
(k, v) in zip(findnz(m.S[:, first(indexin([rxn_id], m.rxns))])...)
)
end
"""
......@@ -109,8 +112,8 @@ end
Return the reaction equation of reaction with id `rxn_ind` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::CoreModel, rxn_ind::Int)::Dict{String, Float64}
Dict(m.mets[k]=>v for (k, v) in zip(findnz(m.S[:, rxn_ind])...))
function reaction_stoichiometry(m::CoreModel, rxn_ind::Int)::Dict{String,Float64}
Dict(m.mets[k] => v for (k, v) in zip(findnz(m.S[:, rxn_ind])...))
end
"""
......
......@@ -110,7 +110,7 @@ end
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::CoreModelCoupled, rxn_id::String)::Dict{String, Float64}
function reaction_stoichiometry(m::CoreModelCoupled, rxn_id::String)::Dict{String,Float64}
reaction_stoichiometry(m.lm, rxn_id)
end
......@@ -120,7 +120,7 @@ end
Return the reaction equation of reaction with id `rxn_ind` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::CoreModelCoupled, rxn_ind::Int)::Dict{String, Float64}
function reaction_stoichiometry(m::CoreModelCoupled, rxn_ind::Int)::Dict{String,Float64}
reaction_stoichiometry(m.lm, rxn_ind)
end
......
......@@ -282,7 +282,7 @@ end
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::JSONModel, rxn_id::String)::Dict{String, Float64}
function reaction_stoichiometry(m::JSONModel, rxn_id::String)::Dict{String,Float64}
ind = findfirst(x -> x["id"] == rxn_id, m.json["reactions"])
m.json["reactions"][ind]["metabolites"]
end
......
......@@ -174,7 +174,7 @@ metabolite_compartment(m::MATModel, mid::String) = _maybemap(
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::MATModel, rxn_id::String)::Dict{String, Float64}
function reaction_stoichiometry(m::MATModel, rxn_id::String)::Dict{String,Float64}
rxn_ind = first(indexin([rxn_id], m.mat["rxns"]))
reaction_stoichiometry(m, rxn_ind)
end
......@@ -186,7 +186,7 @@ Return the reaction equation of reaction with index `rxn_index` in model. The re
equation maps metabolite ids to their stoichiometric coefficients. Note, `rxn_index` can
be any suitable type that can index into an array.
"""
function reaction_stoichiometry(m::MATModel, rxn_ind)::Dict{String, Float64}
function reaction_stoichiometry(m::MATModel, rxn_ind)::Dict{String,Float64}
met_inds = findall(m.mat["S"][:, rxn_ind] .!= 0.0)
Dict(m.mat["mets"][met_ind] => m.mat["S"][met_ind, rxn_ind] for met_ind in met_inds)
end
......
......@@ -143,7 +143,7 @@ _sbml_export_notes = _sbml_export_annotation
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::SBMLModel, rxn_id::String)::Dict{String, Float64}
function reaction_stoichiometry(m::SBMLModel, rxn_id::String)::Dict{String,Float64}
m.sbml.reactions[rxn_id].stoichiometry
end
......
......@@ -288,7 +288,7 @@ end
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::StandardModel, rxn_id::String)::Dict{String, Float64}
function reaction_stoichiometry(m::StandardModel, rxn_id::String)::Dict{String,Float64}
m.reactions[rxn_id].metabolites
end
......
......@@ -312,9 +312,12 @@ end
Return the reaction equation of reaction with id `rxn_id` in model. The reaction
equation maps metabolite ids to their stoichiometric coefficients.
"""
function reaction_stoichiometry(m::MetabolicModel, rxn_id::String)::Dict{String, Float64}
function reaction_stoichiometry(m::MetabolicModel, rxn_id::String)::Dict{String,Float64}
mets = metabolites(m)
Dict(mets[k] => v for (k, v) in zip(findnz(stoichiometry(m)[:, first(indexin([rxn_id], reactions(m)))])...))
Dict(
mets[k] => v for (k, v) in
zip(findnz(stoichiometry(m)[:, first(indexin([rxn_id], reactions(m)))])...)
)
end
"""
......
......@@ -59,7 +59,8 @@ function atom_exchange(flux_dict::Dict{String,Float64}, model::StandardModel)
for (met, stoich_rxn) in model.reactions[rxn_id].metabolites
adict = get_atoms(model.metabolites[met])
for (atom, stoich_molecule) in adict
atom_flux[atom] = get(atom_flux, atom, 0.0) + flux * stoich_rxn * stoich_molecule
atom_flux[atom] =
get(atom_flux, atom, 0.0) + flux * stoich_rxn * stoich_molecule
end
end
end
......
......@@ -23,12 +23,14 @@ findall(x -> looks_like_exchange_reaction(x; exclude_biomass=true), reactions(mo
filter(x -> looks_like_exchange_reaction(x; exclude_biomass=true), reactions(model)) # returns Strings
```
"""
function looks_like_exchange_reaction(rxn_id::String;
function looks_like_exchange_reaction(
rxn_id::String;
exclude_biomass = false,
biomass_strings = _constants.biomass_strings,
exchange_prefixes = _constants.exchange_prefixes,
)::Bool
any(startswith(rxn_id, x) for x in exchange_prefixes) && !(exclude_biomass && any(occursin(x, rxn_id) for x in biomass_strings))
any(startswith(rxn_id, x) for x in exchange_prefixes) &&
!(exclude_biomass && any(occursin(x, rxn_id) for x in biomass_strings))
end
"""
......@@ -67,12 +69,14 @@ filter(looks_like_biomass_reaction, reactions(model)) # returns strings
findall(looks_like_biomass_reaction, reactions(model)) # returns indices
```
"""
function looks_like_biomass_reaction(rxn_id::String;
function looks_like_biomass_reaction(
rxn_id::String;
exclude_exchanges = false,
exchange_prefixes = _constants.exchange_prefixes,
biomass_strings = _constants.biomass_strings,
)::Bool
any(occursin(x, rxn_id) for x in biomass_strings) && !(exclude_exchanges && any(startswith(rxn_id, x) for x in exchange_prefixes))
any(occursin(x, rxn_id) for x in biomass_strings) &&
!(exclude_exchanges && any(startswith(rxn_id, x) for x in exchange_prefixes))
end
"""
find_biomass_reactions(m::MetabolicModel; kwargs...)
......@@ -108,7 +112,8 @@ filter(looks_like_exchange_metabolite, metabolites(model)) # returns strings
findall(looks_like_exchange_metabolite, metabolites(model)) # returns indices
```
"""
function looks_like_exchange_metabolite(met_id::String;
function looks_like_exchange_metabolite(
met_id::String;
exchange_suffixes = _constants.exchange_suffixes,
)::Bool
any(endswith(met_id, x) for x in exchange_suffixes)
......
......@@ -21,20 +21,12 @@ function add_reactions(
xu::AbstractFloat;
check_consistency = false,
) where {V1<:VecType,V2<:VecType}
new_lm = add_reactions(
m.lm,
s,
b,
c,
xl,
xu,
check_consistency = check_consistency,
)
new_lm = add_reactions(m.lm, s, b, c, xl, xu, check_consistency = check_consistency)
return CoreModelCoupled(
new_lm,
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm)-n_reactions(m.lm))),
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
m.cl,
m.cu
m.cu,
)
end
......@@ -64,21 +56,21 @@ function add_reactions(
check_consistency = false,
) where {V1<:VecType,V2<:VecType,K<:StringVecType}
new_lm = add_reactions(
m.lm,
s,
b,
c,
xl,
xu,
rxn,
mets,
check_consistency = check_consistency
)
m.lm,
s,
b,
c,
xl,
xu,
rxn,
mets,
check_consistency = check_consistency,
)
return CoreModelCoupled(
new_lm,
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm)-n_reactions(m.lm))),
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
m.cl,
m.cu
m.cu,
)
end
......@@ -103,20 +95,12 @@ function add_reactions(
xu::V;
check_consistency = false,
) where {M<:MatType,V<:VecType}
new_lm = add_reactions(
m.lm,
Sp,
b,
c,
xl,
xu,
check_consistency = check_consistency,
)
new_lm = add_reactions(m.lm, Sp, b, c, xl, xu, check_consistency = check_consistency)
return CoreModelCoupled(
new_lm,
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm)-n_reactions(m.lm))),
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
m.cl,
m.cu
m.cu,
)
end
......@@ -127,16 +111,12 @@ Add all reactions from `m2` to `m1`.
"""
function add_reactions(m1::CoreModelCoupled, m2::CoreModel; check_consistency = false)
new_lm = add_reactions(
m1.lm,
m2,
check_consistency=check_consistency
)
new_lm = add_reactions(m1.lm, m2, check_consistency = check_consistency)
return CoreModelCoupled(
new_lm,
hcat(m1.C, spzeros(size(m1.C, 1), n_reactions(new_lm)-n_reactions(m1.lm))),
hcat(m1.C, spzeros(size(m1.C, 1), n_reactions(new_lm) - n_reactions(m1.lm))),
m1.cl,
m1.cu
m1.cu,
)
end
......@@ -166,21 +146,21 @@ function add_reactions(
check_consistency = false,
) where {M<:MatType,V<:VecType,K<:StringVecType}
new_lm = add_reactions(
m.lm,
Sp,
b,
c,
xl,
xu,
rxns,
mets,
check_consistency = check_consistency
)
m.lm,
Sp,
b,
c,
xl,
xu,
rxns,
mets,
check_consistency = check_consistency,
)
return CoreModelCoupled(
new_lm,
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm)-n_reactions(m.lm))),
hcat(m.C, spzeros(size(m.C, 1), n_reactions(new_lm) - n_reactions(m.lm))),
m.cl,
m.cu
m.cu,
)
end
......@@ -194,11 +174,11 @@ Also removes any metabolites not involved in any reaction after the deletion.
"""
function remove_reactions(m::CoreModelCoupled, rxns::Vector{Int})
return CoreModelCoupled(
remove_reactions(m.lm, rxns),
m.C[:, filter(e -> e rxns, 1:n_reactions(m))],
m.cl,
m.cu
)
remove_reactions(m.lm, rxns),
m.C[:, filter(e -> e rxns, 1:n_reactions(m))],
m.cl,
m.cu,
)
end
"""
......@@ -222,7 +202,8 @@ end
"""
function remove_reactions(m::CoreModelCoupled, rxns::Vector{String})
rxn_indices = [findfirst(isequal(name), m.lm.rxns) for name in intersect(rxns, m.lm.rxns)]
rxn_indices =
[findfirst(isequal(name), m.lm.rxns) for name in intersect(rxns, m.lm.rxns)]
if isempty(rxn_indices)
return m
else
......@@ -453,12 +434,7 @@ function change_bounds!(
xl::V = Float64[],
xu::V = Float64[],
) where {V<:VecType}
change_bounds!(
model.lm,
rxns,
xl = xl,
xu = xu
)
change_bounds!(model.lm, rxns, xl = xl, xu = xu)
end
"""
......@@ -476,10 +452,5 @@ function change_bounds!(
xl::V = Float64[],
xu::V = Float64[],
) where {V<:VecType}
change_bounds!(
model.lm,
rxns,
xl = xl,
xu = xu
)
change_bounds!(model.lm, rxns, xl = xl, xu = xu)
end
......@@ -32,8 +32,8 @@ end
Add `met` to `model` based on metabolite `id`.
"""
add_metabolite!(model::StandardModel, met::Metabolite) = add_metabolites!(model, [met])
add_metabolite!(model::StandardModel, met::Metabolite) = add_metabolites!(model, [met])
"""
add_genes!(model::StandardModel, genes::Vector{Gene})
......@@ -50,7 +50,7 @@ end
Add `gene` to `model` based on gene `id`.
"""
add_gene!(model::StandardModel, gene::Gene) = add_genes!(model, [gene])
add_gene!(model::StandardModel, gene::Gene) = add_genes!(model, [gene])
"""
@add_reactions!(model::Symbol, ex::Expr)
......@@ -130,7 +130,7 @@ Remove reaction with `id` from `model`. Note, may result in orphan metabolites.
remove_reaction!(model, "EX_glc__D_e")
```
"""
remove_reaction!(model::StandardModel, id::String) = remove_reactions!(model, [id])
remove_reaction!(model::StandardModel, id::String) = remove_reactions!(model, [id])
"""
remove_metabolites!(model::StandardModel, ids::Vector{String})
......@@ -145,7 +145,7 @@ remove_metabolites!(model, ["atp_c", "adp_c"])
```
"""
function remove_metabolites!(model::StandardModel, ids::Vector{String})
pop!.(Ref(model.metabolites), ids)
pop!.(Ref(model.metabolites), ids)
end
"""
......@@ -160,7 +160,7 @@ require the deleted metabolite, in which case analysis functions will error.
remove_metabolite!(model, "atp_c")
```
"""
remove_metabolite!(model::StandardModel, id::String) = remove_metabolites!(model, [id])
remove_metabolite!(model::StandardModel, id::String) = remove_metabolites!(model, [id])
"""
remove_genes!(
......@@ -210,7 +210,8 @@ constrain reactions that require the genes to function to carry zero flux.
remove_gene!(model, "g1")
```
"""
remove_gene!(model::StandardModel, gid::String; knockout_reactions::Bool = false) = remove_genes!(model, [gid]; knockout_reactions = knockout_reactions)
remove_gene!(model::StandardModel, gid::String; knockout_reactions::Bool = false) =
remove_genes!(model, [gid]; knockout_reactions = knockout_reactions)
function set_bound(model::StandardModel, reaction_id::String; ub, lb)
reaction = model.reactions[reaction_id]
......
......@@ -19,8 +19,8 @@
lp = flux_balance_analysis(cp, Tulip.Optimizer)
@test termination_status(lp) == MOI.OPTIMAL
sol = JuMP.value.(lp[:x])
@test isapprox(objective_value(lp), expected_optimum, atol=TEST_TOLERANCE)
@test isapprox(cp.c' * sol, expected_optimum, atol=TEST_TOLERANCE)
@test isapprox(objective_value(lp), expected_optimum, atol = TEST_TOLERANCE)
@test isapprox(cp.c' * sol, expected_optimum, atol = TEST_TOLERANCE)
# test the "nicer output" variants
fluxes_vec = flux_balance_analysis_vec(cp, Tulip.Optimizer)
......
......@@ -5,7 +5,10 @@
add_gene!(m, Gene("g1"))
add_gene!(m, Gene("g2"))
add_reaction!(m, Reaction("v1", metabolites = Dict("A" => -1.0, "B" => 1.0), grr = [["g1"]]))
add_reaction!(
m,
Reaction("v1", metabolites = Dict("A" => -1.0, "B" => 1.0), grr = [["g1"]]),
)
add_reaction!(
m,
Reaction("v2", metabolites = Dict("A" => -1.0, "B" => 1.0), grr = [["g1", "g2"]]),
......
......@@ -12,14 +12,18 @@
# atom tracker
atom_fluxes = atom_exchange(sol, model)
@test isapprox(atom_fluxes["C"], 37.19016648975907; atol=TEST_TOLERANCE)
@test isapprox(atom_fluxes["C"], 37.19016648975907; atol = TEST_TOLERANCE)
@test atom_exchange("FBA", model)["C"] == 0.0
@test isapprox(atom_exchange("BIOMASS_Ecoli_core_w_GAM", model)["C"], -42.5555; atol=TEST_TOLERANCE)
@test isapprox(
atom_exchange("BIOMASS_Ecoli_core_w_GAM", model)["C"],
-42.5555;
atol = TEST_TOLERANCE,
)
# metabolite trackers
consuming, producing = metabolite_fluxes(sol, model)
@test isapprox(consuming["atp_c"]["PFK"], -7.47738; atol=TEST_TOLERANCE)
@test isapprox(producing["atp_c"]["PYK"], 1.75818; atol=TEST_TOLERANCE)
@test isapprox(consuming["atp_c"]["PFK"], -7.47738; atol = TEST_TOLERANCE)
@test isapprox(producing["atp_c"]["PYK"], 1.75818; atol = TEST_TOLERANCE)
# set bounds
cbm = make_optimization_model(model, optimizer)
......
......@@ -25,17 +25,29 @@
["EX_m1"; "Exch_m3"; "Ex_m2"],
["m1"; "m2"; "m3_e"],
)
@test filter(looks_like_exchange_reaction, reactions(cp)) == ["EX_m1", "Exch_m3", "Ex_m2"]
@test filter(x -> looks_like_exchange_reaction(x; exchange_prefixes = ["Exch_"]), reactions(cp)) == ["Exch_m3"]
@test filter(looks_like_exchange_reaction, reactions(cp)) ==
["EX_m1", "Exch_m3", "Ex_m2"]
@test filter(
x -> looks_like_exchange_reaction(x; exchange_prefixes = ["Exch_"]),
reactions(cp),
) == ["Exch_m3"]
# this is originally the "toyModel1.mat"
cp = test_toyModel()
@test filter(looks_like_exchange_reaction, reactions(cp)) == ["EX_m1(e)", "EX_m3(e)", "EX_biomass(e)"]
@test filter(x -> looks_like_exchange_reaction(x; exclude_biomass=true), reactions(cp)) == ["EX_m1(e)", "EX_m3(e)"]
@test filter(looks_like_exchange_reaction, reactions(cp)) ==
["EX_m1(e)", "EX_m3(e)", "EX_biomass(e)"]
@test filter(
x -> looks_like_exchange_reaction(x; exclude_biomass = true),
reactions(cp),
) == ["EX_m1(e)", "EX_m3(e)"]
@test filter(looks_like_exchange_metabolite, metabolites(cp)) == ["m1[e]", "m3[e]"]
@test filter(looks_like_biomass_reaction, reactions(cp)) == ["EX_biomass(e)", "biomass1"]
@test filter(x -> looks_like_biomass_reaction(x; exclude_exchanges=true), reactions(cp)) == ["biomass1"]
@test filter(looks_like_biomass_reaction, reactions(cp)) ==
["EX_biomass(e)", "biomass1"]
@test filter(
x -> looks_like_biomass_reaction(x; exclude_exchanges = true),
reactions(cp),
) == ["biomass1"]
end
@testset "Looks like functions, basic" begin
......@@ -43,25 +55,25 @@ end
@test length(filter(looks_like_exchange_reaction, reactions(model))) == 20
@test length(filter(looks_like_exchange_metabolite, metabolites(model))) == 20
@test length(filter(looks_like_biomass_reaction, reactions(model))) == 1
model = load_model(model_paths["e_coli_core.xml"])
@test length(filter(looks_like_exchange_reaction, reactions(model))) == 20
@test length(filter(looks_like_exchange_metabolite, metabolites(model))) == 20
@test length(filter(looks_like_biomass_reaction, reactions(model))) == 1
model = load_model(model_paths["e_coli_core.mat"])
@test length(filter(looks_like_exchange_reaction, reactions(model))) == 20
@test length(filter(looks_like_exchange_metabolite, metabolites(model))) == 20
@test length(filter(looks_like_biomass_reaction, reactions(model))) == 1
model = convert(StandardModel, model)
@test length(filter(looks_like_exchange_reaction, reactions(model))) == 20
@test length(filter(looks_like_exchange_metabolite, metabolites(model))) == 20
@test length(filter(looks_like_biomass_reaction, reactions(model))) == 1
model = convert(CoreModelCoupled, model)
@test length(filter(looks_like_exchange_reaction, reactions(model))) == 20
@test length(filter(looks_like_exchange_metabolite, metabolites(model))) == 20
@test length(filter(looks_like_biomass_reaction, reactions(model))) == 1
end
......@@ -175,10 +175,10 @@ end
lp = CoreModel(
[1.0 1 1 0; 1 1 1 0; 1 1 1 0; 0 0 0 1],
collect(1.:4),
collect(1.:4),
collect(1.:4),
collect(1.:4),
collect(1.0:4),
collect(1.0:4),
collect(1.0:4),
collect(1.0:4),
["r1"; "r2"; "r3"; "r4"],
["m1"; "m2"; "m3"; "m4"],
)
......
......@@ -54,7 +54,16 @@ end
@test cp.cl == new_cp.cl
@test cp.cu == new_cp.cu
new_cp = add_reactions(cp, 2.0 * ones(4), 3 .* ones(4), 2.0, -1.0, 1.0, "r4", ["m$i" for i in 1:4])
new_cp = add_reactions(
cp,
2.0 * ones(4),
3 .* ones(4),
2.0,
-1.0,
1.0,
"r4",
["m$i" for i = 1:4],
)
@test cp.C == new_cp.C[:, 1:end-1]
@test cp.cl == new_cp.cl
@test cp.cu == new_cp.cu
......@@ -78,34 +87,28 @@ end
2 .* ones(10),
-ones(10),
ones(10),
["r$i" for i in 1:10],
["m$i" for i in 1:4]
["r$i" for i = 1:10],
["m$i" for i = 1:4],
)
@test cp.C == new_cp.C[:, 1:end-7] # 3 reactions were already present
@test cp.cl == new_cp.cl
@test cp.cu == new_cp.cu
new_cp = add_reactions(
cp,
2.0 * sprand(4000, 0.5),
3 .* sprand(4000, 0.5),
2.0,
-1.0,
1.0
)
new_cp =
add_reactions(cp, 2.0 * sprand(4000, 0.5), 3 .* sprand(4000, 0.5), 2.0, -1.0, 1.0)
@test cp.C == new_cp.C[:, 1:end-1]
@test cp.cl == new_cp.cl
@test cp.cu == new_cp.cu
cm = CoreModel(