StandardModel.jl 5.46 KB
Newer Older
1
"""
St. Elmo's avatar
St. Elmo committed
2
    add_reactions!(model::StandardModel, rxns::Vector{Reaction})
3

St. Elmo's avatar
St. Elmo committed
4
Add `rxns` to `model` based on reaction `id`.
5
"""
St. Elmo's avatar
St. Elmo committed
6
function add_reactions!(model::StandardModel, rxns::Vector{Reaction})
7
    for rxn in rxns
St. Elmo's avatar
St. Elmo committed
8
        model.reactions[rxn.id] = rxn
9
10
11
    end
end

St. Elmo's avatar
St. Elmo committed
12
13
14
15
16
17
"""
    add_reaction!(model::StandardModel, rxn::Reaction)

Add `rxn` to `model` based on reaction `id`.
"""
add_reaction!(model::StandardModel, rxn::Reaction) = add_reactions!(model, [rxn])
18
19

"""
St. Elmo's avatar
St. Elmo committed
20
    add_metabolites!(model::StandardModel, mets::Vector{Metabolite})
21

St. Elmo's avatar
St. Elmo committed
22
Add `mets` to `model` based on metabolite `id`.
23
"""
St. Elmo's avatar
St. Elmo committed
24
function add_metabolites!(model::StandardModel, mets::Vector{Metabolite})
25
    for met in mets
St. Elmo's avatar
St. Elmo committed
26
        model.metabolites[met.id] = met
27
28
29
    end
end

St. Elmo's avatar
St. Elmo committed
30
31
32
33
34
"""
    add_metabolite!(model::StandardModel, met::Metabolite)

Add `met` to `model` based on metabolite `id`.
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
35
36
add_metabolite!(model::StandardModel, met::Metabolite) = add_metabolites!(model, [met])

37
"""
St. Elmo's avatar
St. Elmo committed
38
    add_genes!(model::StandardModel, genes::Vector{Gene})
39

St. Elmo's avatar
St. Elmo committed
40
Add `genes` to `model` based on gene `id`.
41
"""
St. Elmo's avatar
St. Elmo committed
42
function add_genes!(model::StandardModel, genes::Vector{Gene})
43
    for gene in genes
St. Elmo's avatar
St. Elmo committed
44
        model.genes[gene.id] = gene
45
46
47
    end
end

St. Elmo's avatar
St. Elmo committed
48
49
50
51
52
"""
    add_gene!(model::StandardModel, genes::Gene)

Add `gene` to `model` based on gene `id`.
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
53
add_gene!(model::StandardModel, gene::Gene) = add_genes!(model, [gene])
54

Marvin van Aalst's avatar
Marvin van Aalst committed
55
"""
56
    @add_reactions!(model::Symbol, ex::Expr)
Marvin van Aalst's avatar
Marvin van Aalst committed
57
58
59
60
61
62

Shortcut to add multiple reactions and their lower and upper bounds

Call variants
-------------
```
Marvin van Aalst's avatar
Marvin van Aalst committed
63
@add_reactions! model begin
Marvin van Aalst's avatar
Marvin van Aalst committed
64
65
66
    reaction_name, reaction
end

Marvin van Aalst's avatar
Marvin van Aalst committed
67
@add_reactions! model begin
Marvin van Aalst's avatar
Marvin van Aalst committed
68
69
70
    reaction_name, reaction, lower_bound
end

Marvin van Aalst's avatar
Marvin van Aalst committed
71
@add_reactions! model begin
Marvin van Aalst's avatar
Marvin van Aalst committed
72
73
74
75
76
77
78
    reaction_name, reaction, lower_bound, upper_bound
end
```

Examples
--------
```
Marvin van Aalst's avatar
Marvin van Aalst committed
79
@add_reactions! model begin
St. Elmo's avatar
St. Elmo committed
80
81
82
    "v1", nothing ⟶ A, 0, 500
    "v2", A ⟷ B + C, -500
    "v3", B + C ⟶ nothing
Marvin van Aalst's avatar
Marvin van Aalst committed
83
84
85
86
87
88
89
90
91
92
end
```
"""
macro add_reactions!(model::Symbol, ex::Expr)
    model = esc(model)
    all_reactions = Expr(:block)
    for line in MacroTools.striplines(ex).args
        args = line.args
        id = esc(args[1])
        reaction = esc(args[2])
St. Elmo's avatar
St. Elmo committed
93
94
        push!(all_reactions.args, :(r = $reaction))
        push!(all_reactions.args, :(r.id = $id))
Marvin van Aalst's avatar
Marvin van Aalst committed
95
96
        if length(args) == 3
            lb = args[3]
St. Elmo's avatar
St. Elmo committed
97
            push!(all_reactions.args, :(r.lb = $lb))
Marvin van Aalst's avatar
Marvin van Aalst committed
98
99
100
        elseif length(args) == 4
            lb = args[3]
            ub = args[4]
St. Elmo's avatar
St. Elmo committed
101
102
            push!(all_reactions.args, :(r.lb = $lb))
            push!(all_reactions.args, :(r.ub = $ub))
Marvin van Aalst's avatar
Marvin van Aalst committed
103
        end
St. Elmo's avatar
St. Elmo committed
104
        push!(all_reactions.args, :(add_reaction!($model, r)))
Marvin van Aalst's avatar
Marvin van Aalst committed
105
106
107
108
    end
    return all_reactions
