Commit 6266f411 authored by St. Elmo's avatar St. Elmo
Browse files

fixed json import

parent 0063c4bf
......@@ -8,6 +8,7 @@ Gurobi = "2e9cd046-0924-5485-92f1-d5272153d98b"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
MATLAB = "10e44e05-a98a-55b3-a45b-ba969058deb6"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
......
# using Revise
using CobraTools
using MATLAB
using JSON
jsonpath = joinpath("test/iMM904.json")
m = JSON.parsefile(jsonpath)
jsonmodel = CobraTools.readmodel(jsonpath)
# matpath = joinpath("test/iMM904.mat")
......@@ -11,3 +11,8 @@ jsonmodel = CobraTools.readmodel(jsonpath)
# matmodel = CobraTools.readmodel(matpath)
# mf = MatFile(matpath)
# model_name = variable_names(mf)[1] # assume model name is the only variable
# modeldict = get_variable(mf, model_name)
# close(mf)
\ No newline at end of file
# CobraTools
# CobraTools.jl
This is package aims to provide basic constraint based reconstruction analysis (COBRA) tools in the Julia environment.
......
"""
Reaction
"""
struct Reaction
id :: String
name :: String
metabolites :: Dict{String, Float64}
lb :: Float64
ub :: Float64
grr :: String
subsystem :: String
notes :: Dict{String, Array{String, 1}}
annotation :: Dict{String, Union{Array{String, 1}, String}} # SBO is a single string term
objective_coefficient :: Float64
end
function Reaction(d)
id = ""
name = ""
metabolites = Dict{String, Float64}()
lb = -1000.0
ub = 1000.0
grr = ""
subsystem = ""
notes = Dict{String, Array{String, 1}}()
annotation = Dict{String, Union{Array{String, 1}, String}}()
objective_coefficient = 0.0
for (k, v) in d
if k == "id"
id = v
elseif k == "name"
name = v
elseif k == "metabolites"
metabolites = Dict{String, Float64}(kk=>vv for (kk, vv) in v)
elseif k == "lower_bound"
lb = v
elseif k == "upper_bound"
ub = v
elseif k == "gene_reaction_rule"
grr = v
elseif k == "subsystem"
subsystem = v
elseif k == "notes"
notes = Dict{String, Array{String, 1}}(kk=>vv for (kk, vv) in v)
elseif k == "annotation"
annotation = Dict{String, Union{Array{String, 1}, String}}()
for (kk, vv) in v
if typeof(vv) == String
annotation[kk] = vv
else
annotation[kk] = convert(Array{String, 1}, vv)
end
end
elseif k == "objective_coefficient"
objective_coefficient = v
else
@warn "Unrecognized reaction field: $k"
end
end
Reaction(id, name, metabolites, lb, ub, grr, subsystem, notes, annotation, objective_coefficient)
end
"""
Model fields define the basic information necessary to run analysis tools
Metabolite
"""
struct Model
id :: String # model name
struct Metabolite
id :: String
name :: String
formula :: String
charge :: Int64
compartment :: String
notes :: Dict{String, Array{String, 1}}
annotation :: Dict{String, Union{Array{String, 1}, String}}
end
function Metabolite(d)
id = ""
name = ""
formula = ""
charge = 0
compartment = ""
notes = Dict{String, Array{String, 1}}()
annotation = Dict{String, Union{Array{String, 1}, String}}()
for (k, v) in d
if k == "id"
id = v
elseif k == "name"
name = v
elseif k == "formula"
formula = v
elseif k == "charge"
charge = v
elseif k == "compartment"
compartment = v
elseif k == "notes"
notes = Dict{String, Array{String, 1}}(kk=>vv for (kk, vv) in v)
elseif k == "annotation"
annotation = Dict{String, Union{Array{String, 1}, String}}()
for (kk, vv) in v
if typeof(vv) == String
annotation[kk] = vv
else
annotation[kk] = convert(Array{String, 1}, vv)
end
end
else
@warn "Unrecognized reaction field: $k"
end
end
Metabolite(id, name, formula, charge, compartment, notes, annotation)
end
"""
Gene
"""
struct Gene
id :: String
name :: String
notes :: Dict{String, Array{String, 1}}
annotation :: Dict{String, Union{Array{String, 1}, String}}
end
function Gene(d)
id = ""
name = ""
notes = Dict{String, Array{String, 1}}()
annotation = Dict{String, Union{Array{String, 1}, String}}()
for (k, v) in d
if k == "id"
id = v
elseif k == "name"
name = v
elseif k == "notes"
notes = Dict{String, Array{String, 1}}(kk=>vv for (kk, vv) in v)
elseif k == "annotation"
annotation = Dict{String, Union{Array{String, 1}, String}}()
for (kk, vv) in v
if typeof(vv) == String
annotation[kk] = vv
else
annotation[kk] = convert(Array{String, 1}, vv)
end
end
else
@warn "Unrecognized reaction field: $k"
end
end
Gene(id, name, notes, annotation)
end
"""
CoreModel fields define the basic information necessary to run analysis tools
"""
struct CoreModel
S :: SparseMatrixCSC{Float64,Int64} # stoichiometric matrix
b :: SparseVector{Float64,Int64} # mass balance rhs
lbs :: Array{Float64, 1} # reaction lower bounds
ubs :: Array{Float64, 1} # rxn upper bounds
rxns :: Array{String, 1} # reactions
mets :: Array{String, 1} # metabolites
grrs :: Dict{String, Array{Array{String, 1}, 1}} # reaction -> [[genes]]
end
"""
CoreModel()
Empty constructor.
"""
function CoreModel()
CoreModel(sparse(rand(0,0)), sparse(rand(0)), Float64[], Float64[])
end
"""
Complete model with metadata associated with the model e.g. formulas etc.
Contains grrs which should make gene reaction look ups easier
"""
struct Model
id :: String # model name
coremodel :: CoreModel # core model
rxns :: Array{Reaction, 1} # reaction metadata
mets :: Array{Metabolite, 1} # metabolite metadata
genes :: Array{Gene, 1} # gene metadata
grrs :: Dict{String, Array{Array{String, 1}, 1}} # reaction -> [[gene & gene...] or [gene & gene...] or [gene & gene...]]
end
"""
......@@ -19,21 +188,91 @@ Model()
Empty constructor.
"""
function Model()
Model("blank",
sparse(rand(0,0)),
sparse(rand(0)),
Float64[],
Float64[],
String[],
String[],
Dict{String, Array{Array{String, 1}, 1}}())
Model("blank", CoreModel(), Array{Reaction, 1}(), Array{Metabolite, 1}(), Array{Gene, 1}(), Dict{String, Array{Array{String, 1}, 1}}())
end
"""
Pretty printing of model.
"""
function Base.show(io::IO, m::Model)
println(io, "Constraint based model: $(m.id)")
println(io, "# rxns: $(length(m.rxns))")
println(io, "# mets: $(length(m.mets))")
end
\ No newline at end of file
println(io, "Constraint based model: ", m.id)
println(io, "Number of reactions: ", length(m.rxns))
println(io, "Number of metabolites: ", length(m.mets))
println(io, "Number of genes: ", length(m.genes))
# println(io, "Objective function: ", )
end
"""
Pretty printing of reaction.
"""
function Base.show(io::IO, r::Reaction)
if r.ub > 0.0 && r.lb < 0.0
arrow = " ⟷ "
elseif r.ub == 0.0 && r.lb < 0.0
arrow = " ⟵ "
elseif r.ub > 0.0 && r.lb == 0.0
arrow = " ⟶ "
else
arrow = " →∣← " # blocked reaction
end
substrates = String[]
products = String[]
for (k, v) in r.metabolites
if v < 0.0
push!(substrates, string(abs(v))*" "*k)
else
push!(products, string(abs(v))*" "*k)
end
end
isempty(substrates) && (substrates = "∅")
isempty(products) && (products = "∅")
println(io, "Reaction ID: ", r.id)
println(io, "Reaction name: ", r.name)
println(io, "Reaction subsystem: ", r.subsystem)
println(io, join(substrates, " + ")*arrow*join(products, " + "))
println(io, "Lower bound: ", r.lb)
println(io, "Upper bound: ", r.ub)
println(io, "Genes: ", r.grr)
println(io, "E.C. number: ", join(get(r.annotation, "ec-code", ["N/A"]), " or "))
end
"""
Pretty printing of reactions.
"""
function Base.show(io::IO, rs::Array{Reaction, 1})
println(io, "Reaction set of length: ", length(rs))
end
"""
Pretty printing of metabolite.
"""
function Base.show(io::IO, m::Metabolite)
println(io, "Metabolite ID: ", m.id)
println(io, "Metabolite name: ", m.name)
println(io, "Formula: ", m.formula)
println(io, "Charge: ", m.charge)
end
"""
Pretty printing of metabolites.
"""
function Base.show(io::IO, ms::Array{Metabolite, 1})
println(io, "Metabolite set of length: ", length(ms))
end
"""
Pretty printing of gene.
"""
function Base.show(io::IO, g::Gene)
println(io, "Gene ID: ", g.id)
println(io, "Gene name: ", g.name)
end
"""
Pretty printing of genes.
"""
function Base.show(io::IO, gs::Array{Gene, 1})
println(io, "Gene set of length: ", length(gs))
end
......@@ -11,7 +11,7 @@ function readmodel(file_location)
model = reconstructmodeljson(JSON.parsefile(file_location))
@info "Done reading JSON model."
catch err
@error "JSON model reading error.\n $err"
@error "JSON model reading error.\n$err"
model = Model()
end
elseif endswith(file_location, ".xml")
......@@ -20,7 +20,7 @@ function readmodel(file_location)
model = Model()
@info "Done reading SBML model."
catch err
@error "SBML model reading error.\n $err"
@error "SBML model reading error.\n$err"
model = Model()
end
elseif endswith(file_location, ".mat")
......@@ -29,7 +29,7 @@ function readmodel(file_location)
model = reconstructmodelmatlab(file_location)
@info "Done reading Matlab model."
catch err
@error "Matlab model reading error.\n $err"
@error "Matlab model reading error.\n$err"
model = Model()
end
else
......@@ -44,25 +44,44 @@ reconstructmodeljson(modeldict)
"""
function reconstructmodeljson(modeldict)
id = modeldict["id"]
rxnids = [rxn["id"] for rxn in modeldict["reactions"]]
ubs = [rxn["upper_bound"] for rxn in modeldict["reactions"]]
lbs = [rxn["lower_bound"] for rxn in modeldict["reactions"]]
metids = [met["id"] for met in modeldict["metabolites"]]
b = spzeros(length(metids))
grrs = Dict{String, Array{Array{String, 1}, 1}}()
S = spzeros(length(metids), length(rxnids))
rxns = Array{Reaction, 1}()
for rxn in modeldict["reactions"]
r = Reaction(rxn)
push!(rxns, r)
end
mets = Array{Metabolite, 1}()
for met in modeldict["metabolites"]
m = Metabolite(met)
push!(mets, m)
end
genes = Array{Gene, 1}()
for gene in modeldict["genes"]
g = Gene(gene)
push!(genes, g)
end
ubs = [rxn.ub for rxn in rxns]
lbs = [rxn.lb for rxn in rxns]
for (i, rxn) in enumerate(modeldict["reactions"])
for (met, coeff) in rxn["metabolites"]
j = findfirst(x -> x == met, metids)
b = spzeros(length(mets))
S = spzeros(length(mets), length(rxns))
grrs = Dict{String, Array{Array{String, 1}, 1}}()
metids = [met.id for met in mets] # need indices for S matrix construction
for (i, rxn) in enumerate(rxns) # column
for (met, coeff) in rxn.metabolites
j = findfirst(x -> x == met, metids) # row
isnothing(j) ? (@error "S matrix construction error: $met not defined."; continue) : nothing
S[j, i] = coeff
end
isempty(rxn["gene_reaction_rule"]) ? continue : (grrs[rxn["id"]] = parsegrr(rxn["gene_reaction_rule"]))
isempty(rxn.grr) ? continue : (grrs[rxn.id] = parsegrr(rxn.grr))
end
return Model(id, S, b, lbs, ubs, rxnids, metids, grrs)
return Model(id, CoreModel(S, b, lbs, ubs), rxns, mets, genes, grrs)
end
"""
......@@ -89,6 +108,10 @@ function reconstructmodelmatlab(file_location)
Model(model_name, S, b, lbs, ubs, rxnids, metids, grrs)
end
function reconstructmodelsbml()
end
"""
parsegrr(string_rule)
......@@ -119,7 +142,7 @@ function savemodel(model :: Model, file_location :: String)
@info "Done saving SBML model."
elseif endswith(file_location, ".mat")
@info "Saving a Matlab formatted model..."
@ingo "Done saving Matlab model."
@info "Done saving Matlab model."
else
@error "Model format not supported. The format is inferred from the file extension. Supported formats: *.mat, *.xml, *.json."
end
......@@ -134,3 +157,7 @@ function savematlabmodel()
end
function savesbmlmodel()
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