Skip to content

Commit

Permalink
make sure yeast gem works nicely
Browse files Browse the repository at this point in the history
...esp the compartments and NaNs in charges
  • Loading branch information
exaexa committed Aug 19, 2024
1 parent ea240cc commit c1bcc40
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MATFBCModels"
uuid = "bd2e13bf-42c0-49e1-9a9c-885d36daf88b"
authors = ["The authors of MATFBCModels.jl"]
version = "0.2.0"
version = "1.0.0"

[deps]
AbstractFBCModels = "5a4f3dfa-1789-40f8-8221-69268c29937c"
Expand Down
4 changes: 4 additions & 0 deletions src/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ metabolite_formulas = ["metFormula", "metFormulas"]

metabolite_charges = ["metCharge", "metCharges"]

metabolite_compartments = ["metCompartment", "metCompartments", "metComp", "metComps"]

metabolite_compartment_tables = ["comps", "compNames"]

reaction_names = ["rxnNames"]

metabolite_names = ["metNames"]
Expand Down
19 changes: 17 additions & 2 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ function A.metabolite_charge(m::MATFBCModel, mid::String)
parse_charge(m.mat[guesskeys(:metabolite_charges, m)][idx])
end

A.metabolite_compartment(m::MATFBCModel, mid::String) = nothing
function A.metabolite_compartment(m::MATFBCModel, mid::String)::Union{Nothing,String}
comp_key = guesskeys_maybe(:metabolite_compartments, m)
isnothing(comp_key) && return nothing
res = m.mat[comp_key][findfirst(==(mid), A.metabolites(m))]
# now, if the metabolite is an integer or a (very integerish) float, it is an
# index to a table of metabolite names (such as in the yeast GEM)
typeof(res) <: Real || return parse_compartment(res)
ct_key = guesskeys_maybe(:metabolite_compartment_tables, m)
isnothing(ct_key) && return nothing
return parse_compartment(m.mat[ct_key][Int(res)])
end

function A.reaction_stoichiometry(m::MATFBCModel, rid::String)
ridx = first(indexin([rid], m.mat[guesskeys(:reactions, m)]))[1] # get the index out of the cartesian index
Expand Down Expand Up @@ -178,7 +188,12 @@ function Base.convert(::Type{MATFBCModel}, m::A.AbstractFBCModel)
],
"metFormulas" =>
[unparse_formula(A.metabolite_formula(m, mid)) for mid in A.metabolites(m)],
"metCharges" => [A.metabolite_charge(m, mid) for mid in A.metabolites(m)],
"metCharges" =>
[unparse_charge(A.metabolite_charge(m, mid)) for mid in A.metabolites(m)],
"metCompartments" => [
unparse_compartment(A.metabolite_compartment(m, mid)) for
mid in A.metabolites(m)
],
),
)
end
29 changes: 24 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@

guesskeys(id, model) = first(intersect(keys(model.mat), getfield(key_names, id)))
guesskeys_maybe(id, model) =
let keys = [k for k in getfield(key_names, id) if k in keys(model.mat)]
isempty(keys) ? nothing : first(keys)
end

guesskeys(id, model) =
let key = guesskeys_maybe(id, model)
isnothing(key) ? throw(KeyError, "no applicable model key found for $id") : key
end

function parse_compartment(x::String)
isempty(x) && return nothing
return x
end

function parse_formula(x::Maybe{String})
isnothing(x) && return nothing
Expand All @@ -15,20 +28,26 @@ end

function parse_charge(x)::Maybe{Int}
if isa(x, Int)
x
return x
elseif isa(x, Float64)
Int(x)
return isnan(x) ? nothing : Int(x)
elseif isa(x, String)
Int(parse(Float64, x))
return Int(parse(Float64, x))
elseif isnothing(x)
nothing
return nothing
else
throw(DomainError(x, "cannot parse charge"))
end
end

unparse_compartment(::Nothing) = ""
unparse_compartment(x::String) = x

function unparse_formula(x::Maybe{A.MetaboliteFormula})
isnothing(x) && return nothing
ks = sort(collect(keys(x)))
join(k * string(x[k]) for k in ks)
end

unparse_charge(::Nothing) = NaN
unparse_charge(x::Int) = float(x)
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import SparseArrays
"223db0b1ed69a7cc782f2a3093c1f48911a3c8bbd5bdf4bcdb13185cab5fdaa0",
true,
),
(
"yeast-GEM",
"https://github.com/SysBioChalmers/yeast-GEM/raw/v8.6.2/model/yeast-GEM.mat",
"c2587e258501737e0141cd47e0f854a60a47faee2d4c6ad582a00e437676b181",
true,
),
]
path = joinpath(modeldir, "$name.mat")
A.download_data_file(url, path, hash)
Expand Down

0 comments on commit c1bcc40

Please sign in to comment.