Skip to content

Commit

Permalink
Merge pull request #521 from LCSB-BioCore/tp-grr-coremodel
Browse files Browse the repository at this point in the history
Gene-reaction rules for CoreModel and CoreModelCoupled #439
  • Loading branch information
exaexa committed Nov 22, 2021
2 parents 0cb74ae + 94c4361 commit 523c6eb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/base/types/CoreModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mutable struct CoreModel <: MetabolicModel
xu::Vector{Float64}
rxns::Vector{String}
mets::Vector{String}
grrs::Vector{Maybe{GeneAssociation}}

function CoreModel(
S::MatType,
Expand All @@ -27,14 +28,19 @@ mutable struct CoreModel <: MetabolicModel
xu::VecType,
rxns::StringVecType,
mets::StringVecType,
grrs::Vector{Maybe{GeneAssociation}} = Vector{Maybe{GeneAssociation}}(
nothing,
length(rxns),
),
)
all([length(b), length(mets)] .== size(S, 1)) ||
throw(DimensionMismatch("inconsistent number of metabolites"))

all([length(c), length(xl), length(xu), length(rxns)] .== size(S, 2)) ||
throw(DimensionMismatch("inconsistent number of reactions"))
all(
[length(c), length(xl), length(xu), length(rxns), length(grrs)] .== size(S, 2),
) || throw(DimensionMismatch("inconsistent number of reactions"))

new(sparse(S), sparse(b), sparse(c), collect(xl), collect(xu), rxns, mets)
new(sparse(S), sparse(b), sparse(c), collect(xl), collect(xu), rxns, mets, grrs)
end
end

Expand Down Expand Up @@ -96,6 +102,31 @@ Return the stoichiometry of reaction at index `ridx`.
reaction_stoichiometry(m::CoreModel, ridx)::Dict{String,Float64} =
Dict(m.mets[k] => v for (k, v) in zip(findnz(m.S[:, ridx])...))

"""
reaction_gene_association_vec(model::CoreModel)::Vector{Maybe{GeneAssociation}}
Retrieve a vector of all gene associations in a [`CoreModel`](@ref), in the
same order as `reactions(model)`.
"""
reaction_gene_association_vec(model::CoreModel)::Vector{Maybe{GeneAssociation}} = model.grrs

"""
reaction_gene_association(model::CoreModel, ridx::Int)::Maybe{GeneAssociation}
Retrieve the [`GeneAssociation`](@ref) from [`CoreModel`](@ref) by reaction
index.
"""
reaction_gene_association(model::CoreModel, ridx::Int)::Maybe{GeneAssociation} =
model.grrs[ridx]

"""
reaction_gene_association(model::CoreModel, rid::String)::Maybe{GeneAssociation}
Retrieve the [`GeneAssociation`](@ref) from [`CoreModel`](@ref) by reaction ID.
"""
reaction_gene_association(model::CoreModel, rid::String)::Maybe{GeneAssociation} =
model.grrs[first(indexin([rid], model.rxns))]

"""
Base.convert(::Type{CoreModel}, m::M) where {M <: MetabolicModel}
Expand All @@ -115,5 +146,8 @@ function Base.convert(::Type{CoreModel}, m::M) where {M<:MetabolicModel}
xu,
reactions(m),
metabolites(m),
Vector{Maybe{GeneAssociation}}([
reaction_gene_association(m, id) for id in reactions(m)
]),
)
end
26 changes: 26 additions & 0 deletions src/base/types/CoreModelCoupled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ function reaction_stoichiometry(m::CoreModelCoupled, ridx)::Dict{String,Float64}
reaction_stoichiometry(m.lm, ridx)
end

"""
reaction_gene_association_vec(model::CoreModelCoupled)::Vector{Maybe{GeneAssociation}}
Retrieve a vector of gene associations in a [`CoreModelCoupled`](@ref), in the
same order as `reactions(model)`.
"""
reaction_gene_association_vec(model::CoreModelCoupled)::Vector{Maybe{GeneAssociation}} =
reaction_gene_association_vec(model.lm)

"""
reaction_gene_association(model::CoreModelCoupled, ridx::Int)::Maybe{GeneAssociation}
Retrieve the [`GeneAssociation`](@ref) from [`CoreModelCoupled`](@ref) by reaction
index.
"""
reaction_gene_association(model::CoreModelCoupled, ridx::Int)::Maybe{GeneAssociation} =
reaction_gene_association(model.lm, ridx)

"""
reaction_gene_association(model::CoreModelCoupled, rid::String)::Maybe{GeneAssociation}
Retrieve the [`GeneAssociation`](@ref) from [`CoreModelCoupled`](@ref) by reaction ID.
"""
reaction_gene_association(model::CoreModelCoupled, rid::String)::Maybe{GeneAssociation} =
reaction_gene_association(model.lm, rid)

"""
Base.convert(::Type{CoreModelCoupled}, mm::MetabolicModel)
Expand Down
13 changes: 13 additions & 0 deletions test/base/types/CoreModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@
@test reaction_stoichiometry(model, "EX_ac_e") == Dict("ac_e" => -1)
@test reaction_stoichiometry(model, 44) == Dict("ac_e" => -1)
end

@testset "Conversion from and to StandardModel" begin
cm = load_model(CoreModel, model_paths["e_coli_core.mat"])

sm = convert(StandardModel, cm)
cm2 = convert(CoreModel, sm)

@test Set(reactions(cm)) == Set(reactions(sm))
@test Set(reactions(cm)) == Set(reactions(cm2))

@test reaction_gene_association(sm, reactions(sm)[1]) ==
reaction_gene_association(cm, reactions(sm)[1])
end
24 changes: 24 additions & 0 deletions test/base/types/CoreModelCoupled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@
@test reaction_stoichiometry(model, 44) == Dict("ac_e" => -1)

end

@testset "Conversion from and to StandardModel" begin
cm = load_model(CoreModelCoupled, model_paths["e_coli_core.mat"])

sm = convert(StandardModel, cm)
cm2 = convert(CoreModelCoupled, sm)

@test Set(reactions(cm)) == Set(reactions(sm))
@test Set(reactions(cm)) == Set(reactions(cm2))

@test reaction_gene_association(sm, reactions(sm)[1]) ==
reaction_gene_association(cm, reactions(sm)[1])

@test reaction_gene_association_vec(cm)[1:3] == [
[["b3916"], ["b1723"]],
[
["b0902", "b0903", "b2579"],
["b0902", "b0903"],
["b0902", "b3114"],
["b3951", "b3952"],
],
[["b4025"]],
]
end

0 comments on commit 523c6eb

Please sign in to comment.