FluxSummary.jl 3.89 KB
Newer Older
St. Elmo's avatar
hmm    
St. Elmo committed
1
"""
St. Elmo's avatar
St. Elmo committed
2
FluxSummary
St. Elmo's avatar
hmm    
St. Elmo committed
3

St. Elmo's avatar
St. Elmo committed
4
5
A struct used to store summary information about the solution 
of a constraint based analysis result.
St. Elmo's avatar
hmm    
St. Elmo committed
6
"""
St. Elmo's avatar
St. Elmo committed
7
struct FluxSummary
St. Elmo's avatar
St. Elmo committed
8
9
10
11
    biomass_fluxes :: OrderedDict{String, Float64}
    import_fluxes :: OrderedDict{String, Float64}
    export_fluxes :: OrderedDict{String, Float64}
    unbounded_fluxes :: OrderedDict{String, Float64}
St. Elmo's avatar
St. Elmo committed
12
13
14
end

"""
St. Elmo's avatar
St. Elmo committed
15
    flux_summary(flux_result::Dict{String, Float64}; 
St. Elmo's avatar
St. Elmo committed
16
17
18
19
20
21
22
23
                exclude_exchanges = false,
                exchange_prefixes = _constants.exchange_prefixes,
                biomass_strings = _constants.biomass_strings,
                exclude_biomass = false,
                small_flux_bound = 1.0/_constants.default_reaction_bound^2,
                large_flux_bound = _constants.default_reaction_bound,
                keep_unbounded = false,
                )::FluxSummary
St. Elmo's avatar
St. Elmo committed
24
25

Return a `FluxSummary` struct based on the `flux_result` of a constraint based
St. Elmo's avatar
St. Elmo committed
26
analysis simulation. Internally this function uses
St. Elmo's avatar
St. Elmo committed
27
[`looks_like_biomass_reaction`](@ref) and
St. Elmo's avatar
St. Elmo committed
28
29
30
31
[`looks_like_exchange_reaction`](@ref). The corresponding keyword arguments
passed to these functions. Use this if your model has non-standard ids for
reactions. Fluxes smaller than `small_flux_bound` are not stored, while fluxes
larger than `large_flux_bound` are only stored if `keep_unbounded` is `true`.
St. Elmo's avatar
St. Elmo committed
32
33
34
35
36
37
38

This function is most useful as a way to generate a struct that has nice pretty
printing of flux results. The resultant struct can also be used in downstream
applications if necessary.

# Example
```
St. Elmo's avatar
St. Elmo committed
39
40
julia> sol = flux_balance_analysis_dict(model, Tulip.Optimizer)
julia> fr = flux_summary(sol)
St. Elmo's avatar
St. Elmo committed
41
42
43
44
45
46
47
48
49
50
51
Biomass:
  BIOMASS_Ecoli_core_w_GAM: 0.8739
Import:
  EX_o2_e:     -21.7995
  EX_glc__D_e: -10.0
  EX_nh4_e:    -4.7653
  EX_pi_e:     -3.2149
Export:
  EX_h_e:      17.5309
  EX_co2_e:    22.8098
  EX_h2o_e:    29.1758
St. Elmo's avatar
St. Elmo committed
52
53
```
"""
St. Elmo's avatar
St. Elmo committed
54
function flux_summary(flux_result::Dict{String, Float64}; 
St. Elmo's avatar
St. Elmo committed
55
56
57
58
59
60
61
62
    exclude_exchanges = false,
    exchange_prefixes = _constants.exchange_prefixes,
    biomass_strings = _constants.biomass_strings,
    exclude_biomass = false,
    small_flux_bound = 1.0/_constants.default_reaction_bound^2,
    large_flux_bound = _constants.default_reaction_bound,
    keep_unbounded = false,
    )
St. Elmo's avatar
St. Elmo committed
63
64
65
    rxn_ids = collect(keys(flux_result))
    ex_rxns = filter(x -> looks_like_exchange_reaction(x, exclude_biomass=exclude_biomass, biomass_strings=biomass_strings, exchange_prefixes=exchange_prefixes), rxn_ids)
    bmasses = filter(x -> looks_like_biomass_reaction(x; exclude_exchanges=exclude_exchanges, exchange_prefixes=exchange_prefixes, biomass_strings=biomass_strings), rxn_ids)
St. Elmo's avatar
St. Elmo committed
66
67
68
69
70
71
72
73
74

    ex_fluxes = [flux_result[k] for k in ex_rxns]
    bmass_fluxes = [flux_result[k] for k in bmasses]

    idx_srt_fluxes = sortperm(ex_fluxes)
    import_fluxes = [idx for idx in idx_srt_fluxes if -large_flux_bound < ex_fluxes[idx] <= -small_flux_bound]
    export_fluxes =  [idx for idx in idx_srt_fluxes if small_flux_bound < ex_fluxes[idx] <= large_flux_bound]

    if keep_unbounded
St. Elmo's avatar
St. Elmo committed
75
76
77
      lower_unbounded = [idx for idx in idx_srt_fluxes if ex_fluxes[idx] <= -large_flux_bound]
      upper_unbounded = [idx for idx in idx_srt_fluxes if ex_fluxes[idx] >= large_flux_bound]
      return FluxSummary(Dict(k => v for (k, v) in zip(bmasses, bmass_fluxes)), 
St. Elmo's avatar
St. Elmo committed
78
79
80
        OrderedDict(k => v for (k, v) in zip(ex_rxns[import_fluxes], ex_fluxes[import_fluxes])),
        OrderedDict(k => v for (k, v) in zip(ex_rxns[export_fluxes], ex_fluxes[export_fluxes])),
        OrderedDict(k => v for (k, v) in zip([ex_rxns[lower_unbounded]; ex_rxns[upper_unbounded]], [ex_fluxes[lower_unbounded]; ex_fluxes[upper_unbounded]])),
St. Elmo's avatar
St. Elmo committed
81
82
        )
    else
St. Elmo's avatar
St. Elmo committed
83
84
85
86
        return FluxSummary(OrderedDict(k => v for (k, v) in zip(bmasses, bmass_fluxes)), 
        OrderedDict(k => v for (k, v) in zip(ex_rxns[import_fluxes], ex_fluxes[import_fluxes])),
        OrderedDict(k => v for (k, v) in zip(ex_rxns[export_fluxes], ex_fluxes[export_fluxes])),
        OrderedDict{String, Float64}(),
St. Elmo's avatar
St. Elmo committed
87
88
89
        )
    end
end