Skip to content

Commit

Permalink
enabled sensitivity analysis for renewed model structure
Browse files Browse the repository at this point in the history
  • Loading branch information
SamiralVdB committed Jul 25, 2024
1 parent 27e05db commit 4316f67
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 28 deletions.
73 changes: 51 additions & 22 deletions src/PAModelpy/PAModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,35 @@ def calculate_csc(self, obj_value, mu, mu_ub, mu_lb, mu_ec_f, mu_ec_b):
len(self.capacity_sensitivity_coefficients)
] = new_row_LB

print(self.enzymes)
for enzyme in self.enzymes:
self.calculate_enzyme_csc(enzyme, mu_ec_f, mu_ec_b, obj_value)
for catalyzing_enzyme in self._get_catalyzing_enzymes_for_enzyme(enzyme):
ce = self.enzymes.get_by_id(catalyzing_enzyme)
self.calculate_enzyme_csc(ce, mu_ec_f, mu_ec_b, obj_value)

def _get_catalyzing_enzymes_for_enzyme(self, enzyme:Union[Enzyme, str])-> list:
""" Retrieves those enzymes which are associated with a constraint
Enzymes which are only present in an enzyme complex are thus ignored
Args:
enzyme: Enzyme object or enzyme identifier
Returns:
"""
if isinstance(enzyme, Enzyme):
enzyme_id = enzyme.id
else:
enzyme_id = enzyme

#get all associated constraints
associated_constraints = ["_".join(cid.split("_")[:-1]) for cid in self.constraints.keys() if (enzyme_id in cid)&("_max" in cid)] # enzymes always have a min and max constraint
#remove the constraints associated with a reaction
associated_enzymes = [cid for cid in associated_constraints if not any([rxn.id in cid for rxn in self.reactions])]
# removing duplicates and empty strings
associated_enzymes = list(set([enz for enz in associated_enzymes if len(enz)>0]))
return associated_enzymes

def calculate_csc_for_molecule(self, molecule: Union[Enzyme],
mu_min:pd.DataFrame, mu_max:pd.DataFrame, obj_value:float,
Expand Down Expand Up @@ -1079,27 +1106,29 @@ def calculate_esc(self, obj_value, mu_ec_f, mu_ec_b):

# calculate enzyme sensitivity coefficient
for enzyme in self.enzymes:
# get the reactions associated with the enzyme
reactions = ",".join(self.get_reactions_with_enzyme_id(enzyme.id))

# get the right row from the shadow price dataframes
sp_ec_f = mu_ec_f[mu_ec_f["rxn_id"] == f"EC_{enzyme.id}"][
"shadow_prices"
].iloc[0]
sp_ec_b = mu_ec_b[mu_ec_b["rxn_id"] == f"EC_{enzyme.id}"][
"shadow_prices"
].iloc[0]
e_fwd = self.enzyme_variables.get_by_id(enzyme.id).forward_variable.primal
e_rev = self.enzyme_variables.get_by_id(enzyme.id).reverse_variable.primal

# EC: enzyme constraint
enzyme_sensitivity_coefficient = (
e_fwd * sp_ec_f + e_rev * sp_ec_b
) / obj_value
# add new_row to dataframe
self.enzyme_sensitivity_coefficients.loc[
len(self.enzyme_sensitivity_coefficients)
] = [reactions, enzyme.id, enzyme_sensitivity_coefficient]
for catalyzing_enzyme in self._get_catalyzing_enzymes_for_enzyme(enzyme):

# get the reactions associated with the enzyme
reactions = ",".join(self.get_reactions_with_enzyme_id(catalyzing_enzyme))

# get the right row from the shadow price dataframes
sp_ec_f = mu_ec_f[mu_ec_f["rxn_id"] == f"EC_{catalyzing_enzyme}"][
"shadow_prices"
].iloc[0]
sp_ec_b = mu_ec_b[mu_ec_b["rxn_id"] == f"EC_{catalyzing_enzyme}"][
"shadow_prices"
].iloc[0]
e_fwd = self.enzyme_variables.get_by_id(catalyzing_enzyme).forward_variable.primal
e_rev = self.enzyme_variables.get_by_id(catalyzing_enzyme).reverse_variable.primal

# EC: enzyme constraint
enzyme_sensitivity_coefficient = (
e_fwd * sp_ec_f + e_rev * sp_ec_b
) / obj_value
# add new_row to dataframe
self.enzyme_sensitivity_coefficients.loc[
len(self.enzyme_sensitivity_coefficients)
] = [reactions, catalyzing_enzyme, enzyme_sensitivity_coefficient]

def calculate_sum_of_enzymes(self):
"""
Expand Down
23 changes: 17 additions & 6 deletions tests/unit_tests/test_pamodel/test_pam_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,23 @@ def test_if_toy_pam_with_enzyme_comples_has_same_growth_rate_as_without():

assert sut.objective.value == pytest.approx(toy_pam.objective.value, abs = 1e-6)

# def test_set_up_ecolicore_pam_works():
# sut = set_up_ecolicore_pam()
# assert True
# def test_set_up_ecoli_pam_works():
# sut = set_up_ecoli_pam()
# assert True
def test_set_up_ecolicore_pam_works():
sut = set_up_ecolicore_pam()
sut.optimize()
assert True
def test_if_ecolicore_pam_optimizes():
sut = set_up_ecolicore_pam()
sut.optimize()
assert sut.objective.value > 0

def test_set_up_ecoli_pam_works():
sut = set_up_ecoli_pam()
assert True

def test_if_ecoli_pam_optimizes():
sut = set_up_ecoli_pam()
sut.optimize()
assert sut.objective.value > 01

#########################################################################################################################
# HELPER FUNCTIONS
Expand Down
13 changes: 13 additions & 0 deletions tests/unit_tests/test_pamodel/test_pamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from cobra.io import load_json_model

from src.PAModelpy import PAModel,Config,ActiveEnzymeSector, UnusedEnzymeSector, TransEnzymeSector
from tests.unit_tests.test_pamodel.test_pam_generation import set_up_toy_pam_with_isozymes_and_enzymecomplex

def test_if_pamodel_change_kcat_function_works():
#arrange
Expand Down Expand Up @@ -155,6 +156,18 @@ def test_if_pamodel_remove_sectors_can_remove_translational_protein_sector():
assert toy_pam.constraints[toy_pam.TOTAL_PROTEIN_CONSTRAINT_ID].ub == tpc_ub + sector.intercept
assert all(coeff == 0 for coeff in lin_coeff.values())

def test_if_pamodel_gets_catalyzing_enzymes_for_enzyme_object():
# Arrange
sut = set_up_toy_pam_with_isozymes_and_enzymecomplex(sensitivity = False)
enzyme_ut = 'E10'
associated_enzymes = ['E10', 'E3_E10_E11']

# Assert
catalyzing_enzymes = sut._get_catalyzing_enzymes_for_enzyme(enzyme_ut)

# Assert
assert all(enz in catalyzing_enzymes for enz in associated_enzymes)

#######################################################################################################
#HELPER METHODS
#######################################################################################################
Expand Down

0 comments on commit 4316f67

Please sign in to comment.