readsbml.jl 2.16 KB
Newer Older
1

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
2
3
4
5
6
7
8
9
10
11
12
13
14
struct UnitPart
    kind :: String
    exponent :: Int
    scale :: Int
    multiplier :: Float64
    UnitPart(k,e,s,m) = new(k,e,s,m)
end

struct Reaction
    stoichiometry :: Dict{String, Float64}
    lb :: Tuple{Float64, String}
    ub :: Tuple{Float64, String}
    oc :: Float64
15
    Reaction(s,l,u,o) = new(s,l,u,o)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
16
17
18
19
20
21
22
23
24
25
26
27
28
end

struct Species
    name :: String
    compartment :: String
    Species(n,c) = new(n,c)
end

struct Model
    units :: Dict{String,Vector{UnitPart}}
    compartments :: Vector{String}
    species :: Dict{String,Species}
    reactions :: Dict{String, Reaction}
29
    Model(u,c,s,r) = new(u,c,s,r)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
30
31
end

32
function readSBML(fn::String)::Model
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
33
    mi = readSBML_internal(fn)
34
    if length(errors(mi))>0
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
35
36
37
        @error "Loading failed!" errors(mi)
    end

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    us = Dict{String, Vector{UnitPart}}()
    for u in units(mi)
        us[unit(u)]=Vector{UnitPart}()
    end
    for u in units(mi)
        push!(us[unit(u)], UnitPart(kind(u), exponent(u), scale(u), multiplier(u)))
    end

    ss = Dict{String, Species}()
    for s in species(mi)
        ss[id(s)] = Species(name(s), compartment(s))
    end

    rs = Dict{String, Reaction}()
    for r in reactions(mi)
        sts = Dict{String, Float64}()
        for s in species(r)
            sts[id(s)] = stoichiometry(s)
        end
        rs[id(r)] = Reaction(sts, lb(r), ub(r), oc(r))
    end
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
59

60
    return Model(us, compartments(mi), ss, rs)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
61
62
end

63
64
65
66
67
68
69
70
71
72
#TODO this needs a sparse version and faster row ID lookup
function getS(m::Model)::Tuple{Vector{String}, Vector{String}, Matrix{Float64}}
    rows = [k for k in keys(m.species)] #TODO this too
    cols = [k for k in keys(m.reactions)]
    S = zeros(Float64, length(rows), length(cols))
    for ri in 1:length(cols)
        stoi = m.reactions[cols[ri]].stoichiometry
        S[indexin(keys(stoi), rows), ri] .= values(stoi)
    end
    return rows, cols, S
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
73
74
75
end

function getLBs(m::Model)::Vector{Tuple{Float64,String}}
76
    return broadcast(x -> x.lb, values(m.reactions))
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
77
78
79
end

function getUBs(m::Model)::Vector{Tuple{Float64,String}}
80
    return broadcast(x -> x.ub, values(m.reactions))
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
81
82
83
end

function getOCs(m::Model)::Vector{Float64}
84
    return broadcast(x -> x.oc, values(m.reactions))
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
85
end