Skip to content

Commit 72ce32e

Browse files
mtefaghiManGHD
andauthored
Compatibility of SRC and Test with MOI (#111)
* Transition to MOI * Compatibility of SRC and Test with MOI (#110) * Editing SRC to be compatible with JuMP * Editing Test to be compatible with JuMP * Editing Config to be compatible with JuMP Co-authored-by: iManGHD <[email protected]>
1 parent 8464e18 commit 72ce32e

20 files changed

+217
-90
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "COBRA"
22
uuid = "58298e0b-d05c-52ec-a210-0694647ebfc7"
3-
version = "0.4.0"
3+
version = "0.4.1"
44

55
[deps]
66
CPLEX = "a076750e-1247-5638-91d2-ce28b192dca0"

config/solverCfg.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if solverName == "CPLEX"
3737
(:CPX_PARAM_LPMETHOD, 0)
3838
] #end of solParams
3939

40-
elseif solverName == "GLPKMathProgInterface" || solverName == "GLPK"
40+
elseif solverName == "GLPK"
4141
solParams = [:Simplex, #Method
4242
true #presolve
4343
] #end of solParams

src/COBRA.jl

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
=#
77

88
#-------------------------------------------------------------------------------------------
9-
109
"""
1110
Main module for `COBRA.jl` - COnstraint-Based Reconstruction and Analysis in Julia
1211
@@ -20,9 +19,7 @@ module COBRA
2019
import Pkg
2120
using SparseArrays, Distributed, LinearAlgebra
2221
using MAT, MathOptInterface, JuMP
23-
if "MATLAB" in keys(Pkg.installed())
24-
using MATLAB
25-
end
22+
using MATLAB
2623

2724
include("checkSetup.jl")
2825
checkSysConfig(0)
@@ -31,9 +28,7 @@ module COBRA
3128
include("solve.jl")
3229
include("distributedFBA.jl")
3330
include("tools.jl")
34-
if "MATLAB" in keys(Pkg.installed())
35-
include("PALM.jl")
36-
end
31+
include("PALM.jl")
3732

3833
end
3934

src/PALM.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
=#
77

88
#-------------------------------------------------------------------------------------------
9+
#=
910
"""
1011
shareLoad(nModels, nMatlab, printLevel)
1112
@@ -183,7 +184,13 @@ function loopModels(dir, p, scriptName, dirContent, startIndex, endIndex, varsCh
183184
MATLAB.@mput PALM_dir
184185
MATLAB.@mput PALM_printLevel
185186
MATLAB.eval_string("run('" * scriptName * "')")
186-
187+
#=
188+
@mput PALM_iModel
189+
@mput PALM_modelFile
190+
@mput PALM_dir
191+
@mput PALM_printLevel
192+
eval_string("run('" * scriptName * "')")
193+
=#
187194
for i = 1:nCharacteristics
188195
data[k, i + 1] = MATLAB.get_variable(Symbol(varsCharact[i]))
189196
end
@@ -380,3 +387,4 @@ function PALM(dir, scriptName; nMatlab::Int=2, outputFile::AbstractString="PALM_
380387
end
381388
382389
export PALM
390+
=#

src/checkSetup.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ function checkSysConfig(printLevel::Int=1)
7171
packages = []
7272

7373
# initialize a vector with supported LP solvers
74-
LPsolvers = [ :GLPKMathProgInterface, :Gurobi, :CPLEX] #:Clp, :Mosek
74+
LPsolvers = [ :GLPK, :Gurobi, :CPLEX] #:Clp, :Mosek
7575

76-
if checkPackage(:MathProgBase, printLevel)
76+
if checkPackage(:JuMP, printLevel)
7777

7878
# loop through all implemented interfaces
7979
for s in 1:length(LPsolvers)
@@ -87,7 +87,7 @@ function checkSysConfig(printLevel::Int=1)
8787
end
8888

8989
# load an additional package for GLPK
90-
if string(pkgName) == "GLPKMathProgInterface"
90+
if string(pkgName) == "GLPK"
9191
checkPackage(:GLPK, printLevel)
9292
end
9393
if string(pkgName) == "Mosek"

src/connect.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ See also: `workers()`, `nprocs()`, `addprocs()`, `gethostname()`
5454
5555
"""
5656

57-
function createPool(localWorkers::Int, connectSSH::Bool=false, connectionFile::String=joinpath(dirname(pathof(COBRA)))*"/../config/sshCfg.jl", printLevel::Int=1)
57+
function createPool(localWorkers::Int, connectSSH::Bool=false, connectionFile::String=joinpath(mkpath("COBRA"))*"/../config/sshCfg.jl", printLevel::Int=1)
5858

5959
# load cores on remote nodes
6060
if connectSSH
@@ -124,7 +124,7 @@ function createPool(localWorkers::Int, connectSSH::Bool=false, connectionFile::S
124124
if printLevel > 0
125125
println(" >> Connecting ", sshWorkers[i]["procs"], " workers on ", sshWorkers[i]["usernode"])
126126
end
127-
127+
128128
try
129129
if !(Sys.iswindows())
130130
# try logging in quietly to defined node using SSH

src/distributedFBA.jl

+28-29
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function preFBA!(model, solver, optPercentage::Float64=0.0, osenseStr::String="m
9595
hasObjective = false
9696
fbaSol = NaN
9797
end
98-
98+
9999
# add a condition if the LP has an extra condition based on the FBA solution
100100
if hasObjective
101101
if printLevel > 0
@@ -204,7 +204,8 @@ function splitRange(model, rxnsList, nWorkers::Int=1, strategy::Int=0, printLeve
204204

205205
# loop through the number of reactions and determine the column density
206206
for i in 1:NrxnsList
207-
cdVect[i] = nnz(model.S[:, rxnsList[i]]) / Nmets * 100.0
207+
S_sparseVector = sparsevec(model.S[:, rxnsList[i]])
208+
cdVect[i] = nnz(S_sparseVector) / Nmets * 100.0
208209
end
209210

210211
# initialize counter vectors
@@ -341,8 +342,8 @@ See also: `distributeFBA()`, `MathProgBase.HighLevelInterface`
341342
342343
"""
343344

344-
function loopFBA(m, rxnsList, nRxns::Int, rxnsOptMode=2 .+ zeros(Int, length(rxnsList)), iRound::Int=0, pid::Int=1,
345-
resultsDir::String=joinpath(dirname(pathof(COBRA)), "..")*"/results", logFiles::Bool=false, onlyFluxes::Bool=false, printLevel::Int=1)
345+
function loopFBA(m, x, c, rxnsList, nRxns::Int, rxnsOptMode=2 .+ zeros(Int, length(rxnsList)), iRound::Int=0, pid::Int=1,
346+
resultsDir::String=joinpath(mkpath("COBRA"), "..")*"/results", logFiles::Bool=false, onlyFluxes::Bool=false, printLevel::Int=1)
346347

347348
# initialize vectors and counters
348349
retObj = zeros(nRxns)
@@ -365,30 +366,28 @@ function loopFBA(m, rxnsList, nRxns::Int, rxnsOptMode=2 .+ zeros(Int, length(rxn
365366
else
366367
performOptim = false
367368
end
368-
369+
369370
if performOptim
371+
372+
# Set the objective vector coefficients
373+
c = zeros(nRxns)
374+
c[rxnsList[k]] = 1000.0 # set the coefficient of the current FBA to 1000
375+
370376
# change the sense of the optimization
371377
if j == 1
372378
if iRound == 0
373-
MathProgBase.HighLevelInterface.setsense!(m, :Min)
379+
@objective(m, Min, c' * x)
374380
if printLevel > 0
375381
println(" -- Minimization (iRound = $iRound). Block $pid [$(length(rxnsList))/$nRxns].")
376382
end
377383
else
378-
MathProgBase.HighLevelInterface.setsense!(m, :Max)
384+
@objective(m, Max, c' * x)
379385
if printLevel > 0
380386
println(" -- Maximization (iRound = $iRound). Block $pid [$(length(rxnsList))/$nRxns].")
381387
end
382388
end
383389
end
384-
385-
# Set the objective vector coefficients
386-
c = zeros(nRxns)
387-
c[rxnsList[k]] = 1000.0 # set the coefficient of the current FBA to 1000
388-
389-
# set the objective of the CPLEX model
390-
MathProgBase.HighLevelInterface.setobj!(m, c)
391-
390+
392391
if logFiles
393392
# save individual logFiles with the CPLEX solver
394393
if isdefined(m, :inner) && string(typeof(m.inner)) == "CPLEX.Model"
@@ -397,34 +396,34 @@ function loopFBA(m, rxnsList, nRxns::Int, rxnsOptMode=2 .+ zeros(Int, length(rxn
397396
end
398397

399398
# solve the model using the general MathProgBase interface
400-
solutionLP = MathProgBase.HighLevelInterface.solvelp(m)
399+
status, objval, sol = solvelp(m, x)
401400

402401
# retrieve the solution status
403-
statLP = solutionLP.status
404-
402+
statLP = status
403+
405404
# output the solution, save the minimum and maximum fluxes
406-
if statLP == :Optimal
405+
if statLP == MathOptInterface.TerminationStatusCode(1)
407406
# retrieve the objective value
408-
retObj[rxnsList[k]] = solutionLP.objval / 1000.0 # solutionLP.sol[rxnsList[k]]
407+
retObj[rxnsList[k]] = objval / 1000.0 # solutionLP.sol[rxnsList[k]]
409408

410409
# retrieve the solution vector
411410
if !onlyFluxes
412-
retFlux[:, k] = solutionLP.sol
411+
retFlux[:, k] = sol
413412
end
414413

415414
# return the solution status
416415
retStat[rxnsList[k]] = 1 # LP problem is optimal
417416

418-
elseif statLP == :Infeasible
417+
elseif statLP == MathOptInterface.TerminationStatusCode(2)
419418
retStat[rxnsList[k]] = 0 # LP problem is infeasible
420419

421-
elseif statLP == :Unbounded
420+
elseif statLP == MathOptInterface.TerminationStatusCode(6)
422421
retStat[rxnsList[k]] = 2 # LP problem is unbounded
423422

424-
elseif statLP == :UserLimit
423+
elseif statLP == MathOptInterface.TerminationStatusCode(11)
425424
retStat[rxnsList[k]] = 3 # Solver for the LP problem has hit a user limit
426425

427-
elseif statLP == :InfeasibleOrUnbounded
426+
elseif statLP == MathOptInterface.TerminationStatusCode(6)
428427
retStat[rxnsList[k]] = 4 # LP problem is infeasible or unbounded
429428

430429
else
@@ -523,7 +522,7 @@ See also: `preFBA!()`, `splitRange()`, `buildCobraLP()`, `loopFBA()`, or `fetch(
523522

524523
function distributedFBA(model, solver; nWorkers::Int=1, optPercentage::Union{Float64, Int64}=0.0, objective::String="max",
525524
rxnsList=1:length(model.rxns), strategy::Int=0, rxnsOptMode=2 .+ zeros(Int, length(model.rxns)),
526-
preFBA::Bool=false, saveChunks::Bool=false, resultsDir::String=joinpath(dirname(pathof(COBRA)), "..")*"/results",
525+
preFBA::Bool=false, saveChunks::Bool=false, resultsDir::String=joinpath(mkpath("COBRA"), "..")*"/results",
527526
logFiles::Bool=false, onlyFluxes::Bool=false, printLevel::Int=1)
528527

529528
# convert type of optPercentage
@@ -736,13 +735,13 @@ function distributedFBA(model, solver; nWorkers::Int=1, optPercentage::Union{Flo
736735

737736
# perform maximizations and minimizations sequentially
738737
else
739-
m = buildCobraLP(model, solver)
738+
m, x, c = buildCobraLP(model, solver)
740739

741740
# adjust the solver parameters based on the model
742741
autoTuneSolver(m, nMets, nRxns, solver)
743742

744-
minFlux, fvamin, statussolmin = loopFBA(m, rxnsList, nRxns, rxnsOptMode, 0, 1, resultsDir, logFiles, onlyFluxes, printLevel)
745-
maxFlux, fvamax, statussolmax = loopFBA(m, rxnsList, nRxns, rxnsOptMode, 1, 1, resultsDir, logFiles, onlyFluxes, printLevel)
743+
minFlux, fvamin, statussolmin = loopFBA(m, x, c, rxnsList, nRxns, rxnsOptMode, 0, 1, resultsDir, logFiles, onlyFluxes, printLevel)
744+
maxFlux, fvamax, statussolmax = loopFBA(m, x, c, rxnsList, nRxns, rxnsOptMode, 1, 1, resultsDir, logFiles, onlyFluxes, printLevel)
746745
end
747746

748747
return minFlux, maxFlux, optSol, fbaSol, fvamin, fvamax, statussolmin, statussolmax

src/load.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,4 @@ end
226226
loadModel(fileName, matrixAS::String="S", modelName::String="model", modelFields::Array{String,1}=["ub", "lb", "osense", "c", "b", "csense", "rxns", "mets"], printLevel::Int=1) = loadModel(fileName, modelName, printLevel)
227227

228228
export loadModel
229-
#-------------------------------------------------------------------------------------------
229+
#-------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)