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

fixed read and write json and mat

parent fce068aa
......@@ -9,17 +9,17 @@ using JSON
# jsonmodel2 = CobraTools.readmodel("test/iMM904_2.json")
# m2 = JSON.parsefile("test/iMM904_2.json")
# matpath = joinpath("test/iMM904.mat")
# matmodel = CobraTools.readmodel(matpath)
# CobraTools.savemodel(matmodel, "test/iMM904_2.mat")
# matpath2 = joinpath("test/iMM904_2.mat")
# matmodel2 = CobraTools.readmodel(matpath)
matpath = joinpath("test/iMM904.mat")
matmodel = CobraTools.readmodel(matpath)
CobraTools.savemodel(matmodel, "test/iMM904_2.mat")
matpath2 = joinpath("test/iMM904_2.mat")
matmodel2 = CobraTools.readmodel(matpath2)
# matpath = joinpath("test", "yeastGEM.mat") # broken
# mf = MatFile(matpath)
# model_name = variable_names(mf)[1] # assume model name is the only variable
# modeldict = get_variable(mf, model_name)
# close(mf)
mf = MatFile(matpath2)
model_name = variable_names(mf)[1] # assume model name is the only variable
modeldict = get_variable(mf, model_name)
close(mf)
......@@ -21,12 +21,14 @@ To install this package: `] add CobraTools`.
- [x] Write Matlab models
- [ ] Write SBML models
- [ ] Write YAML
- [ ] Run FBA
- [ ] Run pFBA
- [ ] Run FVA
- [ ] FBA
- [ ] pFBA
- [ ] MOMA
- [ ] FVA
- [ ] Implement sampling
- [ ] Single gene knockouts
- [ ] Double gene knockout
- [ ] Model construction tools
- [ ] Distributed analysis (COBRA.jl integration?)
## Usage
......
......@@ -324,25 +324,26 @@ function savematlabmodel(model :: Model, file_location :: String)
end
end
end
write_matfile(file_location;
c = [r.objective_coefficient for r in model.rxns],
mets = [m.id for m in model.mets],
subSystems = [r.subsystem for r in model.rxns],
b = Array(model.coremodel.b),
metFormulas = [m.formula for m in model.mets],
rxnGeneMat = rgm,
ub = Array(model.coremodel.ubs),
rxnNames = [r.name for r in model.rxns],
description = model.id,
genes = [g.id for g in model.genes],
rev = rxnrevs,
grRules = [r.grr for r in model.rxns],
S = Array(model.coremodel.S),
metNames = [m.name for m in model.mets],
lb = Array(model.coremodel.lbs),
metCharge = [m.charge for m in model.mets],
rxns = [r.id for r in model.rxns])
mdict = Dict("c" => [r.objective_coefficient for r in model.rxns],
"mets" => [m.id for m in model.mets],
"subSystems" => [r.subsystem for r in model.rxns],
"b" => Array(model.coremodel.b),
"metFormulas" => [m.formula for m in model.mets],
"rxnGeneMat" => rgm,
"ub" => Array(model.coremodel.ubs),
"rxnNames" => [r.name for r in model.rxns],
"description" => model.id,
"genes" => [g.id for g in model.genes],
"rev" => rxnrevs,
"grRules" => [r.grr for r in model.rxns],
"S" => Array(model.coremodel.S),
"metNames" => [m.name for m in model.mets],
"lb" => Array(model.coremodel.lbs),
"metCharge" => [m.charge for m in model.mets],
"rxns" => [r.id for r in model.rxns])
write_matfile(file_location; Dict(Symbol(model.id) => mdict)...)
end
function savesbmlmodel()
......
......@@ -18,13 +18,16 @@ yeast_mat = joinpath("yeastGEM.mat")
sbmlmodel_ecoli = CobraTools.readmodel(iJO1366_xml)
# Yeast GEM models
matlabmodel_yeast = CobraTools.readmodel(yeast_mat)
matlabmodel_yeast = CobraTools.readmodel(yeast_mat) # matlab issue
sbmlmodel_yeast = CobraTools.readmodel(yeast_xml)
@test model_comparison(jsonmodel_ecoli, matlabmodel_ecoli)
@test_broken model_comparison(sbmlmodel_ecoli, matlabmodel_ecoli)
@test_broken model_comparison(sbmlmodel_ecoli, matlabmodel_ecoli) # SBML not implemented yet
@test_broken model_comparison(matlabmodel_yeast, sbmlmodel_yeast) # yeast GEM matlab format issue
@test read_write_read(jsonmodel_ecoli, "json")
@test read_write_read(matlabmodel_ecoli, "mat")
@test_broken read_write_read(sbmlmodel_ecoli, "xml") # SBML not implemented yet
end
# @testset "Test something else" begin
......
......@@ -7,25 +7,34 @@ function model_comparison(model1, model2)
(isempty(model1.coremodel.S) || isempty(model2.coremodel.S)) ? (return false) : nothing
# test same rxn and met ids
rxn_diff = (isempty(setdiff(model1.rxns, model2.rxns)) && isempty(setdiff(model2.rxns, model1.rxns))) ? true : false
met_diff = (isempty(setdiff(model1.mets, model2.mets)) && isempty(setdiff(model2.mets, model1.mets))) ? true : false
rxns1 = [r.id for r in model1.rxns]
rxns2 = [r.id for r in model2.rxns]
mets1 = [m.id for m in model1.mets]
mets2 = [m.id for m in model2.mets]
rxn_diff = (isempty(setdiff(rxns1, rxns2)) && isempty(setdiff(rxns2, rxns1))) ? true : false
met_diff = (isempty(setdiff(mets1, mets2)) && isempty(setdiff(mets2, mets1))) ? true : false
# test same S and b shapes
S_size = all(size(model1.S) .== size(model2.S))
b_size = all(size(model1.b) .== size(model2.b))
S_size = all(size(model1.coremodel.S) .== size(model2.coremodel.S))
b_size = all(size(model1.coremodel.b) .== size(model2.coremodel.b))
# test lb and ub the same (indirectly)
lb_same = sum(abs, model1.lbs) == sum(abs, model2.lbs)
ub_same = sum(abs, model1.ubs) == sum(abs, model2.ubs)
lbs1 = [r.lb for r in model1.rxns]
lbs2 = [r.lb for r in model2.rxns]
ubs1 = [r.lb for r in model1.rxns]
ubs2 = [r.lb for r in model2.rxns]
lb_same = sum(abs, lbs1) == sum(abs, lbs2)
ub_same = sum(abs, ubs1) == sum(abs, ubs2)
# test if S and b the same (indirectly)
if S_size
input_vector = rand(size(model1.S, 2))
S_same = sum(abs, model1.S * input_vector) sum(abs, model2.S * input_vector)
input_vector = rand(size(model1.coremodel.S, 2))
S_same = sum(abs, model1.coremodel.S * input_vector) sum(abs, model2.coremodel.S * input_vector)
else
S_same = false
end
b_same = sum(abs, model1.b) == sum(abs, model2.b)
b_same = sum(abs, model1.coremodel.b) == sum(abs, model2.coremodel.b)
# test grrs
model1_grr_keys = keys(model1.grrs)
......@@ -54,3 +63,12 @@ function model_comparison(model1, model2)
return all([rxn_diff, met_diff, S_size, b_size, lb_same, ub_same, S_same, b_same, same_grr])
end
function read_write_read(model, format)
tmpfile = "temp."*format
CobraTools.savemodel(model, tmpfile)
tmpmodel = CobraTools.readmodel(tmpfile)
rm(tmpfile)
model_comparison(model, tmpmodel)
end
\ No newline at end of file
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