Skip to content

Commit

Permalink
merged with main
Browse files Browse the repository at this point in the history
  • Loading branch information
SamiralVdB committed Jul 22, 2024
1 parent 6a827b0 commit 27e05db
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 47 deletions.
6 changes: 2 additions & 4 deletions src/PAModelpy/EnzymeSectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,8 @@ def check_kcat_values(self, model, reaction, enzyme_dict):
directions = []
kcats = []
for enzyme_info in enzyme_dict.values():
# get all directions from the kcat dictfor key, value in enzyme_info.items():
if key == 'f' or key =='b':
directions += [key]
for key, value in enzyme_info.items():
# get all directions from the kcat dict
for key, value in enzyme_info.items():
if key == 'f' or key =='b':
directions += [key]
kcats += [value]
Expand Down
136 changes: 93 additions & 43 deletions src/PAModelpy/PAModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,8 @@ def remove_sectors(
sectors = [sectors]

for sector in sectors:
if isinstance(sector, str): sector = self.sectors.get_by_id(sector)

print(f"Removing the following protein sector: {sector.id}\n")
# remove the connection to the model
sector._model = None
Expand All @@ -1621,52 +1623,100 @@ def remove_sectors(
except ValueError:
warnings.warn(f"{sector.id} not in {self}")

# remove the sector from the total protein if it is there
if self.TOTAL_PROTEIN_CONSTRAINT_ID in self.constraints.keys():
# remove parts of constraint corresponding to the enzyme sector from the total_protein_constraint
# 1. add the intercept value from the sum of protein (Total_protein == Etot)
tpc_ub = self.constraints[self.TOTAL_PROTEIN_CONSTRAINT_ID].ub
self.constraints[self.TOTAL_PROTEIN_CONSTRAINT_ID].ub = (
tpc_ub + sector.intercept
)
# check if the sector is the ActiveEnzymeSector, this needs to have a different removal mechanism
if isinstance(sector, ActiveEnzymeSector):
self.remove_active_enzymes_sector(sector)
else:
self.remove_linear_sector(sector)

# 2. remove link between flux and enzyme concentration
# link enzyme concentration in the sector to the total enzyme concentration
lin_rxn = self.reactions.get_by_id(sector.id_list[0])
self.constraints[
self.TOTAL_PROTEIN_CONSTRAINT_ID
].set_linear_coefficients(
{lin_rxn.forward_variable: 0, lin_rxn.reverse_variable: 0}
)
def remove_active_enzymes_sector(self, sector: ActiveEnzymeSector) -> None:
"""
Remove an active enzyme sector from the model.
else:
# remove the associated constraints
for constraint in sector.constraints:
if isinstance(constraint, Enzyme):
self.remove_enzymes([constraint])
# check if constraint is in the solver
if constraint in self.constraints.values():
self.remove_cons_vars([constraint])
self.solver.update()
This function performs the following steps:
1. Removes all enzymes associated with the sector.
2. Removes all catalytic events associated with the sector.
3. If a total protein constraint exists, it removes this constraint.
4. Deletes the sector constraint from the model's easy lookup.
5. Removes the sector from the model and disconnects its link to the model.
# remove the associated variables
for variable in sector.variables:
if isinstance(variable, CatalyticEvent):
self.remove_catalytic_events([variable])
else:
self.remove_cons_vars([variable])
self.solver.update()
# remove reference to the sector variables in all groups
associated_groups = self.get_associated_groups(variable)
for group in associated_groups:
group.remove_members(variable)

# remove sector constraint from model easy lookup:
del self.sector_constraints[sector.id]

# remove the sector and its connection to the model
self.sectors.remove(sector)
sector._model = None
Args:
sector (ActiveEnzymeSector): The active enzyme sector to be removed.
Returns:
None
"""

self.remove_enzymes(self.enzymes.copy())
self.remove_catalytic_events(self.catalytic_events)
if self.TOTAL_PROTEIN_CONSTRAINT_ID in self.constraints.keys():
# remove total_protein_constraint
self.remove_cons_vars(self.constraints[self.TOTAL_PROTEIN_CONSTRAINT_ID])

# remove the sector and its connection to the model
self.sectors.remove(sector)
sector._model = None

def remove_linear_sector(self, sector: Union[UnusedEnzymeSector, TransEnzymeSector, CustomSector]) -> None:
"""
Remove a linear sector from the model.
This function performs the following steps:
1. If a total protein constraint exists, it adjusts the constraint to remove the sector's contribution.
2. Removes the associated constraints and variables.
3. Deletes the sector constraint from the model's easy lookup.
4. Removes the sector from the model and disconnects its link to the model.
Args:
sector (Union[UnusedEnzymeSector, TransEnzymeSector, CustomSector]): The linear sector to be removed.
Returns:
None
"""
# remove the sector from the total protein if it is there
if self.TOTAL_PROTEIN_CONSTRAINT_ID in self.constraints.keys():
# remove parts of constraint corresponding to the enzyme sector from the total_protein_constraint
# 1. add the intercept value from the sum of protein (Total_protein == Etot)
tpc_ub = self.constraints[self.TOTAL_PROTEIN_CONSTRAINT_ID].ub
self.constraints[self.TOTAL_PROTEIN_CONSTRAINT_ID].ub = tpc_ub + sector.intercept

# 2. remove link between flux and enzyme concentration
# link enzyme concentration in the sector to the total enzyme concentration
lin_rxn = self.reactions.get_by_id(sector.id_list[0])
self.constraints[self.TOTAL_PROTEIN_CONSTRAINT_ID].set_linear_coefficients({
lin_rxn.forward_variable: 0,
lin_rxn.reverse_variable: 0
})

else:

# remove the associated constraints
for constraint in sector.constraints:
if isinstance(constraint, Enzyme):
self.remove_enzymes([constraint])
# check if constraint is in the solver
if constraint in self.constraints.values():
self.remove_cons_vars([constraint])
self.solver.update()

# remove the associated variables
for variable in sector.variables:
if isinstance(variable, CatalyticEvent):
self.remove_catalytic_events([variable])
else:
self.remove_cons_vars([variable])
self.solver.update()
# remove reference to the sector variables in all groups
associated_groups = self.get_associated_groups(variable)
for group in associated_groups:
group.remove_members(variable)

# remove sector constraint from model easy lookup:
del self.sector_constraints[sector.id]

# remove the sector and its connection to the model
self.sectors.remove(sector)
sector._model = None

def test(self, glc_flux: Union[int, float] = 10):
"""
Expand Down

0 comments on commit 27e05db

Please sign in to comment.