Commit e97a3182 authored by St. Elmo's avatar St. Elmo
Browse files

Revert "fixed formatting"

This reverts commit f379a4e2.
parent f379a4e2
......@@ -3,8 +3,7 @@ using Documenter, COBREXA
# download core model
download("http://bigg.ucsd.edu/static/models/e_coli_core.json", "e_coli_core.json")
makedocs(
modules = [COBREXA],
makedocs(modules = [COBREXA],
clean = false,
sitename = "COBREXA.jl",
format = Documenter.HTML(
......@@ -25,7 +24,7 @@ makedocs(
"Optimization Based Analysis Tools" => "basic_analysis.md",
"Sampling Tools" => "sampling_tools.md",
],
)
)
# delete core model
rm("e_coli_core.json")
\ No newline at end of file
......@@ -27,8 +27,7 @@ include(joinpath("header", "types.jl"))
const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, ".."))
include_dependency(joinpath(PKG_ROOT_DIR, "Project.toml"))
const COBREXA_VERSION =
VersionNumber(Pkg.TOML.parsefile(joinpath(PKG_ROOT_DIR, "Project.toml"))["version"])
const COBREXA_VERSION = VersionNumber(Pkg.TOML.parsefile(joinpath(PKG_ROOT_DIR, "Project.toml"))["version"])
c = Base.text_colors
tx = c[:normal] # text
......@@ -59,31 +58,14 @@ loadSource(["io", "reconstruction", "analysis", "sampling"], @__DIR__)
# export functions
= Metabolite("∅") # for exchange reactions
export speye,
LinearModel,
nReactions,
nMetabolites,
nCouplingConstraints,
addReaction,
addReactions,
removeReactions,
changeBounds!,
addCouplingConstraints!,
addCouplingConstraints,
removeCouplingConstraints!,
removeCouplingConstraints,
changeCouplingBounds!,
changeCouplingBounds,
verifyConsistency,
findExchangeReactions,
findExchangeMetabolites,
solveLP,
loadModel,
loadSBMLModel,
writeModel,
fluxBalanceAnalysis,
fluxVariabilityAnalysis,
parFVA,
export speye, LinearModel, nReactions, nMetabolites, nCouplingConstraints,
addReaction, addReactions, removeReactions, changeBounds!,
addCouplingConstraints!, addCouplingConstraints,
removeCouplingConstraints!, removeCouplingConstraints,
changeCouplingBounds!, changeCouplingBounds,
verifyConsistency, findExchangeReactions, findExchangeMetabolites,
solveLP, loadModel, loadSBMLModel, writeModel,
fluxBalanceAnalysis, fluxVariabilityAnalysis, parFVA,
convertToExportable,
# from CobraTools
......
......@@ -3,15 +3,10 @@ Convert LinearModel to the JuMP model, place objectives and the equality
constraint.
"""
function makeOptimizationModel(model::LinearModel, optimizer)
m, n = size(model.S)
m,n = size(model.S)
optimization_model = JuMP.Model(optimizer)
@variable(
optimization_model,
x[i = 1:n],
lower_bound = model.xl[i],
upper_bound = model.xu[i]
)
@variable(optimization_model, x[i=1:n], lower_bound=model.xl[i], upper_bound=model.xu[i])
@objective(optimization_model, Max, model.c' * x)
@constraint(optimization_model, model.S * x .== model.b)
return (optimization_model, x)
......
......@@ -18,7 +18,7 @@ end
function fluxVariabilityAnalysis(model::LinearModel, reactions::Vector{Int64}, optimizer)
(maximum(reactions) > length(model.rxns)) && error("Index exceeds number of reactions.")
γ = 1.0
γ = 1.
fluxes = zeros(length(reactions), 2)
(optimization_model, x₀) = fluxBalanceAnalysis(model::LinearModel, optimizer)
......@@ -58,32 +58,22 @@ function parFVA(model::LinearModel, reactions::Vector{Int}, optimizer, workers)
throw(ArgumentError("reactions contain an out-of-bounds index"))
end
gamma = 1.0 #TODO parametrize?
gamma = 1. #TODO parametrize?
(optimization_model, x0) = fluxBalanceAnalysis(model::LinearModel, optimizer)
Z0 = JuMP.objective_value(optimization_model)
# make a JuMP optimization model
map(
fetch,
save_at.(
workers,
:cobrexa_parfva_model,
Ref(:(
begin
map(fetch, save_at.(workers, :cobrexa_parfva_model, Ref(:(begin
optmodel, x = COBREXA.makeOptimizationModel($model, $optimizer)
COBREXA.parFVA_add_constraint(optmodel, $(model.c), x, $Z0, $gamma)
optmodel
end
)),
),
)
end))))
# schedule FVA parts parallely using pmap
fluxes = dpmap(
rid -> :(COBREXA.parFVA_get_opt(cobrexa_parfva_model, $rid)),
CachingPool(workers),
[-reactions reactions],
)
[-reactions reactions])
# free the data on workers
map(fetch, remove_from.(workers, :cobrexa_parfva_model))
......
......@@ -5,12 +5,7 @@ function solveLP(model::LinearModel, optimizer)
m, n = size(model.S)
optimization_model = JuMP.Model(optimizer)
@variable(
optimization_model,
x[i = 1:n],
lower_bound = model.xl[i],
upper_bound = model.xu[i]
)
@variable(optimization_model, x[i=1:n], lower_bound=model.xl[i], upper_bound=model.xu[i])
@objective(optimization_model, Min, model.c' * x)
@constraint(optimization_model, model.S * x .== model.b)
......
......@@ -4,29 +4,14 @@
speye(n) = sparse(1.0I, n, n)
function Base.isequal(model1::LinearModel, model2::LinearModel)
return model1.S == model2.S &&
model1.b == model2.b &&
model1.C == model2.C &&
model1.cl == model2.cl &&
model1.cu == model2.cu &&
return model1.S == model2.S && model1.b == model2.b && model1.C == model2.C &&
model1.cl == model2.cl && model1.cu == model2.cu &&
model1.c == model2.c &&
model1.xl == model2.xl &&
model1.xu == model2.xu &&
model1.rxns == model2.rxns &&
model1.mets == model2.mets
model1.xl == model2.xl && model1.xu == model2.xu &&
model1.rxns == model2.rxns && model1.mets == model2.mets
end
function Base.copy(model::LinearModel)
return LinearModel(
model.S,
model.b,
model.C,
model.cl,
model.cu,
model.c,
model.xl,
model.xu,
model.rxns,
model.mets,
)
return LinearModel(model.S, model.b, model.C, model.cl, model.cu,
model.c, model.xl, model.xu, model.rxns, model.mets)
end
......@@ -4,7 +4,7 @@ loadSource(srcDirs, root=@__DIR__)
Load source files
"""
function loadSource(srcDirs, rootDir = @__DIR__, testMode = false)
function loadSource(srcDirs, rootDir=@__DIR__, testMode=false)
returnFlag = []
for d in srcDirs
srcDir = joinpath(rootDir, d)
......
const VT = Union{Array{Float64,1},SparseVector{Float64,Int64}}
const MT = Union{AbstractMatrix,SparseMatrixCSC{Float64,Int64}}
const ST = Union{Array{String,1},SparseVector{String,Int64}}
const VT = Union{Array{Float64,1}, SparseVector{Float64,Int64}}
const MT = Union{AbstractMatrix, SparseMatrixCSC{Float64,Int64}}
const ST = Union{Array{String,1}, SparseVector{String,Int64}}
"""
A linear optimization problem of the form:
......@@ -11,27 +11,26 @@ s.t. S x = b
xₗ ≤ x ≤ xᵤ
```
"""
mutable struct LinearModel{M<:MT,V<:VT,K<:ST}
S::M
b::V
C::M
cl::V
cu::V
c::V
xl::V
xu::V
rxns::K
mets::K
mutable struct LinearModel{M<:MT, V<:VT, K<:ST}
S ::M
b ::V
C ::M
cl ::V
cu ::V
c ::V
xl ::V
xu ::V
rxns ::K
mets ::K
function LinearModel(
S::M,
b::V,
c::V,
xl::V,
xu::V,
rxns::K,
mets::K,
) where {V<:VT,M<:MT,K<:ST}
S ::M,
b ::V,
c ::V,
xl ::V,
xu ::V,
rxns ::K,
mets ::K) where {V<:VT,M<:MT,K<:ST}
sS = sparse(S)
......@@ -47,17 +46,16 @@ mutable struct LinearModel{M<:MT,V<:VT,K<:ST}
end
function LinearModel(
S::M1,
b::V,
C::M2,
cl::V,
cu::V,
c::V,
xl::V,
xu::V,
rxns::K,
mets::K,
) where {V<:VT,M1<:MT,M2<:MT,K<:ST}
S ::M1,
b ::V,
C ::M2,
cl ::V,
cu ::V,
c ::V,
xl ::V,
xu ::V,
rxns ::K,
mets ::K) where {V<:VT,M1<:MT,M2<:MT,K<:ST}
checkInputDimensions(S, b, C, cl, cu, c, xl, xu, rxns, mets)
......@@ -70,18 +68,7 @@ mutable struct LinearModel{M<:MT,V<:VT,K<:ST}
sxl = sparse(xl)
sxu = sparse(xu)
new{SparseMatrixCSC{Float64,Int64},SparseVector{Float64,Int64},Array{String,1}}(
sS,
sb,
sC,
scl,
scu,
sc,
sxl,
sxu,
rxns,
mets,
)
new{SparseMatrixCSC{Float64,Int64}, SparseVector{Float64,Int64}, Array{String,1}}(sS, sb, sC, scl, scu, sc, sxl, sxu, rxns, mets)
end
end
......
......@@ -21,7 +21,7 @@ Convert a dictionary read from a MAT file to LinearModel
function convertToLinearModel(model::Dict)
modelKeys = ["S", "b", "c", "ub", "lb"]
for key in modelKeys
for key = modelKeys
if !(key in keys(model))
error("No variable $key found in the MAT file.")
end
......
......@@ -18,18 +18,14 @@ function loadSBMLModel(filename::String)
# error if not.
unit = lbu[1][2]
getvalue = (val, _)::Tuple -> val
getvalue = (val,_)::Tuple -> val
getunit = (_, unit)::Tuple -> unit
allunits = unique([getunit.(lbu) getunit.(ubu)])
if length(allunits) != 1
throw(
DomainError(
allunits,
"The SBML file uses multiple units; loading needs conversion",
),
)
throw(DomainError(allunits,
"The SBML file uses multiple units; loading needs conversion"))
end
return LinearModel(S, b, c, getvalue.(lbu), getvalue.(ubu), rxns, mets)
......
......@@ -5,7 +5,7 @@ NB: Does NOT export general inequality constraints (eg coupling)
See also: `MAT.jl`
"""
function writeModel(filePath::String, model::LinearModel, varName::String = "model")
function writeModel(filePath::String, model::LinearModel, varName::String="model")
matwrite(filePath, Dict(varName => convertToExportable(model)))
end
......@@ -14,13 +14,11 @@ Convert a LinearModel to exportable format
SparseVectors are not written and read properly, SparseMatrix is okay
"""
function convertToExportable(model::LinearModel)
return Dict(
"S" => model.S,
return Dict("S" => model.S,
"b" => Array(model.b),
"c" => Array(model.c),
"ub" => Array(model.xu),
"lb" => Array(model.xl),
"rxns" => model.rxns,
"mets" => model.mets,
)
"mets" => model.mets)
end
"""
Verifies that vectors and matrices have the expected dimensions.
"""
function checkCouplingConstraintsInputDimensions(
m::LinearModel,
function checkCouplingConstraintsInputDimensions(m::LinearModel,
C::M,
cl::V,
cu::V,
) where {M<:MT,V<:VT}
length(cu) == length(cl) ||
throw(DimensionMismatch("`cl` and `cu` don't have the same size"))
size(C) == (length(cl), nReactions(m)) || throw(
DimensionMismatch("`C` size should be length(cl) x the number of reactions in m"),
)
cu::V) where {M<:MT,V<:VT}
length(cu) == length(cl) || throw(DimensionMismatch("`cl` and `cu` don't have the same size"))
size(C) == (length(cl), nReactions(m)) || throw(DimensionMismatch("`C` size should be length(cl) x the number of reactions in m"))
end
"""
......@@ -21,44 +16,36 @@ Add constraints of the following form to a LinearModel:
cₗ ≤ C x ≤ cᵤ
```
"""
function addCouplingConstraints(
m::LinearModel,
function addCouplingConstraints(m::LinearModel,
c::V,
cl::AbstractFloat,
cu::AbstractFloat,
) where {V<:VT}
return addCouplingConstraints(
m,
sparse(reshape(c, (1, length(c)))),
sparse([cl]),
sparse([cu]),
)
cu::AbstractFloat) where {V<:VT}
return addCouplingConstraints(m, sparse(reshape(c, (1, length(c)))), sparse([cl]), sparse([cu]))
end
function addCouplingConstraints(m::LinearModel, C::M, cl::V, cu::V) where {M<:MT,V<:VT}
function addCouplingConstraints(m::LinearModel,
C::M,
cl::V,
cu::V) where {M<:MT,V<:VT}
newLp = deepcopy(m)
addCouplingConstraints!(newLp, C, cl, cu)
return newLp
end
function addCouplingConstraints!(
m::LinearModel,
function addCouplingConstraints!(m::LinearModel,
c::V,
cl::AbstractFloat,
cu::AbstractFloat,
) where {V<:VT}
return addCouplingConstraints!(
m,
sparse(reshape(c, (1, length(c)))),
sparse([cl]),
sparse([cu]),
)
cu::AbstractFloat) where {V<:VT}
return addCouplingConstraints!(m, sparse(reshape(c, (1, length(c)))), sparse([cl]), sparse([cu]))
end
function addCouplingConstraints!(m::LinearModel, C::M, cl::V, cu::V) where {M<:MT,V<:VT}
function addCouplingConstraints!(m::LinearModel,
C::M,
cl::V,
cu::V) where {M<:MT,V<:VT}
C = sparse(C)
cl = sparse(cl)
......@@ -75,25 +62,29 @@ end
"""
Removes a set of coupling constraints from a LinearModel.
"""
function removeCouplingConstraints(m::LinearModel, constraint::Int64)
function removeCouplingConstraints(m::LinearModel,
constraint::Int64)
return removeCouplingConstraints(m, [constraint])
end
function removeCouplingConstraints(m::LinearModel, constraints::Array{Int64,1})
function removeCouplingConstraints(m::LinearModel,
constraints::Array{Int64, 1})
newModel = deepcopy(m)
removeCouplingConstraints!(newModel, constraints)
return newModel
end
function removeCouplingConstraints!(m::LinearModel, constraint::Int64)
function removeCouplingConstraints!(m::LinearModel,
constraint::Int64)
removeCouplingConstraints!(m, [constraint])
end
function removeCouplingConstraints!(m::LinearModel, constraints::Array{Int64,1})
toBeKept = filter(e -> e constraints, 1:nCouplingConstraints(m))
function removeCouplingConstraints!(m::LinearModel,
constraints::Array{Int64, 1})
toBeKept = filter(e->e constraints, 1:nCouplingConstraints(m))
m.C = m.C[toBeKept, :]
m.cl = m.cl[toBeKept]
m.cu = m.cu[toBeKept]
......@@ -111,26 +102,20 @@ end
"""
Change the lower and/or upper bounds ('cl' and 'cu') for given coupling constraints
"""
function changeCouplingBounds!(
model::LinearModel,
constraints::Array{Int64,1};
cl::V = Array{Float64}(undef, 0),
cu::V = Array{Float64}(undef, 0),
) where {V<:VT}
function changeCouplingBounds!(model::LinearModel, constraints::Array{Int64, 1};
cl::V=Array{Float64}(undef, 0),
cu::V=Array{Float64}(undef, 0)) where {V<:VT}
found = [index 1:nCouplingConstraints(model) for index in constraints]
redConstraints = constraints[found]
length(redConstraints) == length(unique(redConstraints)) ||
error("`constraints` appears to contain duplicates")
length(redConstraints) == length(unique(redConstraints)) || error("`constraints` appears to contain duplicates")
if !isempty(cl)
length(constraints) == length(cl) ||
throw(DimensionMismatch("`constraints` size doesn't match with `cl`"))
length(constraints) == length(cl) || throw(DimensionMismatch("`constraints` size doesn't match with `cl`"))
model.cl[redConstraints] = cl[found]
end
if !isempty(cu)
length(constraints) == length(cu) ||
throw(DimensionMismatch("`constraints` size doesn't match with `cu`"))
length(constraints) == length(cu) || throw(DimensionMismatch("`constraints` size doesn't match with `cu`"))
model.cu[redConstraints] = cu[found]
end
end
"""
Adds reactions to the model `m`
"""
function addReactions(
m::LinearModel,
function addReactions(m::LinearModel,
s::V1,
b::V2,
c::AbstractFloat,
xl::AbstractFloat,
xu::AbstractFloat;
checkConsistency = false,
) where {V1<:VT,V2<:VT}
return addReactions(
m,
sparse(reshape(s, (length(s), 1))),
sparse(b),
sparse([c]),
sparse([xl]),
sparse([xu]),
checkConsistency = checkConsistency,
)
checkConsistency=false) where {V1<:VT,V2<:VT}
return addReactions(m, sparse(reshape(s, (length(s), 1))),
sparse(b), sparse([c]), sparse([xl]), sparse([xu]),
checkConsistency=checkConsistency)
end
function addReactions(
m::LinearModel,
function addReactions(m::LinearModel,
s::V1,
b::V2,
c::AbstractFloat,
......@@ -31,63 +22,33 @@ function addReactions(
xu::AbstractFloat,
rxn::String,
mets::K;
checkConsistency = false,
) where {V1<:VT,V2<:VT,K<:ST}
return addReactions(
m,
sparse(reshape(s, (length(s), 1))),
sparse(b),
sparse([c]),
sparse([xl]),
sparse([xu]),
[rxn],
mets,
checkConsistency = checkConsistency,
)
checkConsistency=false) where {V1<:VT,V2<:VT,K<:ST}
return addReactions(m, sparse(reshape(s, (length(s), 1))),
sparse(b), sparse([c]), sparse([xl]), sparse([xu]),
[rxn], mets, checkConsistency=checkConsistency)
end
function addReactions(
m::LinearModel,
function addReactions(m::LinearModel,
Sp::M,
b::V,
c::V,
xl::V,
xu::V;
checkConsistency = false,
) where {M<:MT,V<:VT}
rxns = ["r$x" for x = length(m.rxns)+1:length(m.rxns)+length(xu)]
mets = ["m$x" for x = length(m.mets)+1:length(m.mets)+size(Sp)[1]]
return addReactions(
m,
Sp,
b,
c,
xl,
xu,
rxns,
mets,
checkConsistency = checkConsistency,
)
checkConsistency=false) where {M<:MT,V<:VT}
rxns = ["r$x" for x in length(m.rxns)+1:length(m.rxns)+length(xu)]
mets = ["m$x" for x in length(m.mets)+1:length(m.mets)+size(Sp)[1]]
return addReactions(m, Sp, b, c, xl, xu, rxns, mets, checkConsistency=checkConsistency)
end
function addReactions(m1::LinearModel, m2::LinearModel; checkConsistency = false)
return addReactions(
m1,
m2.S,
m2.b,
m2.c,
m2.xl,
m2.xu,
m2.rxns,
m2.mets,
checkConsistency = checkConsistency,
)
function addReactions(m1::LinearModel,
m2::LinearModel;
checkConsistency=false)