Making a function for a different type of FBA #1350
-
Hi,` I am currently working on an FBA model where the user can adjust certain fluxes. I have successfully done this for FBA which has the equation Z = ∑Cj*vj where Z is the cellular objective, Cj is the relative weights of each reaction and vj is the optimised flux. My new equation is ∑D*vj < 200000 where D is a known variable for each flux. Is there a way I can edit the source code of model.optimize to make my own function. Or could somebody point me to the parts of this code that I can change to 200000 and D.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, so you should not need to change any code. As far as I understand all that is supported with what is there. from cobra.io import load_model
from numpy.random import uniform
from optlang.symbolics import Zero
import pandas as pd
C = 200 # using a smaller constant here to make the effect visible
model = load_model("textbook")
rids = [r.id for r in model.reactions] # get the reaction IDs
D = dict(zip(rids, uniform(size=len(rids)))) # create a random D vector
# We add an empt constraint and then update the coefficients.
# This is the fastest way and will be quick even for large models.
constraint = model.problem.Constraint(Zero, name="cost", ub=C)
model.add_cons_vars([constraint])
# We add the D_i * (v_f + v_r) terms to the constraint
coefs = dict()
for rid, co in D.items():
rxn = model.reactions.get_by_id(rid)
coefs.update({rxn.forward_variable: co, rxn.reverse_variable: co})
model.constraints.cost.set_linear_coefficients(coefs) # update the constraint coefficients
sol = model.optimize() # get the model solution
# Check that it works by inspecting the solution
print("sum(D*v) =", sum(sol.fluxes.abs() * pd.Series(D))) |
Beta Was this translation helpful? Give feedback.
Hi, so you should not need to change any code. As far as I understand all that is supported with what is there.$\sum_i D_i \cdot v_i < C$ is a constraint and not really an objective. Do you still want to to maximize the biomass here? Another thing is that if you consider irreversible fluxes then this is a bit odd since your $v_i$ can be negative. Or do you want to do something like a cost optimization? In that case you probably meant $\sum_i D_i \cdot |v_i| < C$ which would be something like a weighted PFBA constraint. Assuming that is what you want to do, here is some example code. I will assume that your
D_i
is a dictionary-like object with reaction IDs as keys (I will just gener…