Reaction.jl 4.37 KB
Newer Older
St. Elmo's avatar
St. Elmo committed
1
struct MetaboliteWithCoefficient
St. Elmo's avatar
St. Elmo committed
2
3
    coeff::Float64
    metabolite::Metabolite
St. Elmo's avatar
St. Elmo committed
4
    MetaboliteWithCoefficient(c, m) = new(float(c), m)
St. Elmo's avatar
St. Elmo committed
5
6
end

7
function Base.:*(coeff::Real, met::Metabolite)
St. Elmo's avatar
St. Elmo committed
8
9
10
    return MetaboliteWithCoefficient(coeff, met)
end

St. Elmo's avatar
St. Elmo committed
11
12
13
14
function Base.:+(
    m1::Union{Metabolite,MetaboliteWithCoefficient},
    m2::Union{Metabolite,MetaboliteWithCoefficient},
)
St. Elmo's avatar
St. Elmo committed
15
16
17
18
    if typeof(m1) == Metabolite
        m1 = MetaboliteWithCoefficient(1.0, m1)
    end
    if typeof(m2) == Metabolite
St. Elmo's avatar
St. Elmo committed
19
        m2 = MetaboliteWithCoefficient(1.0, m2)
St. Elmo's avatar
St. Elmo committed
20
21
22
23
    end
    return MetaboliteWithCoefficient[m1, m2]
end

St. Elmo's avatar
St. Elmo committed
24
function Base.:+(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
25
    m1::Vector{MetaboliteWithCoefficient},
St. Elmo's avatar
St. Elmo committed
26
27
    m2::Union{Metabolite,MetaboliteWithCoefficient},
)
St. Elmo's avatar
St. Elmo committed
28
29
30
31
32
33
    if typeof(m2) == Metabolite
        m2 = MetaboliteWithCoefficient(1.0, m2)
    end
    return push!(m1, m2)
end

St. Elmo's avatar
St. Elmo committed
34
function _mkrxn(substrates, products)
St. Elmo's avatar
St. Elmo committed
35
    metdict = Dict{String,Float64}()
St. Elmo's avatar
St. Elmo committed
36

St. Elmo's avatar
St. Elmo committed
37
    if typeof(substrates) == Metabolite
38
        metdict[substrates.id] = get(metdict, substrates.id, 0.0) - 1.0
St. Elmo's avatar
St. Elmo committed
39
    elseif typeof(substrates) == MetaboliteWithCoefficient
40
41
42
        metdict[substrates.metabolite.id] =
            get(metdict, substrates.metabolite.id, 0.0) - 1.0 * abs(substrates.coeff)
    elseif typeof(products) == Vector{MetaboliteWithCoefficient}
St. Elmo's avatar
St. Elmo committed
43
        for mwc in substrates
St. Elmo's avatar
St. Elmo committed
44
45
            metdict[mwc.metabolite.id] =
                get(metdict, mwc.metabolite.id, 0.0) - 1.0 * abs(mwc.coeff)
St. Elmo's avatar
St. Elmo committed
46
47
48
49
        end
    end

    if typeof(products) == Metabolite
50
        metdict[products.id] = get(metdict, products.id, 0.0) + 1.0
St. Elmo's avatar
St. Elmo committed
51
    elseif typeof(products) == MetaboliteWithCoefficient
52
53
54
        metdict[products.metabolite.id] =
            get(metdict, products.metabolite.id, 0.0) + abs(products.coeff)
    elseif typeof(products) == Vector{MetaboliteWithCoefficient}
St. Elmo's avatar
St. Elmo committed
55
        for mwc in products
St. Elmo's avatar
St. Elmo committed
56
57
            metdict[mwc.metabolite.id] =
                get(metdict, mwc.metabolite.id, 0.0) + 1.0 * abs(mwc.coeff)
St. Elmo's avatar
St. Elmo committed
58
59
        end
    end
St. Elmo's avatar
St. Elmo committed
60

St. Elmo's avatar
St. Elmo committed
61
62
63
64
    return metdict
end

