Skip to content

Commit

Permalink
Misc (#485)
Browse files Browse the repository at this point in the history
* report metabolites without formulas when checking biomass weight

* make MCS dialog persistent

* update tooltip when a reaction is modified
  • Loading branch information
axelvonkamp authored Sep 21, 2023
1 parent 80e863b commit b4cc359
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 10 additions & 2 deletions cnapy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl
model.add_cons_vars([gam_slack])
for i in range(len(gam_mets)):
met = gam_mets[i]
sign = numpy.sign(bm_reaction.metabolites[met])
sign = numpy.sign(bm_reaction.metabolites[met]) # !! FIXME: only correct when gam_base is larger than biomass part !!
met.constraint.set_linear_coefficients({gam_slack: sign*gam_max_change*mue_fixed})
gam_mets_sign[i] = sign
qp_terms.append(gam_weight * (gam_slack**2))
Expand Down Expand Up @@ -322,7 +322,14 @@ def check_biomass_weight(model: cobra.Model, bm_reac_id: str) -> float:
It only returns a correct value if the molecular weights of all biomass constituents are given.
"""
bm_coeff = [(m, c) for m,c in model.reactions.get_by_id(bm_reac_id).metabolites.items()]
bm_weight = sum(m.formula_weight/1000*-c for m,c in bm_coeff)
bm_weight = 0.0
for m,c in bm_coeff:
w = m.formula_weight
if w is None or w == 0:
print("Molecular weight of biomass component", m.id, "cannot be calculated from its formula", m.formula)
else:
bm_weight += w/1000*-c
# bm_weight = sum(m.formula_weight/1000*-c for m,c in bm_coeff)
print("Flux of 1 through the biomass reaction produces", bm_weight, "g biomass.")
return bm_weight

Expand Down Expand Up @@ -356,6 +363,7 @@ def replace_ids(dict_list: DictList, annotation_key: str, unambiguous_only: bool
for new_id in candidates:
try:
entry.id = new_id
entry.annotation['original ID'] = old_id
break
except ValueError: # new_id already in use
pass
Expand Down
4 changes: 2 additions & 2 deletions cnapy/gui_elements/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,8 +1837,8 @@ def efmtool(self):
self.efmtool_dialog.exec_()

def mcs(self):
self.mcs_dialog = MCSDialog(
self.appdata, self.centralWidget())
if self.mcs_dialog is None:
self.mcs_dialog = MCSDialog(self.appdata, self.centralWidget())
self.mcs_dialog.show()

def set_onoff(self):
Expand Down
18 changes: 11 additions & 7 deletions cnapy/gui_elements/reactions_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def __lt__(self, other):
# return super().__lt__(other) # infinite recursion with PySide2, __lt__ is a virtual function of QTreeWidgetItem
return self.text(column) < other.text(column)

def update_tooltips(self):
text = "Id: " + self.reaction.id + "\nName: " + self.reaction.name \
+ "\nEquation: " + self.reaction.build_reaction_string()\
+ "\nLowerbound: " + str(self.reaction.lower_bound) \
+ "\nUpper bound: " + str(self.reaction.upper_bound) \
+ "\nObjective coefficient: " + str(self.reaction.objective_coefficient)
self.setToolTip(ReactionListColumn.Id, text)
self.setToolTip(ReactionListColumn.Name, text)

class ReactionList(QWidget):
"""A list of reaction"""

Expand Down Expand Up @@ -172,13 +181,7 @@ def add_reaction(self, reaction: cobra.Reaction) -> ReactionListItem:
item.setFlags(item.flags() | Qt.ItemIsEditable)
item.setText(ReactionListColumn.Id, reaction.id)
item.setText(ReactionListColumn.Name, reaction.name)
text = "Id: " + reaction.id + "\nName: " + reaction.name \
+ "\nEquation: " + reaction.build_reaction_string()\
+ "\nLowerbound: " + str(reaction.lower_bound) \
+ "\nUpper bound: " + str(reaction.upper_bound) \
+ "\nObjective coefficient: " + str(reaction.objective_coefficient)
item.setToolTip(ReactionListColumn.Id, text)
item.setToolTip(ReactionListColumn.Name, text)
item.update_tooltips()
self.update_item(item)
return item

Expand Down Expand Up @@ -316,6 +319,7 @@ def handle_changed_reaction(self, reaction: cobra.Reaction):
old_id = item.text(ReactionListColumn.Id)
item.setText(ReactionListColumn.Id, reaction.id)
item.setText(ReactionListColumn.Name, reaction.name)
item.update_tooltips()
break

self.last_selected = self.reaction_mask.id.text()
Expand Down

0 comments on commit b4cc359

Please sign in to comment.