Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do sensitivities make sense in a tree? #23

Draft
wants to merge 4 commits into
base: mk-gen-substs
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions docs/src/2-differentiate-enzyme-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ model.reactions[glc_idx]["lower_bound"] = -1000.0
pfl_idx = first(indexin(["PFL"], rids))
model.reactions[pfl_idx]["upper_bound"] = 0.0

rid_kcat = Dict(k => FastDifferentiation.Node(Symbol(k)) for (k,_) in ecoli_core_reaction_kcats)
rid_kcat =
Dict(k => FastDifferentiation.Node(Symbol(k)) for (k, _) in ecoli_core_reaction_kcats)
kcats = Symbol.(keys(ecoli_core_reaction_kcats))

parameter_values = Dict{Symbol, Float64}()
parameter_values = Dict{Symbol,Float64}()

reaction_isozymes = Dict{String,Dict{String,ParameterIsozyme}}() # a mapping from reaction IDs to isozyme IDs to isozyme structs.
float_reaction_isozymes = Dict{String,Dict{String,COBREXA.Isozyme}}() #src
Expand Down Expand Up @@ -102,15 +103,15 @@ ec_solution_cobrexa = enzyme_constrained_flux_balance_analysis( #src
@test isapprox(ec_solution.objective, ec_solution_cobrexa.objective; atol = TEST_TOLERANCE) #src

# This solution contains many inactive reactions
sort(collect(ec_solution.fluxes), by=ComposedFunction(abs, last))
sort(collect(ec_solution.fluxes), by = ComposedFunction(abs, last))

@test any(values(ec_solution.fluxes) .≈ 0) #src

# And also many inactive gene products.

sort(collect(ec_solution.gene_product_amounts), by=last)
sort(collect(ec_solution.gene_product_amounts), by = last)

@test any(isapprox.(values(ec_solution.gene_product_amounts), 0, atol=1e-8)) #src
@test any(isapprox.(values(ec_solution.gene_product_amounts), 0, atol = 1e-8)) #src

# With theory, you can show that this introduces flux variability into the
# solution, making it non-unique, and consequently non-differentiable. To fix
Expand Down Expand Up @@ -149,7 +150,7 @@ ec_solution2, x_vals, eq_dual_vals, ineq_dual_vals = optimized_constraints_with_
ec_solution2

# no zero fluxes
sort(collect(ec_solution.fluxes), by=ComposedFunction(abs, last))
sort(collect(ec_solution.fluxes), by = ComposedFunction(abs, last))

# no zero genes
sort(abs.(collect(values(ec_solution2.gene_product_amounts))))
Expand All @@ -164,11 +165,7 @@ sort(abs.(collect(values(ec_solution2.gene_product_amounts))))

parameters = [:capacitylimitation; kcats]

pkm_kkt, vids = differentiate_prepare_kkt(
pkm,
pkm.objective.value,
parameters
)
pkm_kkt, vids = differentiate_prepare_kkt(pkm, pkm.objective.value, parameters)

sens = differentiate_solution(
pkm_kkt,
Expand Down Expand Up @@ -228,3 +225,32 @@ f, a, hm = heatmap(
)
Colorbar(f[1, 2], hm)
f

# Looking at sensitivities in the original model structure:

struct Scalarize{T}
x::T
end

Base.:+(a, b::Scalarize) = Scalarize(a .+ b.x)
Base.:+(a::Scalarize, b) = Scalarize(a.x .+ b)
Base.:+(a::Scalarize, b::Scalarize) = Scalarize(a.x .+ b.x)
Base.:*(a, b::Scalarize) = Scalarize(a .* b.x)
Base.:*(a::Scalarize, b) = Scalarize(a.x .* b)
Base.:*(a::Scalarize, b::Scalarize) = Scalarize(a.x .* b.x)

unscalarize(x::Scalarize) = x.x

pkmv = DifferentiableMetabolism.substitute(pkm, x -> parameter_values[x])

import ConstraintTrees as C
import LinearAlgebra

sensitivity_tree =
:parameters^C.Tree{Vector{Float64}}(
parameters .=> collect.(eachrow(float.(LinearAlgebra.I(length(parameters))))),
) * C.map(
unscalarize,
C.substitute_values(pkmv, Scalarize.(collect.(eachrow(sens)))),
Vector{Float64},
)
Loading