"""
65
    →(
66
        substrates::Union{
67
            Nothing,
68
69
70
71
            Metabolite,
            MetaboliteWithCoefficient,
            Vector{MetaboliteWithCoefficient},
        },
72
        products::Union{
73
            Nothing,
74
75
76
77
            Metabolite,
            MetaboliteWithCoefficient,
            Vector{MetaboliteWithCoefficient}
        },
78
79
80
    )

Make a forward-only [`Reaction`](@ref) from `substrates` and `products`.
St. Elmo's avatar
St. Elmo committed
81
"""
82
function(
St. Elmo's avatar
St. Elmo committed
83
    substrates::Union{
84
        Nothing,
St. Elmo's avatar
St. Elmo committed
85
86
        Metabolite,
        MetaboliteWithCoefficient,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
87
        Vector{MetaboliteWithCoefficient},
St. Elmo's avatar
St. Elmo committed
88
    },
89
    products::Union{
cylon-x's avatar
cylon-x committed
90
        Nothing,
91
92
        Metabolite,
        MetaboliteWithCoefficient,
cylon-x's avatar
cylon-x committed
93
        Vector{MetaboliteWithCoefficient},
94
    },
St. Elmo's avatar
St. Elmo committed
95
)
St. Elmo's avatar
St. Elmo committed
96
    metdict = _mkrxn(substrates, products)
97
    return Reaction("", metdict, :forward)
St. Elmo's avatar
St. Elmo committed
98
99
100
end

"""
101
    ←(
102
        substrates::Union{
103
            Nothing,
104
105
106
107
            Metabolite,
            MetaboliteWithCoefficient,
            Vector{MetaboliteWithCoefficient},
        },
108
109
110
111
112
113
        products::Union{
            Nothing,
            Metabolite,
            MetaboliteWithCoefficient,
            Vector{MetaboliteWithCoefficient}
        },
114
115
116
    )

Make a reverse-only [`Reaction`](@ref) from `substrates` and `products`.
St. Elmo's avatar
St. Elmo committed
117
"""
118
function(
St. Elmo's avatar
St. Elmo committed
119
    substrates::Union{
120
        Nothing,
St. Elmo's avatar
St. Elmo committed
121
122
        Metabolite,
        MetaboliteWithCoefficient,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
123
        Vector{MetaboliteWithCoefficient},
St. Elmo's avatar
St. Elmo committed
124
    },
125
126
127
128
    products::Union{
        Nothing,
        Metabolite,
        MetaboliteWithCoefficient,
cylon-x's avatar
cylon-x committed
129
        Vector{MetaboliteWithCoefficient},
130
    },
St. Elmo's avatar
St. Elmo committed
131
)
St. Elmo's avatar
St. Elmo committed
132
    metdict = _mkrxn(substrates, products)
133
    return Reaction("", metdict, :reverse)
St. Elmo's avatar
St. Elmo committed
134
135
136
end

"""
137
    ↔(
138
        substrates::Union{
139
            Nothing,
140
141
142
143
            Metabolite,
            MetaboliteWithCoefficient,
            Vector{MetaboliteWithCoefficient},
        },
144
145
146
147
148
149
        products::Union{
            Nothing,
            Metabolite,
            MetaboliteWithCoefficient,
            Vector{MetaboliteWithCoefficient}
        },
150
151
    )

152
153
Make a bidirectional (reversible) [`Reaction`](@ref) from `substrates` and
`products`.
St. Elmo's avatar
St. Elmo committed
154
"""
155
function(
St. Elmo's avatar
St. Elmo committed
156
    substrates::Union{
157
        Nothing,
St. Elmo's avatar
St. Elmo committed
158
159
        Metabolite,
        MetaboliteWithCoefficient,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
160
        Vector{MetaboliteWithCoefficient},
St. Elmo's avatar
St. Elmo committed
161
    },
162
163
164
165
    products::Union{
        Nothing,
        Metabolite,
        MetaboliteWithCoefficient,
cylon-x's avatar
cylon-x committed
166
        Vector{MetaboliteWithCoefficient},
167
    },
St. Elmo's avatar
St. Elmo committed
168
)
St. Elmo's avatar
St. Elmo committed
169
    metdict = _mkrxn(substrates, products)
170
    return Reaction("", metdict, :bidirectional)
St. Elmo's avatar
St. Elmo committed
171
end