Unverified Commit a085176e authored by Laurent Heirendt's avatar Laurent Heirendt Committed by GitHub
Browse files

Merge pull request #19 from LCSB-BioCore/mk-sparse

Support sparse S matrix output
parents 2263ee90 a99e4767
...@@ -39,6 +39,10 @@ git-tree-sha1 = "8e924324b2e9275a51407a4e06deb3455b1e359f" ...@@ -39,6 +39,10 @@ git-tree-sha1 = "8e924324b2e9275a51407a4e06deb3455b1e359f"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531" uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.16.0+7" version = "1.16.0+7"
[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
[[Logging]] [[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
...@@ -77,6 +81,10 @@ uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" ...@@ -77,6 +81,10 @@ uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[Sockets]] [[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc" uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
[[Test]] [[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
......
name = "SBML" name = "SBML"
uuid = "e5567a89-2604-4b09-9718-f5f78e97c3bb" uuid = "e5567a89-2604-4b09-9718-f5f78e97c3bb"
authors = ["Mirek Kratochvil <miroslav.kratochvil@uni.lu>", "LCSB R3 team <lcsb-r3@uni.lu>"] authors = ["Mirek Kratochvil <miroslav.kratochvil@uni.lu>", "LCSB R3 team <lcsb-r3@uni.lu>"]
version = "0.1.0" version = "0.2.0"
[deps] [deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SBML_jll = "bb12108a-f4ef-5f88-8ef3-0b33ff7017f1" SBML_jll = "bb12108a-f4ef-5f88-8ef3-0b33ff7017f1"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[compat] [compat]
......
...@@ -31,6 +31,18 @@ julia> metabolites ...@@ -31,6 +31,18 @@ julia> metabolites
julia> S julia> S
77×77 SparseArrays.SparseMatrixCSC{Float64,Int64} with 308 stored entries:
[60, 1] = -1.0
[68, 1] = 1.0
[1 , 2] = 1.0
[6 , 2] = -1.0
[23, 76] = 1.0
[56, 76] = -1.0
[30, 77] = -1.0
[48, 77] = 1.0
julia> Matrix(S)
77×77 Array{Float64,2}: 77×77 Array{Float64,2}:
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
......
module SBML module SBML
using SBML_jll, Libdl, Pkg using SBML_jll, Libdl, Pkg
using SparseArrays
include("structs.jl") include("structs.jl")
include("version.jl") include("version.jl")
......
""" """
function getS(m::Model)::Tuple{Vector{String},Vector{String},Matrix{Float64}} function getS(m::Model; zeros=spzeros)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}}
Extract the vector of species (aka metabolite) identifiers, vector of reaction Extract the vector of species (aka metabolite) identifiers, vector of reaction
identifiers, and the (dense) stoichiometry matrix from an existing `Model`. identifiers, and the (dense) stoichiometry matrix from an existing `Model`.
Returns a tuple with these values. Returns a tuple with these values.
"""
function getS(m::Model)::Tuple{Vector{String},Vector{String},Matrix{Float64}} The matrix is sparse by default (initially constructed by
#TODO this will need a sparse version and faster row ID lookup `SparseArrays.spzeros`). You can fill in a custom empty matrix constructed to
rows = [k for k in keys(m.species)] #TODO this too argument `zeros`; e.g. running with `zeros=zeros` will produce a dense matrix.
"""
function getS(
m::Model;
zeros = spzeros,
)::Tuple{Vector{String},Vector{String},AbstractMatrix{Float64}}
rows = [k for k in keys(m.species)]
cols = [k for k in keys(m.reactions)] cols = [k for k in keys(m.reactions)]
rowsd = Dict(k => i for (i, k) in enumerate(rows))
S = zeros(Float64, length(rows), length(cols)) S = zeros(Float64, length(rows), length(cols))
for ri = 1:length(cols) for col = 1:length(cols)
stoi = m.reactions[cols[ri]].stoichiometry stoi = m.reactions[cols[col]].stoichiometry
S[indexin(keys(stoi), rows), ri] .= values(stoi) S[getindex.(Ref(rowsd), keys(stoi)), col] .= values(stoi)
end end
return rows, cols, S return rows, cols, S
end end
......
...@@ -21,6 +21,10 @@ end ...@@ -21,6 +21,10 @@ end
mets, rxns, S = getS(mdl) mets, rxns, S = getS(mdl)
@test typeof(S) <: AbstractMatrix{Float64}
@test typeof(getS(mdl; zeros=spzeros)[3]) <: SparseMatrixCSC{Float64}
@test typeof(getS(mdl; zeros=zeros)[3]) == Matrix{Float64}
@test length(mets) == 77 @test length(mets) == 77
@test length(rxns) == 77 @test length(rxns) == 77
@test size(S) == (length(mets),length(rxns)) @test size(S) == (length(mets),length(rxns))
......
using Test, SHA using Test, SHA, SparseArrays
using SBML using SBML
import Pkg import Pkg
......
Markdown is supported
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