StandardModel.jl 3.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

"""
    Base.copy(m::StandardModel)

Shallow copy of a [`StandardModel`](@ref)
"""
Base.copy(m::StandardModel) = StandardModel(m.id, m.reactions, m.metabolites, m.genes)

"""
    Base.copy(r::Reaction)

Shallow copy of a [`Reaction`](@ref)
"""
Base.copy(r::Reaction) = Reaction(
    r.id;
    name = r.name,
    metabolites = r.metabolites,
    lb = r.lb,
    ub = r.ub,
    grr = r.grr,
    subsystem = r.subsystem,
    notes = r.notes,
    annotations = r.annotations,
    objective_coefficient = r.objective_coefficient,
)

"""
    Base.copy(m::Metabolite)

Shallow copy of a [`Metabolite`](@ref)
"""
Base.copy(m::Metabolite) = Metabolite(
    m.id;
    name = m.name,
    formula = m.formula,
    charge = m.charge,
    compartment = m.compartment,
    notes = m.notes,
    annotations = m.annotations,
)

"""
    Base.copy(g::Gene)

Shallow copy of a [`Gene`](@ref)
"""
Base.copy(g::Gene) = Gene(g.id; name = g.name, notes = g.notes, annotations = g.annotations)

49
50
51
"""
    atom_exchange(flux_dict::Dict{String, Float64}, model::StandardModel)

St. Elmo's avatar
St. Elmo committed
52
53
Return a dictionary mapping the flux of atoms across the boundary of the model 
given `flux_dict` (the solution of a constraint based analysis) of reactions in `model`.
54
55
56
"""
function atom_exchange(flux_dict::Dict{String,Float64}, model::StandardModel)
    atom_flux = Dict{String,Float64}()
St. Elmo's avatar
St. Elmo committed
57
58
    for (rxn_id, flux) in flux_dict
        if is_boundary(model.reactions[rxn_id])
St. Elmo's avatar
St. Elmo committed
59
            for (met, stoich_rxn) in model.reactions[rxn_id].metabolites
St. Elmo's avatar
St. Elmo committed
60
                adict = get_atoms(model.metabolites[met])
St. Elmo's avatar
St. Elmo committed
61
                for (atom, stoich_molecule) in adict
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
62
63
                    atom_flux[atom] =
                        get(atom_flux, atom, 0.0) + flux * stoich_rxn * stoich_molecule
64
65
66
67
68
69
70
                end
            end
        end
    end
    return atom_flux
end

St. Elmo's avatar
St. Elmo committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
    atom_exchange(rxn_id::String, model::StandardModel)

Return a dictionary mapping the flux of atoms through a reaction in `model`.
"""
function atom_exchange(rxn_id::String, model::StandardModel)
    atom_flux = Dict{String,Float64}()
    for (met, stoich_rxn) in model.reactions[rxn_id].metabolites
        adict = get_atoms(model.metabolites[met])
        for (atom, stoich_molecule) in adict
            atom_flux[atom] = get(atom_flux, atom, 0.0) + stoich_rxn * stoich_molecule
        end
    end
    return atom_flux
end

87
"""
St. Elmo's avatar
St. Elmo committed
88
    metabolite_fluxes(flux_dict::Dict{String, Float64}, model::StandardModel)
89
90
91
92

Return two dictionaries of metabolite `id`s mapped to reactions that consume or 
produce them given the flux distribution supplied in `fluxdict`.
"""
St. Elmo's avatar
St. Elmo committed
93
function metabolite_fluxes(flux_dict::Dict{String,Float64}, model::StandardModel)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
94
    S = stoichiometry(model)
95
96
97
98
99
100
101
    rxnids = reactions(model)
    metids = metabolites(model)

    producing = Dict{String,Dict{String,Float64}}()
    consuming = Dict{String,Dict{String,Float64}}()
    for (row, metid) in enumerate(metids)
        for (col, rxnid) in enumerate(rxnids)
St. Elmo's avatar
St. Elmo committed
102
            mf = flux_dict[rxnid] * S[row, col]
103
            # ignore zero flux
104
            if mf < -_constants.tolerance # consuming rxn
105
106
107
108
109
                if haskey(consuming, metid)
                    consuming[metid][rxnid] = mf
                else
                    consuming[metid] = Dict(rxnid => mf)
                end
110
            elseif mf > _constants.tolerance
111
112
113
114
115
116
117
118
119
120
                if haskey(producing, metid)
                    producing[metid][rxnid] = mf
                else
                    producing[metid] = Dict(rxnid => mf)
                end
            end
        end
    end
    return consuming, producing
end