end

109
"""
St. Elmo's avatar
St. Elmo committed
110
    remove_reactions!(model::StandardModel, ids::Vector{String})
111

St. Elmo's avatar
St. Elmo committed
112
Remove all reactions with `ids` from `model`. Note, may result in orphan metabolites.
St. Elmo's avatar
St. Elmo committed
113
114

# Example
St. Elmo's avatar
St. Elmo committed
115
```
St. Elmo's avatar
St. Elmo committed
116
remove_reactions!(model, ["EX_glc__D_e", "fba"])
St. Elmo's avatar
St. Elmo committed
117
```
118
"""
St. Elmo's avatar
St. Elmo committed
119
function remove_reactions!(model::StandardModel, ids::Vector{String})
St. Elmo's avatar
St. Elmo committed
120
    pop!.(Ref(model.reactions), ids)
St. Elmo's avatar
St. Elmo committed
121
122
end

St. Elmo's avatar
St. Elmo committed
123
124
125
126
127
128
129
130
131
132
"""
    remove_reaction!(model::StandardModel, id::String)

Remove reaction with `id` from `model`. Note, may result in orphan metabolites.

# Example
```
remove_reaction!(model, "EX_glc__D_e")
```
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
133
remove_reaction!(model::StandardModel, id::String) = remove_reactions!(model, [id])
134
135

"""
St. Elmo's avatar
St. Elmo committed
136
    remove_metabolites!(model::StandardModel, ids::Vector{String})
St. Elmo's avatar
St. Elmo committed
137
138

Remove all metabolites with `ids` from `model`.
St. Elmo's avatar
St. Elmo committed
139
Warning, this could leave the model inconsistent, e.g. a reaction might
St. Elmo's avatar
St. Elmo committed
140
require the deleted metabolite, in which case analysis functions will error.
141

St. Elmo's avatar
St. Elmo committed
142
# Example
St. Elmo's avatar
St. Elmo committed
143
144
145
```
remove_metabolites!(model, ["atp_c", "adp_c"])
```
146
"""
St. Elmo's avatar
St. Elmo committed
147
function remove_metabolites!(model::StandardModel, ids::Vector{String})
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
148
    pop!.(Ref(model.metabolites), ids)
St. Elmo's avatar
St. Elmo committed
149
150
end

St. Elmo's avatar
St. Elmo committed
151
152
153
154
155
156
157
158
159
160
161
162
"""
    remove_metabolite!(model::StandardModel, id::String)

Remove metabolite with `id` from `model`.
Warning, this could leave the model inconsistent, e.g. a reaction might
require the deleted metabolite, in which case analysis functions will error.

# Example
```
remove_metabolite!(model, "atp_c")
```
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
163
remove_metabolite!(model::StandardModel, id::String) = remove_metabolites!(model, [id])
164
165

"""
St. Elmo's avatar
St. Elmo committed
166
167
168
169
170
    remove_genes!(
        model::StandardModel,
        ids::Vector{String};
        knockout_reactions::Bool = false,
    )
171

St. Elmo's avatar
St. Elmo committed
172
173
Remove all genes with `ids` from `model`. If `knockout_reactions` is true, then also 
constrain reactions that require the genes to function to carry zero flux.
St. Elmo's avatar
St. Elmo committed
174
175

# Example
St. Elmo's avatar
St. Elmo committed
176
```
St. Elmo's avatar
St. Elmo committed
177
remove_genes!(model, ["g1", "g2"])
St. Elmo's avatar
St. Elmo committed
178
```
179
"""
St. Elmo's avatar
St. Elmo committed
180
function remove_genes!(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
181
    model::StandardModel,
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
182
    gids::Vector{String};
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
183
184
185
186
187
    knockout_reactions::Bool = false,
)
    if knockout_reactions
        rm_reactions = String[]
        for (rid, r) in model.reactions
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
188
            if !isnothing(r.grr) &&
St. Elmo's avatar
St. Elmo committed
189
               all(any(in.(gids, Ref(conjunction))) for conjunction in r.grr)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
190
                push!(rm_reactions, rid)
Marvin van Aalst's avatar
Marvin van Aalst committed
191
192
            end
        end
St. Elmo's avatar
St. Elmo committed
193
        pop!.(Ref(model.reactions), rm_reactions)
Marvin van Aalst's avatar
Marvin van Aalst committed
194
    end
St. Elmo's avatar
St. Elmo committed
195
    pop!.(Ref(model.genes), gids)
196
end
Marvin van Aalst's avatar
Marvin van Aalst committed
197

St. Elmo's avatar
St. Elmo committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
    remove_gene!(
        model::StandardModel,
        id::Vector{String};
        knockout_reactions::Bool = false,
    )

Remove gene with `id` from `model`. If `knockout_reactions` is true, then also 
constrain reactions that require the genes to function to carry zero flux.

# Example
```
remove_gene!(model, "g1")
```
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
213
214
remove_gene!(model::StandardModel, gid::String; knockout_reactions::Bool = false) =
    remove_genes!(model, [gid]; knockout_reactions = knockout_reactions)
St. Elmo's avatar
St. Elmo committed
215

Marvin van Aalst's avatar
Marvin van Aalst committed
216
217
218
219
220
function set_bound(model::StandardModel, reaction_id::String; ub, lb)
    reaction = model.reactions[reaction_id]
    reaction.lb = lb
    reaction.ub = ub
end