In [12]:
import pandas
pandas.options.display.max_rows = 100
import cobra.test
model = cobra.test.create_test_model("textbook")
In [2]:
model.optimize()
Out[2]:
The Model.optimize() function will return a Solution object, which will also be stored at model.solution. A solution object has several attributes:
For example, after the last call to model.optimize(), the status should be 'optimal' if the solver returned no errors, and f should be the objective value
In [3]:
model.solution.status
Out[3]:
In [4]:
model.solution.f
Out[4]:
In [5]:
model.objective
Out[5]:
The objective function can be changed by assigning Model.objective, which can be a reaction object (or just it's name), or a dict of {Reaction: objective_coefficient}.
In [6]:
# change the objective to ATPM
# the upper bound should be 1000 so we get the actual optimal value
model.reactions.get_by_id("ATPM").upper_bound = 1000.
model.objective = "ATPM"
model.objective
Out[6]:
In [7]:
model.optimize()
Out[7]:
The objective function can also be changed by setting Reaction.objective_coefficient directly.
In [9]:
model.reactions.get_by_id("ATPM").objective_coefficient = 0.
model.reactions.get_by_id("Biomass_Ecoli_core").objective_coefficient = 1.
model.objective
Out[9]:
In [35]:
fva_result = cobra.flux_analysis.flux_variability_analysis(model, model.reactions[:20])
pandas.DataFrame.from_dict(fva_result).T
Out[35]:
Setting parameter fraction_of_optimium=0.90 would give the flux ranges for reactions at 90% optimality.
In [36]:
fva_result = cobra.flux_analysis.flux_variability_analysis(model, model.reactions[:20], fraction_of_optimum=0.9)
pandas.DataFrame.from_dict(fva_result).T
Out[36]: