diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml index e0027cfe..ff04866e 100644 --- a/.github/workflows/ci-test.yml +++ b/.github/workflows/ci-test.yml @@ -1,61 +1,60 @@ name: CI Test -on: [pull_request] +on: + pull_request: + workflow_dispatch: -jobs: - build-linux: - runs-on: "ubuntu-latest" - steps: - - uses: actions/checkout@v4 - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install pytest>=7.2 - - name: Install CNApy - run: | - pip install . - - name: Test CNApy - run: | - pytest -v ./cnapy/tests/test.py +env: + QT_QPA_PLATFORM: offscreen - build-windows: - runs-on: "windows-latest" +jobs: + test: + name: test (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] steps: - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: '3.10' + cache: pip + cache-dependency-path: | + pyproject.toml + setup.py + - name: Install Qt runtime libraries for the offscreen platform plugin + if: runner.os == 'Linux' + run: | + sudo apt-get update + sudo apt-get install -y libgl1 libxkbcommon0 libxcb-cursor0 libegl1 - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pytest>=7.2 + pip install "pytest>=7.2" pytest-qt - name: Install CNApy - run: | - pip install . - - name: Test CNApy - run: | - pytest -v ./cnapy/tests/test.py + run: pip install . + - name: Run all tests in the cnapy/tests folder + run: pytest -v ./cnapy/tests - build-macos: - runs-on: "macos-latest" + build: + name: build package + runs-on: ubuntu-latest + timeout-minutes: 10 steps: - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: - python-version: '3.10' - - name: Install dependencies + python-version: '3.x' # same as python-publish.yml + - name: Install build tools run: | python -m pip install --upgrade pip - pip install pytest>=7.2 - - name: Install CNApy - run: | - pip install . - - name: Test CNApy - run: | - pytest -v ./cnapy/tests/test.py + pip install build twine + - name: Build sdist and wheel + run: python -m build + - name: Check distribution metadata + run: twine check dist/* diff --git a/cnapy/appdata.py b/cnapy/appdata.py index eeb44fa6..4b3abda0 100644 --- a/cnapy/appdata.py +++ b/cnapy/appdata.py @@ -11,10 +11,13 @@ from math import isclose import appdirs from enum import IntEnum +from copy import deepcopy +from functools import partial import cobra +from cobra.util.context import get_context from optlang.symbolics import Zero -from optlang_enumerator.cobra_cnapy import CNApyModel +from cnapy.cobra_cnapy import CNApyModel from qtpy.QtCore import Qt, Signal, QObject, QStringListModel from qtpy.QtGui import QColor, QFont from qtpy.QtWidgets import QMessageBox @@ -29,8 +32,26 @@ class ModelItemType(IntEnum): class AppData(QObject): ''' The application data ''' + scenarioHistoryChanged = Signal() + + def add_scenario_to_history(self, scenario): + self.current_scenario_index += 1 + # if we branched off after an undo, drop the now-obsolete redo tail + # instead of only overwriting the single slot at current_scenario_index + if self.current_scenario_index < len(self.scenario_history): + del self.scenario_history[self.current_scenario_index:] + self.scenario_history.append(scenario) + self.scenarioHistoryChanged.emit() + + def clear_scenario_history(self): + self.scenario_history.clear() + self.current_scenario_index = -1 + self.scenarioHistoryChanged.emit() + def __init__(self): QObject.__init__(self) + self.scenario_history: list = [] + self.current_scenario_index: int = -1 self.version = "cnapy-1.2.8" self.format_version = 2 self.unsaved = False @@ -40,7 +61,7 @@ def __init__(self): # more scencolors self.scen_color_good = QColor(130, 190, 0) self.scen_color_warn = QColor(255, 200, 0) - self.scen_color_bad = Qt.red + self.scen_color_bad = Qt.GlobalColor.red font = QFont() font.setFamily(font.defaultFamily()) @@ -65,36 +86,48 @@ def __init__(self): "cnapy", roaming=True, appauthor=False), "cnapy-config.txt") self.cobrapy_conf_path = os.path.join(appdirs.user_config_dir( "cnapy", roaming=True, appauthor=False), "cobrapy-config.txt") - self.scenario_past = [] - self.scenario_future = [] + # self.scenario_past = [] + # self.scenario_future = [] self.recent_cna_files = [] self.auto_fba = False self.is_in_dark_mode = False + self.save_model_as_json: bool=False + + def record_current_scenario(self): + self.project.scen_values = deepcopy(self.project.scen_values) + self.add_scenario_to_history(self.project.scen_values) def scen_values_set(self, reaction: str, values: Tuple[float, float]): - if self.project.scen_values.get(reaction, None) != values: # record only real changes - self.project.scen_values[reaction] = values - self.scenario_past.append(("set", reaction, values)) - self.scenario_future.clear() - self.unsaved_scenario_changes() + self.record_current_scenario() + self.project.scen_values[reaction] = values + self.project.scen_values.set_hash_value() + # self.scenario_past.append(("set", reaction, values)) + # self.scenario_future.clear() + self.unsaved_scenario_changes() def scen_values_set_multiple(self, reactions: List[str], values: List[Tuple[float, float]]): + self.record_current_scenario() for r, v in zip(reactions, values): self.project.scen_values[r] = v - self.scenario_past.append(("set", reactions, values)) - self.scenario_future.clear() + self.project.scen_values.set_hash_value() + # self.scenario_past.append(("set", reactions, values)) + # self.scenario_future.clear() self.unsaved_scenario_changes() def scen_values_pop(self, reaction: str): + self.record_current_scenario() self.project.scen_values.pop(reaction, None) - self.scenario_past.append(("pop", reaction, 0)) - self.scenario_future.clear() + self.project.scen_values.set_hash_value() + # self.scenario_past.append(("pop", reaction, 0)) + # self.scenario_future.clear() self.unsaved_scenario_changes() def scen_values_clear(self): + self.record_current_scenario() self.project.scen_values.clear_flux_values() - self.scenario_past.append(("clear", "all", 0)) - self.scenario_future.clear() + self.project.scen_values.set_hash_value() + # self.scenario_past.append(("clear", "all", 0)) + # self.scenario_future.clear() self.unsaved_scenario_changes() def set_comp_value_as_scen_value(self, reaction: str): @@ -103,20 +136,20 @@ def set_comp_value_as_scen_value(self, reaction: str): self.scen_values_set(reaction, val) self.unsaved_scenario_changes() - def recreate_scenario_from_history(self): - self.project.scen_values.clear_flux_values() - for (tag, reaction, values) in self.scenario_past: - if tag == "set": - if isinstance(reaction, list): - for r, v in zip(reaction, values): - self.project.scen_values[r] = v - else: - self.project.scen_values[reaction] = values - elif tag == "pop": - self.project.scen_values.pop(reaction, None) - elif tag == "clear": - self.project.scen_values.clear_flux_values() - self.unsaved_scenario_changes() + # def recreate_scenario_from_history(self): + # self.project.scen_values.clear_flux_values() + # for (tag, reaction, values) in self.scenario_past: + # if tag == "set": + # if isinstance(reaction, list): + # for r, v in zip(reaction, values): + # self.project.scen_values[r] = v + # else: + # self.project.scen_values[reaction] = values + # elif tag == "pop": + # self.project.scen_values.pop(reaction, None) + # elif tag == "clear": + # self.project.scen_values.clear_flux_values() + # self.unsaved_scenario_changes() def format_flux_value(self, flux_value) -> str: return str(round(float(flux_value), self.rounding)).rstrip("0").rstrip(".") @@ -126,9 +159,9 @@ def flux_value_display(self, vl, vu): # -> str, color, bool if isclose(vl, vu, abs_tol=self.abs_tol): if self.modes_coloring: if vl == 0: - background_color = Qt.red + background_color = Qt.GlobalColor.red else: - background_color = Qt.green + background_color = Qt.GlobalColor.green else: background_color = self.comp_color as_one = True @@ -169,6 +202,7 @@ def save_cnapy_config(self): parser.set('cnapy-config', 'results_cache_directory', str(self.results_cache_dir)) parser.set('cnapy-config', 'recent_cna_files', str(self.recent_cna_files)) parser.set('cnapy-config', 'is_in_dark_mode', str(self.is_in_dark_mode)) + parser.set('cnapy-config', 'save_model_as_json', str(self.save_model_as_json)) parser.write(fp) fp.close() @@ -224,119 +258,258 @@ def unsaved_scenario_changes(self): unsavedScenarioChanges = Signal() +import hashlib +import pickle + class Scenario(Dict[str, Tuple[float, float]]): empty_constraint = (None, "", "") - # cannot do this because of the import problem - # @staticmethod - # def format_constraint(constraint): - # return linexprdict2str(constraint[0])+" "+constraint[1]+" "+str(constraint[2]) - def __init__(self): - super().__init__() # this dictionary contains the flux values - self.objective_coefficients: Dict[str, float] = {} # reaction ID, coefficient - self.objective_direction: str = "max" - self.use_scenario_objective: bool = False + super().__init__() # this dictionary contains the flux values (of model and scenario reactions) + self._objective_coefficients: Dict[str, float] = {} # reaction ID, coefficient + self._objective_direction: str = "max" + self._use_scenario_objective: bool = False self.pinned_reactions: Set[str] = set() self.description: str = "" - self.constraints: List[List(Dict, str, float)] = [] # [reaction_id: coefficient dictionary, type, rhs] - self.reactions = {} # reaction_id: (coefficient dictionary, lb, ub), can overwrite existing reactions + self._constraints: List[List[Dict, str, float]] = [] # [reaction_id: coefficient dictionary, type, rhs] + self._reactions = {} # reaction_id: (coefficient dictionary, lb, ub), can overwrite existing reactions self.annotations = [] # List of dicts with: { "id": $reac_id, "key": $key_value, "value": $value_at_key } self.file_name: str = "" self.has_unsaved_changes = False self.version: int = 4 + self.hash_value: int = 0 + self.set_hash_value() + + # --- Property Getters and Setters to auto-update hash value --- + + @property + def objective_coefficients(self) -> Dict[str, float]: + return self._objective_coefficients + + @objective_coefficients.setter + def objective_coefficients(self, value: Dict[str, float]): + self._objective_coefficients = value + self.set_hash_value() + + @property + def objective_direction(self) -> str: + return self._objective_direction + + @objective_direction.setter + def objective_direction(self, value: str): + self._objective_direction = value + self.set_hash_value() + + @property + def use_scenario_objective(self) -> bool: + return self._use_scenario_objective + + @use_scenario_objective.setter + def use_scenario_objective(self, value: bool): + self._use_scenario_objective = value + self.set_hash_value() + + @property + def reactions(self) -> dict: + return self._reactions + + @reactions.setter + def reactions(self, value: dict): + self._reactions = value + self.set_hash_value() + + @property + def constraints(self) -> List: + return self._constraints + + @constraints.setter + def constraints(self, value: List): + self._constraints = value + self.set_hash_value() + + # --- Overridden Dict Methods to detect updates to parent dictionary --- + + def __setitem__(self, key, value): + super().__setitem__(key, value) + self.set_hash_value() + + def __delitem__(self, key): + super().__delitem__(key) + self.set_hash_value() + + def update(self, *args, **kwargs): + super().update(*args, **kwargs) + self.set_hash_value() + + def setdefault(self, key, default=None): + result = super().setdefault(key, default) + self.set_hash_value() + return result + + def pop(self, *args): + result = super().pop(*args) + self.set_hash_value() + return result + + def popitem(self): + result = super().popitem() + self.set_hash_value() + return result + + def clear_flux_values(self): + super().clear() + self.set_hash_value() + + def clear(self): + super().clear() + self.__init__() + + """In-Place Mutability Caveat: If you modify nested mutable structures in-place + (e.g., doing scenario.constraints.append(...), Python's property setters will + not detect the change automatically. Whenever you mutate a list or dict in-place + without explicit assignment, call scenario.set_hash_value() manually. + Also: use the hash value only when values from the annotation do not influence the + calculation, same as for regular reactions hashes.""" + def set_hash_value(self): + """Calculates and stores an integer hash based on participating attributes.""" + + # Sort items or canonicalize structures to ensure deterministic serialization + def _constraint_sort_key(constraint): + expression, constraint_type, rhs = constraint + expression_key = tuple(sorted(expression.items())) if expression else () + return (expression_key, constraint_type, rhs) + serialized_data = ( + sorted(self.items()), + sorted(self._objective_coefficients.items()) if self._objective_coefficients else [], + self._objective_direction, + self._use_scenario_objective, + sorted(self._reactions.items()) if isinstance(self._reactions, dict) else self._reactions, + sorted(self._constraints, key=_constraint_sort_key) + ) + + raw_bytes = pickle.dumps(serialized_data) + # Convert MD5 digest to an integer hash compatible with Python's hash format + self.hash_value = int(hashlib.md5(raw_bytes).hexdigest(), 16) + + def __hash__(self) -> int: + return self.hash_value def save(self, filename: str): - json_dict = {'fluxes': self, 'pinned_reactions': list(self.pinned_reactions), 'description': self.description, - 'objective_direction': self.objective_direction, 'objective_coefficients': self.objective_coefficients, - 'use_scenario_objective': self.use_scenario_objective, 'reactions': self.reactions, - 'constraints': self.constraints, "annotations": self.annotations, 'version': self.version} - with open(filename, 'w') as fp: - json.dump(json_dict, fp) + if filename.endswith('.xlsx'): + from cnapy.scenario_spreadsheet import write_scenario_xlsx + write_scenario_xlsx(self, filename) + else: + json_dict = {'fluxes': self, 'pinned_reactions': list(self.pinned_reactions), 'description': self.description, + 'objective_direction': self.objective_direction, 'objective_coefficients': self.objective_coefficients, + 'use_scenario_objective': self.use_scenario_objective, 'reactions': self.reactions, + 'constraints': self.constraints, "annotations": self.annotations, 'version': self.version} + with open(filename, 'w') as fp: + json.dump(json_dict, fp) self.has_unsaved_changes = False def load(self, filename: str, appdata: AppData, merge=False) -> Tuple[List[str], List, List]: unknown_ids: List(str)= [] incompatible_constraints = [] skipped_scenario_reactions = [] + if filename.endswith('xlsx'): # spreadsheet, parsed before clearing so that the scenario stays intact on errors + from cnapy.scenario_spreadsheet import read_scenario_xlsx + json_dict = read_scenario_xlsx(filename, appdata.project.cobra_py_model.reactions.list_attr("id")) if not merge: self.clear() - with open(filename, 'r') as fp: - if filename.endswith('scen'): # CNApy scenario - self.file_name = filename - json_dict = json.load(fp) - if {'fluxes', 'pinned_reactions', 'description', 'objective_direction', - 'objective_coefficients', 'use_scenario_objective', 'version'}.issubset(json_dict.keys()): - flux_values = json_dict['fluxes'] - for reac_id in json_dict['pinned_reactions']: - if reac_id in appdata.project.cobra_py_model.reactions: - self.pinned_reactions.add(reac_id) - else: - unknown_ids.append(reac_id) - if not merge: - self.description = json_dict['description'] - self.objective_direction = json_dict['objective_direction'] - all_reaction_ids = set(appdata.project.cobra_py_model.reactions.list_attr("id")) - if json_dict['version'] > 1: - self.reactions = json_dict['reactions'] - for reac_id in self.reactions: - if reac_id in all_reaction_ids: - skipped_scenario_reactions.append(reac_id) - for reac_id in skipped_scenario_reactions: - del self.reactions[reac_id] - self.constraints = [] - all_reaction_ids.update(self.reactions) - for constr in json_dict['constraints']: - if constr[0] is None: - self.constraints.append(Scenario.empty_constraint) - elif set(constr[0].keys()).issubset(all_reaction_ids): - self.constraints.append(constr) - else: - incompatible_constraints.append(constr) - if json_dict['version'] >= 3: - self.annotations = json_dict["annotations"] - for reac_id, val in json_dict['objective_coefficients'].items(): - if reac_id in all_reaction_ids: - self.objective_coefficients[reac_id] = val - else: - unknown_ids.append(reac_id) - self.use_scenario_objective = json_dict['use_scenario_objective'] - self.version = 3 - else: - flux_values = json_dict - elif filename.endswith('val'): # CellNetAnalyzer scenario - self.file_name = "" - flux_values = dict() - for line in fp: - line = line.strip() - if len(line) > 0 and not line.startswith("##"): - try: - reac_id, val = line.split() - val = float(val) - flux_values[reac_id] = (val, val) - except Exception: - print("Could not parse line ", line) + if filename.endswith('xlsx'): + self.file_name = filename + flux_values = self._load_json_dict(json_dict, appdata, merge, unknown_ids, + incompatible_constraints, skipped_scenario_reactions) + else: + with open(filename, 'r') as fp: + if filename.endswith('scen'): # CNApy scenario + self.file_name = filename + json_dict = json.load(fp) + if {'fluxes', 'pinned_reactions', 'description', 'objective_direction', + 'objective_coefficients', 'use_scenario_objective', 'version'}.issubset(json_dict.keys()): + flux_values = self._load_json_dict(json_dict, appdata, merge, unknown_ids, + incompatible_constraints, skipped_scenario_reactions) + else: + flux_values = json_dict + elif filename.endswith('val'): # CellNetAnalyzer scenario + self.file_name = "" + flux_values = dict() + for line in fp: + line = line.strip() + if len(line) > 0 and not line.startswith("##"): + try: + reac_id, val = line.split() + val = float(val) + flux_values[reac_id] = (val, val) + except Exception: + print("Could not parse line ", line) reactions = [] scen_values = [] - for reac_id, val in flux_values.items(): - found_reac_id = False - if reac_id in appdata.project.cobra_py_model.reactions: - found_reac_id = True - elif reac_id.startswith("R_"): + model_reactions = appdata.project.cobra_py_model.reactions + for file_reac_id, val in flux_values.items(): + reac_id = file_reac_id + if reac_id not in model_reactions and reac_id not in self.reactions and reac_id.startswith("R_"): reac_id = reac_id[2:] - if reac_id in appdata.project.cobra_py_model.reactions: - found_reac_id = True - if found_reac_id: + # scenario reactions (those loaded above or, when merging, those of the current scenario) can also have a scenario value + if reac_id in model_reactions or reac_id in self.reactions: reactions.append(reac_id) scen_values.append(val) appdata.project.comp_values[reac_id] = (val[0], val[1]) else: - unknown_ids.append(reac_id) + unknown_ids.append(file_reac_id) # report the ID as given in the file appdata.scen_values_set_multiple(reactions, scen_values) return unknown_ids, incompatible_constraints, skipped_scenario_reactions + def _load_json_dict(self, json_dict, appdata: AppData, merge: bool, unknown_ids: List[str], + incompatible_constraints: List, skipped_scenario_reactions: List[str]) -> Dict: + flux_values = json_dict['fluxes'] + for reac_id in json_dict['pinned_reactions']: + if reac_id in appdata.project.cobra_py_model.reactions: + self.pinned_reactions.add(reac_id) + else: + unknown_ids.append(reac_id) + if not merge: + self.description = json_dict['description'] + self.objective_direction = json_dict['objective_direction'] + all_reaction_ids = set(appdata.project.cobra_py_model.reactions.list_attr("id")) + if json_dict['version'] > 1: + self.reactions = json_dict['reactions'] + for reac_id in self.reactions: + if reac_id in all_reaction_ids: + skipped_scenario_reactions.append(reac_id) + for reac_id in skipped_scenario_reactions: + del self.reactions[reac_id] + self.constraints = [] + all_reaction_ids.update(self.reactions) + for constr in json_dict['constraints']: + if constr[0] is None: + self.constraints.append(Scenario.empty_constraint) + elif set(constr[0].keys()).issubset(all_reaction_ids): + self.constraints.append(constr) + else: + incompatible_constraints.append(constr) + if json_dict['version'] >= 3: + self.annotations = json_dict["annotations"] + for reac_id, val in json_dict['objective_coefficients'].items(): + if reac_id in all_reaction_ids: + self.objective_coefficients[reac_id] = val + else: + unknown_ids.append(reac_id) + self.use_scenario_objective = json_dict['use_scenario_objective'] + self.version = 3 + return flux_values + + def reaction_annotation(self, reaction: cobra.Reaction, key: str, default=None): + """Value of an annotation key of a (model or scenario) reaction where the scenario annotations + override the annotation of the reaction itself (later scenario annotations take precedence, + as in ProjectData.load_scenario_into_model).""" + for annotation in reversed(self.annotations): + if annotation.get("reaction_id") == reaction.id and annotation.get("key") == key: + return annotation.get("value", default) + return reaction.annotation.get(key, default) + def add_scenario_reactions_to_model(self, model: cobra.Model): if len(self.reactions) > 0: scenario_metabolites = set() @@ -352,14 +525,8 @@ def add_scenario_reactions_to_model(self, model: cobra.Model): reaction = cobra.Reaction(reac_id, lower_bound=lb, upper_bound=ub) model.add_reactions([reaction]) reaction.add_metabolites(metabolites) - reaction.set_hash_value() - - def clear_flux_values(self): - super().clear() + # reaction.set_hash_value() # now covered by Scenario.hash_value - def clear(self): - super().clear() - self.__init__() class IDList(object): """ @@ -421,26 +588,42 @@ def __init__(self): "it under 'Config->Configure cobrapy').\n"+\ "Right now, GLPK is set as alternative solver instead of Gurobi." ) - msgBox.setIcon(QMessageBox.Warning) + msgBox.setIcon(QMessageBox.Icon.Warning) msgBox.exec() - self.cobra_py_model = CNApyModel() self.reaction_ids: IDList = IDList() # reaction IDs of the cobra_py_model and scenario reactions - - default_map = CnaMap("Map") - self.maps = {"Map": default_map} self.scen_values: Scenario = Scenario() self.clipboard: Dict[str, Tuple[float, float]] = {} - self.solution: cobra.Solution = None self.comp_values: Dict[str, Tuple[float, float]] = {} - self.comp_values_type = 0 # 0: simple flux vector, 1: bounds/FVA result self.fva_values: Dict[str, Tuple[float, float]] = {} # store FVA results persistently self.conc_values: Dict[str, float] = {} # Metabolite concentrations self.df_values: Dict[str, float] = {} # Driving forces + self.reset() + + def reset(self, model: cobra.Model = None): + '''Replaces the model and discards all data that belongs to the previous one. The ProjectData + object itself and its containers (e.g. reaction_ids, scen_values) keep their identity because + dialogs and completers hold references to them.''' + self.cobra_py_model = CNApyModel() if model is None else model + self.maps = {"Map": CnaMap("Map")} + self.scen_values.clear() + self.clipboard.clear() + self.solution: cobra.Solution = None + self.comp_values.clear() + self.comp_values_type = 0 # 0: simple flux vector, 1: bounds/FVA result + self.fva_values.clear() + self.conc_values.clear() + self.df_values.clear() self.modes = [] self.meta_data = {} + self.high = 0 + self.low = 0 + self.sd_solutions = None def load_scenario_into_model(self, model: cobra.Model): + # add the scenario reactions first so that their scenario values are applied below as well + self.scen_values.add_scenario_reactions_to_model(model) + for x in self.scen_values: try: y = model.reactions.get_by_id(x) @@ -448,9 +631,7 @@ def load_scenario_into_model(self, model: cobra.Model): print('reaction', x, 'not found!') else: y.bounds = self.scen_values[x] - y.set_hash_value() - - self.scen_values.add_scenario_reactions_to_model(model) + # y.set_hash_value() # now covered by Scenario.hash_value if self.scen_values.use_scenario_objective: model.objective = model.problem.Objective( @@ -487,14 +668,22 @@ def load_scenario_into_model(self, model: cobra.Model): for (reaction, coeff) in zip(reactions, expression.values()): constr.set_linear_coefficients({reaction.forward_variable: coeff, reaction.reverse_variable: -coeff}) - reaction_ids = [reaction.id for reaction in model.reactions] + # changes of the annotation are not tracked by the model context, therefore they are registered + # explicitly so that the scenario annotations do not remain in the model after leaving the context + context = get_context(model) for annotation in self.scen_values.annotations: if "reaction_id" not in annotation.keys(): continue - if annotation["reaction_id"] not in reaction_ids: + if annotation["reaction_id"] not in model.reactions: continue reaction: cobra.Reaction = model.reactions.get_by_id(annotation["reaction_id"]) - reaction.annotation[annotation["key"]] = annotation["value"] + key = annotation["key"] + if context is not None: + if key in reaction.annotation: + context(partial(reaction.annotation.__setitem__, key, reaction.annotation[key])) + else: + context(partial(reaction.annotation.pop, key, None)) + reaction.annotation[key] = annotation["value"] def collect_default_scenario_values(self) -> Tuple[List[str], List[Tuple[float, float]]]: reactions = [] @@ -512,6 +701,10 @@ def update_reaction_id_lists(self): # def scenario_hash_value(self): # return hashlib.md5(pickle.dumps(sorted(self.scen_values.items()))).digest() +# values of the "view" entry of a project map +MAP_VIEW_CNAPY = "cnapy" +MAP_VIEW_ESCHER = "escher" + def CnaMap(name): background_svg_path: str = resources.files("cnapy") / "data" / "default-bg.svg" with resources.as_file(background_svg_path) as svg_file_path: @@ -523,7 +716,7 @@ def CnaMap(name): "zoom": 0, "pos": (0, 0), "boxes": {}, - "view": "cnapy", # either "cnapy" or "escher" + "view": MAP_VIEW_CNAPY, # either MAP_VIEW_CNAPY or MAP_VIEW_ESCHER "escher_map_data": "" # JSON string } diff --git a/cnapy/application.py b/cnapy/application.py index 025390c5..4a65d09d 100644 --- a/cnapy/application.py +++ b/cnapy/application.py @@ -23,20 +23,22 @@ from pathlib import Path import cobra +import cnapy.optlang_highs_interface from qtpy.QtCore import Qt, QLocale from qtpy.QtGui import QColor, QPalette from qtpy.QtWidgets import QApplication from qtpy.QtWidgets import QMessageBox # Necessary on some systems so that, e.g., the in/put metabolite flux visualization in the ipython console can run -import nest_asyncio -nest_asyncio.apply() +import nest_asyncio2 +nest_asyncio2.apply() -# ensuring compatibility with high resolution displays -if hasattr(Qt, 'AA_EnableHighDpiScaling'): - QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True) -if hasattr(Qt, 'AA_UseHighDpiPixmaps'): - QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True) +# Ensuring compatibility with high resolution displays. These attributes are +# Qt 5-only, so look them up through ApplicationAttribute for Qt 6 safety. +for attribute_name in ("AA_EnableHighDpiScaling", "AA_UseHighDpiPixmaps"): + attribute = getattr(Qt.ApplicationAttribute, attribute_name, None) + if attribute is not None: + QApplication.setAttribute(attribute, True) from cnapy.appdata import AppData from cnapy.gui_elements.main_window import MainWindow @@ -82,15 +84,15 @@ def make_dark_palette() -> QPalette: palette.setColor(QPalette.ColorRole.Button, QColor(66, 66, 66)) # medium‑dark gray # ---- Text colours ---- - palette.setColor(QPalette.ColorRole.WindowText, Qt.white) # normal text - palette.setColor(QPalette.ColorRole.ButtonText, Qt.white) # button labels - palette.setColor(QPalette.ColorRole.Text, Qt.white) # line‑edit / plain‑text + palette.setColor(QPalette.ColorRole.WindowText, Qt.GlobalColor.white) # normal text + palette.setColor(QPalette.ColorRole.ButtonText, Qt.GlobalColor.white) # button labels + palette.setColor(QPalette.ColorRole.Text, Qt.GlobalColor.white) # line‑edit / plain‑text palette.setColor(QPalette.ColorRole.PlaceholderText, QColor(150, 150, 150)) # a softer gray for placeholders # ---- Highlight (selection) colours ---- palette.setColor(QPalette.ColorRole.Highlight, QColor(0, 120, 215)) - palette.setColor(QPalette.ColorRole.HighlightedText, Qt.black) # black text on the blue highlight + palette.setColor(QPalette.ColorRole.HighlightedText, Qt.GlobalColor.black) # black text on the blue highlight # ---- Disabled state (optional) ---- disabled = QColor(120, 120, 120) # lighter than the normal text gray so it is still visible @@ -99,7 +101,7 @@ def make_dark_palette() -> QPalette: # ---- Other states ---- palette.setColor(QPalette.ColorRole.ToolTipBase, QColor(255, 255, 220)) - palette.setColor(QPalette.ColorRole.ToolTipText, Qt.black) + palette.setColor(QPalette.ColorRole.ToolTipText, Qt.GlobalColor.black) palette.setColor(QPalette.ColorRole.Link, QColor(86, 156, 214)) # a light‑blue link colour palette.setColor(QPalette.ColorRole.LinkVisited, QColor(150, 150, 255)) @@ -113,26 +115,26 @@ def make_light_palette(): # ---- Base colours (backgrounds) ---- # Window background (main window, dialogs, etc.) - palette.setColor(QPalette.Window, QColor(240, 240, 240)) # light gray + palette.setColor(QPalette.ColorRole.Window, QColor(240, 240, 240)) # light gray # General widget background (e.g. QLineEdit, QTextEdit) - palette.setColor(QPalette.Base, QColor(255, 255, 255)) # white + palette.setColor(QPalette.ColorRole.Base, QColor(255, 255, 255)) # white # Button background - palette.setColor(QPalette.Button, QColor(230, 230, 230)) + palette.setColor(QPalette.ColorRole.Button, QColor(230, 230, 230)) # ---- Text colours ---- - palette.setColor(QPalette.WindowText, Qt.black) - palette.setColor(QPalette.ButtonText, Qt.black) - palette.setColor(QPalette.Text, Qt.black) - palette.setColor(QPalette.PlaceholderText, QColor(120, 120, 120)) + palette.setColor(QPalette.ColorRole.WindowText, Qt.GlobalColor.black) + palette.setColor(QPalette.ColorRole.ButtonText, Qt.GlobalColor.black) + palette.setColor(QPalette.ColorRole.Text, Qt.GlobalColor.black) + palette.setColor(QPalette.ColorRole.PlaceholderText, QColor(120, 120, 120)) # ---- Highlight (selection) colours ---- - palette.setColor(QPalette.Highlight, QColor(0, 120, 215)) # classic Windows blue - palette.setColor(QPalette.HighlightedText, Qt.white) + palette.setColor(QPalette.ColorRole.Highlight, QColor(0, 120, 215)) # classic Windows blue + palette.setColor(QPalette.ColorRole.HighlightedText, Qt.GlobalColor.white) # ---- Disabled state (optional) ---- disabled = QColor(150, 150, 150) - palette.setColor(QPalette.Disabled, QPalette.Text, disabled) - palette.setColor(QPalette.Disabled, QPalette.ButtonText, disabled) + palette.setColor(QPalette.ColorGroup.Disabled, QPalette.ColorRole.Text, disabled) + palette.setColor(QPalette.ColorGroup.Disabled, QPalette.ColorRole.ButtonText, disabled) return palette @@ -182,7 +184,10 @@ def __init__( self.qapp.aboutToQuit.connect( self.window.centralWidget().shutdown_kernel ) - sys.exit(self.qapp.exec_()) + exit_code = self.qapp.exec() + # tear down QtWebEngine while the QApplication still exists + self.window.release_web_engine() + sys.exit(exit_code) def first_start_up_message(self): msgBox = QMessageBox() @@ -194,7 +199,7 @@ def first_start_up_message(self): "Also, should CNApy's font size be too small or too large, you can change\n" "it under 'Config->Configure CNApy...'." ) - msgBox.setIcon(QMessageBox.Information) + msgBox.setIcon(QMessageBox.Icon.Information) msgBox.exec() self.window.show_config_dialog(first_start=True) @@ -299,6 +304,8 @@ def read_config(self): except (KeyError, NoOptionError): print("Could not find is_in_dark_mode in cnapy-config.txt") + self.appdata.save_model_as_json = config_parser.getboolean('cnapy-config', + 'save_model_as_json', fallback=self.appdata.save_model_as_json) self.appdata.use_results_cache = config_parser.getboolean('cnapy-config', 'use_results_cache', fallback=self.appdata.use_results_cache) self.appdata.results_cache_dir = Path(config_parser.get('cnapy-config', @@ -316,7 +323,11 @@ def read_cobrapy_config(self): print("No cobrapy-config.txt file found, using COBRApy base settings.") return try: - cobra.Configuration().solver = config_parser.get('cobrapy-config', 'solver') + solver_name = config_parser.get('cobrapy-config', 'solver') + if solver_name == "highs": + cobra.Configuration().solver = cnapy.optlang_highs_interface + else: + cobra.Configuration().solver = solver_name except Exception as e: print("Cannot set solver from cobrapy-config.txt file because:", e, "\nReverting solver to COBRApy base setting.") diff --git a/cnapy/benchmark_end_to_end.py b/cnapy/benchmark_end_to_end.py new file mode 100644 index 00000000..c5de6547 --- /dev/null +++ b/cnapy/benchmark_end_to_end.py @@ -0,0 +1,179 @@ +#!/usr/bin/env python3 +"""benchmark_end_to_end.py + +End-to-end timing of lptools_highs.plot_flux_space_highs against +straindesign's lptools.plot_flux_space, the way the PlotSpaceDialog calls +them (axes as strings / (num, den) tuples, points=20 by default), with +no plot window: matplotlib runs on the Agg backend and show=False, so the +time measured is computation + figure building, not time until a window +is closed. + +For HiGHS three numbers are reported per case: + data plot_flux_space_data_highs only (LP work + meshing) + full plot_flux_space_highs with show=False (data + matplotlib) +and, per --workers value, the data time with that thread count. +straindesign is timed with plot_flux_space(show=False). + +A cheap correctness cross-check is printed as well: the bounding box +of each result's datapoints (max relative difference of the 6 or 4 +extents, relative to the axis range). Mesh/facet differences are +expected; extent differences beyond ~1e-3 are worth a look. + +Usage: + python benchmark_end_to_end.py ECC2anaerob.sbml + python benchmark_end_to_end.py ECC2anaerob.sbml --points 20 30 --repeats 5 --workers 1 2 4 8 + python benchmark_end_to_end.py textbook --anaerobic # cobra's bundled E. coli core + python benchmark_end_to_end.py model.xml --cases 3d_rry 3d_ryy --skip-straindesign + +Default reaction IDs are the ECC2 ones; for the bundled 'textbook' +model the E. coli core IDs are substituted automatically. Override with +--bm/--p1/--p2/--sub. +""" + +import argparse +import gc +import statistics +import time + +import matplotlib +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 + + +ECC2_IDS = dict(bm="Ec_biomass_iJO1366_core_53p95M", p1="EX_etoh_e", p2="EX_succ_e", sub="EX_glc_e") +TEXTBOOK_IDS = dict(bm="Biomass_Ecoli_core", p1="EX_etoh_e", p2="EX_succ_e", sub="EX_glc__D_e") + + +def make_cases(bm, p1, p2, sub): + r = lambda x: f"1.0 {x}" + y = lambda x: (f"1.0 {x}", f"-1.0 {sub}") + return { + "2d_rr": [r(bm), r(p1)], + "2d_ry": [r(bm), y(p1)], + "2d_yy": [y(p1), y(p2)], + "3d_rrr": [r(bm), r(p1), r(p2)], + "3d_rry": [r(bm), r(p1), y(p2)], + "3d_ryy": [r(bm), y(p1), y(p2)], + } + + +def load_model(path, anaerobic): + import cobra + if path == "textbook": + from cobra.io import load_model as lm + m = lm("textbook") + elif path.endswith((".xml", ".sbml")): + m = cobra.io.read_sbml_model(path) + elif path.endswith(".json"): + m = cobra.io.load_json_model(path) + elif path.endswith(".mat"): + m = cobra.io.load_matlab_model(path) + else: + raise ValueError(f"don't know how to load {path!r}") + if anaerobic: + m.reactions.get_by_id("EX_o2_e").lower_bound = 0.0 + return m + + +def timed(fn, repeats): + """Median wall time of `repeats` calls (after one untimed warm-up + call, so imports / first-use costs don't land in the first case), + plus the last result.""" + fn() + plt.close("all") + times, res = [], None + for _ in range(repeats): + gc.collect() + t = time.perf_counter() + res = fn() + times.append(time.perf_counter() - t) + plt.close("all") + return statistics.median(times), res + + +def bbox(points): + pts = [list(map(float, p)) for p in points] + if not pts: + return None + n = len(pts[0]) + return [(min(p[k] for p in pts), max(p[k] for p in pts)) for k in range(n)] + + +def bbox_diff(a, b): + if a is None or b is None or len(a) != len(b): + return float("nan") + worst = 0.0 + for (alo, ahi), (blo, bhi) in zip(a, b): + rng = max(abs(ahi - alo), abs(bhi - blo), 1e-9) + worst = max(worst, abs(alo - blo) / rng, abs(ahi - bhi) / rng) + return worst + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("model", help="model file, or 'textbook' for cobra's bundled E. coli core") + ap.add_argument("--cases", nargs="+", default=None, help="subset of: 2d_rr 2d_ry 2d_yy 3d_rrr 3d_rry 3d_ryy") + ap.add_argument("--points", type=int, nargs="+", default=[20], help="resolution(s); dialog default is 20") + ap.add_argument("--repeats", type=int, default=3) + ap.add_argument("--workers", type=int, nargs="+", default=[1, 4], + help="n_workers values for the HiGHS data timing, 0 means 'auto'") + ap.add_argument("--constraints", default="", help="passed to both, straindesign syntax") + ap.add_argument("--solver", default=None, help="straindesign solver (glpk, cplex, gurobi, scip)") + ap.add_argument("--skip-straindesign", action="store_true") + ap.add_argument("--anaerobic", action="store_true", help="set EX_o2_e lower bound to 0") + for k in ("bm", "p1", "p2", "sub"): + ap.add_argument(f"--{k}", default=None) + args = ap.parse_args() + + ids = dict(TEXTBOOK_IDS if args.model == "textbook" else ECC2_IDS) + ids.update({k: getattr(args, k) for k in ids if getattr(args, k)}) + model = load_model(args.model, args.anaerobic) + missing = [v for v in ids.values() if v not in model.reactions] + if missing: + raise SystemExit(f"reaction(s) not in model: {missing} -- use --bm/--p1/--p2/--sub") + + from lptools_highs import plot_flux_space_data_highs, plot_flux_space_highs + if not args.skip_straindesign: + from straindesign.lptools import plot_flux_space + + cases = make_cases(**ids) + names = args.cases or list(cases) + extra = {"constraints": args.constraints} if args.constraints else {} + sd_extra = dict(extra, **({"solver": args.solver} if args.solver else {})) + + args.workers = [w if w > 0 else "auto" for w in args.workers] + wcols = "".join(f" data w={w:<3}" if w != "auto" else " data auto " for w in args.workers) + print(f"model={args.model} repeats={args.repeats} (median) times in s") + print(f"{'case':8} {'pts':>4}{wcols} {'full':>8} {'straindesign':>12} {'speedup':>7} {'bbox Δ':>8}") + for pts in args.points: + for name in names: + axes = cases[name] + row = f"{name:8} {pts:>4}" + try: + d = None + for w in args.workers: + t, d = timed(lambda: plot_flux_space_data_highs(model, axes, points=pts, n_workers=w, **extra), + args.repeats) + row += f" {t:11.3f}" + t_full, _ = timed(lambda: plot_flux_space_highs(model, axes, points=pts, show=False, + n_workers=args.workers[0], **extra), args.repeats) + row += f" {t_full:8.3f}" + hb = bbox(d["datapoints"]) + except Exception as e: # report and keep going + print(row + f" HiGHS FAILED: {e!r}") + continue + if args.skip_straindesign: + print(row) + continue + try: + t_sd, res = timed(lambda: plot_flux_space(model, axes, points=pts, show=False, **sd_extra), + args.repeats) + diff = bbox_diff(hb, bbox(res[0])) + row += f" {t_sd:12.3f} {t_sd / t_full:6.1f}x {diff:8.1e}" + except Exception as e: + row += f" straindesign FAILED: {e!r}" + print(row, flush=True) + + +if __name__ == "__main__": + main() diff --git a/cnapy/benchmark_plot_trace.py b/cnapy/benchmark_plot_trace.py new file mode 100644 index 00000000..5c1fcfe0 --- /dev/null +++ b/cnapy/benchmark_plot_trace.py @@ -0,0 +1,393 @@ +#!/usr/bin/env python3 +"""benchmark_plot_trace.py (prototype) + +Benchmarking/correctness-cross-check driver for the individual tracer +functions in plot_trace_highs.py (component-level testing against a real +cobra model), as distinct from lptools_highs.py, which is the actual +integration layer meant to replace plot_flux_space. This script builds +each HighsLp once, times sequential vs. threaded tracing, and (with +--compare-straindesign) cross-checks results against lptools.py's +existing straindesign-based tracers as a correctness sanity check +independent of the HiGHS reimplementation. + +straindesign is used here only for parsing the text axis/constraint +expressions (parse_linexpr / parse_constraints), exactly as +plot_space_dialog.py / lptools.py already do -- not for solving. + +Usage: + python benchmark_plot_trace.py model.xml "1.0 EX_glc__D_e" "1.0 BIOMASS_Ecoli_core_w_GAM" + python benchmark_plot_trace.py model.xml "EX_ac_e" "BIOMASS_Ecoli_core_w_GAM" \\ + --constraints "EX_o2_e=0" --workers 2 4 8 16 --repeats 7 --compare-straindesign + python benchmark_plot_trace.py model.xml "1.0 EX_ac_e" "1.0 EX_etoh_e" \\ + --zaxis "1.0 BIOMASS_..." --zmin 0 --zmax 0.5 --slices 25 \\ + --yield-num "1.0 EX_etoh_e" --yield-den "-1.0 EX_glc__D_e" \\ + --boundary --compare-straindesign + +model can be .xml/.sbml, .json, or .mat (anything cobra.io can load). +Axis expressions and --constraints use straindesign's own syntax (same +as typed into plot_space_dialog.py's numerator/denominator fields). + +--zaxis/--zmin/--zmax/--slices benchmarks the 3D one-sliced-axis tracer; +--yield-num/--yield-den benchmarks yield min/max; --boundary (needs +--yield-num/--yield-den too) benchmarks the adaptive 2D boundary tracer +for the rate/yield mixed-axis case, using ax0 as x and the yield as y. +""" + +import argparse +import time + + +def load_model(path): + import cobra + if path.endswith(".xml") or path.endswith(".sbml"): + return cobra.io.read_sbml_model(path) + elif path.endswith(".json"): + return cobra.io.load_json_model(path) + elif path.endswith(".mat"): + return cobra.io.load_matlab_model(path) + else: + raise ValueError(f"Don't know how to load {path!r} -- expected .xml/.sbml, .json, or .mat") + + +def parse_axis(expr, reaction_ids): + from straindesign import parse_linexpr + d = parse_linexpr(expr, reaction_ids) + return d[0] + + +def parse_constraints_arg(constraints_str, reaction_ids): + if not constraints_str: + return [] + from straindesign import parse_constraints + return parse_constraints(constraints_str, reaction_ids) + + +def axis_label(d): + from straindesign.parse_constr import linexprdict2str + return linexprdict2str(d) + + +def median(values): + s = sorted(values) + return s[len(s) // 2] + + +def straindesign_reference(model, ax0_str, ax1_str, constraints_str): + """Run lptools.py's existing sequential straindesign-based tracer on + the same axes/constraints, for a correctness cross-check independent + of the HiGHS reimplementation. Returns (vertices, elapsed_seconds), + or None if lptools/straindesign aren't usable here (missing/ + unlicensed solver, import error, etc.) -- reported, not raised, since + the HiGHS-side benchmark is still useful without this cross-check.""" + try: + from straindesign import lptools + reaction_ids = model.reactions.list_attr("id") + ax0 = [parse_axis(ax0_str, reaction_ids)] + ax1 = [parse_axis(ax1_str, reaction_ids)] + constraints = parse_constraints_arg(constraints_str, reaction_ids) + solver = lptools.select_solver(None, model) + t0 = time.perf_counter() + vertices = lptools._trace_polygon_rate_rate(model, [ax0, ax1], constraints, solver) + elapsed = time.perf_counter() - t0 + return vertices, elapsed + except Exception as exc: # pragma: no cover -- environment-dependent + print(f"[straindesign cross-check skipped: {exc!r}]") + return None + + +def main(): + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("model", help="path to a cobra-loadable model (.xml/.sbml, .json, .mat)") + parser.add_argument("ax0", help="x-axis linear expression, e.g. '1.0 EX_glc__D_e'") + parser.add_argument("ax1", help="y-axis linear expression, e.g. '1.0 BIOMASS_Ecoli_core_w_GAM'") + parser.add_argument("--constraints", default="", + help="extra constraints, straindesign syntax, e.g. 'EX_o2_e=0'") + parser.add_argument("--workers", type=int, nargs="+", default=[2, 4, 8], + help="thread counts to benchmark (default: 2 4 8)") + parser.add_argument("--repeats", type=int, default=5, + help="repeats per timing, median reported (default: 5)") + parser.add_argument("--compare-straindesign", action="store_true", + help="also run lptools.py's existing straindesign-based tracer, " + "for a correctness cross-check") + parser.add_argument("--zaxis", default=None, + help="third (sliced) axis expression, e.g. 'EX_etoh_e' -- if given, " + "also benchmarks the 3D one-sliced-axis tracer (independent " + "slices in a thread pool) instead of just the 2D polygon") + parser.add_argument("--zmin", type=float, default=None, + help="lower end of the sliced axis range (default: that reaction's " + "own lower_bound, if --zaxis names a single reaction)") + parser.add_argument("--zmax", type=float, default=None, + help="upper end of the sliced axis range (default: that reaction's " + "own upper_bound, if --zaxis names a single reaction)") + parser.add_argument("--slices", type=int, default=25, + help="number of slices along the z-axis (default: 25)") + parser.add_argument("--yield-num", default=None, + help="yield numerator expression, e.g. '1.0 EX_etoh_e' -- if given " + "together with --yield-den, benchmarks/cross-checks " + "HighsYieldSolver's max and min yield against lptools.yopt()") + parser.add_argument("--yield-den", default=None, + help="yield denominator expression, e.g. '-1.0 EX_glc__D_e'") + parser.add_argument("--boundary", action="store_true", + help="also benchmark/cross-check trace_boundary_adaptive, using ax0 " + "(positional, as a rate axis) for x and --yield-num/--yield-den " + "for y -- the rate/yield mixed-axis case") + args = parser.parse_args() + + print(f"loading {args.model} ...") + model = load_model(args.model) + print(f"{len(model.reactions)} reactions, {len(model.metabolites)} metabolites") + + from highs_lp_cache import build_rate_lp, HighsRateSolver + from plot_trace_highs import trace_polygon_rate_rate, trace_polygon_rate_rate_parallel, polygon_area + + reaction_ids = model.reactions.list_attr("id") + ax0 = parse_axis(args.ax0, reaction_ids) + ax1 = parse_axis(args.ax1, reaction_ids) + constraints = parse_constraints_arg(args.constraints, reaction_ids) + print(f"x-axis: {axis_label(ax0)}") + print(f"y-axis: {axis_label(ax1)}") + if constraints: + print(f"constraints: {args.constraints}") + + t0 = time.perf_counter() + lp, rxn_ids, rxn_index, fix_slot_rows = build_rate_lp(model, constraints=constraints) + t_build = time.perf_counter() - t0 + print(f"\nbuild_rate_lp: {t_build * 1000:.1f}ms (built once, shared by every trace below)") + + seq_times = [] + seq_vertices = None + for _ in range(args.repeats): + solver = HighsRateSolver(lp, rxn_ids, rxn_index, fix_slot_rows) + t0 = time.perf_counter() + seq_vertices = trace_polygon_rate_rate(solver, ax0, ax1) + seq_times.append(time.perf_counter() - t0) + seq_median = median(seq_times) + seq_area = polygon_area(seq_vertices) + print(f"\nsequential: {len(seq_vertices):3d} vertices, area={seq_area:.6g}, " + f"median {seq_median * 1000:.2f}ms over {args.repeats} runs " + f"(min {min(seq_times) * 1000:.2f}ms, max {max(seq_times) * 1000:.2f}ms)") + + def area_ok(a, b): + return abs(a - b) < 1e-4 * max(1.0, abs(b)) + + for n_workers in args.workers: + par_times = [] + par_vertices = None + for _ in range(args.repeats): + t0 = time.perf_counter() + par_vertices = trace_polygon_rate_rate_parallel( + lp, rxn_ids, rxn_index, ax0, ax1, n_workers=n_workers) + par_times.append(time.perf_counter() - t0) + par_median = median(par_times) + par_area = polygon_area(par_vertices) + speedup = seq_median / par_median if par_median > 0 else float("nan") + tag = "OK" if area_ok(par_area, seq_area) else "MISMATCH" + print(f"{n_workers:>2} threads: {len(par_vertices):3d} vertices, area={par_area:.6g} [{tag}], " + f"median {par_median * 1000:.2f}ms, speedup vs sequential: {speedup:.2f}x") + + if args.compare_straindesign: + print("\ncross-checking against lptools.py's straindesign-based tracer ...") + ref = straindesign_reference(model, args.ax0, args.ax1, args.constraints) + if ref is not None: + ref_vertices, ref_elapsed = ref + ref_area = polygon_area(ref_vertices) + tag = "OK" if area_ok(ref_area, seq_area) else "MISMATCH" + print(f"straindesign: {len(ref_vertices):3d} vertices, area={ref_area:.6g} " + f"[{tag} vs highs], {ref_elapsed * 1000:.2f}ms " + f"({ref_elapsed / seq_median:.1f}x the HiGHS sequential time)") + + if args.zaxis: + print(f"\n--- 3D one-sliced-axis benchmark: z = {args.zaxis!r} ---") + from plot_trace_highs import (trace_3d_slice_polygon, trace_3d_slice_polygon_sequential, + fix_rate_axis) + + z_coeffs = parse_axis(args.zaxis, reaction_ids) + zmin, zmax = args.zmin, args.zmax + if zmin is None or zmax is None: + if len(z_coeffs) == 1: + (only_rid, only_coeff), = z_coeffs.items() + rxn = model.reactions.get_by_id(only_rid) + zmin = zmin if zmin is not None else min(only_coeff * rxn.lower_bound, only_coeff * rxn.upper_bound) + zmax = zmax if zmax is not None else max(only_coeff * rxn.lower_bound, only_coeff * rxn.upper_bound) + else: + raise SystemExit("--zaxis is a multi-reaction expression -- pass --zmin/--zmax explicitly") + print(f"z range: [{zmin:.6g}, {zmax:.6g}], {args.slices} slices") + + z_values = [zmin + (zmax - zmin) * i / (args.slices - 1) for i in range(args.slices)] + + lp3, rxn_ids3, rxn_index3, fix_slot_rows3 = build_rate_lp( + model, constraints=constraints, num_fix_slots=1) + make_fix = fix_rate_axis(z_coeffs) + + t0 = time.perf_counter() + seq_slices = trace_3d_slice_polygon_sequential( + lp3, rxn_ids3, rxn_index3, fix_slot_rows3, make_fix, ax0, ax1, z_values) + t_seq3 = time.perf_counter() - t0 + total_vertices = sum(len(p) for _, p in seq_slices) + print(f"sequential (1 solver, warm-started across slices): {t_seq3 * 1000:.1f}ms total, " + f"{total_vertices} vertices across {args.slices} slices " + f"({t_seq3 / args.slices * 1000:.2f}ms/slice avg)") + + for n_workers in args.workers: + t0 = time.perf_counter() + par_slices = trace_3d_slice_polygon( + lp3, rxn_ids3, rxn_index3, fix_slot_rows3, make_fix, ax0, ax1, z_values, + n_workers=n_workers) + t_par3 = time.perf_counter() - t0 + areas_match = all( + area_ok(polygon_area(p1), polygon_area(p2)) + for (_, p1), (_, p2) in zip(seq_slices, par_slices)) + tag = "OK" if areas_match else "MISMATCH" + speedup = t_seq3 / t_par3 if t_par3 > 0 else float("nan") + print(f"{n_workers:>2} threads: {t_par3 * 1000:.1f}ms total [{tag} vs sequential], " + f"speedup: {speedup:.2f}x") + + if args.yield_num and args.yield_den: + print(f"\n--- yield benchmark: ({args.yield_num}) / ({args.yield_den}) ---") + from highs_lp_cache import build_yopt_lp, HighsYieldSolver + + num_coeffs = parse_axis(args.yield_num, reaction_ids) + den_coeffs = parse_axis(args.yield_den, reaction_ids) + + t0 = time.perf_counter() + lp_y, rxn_ids_y, rxn_index_y, den_row, t_col, fix_rows_y = build_yopt_lp( + model, den_coeffs, constraints=constraints) + t_build_y = time.perf_counter() - t0 + print(f"build_yopt_lp: {t_build_y * 1000:.1f}ms") + + # which sign(s) can the denominator actually take? same check as + # core.highs_yopt, done here on the plain rate LP already built above. + den_tol = 1e-9 + rate_solver = HighsRateSolver(lp, rxn_ids, rxn_index, fix_slot_rows) + status_min, min_den, _ = rate_solver.optimize(den_coeffs, "minimize") + status_max, max_den, _ = rate_solver.optimize(den_coeffs, "maximize") + achievable_signs = [] + if status_min == "unbounded" or (status_min == "optimal" and min_den < -den_tol): + achievable_signs.append(-1.0) + if status_max == "unbounded" or (status_max == "optimal" and max_den > den_tol): + achievable_signs.append(1.0) + + if not achievable_signs: + print("denominator is forced to 0 everywhere on the feasible region -- yield undefined") + else: + solver_y = HighsYieldSolver(lp_y, rxn_ids_y, rxn_index_y, den_row, t_col, fix_rows_y) + highs_results = {} + for sense in ("maximize", "minimize"): + best = None + t0 = time.perf_counter() + for sign in achievable_signs: + status, value, fluxes, scalable = solver_y.optimize(num_coeffs, sense, sign) + if status == "optimal" and ( + best is None or + (sense == "maximize" and value > best[0]) or + (sense == "minimize" and value < best[0])): + best = (value, status) + dt = time.perf_counter() - t0 + value, status = best if best else (float("nan"), "infeasible") + highs_results[sense] = value + print(f"HiGHS yopt {sense}: {value:.6g} [{status}] ({dt * 1000:.2f}ms)") + + if args.compare_straindesign: + try: + from straindesign import lptools + solver_name = lptools.select_solver(None, model) + for sense in ("maximize", "minimize"): + t0 = time.perf_counter() + sol = lptools.yopt(model, obj_num=num_coeffs, obj_den=den_coeffs, + constraints=constraints, solver=solver_name, obj_sense=sense) + dt = time.perf_counter() - t0 + tag = "OK" if area_ok(sol.objective_value, highs_results[sense]) else "MISMATCH" + print(f"straindesign yopt {sense}: {sol.objective_value:.6g} [{sol.status}, " + f"{tag} vs highs] ({dt * 1000:.2f}ms)") + except Exception as exc: + print(f"[straindesign yield cross-check skipped: {exc!r}]") + + if args.boundary: + if not (args.yield_num and args.yield_den): + raise SystemExit("--boundary needs --yield-num and --yield-den too") + print(f"\n--- adaptive boundary benchmark: x = ({args.ax0}) [rate], " + f"y = ({args.yield_num})/({args.yield_den}) [yield] ---") + from highs_lp_cache import build_rate_lp, build_yopt_lp, HighsRateSolver, HighsYieldSolver + from plot_trace_highs import RateAxis, YieldAxis, trace_boundary_adaptive + + num_coeffs = parse_axis(args.yield_num, reaction_ids) + den_coeffs = parse_axis(args.yield_den, reaction_ids) + + rate_lp_b, rxn_ids_b, rxn_index_b, fix_rows_b = build_rate_lp( + model, constraints=constraints, num_fix_slots=1) + rate_solver_b = HighsRateSolver(rate_lp_b, rxn_ids_b, rxn_index_b, fix_rows_b) + + yield_lp_b, _, _, den_row_b, t_col_b, yfix_rows_b = build_yopt_lp( + model, den_coeffs, constraints=constraints, num_fix_slots=1) + yield_solver_b = HighsYieldSolver(yield_lp_b, rxn_ids_b, rxn_index_b, den_row_b, t_col_b, yfix_rows_b) + + ax0_b = RateAxis(rate_solver_b, ax0) + ax1_b = YieldAxis(rate_solver_b, yield_solver_b, num_coeffs, den_coeffs) + + b_times = [] + upper = lower = None + for _ in range(args.repeats): + t0 = time.perf_counter() + upper, lower = trace_boundary_adaptive(rate_solver_b, ax0_b, ax1_b) + b_times.append(time.perf_counter() - t0) + b_median = median(b_times) + print(f"HiGHS: {len(upper)} upper + {len(lower)} lower points, " + f"median {b_median * 1000:.2f}ms over {args.repeats} runs") + + if args.compare_straindesign: + try: + from straindesign import lptools + solver_name = lptools.select_solver(None, model) + axes_sd = [[ax0], [num_coeffs, den_coeffs]] + t0 = time.perf_counter() + ref_upper, ref_lower = lptools._trace_boundary_adaptive( + model, axes_sd, ['rate', 'yield'], constraints, solver_name) + t_sd = time.perf_counter() - t0 + + def interp(boundary, x): + pts = sorted(boundary) + if not pts: + return None + if x <= pts[0][0]: + return pts[0][1] + if x >= pts[-1][0]: + return pts[-1][1] + for (x0, y0), (x1, y1) in zip(pts, pts[1:]): + if x0 <= x <= x1: + if x1 == x0: + return y0 + return y0 + (y1 - y0) * (x - x0) / (x1 - x0) + return None + + # Direction 1: HiGHS's own x's against straindesign's curve. + # This direction ALONE proves nothing about under-refinement: + # with only 2 points (its own endpoints), a boundary passes + # this check by construction even if it missed real + # curvature everywhere in between -- it only confirms the + # endpoints agree. + err_1 = max((abs(y - interp(ref_upper, x)) for x, y in upper + if interp(ref_upper, x) is not None), default=0.0) + + # Direction 2 (the one that actually matters): straindesign's + # x's against HiGHS's curve. If HiGHS under-refined (too few + # points to represent real curvature), THIS is where a sparse + # boundary shows a large error -- straindesign's interior + # points land far from HiGHS's straight-line interpolation + # between its own two widely-spaced points. + err_2 = max((abs(y - interp(upper, x)) for x, y in ref_upper + if interp(upper, x) is not None), default=0.0) + + tag = "OK" if max(err_1, err_2) < 1e-3 else "MISMATCH (or HiGHS under-refined)" + print(f"straindesign: {len(ref_upper)} upper + {len(ref_lower)} lower points, " + f"{t_sd * 1000:.2f}ms ({t_sd / b_median:.1f}x the HiGHS time)") + print(f" HiGHS points checked against straindesign's curve: max error {err_1:.3g}") + print(f" straindesign points checked against HiGHS's curve: max error {err_2:.3g} <-- " + f"this is the one that catches under-refinement") + print(f" [{tag}]") + except Exception as exc: + print(f"[straindesign boundary cross-check skipped: {exc!r}]") + + +if __name__ == "__main__": + main() diff --git a/cnapy/cobra_cnapy.py b/cnapy/cobra_cnapy.py new file mode 100644 index 00000000..05c431f7 --- /dev/null +++ b/cnapy/cobra_cnapy.py @@ -0,0 +1,45 @@ +""" +Extensions of cobra classes used in CNApy +""" +import hashlib +import pickle +import cobra + + +def stoichiometry_hash(model: cobra.Model): + """md5 hash object over the reaction IDs, bounds and stoichiometries of the model. + It is recomputed on every call and is therefore always up to date (also after changes + made from the console). Further data can be added with update() before digest()/hexdigest().""" + # float(c): make stable with respect to the number type of the stoichiometric coefficients (1 != 1.0) + return hashlib.md5(pickle.dumps(tuple( + (r.id, r.lower_bound, r.upper_bound, tuple((m.id, float(c)) for m, c in r.metabolites.items())) + for r in model.reactions), protocol=5)) + + +class CNApyModel(cobra.Model): + @staticmethod + def read_sbml_model(file_name): + model: cobra.Model = cobra.io.read_sbml_model(file_name) + # kludge because in COBRApy creating a model from a SBML file is not implemented as method of the Model class + model.__class__ = CNApyModel + return model + + def __init__(self, id_or_model=None, name=None): + super().__init__(id_or_model=id_or_model, name=name) + if id_or_model is None: + self.id = "" + + @property + def stoichiometry_hash_object(self): + """freshly computed on every access, see stoichiometry_hash""" + return stoichiometry_hash(self) + + # no-ops, only for compatibility with optlang_enumerator.mcs_computation which calls these + def set_reaction_hashes(self): + pass + + def set_stoichiometry_hash_object(self): + pass + + def restore_stoichiometry_hash_object(self, stoichiometry_hash_object): + pass diff --git a/cnapy/complex_test.py b/cnapy/complex_test.py new file mode 100644 index 00000000..ba40053f --- /dev/null +++ b/cnapy/complex_test.py @@ -0,0 +1,268 @@ +""" +A genuinely complex, cross-validated test scenario exercising every feature +of optmdfpathway.py together: + + - a strictly-coupled 3-reaction subset (reaction_subsets) + - a cofactor pair with a concentration ratio *range* (concentration_ratios) + - a reaction with dG0 given as (value, uncertainty) + - per-metabolite Cmin/Cmax via annotation, overriding the global default + - a structurally blocked dead-end reaction (must be pruned by FVA) + - a reaction with dG0 that is explicitly ignored (ignore_reactions) + - two scenario constraints: a Scenario-style bound *and* a raw multi- + reaction yield-style triple + - FVA preprocessing against the real core.py logic + - the full iterative bottleneck-relaxation loop (OptMDFAnalysis.run()) + - shadow_prices() / find_bottleneck() cross-checked against each other + - solve_fba() with a positive B_bounds[0] threshold and a real cobra + objective + +Cross-validated against an independently-built "baseline" model that +avoids every shortcut (no reaction_subsets, no FVA preprocessing) but is +otherwise identical, to confirm the optimizations don't change the answer. +""" +import sys +# sys.path.insert(0, '') # only needed if core.py isn't already importable + +from optmdfpathway import OptMDFAnalysis, optMDFpathway +import cobra +from optlang import highs_interface +cobra.Configuration.solver = highs_interface + + +def build_model(use_subset_friendly_ids=True): + """ + S --EX_S--> [uptake] + S -R1-> I1 -R2-> I2 -R3-> P (strictly coupled chain, dG0: -4, -3(+/-0.5), +1.5) + P -R4-> X (uses ATP->ADP) dG0 = -15 (very favourable) + ADP -ATPreg-> ATP regeneration, no dG0 + X -GROWTH-> Growth_met -EX_GROWTH-> [secretion] (the FBA objective) + P -Rdead-> Pb dG0 = -2, but Pb has NO exit -> blocked + I2 -Rignore-> Junk -EX_Junk-> dG0 = +5, but explicitly ignored + """ + m = cobra.Model('complex') + S = cobra.Metabolite('S', compartment='c') + S.annotation['Cmin'] = 1e-4 + S.annotation['Cmax'] = 0.1 + I1 = cobra.Metabolite('I1', compartment='c') + I2 = cobra.Metabolite('I2', compartment='c') + P = cobra.Metabolite('P', compartment='c') + X = cobra.Metabolite('X', compartment='c') + ATP = cobra.Metabolite('ATP', compartment='c') + ADP = cobra.Metabolite('ADP', compartment='c') + Growth_met = cobra.Metabolite('Growth_met', compartment='c') + Pb = cobra.Metabolite('Pb', compartment='c') + Junk = cobra.Metabolite('Junk', compartment='c') + + EX_S = cobra.Reaction('EX_S', lower_bound=-20, upper_bound=0) + EX_S.add_metabolites({S: -1}) + + R1 = cobra.Reaction('R1', lower_bound=0, upper_bound=1000) + R1.add_metabolites({S: -1, I1: 1}) + R1.annotation['dG0'] = -4.0 + + R2 = cobra.Reaction('R2', lower_bound=0, upper_bound=1000) + R2.add_metabolites({I1: -1, I2: 1}) + R2.annotation['dG0'] = (-3.0, 0.5) # (value, uncertainty) form + + R3 = cobra.Reaction('R3', lower_bound=0, upper_bound=1000) + R3.add_metabolites({I2: -1, P: 1}) + R3.annotation['dG0'] = 1.5 # mildly unfavourable -- expected first bottleneck + + R4 = cobra.Reaction('R4', lower_bound=0, upper_bound=1000) + R4.add_metabolites({P: -1, ATP: -1, X: 1, ADP: 1}) + R4.annotation['dG0'] = -15.0 + + ATPreg = cobra.Reaction('ATPreg', lower_bound=0, upper_bound=1000) + ATPreg.add_metabolites({ADP: -1, ATP: 1}) + + GROWTH = cobra.Reaction('GROWTH', lower_bound=0, upper_bound=1000) + GROWTH.add_metabolites({X: -1, Growth_met: 1}) + EX_GROWTH = cobra.Reaction('EX_GROWTH', lower_bound=0, upper_bound=1000) + EX_GROWTH.add_metabolites({Growth_met: -1}) + + Rdead = cobra.Reaction('Rdead', lower_bound=0, upper_bound=1000) + Rdead.add_metabolites({P: -1, Pb: 1}) + Rdead.annotation['dG0'] = -2.0 + # Pb has NO exit reaction at all -> Rdead is structurally blocked + + Rignore = cobra.Reaction('Rignore', lower_bound=0, upper_bound=1000) + Rignore.add_metabolites({I2: -1, Junk: 1}) + Rignore.annotation['dG0'] = 5.0 + EX_Junk = cobra.Reaction('EX_Junk', lower_bound=0, upper_bound=1000) + EX_Junk.add_metabolites({Junk: -1}) + + m.add_reactions([EX_S, R1, R2, R3, R4, ATPreg, GROWTH, EX_GROWTH, + Rdead, Rignore, EX_Junk]) + m.objective = GROWTH + return m + + +# scenario constraints: a Scenario-style bound + a raw multi-reaction yield triple +scenarios = [ + {'EX_GROWTH': (0.5, 1000.0)}, # force nonzero flux + ({'EX_GROWTH': 1.0, 'EX_S': 0.05}, '>=', 0.0), # GROWTH >= 0.05 * (-EX_S), a yield requirement +] +concentration_ratios = [('ATP', 'ADP', 5.0, 20.0)] # a RANGE, not just a fixed ratio +reaction_subsets = [[('R1', 1.0), ('R2', 1.0), ('R3', 1.0)]] # strict 1:1:1 coupling +ignore_reactions = ['Rignore'] + + +def run_optimized(): + m = build_model() + analysis = OptMDFAnalysis( + m, + Cmin=1e-6, Cmax=1e-2, # default fallback; S overridden via annotation + scenarios=scenarios, + concentration_ratios=concentration_ratios, + reaction_subsets=reaction_subsets, + ignore_reactions=ignore_reactions, + use_fva_preprocessing=True, + verbose=True, + ) + return analysis + + +def run_baseline(): + """Same model/constraints, but with every shortcut disabled: no + reaction_subsets (R1/R2/R3 stand alone), no FVA preprocessing (static + bounds). Independently exercises the same underlying MILP machinery.""" + m = build_model() + analysis = OptMDFAnalysis( + m, + Cmin=1e-6, Cmax=1e-2, + scenarios=scenarios, + concentration_ratios=concentration_ratios, + reaction_subsets=None, + ignore_reactions=ignore_reactions, + use_fva_preprocessing=False, + verbose=True, + ) + return analysis + + +if __name__ == '__main__': + print('=' * 70) + print('OPTIMIZED (reaction_subsets + FVA preprocessing)') + print('=' * 70) + opt = run_optimized() + r_opt = opt.solve() + print(r_opt) + print('fluxes:', r_opt.fluxes) + print('driving_forces:', r_opt.driving_forces) + print('concentrations:', r_opt.concentrations) + + print() + print('=' * 70) + print('BASELINE (no shortcuts)') + print('=' * 70) + base = run_baseline() + r_base = base.solve() + print(r_base) + print('driving_forces:', r_base.driving_forces) + + print() + print('=' * 70) + print('CROSS-VALIDATION') + print('=' * 70) + assert r_opt.status == r_base.status == 'optimal' + assert abs(r_opt.mdf - r_base.mdf) < 1e-6, (r_opt.mdf, r_base.mdf) + print(f'MDF matches exactly: optimized={r_opt.mdf:.10f} baseline={r_base.mdf:.10f}') + + # structural checks: Pb/Rdead never got thermodynamic machinery at all + # in the FVA-preprocessed run, because FVA discovers Rdead is + # structurally blocked (Pb has no exit reaction at all). The baseline + # (use_fva_preprocessing=False) only sees Rdead's own static bounds + # (0, 1000), which don't reveal that global infeasibility, so it still + # builds Rdead's driving-force constraint -- Rdead's flux still comes + # out exactly 0 in the solution either way, it's just that only FVA + # preprocessing *knows in advance* it can never be otherwise. This is + # precisely what FVA preprocessing buys: fewer constraints, not a + # different answer. + assert 'Rdead' not in r_opt.driving_forces + assert abs(r_base.fluxes['Rdead']) < 1e-9 + assert 'Rignore' not in r_opt.driving_forces + assert 'Rignore' not in r_base.driving_forces + print('OK: Rdead correctly pruned by FVA in the optimized run (absent from ' + 'driving_forces); present but always zero-flux in the baseline run. ' + 'Rignore excluded in both (explicitly ignored).') + + # every ACTIVE reaction's driving force must respect the achieved mdf + for label, r in [('optimized', r_opt), ('baseline', r_base)]: + for rid, df in r.driving_forces.items(): + if abs(r.fluxes[rid]) > 1e-9: + assert df >= r.mdf - 1e-6, f'{label}: {rid} active with df={df} < mdf={r.mdf}' + print('OK: every active reaction respects the achieved MDF in both versions') + + # cofactor ratio range respected + for label, r in [('optimized', r_opt), ('baseline', r_base)]: + ratio = r.concentrations['ATP'] / r.concentrations['ADP'] + assert 5.0 - 1e-6 <= ratio <= 20.0 + 1e-6, (label, ratio) + print('OK: ATP/ADP ratio respected in both versions') + + # S concentration respects its annotation-based bounds, not the 1e-6/1e-2 default + for label, r in [('optimized', r_opt), ('baseline', r_base)]: + assert 1e-4 - 1e-9 <= r.concentrations['S'] <= 0.1 + 1e-9, (label, r.concentrations['S']) + print('OK: S respects its annotation Cmin/Cmax override in both versions') + + # yield constraint respected + for label, r in [('optimized', r_opt), ('baseline', r_base)]: + assert r.fluxes['EX_GROWTH'] + 0.05 * r.fluxes['EX_S'] >= -1e-6, label + print('OK: yield scenario constraint respected in both versions') + + print() + print('=' * 70) + print('SHADOW PRICES / BOTTLENECK CROSS-CHECK (optimized instance)') + print('=' * 70) + duals = opt.shadow_prices() + bottleneck = opt.find_bottleneck() + print('duals:', duals) + print('bottleneck:', bottleneck) + # every reaction with a nonzero dual must be part of the verified + # bottleneck OR the bottleneck must at least explain the same mdf + # (duals can over/under-attribute under degeneracy -- see conversation; + # what must ALWAYS hold is that the bottleneck set is non-empty here, + # since R3 was deliberately made unfavourable) + assert bottleneck, 'expected a non-empty bottleneck (R3 chain is unfavourable)' + assert set(bottleneck) <= set(r_opt.driving_forces), bottleneck + print('OK: bottleneck search returns a valid, plausible non-empty set') + + print() + print('=' * 70) + print('ITERATIVE BOTTLENECK RELAXATION (fresh instance)') + print('=' * 70) + iterative = run_optimized() + prev_mdf = None + for i, (result, bn) in enumerate(iterative.run(max_iterations=10)): + print(f' iter {i}: mdf={result.mdf:.4f} bottleneck={bn}') + if prev_mdf is not None: + assert result.mdf >= prev_mdf - 1e-9, 'mdf decreased across iterations!' + prev_mdf = result.mdf + print(f'OK: mdf monotonically improved across {len(iterative.history)} iterations, ' + f'model/FVA/MILP built once') + + print() + print('=' * 70) + print('THERMODYNAMIC FBA') + print('=' * 70) + fba_analysis = run_optimized() + # rebuild with a positive MDF threshold -- reuse the model, new instance + m = build_model() + fba_analysis2 = OptMDFAnalysis( + m, Cmin=1e-6, Cmax=1e-2, scenarios=scenarios, + concentration_ratios=concentration_ratios, + reaction_subsets=reaction_subsets, ignore_reactions=ignore_reactions, + B_bounds=(1.0, 1e4), # require MDF >= 1.0 throughout + use_fva_preprocessing=True, verbose=True, + ) + r_fba = fba_analysis2.solve_fba() + print(r_fba) + print('objective_value (growth):', r_fba.objective_value) + assert r_fba.status == 'optimal' + assert r_fba.mdf >= 1.0 - 1e-6 + for rid, df in r_fba.driving_forces.items(): + if abs(r_fba.fluxes[rid]) > 1e-9: + assert df >= 1.0 - 1e-6, f'{rid} active with df={df} < required 1.0' + print('OK: thermodynamic FBA respects the 1.0 threshold for every active reaction') + + print() + print('ALL COMPLEX-SCENARIO CHECKS PASSED') diff --git a/cnapy/core.py b/cnapy/core.py index d824f159..a48d37aa 100644 --- a/cnapy/core.py +++ b/cnapy/core.py @@ -2,27 +2,37 @@ import os import itertools -from collections import defaultdict +from collections import defaultdict, namedtuple from typing import Dict, Tuple, List from collections import Counter import threading as th import numpy +import math +import pandas +import scipy.sparse as sp import cobra from cobra.util.array import create_stoichiometric_matrix from cobra.core.dictlist import DictList -from optlang.symbolics import Zero, Add +from cobra.core.solution import Solution +from optlang.symbolics import Add +from optlang import cplex_interface, gurobi_interface # , scip_interface +from cobra.exceptions import OptimizationError import highspy +import osqp -import efmtool_link.efmtool4cobra as efmtool4cobra -import efmtool_link.efmtool_extern as efmtool_extern from cnapy.flux_vector_container import FluxVectorMemmap, FluxVectorContainer from cnapy.appdata import Scenario +# from cnapy.conservation_relations import find_redundant_metabolites_qr +# import cnapy.optlang_highs_interface organic_elements = ['C', 'O', 'H', 'N', 'P', 'S'] def efm_computation(model: cobra.Model, scen_values: Dict[str, Tuple[float, float]], constraints: bool, print_progress_function=print, abort_callback=None): + # lazy import so that it is not necessary to start the JVM with CNApy + import efmtool_link.efmtool4cobra as efmtool4cobra + import efmtool_link.efmtool_extern as efmtool_extern stdf = create_stoichiometric_matrix( model, array_type='DataFrame') reversible, irrev_backwards_idx = efmtool4cobra.get_reversibility( @@ -34,7 +44,7 @@ def efm_computation(model: cobra.Model, scen_values: Dict[str, Tuple[float, floa if constraints: for r in scen_values.keys(): (vl, vu) = scen_values[r] - if vl == vu and vl == 0: + if vl == vu and vl == 0 and r in stdf.columns: # scenario reactions are not part of the model r_idx = stdf.columns.get_loc(r) reversible = numpy.delete(reversible, r_idx) del stdf[r] @@ -73,22 +83,62 @@ def efm_computation(model: cobra.Model, scen_values: Dict[str, Tuple[float, floa class QPnotSupportedException(Exception): pass -def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tuple[float, float]], use_QP: bool = False, +SolverFailure = namedtuple('SolverFailure', ('status',)) +# Result container for the standalone QP model (built without cobrapy's +# forward/reverse variable splitting); mirrors the small subset of +# cobra.Solution's interface (.status, .fluxes) that downstream code needs. +QPSolution = namedtuple('QPSolution', ('status', 'objective_value', 'fluxes')) + +def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Scenario, use_QP: bool = False, flux_weight_scale: float = 1.0, abs_flux_weights: bool = False, weights_key: str = None, bm_reac_id: str = "", variable_constituents: List[cobra.Metabolite] = None, max_coeff_change: float = 0.9, min_rel_changes: bool = True, bm_change_in_gram: bool = False, gam_mets_param: Tuple[List[cobra.Metabolite], float, float, float] = ([], 0.0, 0.0, 0.0)): + # For now, unless CPLEX or Gurobi are set as current solvers, OSQP will be used + # for QPs because it performs mich better than HiGHS or SCIP. + # There is some commented-out code that may be reactivated in case HiGHS or SCIP improve. + if use_QP and cobra_model.problem not in (cplex_interface, gurobi_interface): + osqp_settings={ + 'max_iter': 100000, + 'eps_abs': cobra_model.tolerance, # default is 1e-8 + 'eps_rel': cobra_model.tolerance, # default is 1e-8 + } + return make_scenario_feasible_osqp(cobra_model, scen_values, + flux_weight_scale=flux_weight_scale, abs_flux_weights=abs_flux_weights, + weights_key=weights_key, + bm_reac_id=bm_reac_id, variable_constituents=variable_constituents, + max_coeff_change=max_coeff_change, min_rel_changes=min_rel_changes, + bm_change_in_gram=bm_change_in_gram, + gam_mets_param=gam_mets_param, osqp_settings=osqp_settings) + # if flux_weight_scale == 0 only biomass equation is adjusted # if bm_reac_id == "" only fluxes will be adjusted reactions_in_objective = [] bm_mod = dict() # for use with Reaction.add_metabolites gam_mets_sign = [] gam_adjust = 0 - if use_QP: - qp_terms = [] # list of terms for the quadratic objective with cobra_model as model: - model.objective = model.problem.Objective(Zero, direction='min') + model.objective = model.problem.Objective(0, direction='min') scen_values.add_scenario_reactions_to_model(model) + if use_QP: + interface = model.problem # optlang solver interface (shared between cobra_model and the standalone QP model) + flux_vars = None # populated below, only when use_QP + met_constraints = None + qp_terms = [] # list of terms for the quadratic objective + qp_flux_targets = {} # reaction_id -> (target_value, weight); QP terms built once flux_vars exist below + # selection = find_redundant_metabolites_qr(create_stoichiometric_matrix(cobra_model)) + # redundant_met = model.metabolites.get_by_any(selection['remove_idx'].tolist()) + # redundant_met_ids = {m.id for m in redundant_met} + # # Note: we deliberately do NOT call model.remove_metabolites() here. + # # cobra_model is never mutated for the QP case -- the standalone + # # qp_model built below just excludes these metabolites' rows when + # # constructing its own mass-balance constraints. This also avoids + # # a context-manager rollback bug: remove_metabolites() inside a + # # `with cobra_model as model:` block gets replayed (re-added) at + # # __exit__, which was tripping a stale-row-count bug in the HiGHS + # # interface's constraint re-insertion path. + # print("Excluding redundant (conserved-moiety) metabolites from the QP mass balance:") + # print(redundant_met) if flux_weight_scale > 0: for reaction_id, scen_val in scen_values.items(): if reaction_id == bm_reac_id: @@ -110,7 +160,7 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl else: if isinstance(weights_key, str): try: - weight = float(reaction.annotation.get(weights_key, flux_weight_scale)) + weight = float(scen_values.reaction_annotation(reaction, weights_key, flux_weight_scale)) except ValueError: weight = 0 if weight <= 0: @@ -121,11 +171,16 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl weight = flux_weight_scale if use_QP: - qp_terms.append(((reaction.flux_expression - scen_val[0])**2)/weight) + # Variable splitting is avoided entirely: no net_var/ + # net_constr indirection is needed here anymore. We + # just remember the target/weight; the actual QP term + # is built directly on the single, unsplit flux + # variable once the standalone QP model exists below. + qp_flux_targets[reaction_id] = (scen_val[0], weight) else: pos_slack = model.problem.Variable(reaction_id+"_make_feasible_linear_pos_slack", lb=0, ub=None) neg_slack = model.problem.Variable(reaction_id+"_make_feasible_linear_neg_slack", lb=0, ub=None) - elastic_constr = model.problem.Constraint(Zero, lb=scen_val[0], ub=scen_val[0]) + elastic_constr = model.problem.Constraint(0, lb=scen_val[0], ub=scen_val[0]) model.add_cons_vars([pos_slack, neg_slack, elastic_constr]) elastic_constr.set_linear_coefficients({reaction.forward_variable: 1.0, reaction.reverse_variable: -1.0, pos_slack: 1.0, neg_slack: -1.0}) @@ -135,6 +190,41 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl reaction.lower_bound = scen_val[0] reaction.upper_bound = scen_val[1] + if use_QP: + # Build a dedicated, standalone optlang Model with exactly one + # (unsplit) flux variable per reaction, using the same solver + # interface as cobra_model. This is where variable splitting is + # avoided completely: cobra_model's own forward_variable / + # reverse_variable machinery is never touched for the QP. + # This is not strictly necessary for CPLEX/Gurobi, but lowers that numbers + # of variables so that the community models can be used for more models. + qp_model = interface.Model(name="scenario_feasibility_QP") + + flux_vars = {r.id: interface.Variable(r.id, lb=r.lower_bound, ub=r.upper_bound) for r in model.reactions} + qp_model.add(list(flux_vars.values())) + qp_model.update() + + # Mass balance constraints, one per metabolite EXCLUDING the + # redundant (conserved-moiety) rows identified above -- those + # rows are skipped here rather than removed from cobra_model. + met_terms = defaultdict(list) + for r in model.reactions: + v = flux_vars[r.id] + for met, coeff in r.metabolites.items(): + # if met.id in redundant_met_ids: + # continue + met_terms[met.id].append(coeff * v) + + met_constraints = {} + for met_id, terms in met_terms.items(): + expr = terms[0] if len(terms) == 1 else Add(*terms) + met_constraints[met_id] = interface.Constraint(expr, lb=0, ub=0, name=met_id, sloppy=True) + qp_model.add(list(met_constraints.values()), sloppy=True) + qp_model.update() # commit constraints before any later set_linear_coefficients calls on them + + qp_terms += [((flux_vars[rid] - target) ** 2) / weight + for rid, (target, weight) in qp_flux_targets.items()] + if len(bm_reac_id) > 0: mue_fixed = scen_values[bm_reac_id][0] bm_reaction: cobra.Reaction = cobra_model.reactions.get_by_id(bm_reac_id) @@ -144,6 +234,16 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl bm_coeff_var = [[m, m.formula_weight, c,None] for m,f,c in bm_coeff_var if c < 0 and m.formula_weight > 0 and f.get('C', 0) > 0 and f.get('P', 0) == 0] else: bm_coeff_var = [[met, met.formula_weight, bm_reaction.metabolites[met], None] for met in variable_constituents] + # if use_QP: + # # A biomass constituent that happens to be one of the redundant + # # (conserved-moiety) rows has no entry in met_constraints (its + # # balance is only implicitly represented via the rest of its + # # conserved pool) -- skip it here rather than KeyError below. + # excluded = [m for m, *_ in bm_coeff_var if m.id in redundant_met_ids] + # if excluded: + # print("Skipping QP slack for biomass constituent(s) that are part of a " + # "removed conservation relation:", [m.id for m in excluded]) + # bm_coeff_var = [entry for entry in bm_coeff_var if entry[0].id not in redundant_met_ids] if flux_weight_scale == 0: # otherwise they have already been integrated above for reac_id in scen_values: try: @@ -152,10 +252,21 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl print('reaction', reac_id, 'not found!') else: reaction.bounds = scen_values[reac_id] + if use_QP: + # keep the standalone QP's flux variable bounds in sync; + # set_bounds() avoids a transient lb>ub error that separate + # .lb=/.ub= assignments could hit depending on prior bounds + flux_vars[reaction.id].set_bounds(*reaction.bounds) bm_reaction.lower_bound = mue_fixed bm_reaction.upper_bound = mue_fixed - mass_const = model.problem.Constraint(0, lb=0, ub=0) - model.add_cons_vars([mass_const]) + if use_QP: + flux_vars[bm_reaction.id].set_bounds(mue_fixed, mue_fixed) + mass_const = interface.Constraint(0, lb=0, ub=0) + qp_model.add([mass_const]) + qp_model.update() + else: + mass_const = model.problem.Constraint(0, lb=0, ub=0) + model.add_cons_vars([mass_const]) i = 0 while i < len(bm_coeff_var): @@ -167,10 +278,11 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl continue bm_coeff_var[i][2] = coeff if use_QP: - slack = model.problem.Variable(met.id+"_slack", lb=-abs(coeff)*max_coeff_change, ub=abs(coeff)*max_coeff_change) + slack = interface.Variable(met.id+"_slack", lb=-abs(coeff)*max_coeff_change, ub=abs(coeff)*max_coeff_change) bm_coeff_var[i][3] = slack - model.add_cons_vars([slack]) - met.constraint.set_linear_coefficients({slack: mue_fixed}) + qp_model.add([slack]) + qp_model.update() # slack must be committed before referencing it below + met_constraints[met.id].set_linear_coefficients({slack: mue_fixed}) mass_const.set_linear_coefficients({slack: mol_weigt}) else: pos_slack = model.problem.Variable(met.id+"_pos_slack", lb=0, ub=abs(coeff)*max_coeff_change) @@ -185,14 +297,20 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl if len(gam_mets) > 0: gam_mets_sign = [0] * len(gam_mets) if use_QP: - gam_slack = model.problem.Variable("gam_slack", lb=-1.0, ub=1.0) - model.add_cons_vars([gam_slack]) + scale = gam_max_change * mue_fixed + gam_slack = interface.Variable("gam_slack", lb=-scale, ub=scale) + qp_model.add([gam_slack]) + qp_model.update() # commit gam_slack before wiring it into met_constraints below for i in range(len(gam_mets)): met = gam_mets[i] + # if met.id in redundant_met_ids: + # print("Skipping GAM slack wiring for", met.id, + # "-- part of a removed conservation relation.") + # continue 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}) + met_constraints[met.id].set_linear_coefficients({gam_slack: sign}) gam_mets_sign[i] = sign - qp_terms.append(gam_weight * (gam_slack**2)) + qp_terms.append((gam_weight / scale**2) * (gam_slack**2)) else: gam_slack_pos = model.problem.Variable("gam_slack_pos", lb=0.0, ub=1.0) gam_slack_neg = model.problem.Variable("gam_slack_neg", lb=0.0, ub=1.0) @@ -226,43 +344,315 @@ def make_scenario_feasible(cobra_model: cobra.Model, scen_values: Dict[str, Tupl if use_QP: try: - model.objective = model.problem.Objective(Add(*qp_terms), direction='min') + qp_model.objective = interface.Objective(Add(*qp_terms), direction='min') + print(qp_model.objective) except ValueError: # solver does not support QP raise QPnotSupportedException - solution = model.optimize() - print(solution) + try: + # if interface == cnapy.optlang_highs_interface: + # qp_model.problem.setOptionValue("solver", model.solver.problem.getOptionValue("solver")[1]) + # qp_model.problem.setOptionValue("kkt_tolerance", model.tolerance) + # qp_model.problem.setOptionValue("output_flag", True) + # elif interface == scip_interface: + # qp_model.configuration.verbosity = 3 + status = qp_model.optimize() + except Exception as exc: # standalone optlang Model.optimize() generally returns a + status = 'error' # status string rather than raising; guard against solver-level errors anyway + print("QP solver raised an exception:", exc) + if status == 'optimal': + fluxes = pandas.Series({r.id: flux_vars[r.id].primal for r in model.reactions}) + solution = QPSolution(status=status, objective_value=qp_model.objective.value, fluxes=fluxes) + else: + solution = SolverFailure(status=status) + print("Optimization failed, no solution could be found.") + print("Try relaxing model tolerance or choose a different solver.") + else: + try: + solution = model.optimize() + except OptimizationError: + solution = SolverFailure(status=model.solver.status) + print("Optimization failed, no solution could be found.") + print("Try relaxing model tolerance or choose a different solver.") - if len(bm_reac_id) > 0: - format_string = "{:.2g} {:.2g}" - if use_QP: - for m,_,coeff,s in bm_coeff_var: - v = model.solver.variables[s.name].primal - if v != 0: - print(s.name, format_string.format(v, v/abs(coeff))) - bm_mod[m] = v + if solution.status == "optimal": + if len(bm_reac_id) > 0: + format_string = "{:.2g} {:.2g}" + if use_QP: + for m,_,coeff,s in bm_coeff_var: + v = s.primal + if v != 0: + print(s.name, format_string.format(v, v/abs(coeff))) + bm_mod[m] = v + if len(gam_mets) > 0: + # gam_slack already represents the physical adjustment directly + # (its bounds/coefficients were rescaled to +/-gam_max_change*mue_fixed + # with unit constraint coefficients), so recovering the original + # [-gam_max_change, gam_max_change]-scaled quantity means dividing + # back out mue_fixed rather than re-multiplying by gam_max_change. + gam_adjust = gam_slack.primal / mue_fixed + else: + for m,_,coeff,(s_p,s_n) in bm_coeff_var: + v = model.solver.variables[s_p.name].primal + if v != 0: + print(s_p.name, format_string.format(v, v/abs(coeff))) + bm_mod[m] = v + v = model.solver.variables[s_n.name].primal + if v != 0: + print(s_n.name, format_string.format(v, v/abs(coeff))) + bm_mod[m] = -v + if len(gam_mets) > 0: + gam_adjust = gam_max_change * \ + (model.solver.variables["gam_slack_pos"].primal - model.solver.variables["gam_slack_neg"].primal) if len(gam_mets) > 0: - gam_adjust = gam_max_change * model.solver.variables["gam_slack"].primal + if use_QP: + print("gam_slack {:.3g}".format(gam_slack.primal)) + else: + print("gam_slack_pos", model.solver.variables["gam_slack_pos"].primal) + print("gam_slack_neg", model.solver.variables["gam_slack_neg"].primal) + + return solution, reactions_in_objective, bm_mod, gam_mets_sign, gam_adjust + + +def make_scenario_feasible_osqp(cobra_model: cobra.Model, scen_values: Scenario, + flux_weight_scale: float = 1.0, abs_flux_weights: bool = False, weights_key: str = None, + bm_reac_id: str = "", variable_constituents: List[cobra.Metabolite] = None, + max_coeff_change: float = 0.9, min_rel_changes: bool = True, bm_change_in_gram: bool = False, + gam_mets_param: Tuple[List[cobra.Metabolite], float, float, float] = ([], 0.0, 0.0, 0.0), + osqp_settings: dict = None): + """QP variant of make_scenario_feasible that builds and solves the problem directly with the osqp + Python package, bypassing optlang/cobra.util.solver entirely (i.e. this replaces the use_QP=True + branch of make_scenario_feasible; there is no use_QP parameter here since this function is always QP). + + Deliberate differences from the optlang-based use_QP=True path, all a consequence of not building the + problem through cobra's optlang Model: + + - Each reaction is a single (possibly negative) variable with cobra's own bounds, not optlang's + forward/reverse split. Consequently the "net_flux" helper variable used in make_scenario_feasible + (introduced there only to avoid cross-terms between the forward and reverse variables) is not + needed; the flux variable itself is used directly in the quadratic deviation terms. + - Variable bounds are enforced as extra rows appended to the constraint matrix, since osqp has no + native notion of variable bounds. + - `solution` is a real cobra.core.Solution (status/objective_value/fluxes) on success, or the same + SolverFailure(status=...) namedtuple used in make_scenario_feasible on failure/infeasibility, so + callers can keep using `solution.status == "optimal"`. + - `objective_value` includes the constant term(s) dropped when completing the square for osqp's + 0.5 x^T P x + q^T x form, so it is comparable to what make_scenario_feasible would report. + + This function assumes scen_values.add_scenario_reactions_to_model only performs cobra-level edits + (e.g. via add_reactions/bounds) since it is invoked through the normal cobra_model context manager + here, exactly as in make_scenario_feasible; no optlang objects are ever created or required. + + All other parameters have the same meaning as in make_scenario_feasible. + + osqp_settings: optional dict overriding the default osqp solver settings (verbose, polish, eps_abs, + eps_rel, max_iter). + """ + + reactions_in_objective: List[str] = [] + bm_mod = dict() # for use with Reaction.add_metabolites + gam_mets_sign = [] + gam_adjust = 0 + + # (reaction_id, target_value, weight) for the flux-fixation quadratic terms; resolved to column + # indices once the final (post-scenario) reaction list is known. + quad_terms = [] + + with cobra_model as model: + scen_values.add_scenario_reactions_to_model(model) + + if flux_weight_scale > 0: + for reaction_id, scen_val in scen_values.items(): + if reaction_id == bm_reac_id: + continue # growth rate will be fixed below if biomass adjustment is used + try: + reaction: cobra.Reaction = model.reactions.get_by_id(reaction_id) + except KeyError: + print('reaction', reaction_id, 'not found!') + continue + # reactions set to 0 are still considered off + if scen_val[0] == scen_val[1] and scen_val[0] != 0: + reactions_in_objective.append(reaction_id) + if scen_val[0] < reaction.lower_bound: + reaction.lower_bound = cobra.Configuration().lower_bound + if scen_val[0] > reaction.upper_bound: + reaction.upper_bound = cobra.Configuration().upper_bound + if abs_flux_weights: + weight = abs(scen_val[0]) * flux_weight_scale # for scaling relative to biomass adjustment + else: + if isinstance(weights_key, str): + try: + weight = float(scen_values.reaction_annotation(reaction, weights_key, flux_weight_scale)) + except ValueError: + weight = 0 + if weight <= 0: + print("The value of annotation key '" + weights_key + "' of reaction'" + + reaction_id + "' is not a positive number, using default weight.") + weight = flux_weight_scale + else: + weight = flux_weight_scale + quad_terms.append((reaction_id, scen_val[0], weight)) + else: + reaction.lower_bound = scen_val[0] + reaction.upper_bound = scen_val[1] + + bm_coeff_var = [] # [metabolite, mol_weight, coeff], slack column index tracked separately below + gam_mets: List[cobra.Metabolite] = [] + gam_max_change = gam_weight = gam_base = 0.0 + mue_fixed = None + bm_reaction = None + use_gam = False + + if len(bm_reac_id) > 0: + mue_fixed = scen_values[bm_reac_id][0] + bm_reaction = cobra_model.reactions.get_by_id(bm_reac_id) + gam_mets, gam_max_change, gam_weight, gam_base = gam_mets_param + if variable_constituents is None: + bm_coeff_all = [(met, met.elements, coeff) for met, coeff in bm_reaction.metabolites.items()] + bm_coeff_var = [[m, m.formula_weight, c] for m, f, c in bm_coeff_all + if c < 0 and m.formula_weight > 0 and f.get('C', 0) > 0 and f.get('P', 0) == 0] else: - for m,_,coeff,(s_p,s_n) in bm_coeff_var: - v = model.solver.variables[s_p.name].primal - if v != 0: - print(s_p.name, format_string.format(v, v/abs(coeff))) - bm_mod[m] = v - v = model.solver.variables[s_n.name].primal + bm_coeff_var = [[met, met.formula_weight, bm_reaction.metabolites[met]] for met in variable_constituents] + + if flux_weight_scale == 0: # otherwise they have already been integrated above + for reac_id in scen_values: + try: + reaction = model.reactions.get_by_id(reac_id) + except KeyError: + print('reaction', reac_id, 'not found!') + else: + reaction.bounds = scen_values[reac_id] + + bm_reaction.lower_bound = mue_fixed + bm_reaction.upper_bound = mue_fixed + + i = 0 + while i < len(bm_coeff_var): + met, mol_weigt, coeff = bm_coeff_var[i] + if met in gam_mets and gam_base > 0: + coeff = coeff - numpy.sign(coeff) * gam_base + if coeff == 0: # can e.g. occur when ATP is only used for GAM + del bm_coeff_var[i] + continue + bm_coeff_var[i][2] = coeff + i += 1 + + use_gam = len(gam_mets) > 0 + + # ---- final (post-scenario) cobra model structure ---- + n_rxns = len(model.reactions) + n_mets = len(model.metabolites) + rxn_index = {r.id: idx for idx, r in enumerate(model.reactions)} + met_index = {m.id: idx for idx, m in enumerate(model.metabolites)} + + col_lower = numpy.array([r.lower_bound for r in model.reactions], dtype=numpy.double) + col_upper = numpy.array([r.upper_bound for r in model.reactions], dtype=numpy.double) + S = create_stoichiometric_matrix(model, array_type='lil') + + # ---- assign column indices for the extra (slack) variables ---- + extra_lb: List[float] = [] + extra_ub: List[float] = [] + gam_slack_idx = None + if use_gam: + gam_slack_idx = n_rxns + len(extra_lb) + extra_lb.append(-1.0) + extra_ub.append(1.0) + + bm_slack_idx = [] # parallel to bm_coeff_var + for met, mol_weigt, coeff in bm_coeff_var: + bm_slack_idx.append(n_rxns + len(extra_lb)) + extra_lb.append(-abs(coeff) * max_coeff_change) + extra_ub.append(abs(coeff) * max_coeff_change) + + n_vars = n_rxns + len(extra_lb) + col_lower = numpy.concatenate([col_lower, extra_lb]) + col_upper = numpy.concatenate([col_upper, extra_ub]) + + # ---- diagonal quadratic objective: P (diagonal) and q ---- + P_diag = numpy.zeros(n_vars) + q = numpy.zeros(n_vars) + const_offset = 0.0 # only affects the reported objective_value, not the solution itself + + for reaction_id, target, weight in quad_terms: + j = rxn_index[reaction_id] + P_diag[j] += 2.0 / weight + q[j] += -2.0 * target / weight + const_offset += target ** 2 / weight + + if gam_slack_idx is not None: + P_diag[gam_slack_idx] += 2.0 * gam_weight + + for (met, mol_weigt, coeff), s_idx in zip(bm_coeff_var, bm_slack_idx): + scale = mol_weigt if bm_change_in_gram else 1.0 + a = (scale / abs(coeff)) ** 2 if min_rel_changes else scale ** 2 + P_diag[s_idx] += 2.0 * a + + # ---- constraint matrix: stoichiometry (+ slack contributions), mass balance, box bounds ---- + n_eq_extra = 1 if len(bm_reac_id) > 0 else 0 # the mass_const row + A_eq = sp.lil_matrix((n_mets + n_eq_extra, n_vars)) + A_eq[:n_mets, :n_rxns] = S + + if gam_slack_idx is not None: + for met in gam_mets: + sign = numpy.sign(bm_reaction.metabolites[met]) + gam_mets_sign.append(sign) + A_eq[met_index[met.id], gam_slack_idx] += sign * gam_max_change * mue_fixed + + mass_row = n_mets # only used/meaningful when n_eq_extra == 1 + for (met, mol_weigt, coeff), s_idx in zip(bm_coeff_var, bm_slack_idx): + A_eq[met_index[met.id], s_idx] += mue_fixed + if n_eq_extra: + A_eq[mass_row, s_idx] += mol_weigt + + l_eq = numpy.zeros(n_mets + n_eq_extra) + u_eq = numpy.zeros(n_mets + n_eq_extra) + + A_box = sp.eye(n_vars, format='lil') + A = sp.vstack([A_eq, A_box]).tocsc() + l = numpy.concatenate([l_eq, col_lower]) + u = numpy.concatenate([u_eq, col_upper]) + + P = sp.diags(P_diag, format='csc') + + settings = dict(verbose=False, polish=True, eps_abs=1e-8, eps_rel=1e-8, max_iter=20000) + if osqp_settings: + settings.update(osqp_settings) + + prob = osqp.OSQP() + prob.setup(P=P, q=q, A=A, l=l, u=u, **settings) + res = prob.solve() + raw_status = res.info.status + + if raw_status in ("solved", "solved inaccurate"): + status = "optimal" + elif "infeasible" in raw_status: + status = "infeasible" + elif "unbounded" in raw_status: + status = "unbounded" + else: + status = raw_status + + if status == "optimal": + objective_value = res.info.obj_val + const_offset + fluxes = pandas.Series(res.x[:n_rxns], index=[r.id for r in model.reactions]) + solution = Solution(objective_value, status, fluxes) + print(solution) + + if len(bm_reac_id) > 0: + format_string = "{:.2g} {:.2g}" + for (met, mol_weigt, coeff), s_idx in zip(bm_coeff_var, bm_slack_idx): + v = res.x[s_idx] if v != 0: - print(s_n.name, format_string.format(v, v/abs(coeff))) - bm_mod[m] = -v - if len(gam_mets) > 0: - gam_adjust = gam_max_change * \ - (model.solver.variables["gam_slack_pos"].primal - model.solver.variables["gam_slack_neg"].primal) - if len(gam_mets) > 0: - if use_QP: - print("gam_slack {:.3g}".format(model.solver.variables["gam_slack"].primal)) - else: - print("gam_slack_pos", model.solver.variables["gam_slack_pos"].primal) - print("gam_slack_neg", model.solver.variables["gam_slack_neg"].primal) + print(met.id, format_string.format(v, v / abs(coeff))) + bm_mod[met] = v + if gam_slack_idx is not None: + gam_adjust = gam_max_change * res.x[gam_slack_idx] + print("gam_slack {:.3g}".format(res.x[gam_slack_idx])) + else: + solution = SolverFailure(status=status) + print("Optimization failed, no solution could be found. (osqp status: {})".format(raw_status)) + + return solution, reactions_in_objective, bm_mod, gam_mets_sign, gam_adjust - return solution, reactions_in_objective, bm_mod, gam_mets_sign, gam_adjust def element_exchange_balance(model: cobra.Model, scen_values: Scenario, non_boundary_reactions: List[str], organic_elements_only=False, print_func=print): @@ -379,7 +769,7 @@ def replace_ids(dict_list: DictList, annotation_key: str, unambiguous_only: bool if len(candidates) > 0 and new_id != old_id and old_id == entry.id: print("Could not find a new ID for", entry.id, "in", candidates) -def build_highs_fba_model(cobra_model: cobra.Model, constraints=None) -> highspy.HighsLp: +def build_highs_fba_model(cobra_model: cobra.Model, constraints=None): """Build a highspy.HighsLp instance directly from a cobrapy Model, bypassing optlang entirely. """ @@ -446,64 +836,540 @@ def build_highs_fba_model(cobra_model: cobra.Model, constraints=None) -> highspy else highspy.ObjSense.kMinimize ) - return lp + return lp, S + +YoptSolution = namedtuple('YoptSolution', ('status', 'objective_value', 'fluxes', 'scalable')) + +def _highs_fba_optimize(cobra_model: cobra.Model, obj: Dict[str, float], sense: str, constraints=None): + """Plain LP: optimize a linear combination of fluxes over the model's + feasible flux polytope, built via build_highs_fba_model. Returns + (status, objective_value, flux_array_or_None) with status in + {"optimal", "unbounded", "infeasible"}. Used both to determine which + sign(s) a denominator expression can take (see highs_yopt) and, as a + plain LP, to build fallback example flux distributions.""" + lp, _ = build_highs_fba_model(cobra_model, constraints) + reaction_ids = cobra_model.reactions.list_attr("id") + idx = {rid: i for i, rid in enumerate(reaction_ids)} + lp.col_cost_[:] = 0.0 + for rid, coeff in obj.items(): + if rid in idx: + lp.col_cost_[idx[rid]] = coeff + lp.sense_ = highspy.ObjSense.kMaximize if sense == 'maximize' else highspy.ObjSense.kMinimize + h = highspy.Highs() + h.passModel(lp) + h.setOptionValue("output_flag", False) + h.run() + status = h.getModelStatus() + if status == highspy.HighsModelStatus.kOptimal: + x = numpy.asarray(h.getSolution().col_value) + return "optimal", h.getInfo().objective_function_value, x + elif status == highspy.HighsModelStatus.kUnbounded: + # HiGHS' simplex detects unboundedness from a feasible vertex, so + # col_value is still usually a genuine (if not very meaningful) + # feasible point -- good enough as a fallback example distribution. + try: + x = numpy.asarray(h.getSolution().col_value) + except Exception: + x = None + return "unbounded", (math.inf if sense == 'maximize' else -math.inf), x + else: + return "infeasible", float('nan'), None + +def build_highs_yopt_model(cobra_model: cobra.Model, obj_num: numpy.ndarray, obj_den: numpy.ndarray, + denominator_sign: float, sense: str, constraints=None): + """ + Build the Charnes-Cooper linearisation of a single yield-optimization + LP directly as a highspy.HighsLp -- same low-level construction style + as build_highs_fba_model, with one extra variable (a scaling factor t) + and every row made homogeneous in t. + + Derivation + ---------- + We want to optimize (obj_num . v) / (obj_den . v) over the feasible + flux polytope P = {v : S v = 0, lb <= v <= ub, extra constraints}. + Since the sign of obj_den . v isn't known a priori, fix it to a chosen + denominator_sign s in {-1, +1} (the caller tries every sign that is + actually achievable on P -- see highs_yopt) and introduce a scaling + factor t > 0 with x = t * v, so that obj_den . x = s. Then every + constraint on v becomes homogeneous in (x, t): + S v = 0 -> S x = 0 + lb <= v <= ub -> lb*t <= x <= ub*t + a . v {<=,=,>=} rhs -> a . x {<=,=,>=} rhs*t + and the ratio itself simplifies to a *linear* objective: + (obj_num.v)/(obj_den.v) = (obj_num.v)/(s/t) = s * (obj_num . (t*v)) + = s * (obj_num . x) + So maximizing/minimizing the original ratio is exactly + optimize_{sense} s * (obj_num . x) subject to the above (plus t >= 0), + and the LP's optimal objective value *is* the yield -- no rescaling + needed afterwards. + + Variables are [v_1..v_n, t] (t is the last column, x is just v scaled + by t so no separate x columns are needed). + + Returns + ------- + (highspy.HighsLp, reaction_ids) + """ + reaction_ids = cobra_model.reactions.list_attr("id") + idx = {rid: i for i, rid in enumerate(reaction_ids)} + n = len(reaction_ids) + m = len(cobra_model.metabolites) + constraints = list(constraints or []) + n_extra = len(constraints) + + lb_arr = numpy.array([r.lower_bound for r in cobra_model.reactions]) + ub_arr = numpy.array([r.upper_bound for r in cobra_model.reactions]) + real_lb = numpy.where(numpy.isfinite(lb_arr))[0] + real_ub = numpy.where(numpy.isfinite(ub_arr))[0] + + n_rows = m + n_extra + len(real_lb) + len(real_ub) + 1 + n_vars = n + 1 # + t + + S = create_stoichiometric_matrix(cobra_model, array_type="lil") + A = sp.lil_matrix((n_rows, n_vars)) + A[:m, :n] = S # S x = 0, already homogeneous (no t term needed) + row_lower = numpy.zeros(n_rows) + row_upper = numpy.zeros(n_rows) + + row = m + for expr, ctype, rhs in constraints: + for rid, coeff in expr.items(): + if rid not in idx: + print(f"Skipping yield constraint referencing a reaction not in the model: {rid}") + continue + A[row, idx[rid]] += coeff + A[row, n] = -float(rhs) # a.x - rhs*t {0} <=> a.v {rhs} + if ctype == '=': + row_lower[row] = row_upper[row] = 0.0 + elif ctype == '<=': + row_lower[row], row_upper[row] = -math.inf, 0.0 + else: # '>=' + row_lower[row], row_upper[row] = 0.0, math.inf + row += 1 + + for i in real_lb: # x_i - lb_i*t >= 0 <=> x_i >= lb_i*t + A[row, i] = 1.0 + A[row, n] = -lb_arr[i] + row_lower[row], row_upper[row] = 0.0, math.inf + row += 1 + for i in real_ub: # x_i - ub_i*t <= 0 <=> x_i <= ub_i*t + A[row, i] = 1.0 + A[row, n] = -ub_arr[i] + row_lower[row], row_upper[row] = -math.inf, 0.0 + row += 1 + + for i, c in enumerate(obj_den): # obj_den . x == denominator_sign + if c != 0.0: + A[row, i] = c + row_lower[row] = row_upper[row] = float(denominator_sign) + + col_lower = numpy.full(n_vars, -math.inf) + col_upper = numpy.full(n_vars, math.inf) + col_lower[n], col_upper[n] = 0.0, math.inf # t >= 0 + + col_cost = numpy.zeros(n_vars) + col_cost[:n] = denominator_sign * obj_num + + lp = highspy.HighsLp() + lp.a_matrix_.format_ = highspy.MatrixFormat.kRowwise + lp.num_col_ = n_vars + lp.num_row_ = n_rows + lp.col_cost_ = col_cost + lp.col_lower_ = col_lower + lp.col_upper_ = col_upper + lp.row_lower_ = row_lower + lp.row_upper_ = row_upper + A = A.tocsr() + lp.a_matrix_.start_ = A.indptr.astype(numpy.int32) + lp.a_matrix_.index_ = A.indices.astype(numpy.int32) + lp.a_matrix_.value_ = A.data.astype(numpy.double) + lp.sense_ = highspy.ObjSense.kMaximize if sense == 'maximize' else highspy.ObjSense.kMinimize + + return lp, reaction_ids + +def highs_yopt(cobra_model: cobra.Model, obj_num: Dict[str, float], obj_den: Dict[str, float], + obj_sense: str = 'maximize', constraints=None, + denominator_tol: float = 1e-9) -> YoptSolution: + """ + Yield optimization (YOpt), linear-fractional-programming approach directly on top of highspy + (see build_highs_yopt_model's docstring for the Charnes-Cooper derivation). + + Parameters + ---------- + obj_num, obj_den : dict {reaction_id: coefficient} + The linear numerator/denominator expressions of the yield. + obj_sense : 'maximize' or 'minimize' + constraints : optional list of (dict, str, float) triples + Same shape as multi_threaded_HiGHS_FVA's/cnapy Scenario + constraints (see thermodynamics_dialog.py); applied on top of the + model's own reaction bounds. + denominator_tol : float + Absolute tolerance below which obj_den . v is treated as + structurally forced to 0 in a given direction. + + Returns + ------- + YoptSolution + .status "optimal", "unbounded", or "infeasible" + .objective_value the optimal yield; +-inf if truly unbounded, nan + if undefined (denominator can approach, but + never reach, 0 while the numerator stays + nonzero) or if infeasible + .fluxes {reaction_id: flux}, an example flux + distribution (empty if infeasible) + .scalable True if the example flux distribution may be + scaled by an arbitrary positive factor without + changing the yield + """ + reaction_ids = cobra_model.reactions.list_attr("id") + n = len(reaction_ids) + idx = {rid: i for i, rid in enumerate(reaction_ids)} + sense = 'minimize' if obj_sense in ('min', 'minimize') else 'maximize' + + def to_vec(d): + v = numpy.zeros(n) + for rid, c in d.items(): + if rid not in idx: + raise KeyError(f"Reaction {rid!r} referenced in the yield objective is not in the model") + v[idx[rid]] = c + return v -class FVAHiGHSworker(th.Thread): - def __init__(self, job_queue, job_queue_lock, lb, ub, lp_data, tolerance): + num_vec = to_vec(obj_num) + den_vec = to_vec(obj_den) + + # -- which sign(s) can obj_den . v actually take on the feasible region? -- + status_min, min_D, _ = _highs_fba_optimize(cobra_model, obj_den, 'minimize', constraints) + if status_min == "infeasible": + return YoptSolution("infeasible", float('nan'), {}, False) + status_max, max_D, _ = _highs_fba_optimize(cobra_model, obj_den, 'maximize', constraints) + + achievable_signs = [] + if status_min == "unbounded" or (status_min == "optimal" and min_D < -denominator_tol): + achievable_signs.append(-1.0) + if status_max == "unbounded" or (status_max == "optimal" and max_D > denominator_tol): + achievable_signs.append(1.0) + + if not achievable_signs: + # obj_den . v is forced to 0 everywhere on the feasible region + return YoptSolution("infeasible", float('nan'), {}, False) + + # -- solve the LFP for every achievable sign, keep the better result -- + best = None + for s in achievable_signs: + lp, _ = build_highs_yopt_model(cobra_model, num_vec, den_vec, s, sense, constraints) + h = highspy.Highs() + h.passModel(lp) + h.setOptionValue("output_flag", False) + h.run() + status = h.getModelStatus() + if status == highspy.HighsModelStatus.kOptimal: + x = numpy.asarray(h.getSolution().col_value) + t = x[-1] + if t > denominator_tol: + fluxes = {rid: float(x[i] / t) for i, rid in enumerate(reaction_ids)} + scalable = False + else: + fluxes = {rid: float(x[i]) for i, rid in enumerate(reaction_ids)} + scalable = True + candidate = (h.getInfo().objective_function_value, fluxes, scalable, "optimal") + elif status == highspy.HighsModelStatus.kUnbounded: + candidate = (math.inf if sense == 'maximize' else -math.inf, None, True, "unbounded") + else: + continue + + if best is None or (sense == 'maximize' and candidate[0] > best[0]) or \ + (sense == 'minimize' and candidate[0] < best[0]): + best = candidate + + if best is None: + return YoptSolution("infeasible", float('nan'), {}, False) + + value, fluxes, scalable, status = best + if status == "optimal": + return YoptSolution("optimal", value, fluxes, scalable) + + # -- status == "unbounded": distinguish a genuinely unbounded yield from + # an *undefined* one (denominator can approach, but never reach, 0 + # while the numerator stays nonzero) by re-solving with obj_den . v + # pinned to exactly 0. -- + pinned = list(constraints or []) + [(dict(obj_den), '=', 0.0)] + num_obj_signed = obj_num if sense == 'maximize' else {k: -c for k, c in obj_num.items()} + status_p, val_p, x_p = _highs_fba_optimize(cobra_model, num_obj_signed, 'maximize', pinned) + + if status_p == "unbounded": + # numerator is unbounded even with the denominator held at exactly + # 0 -- a genuinely unbounded ratio. + example = {rid: float(x_p[i]) for i, rid in enumerate(reaction_ids)} if x_p is not None else (fluxes or {}) + return YoptSolution("unbounded", math.inf if sense == 'maximize' else -math.inf, example, True) + + if status_p == "optimal" and abs(val_p) > denominator_tol: + # numerator stays finite and nonzero as the denominator -> 0: the + # ratio is undefined. + example = {rid: float(x_p[i]) for i, rid in enumerate(reaction_ids)} + return YoptSolution("unbounded", float('nan'), example, False) + + # numerator also (forced to, or found at) 0 when denominator == 0 + example = {rid: float(x_p[i]) for i, rid in enumerate(reaction_ids)} if status_p == "optimal" else (fluxes or {}) + return YoptSolution("unbounded", float('nan'), example, False) + +def _build_reaction_adjacency(S: sp.spmatrix, lb: numpy.ndarray, ub: numpy.ndarray, + hub_degree_percentile: float = 95.0, + max_neighbors: int = 12): + """ + fwd_only[i] = lb[i] >= 0 (ordinary irreversible reaction) + bwd_only[i] = ub[i] <= 0 (irreversible *in the reverse* direction) + Reversible reactions have neither flag set and never cause exclusion -- + their flux sign is unconstrained, so they're compatible with anything. + """ + S = S.tocsr() + n_mets, n_rxns = S.shape + if n_mets == 0 or n_rxns == 0: + empty_i = numpy.empty(0, dtype=numpy.int32) + empty_r = numpy.empty(0, dtype=numpy.int8) + return [empty_i] * n_rxns, [empty_r] * n_rxns + + met_degree = numpy.asarray((S != 0).sum(axis=1)).ravel() + hub_cutoff = max(numpy.percentile(met_degree, hub_degree_percentile), 2) + keep = (met_degree <= hub_cutoff).astype(numpy.float64) + keep_diag = sp.diags(keep) + + P = (keep_diag @ (S > 0).astype(numpy.float64)).tocsr() + C = (keep_diag @ (S < 0).astype(numpy.float64)).tocsr() + + OPP = (P.T @ C) + (C.T @ P) # opposite role -> same-direction, IF achievable + SAME = (P.T @ P) + (C.T @ C) # same role -> opposite-direction, IF achievable + + fwd_only = (lb >= 0).astype(numpy.float64) + bwd_only = (ub <= 0).astype(numpy.float64) + D_fwd, D_bwd = sp.diags(fwd_only), sp.diags(bwd_only) + + # OPP needs matched half-lines (both fwd-only or both bwd-only or either + # reversible); degenerate when they're MISMATCHED (one fwd, one bwd) + OPP_excluded = (D_fwd @ OPP @ D_bwd) + (D_bwd @ OPP @ D_fwd) + # SAME needs at least one side free to move opposite; degenerate when + # BOTH are pinned to the same half-line + SAME_excluded = (D_fwd @ SAME @ D_fwd) + (D_bwd @ SAME @ D_bwd) + + SIGNED = (OPP - OPP_excluded) - (SAME - SAME_excluded) + SIGNED = SIGNED.tocsr() + #SIGNED.data = numpy.clip(SIGNED.data, a_min=None, a_max=None) # no-op, keeps dtype tidy + + neighbors, relation = [], [] + for j in range(n_rxns): + row = SIGNED.getrow(j) + idx, val = row.indices, row.data + mask = (idx != j) & (val != 0) + idx, val = idx[mask], val[mask] + if len(idx) == 0: + neighbors.append(numpy.empty(0, dtype=numpy.int32)) + relation.append(numpy.empty(0, dtype=numpy.int8)) + continue + strength = numpy.abs(val) + if len(idx) > max_neighbors: + top = numpy.argpartition(-strength, max_neighbors)[:max_neighbors] + idx, val, strength = idx[top], val[top], strength[top] + order = numpy.argsort(-strength) + neighbors.append(idx[order].astype(numpy.int32)) + relation.append(numpy.sign(val[order]).astype(numpy.int8)) + return neighbors, relation + +class GraphAwareJobQueue: + """ + pending[0, i] : job (i, +1) [lower bound] still open + pending[1, i] : job (i, -1) [upper bound] still open + + Two ways to take work, both under a single lock acquisition each: + - claim_best: given a whole batch of candidate (reaction, coef) pairs + in priority order, find and take the best one that's still open in + ONE vectorized boolean-array check, instead of one lock per + candidate. + - claim_chunk: reserve a *chunk* of arbitrary remaining jobs at once + (size shrinks as the pool drains -- "guided scheduling"), so the + fallback path also amortizes lock overhead over several jobs + instead of paying it per job, while still rebalancing finely near + the tail where mismatches matter most. + """ + def __init__(self, n_rxns, jobs): + self._pending = numpy.zeros((2, n_rxns), dtype=bool) + for i, coef in jobs: + self._pending[0 if coef == 1 else 1, i] = True + self._remaining = len(jobs) + self._lock = th.Lock() + + @staticmethod + def _dir_idx(coefs): + return (numpy.asarray(coefs) == -1).astype(numpy.intp) + + def claim_best(self, reaction_idx: numpy.ndarray, coefs: numpy.ndarray): + if len(reaction_idx) == 0: + return None + d = self._dir_idx(coefs) + with self._lock: + avail = self._pending[d, reaction_idx] + if not avail.any(): + return None + pick = int(numpy.argmax(avail)) + self._pending[d[pick], reaction_idx[pick]] = False + self._remaining -= 1 + return int(reaction_idx[pick]), int(coefs[pick]), pick + + def get_chunk(self, num_threads: int, factor: int = 4): + with self._lock: + if self._remaining == 0: + return numpy.empty(0, dtype=numpy.int32), numpy.empty(0, dtype=numpy.int8) + k = max(1, self._remaining // (num_threads * factor)) + d_idx, r_idx = numpy.nonzero(self._pending) + take = min(k, len(r_idx)) + sel_d, sel_r = d_idx[:take].copy(), r_idx[:take].copy() + coefs = numpy.where(sel_d == 0, 1, -1).astype(numpy.int8) + return sel_r.astype(numpy.int32), coefs + + +class LocalStack: + """Per-thread, lock-free (nothing here is shared) stack of candidate + jobs, backed by preallocated numpy buffers instead of a Python list of + tuples -- pushing a reaction's whole neighbor set is one slice + assignment, not a Python loop of individual appends.""" + + def __init__(self, cap: int = 64): + self.r = numpy.empty(cap, dtype=numpy.int32) + self.c = numpy.empty(cap, dtype=numpy.int8) + self.top = 0 + + def _grow(self, min_cap): + new_cap = max(min_cap, len(self.r) * 2) + self.r = numpy.resize(self.r, new_cap) + self.c = numpy.resize(self.c, new_cap) + + def push_neighbors(self, i, coef, neighbors, relation): + nbrs, rel = neighbors[i], relation[i] + m = len(nbrs) + if m == 0: + return + if self.top + m > len(self.r): + self._grow(self.top + m) + self.r[self.top:self.top+m] = nbrs + self.c[self.top:self.top+m] = coef * rel + self.top += m + + def push_batch(self, r_arr, c_arr): + m = len(r_arr) + if m == 0: + return + if self.top + m > len(self.r): + self._grow(self.top + m) + self.r[self.top:self.top+m] = r_arr + self.c[self.top:self.top+m] = c_arr + self.top += m + + def claim_from(self, queue: GraphAwareJobQueue): + if self.top == 0: + return None + rev_r = self.r[:self.top][::-1] # most-recently-pushed first + rev_c = self.c[:self.top][::-1] + result = queue.claim_best(rev_r, rev_c) + if result is None: + self.top = 0 # everything here is stale, drop it + return None + i, coef, pick_rev = result + real_idx = self.top - 1 - pick_rev + self.top -= 1 + if real_idx != self.top: # swap-remove, O(1) + self.r[real_idx] = self.r[self.top] + self.c[real_idx] = self.c[self.top] + return i, coef + + +class GraphAwareFVAWorker(th.Thread): + """ + Same job semantics as FVAHiGHSworker: (reaction_index, coef), coef==1 + solves for the lower bound, coef==-1 for the upper bound. What changes + is job *selection*: each thread keeps a local stack of "reactions + worth trying next", seeded from the neighbors of whatever it just + solved, instead of pulling an unrelated job off the shared queue. + HiGHS keeps reusing this thread's own `h` instance exactly as before, + so the previous optimum is now likely to already satisfy most of the + next problem's binding constraints. + + Priority for candidates pushed after solving reaction i with sign + `coef` (highest priority is popped first, i.e. pushed last): + 1. same direction, irreversible neighbor -- the case from the + prompt: maximize an irreversible reaction -> maximize an + adjacent irreversible reaction next + 2. same direction, reversible neighbor + 3. opposite direction, any neighbor + If every candidate on the stack is already claimed, it drains and the + thread falls back to job_queue.pop_arbitrary(). + """ + + def __init__(self, job_queue: GraphAwareJobQueue, neighbors: List[numpy.ndarray], + relation, lb, ub, lp_data, num_threads, tolerance): super().__init__() self.job_queue = job_queue - self.job_queue_lock = job_queue_lock + self.neighbors = neighbors + self.relation = relation self.lb = lb self.ub = ub self.lp_data = lp_data + self.num_threads = num_threads self.tolerance = tolerance self.n_bad = 0 def run(self): h = highspy.Highs() + h.setOptionValue("output_flag", False) + # a fresh thread per FVA call and serial (primal) simplex: no HiGHS worker threads needed + h.setOptionValue("threads", 1) h.passModel(self.lp_data) h.setOptionValue("solver", "simplex") - h.setOptionValue("simplex_strategy", 4) # primal simplex, typically fastest for FBA-like problems - h.setOptionValue("output_flag", False) # reduces execution time even though no output was visible before + h.setOptionValue("simplex_strategy", 4) h.setOptionValue("kkt_tolerance", self.tolerance) h.changeObjectiveSense(highspy.ObjSense.kMinimize) - # both min and max jobs use MINIMIZE + + stack = LocalStack() while True: - with self.job_queue_lock: - if not self.job_queue: + job = stack.claim_from(self.job_queue) + if job is None: + r_arr, c_arr = self.job_queue.get_chunk(self.num_threads) + if len(r_arr) == 0: break - i, coef = self.job_queue.pop() + stack.push_batch(r_arr, c_arr) + job = stack.claim_from(self.job_queue) + if job is None: + continue + i, coef = job h.changeColCost(i, coef) h.run() - if h.getModelStatus() == highspy.HighsModelStatus.kOptimal: obj_val = h.getInfo().objective_function_value else: obj_val = float("NaN") self.n_bad += 1 - if coef == -1: self.ub[i] = -obj_val else: self.lb[i] = obj_val h.changeColCost(i, 0.0) + if self.neighbors: + stack.push_neighbors(i, coef, self.neighbors, self.relation) + def multi_threaded_HiGHS_FVA(model: cobra.Model, constraints=None): pre_tol = 1e-9 num_proc = os.cpu_count() if num_proc is None: - num_proc = 2 # unlikely that there are any single-core users + num_proc = 2 elif num_proc > 4: num_proc -= 1 if num_proc > 8: - num_proc -= 1 + num_proc -= 1 num_reac = len(model.reactions) lb = [float('NaN')] * num_reac ub = [float('NaN')] * num_reac - job_queue = [] - lp_data = build_highs_fba_model(model, constraints) + lp_data, S = build_highs_fba_model(model, constraints) + lp_data.col_cost_[:] = 1.0 lp_data.sense_ = highspy.ObjSense.kMaximize h = highspy.Highs() @@ -519,11 +1385,12 @@ def multi_threaded_HiGHS_FVA(model: cobra.Model, constraints=None): raise ValueError(f"Unexpected solver status {h.modelStatusToString(status)} during FVA") solution = h.getSolution() pre_ub = list(solution.col_value) + jobs = [] for i in range(num_reac): - if abs(model.reactions[i].upper_bound- pre_ub[i]) < pre_tol: + if abs(model.reactions[i].upper_bound - pre_ub[i]) < pre_tol: ub[i] = model.reactions[i].upper_bound else: - job_queue.append((i, -1)) + jobs.append((i, -1)) h.changeObjectiveSense(highspy.ObjSense.kMinimize) h.run() @@ -538,15 +1405,22 @@ def multi_threaded_HiGHS_FVA(model: cobra.Model, constraints=None): if abs(model.reactions[i].lower_bound - pre_lb[i]) < pre_tol: lb[i] = model.reactions[i].lower_bound else: - job_queue.append((i, 1)) + jobs.append((i, 1)) lp_data.col_cost_[:] = 0.0 - job_queue_lock = th.Lock() - workers = [None] * num_proc - for p in range(num_proc): - workers[p] = FVAHiGHSworker(job_queue, job_queue_lock, lb, ub, lp_data, model.tolerance) - for i in range(num_proc): - workers[i].start() - for i in range(num_proc): - workers[i].join() - return lb, ub, sum(workers[p].n_bad for p in range(num_proc)) + if len(jobs) >= 2000: + if constraints: + S.resize((len(model.metabolites), num_reac)) + neighbors, relation = _build_reaction_adjacency( + S, numpy.asarray([r.lower_bound for r in model.reactions]), + numpy.asarray([r.upper_bound for r in model.reactions])) + else: + neighbors = relation = None # workers skip push_neighbors, pure chunked scheduling + job_queue = GraphAwareJobQueue(num_reac, jobs) + workers = [GraphAwareFVAWorker(job_queue, neighbors, relation, lb, ub, lp_data, num_proc, + model.tolerance) for _ in range(num_proc)] + for w in workers: + w.start() + for w in workers: + w.join() + return lb, ub, sum(w.n_bad for w in workers) diff --git a/cnapy/core_gui.py b/cnapy/core_gui.py index af87cd0c..423826cb 100644 --- a/cnapy/core_gui.py +++ b/cnapy/core_gui.py @@ -13,7 +13,7 @@ def except_likely_community_model_error() -> None: msgBox = QMessageBox() msgBox.setWindowTitle("Error") msgBox.setText(community_error_text) - msgBox.setIcon(QMessageBox.Warning) + msgBox.setIcon(QMessageBox.Icon.Warning) msgBox.exec() @@ -30,8 +30,10 @@ def has_community_error_substring(string: str) -> bool: def model_optimization_with_exceptions(model: cobra.Model) -> None: try: return model.optimize() - except Exception: + except: exstr = get_last_exception_string() # Check for substrings of Gurobi and CPLEX community edition errors if has_community_error_substring(exstr): except_likely_community_model_error() + else: + raise diff --git a/cnapy/data/escher.min.js b/cnapy/data/escher.min.js index 2ac952df..b10e6d5b 100644 --- a/cnapy/data/escher.min.js +++ b/cnapy/data/escher.min.js @@ -1,4 +1,4 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(function(){try{return require("@jupyter-widgets/base")}catch(t){}}()):"function"==typeof define&&define.amd?define(["@jupyter-widgets/base"],e):"object"==typeof exports?exports.escher=e(function(){try{return require("@jupyter-widgets/base")}catch(t){}}()):t.escher=e(t["@jupyter-widgets/base"])}(window,function(r){return o={},i.m=n=[function(t,e,n){"use strict";n.r(e);function i(t){var e=t+="",n=e.indexOf(":");return 0<=n&&"xmlns"!==(e=t.slice(0,n))&&(t=t.slice(n+1)),o.hasOwnProperty(e)?{space:o[e],local:t}:t}var r="http://www.w3.org/1999/xhtml",o={svg:"http://www.w3.org/2000/svg",xhtml:r,xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"};function a(t){var e=i(t);return(e.local?function(t){return function(){return this.ownerDocument.createElementNS(t.space,t.local)}}:function(n){return function(){var t=this.ownerDocument,e=this.namespaceURI;return e===r&&t.documentElement.namespaceURI===r?t.createElement(n):t.createElementNS(e,n)}})(e)}function s(){}function h(t){return null==t?s:function(){return this.querySelector(t)}}function c(){return[]}function u(t){return null==t?c:function(){return this.querySelectorAll(t)}}function d(t){return function(){return this.matches(t)}}function l(t){return new Array(t.length)}function p(t,e){this.ownerDocument=t.ownerDocument,this.namespaceURI=t.namespaceURI,this._next=null,this._parent=t,this.__data__=e}p.prototype={constructor:p,appendChild:function(t){return this._parent.insertBefore(t,this._next)},insertBefore:function(t,e){return this._parent.insertBefore(t,e)},querySelector:function(t){return this._parent.querySelector(t)},querySelectorAll:function(t){return this._parent.querySelectorAll(t)}};function v(t,e,n,r,i,o){for(var a,s=0,c=e.length,l=o.length;s>8&15|e>>4&240,e>>4&15|240&e,(15&e)<<4|15&e,1):(e=h.exec(t))?_(parseInt(e[1],16)):(e=d.exec(t))?new M(e[1],e[2],e[3],1):(e=f.exec(t))?new M(255*e[1]/100,255*e[2]/100,255*e[3]/100,1):(e=p.exec(t))?v(e[1],e[2],e[3],e[4]):(e=A.exec(t))?v(255*e[1]/100,255*e[2]/100,255*e[3]/100,e[4]):(e=g.exec(t))?E(e[1],e[2]/100,e[3]/100,1):(e=b.exec(t))?E(e[1],e[2]/100,e[3]/100,e[4]):m.hasOwnProperty(t)?_(m[t]):"transparent"===t?new M(NaN,NaN,NaN,0):null}function _(t){return new M(t>>16&255,t>>8&255,255&t,1)}function v(t,e,n,r){return r<=0&&(t=e=n=NaN),new M(t,e,n,r)}function w(t){return t instanceof l||(t=y(t)),t?new M((t=t.rgb()).r,t.g,t.b,t.opacity):new M}function x(t,e,n,r){return 1===arguments.length?w(t):new M(t,e,n,null==r?1:r)}function M(t,e,n,r){this.r=+t,this.g=+e,this.b=+n,this.opacity=+r}function C(t){return((t=Math.max(0,Math.min(255,Math.round(t)||0)))<16?"0":"")+t.toString(16)}function E(t,e,n,r){return r<=0?t=e=n=NaN:n<=0||1<=n?t=e=NaN:e<=0&&(t=NaN),new I(t,e,n,r)}function B(t,e,n,r){return 1===arguments.length?function(t){if(t instanceof I)return new I(t.h,t.s,t.l,t.opacity);if(t instanceof l||(t=y(t)),!t)return new I;if(t instanceof I)return t;var e=(t=t.rgb()).r/255,n=t.g/255,r=t.b/255,i=Math.min(e,n,r),o=Math.max(e,n,r),a=NaN,s=o-i,c=(o+i)/2;return s?(a=e===o?(n-r)/s+6*(no&&(i=r.slice(o,i),s[a]?s[a]+=i:s[++a]=i),(e=e[0])===(n=n[0])?s[a]?s[a]+=n:s[++a]=n:(s[++a]=null,c.push({i:a,x:d(e,n)})),o=p.lastIndex;return oMath.PI?t-2*Math.floor((t+Math.PI)/(2*Math.PI))*Math.PI:t}function b(t){return Math.PI/180*t}function m(t){t.sort(function(t,e){return t-e});var e=Math.floor(t.length/2);return t.length%2==1?t[e]:(t[e-1]+t[e])/2}function y(t){for(var e="",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r=0;r\n\n'+r;var i=new Blob([r],{type:"image/svg+xml"});u(i,t+".svg")},downloadPng:function(e,t){h();var n=(new XMLSerializer).serializeToString(t.node());n='\n\n'+n;var r=document.createElement("canvas"),i=r.getContext("2d"),o=t.node().getBBox(),a=o.width+o.x,s=o.height+o.y;a<1e4&&s<1e4?(r.width=a,r.height=s):r.width>r.height?(r.width=1e4,r.height=s/a*1e4):(r.width=a/s*1e4,r.height=1e4);var c=new Image;c.src="data:image/svg+xml;base64,"+btoa(n),c.onload=function(){i.fillStyle="#FFF",i.fillRect(0,0,r.width,r.height),i.drawImage(c,0,0,r.width,r.height),r.toBlob(function(t){u(t,e+".png")})}},rotate_coords_recursive:function(t,e,n){return t.map(function(t){return A(t,e,n)})},rotate_coords:A,get_angle:function(t){var e=t[1].x-t[0].x,n=t[1].y-t[0].y;return 0==e&&0<=n?Math.PI/2:0==e&&n<0?3*Math.PI/2:0<=e&&0<=n?Math.atan(n/e):0<=e?Math.atan(n/e)+2*Math.PI:Math.atan(n/e)+Math.PI},to_degrees:function(t){return 180*t/Math.PI},angleNorm:g,to_radians:b,to_radians_norm:function(t){return g(b(t))},angle_for_event:function(t,e,n){var r=Math.atan2(e.x-n.x,n.y-e.y);return Math.atan2(e.x-n.x+t.x,n.y-e.y-t.y)-r},distance:function(t,e){return Math.sqrt(Math.pow(e.y-t.y,2)+Math.pow(e.x-t.x,2))},check_undefined:function(n,r){r.forEach(function(t,e){void 0===n[e]&&console.error("Argument is undefined: "+r[e])})},compartmentalize:function(t,e){return t+"_"+e},decompartmentalize:function(t){var e=/(.*)_([a-z0-9]{1,2})$/.exec(t);return null!==e?e.slice(1,3):[t,null]},mean:function(t){return t.reduce(function(t,e){return t+e})/t.length},median:m,quartiles:function(t){t.sort(function(t,e){return t-e});var e=Math.floor(t.length/2);return 1===t.length?[t[0],t[0],t[0]]:t.length%2==1?[m(t.slice(0,e)),t[e],m(t.slice(e+1))]:[m(t.slice(0,e)),(t[e-1]+t[e])/2,m(t.slice(e))]},random_characters:y,generate_map_id:function(){return y(12)},check_for_parent_tag:function(t,e){t instanceof r&&(t=t.node());for(;null!==t.parentNode;)if(void 0!==(t=t.parentNode).tagName&&t.tagName.toLowerCase()===e.toLowerCase())return!0;return!1},name_to_url:function(t,e){null!=e&&(e=e.replace(/^\/|\/$/g,""),t=[e,t].join("/"));return t.replace(/^\/|\/$/g,"")+".json"},get_document:_,get_window:function(t){return _(t).defaultView},d3_transform_catch:function(t){if(-1!==t.indexOf("skew")||-1!==t.indexOf("matrix"))throw new Error("d3_transform_catch does not work with skew or matrix");var e=/translate\s*\(\s*([0-9.-]+)\s*,\s*([0-9.-]+)\s*\)/.exec(t),n=l.isNull(e),r=n?0:Number(e[1]),i=n?0:Number(e[2]),o=/rotate\s*\(\s*([0-9.-]+)\s*\)/.exec(t),a=l.isNull(o)?0:Number(o[1]),s=/scale\s*\(\s*([0-9.-]+)\s*\)/.exec(t),c=l.isNull(s)?0:Number(s[1]);return{translate:[r,i],rotate:a,scale:c}}}},function(t,q,e){(function(K,J){var X;!function(){function n(){}var t="object"==typeof self&&self.self===self&&self||"object"==typeof K&&K.global===K&&K||this||{},e=t._,r=Array.prototype,a=Object.prototype,h="undefined"!=typeof Symbol?Symbol.prototype:null,i=r.push,c=r.slice,d=a.toString,o=a.hasOwnProperty,s=Array.isArray,l=Object.keys,u=Object.create,f=function(t){return t instanceof f?t:this instanceof f?void(this._wrapped=t):new f(t)};q.nodeType?t._=f:(!J.nodeType&&J.exports&&(q=J.exports=f),q._=f),f.VERSION="1.9.1";function p(i,o,t){if(void 0===o)return i;switch(null==t?3:t){case 1:return function(t){return i.call(o,t)};case 3:return function(t,e,n){return i.call(o,t,e,n)};case 4:return function(t,e,n,r){return i.call(o,t,e,n,r)}}return function(){return i.apply(o,arguments)}}function A(t,e,n){return f.iteratee!==g?f.iteratee(t,e):null==t?f.identity:f.isFunction(t)?p(t,e,n):f.isObject(t)&&!f.isArray(t)?f.matcher(t):f.property(t)}var g;f.iteratee=g=function(t,e){return A(t,e,1/0)};function b(i,o){return o=null==o?i.length-1:+o,function(){for(var t=Math.max(arguments.length-o,0),e=Array(t),n=0;n":">",'"':""","'":"'","`":"`"},W=f.invert(Y);f.escape=U(Y),f.unescape=U(W),f.result=function(t,e,n){f.isArray(e)||(e=[e]);var r=e.length;if(!r)return f.isFunction(n)?n.call(t):n;for(var i=0;i/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};function R(t){return"\\"+H[t]}var P=/(.)^/,H={"'":"'","\\":"\\","\r":"r","\n":"n","\u2028":"u2028","\u2029":"u2029"},V=/\\|'|\r|\n|\u2028|\u2029/g;f.template=function(o,t,e){!t&&e&&(t=e),t=f.defaults({},t,f.templateSettings);var n,r=RegExp([(t.escape||P).source,(t.interpolate||P).source,(t.evaluate||P).source].join("|")+"|$","g"),a=0,s="__p+='";o.replace(r,function(t,e,n,r,i){return s+=o.slice(a,i).replace(V,R),a=i+t.length,e?s+="'+\n((__t=("+e+"))==null?'':_.escape(__t))+\n'":n?s+="'+\n((__t=("+n+"))==null?'':__t)+\n'":r&&(s+="';\n"+r+"\n__p+='"),t}),s+="';\n",t.variable||(s="with(obj||{}){\n"+s+"}\n"),s="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+s+"return __p;\n";try{n=new Function(t.variable||"obj","_",s)}catch(t){throw t.source=s,t}function i(t){return n.call(this,t,f)}var c=t.variable||"obj";return i.source="function("+c+"){\n"+s+"}",i},f.chain=function(t){var e=f(t);return e._chain=!0,e};function Z(t,e){return t._chain?f(e).chain():e}f.mixin=function(n){return f.each(f.functions(n),function(t){var e=f[t]=n[t];f.prototype[t]=function(){var t=[this._wrapped];return i.apply(t,arguments),Z(this,e.apply(f,t))}}),f},f.mixin(f),f.each(["pop","push","reverse","shift","sort","splice","unshift"],function(e){var n=r[e];f.prototype[e]=function(){var t=this._wrapped;return n.apply(t,arguments),"shift"!==e&&"splice"!==e||0!==t.length||delete t[0],Z(this,t)}}),f.each(["concat","join","slice"],function(t){var e=r[t];f.prototype[t]=function(){return Z(this,e.apply(this._wrapped,arguments))}}),f.prototype.value=function(){return this._wrapped},f.prototype.valueOf=f.prototype.toJSON=f.prototype.value,f.prototype.toString=function(){return String(this._wrapped)},void 0===(X=function(){return f}.apply(q,[]))||(J.exports=X)}()}).call(this,e(22),e(23)(t))},function(t,e,n){"use strict";n.r(e),n.d(e,"h",function(){return r}),n.d(e,"createElement",function(){return r}),n.d(e,"cloneElement",function(){return o}),n.d(e,"createRef",function(){return x}),n.d(e,"Component",function(){return v}),n.d(e,"render",function(){return w}),n.d(e,"rerender",function(){return f}),n.d(e,"options",function(){return E});var c=function(){},E={},l=[],u=[];function r(t,e){var n,r,i,o,a=u;for(o=arguments.length;2=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;function C(t){return new i(t)}function i(t){if(!(e=r.exec(t)))throw new Error("invalid format: "+t);var e;this.fill=e[1]||" ",this.align=e[2]||">",this.sign=e[3]||"-",this.symbol=e[4]||"",this.zero=!!e[5],this.width=e[6]&&+e[6],this.comma=!!e[7],this.precision=e[8]&&+e[8].slice(1),this.trim=!!e[9],this.type=e[10]||""}C.prototype=i.prototype,i.prototype.toString=function(){return this.fill+this.align+this.sign+this.symbol+(this.zero?"0":"")+(null==this.width?"":Math.max(1,0|this.width))+(this.comma?",":"")+(null==this.precision?"":"."+Math.max(0,0|this.precision))+(this.trim?"~":"")+this.type};function o(t,e){var n=s(t,e);if(!n)return t+"";var r=n[0],i=n[1];return i<0?"0."+new Array(-i).join("0")+r:r.length>i+1?r.slice(0,i+1)+"."+r.slice(i+1):r+new Array(i-r.length+2).join("0")}function l(t){return t}function a(t){var w=t.grouping&&t.thousands?function(s,c){return function(t,e){for(var n=t.length,r=[],i=0,o=s[0],a=0;0e));)o=s[i=(i+1)%s.length];return r.reverse().join(c)}}(t.grouping,t.thousands):l,r=t.currency,x=t.decimal,M=t.numerals?function(e){return function(t){return t.replace(/[0-9]/g,function(t){return e[+t]})}}(t.numerals):l,i=t.percent||"%";function a(t){var l=(t=C(t)).fill,u=t.align,h=t.sign,e=t.symbol,d=t.zero,f=t.width,p=t.comma,A=t.precision,g=t.trim,b=t.type;"n"===b?(p=!0,b="g"):B[b]||(null==A&&(A=12),g=!0,b="g"),(d||"0"===l&&"="===u)&&(d=!0,l="0",u="=");var m="$"===e?r[0]:"#"===e&&/[boxX]/.test(b)?"0"+b.toLowerCase():"",y="$"===e?r[1]:/[%p]/.test(b)?i:"",_=B[b],v=/[defgprs%]/.test(b);function n(t){var e,n,r,i=m,o=y;if("c"===b)o=_(t)+o,t="";else{var a=(t=+t)<0;if(t=_(Math.abs(t),A),g&&(t=function(t){t:for(var e,n=t.length,r=1,i=-1;r>1)+i+t+o+c.slice(s);break;default:t=c+i+t+o}return M(t)}return A=null==A?6:/[gprs]/.test(b)?Math.max(1,Math.min(21,A)):Math.max(0,Math.min(20,A)),n.toString=function(){return t+""},n}return{format:a,formatPrefix:function(t,e){var n=a(((t=C(t)).type="f",t)),r=3*Math.max(-8,Math.min(8,Math.floor(c(e)/3))),i=Math.pow(10,-r),o=I[8+r/3];return function(t){return n(i*t)+o}}}}var E,u,h,d,B={"%":function(t,e){return(100*t).toFixed(e)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:function(t){return Math.round(t).toString(10)},e:function(t,e){return t.toExponential(e)},f:function(t,e){return t.toFixed(e)},g:function(t,e){return t.toPrecision(e)},o:function(t){return Math.round(t).toString(8)},p:function(t,e){return o(100*t,e)},r:o,s:function(t,e){var n=s(t,e);if(!n)return t+"";var r=n[0],i=n[1],o=i-(E=3*Math.max(-8,Math.min(8,Math.floor(i/3))))+1,a=r.length;return o===a?r:an._time&&(r=n._time),(t=n)._next):(e=n._next,n._next=null,t?t._next=e:i=e);o=t,w(r)}(),h=0}}function v(){var t=f.now(),e=t-u;lB)throw new Error("too late; already scheduled");return n}function D(t,e){var n=T(t,e);if(n.state>I)throw new Error("too late; already running");return n}function T(t,e){var n=t.__transition;if(!n||!(n=n[e]))throw new Error("transition not found");return n}function N(t,e){var n,r,i,o=t.__transition,a=!0;if(o){for(i in e=null==e?null:e+"",o)(n=o[i]).name===e?(r=2=i?s=!0:10===(e=r.charCodeAt(o++))?c=!0:13===e&&(c=!0,10===r.charCodeAt(o)&&++o),r.slice(n+1,t-1).replace(/""/g,'"')}for(;o=t},hasAtLeast:function(t){return this.queue.length>=t},flatten:!1}),e(D,I),t(D.prototype,{consume:function(){var t=this.queue;return this.queue=[],{value:function(){return t}}},push:function(t){return this.queue.push(t.value())},hasAtLeast:function(){return!0}}),I.isTrigger=function(t){return(null!=t?t._isSource:void 0)?t.sync:null!=t?t._isEventStream:void 0},I.fromObservable=function(t){return(null!=t?t._isSource:void 0)?t:(null!=t?t._isProperty:void 0)?new I(t,!1):new k(t,!0)},t(T.prototype,{_isDesc:!0,deps:function(){return this.cached||(this.cached=Q([this.context].concat(this.args))),this.cached},toString:function(){return m.toString(this.context)+"."+m.toString(this.method)+"("+m.map(m.toString,this.args)+")"}});function N(t,e){var n=t||e;if(n&&n._isDesc)return t||e;for(var r=arguments.length,i=Array(2"},e($,K),$.prototype.isError=function(){return!0},$.prototype.fmap=function(){return this},$.prototype.apply=function(){return this},$.prototype.toString=function(){return" "+m.toString(this.error)},b.Event=K,b.Initial=X,b.Next=J,b.End=q,b.Error=$;function tt(t){return new X(t,!0)}function et(t){return new J(t,!0)}function nt(t){return t&&t._isEvent?t:et(t)}var rt=function(){return new q},it=0,ot=function(){};function at(t){this.desc=t,this.id=++it,this.initialDesc=this.desc}function st(){var t=arguments.length<=0||void 0===arguments[0]?[]:arguments[0];this.unsubscribe=m.bind(this.unsubscribe,this),this.unsubscribed=!1,this.subscriptions=[],this.starting=[];for(var e,n=0;nu?i.push(t):n(t)})}),r.unsubscribe});return n.internalDeps=function(){return h.length?t.concat(h):t},n};function vt(){if(!(this instanceof vt))return new vt;this.unsubAll=m.bind(this.unsubAll,this),this.subscribeAll=m.bind(this.subscribeAll,this),this.guardedSink=m.bind(this.guardedSink,this),this.sink=void 0,this.subscriptions=[],this.ended=!1,lt.call(this,new b.Desc(b,"Bus",[]),this.subscribeAll)}b.Observable.prototype.flatMapWithConcurrencyLimit=function(t){for(var e=arguments.length,n=Array(1=e}))};var Ct=[];ot=function(e){if(Ct.length&&!ot.running)try{ot.running=!0,Ct.forEach(function(t){t(e)})}finally{delete ot.running}};function Et(t){this.observable=t}b.spy=function(t){return Ct.push(t)},b.Property.prototype.startWith=function(t){return S(new b.Desc(this,"startWith",[t]),this.scan(t,function(t,e){return e}))},b.EventStream.prototype.startWith=function(t){return S(new b.Desc(this,"startWith",[t]),b.once(t).concat(this))},b.Observable.prototype.takeWhile=function(t){o(t);for(var e=arguments.length,n=Array(1=o.length)break;c=o[s++]}else{if((s=o.next()).done)break;c=s.value}var l=c;Object.keys(l.options).map(function(t){void 0===i[t]&&(i[t]=n.settings.get(t))})}this.semanticOptions=Object.assign({},i)}m.remove_child_nodes(this.zoom_container.zoomedSel),m.remove_child_nodes(this.mapToolsContainer);var u=this.zoom_container.zoomedSel,h=this.zoom_container.svg;this.map&&this.map.key_manager.toggle(!1),this.map=null!==t?_.default.from_data(t,h,this.embeddedCss,u,this.zoom_container,this.settings,this.cobra_model,this.settings.get("enable_search")):new _.default(h,this.embeddedCss,u,this.zoom_container,this.settings,this.cobra_model,this.settings.get("canvas_size_and_loc"),this.settings.get("enable_search")),this._setupStatus(this.map),this.map.set_status("Loading map ..."),r&&this._updateData(!1,!0),this.build_input=new y.default(this.mapToolsContainer,this.map,this.zoom_container,this.settings),this.text_edit_input=new w.default(this.mapToolsContainer,this.map,this.zoom_container),this.brush=new v.default(u,!1,this.map,".canvas-group"),this.map.canvas.callbackManager.set("resize",function(){"brush"===n.mode&&n.brush.toggle(!0)}),this.setUpSettingsMenu(this.mapToolsContainer),this.setUpButtonPanel(this.mapToolsContainer);var d=this.mapToolsContainer.append("div").attr("class","search-menu-container").append("div").attr("class","search-menu-container-inline");this.setUpMenuBar(d),this.setUpSearchBar(d),this.tooltip_container=new x.default(this.mapToolsContainer,this.settings.get("tooltip_component"),this.zoom_container,this.map,this.settings),this.map.key_manager.assignedKeys=this.getKeys(),this.map.key_manager.inputList=[this.build_input,this.searchBarRef,function(){return n.settingsMenuRef},this.text_edit_input],this.settings.get("enable_keys_with_tooltip")||this.map.key_manager.inputList.push(this.tooltip_container),this.map.key_manager.update(),this.map.key_manager.toggle(this.settings.get("enable_keys")),this.settings.streams.enable_keys.onValue(function(t){n.map.key_manager.toggle(t)});var f=this.settings.get("disabled_buttons")||[];if(this.settings.get("reaction_data")||f.push("Clear reaction data"),this.settings.get("gene_data")||f.push("Clear gene data"),this.settings.get("metabolite_data")||f.push("Clear metabolite data"),this.settings.get("enable_search")||f.push("Find"),this.settings.get("enable_editing")||f.push("Show control points"),this.settings.set("disabled_buttons",f),this.settings.get("zoom_to_element")){var p=this.settings.get("zoom_to_element").type,A=this.settings.get("zoom_to_element").id;if(M.default.isUndefined(p)||-1===["reaction","node"].indexOf(p))throw new Error('zoom_to_element type must be "reaction" or "node"');if(M.default.isUndefined(A))throw new Error("zoom_to_element must include id");"reaction"===p?this.map.zoom_to_reaction(A):"node"===p&&this.map.zoom_to_node(A)}else if(t)this.map.zoom_extent_canvas();else if(this.settings.get("starting_reaction")&&null!==this.cobra_model){var g=this.zoom_container.get_size(),b={x:g.width/2,y:g.height/4};this.map.new_reaction_from_scratch(this.settings.get("starting_reaction"),b,90),this.map.zoom_extent_nodes()}else this.map.zoom_extent_canvas();this.settings.get("enable_editing")?this.zoom_mode():this.view_mode(),this.settings.streams.enable_editing.onValue(function(t){t?n.zoom_mode():n.view_mode()}),this.settings.get("enable_editing")&&this._setupConfirmBeforeExit(),this.map.draw_everything(),this.map.set_status(""),this.callback_manager.run("load_map",null,t,r)},I.prototype.passPropsSettingsMenu=function(t){var e=0e});if(t){var n=!1;M.default.mapObject(t.options,function(t,e){o.settings.get(e)!==t&&(o.settings.set(e,t),n=!0)}),n&&o._updateData(!1,!0)}}}),this.settings.streams.use_3d_transform.onValue(function(t){o.zoom_container.setUse3dTransform(t)}),this.settings.streams.scroll_behavior.onValue(function(t){o.zoom_container.setScrollBehavior(t)}),this.mapToolsContainer=this.selection.append("div").attr("class","map-tools-container"),this._createStatus(this.selection),this.load_model(this.model_data,!1);var c=this.selection.append("div").attr("class","search-menu-container").append("div").attr("class","search-menu-container-inline");this.menu_div=c.append("div"),this.search_bar_div=c.append("div"),this.button_div=this.selection.append("div"),M.default.delay(function(){o.load_map(o.map_data,!1);var t=o._reactionCheckAddAbs();o._updateData(!0,!0),o.settings.statusBus.onValue(function(t){if("accept"===t){if(o._updateData(!0,!0,["reaction","metabolite"],!1),null!==o.zoom_container){var e=o.settings.get("scroll_behavior");o.zoom_container.setScrollBehavior(e)}null!==o.map&&(o.map.draw_all_nodes(!1),o.map.draw_all_reactions(!0,!1),o.map.select_none())}}),null!==t&&setTimeout(t,500),M.default.defer(function(){return o.callback_manager.run("first_load",o)})},50)}e.default=m.class_with_optional_new(B)},function(t,e){ +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(function(){try{return require("@jupyter-widgets/base")}catch(t){}}()):"function"==typeof define&&define.amd?define(["@jupyter-widgets/base"],e):"object"==typeof exports?exports.escher=e(function(){try{return require("@jupyter-widgets/base")}catch(t){}}()):t.escher=e(t["@jupyter-widgets/base"])}(window,function(r){return o={},i.m=n=[function(t,e,n){"use strict";n.r(e);function i(t){var e=t+="",n=e.indexOf(":");return 0<=n&&"xmlns"!==(e=t.slice(0,n))&&(t=t.slice(n+1)),o.hasOwnProperty(e)?{space:o[e],local:t}:t}var r="http://www.w3.org/1999/xhtml",o={svg:"http://www.w3.org/2000/svg",xhtml:r,xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"};function a(t){var e=i(t);return(e.local?function(t){return function(){return this.ownerDocument.createElementNS(t.space,t.local)}}:function(n){return function(){var t=this.ownerDocument,e=this.namespaceURI;return e===r&&t.documentElement.namespaceURI===r?t.createElement(n):t.createElementNS(e,n)}})(e)}function s(){}function h(t){return null==t?s:function(){return this.querySelector(t)}}function c(){return[]}function u(t){return null==t?c:function(){return this.querySelectorAll(t)}}function d(t){return function(){return this.matches(t)}}function l(t){return new Array(t.length)}function p(t,e){this.ownerDocument=t.ownerDocument,this.namespaceURI=t.namespaceURI,this._next=null,this._parent=t,this.__data__=e}p.prototype={constructor:p,appendChild:function(t){return this._parent.insertBefore(t,this._next)},insertBefore:function(t,e){return this._parent.insertBefore(t,e)},querySelector:function(t){return this._parent.querySelector(t)},querySelectorAll:function(t){return this._parent.querySelectorAll(t)}};function v(t,e,n,r,i,o){for(var a,s=0,c=e.length,l=o.length;s>8&15|e>>4&240,e>>4&15|240&e,(15&e)<<4|15&e,1):(e=h.exec(t))?_(parseInt(e[1],16)):(e=d.exec(t))?new M(e[1],e[2],e[3],1):(e=f.exec(t))?new M(255*e[1]/100,255*e[2]/100,255*e[3]/100,1):(e=p.exec(t))?v(e[1],e[2],e[3],e[4]):(e=A.exec(t))?v(255*e[1]/100,255*e[2]/100,255*e[3]/100,e[4]):(e=g.exec(t))?E(e[1],e[2]/100,e[3]/100,1):(e=b.exec(t))?E(e[1],e[2]/100,e[3]/100,e[4]):m.hasOwnProperty(t)?_(m[t]):"transparent"===t?new M(NaN,NaN,NaN,0):null}function _(t){return new M(t>>16&255,t>>8&255,255&t,1)}function v(t,e,n,r){return r<=0&&(t=e=n=NaN),new M(t,e,n,r)}function w(t){return t instanceof l||(t=y(t)),t?new M((t=t.rgb()).r,t.g,t.b,t.opacity):new M}function x(t,e,n,r){return 1===arguments.length?w(t):new M(t,e,n,null==r?1:r)}function M(t,e,n,r){this.r=+t,this.g=+e,this.b=+n,this.opacity=+r}function C(t){return((t=Math.max(0,Math.min(255,Math.round(t)||0)))<16?"0":"")+t.toString(16)}function E(t,e,n,r){return r<=0?t=e=n=NaN:n<=0||1<=n?t=e=NaN:e<=0&&(t=NaN),new I(t,e,n,r)}function B(t,e,n,r){return 1===arguments.length?function(t){if(t instanceof I)return new I(t.h,t.s,t.l,t.opacity);if(t instanceof l||(t=y(t)),!t)return new I;if(t instanceof I)return t;var e=(t=t.rgb()).r/255,n=t.g/255,r=t.b/255,i=Math.min(e,n,r),o=Math.max(e,n,r),a=NaN,s=o-i,c=(o+i)/2;return s?(a=e===o?(n-r)/s+6*(no&&(i=r.slice(o,i),s[a]?s[a]+=i:s[++a]=i),(e=e[0])===(n=n[0])?s[a]?s[a]+=n:s[++a]=n:(s[++a]=null,c.push({i:a,x:d(e,n)})),o=p.lastIndex;return oMath.PI?t-2*Math.floor((t+Math.PI)/(2*Math.PI))*Math.PI:t}function b(t){return Math.PI/180*t}function m(t){t.sort(function(t,e){return t-e});var e=Math.floor(t.length/2);return t.length%2==1?t[e]:(t[e-1]+t[e])/2}function y(t){for(var e="",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r=0;r\n\n'+r;var i=new Blob([r],{type:"image/svg+xml"});u(i,t+".svg")},downloadPng:function(e,t){h();var n=(new XMLSerializer).serializeToString(t.node());n='\n\n'+n;var r=document.createElement("canvas"),i=r.getContext("2d"),o=t.node().getBBox(),a=o.width+o.x,s=o.height+o.y;a<1e4&&s<1e4?(r.width=a,r.height=s):r.width>r.height?(r.width=1e4,r.height=s/a*1e4):(r.width=a/s*1e4,r.height=1e4);var c=new Image;c.src="data:image/svg+xml;base64,"+btoa(n),c.onload=function(){i.fillStyle="#FFF",i.fillRect(0,0,r.width,r.height),i.drawImage(c,0,0,r.width,r.height),r.toBlob(function(t){u(t,e+".png")})}},rotate_coords_recursive:function(t,e,n){return t.map(function(t){return A(t,e,n)})},rotate_coords:A,get_angle:function(t){var e=t[1].x-t[0].x,n=t[1].y-t[0].y;return 0==e&&0<=n?Math.PI/2:0==e&&n<0?3*Math.PI/2:0<=e&&0<=n?Math.atan(n/e):0<=e?Math.atan(n/e)+2*Math.PI:Math.atan(n/e)+Math.PI},to_degrees:function(t){return 180*t/Math.PI},angleNorm:g,to_radians:b,to_radians_norm:function(t){return g(b(t))},angle_for_event:function(t,e,n){var r=Math.atan2(e.x-n.x,n.y-e.y);return Math.atan2(e.x-n.x+t.x,n.y-e.y-t.y)-r},distance:function(t,e){return Math.sqrt(Math.pow(e.y-t.y,2)+Math.pow(e.x-t.x,2))},check_undefined:function(n,r){r.forEach(function(t,e){void 0===n[e]&&console.error("Argument is undefined: "+r[e])})},compartmentalize:function(t,e){return t+"_"+e},decompartmentalize:function(t){var e=/(.*)_([a-z0-9]{1,2})$/.exec(t);return null!==e?e.slice(1,3):[t,null]},mean:function(t){return t.reduce(function(t,e){return t+e})/t.length},median:m,quartiles:function(t){t.sort(function(t,e){return t-e});var e=Math.floor(t.length/2);return 1===t.length?[t[0],t[0],t[0]]:t.length%2==1?[m(t.slice(0,e)),t[e],m(t.slice(e+1))]:[m(t.slice(0,e)),(t[e-1]+t[e])/2,m(t.slice(e))]},random_characters:y,generate_map_id:function(){return y(12)},check_for_parent_tag:function(t,e){t instanceof r&&(t=t.node());for(;null!==t.parentNode;)if(void 0!==(t=t.parentNode).tagName&&t.tagName.toLowerCase()===e.toLowerCase())return!0;return!1},name_to_url:function(t,e){null!=e&&(e=e.replace(/^\/|\/$/g,""),t=[e,t].join("/"));return t.replace(/^\/|\/$/g,"")+".json"},get_document:_,get_window:function(t){return _(t).defaultView},d3_transform_catch:function(t){if(-1!==t.indexOf("skew")||-1!==t.indexOf("matrix"))throw new Error("d3_transform_catch does not work with skew or matrix");var e=/translate\s*\(\s*([0-9.-]+)\s*,\s*([0-9.-]+)\s*\)/.exec(t),n=l.isNull(e),r=n?0:Number(e[1]),i=n?0:Number(e[2]),o=/rotate\s*\(\s*([0-9.-]+)\s*\)/.exec(t),a=l.isNull(o)?0:Number(o[1]),s=/scale\s*\(\s*([0-9.-]+)\s*\)/.exec(t),c=l.isNull(s)?0:Number(s[1]);return{translate:[r,i],rotate:a,scale:c}}}},function(t,q,e){(function(K,J){var X;!function(){function n(){}var t="object"==typeof self&&self.self===self&&self||"object"==typeof K&&K.global===K&&K||this||{},e=t._,r=Array.prototype,a=Object.prototype,h="undefined"!=typeof Symbol?Symbol.prototype:null,i=r.push,c=r.slice,d=a.toString,o=a.hasOwnProperty,s=Array.isArray,l=Object.keys,u=Object.create,f=function(t){return t instanceof f?t:this instanceof f?void(this._wrapped=t):new f(t)};q.nodeType?t._=f:(!J.nodeType&&J.exports&&(q=J.exports=f),q._=f),f.VERSION="1.9.1";function p(i,o,t){if(void 0===o)return i;switch(null==t?3:t){case 1:return function(t){return i.call(o,t)};case 3:return function(t,e,n){return i.call(o,t,e,n)};case 4:return function(t,e,n,r){return i.call(o,t,e,n,r)}}return function(){return i.apply(o,arguments)}}function A(t,e,n){return f.iteratee!==g?f.iteratee(t,e):null==t?f.identity:f.isFunction(t)?p(t,e,n):f.isObject(t)&&!f.isArray(t)?f.matcher(t):f.property(t)}var g;f.iteratee=g=function(t,e){return A(t,e,1/0)};function b(i,o){return o=null==o?i.length-1:+o,function(){for(var t=Math.max(arguments.length-o,0),e=Array(t),n=0;n":">",'"':""","'":"'","`":"`"},W=f.invert(Y);f.escape=U(Y),f.unescape=U(W),f.result=function(t,e,n){f.isArray(e)||(e=[e]);var r=e.length;if(!r)return f.isFunction(n)?n.call(t):n;for(var i=0;i/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};function R(t){return"\\"+H[t]}var P=/(.)^/,H={"'":"'","\\":"\\","\r":"r","\n":"n","\u2028":"u2028","\u2029":"u2029"},V=/\\|'|\r|\n|\u2028|\u2029/g;f.template=function(o,t,e){!t&&e&&(t=e),t=f.defaults({},t,f.templateSettings);var n,r=RegExp([(t.escape||P).source,(t.interpolate||P).source,(t.evaluate||P).source].join("|")+"|$","g"),a=0,s="__p+='";o.replace(r,function(t,e,n,r,i){return s+=o.slice(a,i).replace(V,R),a=i+t.length,e?s+="'+\n((__t=("+e+"))==null?'':_.escape(__t))+\n'":n?s+="'+\n((__t=("+n+"))==null?'':__t)+\n'":r&&(s+="';\n"+r+"\n__p+='"),t}),s+="';\n",t.variable||(s="with(obj||{}){\n"+s+"}\n"),s="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+s+"return __p;\n";try{n=new Function(t.variable||"obj","_",s)}catch(t){throw t.source=s,t}function i(t){return n.call(this,t,f)}var c=t.variable||"obj";return i.source="function("+c+"){\n"+s+"}",i},f.chain=function(t){var e=f(t);return e._chain=!0,e};function Z(t,e){return t._chain?f(e).chain():e}f.mixin=function(n){return f.each(f.functions(n),function(t){var e=f[t]=n[t];f.prototype[t]=function(){var t=[this._wrapped];return i.apply(t,arguments),Z(this,e.apply(f,t))}}),f},f.mixin(f),f.each(["pop","push","reverse","shift","sort","splice","unshift"],function(e){var n=r[e];f.prototype[e]=function(){var t=this._wrapped;return n.apply(t,arguments),"shift"!==e&&"splice"!==e||0!==t.length||delete t[0],Z(this,t)}}),f.each(["concat","join","slice"],function(t){var e=r[t];f.prototype[t]=function(){return Z(this,e.apply(this._wrapped,arguments))}}),f.prototype.value=function(){return this._wrapped},f.prototype.valueOf=f.prototype.toJSON=f.prototype.value,f.prototype.toString=function(){return String(this._wrapped)},void 0===(X=function(){return f}.apply(q,[]))||(J.exports=X)}()}).call(this,e(22),e(23)(t))},function(t,e,n){"use strict";n.r(e),n.d(e,"h",function(){return r}),n.d(e,"createElement",function(){return r}),n.d(e,"cloneElement",function(){return o}),n.d(e,"createRef",function(){return x}),n.d(e,"Component",function(){return v}),n.d(e,"render",function(){return w}),n.d(e,"rerender",function(){return f}),n.d(e,"options",function(){return E});var c=function(){},E={},l=[],u=[];function r(t,e){var n,r,i,o,a=u;for(o=arguments.length;2=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;function C(t){return new i(t)}function i(t){if(!(e=r.exec(t)))throw new Error("invalid format: "+t);var e;this.fill=e[1]||" ",this.align=e[2]||">",this.sign=e[3]||"-",this.symbol=e[4]||"",this.zero=!!e[5],this.width=e[6]&&+e[6],this.comma=!!e[7],this.precision=e[8]&&+e[8].slice(1),this.trim=!!e[9],this.type=e[10]||""}C.prototype=i.prototype,i.prototype.toString=function(){return this.fill+this.align+this.sign+this.symbol+(this.zero?"0":"")+(null==this.width?"":Math.max(1,0|this.width))+(this.comma?",":"")+(null==this.precision?"":"."+Math.max(0,0|this.precision))+(this.trim?"~":"")+this.type};function o(t,e){var n=s(t,e);if(!n)return t+"";var r=n[0],i=n[1];return i<0?"0."+new Array(-i).join("0")+r:r.length>i+1?r.slice(0,i+1)+"."+r.slice(i+1):r+new Array(i-r.length+2).join("0")}function l(t){return t}function a(t){var w=t.grouping&&t.thousands?function(s,c){return function(t,e){for(var n=t.length,r=[],i=0,o=s[0],a=0;0e));)o=s[i=(i+1)%s.length];return r.reverse().join(c)}}(t.grouping,t.thousands):l,r=t.currency,x=t.decimal,M=t.numerals?function(e){return function(t){return t.replace(/[0-9]/g,function(t){return e[+t]})}}(t.numerals):l,i=t.percent||"%";function a(t){var l=(t=C(t)).fill,u=t.align,h=t.sign,e=t.symbol,d=t.zero,f=t.width,p=t.comma,A=t.precision,g=t.trim,b=t.type;"n"===b?(p=!0,b="g"):B[b]||(null==A&&(A=12),g=!0,b="g"),(d||"0"===l&&"="===u)&&(d=!0,l="0",u="=");var m="$"===e?r[0]:"#"===e&&/[boxX]/.test(b)?"0"+b.toLowerCase():"",y="$"===e?r[1]:/[%p]/.test(b)?i:"",_=B[b],v=/[defgprs%]/.test(b);function n(t){var e,n,r,i=m,o=y;if("c"===b)o=_(t)+o,t="";else{var a=(t=+t)<0;if(t=_(Math.abs(t),A),g&&(t=function(t){t:for(var e,n=t.length,r=1,i=-1;r>1)+i+t+o+c.slice(s);break;default:t=c+i+t+o}return M(t)}return A=null==A?6:/[gprs]/.test(b)?Math.max(1,Math.min(21,A)):Math.max(0,Math.min(20,A)),n.toString=function(){return t+""},n}return{format:a,formatPrefix:function(t,e){var n=a(((t=C(t)).type="f",t)),r=3*Math.max(-8,Math.min(8,Math.floor(c(e)/3))),i=Math.pow(10,-r),o=I[8+r/3];return function(t){return n(i*t)+o}}}}var E,u,h,d,B={"%":function(t,e){return(100*t).toFixed(e)},b:function(t){return Math.round(t).toString(2)},c:function(t){return t+""},d:function(t){return Math.round(t).toString(10)},e:function(t,e){return t.toExponential(e)},f:function(t,e){return t.toFixed(e)},g:function(t,e){return t.toPrecision(e)},o:function(t){return Math.round(t).toString(8)},p:function(t,e){return o(100*t,e)},r:o,s:function(t,e){var n=s(t,e);if(!n)return t+"";var r=n[0],i=n[1],o=i-(E=3*Math.max(-8,Math.min(8,Math.floor(i/3))))+1,a=r.length;return o===a?r:an._time&&(r=n._time),(t=n)._next):(e=n._next,n._next=null,t?t._next=e:i=e);o=t,w(r)}(),h=0}}function v(){var t=f.now(),e=t-u;lB)throw new Error("too late; already scheduled");return n}function D(t,e){var n=T(t,e);if(n.state>I)throw new Error("too late; already running");return n}function T(t,e){var n=t.__transition;if(!n||!(n=n[e]))throw new Error("transition not found");return n}function N(t,e){var n,r,i,o=t.__transition,a=!0;if(o){for(i in e=null==e?null:e+"",o)(n=o[i]).name===e?(r=2=i?s=!0:10===(e=r.charCodeAt(o++))?c=!0:13===e&&(c=!0,10===r.charCodeAt(o)&&++o),r.slice(n+1,t-1).replace(/""/g,'"')}for(;o=t},hasAtLeast:function(t){return this.queue.length>=t},flatten:!1}),e(D,I),t(D.prototype,{consume:function(){var t=this.queue;return this.queue=[],{value:function(){return t}}},push:function(t){return this.queue.push(t.value())},hasAtLeast:function(){return!0}}),I.isTrigger=function(t){return(null!=t?t._isSource:void 0)?t.sync:null!=t?t._isEventStream:void 0},I.fromObservable=function(t){return(null!=t?t._isSource:void 0)?t:(null!=t?t._isProperty:void 0)?new I(t,!1):new k(t,!0)},t(T.prototype,{_isDesc:!0,deps:function(){return this.cached||(this.cached=L([this.context].concat(this.args))),this.cached},toString:function(){return m.toString(this.context)+"."+m.toString(this.method)+"("+m.map(m.toString,this.args)+")"}});function N(t,e){var n=t||e;if(n&&n._isDesc)return t||e;for(var r=arguments.length,i=Array(2"},e($,K),$.prototype.isError=function(){return!0},$.prototype.fmap=function(){return this},$.prototype.apply=function(){return this},$.prototype.toString=function(){return" "+m.toString(this.error)},b.Event=K,b.Initial=X,b.Next=J,b.End=q,b.Error=$;function tt(t){return new X(t,!0)}function et(t){return new J(t,!0)}function nt(t){return t&&t._isEvent?t:et(t)}var rt=function(){return new q},it=0,ot=function(){};function at(t){this.desc=t,this.id=++it,this.initialDesc=this.desc}function st(){var t=arguments.length<=0||void 0===arguments[0]?[]:arguments[0];this.unsubscribe=m.bind(this.unsubscribe,this),this.unsubscribed=!1,this.subscriptions=[],this.starting=[];for(var e,n=0;nu?i.push(t):n(t)})}),r.unsubscribe});return n.internalDeps=function(){return h.length?t.concat(h):t},n};function vt(){if(!(this instanceof vt))return new vt;this.unsubAll=m.bind(this.unsubAll,this),this.subscribeAll=m.bind(this.subscribeAll,this),this.guardedSink=m.bind(this.guardedSink,this),this.sink=void 0,this.subscriptions=[],this.ended=!1,lt.call(this,new b.Desc(b,"Bus",[]),this.subscribeAll)}b.Observable.prototype.flatMapWithConcurrencyLimit=function(t){for(var e=arguments.length,n=Array(1=e}))};var Ct=[];ot=function(e){if(Ct.length&&!ot.running)try{ot.running=!0,Ct.forEach(function(t){t(e)})}finally{delete ot.running}};function Et(t){this.observable=t}b.spy=function(t){return Ct.push(t)},b.Property.prototype.startWith=function(t){return S(new b.Desc(this,"startWith",[t]),this.scan(t,function(t,e){return e}))},b.EventStream.prototype.startWith=function(t){return S(new b.Desc(this,"startWith",[t]),b.once(t).concat(this))},b.Observable.prototype.takeWhile=function(t){o(t);for(var e=arguments.length,n=Array(1=o.length)break;c=o[s++]}else{if((s=o.next()).done)break;c=s.value}var l=c;Object.keys(l.options).map(function(t){void 0===i[t]&&(i[t]=n.settings.get(t))})}this.semanticOptions=Object.assign({},i)}m.remove_child_nodes(this.zoom_container.zoomedSel),m.remove_child_nodes(this.mapToolsContainer);var u=this.zoom_container.zoomedSel,h=this.zoom_container.svg;this.map&&this.map.key_manager.toggle(!1),this.map=null!==t?_.default.from_data(t,h,this.embeddedCss,u,this.zoom_container,this.settings,this.cobra_model,this.settings.get("enable_search")):new _.default(h,this.embeddedCss,u,this.zoom_container,this.settings,this.cobra_model,this.settings.get("canvas_size_and_loc"),this.settings.get("enable_search")),this._setupStatus(this.map),this.map.set_status("Loading map ..."),this._updateTooltipSetting(this.settings.get("enable_tooltips")),r&&this._updateData(!1,!0),this.build_input=new y.default(this.mapToolsContainer,this.map,this.zoom_container,this.settings),this.text_edit_input=new w.default(this.mapToolsContainer,this.map,this.zoom_container),this.brush=new v.default(u,!1,this.map,".canvas-group"),this.map.canvas.callbackManager.set("resize",function(){"brush"===n.mode&&n.brush.toggle(!0)}),this.setUpSettingsMenu(this.mapToolsContainer),this.setUpButtonPanel(this.mapToolsContainer);var d=this.mapToolsContainer.append("div").attr("class","search-menu-container").append("div").attr("class","search-menu-container-inline");this.setUpMenuBar(d),this.setUpSearchBar(d),this.tooltip_container=new x.default(this.mapToolsContainer,this.settings.get("tooltip_component"),this.zoom_container,this.map,this.settings),this.map.key_manager.assignedKeys=this.getKeys(),this.map.key_manager.inputList=[this.build_input,this.searchBarRef,function(){return n.settingsMenuRef},this.text_edit_input],this.settings.get("enable_keys_with_tooltip")||this.map.key_manager.inputList.push(this.tooltip_container),this.map.key_manager.update(),this.map.key_manager.toggle(this.settings.get("enable_keys")),this.settings.streams.enable_keys.onValue(function(t){n.map.key_manager.toggle(t)});var f=this.settings.get("disabled_buttons")||[];if(this.settings.get("reaction_data")||f.push("Clear reaction data"),this.settings.get("gene_data")||f.push("Clear gene data"),this.settings.get("metabolite_data")||f.push("Clear metabolite data"),this.settings.get("enable_search")||f.push("Find"),this.settings.get("enable_editing")||f.push("Show control points"),this.settings.set("disabled_buttons",f),this.settings.get("zoom_to_element")){var p=this.settings.get("zoom_to_element").type,A=this.settings.get("zoom_to_element").id;if(M.default.isUndefined(p)||-1===["reaction","node"].indexOf(p))throw new Error('zoom_to_element type must be "reaction" or "node"');if(M.default.isUndefined(A))throw new Error("zoom_to_element must include id");"reaction"===p?this.map.zoom_to_reaction(A):"node"===p&&this.map.zoom_to_node(A)}else if(t)this.map.zoom_extent_canvas();else if(this.settings.get("starting_reaction")&&null!==this.cobra_model){var g=this.zoom_container.get_size(),b={x:g.width/2,y:g.height/4};this.map.new_reaction_from_scratch(this.settings.get("starting_reaction"),b,90),this.map.zoom_extent_nodes()}else this.map.zoom_extent_canvas();this.settings.get("enable_editing")?this.zoom_mode():this.view_mode(),this.settings.streams.enable_editing.onValue(function(t){t?n.zoom_mode():n.view_mode()}),this.settings.get("enable_editing")&&this._setupConfirmBeforeExit(),this.map.draw_everything(),this.map.set_status(""),this.callback_manager.run("load_map",null,t,r)},I.prototype.passPropsSettingsMenu=function(t){var e=0e});if(t){var n=!1;M.default.mapObject(t.options,function(t,e){o.settings.get(e)!==t&&(o.settings.set(e,t),n=!0)}),n&&o._updateData(!1,!0)}}}),this.settings.streams.use_3d_transform.onValue(function(t){o.zoom_container.setUse3dTransform(t)}),this.settings.streams.scroll_behavior.onValue(function(t){o.zoom_container.setScrollBehavior(t)}),this.settings.streams.enable_tooltips.onValue(function(t){o._updateTooltipSetting(t)}),this.mapToolsContainer=this.selection.append("div").attr("class","map-tools-container"),this._createStatus(this.selection),this.load_model(this.model_data,!1);var c=this.selection.append("div").attr("class","search-menu-container").append("div").attr("class","search-menu-container-inline");this.menu_div=c.append("div"),this.search_bar_div=c.append("div"),this.button_div=this.selection.append("div"),M.default.delay(function(){o.load_map(o.map_data,!1);var t=o._reactionCheckAddAbs();o._updateData(!0,!0),o.settings.statusBus.onValue(function(t){if("accept"===t){if(o._updateData(!0,!0,["reaction","metabolite"],!1),null!==o.zoom_container){var e=o.settings.get("scroll_behavior");o.zoom_container.setScrollBehavior(e)}null!==o.map&&(o.map.draw_all_nodes(!1),o.map.draw_all_reactions(!0,!1),o.map.select_none())}}),null!==t&&setTimeout(t,500),M.default.defer(function(){return o.callback_manager.run("first_load",o)})},50)}e.default=m.class_with_optional_new(B)},function(t,e){ /** * vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON, CSS and SQL formats. * @@ -44,7 +44,7 @@ * vkbeautify.sqlmin(text);// minify SQL * */ -function p(t){var e=" ";if(isNaN(parseInt(t)))e=t;else switch(t){case 1:e=" ";break;case 2:e=" ";break;case 3:e=" ";break;case 4:e=" ";break;case 5:e=" ";break;case 6:e=" ";break;case 7:e=" ";break;case 8:e=" ";break;case 9:e=" ";break;case 10:e=" ";break;case 11:e=" ";break;case 12:e=" "}for(var n=["\n"],r=0;r<100;r++)n.push(n[r]+e);return n}function n(){this.step=" ",this.shift=p(this.step)}n.prototype.xml=function(t,e){var n=t.replace(/>\s{0,}<").replace(//)||-1/)||-1/)||-1/)?(a+=n[s],i=!1):/^<\w/.exec(n[s-1])&&/^<\/\w/.exec(n[s])&&/^<[\w:\-\.\,]+/.exec(n[s-1])==/^<\/[\w:\-\.\,]+/.exec(n[s])[0].replace("/","")?(a+=n[s],i||o--):-1/)?a=a+=i?n[s]:c[o++]+n[s]:-1/)?a=a+=i?n[s]:c[o]+n[s]:-1/g,"").replace(/[ \r\n\t]{1,}xmlns/g," xmlns")).replace(/>\s{0,}<")},n.prototype.jsonmin=function(t){return"undefined"==typeof JSON?t:JSON.stringify(JSON.parse(t),null,0)},n.prototype.cssmin=function(t,e){return(e?t:t.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"")).replace(/\s{1,}/g," ").replace(/\{\s{1,}/g,"{").replace(/\}\s{1,}/g,"}").replace(/\;\s{1,}/g,";").replace(/\/\*\s{1,}/g,"/*").replace(/\*\/\s{1,}/g,"*/")},n.prototype.sqlmin=function(t){return t.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")")},t.exports=new n},function(t,e){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children||(t.children=[]),Object.defineProperty(t,"loaded",{enumerable:!0,get:function(){return t.l}}),Object.defineProperty(t,"id",{enumerable:!0,get:function(){return t.i}}),t.webpackPolyfill=1),t}},function(t,e){t.exports=function(){throw new Error("define cannot be used indirect")}},function(e,t){(function(t){e.exports=t}).call(this,{})},function(t,e,n){"use strict";e.__esModule=!0;var s=r(n(3)),c=r(n(13)),i=r(n(4)),l=n(0),o=n(91);function r(t){return t&&t.__esModule?t:{default:t}}var a=(u.prototype.setScrollBehavior=function(t){this._scrollBehavior=t,this._updateScroll()},u.prototype.setUse3dTransform=function(t){this._use3dTransform=t},u.prototype.togglePanDrag=function(t){i.default.isUndefined(t)?this._panDragOn=!this._panDragOn:this._panDragOn=t,this._panDragOn?this.zoomedSel.style("cursor","grab"):i.default.contains(["grab","grabbing"],this.zoomedSel.style("cursor"))&&this.zoomedSel.style("cursor",null),this._updateScroll()},u.prototype._updateScroll=function(){var r=this;if(!i.default.contains(["zoom","pan","none"],this._scrollBehavior))throw Error("Bad value for scroll_behavior: "+this._scrollBehavior);if(this.container.on("mousewheel.zoom",null).on("DOMMouseScroll.zoom",null).on("wheel.zoom",null).on("dblclick.zoom",null).on("mousewheel.escher",null).on("DOMMouseScroll.escher",null).on("wheel.escher",null).on("mousedown.zoom",null).on("touchstart.zoom",null).on("touchmove.zoom",null).on("touchend.zoom",null),this._zoomBehavior=(0,o.zoom)().on("start",function(){l.event.sourceEvent&&"mousedown"===l.event.sourceEvent.type&&r.zoomedSel.style("cursor","grabbing"),null!==l.event.sourceEvent&&(l.event.sourceEvent.stopPropagation(),l.event.sourceEvent.preventDefault())}).on("zoom",function(){r._goToCallback(l.event.transform.k,{x:l.event.transform.x,y:l.event.transform.y})}).on("end",function(){l.event.sourceEvent&&"mouseup"===l.event.sourceEvent.type&&r.zoomedSel.style("cursor","grab")}),this.container.call(this._zoomBehavior),this.container.on("dblclick.zoom",null),this._panDragOn||this.container.on("mousedown.zoom",null).on("touchstart.zoom",null).on("touchmove.zoom",null).on("touchend.zoom",null),"zoom"!==this._scrollBehavior&&this.container.on("mousewheel.zoom",null).on("DOMMouseScroll.zoom",null).on("wheel.zoom",null),"pan"===this._scrollBehavior){var t=function(){var t=l.event;function e(t,e){return.5*(i.default.isUndefined(t)?e:-t/1.5)}t.stopPropagation(),t.preventDefault(),t.returnValue=!1;var n={x:r.windowTranslate.x-e(t.wheelDeltaX,t.deltaX),y:r.windowTranslate.y-e(t.wheelDeltaY,t.deltaY)};r.goTo(r.windowScale,n)};this.container.on("mousewheel.escher",t),this.container.on("DOMMouseScroll.escher",t),this.container.on("wheel.escher",t)}this.goTo(this.windowScale,this.windowTranslate)},u.prototype.goTo=function(t,e){if(t)if(e&&"x"in e&&"y"in e&&!i.default.isNaN(e.x)&&!i.default.isNaN(e.y)){var n=o.zoomIdentity.translate(e.x,e.y).scale(t);this.container.call(this._zoomBehavior.transform,n)}else console.error("Bad translate value");else console.error("Bad scale value")},u.prototype._goToCallback=function(t,e){var n=this;this.windowScale!==t&&(this.windowScale=t,this.callbackManager.run("zoom_change")),this.windowTranslate=e,this._use3dTransform?(i.default.isNull(this._zoomTimeout)||clearTimeout(this._zoomTimeout),this._goTo3d(t,e,this._svgScale,this._svgTranslate),this._zoomTimeout=i.default.delay(function(){n._goToSvg(t,e)},100)):this._goToSvg(t,e),this.callbackManager.run("go_to")},u.prototype._goTo3d=function(t,e,n,r){var i=t/n,o=s.default.c_minus_c(e,s.default.c_times_scalar(r,i)),a="translate("+o.x+"px,"+o.y+"px) scale("+i+")";this.css3TransformContainer.style("transform",a),this.css3TransformContainer.style("-webkit-transform",a),this.css3TransformContainer.style("transform-origin","0 0"),this.css3TransformContainer.style("-webkit-transform-origin","0 0")},u.prototype._clear3d=function(){this.css3TransformContainer.style("transform",null),this.css3TransformContainer.style("-webkit-transform",null),this.css3TransformContainer.style("transform-origin",null),this.css3TransformContainer.style("-webkit-transform-origin",null)},u.prototype._goToSvg=function(t,e,n){var r=this;this.callbackManager.run("svg_start"),i.default.defer(function(){r._clear3d(),r.zoomedSel.attr("transform","translate("+e.x+","+e.y+") scale("+t+")"),r._svgScale=t,r._svgTranslate=e,i.default.defer(function(){r.callbackManager.run("svg_finish"),i.default.isUndefined(n)||n()})})},u.prototype.zoomBy=function(t){var e=this.get_size(),n={x:e.width/2-((e.width/2-this.windowTranslate.x)*t+this.windowTranslate.x),y:e.height/2-((e.height/2-this.windowTranslate.y)*t+this.windowTranslate.y)};this.goTo(this.windowScale*t,s.default.c_plus_c(this.windowTranslate,n))},u.prototype.zoom_in=function(){this.zoomBy(1.5)},u.prototype.zoom_out=function(){this.zoomBy(.667)},u.prototype.get_size=function(){var t=this.selection.node().getBoundingClientRect();return{width:t.width,height:t.height}},u.prototype.translateOffScreen=function(t){var e=this.get_size(),n={min:-this.windowTranslate.x/this.windowScale+120/this.windowScale,max:-this.windowTranslate.x/this.windowScale+(e.width-120)/this.windowScale},r={min:-this.windowTranslate.y/this.windowScale+120/this.windowScale,max:-this.windowTranslate.y/this.windowScale+(e.height-120)/this.windowScale};t.xn.max&&(this.windowTranslate.x=this.windowTranslate.x-(t.x-n.max)*this.windowScale,this.goTo(this.windowScale,this.windowTranslate)),t.yr.max&&(this.windowTranslate.y=this.windowTranslate.y-(t.y-r.max)*this.windowScale,this.goTo(this.windowScale,this.windowTranslate))},u);function u(t,e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,u),t.classed("escher-container",!0),navigator&&navigator.userAgent&&-1!==navigator.userAgent.indexOf("Safari")&&-1===navigator.userAgent.indexOf("Mozilla")&&t.on("touchstart touchmove",function(){l.event.stopPropagation()});var r=t.append("div").attr("class","escher-zoom-container"),i=r.append("div").attr("class","escher-3d-transform-container"),o=i.append("svg").attr("class","escher-svg").attr("xmlns","http://www.w3.org/2000/svg");o.select(".zoom-g").remove();var a=o.append("g").attr("class","zoom-g");this.selection=t,this.container=r,this.css3TransformContainer=i,this.svg=o,this.zoomedSel=a,this.windowTranslate={x:0,y:0},this.windowScale=1,this._scrollBehavior=e,this._use3dTransform=n,this._panDragOn=!0,this._zoomBehavior=null,this._zoomTimeout=null,this._svgScale=this.windowScale,this._svgTranslate=this.windowTranslate,this.callbackManager=new c.default,this._updateScroll()}e.default=a},function(t,e,n){"use strict";e.__esModule=!0;var o=Object.assign||function(t){for(var e=1;eMath.PI)throw new Error("Angle must be between -PI and PI");return Math.abs(t)t[1]?e:t}var I=A.reduce(B,[0,0])[0],k=g.reduce(B,[0,0])[0];for(var D in p.metabolites){var T=p.metabolites[D];T.coefficient<0?(T.is_primary=T.index===I,T.count=b):(T.is_primary=T.index===k,T.count=m)}var N={},S={};[{node_type:"anchor_reactants",dis:{x:20*(y?1:-1),y:0}},{node_type:"center",dis:{x:0,y:0}},{node_type:"anchor_products",dis:{x:20*(y?-1:1),y:0}}].map(function(t){var e=String(++o.nodes),n="center"===t.node_type?"midmarker":"multimarker";N[e]={node_type:n,x:d.x+t.dis.x,y:d.y+t.dis.y,connected_segments:[],name:null,bigg_id:null,label_x:null,label_y:null,node_is_primary:null,data:null},S[t.node_type]=e}),[[S.anchor_reactants,S.center,"reactants"],[S.anchor_products,S.center,"products"]].map(function(t){var e=t[0],n=t[1],r=String(++o.segments),i=0===b&&"reactants"===t[2]&&p.reversibility||0===m&&"products"===t[2];p.segments[r]={b1:null,b2:null,from_node_id:e,to_node_id:n,from_node_coefficient:null,to_node_coefficient:null,reversibility:p.reversibility,data:p.data,reverse_flux:p.reverse_flux,unconnected_segment_with_arrow:i},N[e].connected_segments.push({segment_id:r,reaction_id:l}),N[n].connected_segments.push({segment_id:r,reaction_id:l})});var Q=N;for(var L in p.metabolites){var z=p.metabolites[L],j=void 0,F=void 0;F=z.coefficient<0?(j=I,S.anchor_reactants):(j=k,S.anchor_products);var O=X(z,j,h,d,350,y);if(i.bigg_id===z.bigg_id){var U=String(++o.segments);p.segments[U]={b1:O.b1,b2:O.b2,from_node_id:F,to_node_id:r,from_node_coefficient:null,to_node_coefficient:z.coefficient,reversibility:p.reversibility},i.connected_segments.push({segment_id:U,reaction_id:l}),Q[F].connected_segments.push({segment_id:U,reaction_id:l})}else{var Y=String(++o.segments),W=String(++o.nodes);p.segments[Y]={b1:O.b1,b2:O.b2,from_node_id:F,to_node_id:W,from_node_coefficient:null,to_node_coefficient:z.coefficient,reversibility:p.reversibility};var G=K(c,z.index,z.count,z.is_primary,z.bigg_id,j);Q[W]={connected_segments:[{segment_id:Y,reaction_id:l}],x:O.circle.x,y:O.circle.y,node_is_primary:z.is_primary,label_x:O.circle.x+G.x,label_y:O.circle.y+G.y,name:z.name,bigg_id:z.bigg_id,node_type:"metabolite"},Q[F].connected_segments.push({segment_id:Y,reaction_id:l})}}var R=[];for(var P in p.metabolites)R.push({bigg_id:P,coefficient:p.metabolites[P].coefficient});p.metabolites=R;var H={};H[l]=p;var V=q(H);return Q[r]=i,J(Q,H,V,c,u),{new_reactions:H,new_beziers:V,new_nodes:Q}},e.rotateNodes=J,e.moveNodeAndDependents=function(t,a,n,s,c){var r=d(t,n,c);return t.connected_segments.map(function(t){var e=n[t.reaction_id];if(!l.default.isUndefined(e)){var i=t.segment_id,o=e.segments[i];[["b1","from_node_id"],["b2","to_node_id"]].forEach(function(t){var e=t[0],n=t[1];if(o[n]===a&&o[e]){o[e]=Z.c_plus_c(o[e],c);var r=s[f(i,e)];r.x=o[e].x,r.y=o[e].y}}),r.reaction_ids.indexOf(t.reaction_id)<0&&r.reaction_ids.push(t.reaction_id)}}),r},e.newTextLabel=function(t,e,n){var r=String(++t.text_labels),i={text:e,x:n.x,y:n.y};return{id:r,label:i}},e.bezierIdForSegmentId=f,e.bezierIdsForReactionIds=function(t){var r=[];for(var e in t){var i=t[e],n=function(e){var n=i.segments[e];["b1","b2"].forEach(function(t){null!==n[t]&&r.push(f(e,t))})};for(var o in i.segments)n(o)}return r},e.newBeziersForSegments=o,e.newBeziersForReactions=q;var r,Z=function(t){{if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}}(n(3)),i=n(4),l=(r=i)&&r.__esModule?r:{default:r};function K(t,e,n,r,i,o){var a=Z.angleNorm(t),s=18*i.length,c=-1<=e-(oe?t.index-1:t.index);var u=[{x:c,y:0},{x:i-c,y:0}],h=void 0,d=void 0,f=void 0,p=void 0;return t.coefficient<0!==o&&t.is_primary?(h={x:u[0].x,y:u[0].y},f={x:.6*r.x+.4*u[0].x,y:.6*r.y+.4*u[0].y},p={x:.25*r.x+.75*h.x,y:.25*r.y+.75*h.y},d={x:n[0].x,y:n[0].y}):t.coefficient<0!==o?(h={x:u[0].x+g(20,50,l,s),y:u[0].y+A(24,l,s)},f={x:.6*r.x+.4*u[0].x,y:.6*r.y+.4*u[0].y},p={x:.25*r.x+.75*h.x,y:.25*r.y+.75*h.y},d={x:n[0].x+g(20,50,l,s),y:n[0].y+A(80,l,s)}):0":".","?":"/","|":"\\"},s={option:"alt",command:"meta",return:"enter",escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},e=1;e<20;++e)o[111+e]="f"+e;for(e=0;e<=9;++e)o[e+96]=e.toString();w.prototype.bind=function(t,e,n){return t=t instanceof Array?t:[t],this._bindMultiple.call(this,t,e,n),this},w.prototype.unbind=function(t,e){return this.bind.call(this,t,function(){},e)},w.prototype.trigger=function(t,e){return this._directMap[t+":"+e]&&this._directMap[t+":"+e]({},t),this},w.prototype.reset=function(){return this._callbacks={},this._directMap={},this},w.prototype.stopCallback=function(t,e){if(-1<(" "+e.className+" ").indexOf(" mousetrap "))return!1;if(function t(e,n){return null!==e&&e!==r&&(e===n||t(e.parentNode,n))}(e,this.target))return!1;if("composedPath"in t&&"function"==typeof t.composedPath){var n=t.composedPath()[0];n!==t.target&&(e=n)}return"INPUT"==e.tagName||"SELECT"==e.tagName||"TEXTAREA"==e.tagName||e.isContentEditable},w.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)},w.addKeycodes=function(t){for(var e in t)t.hasOwnProperty(e)&&(o[e]=t[e]);i=null},w.init=function(){var e=w(r);for(var t in e)"_"!==t.charAt(0)&&(w[t]=function(t){return function(){return e[t].apply(e,arguments)}}(t))},w.init(),t.Mousetrap=w,l.exports&&(l.exports=w),void 0===(d=function(){return w}.call(u,h,u,l))||(l.exports=d)}function m(t,e,n){t.addEventListener?t.addEventListener(e,n,!1):t.attachEvent("on"+e,n)}function y(t){if("keypress"!=t.type)return o[t.which]?o[t.which]:n[t.which]?n[t.which]:String.fromCharCode(t.which).toLowerCase();var e=String.fromCharCode(t.which);return t.shiftKey||(e=e.toLowerCase()),e}function _(t){return"shift"==t||"ctrl"==t||"alt"==t||"meta"==t}function c(t,e,n){return"keypress"==(n=n||(function(){if(!i)for(var t in i={},o)95\s{0,}<").replace(//)||-1/)||-1/)||-1/)?(a+=n[s],i=!1):/^<\w/.exec(n[s-1])&&/^<\/\w/.exec(n[s])&&/^<[\w:\-\.\,]+/.exec(n[s-1])==/^<\/[\w:\-\.\,]+/.exec(n[s])[0].replace("/","")?(a+=n[s],i||o--):-1/)?a=a+=i?n[s]:c[o++]+n[s]:-1/)?a=a+=i?n[s]:c[o]+n[s]:-1/g,"").replace(/[ \r\n\t]{1,}xmlns/g," xmlns")).replace(/>\s{0,}<")},n.prototype.jsonmin=function(t){return"undefined"==typeof JSON?t:JSON.stringify(JSON.parse(t),null,0)},n.prototype.cssmin=function(t,e){return(e?t:t.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"")).replace(/\s{1,}/g," ").replace(/\{\s{1,}/g,"{").replace(/\}\s{1,}/g,"}").replace(/\;\s{1,}/g,";").replace(/\/\*\s{1,}/g,"/*").replace(/\*\/\s{1,}/g,"*/")},n.prototype.sqlmin=function(t){return t.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")")},t.exports=new n},function(t,e){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children||(t.children=[]),Object.defineProperty(t,"loaded",{enumerable:!0,get:function(){return t.l}}),Object.defineProperty(t,"id",{enumerable:!0,get:function(){return t.i}}),t.webpackPolyfill=1),t}},function(t,e){t.exports=function(){throw new Error("define cannot be used indirect")}},function(e,t){(function(t){e.exports=t}).call(this,{})},function(t,e,n){"use strict";e.__esModule=!0;var s=r(n(3)),c=r(n(13)),i=r(n(4)),l=n(0),o=n(91);function r(t){return t&&t.__esModule?t:{default:t}}var a=(u.prototype.setScrollBehavior=function(t){this._scrollBehavior=t,this._updateScroll()},u.prototype.setUse3dTransform=function(t){this._use3dTransform=t},u.prototype.togglePanDrag=function(t){i.default.isUndefined(t)?this._panDragOn=!this._panDragOn:this._panDragOn=t,this._panDragOn?this.zoomedSel.style("cursor","grab"):"grab"===this.zoomedSel.style("cursor")&&this.zoomedSel.style("cursor",null),this._updateScroll()},u.prototype._updateScroll=function(){var r=this;if(!i.default.contains(["zoom","pan","none"],this._scrollBehavior))throw Error("Bad value for scroll_behavior: "+this._scrollBehavior);if(this.container.on("mousewheel.zoom",null).on("DOMMouseScroll.zoom",null).on("wheel.zoom",null).on("dblclick.zoom",null).on("mousewheel.escher",null).on("DOMMouseScroll.escher",null).on("wheel.escher",null).on("mousedown.zoom",null).on("touchstart.zoom",null).on("touchmove.zoom",null).on("touchend.zoom",null),this._zoomBehavior=(0,o.zoom)().on("start",function(){null!==l.event.sourceEvent&&(l.event.sourceEvent.stopPropagation(),l.event.sourceEvent.preventDefault())}).on("zoom",function(){r._goToCallback(l.event.transform.k,{x:l.event.transform.x,y:l.event.transform.y})}),this.container.call(this._zoomBehavior),this.container.on("dblclick.zoom",null),this._panDragOn||this.container.on("mousedown.zoom",null).on("touchstart.zoom",null).on("touchmove.zoom",null).on("touchend.zoom",null),"zoom"!==this._scrollBehavior&&this.container.on("mousewheel.zoom",null).on("DOMMouseScroll.zoom",null).on("wheel.zoom",null),"pan"===this._scrollBehavior){var t=function(){var t=l.event;function e(t,e){return.5*(i.default.isUndefined(t)?e:-t/1.5)}t.stopPropagation(),t.preventDefault(),t.returnValue=!1;var n={x:r.windowTranslate.x-e(t.wheelDeltaX,t.deltaX),y:r.windowTranslate.y-e(t.wheelDeltaY,t.deltaY)};r.goTo(r.windowScale,n)};this.container.on("mousewheel.escher",t),this.container.on("DOMMouseScroll.escher",t),this.container.on("wheel.escher",t)}this.goTo(this.windowScale,this.windowTranslate)},u.prototype.goTo=function(t,e){if(t)if(e&&"x"in e&&"y"in e&&!i.default.isNaN(e.x)&&!i.default.isNaN(e.y)){var n=o.zoomIdentity.translate(e.x,e.y).scale(t);this.container.call(this._zoomBehavior.transform,n)}else console.error("Bad translate value");else console.error("Bad scale value")},u.prototype._goToCallback=function(t,e){var n=this;this.windowScale!==t&&(this.windowScale=t,this.callbackManager.run("zoom_change")),this.windowTranslate=e,this._use3dTransform?(i.default.isNull(this._zoomTimeout)||clearTimeout(this._zoomTimeout),this._goTo3d(t,e,this._svgScale,this._svgTranslate),this._zoomTimeout=i.default.delay(function(){n._requestedFrame=!1,n._goToSvg(t,e)},100)):this._goToSvg(t,e),this.callbackManager.run("go_to")},u.prototype._goTo3dFrame=function(){var e=this;this._requestedFrame||(this._requestedFrame=!0,window.requestAnimationFrame(function(){e._requestedFrame=!1;var t=e._3dTransform;t?(e.css3TransformContainer.style("transform",t),e.css3TransformContainer.style("-webkit-transform",t),e.css3TransformContainer.style("transform-origin","0 0"),e.css3TransformContainer.style("-webkit-transform-origin","0 0")):console.warn("No _3dTransform defined")}))},u.prototype._goTo3d=function(t,e,n,r){var i=t/n,o=s.default.c_minus_c(e,s.default.c_times_scalar(r,i)),a="translate("+o.x+"px,"+o.y+"px) scale("+i+")";this._3dTransform=a,this._goTo3dFrame()},u.prototype._clear3d=function(){this._3dTransform&&(this._3dTransform=null,this.css3TransformContainer.style("transform",null),this.css3TransformContainer.style("-webkit-transform",null),this.css3TransformContainer.style("transform-origin",null),this.css3TransformContainer.style("-webkit-transform-origin",null))},u.prototype._goToSvgFrame=function(t){var n=this,r=0n.max&&(this.windowTranslate.x=this.windowTranslate.x-(t.x-n.max)*this.windowScale,this.goTo(this.windowScale,this.windowTranslate)),t.yr.max&&(this.windowTranslate.y=this.windowTranslate.y-(t.y-r.max)*this.windowScale,this.goTo(this.windowScale,this.windowTranslate))},u);function u(t,e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,u),t.classed("escher-container",!0),navigator&&navigator.userAgent&&-1!==navigator.userAgent.indexOf("Safari")&&-1===navigator.userAgent.indexOf("Mozilla")&&t.on("touchstart touchmove",function(){l.event.stopPropagation()});var r=t.append("div").attr("class","escher-zoom-container"),i=r.append("div").attr("class","escher-3d-transform-container"),o=i.append("svg").attr("class","escher-svg").attr("xmlns","http://www.w3.org/2000/svg");o.select(".zoom-g").remove();var a=o.append("g").attr("class","zoom-g");this.selection=t,this.container=r,this.css3TransformContainer=i,this.svg=o,this.zoomedSel=a,this.windowTranslate={x:0,y:0},this.windowScale=1,this._scrollBehavior=e,this._use3dTransform=n,this._panDragOn=!0,this._zoomBehavior=null,this._zoomTimeout=null,this._svgScale=this.windowScale,this._svgTranslate=this.windowTranslate,this._3dTransform=null,this._requestedFrame=!1,this.callbackManager=new c.default,this._updateScroll()}e.default=a},function(t,e,n){"use strict";e.__esModule=!0;var o=Object.assign||function(t){for(var e=1;eMath.PI)throw new Error("Angle must be between -PI and PI");return Math.abs(t)t[1]?e:t}var I=A.reduce(B,[0,0])[0],k=g.reduce(B,[0,0])[0];for(var D in p.metabolites){var T=p.metabolites[D];T.coefficient<0?(T.is_primary=T.index===I,T.count=b):(T.is_primary=T.index===k,T.count=m)}var N={},S={};[{node_type:"anchor_reactants",dis:{x:20*(y?1:-1),y:0}},{node_type:"center",dis:{x:0,y:0}},{node_type:"anchor_products",dis:{x:20*(y?-1:1),y:0}}].map(function(t){var e=String(++o.nodes),n="center"===t.node_type?"midmarker":"multimarker";N[e]={node_type:n,x:d.x+t.dis.x,y:d.y+t.dis.y,connected_segments:[],name:null,bigg_id:null,label_x:null,label_y:null,node_is_primary:null,data:null},S[t.node_type]=e}),[[S.anchor_reactants,S.center,"reactants"],[S.anchor_products,S.center,"products"]].map(function(t){var e=t[0],n=t[1],r=String(++o.segments),i=0===b&&"reactants"===t[2]&&p.reversibility||0===m&&"products"===t[2];p.segments[r]={b1:null,b2:null,from_node_id:e,to_node_id:n,from_node_coefficient:null,to_node_coefficient:null,reversibility:p.reversibility,data:p.data,reverse_flux:p.reverse_flux,unconnected_segment_with_arrow:i},N[e].connected_segments.push({segment_id:r,reaction_id:l}),N[n].connected_segments.push({segment_id:r,reaction_id:l})});var L=N;for(var Q in p.metabolites){var z=p.metabolites[Q],j=void 0,F=void 0;F=z.coefficient<0?(j=I,S.anchor_reactants):(j=k,S.anchor_products);var O=X(z,j,h,d,350,y);if(i.bigg_id===z.bigg_id){var U=String(++o.segments);p.segments[U]={b1:O.b1,b2:O.b2,from_node_id:F,to_node_id:r,from_node_coefficient:null,to_node_coefficient:z.coefficient,reversibility:p.reversibility},i.connected_segments.push({segment_id:U,reaction_id:l}),L[F].connected_segments.push({segment_id:U,reaction_id:l})}else{var Y=String(++o.segments),W=String(++o.nodes);p.segments[Y]={b1:O.b1,b2:O.b2,from_node_id:F,to_node_id:W,from_node_coefficient:null,to_node_coefficient:z.coefficient,reversibility:p.reversibility};var G=K(c,z.index,z.count,z.is_primary,z.bigg_id,j);L[W]={connected_segments:[{segment_id:Y,reaction_id:l}],x:O.circle.x,y:O.circle.y,node_is_primary:z.is_primary,label_x:O.circle.x+G.x,label_y:O.circle.y+G.y,name:z.name,bigg_id:z.bigg_id,node_type:"metabolite"},L[F].connected_segments.push({segment_id:Y,reaction_id:l})}}var R=[];for(var P in p.metabolites)R.push({bigg_id:P,coefficient:p.metabolites[P].coefficient});p.metabolites=R;var H={};H[l]=p;var V=q(H);return L[r]=i,J(L,H,V,c,u),{new_reactions:H,new_beziers:V,new_nodes:L}},e.rotateNodes=J,e.moveNodeAndDependents=function(t,a,n,s,c){var r=d(t,n,c);return t.connected_segments.map(function(t){var e=n[t.reaction_id];if(!l.default.isUndefined(e)){var i=t.segment_id,o=e.segments[i];[["b1","from_node_id"],["b2","to_node_id"]].forEach(function(t){var e=t[0],n=t[1];if(o[n]===a&&o[e]){o[e]=Z.c_plus_c(o[e],c);var r=s[f(i,e)];r.x=o[e].x,r.y=o[e].y}}),r.reaction_ids.indexOf(t.reaction_id)<0&&r.reaction_ids.push(t.reaction_id)}}),r},e.newTextLabel=function(t,e,n){var r=String(++t.text_labels),i={text:e,x:n.x,y:n.y};return{id:r,label:i}},e.bezierIdForSegmentId=f,e.bezierIdsForReactionIds=function(t){var r=[];for(var e in t){var i=t[e],n=function(e){var n=i.segments[e];["b1","b2"].forEach(function(t){null!==n[t]&&r.push(f(e,t))})};for(var o in i.segments)n(o)}return r},e.newBeziersForSegments=o,e.newBeziersForReactions=q;var r,Z=function(t){{if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}}(n(3)),i=n(4),l=(r=i)&&r.__esModule?r:{default:r};function K(t,e,n,r,i,o){var a=Z.angleNorm(t),s=18*i.length,c=-1<=e-(oe?t.index-1:t.index);var u=[{x:c,y:0},{x:i-c,y:0}],h=void 0,d=void 0,f=void 0,p=void 0;return t.coefficient<0!==o&&t.is_primary?(h={x:u[0].x,y:u[0].y},f={x:.6*r.x+.4*u[0].x,y:.6*r.y+.4*u[0].y},p={x:.25*r.x+.75*h.x,y:.25*r.y+.75*h.y},d={x:n[0].x,y:n[0].y}):t.coefficient<0!==o?(h={x:u[0].x+g(20,50,l,s),y:u[0].y+A(24,l,s)},f={x:.6*r.x+.4*u[0].x,y:.6*r.y+.4*u[0].y},p={x:.25*r.x+.75*h.x,y:.25*r.y+.75*h.y},d={x:n[0].x+g(20,50,l,s),y:n[0].y+A(80,l,s)}):0":".","?":"/","|":"\\"},s={option:"alt",command:"meta",return:"enter",escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},e=1;e<20;++e)o[111+e]="f"+e;for(e=0;e<=9;++e)o[e+96]=e.toString();w.prototype.bind=function(t,e,n){return t=t instanceof Array?t:[t],this._bindMultiple.call(this,t,e,n),this},w.prototype.unbind=function(t,e){return this.bind.call(this,t,function(){},e)},w.prototype.trigger=function(t,e){return this._directMap[t+":"+e]&&this._directMap[t+":"+e]({},t),this},w.prototype.reset=function(){return this._callbacks={},this._directMap={},this},w.prototype.stopCallback=function(t,e){if(-1<(" "+e.className+" ").indexOf(" mousetrap "))return!1;if(function t(e,n){return null!==e&&e!==r&&(e===n||t(e.parentNode,n))}(e,this.target))return!1;if("composedPath"in t&&"function"==typeof t.composedPath){var n=t.composedPath()[0];n!==t.target&&(e=n)}return"INPUT"==e.tagName||"SELECT"==e.tagName||"TEXTAREA"==e.tagName||e.isContentEditable},w.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)},w.addKeycodes=function(t){for(var e in t)t.hasOwnProperty(e)&&(o[e]=t[e]);i=null},w.init=function(){var e=w(r);for(var t in e)"_"!==t.charAt(0)&&(w[t]=function(t){return function(){return e[t].apply(e,arguments)}}(t))},w.init(),t.Mousetrap=w,l.exports&&(l.exports=w),void 0===(d=function(){return w}.call(u,h,u,l))||(l.exports=d)}function m(t,e,n){t.addEventListener?t.addEventListener(e,n,!1):t.attachEvent("on"+e,n)}function y(t){if("keypress"!=t.type)return o[t.which]?o[t.which]:n[t.which]?n[t.which]:String.fromCharCode(t.which).toLowerCase();var e=String.fromCharCode(t.which);return t.shiftKey||(e=e.toLowerCase()),e}function _(t){return"shift"==t||"ctrl"==t||"alt"==t||"meta"==t}function c(t,e,n){return"keypress"==(n=n||(function(){if(!i)for(var t in i={},o)95"+m+": "+g.data_string,matches:E,id:A}):c.push({html:""+m+"\t"+(t=C,o=[f],t.replace(new RegExp("(^| )("+o.join("|")+")($| )","g"),"$1$2$3")),matches:E,id:A}),p[A]=!0}}var B=d?function(t,e){return Math.abs(t.reaction_data)>Math.abs(e.reaction_data)?-1:1}:function(t,e){return t.html.toLowerCase()=x.display_limit){var l=h.createElement("div");l.innerHTML=" "+(t.length-p.length)+" more",p.push(l),f.appendChild(l);break}}}0!==p.length&&(g.highlight(0),3*o/g,">"),b.getBoundingClientRect().right}(m)+"px",y.refresh(i,x.options)}};!function(e,n){a=e.value;function t(){var t=e.value;a!==t&&n(a=t)}e.addEventListener("input",t,!1),e.addEventListener("keyup",t,!1),e.addEventListener("change",t,!1)}(s,function(t){x.onChange(t),x.repaint()});return s.addEventListener("keydown",function(t){var e=(t=t||d.event).keyCode;if(33!=e&&34!=e)if(39!=e&&35!=e&&9!=e)if(13!=e){var n;if(40==e)return""==(n=y.move(1))&&x.onArrowDown(),void(c.value=x.get_hint(n));if(38==e)return""==(n=y.move(-1))&&x.onArrowUp(),c.value=x.get_hint(n),t.preventDefault(),void t.stopPropagation();c.value=""}else{var r=y.get_current_row().id;x.onEnter(r)}else if(9==e&&(t.preventDefault(),t.stopPropagation(),0==c.value.length&&x.onTab()),0=e.length&&this.setState({current:0}),n=this.state.current+1+"/"+e.length;var r=e[this.state.current];if("reaction"===r.type)this.props.map.zoom_to_reaction(r.reaction_id),this.props.map.highlight_reaction(r.reaction_id);else if("metabolite"===r.type)this.props.map.zoom_to_node(r.node_id),this.props.map.highlight_node(r.node_id);else{if("text_label"!==r.type)throw new Error("Bad search index data type: "+r.type);this.props.map.zoom_to_text_label(r.text_label_id),this.props.map.highlight_text_label(r.text_label_id)}}else this.props.map.highlight(null);this.setState({searchItem:t,current:0,counter:n,results:e})},l.prototype.dropDuplicates=function(t){var n={metabolite:{type:"m",key:"node_id"},reaction:{type:"r",key:"reaction_id"},text_label:{type:"t",key:"text_label_id"}};return a.default.uniq(t,function(t){var e=n[t.type];return""+e.type+t[e.key]})},l.prototype.next=function(){this.state.results&&0li {\n margin-top: 5px;\n display: block;\n\n /* these sometimes get overridden */\n margin-left: 0 !important;\n}\n\n.escher-container .grouping {\n display: block;\n}\n\n.escher-container .buttonGroup {\n display: block;\n margin-bottom: -1px;\n padding: 5px 0px;\n border-radius: 0;\n}\n\n.escher-container .grouping>.buttonGroup:first-child {\n border-top-left-radius: 4px;\n border-top-right-radius: 4px;\n}\n\n.escher-container .grouping>.buttonGroup:last-child {\n border-bottom-left-radius: 4px;\n border-bottom-right-radius: 4px;\n}\n\n.escher-container .button-panel>.grouping:last-child {\n margin-top: 4px;\n}\n\n.escher-container #currentMode,\n.escher-container .active-button {\n background-image: linear-gradient(#8F4F3F,#834c3c 6%,#8d3a2d) !important;\n}\n\n.escher-container .buttonGroup.btn {\n margin-top: -1px;\n}\n\n.escher-container .button {\n border-radius: 4px;\n}\n\n.escher-container .button.btn, .escher-container .buttonGroup.btn {\n padding: unset;\n color: white!important;\n border: 1px solid #474949;\n background-image: linear-gradient(#4F5151, #474949 6%, #3F4141);\n background-color: white;\n text-align: center;\n vertical-align: middle;\n cursor: pointer;\n font-size: 14px!important;\n font-weight: 400;\n width: 40px;\n height: 40px;\n}\n\n.escher-container .button-panel .button:active, .escher-container .buttonGroup label:active, .escher-container .button-panel .buttonGroup:active {\n background-image: linear-gradient(#3F4141, #474949 6%, #4F5151);\n}\n\n.escher-container .button-panel .fa {\n font-size: 24px;\n}\n\n/* Icons */\n.escher-container .button-panel [class^='icon-'] {\n font-size: 23px;\n}\n",""])},function(t,e,n){"use strict";e.__esModule=!0;var a=i(n(13)),r=i(n(17)),s=i(n(36)),d=i(n(4));function i(t){return t&&t.__esModule?t:{default:t}}var o=(c.prototype.disableTooltips=function(){this.settings.set("enable_tooltips",!1),this.hide(),this.map.set_status("Tooltips disabled. You can enable them again in the\n settings menu.",3e3)},c.prototype.passProps=function(t){var e=0o.width&&(a.x=-(l-o.width)/i),u>o.height-74&&(a.y=-(u-o.height+77)/i)):(i*s+r.x+.5*n.width>o.width?a.x=-n.width/i:l>o.width&&(a.x=-(l-o.width)/i),i*c+r.y+.5*n.height>o.height-45?a.y=-n.height/i:u>o.height-45&&(a.y=-(u-o.height+47)/i));var h={x:s+a.x,y:c+10+a.y};this.placedDiv.place(h),this.passProps({display:!0,biggId:e.bigg_id,name:e.name,loc:h,data:e.data_string,type:t.replace("_label","").replace("node","metabolite").replace("_object","")})},c.prototype.hide=function(){this.placedDiv.hide(),this.currentTooltip=null},c.prototype.delayHide=function(){var t=this;this.delayHideTimeout=setTimeout(function(){return t.hide()},100)},c.prototype.cancelHideTooltip=function(){null!==this.delayHideTimeout&&clearTimeout(this.delayHideTimeout)},c);function c(t,e,n,r,i){var o=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,c),this.div=t.append("div").attr("id","tooltip-container"),this.tooltipRef=null,this.zoomContainer=n,this.setUpZoomCallbacks(n),this.callbackManager=new a.default,this.div.on("mouseover",this.cancelHideTooltip.bind(this)),this.div.on("mouseleave",this.hide.bind(this)),this.map=r,this.setUpMapCallbacks(r),this.settings=i,this.delay_hide_timeout=null,this.currentTooltip=null,(0,s.default)(e,null,function(t){return o.callbackManager.set("pass_props",t)},this.div.node(),function(t){o.tooltipRef=t}),this.passProps({display:!1,disableTooltips:function(){return o.disableTooltips()}})}e.default=o},function(t,e,n){"use strict";e.__esModule=!0;var r=n(5);n(75);var i=function(t){{if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}}(n(3));var o,a=(function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(s,o=r.Component),s.prototype.decompartmentalizeCheck=function(t,e){return"metabolite"===e?i.decompartmentalize(t)[0]:t},s.prototype.openBigg=function(){var t=this.props.type,e=this.props.biggId,n="http://bigg.ucsd.edu/",r="gene"===t?n+"search?query="+e:n+"universal/"+t+"s/"+this.decompartmentalizeCheck(e,t);window.open(r)},s.prototype.capitalizeFirstLetter=function(t){return"string"==typeof t?t.charAt(0).toUpperCase()+t.slice(1):console.warn("capitalizeFirstLetter was passed something other than a string")},s.prototype.render=function(){var t="Open "+this.decompartmentalizeCheck(this.props.biggId,this.props.type)+" in BiGG Models.";return(0,r.h)("div",{className:"default-tooltip"},(0,r.h)("div",{className:"id"},this.props.biggId),(0,r.h)("div",{className:"name"},"name: ",this.props.name),(0,r.h)("div",{className:"data"},"data: ",this.props.data&&"(nd)"!==this.props.data?this.props.data:"no data"),(0,r.h)("button",{onClick:this.openBigg},t),(0,r.h)("div",{className:"top-right"},(0,r.h)("div",{className:"type-label"},this.capitalizeFirstLetter(this.props.type)),(0,r.h)("a",{onClick:this.props.disableTooltips},"Disable Tooltips")))},s);function s(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,s);var t=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,o.call(this));return t.openBigg=t.openBigg.bind(t),t}e.default=a},function(t,e,n){var r=n(76);"string"==typeof r&&(r=[[t.i,r,""]]);var i={hmr:!0,transform:void 0,insertInto:void 0};n(10)(r,i);r.locals&&(t.exports=r.locals)},function(t,e,n){(t.exports=n(9)(!1)).push([t.i,".escher-container .default-tooltip {\n box-sizing: border-box;\n min-width: 300px;\n min-height: 100px;\n border-radius: 2px;\n border: 1px solid #b58787;\n padding: 7px;\n background-color: #fff;\n text-align: left;\n font-size: 16px;\n font-family: sans-serif;\n color: #111;\n box-shadow: 4px 6px 20px 0px rgba(0, 0, 0, 0.4);\n}\n\n.escher-container .default-tooltip .id{\n font-size: 18px;\n font-weight: bold;\n}\n\n.escher-container .default-tooltip button {\n border-radius: 3px;\n background-color: #eee;\n border: 1px solid #ddd;\n margin-top: 4px;\n font-size: 16px;\n padding: 3px 6px;\n}\n\n.escher-container .default-tooltip .top-right {\n position: absolute;\n top: 4px;\n right: 4px;\n}\n\n.escher-container .default-tooltip .top-right * {\n float: right;\n text-align: right;\n}\n\n.escher-container .default-tooltip .top-right a {\n font-size: 11px;\n color: #999;\n cursor: pointer;\n}\n.escher-container .default-tooltip .top-right a:hover {\n color: #444;\n text-decoration: underline;\n}\n\n.escher-container .default-tooltip .type-label {\n font-size: 15px;\n color: #d27066;\n background-color: #ffeded;\n border-radius: 2px;\n margin: 0 0 0 10px;\n padding: 0 5px;\n}\n",""])},function(t,e,n){var r=n(78);"string"==typeof r&&(r=[[t.i,r,""]]);var i={hmr:!0,transform:void 0,insertInto:void 0};n(10)(r,i);r.locals&&(t.exports=r.locals)},function(t,e,n){e=t.exports=n(9)(!1);var r=n(79),i=r(n(37)),o=r(n(37)+"#iefix"),a=r(n(80)),s=r(n(81)),c=r(n(82)),l=r(n(83)+"#fontello");e.push([t.i,"@font-face {\n font-family: 'fontello';\n src: url("+i+");\n src: url("+o+") format('embedded-opentype'),\n url("+a+") format('woff2'),\n url("+s+") format('woff'),\n url("+c+") format('truetype'),\n url("+l+") format('svg');\n font-weight: normal;\n font-style: normal;\n}\n/* Chrome hack: SVG is rendered more smooth in Windozze. 100% magic, uncomment if you need it. */\n/* Note, that will break hinting! In other OS-es font will be not as sharp as it could be */\n/*\n@media screen and (-webkit-min-device-pixel-ratio:0) {\n @font-face {\n font-family: 'fontello';\n src: url('../font/fontello.svg?9130959#fontello') format('svg');\n }\n}\n*/\n \n [class^=\"icon-\"]:before, [class*=\" icon-\"]:before {\n font-family: \"fontello\";\n font-style: normal;\n font-weight: normal;\n speak: none;\n \n display: inline-block;\n text-decoration: inherit;\n width: 1em;\n margin-right: .2em;\n text-align: center;\n /* opacity: .8; */\n \n /* For safety - reset parent styles, that can break glyph codes*/\n font-variant: normal;\n text-transform: none;\n \n /* fix buttons height, for twitter bootstrap */\n line-height: 1em;\n \n /* Animation center compensation - margins should be symmetric */\n /* remove if not needed */\n margin-left: .2em;\n \n /* you can be more comfortable with increased icons size */\n /* font-size: 120%; */\n \n /* Font smoothing. That was taken from TWBS */\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n \n /* Uncomment for 3D effect */\n /* text-shadow: 1px 1px 1px rgba(127, 127, 127, 0.3); */\n}\n \n.icon-ok:before { content: '\\e800'; } /* '' */\n.icon-cancel:before { content: '\\e801'; } /* '' */\n.icon-plus:before { content: '\\e802'; } /* '' */\n.icon-down-big:before { content: '\\e803'; } /* '' */\n.icon-left-big:before { content: '\\e804'; } /* '' */\n.icon-right-big:before { content: '\\e805'; } /* '' */\n.icon-up-big:before { content: '\\e806'; } /* '' */\n.icon-left-open:before { content: '\\e807'; } /* '' */\n.icon-right-open:before { content: '\\e808'; } /* '' */\n.icon-wrench:before { content: '\\e809'; } /* '' */\n.icon-resize-full:before { content: '\\e80a'; } /* '' */\n.icon-cw:before { content: '\\e80b'; } /* '' */\n.icon-trash-empty:before { content: '\\e80e'; } /* '' */\n.icon-font:before { content: '\\e80f'; } /* '' */\n.icon-zoom-in:before { content: '\\e810'; } /* '' */\n.icon-zoom-out:before { content: '\\e811'; } /* '' */\n.icon-move:before { content: '\\f047'; } /* '' */\n.icon-resize-full-alt:before { content: '\\f0b2'; } /* '' */\n.icon-blank:before { content: '\\f0c8'; } /* '' */\n.icon-sort-down:before { content: '\\f0dd'; } /* '' */\n.icon-mouse-pointer:before { content: '\\f245'; } /* '' */",""])},function(t,e,n){"use strict";t.exports=function(t,e){return"string"!=typeof t?t:(/^['"].*['"]$/.test(t)&&(t=t.slice(1,-1)),/["'() \t\n]/.test(t)||e?'"'.concat(t.replace(/"/g,'\\"').repl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LTI1bC0yNTAtMjUwcS0xMS0xMS0yNS0xMXQtMjUgMTFsLTI1MCAyNTBxLTExIDEwLTExIDI1dDExIDI1IDI1IDExaDUwMHExNCAwIDI1LTExdDEwLTI1eiIgaG9yaXotYWR2LXg9IjU3MS40IiAvPgoKPGdseXBoIGdseXBoLW5hbWU9Im1vdXNlLXBvaW50ZXIiIHVuaWNvZGU9IiYjeGYyNDU7IiBkPSJNNjMyIDI2OHExOC0xNyA4LTM4LTktMjMtMzMtMjNoLTIxM2wxMTItMjY2cTYtMTMgMC0yN3QtMTktMTlsLTk5LTQycS0xNC02LTI3IDB0LTE5IDE5bC0xMDcgMjUyLTE3NC0xNzRxLTExLTExLTI1LTExLTcgMC0xNCAzLTIyIDEwLTIyIDMzdjgzOXEwIDI0IDIyIDMzIDcgMyAxNCAzIDE1IDAgMjUtMTF6IiBob3Jpei1hZHYteD0iNzE0LjMiIC8+CjwvZm9udD4KPC9kZWZzPgo8L3N2Zz4="},function(t,e,n){var r=n(85);"string"==typeof r&&(r=[[t.i,r,""]]);var i={hmr:!0,transform:void 0,insertInto:void 0};n(10)(r,i);r.locals&&(t.exports=r.locals)},function(t,e,n){(t.exports=n(9)(!1)).push([t.i,'/* Containers */\n\n/* The top level container for an Escher Builder */\n.escher-container {\n font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !important;\n background-color: #F3F3F3;\n text-align: center;\n position: relative;\n font-size: 14px;\n color: #333333;\n}\n\n/* Applied to the body when Escher fills the screen. This stops browser from\nshowing scroll-end animations. */\nhtml.fill-screen {\n height: 100%;\n width: 100%;\n}\nbody.fill-screen {\n margin: 0;\n position: relative;\n overflow: hidden;\n height: 100%;\n width: 100%;\n}\n\n/* Applied to top level container (generally .escher-container) when Escher\nfills the screen. These make sure Escher completely fills the screen, even after\nresizes. */\n.fill-screen-div {\n margin: 0;\n padding: 0;\n position: fixed;\n top: 0px;\n bottom: 0px;\n left: 0px;\n right: 0px;\n width: 100% !important;\n height: 100% !important;\n z-index: 1000;\n}\n\n/* The zoom container classes. */\n.escher-container .escher-zoom-container,\n.escher-container .escher-3d-transform-container,\n.escher-container svg.escher-svg {\n width: 100% !important;\n height: 100% !important;\n overflow: hidden;\n}\n\n/* SVG text should not be selectable */\n.escher-container svg text {\n -webkit-user-select: none;\n -moz-user-select: none;\n -ms-user-select: none;\n user-select: none;\n}\n\n/* Status */\n.escher-container #status {\n position:absolute;\n bottom:10px;\n left: 20px;\n color: red;\n background-color: white;\n font-size: 16px\n}\n\n/* Search & Menu */\n.escher-container .search-menu-container {\n position: absolute;\n width: 100%;\n top: 0px;\n left: 0px;\n margin: 0;\n text-align: center;\n pointer-events: none;\n}\n.escher-container .search-menu-container-inline {\n box-sizing: border-box;\n width: 320px;\n display: inline-block;\n text-align: left;\n pointer-events: auto;\n}\n@media (min-width: 550px) {\n .escher-container .search-menu-container-inline {\n width: 410px;\n }\n}\n\n/* Reaction input */\n.escher-container #rxn-input {\n z-index: 10;\n width: 200px;\n}\n.escher-container .input-close-button {\n position: absolute;\n right: 0px;\n width: 18px;\n bottom: 0px;\n padding: 0px;\n border-width: 0px;\n margin: 0px;\n background: none;\n font-size: 20px;\n font-weight: normal;\n top: -8px;\n}\n.escher-container .input-close-button:hover {\n color: #ff3333;\n font-weight: bold;\n}\n\n/* text edit input */\n.escher-container #text-edit-input input {\n width: 500px;\n border: 1px solid #cccccc;\n font-size: 22px;\n}\n\n.escher-container #tooltip-container {\n -ms-touch-action: none;\n touch-action: none;\n}\n\n/* Buttons */\n.escher-container .btn {\n color: white!important;\n border: 1px solid #2E2F2F;\n background-image: linear-gradient(#4F5151, #474949 6%, #3F4141);\n background-color: white;\n cursor: pointer;\n}\n\n.escher-container .btn:active {\n background-image: linear-gradient(#3F4141, #474949 6%, #4F5151);\n}\n',""])},function(t,e,n){"use strict";n.r(e),e.default="svg.escher-svg #mouse-node {\n fill: none;\n}\nsvg.escher-svg #canvas {\n stroke: #ccc;\n stroke-width: 7px;\n fill: white;\n}\nsvg.escher-svg .resize-rect {\n fill: black;\n opacity: 0;\n stroke: none;\n}\nsvg.escher-svg .label {\n font-family: sans-serif;\n font-style: italic;\n font-weight: bold;\n font-size: 8px;\n fill: black;\n stroke: none;\n text-rendering: optimizelegibility;\n cursor: default;\n}\nsvg.escher-svg .reaction-label {\n font-size: 30px;\n fill: rgb(32, 32, 120);\n text-rendering: optimizelegibility;\n}\nsvg.escher-svg .node-label {\n font-size: 20px;\n}\nsvg.escher-svg .gene-label {\n font-size: 18px;\n fill: rgb(32, 32, 120);\n text-rendering: optimizelegibility;\n cursor: default;\n}\nsvg.escher-svg .text-label .label {\n font-size: 50px;\n}\nsvg.escher-svg .text-label-input {\n font-size: 50px;\n}\nsvg.escher-svg .node-circle {\n stroke-width: 2px;\n}\nsvg.escher-svg .midmarker-circle, svg.escher-svg .multimarker-circle {\n fill: white;\n fill-opacity: 0.2;\n stroke: rgb(50, 50, 50);\n}\nsvg.escher-svg g.selected .node-circle{\n stroke-width: 6px;\n stroke: rgb(20, 113, 199);\n}\nsvg.escher-svg g.selected .label {\n fill: rgb(20, 113, 199);\n}\nsvg.escher-svg .metabolite-circle {\n stroke: rgb(162, 69, 16);\n fill: rgb(224, 134, 91);\n}\nsvg.escher-svg g.selected .metabolite-circle {\n stroke: rgb(5, 2, 0);\n}\nsvg.escher-svg .segment {\n stroke: #334E75;\n stroke-width: 10px;\n fill: none;\n}\nsvg.escher-svg .arrowhead {\n fill: #334E75;\n}\nsvg.escher-svg .stoichiometry-label-rect {\n fill: white;\n opacity: 0.5;\n}\nsvg.escher-svg .stoichiometry-label {\n fill: #334E75;\n font-size: 17px;\n}\nsvg.escher-svg .membrane {\n fill: none;\n stroke: rgb(255, 187, 0);\n}\nsvg.escher-svg .brush .extent {\n fill-opacity: 0.1;\n fill: black;\n stroke: #fff;\n shape-rendering: crispEdges;\n}\nsvg.escher-svg #brush-container .background {\n fill: none;\n}\nsvg.escher-svg .bezier-circle {\n fill: rgb(255,255,255);\n}\nsvg.escher-svg .bezier-circle.b1 {\n stroke: red;\n}\nsvg.escher-svg .bezier-circle.b2 {\n stroke: blue;\n}\nsvg.escher-svg .connect-line{\n stroke: rgb(200,200,200);\n}\nsvg.escher-svg .direction-arrow {\n cursor: default;\n stroke: black;\n stroke-width: 1px;\n fill: white;\n opacity: 0.3;\n}\nsvg.escher-svg .start-reaction-target {\n stroke: rgb(100,100,100);\n fill: none;\n opacity: 0.5;\n}\nsvg.escher-svg .rotation-center-line {\n stroke: red;\n stroke-width: 5px;\n}\nsvg.escher-svg .highlight {\n fill: #D97000;\n text-decoration: underline;\n}\nsvg.escher-svg .node-to-combine {\n stroke-width: 12px !important;\n}\n"},function(t,e,n){"use strict";var o=n(3),c=n(16).json;t.exports=function(t){var i=o.set_options(t,{selection:null,getdatafiles:null,datafiles:null,update_callback:null,target:null});if(null===i.selection)throw new Error("No selection provided for DataMenu");var e=i.selection.select(".data-menu");e.empty()&&(e=i.selection.append("div").attr("class","data-menu"));var n=e.append("form").append("select").attr("class","dropdown-menu");return i.getdatafiles?(i.datafiles&&console.warn("DataMenu: getdatafiles option overrides datafiles"),c(i.getdatafiles,function(t,e){return t?console.warn(t):(r(i.target,e.data,n,i.update_callback,i.selection),null)})):i.datafiles?r(i.target,i.datafiles,n,i.update_callback,i.selection):console.warn("DataMenu: No datafiles given"),{update:s};function r(t,e,n,r,i){n.node().addEventListener("change",function(){a(t,this.value,i,r)},!1);var o=e[0];s(e,n),a(t,o,i,r)}function a(t,e,n,r){o.load_the_file(t,e,function(t,e){if(t)return console.warn(t);i.data=e,r&&r(e)})}function s(t,e){e.selectAll(".menu-option").data(t).enter().append("option").attr("value",function(t){return t}).text(function(t){return t}),e.node().focus()}}},function(t,e,n){"use strict";e.__esModule=!0,e.EscherMapModel=e.EscherMapView=void 0;var r=i(n(20)),o=n(0),a=i(n(4));function i(t){return t&&t.__esModule?t:{default:t}}function s(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function l(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.EscherMapView=null,e.EscherMapModel=null;var u,h,d=void 0;try{d=n(89)}catch(t){}if(d){var f="1.7.0-beta.19",p=["menu","scroll_behavior","use_3d_transform","enable_editing","enable_keys","full_screen_button","reaction_data","metabolite_data","gene_data"],A={reaction_data:"set_reaction_data",metabolite_data:"set_metabolite_data",gene_data:"set_gene_data"},g=(l(y,h=d.DOMWidgetView),y.prototype.render=function(){var i=this;if(!d)throw Error("@jupyter-widgets/base not installed. You must install it to use the jupyter widget");var t=(0,o.select)(this.el).append("div");this.setHeight(t),a.default.defer(function(){i.builder=new r.default(i.getMapData(),i.getModelData(),i.model.get("embedded_css"),t,{first_load_callback:function(r){r.callback_manager.set("clear_map",function(){i.model.set("_loaded_map_json",null),i.model.save_changes()}),r.callback_manager.set("clear_model",function(){i.model.set("_loaded_model_json",null),i.model.save_changes()}),i.model.on("change:height",function(){i.setHeight(t)}),i.model.on("change:_loaded_map_json",function(){r.load_map(i.getMapData())}),i.model.on("change:_loaded_model_json",function(){r.load_model(i.getModelData())}),a.default.mapObject(r.settings.acceptedStreams,function(t,e){if(i.model.keys().includes(e)){var n=i.model.get(e);null!==n?e in A?r[A[e]](n):r.settings.set(e,n):(i.model.set(e,r.settings.get(e)),i.model.save_changes()),i.model.on("change:"+e,function(){var t=i.model.get(e);a.default.isEqual(t,r.settings.get(e))||(e in A?r[A[e]](t):r.settings.set(e,t),p.includes(e)||r.map.draw_everything())})}t.onValue(function(t){a.default.isEqual(t,i.model.get(e))||(i.model.set(e,t),i.model.save_changes())})})}})})},y.prototype.setHeight=function(t){t.style("height",this.model.get("height")+"px")},y.prototype.getMapData=function(){var t=this.model.get("_loaded_map_json");return t?JSON.parse(t):null},y.prototype.getModelData=function(){var t=this.model.get("_loaded_model_json");return t?JSON.parse(t):null},y),b=(l(m,u=d.DOMWidgetModel),m.prototype.defaults=function(){return a.default.extend(u.prototype.defaults.call(this),{_model_name:"EscherMapModel",_view_name:"EscherMapView",_model_module:"escher",_view_module:"escher",_model_module_version:f,_view_module_version:f})},m);e.EscherMapView=g,e.EscherMapModel=b}function m(){return s(this,m),c(this,u.apply(this,arguments))}function y(){return s(this,y),c(this,h.apply(this,arguments))}},function(t,e){if(void 0===r){var n=new Error("Cannot find module '@jupyter-widgets/base'");throw n.code="MODULE_NOT_FOUND",n}t.exports=r},function(t,e,n){"use strict";n.r(e);var s=function(t,e){return t>>1;o(t[i],e)<0?n=1+i:r=i}return n},right:function(t,e,n,r){for(null==n&&(n=0),null==r&&(r=t.length);n>>1;0Math.abs(t[1]-B[1])?f=!0:d=!0),B=t,h=!0,R(),S()}function S(){var t;switch(l=B[0]-E[0],u=B[1]-E[1],g){case H:case P:b&&(l=Math.max(v-e,Math.min(x-o,l)),n=e+l,a=o+l),m&&(u=Math.max(w-r,Math.min(M-s,u)),i=r+u,c=s+u);break;case V:b<0?(l=Math.max(v-e,Math.min(x-e,l)),n=e+l,a=o):0"+m+": "+g.data_string,matches:E,id:A}):c.push({html:""+m+"\t"+(t=C,o=[f],t.replace(new RegExp("(^| )("+o.join("|")+")($| )","g"),"$1$2$3")),matches:E,id:A}),p[A]=!0}}var B=d?function(t,e){return Math.abs(t.reaction_data)>Math.abs(e.reaction_data)?-1:1}:function(t,e){return t.html.toLowerCase()=x.display_limit){var l=h.createElement("div");l.innerHTML=" "+(t.length-p.length)+" more",p.push(l),f.appendChild(l);break}}}0!==p.length&&(g.highlight(0),3*o/g,">"),b.getBoundingClientRect().right}(m)+"px",y.refresh(i,x.options)}};!function(e,n){a=e.value;function t(){var t=e.value;a!==t&&n(a=t)}e.addEventListener("input",t,!1),e.addEventListener("keyup",t,!1),e.addEventListener("change",t,!1)}(s,function(t){x.onChange(t),x.repaint()});return s.addEventListener("keydown",function(t){var e=(t=t||d.event).keyCode;if(33!=e&&34!=e)if(39!=e&&35!=e&&9!=e)if(13!=e){var n;if(40==e)return""==(n=y.move(1))&&x.onArrowDown(),void(c.value=x.get_hint(n));if(38==e)return""==(n=y.move(-1))&&x.onArrowUp(),c.value=x.get_hint(n),t.preventDefault(),void t.stopPropagation();c.value=""}else{var r=y.get_current_row().id;x.onEnter(r)}else if(9==e&&(t.preventDefault(),t.stopPropagation(),0==c.value.length&&x.onTab()),0=e.length&&this.setState({current:0}),n=this.state.current+1+"/"+e.length;var r=e[this.state.current];if("reaction"===r.type)this.props.map.zoom_to_reaction(r.reaction_id),this.props.map.highlight_reaction(r.reaction_id);else if("metabolite"===r.type)this.props.map.zoom_to_node(r.node_id),this.props.map.highlight_node(r.node_id);else{if("text_label"!==r.type)throw new Error("Bad search index data type: "+r.type);this.props.map.zoom_to_text_label(r.text_label_id),this.props.map.highlight_text_label(r.text_label_id)}}else this.props.map.highlight(null);this.setState({searchItem:t,current:0,counter:n,results:e})},l.prototype.dropDuplicates=function(t){var n={metabolite:{type:"m",key:"node_id"},reaction:{type:"r",key:"reaction_id"},text_label:{type:"t",key:"text_label_id"}};return a.default.uniq(t,function(t){var e=n[t.type];return""+e.type+t[e.key]})},l.prototype.next=function(){this.state.results&&0li {\n margin-top: 5px;\n display: block;\n\n /* these sometimes get overridden */\n margin-left: 0 !important;\n}\n\n.escher-container .grouping {\n display: block;\n}\n\n.escher-container .buttonGroup {\n display: block;\n margin-bottom: -1px;\n padding: 5px 0px;\n border-radius: 0;\n}\n\n.escher-container .grouping>.buttonGroup:first-child {\n border-top-left-radius: 4px;\n border-top-right-radius: 4px;\n}\n\n.escher-container .grouping>.buttonGroup:last-child {\n border-bottom-left-radius: 4px;\n border-bottom-right-radius: 4px;\n}\n\n.escher-container .button-panel>.grouping:last-child {\n margin-top: 4px;\n}\n\n.escher-container #currentMode,\n.escher-container .active-button {\n background-image: linear-gradient(#8F4F3F,#834c3c 6%,#8d3a2d) !important;\n}\n\n.escher-container .buttonGroup.btn {\n margin-top: -1px;\n}\n\n.escher-container .button {\n border-radius: 4px;\n}\n\n.escher-container .button.btn, .escher-container .buttonGroup.btn {\n padding: unset;\n color: white!important;\n border: 1px solid #474949;\n background-image: linear-gradient(#4F5151, #474949 6%, #3F4141);\n background-color: white;\n text-align: center;\n vertical-align: middle;\n cursor: pointer;\n font-size: 14px!important;\n font-weight: 400;\n width: 40px;\n height: 40px;\n}\n\n.escher-container .button-panel .button:active, .escher-container .buttonGroup label:active, .escher-container .button-panel .buttonGroup:active {\n background-image: linear-gradient(#3F4141, #474949 6%, #4F5151);\n}\n\n.escher-container .button-panel .fa {\n font-size: 24px;\n}\n\n/* Icons */\n.escher-container .button-panel [class^='icon-'] {\n font-size: 23px;\n}\n",""])},function(t,e,n){"use strict";e.__esModule=!0;var a=i(n(13)),r=i(n(17)),s=i(n(36)),d=i(n(4));function i(t){return t&&t.__esModule?t:{default:t}}var o=(c.prototype.disableTooltips=function(){this.settings.set("enable_tooltips",!1),this.map.draw_everything(),this.hide(),this.map.set_status("Tooltips disabled. You can enable them again in the\n settings menu.",3e3)},c.prototype.passProps=function(t){var e=0o.width&&(a.x=-(l-o.width)/i),u>o.height-74&&(a.y=-(u-o.height+77)/i)):(i*s+r.x+.5*n.width>o.width?a.x=-n.width/i:l>o.width&&(a.x=-(l-o.width)/i),i*c+r.y+.5*n.height>o.height-45?a.y=-n.height/i:u>o.height-45&&(a.y=-(u-o.height+47)/i));var h={x:s+a.x,y:c+10+a.y};this.placedDiv.place(h),this.passProps({display:!0,biggId:e.bigg_id,name:e.name,loc:h,data:e.data_string,type:t.replace("_label","").replace("node","metabolite").replace("_object","")})},c.prototype.hide=function(){this.placedDiv.hide(),this.currentTooltip=null},c.prototype.delayHide=function(){var t=this;this.delayHideTimeout=setTimeout(function(){return t.hide()},100)},c.prototype.cancelHideTooltip=function(){null!==this.delayHideTimeout&&clearTimeout(this.delayHideTimeout)},c);function c(t,e,n,r,i){var o=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,c),this.div=t.append("div").attr("id","tooltip-container"),this.tooltipRef=null,this.zoomContainer=n,this.setUpZoomCallbacks(n),this.callbackManager=new a.default,this.div.on("mouseover",this.cancelHideTooltip.bind(this)),this.div.on("mouseleave",this.hide.bind(this)),this.map=r,this.setUpMapCallbacks(r),this.settings=i,this.delay_hide_timeout=null,this.currentTooltip=null,(0,s.default)(e,null,function(t){return o.callbackManager.set("pass_props",t)},this.div.node(),function(t){o.tooltipRef=t}),this.passProps({display:!1,disableTooltips:function(){return o.disableTooltips()}})}e.default=o},function(t,e,n){"use strict";e.__esModule=!0;var r=n(5);n(75);var i=function(t){{if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}}(n(3));var o,a=(function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(s,o=r.Component),s.prototype.decompartmentalizeCheck=function(t,e){return"metabolite"===e?i.decompartmentalize(t)[0]:t},s.prototype.openBigg=function(){var t=this.props.type,e=this.props.biggId,n="http://bigg.ucsd.edu/",r="gene"===t?n+"search?query="+e:n+"universal/"+t+"s/"+this.decompartmentalizeCheck(e,t);window.open(r)},s.prototype.capitalizeFirstLetter=function(t){return"string"==typeof t?t.charAt(0).toUpperCase()+t.slice(1):console.warn("capitalizeFirstLetter was passed something other than a string")},s.prototype.render=function(){var t="Open "+this.decompartmentalizeCheck(this.props.biggId,this.props.type)+" in BiGG Models.";return(0,r.h)("div",{className:"default-tooltip"},(0,r.h)("div",{className:"id"},this.props.biggId),(0,r.h)("div",{className:"name"},"name: ",this.props.name),(0,r.h)("div",{className:"data"},"data: ",this.props.data&&"(nd)"!==this.props.data?this.props.data:"no data"),(0,r.h)("button",{onClick:this.openBigg},t),(0,r.h)("div",{className:"top-right"},(0,r.h)("div",{className:"type-label"},this.capitalizeFirstLetter(this.props.type)),(0,r.h)("a",{onClick:this.props.disableTooltips},"Disable Tooltips")))},s);function s(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,s);var t=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,o.call(this));return t.openBigg=t.openBigg.bind(t),t}e.default=a},function(t,e,n){var r=n(76);"string"==typeof r&&(r=[[t.i,r,""]]);var i={hmr:!0,transform:void 0,insertInto:void 0};n(10)(r,i);r.locals&&(t.exports=r.locals)},function(t,e,n){(t.exports=n(9)(!1)).push([t.i,".escher-container .default-tooltip {\n box-sizing: border-box;\n min-width: 300px;\n min-height: 100px;\n border-radius: 2px;\n border: 1px solid #b58787;\n padding: 7px;\n background-color: #fff;\n text-align: left;\n font-size: 16px;\n font-family: sans-serif;\n color: #111;\n box-shadow: 4px 6px 20px 0px rgba(0, 0, 0, 0.4);\n}\n\n.escher-container .default-tooltip .id{\n font-size: 18px;\n font-weight: bold;\n}\n\n.escher-container .default-tooltip button {\n border-radius: 3px;\n background-color: #eee;\n border: 1px solid #ddd;\n margin-top: 4px;\n font-size: 16px;\n padding: 3px 6px;\n}\n\n.escher-container .default-tooltip .top-right {\n position: absolute;\n top: 4px;\n right: 4px;\n}\n\n.escher-container .default-tooltip .top-right * {\n float: right;\n text-align: right;\n}\n\n.escher-container .default-tooltip .top-right a {\n font-size: 11px;\n color: #999;\n cursor: pointer;\n}\n.escher-container .default-tooltip .top-right a:hover {\n color: #444;\n text-decoration: underline;\n}\n\n.escher-container .default-tooltip .type-label {\n font-size: 15px;\n color: #d27066;\n background-color: #ffeded;\n border-radius: 2px;\n margin: 0 0 0 10px;\n padding: 0 5px;\n}\n",""])},function(t,e,n){var r=n(78);"string"==typeof r&&(r=[[t.i,r,""]]);var i={hmr:!0,transform:void 0,insertInto:void 0};n(10)(r,i);r.locals&&(t.exports=r.locals)},function(t,e,n){e=t.exports=n(9)(!1);var r=n(79),i=r(n(37)),o=r(n(37)+"#iefix"),a=r(n(80)),s=r(n(81)),c=r(n(82)),l=r(n(83)+"#fontello");e.push([t.i,"@font-face {\n font-family: 'fontello';\n src: url("+i+");\n src: url("+o+") format('embedded-opentype'),\n url("+a+") format('woff2'),\n url("+s+") format('woff'),\n url("+c+") format('truetype'),\n url("+l+") format('svg');\n font-weight: normal;\n font-style: normal;\n}\n/* Chrome hack: SVG is rendered more smooth in Windozze. 100% magic, uncomment if you need it. */\n/* Note, that will break hinting! In other OS-es font will be not as sharp as it could be */\n/*\n@media screen and (-webkit-min-device-pixel-ratio:0) {\n @font-face {\n font-family: 'fontello';\n src: url('../font/fontello.svg?9130959#fontello') format('svg');\n }\n}\n*/\n \n [class^=\"icon-\"]:before, [class*=\" icon-\"]:before {\n font-family: \"fontello\";\n font-style: normal;\n font-weight: normal;\n speak: none;\n \n display: inline-block;\n text-decoration: inherit;\n width: 1em;\n margin-right: .2em;\n text-align: center;\n /* opacity: .8; */\n \n /* For safety - reset parent styles, that can break glyph codes*/\n font-variant: normal;\n text-transform: none;\n \n /* fix buttons height, for twitter bootstrap */\n line-height: 1em;\n \n /* Animation center compensation - margins should be symmetric */\n /* remove if not needed */\n margin-left: .2em;\n \n /* you can be more comfortable with increased icons size */\n /* font-size: 120%; */\n \n /* Font smoothing. That was taken from TWBS */\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n \n /* Uncomment for 3D effect */\n /* text-shadow: 1px 1px 1px rgba(127, 127, 127, 0.3); */\n}\n \n.icon-ok:before { content: '\\e800'; } /* '' */\n.icon-cancel:before { content: '\\e801'; } /* '' */\n.icon-plus:before { content: '\\e802'; } /* '' */\n.icon-down-big:before { content: '\\e803'; } /* '' */\n.icon-left-big:before { content: '\\e804'; } /* '' */\n.icon-right-big:before { content: '\\e805'; } /* '' */\n.icon-up-big:before { content: '\\e806'; } /* '' */\n.icon-left-open:before { content: '\\e807'; } /* '' */\n.icon-right-open:before { content: '\\e808'; } /* '' */\n.icon-wrench:before { content: '\\e809'; } /* '' */\n.icon-resize-full:before { content: '\\e80a'; } /* '' */\n.icon-cw:before { content: '\\e80b'; } /* '' */\n.icon-trash-empty:before { content: '\\e80e'; } /* '' */\n.icon-font:before { content: '\\e80f'; } /* '' */\n.icon-zoom-in:before { content: '\\e810'; } /* '' */\n.icon-zoom-out:before { content: '\\e811'; } /* '' */\n.icon-move:before { content: '\\f047'; } /* '' */\n.icon-resize-full-alt:before { content: '\\f0b2'; } /* '' */\n.icon-blank:before { content: '\\f0c8'; } /* '' */\n.icon-sort-down:before { content: '\\f0dd'; } /* '' */\n.icon-mouse-pointer:before { content: '\\f245'; } /* '' */",""])},function(t,e,n){"use strict";t.exports=function(t,e){return"string"!=typeof t?t:(/^['"].*['"]$/.test(t)&&(t=t.slice(1,-1)),/["'() \t\n]/.test(t)||e?'"'.concat(t.replace(/"/g,'\\"').replace(/\n/g,"\\n"),'"'):t)}},function(t,e){t.exports="data:application/font-woff2;base64,d09GMgABAAAAABKUAA8AAAAAJPQAABI7AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHFQGVgCFOAggCZZwEQgKnxyaVAE2AiQDWAsuAAQgBYVNB4IDDIEGG3UhBdwYumHjAJ55bgvZ/4fjjpUVOJbYEKqpuEgYY4RGw1xkC9qrly7T8mghbhYrHvTL5IwNHzluZQ3s/HCVNusnK5jg8CFWfFBkvTioSxtKCf//2Urv+7+qurp7ehZQRDgCgBHBamXAIATMnJkjPDmm9skdQkgQJAqA+X83/ZvWE3QrSdsBnVBzmECdpkbPg5YESkVhVGbOdk7YHGaVFFLmZRK6vlPazZ+IIE/sz2VGAQQczmWuiClj+qMnysBNTbpZ81pOz0xVSbnUgoMG4ofZ7U+7e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f+TsKUKv3Y02/8KIrATrkq+TV7YvYsAwNfDJ/VuQmBaope1E0rzTqavMvTck2WKe5Qi4YxS7h363N4GKdAIJhL1ICchVCuGYmIHPEZp+EAkk4YC0de/wDQu/ibVK/+4V5uz2vP0flq8967FDr90V4F86CmnFqK3lttmqh33SLoyEmiW6O/jst0Nib7mANNG2SFgkZGy0bLFXusXutUcBFfSXNNzkCopKKpRVqlKtlhq11aNe6IN+GIBBGIJhGEFGHi1PtIoaw1EKJZ/VtI6tytuzgEZ2+jysYN9+UtKFy8xSwnjRhcMncbJmjNpdMma/w5YpIRzo9aWwyq6FTZLtkS9sebojalo+nsXPTEWOGlrhbY6SLRXkuAoqLs8zsbRkeY/XDZWMrUQ+CvLenr3dmdv8EHTSNAf96pvc5Af87371YOEuuwBHQQndcTwQFWruRb0QlH0mjgc0Eg12+2XQ0ZafKlNH2/zR5ltk5LmXQAGIqTo6QNywYLeJll7gREGcnDtQ5BAxJ/xRYDcAAA=="},function(t,e){t.exports="data:application/font-woff;base64,d09GRgABAAAAABXUAA8AAAAAJPQAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABWAAAADsAAABUIIslek9TLzIAAAGUAAAAQwAAAFY+I1MqY21hcAAAAdgAAADTAAACuIda97BjdnQgAAACrAAAABMAAAAgBtX+/mZwZ20AAALAAAAFkAAAC3CKkZBZZ2FzcAAACFAAAAAIAAAACAAAABBnbHlmAAAIWAAACf0AAA+aE9DL/mhlYWQAABJYAAAAMgAAADYPoZfIaGhlYQAAEowAAAAfAAAAJAc7A2hobXR4AAASrAAAAC0AAABYSVz//GxvY2EAABLcAAAALgAAAC4p+yYIbWF4cAAAEwwAAAAgAAAAIAElDApuYW1lAAATLAAAAXcAAALNzJ0dH3Bvc3QAABSkAAAAswAAAQOd6TSHcHJlcAAAFVgAAAB6AAAAhuVBK7x4nGNgZGBg4GIwYLBjYHJx8wlh4MtJLMljkGJgYYAAkDwymzEnMz2RgQPGA8qxgGkOIGaDiAIAJj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MjUtMTF6IiBob3Jpei1hZHYteD0iNzE0LjMiIC8+CjwvZm9udD4KPC9kZWZzPgo8L3N2Zz4="},function(t,e,n){var r=n(85);"string"==typeof r&&(r=[[t.i,r,""]]);var i={hmr:!0,transform:void 0,insertInto:void 0};n(10)(r,i);r.locals&&(t.exports=r.locals)},function(t,e,n){(t.exports=n(9)(!1)).push([t.i,'/* Containers */\n\n/* The top level container for an Escher Builder */\n.escher-container {\n font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !important;\n background-color: #F3F3F3;\n text-align: center;\n position: relative;\n font-size: 14px;\n color: #333333;\n}\n\n/* Applied to the body when Escher fills the screen. This stops browser from\nshowing scroll-end animations. */\nhtml.fill-screen {\n height: 100%;\n width: 100%;\n}\nbody.fill-screen {\n margin: 0;\n position: relative;\n overflow: hidden;\n height: 100%;\n width: 100%;\n}\n\n/* Applied to top level container (generally .escher-container) when Escher\nfills the screen. These make sure Escher completely fills the screen, even after\nresizes. */\n.fill-screen-div {\n margin: 0;\n padding: 0;\n position: fixed;\n top: 0px;\n bottom: 0px;\n left: 0px;\n right: 0px;\n width: 100% !important;\n height: 100% !important;\n z-index: 1000;\n}\n\n/* The zoom container classes. */\n.escher-container .escher-zoom-container,\n.escher-container .escher-3d-transform-container,\n.escher-container svg.escher-svg {\n width: 100% !important;\n height: 100% !important;\n overflow: hidden;\n}\n\n/* SVG text should not be selectable */\n.escher-container svg text {\n -webkit-user-select: none;\n -moz-user-select: none;\n -ms-user-select: none;\n user-select: none;\n}\n\n/* Status */\n.escher-container #status {\n position:absolute;\n bottom:10px;\n left: 20px;\n color: red;\n background-color: white;\n font-size: 16px\n}\n\n/* Search & Menu */\n.escher-container .search-menu-container {\n position: absolute;\n width: 100%;\n top: 0px;\n left: 0px;\n margin: 0;\n text-align: center;\n pointer-events: none;\n}\n.escher-container .search-menu-container-inline {\n box-sizing: border-box;\n width: 320px;\n display: inline-block;\n text-align: left;\n pointer-events: auto;\n}\n@media (min-width: 550px) {\n .escher-container .search-menu-container-inline {\n width: 410px;\n }\n}\n\n/* Reaction input */\n.escher-container #rxn-input {\n z-index: 10;\n width: 200px;\n}\n.escher-container .input-close-button {\n position: absolute;\n right: 0px;\n width: 18px;\n bottom: 0px;\n padding: 0px;\n border-width: 0px;\n margin: 0px;\n background: none;\n font-size: 20px;\n font-weight: normal;\n top: -8px;\n}\n.escher-container .input-close-button:hover {\n color: #ff3333;\n font-weight: bold;\n}\n\n/* text edit input */\n.escher-container #text-edit-input input {\n width: 500px;\n border: 1px solid #cccccc;\n font-size: 22px;\n}\n\n.escher-container #tooltip-container {\n -ms-touch-action: none;\n touch-action: none;\n}\n\n/* Buttons */\n.escher-container .btn {\n color: white!important;\n border: 1px solid #2E2F2F;\n background-image: linear-gradient(#4F5151, #474949 6%, #3F4141);\n background-color: white;\n cursor: pointer;\n}\n\n.escher-container .btn:active {\n background-image: linear-gradient(#3F4141, #474949 6%, #4F5151);\n}\n',""])},function(t,e,n){"use strict";n.r(e),e.default="svg.escher-svg #mouse-node {\n fill: none;\n}\nsvg.escher-svg #canvas {\n stroke: #ccc;\n stroke-width: 7px;\n fill: white;\n}\nsvg.escher-svg .resize-rect {\n fill: black;\n opacity: 0;\n stroke: none;\n}\nsvg.escher-svg .label {\n font-family: sans-serif;\n font-style: italic;\n font-weight: bold;\n font-size: 8px;\n fill: black;\n stroke: none;\n text-rendering: optimizelegibility;\n cursor: default;\n}\nsvg.escher-svg .reaction-label {\n font-size: 30px;\n fill: rgb(32, 32, 120);\n text-rendering: optimizelegibility;\n}\nsvg.escher-svg .node-label {\n font-size: 20px;\n}\nsvg.escher-svg .gene-label {\n font-size: 18px;\n fill: rgb(32, 32, 120);\n text-rendering: optimizelegibility;\n cursor: default;\n}\nsvg.escher-svg .text-label .label {\n font-size: 50px;\n}\nsvg.escher-svg .text-label-input {\n font-size: 50px;\n}\nsvg.escher-svg .node-circle {\n stroke-width: 2px;\n}\nsvg.escher-svg .midmarker-circle, svg.escher-svg .multimarker-circle {\n fill: white;\n fill-opacity: 0.2;\n stroke: rgb(50, 50, 50);\n}\nsvg.escher-svg g.selected .node-circle{\n stroke-width: 6px;\n stroke: rgb(20, 113, 199);\n}\nsvg.escher-svg g.selected .label {\n fill: rgb(20, 113, 199);\n}\nsvg.escher-svg .metabolite-circle {\n stroke: rgb(162, 69, 16);\n fill: rgb(224, 134, 91);\n}\nsvg.escher-svg g.selected .metabolite-circle {\n stroke: rgb(5, 2, 0);\n}\nsvg.escher-svg .segment {\n stroke: #334E75;\n stroke-width: 10px;\n fill: none;\n}\nsvg.escher-svg .arrowhead {\n fill: #334E75;\n}\nsvg.escher-svg .stoichiometry-label-rect {\n fill: white;\n opacity: 0.5;\n}\nsvg.escher-svg .stoichiometry-label {\n fill: #334E75;\n font-size: 17px;\n}\nsvg.escher-svg .membrane {\n fill: none;\n stroke: rgb(255, 187, 0);\n}\nsvg.escher-svg .brush .extent {\n fill-opacity: 0.1;\n fill: black;\n stroke: #fff;\n shape-rendering: crispEdges;\n}\nsvg.escher-svg #brush-container .background {\n fill: none;\n}\nsvg.escher-svg .bezier-circle {\n fill: rgb(255,255,255);\n}\nsvg.escher-svg .bezier-circle.b1 {\n stroke: red;\n}\nsvg.escher-svg .bezier-circle.b2 {\n stroke: blue;\n}\nsvg.escher-svg .connect-line{\n stroke: rgb(200,200,200);\n}\nsvg.escher-svg .direction-arrow {\n cursor: default;\n stroke: black;\n stroke-width: 1px;\n fill: white;\n opacity: 0.3;\n}\nsvg.escher-svg .start-reaction-target {\n stroke: rgb(100,100,100);\n fill: none;\n opacity: 0.5;\n}\nsvg.escher-svg .rotation-center-line {\n stroke: red;\n stroke-width: 5px;\n}\nsvg.escher-svg .highlight {\n fill: #D97000;\n text-decoration: underline;\n}\nsvg.escher-svg .node-to-combine {\n stroke-width: 12px !important;\n}\n"},function(t,e,n){"use strict";var o=n(3),c=n(16).json;t.exports=function(t){var i=o.set_options(t,{selection:null,getdatafiles:null,datafiles:null,update_callback:null,target:null});if(null===i.selection)throw new Error("No selection provided for DataMenu");var e=i.selection.select(".data-menu");e.empty()&&(e=i.selection.append("div").attr("class","data-menu"));var n=e.append("form").append("select").attr("class","dropdown-menu");return i.getdatafiles?(i.datafiles&&console.warn("DataMenu: getdatafiles option overrides datafiles"),c(i.getdatafiles,function(t,e){return t?console.warn(t):(r(i.target,e.data,n,i.update_callback,i.selection),null)})):i.datafiles?r(i.target,i.datafiles,n,i.update_callback,i.selection):console.warn("DataMenu: No datafiles given"),{update:s};function r(t,e,n,r,i){n.node().addEventListener("change",function(){a(t,this.value,i,r)},!1);var o=e[0];s(e,n),a(t,o,i,r)}function a(t,e,n,r){o.load_the_file(t,e,function(t,e){if(t)return console.warn(t);i.data=e,r&&r(e)})}function s(t,e){e.selectAll(".menu-option").data(t).enter().append("option").attr("value",function(t){return t}).text(function(t){return t}),e.node().focus()}}},function(t,e,n){"use strict";e.__esModule=!0,e.EscherMapModel=e.EscherMapView=void 0;var r=i(n(20)),o=n(0),a=i(n(4));function i(t){return t&&t.__esModule?t:{default:t}}function s(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function l(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.EscherMapView=null,e.EscherMapModel=null;var u,h,d=void 0;try{d=n(89)}catch(t){}if(d){var f=["menu","scroll_behavior","use_3d_transform","enable_editing","enable_keys","full_screen_button","reaction_data","metabolite_data","gene_data"],p={reaction_data:"set_reaction_data",metabolite_data:"set_metabolite_data",gene_data:"set_gene_data"},A=(l(m,h=d.DOMWidgetView),m.prototype.render=function(){var i=this;if(!d)throw Error("@jupyter-widgets/base not installed. You must install it to use the jupyter widget");var t=(0,o.select)(this.el).append("div");this.setHeight(t),a.default.defer(function(){i.builder=new r.default(i.getMapData(),i.getModelData(),i.model.get("embedded_css"),t,{first_load_callback:function(r){r.callback_manager.set("clear_map",function(){i.model.set("_loaded_map_json",null),i.model.save_changes()}),r.callback_manager.set("clear_model",function(){i.model.set("_loaded_model_json",null),i.model.save_changes()}),i.model.on("change:height",function(){i.setHeight(t)}),i.model.on("change:_loaded_map_json",function(){r.load_map(i.getMapData())}),i.model.on("change:_loaded_model_json",function(){r.load_model(i.getModelData())}),a.default.mapObject(r.settings.acceptedStreams,function(t,e){if(i.model.keys().includes(e)){var n=i.model.get(e);null!==n?e in p?r[p[e]](n):r.settings.set(e,n):(i.model.set(e,r.settings.get(e)),i.model.save_changes()),i.model.on("change:"+e,function(){var t=i.model.get(e);a.default.isEqual(t,r.settings.get(e))||(e in p?r[p[e]](t):r.settings.set(e,t),f.includes(e)||r.map.draw_everything())})}t.onValue(function(t){a.default.isEqual(t,i.model.get(e))||(i.model.set(e,t),i.model.save_changes())})}),r.map.draw_everything()}})})},m.prototype.setHeight=function(t){t.style("height",this.model.get("height")+"px")},m.prototype.getMapData=function(){var t=this.model.get("_loaded_map_json");return t?JSON.parse(t):null},m.prototype.getModelData=function(){var t=this.model.get("_loaded_model_json");return t?JSON.parse(t):null},m),g=(l(b,u=d.DOMWidgetModel),b.prototype.defaults=function(){return a.default.extend(u.prototype.defaults.call(this),{_model_name:"EscherMapModel",_view_name:"EscherMapView",_model_module:"escher",_view_module:"escher",_model_module_version:"1.7.3",_view_module_version:"1.7.3"})},b);e.EscherMapView=A,e.EscherMapModel=g}function b(){return s(this,b),c(this,u.apply(this,arguments))}function m(){return s(this,m),c(this,h.apply(this,arguments))}},function(t,e){if(void 0===r){var n=new Error("Cannot find module '@jupyter-widgets/base'");throw n.code="MODULE_NOT_FOUND",n}t.exports=r},function(t,e,n){"use strict";n.r(e);var s=function(t,e){return t>>1;o(t[i],e)<0?n=1+i:r=i}return n},right:function(t,e,n,r){for(null==n&&(n=0),null==r&&(r=t.length);n>>1;0Math.abs(t[1]-B[1])?f=!0:d=!0),B=t,h=!0,R(),S()}function S(){var t;switch(l=B[0]-E[0],u=B[1]-E[1],g){case H:case P:b&&(l=Math.max(v-e,Math.min(x-o,l)),n=e+l,a=o+l),m&&(u=Math.max(w-r,Math.min(M-s,u)),i=r+u,c=s+u);break;case V:b<0?(l=Math.max(v-e,Math.min(x-e,l)),n=e+l,a=o):0 + +
diff --git a/cnapy/data/scenario_template.xlsx b/cnapy/data/scenario_template.xlsx new file mode 100644 index 00000000..721a373a Binary files /dev/null and b/cnapy/data/scenario_template.xlsx differ diff --git a/cnapy/data/zoom-fit.svg b/cnapy/data/zoom-fit.svg new file mode 100644 index 00000000..022554f0 --- /dev/null +++ b/cnapy/data/zoom-fit.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/cnapy/gui_elements/about_dialog.py b/cnapy/gui_elements/about_dialog.py index 854d2040..a5c2a13b 100644 --- a/cnapy/gui_elements/about_dialog.py +++ b/cnapy/gui_elements/about_dialog.py @@ -16,24 +16,24 @@ def __init__(self, appdata: AppData): "Version: {version}\ \n\nCNApy is an integrated environment for metabolic modeling.\ \nFor more information visit us at:".format(version=appdata.version)) - self.text1.setAlignment(Qt.AlignCenter) + self.text1.setAlignment(Qt.AlignmentFlag.AlignCenter) self.url1 = QLabel( " https://github.com/cnapy-org/CNApy ") self.url1.setOpenExternalLinks(True) - self.url1.setAlignment(Qt.AlignCenter) + self.url1.setAlignment(Qt.AlignmentFlag.AlignCenter) self.text2 = QLabel( "
If you use CNApy in your scientific work, please consider to cite CNApy's publication:
" "Thiele et al. (2022). CNApy: a CellNetAnalyzer GUI in Python for analyzing and designing metabolic networks. " "Bioinformatics 38, 1467-1469:" ) - self.text2.setAlignment(Qt.AlignCenter) + self.text2.setAlignment(Qt.AlignmentFlag.AlignCenter) self.url2 = QLabel( " https://doi.org/10.1093/bioinformatics/btab828 ") self.url2.setOpenExternalLinks(True) - self.url2.setAlignment(Qt.AlignCenter) + self.url2.setAlignment(Qt.AlignmentFlag.AlignCenter) self.button = QPushButton("Close") diff --git a/cnapy/gui_elements/annotation_widget.py b/cnapy/gui_elements/annotation_widget.py index a78ec05e..db8afad1 100644 --- a/cnapy/gui_elements/annotation_widget.py +++ b/cnapy/gui_elements/annotation_widget.py @@ -21,7 +21,7 @@ def __init__(self, parent): check_button = QPushButton("identifiers.org check") check_button.setIcon(QIcon.fromTheme("list-add")) policy = QSizePolicy() - policy.ShrinkFlag = True + policy.setHorizontalPolicy(QSizePolicy.Policy.Preferred) check_button.setSizePolicy(policy) check_button.clicked.connect(self.check_in_identifiers_org) lh.addWidget(check_button) @@ -31,7 +31,8 @@ def __init__(self, parent): self.annotation = QTableWidget(0, 2) self.annotation.setHorizontalHeaderLabels( ["key", "value"]) - self.annotation.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) + self.annotation.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) + self.annotation.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) lh2.addWidget(self.annotation) lh3 = QVBoxLayout() diff --git a/cnapy/gui_elements/box_position_dialog.py b/cnapy/gui_elements/box_position_dialog.py index d8e10edb..f6a8669d 100644 --- a/cnapy/gui_elements/box_position_dialog.py +++ b/cnapy/gui_elements/box_position_dialog.py @@ -1,5 +1,4 @@ """The cnapy clipboard calculator dialog""" -from tkinter.tix import X_REGION from qtpy.QtWidgets import (QButtonGroup, QComboBox, QDialog, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QRadioButton, QVBoxLayout) @@ -58,7 +57,7 @@ def set_position(self): msgBox = QMessageBox() msgBox.setWindowTitle("X position error") msgBox.setText("The X value you typed in is no valid number, hence, the new box position could not be set.") - msgBox.setIcon(QMessageBox.Warning) + msgBox.setIcon(QMessageBox.Icon.Warning) msgBox.exec() return @@ -68,7 +67,7 @@ def set_position(self): msgBox = QMessageBox() msgBox.setWindowTitle("Y position error") msgBox.setText("The Y value you typed in is no valid number, hence, the new box position could not be set.") - msgBox.setIcon(QMessageBox.Warning) + msgBox.setIcon(QMessageBox.Icon.Warning) msgBox.exec() return diff --git a/cnapy/gui_elements/central_widget.py b/cnapy/gui_elements/central_widget.py index 6aac86e4..d1856be4 100644 --- a/cnapy/gui_elements/central_widget.py +++ b/cnapy/gui_elements/central_widget.py @@ -3,14 +3,16 @@ import numpy from enum import IntEnum import cobra +from IPython.core.interactiveshell import InteractiveShell from qtconsole.inprocess import QtInProcessKernelManager from qtconsole.rich_jupyter_widget import RichJupyterWidget -from qtpy.QtCore import Qt, Signal, Slot, QSignalBlocker +from qtpy.QtCore import Qt, Signal, Slot, QSignalBlocker, QEvent from qtpy.QtGui import QColor, QBrush from qtpy.QtWidgets import (QCheckBox, QDialog, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSplitter, - QTabWidget, QVBoxLayout, QWidget, QAction, QApplication, QComboBox, QFrame) - -from cnapy.appdata import AppData, CnaMap, ModelItemType, parse_scenario + QTabWidget, QVBoxLayout, QWidget, QAction, QApplication, QComboBox, QFrame, + QStackedWidget, QSizePolicy) +from urllib.parse import unquote +from cnapy.appdata import AppData, CnaMap, ModelItemType, parse_scenario, MAP_VIEW_ESCHER from cnapy.gui_elements.map_view import MapView from cnapy.gui_elements.escher_map_view import EscherMapView from cnapy.gui_elements.metabolite_list import MetaboliteList @@ -18,7 +20,7 @@ from cnapy.gui_elements.mode_navigator import ModeNavigator from cnapy.gui_elements.model_info import ModelInfo from cnapy.gui_elements.scenario_tab import ScenarioTab -from cnapy.gui_elements.reactions_list import ReactionList, ReactionListColumn +from cnapy.gui_elements.reactions_list import ReactionList, ReactionListColumn, ReactionSearchLineEdit from cnapy.utils import SignalThrottler class ModelTabIndex(IntEnum): @@ -28,6 +30,48 @@ class ModelTabIndex(IntEnum): Scenario = 3 Model = 4 + +class CnaRichJupyterWidget(RichJupyterWidget): + """RichJupyterWidget subclass that turns anchors of the form + 'cnapy-reaction:' / 'cnapy-metabolite:' -- e.g. as produced by + build_reaction_equation_html() in reactions_list.py, or by any HTML + displayed in the console via IPython.display.display(HTML(...)) -- into + clickable links that jump to the corresponding reaction/metabolite in + CNApy, instead of just being inert rich text. + """ + + reactionClicked = Signal(str) + metaboliteClicked = Signal(str) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # RichJupyterWidget stores its actual rich-text control here. + self._control.installEventFilter(self) + + def eventFilter(self, obj, event): + if obj is self._control and event.type() == QEvent.Type.MouseButtonRelease: + if event.button() == Qt.MouseButton.LeftButton: + pos = event.position().toPoint() + href = self._control.anchorAt(pos) + + if href.startswith("cnapy-reaction:"): + reaction_id = unquote( + href[len("cnapy-reaction:"):] + ) + self.reactionClicked.emit(reaction_id) + return True + + if href.startswith("cnapy-metabolite:"): + metabolite_id = unquote( + href[len("cnapy-metabolite:"):] + ) + self.metaboliteClicked.emit(metabolite_id) + return True + + return super().eventFilter(obj, event) + + class CentralWidget(QWidget): """The PyNetAnalyzer central widget""" @@ -38,17 +82,33 @@ def __init__(self, parent): self.map_counter = 0 searchbar_layout = QHBoxLayout() - self.searchbar = QLineEdit() - self.searchbar.setPlaceholderText("Enter search term") - self.searchbar.setClearButtonEnabled(True) - searchbar_layout.addWidget(self.searchbar) + + # Independent, tab-specific search bars. Only one is visible at a + # time (kept in sync with the currently active list tab), so each + # tab's search text and highlighting persists across tab switches. + self.search_bars = QStackedWidget() + self.search_bars.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) + + self.reaction_searchbar = ReactionSearchLineEdit(self) + self.reaction_searchbar.setPlaceholderText( + "Search reactions, or enter metabolite id(s) for reactions containing all of them") + self.metabolite_searchbar = QLineEdit() + self.metabolite_searchbar.setPlaceholderText("Enter search term") + self.gene_searchbar = QLineEdit() + self.gene_searchbar.setPlaceholderText("Enter search term") + + for bar in (self.reaction_searchbar, self.metabolite_searchbar, self.gene_searchbar): + bar.setClearButtonEnabled(True) + self.search_bars.addWidget(bar) + + searchbar_layout.addWidget(self.search_bars) searchbar_layout.addSpacing(1) self.search_annotations = QCheckBox("+Annotations") self.search_annotations.setChecked(False) searchbar_layout.addWidget(self.search_annotations) line = QFrame() - line.setFrameShape(QFrame.VLine) - line.setFrameShadow(QFrame.Sunken) + line.setFrameShape(QFrame.Shape.VLine) + line.setFrameShadow(QFrame.Shadow.Sunken) searchbar_layout.addWidget(line) searchbar_layout.addSpacing(10) self.model_item_history = QComboBox() @@ -64,7 +124,9 @@ def __init__(self, parent): model_item_history_clear.clicked.connect(self.clear_model_item_history) self.throttler = SignalThrottler(300) - self.searchbar.textChanged.connect(self.throttler.throttle) + self.reaction_searchbar.textChanged.connect(self.throttler.throttle) + self.metabolite_searchbar.textChanged.connect(self.throttler.throttle) + self.gene_searchbar.textChanged.connect(self.throttler.throttle) self.throttler.triggered.connect(self.update_selected) self.search_annotations.clicked.connect(self.update_selected) @@ -93,6 +155,16 @@ def __init__(self, parent): myglobals = globals() myglobals["cna"] = self.parent self.kernel_shell = kernel_manager.kernel.shell + # The kernel's shell is an InProcessInteractiveShell, a subclass of + # InteractiveShell; qtconsole/ipykernel registers the singleton on + # that subclass via InProcessInteractiveShell.instance(), not on + # InteractiveShell itself. IPython.display.display() (and + # get_ipython()) check InteractiveShell.initialized(), which only + # looks at InteractiveShell's own _instance -- so without this line + # they see no active shell and silently fall back to printing the + # object's repr instead of rendering it (e.g. "" instead of the actual HTML). + InteractiveShell._instance = self.kernel_shell self.kernel_shell.push(myglobals) self.kernel_client = kernel_manager.client() self.kernel_client.start_channels() @@ -105,8 +177,19 @@ def __init__(self, parent): self.kernel_client.execute('%matplotlib qt', store_history=False) self.kernel_client.execute( "%config InlineBackend.figure_format = 'svg'", store_history=False) - self.console = RichJupyterWidget() - + self.console = CnaRichJupyterWidget() + + if parent.appdata.is_in_dark_mode: + self.console.set_default_style("linux") + else: + self.console.set_default_style("lightbg") + + self.console.kernel_manager = kernel_manager + self.console.kernel_client = self.kernel_client + + self.console.reactionClicked.connect(self.jump_to_reaction) + self.console.metaboliteClicked.connect(self.jump_to_metabolite) + if parent.appdata.is_in_dark_mode: self.console.set_default_style("linux") # A more 'classic' dark theme :3 else: @@ -120,7 +203,7 @@ def __init__(self, parent): self.mode_navigator = ModeNavigator(self.appdata, self) self.splitter2.addWidget(self.mode_navigator) self.splitter2.addWidget(self.console) - self.splitter2.setOrientation(Qt.Vertical) + self.splitter2.setOrientation(Qt.Orientation.Vertical) self.splitter.addWidget(self.splitter2) self.splitter.addWidget(self.tabs) self.console.show() @@ -134,6 +217,7 @@ def __init__(self, parent): self.layout().setContentsMargins(margins) self.tabs.currentChanged.connect(self.tabs_changed) + self.tabs.currentChanged.connect(self.switch_search_bar) self.reaction_list.jumpToMap.connect(self.jump_to_map) self.reaction_list.jumpToMetabolite.connect(self.jump_to_metabolite) self.reaction_list.reactionChanged.connect( @@ -143,6 +227,7 @@ def __init__(self, parent): self.metabolite_list.metaboliteChanged.connect( self.handle_changed_metabolite) self.metabolite_list.jumpToReaction.connect(self.jump_to_reaction) + self.metabolite_list.metaboliteSelected.connect(self.metabolite_row_selected) self.metabolite_list.computeInOutFlux.connect(self.in_out_fluxes) self.metabolite_list.metabolite_mask.metaboliteChanged.connect( self.reaction_list.reaction_mask.update_reaction_string) @@ -166,6 +251,10 @@ def __init__(self, parent): self.mode_navigator.reaction_participation_button.clicked.connect(self.reaction_participation) self.mode_normalization_reaction = "" + # (search string, list tab index, found reaction IDs, found metabolite IDs) of the last search, + # applied to the map that becomes visible when switching maps + self.search_result = None + self.map_tabs.currentChanged.connect(self.map_tab_changed) self.update() @@ -190,13 +279,14 @@ def handle_changed_reaction(self, previous_id: str, reaction: cobra.Reaction): self.appdata.project.maps[mmap]["boxes"][reaction.id] = self.appdata.project.maps[mmap]["boxes"].pop( previous_id) reaction_has_box = True - if self.appdata.project.maps[mmap].get('view', '') == "escher": + if self.appdata.project.maps[mmap].get('view', '') == MAP_VIEW_ESCHER: escher_map_present = True if reaction_has_box or escher_map_present: self.update_reaction_on_maps(previous_id, reaction.id, reaction_has_box, escher_map_present) if reaction.id != previous_id: self.appdata.project.reaction_ids.replace_entry(previous_id, reaction.id) self.update_item_in_history(previous_id, reaction.id, reaction.name, ModelItemType.Reaction) + self.reaction_searchbar.refresh_wordlist() def handle_deleted_reaction(self, reaction: cobra.Reaction): self.appdata.project.cobra_py_model.remove_reactions( @@ -211,6 +301,7 @@ def handle_deleted_reaction(self, reaction: cobra.Reaction): self.appdata.project.maps[mmap]["boxes"].pop(reaction.id) self.delete_reaction_on_maps(reaction.id) self.appdata.project.update_reaction_id_lists() + self.reaction_searchbar.refresh_wordlist() if self.appdata.auto_fba: self.parent.fba() @@ -225,6 +316,7 @@ def handle_changed_metabolite(self, metabolite: cobra.Metabolite, affected_react if isinstance(m, EscherMapView): m.change_metabolite_id(previous_id, metabolite.id) self.update_item_in_history(previous_id, metabolite.id, metabolite.name, ModelItemType.Metabolite) + self.reaction_searchbar.refresh_wordlist() def handle_changed_gene(self, previous_id: str, gene: cobra.Gene): self.parent.unsaved_changes() @@ -250,6 +342,7 @@ def shutdown_kernel(self): def switch_to_reaction(self, reaction: str): with QSignalBlocker(self.tabs): # set_current_item will update self.tabs.setCurrentIndex(ModelTabIndex.Reactions) + self.switch_search_bar(ModelTabIndex.Reactions) if self.tabs.width() == 0: (left, _) = self.splitter.sizes() self.splitter.setSizes([left, 1]) @@ -325,12 +418,9 @@ def add_map(self, base_name="Map", escher=False): mmap: EscherMapView = EscherMapView(self, name) self.connect_escher_map_view_signals(mmap) self.appdata.project.maps[name][EscherMapView] = mmap - self.appdata.project.maps[name]['view'] = 'escher' + self.appdata.project.maps[name]['view'] = MAP_VIEW_ESCHER self.appdata.project.maps[name]['pos'] = '{"x":0,"y":0}' self.appdata.project.maps[name]['zoom'] = '1' - # mmap.loadFinished.connect(self.finish_add_escher_map) - # mmap.cnapy_bridge.reactionValueChanged.connect(self.update_reaction_value) # connection is not made?! - # self.appdata.qapp.processEvents() # does not help idx = self.map_tabs.addTab(mmap, m["name"]) else: mmap = MapView(self.appdata, self, name) @@ -347,36 +437,49 @@ def delete_map(self, idx: int): diag = ConfirmMapDeleteDialog(self, idx, name) diag.exec() + def current_searchbar(self) -> QLineEdit: + """Search bar belonging to the currently active list tab.""" + return self.search_bars.currentWidget() + + def switch_search_bar(self, idx): + """Show the search bar matching the newly active tab, and re-apply + it so the map (and list) highlighting reflects the shown bar + instead of whatever was last searched.""" + mapping = { + ModelTabIndex.Reactions: self.reaction_searchbar, + ModelTabIndex.Metabolites: self.metabolite_searchbar, + ModelTabIndex.Genes: self.gene_searchbar, + } + bar = mapping.get(idx) + if bar is not None: + self.search_bars.setCurrentWidget(bar) + self.update_selected() + def update_selected(self): - string = self.searchbar.text() + string = self.current_searchbar().text() idx = self.tabs.currentIndex() map_idx = self.map_tabs.currentIndex() with_annotations = self.search_annotations.isChecked() and self.search_annotations.isEnabled() - QApplication.setOverrideCursor(Qt.BusyCursor) + QApplication.setOverrideCursor(Qt.CursorShape.BusyCursor) QApplication.processEvents() # to put the change above into effect + + found_metabolite_ids = [] # only populated when the Metabolites tab is active if idx == ModelTabIndex.Reactions: - found_ids = self.reaction_list.update_selected(string, with_annotations) - found_reaction_ids = found_ids + found_reaction_ids = set(self.reaction_list.update_selected(string, with_annotations)) elif idx == ModelTabIndex.Metabolites: - found_ids = self.metabolite_list.update_selected(string, with_annotations) - if map_idx >= 0: - found_reaction_ids = [] - for found_id in found_ids: - metabolite = self.appdata.project.cobra_py_model.metabolites.get_by_id(found_id) - found_reaction_ids += [x.id for x in metabolite.reactions] - else: - found_reaction_ids = found_ids + found_metabolite_ids = self.metabolite_list.update_selected(string, with_annotations) + found_reaction_ids = set() + for found_id in found_metabolite_ids: + metabolite = self.appdata.project.cobra_py_model.metabolites.get_by_id(found_id) + found_reaction_ids |= {x.id for x in metabolite.reactions} elif idx == ModelTabIndex.Genes: found_ids = self.gene_list.update_selected(string, with_annotations) - if map_idx >= 0: - found_reaction_ids = [] - for found_id in found_ids: - gene = self.appdata.project.cobra_py_model.genes.get_by_id(found_id) - found_reaction_ids += [x.id for x in gene.reactions] - else: - found_reaction_ids = found_ids + found_reaction_ids = set() + for found_id in found_ids: + gene = self.appdata.project.cobra_py_model.genes.get_by_id(found_id) + found_reaction_ids |= {x.id for x in gene.reactions} else: if len(string) == 0: # needed to reset selection on map @@ -385,13 +488,57 @@ def update_selected(self): QApplication.restoreOverrideCursor() return - if map_idx >= 0: + self.search_result = (string, idx, found_reaction_ids, found_metabolite_ids) + self.highlight_search_on_map(map_idx) + QApplication.restoreOverrideCursor() + + def map_tab_changed(self, idx: int): + # the search highlighting is only shown on the current map + for i in range(self.map_tabs.count()): + if i != idx: + self.map_tabs.widget(i).reset_search_highlight() + self.highlight_search_on_map(idx) + + def highlight_search_on_map(self, map_idx: int): + '''applies the result of the last search to the map with index map_idx''' + if map_idx >= 0 and self.search_result is not None: + (string, idx, found_reaction_ids, found_metabolite_ids) = self.search_result m = self.map_tabs.widget(map_idx) if isinstance(m, EscherMapView): - m.update_selected(string) + if not string: + # An empty search resets the list filters but is not a + # search match. In particular, do not turn the list's + # "all reactions" result into map highlighting. + m.update_selected([], []) + elif idx == ModelTabIndex.Metabolites: + # Let the user step through the individual instances of + # the metabolite on the map via Escher's own search bar + # instead of the additive multi-result highlighting. + m.search_metabolite(string) + else: + current_reaction_id = None + if idx == ModelTabIndex.Reactions: + current_item = self.reaction_list.reaction_list.currentItem() + if current_item is not None: + current_reaction_id = current_item.text(0) + m.update_selected(list(found_reaction_ids), list(found_metabolite_ids), current_reaction_id) + elif not string: + m.reset_search_highlight() else: m.update_selected(found_reaction_ids) - QApplication.restoreOverrideCursor() + + def metabolite_row_selected(self, metabolite_id: str): + """Forward the metabolite the user just clicked in the metabolite + table to an open Escher map's own search bar, so they can step + through that metabolite's instances on the map.""" + if self.tabs.currentIndex() != ModelTabIndex.Metabolites: + return + map_idx = self.map_tabs.currentIndex() + if map_idx < 0: + return + m = self.map_tabs.widget(map_idx) + if isinstance(m, EscherMapView): + m.search_metabolite(metabolite_id) def update_mode(self): if self.mode_navigator.mode_type <= 1: @@ -444,11 +591,8 @@ def update_mode(self): idx = self.appdata.window.centralWidget().tabs.currentIndex() if idx == ModelTabIndex.Reactions and self.appdata.project.comp_values_type == 0: view = self.appdata.window.centralWidget().reaction_list - view.reaction_list.blockSignals(True) # block itemChanged while recoloring - root = view.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) + view.reaction_list.blockSignals(True) # block selection signals while recoloring + for item in view.reaction_model.items: if item.text(0) in bnd_dict: v = bnd_dict[item.text(0)] if numpy.any(numpy.isnan(v)): @@ -535,6 +679,7 @@ def update(self, rebuild_all_tabs=False): self.scenario_tab.recreate_scenario_items_needed = True self.scenario_tab.update() self.model_info.update() + self.reaction_searchbar.refresh_wordlist() else: idx = self.tabs.currentIndex() if idx == ModelTabIndex.Reactions: @@ -573,13 +718,13 @@ def update_reaction_on_maps(self, old_reaction_id: str, new_reaction_id: str, else: m.update_reaction_stoichiometry(old_reaction_id) - def delete_reaction_on_maps(self, reation_id: str): + def delete_reaction_on_maps(self, reaction_id: str): for idx in range(0, self.map_tabs.count()): m = self.map_tabs.widget(idx) if isinstance(m, MapView): - m.delete_box(reation_id) + m.delete_box(reaction_id) else: - m.delete_reaction(reation_id) + m.delete_reaction(reaction_id) def update_maps(self): for idx in range(0, self.map_tabs.count()): @@ -597,13 +742,34 @@ def jump_to_map(self, identifier: str, reaction: str): m.update() m.focus_reaction(reaction) self.__recolor_map() - m.highlight_reaction(reaction) + if isinstance(m, EscherMapView): + m.jump_to_reaction(reaction) # cycles through the occurrences of the reaction + else: + m.highlight_reaction(reaction) break def reaction_selected(self, reac_id: str): for idx in range(0, self.map_tabs.count()): self.map_tabs.widget(idx).select_single_reaction(reac_id) + def set_default_colors(self): + ''' fast path for "Default Coloring": recolor the reaction list via its + already-lightweight, lazy update() (it only clears the per-item Flux + background override and lets the model/view re-query visible cells), + and recolor the map via MapView.recolor_all() -- instead of routing + through the full central_widget.update(), which additionally rebuilds + whichever tab is active and rescales/repositions/re-texts every box + on the map before it gets around to recoloring it. ''' + idx = self.tabs.currentIndex() + if idx == ModelTabIndex.Reactions and self.appdata.project.comp_values_type == 0: + self.reaction_list.update() + idx = self.map_tabs.currentIndex() + if idx < 0: + return + map_view = self.map_tabs.widget(idx) + if isinstance(map_view, MapView): + map_view.recolor_all() + def set_onoff(self): idx = self.tabs.currentIndex() if idx == ModelTabIndex.Reactions and self.appdata.project.comp_values_type == 0: @@ -613,12 +779,9 @@ def set_onoff(self): def __set_onoff_reaction_list(self): # do coloring of LB/UB columns in this case? view = self.reaction_list - # block itemChanged while recoloring + # block selection signals while recoloring view.reaction_list.blockSignals(True) - root = view.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) + for item in view.reaction_model.items: key = item.text(0) if key in self.appdata.project.scen_values: value = self.appdata.project.scen_values[key] @@ -656,12 +819,9 @@ def set_heaton(self): def __set_heaton_reaction_list(self, low, high): # TODO: coloring of LB/UB columns view = self.reaction_list - # block itemChanged while recoloring + # block selection signals while recoloring view.reaction_list.blockSignals(True) - root = view.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) + for item in view.reaction_model.items: key = item.text(0) if key in self.appdata.project.scen_values: value = self.appdata.project.scen_values[key] @@ -732,7 +892,7 @@ def add_model_item_to_history(self, item_id: str, item_name: str, item_type: Mod if index >= 0: index = self.model_item_history.removeItem(index) self.model_item_history.insertItem(0, item_id + " (" + ModelItemType(item_type).name + ")", item_data) - self.model_item_history.setItemData(0, item_name, Qt.ToolTipRole) + self.model_item_history.setItemData(0, item_name, Qt.ItemDataRole.ToolTipRole) self.model_item_history.setCurrentIndex(0) def update_item_in_history(self, previous_id: str, new_id: str, new_name: str, item_type: ModelItemType): @@ -753,9 +913,35 @@ def clear_model_item_history(self): with QSignalBlocker(self.model_item_history): self.model_item_history.clear() + def prune_model_item_history(self): + ''' Remove model item history entries that no longer occur in the current + model (e.g. after loading a project/SBML file), while keeping those entries + that are still present in the newly loaded model. ''' + model = self.appdata.project.cobra_py_model + with QSignalBlocker(self.model_item_history): + for idx in range(self.model_item_history.count() - 1, -1, -1): + item_id, item_type = self.model_item_history.itemData(idx) + if item_type == ModelItemType.Reaction: + still_exists = model.reactions.has_id(item_id) + elif item_type == ModelItemType.Metabolite: + still_exists = model.metabolites.has_id(item_id) + elif item_type == ModelItemType.Gene: + still_exists = model.genes.has_id(item_id) + else: + still_exists = False + if not still_exists: + self.model_item_history.removeItem(idx) + self.model_item_history.setCurrentIndex(-1) + def in_out_fluxes(self, metabolite): + # Note: kernel_client.execute() only queues the code for the kernel; + # by the time it returns, the plot has not actually been rendered or + # inserted into the console yet. Calling show_bottom_of_console() + # here used to race against that, scrolling based on the console's + # *old* (pre-plot) content. Scrolling is now handled directly by + # InOutFluxConsolePlot once the plot/legend are actually inserted + # (see _append_clickable_flux_plot), so no extra call is needed here. self.kernel_client.execute("cna.print_in_out_fluxes('"+metabolite+"')") - self.show_bottom_of_console() broadcastReactionID = Signal(str) @@ -783,7 +969,9 @@ def __init__(self, parent, idx: int, name: str): def delete(self): del self.parent.appdata.project.maps[self.name] + map_view = self.parent.map_tabs.widget(self.idx) self.parent.map_tabs.removeTab(self.idx) + map_view.deleteLater() # an Escher page must not outlive the shared web engine profile self.parent.reaction_list.reaction_mask.update_state() self.parent.parent.unsaved_changes() self.accept() diff --git a/cnapy/gui_elements/config_cobrapy_dialog.py b/cnapy/gui_elements/config_cobrapy_dialog.py index bd933b7e..f93b981c 100644 --- a/cnapy/gui_elements/config_cobrapy_dialog.py +++ b/cnapy/gui_elements/config_cobrapy_dialog.py @@ -4,7 +4,8 @@ import appdirs import cobra -from cobra.util.solver import interface_to_str, solvers +from cobra.util.solver import interface_to_str as cobrapy_interface_to_str +from cobra.util.solver import solvers from multiprocessing import cpu_count from qtpy.QtCore import Signal @@ -13,6 +14,12 @@ QHBoxLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout) from cnapy.appdata import AppData +import cnapy.optlang_highs_interface + +def interface_to_str(module): + if module == cnapy.optlang_highs_interface: + return "highs" + return cobrapy_interface_to_str(module) class ConfigCobrapyDialog(QDialog): @@ -27,7 +34,7 @@ def __init__(self, appdata: AppData): # allow MILP solvers only? # SCIPY currently not even usable for FBA - avail_solvers = list(set(solvers.keys()) - {'scipy'}) + avail_solvers = ['highs'] + list(set(solvers.keys()) - {'scipy'}) h2 = QHBoxLayout() label = QLabel("Default solver:\n(set when loading a model)") @@ -100,7 +107,11 @@ def __init__(self, appdata: AppData): self.button.clicked.connect(self.apply) def apply(self): - cobra.Configuration().solver = self.default_solver.currentText() + solver_name = self.default_solver.currentText() + if solver_name == "highs": + cobra.Configuration().solver = cnapy.optlang_highs_interface + else: + cobra.Configuration().solver = solver_name cobra.Configuration().processes = int(self.num_processes.text()) try: val = float(self.default_tolerance.text()) @@ -113,7 +124,11 @@ def apply(self): "Choose a value between 0.1 and 1e-9 as default tolerance.") return try: - self.appdata.project.cobra_py_model.solver = self.current_solver.currentText() + solver_name = self.current_solver.currentText() + if solver_name == "highs": + self.appdata.project.cobra_py_model.solver = cnapy.optlang_highs_interface + else: + self.appdata.project.cobra_py_model.solver = solver_name self.appdata.project.cobra_py_model.tolerance = float( self.current_tolerance.text()) self.optlang_solver_set.emit() diff --git a/cnapy/gui_elements/config_dialog.py b/cnapy/gui_elements/config_dialog.py index 768b76f7..140c4488 100644 --- a/cnapy/gui_elements/config_dialog.py +++ b/cnapy/gui_elements/config_dialog.py @@ -3,7 +3,7 @@ from pathlib import Path from qtpy.QtGui import QDoubleValidator, QIntValidator, QPalette -from qtpy.QtWidgets import (QColorDialog, QDialog, QFileDialog, +from qtpy.QtWidgets import (QColorDialog, QComboBox, QDialog, QFileDialog, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QVBoxLayout, QCheckBox) from cnapy.appdata import AppData @@ -45,7 +45,7 @@ def __init__(self, main_window, first_start: bool): self.scen_color_btn = QPushButton() self.scen_color_btn.setFixedWidth(100) palette = self.scen_color_btn.palette() - palette.setColor(QPalette.Button, self.appdata.scen_color) + palette.setColor(QPalette.ColorRole.Button, self.appdata.scen_color) self.scen_color_btn.setPalette(palette) h2.addWidget(self.scen_color_btn) self.layout.addItem(h2) @@ -57,7 +57,7 @@ def __init__(self, main_window, first_start: bool): self.comp_color_btn = QPushButton() self.comp_color_btn.setFixedWidth(100) palette = self.comp_color_btn.palette() - palette.setColor(QPalette.Button, self.appdata.comp_color) + palette.setColor(QPalette.ColorRole.Button, self.appdata.comp_color) self.comp_color_btn.setPalette(palette) h3.addWidget(self.comp_color_btn) self.layout.addItem(h3) @@ -69,7 +69,7 @@ def __init__(self, main_window, first_start: bool): self.spec1_color_btn = QPushButton() self.spec1_color_btn.setFixedWidth(100) palette = self.spec1_color_btn.palette() - palette.setColor(QPalette.Button, self.appdata.special_color_1) + palette.setColor(QPalette.ColorRole.Button, self.appdata.special_color_1) self.spec1_color_btn.setPalette(palette) h4.addWidget(self.spec1_color_btn) self.layout.addItem(h4) @@ -81,7 +81,7 @@ def __init__(self, main_window, first_start: bool): self.spec2_color_btn = QPushButton() self.spec2_color_btn.setFixedWidth(100) palette = self.spec2_color_btn.palette() - palette.setColor(QPalette.Button, self.appdata.special_color_2) + palette.setColor(QPalette.ColorRole.Button, self.appdata.special_color_2) self.spec2_color_btn.setPalette(palette) h5.addWidget(self.spec2_color_btn) self.layout.addItem(h5) @@ -93,7 +93,7 @@ def __init__(self, main_window, first_start: bool): self.default_color_btn = QPushButton() self.default_color_btn.setFixedWidth(100) palette = self.default_color_btn.palette() - palette.setColor(QPalette.Button, self.appdata.default_color) + palette.setColor(QPalette.ColorRole.Button, self.appdata.default_color) self.default_color_btn.setPalette(palette) h6.addWidget(self.default_color_btn) self.layout.addItem(h6) @@ -131,6 +131,15 @@ def __init__(self, main_window, first_start: bool): h8.addWidget(self.abs_tol) self.layout.addItem(h8) + h9 = QHBoxLayout() + label_model_format = QLabel("Default model format for saving CNApy projects:") + h9.addWidget(label_model_format) + self.model_format = QComboBox() + self.model_format.addItems(["SBML (.sbml)", "COBRApy JSON (.json)"]) + self.model_format.setCurrentIndex(int(self.appdata.save_model_as_json)) + h9.addWidget(self.model_format) + self.layout.addItem(h9) + h = QHBoxLayout() self.use_results_cache = QCheckBox("Cache results (e.g. FVA) in ") self.use_results_cache.setChecked(self.appdata.use_results_cache) @@ -185,52 +194,52 @@ def choose_results_cache_directory(self): def choose_scen_color(self): palette = self.scen_color_btn.palette() - initial = palette.color(QPalette.Button) + initial = palette.color(QPalette.ColorRole.Button) dialog = QColorDialog(self) color: str = dialog.getColor(initial) if color.isValid(): - palette.setColor(QPalette.Button, color) + palette.setColor(QPalette.ColorRole.Button, color) self.scen_color_btn.setPalette(palette) def choose_comp_color(self): palette = self.comp_color_btn.palette() - initial = palette.color(QPalette.Button) + initial = palette.color(QPalette.ColorRole.Button) dialog = QColorDialog(self) color: str = dialog.getColor(initial) if color.isValid(): - palette.setColor(QPalette.Button, color) + palette.setColor(QPalette.ColorRole.Button, color) self.comp_color_btn.setPalette(palette) def choose_spec1_color(self): palette = self.spec1_color_btn.palette() - initial = palette.color(QPalette.Button) + initial = palette.color(QPalette.ColorRole.Button) dialog = QColorDialog(self) color: str = dialog.getColor(initial) if color.isValid(): - palette.setColor(QPalette.Button, color) + palette.setColor(QPalette.ColorRole.Button, color) self.spec1_color_btn.setPalette(palette) def choose_spec2_color(self): palette = self.spec2_color_btn.palette() - initial = palette.color(QPalette.Button) + initial = palette.color(QPalette.ColorRole.Button) dialog = QColorDialog(self) color: str = dialog.getColor(initial) if color.isValid(): - palette.setColor(QPalette.Button, color) + palette.setColor(QPalette.ColorRole.Button, color) self.spec2_color_btn.setPalette(palette) def choose_default_color(self): palette = self.default_color_btn.palette() - initial = palette.color(QPalette.Button) + initial = palette.color(QPalette.ColorRole.Button) dialog = QColorDialog(self) color: str = dialog.getColor(initial) if color.isValid(): - palette.setColor(QPalette.Button, color) + palette.setColor(QPalette.ColorRole.Button, color) self.default_color_btn.setPalette(palette) def apply(self): @@ -251,23 +260,24 @@ def apply(self): self.appdata.last_scen_directory = self.work_directory.text() palette = self.scen_color_btn.palette() - self.appdata.scen_color = palette.color(QPalette.Button) + self.appdata.scen_color = palette.color(QPalette.ColorRole.Button) palette = self.comp_color_btn.palette() - self.appdata.comp_color = palette.color(QPalette.Button) + self.appdata.comp_color = palette.color(QPalette.ColorRole.Button) palette = self.spec1_color_btn.palette() - self.appdata.special_color_1 = palette.color(QPalette.Button) + self.appdata.special_color_1 = palette.color(QPalette.ColorRole.Button) palette = self.spec2_color_btn.palette() - self.appdata.special_color_2 = palette.color(QPalette.Button) + self.appdata.special_color_2 = palette.color(QPalette.ColorRole.Button) palette = self.default_color_btn.palette() - self.appdata.default_color = palette.color(QPalette.Button) + self.appdata.default_color = palette.color(QPalette.ColorRole.Button) self.appdata.box_width = int(self.box_width.text()) self.appdata.rounding = int(self.rounding.text()) self.appdata.abs_tol = float(self.abs_tol.text()) + self.appdata.save_model_as_json = bool(self.model_format.currentIndex()) self.appdata.results_cache_dir = Path(self.results_cache_directory.text()) if not self.appdata.results_cache_dir.exists(): self.use_results_cache.setChecked(False) diff --git a/cnapy/gui_elements/download_dialog.py b/cnapy/gui_elements/download_dialog.py index 792a69d1..898ce999 100644 --- a/cnapy/gui_elements/download_dialog.py +++ b/cnapy/gui_elements/download_dialog.py @@ -78,5 +78,5 @@ def download(self, download_all=False): msgBox.setText( "Projects were downloaded successfully in the working directory." ) - msgBox.setIcon(QMessageBox.Information) + msgBox.setIcon(QMessageBox.Icon.Information) msgBox.exec() diff --git a/cnapy/gui_elements/efm_dialog.py b/cnapy/gui_elements/efm_dialog.py index e7f2ffca..58092291 100644 --- a/cnapy/gui_elements/efm_dialog.py +++ b/cnapy/gui_elements/efm_dialog.py @@ -26,7 +26,7 @@ def __init__(self, appdata: AppData, central_widget): l1 = QHBoxLayout() self.constraints = QCheckBox("consider 0 in current scenario as off") - self.constraints.setCheckState(Qt.Checked) + self.constraints.setCheckState(Qt.CheckState.Checked) l1.addWidget(self.constraints) self.layout.addItem(l1) @@ -51,7 +51,7 @@ def __init__(self, appdata: AppData, central_widget): l3 = QHBoxLayout() self.check_reversibility = QCheckBox( "check reversibility") - self.check_reversibility.setCheckState(Qt.Checked) + self.check_reversibility.setCheckState(Qt.CheckState.Checked) l3.addWidget(self.check_reversibility) self.layout.addItem(l3) @@ -100,7 +100,7 @@ def compute(self): a = self.eng.eval("constraints = {};", nargout=0, stdout=self.out, stderr=self.err) scenario = {} - if self.constraints.checkState() == Qt.Checked or self.flux_bounds.isChecked(): + if self.constraints.checkState() == Qt.CheckState.Checked or self.flux_bounds.isChecked(): onoff_str = "" for r in reac_id: if r in self.appdata.project.scen_values.keys(): @@ -165,7 +165,7 @@ def compute(self): a = self.eng.eval("solver = 4;", nargout=0, stdout=self.out, stderr=self.err) - if self.check_reversibility.checkState() == Qt.Checked: + if self.check_reversibility.checkState() == Qt.CheckState.Checked: a = self.eng.eval("irrev_flag = 1;", nargout=0, stdout=self.out, stderr=self.err) else: @@ -173,14 +173,14 @@ def compute(self): nargout=0, stdout=self.out, stderr=self.err) # convex basis computation is only possible with METATOOL solver=3 - if self.convex_basis.checkState() == Qt.Checked: + if self.convex_basis.checkState() == Qt.CheckState.Checked: a = self.eng.eval("conv_basis_flag = 1; solver = 3;", nargout=0, stdout=self.out, stderr=self.err) else: a = self.eng.eval("conv_basis_flag = 0;", nargout=0, stdout=self.out, stderr=self.err) - if self.isozymes.checkState() == Qt.Checked: + if self.isozymes.checkState() == Qt.CheckState.Checked: a = self.eng.eval("iso_flag = 1;", nargout=0, stdout=self.out, stderr=self.err) else: @@ -191,7 +191,7 @@ def compute(self): a = self.eng.eval("c_macro=[]; display= 'ALL';", nargout=0, stdout=self.out, stderr=self.err) - if self.rational_numbers.checkState() == Qt.Checked: + if self.rational_numbers.checkState() == Qt.CheckState.Checked: a = self.eng.eval("efmtool_options = {'arithmetic', 'fractional'};", nargout=0, stdout=self.out, stderr=self.err) else: diff --git a/cnapy/gui_elements/efmtool_dialog.py b/cnapy/gui_elements/efmtool_dialog.py index 6a913314..a962c157 100644 --- a/cnapy/gui_elements/efmtool_dialog.py +++ b/cnapy/gui_elements/efmtool_dialog.py @@ -21,7 +21,7 @@ def __init__(self, appdata: AppData, central_widget): l1 = QHBoxLayout() self.constraints = QCheckBox("consider 0 in current scenario as off") - self.constraints.setCheckState(Qt.Checked) + self.constraints.setCheckState(Qt.CheckState.Checked) l1.addWidget(self.constraints) self.layout.addItem(l1) @@ -43,9 +43,9 @@ def __init__(self, appdata: AppData, central_widget): self.button.clicked.connect(self.compute) def compute(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) self.efm_computation = EFMComputationThread(self.appdata.project.cobra_py_model, self.appdata.project.scen_values, - self.constraints.checkState() == Qt.Checked) + self.constraints.checkState() == Qt.CheckState.Checked) self.button.setText("Abort computation") self.button.clicked.disconnect(self.compute) self.button.clicked.connect(self.efm_computation.activate_abort) @@ -56,7 +56,7 @@ def compute(self): self.efm_computation.start() def conclude_computation(self): - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) if self.efm_computation.abort: self.accept() else: diff --git a/cnapy/gui_elements/escher_map_view.py b/cnapy/gui_elements/escher_map_view.py index 418cd284..b3f155bd 100644 --- a/cnapy/gui_elements/escher_map_view.py +++ b/cnapy/gui_elements/escher_map_view.py @@ -1,12 +1,16 @@ import importlib.resources as resources +import json import os +import re from math import isclose -from qtpy.QtCore import Signal, Slot, QUrl, QObject, Qt -from qtpy.QtWidgets import QFileDialog, QMessageBox +from typing import Callable +from qtpy.QtCore import Signal, Slot, QUrl, QObject, Qt, QCoreApplication, QEvent +from qtpy.QtWidgets import QApplication, QFileDialog, QMessageBox from qtpy.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage from qtpy.QtWebChannel import QWebChannel import cobra from cnapy.appdata import AppData +from cnapy.cobra_cnapy import stoichiometry_hash from cnapy.gui_elements.map_view import validate_value class EscherMapView(QWebEngineView): @@ -14,18 +18,30 @@ class EscherMapView(QWebEngineView): download_directory: str = "" @staticmethod - @Slot("QWebEngineDownloadItem*") # QWebEngineDownloadItem not declared in qtpy + @Slot(object) # QWebEngineDownloadRequest not declared in qtpy def save_from_escher(download): - file_name = os.path.basename(download.path()) # path()/setPath() delared in PyQt - (_, ext) = os.path.splitext(file_name) - file_name = QFileDialog.getSaveFileName(directory=EscherMapView.download_directory, filter="*"+ext)[0] + (default_name, ext) = os.path.splitext(download.downloadFileName()) + view = download.page().parent() if download.page() is not None else None + if isinstance(view, EscherMapView): + project_name = os.path.splitext(os.path.basename(view.appdata.project.name))[0] + map_name = re.sub(r'(\.escher)?\.json$', "", view.name, flags=re.IGNORECASE) + if len(project_name) == 0: + default_name = map_name + elif map_name in project_name: + default_name = project_name + else: + default_name = project_name + "_" + map_name + default_name = re.sub(r'[<>:"/\\|?*]', "_", default_name) + file_name = QFileDialog.getSaveFileName(directory=os.path.join(EscherMapView.download_directory, default_name + ext), + filter="*"+ext)[0] if file_name is None or len(file_name) == 0: download.cancel() else: if not file_name.endswith(ext): file_name += ext EscherMapView.download_directory = os.path.dirname(file_name) - download.setPath(file_name) + download.setDownloadDirectory(EscherMapView.download_directory) + download.setDownloadFileName(os.path.basename(file_name)) download.accept() def __init__(self, central_widget, name: str): @@ -37,41 +53,40 @@ def __init__(self, central_widget, name: str): EscherMapView.web_engine_profile.downloadRequested.connect(EscherMapView.save_from_escher) page = QWebEnginePage(EscherMapView.web_engine_profile, self) self.setPage(page) - self.setContextMenuPolicy(Qt.NoContextMenu) + self.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu) self.initialized = False self.central_widget = central_widget self.cnapy_bridge = CnapyBridge(self, central_widget) - self.channel = QWebChannel() # reference to channel necessary on Python side for correct operation + self.channel = QWebChannel(self) # reference to channel necessary on Python side for correct operation self.page().setWebChannel(self.channel) self.channel.registerObject("cnapy_bridge", self.cnapy_bridge) html_path = resources.files("cnapy") / "data" / "escher_cnapy.html" self.load(QUrl.fromLocalFile(str(html_path))) self.name: str = name # map name for self.appdata.project.maps self.editing_enabled = False + self.dark_mode_applied = False + self.sent_model_hash = None # hash of the model last sent to Escher for editing + + @staticmethod + def release_web_engine_profile(): + '''deletes the shared profile; all pages using it must already be deleted''' + profile = EscherMapView.web_engine_profile + if profile is not None: + EscherMapView.web_engine_profile = None + profile.deleteLater() + QCoreApplication.sendPostedEvents(profile, QEvent.Type.DeferredDelete) def finish_setup(self): - # print("finish_setup") - self.page().runJavaScript( - r"var search_container=document.getElementsByClassName('search-container')[0];var search_field=document.getElementsByClassName('search-field')[0];search_container.style.display='none';document.getElementsByClassName('search-bar-button')[2].hidden=true") + self.page().runJavaScript("setupSearchBar()") self.show() self.initialized = True self.enable_editing(len(self.appdata.project.maps[self.name].get('escher_map_data', "")) == 0) self.update() - def set_map_data(self) -> bool: - map_data = self.appdata.project.maps[self.name].get('escher_map_data', "") - if len(map_data) > 0: - self.page().runJavaScript("builder.load_map("+map_data+")") - return True - else: - return False - - def set_geometry(self): - self.page().runJavaScript("builder.map.zoomContainer.goTo("+self.appdata.project.maps[self.name]["zoom"] - +","+self.appdata.project.maps[self.name]["pos"]+")") - def set_cobra_model(self): - self.cnapy_bridge.setCobraModel.emit(cobra.io.to_json(self.appdata.project.cobra_py_model)) + model = self.appdata.project.cobra_py_model + self.cnapy_bridge.setCobraModel.emit(cobra.io.to_json(model)) + self.sent_model_hash = stoichiometry_hash(model).digest() def visualize_comp_values(self): if len(self.appdata.project.comp_values) == 0: @@ -86,27 +101,30 @@ def visualize_comp_values(self): for reac_id, val in self.appdata.project.comp_values.items()}, True) def enable_editing(self, enable: bool): + if self.initialized: # otherwise the signals are not received + QApplication.setOverrideCursor(Qt.CursorShape.BusyCursor) # restored by CnapyBridge.editing_mode_set if enable: self.set_cobra_model() self.cnapy_bridge.enableEditing.emit(enable) self.editing_enabled = enable + if self.central_widget.map_tabs.currentWidget() is self: # otherwise set when switching to this map + self.central_widget.parent.escher_edit_mode_action.setChecked(enable) def update(self): if self.initialized: - if self.appdata.is_in_dark_mode: - self.page().runJavaScript(""" - document.documentElement.style.filter = "invert(1) hue-rotate(180deg)"; - """) - if self.editing_enabled: - self.set_cobra_model() # TODO: is this still required? - # currently need to handle the checkbox myself - self.central_widget.parent.escher_edit_mode_action.setChecked(self.editing_enabled) + if self.appdata.is_in_dark_mode != self.dark_mode_applied: + self.page().runJavaScript("document.documentElement.style.filter = "+ + ('"invert(1) hue-rotate(180deg)"' if self.appdata.is_in_dark_mode else '""')) + self.dark_mode_applied = self.appdata.is_in_dark_mode + # Escher needs the current model to add reactions from it in edit mode + if self.editing_enabled and stoichiometry_hash(self.appdata.project.cobra_py_model).digest() != self.sent_model_hash: + self.set_cobra_model() self.visualize_comp_values() # currently unused # def eventFilter(self, obj: QObject, event: QEvent) -> bool: # print("eventFilter", type(obj), type(event)) - # return False # keep processung the event + # return False # keep processing the event @Slot() def zoom_in(self): @@ -116,32 +134,38 @@ def zoom_in(self): def zoom_out(self): self.cnapy_bridge.zoomOut.emit() - # should this be regularily called when the map is editable? - def retrieve_map_data(self, semaphore = None): # semaphore is a list with one integer to emulate call by reference - def set_escher_map_data(new_map_data): - self.appdata.project.maps[self.name]['escher_map_data'] = new_map_data - if semaphore is not None: - semaphore[0] += 1 - self.page().runJavaScript("JSON.stringify(builder.map.map_for_export())", set_escher_map_data) # JSON.stringify not strictly necessary - - def retrieve_pos_and_zoom(self, semaphore = None): # semaphore is a list with one integer to emulate call by reference - def set_pos(result): - self.appdata.project.maps[self.name]['pos'] = result - def set_zoom(result): - self.appdata.project.maps[self.name]['zoom'] = result - self.page().runJavaScript("JSON.stringify(builder.map.zoomContainer.windowTranslate)", set_pos) # JSON.stringify not strictly necessary - self.page().runJavaScript("JSON.stringify(builder.map.zoomContainer.windowScale)", set_zoom) # JSON.stringify not strictly necessary - if semaphore is not None: - semaphore[0] += 1 + @Slot() + def zoom_to_canvas(self): + self.cnapy_bridge.zoomToCanvas.emit() + + def retrieve_map_state(self, done: Callable[[], None]): + '''Asynchronously copies the map data, position and zoom from Escher into the project map, + calls done() afterwards. The map data is otherwise only updated in the project when this is called.''' + if not self.initialized: # the project map still has the data the view was created from + done() + return + mmap = self.appdata.project.maps[self.name] # keep the reference in case the map is renamed or replaced meanwhile + def set_map_state(result): + if result is not None: # None if the page could not evaluate the script + (mmap['escher_map_data'], mmap['pos'], mmap['zoom']) = result + done() + self.page().runJavaScript("[JSON.stringify(builder.map.map_for_export()), "+ + "JSON.stringify(builder.map.zoomContainer.windowTranslate), JSON.stringify(builder.map.zoomContainer.windowScale)]", + set_map_state) def focus_reaction(self, reac_id: str): - # Escher allows the same reaction to be multiple times on a map, so we abuse its search bar here - self.central_widget.searchbar.setText(reac_id) + # Escher allows the same reaction to be multiple times on a map, so the CNApy search bar is used + # to highlight all occurrences + self.central_widget.reaction_searchbar.setText(reac_id) def highlight_reaction(self, reac_id: str): - # highlights and focuses on the first reatcion with reac_id + # highlights and focuses on the first reaction with reac_id self.cnapy_bridge.highlightAndFocusReaction.emit(reac_id) + def jump_to_reaction(self, reac_id: str): + # highlights and focuses on the next occurrence of reac_id each time it is called + self.cnapy_bridge.jumpToReaction.emit(reac_id) + def select_single_reaction(self, reac_id: str): # highlight all reactions with this reac_id self.cnapy_bridge.highlightReaction.emit(reac_id) @@ -160,23 +184,52 @@ def update_reaction_stoichiometry(self, reac_id: str): self.cnapy_bridge.updateReactionStoichiometry.emit(reac_id, {m.id: round(c, 4) for m,c in reaction.metabolites.items()}, reaction.reversibility) - def update_selected(self, find): - if len(find) == 0: + def update_selected(self, reaction_ids, metabolite_ids=None, current_reaction_id=None): + """Highlight the given reaction/metabolite ids (by bigg_id/id) on + the map. Called from CentralWidget.update_selected with the + already-resolved id sets for whichever tab/search-bar is active + (not the raw search text), so that AND-logic / tagged tokens in + the reaction search bar are already applied before reaching here. + + current_reaction_id, when given (reaction-tab search only), is + the id of whichever reaction is currently selected in the + reaction list. On top of the additive orange highlighting of all + search matches above, that one reaction also gets Escher's + native single-reaction highlight and the map zooms/pans to it. + """ + if not reaction_ids and not metabolite_ids: + # Clear additive CNApy highlights as well as Escher's native + # search UI; hiding the search bar alone leaves stale matches on + # the map. + self.cnapy_bridge.highlightSearchResults.emit([], []) self.cnapy_bridge.hideSearchBar.emit() else: - self.cnapy_bridge.displaySearchBarFor.emit(find) + self.cnapy_bridge.highlightSearchResults.emit(list(reaction_ids), list(metabolite_ids or [])) + if current_reaction_id is not None: + self.highlight_reaction(current_reaction_id) + + def reset_search_highlight(self): + self.update_selected([], []) + + def search_metabolite(self, search_string: str): + """Route a metabolite-tab search into Escher's own search bar + instead of the additive multi-result highlighting used for + reactions, so the user can use Escher's built-in next/previous + controls to step through the individual instances of a + metabolite on the map.""" + if len(search_string) == 0: + self.cnapy_bridge.hideSearchBar.emit() + else: + self.cnapy_bridge.displaySearchBarFor.emit(search_string) def dragEnterEvent(self, event): event.ignore() - def closeEvent(self, event): - self.channel.deregisterObject(self.cnapy_bridge) - event.accept() - class CnapyBridge(QObject): zoomIn = Signal() zoomOut = Signal() + zoomToCanvas = Signal() reactionValueChanged = Signal(str, str) switchToReactionMask = Signal(str) highlightAndFocusReaction = Signal(str) @@ -187,8 +240,10 @@ class CnapyBridge(QObject): changeMetId = Signal(str, str) updateReactionStoichiometry = Signal(str, 'QVariantMap', bool) # QVariantMap encapsulates a dict addMapToJumpListIfReactionPresent = Signal(str, str) + jumpToReaction = Signal(str) hideSearchBar = Signal() displaySearchBarFor = Signal(str) + highlightSearchResults = Signal('QVariantList', 'QVariantList') # (reaction bigg_ids, metabolite bigg_ids) setCobraModel = Signal(str) # cannot get passing the model dictionary as QVariantMap to work enableEditing = Signal(bool) visualizeCompValues = Signal('QVariantMap', bool) @@ -199,35 +254,38 @@ def __init__(self, escher_map: EscherMapView, central_widget): self.escher_map: EscherMapView = escher_map self.central_widget = central_widget self.appdata: AppData = self.escher_map.appdata - self.last_accepted_value: str = "" + self.last_accepted_value: str = "" # value of the reaction box input field that was last accepted or set + + def run_on_reaction_box_input(self, statement: str): + '''statement is executed with input referring to the input field of the reaction tooltip''' + self.escher_map.page().runJavaScript( + "(function(input) {if (input) {"+statement+"}})(document.getElementById('reaction-box-input'))") + + def set_input_color(self, valid: bool): + self.run_on_reaction_box_input("input.style.color = '"+("black" if valid else "red")+"';") @Slot(str, str, bool) def value_changed(self, reac_id: str, value: str, accept_if_valid: bool): if reac_id in self.appdata.project.cobra_py_model.reactions: - if validate_value(value): - self.escher_map.page().runJavaScript('document.getElementById("reaction-box-input").setAttribute("style", "color: black")') - if accept_if_valid and self.last_accepted_value != value: # avoid redundant calls - self.last_accepted_value = value - self.reactionValueChanged.emit(reac_id, value) - if self.appdata.auto_fba: - self.central_widget.parent.fba() - else: - self.escher_map.page().runJavaScript('document.getElementById("reaction-box-input").setAttribute("style", "color: red")') + valid = validate_value(value) is not False + self.set_input_color(valid) + if valid and accept_if_valid and self.last_accepted_value != value: # avoid redundant calls + self.last_accepted_value = value + self.reactionValueChanged.emit(reac_id, value) + if self.appdata.auto_fba: + self.central_widget.parent.fba() else: - if value in self.appdata.project.cobra_py_model.reactions: - self.escher_map.page().runJavaScript('document.getElementById("reaction-box-input").setAttribute("style", "color: black")') - if accept_if_valid and self.last_accepted_value != value: # avoid redundant calls - self.last_accepted_value = value - ret = QMessageBox.question(self.escher_map, f"Change reaction ID on map to {value}?", - "This is only useful if this is the same reaction as in the model but with a different ID on the map because the metabolites displayed on the map will not change!", - QMessageBox.Ok | QMessageBox.Cancel) - if ret == QMessageBox.Ok: - self.escher_map.change_reaction_id(reac_id, value) - self.escher_map.update_reaction_stoichiometry(value) - self.central_widget.unsaved_changes() - else: - self.escher_map.page().runJavaScript('document.getElementById("reaction-box-input").setAttribute("style", "color: red")') - + valid = value in self.appdata.project.cobra_py_model.reactions + self.set_input_color(valid) + if valid and accept_if_valid and self.last_accepted_value != value: # avoid redundant calls + self.last_accepted_value = value + ret = QMessageBox.question(self.escher_map, f"Change reaction ID on map to {value}?", + "This is only useful if this is the same reaction as in the model but with a different ID on the map because the metabolites displayed on the map will not change!", + QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel) + if ret == QMessageBox.StandardButton.Ok: + self.escher_map.change_reaction_id(reac_id, value) + self.escher_map.update_reaction_stoichiometry(value) + self.appdata.window.unsaved_changes() @Slot(str, str) def clicked_on_id(self, id_type: str, identifier: str): @@ -242,32 +300,41 @@ def finish_setup(self): @Slot(str) def set_reaction_box_scenario_value(self, reac_id: str): + self.last_accepted_value = "" if reac_id in self.appdata.project.scen_values: (lb, ub) = self.appdata.project.scen_values[reac_id] - attr_val = "value='"+self.appdata.format_flux_value(lb) + value = self.appdata.format_flux_value(lb) if lb != ub: - attr_val += ", "+self.appdata.format_flux_value(ub) - attr_val += "'" - self.last_accepted_value = attr_val + value += ", "+self.appdata.format_flux_value(ub) + self.last_accepted_value = value + self.run_on_reaction_box_input("input.value = "+json.dumps(value)+";") + elif reac_id in self.appdata.project.cobra_py_model.reactions: + self.run_on_reaction_box_input("input.placeholder = 'enter scenario value'; input.value = '';") else: - if reac_id in self.appdata.project.cobra_py_model.reactions: - self.escher_map.page().runJavaScript( - "document.getElementById('reaction-box-input').placeholder='enter scenario value'") - attr_val = "value=''" # to clear the input field - self.last_accepted_value = attr_val - else: - attr_val = "placeholder='map to reaction ID...'" - self.escher_map.page().runJavaScript("document.getElementById('reaction-box-input')."+attr_val) + self.run_on_reaction_box_input("input.placeholder = 'map to reaction ID...';") @Slot(result=list) def get_map_and_geometry(self) -> list: # list of strings - return [self.appdata.project.maps[self.escher_map.name].get('escher_map_data', ""), - self.appdata.project.maps[self.escher_map.name]["zoom"], self.appdata.project.maps[self.escher_map.name]["pos"]] - - @Slot(str) - def add_map_to_jump_list(self, map_name: str): - self.central_widget.reaction_list.reaction_mask.jump_list.add(map_name) + mmap = self.appdata.project.maps[self.escher_map.name] + return [mmap.get('escher_map_data', ""), mmap["zoom"], mmap["pos"]] + + @Slot(str, str, int) + def add_map_to_jump_list(self, map_name: str, reac_id: str, count: int): + reaction_mask = self.central_widget.reaction_list.reaction_mask + # the reply arrives asynchronously, ignore it if another reaction is shown in the meantime + if reaction_mask.id.text() == reac_id: + reaction_mask.jump_list.add(map_name, count) + + @Slot(str, int, int) + def jump_position_changed(self, reac_id: str, index: int, count: int): + reaction_mask = self.central_widget.reaction_list.reaction_mask + if reaction_mask.id.text() == reac_id: + reaction_mask.jump_list.set_position(self.escher_map.name, index, count) @Slot() def unsaved_changes(self): self.appdata.window.unsaved_changes() + + @Slot() + def editing_mode_set(self): + QApplication.restoreOverrideCursor() diff --git a/cnapy/gui_elements/flux_feasibility_dialog.py b/cnapy/gui_elements/flux_feasibility_dialog.py index a571ecc3..20342854 100644 --- a/cnapy/gui_elements/flux_feasibility_dialog.py +++ b/cnapy/gui_elements/flux_feasibility_dialog.py @@ -1,13 +1,14 @@ from math import copysign from qtpy.QtCore import Qt, Slot, QSignalBlocker, QStringListModel, QSize from qtpy.QtWidgets import (QDialog, QGroupBox, QHBoxLayout, QTableWidget, QCheckBox, QMainWindow, - QLabel, QLineEdit, QMessageBox, QPushButton, QAbstractItemView, QAction, + QLabel, QLineEdit, QMessageBox, QPushButton, QAbstractItemView, QRadioButton, QVBoxLayout, QTableWidgetItem, QButtonGroup, QWidget, - QStyledItemDelegate, QTableWidgetSelectionRange, QCompleter) -from qtpy.QtGui import QGuiApplication, QDoubleValidator + QStyledItemDelegate, QTableWidgetSelectionRange, QCompleter, QApplication) +from qtpy.QtGui import QAction, QGuiApplication, QDoubleValidator from cnapy.utils import QComplReceivLineEdit from cnapy.core import make_scenario_feasible, QPnotSupportedException, element_exchange_balance +from cnapy.core_gui import get_last_exception_string, has_community_error_substring, except_likely_community_model_error from cnapy.gui_elements.central_widget import ModelTabIndex from cnapy.appdata import Scenario import cobra @@ -98,8 +99,8 @@ def __init__(self, main_window: QMainWindow): self.bm_constituents.setHorizontalHeaderLabels([" ", "Component", "Formula", "Coefficient", "Adjustment", "Change [%]"]) self.bm_constituents.setSortingEnabled(True) self.bm_constituents.verticalHeader().setVisible(False) - self.bm_constituents.setEditTriggers(QAbstractItemView.NoEditTriggers) - self.bm_constituents.setSelectionMode(QAbstractItemView.ContiguousSelection) + self.bm_constituents.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) + self.bm_constituents.setSelectionMode(QAbstractItemView.SelectionMode.ContiguousSelection) self.bm_constituents.resizeColumnsToContents() coefficient_delegate = CoefficientDelegate() self.bm_constituents.setItemDelegateForColumn(3, coefficient_delegate) @@ -201,6 +202,13 @@ def compute(self): self.bm_constituents.setItem(i, 4, None) self.bm_constituents.setItem(i, 5, None) + # if the last scenario change comes from the previous computation undo it + # (before the checks below so that these are based on the restored scenario) + if self.last_scenario_change_from_previous_computation(): + if self.bm_mod_reac_id in self.appdata.project.scen_values.reactions: + self.main_window.centralWidget().tabs.widget(ModelTabIndex.Scenario).recreate_scenario_items_needed = True + self.main_window.undo_scenario_edit() + if self.flux_group.isChecked(): if self.abs_flux_weights_button.isChecked(): abs_flux_weights = True @@ -218,13 +226,18 @@ def compute(self): self.enable_biomass_equation_modifications(False) return bm_reac_id = self.bm_reac_id + if "adjusted_" + bm_reac_id in self.appdata.project.scen_values.reactions: + QMessageBox.information(self, "Adjusted biomass reaction already in scenario", + "The scenario already contains the adjusted biomass reaction adjusted_" + bm_reac_id + + ".\nDelete it from the scenario reactions before adjusting the composition of " + bm_reac_id + " again.") + return self.bm_reac: cobra.Reaction = self.appdata.project.cobra_py_model.reactions.get_by_id(self.bm_reac_id) if self.adjust_bm_coeff.isChecked(): for i in range(self.bm_constituents.rowCount()): checkbox = self.bm_constituents.cellWidget(i, 0) if checkbox is not None and checkbox.isChecked(): - bm_constituent = self.bm_constituents.item(i, 1).data(Qt.UserRole) + bm_constituent = self.bm_constituents.item(i, 1).data(Qt.ItemDataRole.UserRole) if bm_constituent not in self.bm_reac.metabolites: QMessageBox.critical(self, "Invalid biomass constituent encountered", bm_constituent.id + " is not in the current biomass reaction, it appears to have been modified.\nReset the currently selected biomass reaction.") @@ -252,27 +265,19 @@ def compute(self): else: bm_reac_id = "" - # if the last scenario change comes from the previous computation undo it - if len(self.appdata.scenario_past) > 0 and self.modified_scenario is self.appdata.scenario_past[-1]: - print("Resetting scenario") - self.main_window.undo_scenario_edit() - self.modified_scenario = None - if self.bm_mod_reac_id in self.appdata.project.scen_values.reactions: - print("Removing scenario BM") - del self.appdata.project.scen_values.reactions[self.bm_mod_reac_id] - self.bm_mod_reac_id = "" - self.main_window.centralWidget().tabs.widget(ModelTabIndex.Scenario).recreate_scenario_items_needed = True - - self.main_window.setCursor(Qt.BusyCursor) + QApplication.setOverrideCursor(Qt.BusyCursor) + QApplication.processEvents() try: self.appdata.project.solution, reactions_in_objective, bm_mod, gam_mets_sign, gam_adjust = make_scenario_feasible( self.appdata.project.cobra_py_model, self.appdata.project.scen_values, use_QP=self.method_qp.isChecked(), flux_weight_scale=flux_weight_scale, abs_flux_weights=abs_flux_weights, weights_key=weights_key, bm_reac_id=bm_reac_id, variable_constituents=variable_constituents, max_coeff_change=max_coeff_change/100, bm_change_in_gram=self.bm_gram.isChecked(), gam_mets_param=gam_mets_param) - self.main_window.process_fba_solution(update=False) if self.appdata.project.solution.status == 'optimal': + self.main_window.process_fba_solution(update=False) bm_is_modified = len(bm_mod) > 0 or gam_adjust != 0 + add_bm_mod = bm_is_modified and self.bm_mod_scenario.isChecked() + scenario_changes = {} # reaction ID: new scenario value if len(reactions_in_objective) > 0: self.main_window.centralWidget().console._append_plain_text( "\nThe fluxes of the following reactions were changed to make the scenario feasible:\n", before_prompt=True) @@ -282,51 +287,62 @@ def compute(self): if abs(given_value - computed_value) > self.appdata.project.cobra_py_model.tolerance: self.main_window.centralWidget().console._append_plain_text(r+": "+self.appdata.format_flux_value(given_value) +" --> "+self.appdata.format_flux_value(computed_value)+"\n", before_prompt=True) - scenario_fluxes = [self.appdata.project.comp_values[r] for r in reactions_in_objective] - if bm_is_modified and self.bm_mod_scenario.isChecked(): - fixed_growth_rate = self.appdata.project.scen_values[bm_reac_id][0] - self.appdata.scen_values_set_multiple(reactions_in_objective+[bm_reac_id], scenario_fluxes+[(0, 0)]) - else: - self.appdata.scen_values_set_multiple(reactions_in_objective, scenario_fluxes) - self.modified_scenario = self.appdata.scenario_past[-1] + scenario_changes[r] = self.appdata.project.comp_values[r] if bm_is_modified: bm_reac_mod = self.bm_reac.copy() if len(bm_mod) > 0: self.bm_constituents.setSortingEnabled(False) for i in range(self.bm_constituents.rowCount()): - met = self.bm_constituents.item(i, 1).data(Qt.UserRole) + met = self.bm_constituents.item(i, 1).data(Qt.ItemDataRole.UserRole) if met in bm_mod: mod = bm_mod[met] item = QTableWidgetItem() - item.setData(Qt.DisplayRole, mod) + item.setData(Qt.ItemDataRole.DisplayRole, mod) self.bm_constituents.setItem(i, 4, item) ref = self.bm_reac.metabolites[met] if met in gam_mets: ref -= copysign(gam_base, ref) item = QTableWidgetItem() - item.setData(Qt.DisplayRole, 100 * mod/ref) + item.setData(Qt.ItemDataRole.DisplayRole, 100 * mod/ref) self.bm_constituents.setItem(i, 5, item) self.bm_constituents.setSortingEnabled(True) bm_reac_mod.add_metabolites(bm_mod) bm_reac_mod.add_metabolites({met: sign*gam_adjust for met, sign in zip(gam_mets, gam_mets_sign)}) self.main_window.centralWidget().console._append_plain_text("\nModified biomass reaction:\n" +bm_reac_mod.build_reaction_string(), before_prompt=True) - if self.bm_mod_scenario.isChecked(): - self.bm_mod_reac_id = "adjusted_" + bm_reac_id + if add_bm_mod: + # the adjusted biomass reaction replaces the original one which is switched off; + # it gets the bounds of the original reaction and the fixed growth rate as scenario value + fixed_growth_rate = self.appdata.project.scen_values[bm_reac_id][0] + self.bm_mod_reac_id = "adjusted_" + bm_reac_id + scenario_changes[bm_reac_id] = (0, 0) + scenario_changes[self.bm_mod_reac_id] = (fixed_growth_rate, fixed_growth_rate) + if len(scenario_changes) > 0: + # all scenario changes of this computation are recorded as one step so that they can be undone together + self.appdata.scen_values_set_multiple(list(scenario_changes.keys()), list(scenario_changes.values())) + if add_bm_mod: self.appdata.project.scen_values.reactions[self.bm_mod_reac_id] = \ - [{met.id: coeff for met, coeff in bm_reac_mod.metabolites.items()}, fixed_growth_rate, fixed_growth_rate] + [{met.id: coeff for met, coeff in bm_reac_mod.metabolites.items()}, + self.bm_reac.lower_bound, self.bm_reac.upper_bound] + self.appdata.project.scen_values.set_hash_value() self.main_window.centralWidget().tabs.widget(ModelTabIndex.Scenario).recreate_scenario_items_needed = True + self.modified_scenario = self.appdata.scenario_history[self.appdata.current_scenario_index] if self.bm_group.isChecked() and self.adjust_gam.isChecked(): self.gam_adjustment.setText("Calculated GAM adjustment: "+coefficient_format.format(gam_adjust)) else: QMessageBox.critical(self, "Solver could not find an optimal solution", - "No optimal solution was found, solver returned status '"+self.appdata.project.solution.status+"'.") + "Solver status is '"+self.appdata.project.solution.status+"'.\nTry relaxing model tolerance or choose a different solver.") self.main_window.centralWidget().update() except QPnotSupportedException: QMessageBox.critical(self, "Solver with support for quadratic objectives required", "Choose an appropriate solver, e.g. cplex, gurobi, cbc-coinor (see Configure COBRApy in the Config menu).") + except Exception: + exstr = get_last_exception_string() + # Check for substrings of Gurobi and CPLEX community edition errors + if has_community_error_substring(exstr): + except_likely_community_model_error() finally: - self.main_window.setCursor(Qt.ArrowCursor) + QApplication.restoreOverrideCursor() def update_bm_constituents_table(self): bm_reac: cobra.Reaction = self.appdata.project.cobra_py_model.reactions.get_by_id(self.bm_reac_id) @@ -351,11 +367,11 @@ def update_bm_constituents_table(self): checkbox.setToolTip("cannot be adjusted because it has no formula") item = QTableWidgetItem(met.id) item.setToolTip(met.name) - item.setData(Qt.UserRole, met) + item.setData(Qt.ItemDataRole.UserRole, met) self.bm_constituents.setItem(i, 1, item) self.bm_constituents.setItem(i, 2, QTableWidgetItem(met.formula)) item = QTableWidgetItem() - item.setData(Qt.DisplayRole, coeff) + item.setData(Qt.ItemDataRole.DisplayRole, coeff) self.bm_constituents.setItem(i, 3, item) # clear possible previous results self.bm_constituents.setItem(i, 4, None) @@ -363,11 +379,28 @@ def update_bm_constituents_table(self): self.bm_constituents.resizeColumnToContents(0) self.bm_constituents.setSortingEnabled(True) + def last_scenario_change_from_previous_computation(self) -> bool: + # then compute() undoes this change before the next computation + return self.appdata.current_scenario_index >= 1 and \ + self.modified_scenario is self.appdata.scenario_history[self.appdata.current_scenario_index] + def growth_rate_fixed(self) -> bool: if self.bm_reac_id in self.appdata.project.cobra_py_model.reactions: - scen_val = self.appdata.project.scen_values.get(self.bm_reac_id, None) + if self.last_scenario_change_from_previous_computation(): + # check the scenario on which the next computation will be based + scen_values = self.appdata.scenario_history[self.appdata.current_scenario_index - 1] + else: + scen_values = self.appdata.project.scen_values + scen_val = scen_values.get(self.bm_reac_id, None) if scen_val is not None and scen_val[0] == scen_val[1]: - return True + if scen_val[0] != 0: + return True + text = "The growth rate of " + self.bm_reac_id + " is fixed to 0, adjustments to its composition require a non-zero growth rate." + if "adjusted_" + self.bm_reac_id in scen_values.reactions: + text += "\nThe scenario contains the adjusted biomass reaction adjusted_" + self.bm_reac_id + \ + ", delete it from the scenario reactions and set the growth rate of " + self.bm_reac_id + \ + " again to adjust the biomass composition anew." + QMessageBox.information(self, "Growth rate fixed to 0", text) else: QMessageBox.information(self, "Growth rate not fixed in current scenario", "Adjustments to biomass composition are only possible if the growth rate is fixed.") @@ -381,7 +414,7 @@ def verify_biomass_reaction(self, correct: bool) -> bool: verified = False if correct: with QSignalBlocker(self.bm_reac_id_select): - self.bm_group.setFocus(True) # remove focus from self.bm_reac_id_select to suppress further signals + self.bm_group.setFocus() # remove focus from self.bm_reac_id_select to suppress further signals self.bm_reac_id = self.bm_reac_id_select.text().strip() if self.growth_rate_fixed(): verified = True @@ -425,7 +458,7 @@ def enable_biomass_equation_modifications(self, enable: bool): self.adjust_gam.setEnabled(enable) def enable_gam_mets_parts(self): - for widget in self.adjust_gam.findChildren(QWidget, options=Qt.FindDirectChildrenOnly): + for widget in self.adjust_gam.findChildren(QWidget, options=Qt.FindChildOption.FindDirectChildrenOnly): widget.setEnabled(True) @Slot(bool) @@ -450,14 +483,14 @@ def copy_table_selection(self): if item is None: row.append("") else: - row.append(str(item.data(Qt.DisplayRole))) + row.append(str(item.data(Qt.ItemDataRole.DisplayRole))) c += 1 table.append('\t'.join(row)) r += 1 clipboard = QGuiApplication.clipboard() clipboard.setText('\r'.join(table)) - def get_gam_removal_parameters(self, bm_reac: cobra.Reaction()): + def get_gam_removal_parameters(self, bm_reac: cobra.Reaction): valid, gam_mets = self.validate_gam_mets([met.id for met in bm_reac.metabolites]) if valid: valid, gam_base = self.validate_gam_remove(gam_mets) @@ -532,9 +565,11 @@ def print_to_console(*txt): val = self.appdata.project.comp_values.get(reaction.id, None) if val: values[reaction.id] = val - val = self.appdata.project.comp_values.get(self.bm_reac_id, None) - if val: - values[self.bm_reac_id] = val + for reac_id in non_boundary_reactions + [self.bm_reac_id]: + # the bounds of the adjusted biomass reaction do not fix its flux, use its scenario value if there is no computed value + val = self.appdata.project.comp_values.get(reac_id, self.appdata.project.scen_values.get(reac_id, None)) + if val: + values[reac_id] = val if len(values) > 0: print_to_console("All boundary fluxes" + biomass_text + ":") element_exchange_balance(self.appdata.project.cobra_py_model, values, @@ -544,7 +579,7 @@ class MultiCompleter(QCompleter): def __init__(self, parent=None): QCompleter.__init__(self, parent) self.setModel(QStringListModel()) - self.setCaseSensitivity(Qt.CaseInsensitive) + self.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive) def pathFromIndex(self, index): # overrides Qcompleter method path = QCompleter.pathFromIndex(self, index) diff --git a/cnapy/gui_elements/flux_optimization_dialog.py b/cnapy/gui_elements/flux_optimization_dialog.py index 4c31d04d..10b67b4c 100644 --- a/cnapy/gui_elements/flux_optimization_dialog.py +++ b/cnapy/gui_elements/flux_optimization_dialog.py @@ -79,7 +79,7 @@ def validate_dialog(self,b=True): self.button.setEnabled(False) def compute(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) if self.sense_combo.currentText() == 'maximize': sense = 'Maximum' else: @@ -107,7 +107,7 @@ def compute(self): QMessageBox.warning(self, 'Problem infeasible.', 'The scenario seems to be infeasible.',) return - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) self.accept() def set_boxes(self,sol): diff --git a/cnapy/gui_elements/gene_list.py b/cnapy/gui_elements/gene_list.py index 26d2d123..b85d2e29 100644 --- a/cnapy/gui_elements/gene_list.py +++ b/cnapy/gui_elements/gene_list.py @@ -3,9 +3,10 @@ import cobra import cobra.manipulation from qtpy.QtCore import Qt, Signal, Slot -from qtpy.QtWidgets import (QAction, QHBoxLayout, QLabel, +from qtpy.QtGui import QAction +from qtpy.QtWidgets import (QAbstractItemView, QHBoxLayout, QLabel, QHeaderView, QLineEdit, QMenu, QMessageBox, QPushButton, QSizePolicy, QSplitter, - QTableWidgetItem, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget) + QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget) from cnapy.appdata import AppData, ModelItemType from cnapy.utils import SignalThrottler, turn_red, turn_white, update_selected @@ -22,14 +23,21 @@ def __init__(self, central_widget): self.central_widget = central_widget self.last_selected = None - self.gene_list = QTreeWidget() - self.gene_list.setHeaderLabels(["Id", "Name"]) - self.gene_list.setSortingEnabled(True) - self.gene_list.sortByColumn(0, Qt.AscendingOrder) + self.gene_list = QTableWidget() + self.gene_list.setColumnCount(2) + self.gene_list.setHorizontalHeaderLabels(["Id", "Name"]) + self.gene_list.verticalHeader().setVisible(False) + self.gene_list.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) + self.gene_list.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) + self.gene_list.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) + self.gene_list.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) + self.gene_list.setSortingEnabled(False) for m in self.appdata.project.cobra_py_model.genes: self.add_gene(m) - self.gene_list.setContextMenuPolicy(Qt.CustomContextMenu) + self.gene_list.setSortingEnabled(True) + self.gene_list.sortItems(0, Qt.SortOrder.AscendingOrder) + self.gene_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) self.gene_list.customContextMenuRequested.connect( self.on_context_menu) @@ -41,7 +49,7 @@ def __init__(self, central_widget): self.layout.setContentsMargins(0, 0, 0, 0) self.splitter = QSplitter() - self.splitter.setOrientation(Qt.Vertical) + self.splitter.setOrientation(Qt.Orientation.Vertical) self.splitter.addWidget(self.gene_list) self.splitter.addWidget(self.gene_mask) self.layout.addWidget(self.splitter) @@ -58,29 +66,32 @@ def __init__(self, central_widget): ) def clear(self): - self.gene_list.clear() + self.gene_list.setRowCount(0) self.gene_mask.hide() def add_gene(self, gene): - item = QTreeWidgetItem(self.gene_list) - item.setText(0, gene.id) - item.setText(1, gene.name) - item.setData(2, 0, gene) + row = self.gene_list.rowCount() + self.gene_list.insertRow(row) + id_item = QTableWidgetItem(gene.id) + name_item = QTableWidgetItem(gene.name) + id_item.setData(Qt.ItemDataRole.UserRole, gene) + self.gene_list.setItem(row, 0, id_item) + self.gene_list.setItem(row, 1, name_item) def on_context_menu(self, point): if len(self.appdata.project.cobra_py_model.genes) > 0: - self.pop_menu.exec_(self.mapToGlobal(point)) + self.pop_menu.exec(self.mapToGlobal(point)) def handle_changed_gene(self, gene: cobra.Gene): # Update gene item in list - root = self.gene_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) - if item.data(2, 0) == gene: - old_id = item.text(0) - item.setText(0, gene.id) - item.setText(1, gene.name) + old_id = None + row_count = self.gene_list.rowCount() + for row in range(row_count): + id_item = self.gene_list.item(row, 0) + if id_item.data(Qt.ItemDataRole.UserRole) == gene: + old_id = id_item.text() + id_item.setText(gene.id) + self.gene_list.item(row, 1).setText(gene.name) break for reaction_x in self.appdata.project.cobra_py_model.reactions: @@ -105,7 +116,8 @@ def gene_selected(self, item, _column): self.gene_mask.hide() else: self.gene_mask.show() - gene: cobra.Gene = item.data(2, 0) + row = item.row() + gene: cobra.Gene = self.gene_list.item(row, 0).data(Qt.ItemDataRole.UserRole) self.gene_mask.gene = gene @@ -120,20 +132,23 @@ def gene_selected(self, item, _column): self.central_widget.add_model_item_to_history(gene.id, gene.name, ModelItemType.Gene) def update(self): - self.gene_list.clear() + self.gene_list.setSortingEnabled(False) + self.gene_list.setRowCount(0) for m in self.appdata.project.cobra_py_model.genes: self.add_gene(m) + self.gene_list.setSortingEnabled(True) if self.last_selected is None: self.gene_list.setCurrentItem(None) else: items = self.gene_list.findItems( - self.last_selected, Qt.MatchExactly) + self.last_selected, Qt.MatchFlag.MatchExactly) for i in items: - self.gene_list.setCurrentItem(i) - self.gene_list.scrollToItem(i) - break + if i.column() == 0: + self.gene_list.setCurrentItem(i) + self.gene_list.scrollToItem(i) + break def set_current_item(self, key): self.last_selected = key @@ -177,7 +192,7 @@ def __init__(self, gene_list, appdata): "Delete this gene and remove it from associated reactions." ) policy = QSizePolicy() - policy.ShrinkFlag = True + policy.setHorizontalPolicy(QSizePolicy.Policy.Preferred) self.delete_button.setSizePolicy(policy) l.addWidget(self.delete_button) @@ -242,11 +257,10 @@ def delete_gene(self): ) self.appdata.window.unsaved_changes() self.hide() - current_row_index = self.gene_list.gene_list.currentIndex().row() + current_row_index = self.gene_list.gene_list.currentRow() self.gene_list.gene_list.setCurrentItem(None) self.gene_list.last_selected = None - self.gene_list.gene_list.takeTopLevelItem( - current_row_index) + self.gene_list.gene_list.removeRow(current_row_index) self.appdata.window.setFocus() def delete_selected_annotation(self, identifier_key): diff --git a/cnapy/gui_elements/in_out_flux_console_plot.py b/cnapy/gui_elements/in_out_flux_console_plot.py new file mode 100644 index 00000000..a8257d57 --- /dev/null +++ b/cnapy/gui_elements/in_out_flux_console_plot.py @@ -0,0 +1,679 @@ +"""Interactive in/out-flux plot embedded inline in the CNApy console. + +This module owns everything needed to: + +- compute the in/out flux bar plot for a single metabolite + (:meth:`InOutFluxConsolePlot.in_out_fluxes`, called e.g. from + ``cna.print_in_out_fluxes(...)`` in the console), +- render it as a fixed-width image inserted into the QtConsole's + scrollback next to a clickable legend (reaction and metabolite IDs are + clickable links, using the same ``cnapy-reaction:``/``cnapy-metabolite:`` + href scheme as e.g. ``reactions_list.build_reaction_equation_html``), and +- make the individual stacked-bar segments in the resulting inline image + clickable too, via a Qt event filter installed on the console's + viewport. + +A single :class:`InOutFluxConsolePlot` instance is created by +:class:`~cnapy.gui_elements.main_window.MainWindow` and lives for as long +as the console does, so old plots further up in the scrollback stay fully +interactive. +""" +from io import BytesIO +from urllib.parse import quote, unquote + +import matplotlib.pyplot as plt +from matplotlib.colors import to_hex + +from qtpy.QtCore import QEvent, QObject, QRect, QUrl, Qt, Signal +from qtpy.QtGui import ( + QColor, + QFont, + QFontMetrics, + QImage, + QPalette, + QTextBlockFormat, + QTextCharFormat, + QTextCursor, + QTextDocument, + QTextImageFormat, + QTextLength, + QTextTableFormat, +) +from qtpy.QtWidgets import QToolTip + + +class InOutFluxConsolePlot(QObject): + """Computes and displays a clickable in/out-flux plot in the console.""" + + # Emitted when an inline-console link created by this class is clicked + # (a "Metabolite: X" header link, a legend reaction/metabolite link, or + # a bar segment in the plot itself). Handled here via an event filter + # rather than qtconsole's own link handling, so old console output + # remains fully scrollable and interactive. + reactionClicked = Signal(str) + metaboliteClicked = Signal(str) + + def __init__(self, appdata, central_widget): + super().__init__(central_widget) + self.appdata = appdata + self.central_widget = central_widget + + # Inline plots are inserted into the QtConsole document as images. + # Keep the image-local hit regions so the stacked rectangles can be + # made interactive even though Matplotlib itself is no longer + # receiving mouse events after the figure is rendered inline. + self._console_plot_regions = {} + self._console_plot_counter = 0 + self._console_plot_tooltip_reaction = None + + # The QtConsole uses a QTextEdit internally for rich output. Install + # an event filter there so custom cnapy-* hyperlinks and the inline + # plot hit-testing below work without replacing the inline backend. + self._console_control = getattr(central_widget.console, "_control", None) + self._console_viewport = None + if self._console_control is not None: + # Qt delivers text-edit mouse events to the viewport. The + # coordinates passed to QTextEdit.anchorAt() are viewport + # coordinates, so keep the filter there and do not remap them. + self._console_viewport = self._console_control.viewport() + self._console_viewport.installEventFilter(self) + self.reactionClicked.connect(central_widget.jump_to_reaction) + self.metaboliteClicked.connect(central_widget.jump_to_metabolite) + + # ------------------------------------------------------------------ + # Computing and inserting a plot + # ------------------------------------------------------------------ + def in_out_fluxes(self, metabolite_id, soldict): + self.central_widget.kernel_client.execute('%matplotlib inline', store_history=False) + # Disable pyplot's interactive mode while building the figure: with + # an interactive backend active, each individual ax.bar() call below + # can trigger its own canvas redraw, which visibly adds up for + # metabolites involved in many reactions. The figure is still + # rendered explicitly (once) in _append_clickable_flux_plot. + with plt.ioff(), self.appdata.project.cobra_py_model as model: + self.appdata.project.scen_values.add_scenario_reactions_to_model(model) + met = model.metabolites.get_by_id(metabolite_id) + fig, ax = plt.subplots() + ax.set_xticks([1, 2]) + ax.set_xticklabels(['In', 'Out']) + cons = [] + prod = [] + sum_cons = 0 + sum_prod = 0 + + # Keep the matplotlib bar colors so the clickable console legend + # can visually match the corresponding stacked bars. + legend_entries = [] + + for rxn in met.reactions: + flux = soldict.get(rxn.id, 0.0) + if abs(flux) > model.tolerance: + flux *= rxn.get_coefficient(metabolite_id) + if flux < 0: + cons.append((rxn, -flux)) + elif flux > 0: + prod.append((rxn, flux)) + cons = sorted(cons, key=lambda x: x[1], reverse=True) + prod = sorted(prod, key=lambda x: x[1], reverse=True) + + for rxn, flux in prod: + bar = ax.bar( + 1, flux, width=0.8, bottom=sum_prod, + label=rxn.id + ": " + rxn.build_reaction_string() + ) + legend_entries.append((rxn.id, rxn, flux, bar.patches[0])) + sum_prod += flux + + for rxn, flux in cons: + bar = ax.bar( + 2, flux, width=0.8, bottom=sum_cons, + label=rxn.id + ": " + rxn.build_reaction_string() + ) + legend_entries.append((rxn.id, rxn, flux, bar.patches[0])) + sum_cons += flux + + ax.set_ylabel('Flux') + ax.set_title('In/Out fluxes at metabolite ' + metabolite_id) + + # Insert plot and clickable legend together as one scrollback item. + self._append_clickable_flux_plot( + fig, metabolite_id, legend_entries + ) + + self.central_widget.kernel_client.execute('%matplotlib qt', store_history=False) + + return prod, cons + + def _append_clickable_flux_plot(self, fig, metabolite_id, legend_entries): + """Insert a fixed-width flux plot and clickable legend side-by-side. + + ``legend_entries`` contains ``(reaction_id, reaction, flux, patch)``. + The plot is rendered at a fixed physical width so its size does not + depend on the QtConsole width. The legend contains clickable + reaction and metabolite IDs and the numerical flux value in front of + each equation. + """ + control = self._console_control + if control is None: + return + + # Render the matplotlib figure without sending it separately through + # the inline backend; the rendered image is inserted into the same + # QTextDocument as the clickable legend. + # Render at a fixed physical width and a fixed, known dpi. The plot + # only contains two stacked bars, so a relatively compact width is + # sufficient. + fixed_plot_width = 3.2 + fig.set_size_inches(fixed_plot_width, max(2.4, fixed_plot_width * fig.get_figheight() / max(fig.get_figwidth(), 0.1)), forward=True) + fig.set_dpi(100) + + # Draw the figure exactly once. Previously it was rendered twice -- + # once implicitly via get_renderer() below to compute the legend's + # clickable hit-boxes, and again via savefig() to produce the PNG -- + # which visibly doubles the redraw cost for metabolites with many + # reactions (many bar segments). Reading the already-rendered RGBA + # buffer straight into a QImage also skips a full PNG encode/decode + # round trip that savefig()+loadFromData() would otherwise do. + try: + fig.canvas.draw() + renderer = fig.canvas.get_renderer() + image_width = int(renderer.width) + image_height = int(renderer.height) + buf = renderer.buffer_rgba() + # .copy() so the QImage owns its pixel data independently of the + # renderer's buffer, which is freed once the figure is closed + # further down (Qt may not actually paint the image until well + # after that). + image = QImage( + buf, image_width, image_height, QImage.Format.Format_RGBA8888 + ).copy() + except AttributeError: + # Fallback for a canvas that isn't Agg-based (buffer_rgba() is + # only available on Agg-derived canvases -- which covers every + # backend used here, so this should be rare in practice). + png = BytesIO() + fig.savefig(png, format="png", dpi=100) + png.seek(0) + image = QImage() + if not image.loadFromData(png.read(), "PNG"): + return + renderer = fig.canvas.get_renderer() + image_width = image.width() + image_height = image.height() + + # Record one image-local rectangle for each stacked bar, from the + # very renderer used above (no second render pass). Matplotlib's + # display coordinate system has its origin at the lower left, while a + # QImage/Qt text document has its origin at the upper left. + regions = [] + for reaction_id, _rxn, flux, patch in legend_entries: + bbox = patch.get_window_extent(renderer) + x0 = max(0.0, bbox.x0) + x1 = min(float(image_width), bbox.x1) + y0 = max(0.0, float(image_height) - bbox.y1) + y1 = min(float(image_height), float(image_height) - bbox.y0) + regions.append((x0, y0, x1, y1, reaction_id)) + + self._console_plot_counter += 1 + image_id = f"cnapy-flux-plot-{self._console_plot_counter}" + + document = control.document() + + # Find the position of the console's current prompt (if any), so the + # plot/legend can be inserted *before* it instead of always being + # appended at the absolute end of the document. kernel_client.execute() + # (used by in_out_fluxes() above) queues code directly with the + # kernel, bypassing the console's own execute() bookkeeping -- so + # whatever prompt was already on screen (waiting for the user, with + # or without partially typed text) is still sitting at the end of + # the document while this runs, and appending after it would insert + # the new output below that prompt instead of above it. + console_widget = self.central_widget.console + prompt_pos = getattr(console_widget, "_prompt_pos", None) + insert_before_prompt = ( + prompt_pos is not None and 0 <= prompt_pos <= document.characterCount() + ) + + if insert_before_prompt: + cursor = QTextCursor(document) + cursor.setPosition(prompt_pos) + cursor.movePosition(QTextCursor.MoveOperation.StartOfBlock) + else: + cursor = control.textCursor() + cursor.movePosition(QTextCursor.MoveOperation.End) + + # Batch every insertion below (table, image, and all legend text/ + # links) into a single edit block. Without this, each of the many + # small insertText()/insertLink() calls in the legend loop can make + # Qt re-run layout for the surrounding table on its own, which is + # the main source of the delay for metabolites with many reactions + # (many legend entries -> many small edits). Wrapped in one block, + # Qt defers that to a single pass once endEditBlock() is reached. + cursor.beginEditBlock() + cursor.insertBlock() + # Position of the very top of the newly inserted content, used below + # to scroll it into view once everything has been inserted. + top_of_new_content = cursor.position() + + # Fix both column widths so the table's total width is independent of + # the legend text content. A long reaction equation can then never + # grow the table (and therefore can no longer push the plot column + # around / shift the whole thing left) -- it can only wrap or, if + # it's excessive, get truncated within its own fixed-width cell. + legend_width_px = max( + 300, min(600, control.viewport().width() - image.width() - 40) + ) + table_format = QTextTableFormat() + table_format.setCellPadding(0) + table_format.setCellSpacing(12) + table_format.setBorder(0) + table_format.setColumnWidthConstraints([ + QTextLength(QTextLength.Type.FixedLength, image.width()), + QTextLength(QTextLength.Type.FixedLength, legend_width_px), + ]) + table = cursor.insertTable(1, 2, table_format) + + # Left cell: plot. The image is deliberately not rescaled based on + # console width; its pixel dimensions are fixed by the figure size. + plot_cursor = table.cellAt(0, 0).firstCursorPosition() + + # Register the image as a document resource so we can identify this + # particular inline plot later during mouse hit-testing. + control.document().addResource( + QTextDocument.ResourceType.ImageResource, + QUrl(image_id), + image, + ) + image_format = QTextImageFormat() + image_format.setName(image_id) + image_format.setWidth(image.width()) + image_format.setHeight(image.height()) + image_format.setVerticalAlignment( + QTextCharFormat.VerticalAlignment.AlignTop + ) + plot_cursor.insertImage(image_format) + + # Store the image dimensions and reaction hit boxes. The actual screen + # size is obtained from the image format during hit testing, allowing + # the method to remain correct if Qt scales the image. + self._console_plot_regions[image_id] = { + "width": image.width(), + "height": image.height(), + "image_width": image.width(), + "image_height": image.height(), + "regions": regions, + } + + # Right cell: clickable legend. + legend_cursor = table.cellAt(0, 1).firstCursorPosition() + + # Now that the cell has a fixed width (see above), plain line-wrapping + # is safe: it can only make an entry's block taller, never widen the + # table. Each block is still allowed to break, matching the default + # QTextBlockFormat. + line_format = QTextBlockFormat() + legend_cursor.setBlockFormat(line_format) + + def insert_link(text, href, bold=False, tooltip=None): + fmt = QTextCharFormat() + fmt.setAnchor(True) + fmt.setAnchorHref(href) + fmt.setToolTip(tooltip if tooltip is not None else "Click to select " + text) + fmt.setForeground(control.palette().color(QPalette.ColorRole.Link)) + if bold: + fmt.setFontWeight(QFont.Weight.Bold) + legend_cursor.insertText(text, fmt) + + legend_cursor.insertText("Metabolite: ") + insert_link( + metabolite_id, + "cnapy-metabolite:" + quote(metabolite_id, safe=""), + bold=True, + ) + legend_cursor.insertBlock(line_format) + legend_cursor.insertBlock(line_format) + + # Bound each entry's height instead of letting it wrap indefinitely: + # a reaction with many metabolites could otherwise wrap onto many + # lines and make the legend (and thus the whole plot/legend block) + # very tall. Budget roughly this many wrapped lines' worth of pixel + # width per entry; anything beyond that is replaced by a plain, + # non-clickable ellipsis whose tooltip shows the full equation. + max_equation_lines = 2 + cell_inner_width_px = max(100, legend_width_px - 20) + fm = QFontMetrics(control.font()) + + def insert_reaction_equation(rxn, budget_px): + """Insert ``rxn``'s equation with each metabolite id as a + clickable ``cnapy-metabolite:`` link, mirroring the metabolite + links in reactions_list.build_reaction_equation_html. Stops and + appends a tooltip-bearing "…" once the estimated rendered width + exceeds ``budget_px``, so one equation can't blow up the legend's + height. + """ + def side_segments(metabolites, sign): + segments = [] + for i, (met, coeff) in enumerate(metabolites.items()): + if i: + segments.append((" + ", None)) + coeff = coeff * sign + factor = "" if abs(coeff) == 1.0 else f"{abs(coeff):g} " + if factor: + segments.append((factor, None)) + segments.append((met.id, met.id)) + return segments + + reactants = {met: c for met, c in rxn.metabolites.items() if c < 0} + products = {met: c for met, c in rxn.metabolites.items() if c > 0} + segments = side_segments(reactants, -1) + if rxn.reversibility: + arrow = "<=>" + elif rxn.upper_bound <= 0: + arrow = "<--" + else: + arrow = "-->" + segments.append((" " + arrow + " ", None)) + segments.extend(side_segments(products, 1)) + + used_px = 0 + for i, (text, met_id) in enumerate(segments): + used_px += fm.horizontalAdvance(text) + if used_px > budget_px and i > 0: + ellipsis_fmt = QTextCharFormat() + ellipsis_fmt.setToolTip(rxn.build_reaction_string()) + legend_cursor.insertText("…", ellipsis_fmt) + return + if met_id is not None: + insert_link( + text, + "cnapy-metabolite:" + quote(met_id, safe=""), + tooltip="Click to select " + met_id, + ) + else: + legend_cursor.insertText(text) + + for index, (reaction_id, rxn, flux, patch) in enumerate(legend_entries): + marker_color = to_hex(patch.get_facecolor()) + + marker_fmt = QTextCharFormat() + marker_fmt.setForeground(QColor(marker_color)) + marker_fmt.setFontWeight(QFont.Weight.Bold) + legend_cursor.insertText("■ ", marker_fmt) + + insert_link( + reaction_id, + "cnapy-reaction:" + quote(reaction_id, safe=""), + bold=False, + ) + prefix = f": {flux:g} " + legend_cursor.insertText(prefix) + + used_px = ( + fm.horizontalAdvance("■ ") + + fm.horizontalAdvance(reaction_id) + + fm.horizontalAdvance(prefix) + ) + insert_reaction_equation( + rxn, max_equation_lines * cell_inner_width_px - used_px + ) + + if index != len(legend_entries) - 1: + legend_cursor.insertBlock(line_format) + + legend_cursor.insertBlock(line_format) + cursor.endEditBlock() + + if not insert_before_prompt: + # No prompt to preserve in this case -- keep the console's own + # cursor at the end of what was just appended, as before. + control.setTextCursor(legend_cursor) + # When inserting before an existing prompt, Qt automatically keeps + # every other QTextCursor tied to this document -- including + # qtconsole's own internal prompt cursor and the widget's visible + # input caret (and any text already typed there) -- correctly + # positioned relative to the content just inserted ahead of them, + # so nothing further needs to be done here. + + # Measure how tall the block we just inserted actually is (from its + # top down to where the prompt now starts), so the map/console + # splitter can be grown -- only if it actually needs to be -- to fit + # the whole thing without scrolling. + top_cursor = QTextCursor(document) + top_cursor.setPosition(min(top_of_new_content, document.characterCount() - 1)) + top_rect = control.cursorRect(top_cursor) + + bottom_pos = ( + console_widget._prompt_pos + if insert_before_prompt + else document.characterCount() - 1 + ) + bottom_cursor = QTextCursor(document) + bottom_cursor.setPosition(min(bottom_pos, document.characterCount() - 1)) + bottom_rect = control.cursorRect(bottom_cursor) + content_height = bottom_rect.bottom() - top_rect.top() + + self._ensure_console_plot_fits(content_height) + + # Re-measure after a possible splitter resize above: that changes + # the console pane's height (and therefore its scroll range), not + # its width, so the document's own line-wrapping is unaffected, but + # the cursor's position relative to the *current* scroll offset can + # shift slightly. + top_rect = control.cursorRect(top_cursor) + + # The cell width (and therefore the whole table) is now fixed, so + # long equations wrap within it instead of forcing a horizontal + # scroll. Scroll just far enough to bring the *top* of the newly + # inserted plot into view -- jumping straight to the scrollbar's + # maximum (as before) shows the bottom of the plot/legend (and the + # prompt below it) instead, forcing the user to scroll back up to + # see the top of the image. + vbar = control.verticalScrollBar() + margin = 8 + new_scroll_value = vbar.value() + top_rect.top() - margin + vbar.setValue(max(0, min(new_scroll_value, vbar.maximum()))) + + # The figure is no longer needed once its pixels are in the document. + plt.close(fig) + + def _ensure_console_plot_fits(self, required_height): + """Grow the console's share of the map/console splitter, if (and + only if) necessary, so a plot/legend block of ``required_height`` + pixels fits inside the console's visible area without scrolling. + + Space is borrowed from the panes above the console (the map view + and mode navigator) proportionally to their current size, leaving + them a reasonable minimum so they never fully collapse. + """ + splitter = getattr(self.central_widget, "splitter2", None) + control = self._console_control + console_widget = self.central_widget.console + if splitter is None or control is None: + return + + index = splitter.indexOf(console_widget) + if index == -1: + return + + sizes = splitter.sizes() + if len(sizes) <= index: + return + + # A small margin so the plot isn't flush against the console's own + # edges once it fits. + margin = 16 + needed = required_height + margin + viewport_height = control.viewport().height() + if needed <= viewport_height: + return # Already fits -- nothing to adjust. + + missing = needed - viewport_height + other_indices = [i for i in range(len(sizes)) if i != index] + other_total = sum(sizes[i] for i in other_indices) + + # Keep at least this much combined height for the panes above the + # console (map view + mode navigator), so they stay usable. + min_other_total = 300 + available = max(0, other_total - min_other_total) + grow = min(missing, available) + if grow <= 0: + return + + new_sizes = list(sizes) + for i in other_indices: + share = sizes[i] / other_total if other_total else 0 + new_sizes[i] = max(0, sizes[i] - round(grow * share)) + new_sizes[index] = sizes[index] + grow + splitter.setSizes(new_sizes) + + # ------------------------------------------------------------------ + # Hit-testing helpers + # ------------------------------------------------------------------ + def _find_console_image_at_position(self, pos): + """Return (image_id, image_rect) for the inline image under *pos*. + + Looking up the image by its document position, rather than relying on + ``cursorForPosition(pos).charFormat()`` at the mouse position, also + works in the right-hand part of a wide inline image where Qt can return + the surrounding table/cell format instead of the image format. + """ + control = self._console_control + if control is None: + return None, None + + document = control.document() + block = document.begin() + while block.isValid(): + iterator = block.begin() + while not iterator.atEnd(): + fragment = iterator.fragment() + if fragment.isValid() and fragment.charFormat().isImageFormat(): + image_format = fragment.charFormat().toImageFormat() + image_id = image_format.name() + if image_id in self._console_plot_regions: + cursor = QTextCursor(document) + cursor.setPosition(fragment.position()) + image_rect = control.cursorRect(cursor) + width = image_format.width() or self._console_plot_regions[image_id]["image_width"] + height = image_format.height() or self._console_plot_regions[image_id]["image_height"] + image_rect.setWidth(round(width)) + image_rect.setHeight(round(height)) + if image_rect.contains(pos): + return image_id, image_rect + iterator += 1 + block = block.next() + + return None, None + + def _reaction_at_console_plot_position(self, pos): + """Return the reaction ID under a point in the QtConsole viewport.""" + image_id, image_rect = self._find_console_image_at_position(pos) + if image_id is None: + return None + + plot = self._console_plot_regions.get(image_id) + if plot is None: + return None + + rel_x = pos.x() - image_rect.left() + rel_y = pos.y() - image_rect.top() + if rel_x < 0 or rel_y < 0: + return None + + displayed_width = image_rect.width() + displayed_height = image_rect.height() + if displayed_width <= 0 or displayed_height <= 0: + return None + if rel_x >= displayed_width or rel_y >= displayed_height: + return None + + scale_x = plot["image_width"] / displayed_width + scale_y = plot["image_height"] / displayed_height + px = rel_x * scale_x + py = rel_y * scale_y + + for x0, y0, x1, y1, reaction_id in plot["regions"]: + if x0 <= px <= x1 and y0 <= py <= y1: + return reaction_id + return None + + # ------------------------------------------------------------------ + # Event filter + # ------------------------------------------------------------------ + def eventFilter(self, watched, event): + """Handle clickable CNApy links and interactive inline flux plots.""" + console_control = self._console_control + console_viewport = self._console_viewport + if console_control is None or watched is not console_viewport: + return super().eventFilter(watched, event) + + if event.type() == QEvent.Type.MouseMove: + pos = event.position().toPoint() + + href = console_control.anchorAt(pos) + if href.startswith(("cnapy-reaction:", "cnapy-metabolite:")): + # Use the hand cursor only for the explicitly clickable IDs. + console_viewport.setCursor(Qt.CursorShape.PointingHandCursor) + QToolTip.hideText() + self._console_plot_tooltip_reaction = None + # Swallow the event instead of letting it propagate further. + # qtconsole's own ConsoleWidget installs an eventFilter on + # this same viewport (before ours) that unconditionally does + # QToolTip.showText(pos, anchor) on every MouseMove -- i.e. + # it immediately shows the raw "cnapy-metabolite:..." href as + # a tooltip. Because eventFilters run most-recently-installed + # first, returning True here stops that call from firing, so + # only Qt's own per-character tooltip (the friendly "Click to + # select ..." text set via QTextCharFormat.setToolTip(), + # shown through the unrelated QEvent.ToolTip mechanism) is + # ever displayed, instead of the raw href flashing first. + return True + + reaction_id = self._reaction_at_console_plot_position(pos) + if reaction_id is not None: + # Keep the standard arrow cursor over the plot. + console_viewport.setCursor(Qt.CursorShape.ArrowCursor) + # Stop propagation so the console cannot dismiss/reposition + # the tooltip on every move. + if reaction_id != self._console_plot_tooltip_reaction: + QToolTip.showText( + event.globalPosition().toPoint(), + reaction_id, + console_viewport, + QRect(), + 10000, + ) + self._console_plot_tooltip_reaction = reaction_id + return True + + if self._console_plot_tooltip_reaction is not None: + QToolTip.hideText() + self._console_plot_tooltip_reaction = None + console_viewport.setCursor(Qt.CursorShape.ArrowCursor) + return False + + if ( + event.type() == QEvent.Type.MouseButtonRelease + and event.button() == Qt.MouseButton.LeftButton + ): + pos = event.position().toPoint() + href = console_control.anchorAt(pos) + + if href.startswith("cnapy-reaction:"): + self.reactionClicked.emit( + unquote(href[len("cnapy-reaction:"):]) + ) + return True + + if href.startswith("cnapy-metabolite:"): + self.metaboliteClicked.emit( + unquote(href[len("cnapy-metabolite:"):]) + ) + return True + + reaction_id = self._reaction_at_console_plot_position(pos) + if reaction_id is not None: + self.reactionClicked.emit(reaction_id) + return True + + return super().eventFilter(watched, event) diff --git a/cnapy/gui_elements/main_window.py b/cnapy/gui_elements/main_window.py index aaa93f6b..0e2bc073 100644 --- a/cnapy/gui_elements/main_window.py +++ b/cnapy/gui_elements/main_window.py @@ -1,32 +1,35 @@ +import io import json import os import sys import traceback +#from copy import deepcopy from pathlib import Path from tempfile import TemporaryDirectory -from zipfile import BadZipFile, ZipFile +from zipfile import BadZipFile, ZipFile, ZIP_DEFLATED import pickle import xml.etree.ElementTree as ET from cnapy.flux_vector_container import FluxVectorContainer -from cnapy.core import multi_threaded_HiGHS_FVA +from cnapy.core import multi_threaded_HiGHS_FVA, replace_ids from cnapy.core_gui import model_optimization_with_exceptions, except_likely_community_model_error, get_last_exception_string, has_community_error_substring import cobra import optlang -from optlang_enumerator.cobra_cnapy import CNApyModel +from cnapy.cobra_cnapy import CNApyModel, stoichiometry_hash from optlang.symbolics import Zero import numpy as np +import importlib.resources as resources import cnapy.resources # Do not delete this import - it seems to be unused but in fact it provides the menu icons -import matplotlib.pyplot as plt from typing import Any, Dict import openpyxl +from openpyxl.utils.exceptions import InvalidFileException -from qtpy.QtCore import QFileInfo, Qt, Slot, QTimer, QSignalBlocker, QSize +from qtpy.QtCore import QFileInfo, Qt, Slot, QTimer, QSignalBlocker, QSize, QCoreApplication, QEvent, QEventLoop from qtpy.QtGui import QColor, QIcon, QKeySequence from qtpy.QtWidgets import (QAction, QActionGroup, QApplication, QFileDialog, QStyle, QMainWindow, QMessageBox, QToolBar, QShortcut, QStatusBar, QLabel) from qtpy.QtWebEngineWidgets import QWebEngineView -from cnapy.appdata import AppData, CnaMap +from cnapy.appdata import AppData, Scenario, MAP_VIEW_CNAPY, MAP_VIEW_ESCHER from cnapy.gui_elements.about_dialog import AboutDialog from cnapy.gui_elements.central_widget import CentralWidget, ModelTabIndex from cnapy.gui_elements.clipboard_calculator import ClipboardCalculator @@ -49,7 +52,9 @@ from cnapy.gui_elements.configuration_cplex_new import CplexNewConfigurationDialog from cnapy.gui_elements.configuration_gurobi import GurobiConfigurationDialog from cnapy.gui_elements.thermodynamics_dialog import ThermodynamicAnalysisTypes, ThermodynamicDialog +from cnapy.gui_elements.in_out_flux_console_plot import InOutFluxConsolePlot import cnapy.utils as utils +from cnapy.scenario_spreadsheet import ScenarioSpreadsheetError, write_scenario_xlsx SBML_suffixes = "*.xml *.sbml *.xml.gz *.sbml.gz *.xml.zip *.sbml.zip" @@ -73,6 +78,12 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.central_widget = CentralWidget(self) self.setCentralWidget(self.central_widget) + # Owns the clickable, inline in/out-flux plot feature (computing the + # plot, inserting it with a clickable legend into the console, and + # the event filter that keeps old plots in the scrollback + # interactive). See in_out_flux_console_plot.py. + self._in_out_flux_console_plot = InOutFluxConsolePlot(self.appdata, self.central_widget) + self.menu = self.menuBar() self.file_menu = self.menu.addMenu("&Project") @@ -81,16 +92,10 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.file_menu.addAction(new_project_action) new_project_action.triggered.connect(self.new_project) - new_project_from_sbml_action = QAction( - "New project from SBML...", self) - self.file_menu.addAction(new_project_from_sbml_action) - new_project_from_sbml_action.triggered.connect( - self.new_project_from_sbml) - - open_project_action = QAction("&Open project...", self) - open_project_action.setShortcut("Ctrl+O") - self.file_menu.addAction(open_project_action) - open_project_action.triggered.connect(self.open_project_dialog) + open_action = QAction("&Open project or SBML model...", self) + open_action.setShortcut("Ctrl+O") + self.file_menu.addAction(open_action) + open_action.triggered.connect(self.open_dialog) self.recent_cna_menu = self.file_menu.addMenu("Open recent") self.recent_cna_actions: Dict[str, QAction] = {} @@ -116,7 +121,7 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st exit_action = QAction("Exit", self) exit_action.setShortcut("Ctrl+Q") self.file_menu.addAction(exit_action) - exit_action.triggered.connect(self.exit_app) + exit_action.triggered.connect(self.close) # closeEvent asks about unsaved changes self.scenario_menu = self.menu.addMenu("Scenario") @@ -152,15 +157,21 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.scenario_menu.addAction(save_scenario_as_action) save_scenario_as_action.triggered.connect(self.save_scenario_as) - undo_scenario_action = QAction("Undo scenario flux values edit", self) - undo_scenario_action.setIcon(QIcon(":/icons/undo.png")) - self.scenario_menu.addAction(undo_scenario_action) - undo_scenario_action.triggered.connect(self.undo_scenario_edit) + save_scenario_template_action = QAction("Save scenario spreadsheet template...", self) + self.scenario_menu.addAction(save_scenario_template_action) + save_scenario_template_action.triggered.connect(self.save_scenario_template) - redo_scenario_action = QAction("Redo scenario flux values edit", self) - redo_scenario_action.setIcon(QIcon(":/icons/redo.png")) - self.scenario_menu.addAction(redo_scenario_action) - redo_scenario_action.triggered.connect(self.redo_scenario_edit) + self.undo_scenario_action = QAction("Undo scenario edit", self) + self.undo_scenario_action.setIcon(QIcon(":/icons/undo.png")) + self.scenario_menu.addAction(self.undo_scenario_action) + self.undo_scenario_action.triggered.connect(self.undo_scenario_edit) + + self.redo_scenario_action = QAction("Redo scenario edit", self) + self.redo_scenario_action.setIcon(QIcon(":/icons/redo.png")) + self.scenario_menu.addAction(self.redo_scenario_action) + self.redo_scenario_action.triggered.connect(self.redo_scenario_edit) + self.appdata.scenarioHistoryChanged.connect(self.update_undo_redo_actions) + self.update_undo_redo_actions() clear_scenario_action = QAction("Clear scenario", self) self.scenario_menu.addAction(clear_scenario_action) @@ -187,6 +198,12 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st set_model_bounds_to_scenario_action.triggered.connect( self.set_model_bounds_to_scenario) + add_scenario_reactions_to_model_action = QAction( + "Add scenario reactions to model", self) + self.scenario_menu.addAction(add_scenario_reactions_to_model_action) + add_scenario_reactions_to_model_action.triggered.connect( + self.add_scenario_reactions_to_model) + pin_scenario_reactions_action = QAction( "Pin all scenario reactions to top of reaction list", self) self.scenario_menu.addAction(pin_scenario_reactions_action) @@ -203,7 +220,7 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st update_action = QAction("Default Coloring", self) update_action.setIcon(QIcon(":/icons/default-color.png")) - update_action.triggered.connect(self.central_widget.update) + update_action.triggered.connect(self.centralWidget().set_default_colors) self.scenario_menu.addAction(self.heaton_action) self.scenario_menu.addAction(self.onoff_action) @@ -234,11 +251,14 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st save_fluxes_as_xlsx_action.triggered.connect(self.save_fluxes_as_xlsx) self.map_menu = self.menu.addMenu("Map") + # the action groups are only used to show/hide the actions depending on the map type self.cnapy_map_actions = QActionGroup(self) + self.cnapy_map_actions.setExclusive(False) separator = QAction(" CNApy map", self) separator.setSeparator(True) self.cnapy_map_actions.addAction(separator) self.escher_map_actions = QActionGroup(self) + self.escher_map_actions.setExclusive(False) separator = QAction(" Escher map", self) separator.setSeparator(True) self.escher_map_actions.addAction(separator) @@ -300,6 +320,10 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.dec_box_size_action.setEnabled(False) self.cnapy_map_actions.addAction(self.dec_box_size_action) + cnapy_zoom_canvas_action = QAction("Zoom to canvas", self) + cnapy_zoom_canvas_action.triggered.connect(self.zoom_to_canvas) + self.cnapy_map_actions.addAction(cnapy_zoom_canvas_action) + self.cnapy_screenshot_action = QAction("Take map view screenshot...", self) self.cnapy_screenshot_action.triggered.connect(self.take_screenshot_cnapy) self.cnapy_map_actions.addAction(self.cnapy_screenshot_action) @@ -315,16 +339,9 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.escher_map_actions.addAction(escher_export_png_action) escher_zoom_canvas_action = QAction("Zoom to canvas") - escher_zoom_canvas_action.triggered.connect( - lambda: self.centralWidget().map_tabs.currentWidget().page().runJavaScript("builder.map.zoom_extent_canvas()")) + escher_zoom_canvas_action.triggered.connect(self.zoom_to_canvas) self.escher_map_actions.addAction(escher_zoom_canvas_action) - # does not work as expected (TODO: why?), for now save JSON via Escher menu in edit mode - # escher_save_map_action = QAction("Save map JSON...") - # escher_save_map_action.triggered.connect( - # lambda: self.centralWidget().map_tabs.currentWidget().page().runJavaScript("builder.map.saveMap()")) - # self.escher_map_actions.addAction(escher_save_map_action) - escher_settings_action = QAction("Escher settings...") escher_settings_action.triggered.connect( lambda: self.centralWidget().map_tabs.currentWidget().page().runJavaScript(r"builder.passPropsSettingsMenu({display: true})")) @@ -393,6 +410,11 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st sd_action.triggered.connect(self.mcs) self.sd_menu.addAction(sd_action) self.mcs_dialog = None + self.sd_viewer = None + self.sd_computation = None + self.plot_space_dialog = None + self.optmdfpathway_dialog = None + self.thermodynamic_fba_dialog = None load_mcs_action = QAction("Load Minimal Cut Sets (legacy)...", self) self.sd_menu.addAction(load_mcs_action) @@ -414,7 +436,7 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.thermodynamic_menu = self.analysis_menu.addMenu("Thermodynamic analyses") - optmdf_action = QAction("OptMDFpathway...", self) + optmdf_action = QAction("OptMDFpathway with bottleneck analysis...", self) optmdf_action.triggered.connect(self.perform_optmdfpathway) self.thermodynamic_menu.addAction(optmdf_action) @@ -422,9 +444,9 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st tfba_action.triggered.connect(self.perform_thermodynamic_fba) self.thermodynamic_menu.addAction(tfba_action) - bottleneck_action = QAction("Thermodynamic bottleneck analysis...", self) - bottleneck_action.triggered.connect(self.perform_bottleneck_analysis) - self.thermodynamic_menu.addAction(bottleneck_action) + # bottleneck_action = QAction("Thermodynamic bottleneck analysis...", self) + # bottleneck_action.triggered.connect(self.perform_bottleneck_analysis) + # self.thermodynamic_menu.addAction(bottleneck_action) self.thermodynamic_menu.addSeparator() @@ -500,27 +522,27 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.config_menu = self.menu.addMenu("Config") config_action = QAction("Configure CNApy...", self) - config_action.setMenuRole(QAction.NoRole) + config_action.setMenuRole(QAction.MenuRole.NoRole) self.config_menu.addAction(config_action) config_action.triggered.connect(self.show_config_dialog) config_action = QAction("Configure COBRApy...", self) - config_action.setMenuRole(QAction.NoRole) + config_action.setMenuRole(QAction.MenuRole.NoRole) self.config_menu.addAction(config_action) config_action.triggered.connect(self.show_config_cobrapy_dialog) config_action = QAction("Configure IBM CPLEX Full Version (up to CPLEX version 22.1.1)...", self) - config_action.setMenuRole(QAction.NoRole) + config_action.setMenuRole(QAction.MenuRole.NoRole) self.config_menu.addAction(config_action) config_action.triggered.connect(self.show_cplex_configuration_dialog) config_action = QAction("Configure IBM CPLEX Full Version (for CPLEX versions >=22.1.2)...", self) - config_action.setMenuRole(QAction.NoRole) + config_action.setMenuRole(QAction.MenuRole.NoRole) self.config_menu.addAction(config_action) config_action.triggered.connect(self.show_new_cplex_configuration_dialog) config_action = QAction("Configure Gurobi Full Version...", self) - config_action.setMenuRole(QAction.NoRole) + config_action.setMenuRole(QAction.MenuRole.NoRole) self.config_menu.addAction(config_action) config_action.triggered.connect(self.show_gurobi_configuration_dialog) @@ -537,7 +559,7 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st show_model_view_action.triggered.connect(self.show_model_view) about_action = QAction("About CNApy...", self) - about_action.setMenuRole(QAction.NoRole) + about_action.setMenuRole(QAction.MenuRole.NoRole) self.config_menu.addAction(about_action) about_action.triggered.connect(self.show_about) @@ -549,6 +571,11 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st zoom_out_action.setIcon(QIcon(":/icons/zoom-out.png")) zoom_out_action.triggered.connect(self.zoom_out) + zoom_to_canvas_action = QAction("Zoom to canvas", self) + zoom_to_canvas_action.setToolTip("Fit the whole map into the view") + zoom_to_canvas_action.setIcon(QIcon(str(resources.files("cnapy") / "data" / "zoom-fit.svg"))) + zoom_to_canvas_action.triggered.connect(self.zoom_to_canvas) + self.set_current_filename("Untitled project") self.heaton_action.setCheckable(True) @@ -574,8 +601,8 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.tool_bar.addAction(save_scenario_as_action) self.tool_bar.addSeparator() self.tool_bar.addAction(clear_all_action) - self.tool_bar.addAction(undo_scenario_action) - self.tool_bar.addAction(redo_scenario_action) + self.tool_bar.addAction(self.undo_scenario_action) + self.tool_bar.addAction(self.redo_scenario_action) self.tool_bar.addSeparator() self.tool_bar.addAction(self.heaton_action) self.tool_bar.addAction(self.onoff_action) @@ -583,6 +610,7 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.tool_bar.addSeparator() self.tool_bar.addAction(zoom_in_action) self.tool_bar.addAction(zoom_out_action) + self.tool_bar.addAction(zoom_to_canvas_action) self.addToolBar(self.tool_bar) self.focus_search_action = QShortcut( @@ -599,7 +627,7 @@ def __init__(self, appdata: AppData, project_path: str | None, scenario_path: st self.centralWidget().map_tabs.currentChanged.connect(self.on_tab_change) if project_path is not None: - self.open_project(project_path) + self.open_file(project_path) if scenario_path is not None: self.load_scenario_file(scenario_path) @@ -615,24 +643,21 @@ def closeEvent(self, event): event.ignore() def checked_unsaved(self) -> bool: - # TODO: check for changes in Escher maps instead of just setting unsaved changes - # when acticvating the edit mode on an Escher map if self.appdata.unsaved: msgBox = QMessageBox() msgBox.setText("The project has been modified.") msgBox.setInformativeText("Do you want to save your changes?") msgBox.setStandardButtons( - QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) - msgBox.setDefaultButton(QMessageBox.Save) - ret = msgBox.exec_() - if ret == QMessageBox.Save: - # Save was clicked - self.save_project_as() - return True - if ret == QMessageBox.Discard: + QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard | QMessageBox.StandardButton.Cancel) + msgBox.setDefaultButton(QMessageBox.StandardButton.Save) + ret = msgBox.exec() + if ret == QMessageBox.StandardButton.Save: + # Save was clicked, only continue if the project was actually saved + return self.save_project() + if ret == QMessageBox.StandardButton.Discard: # Don't save was clicked return True - if ret == QMessageBox.Cancel: + if ret == QMessageBox.StandardButton.Cancel: return False return True @@ -643,7 +668,7 @@ def unsaved_changes(self): if len(self.appdata.project.name) == 0: shown_name = "Untitled project" else: - shown_name = QFileInfo(self.appdata.project.name).fileName() + shown_name = os.path.basename(self.appdata.project.name) self.setWindowTitle("CNApy - " + shown_name + ' - unsaved changes') @@ -654,19 +679,12 @@ def nounsaved_changes(self): if len(self.appdata.project.name) == 0: shown_name = "Untitled project" else: - shown_name = QFileInfo(self.appdata.project.name).fileName() + shown_name = os.path.basename(self.appdata.project.name) self.setWindowTitle("CNApy - " + shown_name) - def disable_enable_dependent_actions(self): - self.efm_action.setEnabled(False) - - @Slot() - def exit_app(self): - if self.checked_unsaved(): - # releases the memory map file if this is a FluxVectorMemmap - self.appdata.project.modes.clear() - QApplication.quit() + # def disable_enable_dependent_actions(self): + # self.efm_action.setEnabled(False) def set_current_filename(self, filename): self.appdata.project.name = filename @@ -674,19 +692,19 @@ def set_current_filename(self, filename): if len(self.appdata.project.name) == 0: shown_name = "Untitled project" else: - shown_name = QFileInfo(self.appdata.project.name).fileName() + shown_name = os.path.basename(filename) self.setWindowTitle("CNApy - " + shown_name) @Slot() def show_about(self): dialog = AboutDialog(self.appdata) - dialog.exec_() + dialog.exec() @Slot() def plot_space(self): - self.plot_space = PlotSpaceDialog(self.appdata) - self.plot_space.show() + self.plot_space_dialog = PlotSpaceDialog(self.appdata) + self.plot_space_dialog.show() # Strain design computation and viewing functions def strain_design(self): @@ -702,11 +720,11 @@ def strain_design_with_setup(self, sd_setup): def compute_strain_design(self,sd_setup): # launch progress viewer and computation thread self.sd_viewer = SDComputationViewer(self, self.appdata, sd_setup) - self.sd_viewer.show_sd_signal.connect(self.show_strain_designs_with_setup, Qt.QueuedConnection) + self.sd_viewer.show_sd_signal.connect(self.show_strain_designs_with_setup, Qt.ConnectionType.QueuedConnection) # connect signals to update progress self.sd_computation = SDComputationThread(self.appdata, sd_setup) - self.sd_computation.output_connector.connect(self.sd_viewer.receive_progress_text, Qt.QueuedConnection) - self.sd_computation.finished_computation.connect(self.sd_viewer.conclude_computation, Qt.QueuedConnection) + self.sd_computation.output_connector.connect(self.sd_viewer.receive_progress_text, Qt.ConnectionType.QueuedConnection) + self.sd_computation.finished_computation.connect(self.sd_viewer.conclude_computation, Qt.ConnectionType.QueuedConnection) self.sd_viewer.cancel_computation.connect(self.terminate_strain_design_computation) # show dialog and launch process # self.sd_viewer.exec() @@ -719,14 +737,11 @@ def open_selected_recent_project(self): QMessageBox.critical( self, "File not found", - "The selected recently opened .cna file could not be found. Possible reasons: The file was deleted, moved or renamed." + "The selected recently opened file could not be found. Possible reasons: The file was deleted, moved or renamed." ) return - if selected_last_project.endswith(".cna"): - if self.checked_unsaved(): - self.open_project(filename=selected_last_project) - else: - self.new_project_from_sbml(filename=selected_last_project) # calls self.checked_unsaved() + if self.checked_unsaved(): + self.open_file(selected_last_project) def update_recently_used_models(self, filename: str): if filename in self.appdata.recent_cna_files: @@ -779,18 +794,18 @@ def load_strain_designs(self): @Slot() def optimize_yield(self): dialog = YieldOptimizationDialog(self.appdata, self.centralWidget()) - dialog.exec_() + dialog.exec() @Slot() def optimize_flux(self): dialog = FluxOptimizationDialog(self.appdata, self.centralWidget()) - dialog.exec_() + dialog.exec() @Slot() def show_config_dialog(self, first_start=False): dialog = ConfigDialog(self, first_start) if not first_start: - dialog.exec_() + dialog.exec() @Slot() def show_config_cobrapy_dialog(self): @@ -800,23 +815,24 @@ def show_config_cobrapy_dialog(self): dialog.optlang_solver_set.connect(self.mcs_dialog.configure_solver_options) if self.sd_dialog is not None: dialog.optlang_solver_set.connect(self.sd_dialog.set_optlang_solver_text) - dialog.optlang_solver_set.connect(self.sd_dialog.configure_solver_options) - dialog.exec_() + dialog.optlang_solver_set.connect(lambda: self.sd_dialog.configure_solver_options( + self.sd_dialog.solver_buttons['group'].checkedButton())) + dialog.exec() @Slot() def show_cplex_configuration_dialog(self): dialog = CplexConfigurationDialog(self.appdata) - dialog.exec_() + dialog.exec() @Slot() def show_new_cplex_configuration_dialog(self): dialog = CplexNewConfigurationDialog(self.appdata) - dialog.exec_() + dialog.exec() @Slot() def show_gurobi_configuration_dialog(self): dialog = GurobiConfigurationDialog(self.appdata) - dialog.exec_() + dialog.exec() @Slot() def export_sbml(self): @@ -826,19 +842,19 @@ def export_sbml(self): if not filename or len(filename) == 0: return - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) try: - self.save_sbml(filename) + self.save_model(filename) except ValueError: exstr = get_last_exception_string() utils.show_unknown_error_box(exstr) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) @Slot() def download_examples(self): dialog = DownloadDialog(self.appdata) - dialog.exec_() + dialog.exec() @Slot() def load_box_positions(self): @@ -879,20 +895,30 @@ def load_scenario(self, merge=False): filename: str = dialog.getOpenFileName( caption="Load scenario", directory=self.appdata.last_scen_directory, - filter="*.scen *.val" + filter="Scenario files (*.scen *.val *.xlsx);;CNApy scenario (*.scen);;" + "Spreadsheet (*.xlsx);;CellNetAnalyzer scenario fluxes(*.val)" )[0] if not filename or len(filename) == 0 or not os.path.exists(filename): return self.load_scenario_file(filename, merge=merge) def load_scenario_file(self, filename, merge=False): - self.appdata.scenario_past.clear() - self.appdata.scenario_future.clear() + self.appdata.record_current_scenario() self.appdata.project.comp_values.clear() try: missing_reactions, incompatible_constraints, skipped_scenario_reactions = \ self.appdata.project.scen_values.load(filename, self.appdata, merge=merge) - except json.decoder.JSONDecodeError: + except ScenarioSpreadsheetError as e: + self.discard_failed_scenario_load() + max_lines = 30 + message = "\n".join(e.messages[:max_lines]) + if len(e.messages) > max_lines: + message += f"\n... and {len(e.messages) - max_lines} more" + QMessageBox.critical(self, 'Could not load scenario spreadsheet', + "The scenario was not loaded because of the following errors:\n" + message) + return + except (json.decoder.JSONDecodeError, BadZipFile, InvalidFileException, KeyError): + self.discard_failed_scenario_load() QMessageBox.critical( self, 'Could not open file', @@ -980,39 +1006,6 @@ def change_background(self, caption="Select a SVG file", directory=None): return filename - # the variant below requires both the Escher JSON and an exported SVG - # @Slot() - # def add_escher_map(self): - # dialog = QFileDialog(self) - # filename: str = dialog.getOpenFileName(caption="Select an Escher JSON file", - # directory=self.appdata.work_directory, filter="*.json")[0] - # if not filename or len(filename) == 0 or not os.path.exists(filename): - # return - # - # with open(filename) as fp: - # map_json = json.load(fp) - # - # map_name, map_idx = self.centralWidget().add_map(base_name=map_json[0]['map_name']) - # self.change_background(caption="Select a SVG file that corresponds to the previously loaded Escher JSON file") - # - # reaction_bigg_ids = dict() - # for r in self.appdata.project.cobra_py_model.reactions: - # bigg_id = r.annotation.get("bigg.reaction", None) - # if bigg_id is None: # if there is no BiGG ID in the annotation... - # bigg_id = r.id # ... use the reaction ID as proxy - # reaction_bigg_ids[bigg_id] = r.id - # - # offset_x = map_json[1]['canvas']['x'] - # offset_y = map_json[1]['canvas']['y'] - # self.appdata.project.maps[map_name]["boxes"] = dict() - # for r in map_json[1]["reactions"].values(): - # bigg_id = r["bigg_id"] - # if bigg_id in reaction_bigg_ids: - # self.appdata.project.maps[map_name]["boxes"][reaction_bigg_ids[bigg_id]] = [r["label_x"] - offset_x, r["label_y"] - offset_y] - # - # self.recreate_maps() - # self.centralWidget().map_tabs.setCurrentIndex(map_idx) - @Slot() def add_escher_map(self, annotation_key_for_id="bigg.reaction", strip_compartment=False): # maps gets a default name because an Escher SVG file does not contain the map name @@ -1086,7 +1079,7 @@ def change_map_name(self): '''Execute RenameMapDialog''' dialog = RenameMapDialog( self.appdata, self.centralWidget()) - dialog.exec_() + dialog.exec() @Slot() def inc_box_size(self): @@ -1132,18 +1125,19 @@ def zoom_out(self): if mv is not None: mv.zoom_out() + @Slot() + def zoom_to_canvas(self): + mv = self.centralWidget().map_tabs.currentWidget() + if mv is not None: + mv.zoom_to_canvas() + @Slot(bool) def set_escher_edit_mode(self, checked: bool): - # cannot use the parameter checked because in ActionGroup the entries are mutually exclusive - # and the policy cannot be changed in Python (why?) - now_enabled = not self.centralWidget().map_tabs.currentWidget().editing_enabled - self.centralWidget().map_tabs.currentWidget().enable_editing(now_enabled) - self.escher_edit_mode_action.setChecked(now_enabled) - self.unsaved_changes() # preliminary solution until checking for changes in Escher maps is implemented + self.centralWidget().map_tabs.currentWidget().enable_editing(checked) @Slot() def focus_search_box(self): - self.centralWidget().searchbar.setFocus() + self.centralWidget().current_searchbar().setFocus() @Slot() def save_box_positions(self): @@ -1164,37 +1158,60 @@ def save_scenario(self): self.appdata.project.scen_values.save(self.appdata.project.scen_values.file_name) self.update_scenario_file_name() + def discard_failed_scenario_load(self): + # restore the scenario that was recorded before loading and drop the entry of the failed load + self.undo_scenario_edit() + del self.appdata.scenario_history[self.appdata.current_scenario_index + 1:] + self.update_undo_redo_actions() + @Slot() def save_scenario_as(self): - filename: str = QFileDialog.getSaveFileName( - directory=self.appdata.last_scen_directory, filter="*.scen")[0] + filename, selected_filter = QFileDialog.getSaveFileName( + directory=self.appdata.last_scen_directory, + filter="CNApy scenario (*.scen);;Spreadsheet (*.xlsx)") if len(filename) > 0: - if not filename.endswith(".scen"): - filename += ".scen" + if not filename.endswith((".scen", ".xlsx")): + filename += ".xlsx" if "xlsx" in selected_filter else ".scen" self.appdata.project.scen_values.file_name = filename self.appdata.project.scen_values.save(filename) self.appdata.last_scen_directory = os.path.dirname(filename) self.update_scenario_file_name() + @Slot() + def save_scenario_template(self): + filename: str = QFileDialog.getSaveFileName( + directory=self.appdata.last_scen_directory, filter="Spreadsheet (*.xlsx)")[0] + if len(filename) > 0: + if not filename.endswith(".xlsx"): + filename += ".xlsx" + write_scenario_xlsx(Scenario(), filename, include_examples=True) + def undo_scenario_edit(self): ''' undo last edit in scenario history ''' - if len(self.appdata.scenario_past) > 0: - last = self.appdata.scenario_past.pop() - self.appdata.scenario_future.append(last) - self.appdata.recreate_scenario_from_history() + if self.appdata.current_scenario_index >= 1: + self.appdata.current_scenario_index -= 1 + self.appdata.project.scen_values = self.appdata.scenario_history[self.appdata.current_scenario_index] + self.update_undo_redo_actions() if self.appdata.auto_fba: self.fba() self.centralWidget().update() + self.centralWidget().scenario_tab.recreate_scenario_items() def redo_scenario_edit(self): ''' redo last undo of scenario history ''' - if len(self.appdata.scenario_future) > 0: - nex = self.appdata.scenario_future.pop() - self.appdata.scenario_past.append(nex) - self.appdata.recreate_scenario_from_history() + if self.appdata.current_scenario_index < len(self.appdata.scenario_history) - 1: + self.appdata.current_scenario_index += 1 + self.appdata.project.scen_values = self.appdata.scenario_history[self.appdata.current_scenario_index] + self.update_undo_redo_actions() if self.appdata.auto_fba: self.fba() self.centralWidget().update() + self.centralWidget().scenario_tab.recreate_scenario_items() + + def update_undo_redo_actions(self): + self.undo_scenario_action.setEnabled(self.appdata.current_scenario_index >= 1) + self.redo_scenario_action.setEnabled( + self.appdata.current_scenario_index < len(self.appdata.scenario_history) - 1) def clear_scenario(self): self.appdata.scen_values_clear() @@ -1239,34 +1256,47 @@ def load_default_scenario(self): def new_project(self): if self.checked_unsaved(): self.new_project_unchecked() - self.recreate_maps() - def new_project_unchecked(self): - self.delete_maps() - - self.centralWidget().mode_navigator.clear() - self.centralWidget().clear_model_item_history() - self.centralWidget().reaction_list.reaction_list.clear() + def new_project_unchecked(self, model: cobra.Model = None): + '''replaces the current project with an empty one (or one with the given model)''' self.close_project_dialogs() - - self.appdata.project.scen_values.clear() - self.appdata.scenario_past.clear() - self.appdata.scenario_future.clear() - - self.set_current_filename("Untitled project") + self.appdata.project.reset(model) + self.reset_gui_for_new_project() + self.set_current_filename("") self.nounsaved_changes() - @Slot() - def new_project_from_sbml(self, filename=''): - if self.checked_unsaved(): - if len(filename) == 0: - dialog = QFileDialog(self) - filename: str = dialog.getOpenFileName( - directory=self.appdata.work_directory, filter=SBML_suffixes)[0] - if not filename or len(filename) == 0 or not os.path.exists(filename): - return + def reset_gui_for_new_project(self): + '''brings the GUI in line with a project that was just reset (see ProjectData.reset) and filled''' + central = self.centralWidget() + central.search_result = None # belongs to the previous project + # first, because the following calls update the map tabs, which must belong to the new project + self.recreate_maps(keep_escher_edits=False) + central.mode_navigator.clear() + central.prune_model_item_history() + for item_list in (central.reaction_list, central.metabolite_list, central.gene_list): + item_list.last_selected = None + for search_bar in (central.reaction_searchbar, central.metabolite_searchbar, central.gene_searchbar): + search_bar.clear() + self.appdata.clear_scenario_history() + (reactions, values) = self.appdata.project.collect_default_scenario_values() + if len(reactions) > 0: + self.appdata.scen_values_set_multiple(reactions, values) + else: + self.appdata.add_scenario_to_history(Scenario()) + self.clear_status_bar() + self.update_scenario_file_name() + central.update(rebuild_all_tabs=True) - self.setCursor(Qt.BusyCursor) + def open_file(self, filename: str): + '''opens a CNApy project (.cna) or creates a new project from an SBML model, depending on the file ending''' + if filename.endswith(".cna"): + self.open_project(filename) + else: + self.load_sbml_as_new_project(filename) + + def load_sbml_as_new_project(self, filename: str): + self.setCursor(Qt.CursorShape.BusyCursor) + try: try: cobra_py_model = CNApyModel.read_sbml_model(filename) except cobra.io.sbml.CobraSBMLError: @@ -1274,80 +1304,70 @@ def new_project_from_sbml(self, filename=''): QMessageBox.warning( self, 'Could not read sbml.', exstr) return - self.new_project_unchecked() - self.appdata.project.cobra_py_model = cobra_py_model + self.new_project_unchecked(cobra_py_model) self.set_current_filename(filename) - - default_map = CnaMap("Map") - self.appdata.project.maps = {"Map": default_map} - self.recreate_maps() - self.centralWidget().update(rebuild_all_tabs=True) - self.update_scenario_file_name() self.update_recently_used_models(filename) - - self.setCursor(Qt.ArrowCursor) + finally: + self.setCursor(Qt.CursorShape.ArrowCursor) def open_project(self, filename): - self.close_project_dialogs() temp_dir = TemporaryDirectory() - - self.setCursor(Qt.BusyCursor) + base_path = Path(temp_dir.name) + QApplication.setOverrideCursor(Qt.BusyCursor) + QApplication.processEvents() try: with ZipFile(filename, 'r') as zip_ref: - zip_ref.extractall(temp_dir.name) - - box_positions_path = temp_dir.name+"/box_positions.json" - if not os.path.exists(box_positions_path): + arc_names = set(zip_ref.namelist()) + if "box_positions.json" not in arc_names: QMessageBox.critical( self, 'Could not open file', "File could not be opened as it does not seem to be a valid CNApy project, even though the file is a zip file. " "Maybe the file got the .cna ending for other reasons than being a CNApy project or the file is corrupted." ) - self.setCursor(Qt.ArrowCursor) return - with open(box_positions_path, 'r') as fp: + with zip_ref.open("box_positions.json") as fp: maps = json.load(fp) - count = 1 - for _name, m in maps.items(): - m["background"] = temp_dir.name + \ - "/map" + str(count) + ".svg" - count += 1 + # only the SVG backgrounds are extracted, everything else is read directly from the archive + count = 1 + for _name, m in maps.items(): + arc_name = "map" + str(count) + ".svg" + if arc_name in arc_names: + zip_ref.extract(arc_name, base_path) + m["background"] = str(base_path / arc_name) + count += 1 # load meta_data - with open(temp_dir.name+"/meta.json", 'r') as fp: + if "meta.json" not in arc_names: + raise FileNotFoundError("Project does not contain a meta.json file.") + with zip_ref.open("meta.json") as fp: meta_data = json.load(fp) try: - cobra_py_model = CNApyModel.read_sbml_model( - temp_dir.name + "/model.sbml") + if "model.json" in arc_names: + with zip_ref.open("model.json") as fp: + cobra_py_model = cobra.io.load_json_model(fp) + cobra_py_model.__class__ = CNApyModel # like in CNApyModel.read_sbml_model + elif "model.sbml" in arc_names: + with io.TextIOWrapper(zip_ref.open("model.sbml"), encoding="utf-8") as fp: + cobra_py_model = CNApyModel.read_sbml_model(fp) + else: + raise FileNotFoundError("Project does not contain a model.json or model.sbml file.") except cobra.io.sbml.CobraSBMLError: exstr = get_last_exception_string() QMessageBox.warning( self, 'Could not open project.', exstr) return + # the project file was read successfully, now replace the current project + self.close_project_dialogs() self.appdata.temp_dir = temp_dir + self.appdata.project.reset(cobra_py_model) self.appdata.project.maps = maps self.appdata.project.meta_data = meta_data - self.appdata.project.cobra_py_model = cobra_py_model self.set_current_filename(filename) - self.recreate_maps() - self.centralWidget().mode_navigator.clear() - self.centralWidget().clear_model_item_history() - self.centralWidget().reaction_list.last_selected = None - self.centralWidget().metabolite_list.last_selected = None - self.centralWidget().gene_list.last_selected = None - self.appdata.project.scen_values.clear() - self.appdata.project.comp_values.clear() - self.appdata.project.fva_values.clear() - self.appdata.scenario_past.clear() - self.appdata.scenario_future.clear() - self.clear_status_bar() - self.update_scenario_file_name() - (reactions, values) = self.appdata.project.collect_default_scenario_values() - if len(reactions) > 0: - self.appdata.scen_values_set_multiple(reactions, values) + self.appdata.last_scen_directory = os.path.dirname(filename) + self.reset_gui_for_new_project() self.nounsaved_changes() # if project contains maps move splitter and fit mapview @@ -1356,7 +1376,6 @@ def open_project(self, filename): self.centralWidget().splitter2.moveSplitter(round(r*0.8), 1) self.centralWidget().fit_mapview() - self.centralWidget().update(rebuild_all_tabs=True) self.update_recently_used_models(filename) except FileNotFoundError: @@ -1369,18 +1388,20 @@ def open_project(self, filename): "File could not be opened as it does not seem to be a valid CNApy project. " "Maybe the file got the .cna ending for other reasons than being a CNApy project or the file is corrupted." ) - - self.setCursor(Qt.ArrowCursor) + finally: + QApplication.restoreOverrideCursor() @Slot() - def open_project_dialog(self): + def open_dialog(self): if self.checked_unsaved(): dialog = QFileDialog(self) filename: str = dialog.getOpenFileName( - directory=self.appdata.work_directory, filter="*.cna")[0] + directory=self.appdata.work_directory, + filter=f"CNApy projects and SBML models (*.cna {SBML_suffixes});;" + f"CNApy projects (*.cna);;SBML models ({SBML_suffixes})")[0] if not filename or len(filename) == 0 or not os.path.exists(filename): return - self.open_project(filename) + self.open_file(filename) def close_project_dialogs(self): '''closes modeless dialogs''' @@ -1396,8 +1417,23 @@ def close_project_dialogs(self): if self.make_scenario_feasible_dialog is not None: self.make_scenario_feasible_dialog.close() self.make_scenario_feasible_dialog = None + for name in ("plot_space_dialog", "optmdfpathway_dialog", "thermodynamic_fba_dialog"): + dialog = getattr(self, name) + if dialog is not None: + dialog.close() + setattr(self, name, None) + # a strain design computation still running would deliver its results to the new project + if self.sd_computation is not None and self.sd_computation.isRunning(): + self.terminate_strain_design_computation() + self.sd_computation = None + if self.sd_viewer is not None: + try: + self.sd_viewer.close() + except RuntimeError: # already deleted by Qt (deleteLater after cancel/show) + pass + self.sd_viewer = None - def save_sbml(self, filename): + def save_model(self, filename, save_as_json: bool = False): '''Save model as SBML''' # cleanup to work around cobrapy not setting a default compartment @@ -1426,111 +1462,130 @@ def save_sbml(self, filename): self.appdata.project.cobra_py_model = clean_model - cobra.io.write_sbml_model( - self.appdata.project.cobra_py_model, filename) - - @Slot() - def save_project(self): - escher_map_count: int = 0 - semaphore = [0] # list with one integer to emulate pass by reference - for i in range(len(self.centralWidget().map_tabs)): - if isinstance(self.centralWidget().map_tabs.widget(i), EscherMapView): - self.centralWidget().map_tabs.widget(i).retrieve_map_data(semaphore=semaphore) - self.centralWidget().map_tabs.widget(i).retrieve_pos_and_zoom(semaphore=semaphore) - escher_map_count += 1 - if escher_map_count > 0: # give some time for retrieve_map_data to finish - escher_map_count *= 2 - timer = QTimer() - wait_count = 0 - timer.setInterval(escher_map_count*10) - def wait_for_retrieval(): - nonlocal wait_count - if semaphore[0] == escher_map_count: - timer.stop() - self.continue_save_project() - if wait_count >= 20: - timer.stop() - raise ValueError("Failed to retrieve Escher data, cannot save project.") - wait_count += 1 - timer.timeout.connect(wait_for_retrieval) - timer.start() + if save_as_json: + cobra.io.save_json_model(self.appdata.project.cobra_py_model, filename) else: - self.continue_save_project() + cobra.io.write_sbml_model(self.appdata.project.cobra_py_model, filename) + + def retrieve_escher_map_states(self, timeout_ms: int = 5000) -> bool: + '''Copies the current map data, position and zoom of all Escher maps into the project maps. + Waits until Escher has delivered them, returns False if this did not happen within the timeout.''' + escher_maps = [self.centralWidget().map_tabs.widget(i) for i in range(self.centralWidget().map_tabs.count())] + escher_maps = [m for m in escher_maps if isinstance(m, EscherMapView)] + if len(escher_maps) == 0: + return True + pending = len(escher_maps) + loop = QEventLoop() + def done(): + nonlocal pending + pending -= 1 + if pending == 0: + loop.quit() + for m in escher_maps: + m.retrieve_map_state(done) + if pending > 0: + QTimer.singleShot(timeout_ms, loop.quit) + loop.exec() # the results of runJavaScript are delivered through the event loop + return pending == 0 @Slot() - def continue_save_project(self): - ''' Save the project ''' - tmp_dir = TemporaryDirectory().name + def save_project(self) -> bool: + '''returns whether the project was saved''' + if not self.appdata.project.name.endswith(".cna"): # new project or imported SBML model + return self.save_project_as() + if not self.retrieve_escher_map_states(): + QMessageBox.warning(self, "Project not saved", + "Failed to retrieve the data of the Escher maps, cannot save the project.") + return False + return self.write_project_file() + + def write_project_file(self) -> bool: + ''' Save the project, the data of the Escher maps must already have been retrieved ''' filename: str = self.appdata.project.name + model_file: str = "model.json" if self.appdata.save_model_as_json else "model.sbml" - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) try: - self.save_sbml(tmp_dir + "model.sbml") - except ValueError: - exstr = get_last_exception_string() - utils.show_unknown_error_box(exstr) - - return + model_buffer = io.StringIO() + try: + self.save_model(model_buffer, save_as_json=self.appdata.save_model_as_json) + except ValueError: + exstr = get_last_exception_string() + utils.show_unknown_error_box(exstr) + return False - svg_files = {} - count = 1 - for name, m in self.appdata.project.maps.items(): - if m.get('view', 'cnapy') == 'cnapy': - arc_name = "map" + str(count) + ".svg" - svg_files[m["background"]] = arc_name - m["background"] = arc_name - count += 1 - - # Save maps information - # also contains the Escher map JSONs - with open(tmp_dir + "box_positions.json", 'w') as fp: - json.dump(self.appdata.project.maps, fp, skipkeys=True) - - # Save meta data - self.appdata.project.meta_data["format version"] = self.appdata.format_version - with open(tmp_dir + "meta.json", 'w') as fp: - json.dump(self.appdata.project.meta_data, fp) - - with ZipFile(filename, 'w') as zip_obj: - zip_obj.write(tmp_dir + "model.sbml", arcname="model.sbml") - zip_obj.write(tmp_dir + "box_positions.json", - arcname="box_positions.json") - zip_obj.write(tmp_dir + "meta.json", arcname="meta.json") - for name, m in svg_files.items(): - zip_obj.write(name, arcname=m) - - # put svgs into temporary directory and update references - with ZipFile(filename, 'r') as zip_ref: - zip_ref.extractall(self.appdata.temp_dir.name) + # the archive refers to the SVG backgrounds by their archive names, + # the project maps keep their paths until the archive has been written + svg_files = {} # archive name -> current path of the SVG file + maps_to_save = {} count = 1 for name, m in self.appdata.project.maps.items(): - m["background"] = self.appdata.temp_dir.name + \ - "/map" + str(count) + ".svg" + if m.get('view', MAP_VIEW_CNAPY) == MAP_VIEW_CNAPY: + arc_name = "map" + str(count) + ".svg" + svg_files[arc_name] = m["background"] + m = dict(m, background=arc_name) + maps_to_save[name] = m count += 1 - self.nounsaved_changes() - self.setCursor(Qt.ArrowCursor) + self.appdata.project.meta_data["format version"] = self.appdata.format_version + + # write to a temporary file first so that a failed save does not destroy the existing project file + tmp_filename = filename + ".tmp" + try: + # ZIP_DEFLATED only requires zlib which is available in all Python distributions + with ZipFile(tmp_filename, 'w', compression=ZIP_DEFLATED) as zip_obj: + zip_obj.writestr(model_file, model_buffer.getvalue()) + # maps information, also contains the Escher map JSONs + zip_obj.writestr("box_positions.json", json.dumps(maps_to_save, skipkeys=True)) + zip_obj.writestr("meta.json", json.dumps(self.appdata.project.meta_data)) + for arc_name, svg_path in svg_files.items(): + zip_obj.write(svg_path, arcname=arc_name) + os.replace(tmp_filename, filename) + except Exception: + if os.path.exists(tmp_filename): + os.remove(tmp_filename) + raise + + # put svgs into temporary directory and update references + with ZipFile(filename, 'r') as zip_ref: + for name, m in self.appdata.project.maps.items(): + arc_name = maps_to_save[name].get("background") + if arc_name in svg_files: + zip_ref.extract(arc_name, self.appdata.temp_dir.name) + m["background"] = str(Path(self.appdata.temp_dir.name) / arc_name) + + self.nounsaved_changes() + finally: + self.setCursor(Qt.CursorShape.ArrowCursor) + return True @Slot() - def save_project_as(self): + def save_project_as(self) -> bool: + '''returns whether the project was saved''' filename: str = QFileDialog.getSaveFileName( directory=self.appdata.work_directory, filter="*.cna")[0] - if len(filename) > 0: - if not filename.endswith(".cna"): - filename += ".cna" - self.set_current_filename(filename) - self.save_project() - self.update_recently_used_models(filename) + if len(filename) == 0: + return False + if not filename.endswith(".cna"): + filename += ".cna" + self.set_current_filename(filename) + if not self.save_project(): + return False + self.update_recently_used_models(filename) + return True - # TODO: are there really situations where _all_ maps need to be recreated? - def recreate_maps(self): + def recreate_maps(self, keep_escher_edits: bool = True): + '''keep_escher_edits: retrieve the current state of the Escher maps first because the map data in + the project is otherwise only updated on saving; must be False if the project maps have been replaced''' + if keep_escher_edits: + self.retrieve_escher_map_states() self.delete_maps() for name, mmap in self.appdata.project.maps.items(): - if mmap.get("view", "cnapy") == "cnapy": + if mmap.get("view", MAP_VIEW_CNAPY) == MAP_VIEW_CNAPY: mmap = MapView(self.appdata, self.centralWidget(), name) mmap.show() self.centralWidget().connect_map_view_signals(mmap) - elif mmap["view"] == "escher": + elif mmap["view"] == MAP_VIEW_ESCHER: mmap = EscherMapView(self.centralWidget(), name) self.centralWidget().connect_escher_map_view_signals(mmap) self.appdata.project.maps[name][EscherMapView] = mmap @@ -1539,6 +1594,13 @@ def recreate_maps(self): self.centralWidget().map_tabs.addTab(mmap, name) mmap.update() + def release_web_engine(self): + '''Deletes the Escher maps and then their web engine profile. Must be called before the QApplication + is destroyed, otherwise QtWebEngine may crash when pages are destroyed after their profile.''' + self.delete_maps() + QCoreApplication.sendPostedEvents(None, QEvent.Type.DeferredDelete) # deleteLater does not wait for the event loop + EscherMapView.release_web_engine_profile() + def delete_maps(self): with QSignalBlocker(self.centralWidget().map_tabs): for i in range(0, self.centralWidget().map_tabs.count()): @@ -1556,7 +1618,7 @@ def take_screenshot_cnapy(self): elif len(filename) <= 4 or filename[-4:] != ".png": filename += ".png" - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) scale_factor = 10.0 view = self.centralWidget().map_tabs.currentWidget() original_size = QSize(view.size()) @@ -1569,7 +1631,7 @@ def take_screenshot_cnapy(self): view.setTransform(view.transform().scale(1/scale_factor, 1/scale_factor)) view.resize(original_size) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) def on_tab_change(self, idx): if idx >= 0: @@ -1585,12 +1647,13 @@ def on_tab_change(self, idx): self.escher_map_actions.setVisible(False) self.cnapy_map_actions.setVisible(True) self.colorings.setEnabled(True) - self.central_widget.search_annotations.setEnabled(True) + #self.central_widget.search_annotations.setEnabled(True) else: # EscherMapView self.cnapy_map_actions.setVisible(False) self.escher_map_actions.setVisible(True) + self.escher_edit_mode_action.setChecked(self.centralWidget().map_tabs.widget(idx).editing_enabled) self.colorings.setEnabled(False) - self.central_widget.search_annotations.setEnabled(False) + #self.central_widget.search_annotations.setEnabled(False) else: self.change_map_name_action.setEnabled(False) self.change_background_action.setEnabled(False) @@ -1610,8 +1673,10 @@ def paste_clipboard(self): try: self.appdata.project.comp_values = self.appdata.clipboard_comp_values.copy() - for key in (self.appdata.project.scen_values.keys() & self.appdata.clipboard_comp_values.keys()): - self.appdata.project.scen_values[key] = self.appdata.clipboard_comp_values[key] + common_keys = list(self.appdata.project.scen_values.keys() & self.appdata.clipboard_comp_values.keys()) + if len(common_keys) > 0: + self.appdata.scen_values_set_multiple( + common_keys, [self.appdata.clipboard_comp_values[key] for key in common_keys]) except AttributeError: QMessageBox.warning( self, @@ -1624,7 +1689,7 @@ def paste_clipboard(self): @Slot() def clipboard_arithmetics(self): dialog = ClipboardCalculator(self.appdata) - dialog.exec_() + dialog.exec() self.centralWidget().update() def add_values_to_scenario(self): @@ -1643,9 +1708,40 @@ def set_model_bounds_to_scenario(self): reaction.upper_bound = vu self.centralWidget().update() + @Slot() + def add_scenario_reactions_to_model(self): + ''' moves all scenario reactions permanently into the model, clears the scenario history ''' + scen_values = self.appdata.project.scen_values + if len(scen_values.reactions) == 0: + QMessageBox.information(self, "No scenario reactions", + "The current scenario does not contain any scenario reactions.") + return + answer = QMessageBox.question(self, "Add scenario reactions to model", + "The following scenario reactions will be added to the model and removed from the scenario:\n" + + ", ".join(scen_values.reactions) + + "\n\nThis clears the scenario history, i.e. previous scenario edits cannot be undone anymore, " + "and the addition of the reactions to the model can only be undone by reloading it.\n\nDo you want to proceed?", + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, QMessageBox.StandardButton.No) + if answer != QMessageBox.StandardButton.Yes: + return + # scenario values, constraints, objective and annotations that refer to these reactions remain valid + scen_values.add_scenario_reactions_to_model(self.appdata.project.cobra_py_model) + scen_values.reactions = {} + # the history entries still contain the scenario reactions which would clash with the new model reactions + self.appdata.clear_scenario_history() + self.appdata.add_scenario_to_history(scen_values) + self.update_undo_redo_actions() + scen_values.has_unsaved_changes = True + self.appdata.unsaved_scenario_changes() + self.unsaved_changes() + self.appdata.project.update_reaction_id_lists() + self.centralWidget().update(rebuild_all_tabs=True) + @Slot() def pin_scenario_reactions(self): - self.centralWidget().reaction_list.pin_multiple(self.appdata.project.scen_values.keys()) + model_reactions = self.appdata.project.cobra_py_model.reactions + # scenario reactions can also have scenario values but are not in the reaction list + self.centralWidget().reaction_list.pin_multiple({r for r in self.appdata.project.scen_values if r in model_reactions}) def set_scenario_to_default_scenario(self): ''' write current scenario into sbml annotation ''' @@ -1704,6 +1800,8 @@ def make_scenario_feasible(self): else: self.make_scenario_feasible_dialog.modified_scenario = None self.make_scenario_feasible_dialog.show() + # have bm_reac_id_select always enabled even if the parent is not + self.make_scenario_feasible_dialog.bm_reac_id_select.setEnabled(True) def fba_optimize_reaction(self, reaction: str, mmin: bool): with self.appdata.project.cobra_py_model as model: @@ -1737,6 +1835,7 @@ def pfba(self): print(exstr) utils.show_unknown_error_box(exstr) else: + self.appdata.project.solution = solution if solution.status == 'optimal': soldict = solution.fluxes.to_dict() for i in soldict: @@ -1863,6 +1962,42 @@ def print_in_out_fluxes(self, metabolite): soldict = {id: val[0] for (id, val) in self.appdata.project.comp_values.items()} self.in_out_fluxes(metabolite, soldict) + def replace_ids(self, items: str, annotation_key: str, unambiguous_only: bool = False, + unique_only: bool = True, candidates_separator: str = ""): + """Console API: replace the IDs of the model's 'reactions' or 'metabolites' with IDs + taken from their annotation under annotation_key (see cnapy.core.replace_ids). + Returns a list of (old_id, new_id) pairs of the renamed entries.""" + if items not in ("reactions", "metabolites"): + raise ValueError("items must be 'reactions' or 'metabolites'") + project = self.appdata.project + dict_list = getattr(project.cobra_py_model, items) + old_ids = [entry.id for entry in dict_list] + replace_ids(dict_list, annotation_key, unambiguous_only=unambiguous_only, + unique_only=unique_only, candidates_separator=candidates_separator) + changed = [(old_id, entry) for old_id, entry in zip(old_ids, dict_list) if entry.id != old_id] + central_widget = self.centralWidget() + for old_id, entry in changed: + if items == "reactions": + scen_values = project.scen_values + if old_id in scen_values: + scen_values[entry.id] = scen_values.pop(old_id) + if old_id in scen_values.objective_coefficients: + scen_values.objective_coefficients[entry.id] = scen_values.objective_coefficients.pop(old_id) + if old_id in scen_values.pinned_reactions: + scen_values.pinned_reactions.discard(old_id) + scen_values.pinned_reactions.add(entry.id) + for values in (project.comp_values, project.fva_values): + if old_id in values: + values[entry.id] = values.pop(old_id) + central_widget.handle_changed_reaction(old_id, entry) + else: + central_widget.handle_changed_metabolite(entry, entry.reactions, old_id) + if len(changed) > 0: + project.scen_values.set_hash_value() + central_widget.update(rebuild_all_tabs=True) + print("Replaced the IDs of", len(changed), items) + return [(old_id, entry.id) for old_id, entry in changed] + def show_model_bounds(self): for reaction in self.appdata.project.cobra_py_model.reactions: self.appdata.project.comp_values[reaction.id] = ( @@ -1870,69 +2005,87 @@ def show_model_bounds(self): self.appdata.project.comp_values_type = 1 self.centralWidget().update() + def compute_fva_result(self, model, constraints=None): # -> Optional[Dict[str, Tuple[float, float]]]: + """Run (multi-threaded HiGHS) FVA on `model` and return + {reaction_id: (min_flux, max_flux)}, or None if the computation + didn't succeed (infeasible / solver error -- a message box is shown + to the user in that case). + + `model` is expected to already have the current scenario loaded + into it (via load_scenario_into_model), with the exception of + `constraints` (linear constraints), which multi_threaded_HiGHS_FVA + handles separately and must therefore be passed in here rather than + loaded into the model beforehand. Defaults to the project's current + scenario constraints if not given. + + This is the shared core of fva() (which runs it against the + project's own model/scenario and stores the result into + appdata.project.fva_values) and is also reused by ThermodynamicDialog + so that OptMDFpathway can be given the same FVA result -- including + the infinite-bound clipping and results-cache lookup below -- instead + of computing its own. + """ + if constraints is None: + constraints = [] + + fva_result = None + for r in model.reactions: + if r.lower_bound == -float('inf'): + r.lower_bound = cobra.Configuration().lower_bound + if r.upper_bound == float('inf'): + r.upper_bound = cobra.Configuration().upper_bound + if self.appdata.use_results_cache: + self.appdata.project.scen_values.set_hash_value() # call here for simplicity, but should in the long term be directly executed after scenario modification + fva_hash = hash((int(stoichiometry_hash(model).hexdigest(), 16), + hash(self.appdata.project.scen_values), hash(model.tolerance))) + file_path = self.appdata.results_cache_dir / (model.id+"_FVA_"+str(fva_hash)+".pkl") + + if Path.exists(file_path): + try: + with open(file_path, 'rb') as file: + fva_result = pickle.load(file) + self.statusBar().showMessage("Loaded FVA result from " + str(file_path)) + except: + self.statusBar().showMessage("Loading FVA result from " + str(file_path) + " failed, running FVA.") + + if not fva_result: + try: + lb, ub, dud = multi_threaded_HiGHS_FVA(model, constraints) + fva_result = (lb, ub) + if dud > 0: + QMessageBox.information(self, 'Incomplete FVA', 'Some flux limits could not be calculated.') + elif self.appdata.use_results_cache: + with open(file_path, 'wb') as file: + pickle.dump(fva_result, file) + self.statusBar().showMessage("Saved FVA result to " + str(file_path)) + except cobra.exceptions.Infeasible: + QMessageBox.information( + self, 'FVA not possible', 'The scenario is infeasible.') + return None + except Exception: + exstr = get_last_exception_string() + print(exstr) + utils.show_unknown_error_box(exstr) + return None + + if fva_result: + return {model.reactions[i].id: (fva_result[0][i], fva_result[1][i]) for i in range(len(model.reactions))} + return None + def fva(self): - QApplication.setOverrideCursor(Qt.BusyCursor) + QApplication.setOverrideCursor(Qt.CursorShape.BusyCursor) QApplication.processEvents() - fva_result = None + fva_result_dict = None with self.appdata.project.cobra_py_model as model: constraints = self.appdata.project.scen_values.constraints # do not load constraints into model, they are processed separately in multi_threaded_HiGHS_FVA self.appdata.project.scen_values.constraints = [] self.appdata.project.load_scenario_into_model(model) self.appdata.project.scen_values.constraints = constraints - if len(self.appdata.project.scen_values) > 0 or len(self.appdata.project.scen_values.reactions) > 0: - update_stoichiometry_hash = True - else: - update_stoichiometry_hash = False - for r in self.appdata.project.cobra_py_model.reactions: - if r.lower_bound == -float('inf'): - r.lower_bound = cobra.Configuration().lower_bound - if self.appdata.use_results_cache: - r.set_hash_value() - update_stoichiometry_hash = True - if r.upper_bound == float('inf'): - r.upper_bound = cobra.Configuration().upper_bound - if self.appdata.use_results_cache: - r.set_hash_value() - update_stoichiometry_hash = True - if self.appdata.use_results_cache: - if update_stoichiometry_hash: - model.set_stoichiometry_hash_object() - fva_hash = model.stoichiometry_hash_object.copy() - if len(self.appdata.project.scen_values.constraints) > 0: - fva_hash.update(pickle.dumps(sorted(self.appdata.project.scen_values.constraints))) - fva_hash.update(pickle.dumps(model.tolerance)) - file_path = self.appdata.results_cache_dir / (model.id+"_FVA_"+fva_hash.hexdigest()+".pkl") - - if Path.exists(file_path): - try: - with open(file_path, 'rb') as file: - fva_result = pickle.load(file) - self.statusBar().showMessage("Loaded FVA result from " + str(file_path)) - except: - self.statusBar().showMessage("Loading FVA result from " + str(file_path) + " failed, running FVA.") - - if not fva_result: - try: - lb, ub, dud = multi_threaded_HiGHS_FVA(model, self.appdata.project.scen_values.constraints) - fva_result = (lb, ub) - if dud > 0: - QMessageBox.information(self, 'Incomplete FVA', 'Some flux limits could not be calculated.') - elif self.appdata.use_results_cache: - with open(file_path, 'wb') as file: - pickle.dump(fva_result, file) - self.statusBar().showMessage("Saved FVA result to " + str(file_path)) - except cobra.exceptions.Infeasible: - QMessageBox.information( - self, 'FVA not possible', 'The scenario is infeasible.') - except Exception: - exstr = get_last_exception_string() - print(exstr) - utils.show_unknown_error_box(exstr) + fva_result_dict = self.compute_fva_result(model, constraints) - if fva_result: - self.appdata.project.comp_values = { - model.reactions[i].id: (fva_result[0][i], fva_result[1][i]) for i in range(len(model.reactions))} + if fva_result_dict: + self.appdata.project.comp_values = fva_result_dict self.appdata.project.fva_values = self.appdata.project.comp_values.copy() self.appdata.project.comp_values_type = 1 @@ -1942,12 +2095,12 @@ def fva(self): # def efm(self): # self.efm_dialog = EFMDialog( # self.appdata, self.centralWidget()) - # self.efm_dialog.exec_() + # self.efm_dialog.exec() def in_out_flux(self): in_out_flux_dialog = InOutFluxDialog( self.appdata) - in_out_flux_dialog.exec_() + in_out_flux_dialog.exec() def all_in_out_fluxes(self): filename = self._get_filename("xlsx") @@ -2080,7 +2233,7 @@ def all_in_out_fluxes(self): def efmtool(self): self.efmtool_dialog = EFMtoolDialog( self.appdata, self.centralWidget()) - self.efmtool_dialog.exec_() + self.efmtool_dialog.exec() def mcs(self): if self.mcs_dialog is None: @@ -2094,56 +2247,7 @@ def set_heaton(self): self.centralWidget().set_heaton() def in_out_fluxes(self, metabolite_id, soldict): - self.centralWidget().kernel_client.execute('%matplotlib inline', store_history=False) - with self.appdata.project.cobra_py_model as model: - self.appdata.project.scen_values.add_scenario_reactions_to_model(model) - met = model.metabolites.get_by_id(metabolite_id) - fig, ax = plt.subplots() - ax.set_xticks([1, 2]) - ax.set_xticklabels(['In', 'Out']) - cons = [] - prod = [] - sum_cons = 0 - sum_prod = 0 - for rxn in met.reactions: - flux = soldict.get(rxn.id, 0.0) - if abs(flux) > model.tolerance: - flux *= rxn.get_coefficient(metabolite_id) - if flux < 0: - cons.append((rxn, -flux)) - elif flux > 0: - prod.append((rxn, flux)) - cons = sorted(cons, key=lambda x: x[1], reverse=True) - prod = sorted(prod, key=lambda x: x[1], reverse=True) - for rxn, flux in prod: - ax.bar(1, flux, width=0.8, bottom=sum_prod, label=rxn.id+": "+rxn.build_reaction_string()) - sum_prod += flux - for rxn, flux in cons: - ax.bar(2, flux, width=0.8, bottom=sum_cons, label=rxn.id+": "+rxn.build_reaction_string()) - sum_cons += flux - ax.set_ylabel('Flux') - ax.set_title('In/Out fluxes at metabolite ' + metabolite_id) - ax.legend(bbox_to_anchor=(1, 1), loc="upper left") - - # Print plot in CNApy's console - plt.show() - - # Pretty print cons and prod lists of tuples - pretty_prod_dict = f"\nProducing reactions of {metabolite_id}:\n"+json.dumps({ - x[0].id: x[1] - for x in prod - }, indent=2) - pretty_cons_dict = f"\nConsuming reactions of {metabolite_id}:\n"+json.dumps({ - x[0].id: x[1] - for x in cons - }, indent=2) - # The next print statements are directly executed in CNApy's Jupyter console - print(pretty_prod_dict) - print(pretty_cons_dict) - - self.centralWidget().kernel_client.execute('%matplotlib qt', store_history=False) - - return prod, cons + return self._in_out_flux_console_plot.in_out_fluxes(metabolite_id, soldict) def show_console(self): print("show model view") @@ -2178,33 +2282,33 @@ def set_status_unknown(self): @Slot() def perform_optmdfpathway(self): - # Has to be in self to keep computation thread self.optmdfpathway_dialog = ThermodynamicDialog( self.appdata, self.centralWidget(), + self, analysis_type=ThermodynamicAnalysisTypes.OPTMDFPATHWAY ) - self.optmdfpathway_dialog.exec_() + self.optmdfpathway_dialog.show() @Slot() def perform_thermodynamic_fba(self): - # Has to be in self to keep computation thread self.thermodynamic_fba_dialog = ThermodynamicDialog( self.appdata, self.centralWidget(), + self, analysis_type=ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA ) - self.thermodynamic_fba_dialog.exec_() + self.thermodynamic_fba_dialog.show() - @Slot() - def perform_bottleneck_analysis(self): - # Has to be in self to keep computation thread - self.bottleneck_dialog = ThermodynamicDialog( - self.appdata, - self.centralWidget(), - analysis_type=ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS - ) - self.bottleneck_dialog.exec_() + # @Slot() + # def perform_bottleneck_analysis(self): + # # Has to be in self to keep computation thread + # self.bottleneck_dialog = ThermodynamicDialog( + # self.appdata, + # self.centralWidget(), + # analysis_type=ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS + # ) + # self.bottleneck_dialog.exec_() def _load_json(self) -> Dict[Any, Any]: dialog = QFileDialog(self) diff --git a/cnapy/gui_elements/map_view.py b/cnapy/gui_elements/map_view.py index 6e1cd1af..047d962c 100644 --- a/cnapy/gui_elements/map_view.py +++ b/cnapy/gui_elements/map_view.py @@ -6,10 +6,10 @@ from typing import Dict, Tuple from qtpy.QtCore import QMimeData, QRectF, Qt, Signal, Slot -from qtpy.QtGui import QPalette, QPen, QColor, QDrag, QMouseEvent, QKeyEvent, QPainter, QFont -from qtpy.QtSvg import QGraphicsSvgItem -from qtpy.QtWidgets import (QApplication, QAction, QGraphicsItem, QGraphicsScene, - QGraphicsSceneDragDropEvent, QTreeWidget, +from qtpy.QtGui import QAction, QPalette, QPen, QColor, QDrag, QMouseEvent, QKeyEvent, QPainter, QFont +from qtpy.QtSvgWidgets import QGraphicsSvgItem +from qtpy.QtWidgets import (QApplication, QGraphicsItem, QGraphicsScene, + QGraphicsSceneDragDropEvent, QAbstractItemView, QGraphicsSceneMouseEvent, QGraphicsView, QLineEdit, QMenu, QWidget, QGraphicsProxyWidget) @@ -29,12 +29,12 @@ def __init__(self, appdata: AppData, central_widget, name: str): self.background: QGraphicsSvgItem = None palette = self.palette() if appdata.is_in_dark_mode: - palette.setColor(QPalette.Base, QColor(90, 90, 90)) # Map etc. backgrounds + palette.setColor(QPalette.ColorRole.Base, QColor(90, 90, 90)) # Map etc. backgrounds else: - palette.setColor(QPalette.Base, QColor(250, 250, 250)) # Map etc. backgrounds + palette.setColor(QPalette.ColorRole.Base, QColor(250, 250, 250)) # Map etc. backgrounds self.setPalette(palette) self.setInteractive(True) - self.setDragMode(QGraphicsView.ScrollHandDrag) + self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag) self.appdata = appdata self.central_widget = central_widget self.name: str = name @@ -45,6 +45,7 @@ def __init__(self, appdata: AppData, central_widget, name: str): self.previous_point = None self.select = False self.select_start = None + self.leaving = False # True while leaveEvent removes the focus from the map # initial scale self._zoom = self.appdata.project.maps[self.name]["zoom"] @@ -80,7 +81,7 @@ def dragMoveEvent(self, event: QGraphicsSceneDragDropEvent): r_id = event.mimeData().text() if r_id in self.appdata.project.maps[self.name]["boxes"].keys(): - if isinstance(event.source(), QTreeWidget): # existing/continued drag from reaction list + if isinstance(event.source(), QAbstractItemView): # existing/continued drag from reaction list self.appdata.project.maps[self.name]["boxes"][r_id] = (point_item.x(), point_item.y()) self.mapChanged.emit(r_id) else: @@ -108,12 +109,12 @@ def dropEvent(self, event: QGraphicsSceneDragDropEvent): identifier = event.mimeData().text() self.mapChanged.emit(identifier) self.scene.setSceneRect(self.scene.itemsBoundingRect()) - self.viewport().setCursor(Qt.OpenHandCursor) + self.viewport().setCursor(Qt.CursorShape.OpenHandCursor) self.update() def wheelEvent(self, event): modifiers = QApplication.queryKeyboardModifiers() - if modifiers == Qt.ControlModifier: + if modifiers == Qt.KeyboardModifier.ControlModifier: if event.angleDelta().y() > 0: self.appdata.project.maps[self.name]["box-size"] *= INCREASE_FACTOR else: @@ -128,7 +129,13 @@ def wheelEvent(self, event): self.zoom_out() def fit(self): - self.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio) + self.fitInView(self.scene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio) + + def zoom_to_canvas(self): + self.fit() + # keep the zoom level in line with the scale set by fit + self._zoom = round(math.log(self.transform().m11()) / math.log(INCREASE_FACTOR)) + self.appdata.project.maps[self.name]["zoom"] = self._zoom def zoom_in(self): self._zoom += 1 @@ -143,15 +150,15 @@ def zoom_out(self): self.scale(DECREASE_FACTOR, DECREASE_FACTOR) def keyPressEvent(self, event: QKeyEvent): - if not self.drag_map and event.key() in (Qt.Key_Control, Qt.Key_Shift): - self.viewport().setCursor(Qt.ArrowCursor) + if not self.drag_map and event.key() in (Qt.Key.Key_Control, Qt.Key.Key_Shift): + self.viewport().setCursor(Qt.CursorShape.ArrowCursor) self.select = True else: super().keyPressEvent(event) def keyReleaseEvent(self, event: QKeyEvent): - if self.select and QApplication.mouseButtons() != Qt.LeftButton and event.key() in (Qt.Key_Control, Qt.Key_Shift): - self.viewport().setCursor(Qt.OpenHandCursor) + if self.select and QApplication.mouseButtons() != Qt.MouseButton.LeftButton and event.key() in (Qt.Key.Key_Control, Qt.Key.Key_Shift): + self.viewport().setCursor(Qt.CursorShape.OpenHandCursor) self.select = False else: super().keyReleaseEvent(event) @@ -159,22 +166,22 @@ def keyReleaseEvent(self, event: QKeyEvent): def mousePressEvent(self, event: QMouseEvent): if self.hasFocus(): if self.select: # select multiple boxes - self.setDragMode(QGraphicsView.RubberBandDrag) # switches to ArrowCursor + self.setDragMode(QGraphicsView.DragMode.RubberBandDrag) # switches to ArrowCursor self.select_start = self.mapToScene(event.pos()) else: # drag entire map - self.viewport().setCursor(Qt.ClosedHandCursor) - self.setDragMode(QGraphicsView.ScrollHandDrag) + self.viewport().setCursor(Qt.CursorShape.ClosedHandCursor) + self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag) self.drag_map = True super(MapView, self).mousePressEvent(event) # generates events for the graphics scene items def mouseReleaseEvent(self, event: QMouseEvent): if self.drag_map: - self.viewport().setCursor(Qt.OpenHandCursor) + self.viewport().setCursor(Qt.CursorShape.OpenHandCursor) self.drag_map = False if self.select: modifiers = QApplication.keyboardModifiers() - if modifiers != Qt.ControlModifier and modifiers != Qt.ShiftModifier: - self.viewport().setCursor(Qt.OpenHandCursor) + if modifiers != Qt.KeyboardModifier.ControlModifier and modifiers != Qt.KeyboardModifier.ShiftModifier: + self.viewport().setCursor(Qt.CursorShape.OpenHandCursor) self.select = False point = self.mapToScene(event.pos()) @@ -195,7 +202,7 @@ def mouseReleaseEvent(self, event: QMouseEvent): def focusOutEvent(self, event): super(MapView, self).focusOutEvent(event) - self.viewport().setCursor(Qt.OpenHandCursor) + self.viewport().setCursor(Qt.CursorShape.OpenHandCursor) self.select = False def enterEvent(self, event) -> None: @@ -210,7 +217,12 @@ def enterEvent(self, event) -> None: def leaveEvent(self, event) -> None: super().leaveEvent(event) - self.scene.clearFocus() # finishes editing of potentially active ReactionBox + # finishes editing of potentially active ReactionBox; the box stays selected + self.leaving = True + try: + self.scene.clearFocus() + finally: + self.leaving = False def update_selected(self, found_ids): @@ -223,6 +235,10 @@ def update_selected(self, found_ids): box.item.setHidden(False) + def reset_search_highlight(self): + for box in self.reaction_boxes.values(): + box.item.setHidden(False) + def focus_reaction(self, reaction: str): x = self.appdata.project.maps[self.name]["boxes"][reaction][0] y = self.appdata.project.maps[self.name]["boxes"][reaction][1] @@ -244,10 +260,11 @@ def highlight_reaction(self, string): treffer.item.setFocus() def select_single_reaction(self, reac_id: str): + # also deselect the previously selected box when the reaction has no box on this map + self.scene.clearSelection() + self.scene.clearFocus() box: ReactionBox = self.reaction_boxes.get(reac_id, None) if box is not None: - self.scene.clearSelection() - self.scene.clearFocus() box.setSelected(True) def set_background(self): @@ -387,7 +404,8 @@ def __init__(self, parent): def focusOutEvent(self, event): super().focusOutEvent(event) - self.parent.setSelected(False) + if not self.parent.map.leaving: # focus lost only because the mouse left the map + self.parent.setSelected(False) if self.isModified() and self.parent.map.appdata.auto_fba: self.parent.map.central_widget.parent.fba() self.parent.update() @@ -433,7 +451,7 @@ def __init__(self, parent: MapView, r_id: str, name): point_size = font.pointSize() font.setPointSizeF(point_size+13.0) self.item.setFont(font) - self.item.setAttribute(Qt.WA_TranslucentBackground) + self.item.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) self.item.setFixedWidth(self.map.appdata.box_width) self.item.setMaximumHeight(self.map.appdata.box_height) @@ -451,12 +469,12 @@ def __init__(self, parent: MapView, r_id: str, name): self.set_default_style() - self.setCursor(Qt.OpenHandCursor) - self.setAcceptedMouseButtons(Qt.LeftButton) + self.setCursor(Qt.CursorShape.OpenHandCursor) + self.setAcceptedMouseButtons(Qt.MouseButton.LeftButton) self.item.textEdited.connect(self.value_changed) self.item.returnPressed.connect(self.returnPressed) - self.item.setContextMenuPolicy(Qt.CustomContextMenu) + self.item.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) self.item.customContextMenuRequested.connect(self.on_context_menu) # create context menu @@ -491,20 +509,20 @@ def mousePressEvent(self, event: QGraphicsSceneMouseEvent): else: self.setSelected(True) else: - self.setCursor(Qt.ClosedHandCursor) + self.setCursor(Qt.CursorShape.ClosedHandCursor) def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent): event.accept() self.ungrabMouse() if not self.map.select: - self.setCursor(Qt.OpenHandCursor) + self.setCursor(Qt.CursorShape.OpenHandCursor) super().mouseReleaseEvent(event) # here deselection of the other boxes occurs def hoverEnterEvent(self, event): if self.map.select: - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) else: - self.setCursor(Qt.OpenHandCursor) + self.setCursor(Qt.CursorShape.OpenHandCursor) super().hoverEnterEvent(event) def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent): @@ -513,7 +531,7 @@ def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent): mime = QMimeData() mime.setText(str(self.id)) drag.setMimeData(mime) - drag.exec_() + drag.exec() def add_line_widget(self): self.proxy = self.map.scene.addWidget(self.item) @@ -533,24 +551,28 @@ def handle_editing_finished(self): def value_changed(self): test = self.item.text().replace(" ", "") if test == "": - if not self.item.accept_next_change_into_history: - if len(self.map.appdata.scenario_past) > 0: - self.map.appdata.scenario_past.pop() # replace previous change - self.item.accept_next_change_into_history = False - self.map.value_changed(self.id, test) + if self.id in self.map.appdata.project.scen_values: + if not self.item.accept_next_change_into_history: + if len(self.map.appdata.scenario_history) > 0: + self.map.appdata.current_scenario_index -= 1 # replace previous change + self.item.accept_next_change_into_history = False + self.map.value_changed(self.id, test) self.set_default_style() - elif validate_value(self.item.text()): - if not self.item.accept_next_change_into_history: - if len(self.map.appdata.scenario_past) > 0: - self.map.appdata.scenario_past.pop() # replace previous change - self.item.accept_next_change_into_history = False - self.map.value_changed(self.id, self.item.text()) - if self.id in self.map.appdata.project.scen_values.keys(): - self.set_scen_style() - else: - self.set_comp_style() else: - self.set_error_style() + new_value = validate_value(self.item.text()) + if new_value: + if new_value != self.map.appdata.project.scen_values.get(self.id, None): + if not self.item.accept_next_change_into_history: + if len(self.map.appdata.scenario_history) > 0: + self.map.appdata.current_scenario_index -= 1 # replace previous change + self.item.accept_next_change_into_history = False + self.map.value_changed(self.id, self.item.text()) + if self.id in self.map.appdata.project.scen_values.keys(): + self.set_scen_style() + else: + self.set_comp_style() + else: + self.set_error_style() def set_default_style(self): ''' set the reaction box to error style''' @@ -560,14 +582,14 @@ def set_default_style(self): color.setAlphaF(0.4) palette.setColor(role, color) role = self.item.foregroundRole() - palette.setColor(role, Qt.black) + palette.setColor(role, Qt.GlobalColor.black) self.item.setPalette(palette) self.set_font_style(QFont.StyleNormal) def set_error_style(self): ''' set the reaction box to error style''' - self.set_color(Qt.white) + self.set_color(Qt.GlobalColor.white) self.set_fg_color(self.map.appdata.scen_color_bad) self.set_font_style(QFont.StyleOblique) @@ -604,9 +626,9 @@ def recolor(self): if math.isclose(vl, vu, abs_tol=self.map.appdata.abs_tol): if self.map.appdata.modes_coloring: if vl == 0: - self.set_color(Qt.red) + self.set_color(Qt.GlobalColor.red) else: - self.set_color(Qt.green) + self.set_color(Qt.GlobalColor.green) else: self.set_comp_style() else: @@ -626,7 +648,7 @@ def set_color(self, color: QColor): role = self.item.backgroundRole() palette.setColor(role, color) role = self.item.foregroundRole() - palette.setColor(role, Qt.black) + palette.setColor(role, Qt.GlobalColor.black) self.item.setPalette(palette) def set_font_style(self, style: QFont.Style): @@ -679,10 +701,10 @@ def paint(self, painter: QPainter, _option, _widget: QWidget): painter.drawEllipse(-15, -15, 20, 20) else: - painter.setPen(Qt.darkGray) + painter.setPen(Qt.GlobalColor.darkGray) painter.drawEllipse(-15, -15, 20, 20) - painter.setPen(Qt.darkGray) + painter.setPen(Qt.GlobalColor.darkGray) painter.drawLine(-5, 0, -5, -10) painter.drawLine(0, -5, -10, -5) @@ -694,7 +716,7 @@ def setPos(self, x, y): def on_context_menu(self, point): # show context menu - self.pop_menu.exec_(self.item.mapToGlobal(point)) + self.pop_menu.exec(self.item.mapToGlobal(point)) def position(self): position_dialog = BoxPositionDialog(self, self.map) @@ -732,10 +754,10 @@ def validate_value(value): try: (vl, vh) = make_tuple(value) if isinstance(vl, (int, float)) and isinstance(vh, (int, float)) and vl <= vh: - return True + return (vl, vh) else: return False except (ValueError, SyntaxError, TypeError): return False else: - return True + return (_x, _x) diff --git a/cnapy/gui_elements/mcs_dialog.py b/cnapy/gui_elements/mcs_dialog.py index 29ec5551..f0b687b1 100644 --- a/cnapy/gui_elements/mcs_dialog.py +++ b/cnapy/gui_elements/mcs_dialog.py @@ -40,10 +40,10 @@ def __init__(self, appdata: AppData, central_widget): self.target_list = QTableWidget(1, 4) self.target_list.setHorizontalHeaderLabels( ["region no.", "T", "≥/≤", "t"]) - self.target_list.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) - self.target_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) + self.target_list.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) + self.target_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) self.target_list.horizontalHeader().resizeSection(0, 75) - self.target_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.Fixed) + self.target_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed) self.target_list.horizontalHeader().resizeSection(2, 50) item = QLineEdit("1") self.target_list.setCellWidget(0, 0, item) @@ -75,10 +75,10 @@ def __init__(self, appdata: AppData, central_widget): self.desired_list = QTableWidget(1, 4) self.desired_list.setHorizontalHeaderLabels( ["region no.", "D", "≥/≤", "d"]) - self.desired_list.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) - self.desired_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) + self.desired_list.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) + self.desired_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) self.desired_list.horizontalHeader().resizeSection(0, 75) - self.desired_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.Fixed) + self.desired_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed) self.desired_list.horizontalHeader().resizeSection(2, 50) item = QLineEdit("1") self.desired_list.setCellWidget(0, 0, item) @@ -295,22 +295,13 @@ def compute_optlang(self): enum_method = 4 with self.appdata.project.cobra_py_model as model: - update_stoichiometry_hash = False if self.consider_scenario.isChecked(): # integrate scenario into model bounds self.appdata.project.load_scenario_into_model(model) - if len(self.appdata.project.scen_values) > 0: - update_stoichiometry_hash = True for r in model.reactions: # make all reactions bounded for COBRApy FVA if r.lower_bound == -float('inf'): r.lower_bound = cobra.Configuration().lower_bound - r.set_hash_value() - update_stoichiometry_hash = True if r.upper_bound == float('inf'): r.upper_bound = cobra.Configuration().upper_bound - r.set_hash_value() - update_stoichiometry_hash = True - if self.appdata.use_results_cache and update_stoichiometry_hash: - model.set_stoichiometry_hash_object() reac_id = model.reactions.list_attr("id") reac_id_symbols = mcs_computation.get_reac_id_symbols(reac_id) rows = self.target_list.rowCount() @@ -379,7 +370,7 @@ def compute_optlang(self): ) return - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) try: mcs, err_val = mcs_computation.compute_mcs(model, targets=targets, desired=desired, enum_method=enum_method, @@ -399,7 +390,7 @@ def compute_optlang(self): utils.show_unknown_error_box(exstr) return targets, desired finally: - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) print(err_val) if err_val == 1: diff --git a/cnapy/gui_elements/metabolite_list.py b/cnapy/gui_elements/metabolite_list.py index 586f35a2..81885163 100644 --- a/cnapy/gui_elements/metabolite_list.py +++ b/cnapy/gui_elements/metabolite_list.py @@ -2,11 +2,11 @@ import cobra from qtpy.QtCore import Qt, QPoint, Signal, Slot -from qtpy.QtGui import QColor, QGuiApplication, QIcon -from qtpy.QtWidgets import (QAction, QHBoxLayout, QHeaderView, QLabel, +from qtpy.QtGui import QAction, QColor, QGuiApplication, QIcon +from qtpy.QtWidgets import (QAbstractItemView, QHBoxLayout, QHeaderView, QLabel, QLineEdit, QMenu, QMessageBox, QPushButton, QSizePolicy, QSplitter, QTableWidget, QTableWidgetItem, - QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget) + QVBoxLayout, QWidget) from cnapy.appdata import AppData, ModelItemType from cnapy.gui_elements.annotation_widget import AnnotationWidget @@ -21,30 +21,26 @@ class MetaboliteListColumn(IntEnum): Concentration = 2 -class MetaboliteListItem(QTreeWidgetItem): +class MetaboliteListItem(QTableWidgetItem): """ For custom sorting of columns """ - def __init__(self, parent: QTreeWidget): - # although QTreeWidgetItem is constructed with the metabolite_list as parent this - # will not be its parent() which is None because it is a top-level item - QTreeWidgetItem.__init__(self, parent) - def __lt__(self, other): - """ overrides QTreeWidgetItem::operator< """ - column = self.treeWidget().sortColumn() - if column == MetaboliteListColumn.Concentration: + """ overrides QTableWidgetItem::operator< """ + # unlike QTreeWidgetItem (one item per row), a QTableWidgetItem is a single + # cell, so its own column already tells us which column is being sorted + if self.column() == MetaboliteListColumn.Concentration: try: - current_value = float(self.text(column)) + current_value = float(self.text()) except ValueError: current_value = -float("inf") try: - other_value = float(other.text(column)) + other_value = float(other.text()) except ValueError: other_value = -float("inf") return current_value < other_value else: # use Qt default comparison for the other columns -# return super().__lt__(other) # infinite recursion with PySide2, __lt__ is a virtual function of QTreeWidgetItem - return self.text(column) < other.text(column) +# return super().__lt__(other) # infinite recursion with PySide2, __lt__ is a virtual function of QTableWidgetItem + return self.text() < other.text() class MetaboliteList(QWidget): @@ -56,17 +52,24 @@ def __init__(self, central_widget): self.central_widget = central_widget self.last_selected = None - self.metabolite_list = QTreeWidget() + self.metabolite_list = QTableWidget() self.header_labels = [MetaboliteListColumn(i).name for i in range(len(MetaboliteListColumn))] - self.metabolite_list.setHeaderLabels(self.header_labels) + self.metabolite_list.setColumnCount(len(self.header_labels)) + self.metabolite_list.setHorizontalHeaderLabels(self.header_labels) + self.metabolite_list.verticalHeader().setVisible(False) + self.metabolite_list.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) + self.metabolite_list.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) + self.metabolite_list.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) + self.metabolite_list.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) self.visible_column = [True]*len(self.header_labels) - self.metabolite_list.setSortingEnabled(True) - self.metabolite_list.sortByColumn(MetaboliteListColumn.Id, Qt.AscendingOrder) + self.metabolite_list.setSortingEnabled(False) for m in self.appdata.project.cobra_py_model.metabolites: self.add_metabolite(m) - self.metabolite_list.setContextMenuPolicy(Qt.CustomContextMenu) + self.metabolite_list.setSortingEnabled(True) + self.metabolite_list.sortItems(MetaboliteListColumn.Id, Qt.SortOrder.AscendingOrder) + self.metabolite_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) self.metabolite_list.customContextMenuRequested.connect( self.on_context_menu) @@ -84,7 +87,7 @@ def __init__(self, central_widget): self.layout.setContentsMargins(0, 0, 0, 0) self.splitter = QSplitter() - self.splitter.setOrientation(Qt.Vertical) + self.splitter.setOrientation(Qt.Orientation.Vertical) self.splitter.addWidget(self.metabolite_list) self.splitter.addWidget(self.metabolite_mask) self.layout.addWidget(self.splitter) @@ -92,43 +95,52 @@ def __init__(self, central_widget): self.metabolite_list.currentItemChanged.connect( self.metabolite_selected) + self.metabolite_list.itemDoubleClicked.connect( + self.emit_metabolite_selected) self.metabolite_mask.metaboliteChanged.connect( self.handle_changed_metabolite) self.metabolite_mask.jumpToReaction.connect( self.emit_jump_to_reaction) - self.metabolite_list.header().setContextMenuPolicy(Qt.CustomContextMenu) - self.metabolite_list.header().customContextMenuRequested.connect(self.header_context_menu) + self.metabolite_list.horizontalHeader().setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.metabolite_list.horizontalHeader().customContextMenuRequested.connect(self.header_context_menu) def clear(self): - self.metabolite_list.clear() + self.metabolite_list.setRowCount(0) self.metabolite_mask.hide() def add_metabolite(self, metabolite): - item = MetaboliteListItem(self.metabolite_list) - item.setText(MetaboliteListColumn.Id, metabolite.id) - item.setText(MetaboliteListColumn.Name, metabolite.name) - if metabolite.id in self.appdata.project.conc_values.keys(): - item.setText(MetaboliteListColumn.Concentration, str(self.appdata.project.conc_values[metabolite.id])) - item.setData(3, 0, metabolite) + row = self.metabolite_list.rowCount() + self.metabolite_list.insertRow(row) + + id_item = MetaboliteListItem(metabolite.id) + name_item = MetaboliteListItem(metabolite.name) + concentration_item = MetaboliteListItem( + str(self.appdata.project.conc_values[metabolite.id]) + if metabolite.id in self.appdata.project.conc_values.keys() else "") + id_item.setData(Qt.ItemDataRole.UserRole, metabolite) + + self.metabolite_list.setItem(row, MetaboliteListColumn.Id, id_item) + self.metabolite_list.setItem(row, MetaboliteListColumn.Name, name_item) + self.metabolite_list.setItem(row, MetaboliteListColumn.Concentration, concentration_item) def on_context_menu(self, point): if len(self.appdata.project.cobra_py_model.metabolites) > 0: - self.pop_menu.exec_(self.mapToGlobal(point)) + self.pop_menu.exec(self.mapToGlobal(point)) def update_annotations(self, annotation): self.metabolite_mask.annotation_widget.update_annotations(annotation) def handle_changed_metabolite(self, metabolite: cobra.Metabolite, affected_reactions, previous_id: str): # Update metabolite item in list - root = self.metabolite_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) - if item.data(3, 0) == metabolite: - item.setText(MetaboliteListColumn.Id, metabolite.id) - item.setText(MetaboliteListColumn.Name, metabolite.name) + row_count = self.metabolite_list.rowCount() + for row in range(row_count): + id_item = self.metabolite_list.item(row, MetaboliteListColumn.Id) + if id_item.data(Qt.ItemDataRole.UserRole) == metabolite: + id_item.setText(metabolite.id) + self.metabolite_list.item(row, MetaboliteListColumn.Name).setText(metabolite.name) if metabolite.id in self.appdata.project.conc_values.keys(): - item.setText(MetaboliteListColumn.Concentration, str(self.appdata.project.conc_values[metabolite.id])) + self.metabolite_list.item(row, MetaboliteListColumn.Concentration).setText( + str(self.appdata.project.conc_values[metabolite.id])) break self.last_selected = self.metabolite_mask.id.text() @@ -150,7 +162,9 @@ def metabolite_selected(self, item, _column): self.metabolite_mask.hide() else: self.metabolite_mask.show() - metabolite: cobra.Metabolite = item.data(3, 0) + row = item.row() + metabolite: cobra.Metabolite = self.metabolite_list.item( + row, MetaboliteListColumn.Id).data(Qt.ItemDataRole.UserRole) self.metabolite_mask.metabolite = metabolite @@ -174,21 +188,32 @@ def metabolite_selected(self, item, _column): self.metabolite_mask.reactions.update_state(self.metabolite_mask.id.text(), self.metabolite_mask.metabolite_list) self.central_widget.add_model_item_to_history(metabolite.id, metabolite.name, ModelItemType.Metabolite) + def emit_metabolite_selected(self, item): + """Fired on a double click on a table row (single clicks only + change the current item/mask, they don't touch the map).""" + row = item.row() + metabolite: cobra.Metabolite = self.metabolite_list.item( + row, MetaboliteListColumn.Id).data(Qt.ItemDataRole.UserRole) + self.metaboliteSelected.emit(metabolite.id) + def update(self): - self.metabolite_list.clear() + self.metabolite_list.setSortingEnabled(False) + self.metabolite_list.setRowCount(0) for m in self.appdata.project.cobra_py_model.metabolites: self.add_metabolite(m) + self.metabolite_list.setSortingEnabled(True) if self.last_selected is None: self.metabolite_list.setCurrentItem(None) else: items = self.metabolite_list.findItems( - self.last_selected, Qt.MatchExactly) + self.last_selected, Qt.MatchFlag.MatchExactly) for i in items: - self.metabolite_list.setCurrentItem(i) - self.metabolite_list.scrollToItem(i) - break + if i.column() == MetaboliteListColumn.Id: + self.metabolite_list.setCurrentItem(i) + self.metabolite_list.scrollToItem(i) + break def set_current_item(self, key): self.last_selected = key @@ -198,20 +223,22 @@ def emit_jump_to_reaction(self, reaction): self.jumpToReaction.emit(reaction) def emit_in_out_fluxes_action(self): - self.computeInOutFlux.emit(self.metabolite_list.currentItem().text(0)) + current_item = self.metabolite_list.currentItem() + if current_item is not None: + id_item = self.metabolite_list.item(current_item.row(), MetaboliteListColumn.Id) + self.computeInOutFlux.emit(id_item.text()) @Slot() def copy_to_clipboard(self): clipboard = QGuiApplication.clipboard() visible_columns = [j.value for j in MetaboliteListColumn if not self.metabolite_list.isColumnHidden(j)] table = ["\t".join([MetaboliteListColumn(j).name for j in visible_columns])] - root = self.metabolite_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) + row_count = self.metabolite_list.rowCount() + for row in range(row_count): line = [] for j in visible_columns: - line.append(item.text(j)) + item = self.metabolite_list.item(row, j) + line.append(item.text() if item is not None else "") table.append("\t".join(line)) clipboard.setText("\r".join(table)) @@ -223,7 +250,7 @@ def set_column_visibility_action(self, visible): @Slot(QPoint) def header_context_menu(self, position): - menu = QMenu(self.metabolite_list.header()) + menu = QMenu(self.metabolite_list.horizontalHeader()) for col_idx in range(1, len(self.header_labels)): action = menu.addAction(self.header_labels[col_idx]) action.setCheckable(True) @@ -233,12 +260,13 @@ def header_context_menu(self, position): menu.addSeparator() action = menu.addAction("Copy table to system clipboard") action.triggered.connect(self.copy_to_clipboard) - menu.exec_(self.metabolite_list.header().mapToGlobal(position)) + menu.exec(self.metabolite_list.horizontalHeader().mapToGlobal(position)) itemActivated = Signal(str) metaboliteChanged = Signal(cobra.Metabolite, object, str) jumpToReaction = Signal(str) computeInOutFlux = Signal(str) + metaboliteSelected = Signal(str) # emitted with the metabolite id on a double click on a table row class MetabolitesMask(QWidget): @@ -266,7 +294,7 @@ def __init__(self, metabolite_list: MetaboliteList, appdata): self.delete_button.setToolTip( "Delete this metabolite and remove it from associated reactions.") policy = QSizePolicy() - policy.ShrinkFlag = True + policy.setHorizontalPolicy(QSizePolicy.Policy.Preferred) self.delete_button.setSizePolicy(policy) l.addWidget(self.delete_button) layout.addItem(l) @@ -332,13 +360,12 @@ def __init__(self, metabolite_list: MetaboliteList, appdata): def delete_metabolite(self): self.hide() # in C++ the currentItem can just be destructed but in Python this is more convoluted - current_row_index = self.metabolite_list.metabolite_list.currentIndex().row() + current_row_index = self.metabolite_list.metabolite_list.currentRow() self.metabolite_list.metabolite_list.setCurrentItem(None) affected_reactions = self.metabolite.reactions # remember these before removal self.metabolite.remove_from_model() self.metabolite_list.last_selected = None - self.metabolite_list.metabolite_list.takeTopLevelItem( - current_row_index) + self.metabolite_list.metabolite_list.removeRow(current_row_index) self.appdata.window.unsaved_changes() self.appdata.window.setFocus() self.metaboliteDeleted.emit(self.metabolite, affected_reactions, self.metabolite.id) @@ -440,12 +467,12 @@ def validate_compartment(self): message_box.setInformativeText( "Do you want to create the compartment?") message_box.setStandardButtons( - QMessageBox.Ok | QMessageBox.Cancel) - message_box.setDefaultButton(QMessageBox.Ok) + QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel) + message_box.setDefaultButton(QMessageBox.StandardButton.Ok) ret = message_box.exec() self.compartment.blockSignals(False) - if ret == QMessageBox.Cancel: + if ret == QMessageBox.StandardButton.Cancel: metabolite = self.appdata.project.cobra_py_model.metabolites.get_by_id( self.id.text()) self.compartment.setText(metabolite.compartment) diff --git a/cnapy/gui_elements/mode_navigator.py b/cnapy/gui_elements/mode_navigator.py index cfd5bd1a..b784b5a4 100644 --- a/cnapy/gui_elements/mode_navigator.py +++ b/cnapy/gui_elements/mode_navigator.py @@ -75,11 +75,11 @@ def __init__(self, appdata, central_widget): self.completion_list = QStringListModel() self.completer = CustomCompleter(self) self.completer.setModel(self.completion_list) - self.completer.setCaseSensitivity(Qt.CaseInsensitive) + self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive) self.selector.setCompleter(self.completer) l12 = QHBoxLayout() - l12.setAlignment(Qt.AlignRight) + l12.setAlignment(Qt.AlignmentFlag.AlignRight) l12.addWidget(self.save_button) l12.addWidget(self.clear_button) l1.addWidget(self.title) @@ -209,15 +209,15 @@ def clear(self): self.central_widget.mode_normalization_reaction = "" self.mode_type = 0 # EFM or some sort of flux vector self.appdata.project.modes.clear() - self.appdata.recreate_scenario_from_history() + # self.appdata.recreate_scenario_from_history() # what to do instead? self.selector.accept_signal_input = False self.hide() self.modeNavigatorClosed.emit() def display_mode(self): # if the last scenario change comes from a previous apply undo it - if len(self.appdata.scenario_past) > 0 and self.modified_scenario is self.appdata.scenario_past[-1]: - print("Resetting scenario") + if self.appdata.current_scenario_index >= 0 and \ + self.modified_scenario is self.appdata.scenario_history[self.appdata.current_scenario_index]: self.central_widget.parent.undo_scenario_edit() self.modified_scenario = None self.appdata.modes_coloring = True @@ -252,7 +252,7 @@ def next(self): def apply(self): self.appdata.scen_values_set_multiple(list(self.current_flux_values.keys()), list(self.current_flux_values.values())) - self.modified_scenario = self.appdata.scenario_past[-1] + self.modified_scenario = self.appdata.scenario_history[self.appdata.current_scenario_index] if self.appdata.auto_fba: self.central_widget.parent.fba() else: @@ -352,7 +352,7 @@ def size_histogram(self): def normalization(self): dialog = NormalizationDialog(self.appdata, self) - dialog.exec_() + dialog.exec() def __del__(self): self.central_widget.mode_normalization_reaction = "" @@ -452,8 +452,8 @@ def validate_dialog(self): @Slot() def normalize(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) self.parent.central_widget.mode_normalization_reaction = self.expr.text().strip() self.parent.central_widget.update_mode() - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) self.accept() diff --git a/cnapy/gui_elements/plot_space_dialog.py b/cnapy/gui_elements/plot_space_dialog.py index 35418e8c..d633e647 100644 --- a/cnapy/gui_elements/plot_space_dialog.py +++ b/cnapy/gui_elements/plot_space_dialog.py @@ -7,6 +7,7 @@ from cnapy.utils import QComplReceivLineEdit, QHSeperationLine from straindesign import plot_flux_space from straindesign.names import * +from cnapy.lptools_highs import plot_flux_space_highs class PlotSpaceDialog(QDialog): """A dialog to create Flux space plots""" @@ -31,7 +32,7 @@ def __init__(self, appdata): self.r[5] = 'r_substrate_z' self.layout = QVBoxLayout() - self.layout.setAlignment(Qt.Alignment(Qt.AlignTop^Qt.AlignLeft)) + self.layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft) self.layout.setSizeConstraint(QLayout.SetFixedSize) text = QLabel('Specify the yield terms that should be used for the different axes.\n'+ 'Keep in mind that exchange reactions are often defined in the direction of export.\n'+ @@ -41,7 +42,7 @@ def __init__(self, appdata): self.third_axis.clicked.connect(self.box_3d_clicked) self.layout.addWidget(self.third_axis) points_layout = QHBoxLayout() - points_layout.setAlignment(Qt.AlignLeft) + points_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) numpoints_text = QLabel('Number of datapoints:') self.numpoints = QLineEdit('20') self.numpoints.setMaximumWidth(50) @@ -65,12 +66,12 @@ def __init__(self, appdata): x_num_den_layout.addWidget(self.x_numerator) self.x_denominator.setHidden(True) self.x_sep = QHSeperationLine() - self.x_sep.setFrameShadow(QFrame.Plain) + self.x_sep.setFrameShadow(QFrame.Shadow.Plain) self.x_sep.setLineWidth(2) self.x_sep.setHidden(True) x_num_den_layout.addWidget(self.x_sep) x_num_den_layout.addWidget(self.x_denominator) - x_num_den_layout.setAlignment(Qt.AlignTop) + x_num_den_layout.setAlignment(Qt.AlignmentFlag.AlignTop) x_groupbox.setLayout(x_num_den_layout) x_groupbox.setMinimumWidth(230) editor_layout.addWidget(x_groupbox) @@ -89,14 +90,14 @@ def __init__(self, appdata): y_num_den_layout.addWidget(self.y_numerator) self.y_denominator.setHidden(True) self.y_sep = QHSeperationLine() - self.y_sep.setFrameShadow(QFrame.Plain) + self.y_sep.setFrameShadow(QFrame.Shadow.Plain) self.y_sep.setLineWidth(2) self.y_sep.setHidden(True) y_num_den_layout.addWidget(self.y_sep) y_num_den_layout.addWidget(self.y_denominator) y_groupbox.setLayout(y_num_den_layout) y_groupbox.setMinimumWidth(230) - y_num_den_layout.setAlignment(Qt.AlignTop) + y_num_den_layout.setAlignment(Qt.AlignmentFlag.AlignTop) editor_layout.addWidget(y_groupbox) # Define for longitudinal axis self.z_groupbox = QGroupBox('z-axis') @@ -113,14 +114,14 @@ def __init__(self, appdata): z_num_den_layout.addWidget(self.z_numerator) self.z_denominator.setHidden(True) self.z_sep = QHSeperationLine() - self.z_sep.setFrameShadow(QFrame.Plain) + self.z_sep.setFrameShadow(QFrame.Shadow.Plain) self.z_sep.setLineWidth(2) self.z_sep.setHidden(True) z_num_den_layout.addWidget(self.z_sep) z_num_den_layout.addWidget(self.z_denominator) self.z_groupbox.setLayout(z_num_den_layout) self.z_groupbox.setMinimumWidth(230) - z_num_den_layout.setAlignment(Qt.AlignTop) + z_num_den_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.z_groupbox.setHidden(True) editor_layout.addWidget(self.z_groupbox) self.layout.addItem(editor_layout) @@ -138,7 +139,7 @@ def __init__(self, appdata): self.button.clicked.connect(self.compute) def compute(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) with self.appdata.project.cobra_py_model as model: self.appdata.project.load_scenario_into_model(model) if self.third_axis.isChecked(): @@ -159,7 +160,21 @@ def compute(self): else: axes[2] = (self.z_numerator.text()) try: - plot_flux_space(model,axes,points=int(self.numpoints.text())) + # HiGHS-native path first (see lptools_highs.py): build-once + # solvers instead of straindesign's rebuild-every-call + # fba()/yopt(), threaded where that's actually paid off in + # benchmarking (3D one-sliced-axis plots). Falls back to the + # original straindesign-based plot_flux_space -- silently to + # the console, not to the user -- for anything the HiGHS + # path doesn't cover yet or hits an unexpected error on, so + # this can only ever be as fragile as the code it replaces, + # never more so. + try: + plot_flux_space_highs(model,axes,points=int(self.numpoints.text())) + except Exception as e_highs: + print("plot_flux_space_highs failed, falling back to straindesign's " + "plot_flux_space:", repr(e_highs)) + plot_flux_space(model,axes,points=int(self.numpoints.text())) except Exception as e: QMessageBox.warning( self, @@ -168,7 +183,7 @@ def compute(self): f"{e}" ) self.appdata.window.centralWidget().show_bottom_of_console() - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) def box_3d_clicked(self): if self.third_axis.isChecked(): diff --git a/cnapy/gui_elements/reaction_table_widget.py b/cnapy/gui_elements/reaction_table_widget.py index 58f8f027..7581e1e6 100644 --- a/cnapy/gui_elements/reaction_table_widget.py +++ b/cnapy/gui_elements/reaction_table_widget.py @@ -1,7 +1,8 @@ from enum import Enum -from qtpy.QtCore import Qt, Signal, Slot -from qtpy.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QAbstractItemView, QPlainTextEdit, QFrame -from qtpy.QtGui import QMouseEvent, QTextCursor +from qtpy.QtCore import Qt, Signal, Slot, QUrl +from qtpy.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QAbstractItemView, QTextEdit, QTextBrowser, QFrame, QToolTip + +from cnapy.gui_elements.reactions_list import build_reaction_equation_html class ModelElementType(Enum): @@ -9,35 +10,51 @@ class ModelElementType(Enum): GENE = 2 -class ReactionString(QPlainTextEdit): +class ReactionString(QTextBrowser): + """Read-only display of a reaction equation with clickable metabolite + links, built the exact same way (build_reaction_equation_html) as the + Equation field in ReactionMask: every metabolite id is an HTML link + (href = metabolite id); QTextBrowser.anchorClicked distinguishes a + click on a link from a text selection. + """ + def __init__(self, reaction, metabolite_list): super().__init__() - reaction_string = reaction.build_reaction_string() + " " # extra space to be able to click outside the equation without triggering a jump to the metabolite - self.setPlainText(reaction_string) - self.text_width = self.fontMetrics().horizontalAdvance(reaction_string) self.setReadOnly(True) - self.setFrameStyle(QFrame.NoFrame) + self.setFrameStyle(QFrame.Shape.NoFrame) + self.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth) + self.setTextInteractionFlags( + Qt.TextInteractionFlag.LinksAccessibleByMouse | Qt.TextInteractionFlag.TextSelectableByMouse + ) + self.setMouseTracking(True) + self.setOpenLinks(False) + self.anchorClicked.connect(self.link_clicked) + self.setHtml(build_reaction_equation_html(reaction)) + # Natural (unwrapped) width of the equation, used by + # ReactionTableWidget.section_resized to decide whether the row + # needs a second line. + self.document().setTextWidth(-1) + self.text_width = self.document().idealWidth() self.model = reaction.model self.metabolite_list = metabolite_list jumpToMetabolite = Signal(str) - def mouseReleaseEvent(self, event: QMouseEvent): - if event.button() == Qt.LeftButton: - text_cursor: QTextCursor = self.textCursor() - if not text_cursor.hasSelection(): - start: int = text_cursor.position() - text: str = self.toPlainText() - if start >= len(text): - return - while start > 0: - start -= 1 - if text[start].isspace(): - break - text = text[start:].split(maxsplit=1)[0] - if self.model.metabolites.has_id(text): - self.jumpToMetabolite.emit(text) - self.metabolite_list.set_current_item(text) + def mouseMoveEvent(self, event): + super().mouseMoveEvent(event) + link = self.anchorAt(event.pos()) + if link and self.model.metabolites.has_id(link): + self.viewport().setCursor(Qt.CursorShape.PointingHandCursor) + QToolTip.showText(event.globalPos(), self.model.metabolites.get_by_id(link).name, self) + else: + self.viewport().setCursor(Qt.CursorShape.IBeamCursor) + QToolTip.hideText() + + def link_clicked(self, url: QUrl): + metabolite_id = url.toString() + if self.model.metabolites.has_id(metabolite_id): + self.jumpToMetabolite.emit(metabolite_id) + self.metabolite_list.set_current_item(metabolite_id) class ReactionTableWidget(QTableWidget): def __init__(self, appdata, element_type: ModelElementType) -> None: @@ -48,11 +65,11 @@ def __init__(self, appdata, element_type: ModelElementType) -> None: self.setColumnCount(2) self.setHorizontalHeaderLabels(["Id", "Reaction"]) self.horizontalHeader().setStretchLastSection(True) - self.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel) + self.setHorizontalScrollMode(QAbstractItemView.ScrollMode.ScrollPerPixel) self.horizontalHeader().sectionResized.connect(self.section_resized) def update_state(self, id_text, metabolite_list): - QApplication.setOverrideCursor(Qt.BusyCursor) + QApplication.setOverrideCursor(Qt.CursorShape.BusyCursor) QApplication.processEvents() # to put the change above into effect self.clearContents() self.setRowCount(0) # also resets manually changed row heights @@ -89,10 +106,10 @@ def section_resized(self, index: int, old_size: int, new_size: int): margins = reaction_string_widget.contentsMargins() height_margin = 12 if reaction_string_widget.text_width + margins.left() + margins.right() > new_size: - reaction_string_widget.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) + reaction_string_widget.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded) self.setRowHeight(row, base_height*2 + font_metrics.leading() + height_margin) # font_metrics.leading(): space between two lines else: - reaction_string_widget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + reaction_string_widget.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.setRowHeight(row, base_height + height_margin) jumpToMetabolite = Signal(str) diff --git a/cnapy/gui_elements/reactions_list.py b/cnapy/gui_elements/reactions_list.py index 2953e213..a89dd0f9 100644 --- a/cnapy/gui_elements/reactions_list.py +++ b/cnapy/gui_elements/reactions_list.py @@ -5,20 +5,70 @@ import cobra import copy -from qtpy.QtCore import QMimeData, Qt, Signal, Slot, QPoint, QSignalBlocker -from qtpy.QtGui import QColor, QDrag, QIcon, QGuiApplication, QKeyEvent -from qtpy.QtWidgets import (QHBoxLayout, QTreeWidget, QLabel, QLineEdit, - QMessageBox, QPushButton, QSizePolicy, QSplitter, - QTreeWidgetItem, QVBoxLayout, QWidget, QMenu, - QAbstractItemView) +import re +from qtpy.QtCore import (QAbstractTableModel, QModelIndex, QMimeData, QSortFilterProxyModel, Qt, + Signal, Slot, QPoint, QSignalBlocker, QEvent, QTimer, QStringListModel) +from qtpy.QtGui import QColor, QDrag, QIcon, QGuiApplication +from qtpy.QtWidgets import (QHBoxLayout, QTableView, QTableWidget, QTableWidgetItem, QLabel, QLineEdit, + QMessageBox, QPushButton, QSizePolicy, QSplitter, QStyledItemDelegate, + QVBoxLayout, QWidget, QMenu, QAbstractItemView, QHeaderView, QStackedWidget, + QToolButton, QFrame, QPlainTextEdit, QCompleter) from cnapy.appdata import AppData, ModelItemType from cnapy.gui_elements.annotation_widget import AnnotationWidget -from cnapy.utils import SignalThrottler, turn_red, turn_white, update_selected +from cnapy.utils import SignalThrottler, turn_red, turn_white from cnapy.utils_for_cnapy_api import check_identifiers_org_entry, check_in_identifiers_org from cnapy.gui_elements.map_view import validate_value from cnapy.gui_elements.escher_map_view import EscherMapView +def build_reaction_equation_html(reaction: cobra.Reaction, multiline: bool = False) -> str: + """Build a rich-text version of a reaction equation in which every + metabolite id is a clickable link (href = metabolite id). + + Shared between ReactionMask's Equation field and ReactionTableWidget's + reaction column so that both display equations the exact same way. + + Reactants/products are always ' + '-separated (breakable spaces), + matching reaction.build_reaction_string() and letting word-wrap lay + out several metabolites per line when there's room, rather than + forcing one per line. With multiline=True (used for ReactionMask's + expanded overlay) the arrow additionally gets its own line, visually + separating the reactant and product sides. + + The arrow itself is rendered using cobra's own ASCII notation + ('-->' / '<=>', HTML-escaped) rather than a Unicode arrow glyph: + besides matching what the plain QLineEdit shows, this avoids a + Unicode glyph missing from the active font falling back to a + different font just for that character, which can shift the + vertical centering of the whole line. + + A reaction that is irreversible "backwards" (upper_bound <= 0, so + it can only run from products to reactants) is shown with the + arrow reversed ('<--') even though the reactant/product sides + keep their usual left/right positions. + """ + def format_side(metabolites, sign): + parts = [] + for m, coeff in metabolites.items(): + coeff = coeff * sign + factor = "" if isclose(abs(coeff), 1.0) else f"{abs(coeff):g} " + parts.append(f'{factor}{m.id}') + return " + ".join(parts) + + reactants = {m: c for m, c in reaction.metabolites.items() if c < 0} + products = {m: c for m, c in reaction.metabolites.items() if c > 0} + lhs = format_side(reactants, -1) + rhs = format_side(products, 1) + if reaction.reversibility: + arrow = "<=>" + elif reaction.upper_bound <= 0: + arrow = "<--" + else: + arrow = "-->" + arrow_html = f"
{arrow}
" if multiline else f" {arrow} " + return f"{lhs}{arrow_html}{rhs}" + + class ReactionListColumn(IntEnum): Id = 0 Name = 1 @@ -28,73 +78,49 @@ class ReactionListColumn(IntEnum): UB = 5 DF = 6 -class DragableTreeWidget(QTreeWidget): - '''A list of dragable reaction items''' - - def mouseMoveEvent(self, _event): - item = self.currentItem() - if item is not None: - mime_data = QMimeData() - mime_data.setText(item.reaction.id) - drag = QDrag(self) - drag.setMimeData(mime_data) - drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) +class ReactionListItem: + """Row data for reactions in the table model.""" - def keyPressEvent(self, event: QKeyEvent): - # enable sequential editing of scenario values using up/down arrow keys - super().keyPressEvent(event) - if self.currentColumn() == ReactionListColumn.Scenario: - if not self.isPersistentEditorOpen(self.currentItem(), self.currentColumn()): - key = event.key() - if key == Qt.Key_Up or key == Qt.Key_Down: - self.editItem(self.currentItem(), self.currentColumn()) - -class ReactionListItem(QTreeWidgetItem): - """ For custom sorting of columns """ - - def __init__(self, reaction: cobra.Reaction, parent: QTreeWidget): - # although QTreeWidgetItem is constructed with the reaction_list as parent this - # will not be its parent() which is None because it is a top-level item - QTreeWidgetItem.__init__(self, parent) + def __init__(self, reaction: cobra.Reaction, model=None): self.reaction: cobra.Reaction = reaction - self.flux_sort_val = -float('inf') - self.lb_val = -float('inf') - self.ub_val = float('inf') - self.df_val = -float("inf") + self.model = model + self.texts = [""] * len(ReactionListColumn) + self.backgrounds = [None] * len(ReactionListColumn) + self.foregrounds = [None] * len(ReactionListColumn) + self.tooltips = [""] * len(ReactionListColumn) self.pin_at_top = False - def set_flux_data(self, text, value): - self.setText(ReactionListColumn.Flux, text) - if isinstance(value, (int, float)): - self.flux_sort_val = abs(value) - else: # assumes value is a pair of numbers - self.flux_sort_val = value[1] - value[0] - - def reset_flux_data(self): - self.setText(ReactionListColumn.Flux, "") - self.flux_sort_val = -float('inf') - self.lb_val = -float('inf') - self.ub_val = float('inf') - - def __lt__(self, other): - """ overrides QTreeWidgetItem::operator< """ - if self.pin_at_top != other.pin_at_top: - if self.treeWidget().header().sortIndicatorOrder() == Qt.DescendingOrder: - return self.pin_at_top < other.pin_at_top - else: - return other.pin_at_top < self.pin_at_top - column = self.treeWidget().sortColumn() - if column == ReactionListColumn.Flux: - return self.flux_sort_val < other.flux_sort_val - elif column == ReactionListColumn.LB: - return self.lb_val < other.lb_val - elif column == ReactionListColumn.UB: - return self.ub_val < other.ub_val - elif column == ReactionListColumn.DF: - return self.df_val < other.df_val - else: # use Qt default comparison for the other columns -# return super().__lt__(other) # infinite recursion with PySide2, __lt__ is a virtual function of QTreeWidgetItem - return self.text(column) < other.text(column) + def flags(self): + # Vestigial QTreeWidgetItem-compatibility shim: ReactionListModel.flags() + # hardcodes which column is editable and never consults this, so this + # value is not authoritative and setFlags() below does not store anything. + return Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable + + def setFlags(self, _flags): + return None + + def text(self, column): + return self.texts[int(column)] + + def setText(self, column, text): + self.texts[int(column)] = text + self._emit_changed(column) + + def setBackground(self, column, color): + self.backgrounds[int(column)] = color + self._emit_changed(column) + + def setForeground(self, column, color): + self.foregrounds[int(column)] = color + self._emit_changed(column) + + def setToolTip(self, column, text): + self.tooltips[int(column)] = text + self._emit_changed(column) + + def setSelected(self, selected): + if self.model is not None and selected: + self.model.view.setCurrentItem(self) def update_tooltips(self): text = "Id: " + self.reaction.id + "\nName: " + self.reaction.name \ @@ -105,6 +131,545 @@ def update_tooltips(self): self.setToolTip(ReactionListColumn.Id, text) self.setToolTip(ReactionListColumn.Name, text) + def _emit_changed(self, column): + if self.model is not None: + self.model.emit_item_changed(self, int(column)) + + +class ReactionListModel(QAbstractTableModel): + """Custom table model backing the reaction list view.""" + + itemChanged = Signal(object, int) + + def __init__(self, header_labels, appdata, parent=None): + super().__init__(parent) + self.header_labels = header_labels + self.appdata = appdata + self.items = [] + self.sort_column = ReactionListColumn.Id + self.sort_order = Qt.SortOrder.AscendingOrder + self.sorting_enabled = False + self.view = None + # Snapshot of appdata.project.comp_values for display in the Flux + # column, decoupled from that dict itself: appdata.project.comp_values + # is shared between FBA (comp_values_type == 0, real flux values) and + # FVA (comp_values_type == 1, used only for the map's coloring), but + # the Flux column should only ever reflect the last real FBA solution. + self.flux_values = {} + self.refresh_flux_values() + + def refresh_flux_values(self): + """Pick up appdata.project.comp_values for the Flux column, but only + when it currently holds real flux values rather than FVA results.""" + if self.appdata.project.comp_values_type == 0: + self.flux_values = dict(self.appdata.project.comp_values) + + def rowCount(self, parent=QModelIndex()): + return 0 if parent.isValid() else len(self.items) + + def columnCount(self, parent=QModelIndex()): + return 0 if parent.isValid() else len(self.header_labels) + + def data(self, index, role=Qt.ItemDataRole.DisplayRole): + if not index.isValid(): + return None + item = self.items[index.row()] + column = index.column() + text, background, foreground, tooltip = self.cell_data(item, column) + if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole): + return text + if role == Qt.ItemDataRole.BackgroundRole: + return background + if role == Qt.ItemDataRole.ForegroundRole: + return foreground + if role == Qt.ItemDataRole.ToolTipRole: + return tooltip + return None + + def cell_data(self, item, column): + column = ReactionListColumn(column) + text = item.text(column) + background = item.backgrounds[column] + foreground = item.foregrounds[column] + tooltip = item.tooltips[column] + default_background = QColor(75, 75, 75) if self.appdata.is_in_dark_mode else Qt.GlobalColor.white + default_foreground = QColor(255, 255, 255) if self.appdata.is_in_dark_mode else QColor(0, 0, 0) + key = item.reaction.id + + if column == ReactionListColumn.Scenario: + if key in self.appdata.project.scen_values: + vl, vu = self.appdata.project.scen_values[key] + text = self.appdata.format_flux_value(vl) + if vl != vu: + text = text + ", " + self.appdata.format_flux_value(vu) + background = self.appdata.scen_color + else: + text = "" + background = default_background + foreground = default_foreground + elif column == ReactionListColumn.Flux: + if key in self.flux_values: + vl, vu = self.flux_values[key] + text, background, as_one = self.appdata.flux_value_display(vl, vu) + else: + text = "" + background = default_background + foreground = default_foreground + elif column in (ReactionListColumn.LB, ReactionListColumn.UB): + vl, vu, background = self.bounds_data(item) + text = self.appdata.format_flux_value(vl if column == ReactionListColumn.LB else vu) + if key not in self.appdata.project.fva_values: + foreground = default_foreground + elif column == ReactionListColumn.DF and key in self.appdata.project.df_values: + text = str(self.appdata.project.df_values[key]) + if item.backgrounds[column] is not None: + background = item.backgrounds[column] + if item.foregrounds[column] is not None: + foreground = item.foregrounds[column] + return text, background, foreground, tooltip + + def bounds_data(self, item): + key = item.reaction.id + if key in self.appdata.project.fva_values.keys(): + vl, vu = self.appdata.project.fva_values[key] + if isclose(vl, vu, abs_tol=self.appdata.abs_tol): + if self.appdata.modes_coloring: + background = Qt.GlobalColor.red if vl == 0 else Qt.GlobalColor.green + else: + background = self.appdata.comp_color + elif isclose(vl, 0.0, abs_tol=self.appdata.abs_tol) or isclose(vu, 0.0, abs_tol=self.appdata.abs_tol) or vl <= 0 and vu >= 0: + background = self.appdata.special_color_1 + else: + background = self.appdata.special_color_2 + else: + vl = item.reaction.lower_bound + vu = item.reaction.upper_bound + background = QColor(75, 75, 75) if self.appdata.is_in_dark_mode else Qt.GlobalColor.white + return vl, vu, background + + def setData(self, index, value, role=Qt.ItemDataRole.EditRole): + if not index.isValid() or role != Qt.ItemDataRole.EditRole: + return False + item = self.items[index.row()] + column = index.column() + if column == ReactionListColumn.Scenario: + current_text, *_ = self.cell_data(item, column) + if value == current_text: + # Nothing actually changed -- e.g. the editor was opened (by a + # click, or by ScenarioValueDelegate moving between rows while + # editing) and closed again without being typed into. Treating + # this as a real edit would make handle_item_changed invalidate + # the previously computed flux values on every such no-op commit, + # including every arrow-key step while just browsing the column. + return True + item.texts[column] = value + self.dataChanged.emit(index, index, [Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole]) + self.itemChanged.emit(item, column) + return True + + def flags(self, index): + if not index.isValid(): + return Qt.ItemFlag.NoItemFlags + flags = Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsDragEnabled + if index.column() == ReactionListColumn.Scenario: + flags |= Qt.ItemFlag.ItemIsEditable + return flags + + def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole): + if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole: + return self.header_labels[section] + return None + + def add_item(self, item): + item.model = self + self.beginInsertRows(QModelIndex(), len(self.items), len(self.items)) + self.items.append(item) + self.endInsertRows() + if self.sorting_enabled: + self.sort(self.sort_column, self.sort_order) + + def clear(self): + self.beginResetModel() + self.items.clear() + self.endResetModel() + + def emit_item_changed(self, item, column): + row = self.indexOfTopLevelItem(item) + if row >= 0: + index = self.index(row, int(column)) + self.dataChanged.emit(index, index) + + def indexOfTopLevelItem(self, item): + try: + return self.items.index(item) + except ValueError: + return -1 + + def takeTopLevelItem(self, row): + if row < 0 or row >= len(self.items): + return None + self.beginRemoveRows(QModelIndex(), row, row) + item = self.items.pop(row) + item.model = None + self.endRemoveRows() + return item + + def sort_value(self, item, column): + key = item.reaction.id + if column == ReactionListColumn.Scenario: + if key in self.appdata.project.scen_values: + vl, vu = self.appdata.project.scen_values[key] + return abs(vl) if vl == vu else vu - vl + return -float('inf') + if column == ReactionListColumn.Flux: + if key in self.flux_values: + vl, vu = self.flux_values[key] + return abs(vl) if vl == vu else vu - vl + return -float('inf') + if column == ReactionListColumn.LB: + vl, _vu, _background = self.bounds_data(item) + return vl + if column == ReactionListColumn.UB: + _vl, vu, _background = self.bounds_data(item) + return vu + if column == ReactionListColumn.DF: + return self.appdata.project.df_values.get(key, -float('inf')) + return item.text(column) + + def sort(self, column, order=Qt.SortOrder.AscendingOrder): + self.sort_column = column + self.sort_order = order + if not self.sorting_enabled: + return + self.layoutAboutToBeChanged.emit() + reverse = order == Qt.SortOrder.DescendingOrder + pinned = [item for item in self.items if item.pin_at_top] + unpinned = [item for item in self.items if not item.pin_at_top] + pinned.sort(key=lambda item: self.sort_value(item, column), reverse=reverse) + unpinned.sort(key=lambda item: self.sort_value(item, column), reverse=reverse) + new_items = pinned + unpinned + + new_row_of_item = {id(item): row for row, item in enumerate(new_items)} + for old_index in self.persistentIndexList(): + old_item = self.items[old_index.row()] + new_row = new_row_of_item.get(id(old_item)) + new_index = (self.index(new_row, old_index.column()) + if new_row is not None else QModelIndex()) + self.changePersistentIndex(old_index, new_index) + + self.items[:] = new_items + self.layoutChanged.emit() + + +class ReactionSearchFilterProxyModel(QSortFilterProxyModel): + """Filters ReactionListModel rows down to a precomputed set of matching + reaction ids (see ReactionList.update_selected/_apply_visibility). + + Sorting is deliberately left to the source model: ReactionListModel.sort() + has pinned-row handling and per-column custom sort values (sort_value) + that don't map onto QSortFilterProxyModel's own lessThan()-based sorting, + so dynamicSortFilter is disabled here and this proxy only ever filters. + """ + + def __init__(self, parent=None): + super().__init__(parent) + self._found_ids = None # None = no filter applied, i.e. show everything + self._exempt_ids = set() # ids to show regardless of the active filter + self.setDynamicSortFilter(False) + + def set_found_ids(self, found_ids): + self._found_ids = found_ids + self._exempt_ids.clear() # a fresh search should apply cleanly, no stale exemptions + self.invalidateFilter() + + def exempt_from_filter(self, reaction_id): + """Keep a specific reaction visible regardless of the active search + filter. Used when a reaction is added or edited: a search still being + active at that point is far more likely to be something the user + forgot to clear than something they meant to apply to a reaction + they're actively working on -- so don't let it disappear on them.""" + if self._found_ids is not None and reaction_id not in self._found_ids: + self._exempt_ids.add(reaction_id) + self.invalidateFilter() + + def sort(self, column, order=Qt.SortOrder.AscendingOrder): + # QTableView routes both explicit sortByColumn() calls and its internal + # sortingEnabled-header-click mechanism through self.model().sort(...), + # which after inserting this proxy means QSortFilterProxyModel's own + # sort() would otherwise run instead of the source model's -- silently + # dropping the pinned-row handling and custom per-column sort_value + # logic in ReactionListModel.sort(). Delegate instead of calling + # super().sort(), so those paths keep behaving exactly as before. + self.sourceModel().sort(column, order) + + def filterAcceptsRow(self, source_row, source_parent): + if self._found_ids is None: + return True + reaction_id = self.sourceModel().items[source_row].reaction.id + return reaction_id in self._found_ids or reaction_id in self._exempt_ids + + +class ScenarioValueDelegate(QStyledItemDelegate): + """Editor delegate for the Scenario column. + + Qt installs the delegate itself as an event filter on the editor widget + it creates (this is how Tab/Backtab/Enter/Escape are normally handled, + see QAbstractItemDelegate.eventFilter). We hook the same mechanism for + Up/Down so they commit the current row and move the *editor* to the + row above/below instead of just moving the cursor inside the QLineEdit + (which ignores Up/Down anyway). + + This intentionally does not rely on the key event bubbling up from the + editor to the view: that bubbling does happen in plain Qt, but the + editor's parent view here also reacts to currentIndex changes (to keep + the reaction detail mask in sync), and that reaction can itself change + the current index again before a view-level keyPressEvent handler gets + a chance to reopen the editor. Handling it here, before the event ever + leaves the editor, sidesteps that reentrancy entirely. + """ + + def eventFilter(self, editor, event): + if event.type() == QEvent.KeyPress and event.key() in (Qt.Key.Key_Up, Qt.Key.Key_Down): + view = self.parent() + index = view.currentIndex() + row_count = view.model().rowCount() + row = index.row() + row += 1 if event.key() == Qt.Key.Key_Down else -1 + new_row = row if 0 <= row < row_count else -1 + # Commit unconditionally, even at the boundary (no row to move to): + # otherwise a value typed into the first/last row is silently lost + # instead of saved, since there's nowhere left to arrow away to. + self.commitData.emit(editor) + if new_row >= 0: + self.closeEditor.emit(editor, QStyledItemDelegate.NoHint) + new_index = view.model().index(new_row, index.column()) + view.setCurrentIndex(new_index) + view.scrollTo(new_index) + view.edit(new_index) + return True + return super().eventFilter(editor, event) + + +class DragableTableView(QTableView): + """A table of dragable reaction items.""" + + currentItemChanged = Signal(object) + itemClicked = Signal(object, int) + + def __init__(self): + super().__init__() + self.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) + self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) + self.setWordWrap(False) + self.verticalHeader().setVisible(False) + self.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Fixed) + self.verticalHeader().setMinimumSectionSize(self.fontMetrics().lineSpacing()) + self.verticalHeader().setDefaultSectionSize(self.fontMetrics().lineSpacing()) + self.setItemDelegateForColumn(ReactionListColumn.Scenario, ScenarioValueDelegate(self)) + + def setModel(self, model): + super().setModel(model) + # `model` may be the ReactionListModel directly, or a + # ReactionSearchFilterProxyModel wrapping it -- unwrap down to the + # real source model, since that's what items/indexOfTopLevelItem/etc. + # below need to operate on regardless of which one is currently set. + source_model = model + while isinstance(source_model, QSortFilterProxyModel): + source_model = source_model.sourceModel() + self._source_model = source_model + source_model.view = self + self.selectionModel().currentChanged.connect(self._current_changed) + self.clicked.connect(self._clicked) + + def _to_source(self, index): + """Translate an index from self.model() (proxy or source) to the source model.""" + model = self.model() + return model.mapToSource(index) if isinstance(model, QSortFilterProxyModel) else index + + def _from_source(self, index): + """Translate a source-model index to self.model()'s index space (proxy or source).""" + model = self.model() + return model.mapFromSource(index) if isinstance(model, QSortFilterProxyModel) else index + + def mouseMoveEvent(self, _event): + item = self.currentItem() + if item is not None: + mime_data = QMimeData() + mime_data.setText(item.reaction.id) + drag = QDrag(self) + drag.setMimeData(mime_data) + drag.exec(Qt.DropAction.CopyAction | Qt.DropAction.MoveAction, Qt.DropAction.CopyAction) + + def _current_changed(self, current, _previous): + self.currentItemChanged.emit(self.itemFromIndex(current)) + + def _clicked(self, index): + self.itemClicked.emit(self.itemFromIndex(index), index.column()) + + def itemFromIndex(self, index): + source_index = self._to_source(index) + if source_index.isValid(): + return self._source_model.items[source_index.row()] + return None + + def currentItem(self): + return self.itemFromIndex(self.currentIndex()) + + def currentColumn(self): + return self.currentIndex().column() + + def clear(self): + self._source_model.clear() + + def clearSelection(self): + super().clearSelection() + self.setCurrentIndex(QModelIndex()) + + def setCurrentItem(self, item): + if item is None: + self.clearSelection() + return + row = self._source_model.indexOfTopLevelItem(item) + if row >= 0: + proxy_index = self._from_source(self._source_model.index(row, 0)) + if proxy_index.isValid(): # invalid if `item` is currently filtered out + self.setCurrentIndex(proxy_index) + + def scrollToItem(self, item): + row = self._source_model.indexOfTopLevelItem(item) + if row >= 0: + proxy_index = self._from_source(self._source_model.index(row, 0)) + if proxy_index.isValid(): + self.scrollTo(proxy_index) + + def editItem(self, item, column): + row = self._source_model.indexOfTopLevelItem(item) + if row >= 0: + proxy_index = self._from_source(self._source_model.index(row, int(column))) + if proxy_index.isValid(): + self.edit(proxy_index) + + def topLevelItemCount(self): + return len(self._source_model.items) + + def topLevelItem(self, row): + return self._source_model.items[row] + + def findItems(self, text, _flags, column=ReactionListColumn.Id): + return [item for item in self._source_model.items + if self._source_model.cell_data(item, column)[0] == text] + + def sortItems(self, column, order): + self._source_model.sort(column, order) + + def sortColumn(self): + return self._source_model.sort_column + + def setSortingEnabled(self, enable): + self._source_model.sorting_enabled = enable + super().setSortingEnabled(enable) + + def indexOfTopLevelItem(self, item): + return self._source_model.indexOfTopLevelItem(item) + + def takeTopLevelItem(self, row): + return self._source_model.takeTopLevelItem(row) + + +class ReactionAndMetaboliteCompleter(QCompleter): + """Completer for ReactionSearchLineEdit. Completes the last + whitespace-separated token only, leaving earlier tokens in place + (same pattern as MultiCompleter/CustomCompleter used elsewhere in + this codebase, e.g. flux_feasibility_dialog.py / mode_navigator.py). + + For strings that are ambiguous (i.e. match both a metabolite id and + a reaction id/name), the wordlist contains two explicitly tagged + variants ('#met', '#rxn') so the user can pick one + interpretation from the dropdown; unambiguous strings appear + untagged. See ReactionList.update_selected for how the tags (or + their absence, for ambiguous-but-untagged tokens) are interpreted. + """ + + def __init__(self, parent=None): + QCompleter.__init__(self, parent) + self.setModel(QStringListModel()) + self.setCaseSensitivity(Qt.CaseInsensitive) + + def pathFromIndex(self, index): # overrides QCompleter method + path = QCompleter.pathFromIndex(self, index) + current_text = str(self.widget().text()) + + # A completion can itself contain whitespace (for example, a reaction + # name). QCompleter may ask for its path again after updating the line + # edit; in that case, adding the prefix a second time duplicates the + # completion. Leave an already accepted completion unchanged. + if current_text.endswith(path): + return current_text + + # Replace only the active token while preserving every preceding + # whitespace character, including a trailing separator. + prefix = re.sub(r'\S*$', '', current_text) + return prefix + path + + def splitPath(self, path): # overrides QCompleter method + if not path.strip(): + return [""] + path = str(path.split()[-1]).lstrip(' ') + return [path] + + +class ReactionSearchLineEdit(QLineEdit): + """Reaction-tab search box. + + Supports plain substring search over reaction id/name(/annotations) + same as before, PLUS space-separated metabolite id tokens: a token + that is a valid metabolite id restricts results to reactions that + contain ALL such metabolites (AND logic across tokens). + + A token that happens to be both a valid metabolite id and also + matches some reaction's id/name/annotation text is ambiguous. Left + untagged, both interpretations are unioned (OR) for that token, so + nothing is hidden that either interpretation would have shown; the + completer offers '#met' / '#rxn' tagged entries so the + user can commit to one interpretation, after which only that + interpretation is used for that token going forward (their choice is + encoded directly in the search text, so it persists as long as the + tag stays in the text box). + """ + + def __init__(self, central_widget, parent=None): + super().__init__(parent) + self.central_widget = central_widget + self.completer_model = QStringListModel() + completer = ReactionAndMetaboliteCompleter(self) + completer.setModel(self.completer_model) + self.setCompleter(completer) + self.refresh_wordlist() + + def refresh_wordlist(self): + """Rebuild the autocomplete wordlist from the current model. Call + this whenever reactions or metabolites are added, removed, or + renamed (e.g. from CentralWidget.update(rebuild_all_tabs=True), + and after individual reaction/metabolite id changes).""" + model = self.central_widget.appdata.project.cobra_py_model + reac_ids = set(model.reactions.list_attr("id")) + reac_names = {n for n in model.reactions.list_attr("name") if n} + met_ids = set(model.metabolites.list_attr("id")) + + reaction_strings = reac_ids | reac_names + ambiguous = reaction_strings & met_ids + + wordlist = set() + for s in (reaction_strings | met_ids): + if s not in ambiguous: + wordlist.add(s) + for s in ambiguous: + wordlist.add(s + "#met") + wordlist.add(s + "#rxn") + + self.completer_model.setStringList(sorted(wordlist)) + + class ReactionList(QWidget): """A list of reaction""" @@ -118,29 +683,34 @@ def __init__(self, central_widget): self.add_button = QPushButton("Add new reaction") self.add_button.setIcon(QIcon.fromTheme("list-add")) policy = QSizePolicy() - policy.ShrinkFlag = True + policy.setHorizontalPolicy(QSizePolicy.Policy.Preferred) self.add_button.setSizePolicy(policy) - self.reaction_list: DragableTreeWidget = DragableTreeWidget() + self.reaction_list: DragableTableView = DragableTableView() self.reaction_list.setDragEnabled(True) - self.reaction_list.setColumnCount(len(ReactionListColumn)) - self.reaction_list.setRootIsDecorated(False) - self.reaction_list.setContextMenuPolicy(Qt.CustomContextMenu) - self.reaction_list.customContextMenuRequested.connect(self.context_menu) self.header_labels = [ReactionListColumn(i).name for i in range(len(ReactionListColumn))] - self.reaction_list.setHeaderLabels(self.header_labels) + self.reaction_model = ReactionListModel(self.header_labels, self.appdata, self.reaction_list) + self.filter_proxy = ReactionSearchFilterProxyModel(self.reaction_list) + self.filter_proxy.setSourceModel(self.reaction_model) + self.reaction_list.setModel(self.filter_proxy) + self.reaction_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.reaction_list.customContextMenuRequested.connect(self.context_menu) # heuristic initial column widths self.reaction_list.resizeColumnToContents(ReactionListColumn.Scenario) - self.reaction_list.resizeColumnToContents(ReactionListColumn.LB) - width = self.reaction_list.header().sectionSize(ReactionListColumn.Scenario) + \ - self.reaction_list.header().sectionSize(ReactionListColumn.LB) - self.reaction_list.header().resizeSection(ReactionListColumn.Id, width) - self.reaction_list.header().resizeSection(ReactionListColumn.Name, width) + fm = self.reaction_list.fontMetrics() + width = fm.horizontalAdvance("-1000") + self.reaction_list.horizontalHeader().resizeSection(ReactionListColumn.LB, width) + self.reaction_list.horizontalHeader().resizeSection(ReactionListColumn.UB, width) + width = self.reaction_list.horizontalHeader().sectionSize(ReactionListColumn.Scenario) + self.reaction_list.horizontalHeader().resizeSection(ReactionListColumn.Flux, width) + width += self.reaction_list.horizontalHeader().sectionSize(ReactionListColumn.LB) + self.reaction_list.horizontalHeader().resizeSection(ReactionListColumn.Id, width) + self.reaction_list.horizontalHeader().resizeSection(ReactionListColumn.Name, width) self.visible_column = [True]*len(self.header_labels) self.reaction_list.setSortingEnabled(True) - self.reaction_list.sortByColumn(ReactionListColumn.Id, Qt.AscendingOrder) - self.reaction_list.header().setContextMenuPolicy(Qt.CustomContextMenu) - self.reaction_list.header().customContextMenuRequested.connect(self.header_context_menu) + self.reaction_list.sortByColumn(ReactionListColumn.Id, Qt.SortOrder.AscendingOrder) + self.reaction_list.horizontalHeader().setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.reaction_list.horizontalHeader().customContextMenuRequested.connect(self.header_context_menu) for r in self.appdata.project.cobra_py_model.reactions: self.add_reaction(r) @@ -151,10 +721,10 @@ def __init__(self, central_widget): self.layout = QVBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) l = QHBoxLayout() - l.setAlignment(Qt.AlignRight) + l.setAlignment(Qt.AlignmentFlag.AlignRight) l.addWidget(self.add_button) self.splitter = QSplitter() - self.splitter.setOrientation(Qt.Vertical) + self.splitter.setOrientation(Qt.Orientation.Vertical) self.splitter.addWidget(self.reaction_list) self.splitter.addWidget(self.reaction_mask) self.layout.addItem(l) @@ -162,9 +732,9 @@ def __init__(self, central_widget): self.setLayout(self.layout) self.reaction_list.currentItemChanged.connect(self.reaction_selected) - self.reaction_list.setEditTriggers(QAbstractItemView.NoEditTriggers) + self.reaction_list.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) self.reaction_list.itemClicked.connect(self.handle_item_clicked) - self.reaction_list.itemChanged.connect(self.handle_item_changed) + self.reaction_model.itemChanged.connect(self.handle_item_changed) self.reaction_mask.reactionChanged.connect( self.handle_changed_reaction) @@ -185,81 +755,22 @@ def clear(self): def add_reaction(self, reaction: cobra.Reaction) -> ReactionListItem: ''' create a new item in the reaction list''' self.reaction_list.clearSelection() - item = ReactionListItem(reaction, self.reaction_list) - item.setFlags(item.flags() | Qt.ItemIsEditable) + item = ReactionListItem(reaction) + self.reaction_model.add_item(item) item.setText(ReactionListColumn.Id, reaction.id) item.setText(ReactionListColumn.Name, reaction.name) item.update_tooltips() - self.update_item(item) + self.filter_proxy.exempt_from_filter(reaction.id) return item def update_item(self, item: ReactionListItem): - ''' update Scenario, Flux, LB, UB columns ''' - if self.appdata.project.comp_values_type == 0: - self.set_flux_value(item) - self.set_bounds_values(item) - if item.reaction.id in self.appdata.project.scen_values: - scen_background_color = self.appdata.scen_color - (vl, vu) = self.appdata.project.scen_values[item.reaction.id] - scen_text = self.appdata.format_flux_value(vl) - if vl != vu: - scen_text = scen_text+", "+self.appdata.format_flux_value(vu) - else: - scen_background_color = QColor(75, 75, 75) if self.appdata.is_in_dark_mode else Qt.white - scen_text = "" - item.setForeground(ReactionListColumn.Scenario, QColor(255, 255, 255) if self.appdata.is_in_dark_mode else QColor(0, 0, 0)) - item.setBackground(ReactionListColumn.Scenario, scen_background_color) - item.setText(ReactionListColumn.Scenario, scen_text) - if item.reaction.id in self.appdata.project.df_values.keys(): - item.setText(ReactionListColumn.DF, str(self.appdata.project.df_values[item.reaction.id])) - item.df_val = self.appdata.project.df_values[item.reaction.id] - - def set_flux_value(self, item: ReactionListItem): - key = item.reaction.id - if key in self.appdata.project.comp_values.keys(): - (vl, vu) = self.appdata.project.comp_values[key] - flux_text, background_color, as_one = self.appdata.flux_value_display(vl, vu) - item.set_flux_data(flux_text, vl if as_one else (vl, vu)) - else: - item.reset_flux_data() - background_color = QColor(75, 75, 75) if self.appdata.is_in_dark_mode else Qt.white - item.setForeground(ReactionListColumn.Flux, QColor(255, 255, 255) if self.appdata.is_in_dark_mode else QColor(0, 0, 0)) - item.setBackground(ReactionListColumn.Flux, background_color) - item.setForeground(ReactionListColumn.Flux, Qt.black) - - def set_bounds_values(self, item): - key = item.reaction.id - if key in self.appdata.project.fva_values.keys(): - (vl, vu) = self.appdata.project.fva_values[key] - if isclose(vl, vu, abs_tol=self.appdata.abs_tol): - if self.appdata.modes_coloring: - if vl == 0: - background_color = Qt.red - else: - background_color = Qt.green - else: - background_color = self.appdata.comp_color - else: - if isclose(vl, 0.0, abs_tol=self.appdata.abs_tol): - background_color = self.appdata.special_color_1 - elif isclose(vu, 0.0, abs_tol=self.appdata.abs_tol): - background_color = self.appdata.special_color_1 - elif vl <= 0 and vu >= 0: - background_color = self.appdata.special_color_1 - else: - background_color = self.appdata.special_color_2 - else: - vl = item.reaction.lower_bound - vu = item.reaction.upper_bound - background_color = QColor(75, 75, 75) if self.appdata.is_in_dark_mode else Qt.white - item.setForeground(ReactionListColumn.LB, QColor(255, 255, 255) if self.appdata.is_in_dark_mode else QColor(0, 0, 0)) - item.setForeground(ReactionListColumn.UB, QColor(255, 255, 255) if self.appdata.is_in_dark_mode else QColor(0, 0, 0)) - item.setBackground(ReactionListColumn.LB, background_color) #ZZZ - item.lb_val = vl - item.setText(ReactionListColumn.LB, self.appdata.format_flux_value(vl)) - item.setBackground(ReactionListColumn.UB, background_color) - item.ub_val = vu - item.setText(ReactionListColumn.UB, self.appdata.format_flux_value(vu)) + ''' notify the view that lazily computed columns changed ''' + row = self.reaction_model.indexOfTopLevelItem(item) + if row >= 0: + self.reaction_model.dataChanged.emit( + self.reaction_model.index(row, ReactionListColumn.Scenario), + self.reaction_model.index(row, ReactionListColumn.DF), + ) def add_new_reaction(self): self.reaction_mask.show() @@ -271,13 +782,12 @@ def add_new_reaction(self): reaction = cobra.Reaction(name) self.appdata.project.cobra_py_model.add_reactions([reaction]) self.appdata.project.update_reaction_id_lists() - reaction.set_hash_value() - self.appdata.project.cobra_py_model.set_stoichiometry_hash_object() self.reaction_list.blockSignals(True) item = self.add_reaction(reaction) self.reaction_list.blockSignals(False) self.reaction_selected(item) self.appdata.window.unsaved_changes() + self.central_widget.reaction_searchbar.refresh_wordlist() def update_annotations(self, annotation): self.reaction_mask.annotation_widget.update_annotations(annotation) @@ -286,7 +796,13 @@ def reaction_selected(self, item: ReactionListItem): if item is None: self.reaction_mask.hide() elif self.reaction_list.currentColumn() != ReactionListColumn.Scenario or self.splitter.sizes()[1] > 0: - item.setSelected(True) + if self.reaction_list.currentItem() is not item: + # Only needed to make `item` current in the first place (e.g. a + # newly added reaction, called outside the currentChanged signal). + # Calling it when `item` is already current would reset the + # current column back to 0, fighting with in-row column + # navigation (see ScenarioValueDelegate). + item.setSelected(True) self.reaction_mask.show() reaction: cobra.Reaction = item.reaction @@ -295,8 +811,7 @@ def reaction_selected(self, item: ReactionListItem): self.reaction_mask.id.setText(reaction.id) self.reaction_mask.name.setText(reaction.name) - self.reaction_mask.equation.setText( - reaction.build_reaction_string()) + self.reaction_mask.set_equation_from_reaction(reaction) self.reaction_mask.lower_bound.setText(str(reaction.lower_bound)) self.reaction_mask.upper_bound.setText(str(reaction.upper_bound)) self.reaction_mask.coefficent.setText( @@ -315,8 +830,9 @@ def reaction_selected(self, item: ReactionListItem): turn_white(self.reaction_mask.gene_reaction_rule, self.appdata.is_in_dark_mode) self.reaction_mask.is_valid = True - (_, r) = self.splitter.getRange(1) - self.splitter.moveSplitter(int(r/2), 1) + if self.splitter.sizes()[1] == 0: + (_, r) = self.splitter.getRange(1) + self.splitter.moveSplitter(int(r/2), 1) self.reaction_list.scrollToItem(item) self.reaction_mask.update_state() @@ -325,10 +841,7 @@ def reaction_selected(self, item: ReactionListItem): def handle_changed_reaction(self, reaction: cobra.Reaction): # Update reaction item in list - root = self.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) + for item in self.reaction_model.items: if item.reaction == reaction: old_id = item.text(ReactionListColumn.Id) item.setText(ReactionListColumn.Id, reaction.id) @@ -336,16 +849,15 @@ def handle_changed_reaction(self, reaction: cobra.Reaction): item.update_tooltips() break + self.filter_proxy.exempt_from_filter(reaction.id) self.last_selected = self.reaction_mask.id.text() self.reactionChanged.emit(old_id, reaction) + self.central_widget.reaction_searchbar.refresh_wordlist() def handle_deleted_reaction(self, reaction: cobra.Reaction): '''Remove reaction item from reaction list''' - root = self.reaction_list.invisibleRootItem() - child_count = root.childCount() with QSignalBlocker(self.reaction_list): - for i in range(child_count): - item = root.child(i) + for item in list(self.reaction_model.items): if item.reaction == reaction: # remove item self.reaction_list.takeTopLevelItem( @@ -354,18 +866,21 @@ def handle_deleted_reaction(self, reaction: cobra.Reaction): self.last_selected = self.reaction_mask.id.text() self.reactionDeleted.emit(reaction) + self.central_widget.reaction_searchbar.refresh_wordlist() - @Slot(QTreeWidgetItem, int) + @Slot(object, int) def handle_item_clicked(self, item: ReactionListItem, column): self.last_selected = item.reaction.id if column == ReactionListColumn.Scenario: self.reaction_list.editItem(item, column) - @Slot(QTreeWidgetItem, int) + @Slot(object, int) def handle_item_changed(self, item: ReactionListItem, column: int): if column == ReactionListColumn.Scenario: scen_text = item.text(column).strip() - if len(scen_text) == 0 or validate_value(scen_text): + new_value = validate_value(scen_text) + if (len(scen_text) == 0 and item.reaction.id in self.appdata.project.scen_values) or \ + new_value != self.appdata.project.scen_values.get(item.reaction.id, None): self.central_widget.update_reaction_value(item.reaction.id, scen_text, update_reaction_list=False) # not necessary to update the whole reaction list if self.appdata.auto_fba: @@ -374,38 +889,148 @@ def handle_item_changed(self, item: ReactionListItem, column: int): self.update_item(item) self.central_widget.update_maps() else: - item.setBackground(column, Qt.red) + item.setBackground(column, Qt.GlobalColor.red) def update_selected(self, string, with_annotations): - return update_selected( - string=string, - with_annotations=with_annotations, - model_elements=self.appdata.project.cobra_py_model.reactions, - element_list=self.reaction_list, - ) + """Resolve the reaction search bar's text into a set of matching + reaction ids, applying tab-specific rules on top of the plain + substring/annotation matcher below: + + - Tokens ending in '#met' / '#rxn' are explicitly tagged by the + user (via the completer) to mean "this token is a metabolite + id" / "this token is reaction text", respectively. + - An untagged token that is a valid metabolite id AND also + matches some reaction's id/name/annotation text is ambiguous; + both interpretations are unioned for that token. + - An untagged token that is a valid metabolite id but matches no + reaction text is treated purely as a metabolite filter. + - All other tokens (and any remaining free text) are batched + together and passed through as one text query, so annotation + search and the '*' wildcard behave exactly as before. + - Multiple tokens are AND'ed together (must all be satisfied by + the same reaction). + """ + model = self.appdata.project.cobra_py_model + metabolite_ids = set(model.metabolites.list_attr("id")) + + tokens = string.split() + if not tokens: + found_ids = self._reactions_matching_text("", with_annotations) + self._apply_visibility(found_ids) + return list(found_ids) + + per_token_matches = [] + plain_text_tokens = [] + + for token in tokens: + if token.endswith("#met"): + met_id = token[:-4] + per_token_matches.append(self._reactions_containing_metabolite(met_id)) + elif token.endswith("#rxn"): + text = token[:-4] + per_token_matches.append(self._reactions_matching_text(text, with_annotations)) + elif token in metabolite_ids: + text_hits = self._reactions_matching_text(token, with_annotations) + if text_hits: + # ambiguous and not disambiguated -> union of both interpretations + per_token_matches.append(self._reactions_containing_metabolite(token) | text_hits) + else: + per_token_matches.append(self._reactions_containing_metabolite(token)) + else: + plain_text_tokens.append(token) + + if plain_text_tokens: + per_token_matches.append( + self._reactions_matching_text(" ".join(plain_text_tokens), with_annotations) + ) + + if per_token_matches: + found_ids = set.intersection(*per_token_matches) + else: + found_ids = set(model.reactions.list_attr("id")) + + self._apply_visibility(found_ids) + return found_ids + + def _reactions_containing_metabolite(self, met_id: str) -> set: + model = self.appdata.project.cobra_py_model + if not model.metabolites.has_id(met_id): + return set() + return {r.id for r in model.metabolites.get_by_id(met_id).reactions} + + def _reactions_matching_text(self, text: str, with_annotations: bool) -> set: + """Same substring/annotation matching rules as the previous + single-query search: case-insensitive, '*' as a glob wildcard, + matches id/name and (optionally) annotations; strings under 2 + characters match everything (pre-existing behavior, kept as-is + here -- see chat history for why this is a known, accepted + edge case rather than a bug).""" + if len(text) < 2: + return set(self.appdata.project.cobra_py_model.reactions.list_attr("id")) + regex = re.compile(".*".join(map(re.escape, text.split("*"))), re.IGNORECASE) + return { + reaction.id + for reaction in self.appdata.project.cobra_py_model.reactions + if regex.search(reaction.id) + or regex.search(reaction.name) + or ( + with_annotations + and ( + any(regex.search(key) for key in reaction.annotation.keys()) + or any(regex.search(str(value)) for value in reaction.annotation.values()) + ) + ) + } + + def _apply_visibility(self, found_ids: set): + self.filter_proxy.set_found_ids(found_ids) + + # If the previously-current item got filtered out, currentItem() now + # comes back None (its proxy index is invalid), so there's nothing + # to scroll to -- this replaces the old isHidden() check. + current_item = self.reaction_list.currentItem() + if current_item is not None: + self.reaction_list.scrollToItem(current_item) def update(self, rebuild=False): if len(self.appdata.project.df_values.keys()) > 0: self.reaction_list.setColumnHidden(ReactionListColumn.DF, False) self.visible_column[ReactionListColumn.DF] = True - # should only need to rebuild the whole list if the model changes - self.reaction_list.itemChanged.disconnect(self.handle_item_changed) - self.reaction_list.setSortingEnabled(False) # keep row order stable so that each item is updated + # should only need to rebuild the whole list if the model changes; computed + # columns are evaluated lazily by ReactionListModel.data() for visible rows. if rebuild: + self.reaction_model.itemChanged.disconnect(self.handle_item_changed) + self.reaction_list.setSortingEnabled(False) self.reaction_list.clear() for r in self.appdata.project.cobra_py_model.reactions: self.add_reaction(r) - else: - for i in range(self.reaction_list.topLevelItemCount()): - self.update_item(self.reaction_list.topLevelItem(i)) - self.reaction_list.itemChanged.connect(self.handle_item_changed) + self.reaction_model.itemChanged.connect(self.handle_item_changed) + # the model may have been replaced (new project): drop the Flux values and the DF + # column of the previous one + self.reaction_model.refresh_flux_values() + if len(self.appdata.project.df_values) == 0: + self.reaction_list.setColumnHidden(ReactionListColumn.DF, True) + self.visible_column[ReactionListColumn.DF] = False + # resetting the item model does not reliably emit currentChanged, so the detail panel + # could otherwise keep showing a reaction of the previous model + if self.last_selected is None or not self.reaction_list.findItems( + self.last_selected, Qt.MatchFlag.MatchExactly): + self.reaction_mask.hide() + elif self.reaction_model.rowCount() > 0: + self.reaction_model.refresh_flux_values() + for item in self.reaction_model.items: + item.backgrounds[ReactionListColumn.Flux] = None + self.reaction_model.dataChanged.emit( + self.reaction_model.index(0, ReactionListColumn.Scenario), + self.reaction_model.index(self.reaction_model.rowCount() - 1, ReactionListColumn.DF), + ) if self.last_selected is None: self.reaction_list.setCurrentItem(None) else: items = self.reaction_list.findItems( - self.last_selected, Qt.MatchExactly) + self.last_selected, Qt.MatchFlag.MatchExactly) for i in items: # triggers self.reaction_selected which also does a self.reaction_mask.update_state() self.reaction_list.setCurrentItem(i) @@ -413,10 +1038,10 @@ def update(self, rebuild=False): break self.reaction_list.setSortingEnabled(True) - self.reaction_list.resizeColumnToContents(ReactionListColumn.Flux) - self.reaction_list.resizeColumnToContents(ReactionListColumn.LB) - self.reaction_list.resizeColumnToContents(ReactionListColumn.UB) - self.reaction_list.resizeColumnToContents(ReactionListColumn.DF) + self.reaction_list.sortItems( + self.reaction_list.sortColumn(), + self.reaction_list.horizontalHeader().sortIndicatorOrder(), + ) def set_current_item(self, key: str): self.last_selected = key @@ -449,33 +1074,27 @@ def context_menu(self, position): minimize_action.triggered.connect(self.minimize_reaction) set_scen_value_action = menu.addAction("add computed value to scenario") set_scen_value_action.triggered.connect(self.set_scen_value_action) - menu.exec_(self.reaction_list.mapToGlobal(position)) + menu.exec(self.reaction_list.mapToGlobal(position)) @Slot(bool) def change_pinned(self, checked: bool): self.reaction_list.currentItem().pin_at_top = checked if checked: - self.reaction_list.sortItems(self.reaction_list.sortColumn(), self.reaction_list.header().sortIndicatorOrder()) + self.reaction_list.sortItems(self.reaction_list.sortColumn(), self.reaction_list.horizontalHeader().sortIndicatorOrder()) self.appdata.project.scen_values.pinned_reactions.add(self.reaction_list.currentItem().reaction.id) else: self.appdata.project.scen_values.pinned_reactions.discard(self.reaction_list.currentItem().reaction.id) def pin_multiple(self, reac_ids): - root = self.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item: ReactionListItem = root.child(i) + for item in self.reaction_model.items: if item.reaction.id in reac_ids: item.pin_at_top = True - self.reaction_list.sortItems(self.reaction_list.sortColumn(), self.reaction_list.header().sortIndicatorOrder()) + self.reaction_list.sortItems(self.reaction_list.sortColumn(), self.reaction_list.horizontalHeader().sortIndicatorOrder()) self.appdata.project.scen_values.pinned_reactions.update(reac_ids) @Slot() def unpin_all(self): - root = self.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item: ReactionListItem = root.child(i) + for item in self.reaction_model.items: if item.reaction.id in self.appdata.project.scen_values.pinned_reactions: item.pin_at_top = False self.appdata.project.scen_values.pinned_reactions = set() @@ -492,15 +1111,9 @@ def minimize_reaction(self): def set_scen_value_action(self): self.central_widget.set_scen_value(self.reaction_list.currentItem().reaction.id) - @Slot() - def delete_reaction_action(self): - self.central_widget.map_tabs.currentWidget().delete_reaction(self.reaction_list.currentItem().reaction.id) - self.reaction_mask.update_state() - self.appdata.window.unsaved_changes() - @Slot(QPoint) def header_context_menu(self, position): - menu = QMenu(self.reaction_list.header()) + menu = QMenu(self.reaction_list.horizontalHeader()) for col_idx in range(1, len(self.header_labels)): action = menu.addAction(self.header_labels[col_idx]) action.setCheckable(True) @@ -510,18 +1123,15 @@ def header_context_menu(self, position): menu.addSeparator() action = menu.addAction("Copy table to system clipboard") action.triggered.connect(self.copy_to_clipboard) - menu.exec_(self.reaction_list.header().mapToGlobal(position)) + menu.exec(self.reaction_list.horizontalHeader().mapToGlobal(position)) def get_as_table(self) -> str: visible_columns = [j.value for j in ReactionListColumn if not self.reaction_list.isColumnHidden(j)] table = ["\t".join([ReactionListColumn(j).name for j in visible_columns])] - root = self.reaction_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) + for item in self.reaction_model.items: line = [] for j in visible_columns: - line.append(item.text(j)) + line.append(self.reaction_model.cell_data(item, j)[0]) table.append("\t".join(line)) return "\r".join(table) @@ -560,26 +1170,41 @@ def __init__(self, parent): QWidget.__init__(self) self.parent = parent self.layout = QHBoxLayout() - self.layout.setAlignment(Qt.AlignLeft) + self.layout.setAlignment(Qt.AlignmentFlag.AlignLeft) + self.buttons: dict[str, JumpButton] = {} # map name -> button def clear(self): for i in reversed(range(self.layout.count())): self.layout.itemAt(i).widget().setParent(None) + self.buttons.clear() - def add(self, name: str): + def add(self, name: str, count: int = 1): + ''' count: number of occurrences of the reaction on the (Escher) map ''' + if name in self.buttons: # replies from Escher maps can arrive more than once + return if self.layout.count() == 0: label = QLabel("Jump to reaction on map:") self.layout.addWidget(label) jb = JumpButton(self, name) + self.buttons[name] = jb + if count > 1: + jb.setText(f"{name} (–/{count})") + jb.setToolTip(f"The reaction occurs {count} times on this map, click again to jump to the next occurrence.") policy = QSizePolicy() - policy.ShrinkFlag = True + policy.setHorizontalPolicy(QSizePolicy.Policy.Preferred) jb.setSizePolicy(policy) self.layout.addWidget(jb) self.setLayout(self.layout) jb.jumpToMap.connect(self.parent.emit_jump_to_map) + def set_position(self, name: str, index: int, count: int): + ''' show which of the occurrences of the reaction on the map was jumped to ''' + jb = self.buttons.get(name, None) + if jb is not None and count > 1: + jb.setText(f"{name} ({index}/{count})") + @ Slot(str) def emit_jump_to_map(self: JumpButton, name: str): self.parent.emit_jump_to_map(name) @@ -587,6 +1212,119 @@ def emit_jump_to_map(self: JumpButton, name: str): jumpToMap = Signal(str) +class ClickableEquationLabel(QLabel): + """A read-only label used to display a reaction equation with clickable + metabolite links (see ReactionMask.build_equation_html). + + Clicking a link is handled through the inherited linkActivated signal. + Clicking anywhere else on the label emits `clicked`, which + ReactionMask uses to switch back to the editable QLineEdit. + """ + + clicked = Signal() + + def __init__(self, parent=None): + super().__init__(parent) + self._hovering_link = False + self._metabolite_names = {} + self.setTextFormat(Qt.TextFormat.RichText) + self.setTextInteractionFlags(Qt.TextInteractionFlag.LinksAccessibleByMouse) + self.setWordWrap(False) + self.setMouseTracking(True) + # Ignored so the layout can shrink this label below its content's + # sizeHint instead of growing the whole mask to fit a long equation; + # Qt still clips the painted rich text to the label's actual + # geometry, so an over-long equation is simply cut off here (the + # expand button/overlay is how the full text stays reachable). + self.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Preferred) + self.linkHovered.connect(self._on_link_hovered) + + def set_metabolite_names(self, reaction: cobra.Reaction): + self._metabolite_names = {metabolite.id: metabolite.name for metabolite in reaction.metabolites} + + def _on_link_hovered(self, link: str): + self._hovering_link = bool(link) + self.setCursor(Qt.CursorShape.PointingHandCursor if link else Qt.CursorShape.IBeamCursor) + self.setToolTip(self._metabolite_names.get(link, "")) + + def mouseReleaseEvent(self, event): + super().mouseReleaseEvent(event) + if not self._hovering_link: + self.clicked.emit() + + +class EquationTextEdit(QPlainTextEdit): + """A word-wrapping text editor used to edit the *whole* reaction + equation inside the overlay, filling its entire area rather than a + single cramped line. + + The underlying reaction string is logically single-line, so Enter + commits (emits editingFinished) instead of inserting a newline; + losing focus also commits, mirroring QLineEdit's editingFinished so + ReactionMask can run the exact same commit pipeline + (reaction_data_changed) it already uses for self.equation. Escape + discards the edit instead (emits cancelled). + """ + + editingFinished = Signal() + cancelled = Signal() + + def __init__(self, parent=None): + super().__init__(parent) + self.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth) + self.setTabChangesFocus(True) + self.setFrameShape(QFrame.Shape.NoFrame) + + def keyPressEvent(self, event): + if event.key() in (Qt.Key.Key_Return, Qt.Key.Key_Enter): + # Commit via the same path as a normal focus-out, rather than + # inserting a newline: the reaction string this editor stands + # in for has no line breaks of its own. + self.clearFocus() + event.accept() + return + if event.key() == Qt.Key.Key_Escape: + self.cancelled.emit() + event.accept() + return + super().keyPressEvent(event) + + def focusOutEvent(self, event): + super().focusOutEvent(event) + self.editingFinished.emit() + + +class EquationOverlay(QFrame): + """Popup used by ReactionMask.show_equation_overlay() to display (and, + via EquationTextEdit, edit) the full reaction equation. + + A Qt::Popup can be dismissed in more than one way (a click outside it, + Escape, the toggle button, or a metabolite link), so both closeEvent + and hideEvent are used to make sure any edit still in progress in the + overlay's (throwaway) EquationTextEdit gets committed before the + overlay is destroyed (WA_DeleteOnClose), no matter which path + triggered the dismissal. + """ + + def __init__(self, mask: "ReactionMask"): + super().__init__(mask, Qt.WindowType.Popup) + self._mask = mask + self._closing_handled = False + + def _handle_closing_once(self): + if not self._closing_handled: + self._closing_handled = True + self._mask.handle_overlay_closing() + + def closeEvent(self, event): + self._handle_closing_once() + super().closeEvent(event) + + def hideEvent(self, event): + self._handle_closing_once() + super().hideEvent(event) + + class ReactionMask(QWidget): """The input mask for a reaction""" @@ -596,8 +1334,19 @@ def __init__(self, parent: ReactionList): self.parent: ReactionList = parent self.reaction = None self.is_valid = True + self.equation_valid = True self.fba_relevant_change = False self.setAcceptDrops(False) + # State for the equation overlay opened by equation_expand_button + # (see show_equation_overlay/handle_overlay_closing): the overlay + # itself while open (None otherwise); its current content, which is + # either a read-only ClickableEquationLabel or -- while the user is + # editing -- a throwaway EquationTextEdit (self.equation itself is + # never moved into the overlay, so there is nothing shared to keep + # track of or reclaim). + self._equation_overlay = None + self._overlay_label = None + self._overlay_editor = None layout = QVBoxLayout() @@ -610,7 +1359,7 @@ def __init__(self, parent: ReactionList): self.delete_button = QPushButton("Delete reaction") self.delete_button.setIcon(QIcon.fromTheme("edit-delete")) policy = QSizePolicy() - policy.ShrinkFlag = True + policy.setHorizontalPolicy(QSizePolicy.Policy.Preferred) self.delete_button.setSizePolicy(policy) l.addWidget(self.delete_button) layout.addItem(l) @@ -623,10 +1372,51 @@ def __init__(self, parent: ReactionList): layout.addItem(l) l = QHBoxLayout() - label = QLabel("Equation:") + label = QLabel("Equation: ") self.equation = QLineEdit() - l.addWidget(label) - l.addWidget(self.equation) + # Read-only view of the equation with clickable metabolite links, + # shown in place of the plain QLineEdit whenever it is not being + # edited (see set_equation_from_reaction/enter_equation_edit_mode). + self.equation_link_label = ClickableEquationLabel() + self.equation_link_label.linkActivated.connect(self.jump_to_metabolite_by_id) + self.equation_link_label.clicked.connect(self.enter_equation_edit_mode) + # Match font and height to self.equation exactly, so that switching + # between the two stack pages never changes the row's geometry and + # both widgets vertically center their text within the identical + # box (this is also why build_equation_html renders cobra's own + # ASCII arrows rather than Unicode ones: a Unicode arrow glyph the + # active font lacks would fall back to a different font just for + # that character and can shift the whole line's vertical centering). + self.equation_link_label.setFont(self.equation.font()) + self.equation_link_label.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter) + self.equation_link_label.setFixedHeight(self.equation.sizeHint().height()) + self.equation_stack = QStackedWidget() + self.equation_stack.addWidget(self.equation) + self.equation_stack.addWidget(self.equation_link_label) + self.equation_stack.setCurrentWidget(self.equation_link_label) + # Only shown once the equation no longer fits in the field; opens an + # overlay with the full, multi-line, link-ified equation, and acts + # as a close button for as long as that overlay is open. + self.equation_expand_button = QToolButton() + self.equation_expand_button.setText("\u25be") + self.equation_expand_button.setToolTip("Show full reaction equation") + self.equation_expand_button.setVisible(False) + self.equation_expand_button.clicked.connect(self.show_equation_overlay) + # Explicit AlignVCenter on every item in this row: QBoxLayout's + # default behavior for an item that can't grow to fill the row + # (like equation_link_label's fixed height, or a QLabel/QToolButton + # at their natural size) is to pin it to the *top* of the row, not + # center it. That's invisible as long as every item is roughly the + # same height, but equation_expand_button is only shown for long + # equations, and a QToolButton's natural height varies a lot by + # style -- notably taller under Linux's Fusion/GTK-based styles + # than under Windows' native one. Once it's the tallest item and + # sets the row's height, the shorter equation text would otherwise + # get shoved to the top of that taller row instead of staying + # vertically centered in it. + l.addWidget(label, 0, Qt.AlignmentFlag.AlignVCenter) + l.addWidget(self.equation_stack, 0, Qt.AlignmentFlag.AlignVCenter) + l.addWidget(self.equation_expand_button, 0, Qt.AlignmentFlag.AlignVCenter) layout.addItem(l) l = QHBoxLayout() @@ -659,18 +1449,24 @@ def __init__(self, parent: ReactionList): self.annotation_widget = AnnotationWidget(self) layout.addItem(self.annotation_widget) - l = QVBoxLayout() - label = QLabel("Metabolites involved in this reaction:") - l.addWidget(label) - l2 = QHBoxLayout() - self.metabolites = QTreeWidget() - self.metabolites.setHeaderLabels(["Id"]) - self.metabolites.setSortingEnabled(True) - l2.addWidget(self.metabolites) - l.addItem(l2) - self.metabolites.itemDoubleClicked.connect( - self.emit_jump_to_metabolite) - layout.addItem(l) + # l = QVBoxLayout() + # label = QLabel("Metabolites involved in this reaction:") + # l.addWidget(label) + # l2 = QHBoxLayout() + # self.metabolites = QTableWidget() + # self.metabolites.setColumnCount(2) + # self.metabolites.setHorizontalHeaderLabels(["Id", "Name"]) + # self.metabolites.setWordWrap(False) + # self.metabolites.verticalHeader().setVisible(False) + # self.metabolites.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Fixed) + # self.metabolites.verticalHeader().setMinimumSectionSize(self.metabolites.fontMetrics().lineSpacing()) + # self.metabolites.verticalHeader().setDefaultSectionSize(self.metabolites.fontMetrics().lineSpacing()) + # self.metabolites.setSortingEnabled(True) + # l2.addWidget(self.metabolites) + # l.addItem(l2) + # self.metabolites.itemDoubleClicked.connect( + # self.emit_jump_to_metabolite) + # layout.addItem(l) self.jump_list = JumpList(self) layout.addWidget(self.jump_list) @@ -746,8 +1542,6 @@ def apply(self): if bounds != self.reaction.bounds or metabolites != self.reaction.metabolites or \ objective_coefficient != self.reaction.objective_coefficient: self.fba_relevant_change = True - self.reaction.set_hash_value() - self.parent.appdata.project.cobra_py_model.set_stoichiometry_hash_object() if self.fba_relevant_change or name != self.reaction.name or \ gene_reaction_rule != self.reaction.gene_reaction_rule or id_ != self.reaction.id or \ annotation != self.reaction.annotation: @@ -783,13 +1577,13 @@ def handle_changed_gpr(self): if self.grp_test_model.reactions.get_by_id("GPR_TEST").gene_reaction_rule == "": self.gene_reaction_rule.blockSignals(True) msg_box = QMessageBox(self) - msg_box.setIcon(QMessageBox.Question) + msg_box.setIcon(QMessageBox.Icon.Question) msg_box.setWindowTitle("Malformed GPR rule") msg_box.setText("It appears that your changed GPR rule is not valid. Do you want to edit or revert your changes?") - edit_but = msg_box.addButton("Edit GPR rule", QMessageBox.RejectRole) - revert_but = msg_box.addButton("Revert GPR rule", QMessageBox.ResetRole) + edit_but = msg_box.addButton("Edit GPR rule", QMessageBox.ButtonRole.RejectRole) + revert_but = msg_box.addButton("Revert GPR rule", QMessageBox.ButtonRole.ResetRole) msg_box.setDefaultButton(revert_but) - msg_box.exec_() + msg_box.exec() self.gene_reaction_rule.blockSignals(False) if msg_box.clickedButton() == edit_but: @@ -818,14 +1612,14 @@ def handle_changed_gpr(self): if len(genes_to_add) > 0: self.gene_reaction_rule.blockSignals(True) msg_box = QMessageBox(self) - msg_box.setIcon(QMessageBox.Question) + msg_box.setIcon(QMessageBox.Icon.Question) msg_box.setWindowTitle("Create new genes?") msg_box.setText("The following genes do not exist and will be added to the model:\n" + ', '.join(genes_to_add)) - msg_box.setDefaultButton(msg_box.addButton(QMessageBox.Ok)) - edit_but = msg_box.addButton("Edit GPR rule", QMessageBox.RejectRole) - revert_but = msg_box.addButton("Revert GPR rule", QMessageBox.ResetRole) - msg_box.exec_() + msg_box.setDefaultButton(msg_box.addButton(QMessageBox.StandardButton.Ok)) + edit_but = msg_box.addButton("Edit GPR rule", QMessageBox.ButtonRole.RejectRole) + revert_but = msg_box.addButton("Revert GPR rule", QMessageBox.ButtonRole.ResetRole) + msg_box.exec() self.gene_reaction_rule.blockSignals(False) if msg_box.clickedButton() == edit_but: self.gene_reaction_rule.setFocus() @@ -892,14 +1686,14 @@ def validate_equation(self): if len(new_metabolites) > 0: self.equation.blockSignals(True) msg_box = QMessageBox(self) - msg_box.setIcon(QMessageBox.Question) + msg_box.setIcon(QMessageBox.Icon.Question) msg_box.setWindowTitle("Create new metabolites?") msg_box.setText("The following metabolites do not exist and will be added to the model:\n" + ', '.join(new_metabolites)) - msg_box.setDefaultButton(msg_box.addButton(QMessageBox.Ok)) #"Ok", QMessageBox.AcceptRole)) - edit_but = msg_box.addButton("Edit equation", QMessageBox.RejectRole) - revert_but = msg_box.addButton("Revert equation", QMessageBox.ResetRole) - msg_box.exec_() + msg_box.setDefaultButton(msg_box.addButton(QMessageBox.StandardButton.Ok)) #"Ok", QMessageBox.AcceptRole)) + edit_but = msg_box.addButton("Edit equation", QMessageBox.ButtonRole.RejectRole) + revert_but = msg_box.addButton("Revert equation", QMessageBox.ButtonRole.ResetRole) + msg_box.exec() if msg_box.clickedButton() == edit_but: self.equation.setFocus() ok = False @@ -955,6 +1749,11 @@ def validate_mask(self): valid_lb = self.validate_lowerbound() valid_ub = self.validate_upperbound() valid_coefficient = self.validate_coefficient() + # Tracked separately so refresh_equation_view() can decide whether to + # collapse the equation field back to the link view -- that's only + # sensible once the equation itself parses correctly, independent of + # whether some other field (id, name, bounds, ...) is invalid. + self.equation_valid = valid_equation if valid_id & valid_name & valid_equation & valid_lb & valid_ub & valid_coefficient: self.is_valid = True else: @@ -965,6 +1764,7 @@ def reaction_data_changed(self): if self.is_valid: self.apply() self.update_state() + self.refresh_equation_view() def update_state(self): self.jump_list.clear() @@ -976,28 +1776,280 @@ def update_state(self): if self.id.text() in mmap["boxes"]: self.jump_list.add(name) - self.metabolites.clear() - if self.parent.appdata.project.cobra_py_model.reactions.has_id(self.id.text()): - reaction = self.parent.appdata.project.cobra_py_model.reactions.get_by_id( - self.id.text()) - for m in reaction.metabolites: - item = QTreeWidgetItem(self.metabolites) - item.setText(0, m.id) - item.setText(1, m.name) - item.setData(2, 0, m) - text = "Id: " + m.id + "\nName: " + m.name - item.setToolTip(1, text) + # self.metabolites.setSortingEnabled(False) + # self.metabolites.setRowCount(0) + # if self.parent.appdata.project.cobra_py_model.reactions.has_id(self.id.text()): + # reaction = self.parent.appdata.project.cobra_py_model.reactions.get_by_id( + # self.id.text()) + # for row, m in enumerate(reaction.metabolites): + # self.metabolites.insertRow(row) + # id_item = QTableWidgetItem(m.id) + # name_item = QTableWidgetItem(m.name) + # id_item.setData(Qt.ItemDataRole.UserRole, m) + # text = "Id: " + m.id + "\nName: " + m.name + # id_item.setToolTip(text) + # name_item.setToolTip(text) + # self.metabolites.setItem(row, 0, id_item) + # self.metabolites.setItem(row, 1, name_item) + # self.metabolites.setSortingEnabled(True) def emit_jump_to_map(self, name): self.jumpToMap.emit(name, self.id.text()) def emit_jump_to_metabolite(self, metabolite): - self.jumpToMetabolite.emit(str(metabolite.data(2, 0))) + item = self.metabolites.item(metabolite.row(), 0) + self.jumpToMetabolite.emit(str(item.data(Qt.ItemDataRole.UserRole))) @Slot() def update_reaction_string(self): if self.reaction is not None: - self.equation.setText(self.reaction.build_reaction_string()) + self.set_equation_from_reaction(self.reaction) + + def build_equation_html(self, reaction: cobra.Reaction, multiline: bool = False) -> str: + """Thin wrapper around the shared build_reaction_equation_html, kept + as a method so existing call sites in this class don't need to + change.""" + return build_reaction_equation_html(reaction, multiline=multiline) + + def set_equation_from_reaction(self, reaction: cobra.Reaction): + """Populate both the editable equation field and its link-ified + display counterpart from `reaction`, and show the display view.""" + self.equation.setText(reaction.build_reaction_string()) + self.equation.setModified(False) + self.equation_link_label.setText(self.build_equation_html(reaction)) + self.equation_link_label.set_metabolite_names(reaction) + self.equation_stack.setCurrentWidget(self.equation_link_label) + self.equation_valid = True + self.update_equation_expand_button() + + def refresh_equation_view(self): + """Called after every edit attempt on the mask (successful or not). + + Collapses the equation field back to the read-only link view once it + holds a valid equation; otherwise leaves it in edit mode so the red + highlight from validate_equation() stays visible to the user. Never + interrupts an edit that is currently in progress. + """ + if self.equation_stack.currentWidget() is self.equation and self.equation.hasFocus(): + return + if self.equation_valid and self.reaction is not None: + self.set_equation_from_reaction(self.reaction) + else: + self.equation_stack.setCurrentWidget(self.equation) + + def enter_equation_edit_mode(self): + """Switch the equation field from the link view to the plain, + editable QLineEdit (triggered by clicking the link view anywhere + that isn't itself a metabolite link).""" + self.equation_stack.setCurrentWidget(self.equation) + self.equation.setFocus() + #self.equation.selectAll() + + def jump_to_metabolite_by_id(self, metabolite_id: str): + """Handle a click on a metabolite link, whether in the inline + equation view or in the expanded overlay.""" + self.jumpToMetabolite.emit(metabolite_id) + + def update_equation_expand_button(self): + """Show the dropdown/expand button only once the equation is too + wide to fit in the field at its current size.""" + if self.reaction is None: + self.equation_expand_button.setVisible(False) + return + metrics = self.equation_link_label.fontMetrics() + text_width = metrics.horizontalAdvance(self.reaction.build_reaction_string()) + available_width = self.equation_link_label.width() + self.equation_expand_button.setVisible(text_width > available_width) + + def build_overlay_label(self, overlay: "EquationOverlay") -> "ClickableEquationLabel": + """Build the read-only, word-wrapped, link-ified equation view used + to fill the overlay, sized (and the overlay resized) to fit its + content exactly. Shared by show_equation_overlay (initial open) and + restore_overlay_to_link_view (after committing an edit), so the two + never drift out of sync. + """ + margin = 8 + content_width = max(200, self.width() - 2 * margin) + + label = ClickableEquationLabel(overlay) + # Reset to a plain Preferred policy for this instance: the + # "Ignored" horizontal policy set by ClickableEquationLabel's + # constructor (needed so the compact inline view can shrink below + # its sizeHint instead of stretching the mask's layout) confuses + # Qt's heightForWidth bookkeeping once word-wrap is turned on here, + # which left a large empty band above the text instead of sizing + # the overlay to the actual wrapped content. + label.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred) + label.setWordWrap(True) + label.setText(self.build_equation_html(self.reaction, multiline=True)) + label.set_metabolite_names(self.reaction) + label.linkActivated.connect(self.jump_to_metabolite_by_id) + # Closing on link-click as well as on focus-out (default Popup + # behavior) keeps the overlay from lingering after it's served its + # purpose. + label.linkActivated.connect(overlay.close) + # Clicking anywhere else in the overlay's equation view switches it + # into an editable one (see enter_equation_edit_mode_in_overlay). + label.clicked.connect(self.enter_equation_edit_mode_in_overlay) + + # Compute the wrapped height directly for the fixed content width + # instead of going through a layout + adjustSize(), and position + # the label explicitly -- see the size-policy note above for why. + label.setFixedWidth(content_width) + label.setFixedHeight(label.heightForWidth(content_width)) + label.move(margin, margin) + overlay.setFixedSize(content_width + 2 * margin, label.height() + 2 * margin) + return label + + def enter_equation_edit_mode_in_overlay(self): + """Triggered by clicking the overlay's read-only equation view + anywhere except a metabolite link: replaces it with a throwaway + EquationTextEdit that fills the *entire* overlay, so the whole + (possibly very long) equation is editable at once with proper word + wrap, rather than shrinking down to a single cramped line. + + self.equation itself is never touched here -- it stays put in + equation_stack the whole time -- so there is nothing shared that + could be left dangling if the overlay closes mid-edit; see + commit_overlay_equation_edit/handle_overlay_closing for how the + result gets written back into it. + """ + overlay = self._equation_overlay + if overlay is None or self._overlay_editor is not None: + return + self._overlay_label.hide() + + margin = 8 + editor = EquationTextEdit(overlay) + editor.setPlainText(self.equation.text()) + editor.editingFinished.connect(self.commit_overlay_equation_edit) + editor.cancelled.connect(self.cancel_overlay_equation_edit) + editor.move(margin, margin) + editor.resize(overlay.width() - 2 * margin, overlay.height() - 2 * margin) + editor.show() + editor.setFocus() + #editor.selectAll() + self._overlay_editor = editor + + def commit_overlay_equation_edit(self, closing: bool = False): + """Write the overlay editor's text back into self.equation and run + it through the normal validation/apply pipeline. Called on Enter, + on focus-out (blur), and -- with closing=True -- when the overlay + itself is being dismissed while an edit is still in progress. + """ + if self._overlay_editor is None: + return + # Collapse any wrapping/whitespace introduced by the multi-line + # display back into the single-line string self.equation expects. + new_text = " ".join(self._overlay_editor.toPlainText().split()) + if new_text != self.equation.text(): + self.equation.setText(new_text) + self.equation.setModified(True) + if self.equation.isModified(): + self.reaction_data_changed() # validates, applies and calls refresh_equation_view() + if not closing: + if self.equation_valid: + self.restore_overlay_to_link_view() + else: + turn_red(self._overlay_editor) + + def cancel_overlay_equation_edit(self): + """Escape in the overlay editor: discard whatever was typed and go + back to the read-only view without touching self.equation at all.""" + self.restore_overlay_to_link_view() + + def restore_overlay_to_link_view(self): + """Swap the overlay's editor back out for the read-only, link-ified + view, resizing the overlay to fit the (possibly just-changed) + equation.""" + overlay = self._equation_overlay + if overlay is None or self._overlay_editor is None: + return + editor = self._overlay_editor + self._overlay_editor = None # cleared first: see commit_overlay_equation_edit's guard + editor.hide() + editor.deleteLater() + self._overlay_label = self.build_overlay_label(overlay) + self._overlay_label.show() + + def handle_overlay_closing(self): + """Called by EquationOverlay before it is destroyed, however it was + dismissed (outside click, Escape closing the whole overlay, a + metabolite link, or the toggle button). Commits any edit still in + progress in the overlay's editor before it goes away, and schedules + equation_expand_button's click handler to be reconnected -- see + show_equation_overlay for why a short real delay is used rather + than reconnecting immediately or via a queued (0 ms) call. + """ + if self._overlay_editor is not None: + self.commit_overlay_equation_edit(closing=True) + self._equation_overlay = None + self._overlay_label = None + self._overlay_editor = None + self.equation_expand_button.setText("\u25be") + self.equation_expand_button.setToolTip("Show full reaction equation") + QTimer.singleShot(200, self._reconnect_equation_expand_button) + + @Slot() + def _reconnect_equation_expand_button(self): + self.equation_expand_button.clicked.connect(self.show_equation_overlay) + + def show_equation_overlay(self): + """Show the full reaction equation, formatted with word-wrap and + clickable metabolite links, in a popup overlay spanning the width + of the Reactions tab. + + equation_expand_button's own clicked signal is disconnected for as + long as the overlay stays open, and only reconnected -- after a + short delay, see handle_overlay_closing -- once it's fully closed + again. This is what makes the button double as a close button + without any risk of it immediately reopening what it just closed: + a Qt::Popup's automatic outside-click dismissal can redeliver that + same closing click to the button underneath as an ordinary second + click, but since nothing is connected to react to it for the + entire time the overlay is open, that redelivered click (however + many times it happens to fire) simply does nothing. + + The reconnect uses a short (200 ms) QTimer rather than a queued + (0 ms) call: a queued call's ordering relative to the redelivered + click turned out to be platform-dependent -- on Windows, hiding + the popup appears to pump the native event queue as part of tearing + the window down, which can drain even a 0 ms queued call before + the redelivered click is dispatched, reconnecting too early; on + Linux it does not. A real, if brief, delay reconnects reliably + after both have settled on either platform, while still being far + too short to affect a genuine, deliberate next click. + """ + if self.reaction is None or self._equation_overlay is not None: + return + self.equation_expand_button.clicked.disconnect(self.show_equation_overlay) + + overlay = EquationOverlay(self) + overlay.setFrameShape(QFrame.Shape.StyledPanel) + overlay.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) + # overlay.setAttribute(Qt.WidgetAttribute.WA_NoMouseReplay) + # overlay.setAttribute(Qt.WidgetAttribute.WA_NoMousePropagation) + self._equation_overlay = overlay + + # This mask sits in a vertical splitter above/below the reaction + # list, so it already spans the Reactions tab's full width; using + # that width (instead of a narrow fixed-size popup that can run off + # the right edge of the window) gives word-wrap enough room to lay + # out several metabolites per line, which is what long equations + # like biomass reactions need. + self._overlay_label = self.build_overlay_label(overlay) + + top_left = self.mapToGlobal(QPoint(0, 0)) + button_bottom = self.equation_expand_button.mapToGlobal( + QPoint(0, self.equation_expand_button.height())).y() + overlay.move(top_left.x(), button_bottom) + self.equation_expand_button.setText("\u25b4") + self.equation_expand_button.setToolTip("Hide full reaction equation") + overlay.show() + + def resizeEvent(self, event): + super().resizeEvent(event) + self.update_equation_expand_button() jumpToMap = Signal(str, str) jumpToMetabolite = Signal(str) diff --git a/cnapy/gui_elements/rename_map_dialog.py b/cnapy/gui_elements/rename_map_dialog.py index 5e93463f..653309e0 100644 --- a/cnapy/gui_elements/rename_map_dialog.py +++ b/cnapy/gui_elements/rename_map_dialog.py @@ -41,6 +41,7 @@ def apply(self): if not new_name in self.appdata.project.maps.keys(): self.appdata.project.maps[new_name] = self.appdata.project.maps.pop( self.old_name) + self.appdata.project.maps[new_name]["name"] = new_name self.central_widget.map_tabs.setTabText(self.idx, new_name) m = self.central_widget.map_tabs.widget(self.idx) m.name = new_name diff --git a/cnapy/gui_elements/scenario_tab.py b/cnapy/gui_elements/scenario_tab.py index fbe4d64c..8e9171fc 100644 --- a/cnapy/gui_elements/scenario_tab.py +++ b/cnapy/gui_elements/scenario_tab.py @@ -9,6 +9,7 @@ import cobra from cnapy.appdata import AppData, Scenario +from cnapy.gui_elements.map_view import validate_value from cnapy.utils import QComplReceivLineEdit, format_scenario_constraint, turn_red, turn_white, BACKGROUND_COLOR from straindesign.parse_constr import linexpr2dict, linexprdict2str, lineq2list @@ -18,9 +19,10 @@ class OptimizationDirection(IntEnum): class ScenarioReactionColumn(IntEnum): Id = 0 - Flux = 1 - LB = 2 - UB = 3 + Scenario = 1 + Flux = 2 + LB = 3 + UB = 4 class ScenarioAnnotationColumn(IntEnum): Reaction_id = 0 @@ -61,7 +63,7 @@ def __init__(self, central_widget): upper = QWidget() upper_layout = QVBoxLayout(upper) label = QLabel("Scenario reactions") - label.setToolTip("The IDs of the scenario reactions must be distinct from those in the network.\nYou may introduce new metabolites in scenario reactions.") + label.setToolTip("The IDs of the scenario reactions must be distinct from those in the network.\nYou may introduce new metabolites in scenario reactions.\nLike for the network reactions, a scenario value overrides the bounds (LB, UB).") hbox = QHBoxLayout() hbox.addWidget(label) self.add_reaction = QPushButton("+") @@ -73,7 +75,7 @@ def __init__(self, central_widget): upper_layout.addLayout(hbox) self.reactions = QTableWidget(0, len(ScenarioReactionColumn)) self.reactions.setHorizontalHeaderLabels([ScenarioReactionColumn(i).name for i in range(len(ScenarioReactionColumn))]) - self.reactions.setEditTriggers(QAbstractItemView.CurrentChanged | QAbstractItemView.SelectedClicked) + self.reactions.setEditTriggers(QAbstractItemView.EditTrigger.CurrentChanged | QAbstractItemView.EditTrigger.SelectedClicked) upper_layout.addWidget(self.reactions) upper_layout.addWidget(QLabel("Reaction equation")) @@ -114,7 +116,7 @@ def __init__(self, central_widget): annotations_layout.addLayout(hbox) self.annotations = QTableWidget(0, len(ScenarioAnnotationColumn)) self.annotations.setHorizontalHeaderLabels([ScenarioAnnotationColumn(i).name for i in range(len(ScenarioAnnotationColumn))]) - self.annotations.setEditTriggers(QAbstractItemView.CurrentChanged | QAbstractItemView.SelectedClicked) + self.annotations.setEditTriggers(QAbstractItemView.EditTrigger.CurrentChanged | QAbstractItemView.EditTrigger.SelectedClicked) annotations_layout.addWidget(self.annotations) bottom = QWidget() @@ -125,7 +127,7 @@ def __init__(self, central_widget): self.description.setPlaceholderText("Enter a description for this scenario") bottom_layout.addWidget(self.description) - self.splitter = QSplitter(Qt.Vertical) + self.splitter = QSplitter(Qt.Orientation.Vertical) self.splitter.addWidget(upper) self.splitter.addWidget(lower) self.splitter.addWidget(scenario_annotations) @@ -159,20 +161,25 @@ def update(self): with QSignalBlocker(self.scenario_opt_direction): self.scenario_opt_direction.setCurrentIndex(OptimizationDirection[self.appdata.project.scen_values.objective_direction]) - for row in range(self.reactions.rowCount()): - reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.UserRole) - if reac_id in self.appdata.project.comp_values: - (vl, vu) = self.appdata.project.comp_values[reac_id] - flux_text, background_color, _ = self.appdata.flux_value_display(vl, vu) - else: - flux_text = '' - background_color = Qt.white - item = self.reactions.item(row, ScenarioReactionColumn.Flux) - item.setText(flux_text) - item.setBackground(QBrush(background_color)) + with QSignalBlocker(self.reactions): # the scenario values are editable, setting them must not trigger cell_content_changed + for row in range(self.reactions.rowCount()): + reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.ItemDataRole.UserRole) + self.update_scenario_value_item(row, reac_id) + if reac_id in self.appdata.project.comp_values: + (vl, vu) = self.appdata.project.comp_values[reac_id] + flux_text, background_color, _ = self.appdata.flux_value_display(vl, vu) + else: + flux_text = '' + background_color = Qt.GlobalColor.white + item = self.reactions.item(row, ScenarioReactionColumn.Flux) + item.setText(flux_text) + item.setBackground(QBrush(background_color)) def recreate_scenario_items(self): # assumes that the objective, reactions and constraints are all valid + focus_widget: QWidget = self.focusWidget() + if focus_widget: + focus_widget.clearFocus() # make sure no focusOut events are triggered while the destruction of the previous widgets proceeds with QSignalBlocker(self.scenario_objective): self.scenario_objective.setText(linexprdict2str(self.appdata.project.scen_values.objective_coefficients)) with QSignalBlocker(self.use_scenario_objective): @@ -199,21 +206,40 @@ def recreate_scenario_items(self): with QSignalBlocker(constraint_edit): constraint_edit.setText(format_scenario_constraint(constr)) + self.annotations.clearContents() + annotations = self.appdata.project.scen_values.annotations + with QSignalBlocker(self.annotations): + self.annotations.setRowCount(len(annotations)) + for row, annotation in enumerate(annotations): + self.new_annotation_row(row) + reaction_id_edit = self.annotations.cellWidget(row, ScenarioAnnotationColumn.Reaction_id) + with QSignalBlocker(reaction_id_edit): + reaction_id_edit.setText(annotation.get("reaction_id", "")) + self.annotations.item(row, ScenarioAnnotationColumn.Key).setText(annotation.get("key", "")) + self.annotations.item(row, ScenarioAnnotationColumn.Value).setText(annotation.get("value", "")) + with QSignalBlocker(self.description): self.description.setText(self.appdata.project.scen_values.description) @Slot(int, int) def cell_content_changed(self, row: int, column: int): - reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.UserRole) + reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.ItemDataRole.UserRole) if column == ScenarioReactionColumn.Id: new_reac_id = self.reactions.currentItem().text().strip() with QSignalBlocker(self.reactions): if len(new_reac_id) == 0: self.reactions.item(row, ScenarioReactionColumn.Id).setText(reac_id) elif self.verify_scenario_reaction_id(new_reac_id): - self.reactions.item(row, ScenarioReactionColumn.Id).setData(Qt.UserRole, new_reac_id) - self.appdata.project.scen_values.reactions[new_reac_id] = self.appdata.project.scen_values.reactions[reac_id] - del self.appdata.project.scen_values.reactions[reac_id] + self.reactions.item(row, ScenarioReactionColumn.Id).setData(Qt.ItemDataRole.UserRole, new_reac_id) + self.appdata.record_current_scenario() + scen_values = self.appdata.project.scen_values + scen_values.reactions[new_reac_id] = scen_values.reactions[reac_id] + del scen_values.reactions[reac_id] + if reac_id in scen_values: # the scenario value moves along with the reaction + scen_values[new_reac_id] = scen_values.pop(reac_id) + if reac_id in self.appdata.project.comp_values: + self.appdata.project.comp_values[new_reac_id] = self.appdata.project.comp_values.pop(reac_id) + scen_values.set_hash_value() else: self.reactions.item(row, ScenarioReactionColumn.Id).setText(reac_id) QMessageBox.information(self, 'Reaction ID already in use', @@ -228,6 +254,7 @@ def cell_content_changed(self, row: int, column: int): ub, ub_brush = self.verify_bound(self.reactions.item(row, ScenarioReactionColumn.UB)) if not (isnan(lb) or isnan(ub)): if lb <= ub: + self.appdata.record_current_scenario() self.appdata.project.scen_values.reactions[reac_id][1] = lb self.appdata.project.scen_values.reactions[reac_id][2] = ub self.update_reaction_equation(reac_id) @@ -239,6 +266,19 @@ def cell_content_changed(self, row: int, column: int): ub_brush = red_brush self.reactions.item(row, ScenarioReactionColumn.LB).setBackground(lb_brush) self.reactions.item(row, ScenarioReactionColumn.UB).setBackground(ub_brush) + elif column == ScenarioReactionColumn.Scenario: + scen_text = self.reactions.item(row, ScenarioReactionColumn.Scenario).text().strip() + new_value = validate_value(scen_text) if len(scen_text) > 0 else None # None: remove the scenario value + if new_value is False: + with QSignalBlocker(self.reactions): + self.reactions.item(row, ScenarioReactionColumn.Scenario).setBackground(red_brush) + return + if new_value != self.appdata.project.scen_values.get(reac_id, None): + self.central_widget.update_reaction_value(reac_id, scen_text) + self.scenario_changed() + if self.appdata.auto_fba: + self.central_widget.parent.fba() + self.update() # displays the value in the standard format @Slot(int, int) def cell_content_changed_annotations(self, row: int, column: int): @@ -250,9 +290,20 @@ def cell_content_changed_annotations(self, row: int, column: int): "key": key, "value": value, } + self.appdata.record_current_scenario() self.appdata.project.scen_values.annotations[row] = changed_annotation self.scenario_changed() + @Slot() + def annotation_reaction_id_edited(self): + # the reaction ID is edited in a cell widget which does not trigger cellChanged + reaction_id_edit = self.sender() + for row in range(self.annotations.rowCount()): + if self.annotations.cellWidget(row, ScenarioAnnotationColumn.Reaction_id) is reaction_id_edit: + if reaction_id_edit.text().strip() != self.appdata.project.scen_values.annotations[row].get("reaction_id", ""): + self.cell_content_changed_annotations(row, ScenarioAnnotationColumn.Reaction_id) + break + def verify_bound(self, item: QTableWidgetItem): try: val = float(item.text()) @@ -267,7 +318,7 @@ def equation_edited(self): if self.equation.isModified(): self.equation.setModified(False) row: int = self.reactions.currentRow() - reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.UserRole) + reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.ItemDataRole.UserRole) existing_metabolites = set(self.appdata.project.cobra_py_model.metabolites.list_attr('id')) if reac_id in self.appdata.project.cobra_py_model.reactions: # overwrite existing reaction reaction = self.appdata.project.cobra_py_model.reactions.get_by_id(reac_id) @@ -281,6 +332,7 @@ def equation_edited(self): if len(eqtxt) > 0 and eqtxt[-1] == '+': raise ValueError reaction.build_reaction_from_string(eqtxt) + self.appdata.record_current_scenario() self.appdata.project.scen_values.reactions[reac_id][0] = {m.id: c for m,c in reaction.metabolites.items()} if (self.appdata.project.scen_values.reactions[reac_id][1] < 0 and reaction.lower_bound >= 0) or \ (self.appdata.project.scen_values.reactions[reac_id][1] > 0 and reaction.lower_bound <= 0): @@ -319,7 +371,7 @@ def handle_current_cell_changed(self, row: int, column: int, previous_row: int, else: self.equation.setEnabled(True) if row != previous_row: - self.update_reaction_equation(self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.UserRole)) + self.update_reaction_equation(self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.ItemDataRole.UserRole)) turn_white(self.equation, self.appdata.is_in_dark_mode) def update_reaction_equation(self, reac_id: str): @@ -363,9 +415,12 @@ def add_scenario_reaction(self): def delete_scenario_reaction(self): row: int = self.reactions.currentRow() if row >= 0: - reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.UserRole) + reac_id: str = self.reactions.item(row, ScenarioReactionColumn.Id).data(Qt.ItemDataRole.UserRole) self.reactions.removeRow(row) + self.appdata.record_current_scenario() del self.appdata.project.scen_values.reactions[reac_id] + self.appdata.project.scen_values.pop(reac_id, None) # also rehashes + self.appdata.project.comp_values.pop(reac_id, None) self.appdata.project.update_reaction_id_lists() self.check_constraints_and_objective() self.reactions.setCurrentCell(self.reactions.currentRow(), 0) # to make the cell appear selected in the GUI @@ -377,32 +432,37 @@ def add_scenario_annotation(self): row: int = self.annotations.rowCount() self.annotations.setRowCount(row + 1) with QSignalBlocker(self.annotations): + self.appdata.record_current_scenario() self.appdata.project.scen_values.annotations.append({}) self.new_annotation_row(row) self.scenario_changed() - self.annotations.setCurrentCell(row, ScenarioReactionColumn.Id) + self.annotations.setCurrentCell(row, ScenarioAnnotationColumn.Reaction_id) def delete_scenario_annotation(self): row: int = self.annotations.currentRow() if row >= 0: + self.appdata.record_current_scenario() # before the deletion so that it can be undone del(self.appdata.project.scen_values.annotations[row]) self.annotations.removeRow(row) self.annotations.setCurrentCell(self.annotations.currentRow(), 0) # to make the cell appear selected in the GUI self.scenario_changed() def new_annotation_row(self, row: int): - self.annotations.setCellWidget(row, ScenarioAnnotationColumn.Reaction_id, - QComplReceivLineEdit(self, self.appdata.project.reaction_ids)) + reaction_id_edit = QComplReceivLineEdit(self, self.appdata.project.reaction_ids) + reaction_id_edit.editingFinished.connect(self.annotation_reaction_id_edited) + self.annotations.setCellWidget(row, ScenarioAnnotationColumn.Reaction_id, reaction_id_edit) self.annotations.setItem(row, ScenarioAnnotationColumn.Key, QTableWidgetItem()) self.annotations.setItem(row, ScenarioAnnotationColumn.Value, QTableWidgetItem()) - self.scenario_changed() def new_reaction_row(self, row: int): item = QTableWidgetItem() self.reactions.setItem(row, ScenarioReactionColumn.Id, item) + scen_item = QTableWidgetItem() + scen_item.setToolTip("Scenario value, either a single value or lower and upper bound separated by a comma.\nOverrides the bounds of the scenario reaction.") + self.reactions.setItem(row, ScenarioReactionColumn.Scenario, scen_item) flux_item = QTableWidgetItem() - flux_item.setFlags(Qt.ItemIsSelectable) # not editable - flux_item.setForeground(item.foreground()) # to keep text color black + flux_item.setFlags(Qt.ItemFlag.ItemIsSelectable) # not editable + flux_item.setForeground(QBrush(Qt.GlobalColor.black)) # the background is always a light color, also in dark mode self.reactions.setItem(row, ScenarioReactionColumn.Flux, flux_item) self.reactions.setItem(row, ScenarioReactionColumn.LB, QTableWidgetItem()) self.reactions.setItem(row, ScenarioReactionColumn.UB, QTableWidgetItem()) @@ -412,11 +472,25 @@ def update_reaction_row(self, row: int, reac_id: str): reac_id_item = self.reactions.item(row, ScenarioReactionColumn.Id) reac_id_item.setText(reac_id) reac_id_item.setToolTip(reac_id) - reac_id_item.setData(Qt.UserRole, reac_id) + reac_id_item.setData(Qt.ItemDataRole.UserRole, reac_id) + self.update_scenario_value_item(row, reac_id) _, lb, ub = self.appdata.project.scen_values.reactions[reac_id] self.reactions.item(row, ScenarioReactionColumn.LB).setText(str(lb)) self.reactions.item(row, ScenarioReactionColumn.UB).setText(str(ub)) + def update_scenario_value_item(self, row: int, reac_id: str): + item = self.reactions.item(row, ScenarioReactionColumn.Scenario) + if reac_id in self.appdata.project.scen_values: + vl, vu = self.appdata.project.scen_values[reac_id] + text = self.appdata.format_flux_value(vl) + if vl != vu: + text = text + ", " + self.appdata.format_flux_value(vu) + item.setText(text) + item.setBackground(QBrush(self.appdata.scen_color)) + else: + item.setText("") + item.setData(Qt.ItemDataRole.BackgroundRole, None) # default background + @Slot() def add_constraint(self): row, _ = self.add_constraint_row() @@ -448,6 +522,7 @@ def delete_constraint(self): if row >= 0: with QSignalBlocker(self.constraints): # does not appear to suppress the focus out event self.constraints.removeRow(row) + self.appdata.record_current_scenario() del self.appdata.project.scen_values.constraints[row] self.scenario_changed() if self.appdata.auto_fba: @@ -459,6 +534,7 @@ def constraint_edited(self, text_correct: bool): if row >= 0: # in case this is triggered when nothing is selected constraint_edit: QComplReceivLineEdit = self.constraints.cellWidget(row, 0) if text_correct: + self.appdata.record_current_scenario() self.appdata.project.scen_values.constraints[row] = lineq2list([constraint_edit.text()], self.appdata.project.reaction_ids.id_list)[0] else: @@ -479,6 +555,7 @@ def change_scenario_objective_coefficients(self, text_correct: bool): if text_correct: new_objective = linexpr2dict(self.scenario_objective.text(), self.appdata.project.reaction_ids.id_list) if new_objective != self.appdata.project.scen_values.objective_coefficients: + self.appdata.record_current_scenario() self.appdata.project.scen_values.objective_coefficients = new_objective self.scenario_changed() if self.appdata.project.scen_values.use_scenario_objective: @@ -500,10 +577,7 @@ def validate_objective(self): def use_scenario_objective_changed(self, state: int): self.validate_objective() if self.use_scenario_objective.isEnabled(): - if state == Qt.Checked: - self.appdata.project.scen_values.use_scenario_objective = True - elif state == Qt.Unchecked: - self.appdata.project.scen_values.use_scenario_objective = False + self.appdata.project.scen_values.use_scenario_objective = self.use_scenario_objective.isChecked() self.objectiveSetupChanged.emit() @Slot(int) diff --git a/cnapy/gui_elements/strain_design_dialog.py b/cnapy/gui_elements/strain_design_dialog.py index 4c15cf8e..aa7028a5 100644 --- a/cnapy/gui_elements/strain_design_dialog.py +++ b/cnapy/gui_elements/strain_design_dialog.py @@ -1,6 +1,7 @@ """The dialog for calculating minimal cut sets""" from contextlib import redirect_stdout, redirect_stderr +from functools import partial import io import json import os @@ -106,7 +107,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.current_module = 0 self.scrollArea = QScrollArea() self.layout = QVBoxLayout() - # self.layout.setAlignment(Qt.Alignment(Qt.AlignTop^Qt.AlignLeft)) + self.layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft) self.layout.setSizeConstraint(QLayout.SetFixedSize) self.layout.setSizeConstraint(QLayout.SetMinAndMaxSize) self.modules_box = QGroupBox("Strain design module(s)") @@ -122,10 +123,10 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.module_list.setFixedWidth(195) self.module_list.setMinimumHeight(40) self.module_list.setHorizontalHeaderLabels(["Module Type",""]) - # self.module_list.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) + # self.module_list.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) # self.module_list.verticalHeader().setVisible(False) - self.module_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) - self.module_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.Fixed) + self.module_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) + self.module_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed) self.module_list.horizontalHeader().resizeSection(0, 115) self.module_list.horizontalHeader().resizeSection(1, 60) # -> first entry in module list @@ -155,7 +156,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.module_spec_box.setObjectName("EditModule") self.module_spec_box.setStyleSheet(BORDER_COLOR("#b0b0b0")) module_spec_layout = QVBoxLayout() - module_spec_layout.setAlignment(Qt.AlignTop) + module_spec_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.module_edit = {} # module sense @@ -213,7 +214,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.module_edit[CONSTRAINTS+"_label"] = QLabel("Constraints") module_spec_layout.addWidget(self.module_edit[CONSTRAINTS+"_label"]) constr_list_layout = QHBoxLayout() - constr_list_layout.setAlignment(Qt.Alignment(Qt.AlignTop^Qt.AlignLeft)) + constr_list_layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft) module_spec_layout.addItem(constr_list_layout) # layout for constraint list and buttons @@ -222,7 +223,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.module_edit[CONSTRAINTS].setMinimumHeight(40) self.module_edit[CONSTRAINTS].verticalHeader().setDefaultSectionSize(18) self.module_edit[CONSTRAINTS].verticalHeader().setVisible(False) - self.module_edit[CONSTRAINTS].horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) + self.module_edit[CONSTRAINTS].horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) self.module_edit[CONSTRAINTS].horizontalHeader().setVisible(False) # -> first entry in constraint list # constr_entry = ComplReceivLineEdit(self,self.reac_wordlist) @@ -249,7 +250,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.module_edit["module_apply_button"] = QPushButton("Check module") self.module_edit["module_apply_button"].clicked.connect(self.module_apply) self.module_edit["module_del_button"] = QPushButton("Delete module") - self.module_edit["module_del_button"].clicked.connect(self.rem_module,True) + self.module_edit["module_del_button"].clicked.connect(partial(self.rem_module, True)) module_buttons_layout.addWidget(self.module_edit["module_apply_button"]) module_buttons_layout.addWidget(self.module_edit["module_del_button"]) module_spec_layout.addItem(module_buttons_layout) @@ -379,7 +380,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.regulatory_box.setObjectName("reg") self.regulatory_layout = QVBoxLayout() - self.regulatory_layout.setAlignment(Qt.AlignLeft) + self.regulatory_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) # self.regulatory_itv_list_label = QLabel("") # self.regulatory_layout.addWidget(self.regulatory_itv_list_label) @@ -390,8 +391,8 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): # self.regulatory_itv_list.horizontalHeader().setVisible(False) self.regulatory_itv_list.setHorizontalHeaderLabels(["Regulatory constraint","Cost"]) self.regulatory_itv_list.setMinimumWidth(110) - self.regulatory_itv_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) - self.regulatory_itv_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.Fixed) + self.regulatory_itv_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) + self.regulatory_itv_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed) self.regulatory_itv_list.horizontalHeader().resizeSection(0, 50) self.regulatory_itv_list.horizontalHeader().resizeSection(1, 40) # # -> first entry in constraint list @@ -423,7 +424,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.ko_ki_box.setHidden(True) self.ko_ki_box.setObjectName("ko_ki") ko_ki_layout = QVBoxLayout() - ko_ki_layout.setAlignment(Qt.AlignLeft) + ko_ki_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) # ko_ki_lists_layout.addWidget(self.reaction_itv_list_widget) @@ -441,7 +442,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): # reaction list reaction_interventions_layout = QVBoxLayout() - reaction_interventions_layout.setAlignment(Qt.AlignTop) + reaction_interventions_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.reaction_itv_list_widget = QWidget() self.reaction_itv_list_widget.setFixedWidth(270) self.reaction_itv_list = QTableCopyable(0, 3) @@ -452,9 +453,9 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.reaction_itv_list.setMinimumHeight(35) self.reaction_itv_list.setMaximumHeight(150) self.reaction_itv_list.setHorizontalHeaderLabels(["Reaction","KO N/A KI ","Cost"]) - self.reaction_itv_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) - self.reaction_itv_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.Fixed) - self.reaction_itv_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.Fixed) + self.reaction_itv_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) + self.reaction_itv_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed) + self.reaction_itv_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed) self.reaction_itv_list.horizontalHeader().resizeSection(0, 80) self.reaction_itv_list.horizontalHeader().resizeSection(1, 80) self.reaction_itv_list.horizontalHeader().resizeSection(2, 40) @@ -472,15 +473,15 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): self.reaction_itv[r]['cost'].setEditable(True) r_ko_ki_button_widget = QWidget() r_ko_ki_button_layout = QHBoxLayout() - r_ko_ki_button_layout.setAlignment(Qt.AlignCenter) + r_ko_ki_button_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) r_ko_ki_button_layout.setContentsMargins(0,0,0,0) r_ko_button = QRadioButton() r_na_button = QRadioButton() r_ki_button = QRadioButton() r_ko_button.setChecked(True) - r_ko_ki_button_widget.setFocusPolicy(Qt.NoFocus) - # self.reaction_itv_list.setEditTriggers(QAbstractItemView.NoEditTriggers); - # self.reaction_itv_list.setFocusPolicy(Qt.NoFocus) + r_ko_ki_button_widget.setFocusPolicy(Qt.FocusPolicy.NoFocus) + # self.reaction_itv_list.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers); + # self.reaction_itv_list.setFocusPolicy(Qt.FocusPolicy.NoFocus) # self.reaction_itv_list.setSelectionMode(QAbstractItemView.NoSelection) self.reaction_itv[r]['button_group'].addButton(r_ko_button,1) self.reaction_itv[r]['button_group'].addButton(r_na_button,2) @@ -513,22 +514,22 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): # gene list gene_interventions_layout = QVBoxLayout() - gene_interventions_layout.setAlignment(Qt.AlignTop) + gene_interventions_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.gene_itv_list_widget = QWidget() self.gene_itv_list_widget.setHidden(True) self.gene_itv_list_widget.setFixedWidth(270) self.gene_itv_list = QTableCopyable(0, 3) - # self.gene_itv_list.setEditTriggers(QAbstractItemView.NoEditTriggers); - # self.gene_itv_list.setFocusPolicy(Qt.NoFocus) + # self.gene_itv_list.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers); + # self.gene_itv_list.setFocusPolicy(Qt.FocusPolicy.NoFocus) # self.gene_itv_list.setSelectionMode(QAbstractItemView.NoSelection) self.gene_itv_list.verticalHeader().setDefaultSectionSize(18) self.gene_itv_list.verticalHeader().setVisible(False) self.gene_itv_list.setFixedWidth(220) self.gene_itv_list.setMinimumHeight(50) self.gene_itv_list.setHorizontalHeaderLabels(["Gene","KO N/A KI ","Cost"]) - self.gene_itv_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) - self.gene_itv_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.Fixed) - self.gene_itv_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.Fixed) + self.gene_itv_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) + self.gene_itv_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed) + self.gene_itv_list.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed) self.gene_itv_list.horizontalHeader().resizeSection(0, 80) self.gene_itv_list.horizontalHeader().resizeSection(1, 80) self.gene_itv_list.horizontalHeader().resizeSection(2, 40) @@ -549,7 +550,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): 'button_group': QButtonGroup()}}) g_ko_ki_button_widget = QWidget() g_ko_ki_button_layout = QHBoxLayout() - g_ko_ki_button_layout.setAlignment(Qt.AlignCenter) + g_ko_ki_button_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) g_ko_ki_button_layout.setContentsMargins(0,0,0,0) g_ko_button = QRadioButton() g_na_button = QRadioButton() @@ -613,7 +614,7 @@ def __init__(self, appdata: AppData, sd_setup: Dict = {}): # Connecting signals try: self.appdata.window.centralWidget().broadcastReactionID.connect(self.receive_input) - self.launch_computation_signal.connect(self.appdata.window.compute_strain_design,Qt.QueuedConnection) + self.launch_computation_signal.connect(self.appdata.window.compute_strain_design,Qt.ConnectionType.QueuedConnection) except: print('Signals to main window could not be connected.') @@ -678,12 +679,13 @@ def add_module(self): self.update_module_edit() self.update_global_objective() - def rem_module(self,*args): + @Slot(bool) + def rem_module(self, arg: bool): if self.module_list.rowCount() == 0: self.modules = [] self.current_module = -1 return - if args: + if arg: i = self.current_module if self.module_list.selectedIndexes(): i = self.module_list.selectedIndexes()[0].row() @@ -908,7 +910,7 @@ def update_module_edit(self): self.update_global_objective() def verify_module(self,*args): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) if not self.modules: return True, None module_no = args[0] @@ -950,14 +952,14 @@ def verify_module(self,*args): module = SDModule(model,module_type=OPTCOUPLE, inner_objective=inner_objective,\ inner_opt_sense=MAXIMIZE, prod_id=prod_id,\ min_gcp=min_gcp, constraints=constraints) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) return True, module except Exception as e: QMessageBox.warning(self,"Module invalid",\ "The current module is either infeasible or "+\ "syntactic errors persist in the module's specificaiton. \n\n"+\ "Exception details: \n\n"+str(e)) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) return False, None @Slot(bool) @@ -1056,7 +1058,7 @@ def show_ko_ki(self): self.layout.setSizeConstraint(QLayout.SetMinAndMaxSize) def ko_ki_filter_text_changed(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) txt = self.ko_ki_filter.text().lower().strip() hide_reacs = [True if txt not in r.lower() else False for r in self.reac_ids] for i,h in enumerate(hide_reacs): @@ -1065,7 +1067,7 @@ def ko_ki_filter_text_changed(self): for g,n in zip(self.gene_ids,self.gene_names)] for i,h in enumerate(hide_genes): self.gene_itv_list.setRowHidden(i,h) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) def knock_changed(self,id,gene_or_reac): if gene_or_reac == 'reac': @@ -1158,7 +1160,7 @@ def set_none_g_koable(self): self.knock_changed(r,'gene') def parse_dialog_inputs(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) sd_setup = {} # strain design setup sd_setup.update({MODEL_ID : self.appdata.project.cobra_py_model.id}) # Save modules. Therefore, first remove cobra model from all modules. It is reinserted afterwards @@ -1206,16 +1208,16 @@ def parse_dialog_inputs(self): gkiCost.update({self.gene_names[i]:float(self.gene_itv[g]['cost'].text())}) sd_setup.update({GKOCOST : gkoCost}) sd_setup.update({GKICOST : gkiCost}) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) return sd_setup def save(self): # if current module is invalid, abort - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) valid = self.module_apply() if not valid: return - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) # open file dialog dialog = QFileDialog(self) filename: str = dialog.getSaveFileName( @@ -1346,7 +1348,7 @@ def load(self, sd_setup = {}): self.compute_sd_button.setFocus() def compute(self): - QApplication.setOverrideCursor(Qt.BusyCursor) + QApplication.setOverrideCursor(Qt.CursorShape.BusyCursor) QApplication.processEvents() valid = self.module_apply() if not valid: @@ -1384,13 +1386,13 @@ def compute(self): if QMessageBox.information(self, "Regulatory interventions not supported", "optlang_enumerator does not support regulatory " +\ "interventions.\nAll regulatory interventions will be ignored.", - QMessageBox.Ok | QMessageBox.Cancel) == QMessageBox.Cancel: + QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel) == QMessageBox.StandardButton.Cancel: return if any(m[INNER_OBJECTIVE] is not None for m in sd_setup[MODULES]): if QMessageBox.information(self, "Inner objectives not supported", "optlang_enumerator does not support inner objectives.\n" +\ "All inner objectives will be ignored.", - QMessageBox.Ok | QMessageBox.Cancel) == QMessageBox.Cancel: + QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel) == QMessageBox.StandardButton.Cancel: return close_sd_dialog = self.compute_optlang(sd_setup) @@ -1429,22 +1431,13 @@ def compute_optlang(self, sd_setup) -> bool: enum_method = 3 with self.appdata.project.cobra_py_model as model: - update_stoichiometry_hash = False if sd_setup['use_scenario']: # integrate scenario into model bounds self.appdata.project.load_scenario_into_model(model) - if len(self.appdata.project.scen_values) > 0: - update_stoichiometry_hash = True for r in model.reactions: # make all reactions bounded for COBRApy FVA if r.lower_bound == -float('inf'): r.lower_bound = cobra.Configuration().lower_bound - r.set_hash_value() - update_stoichiometry_hash = True if r.upper_bound == float('inf'): r.upper_bound = cobra.Configuration().upper_bound - r.set_hash_value() - update_stoichiometry_hash = True - if self.appdata.use_results_cache and update_stoichiometry_hash: - model.set_stoichiometry_hash_object() targets = [] desired = [] @@ -1572,7 +1565,7 @@ def __init__(self, parent, appdata: AppData, sd_setup): @Slot(bytes) def conclude_computation(self,results): self.solutions = pickle.loads(results) - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) if self.solutions.get_num_sols() > 0: self.explore.setEnabled(True) @@ -1683,8 +1676,8 @@ def __init__(self, appdata: AppData, solutions, with_setup: bool): self.close() return self.setWindowTitle("Strain Design Solutions") - self.setWindowModality(Qt.NonModal) - self.setWindowFlags((self.windowFlags() | Qt.Window) & ~Qt.WindowStaysOnTopHint) + self.setWindowModality(Qt.WindowModality.NonModal) + self.setWindowFlags((self.windowFlags() | Qt.WindowType.Window) & ~Qt.WindowType.WindowStaysOnTopHint) self.setMinimumWidth(620) self.appdata = appdata appdata.project.sd_solutions = self.solutions @@ -1696,7 +1689,7 @@ def __init__(self, appdata: AppData, solutions, with_setup: bool): else: self.sd_table = QTableCopyable(0, 2) palette = QPalette() - palette.setColor(QPalette.Text, Qt.black) # Text color in widgets + palette.setColor(QPalette.ColorRole.Text, Qt.GlobalColor.black) # Text color in widgets self.sd_table.setPalette(palette) self.sd_table.verticalHeader().setDefaultSectionSize(20) self.sd_table.verticalHeader().setVisible(False) @@ -1739,10 +1732,10 @@ def __init__(self, appdata: AppData, solutions, with_setup: bool): self.sd_table.setMinimumHeight(150) self.sd_table.setHorizontalHeaderLabels(["Equiv. class","Intervention set",\ "Reaction-phenotype interventions"]) - self.sd_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) - self.sd_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Interactive) + self.sd_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) + self.sd_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Interactive) self.sd_table.horizontalHeader().resizeSection(0, 90) - self.sd_table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch) + self.sd_table.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.Stretch) self.rsd = ["" for _ in range(len(rsd))] for i,s in enumerate(rsd): for k,v in s.items(): @@ -1769,7 +1762,7 @@ def __init__(self, appdata: AppData, solutions, with_setup: bool): self.sd_table.insertRow(i) item = QTableItem(str(a+1)) item.setEditable(False) - item.setTextAlignment(Qt.AlignCenter) + item.setTextAlignment(Qt.AlignmentFlag.AlignCenter) self.sd_table.setItem(i, 0, item) # set non-editable item = QTableItem(g) @@ -1792,15 +1785,15 @@ def __init__(self, appdata: AppData, solutions, with_setup: bool): self.rsd[i] = self.rsd[i][0:-2] self.sd_table.setMinimumWidth(320) self.sd_table.setMinimumHeight(150) - self.sd_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) - self.sd_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) + self.sd_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) + self.sd_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) self.sd_table.horizontalHeader().resizeSection(0, 90) self.sd_table.setHorizontalHeaderLabels(["Equiv. class","Intervention set"]) for i,s in enumerate(self.rsd): self.sd_table.insertRow(i) item = QTableItem(str(i+1)) item.setEditable(False) - item.setTextAlignment(Qt.AlignCenter) + item.setTextAlignment(Qt.AlignmentFlag.AlignCenter) self.sd_table.setItem(i, 0, item) item = QTableItem(s) item.setEditable(False) diff --git a/cnapy/gui_elements/thermodynamics_dialog.py b/cnapy/gui_elements/thermodynamics_dialog.py index 5c7ac496..7507c39b 100644 --- a/cnapy/gui_elements/thermodynamics_dialog.py +++ b/cnapy/gui_elements/thermodynamics_dialog.py @@ -1,7 +1,34 @@ -"""The CNApy OptMDFpathway dialog""" +"""The CNApy OptMDFpathway dialog + +This dialog now uses ``OptMDFAnalysis`` from ``optmdfpathway.py`` directly on +the project's cobrapy model (ΔG'° / Cmin / Cmax are read straight from the +model's own reaction/metabolite annotations), instead of converting through +cobrak. This has two consequences for the dialog compared to the previous +cobrak-based version: + +* OptMDFpathway and thermodynamic bottleneck analysis are no longer two + separate computations that each rebuild the whole optimization problem + from scratch. Given one ``OptMDFAnalysis`` instance, finding the current + thermodynamic bottleneck (``find_bottleneck()``) is just another cheap + query against the already-solved problem, so both analyses are now part + of the single "OptMDFpathway" analysis type below (the old + ``BOTTLENECK_ANALYSIS`` type has been folded into it). Any other CNApy + code that used to construct this dialog with + ``ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS`` should be updated to + use ``ThermodynamicAnalysisTypes.OPTMDFPATHWAY`` instead. +* Because the ``OptMDFAnalysis`` object is kept alive for as long as the + dialog stays open (it is no longer auto-closed after a computation), the + user can now iteratively relax the reaction(s) currently limiting the MDF + and resolve -- one step at a time, or automatically down to a target MDF + -- watching how the optimum shifts as bottlenecks are removed. This + mirrors the iterative bottleneck-relaxation workflow demonstrated in + ``optmdfpathway.py``'s own ``__main__`` section (``OptMDFAnalysis.step()`` + / ``.run()``). +""" +from enum import Enum +from typing import Optional, Set + import cobra -import cobra.util.solver -from numpy import exp from qtpy.QtCore import Qt, Slot from qtpy.QtWidgets import ( QDialog, @@ -12,78 +39,123 @@ QPushButton, QVBoxLayout, QGroupBox, + QGridLayout, + QCompleter, + QAbstractItemView, + QTableWidget, + QTableWidgetItem, + QHeaderView, + QCheckBox, + QWidget ) from cnapy.appdata import AppData from cnapy.gui_elements.central_widget import CentralWidget -from cnapy.gui_elements.solver_buttons import get_solver_buttons -from enum import Enum -from cobrak.constants import LNCONC_VAR_PREFIX, DF_VAR_PREFIX, MDF_VAR_ID, ALL_OK_KEY, OBJECTIVE_VAR_NAME, TERMINATION_CONDITION_KEY -from cobrak.dataclasses import ExtraLinearConstraint, Solver -from cobrak.lps import perform_lp_optimization, perform_lp_thermodynamic_bottleneck_analysis -from cobrak.io import load_annotated_cobrapy_model_as_cobrak_model -from cobrak.cobrapy_model_functionality import get_fullsplit_cobra_model +# from cnapy.gui_elements.solver_buttons import get_solver_buttons +from cnapy.optmdfpathway import OptMDFAnalysis, OptMDFResult class ThermodynamicAnalysisTypes(Enum): + # OptMDFpathway now always also makes the current thermodynamic + # bottleneck (if any) available and lets the user relax it iteratively + # -- what used to be the separate BOTTLENECK_ANALYSIS type is simply + # part of this workflow now (see the module docstring above). OPTMDFPATHWAY = 1 THERMODYNAMIC_FBA = 2 - BOTTLENECK_ANALYSIS = 3 class ThermodynamicDialog(QDialog): """A dialog to perform several thermodynamic methods.""" + #: optlang solver-status -> (message box title, message box text) + _STATUS_MESSAGES = { + "infeasible": ( + "Problem infeasible", + "The problem appears to be infeasible, i.e. the constraints make a solution impossible.", + ), + "unbounded": ( + "Problem unbounded", + "The problem appears to be unbounded, i.e. there is no constraint limiting the objective values.", + ), + "infeasible_or_unbounded": ( + "Problem infeasible or unbounded", + "The solver could not tell infeasibility and unboundedness apart. Try tightening the model's bounds or use another solver.", + ), + "time_limit": ( + "Time limit", + "Solver's time limit hit. Please change solver or problem complexity.", + ), + "iteration_limit": ( + "Iterations limit", + "Solver's iterations limit hit. Please change solver or problem complexity.", + ), + "numeric": ( + "Numerical problem", + "The solver ran into numerical difficulties. Try another solver.", + ), + } + def __init__( - self, appdata: AppData, central_widget: CentralWidget, analysis_type: ThermodynamicAnalysisTypes + self, + appdata: AppData, + central_widget: CentralWidget, + main_window, + analysis_type: ThermodynamicAnalysisTypes ) -> None: - self.FWDID = "_FWDCNAPY" - self.REVID = "_REVCNAPY" + QDialog.__init__(self, main_window) - QDialog.__init__(self) if analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: - window_title = "Perform OptMDFpathway" - elif analysis_type == ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - window_title = "Perform OptMDFpathway bottleneck analysis" + window_title = "Perform OptMDFpathway (incl. bottleneck analysis)" elif analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: window_title = "Perform thermodynamic FBA" self.setWindowTitle(window_title) + self.setWindowModality(Qt.NonModal) self.appdata = appdata self.central_widget = central_widget self.analysis_type = analysis_type + self.main_window = main_window self.reac_ids = self.appdata.project.cobra_py_model.reactions.list_attr("id") self.metabolite_ids = self.appdata.project.cobra_py_model.metabolites.list_attr( "id" ) + # Set up once "Compute" has succeeded, and kept alive across button + # clicks so relaxing a bottleneck and resolving doesn't need to + # rebuild the model / FVA / MILP from scratch every time. + self.analysis: Optional[OptMDFAnalysis] = None + self.current_result: Optional[OptMDFResult] = None + self._solve = None # bound to analysis.solve or analysis.solve_fba once built + self._relaxed_so_far: Set[str] = set() + self._ratio_rows = [] + self.layout = QVBoxLayout() match analysis_type: case ThermodynamicAnalysisTypes.OPTMDFPATHWAY: label = QLabel( "Perform OptMDFpathway. ΔG'° values and metabolite concentration " - "ranges have to be given in relevant annotations." - ) - case ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - label = QLabel( - "Perform OptMDFpathway bottleneck analysis. ΔG'° values and metabolite concentration " - "ranges have to be given in relevant annotations.\nThe minimal amount of bottlenecks and their IDs " - "to reach the given minimal MDF will be shown in the console afterwards." + "ranges have to be given in relevant annotations.\n" + "After computing, the reaction(s) currently limiting the MDF (the " + "thermodynamic bottleneck, if any) are shown in the console below,\n" + "and can be relaxed -- one step at a time, or automatically up to " + "a target MDF -- to see how the optimum shifts as bottlenecks are " + "removed." ) case ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: label = QLabel( "Perform thermodynamic Flux Balance Analysis. Based on OptMDFpathway, you can perform an FBA at an OptMDF greater than the given value. " "\nE.g., if the OptMDF is greater than 0 kJ/mol, you perform an FBA with enforced thermodynamic feasibility." "\nFor this analysis, ΔG'° values and metabolite concentration " - "ranges have to be given in relevant annotations.\nThe minimal amount of bottlenecks and their IDs " - "to reach the given minimal MDF will be shown in the console afterwards." + "ranges have to be given in relevant annotations.\n" + "As with OptMDFpathway, any reaction(s) currently limiting the " + "reached MDF can afterwards be relaxed iteratively as well." ) self.layout.addWidget(label) - if (analysis_type == ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS) or (analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA): - lineedit_text = QLabel("MDF to reach [in kJ/mol]:") + if analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: + lineedit_text = QLabel("Minimal MDF to enforce [in kJ/mol]:") min_mdf_layout = QHBoxLayout() self.min_mdf = QLineEdit() @@ -114,242 +186,629 @@ def __init__( self.layout.addItem(default_concs_layout) - solver_group = QGroupBox("Solver:") - solver_buttons_layout, self.solver_buttons = get_solver_buttons(appdata) - solver_group.setLayout(solver_buttons_layout) - self.layout.addWidget(solver_group) + # Optional concentration-ratio ranges. These are supplied directly to + # OptMDFAnalysis(concentration_ratios=...) at the outset, so the ratio + # constraints are part of the initial optimisation problem rather than + # an after-the-fact bottleneck relaxation. + ratio_group = QGroupBox("Metabolite concentration ratio ranges (optional)") + ratio_layout = QVBoxLayout() + ratio_layout.addWidget( + QLabel( + "Constrain c(metabolite 1) / c(metabolite 2) to a range. " + "Type the metabolite ID; matching IDs are suggested automatically." + ) + ) + + self._ratio_grid = QGridLayout() + self._ratio_grid.addWidget(QLabel("Metabolite 1"), 0, 0) + self._ratio_grid.addWidget(QLabel("Metabolite 2"), 0, 1) + self._ratio_grid.addWidget(QLabel("Minimum ratio"), 0, 2) + self._ratio_grid.addWidget(QLabel("Maximum ratio"), 0, 3) + self._ratio_grid.addWidget(QLabel(""), 0, 4) + ratio_layout.addLayout(self._ratio_grid) + + ratio_buttons = QHBoxLayout() + self.button_add_ratio = QPushButton("Add ratio range") + self.button_add_ratio.clicked.connect(self._add_ratio_row) + ratio_buttons.addWidget(self.button_add_ratio) + ratio_buttons.addStretch() + ratio_layout.addLayout(ratio_buttons) + ratio_group.setLayout(ratio_layout) + self.layout.addWidget(ratio_group) + self._add_ratio_row() + + if analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: + target_mdf_text = QLabel( + "Target MDF for the optional iterative bottleneck relaxation [in kJ/mol]:" + ) + self.layout.addWidget(target_mdf_text) + target_mdf_layout = QHBoxLayout() + self.target_mdf = QLineEdit() + self.target_mdf.setText("0.0") + target_mdf_layout.addWidget(self.target_mdf) + self.layout.addItem(target_mdf_layout) + + # Current MDF is shown persistently while the dialog remains open. + self.current_mdf_label = QLabel("Current MDF: — kJ/mol") + self.current_mdf_label.setStyleSheet("font-weight: bold;") + self.layout.addWidget(self.current_mdf_label) + + if analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: + self.current_opt_label = QLabel("tFBA optimal value: —") + self.current_opt_label.setStyleSheet("font-weight: bold;") + self.layout.addWidget(self.current_opt_label) + + if analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: + # Keep the bottleneck-selection pane visible: the user can choose + # exactly which member(s) of the current bottleneck are relaxed in the + # next iteration. + bottleneck_group = QGroupBox("Bottleneck reactions to relax in the next round") + bottleneck_layout = QVBoxLayout() + bottleneck_layout.addWidget( + QLabel( + "Check the reaction(s) to relax. A bottleneck can contain multiple " + "reactions, but relaxing all of them is not required." + ) + ) + self.bottleneck_reaction_list = QTableWidget(0, 2) + self.bottleneck_reaction_list.horizontalHeader().setVisible(False) + self.bottleneck_reaction_list.verticalHeader().setVisible(False) + self.bottleneck_reaction_list.setShowGrid(False) + self.bottleneck_reaction_list.setSelectionMode(QAbstractItemView.NoSelection) + self.bottleneck_reaction_list.setEditTriggers(QAbstractItemView.NoEditTriggers) + self.bottleneck_reaction_list.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents) + self.bottleneck_reaction_list.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) + self.bottleneck_reaction_list.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) + #self.bottleneck_reaction_list.itemChanged.connect(lambda _item: self._update_iteration_buttons()) + self.bottleneck_reaction_list.itemClicked.connect(self._bottleneck_item_clicked) + bottleneck_layout.addWidget(self.bottleneck_reaction_list) + bottleneck_buttons = QHBoxLayout() + self.button_select_all_bottlenecks = QPushButton("Select all") + self.button_select_no_bottlenecks = QPushButton("Select none") + self.button_select_all_bottlenecks.clicked.connect(self._select_all_bottleneck_reactions) + self.button_select_no_bottlenecks.clicked.connect(self._select_no_bottleneck_reactions) + bottleneck_buttons.addWidget(self.button_select_all_bottlenecks) + bottleneck_buttons.addWidget(self.button_select_no_bottlenecks) + bottleneck_buttons.addStretch() + bottleneck_layout.addLayout(bottleneck_buttons) + bottleneck_group.setLayout(bottleneck_layout) + self.layout.addWidget(bottleneck_group) l3 = QHBoxLayout() self.button_optmdf = QPushButton("Compute") - self.cancel = QPushButton("Close") l3.addWidget(self.button_optmdf) + if analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: + self.button_relax_once = QPushButton("Relax selected reaction(s) and resolve") + self.button_relax_once.setEnabled(False) + self.button_relax_to_target = QPushButton("Iterate to target MDF") + self.button_relax_to_target.setEnabled(False) + l3.addWidget(self.button_relax_once) + l3.addWidget(self.button_relax_to_target) + self.button_relax_once.clicked.connect(self.relax_bottleneck_once) + self.button_relax_to_target.clicked.connect(self.relax_to_target_mdf) + self.cancel = QPushButton("Close") l3.addWidget(self.cancel) self.layout.addItem(l3) self.setLayout(self.layout) - # Connecting the signal self.cancel.clicked.connect(self.reject) - self.button_optmdf.clicked.connect(self.compute_optmdf) - - def get_solution_from_thread(self, solution: dict[str, float]) -> None: - if not solution[ALL_OK_KEY]: - if TERMINATION_CONDITION_KEY in solution: - match solution[TERMINATION_CONDITION_KEY]: - case 1: - warning_title = "Time limit" - warning_text = "Solver's time limit hit. Please change solver or problem complexity." - case 2: - warning_title = "Iterations limit" - warning_text = "Solver's iterations limit hit. Please change solver or problem complexity." - case 7: - warning_title = "Problem unbounded" - warning_text = "The problem appears to be unbounded, i.e. there is no constraint limiting the objective values." - case 8: - warning_title = "Problem infeasible" - warning_text = "The problem appears to be infeasible, i.e. the constraints make a solution impossible." - case 10: - warning_title = "Solver failure" - warning_text = "Your solver seems to have crashed. Try another solver." - case 11: - warning_title = "Internal solver failure" - warning_text = "Your solver seems to have crashed. Try another solver." - case 15: - warning_title = "License problem" - warning_text = "License problem with the solver! Check out CNApy's documentation for more about how to solve it for CPLEX and Gurobi, or try another solver." - case _: - warning_title = "Solver problem" - warning_text = "The solution process or the solution failed somehow." - QMessageBox.warning( - self, - warning_title, - warning_text, - ) - else: - QMessageBox.warning( - self, - "Computational error", - "Something went wrong (CNApy could not identify the type of error). The computation could not run. Check your problem's constraints or try a different solver.", + self.button_optmdf.clicked.connect(self.compute) + + # ------------------------------------------------------------------ + # small helpers + # ------------------------------------------------------------------ + + def _make_metabolite_completer(self) -> QCompleter: + completer = QCompleter(self.metabolite_ids, self) + completer.setCaseSensitivity(Qt.CaseInsensitive) + completer.setFilterMode(Qt.MatchStartsWith) + completer.setCompletionMode(QCompleter.PopupCompletion) + return completer + + def _add_ratio_row(self) -> None: + row = len(self._ratio_rows) + 1 + met1 = QLineEdit() + met2 = QLineEdit() + met1.setPlaceholderText("metabolite ID") + met2.setPlaceholderText("metabolite ID") + met1.setCompleter(self._make_metabolite_completer()) + met2.setCompleter(self._make_metabolite_completer()) + + ratio_min = QLineEdit() + ratio_min.setPlaceholderText("> 0") + ratio_max = QLineEdit() + ratio_max.setPlaceholderText("> 0") + + remove = QPushButton("Remove") + remove.clicked.connect(lambda _checked=False, widgets=(met1, met2, ratio_min, ratio_max, remove): self._remove_ratio_row(widgets)) + + self._ratio_grid.addWidget(met1, row, 0) + self._ratio_grid.addWidget(met2, row, 1) + self._ratio_grid.addWidget(ratio_min, row, 2) + self._ratio_grid.addWidget(ratio_max, row, 3) + self._ratio_grid.addWidget(remove, row, 4) + self._ratio_rows.append((met1, met2, ratio_min, ratio_max, remove)) + + def _remove_ratio_row(self, widgets) -> None: + try: + idx = self._ratio_rows.index(widgets) + except ValueError: + return + for widget in widgets: + self._ratio_grid.removeWidget(widget) + widget.deleteLater() + self._ratio_rows.pop(idx) + # Rebuild row positions so rows stay compact. + for row, row_widgets in enumerate(self._ratio_rows, start=1): + for col, widget in enumerate(row_widgets): + self._ratio_grid.addWidget(widget, row, col) + + def _get_concentration_ratios(self): + ratios = [] + for row_number, (met1, met2, ratio_min, ratio_max, _remove) in enumerate(self._ratio_rows, start=1): + mi = met1.text().strip() + mj = met2.text().strip() + lo_text = ratio_min.text().strip() + hi_text = ratio_max.text().strip() + if not mi and not mj and not lo_text and not hi_text: + continue + if mi not in self.metabolite_ids or mj not in self.metabolite_ids: + raise ValueError( + f"Ratio row {row_number}: both metabolite IDs must be selected " + "from the model's metabolites." ) + if mi == mj: + raise ValueError(f"Ratio row {row_number}: choose two different metabolites.") + try: + lo = float(lo_text) + hi = float(hi_text) + except ValueError: + raise ValueError(f"Ratio row {row_number}: minimum and maximum ratios must be valid numbers.") from None + if lo <= 0 or hi <= 0: + raise ValueError(f"Ratio row {row_number}: ratio bounds must be positive.") + if lo > hi: + raise ValueError(f"Ratio row {row_number}: minimum ratio cannot exceed maximum ratio.") + ratios.append((mi, mj, lo, hi)) + return ratios + + def _bottleneck_checkbox(self, row: int) -> QCheckBox: + return self.bottleneck_reaction_list.cellWidget(row, 0).findChild(QCheckBox) + + def _selected_bottleneck_reactions(self): + return [ + self.bottleneck_reaction_list.item(row, 1).data(Qt.UserRole) + for row in range(self.bottleneck_reaction_list.rowCount()) + if self._bottleneck_checkbox(row).isChecked() + ] + + def _set_bottleneck_reaction_choices(self, reactions, duals: Optional[dict] = None) -> None: + # `duals` is the {reaction_id: shadow price} dict from + # OptMDFAnalysis.shadow_prices() (result.driving_force_duals), shown + # next to each bottleneck reaction so the user can see, at a glance, + # how much relaxing that reaction's driving-force constraint would + # improve the MDF. Column 0 holds a real QCheckBox widget (rather + # than a checkable QTableWidgetItem) so its on-screen size is just + # the checkbox itself, with no style-dependent item padding around + # it; column 1 holds the reaction's label (whose Qt.UserRole stores + # the raw reaction ID). Keeping the checkbox and the clickable label + # in separate columns means checking/unchecking a reaction for + # relaxation doesn't also jump to it. + old_checked = set(self._selected_bottleneck_reactions()) + reactions = list(reactions or []) + duals = duals or {} + self.bottleneck_reaction_list.blockSignals(True) + try: + self.bottleneck_reaction_list.setRowCount(0) + preserve = bool(old_checked.intersection(reactions)) + for rid in reactions: + row = self.bottleneck_reaction_list.rowCount() + self.bottleneck_reaction_list.insertRow(row) + + checkbox = QCheckBox() + checkbox.setChecked(rid in old_checked if preserve else True) + checkbox.stateChanged.connect(lambda _state: self._update_iteration_buttons()) + container = QWidget() + container_layout = QHBoxLayout(container) + container_layout.addWidget(checkbox) + container_layout.setAlignment(Qt.AlignCenter) + container_layout.setContentsMargins(0, 0, 0, 0) + self.bottleneck_reaction_list.setCellWidget(row, 0, container) + + dual = duals.get(rid) + label = f"{rid} (shadow price: {dual:.4g})" if dual is not None else rid + label_item = QTableWidgetItem(label) + label_item.setData(Qt.UserRole, rid) + label_item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) + try: + reaction = self.appdata.project.cobra_py_model.reactions.get_by_id(rid) + label_item.setToolTip(reaction.build_reaction_string(use_metabolite_names=True)) + except KeyError: + pass + self.bottleneck_reaction_list.setItem(row, 1, label_item) + self.bottleneck_reaction_list.resizeColumnToContents(0) + finally: + self.bottleneck_reaction_list.blockSignals(False) + + @Slot(QTableWidgetItem) + def _bottleneck_item_clicked(self, item: QTableWidgetItem) -> None: + # Only the label column (1) jumps to the reaction; clicking the + # checkbox column (0) only toggles selection for relaxation. + if item.column() != 1: + return + rid = item.data(Qt.UserRole) + if rid: + self.central_widget.jump_to_reaction(rid) + + @Slot() + def _select_all_bottleneck_reactions(self): + for row in range(self.bottleneck_reaction_list.rowCount()): + self._bottleneck_checkbox(row).setChecked(True) + self._update_iteration_buttons() + + @Slot() + def _select_no_bottleneck_reactions(self): + for row in range(self.bottleneck_reaction_list.rowCount()): + self._bottleneck_checkbox(row).setChecked(False) + self._update_iteration_buttons() + + def _update_iteration_buttons(self) -> None: + has_bottleneck = ( + self.analysis is not None + and self.current_result is not None + and self.current_result.status == "optimal" + and bool(self.current_result.bottleneck_reactions) + ) + has_selection = bool(self._selected_bottleneck_reactions()) + self.button_relax_once.setEnabled(has_bottleneck and has_selection) + self.button_relax_to_target.setEnabled(has_bottleneck and has_selection) + + def _status_message(self, status: str): + return self._STATUS_MESSAGES.get( + status, + ( + "Solver problem", + f"The solution process did not reach an optimal solution (solver status: '{status}').", + ), + ) + + def _build_scenario_constraints(self): + # ({reaction_id: coefficient}, constraint_type, rhs) triples -- this + # is exactly what OptMDFAnalysis(scenarios=...) expects, so unlike + # before, no separate constraint dataclass (ExtraLinearConstraint) is + # needed at all. + scenario_constraints = [] + for constraint in self.appdata.project.scen_values.constraints: + # e.g., [({'EDD': 1.0}, '>=', 1.0)] + stoichiometry = {key: value for key, value in constraint[0].items()} + direction = constraint[1] + rhs = constraint[2] + if direction not in ("=", "<=", ">="): + direction = "=" + scenario_constraints.append((stoichiometry, direction, float(rhs))) + return scenario_constraints + + # def _apply_selected_solver(self, model: cobra.Model) -> None: + # solver_name = self.solver_buttons["group"].checkedButton().property("cobrak_name") + # if not solver_name: + # return + # try: + # model.solver = solver_name.lower() + # except Exception: + # QMessageBox.warning( + # self, + # "Solver unavailable", + # f"Could not switch to solver '{solver_name}'; using the model's " + # "current default solver instead.", + # ) + + # ------------------------------------------------------------------ + # computing / displaying a result + # ------------------------------------------------------------------ + + def process_solution(self, result: OptMDFResult) -> None: + if result.status != "optimal": + self.current_mdf_label.setText("Current MDF: — kJ/mol") + if hasattr(self, "current_opt_label"): + self.current_opt_label.setText("tFBA optimal value: —") + warning_title, warning_text = self._status_message(result.status) + QMessageBox.warning(self, warning_title, warning_text) else: - self.set_boxes(solution=solution) + self.set_boxes(result) + self.current_mdf_label.setText(f"Current MDF: {result.mdf:.6g} kJ/mol") + if hasattr(self, "current_opt_label"): + if result.objective_value is not None: + self.current_opt_label.setText( + f"tFBA optimal value: {round(result.objective_value, self.appdata.rounding)}" + ) + else: + self.current_opt_label.setText("tFBA optimal value: —") + if self.analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: + self._set_bottleneck_reaction_choices(result.bottleneck_reactions, result.driving_force_duals) + self._update_iteration_buttons() self.setCursor(Qt.ArrowCursor) - self.accept() + # Deliberately not calling self.accept()/self.reject() here: the + # dialog stays open so the bottleneck-relaxation buttons above stay + # usable. The user closes the dialog explicitly via "Close". @Slot() - def compute_optmdf(self): + def compute(self): self.setCursor(Qt.BusyCursor) - # Decouple models ("with" and "deepcopy" do not work) so that no - # extra COBRA-k annotation spills into the original model - modelstr = cobra.io.to_json(self.appdata.project.cobra_py_model) - model = cobra.io.from_json(modelstr) + self.analysis = None + self.current_result = None + self._relaxed_so_far = set() + self.current_mdf_label.setText("Current MDF: — kJ/mol") + if hasattr(self, "current_opt_label"): + self.current_opt_label.setText("tFBA optimal value: —") + if self.analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: + self._set_bottleneck_reaction_choices([]) + self._update_iteration_buttons() + + # Use the project's own model the same way fva()/compute_fva_result() + # do (see MainWindow.fva() in main_window.py), instead of the old + # to_json/from_json round-trip: that round-trip produced a plain + # cobra.Model, not a CNApyModel, so it lost the stoichiometry/reaction + # hashes that compute_fva_result() relies on to recognize a + # precomputed FVA result and load it from the results cache. The + # "with" block reverts the scenario bounds on exit, so they never + # spill into the original model. + with self.appdata.project.cobra_py_model as model: + self.appdata.project.load_scenario_into_model(model) + + # self._apply_selected_solver(model) - self.appdata.project.load_scenario_into_model(model) + try: + min_default_conc = float(self.min_default_conc.text()) + max_default_conc = float(self.max_default_conc.text()) + if min_default_conc <= 0 or max_default_conc <= 0 or min_default_conc > max_default_conc: + raise ValueError + concentration_ratios = self._get_concentration_ratios() + except ValueError as exc: + message = str(exc) or "Default Cmin/Cmax must be valid positive numbers with Cmin <= Cmax." + QMessageBox.warning( + self, + "Invalid concentration / ratio settings", + message, + ) + self.setCursor(Qt.ArrowCursor) + return - extra_linear_constraints: list[ExtraLinearConstraint] = [] - for constraint in self.appdata.project.scen_values.constraints: - # e.g., [({'EDD': 1.0}, '>=', 1.0)] - extra_linear_constraint = ExtraLinearConstraint( - stoichiometries={key: value for key, value in constraint[0].items()}, - ) + if self.analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: + try: + min_mdf = float(self.min_mdf.text()) + except ValueError: + QMessageBox.warning( + self, + "Invalid minimal OptMDF", + "The given minimal OptMDF could not be converted into a valid number " + "(such as, e.g., 1.231). Aborting calculation...", + ) + self.setCursor(Qt.ArrowCursor) + return + B_bounds = (min_mdf, 1e4) + else: + B_bounds = (-1e4, 1e4) - direction = constraint[1] - rhs = constraint[2] - if direction == ">=": - extra_linear_constraint.lower_value = rhs - elif direction == "<=": - extra_linear_constraint.upper_value = rhs - else: # == "=" - extra_linear_constraint.lower_value = rhs - extra_linear_constraint.upper_value = rhs + try: + concentration_ratios = self._get_concentration_ratios() + except ValueError as exc: + QMessageBox.warning(self, "Invalid concentration ratio", str(exc)) + self.setCursor(Qt.ArrowCursor) + return - extra_linear_constraints.append(extra_linear_constraint) + if not any(rxn.annotation.get("dG0") is not None for rxn in model.reactions): + QMessageBox.warning( + self, + "No ΔG'° set", + "To run a thermodynamic calculation, your model needs at least one " + "reaction with a ΔG'° (annotation 'dG0'). Check out CNApy's " + "documentation for more", + ) + self.setCursor(Qt.ArrowCursor) + return - solver_name = self.solver_buttons["group"].checkedButton().property("cobrak_name") + scenario_constraints = self._build_scenario_constraints() + + # Reuse MainWindow's own FVA computation (incl. its infinite-bound + # clipping and results cache) for OptMDFAnalysis's flux-range + # preprocessing, instead of letting it run its own internal FVA from + # scratch. Falls back to OptMDFAnalysis's internal FVA (its default + # behaviour) if no main_window was given to this dialog, or if the + # computation didn't succeed (e.g. infeasible scenario -- already + # reported to the user via a message box inside compute_fva_result). + precomputed_fva = self.main_window.compute_fva_result(model, scenario_constraints) + if precomputed_fva is None: + # Infeasible scenario or solver error -- compute_fva_result() + # already showed the relevant message box, and OptMDFAnalysis + # would only hit the same failure again internally. + self.setCursor(Qt.ArrowCursor) + return - if self.analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: - min_mdf = -float("inf") - else: try: - min_mdf = float(self.min_mdf.text()) - except ValueError: + self.analysis = OptMDFAnalysis( + model, + Cmin=min_default_conc, + Cmax=max_default_conc, + scenarios=scenario_constraints, + concentration_ratios=concentration_ratios, + B_bounds=B_bounds, + precomputed_fva=precomputed_fva, + verbose=True, + ) + except Exception as e: QMessageBox.warning( self, - "Invalid minimal OptMDF", - "The given minimal OptMDF could not be converted into a valid number (such as, e.g., 1.231)." - "Aborting calculation...", + "Setup error", + f"Could not set up the thermodynamic analysis:\n{e}", ) + self.analysis = None + self.setCursor(Qt.ArrowCursor) + self._update_iteration_buttons() return - match self.analysis_type: - case ThermodynamicAnalysisTypes.OPTMDFPATHWAY: - objective = {MDF_VAR_ID: 1} - direction = +1 - case ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - objective = {"bottleneck_z_sum": 1} - direction = -1 - case ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: - objective = {} - for ( - reaction, - coefficient, - ) in cobra.util.solver.linear_reaction_coefficients(model).items(): - if reaction.reversibility: - objective[reaction.id + self.FWDID] = coefficient - objective[reaction.id + self.REVID] = -coefficient - else: - objective[reaction.id] = coefficient - direction = +1 - - cobrak_model = load_annotated_cobrapy_model_as_cobrak_model( - get_fullsplit_cobra_model( - model, - fwd_suffix=self.FWDID, - rev_suffix=self.REVID, - add_cobrak_sbml_annotation=True, - cobrak_default_min_conc=float(self.min_default_conc.text()), - cobrak_default_max_conc=float(self.max_default_conc.text()), - cobrak_extra_linear_constraints=extra_linear_constraints, - cobrak_kinetic_ignored_metabolites=[], - cobrak_no_extra_versions=True, - reac_lb_ub_cap=1_000.0, + # OPTMDFPATHWAY maximises the MDF itself (analysis.solve()); + # THERMODYNAMIC_FBA optimises the model's own objective subject to + # the enforced minimal MDF from B_bounds (analysis.solve_fba()). In + # both cases the same analysis object is reused for every later + # relax-and-resolve step below. + self._solve = ( + self.analysis.solve_fba + if self.analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA + else self.analysis.solve ) - ) - if all(cobrak_model.reactions[reac_id].dG0 is None for reac_id in cobrak_model.reactions): - QMessageBox.warning( + result = self._solve() + if result.status == "optimal": + if self.analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: + result.driving_force_duals = self.analysis.shadow_prices() + result.bottleneck_reactions = self.analysis.find_bottleneck() + self.current_result = result + + self.process_solution(result) + + # ------------------------------------------------------------------ + # iterative bottleneck relaxation (see optmdfpathway.py's __main__ demo, + # i.e. the OptMDFAnalysis.step()/.run() pattern: find the current + # bottleneck, relax() exactly it, resolve, repeat) + # ------------------------------------------------------------------ + + @Slot() + def relax_bottleneck_once(self): + if self.analysis is None or self.current_result is None: + return + bottleneck = self.current_result.bottleneck_reactions + if not bottleneck: + QMessageBox.information( self, - "No ΔG'° set", - "To run a thermodynamic calculation, your model needs at least one reaction with a ΔG'° (annotation 'dG0')." - "Check out CNApy's documentation for more", + "No bottleneck", + "The current MDF is not limited by any reaction's thermodynamics; " + "there is nothing left to relax.", ) - self.setCursor(Qt.ArrowCursor) return - if self.analysis_type in (ThermodynamicAnalysisTypes.OPTMDFPATHWAY, ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA): - solution = perform_lp_optimization( - cobrak_model=cobrak_model, - objective_target=objective, - objective_sense=direction, - with_enzyme_constraints=False, - with_thermodynamic_constraints=True, - with_loop_constraints=False, - min_mdf=min_mdf, - solver=Solver(name=solver_name), - verbose=True, + + selected = self._selected_bottleneck_reactions() + if not selected: + QMessageBox.information( + self, + "No reaction selected", + "Select at least one bottleneck reaction to relax.", ) - elif self.analysis_type == ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - bottlenecks, solution = perform_lp_thermodynamic_bottleneck_analysis( - cobrak_model=cobrak_model, - with_enzyme_constraints=False, - min_mdf=min_mdf, - solver=Solver(name=solver_name), - verbose=True, + return + + self.setCursor(Qt.BusyCursor) + self.analysis.relax(selected) + self._relaxed_so_far.update(selected) + + result = self._solve() + if result.status == "optimal": + result.driving_force_duals = self.analysis.shadow_prices() + result.bottleneck_reactions = self.analysis.find_bottleneck() + self.current_result = result + + self.process_solution(result) + + @Slot() + def relax_to_target_mdf(self): + if self.analysis is None or self.current_result is None: + return + try: + target_mdf = float(self.target_mdf.text()) + except ValueError: + QMessageBox.warning( + self, + "Invalid target MDF", + "The given target MDF could not be converted into a valid number " + "(such as, e.g., 0.0).", ) - if solution != {}: - solution[ALL_OK_KEY] = True - self.get_solution_from_thread(solution) - # except Exception: - # self.send_solution.emit(pickle.dumps("ERROR")) - # self.finished_computation.emit() - - def set_boxes(self, solution: dict[str, float]): - # Combine FWD and REV flux solutions - combined_solution = {} - for var_id in solution.keys(): - if var_id.endswith(self.FWDID): - key = var_id.replace(self.FWDID, "") - multiplier = 1.0 - elif var_id.endswith(self.REVID): - key = var_id.replace(self.REVID, "") - multiplier = -1.0 - else: - key = var_id - multiplier = 1.0 - if key not in combined_solution.keys(): - combined_solution[key] = 0.0 - combined_solution[key] += multiplier * solution[var_id] + return - # write results into comp_values - for search_key in self.reac_ids: - if search_key in combined_solution.keys(): - self.appdata.project.comp_values[search_key] = ( - float(combined_solution[search_key]), - float(combined_solution[search_key]), - ) - if DF_VAR_PREFIX + search_key in combined_solution.keys(): - rounded_df = round( - combined_solution[DF_VAR_PREFIX + search_key], self.appdata.rounding + self.setCursor(Qt.BusyCursor) + result = self.current_result + max_iterations = 50 # safety cap against pathological relax loops + n = 0 + while n < max_iterations: + bottleneck = result.bottleneck_reactions + if not bottleneck or (result.mdf is not None and result.mdf >= target_mdf): + break + selected = self._selected_bottleneck_reactions() + if not selected: + QMessageBox.information( + self, + "Select bottleneck reaction(s)", + "Iteration to the target stopped because no reaction is selected. " + "Choose one or more reactions in the bottleneck list and run " + "the operation again.", ) + break + self.analysis.relax(selected) + self._relaxed_so_far.update(selected) + result = self._solve() + if result.status != "optimal": + break + result.driving_force_duals = self.analysis.shadow_prices() + result.bottleneck_reactions = self.analysis.find_bottleneck() + self._set_bottleneck_reaction_choices(result.bottleneck_reactions, result.driving_force_duals) + n += 1 + # Do not silently carry the previous selection into a newly found + # bottleneck; the user should explicitly choose the next relaxation. + if result.bottleneck_reactions and result.mdf is not None and result.mdf < target_mdf: + break + self.current_result = result + + self.process_solution(result) + + # ------------------------------------------------------------------ + # writing a result into the CNApy project / console + # ------------------------------------------------------------------ + + def set_boxes(self, result: OptMDFResult): + # write flux and driving-force results into comp_values / df_values + for search_key in self.reac_ids: + if search_key in result.fluxes: + flux = float(result.fluxes[search_key]) + self.appdata.project.comp_values[search_key] = (flux, flux) + if search_key in result.driving_forces: + rounded_df = round(result.driving_forces[search_key], self.appdata.rounding) self.appdata.project.df_values[search_key] = rounded_df - # Write metabolite concentrations + # write metabolite concentrations (OptMDFResult already reports + # these as actual concentrations in M, not log-concentrations) for metabolite_id in self.metabolite_ids: - var_id = f"{LNCONC_VAR_PREFIX}{metabolite_id}" - if var_id in solution.keys(): - rounded_conc = round(exp(solution[var_id]), 9) - self.appdata.project.conc_values[var_id[len(LNCONC_VAR_PREFIX):]] = rounded_conc + if metabolite_id in result.concentrations: + rounded_conc = round(result.concentrations[metabolite_id], 9) + self.appdata.project.conc_values[metabolite_id] = rounded_conc # Show selected reaction-dependent values self.appdata.project.comp_values_type = 0 self.central_widget.update() - # Show OptMDF - console_text = "print('\\n" - if self.analysis_type == ThermodynamicAnalysisTypes.OPTMDFPATHWAY: - console_text += f"OptMDF: {solution[MDF_VAR_ID]} kJ/mol" - elif self.analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: - console_text += f"Reached objective value: {solution[OBJECTIVE_VAR_NAME]}" - console_text += f"\\nReached MDF @ optimum of objective: {solution[MDF_VAR_ID]} kJ/mol" - elif self.analysis_type == ThermodynamicAnalysisTypes.BOTTLENECK_ANALYSIS: - combined_bottleneck_sum = 0 - for key in combined_solution.keys(): - if key.startswith("zb_var_"): - if combined_solution[key] > 0.1: - console_text += f"\\n* {key.replace('zb_var_', '')}" - combined_bottleneck_sum += 1 - console_text += f"\\n↳Total number of thermodynamically deactivated bottlenecks to reach minimal MDF: {combined_bottleneck_sum}" - console_text += "')" - self.central_widget.kernel_client.execute(console_text) + # Show OptMDF / objective, and the current thermodynamic bottleneck + lines = [] + if self.analysis_type == ThermodynamicAnalysisTypes.THERMODYNAMIC_FBA: + lines.append(f"Reached objective value: {result.objective_value}") + lines.append(f"Reached MDF @ optimum of objective: {result.mdf} kJ/mol") + else: + lines.append(f"OptMDF: {result.mdf} kJ/mol") + if result.bottleneck_reactions: + lines.append("Thermodynamic bottleneck reaction(s) currently limiting the MDF:") + for rid in result.bottleneck_reactions: + lines.append(f"* {rid}") + lines.append( + "Use 'Relax bottleneck and resolve' or 'Iterate to target MDF' " + "to relax them and see how the MDF improves." + ) + else: + lines.append( + "No thermodynamic bottleneck currently limits the MDF further." + ) + + if self._relaxed_so_far: + lines.append( + f"Reaction(s) relaxed so far this session: {sorted(self._relaxed_so_far)}" + ) + + console_text = "\n".join(lines) + self.central_widget.console._append_plain_text(console_text, before_prompt=True) self.central_widget.show_bottom_of_console() diff --git a/cnapy/gui_elements/yield_optimization_dialog.py b/cnapy/gui_elements/yield_optimization_dialog.py index c023f1b2..27b184f4 100644 --- a/cnapy/gui_elements/yield_optimization_dialog.py +++ b/cnapy/gui_elements/yield_optimization_dialog.py @@ -1,7 +1,6 @@ """The cnapy yield optimization dialog""" from random import randint from numpy import isnan, isinf -import re from qtpy.QtCore import Qt, Signal, Slot from qtpy.QtWidgets import (QDialog, QHBoxLayout, QLabel, QComboBox, QMessageBox, QPushButton, QVBoxLayout, QFrame) @@ -9,8 +8,8 @@ from cnapy.appdata import AppData from cnapy.gui_elements.central_widget import CentralWidget from cnapy.utils import QComplReceivLineEdit, QHSeperationLine -from straindesign import yopt, linexpr2dict, linexprdict2str, avail_solvers -from straindesign.names import * +from straindesign import linexpr2dict, linexprdict2str +from cnapy.core import highs_yopt class YieldOptimizationDialog(QDialog): """A dialog to perform yield optimization""" @@ -55,7 +54,7 @@ def __init__(self, appdata: AppData, central_widget: CentralWidget): self.denominator.setPlaceholderText('denominator (e.g. 1.0 '+r2+')') num_den_layout.addWidget(self.numerator) sep = QHSeperationLine() - sep.setFrameShadow(QFrame.Plain) + sep.setFrameShadow(QFrame.Shadow.Plain) sep.setLineWidth(2) num_den_layout.addWidget(sep) num_den_layout.addWidget(self.denominator) @@ -87,57 +86,55 @@ def validate_dialog(self,b=True): def compute(self): self.setCursor(Qt.BusyCursor) - if self.sense_combo.currentText() == 'maximize': - sense = 'Maximum' - else: - sense = 'Minimum' + obj_sense = self.sense_combo.currentText() # 'maximize' or 'minimize' + sense_label = 'Maximum' if obj_sense == 'maximize' else 'Minimum' with self.appdata.project.cobra_py_model as model: self.appdata.project.load_scenario_into_model(model) - solver = re.search('('+'|'.join(avail_solvers)+')',model.solver.interface.__name__) - if solver is not None: - solver = solver[0] - sol = yopt(model, - obj_num=self.numerator.text(), - obj_den=self.denominator.text(), - obj_sense=self.sense_combo.currentText(), - solver=solver) - if sol.status == UNBOUNDED and isinf(sol.objective_value): + obj_num = linexpr2dict(self.numerator.text(), self.reac_ids) + obj_den = linexpr2dict(self.denominator.text(), self.reac_ids) + # scen_values.constraints is already a list of (dict, str, float) + # triples -- exactly what highs_yopt/build_highs_fba_model expect. + constraints = list(self.appdata.project.scen_values.constraints) + + sol = highs_yopt(model, obj_num=obj_num, obj_den=obj_den, + obj_sense=obj_sense, constraints=constraints) + + if sol.status == 'unbounded' and isinf(sol.objective_value): self.set_boxes(sol) - QMessageBox.warning(self, sense+' yield is unbounded. ', + QMessageBox.warning(self, sense_label+' yield is unbounded. ', 'Yield unbounded. \n'+\ 'Parts of the shown example flux distribution can be scaled indefinitely. The numerator "'+\ - linexprdict2str(linexpr2dict(self.numerator.text(),self.reac_ids))+'" is unbounded.',) - elif sol.status == UNBOUNDED and isnan(sol.objective_value): + linexprdict2str(obj_num)+'" is unbounded.',) + elif sol.status == 'unbounded' and isnan(sol.objective_value): self.set_boxes(sol) - QMessageBox.warning(self, sense+' yield is undefined. ', + QMessageBox.warning(self, sense_label+' yield is undefined. ', 'Yield undefined. \n'+\ 'The denominator "'+\ - linexprdict2str(linexpr2dict(self.denominator.text(),self.reac_ids))+\ + linexprdict2str(obj_den)+\ '" can take the value 0, as shown in the example flux distibution.',) - elif sol.status == OPTIMAL: + elif sol.status == 'optimal': self.set_boxes(sol) if sol.scalable: txt_scalable = '\nThe shown example flux distribution can be scaled indefinitely.' else: txt_scalable = '' - QMessageBox.information(self, 'Solution', - 'Maximum yield ('+linexprdict2str(linexpr2dict(self.numerator.text(),self.reac_ids))+\ - ') / ('+linexprdict2str(linexpr2dict(self.denominator.text(),self.reac_ids))+\ - '): '+str(round(sol.objective_value,9)) + \ + display_text = sense_label+' yield ('+linexprdict2str(obj_num)+\ + ') / ('+linexprdict2str(obj_den)+\ + '): '+str(round(sol.objective_value,9)) + self.central_widget.console._append_plain_text("\n"+display_text, before_prompt=True) + QMessageBox.information(self, 'Solution', display_text + \ '\nShowing yield-optimal example flux distribution.' + txt_scalable) else: QMessageBox.warning(self, 'Problem infeasible.', 'The scenario seems to be infeasible.',) return - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) self.accept() - def set_boxes(self,sol): + def set_boxes(self, sol): # write results into comp_values - idx = 0 for r in self.reac_ids: - self.appdata.project.comp_values[r] = ( - float(sol.fluxes[r]), float(sol.fluxes[r])) - idx = idx+1 + flux = sol.fluxes.get(r, 0.0) + self.appdata.project.comp_values[r] = (float(flux), float(flux)) self.appdata.project.comp_values_type = 0 - self.central_widget.update() \ No newline at end of file + self.central_widget.update() diff --git a/cnapy/gui_elements/yield_space_dialog.py b/cnapy/gui_elements/yield_space_dialog.py index ec2cfc77..c94bdfd1 100644 --- a/cnapy/gui_elements/yield_space_dialog.py +++ b/cnapy/gui_elements/yield_space_dialog.py @@ -49,7 +49,7 @@ def __init__(self, appdata): self.x_denominator.setPlaceholderText('denominator (e.g. 1.0 '+r2+')') x_num_den_layout.addWidget(self.x_numerator) sep = QHSeperationLine() - sep.setFrameShadow(QFrame.Plain) + sep.setFrameShadow(QFrame.Shadow.Plain) sep.setLineWidth(3) x_num_den_layout.addWidget(sep) x_num_den_layout.addWidget(self.x_denominator) @@ -64,7 +64,7 @@ def __init__(self, appdata): self.y_denominator.setPlaceholderText('denominator (e.g. '+r4+')') y_num_den_layout.addWidget(self.y_numerator) sep = QHSeperationLine() - sep.setFrameShadow(QFrame.Plain) + sep.setFrameShadow(QFrame.Shadow.Plain) sep.setLineWidth(3) y_num_den_layout.addWidget(sep) y_num_den_layout.addWidget(self.y_denominator) @@ -85,7 +85,7 @@ def __init__(self, appdata): self.button.clicked.connect(self.compute) def compute(self): - self.setCursor(Qt.BusyCursor) + self.setCursor(Qt.CursorShape.BusyCursor) with self.appdata.project.cobra_py_model as model: self.appdata.project.load_scenario_into_model(model) solver = re.search('('+'|'.join(avail_solvers)+')',model.solver.interface.__name__) @@ -140,4 +140,4 @@ def compute(self): self.appdata.window.centralWidget().show_bottom_of_console() - self.setCursor(Qt.ArrowCursor) + self.setCursor(Qt.CursorShape.ArrowCursor) diff --git a/cnapy/highs_lp_cache.py b/cnapy/highs_lp_cache.py new file mode 100644 index 00000000..4175d2ae --- /dev/null +++ b/cnapy/highs_lp_cache.py @@ -0,0 +1,712 @@ +"""cnapy/highs_lp_cache.py (prototype) + +Build-once, incrementally-updated HiGHS LP wrappers for FVA-style repeated +optimization over a *fixed* feasible region -- plain rate objectives, and +Charnes-Cooper yield (linear-fractional) objectives -- meant to be handed +out to worker threads the same way core.multi_threaded_HiGHS_FVA already +hands out its `lp_data` HighsLp to GraphAwareFVAWorker. + +No dependency on straindesign. cobra is only needed by the two +`build_*_lp(cobra_model, ...)` convenience wrappers; the array-level +builders (`_build_rate_lp_arrays` / `_build_yopt_lp_arrays`) take a +stoichiometric matrix + bound arrays directly and have no cobra +dependency at all, which is also what makes them unit-testable without a +real cobra.Model (see test_highs_lp_cache.py). + +Design +------ +Each HighsLp is constructed exactly once. Every worker thread then does + + solver = HighsRateSolver(lp, reaction_ids, rxn_index, fix_slot_rows) # or HighsYieldSolver + +which calls `Highs().passModel(lp)` -- HiGHS copies the model into its own +internal state at that point, so the same `lp` object can safely be +handed to multiple threads' solvers (mirrors multi_threaded_HiGHS_FVA's +`lp_data` sharing). From then on a solver only ever touches its own +Highs() instance via the incremental API: + + - optimize(obj, sense[, denominator_sign]) -> changeColCost (+ one + changeRowBounds for the yield case) + run(). No Python-side matrix + ever gets rebuilt. + - set_fix_row(slot, coeffs, value) -> used for plot_flux_space's + "fix axis i to this value" pattern (boundary bisection, y-slicing). + `num_fix_slots` reserved rows are placeholders (inert: free bounds / + all-zero row) until first used. As long as a given slot keeps being + aimed at the *same* linear expression across a trace (true for + plot_flux_space: the axis identity is constant through one adaptive + refinement or slice scan, only the target value changes), every call + after the first is a single changeRowBounds (rate) or changeCoeff + (yield, homogeneous in the LFP scaling variable t) call -- no + Python-side sparse-matrix construction, no re-running + create_stoichiometric_matrix. + - clear_fix_row(slot) -> deactivate a slot again. + +This is the piece that's missing from core.py's existing +_highs_fba_optimize / build_highs_yopt_model / highs_yopt: those rebuild +a fresh HighsLp (including create_stoichiometric_matrix and a fresh +scipy.sparse matrix) on *every* call, which is fine for FVA's one-shot +sign-check calls but would dominate wall-clock (and be entirely +GIL-bound, i.e. not helped by threading at all) if used as the inner +loop of an adaptive boundary trace that issues dozens to hundreds of +solves per axis. + +NOT thread-safe to share one HighsRateSolver / HighsYieldSolver instance +across threads -- build one per worker thread from the same read-only +HighsLp, exactly like GraphAwareFVAWorker builds its own `h` from the +shared `lp_data`. +""" + +import concurrent.futures as cf +import math +import threading as th +from typing import Dict, Iterable, List, Optional, Tuple + +import numpy +import scipy.sparse as sp + + +# --------------------------------------------------------------------------- +# HiGHS thread scheduler handling +# --------------------------------------------------------------------------- +# HiGHS keeps one task scheduler per OS thread. The first run() on a thread +# creates it, sized by that run's `threads` option (default 0 = half the +# cores), and it is never resized: later runs on the same thread must use +# threads=0 or exactly that size, otherwise run() returns kError. A thread +# whose scheduler was created with threads=1 therefore also stays +# single-threaded for everything that runs there later (e.g. a MILP). +# +# So solvers built here only pin threads=1 on threads started by +# worker_pool() (fresh, so nothing else has touched their scheduler, and +# all parallelism comes from the pool). Anywhere else they keep the default +# and fit whatever scheduler the calling thread already has. + +_thread_state = th.local() + + +def _mark_worker_thread(): + _thread_state.is_worker = True + + +def worker_pool(n_workers: int) -> cf.ThreadPoolExecutor: + """ThreadPoolExecutor whose threads build their HiGHS solvers with + threads=1 (see the comment above).""" + return cf.ThreadPoolExecutor(max_workers=n_workers, initializer=_mark_worker_thread) + + +def _configure_highs(h): + """Silent logging, and threads=1 on worker_pool threads only. Must be + called before passModel() so that no log output is produced.""" + h.setOptionValue("output_flag", False) + h.setOptionValue("log_to_console", False) + if getattr(_thread_state, "is_worker", False): + h.setOptionValue("threads", 1) + + +def _run_checked(h): + """h.run(), raising on a failed run: after a kError run (e.g. a threads + mismatch, see above) a reused instance still reports the model status + of its previous solve, which would otherwise be read as a valid result.""" + import highspy + status = h.run() + if status not in (highspy.HighsStatus.kOk, highspy.HighsStatus.kWarning): + raise RuntimeError(f"HiGHS run() failed with {status} " + f"(model status: {h.modelStatusToString(h.getModelStatus())})") + + +# --------------------------------------------------------------------------- +# Rate (plain FBA-style) model +# --------------------------------------------------------------------------- + +def _build_rate_lp_arrays(S: sp.spmatrix, col_lower, col_upper, + extra_rows: Iterable[Tuple[Dict[int, float], float, float]] = (), + num_fix_slots: int = 0): + """Array-level builder, no cobra dependency. + + Args: + S: stoichiometric matrix, shape (n_mets, n_rxns). + col_lower, col_upper: per-reaction bounds, length n_rxns. + extra_rows: iterable of (coeffs_by_col_idx, row_lower, row_upper) + for caller-supplied constraints already resolved to column + indices (mirrors core.build_highs_fba_model's `constraints`, + just pre-resolved so this function stays cobra-free). NOTE: + this is (coeffs, lower, upper) -- different from + _build_yopt_lp_arrays's extra_rows, which takes + (coeffs, ctype_str, rhs) instead, matching the two + functions' different callers (build_rate_lp resolves ctype + strings itself; build_yopt_lp passes them straight through). + num_fix_slots: number of extra placeholder rows to reserve for + later reuse via HighsRateSolver.set_fix_row(). + + Returns: + (HighsLp, fix_slot_rows) + """ + import highspy + + n_mets, n_rxns = S.shape + extra_rows = list(extra_rows) + n_extra = len(extra_rows) + n_rows = n_mets + n_extra + num_fix_slots + + S = sp.lil_matrix(S) + S.resize((n_rows, n_rxns)) + + row_lower = numpy.zeros(n_rows, dtype=numpy.double) + row_upper = numpy.zeros(n_rows, dtype=numpy.double) + + row = n_mets + for coeffs, lo, hi in extra_rows: + for col, c in coeffs.items(): + S[row, col] = c + row_lower[row], row_upper[row] = lo, hi + row += 1 + + fix_slot_rows = list(range(row, row + num_fix_slots)) + for r in fix_slot_rows: + row_lower[r], row_upper[r] = -math.inf, math.inf # inert until set_fix_row() + + lp = highspy.HighsLp() + lp.a_matrix_.format_ = highspy.MatrixFormat.kRowwise + lp.num_col_ = n_rxns + lp.num_row_ = n_rows + lp.col_cost_ = numpy.zeros(n_rxns, dtype=numpy.double) + lp.col_lower_ = numpy.asarray(col_lower, dtype=numpy.double) + lp.col_upper_ = numpy.asarray(col_upper, dtype=numpy.double) + lp.row_lower_ = row_lower + lp.row_upper_ = row_upper + Scsr = S.tocsr() + lp.a_matrix_.start_ = Scsr.indptr.astype(numpy.int32) + lp.a_matrix_.index_ = Scsr.indices.astype(numpy.int32) + lp.a_matrix_.value_ = Scsr.data.astype(numpy.double) + lp.sense_ = highspy.ObjSense.kMinimize # optimize() sets the real sense per call + return lp, fix_slot_rows + + +def build_rate_lp(cobra_model, constraints=None, num_fix_slots: int = 0): + """cobra.Model -> (HighsLp, reaction_ids, rxn_index, fix_slot_rows). + + `constraints`: same [dict, '<='/'='/'>=', rhs] triples + core.build_highs_fba_model accepts. + """ + from cobra.util.array import create_stoichiometric_matrix + reaction_ids = cobra_model.reactions.list_attr("id") + rxn_index = {rid: i for i, rid in enumerate(reaction_ids)} + col_lower = [r.lower_bound for r in cobra_model.reactions] + col_upper = [r.upper_bound for r in cobra_model.reactions] + S = create_stoichiometric_matrix(cobra_model, array_type="lil") + + extra_rows = [] + for expr, ctype, rhs in (constraints or []): + coeffs = {rxn_index[rid]: c for rid, c in expr.items() if rid in rxn_index} + if ctype == '=': + extra_rows.append((coeffs, rhs, rhs)) + elif ctype == '<=': + extra_rows.append((coeffs, -math.inf, rhs)) + elif ctype == '>=': + extra_rows.append((coeffs, rhs, math.inf)) + else: + raise ValueError(f"Unknown constraint type {ctype!r}") + + lp, fix_slot_rows = _build_rate_lp_arrays(S, col_lower, col_upper, extra_rows, num_fix_slots) + return lp, reaction_ids, rxn_index, fix_slot_rows + + +class HighsRateSolver: + """One thread's private solver for a build-once rate LP.""" + + def __init__(self, lp, reaction_ids: List[str], rxn_index: Dict[str, int], + fix_slot_rows: List[int]): + import highspy + self._highspy = highspy + self.reaction_ids = reaction_ids + self.rxn_index = rxn_index + self.fix_slot_rows = fix_slot_rows + self.h = highspy.Highs() + # threads=1 only on worker_pool threads, see "HiGHS thread scheduler + # handling" at the top of this module + _configure_highs(self.h) + self.h.passModel(lp) + self._active_cost_cols: set = set() + self._fix_support: Dict[int, Dict[int, float]] = {} + + def optimize(self, obj: Dict[str, float], sense: str): + """Set the objective to `obj` (all other columns zeroed), run, + return (status, value, fluxes_or_None).""" + h, hp = self.h, self._highspy + for c in self._active_cost_cols: + h.changeColCost(c, 0.0) + self._active_cost_cols = set() + for rid, coeff in obj.items(): + c = self.rxn_index[rid] + h.changeColCost(c, coeff) + self._active_cost_cols.add(c) + h.changeObjectiveSense(hp.ObjSense.kMaximize if sense == 'maximize' else hp.ObjSense.kMinimize) + _run_checked(h) + status = h.getModelStatus() + if status == hp.HighsModelStatus.kOptimal: + x = numpy.asarray(h.getSolution().col_value) + return "optimal", h.getInfo().objective_function_value, dict(zip(self.reaction_ids, x)) + elif status == hp.HighsModelStatus.kUnbounded: + return "unbounded", (math.inf if sense == 'maximize' else -math.inf), None + else: + return "infeasible", float('nan'), None + + def set_fix_row(self, slot: int, coeffs: Dict[str, float], rhs: float): + """Activate/update fix-row `slot` to `sum(coeffs[r]*v_r) = rhs`. + Reusing the same `coeffs` across calls (only `rhs` varying, as in + bisection refinement or a slice scan) costs one changeRowBounds + call; only the first call for a given `coeffs` pays for the + (small, sparse) changeCoeff calls.""" + row = self.fix_slot_rows[slot] + new_support = {self.rxn_index[rid]: c for rid, c in coeffs.items()} + old_support = self._fix_support.get(slot, {}) + if new_support != old_support: + for col in old_support.keys() - new_support.keys(): + self.h.changeCoeff(row, col, 0.0) + for col, c in new_support.items(): + if old_support.get(col) != c: + self.h.changeCoeff(row, col, c) + self._fix_support[slot] = new_support + self.h.changeRowBounds(row, rhs, rhs) + + def clear_fix_row(self, slot: int): + self.h.changeRowBounds(self.fix_slot_rows[slot], -math.inf, math.inf) + + +# --------------------------------------------------------------------------- +# Yield (Charnes-Cooper LFP) model +# --------------------------------------------------------------------------- + +def _build_yopt_lp_arrays(S: sp.spmatrix, col_lower, col_upper, den_coeffs: Dict[int, float], + extra_rows: Iterable[Tuple[Dict[int, float], str, float]] = (), + num_fix_slots: int = 0): + """Charnes-Cooper LFP structure, build-once variant of + core.build_highs_yopt_model (see that function's docstring for the + full derivation). Variables are [v_1..v_n, t] with t the last column. + + `den_coeffs` (column index -> coefficient) is the denominator + expression; it's baked into a dedicated row here, but that row's RHS + (the +-1 sign) is left as a placeholder and is set per-call by + HighsYieldSolver.optimize(), since the same structure is reused for + both achievable signs and for many different numerators. + + `extra_rows` are caller constraints already resolved to column + indices, as (coeffs_by_col, ctype, rhs) -- homogenized in t here so + the caller doesn't need to know about the LFP transformation. NOTE: + this is (coeffs, ctype_str, rhs) -- different from + _build_rate_lp_arrays's extra_rows, which takes + (coeffs, lower, upper) instead; see that function's docstring. + + Returns: + (HighsLp, den_row, t_col, fix_slot_rows) + """ + import highspy + n_mets, n = S.shape + lb_arr = numpy.asarray(col_lower, dtype=numpy.double) + ub_arr = numpy.asarray(col_upper, dtype=numpy.double) + real_lb = numpy.where(numpy.isfinite(lb_arr))[0] + real_ub = numpy.where(numpy.isfinite(ub_arr))[0] + + extra_rows = list(extra_rows) + n_extra = len(extra_rows) + n_rows = n_mets + n_extra + len(real_lb) + len(real_ub) + 1 + num_fix_slots + n_vars = n + 1 + t_col = n + + A = sp.lil_matrix((n_rows, n_vars)) + A[:n_mets, :n] = S + row_lower = numpy.zeros(n_rows) + row_upper = numpy.zeros(n_rows) + + row = n_mets + for coeffs, ctype, rhs in extra_rows: + for col, c in coeffs.items(): + A[row, col] = c + A[row, t_col] = -float(rhs) + if ctype == '=': + row_lower[row] = row_upper[row] = 0.0 + elif ctype == '<=': + row_lower[row], row_upper[row] = -math.inf, 0.0 + elif ctype == '>=': + row_lower[row], row_upper[row] = 0.0, math.inf + else: + raise ValueError(f"Unknown constraint type {ctype!r}") + row += 1 + + for i in real_lb: + A[row, i] = 1.0 + A[row, t_col] = -lb_arr[i] + row_lower[row], row_upper[row] = 0.0, math.inf + row += 1 + for i in real_ub: + A[row, i] = 1.0 + A[row, t_col] = -ub_arr[i] + row_lower[row], row_upper[row] = -math.inf, 0.0 + row += 1 + + den_row = row + for col, c in den_coeffs.items(): + A[den_row, col] = c + row_lower[den_row] = row_upper[den_row] = 1.0 # placeholder, overwritten by optimize() + row += 1 + + fix_slot_rows = list(range(row, row + num_fix_slots)) + for r in fix_slot_rows: + row_lower[r] = row_upper[r] = 0.0 # homogeneous eq.; all-zero row -> "0=0", inert + + col_lower_full = numpy.full(n_vars, -math.inf) + col_upper_full = numpy.full(n_vars, math.inf) + col_lower_full[t_col], col_upper_full[t_col] = 0.0, math.inf # t >= 0 + # v columns stay free here -- their real bounds are enforced via the + # real_lb/real_ub homogeneous rows above, exactly as in + # core.build_highs_yopt_model. + + lp = highspy.HighsLp() + lp.a_matrix_.format_ = highspy.MatrixFormat.kRowwise + lp.num_col_ = n_vars + lp.num_row_ = n_rows + lp.col_cost_ = numpy.zeros(n_vars) + lp.col_lower_ = col_lower_full + lp.col_upper_ = col_upper_full + lp.row_lower_ = row_lower + lp.row_upper_ = row_upper + Acsr = A.tocsr() + lp.a_matrix_.start_ = Acsr.indptr.astype(numpy.int32) + lp.a_matrix_.index_ = Acsr.indices.astype(numpy.int32) + lp.a_matrix_.value_ = Acsr.data.astype(numpy.double) + lp.sense_ = highspy.ObjSense.kMinimize + return lp, den_row, t_col, fix_slot_rows + + +def build_yopt_lp(cobra_model, obj_den: Dict[str, float], constraints=None, num_fix_slots: int = 0): + """cobra.Model -> (HighsLp, reaction_ids, rxn_index, den_row, t_col, fix_slot_rows).""" + from cobra.util.array import create_stoichiometric_matrix + reaction_ids = cobra_model.reactions.list_attr("id") + rxn_index = {rid: i for i, rid in enumerate(reaction_ids)} + col_lower = [r.lower_bound for r in cobra_model.reactions] + col_upper = [r.upper_bound for r in cobra_model.reactions] + S = create_stoichiometric_matrix(cobra_model, array_type="lil") + den_coeffs = {rxn_index[rid]: c for rid, c in obj_den.items() if rid in rxn_index} + + extra_rows = [] + for expr, ctype, rhs in (constraints or []): + coeffs = {rxn_index[rid]: c for rid, c in expr.items() if rid in rxn_index} + extra_rows.append((coeffs, ctype, rhs)) + + lp, den_row, t_col, fix_slot_rows = _build_yopt_lp_arrays( + S, col_lower, col_upper, den_coeffs, extra_rows, num_fix_slots) + return lp, reaction_ids, rxn_index, den_row, t_col, fix_slot_rows + + +class HighsYieldSolver: + """One thread's private solver for a build-once Charnes-Cooper LFP.""" + + def __init__(self, lp, reaction_ids: List[str], rxn_index: Dict[str, int], + den_row: int, t_col: int, fix_slot_rows: List[int], + denominator_tol: float = 1e-9): + import highspy + self._highspy = highspy + self.reaction_ids = reaction_ids + self.rxn_index = rxn_index + self.den_row = den_row + self.t_col = t_col + self.fix_slot_rows = fix_slot_rows + self.denominator_tol = denominator_tol + self.h = highspy.Highs() + _configure_highs(self.h) # see HighsRateSolver.__init__ + self.h.passModel(lp) + self._active_num_cols: set = set() + self._fix_support: Dict[int, Dict[int, float]] = {} + + def optimize(self, obj_num: Dict[str, float], sense: str, denominator_sign: float): + """(status, value, fluxes_or_None, scalable).""" + h, hp = self.h, self._highspy + for c in self._active_num_cols: + h.changeColCost(c, 0.0) + self._active_num_cols = set() + for rid, coeff in obj_num.items(): + c = self.rxn_index[rid] + h.changeColCost(c, denominator_sign * coeff) + self._active_num_cols.add(c) + h.changeRowBounds(self.den_row, denominator_sign, denominator_sign) + h.changeObjectiveSense(hp.ObjSense.kMaximize if sense == 'maximize' else hp.ObjSense.kMinimize) + _run_checked(h) + status = h.getModelStatus() + if status == hp.HighsModelStatus.kOptimal: + x = numpy.asarray(h.getSolution().col_value) + t = x[self.t_col] + if t > self.denominator_tol: + fluxes = {rid: float(x[i] / t) for i, rid in enumerate(self.reaction_ids)} + scalable = False + else: + fluxes = {rid: float(x[i]) for i, rid in enumerate(self.reaction_ids)} + scalable = True + return "optimal", h.getInfo().objective_function_value, fluxes, scalable + elif status == hp.HighsModelStatus.kUnbounded: + return "unbounded", (math.inf if sense == 'maximize' else -math.inf), None, True + else: + return "infeasible", float('nan'), None, False + + def set_fix_row(self, slot: int, coeffs: Dict[str, float], value: float): + """Fix a *rate-type* linear expression `coeffs` to `value`, in the + LFP's homogeneous space: sum(coeffs[r]*x_r) - value*t = 0. Reusing + the same `coeffs` across calls (only `value` varying) costs one + changeCoeff call (the t-column entry).""" + row = self.fix_slot_rows[slot] + new_support = {self.rxn_index[rid]: c for rid, c in coeffs.items()} + old_support = self._fix_support.get(slot, {}) + if new_support != old_support: + for col in old_support.keys() - new_support.keys(): + self.h.changeCoeff(row, col, 0.0) + for col, c in new_support.items(): + if old_support.get(col) != c: + self.h.changeCoeff(row, col, c) + self._fix_support[slot] = new_support + self.h.changeCoeff(row, self.t_col, -float(value)) + + def clear_fix_row(self, slot: int): + row = self.fix_slot_rows[slot] + for col in self._fix_support.get(slot, {}): + self.h.changeCoeff(row, col, 0.0) + self.h.changeCoeff(row, self.t_col, 0.0) + self._fix_support[slot] = {} + + +# --------------------------------------------------------------------------- +# Standalone demo / benchmark (needs real cobra + highspy; not runnable in +# this sandbox -- see test_highs_lp_cache.py for a sandbox-runnable +# correctness check against a fake highspy backed by scipy's own HiGHS). +# --------------------------------------------------------------------------- + +def _toy_model(): # pragma: no cover + import cobra + model = cobra.Model("toy") + a, b, c = cobra.Metabolite("A"), cobra.Metabolite("B"), cobra.Metabolite("C") + ex_a = cobra.Reaction("EX_A"); ex_a.add_metabolites({a: 1}); ex_a.bounds = (-10, 0) + r1 = cobra.Reaction("R1"); r1.add_metabolites({a: -1, b: 1}); r1.bounds = (0, 1000) + r2 = cobra.Reaction("R2"); r2.add_metabolites({b: -1, c: 1}); r2.bounds = (0, 1000) + ex_c = cobra.Reaction("EX_C"); ex_c.add_metabolites({c: -1}); ex_c.bounds = (0, 1000) + model.add_reactions([ex_a, r1, r2, ex_c]) + return model + + +def _numpy_gil_sanity_check(): # pragma: no cover + """Sanity-check that *this* interpreter/environment can show real + threaded speedup at all, using a numpy matmul (known to release the + GIL during the BLAS call). Subject to the exact same oversubscription + trap as HiGHS's own `threads` default: OpenBLAS/MKL default to using + several threads *inside* a single matmul call, so without pinning + that down first, "N Python threads" here really means "N x (BLAS's + own thread count)" -- confounded the same way the HiGHS numbers were. + threadpoolctl pins it down at runtime; without it, set + OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1 in the + environment *before* starting Python (numpy reads these at import + time, so setting them after numpy is already imported won't help).""" + import time + import threading as th + try: + import threadpoolctl + limiter = threadpoolctl.threadpool_limits(1) + except ImportError: + threadpoolctl = None + limiter = None + print("[sanity check] threadpoolctl not installed -- this reading may still be " + "confounded by BLAS's own internal threading; `pip install threadpoolctl` " + "for a clean one, or set OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 " + "MKL_NUM_THREADS=1 before starting Python.") + + def run_check(pinned: bool): + n = 700 + a = numpy.random.rand(n, n) + b = numpy.random.rand(n, n) + + def work(k): + for _ in range(k): + a @ b + + reps = 12 + t0 = time.perf_counter() + work(reps) + t_seq = time.perf_counter() - t0 + + n_threads = 4 + per = reps // n_threads + t0 = time.perf_counter() + threads = [th.Thread(target=work, args=(per,)) for _ in range(n_threads)] + for t in threads: t.start() + for t in threads: t.join() + t_par = time.perf_counter() - t0 + pin_note = "BLAS pinned to 1 internal thread" if pinned else "BLAS NOT pinned -- reading may be confounded" + print(f"[sanity check] numpy matmul, {n}x{n}, {pin_note}: " + f"sequential {t_seq:.3f}s, {n_threads} threads {t_par:.3f}s " + f"(speedup {t_seq / t_par:.2f}x -- should be well above 1x on a multi-core " + f"machine; if it isn't, the HiGHS numbers below can't be trusted either way)") + + if limiter is not None: + with limiter: + run_check(pinned=True) + else: + run_check(pinned=False) + + +def _gil_release_direct_test(lp, n_run_calls: int = 4000): # pragma: no cover + """Directly tests whether h.run() releases the GIL, independent of + problem size or Python/pybind11 call-overhead noise (which is what + made the wall-clock speedup numbers hard to read once individual + solves got down to microseconds). A background thread does a tight + pure-Python increment loop -- CPU-bound work that needs the GIL for + every step, and *cannot make any progress at all* while some other + thread holds the GIL uninterrupted. Meanwhile the main thread hammers + h.run(). If run() releases the GIL even briefly per call, the counter + accumulates real progress during that period; if run() never releases + it, the counter is starved almost completely, regardless of how many + run() calls happen or how long each one takes.""" + import threading as th + import time + import highspy + + h = highspy.Highs() + h.setOptionValue("output_flag", False) + h.setOptionValue("log_to_console", False) + h.setOptionValue("threads", 1) + h.passModel(lp) + + def counter_loop(counter, stop): + c = 0 + while not stop.is_set(): + c += 1 + counter[0] = c + + counter = [0] + stop = th.Event() + t = th.Thread(target=counter_loop, args=(counter, stop)) + t.start() + t0 = time.perf_counter() + for _ in range(n_run_calls): + h.run() + dt = time.perf_counter() - t0 + stop.set() + t.join() + concurrent_count = counter[0] + + # reference: same counter thread, undisturbed, for the same wall time + counter2 = [0] + stop2 = th.Event() + t2 = th.Thread(target=counter_loop, args=(counter2, stop2)) + t2.start() + time.sleep(dt) + stop2.set() + t2.join() + solo_count = counter2[0] + + frac = concurrent_count / solo_count if solo_count else float("nan") + print(f"[GIL direct test] {n_run_calls} h.run() calls took {dt:.3f}s; counter thread " + f"reached {concurrent_count:,} running alongside them vs {solo_count:,} running " + f"solo for the same wall time ({frac:.1%}). Near 0% => run() holds the GIL " + f"throughout; a sizeable fraction => run() does release it.") + + +def _pure_run_benchmark(lp, n_calls, n_threads): # pragma: no cover + """Isolates h.run() itself from all the Python-side bookkeeping in + HighsRateSolver.optimize()/set_fix_row() (dict construction, set + diffing, etc.) -- objective/bounds are set once, then run() is called + repeatedly with nothing changed. This is the cleanest available test + of whether run() itself releases the GIL, independent of whatever + overhead this prototype's wrapper adds.""" + import time + import threading as th + import highspy + + def make_h(): + h = highspy.Highs() + h.setOptionValue("output_flag", False) + h.setOptionValue("log_to_console", False) + h.setOptionValue("threads", 1) + h.passModel(lp) + return h + + h = make_h() + t0 = time.perf_counter() + for _ in range(n_calls): + h.run() + t_seq = time.perf_counter() - t0 + print(f"[pure run()] sequential: {n_calls} calls in {t_seq:.3f}s " + f"({1e6 * t_seq / n_calls:.1f}us/call)") + + def worker(k): + hh = make_h() + for _ in range(k): + hh.run() + + per = n_calls // n_threads + t0 = time.perf_counter() + threads = [th.Thread(target=worker, args=(per,)) for _ in range(n_threads)] + for t in threads: t.start() + for t in threads: t.join() + t_par = time.perf_counter() - t0 + print(f"[pure run()] {n_threads} threads x {per} calls: {t_par:.3f}s " + f"(speedup vs sequential: {t_seq / t_par:.2f}x)") + + +def _demo(model=None, n_calls=2000, n_threads=4): # pragma: no cover + import os + import time + import threading as th + + if model is None: + model = _toy_model() + print(f"model: {len(model.reactions)} reactions, {len(model.metabolites)} " + f"metabolites; os.cpu_count()={os.cpu_count()}") + + _numpy_gil_sanity_check() + + reaction_ids0 = model.reactions.list_attr("id") + fix_rid, obj_rid = reaction_ids0[0], reaction_ids0[-1] + + lp, reaction_ids, rxn_index, fix_slot_rows = build_rate_lp(model, num_fix_slots=1) + + solver = HighsRateSolver(lp, reaction_ids, rxn_index, fix_slot_rows) + t0 = time.perf_counter() + for i in range(n_calls): + solver.set_fix_row(0, {fix_rid: 1.0}, -float(1 + i % 9)) + solver.optimize({obj_rid: 1.0}, "maximize") + t_reuse = time.perf_counter() - t0 + print(f"build-once + incremental updates: {n_calls} solves in {t_reuse:.3f}s") + + t0 = time.perf_counter() + for i in range(n_calls): + lp_i, rid_i, ridx_i, _ = build_rate_lp( + model, constraints=[({fix_rid: 1.0}, '=', -float(1 + i % 9))]) + HighsRateSolver(lp_i, rid_i, ridx_i, []).optimize({obj_rid: 1.0}, "maximize") + t_rebuild = time.perf_counter() - t0 + print(f"rebuild-every-call: {n_calls} solves in {t_rebuild:.3f}s") + + def worker(n): + s = HighsRateSolver(lp, reaction_ids, rxn_index, fix_slot_rows) + for i in range(n): + s.set_fix_row(0, {fix_rid: 1.0}, -float(1 + i % 9)) + s.optimize({obj_rid: 1.0}, "maximize") + + per_thread = n_calls // n_threads + t0 = time.perf_counter() + threads = [th.Thread(target=worker, args=(per_thread,)) for _ in range(n_threads)] + for t in threads: t.start() + for t in threads: t.join() + t_threaded = time.perf_counter() - t0 + print(f"{n_threads} threads x {per_thread} solves (via optimize()): {t_threaded:.3f}s " + f"(speedup vs single-thread reuse: {t_reuse / t_threaded:.2f}x)") + + print() + print("Isolating h.run() from this wrapper's own Python overhead:") + _pure_run_benchmark(lp, n_calls, n_threads) + + print() + _gil_release_direct_test(lp) + + +if __name__ == "__main__": # pragma: no cover + import sys + if len(sys.argv) > 1: + import cobra + _demo(model=cobra.io.read_sbml_model(sys.argv[1])) + else: + _demo() diff --git a/cnapy/lptools_highs.py b/cnapy/lptools_highs.py new file mode 100644 index 00000000..8da8e6d1 --- /dev/null +++ b/cnapy/lptools_highs.py @@ -0,0 +1,853 @@ +"""lptools_highs.py (prototype) + +HiGHS-native port of lptools.plot_flux_space, dispatching to the +already-validated tracers in plot_trace_highs.py instead of +straindesign's fba()/yopt(). straindesign is kept ONLY for parsing text +axis/constraint expressions (parse_linexpr, parse_constraints, +linexprdict2str) and for the cmp_model/cmp_map compression-ID remapping +(straindesign.networktools) -- neither of those does any LP solving, so +neither is in tension with the point of this rewrite. + +Split into two layers, same reasoning as everywhere else in this effort: + + - plot_flux_space_data_highs(): computes everything BUT the actual + matplotlib rendering -- axis names/types/limits, degeneracy + classification, and (datapoints, triang, face_polys/slice_outlines). + Pure data, fully unit-testable without matplotlib or a display. + + - plot_flux_space_highs(): thin wrapper that calls the above and then + renders it with the *same* matplotlib code plot_flux_space already + uses (copied essentially unmodified) -- low risk precisely because + it isn't new logic, just new data feeding proven rendering code. + +Covered: 2D point/line/full and 3D point/line/plane/full (0 yield axes: +exact polytope; 1 yield axis: per-slice polygons; 2 or 3 yield axes: the +grid-scanning fallback, ported as trace_grid_3d -- approximate and the +slowest path, exactly as in plot_flux_space itself, and, like the 1-yield +slicing case, a natural target for slice-level threading that isn't done +here). + +Everything below the "pure data" line has NOT been run against a real +cobra model or real highspy -- see the module docstring notes at each +untested seam. +""" + +import math +import os +from typing import Dict, List, Optional, Tuple + +from cnapy.highs_lp_cache import build_rate_lp, build_yopt_lp, HighsRateSolver, HighsYieldSolver, worker_pool +from cnapy.plot_trace_highs import ( + RateAxis, YieldAxis, fix_rate_axis, fix_yield_axis, + trace_polygon_rate_rate, trace_polygon_rate_rate_parallel, + trace_boundary_adaptive, + trace_polytope_3d_rate, trace_polytope_3d_rate_parallel, + trace_3d_slice_polygon, trace_3d_slice_polygon_sequential, + GridAxis, trace_grid_3d, + polygon_area, +) + +# Re-used verbatim from lptools.py: pure math, no solver dependency at +# all, so reusing them is not in tension with dropping straindesign for +# solving. Imported lazily inside functions below to avoid a hard +# top-level dependency on lptools.py for anyone unit-testing this module +# in isolation (as the tests in this prototype do). + + +def _den_key(den: Dict[str, float]) -> Tuple[Tuple[str, float], ...]: + """Canonical, hashable key for a denominator dict, so two axes that + happen to share the same denominator (common -- e.g. several + products' yields relative to the same substrate) share one + HighsYieldSolver/HighsLp instead of building a redundant duplicate.""" + return tuple(sorted(den.items())) + + +class _SolverBundle: + """Everything needed to compute limits/traces for one call's worth of + axes: one shared rate solver (fix slot 0 reserved for pinning "axis + 0" in whichever 2-axis sub-trace is currently running) plus one + HighsYieldSolver per distinct denominator among the axes that are + yield-typed.""" + + def __init__(self, model, reaction_ids, base_constraints, num_fix_slots: int = 1): + rate_lp, rxn_ids, rxn_index, fix_rows = build_rate_lp( + model, constraints=base_constraints, num_fix_slots=num_fix_slots) + self.rate_lp = rate_lp + self.reaction_ids = rxn_ids + self.rxn_index = rxn_index + self.rate_fix_rows = fix_rows + self.rate_solver = HighsRateSolver(rate_lp, rxn_ids, rxn_index, fix_rows) + self._yield_lps: Dict[Tuple, Tuple] = {} # den_key -> (lp, den_row, t_col, fix_rows) + self._yield_solvers: Dict[Tuple, HighsYieldSolver] = {} + self._model = model + self._base_constraints = base_constraints + self._num_fix_slots = num_fix_slots + + def yield_solver_for(self, den: Dict[str, float]) -> HighsYieldSolver: + key = _den_key(den) + solver = self._yield_solvers.get(key) + if solver is None: + lp, rxn_ids, rxn_index, den_row, t_col, fix_rows = build_yopt_lp( + self._model, den, constraints=self._base_constraints, + num_fix_slots=self._num_fix_slots) + solver = HighsYieldSolver(lp, rxn_ids, rxn_index, den_row, t_col, fix_rows) + self._yield_lps[key] = (lp, den_row, t_col, fix_rows) + self._yield_solvers[key] = solver + return solver + + def clone(self) -> "_SolverBundle": + """A bundle with its own fresh solvers (for another thread) that + shares this one's already-built, read-only HighsLp objects -- + one passModel per solver, no re-reading of the cobra model. Yield + LPs not built yet on this bundle are built lazily by the clone, + so call yield_solver_for() here first for every denominator the + workers will need.""" + c = object.__new__(_SolverBundle) + c.rate_lp, c.reaction_ids, c.rxn_index = self.rate_lp, self.reaction_ids, self.rxn_index + c.rate_fix_rows = self.rate_fix_rows + c.rate_solver = HighsRateSolver(self.rate_lp, self.reaction_ids, self.rxn_index, self.rate_fix_rows) + c._yield_lps = dict(self._yield_lps) + c._yield_solvers = { + key: HighsYieldSolver(lp, self.reaction_ids, self.rxn_index, den_row, t_col, fix_rows) + for key, (lp, den_row, t_col, fix_rows) in self._yield_lps.items()} + c._model, c._base_constraints = self._model, self._base_constraints + c._num_fix_slots = self._num_fix_slots + return c + + def rate_axis(self, coeffs: Dict[str, float]) -> RateAxis: + return RateAxis(self.rate_solver, coeffs) + + def yield_axis(self, num: Dict[str, float], den: Dict[str, float]) -> YieldAxis: + return YieldAxis(self.rate_solver, self.yield_solver_for(den), num, den) + + +def _axis_limits(bundle: _SolverBundle, ax_type: str, ax) -> Tuple[float, float]: + """Unconstrained min/max of one axis (rate: ax=[coeffs]; yield: + ax=[num, den]). Raises on unbounded/infeasible, matching + plot_flux_space's own behavior for that case.""" + if ax_type == "rate": + axis = bundle.rate_axis(ax[0]) + v_min = axis.optimize("minimize") + v_max = axis.optimize("maximize") + else: + axis = bundle.yield_axis(ax[0], ax[1]) + v_min = axis.optimize("minimize") + v_max = axis.optimize("maximize") + if v_min is None or v_max is None: + raise Exception( + "One of the specified axes is unbounded, undefined, or the problem is " + "infeasible. Plot cannot be generated.") + return v_min, v_max + + +def _ceil_dec(v: float, n: int) -> float: + return math.ceil(v * (10 ** n)) / (10 ** n) + + +def _floor_dec(v: float, n: int) -> float: + return math.floor(v * (10 ** n)) / (10 ** n) + + +def _detect_degeneracy(val_limits, num_axes): + """Verbatim port of lptools._detect_degeneracy -- pure math.""" + degenerate = [] + for vmin, vmax in val_limits: + scale = max(1.0, abs(vmin), abs(vmax)) + degenerate.append(abs(vmax - vmin) < 1e-8 * scale) + n_degen = sum(degenerate) + if n_degen == num_axes: + return "point", degenerate + elif n_degen == num_axes - 1: + return "line", degenerate + elif n_degen == num_axes - 2: + return "plane", degenerate + return "full", degenerate + + +def _available_cpus() -> int: + try: + return len(os.sched_getaffinity(0)) # respects taskset/cgroup limits on Linux + except AttributeError: + return os.cpu_count() or 1 + + +def auto_n_workers(n_units: int, min_units_per_worker: int, max_workers: Optional[int] = None) -> int: + """thread count for n_units independent work items: every worker gets at least + min_units_per_worker items, and there are never more workers than available CPUs + (or max_workers)""" + cap = _available_cpus() if max_workers is None else min(max_workers, _available_cpus()) + return max(1, min(cap, n_units // min_units_per_worker)) + + +def plot_flux_space_data_highs(model, axes, **kwargs) -> dict: + """Computes everything plot_flux_space needs except the matplotlib + rendering itself. See the module docstring for what's ported and + what's not (the 3D 2+-yield case). + + kwargs accepted (subset of plot_flux_space's, plotting-only ones + like plt_backend/show/cmap are collected here and passed through + untouched for the rendering layer to use): + constraints, points, n_workers (thread count for the parallel + tracers of the 3D cases with yield axes; default "auto" chooses it + from the problem type and the number of slices/grid rows, see + _fill_3d), show, cmap. + + Returns a dict with everything plot_flux_space_highs needs to + reproduce the original's exact plotting branches: ax_name, ax_limits, + val_limits, ax_type, num_axes, degen, degen_axes, datapoints, triang, + face_polys, slice_outlines, show, cmap. + """ + # All solves run on fresh worker_pool threads, so that this never + # touches the HiGHS scheduler of the calling (e.g. GUI) thread; see + # "HiGHS thread scheduler handling" in highs_lp_cache.py. + with worker_pool(1) as ex: + return ex.submit(_plot_flux_space_data_highs, model, axes, **kwargs).result() + + +def _plot_flux_space_data_highs(model, axes, **kwargs) -> dict: + from straindesign import parse_linexpr, parse_constraints + from straindesign.parse_constr import linexprdict2str + + n_workers = kwargs.get("n_workers", "auto") + reaction_ids = model.reactions.list_attr("id") + + constraints_in = kwargs.get("constraints", "") + constraints = parse_constraints(constraints_in, reaction_ids) if constraints_in else [] + + axes = [list(ax) if not isinstance(ax, str) else [ax] for ax in axes] + num_axes = len(axes) + if num_axes not in (2, 3): + raise Exception( + "Please define 2 or 3 axes as a list of tuples [ax1, ax2, (optional) ax3] " + 'with ax1 = (den,num). "den" and "num" being linear expressions.') + + points = kwargs.get("points", 40 if num_axes == 2 else 25) + + ax_name = ["" for _ in range(num_axes)] + ax_type = ["" for _ in range(num_axes)] + for i, ax in enumerate(axes): + if len(ax) == 1: + ax_type[i] = "rate" + ax[0] = parse_linexpr(ax[0], reaction_ids)[0] + ax_name[i] = linexprdict2str(ax[0]) + elif len(ax) == 2: + ax_type[i] = "yield" + ax[0] = parse_linexpr(ax[0], reaction_ids)[0] + ax[1] = parse_linexpr(ax[1], reaction_ids)[0] + ax_name[i] = "(" + linexprdict2str(ax[0]) + ") / (" + linexprdict2str(ax[1]) + ")" + else: + raise Exception(f"Axis {i} must be a single expression (rate) or a " + f"[numerator, denominator] pair (yield), got {ax!r}") + + # one shared solver bundle for everything that doesn't need the + # "plane" case's extra fixed-axis constraint + bundle = _SolverBundle(model, reaction_ids, constraints, num_fix_slots=1) + + val_limits = [(math.nan, math.nan) for _ in range(num_axes)] + ax_limits = [(math.nan, math.nan) for _ in range(num_axes)] + for i, ax in enumerate(axes): + v_min, v_max = _axis_limits(bundle, ax_type[i], ax) + lo, hi = _ceil_dec(v_min, 8), _floor_dec(v_max, 8) + if lo > hi: + # range narrower than the 1e-8 rounding grid, i.e. a fixed axis: rounding + # inward would invert it by 1e-8, which _detect_degeneracy's 1e-8 * max(1, |v|) + # threshold misses for |v| < 1 depending on float noise (lptools.py has the + # same issue), so pin it to the solver's own value instead + lo = hi = (v_min + v_max) / 2 + val_limits[i] = [lo, hi] + ax_limits[i] = [min(0, val_limits[i][0]), max(0, val_limits[i][1])] + if ax_limits[i][0] == ax_limits[i][1]: + pad = max(0.5, abs(ax_limits[i][0]) * 0.1) + ax_limits[i] = [ax_limits[i][0] - pad, ax_limits[i][1] + pad] + + degen, degen_axes = _detect_degeneracy(val_limits, num_axes) + + result = { + "ax_name": ax_name, "ax_type": ax_type, "ax_limits": ax_limits, + "val_limits": val_limits, "num_axes": num_axes, "degen": degen, + "degen_axes": degen_axes, "show": kwargs.get("show", True), + "cmap": kwargs.get("cmap", "managua"), + "datapoints": None, "triang": None, "face_polys": None, "slice_outlines": None, + } + + if num_axes == 2: + _fill_2d(result, bundle, axes, ax_type, val_limits, points, n_workers) + else: + _fill_3d(result, model, reaction_ids, constraints, bundle, axes, ax_type, + val_limits, points, n_workers) + + return result + + +def _fill_2d(result, bundle, axes, ax_type, val_limits, points, n_workers): + # NOTE: _detect_degeneracy's 'plane' label (n_degen == num_axes - 2) + # is only meaningful for num_axes == 3. For num_axes == 2 it + # triggers on n_degen == 0 -- i.e. a perfectly ordinary, fully + # non-degenerate 2D case gets labelled 'plane' here, not 'full'. + # This is inherited from lptools.py's own _detect_degeneracy and is + # harmless there (and here) only because neither dispatch branch + # checks for an explicit 'full' -- both just handle 'point' and + # 'line' and let anything else, including this mislabeled 'plane', + # fall through to full treatment. Don't rely on degen == 'full' for 2D. + degen = result["degen"] + if degen == "point": + result["datapoints"] = [[val_limits[0][0], val_limits[1][0]]] + result["triang"] = [] + return + if degen == "line": + if result["degen_axes"][0]: + x_pts = [val_limits[0][0], val_limits[0][0]] + y_pts = [val_limits[1][0], val_limits[1][1]] + else: + x_pts = [val_limits[0][0], val_limits[0][1]] + y_pts = [val_limits[1][0], val_limits[1][0]] + result["datapoints"] = [[x, y] for x, y in zip(x_pts, y_pts)] + result["triang"] = [] + return + + # full + if all(t == "rate" for t in ax_type): + solver = HighsRateSolver(bundle.rate_lp, bundle.reaction_ids, bundle.rxn_index, []) + vertices = trace_polygon_rate_rate(solver, axes[0][0], axes[1][0]) + else: + ax0_obj = bundle.rate_axis(axes[0][0]) if ax_type[0] == "rate" \ + else bundle.yield_axis(axes[0][0], axes[0][1]) + ax1_obj = bundle.rate_axis(axes[1][0]) if ax_type[1] == "rate" \ + else bundle.yield_axis(axes[1][0], axes[1][1]) + upper, lower = trace_boundary_adaptive(bundle.rate_solver, ax0_obj, ax1_obj, max_depth=max(5, int(math.log2(max(points, 2))))) + vertices = (upper + list(reversed(lower))) if upper and lower else (upper or lower) + if len(vertices) > 1 and vertices[0] != vertices[-1]: + vertices.append(vertices[0]) + + if not vertices: + raise Exception("Could not trace any boundary. Problem may be infeasible.") + + n_v = len(vertices) + if n_v > 2 and vertices[0] == vertices[-1]: + n_v -= 1 + result["triang"] = [[0, i, i + 1] for i in range(1, n_v - 1)] if n_v >= 3 else [] + result["datapoints"] = [[v[0], v[1]] for v in vertices] + result["is_approximate"] = any(t == "yield" for t in ax_type) + + +def _ring_cleanup(ring, scale, tol=1e-9): + """Drop consecutive (and wrap-around) duplicates, measured in + axis-normalized coordinates, and return the ring counter-clockwise.""" + out = [] + for p in ring: + if not out or (abs(p[0] - out[-1][0]) / scale[0] > tol or abs(p[1] - out[-1][1]) / scale[1] > tol): + out.append(p) + while len(out) > 1 and abs(out[0][0] - out[-1][0]) / scale[0] <= tol \ + and abs(out[0][1] - out[-1][1]) / scale[1] <= tol: + out.pop() + if len(out) >= 3: + a2 = sum(out[k][0] * out[(k + 1) % len(out)][1] - out[(k + 1) % len(out)][0] * out[k][1] + for k in range(len(out))) + if a2 < 0: + out.reverse() + return out + + +def _ring_angles(ring, scale): + """Angle in [0, 2pi) of each vertex around the ring's vertex centroid, + in axis-normalized coordinates. Monotone around a convex ring.""" + cx = sum(p[0] for p in ring) / len(ring) + cy = sum(p[1] for p in ring) / len(ring) + return [math.atan2((p[1] - cy) / scale[1], (p[0] - cx) / scale[0]) % (2 * math.pi) for p in ring] + + +def _stitch_rings(A, B, tA, tB, triang): + """Join two closed convex rings (index lists, CCW, A below B) into an + outward-facing triangle band, merging vertices by angle. Robust to + rings with very different vertex counts and to degenerate rings + (1 or 2 vertices), unlike the greedy shortest-diagonal walker.""" + # rotate each ring to start at its smallest angle + ia = min(range(len(A)), key=lambda k: tA[k]) + ib = min(range(len(B)), key=lambda k: tB[k]) + A, tA = A[ia:] + A[:ia], tA[ia:] + tA[:ia] + B, tB = B[ib:] + B[:ib], tB[ib:] + tB[:ib] + # make angles non-decreasing along the ring, then close it at +2pi + def unwrap(t): + u = [t[0]] + for v in t[1:]: + while v < u[-1]: + v += 2 * math.pi + u.append(v) + return u + tA, tB = unwrap(tA), unwrap(tB) + A2, tA2 = A + [A[0]], tA + [tA[0] + 2 * math.pi] + B2, tB2 = B + [B[0]], tB + [tB[0] + 2 * math.pi] + i = j = 0 + while i < len(A2) - 1 or j < len(B2) - 1: + adv_a = j >= len(B2) - 1 or (i < len(A2) - 1 and tA2[i + 1] <= tB2[j + 1]) + if adv_a: + if A2[i] != A2[i + 1]: + triang.append([A2[i], A2[i + 1], B2[j]]) + i += 1 + else: + if B2[j] != B2[j + 1]: + triang.append([A2[i], B2[j + 1], B2[j]]) + j += 1 + + +def build_slice_mesh(slices, rate_indices, yield_idx): + """Turn per-slice 2D polygons into a closed, consistently outward- + oriented 3D triangle mesh. + + Replaces the stitching of lptools._trace_3d_slice_polygon, whose + greedy shortest-diagonal walker (_triangulate_strips) compares raw + distances: when the two rate axes differ in scale it can run around + one ring before the other, folding triangles through the interior + and leaving parts of the side uncovered (visible as see-through + gaps from some view angles). Here, rings are de-duplicated and made + CCW, and adjacent rings are merged by angle around their centroids + in axis-normalized coordinates -- correct because each slice is a + convex polygon (rate/rate projection at a fixed yield). + + slices: iterable of (y_val, polygon_2d). Empty slices are skipped. + Returns (datapoints, triang, slice_outlines). + """ + slices = sorted(((y, list(p)) for y, p in slices if p), key=lambda s: s[0]) + allp = [pt for _, p in slices for pt in p] + if not allp: + return [], [], [] + scale = [max(1e-12, max(p[k] for p in allp) - min(p[k] for p in allp)) for k in (0, 1)] + + datapoints: List[List[float]] = [] + slice_outlines: List[List[int]] = [] + angles: List[List[float]] = [] + for y_val, poly in slices: + ring = _ring_cleanup(poly, scale) + if not ring: + continue + idx_list = [] + for pt in ring: + p3d = [0.0, 0.0, 0.0] + p3d[rate_indices[0]] = pt[0] + p3d[rate_indices[1]] = pt[1] + p3d[yield_idx] = y_val + idx_list.append(len(datapoints)) + datapoints.append(p3d) + slice_outlines.append(idx_list) + angles.append(_ring_angles(ring, scale)) + + if not slice_outlines: + return [], [], [] + + triang: List[List[int]] = [] + for s in range(len(slice_outlines) - 1): + _stitch_rings(slice_outlines[s], slice_outlines[s + 1], angles[s], angles[s + 1], triang) + + first, last = slice_outlines[0], slice_outlines[-1] + if len(first) >= 3: # bottom cap faces -yield + for k in range(1, len(first) - 1): + triang.append([first[0], first[k + 1], first[k]]) + if len(last) >= 3 and len(slice_outlines) > 1: # top cap faces +yield + for k in range(1, len(last) - 1): + triang.append([last[0], last[k], last[k + 1]]) + + # orientation above assumes (rate0, rate1, yield) is a right-handed + # ordering of the plot axes; flip everything for an odd permutation + perm = [rate_indices[0], rate_indices[1], yield_idx] + inversions = sum(1 for a in range(3) for b in range(a + 1, 3) if perm[a] > perm[b]) + if inversions % 2: + triang = [[t[0], t[2], t[1]] for t in triang] + return datapoints, triang, slice_outlines + + +def _fill_3d(result, model, reaction_ids, constraints, bundle, axes, ax_type, + val_limits, points, n_workers): + degen = result["degen"] + degen_axes = result["degen_axes"] + + if degen == "point": + result["datapoints"] = [[val_limits[i][0] for i in range(3)]] + result["triang"] = [] + return + + if degen == "line": + free_ax = [i for i, d in enumerate(degen_axes) if not d][0] + pts_3d = [] + for val in (val_limits[free_ax][0], val_limits[free_ax][1]): + pt = [val_limits[i][0] for i in range(3)] + pt[free_ax] = val + pts_3d.append(pt) + result["datapoints"] = pts_3d + result["triang"] = [] + return + + if degen == "plane": + free_axes = [i for i, d in enumerate(degen_axes) if not d] + fixed_axis = [i for i, d in enumerate(degen_axes) if d][0] + fixed_val = val_limits[fixed_axis][0] + make_fix = fix_rate_axis(axes[fixed_axis][0]) if ax_type[fixed_axis] == "rate" \ + else fix_yield_axis(axes[fixed_axis][0], axes[fixed_axis][1]) + plane_constraints = list(constraints) + # the fixed axis is constant for the WHOLE 2D sub-trace, so bake + # it into the sub-trace's own baseline constraints rather than + # using a per-value fix slot (unlike trace_boundary_adaptive's + # axis 0, which varies). + fix_coeffs, fix_rhs = make_fix(fixed_val) + plane_constraints.append([fix_coeffs, "=", fix_rhs]) + + sub_axes = [axes[free_axes[0]], axes[free_axes[1]]] + sub_types = [ax_type[free_axes[0]], ax_type[free_axes[1]]] + plane_bundle = _SolverBundle(model, reaction_ids, plane_constraints, num_fix_slots=1) + + if all(t == "rate" for t in sub_types): + solver = HighsRateSolver(plane_bundle.rate_lp, plane_bundle.reaction_ids, + plane_bundle.rxn_index, []) + polygon_2d = trace_polygon_rate_rate(solver, sub_axes[0][0], sub_axes[1][0]) + else: + a0 = plane_bundle.rate_axis(sub_axes[0][0]) if sub_types[0] == "rate" \ + else plane_bundle.yield_axis(sub_axes[0][0], sub_axes[0][1]) + a1 = plane_bundle.rate_axis(sub_axes[1][0]) if sub_types[1] == "rate" \ + else plane_bundle.yield_axis(sub_axes[1][0], sub_axes[1][1]) + upper, lower = trace_boundary_adaptive(plane_bundle.rate_solver, a0, a1) + polygon_2d = (upper + list(reversed(lower))) if upper and lower else (upper or lower) + if len(polygon_2d) > 1 and polygon_2d[0] != polygon_2d[-1]: + polygon_2d.append(polygon_2d[0]) + + datapoints = [] + for pt in polygon_2d: + p3d = [0.0, 0.0, 0.0] + p3d[free_axes[0]] = pt[0] + p3d[free_axes[1]] = pt[1] + p3d[fixed_axis] = fixed_val + datapoints.append(p3d) + n_pts = len(datapoints) + if n_pts > 1 and all(abs(datapoints[0][k] - datapoints[-1][k]) < 1e-8 for k in range(3)): + n_pts -= 1 + result["triang"] = [[0, i, i + 1] for i in range(1, n_pts - 1)] if n_pts >= 3 else [] + result["datapoints"] = datapoints + return + + # full 3D + n_yields = sum(1 for t in ax_type if t == "yield") + if n_yields == 0: + solver = HighsRateSolver(bundle.rate_lp, bundle.reaction_ids, bundle.rxn_index, []) + vertices, triang, face_polys = trace_polytope_3d_rate( + solver, axes[0][0], axes[1][0], axes[2][0]) + result["datapoints"] = [list(v) for v in vertices] + result["triang"] = triang + result["face_polys"] = face_polys + elif n_yields == 1: + yield_idx = next(i for i, t in enumerate(ax_type) if t == "yield") + rate_indices = [i for i, t in enumerate(ax_type) if t == "rate"] + y_lo, y_hi = val_limits[yield_idx] + y_space = [y_lo + (y_hi - y_lo) * i / (points - 1) for i in range(points)] if points > 1 else [y_lo] + make_fix = fix_yield_axis(axes[yield_idx][0], axes[yield_idx][1]) + if n_workers == "auto": + # each slice is a cheap 2D rate-rate trace whose Python bookkeeping holds the GIL, + # so the speedup levels off at about 4 threads (benchmarked on ECC2); threads already + # pay off with only one or two slices per worker + n_workers = auto_n_workers(len(y_space), min_units_per_worker=1, max_workers=4) + + # slices are independent; trace_3d_slice_polygon falls back to the + # sequential version itself for n_workers <= 1 + slices = trace_3d_slice_polygon( + bundle.rate_lp, bundle.reaction_ids, bundle.rxn_index, bundle.rate_fix_rows, + make_fix, axes[rate_indices[0]][0], axes[rate_indices[1]][0], y_space, + n_workers=n_workers) + + datapoints, triang, slice_outlines = build_slice_mesh(slices, rate_indices, yield_idx) + result["datapoints"] = datapoints + result["triang"] = triang + result["slice_outlines"] = slice_outlines + result["is_approximate"] = True + else: + # 2 or 3 yield axes: grid-scanning fallback. Unlike every other + # branch this needs TWO simultaneous fix slots (axis 0's value and + # axis 1's value both pinned when reading axis 2), so it gets its + # own bundle built with num_fix_slots=2 rather than reusing the + # single-slot `bundle` used for the axis limits above. + grid_bundle = _SolverBundle(model, reaction_ids, constraints, num_fix_slots=2) + + def grid_axes(b): + def grid_axis(i, needs_own_solver): + if ax_type[i] == "rate": + return GridAxis("rate", b.rate_solver, axes[i][0]) + # axis 0 is only ever pinned (make_fix), never optimized, so + # it doesn't need a yield solver of its own + ys = b.yield_solver_for(axes[i][1]) if needs_own_solver else None + return GridAxis("yield", b.rate_solver, axes[i][0], + den=axes[i][1], yield_solver=ys) + return grid_axis(0, False), grid_axis(1, True), grid_axis(2, True) + + # builds every yield LP the workers need, on this thread, before any + # clone() is taken -- so workers only ever passModel shared LPs + g0, g1, g2 = grid_axes(grid_bundle) + if n_workers == "auto": + # grid rows are expensive (many yield LPs each), scales almost linearly with the threads + n_workers = auto_n_workers(points, min_units_per_worker=1) + y_range_global = max(1e-10, abs(val_limits[1][1] - val_limits[1][0])) + datapoints, triang = trace_grid_3d( + g0, g1, g2, (val_limits[0][0], val_limits[0][1]), y_range_global, points=points, + n_workers=n_workers, make_axes=lambda: grid_axes(grid_bundle.clone())) + if not datapoints: + raise Exception("No feasible slices found. Problem may be infeasible.") + result["datapoints"] = datapoints + result["triang"] = triang + result["is_approximate"] = True + + +def orient_closed_mesh(datapoints, triang): + """Make a triangle mesh consistently oriented with outward normals. + + Flood-fills orientation across shared edges (a neighbour must + traverse a shared edge in the opposite direction), then flips the + whole mesh if its signed volume is negative. Returns + (triang, is_closed); is_closed is True only if every edge is + shared by exactly two triangles and the fill succeeded without + contradiction -- the precondition for back-face culling. Meshes + that aren't closed are returned unchanged with is_closed=False. + Degenerate triangles (repeated vertex index) are dropped. + """ + from collections import defaultdict, deque + tris = [list(t) for t in triang if len({t[0], t[1], t[2]}) == 3] + edge_tris = defaultdict(list) + for ti, t in enumerate(tris): + for k in range(3): + edge_tris[frozenset((t[k], t[(k + 1) % 3]))].append(ti) + if not tris or any(len(v) != 2 for v in edge_tris.values()): + return [list(t) for t in triang], False + + def directed(t): + return {(t[k], t[(k + 1) % 3]) for k in range(3)} + + done = [False] * len(tris) + for seed in range(len(tris)): + if done[seed]: + continue + done[seed] = True + queue = deque([seed]) + while queue: + ti = queue.popleft() + dt = directed(tris[ti]) + for (a, b) in dt: + for nj in edge_tris[frozenset((a, b))]: + if nj == ti: + continue + if not done[nj]: + if (a, b) in directed(tris[nj]): + tris[nj] = [tris[nj][0], tris[nj][2], tris[nj][1]] + done[nj] = True + queue.append(nj) + elif (a, b) in directed(tris[nj]): + return [list(t) for t in triang], False # non-orientable + + vol = 0.0 + for a, b, c in tris: + A, B, C = datapoints[a], datapoints[b], datapoints[c] + vol += (A[0] * (B[1] * C[2] - B[2] * C[1]) - A[1] * (B[0] * C[2] - B[2] * C[0]) + + A[2] * (B[0] * C[1] - B[1] * C[0])) + if vol < 0: + tris = [[t[0], t[2], t[1]] for t in tris] + return tris, True + + +def _ensure_plotting(): + global plt, Poly3DCollection, CulledTriCollection + import matplotlib.pyplot as _plt + from mpl_toolkits.mplot3d.art3d import Poly3DCollection as _poly3d + plt = _plt + Poly3DCollection = _poly3d + CulledTriCollection = _make_culled_collection_class(_poly3d) + + +def _make_culled_collection_class(base): + """Poly3DCollection for a closed, outward-oriented triangle mesh that + hides back-facing triangles on every redraw. mplot3d has no + z-buffer and depth-sorts whole triangles, so on a closed surface + back faces regularly win the sort and the "inside" shows through; + culling them removes that whole class of artifacts. Front-facing + is decided from the triangle's winding after projection to screen + space, so it follows rotation, zoom and axis aspect automatically. + If anything about the projection fails (e.g. a future mplot3d + internals change), it silently draws all triangles.""" + import numpy as np + from mpl_toolkits.mplot3d import proj3d + + class _Culled(base): + def __init__(self, tri_verts, facecolors, **kw): + self._all_verts = np.asarray(tri_verts, dtype=float) # (n, 3, 3) + self._all_fc = np.asarray(facecolors) + super().__init__(list(self._all_verts), facecolors=self._all_fc, + edgecolors=self._all_fc, **kw) + + def do_3d_projection(self): + try: + v = self._all_verts.reshape(-1, 3) + xs, ys, _ = proj3d.proj_transform(v[:, 0], v[:, 1], v[:, 2], self.axes.M) + xs = np.asarray(xs).reshape(-1, 3) + ys = np.asarray(ys).reshape(-1, 3) + area2 = ((xs[:, 1] - xs[:, 0]) * (ys[:, 2] - ys[:, 0]) + - (xs[:, 2] - xs[:, 0]) * (ys[:, 1] - ys[:, 0])) + vis = area2 > 0 + if not vis.any(): + vis[:] = True + except Exception: + vis = np.ones(len(self._all_verts), dtype=bool) + base.set_verts(self, list(self._all_verts[vis])) + base.set_facecolor(self, self._all_fc[vis]) + base.set_edgecolor(self, self._all_fc[vis]) + return super().do_3d_projection() + + return _Culled + + +plt = None +Poly3DCollection = None +CulledTriCollection = None + + +def plot_flux_space_highs(model, axes, **kwargs) -> Tuple[list, list, object]: + """Thin rendering wrapper around plot_flux_space_data_highs: computes + the data (see that function's docstring for what's covered and what + isn't), then renders it with matplotlib calls copied essentially + unmodified from plot_flux_space itself -- this layer is intentionally + almost no new logic, just new (already-validated) data feeding proven + rendering code. NOT run against a real display/model here; only + checked for not crashing headlessly (matplotlib's Agg backend) across + the same scenarios plot_flux_space_data_highs's own tests cover -- + visual correctness of the actual figures still needs a real look. + + Returns (datapoints, triang, plot1) -- same contract as + plot_flux_space itself. + """ + _ensure_plotting() + d = plot_flux_space_data_highs(model, axes, **kwargs) + num_axes = d["num_axes"] + ax_name = d["ax_name"] + ax_limits = d["ax_limits"] + degen = d["degen"] + datapoints = d["datapoints"] + triang = d["triang"] + show = d["show"] + cmap = d["cmap"] + is_approx = d.get("is_approximate", False) + + def _maybe_show(): + if show: + try: + plt.show() + except UserWarning: + pass + + if num_axes == 2: + if degen == "point" or len(datapoints) == 1: + plot1 = plt.plot([datapoints[0][0]], [datapoints[0][1]], "o")[0] + elif degen == "line" or not triang: + x = [p[0] for p in datapoints] + y = [p[1] for p in datapoints] + plot1 = plt.plot(x, y, linewidth=1.5)[0] + else: + x = [p[0] for p in datapoints] + y = [p[1] for p in datapoints] + plot1 = plt.fill(x, y, linewidth=0.5)[0] + if is_approx: + plot1.axes.set_title("approximate", fontsize=8, color="gray") + plot1.axes.set_xlabel(ax_name[0]) + plot1.axes.set_ylabel(ax_name[1]) + plot1.axes.set_xlim(ax_limits[0][0] * 1.05, ax_limits[0][1] * 1.05) + plot1.axes.set_ylim(ax_limits[1][0] * 1.05, ax_limits[1][1] * 1.05) + _maybe_show() + return datapoints, triang, plot1 + + # 3D + ax3 = plt.figure().add_subplot(projection="3d") + ax3.set_xlim(ax_limits[0]) + ax3.set_ylim(ax_limits[1]) + ax3.set_zlim(ax_limits[2]) + ax3.set_xlabel(ax_name[0]) + ax3.set_ylabel(ax_name[1]) + ax3.set_zlabel(ax_name[2]) + + if degen == "point" or (len(datapoints) == 1): + ax3.scatter([datapoints[0][0]], [datapoints[0][1]], [datapoints[0][2]], s=50) + _maybe_show() + return datapoints, triang, ax3 + if degen == "line" or (not triang and len(datapoints) <= 2): + xs = [p[0] for p in datapoints] + ys = [p[1] for p in datapoints] + zs = [p[2] for p in datapoints] + ax3.plot3D(xs, ys, zs, linewidth=1.5) + _maybe_show() + return datapoints, triang, ax3 + + if is_approx: + ax3.set_title("approximate", fontsize=8, color="gray") + + face_polys = d.get("face_polys") + slice_outlines = d.get("slice_outlines") + + def _normal_color(face_pts): + import numpy as _np + pts = _np.array(face_pts) + if len(pts) < 3: + return 0.5 + e1, e2 = pts[1] - pts[0], pts[2] - pts[0] + normal = _np.array([ + e1[1] * e2[2] - e1[2] * e2[1], + e1[2] * e2[0] - e1[0] * e2[2], + e1[0] * e2[1] - e1[1] * e2[0], + ]) + length = (normal.dot(normal)) ** 0.5 + if length > 0: + normal = normal / length + # atan2 term weighted by the horizontal part of the normal: same + # colours as lptools on side faces, but continuous towards + # horizontal faces, where atan2 of rounding noise would + # otherwise give random colours (speckled flat faces) + h = math.hypot(normal[0], normal[1]) + ang = math.atan2(normal[1], normal[0]) if h > 1e-6 else 0.0 + return ang * h + 1.5 * normal[2] + + if face_polys is not None and face_polys: + poly_verts = [[datapoints[i] for i in face] for face in face_polys] + color_vals = [_normal_color(v) for v in poly_verts] + mn, mx = min(color_vals), max(color_vals) + rng = mx - mn if mx > mn else 1 + face_colors = plt.get_cmap(cmap)([(c - mn) / rng for c in color_vals]) + face_colors[:, 3] = 1.0 + collection = Poly3DCollection(poly_verts, facecolors=face_colors, edgecolors="black", linewidths=1.0) + ax3.add_collection3d(collection) + plot1 = collection + elif triang: + # orient outward + detect closedness; closed meshes get back-face + # culling and face-coloured edges (closes antialiasing cracks) + draw_triang, closed = orient_closed_mesh(datapoints, triang) + if closed: + # drop zero-area slivers from what is drawn (only after the + # closedness check, which needs them): their normals, colour + # and front/back test are all numerical noise + import numpy as _np + _P = _np.asarray(datapoints, float) + _P = (_P - _P.min(0)) / _np.maximum(_np.ptp(_P, axis=0), 1e-12) + _T = _np.asarray(draw_triang) + _a = _np.linalg.norm(_np.cross(_P[_T[:, 1]] - _P[_T[:, 0]], _P[_T[:, 2]] - _P[_T[:, 0]]), axis=1) + if (_a > 1e-9).any(): + draw_triang = [t for t, keep in zip(draw_triang, _a > 1e-9) if keep] + tri_verts = [[datapoints[i] for i in t] for t in draw_triang] + color_vals = [_normal_color(v) for v in tri_verts] + mn, mx = min(color_vals), max(color_vals) + rng = mx - mn if mx > mn else 1 + face_colors = plt.get_cmap(cmap)([(c - mn) / rng for c in color_vals]) + face_colors[:, 3] = 1.0 + if closed: + collection = CulledTriCollection(tri_verts, face_colors, linewidths=0.3) + else: + collection = Poly3DCollection(tri_verts, facecolors=face_colors, edgecolors="none", linewidths=0) + ax3.add_collection3d(collection) + if slice_outlines: + for idx_list in slice_outlines: + pts = [datapoints[i] for i in idx_list] + [datapoints[idx_list[0]]] + ax3.plot([p[0] for p in pts], [p[1] for p in pts], [p[2] for p in pts], + color="gray", linewidth=0.4) + plot1 = collection + else: + plot1 = ax3.scatter([p[0] for p in datapoints], [p[1] for p in datapoints], + [p[2] for p in datapoints], s=20) + + _maybe_show() + return datapoints, triang, plot1 diff --git a/cnapy/optlang_highs_interface.py b/cnapy/optlang_highs_interface.py new file mode 100644 index 00000000..62c6b649 --- /dev/null +++ b/cnapy/optlang_highs_interface.py @@ -0,0 +1,1556 @@ +""" +optlang interface for the HiGHS solver, using the highspy Python bindings. + +Supports LP, QP (Quadratic Programming) and MILP (Mixed Integer Linear +Programming). +- Continuous, integer and binary variables. Note: mixing integer/binary + variables with a quadratic objective (MIQP) is not supported by HiGHS; + use a linear objective whenever the model contains integer/binary + variables. +- Linear constraints. +- Linear or Quadratic objectives (continuous models only, see above). + +Install with: + pip install highspy +""" + +import logging +from collections import defaultdict + +import numpy as np + +try: + import highspy +except ImportError: + raise ImportError("The highs_interface requires highspy: pip install highspy") + +from optlang import interface +from optlang import symbolics +from optlang.expression_parsing import parse_optimization_expression +from optlang.exceptions import ContainerAlreadyContains + +log = logging.getLogger(__name__) + + +_HIGHS_STATUS_TO_STATUS = { + "Optimal": interface.OPTIMAL, + "Infeasible": interface.INFEASIBLE, + "Unbounded": interface.UNBOUNDED, + "Unbounded or infeasible": interface.INFEASIBLE_OR_UNBOUNDED, + "Infeasible or unbounded": interface.INFEASIBLE_OR_UNBOUNDED, + "Time limit reached": interface.TIME_LIMIT, + "Iteration limit reached": interface.ITERATION_LIMIT, + # MIP-specific early-termination statuses (only ever returned once a + # model has integer/binary variables and HiGHS' branch-and-bound runs): + "Solution limit reached": interface.SOLUTION_LIMIT, + "Objective bound": interface.SUBOPTIMAL, + "Objective target": interface.SUBOPTIMAL, + "Interrupted by user callback": interface.ABORTED, + "Solve error": interface.UNDEFINED, + "Not Set": interface.UNDEFINED, +} + +_STATUSES_WITH_USABLE_SOLUTION = frozenset([ + interface.OPTIMAL, interface.SUBOPTIMAL, interface.TIME_LIMIT, interface.ITERATION_LIMIT, + interface.INFEASIBLE # needed for cobrapy, see cobra/util/solver.has_primals +]) + + +def _linear_expression_to_dict(expression): + """Turn a linear sympy/symengine expression into ({var_name: coeff}, constant).""" + if getattr(expression, "is_Add", False): + terms = expression.args + else: + terms = (expression,) + + coeffs = defaultdict(float) + constant = 0.0 + + for term in terms: + if term.is_Number: + constant += float(term) + elif term.is_Symbol: + coeffs[term.name] += 1.0 + elif term.is_Mul and len(term.args) == 2: + coeff, sym = term.args + if coeff.is_Number and sym.is_Symbol: + coeffs[sym.name] += float(coeff) + else: + return _linear_expression_to_dict_fallback(expression) + else: + return _linear_expression_to_dict_fallback(expression) + + return dict(coeffs), constant + +def _linear_expression_to_dict_fallback(expression): + offset, linear_coeffs, _ = parse_optimization_expression(None, linear=True, expression=expression) + coeffs = {var.name: float(coeff) for var, coeff in linear_coeffs.items()} + return coeffs, float(offset) + +def _separate_linear_and_quadratic_from_expr(expression): + """ + Separates a raw SymPy expression into linear and quadratic coefficients. + """ + # if isinstance(expression, (int, float)): + # expression = symbolics.sympify(expression) + # else: + expression = expression.expand() + + # 1. Extract Quadratic Terms and get the remaining (linear) expression + quadratic_coeffs, remaining_expr = _get_quadratic_terms_from_expr(expression) + + # 2. Parse the remaining expression using the dedicated linear parser + linear_coeffs, constant = _linear_expression_to_dict(remaining_expr) + + return linear_coeffs, quadratic_coeffs, constant + +def _get_quadratic_terms_from_expr(expression): + """Extract quadratic terms from a raw expression. + + Returns: + quadratic_coeffs (dict): {(var1_name, var2_name): coeff} + remaining_expression (sympy.Expr): The expression with quadratic terms removed. + """ + quadratic_coeffs = defaultdict(float) + + # # Ensure we are working with a SymPy object + # if isinstance(expression, (int, float)): + # expression = symbolics.sympify(expression) + # else: + # expression = expression.expand() + + # We will build the remaining expression by subtracting quadratic terms + # However, subtracting SymPy objects can be tricky with types. + # Instead, we can iterate through the terms and pick out what's NOT quadratic. + + remaining_terms = [] + + def process_term(term): + coeff = 1.0 + factors = [] + + if term.is_Number: + remaining_terms.append(term) + return True + + if term.is_Mul: + args = term.args + else: + args = [term] + + for arg in args: + if arg.is_Number: + coeff *= float(arg) + elif arg.is_Symbol: + factors.append(arg.name) + elif arg.is_Pow: + base, exp = arg.args + if exp.is_Number and float(exp) == 2.0 and base.is_Symbol: + factors.append(base.name) + factors.append(base.name) + else: + # Non-quadratic power, keep it in remaining + remaining_terms.append(term) + return False + else: + # Complex term, keep it + remaining_terms.append(term) + return False + + if len(factors) == 2: + # Quadratic term x*y + v1, v2 = sorted(factors) + quadratic_coeffs[(v1, v2)] += coeff + return True + elif len(factors) == 0: + # This was just a number, handled by is_Number check above + return True + elif len(factors) == 1: + # Linear term + remaining_terms.append(term) + return True + else: + # Degree > 2 + raise ValueError("Degree > 2 detected.") + + if getattr(expression, "is_Add", False): + for term in expression.args: + process_term(term) + else: + process_term(expression) + + # Reconstruct the remaining expression from the kept terms + remaining_expression = symbolics.add(remaining_terms) if remaining_terms else symbolics.sympify(0) + + return dict(quadratic_coeffs), remaining_expression + + +class Variable(interface.Variable): + def __init__(self, name, *args, **kwargs): + var_type = kwargs.get("type", "continuous") + if var_type not in ("continuous", "integer", "binary"): + raise ValueError( + "This HiGHS interface only supports continuous, integer " + "and binary variables (type=%r is not one of these)." % (var_type,) + ) + super(Variable, self).__init__(name, **kwargs) + # The column index of this variable in the HiGHS problem it currently + # belongs to (None if it isn't attached to a model). New columns are + # always appended at the end, so Model._add_variables can just read + # off problem.getNumCol() - 1 to set this; on removal, the remaining + # variables' indices are shifted down to match HiGHS's own reindexing + # (see Model._remove_variables). This makes a separate + # name -> index map on the Model unnecessary. + self._solver_index = None + + @interface.Variable.lb.setter + def lb(self, value): + interface.Variable.lb.fset(self, value) + if self.problem is not None: + self.problem._highs_set_col_bounds(self) + + @interface.Variable.ub.setter + def ub(self, value): + interface.Variable.ub.fset(self, value) + if self.problem is not None: + self.problem._highs_set_col_bounds(self) + + @interface.Variable.name.setter + def name(self, value): + # Reimplemented rather than delegating to interface.Variable.name's + # base setter (interface.Variable.name.fset): that base setter also + # updates problem._variables_to_constraints_mapping, which this + # interface no longer maintains -- HiGHS's own row/column data is the + # sole source of truth for constraint/variable membership (see + # Model._add_variables/_add_constraints/_remove_variables below). + # + # getattr(self, 'problem', None), not self.problem: sympy's + # Symbol.__new__ (via __xnew__) assigns .name during construction, + # before Variable.__init__ has set self.problem at all - a direct + # attribute access would raise AttributeError at that point. + if len(value) < 1: + raise ValueError('Variable name must not be empty string') + for char in value: + if char.isspace(): + raise ValueError( + 'Variable names cannot contain whitespace characters. "%s" contains whitespace character "%s".' % ( + value, char)) + old_name = getattr(self, 'name', None) + self._name = value + problem = getattr(self, 'problem', None) + if problem is not None and value != old_name: + problem.variables.update_key(old_name) + if problem is not None: + problem._highs_set_col_name(self) + + @interface.Variable.type.setter + def type(self, value): + # The base setter (interface.Variable.type.fset) may itself go + # through self.lb/self.ub (for type == 'integer', via the property + # setters above - which already sync bounds to HiGHS on their own) + # or bypass them entirely and poke self._lb/self._ub directly (for + # type == 'binary'). Either way, once the base setter returns, + # self.lb/self.ub/self.type all already reflect the final, settled + # state, so _highs_set_col_type below can simply push that settled + # state (bounds + integrality) to HiGHS in one go - no further + # self.lb=/self.ub=/self.type= assignments happen here, so there is + # no risk of this setter (transitively) re-entering itself. + interface.Variable.type.fset(self, value) + if self.problem is not None: + self.problem._highs_set_col_type(self) + + def set_bounds(self, lb, ub): + super(Variable, self).set_bounds(lb, ub) + if self.problem is not None: + self.problem._highs_set_col_bounds(self) + + @property + def primal(self): + if self.problem is None: + return None + return self.problem._variable_primal(self) + + @property + def dual(self): + if self.problem is None: + return None + return self.problem._variable_dual(self) + + +class Constraint(interface.Constraint): + """ + Unlike the earlier version of this interface, this Constraint does not keep + its own copy of the linear coefficients around. Once the constraint is part + of a model, HiGHS's own row data is the single source of truth - writes go + straight through to the solver (mirroring the GLPK interface's design), so + there's no local coefficient cache that can silently drift from it. + + .expression is the one exception: rebuilding it from HiGHS's row data on + every access is expensive, so it's cached in `_expression` and only + rebuilt when `_expression_expired` is set. Anything that changes what a + constraint's row actually contains - a direct coefficient write, or a + referenced variable disappearing out from under it - is responsible for + setting that flag. + + `_initial_coeffs` is only a bootstrap value: it is parsed once at + construction time so `_add_constraints` has something to build the HiGHS + row from, and is never consulted again once that row exists. + + `_constant` remains local state (HiGHS rows don't carry a constant offset + themselves, so it has to be folded into the row bounds), but a bare + constant can never reference a variable, so it can never go stale the way + a coefficient cache can. + """ + + _INDICATOR_CONSTRAINT_SUPPORT = False + + def __init__(self, expression, sloppy=False, *args, **kwargs): + if isinstance(expression, (int, float)): + sloppy = True + expression = symbolics.Real(expression) + + self._expression_expired = False + super(Constraint, self).__init__(expression, sloppy=sloppy, *args, **kwargs) + + if not sloppy and not self.is_Linear: + raise ValueError( + "The HiGHS interface only supports linear constraints. " + "%s is not linear." % self + ) + + self._initial_coeffs, self._constant = _linear_expression_to_dict(self.expression) + # The row index of this constraint in the HiGHS problem it currently + # belongs to (None if it isn't attached to a model, or not yet added + # to the model's HiGHS instance). See Variable._solver_index for why + # this replaces a name -> index map kept on the Model. + self._solver_index = None + + @interface.Constraint.lb.setter + def lb(self, value): + interface.Constraint.lb.fset(self, value) + if self.problem is not None: + self.problem._highs_set_row_bounds(self) + + @interface.Constraint.ub.setter + def ub(self, value): + interface.Constraint.ub.fset(self, value) + if self.problem is not None: + self.problem._highs_set_row_bounds(self) + + @interface.Constraint.name.setter + def name(self, value): + interface.Constraint.name.fset(self, value) + if getattr(self, 'problem', None) is not None: + self.problem._highs_set_row_name(self) + + @property + def primal(self): + if self.problem is None: + return None + return self.problem._constraint_primal(self) + + @property + def dual(self): + if self.problem is None: + return None + return self.problem._constraint_dual(self) + + def _get_expression(self): + """Reconstruct the expression by reading HiGHS's live row data if necessary.""" + if not self._expression_expired: + return self._expression + elif self.problem is not None: # constraint may be pending removal + self.problem.update() + + if self.problem is None: + raise ValueError("Constraint expression cannot be constructed.") + + row_index = self._solver_index + if row_index is None: + # Constructed, but not yet added to the model's HiGHS instance - + # report the bootstrap coefficients that _add_constraints will use. + terms = [] + for var_name, coeff in self._initial_coeffs.items(): + var = self.problem.variables.get(var_name) + if var is not None: + terms.append(coeff * var) + else: + _, idx, val = self.problem.problem.getRowEntries(row_index) + terms = [] + for i, v in zip(idx, val): + var_name = self.problem.problem.variableName(int(i)) + var = self.problem.variables.get(var_name) + if var is not None: + terms.append(float(v) * var) + + if self._constant != 0: + # symbolics.add() uses SymPy's low-level Add._from_args(), which + # (unlike Add(*args)) does not auto-sympify its arguments - a bare + # Python float here breaks it (AttributeError: 'float' object has + # no attribute 'is_commutative'), so sympify explicitly. + terms.append(symbolics.Real(self._constant)) + + self._expression = symbolics.add(terms) if terms else symbolics.sympify(0) + self._expression_expired = False + return self._expression + + def set_linear_coefficients(self, coefficients, sloppy=False): + """ + Sets the linear coefficients of the constraint directly, bypassing SymPy + parsing. Writes go straight through to HiGHS's row - there's no local + cache to keep in sync, so this can never drift from the solver's state. + + Args: + coefficients (dict): A dictionary of {var: coeff}. + sloppy (bool): If True, skip re-resolving each variable through + the model (self.problem.variables.get(var.name)) and pass + `coefficients` straight through to HiGHS. Only safe when the + variables in `coefficients` are already known to belong to + this constraint's model (e.g. they came from model.variables + in the first place) - saves a dict lookup per variable, which + matters when this is called in a hot loop. + """ + if self.problem is None: + raise Exception("Can't change coefficients if constraint is not associated with a model.") + + if self._solver_index is None: + # Not yet added to the model - update the bootstrap coefficients + # that will be used to build the row. + for var, coeff in coefficients.items(): + self._initial_coeffs[var.name] = coeff + return + + if sloppy: + self.problem._highs_set_coefficients(self, coefficients) + self._expression_expired = True + return + + model_coeffs = {} + for var, coeff in coefficients.items(): + var_obj = self.problem.variables.get(var.name) + if var_obj is not None: + model_coeffs[var_obj] = coeff + + self.problem._highs_set_coefficients(self, model_coeffs) + self._expression_expired = True + + def get_linear_coefficients(self, variables): + """ + Get coefficients of linear terms in constraint. + + Like everything else on this class, this reads straight from HiGHS's + row data when the constraint is attached to a model (via the same + helper _get_expression uses), rather than from any local cache. + + Parameters + ---------- + variables : iterable + An iterable of Variable objects + + Returns + ------- + Coefficients : dict + {var1: coefficient, var2: coefficient ...}. Variables with no + entry in the constraint get a coefficient of 0. + """ + if self.problem is None: + raise Exception( + "Can't get coefficients from solver if constraint is not associated with a model.") + coeffs, _, _ = self.problem._constraint_to_coeffs(self) + return {variable: coeffs.get(variable.name, 0) for variable in variables} + + +class Objective(interface.Objective): + """ + The linear part of the objective follows the same ground-truth design as + Constraint: once attached to a model, HiGHS's own cost vector (readable via + Highs.getObjective()) is authoritative, and coefficient writes go straight + through to Highs.changeColCost. There's no local linear-coefficient cache + left to go stale. + + The quadratic part (_quadratic_coeffs) is still cached locally. highspy's + high-level wrapper (highs.py) has no Hessian read-back equivalent to + getObjective()/getRowEntries() - passHessian is write-only from what's + exposed there. Until that's confirmed one way or another against the + compiled _core module, the Hessian remains the one piece still built from + a local cache rather than queried live, and _remove_variables still has to + prune it explicitly when a variable disappears. + """ + + def __init__(self, expression, sloppy=False, *args, **kwargs): + if isinstance(expression, (int, float)): + expression = symbolics.Real(expression) + sloppy = True + + kwargs['sloppy'] = sloppy + super(Objective, self).__init__(expression, *args, **kwargs) + + if not sloppy: + if not (self.is_Linear or self.is_Quadratic): + raise ValueError("The HiGHS interface only supports linear or quadratic objectives.") + + # Bootstrap values only: _initial_linear_coeffs seeds HiGHS's cost + # vector the first time this Objective is attached to a model (see + # Model._update_linear_objective) and is never consulted again after + # that. _quadratic_coeffs remains the ongoing source of truth for the + # Hessian (see class docstring). + self._initial_linear_coeffs, self._quadratic_coeffs, self._constant = \ + _separate_linear_and_quadratic_from_expr(self.expression) + + def _get_expression(self): + """Reconstruct the expression, reading the linear part live from HiGHS.""" + if self.problem is None: + return self._expression + + obj_expr, _ = self.problem.problem.getObjective() + linear_coeffs = {} + for i, v in zip(obj_expr.idxs, obj_expr.vals): + var_name = self.problem.problem.variableName(int(i)) + linear_coeffs[var_name] = float(v) + + terms = [] + + for (v1_name, v2_name), coeff in self._quadratic_coeffs.items(): + v1 = self.problem.variables.get(v1_name) + v2 = self.problem.variables.get(v2_name) + if v1 is not None and v2 is not None: + terms.append(coeff * v1 * v2) + + for var_name, coeff in linear_coeffs.items(): + var = self.problem.variables.get(var_name) + if var is not None: + terms.append(coeff * var) + + if self._constant != 0: + # See Constraint._get_expression - symbolics.add() needs an + # already-sympified argument, not a bare Python float. + terms.append(symbolics.sympify(self._constant)) + + return symbolics.add(terms) if terms else symbolics.sympify(0) + + @interface.Objective.expression.getter + def expression(self): + return self._get_expression() + + @interface.Objective.direction.setter + def direction(self, value): + interface.Objective.direction.fset(self, value) + if self.problem is not None: + self.problem._highs_set_objective_sense(value) + + @property + def value(self): + if self.problem is None: + return None + return self.problem._get_objective_value() + + def set_linear_coefficients(self, coefficients): + self.set_coefficients(linear=coefficients) + + def set_coefficients(self, linear=None, quadratic=None, constant=None): + """ + Sets the objective coefficients directly, bypassing SymPy parsing. + + Args: + linear (dict, optional): Dictionary of {Variable: coeff} for linear terms. + quadratic (dict, optional): Dictionary of {(Variable, Variable): coeff} + for quadratic terms. + constant (float, optional): Constant term of the objective. + """ + if self.problem is None: + raise Exception("Can't change coefficients if objective is not associated with a model.") + + # Linear coefficients go straight through to HiGHS's cost vector - + # only the named variables are touched, so this is naturally a merge, + # not a replace, with no local bookkeeping required. (This matters in + # practice: callers like cobrapy build up an objective by calling this + # incrementally, one or two variables at a time, across many calls, + # and expect earlier calls' coefficients to survive.) + if linear is not None: + self.problem._highs_set_objective_coefficients(linear) + + # Quadratic coefficients are different: _quadratic_coeffs is still a + # local cache standing in for the whole Hessian (see class + # docstring), and passing quadratic={} is the only way to express + # "there are now fewer quadratic terms than before" - including zero + # of them, e.g. when moving from a QP back to a plain LP. So unlike + # linear, an explicitly-given quadratic dict *replaces* the cache + # outright rather than merging into it. + if quadratic is not None: + self._quadratic_coeffs = { + (v1.name, v2.name): coeff for (v1, v2), coeff in quadratic.items() + } + self.problem._update_quadratic_objective() + + if constant is not None: + self._constant = float(constant) + self.problem.problem.changeObjectiveOffset(float(constant)) + + def get_linear_coefficients(self, variables): + """ + Get coefficients of linear terms in objective. + + Reads the linear part straight from HiGHS's live cost vector when + attached to a model, the same way _get_expression does. The + quadratic part is intentionally not reflected here since this only + reports linear coefficients (see set_linear_coefficients/ + set_coefficients for the quadratic equivalent). + + Parameters + ---------- + variables : iterable + An iterable of Variable objects + + Returns + ------- + Coefficients : dict + {var1: coefficient, var2: coefficient ...}. Variables with no + linear entry in the objective get a coefficient of 0. + """ + if self.problem is None: + raise Exception( + "Can't get coefficients from solver if objective is not associated with a model.") + obj_expr, _ = self.problem.problem.getObjective() + linear_coeffs = { + self.problem.problem.variableName(int(i)): float(v) + for i, v in zip(obj_expr.idxs, obj_expr.vals) + } + return {variable: linear_coeffs.get(variable.name, 0) for variable in variables} + + +class Tolerances(object): + def __init__(self, configuration): + self._configuration = configuration + self._feasibility = 1e-7 + self._optimality = 1e-7 + # HiGHS always solves QPs with its interior-point method, which has its + # own convergence tolerance distinct from the simplex-oriented + # primal/dual feasibility tolerances above. Exposed separately since + # loosening it is often the fix for QPs that fail to converge for + # purely numerical/scaling reasons. + self._ipm_optimality = 1e-8 + # Relative MIP optimality gap (HiGHS option 'mip_rel_gap'). Only + # meaningful once a model has integer/binary variables and its + # branch-and-bound actually runs; applying it unconditionally to + # every model (see _apply_tolerances) is harmless for pure LP/QP + # problems, since HiGHS simply ignores it there. + self._mip_gap = 1e-4 + + @property + def feasibility(self): + return self._feasibility + + @feasibility.setter + def feasibility(self, value): + self._feasibility = value + self._configuration._apply_tolerances() + + @property + def optimality(self): + return self._optimality + + @optimality.setter + def optimality(self, value): + self._optimality = value + self._configuration._apply_tolerances() + + @property + def ipm_optimality(self): + return self._ipm_optimality + + @ipm_optimality.setter + def ipm_optimality(self, value): + self._ipm_optimality = value + self._configuration._apply_tolerances() + + @property + def mip_gap(self): + return self._mip_gap + + @mip_gap.setter + def mip_gap(self, value): + self._mip_gap = value + self._configuration._apply_tolerances() + + def to_dict(self): + return { + "feasibility": self.feasibility, + "optimality": self.optimality, + "ipm_optimality": self.ipm_optimality, + "mip_gap": self.mip_gap, + } + + +class Configuration(interface.MathematicalProgrammingConfiguration): + def __init__(self, verbosity=0, timeout=None, presolve=True, + problem=None, *args, **kwargs): + self.problem = problem + self._verbosity = verbosity + self._timeout = timeout + self._presolve = presolve + self._tolerances = Tolerances(self) + super(Configuration, self).__init__(*args, **kwargs) + self.problem = problem + if self.problem is not None: + self._apply_all() + + def _apply_all(self): + h = self.problem.problem + if h is None: + return + h.setOptionValue("output_flag", self._verbosity != 0) + if self._timeout is not None: + h.setOptionValue("time_limit", float(self._timeout)) + h.setOptionValue("presolve", "on" if self._presolve else "off") + self._apply_tolerances() + + def _apply_tolerances(self): + if self.problem is not None and self.problem.problem is not None: + h = self.problem.problem + h.setOptionValue("primal_feasibility_tolerance", self._tolerances.feasibility) + h.setOptionValue("dual_feasibility_tolerance", self._tolerances.feasibility) + h.setOptionValue("ipm_optimality_tolerance", self._tolerances.ipm_optimality) + h.setOptionValue("mip_rel_gap", self._tolerances.mip_gap) + + @property + def tolerances(self): + return self._tolerances + + @tolerances.setter + def tolerances(self, value): + if isinstance(value, Tolerances): + self._tolerances = value + + @property + def verbosity(self): + return self._verbosity + + @verbosity.setter + def verbosity(self, value): + self._verbosity = value + if self.problem is not None and self.problem.problem is not None: + self.problem.problem.setOptionValue("output_flag", value != 0) + + @property + def timeout(self): + return self._timeout + + @timeout.setter + def timeout(self, value): + self._timeout = value + if self.problem is not None and self.problem.problem is not None: + if value is not None: + self.problem.problem.setOptionValue("time_limit", float(value)) + + @property + def presolve(self): + return self._presolve + + @presolve.setter + def presolve(self, value): + self._presolve = value + if self.problem is not None and self.problem.problem is not None: + self.problem.problem.setOptionValue("presolve", "on" if value else "off") + + +class _HighsBackedContainer(object): + """ + Shared base for the model.variables / model.constraints containers. Their + ordering and indexing are read directly from the live HiGHS instance - + the same "ask the solver" principle used throughout this interface for + Constraint/Objective coefficients - instead of a private _object_list / + _indices that could drift out of sync with the solver's own column/row + order (e.g. after HiGHS shifts indices down on _remove_variables's + deleteVars call). + + A name -> object dict is still kept, since HiGHS itself has no notion of + the optlang Variable/Constraint wrapper objects, only of names and + column/row numbers - "how many are there" and "what order are they in" + come from HiGHS, "which Python object goes with this name" comes from + this dict. + + This deliberately does not subclass optlang.container.Container. That + class carries its own private _object_list/_indices bookkeeping (exactly + what this class exists to avoid), plus API surface this interface never + touches: __setitem__, fromkeys, and the py2-era iterkeys/itervalues/ + iteritems/has_key aliases. Implementing just the subset actually used by + optlang.interface.Model and this file - append/extend, __contains__/ + __iter__/__getitem__/__delitem__/__len__, get/keys/values/items, and + update_key (needed by Variable.name's and Constraint.name's setters when + an attached object is renamed) - keeps this class's behavior easy to + verify against what's really exercised. + + Subclasses only need to implement _num() and _name_at(idx). + """ + + def __init__(self, model, iterable=()): + self._model = model + self._dict = {} + for item in iterable: + self.append(item) + + def _num(self): + raise NotImplementedError("Subclasses must implement _num().") + + def _name_at(self, idx): + raise NotImplementedError("Subclasses must implement _name_at().") + + def _live_names(self): + return [self._name_at(i) for i in range(self._num())] + + def __len__(self): + return self._num() + + def __contains__(self, item): + if isinstance(item, str): + return item in self._dict + name = getattr(item, "name", None) + return name is not None and name in self._dict and item is self._dict[name] + + def __iter__(self): + for name in self._live_names(): + obj = self._dict.get(name) + if obj is not None: + yield obj + + def __getitem__(self, item): + if isinstance(item, slice): + names = self._live_names()[item] + return [self._dict[name] for name in names if name in self._dict] + if isinstance(item, int): + names = self._live_names() + return self._dict[names[item]] + return self._dict[item] # by name + + def __delitem__(self, key): + name = key if isinstance(key, str) else self[key].name + del self._dict[name] + + def keys(self): + return list(self._live_names()) + + def values(self): + return list(self) + + def items(self): + return [(name, self._dict[name]) for name in self._live_names() if name in self._dict] + + def get(self, key, default=None): + try: + return self[key] + except (KeyError, IndexError): + return default + + def append(self, value): + if not hasattr(value, "name"): + raise AttributeError( + 'Object %s does not have a "name" attribute and cannot be stored.' % value) + name = value.name + if name in self._dict: + raise ContainerAlreadyContains( + "Container '%s' already contains an object with name '%s'." % (self, value.name)) + self._dict[name] = value + + def extend(self, values): + for value in values: + self.append(value) + + def update_key(self, key): + """Re-files an already-stored item under its current .name. + + Called by Variable.name's and Constraint.name's setters (in + optlang.interface) right after an attached object's name changes, so + this dict's key stays in sync with the object it points at. + """ + item = self._dict.get(key) + if item is None: + return + name = item.name + if key != name: + self._dict[name] = item + del self._dict[key] + + def __getstate__(self): + return list(self) + + def __setstate__(self, obj_list): + # Unlike optlang.container.Container, this can't just replay + # self.__init__(obj_list): a live HiGHS-backed container needs its + # owning Model, which isn't part of the pickled state. + # Reconstructing the Model rebuilds this instead. + raise NotImplementedError( + "%s is backed by a live HiGHS instance and cannot be unpickled on " + "its own - restore the owning Model instead." % type(self).__name__ + ) + + def __getattr__(self, name): + try: + return self[name] + except KeyError: + raise AttributeError("'%s' object has no attribute %s" % (self, name)) + + def __dir__(self): + attributes = list(self.__class__.__dict__.keys()) + attributes.extend(self._dict.keys()) + return attributes + + +class HighsVariablesContainer(_HighsBackedContainer): + """model.variables, backed by HiGHS's live column order/names.""" + + def _num(self): + return self._model.problem.getNumCol() + + def _name_at(self, idx): + return self._model.problem.variableName(idx) + + +class HighsConstraintsContainer(_HighsBackedContainer): + """model.constraints, backed by HiGHS's live row order/names.""" + + def _num(self): + return self._model.problem.getNumRow() + + def _name_at(self, idx): + _, name = self._model.problem.getRowName(idx) + return name + + +class Model(interface.Model): + def __init__(self, problem=None, *args, **kwargs): + self.problem = None + self._has_solution = False + self._solution_col_value = None + self._solution_col_dual = None + self._solution_row_value = None + self._solution_row_dual = None + self._objective_value = None + self._last_objective_was_quadratic = False + super(Model, self).__init__(problem=problem, *args, **kwargs) + + def _initialize_problem(self): + self.problem: highspy.Highs = highspy.Highs() + self.problem.setOptionValue("output_flag", False) + # Replace the base class's plain Containers (created before this + # point, so still empty here) with ones backed by this Highs + # instance's live column/row order. + self._variables = HighsVariablesContainer(self) + self._constraints = HighsConstraintsContainer(self) + + def _initialize_model_from_problem(self, problem: highspy.Highs): + """ + Copies an already-populated highspy.Highs instance, reconstructing + optlang Variable/Constraint/Objective wrappers for its current + columns, rows and cost vector. + """ + if not isinstance(problem, highspy.Highs): + raise TypeError( + "Provided problem must be a highspy.Highs instance, not %s." % type(problem)) + + # self.problem = problem + # self._variables = HighsVariablesContainer(self) + # self._constraints = HighsConstraintsContainer(self) + + # lp = problem.getLp() + + self._initialize_problem() # create a new highspy.Highs problem + self.problem.passModel(problem.getModel()) # copy the source problem + lp = self.problem.getLp() + problem = self.problem + + inf = highspy.kHighsInf + + # Variables: one wrapper per existing column, bounds, integrality + # and index taken straight from the live problem. + integrality = getattr(lp, "integrality_", None) + for i in range(problem.getNumCol()): + name = problem.variableName(i) + lb = float(lp.col_lower_[i]) + ub = float(lp.col_upper_[i]) + is_integer = (integrality is not None and len(integrality) > i and + int(integrality[i]) == int(highspy.HighsVarType.kInteger)) + if is_integer: + var_type = "binary" if (lb == 0.0 and ub == 1.0) else "integer" + else: + var_type = "continuous" + variable = Variable( + name, + lb=None if lb <= -inf else lb, + ub=None if ub >= inf else ub, + type=var_type, + ) + # Raw assignment, not variable.problem = self: mirrors + # _add_variables/the rest of this file, and (unlike Constraint's + # or Objective's `problem`) Variable.problem is a plain + # attribute with no side effects, so either would work - this + # just keeps the pattern identical across all three. + variable._solver_index = i + variable.problem = self + self._variables.append(variable) + self._variables_to_constraints_mapping[name] = set() + + # Constraints: one wrapper per existing row. Built the same way + # Constraint.__init__ always is elsewhere in this file - constructed + # unattached, with _solver_index and _problem only patched in + # afterwards. Constraint._get_expression touches self._solver_index, + # so constructing it directly with problem=self here (before that + # attribute exists) would blow up; this order avoids that. + for i in range(problem.getNumRow()): + _, name = problem.getRowName(i) + _, row_lb, row_ub, _ = problem.getRow(i) + _, idx, val = problem.getRowEntries(i) + # terms = [float(v) * self._variables[problem.variableName(int(j))] for j, v in zip(idx, val)] + # expression = symbolics.add(terms) if terms else symbolics.sympify(0) + # constraint = Constraint( + constraint = Constraint( + 0, # do not set up explicit expression here, can be constructed on demand + lb=None if row_lb <= -inf else float(row_lb), + ub=None if row_ub >= inf else float(row_ub), + name=name, + sloppy=True, + ) + constraint._expression_expired = True + constraint._solver_index = i + constraint._problem = self + self._constraints.append(constraint) + for j in idx: + var_name = problem.variableName(int(j)) + self._variables_to_constraints_mapping[var_name].add(name) + + # Objective: linear part only, see docstring. Same construction + # order rationale as Constraint above - Objective._get_expression + # reads self._quadratic_coeffs, which does not exist until after + # __init__ returns, so this too is built unattached first. + obj_expr, sense = problem.getObjective() + linear_coeffs = {} + terms = [] + for i, v in zip(obj_expr.idxs, obj_expr.vals): + var_name = problem.variableName(int(i)) + linear_coeffs[var_name] = float(v) + terms.append(float(v) * self._variables[var_name]) + constant = float(obj_expr.constant or 0.0) + if constant: + terms.append(symbolics.Real(constant)) + expression = symbolics.add(terms) if terms else symbolics.sympify(0) + direction = "max" if sense == highspy.ObjSense.kMaximize else "min" + + objective = Objective(expression, direction=direction, sloppy=True) + objective._initial_linear_coeffs = linear_coeffs + objective._quadratic_coeffs = {} + objective._constant = constant + objective._problem = self + self._objective = objective + + def _build_hessian_data(self, quadratic_coeffs, num_col): + """ + Helper to build Hessian matrix data (start, index, value) for HiGHS. + Returns (q_start, q_index, q_value) or (None, None, None) if empty. + """ + if not quadratic_coeffs: + return None, None, None + + q_start = np.zeros(num_col + 1, dtype=np.int32) + q_index_list = [] + q_value_list = [] + q_col_entries = [[] for _ in range(num_col)] + + for (v1_name, v2_name), coeff in quadratic_coeffs.items(): + v1 = self._variables.get(v1_name) + v2 = self._variables.get(v2_name) + if v1 is None or v2 is None: + continue + i = v1._solver_index + j = v2._solver_index + + # Lower Triangular (i >= j) + if i < j: + i, j = j, i + + val = coeff + if i == j: + val = 2.0 * coeff + + q_col_entries[j].append((i, val)) + + # Sort and build CSC arrays + pos = 0 + for j in range(num_col): + q_start[j] = pos + q_col_entries[j].sort(key=lambda x: x[0]) + for (row_idx, value) in q_col_entries[j]: + q_index_list.append(row_idx) + q_value_list.append(value) + pos += 1 + q_start[num_col] = pos + + return q_start, np.array(q_index_list, dtype=np.int32), np.array(q_value_list, dtype=np.double) + + def _update_linear_objective(self): + """ + Fully (re)seeds the linear part (costs, offset, sense) of the objective + in HiGHS. This only runs when the whole Objective object is (re)assigned + via the `model.objective = ...` setter, so wiping all costs first and + seeding from the new objective's bootstrap coefficients is intentional - + it's a full replace, not an incremental update. Incremental updates go + through Objective.set_coefficients / _highs_set_objective_coefficients + instead, which only ever touch the specific columns being changed. + """ + if self.problem is None or self.objective is None: + return + + n = self.problem.getNumCol() + if n > 0: + self.problem.changeColsCost(n, np.arange(n, dtype=np.int32), np.zeros(n, dtype=np.double)) + + # Re-derive from the objective's own (unattached-side) expression + # rather than trusting _initial_linear_coeffs/_constant verbatim: + # those are snapshotted once at Objective.__init__ time, but + # +=/-=/*= (OptimizationExpression's __iadd__/__isub__/__imul__) + # mutate _expression directly and have no way to know they should + # keep that snapshot in sync. Re-parsing here, at the one place the + # snapshot is actually consumed, keeps a mutated-before-attachment + # Objective (e.g. `obj = Objective(x); obj += 2 * y; model.objective + # = obj`) working correctly without having to intercept every + # mutating operator individually. + linear_coeffs, _, obj_constant = _separate_linear_and_quadratic_from_expr(self.objective._expression) + self.objective._initial_linear_coeffs = linear_coeffs + self.objective._constant = obj_constant + + for var_name, coeff in linear_coeffs.items(): + var = self._variables.get(var_name) + if var is not None: + self.problem.changeColCost(var._solver_index, float(coeff)) + + self.problem.changeObjectiveOffset(float(obj_constant)) + self._highs_set_objective_sense(self.objective.direction) + + def _update_quadratic_objective(self): + """Updates the quadratic part (Hessian) of the objective in HiGHS.""" + if self.problem is None or self.objective is None: + return + + # Same rationale as the _highs_set_* helpers below: quadratic terms + # may reference variables added but not yet flushed. + self.update() + + quadratic_coeffs = self.objective._quadratic_coeffs + num_col = self.problem.getNumCol() + + q_start, q_index, q_value = self._build_hessian_data(quadratic_coeffs, num_col) + + if q_start is not None: + hessian = highspy.HighsHessian() + hessian.dim_ = num_col + hessian.format_ = highspy.HessianFormat.kTriangular + hessian.start_ = q_start + hessian.index_ = q_index + hessian.value_ = q_value + self.problem.passHessian(hessian) + else: + # Clear the Hessian if no quadratic terms + empty_hessian = highspy.HighsHessian() + empty_hessian.dim_ = num_col + empty_hessian.format_ = highspy.HessianFormat.kTriangular + empty_hessian.start_ = np.zeros(num_col + 1, dtype=np.int32) + empty_hessian.index_ = np.array([], dtype=np.int32) + empty_hessian.value_ = np.array([], dtype=np.double) + self.problem.passHessian(empty_hessian) + + self._last_objective_was_quadratic = len(quadratic_coeffs) > 0 + + def _sync_objective_to_solver(self): + """Sync the current objective's internal state to the HiGHS solver.""" + self._update_linear_objective() + self._update_quadratic_objective() + + @interface.Model.objective.setter + def objective(self, value): + interface.Model.objective.fset(self, value) + if self.problem is not None: + self._sync_objective_to_solver() + + def _add_variables(self, variables): + # Same as interface.Model._add_variables, minus its + # _variables_to_constraints_mapping bookkeeping: HiGHS's own row data + # is the sole source of truth for which constraints reference which + # variable (see Constraint._get_expression / get_linear_coefficients, + # and _remove_variables below), so no separate mapping is kept here. + for variable in variables: + self._variables.append(variable) + variable.problem = self + + inf = highspy.kHighsInf + for variable in variables: + lb = -inf if variable.lb is None else variable.lb + ub = inf if variable.ub is None else variable.ub + vtype = (highspy.HighsVarType.kInteger if variable.type in ("integer", "binary") + else highspy.HighsVarType.kContinuous) + # name=... is required here: every read path (Constraint/Objective + # .expression, _constraint_to_coeffs) maps a + # HiGHS column index back to an optlang variable name via + # variableName(idx), which raises if the column was never named. + self.problem.addVariable(lb, ub, type=vtype, name=variable.name) + # New columns are always appended at the end, so the newly added + # variable's index is simply the last column. + variable._solver_index = self.problem.getNumCol() - 1 + + def _add_constraints(self, constraints, sloppy=False): + # Same as interface.Model._add_constraints, minus its + # _variables_to_constraints_mapping bookkeeping -- not needed here + # for the same reason as in _add_variables above. + for constraint in constraints: + if sloppy is False: + variables = constraint.variables + if constraint.indicator_variable is not None: + variables.add(constraint.indicator_variable) + missing_vars = [var for var in variables if var.problem is not self] + if len(missing_vars) > 0: + self._add_variables(missing_vars) + self._constraints.append(constraint) + constraint._problem = self + + inf = highspy.kHighsInf + for constraint in constraints: + coeffs, lb, ub = self._constraint_to_coeffs(constraint) + row_lb = -inf if lb is None else lb + row_ub = inf if ub is None else ub + indices = np.array([self._variables[name]._solver_index for name in coeffs.keys()], dtype=np.int32) + values = np.array(list(coeffs.values()), dtype=np.double) + self.problem.addRow(row_lb, row_ub, len(indices), indices, values) + # New rows are always appended at the end, so the newly added + # constraint's index is simply the last row. + row_index = self.problem.getNumRow() - 1 + constraint._solver_index = row_index + # addRow (unlike addVariable) has no name kwarg, so name the row + # in a separate call. Needed by variableName's row counterpart + # (getRowName) used in _constraint_to_coeffs and + # HighsConstraintsContainer. + self.problem.passRowName(row_index, constraint.name) + + @staticmethod + def _rows_referencing_columns(highs: "highspy.Highs", col_indices) -> set: + """Returns a sorted list of unique row indices containing any of the given variable indices.""" + matching_rows = set() + + for col in col_indices: + # getColEntries abstracts away CSR/CSC storage formats + status, indices, _ = highs.getColEntries(col) + + if status == highspy.HighsStatus.kOk: + matching_rows.update(indices) + + return matching_rows + + def _remove_variables(self, variables): + for variable in variables: + try: + self._variables[variable.name] + except KeyError: + raise LookupError("Variable %s not in solver" % variable.name) + + removed_names = set(variable.name for variable in variables) + removed_indices = sorted(variable._solver_index for variable in variables) + old_num_col = self.problem.getNumCol() + + # Find exactly which constraints reference any of the columns about + # to be deleted -- must happen before deleteVars, since that call + # rewrites the matrix and invalidates these column indices. This + # replaces the old _variables_to_constraints_mapping lookup below. + affected_rows = self._rows_referencing_columns(self.problem, removed_indices) + + # Physically delete the columns from HiGHS, not just from our own + # bookkeeping. This is essential under the ground-truth design: + # Constraint/Objective now read their linear coefficients straight + # from HiGHS's live row/cost data rather than from a local cache, so + # if the column itself is left in place, every row that referenced it + # keeps reporting it - even though our _variables container has + # already forgotten about it. Deleting the column here lets HiGHS + # itself cascade the removal through every row and the objective, + # exactly like GLPK's glp_del_cols. + self.problem.deleteVars(len(removed_indices), removed_indices) + + # HiGHS shifts every remaining column's index down to close the gaps + # left by the deleted ones - shift the remaining variables' own + # _solver_index attributes to match. But if every removed column was + # already at the tail (the common case: variables added and then + # removed again within the same `with model:` block, e.g. slack + # variables in core.make_scenario_feasible), there are no gaps to + # close below them and nothing to shift - skip the loop entirely. + # (Sorted, unique indices whose smallest value equals + # old_num_col - len(removed_indices) can only be the top + # len(removed_indices) indices; there's no other way to fit that + # many distinct values into that range.) + if removed_indices[0] != old_num_col - len(removed_indices): + for variable in self._variables: + if variable.name in removed_names: + continue + shift = sum(1 for r in removed_indices if r < variable._solver_index) + variable._solver_index -= shift + + # Constraint caches its expression (_expression_expired) -- a + # constraint referencing a removed variable has a stale cache the + # moment that variable's column disappears from its row, so mark it + # expired. affected_rows (computed above, before deleteVars) already + # pins down exactly which rows those were; resolve each row index + # back to its constraint via getRowName + self._constraints (an O(1) + # dict lookup by name) rather than scanning every constraint in the + # model, and rather than any _variables_to_constraints_mapping. + for row_index in affected_rows: + _, row_name = self.problem.getRowName(row_index) + constraint = self._constraints.get(row_name) + if constraint is not None: + constraint._expression_expired = True + + for variable in variables: + variable.problem = None + del self._variables[variable.name] + + # The objective's quadratic part is still cached locally (see + # Objective's docstring - no live Hessian read-back is available yet), + # so it's the one remaining piece that needs explicit pruning: a + # lingering quadratic term referencing a removed variable would break + # _build_hessian_data the next time the objective is rebuilt. + if self.objective is not None and self.objective._quadratic_coeffs: + self.objective._quadratic_coeffs = { + k: v for k, v in self.objective._quadratic_coeffs.items() + if k[0] not in removed_names and k[1] not in removed_names + } + + def _remove_constraints(self, constraints): + for constraint in constraints: + try: + self._constraints[constraint.name] + except KeyError: + raise LookupError("Constraint %s not in solver" % constraint) + + removed_names = set(constraint.name for constraint in constraints) + removed_indices = sorted(constraint._solver_index for constraint in constraints) + old_num_row = self.problem.getNumRow() + + # Mirrors _remove_variables: physically delete the rows from HiGHS + # and let it cascade the reindexing, rather than rebuilding the + # whole LP from scratch. Removing rows doesn't touch any column, so + # unlike variable removal there's no Hessian/objective cache to + # prune here - constraints don't appear in any other object's cache. + self.problem.deleteRows(len(removed_indices), removed_indices) + + # Same end-of-range shortcut as _remove_variables: if every removed + # row was already at the tail, nothing below them moves. + if removed_indices[0] != old_num_row - len(removed_indices): + for constraint in self._constraints: + if constraint.name in removed_names: + continue + shift = sum(1 for r in removed_indices if r < constraint._solver_index) + constraint._solver_index -= shift + + for constraint in constraints: + constraint.problem = None + del self._constraints[constraint.name] + + def _constraint_bounds(self, constraint: Constraint): + lb, ub = constraint.lb, constraint.ub + if lb is not None: + lb = lb - constraint._constant + if ub is not None: + ub = ub - constraint._constant + return lb, ub + + def _constraint_to_coeffs(self, constraint: Constraint): + lb, ub = self._constraint_bounds(constraint) + + row_index = constraint._solver_index + if row_index is not None: + # The row already exists in HiGHS, which is the source of truth + # for its own coefficients - read them back directly rather than + # trusting any local copy (there isn't one anymore anyway). + _, idx, val = self.problem.getRowEntries(row_index) + coeffs = { + self.problem.variableName(int(i)): float(v) + for i, v in zip(idx, val) + } + else: + # Constraint not yet added to this model - use its bootstrap + # coefficients (this is the path _add_constraints takes). + coeffs = dict(constraint._initial_coeffs) + + return coeffs, lb, ub + + def _highs_set_col_bounds(self, variable): + # Flush any pending add()s first: a variable can have its lb/ub + # setter (or set_bounds) called right after being added to a model, + # before update() has run to assign it a _solver_index. Without this, + # changeColBounds would be called with index None. update() is a + # cheap no-op when there is nothing pending, so this costs nothing + # in the common case where the variable was already committed. + self.update() + inf = highspy.kHighsInf + lb = -inf if variable.lb is None else variable.lb + ub = inf if variable.ub is None else variable.ub + self.problem.changeColBounds(variable._solver_index, lb, ub) + + def _highs_set_col_type(self, variable): + # Flush any pending add()s first - same rationale as + # _highs_set_col_bounds: a variable's type can be changed right + # after it was added to a model, before update() has assigned it a + # _solver_index. update() only ever touches pending + # add/remove-variable/constraint bookkeeping here (variable type + # changes never go through self._pending_modifications - they are + # written straight through below, exactly like bounds/coefficients + # elsewhere in this file), so this can never trigger another call + # back into this method or into the type setter that called us. + inf = highspy.kHighsInf + # Push bounds too: interface.Variable.type.fset's 'binary' branch + # sets self._lb/self._ub directly (bypassing the lb/ub property + # setters and therefore _highs_set_col_bounds), so HiGHS would + # otherwise miss that change. Re-sending the (by now settled) + # bounds here is a cheap no-op for every other case. + lb = -inf if variable.lb is None else variable.lb + ub = inf if variable.ub is None else variable.ub + self.problem.changeColBounds(variable._solver_index, lb, ub) + vtype = (highspy.HighsVarType.kInteger if variable.type in ("integer", "binary") + else highspy.HighsVarType.kContinuous) + self.problem.changeColIntegrality(variable._solver_index, vtype) + + def _highs_set_row_bounds(self, constraint): + # See _highs_set_col_bounds -- same rationale, for constraint rows. + self.update() + inf = highspy.kHighsInf + lb, ub = self._constraint_bounds(constraint) + row_lb = -inf if lb is None else lb + row_ub = inf if ub is None else ub + self.problem.changeRowBounds(constraint._solver_index, row_lb, row_ub) + + def _highs_set_col_name(self, variable): + # Same flush rationale as _highs_set_col_bounds. + self.update() + self.problem.passColName(variable._solver_index, variable.name) + + def _highs_set_row_name(self, constraint): + # Same flush rationale as _highs_set_col_bounds. + self.update() + self.problem.passRowName(constraint._solver_index, constraint.name) + + def _highs_set_coefficients(self, constraint, coefficients): + # Same rationale as _highs_set_col_bounds: coefficients may reference + # a variable that was just add()ed but not yet update()d, so its + # _solver_index would still be None. Constraint.set_linear_coefficients + # already resolves variables through the auto-updating `variables` + # property in its default (non-sloppy) path, making this redundant + # there -- but the sloppy=True path bypasses that resolution + # entirely and calls straight through to here, so this is the one + # place that also has to hold for that path to be safe by default. + self.update() + i = constraint._solver_index + for var, coeff in coefficients.items(): + j = var._solver_index + self.problem.changeCoeff(i, j, float(coeff)) + + def _highs_set_objective_coefficients(self, coefficients): + # Unlike Constraint.set_linear_coefficients' default path, Objective's + # linear coefficient path does not resolve variables through the + # auto-updating `variables` property before reaching here -- so this + # is the one place that needs to force freshness for it. + self.update() + for var, coeff in coefficients.items(): + self.problem.changeColCost(var._solver_index, float(coeff)) + + def _highs_set_objective_sense(self, direction): + self.problem.changeObjectiveSense( + highspy.ObjSense.kMaximize if direction == "max" else highspy.ObjSense.kMinimize + ) + + def _optimize(self): + h = self.problem + h.run() + + status_str = h.modelStatusToString(h.getModelStatus()) + status = _HIGHS_STATUS_TO_STATUS.get(status_str, interface.UNDEFINED) + + self._has_solution = status in _STATUSES_WITH_USABLE_SOLUTION + self._solution_col_value = None + self._solution_col_dual = None + self._solution_row_value = None + self._solution_row_dual = None + self._objective_value = None + + return status + + @property + def is_integer(self): + return any(int_type != highspy.HighsVarType.kContinuous for int_type in self.problem.getLp().integrality_) + + def _ensure_solution_arrays(self): + if self._solution_col_value is not None: + return + solution = self.problem.getSolution() + self._solution_col_value = list(solution.col_value) + self._solution_row_value = list(solution.row_value) + if not self.is_integer: + self._solution_row_dual = list(solution.row_dual) + self._solution_col_dual = list(solution.col_dual) + + def _get_primal_values(self): + if not self._has_solution: + return None + self._ensure_solution_arrays() + return self._solution_col_value + + def _get_reduced_costs(self): + if not self._has_solution: + return None + if self.is_integer: + raise ValueError("Dual values are not well-defined for integer problems") + self._ensure_solution_arrays() + return self._solution_col_dual + + def _get_constraint_values(self): + if not self._has_solution: + return None + self._ensure_solution_arrays() + return self._solution_row_value + + def _get_shadow_prices(self): + if not self._has_solution: + return None + if self.is_integer: + raise ValueError("Dual values are not well-defined for integer problems") + self._ensure_solution_arrays() + return self._solution_row_dual + + def _variable_primal(self, variable): + if not self._has_solution: + return None + self._ensure_solution_arrays() + return self._solution_col_value[variable._solver_index] + + def _variable_dual(self, variable): + if not self._has_solution: + return None + if self.is_integer: + raise ValueError("Dual values are not well-defined for integer problems") + self._ensure_solution_arrays() + return float(self._solution_col_dual[variable._solver_index]) + + def _constraint_primal(self, constraint): + if not self._has_solution: + return None + self._ensure_solution_arrays() + return float(self._solution_row_value[constraint._solver_index]) + + def _constraint_dual(self, constraint): + if not self._has_solution: + return None + if self.is_integer: + raise ValueError("Dual values are not well-defined for integer problems") + self._ensure_solution_arrays() + return float(self._solution_row_dual[constraint._solver_index]) + + def _get_objective_value(self): + if not self._has_solution: + return None + if self._objective_value is None: + info = self.problem.getInfo() + self._objective_value = float(info.objective_function_value) + return self._objective_value + +__all__ = ["Variable", "Constraint", "Objective", "Configuration", "Model", "_get_quadratic_terms"] diff --git a/cnapy/optmdfpathway.py b/cnapy/optmdfpathway.py new file mode 100644 index 00000000..05359561 --- /dev/null +++ b/cnapy/optmdfpathway.py @@ -0,0 +1,1968 @@ +""" +optmdfpathway.py +================= + +A cobrapy / optlang re-implementation of CellNetAnalyzer's OptMDFpathway +MILP (Haedicke et al., 2018, PLOS Comput Biol 14:e1006492), ported from the +CPLEX-based CNA implementation (CNAcomputeOptMDFpathway.m, +setup_driving_force_constraints.m, driving_forces.m). + +Thermodynamic and concentration data live directly on the cobrapy model as +annotations (all optional; anything missing just falls back to a default / +gets no thermodynamic constraint): + + reaction.annotation['dG0'] standard Gibbs energy of reaction, + delta_r G'0, as a number or a + (value, uncertainty) pair. Reactions + without this annotation may still carry + flux but get no driving-force + constraint. + metabolite.annotation['Cmin'] per-metabolite concentration lower bound + metabolite.annotation['Cmax'] per-metabolite concentration upper bound + (in M); metabolites without these fall + back to the Cmin/Cmax function + arguments. + +``optMDFpathway`` finds a flux distribution v and metabolite +log-concentrations x = log(c) such that + + S v = 0 + lb <= v <= ub (plus any linear scenario constraints, see below) + log(Cmin) <= x <= log(Cmax) + ratio_min <= exp(x_i - x_j) <= ratio_max (optional; a fixed ratio is + the special case + ratio_min == ratio_max) + +and, among all reactions that carry (nonzero) flux, the smallest +thermodynamic driving force + + df_r = -delta_r G'(x) = -( dG0_r + RT * sum_m S[m, r] * x_m ) + +is maximised (the classical max-min driving force, MDF -- Noor et al. 2014). +Unlike plain MDF, the pathway itself is a decision variable here +(OptMDFpathway), not fixed in advance. + +Because "the MDF bound only has to hold for reactions that are actually +used" is a disjunctive condition, this needs a MILP: a binary indicator is +introduced for every candidate reaction direction and linked to the flux +through a big-M constraint, and the driving-force bound is only imposed +when that indicator is 1. Like the original CNA implementation, +``optMDFpathway`` runs an FVA preprocessing pass (via +core.multi_threaded_HiGHS_FVA) first, both to tighten those big-M values +and to prune reactions that are structurally blocked given the model and +any Scenario constraints. + +Additional flux constraints -- fixed uptake rates, a fixed growth rate, a +minimal yield requirement, and so on -- are supplied via ``scenarios``: +either raw (expression, constraint_type, rhs) triples for arbitrary linear +constraints over reaction fluxes, or cnapy Scenario-like objects +(reaction_id -> (lb, ub) mappings, the same shape +core.make_scenario_feasible / element_exchange_balance consume), which are +normalised into the same triple form. These triples are used directly both +as core.multi_threaded_HiGHS_FVA's ``constraints`` argument and as ordinary +linear constraints added to the MILP -- no separate D/d matrix needed. + +Strictly (fully) flux-coupled reaction groups -- e.g. from flux coupling +analysis -- can be supplied via ``reaction_subsets`` to speed up both +stages: FVA preprocessing runs on a compressed model with one reaction per +group (see _compress_for_fva), and the MILP shares one pair of binaries and +one flux-linking constraint per group instead of per reaction. Each +member's own driving-force constraint is still built individually -- +thermodynamics is inherently per-reaction -- just gated by the group's +shared binaries. + +For solving more than once against the same model/constraints -- most +importantly, iterative thermodynamic-bottleneck analysis (solve, find the +current minimal bottleneck, relax exactly its driving-force constraint(s), +repeat for as many iterations as the caller wants) -- use OptMDFAnalysis +directly instead of optMDFpathway: it builds the model copy, FVA, and MILP +exactly once, and every subsequent solve()/find_bottleneck()/relax() call +only touches what actually changed. + +Public API: + + OptMDFAnalysis(...) stateful solver: build once, call .solve(), + .shadow_prices(), .find_bottleneck(), + .relax(...), .step(), or .run() as needed. + optMDFpathway(...) one-shot wrapper around OptMDFAnalysis, for + callers that just want a single solve. + driving_force_ranges(...) per-reaction min/max driving force given only + the concentration bounds (no flux coupling), + analogous to CNA's driving_forces.m. A + reaction only needs an LP when two or more of + its own metabolites participate in a + concentration_ratios constraint; otherwise + the range is computed analytically, exactly + as driving_forces.m does. In particular, if + concentration_ratios is empty (the common + case), no LPs are run at all. + +Both OptMDFAnalysis and driving_force_ranges operate on an internal copy of +the model, so the model passed in is never modified. + +Performance note +----------------- +Every constraint and the objective are built from a placeholder zero +expression and added to the solver *before* their real coefficients are +attached via ``set_linear_coefficients``. That method talks directly to the +solver's constraint matrix (e.g. glp_set_mat_row for GLPK, the CPLEX/Gurobi +C APIs otherwise) and skips building/parsing a sympy expression tree +entirely. For models with many reactions and metabolites this is +substantially cheaper than constructing constraints as +``sum(coeff * variable for ...)`` expressions, which is what the +straightforward optlang usage would do. The one wrinkle is that +``set_linear_coefficients`` only works on variables/constraints/objectives +that are already attached to the solver *and* have been flushed there via +``solver.update()`` (additions are batched lazily), so every call site here +follows the pattern: build with ``Zero`` -> ``solver.add(...)`` -> +``solver.update()`` -> ``set_linear_coefficients(...)``. The same pattern is +used for driving_force_ranges' (occasional) per-reaction LPs: one Objective +is created once and its coefficients patched per reaction, rather than +rebuilding the Objective from scratch on every iteration. +""" + +from __future__ import annotations + +import contextlib +import math +from dataclasses import dataclass, field +from typing import ( + Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, + TYPE_CHECKING, Union, +) +import time + +import cobra +from optlang.symbolics import Zero +from cnapy.core import multi_threaded_HiGHS_FVA + +if TYPE_CHECKING: + # cnapy.appdata.Scenario is duck-typed here (a dict-like reaction_id -> + # (lb, ub) mapping, optionally with an add_scenario_reactions_to_model + # method) rather than imported for real, so this module doesn't need the + # cnapy/efmtool_link/CPLEX/Gurobi stack just to be importable. + from cnapy.appdata import Scenario + +Number = Union[int, float] +DG0Spec = Union[Number, Tuple[Number, Number]] # value, or (value, uncertainty) + + +# --------------------------------------------------------------------------- +# Result container +# --------------------------------------------------------------------------- + +@dataclass +class OptMDFResult: + """Result of an optMDFpathway() call.""" + + status: str + mdf: Optional[float] = None + fluxes: Dict[str, float] = field(default_factory=dict) + concentrations: Dict[str, float] = field(default_factory=dict) + driving_forces: Dict[str, float] = field(default_factory=dict) + driving_force_duals: Dict[str, float] = field(default_factory=dict) + bottleneck_reactions: List[str] = field(default_factory=list) + objective_value: Optional[float] = None + + def __repr__(self) -> str: # pragma: no cover - cosmetic + if self.mdf is None: + return f"" + return f"" + + +# --------------------------------------------------------------------------- +# small helpers +# --------------------------------------------------------------------------- + +def _as_bound_dict(value: Union[Number, Dict[str, Number]], + metabolite_ids: Iterable[str]) -> Dict[str, float]: + """Turn a scalar-or-dict concentration bound spec into a full dict.""" + if isinstance(value, dict): + return {k: float(v) for k, v in value.items()} + return {mid: float(value) for mid in metabolite_ids} + + +def _split_G0(spec: DG0Spec) -> Tuple[float, float]: + """Unpack a dG0 entry into (value, uncertainty).""" + if isinstance(spec, (tuple, list)): + return float(spec[0]), float(spec[1]) + return float(spec), 0.0 + + +def _reaction_dG0(rxn: cobra.Reaction) -> Optional[Tuple[float, float]]: + """ + Read a reaction's standard Gibbs energy from annotation['dG0'] (a + number, or a (value, uncertainty) pair). Returns None -- meaning "no + thermodynamic constraint for this reaction" -- if the annotation is + absent or NaN. + """ + spec = rxn.annotation.get("dG0") + if spec is None: + return None + val, unc = _split_G0(spec) + if math.isnan(val): + return None + return val, unc + + +def _resolve_concentration_bounds( + model: cobra.Model, + Cmin: Union[Number, Dict[str, Number]], + Cmax: Union[Number, Dict[str, Number]], +) -> Tuple[Dict[str, float], Dict[str, float]]: + """ + Per-metabolite concentration bounds (in M): metabolite.annotation + ['Cmin']/['Cmax'] take precedence when present; everything else falls + back to the Cmin/Cmax arguments (a scalar applies to every metabolite, + a dict supplies specific defaults). + """ + met_ids = [met.id for met in model.metabolites] + Cmin_d = _as_bound_dict(Cmin, met_ids) + Cmax_d = _as_bound_dict(Cmax, met_ids) + for met in model.metabolites: + cmin_annot = met.annotation.get("Cmin") + if cmin_annot is not None: + Cmin_d[met.id] = float(cmin_annot) + cmax_annot = met.annotation.get("Cmax") + if cmax_annot is not None: + Cmax_d[met.id] = float(cmax_annot) + missing = [mid for mid in met_ids if mid not in Cmin_d or mid not in Cmax_d] + if missing: + raise ValueError( + "Missing concentration bounds for metabolites: " + f"{missing[:10]}{' ...' if len(missing) > 10 else ''}" + ) + return Cmin_d, Cmax_d + + +def _normalize_concentration_ratios( + concentration_ratios: Optional[Sequence[Sequence[Any]]], +) -> List[Tuple[str, str, float, float]]: + """ + Normalize concentration_ratios entries into (met_i, met_j, ratio_min, + ratio_max) tuples constraining concentration_i / concentration_j to + [ratio_min, ratio_max]. Each entry may be given as: + (met_i, met_j, ratio) -- a fixed ratio + (met_i, met_j, ratio_min, ratio_max) -- a ratio range + """ + out: List[Tuple[str, str, float, float]] = [] + for entry in concentration_ratios or []: + if len(entry) == 3: + mi, mj, ratio = entry + out.append((mi, mj, float(ratio), float(ratio))) + elif len(entry) == 4: + mi, mj, lo, hi = entry + out.append((mi, mj, float(lo), float(hi))) + else: + raise ValueError( + "Invalid concentration_ratios entry (expected length 3 for " + f"a fixed ratio or 4 for a range): {entry!r}") + return out + + +ConstraintTriple = Tuple[Dict[str, float], str, float] +ScenarioConstraint = Union["Scenario", ConstraintTriple] + + +def _is_constraint_triple(item: Any) -> bool: + return (isinstance(item, tuple) and len(item) == 3 + and isinstance(item[0], dict) and isinstance(item[1], str)) + + +def _as_constraint_list( + scenario_constraints: Optional[Union[ScenarioConstraint, Iterable[ScenarioConstraint]]], +) -> List[ScenarioConstraint]: + if scenario_constraints is None: + return [] + if _is_constraint_triple(scenario_constraints): + return [scenario_constraints] + if hasattr(scenario_constraints, "items"): # a single dict-like Scenario + return [scenario_constraints] + return list(scenario_constraints) + + +def scenario_constraints_to_triples( + scenarios: Optional[Union[ScenarioConstraint, Iterable[ScenarioConstraint]]], + m: cobra.Model, +) -> List[ConstraintTriple]: + """ + Normalize a heterogeneous list of scenario constraints into + (expression, constraint_type, rhs) triples -- exactly the format + core.multi_threaded_HiGHS_FVA's ``constraints`` argument expects, so the + same list drives both the FVA preprocessing step and the MILP's own + linear constraints. Each entry may be: + + - a raw triple ({reaction_id: coefficient}, constraint_type, rhs), + with constraint_type in {'=', '<=', '>='} -- for arbitrary linear + constraints over reaction fluxes that a single reaction's bounds + can't express, e.g. a minimal yield requirement (product flux at + least half the substrate uptake magnitude): + ({"EX_product": 1.0, "EX_substrate": 0.5}, '>=', 0.0) + + - a Scenario-like object: a reaction_id -> (lb, ub) flux-bound + mapping, the same shape core.make_scenario_feasible / + element_exchange_balance consume. Any reactions it needs are added + to `m` first via .add_scenario_reactions_to_model (if it has that + method). A fixed bound (lb == ub) becomes a single '=' triple; a + genuine range becomes up to two triples ('>=' / '<=', one per + finite side). + + `m` is mutated in place only via Scenario.add_scenario_reactions_to_model + calls (to register any reactions a Scenario needs); the constraints + themselves are returned as triples, not applied to `m`. + """ + triples: List[ConstraintTriple] = [] + for item in _as_constraint_list(scenarios): + if _is_constraint_triple(item): + expr, ctype, rhs = item + if ctype not in ("=", "<=", ">="): + raise ValueError(f"Unknown constraint type {ctype!r} in {item!r}") + triples.append((dict(expr), ctype, float(rhs))) + else: # a Scenario-like reaction_id -> (lb, ub) mapping + add_scenario_reactions = getattr(item, "add_scenario_reactions_to_model", None) + if add_scenario_reactions is not None: + add_scenario_reactions(m) + for rid, (lb, ub) in item.items(): + expr = {rid: 1.0} + if lb == ub: + triples.append((expr, "=", float(lb))) + else: + if math.isfinite(lb): + triples.append((expr, ">=", float(lb))) + if math.isfinite(ub): + triples.append((expr, "<=", float(ub))) + return triples + + +# --------------------------------------------------------------------------- +# strictly-coupled reaction subsets (flux coupling) +# --------------------------------------------------------------------------- + +def _normalize_reaction_subsets( + reaction_subsets: Optional[Sequence[Sequence[Tuple[str, Number]]]], +) -> Dict[str, Tuple[str, float]]: + """ + Normalize a list of strictly-coupled reaction subsets into a flat + {reaction_id: (representative_id, ratio)} lookup (including the + representative's own entry, mapped to itself with ratio 1.0), where + + flux(reaction_id) == ratio * flux(representative_id) + + holds in every feasible steady state (ratio may be negative, meaning + the reaction runs opposite the representative as oriented in the + model). Reactions not mentioned in any subset simply aren't present in + the returned dict -- callers should treat a missing entry as + (reaction_id, 1.0), i.e. its own singleton subset. + + Each subset is a sequence of (reaction_id, factor) pairs; the first + entry is taken as that subset's representative (factor must be + nonzero), and every other member's ratio to it is factor_i / factor_rep. + A subset with fewer than two entries carries no coupling information + and is ignored. + """ + out: Dict[str, Tuple[str, float]] = {} + for subset in reaction_subsets or []: + subset = list(subset) + if len(subset) < 2: + continue + rep_id, rep_factor = subset[0] + rep_factor = float(rep_factor) + if rep_factor == 0: + raise ValueError(f"Representative reaction {rep_id!r} in a " + "reaction_subsets entry has a zero factor") + for rid, factor in subset: + if rid in out: + raise ValueError( + f"Reaction {rid!r} appears in more than one " + "reaction_subsets entry") + out[rid] = (rep_id, float(factor) / rep_factor) + return out + + +def _compress_for_fva( + m: cobra.Model, + subset_ratio: Dict[str, Tuple[str, float]], + scenario_triples: Sequence[ConstraintTriple], +) -> Tuple[cobra.Model, List[ConstraintTriple]]: + """ + Build a smaller, throwaway model for FVA purposes only: for every + reaction_subsets group, keep just the representative reaction, with its + stoichiometry rebuilt as the ratio-weighted sum of every member's + original stoichiometry (so metabolites shared with reactions outside + the subset keep the correct net coefficient; a metabolite touched only + by members of one subset -- e.g. an intermediate in an unbranched chain + -- automatically ends up with an all-zero row, which is harmless: HiGHS + treats it as a trivially satisfied 0 <= 0 <= 0 constraint) and bounds + tightened to the intersection of every member's own bound (translated + through the group's ratios). The other members are then removed. + scenario_triples referencing a dropped member are rewritten in terms of + its representative (coefficient scaled by the member's ratio). + + This model is for FVA only -- it must never be used for the MILP + itself, since each individual member's own identity (and therefore its + own thermodynamic data) has been thrown away. The original `m` is not + modified; if there is nothing to compress, `m` itself is returned + unchanged (no copy made). + + Returns + ------- + (compressed_model, rewritten_scenario_triples) + """ + groups: Dict[str, List[Tuple[str, float]]] = {} + for member_id, (rep_id, ratio) in subset_ratio.items(): + groups.setdefault(rep_id, []).append((member_id, ratio)) + groups = {rep_id: members for rep_id, members in groups.items() if len(members) > 1} + + if not groups: + return m, list(scenario_triples) + + m_fva = m.copy() + + for rep_id, members in groups.items(): + combined: Dict[str, float] = {} + lo, hi = -math.inf, math.inf + for member_id, ratio in members: + member = m.reactions.get_by_id(member_id) # original (uncompressed) stoichiometry/bounds + for met, coeff in member.metabolites.items(): + combined[met.id] = combined.get(met.id, 0.0) + coeff * ratio + m_lo, m_hi = member.lower_bound, member.upper_bound + if ratio > 0: + cand_lo, cand_hi = m_lo / ratio, m_hi / ratio + else: + cand_lo, cand_hi = m_hi / ratio, m_lo / ratio + lo, hi = max(lo, cand_lo), min(hi, cand_hi) + if lo > hi: + raise ValueError( + f"reaction_subsets entry for representative {rep_id!r} has " + f"inconsistent bounds/ratios: intersected range is " + f"[{lo}, {hi}]. Check the given factors against each " + "member's actual bounds.") + + rep = m_fva.reactions.get_by_id(rep_id) + rep.subtract_metabolites(dict(rep.metabolites)) + rep.add_metabolites({m_fva.metabolites.get_by_id(mid): coeff + for mid, coeff in combined.items() if coeff != 0.0}) + rep.bounds = (lo, hi) + + other_members = [m_fva.reactions.get_by_id(mid) for mid, _ in members if mid != rep_id] + m_fva.remove_reactions(other_members) + + rewritten: List[ConstraintTriple] = [] + for expr, ctype, rhs in scenario_triples: + new_expr: Dict[str, float] = {} + for rid, coeff in expr.items(): + rep_id, ratio = subset_ratio.get(rid, (rid, 1.0)) + new_expr[rep_id] = new_expr.get(rep_id, 0.0) + coeff * ratio + new_expr = {k: v for k, v in new_expr.items() if v != 0.0} + rewritten.append((new_expr, ctype, rhs)) + + return m_fva, rewritten + + +# --------------------------------------------------------------------------- +# thermodynamic bottleneck search (deletion filter) +# --------------------------------------------------------------------------- + +def _find_minimal_bottleneck( + solver, + df_constraints: Dict[str, Dict[str, Any]], + fluxes: Dict[str, float], + driving_forces: Dict[str, float], + mdf: float, + tol: float = 1e-6, +) -> List[str]: + """ + Grow-Shrink (additive-deletion) search for the complete minimal + thermodynamic bottleneck set(s): every reaction whose driving-force + constraint must be jointly relaxed to allow mdf to improve. + + A naive one-at-a-time deletion filter is not enough here: reactions + tied at mdf can be coupled through a shared limiting metabolite, so + relaxing just one of them can already permit *some* improvement even + though it doesn't capture the full achievable improvement -- masking + that the others are equally necessary. Grow-Shrink avoids this: + + 1. Candidates = reactions tied exactly at mdf (df_r <= mdf + tol). + 2. Relax every candidate at once and confirm this improves mdf at + all (otherwise mdf isn't thermodynamically limited here -- e.g. + it's capped by B_bounds or a scenario constraint instead). + 3. Grow: re-enforce candidates one at a time (in a fixed order), + re-solving after each, until mdf drops back to (at or below) the + original value. The reaction whose re-enforcement causes that + drop is the "trigger": the set enforced at that point contains at + least one complete bottleneck set, possibly plus bystanders that + were enforced along the way but aren't actually necessary. + 4. Shrink: keeping the trigger enforced, walk backward through the + other reactions enforced during Grow, relaxing each in turn -- + if mdf pops back up, that reaction was necessary (re-enforce it + and keep it in the set); if mdf stays at the original value, it + was a bystander (leave it relaxed, it's not part of this set). + 5. The reactions left enforced after Shrink form one irreducible + bottleneck set. Relax all of them for real, drop them from the + candidate pool, and repeat from step 3 with whatever candidates + remain, until either none remain or relaxing everything found so + far already exceeds the original mdf (nothing further to find at + this mdf level). + + Returns the union of every bottleneck set found this way -- relaxing + this whole list is what's needed to guarantee mdf actually improves, + which is what OptMDFAnalysis.relax()/step() rely on. + + Must be called with `solver` already solved as a fixed-binary LP at its + optimum (see optMDFpathway) -- duals/constraint-removal comparisons + aren't meaningful while the solver could still restructure which + reactions are active. + + Note: reactions whose driving-force constraints are algebraically + redundant with each other (e.g. literal isozymes: identical + stoichiometry and dG0) are a known edge case this can still + under-report on, since Grow can trigger on a strict subset that + happens to numerically reproduce the original mdf on its own, before + the redundant partner is ever tested. + """ + def active_constraint(rid: str): + direction = "fwd" if fluxes[rid] >= 0 else "rev" + return df_constraints.get(rid, {}).get(direction) + + tied_cons = { + rid: active_constraint(rid) + for rid, df in driving_forces.items() if df <= mdf + tol + } + tied_cons = {rid: con for rid, con in tied_cons.items() if con is not None} + if not tied_cons: + return [] + + orig_lb = {rid: con.lb for rid, con in tied_cons.items()} + + def set_state(rids, relaxed: bool) -> None: + for rid in rids: + tied_cons[rid].lb = None if relaxed else orig_lb[rid] + + def current_mdf() -> Optional[float]: + status = solver.optimize() + return solver.objective.value if status == "optimal" else None + + # -- Step 2: relax every candidate; bail out if that doesn't even help -- + set_state(tied_cons, relaxed=True) + m0 = current_mdf() + if m0 is None or m0 <= mdf + tol: + set_state(tied_cons, relaxed=False) + solver.optimize() + return [] + + found: List[str] = [] + remaining = list(tied_cons.keys()) + + while remaining: + # -- Step 3 (Grow): re-enforce remaining candidates one at a time + # until mdf drops back to the original value. + enforced_so_far: List[str] = [] + trigger: Optional[str] = None + for rid in remaining: + tied_cons[rid].lb = orig_lb[rid] + enforced_so_far.append(rid) + m = current_mdf() + if m is None or m <= mdf + tol: + trigger = rid + break + if trigger is None: + # Re-enforcing every remaining candidate never dropped mdf back + # down on its own -- shouldn't normally happen (re-enforcing + # all of them reproduces the fully-enforced problem), but stop + # defensively rather than loop forever. + raise RuntimeError("Inconsistent MDFs calculated during bottleneck analysis, aborting.") + + # -- Step 4 (Shrink): keep the trigger enforced; relax the others + # (reverse order of addition) one at a time, re-enforcing any + # whose absence lets mdf rise again. + necessary = {trigger} + for rid in reversed(enforced_so_far): + if rid == trigger: + continue + tied_cons[rid].lb = -1e9 + m = current_mdf() + if m is not None and m > mdf + tol: + tied_cons[rid].lb = orig_lb[rid] + necessary.add(rid) + # else: bystander -- stays relaxed, left in `remaining` in case + # it's needed by a later, independent bottleneck set. + + # -- Step 5: log this set, relax it for real, continue with the rest. + found.extend(sorted(necessary)) + set_state(necessary, relaxed=True) + remaining = [rid for rid in remaining if rid not in necessary] + if not remaining: + break + m = current_mdf() + if m is None or m > mdf + tol: + break # already past the original mdf; nothing further to find + + set_state(tied_cons, relaxed=False) + solver.optimize() # leave the solver back at the original optimum + return found + +# --------------------------------------------------------------------------- +# thermodynamic bottleneck search (reset / grow / shrink) +# --------------------------------------------------------------------------- + +def _find_minimal_bottleneck2( + solver, + df_constraints: Dict[str, Dict[str, Any]], + fluxes: Dict[str, float], + driving_forces: Dict[str, float], + mdf: float, + tol: float = 1e-6, + flux_tol: float = 1e-9, +) -> List[str]: + """Find one inclusion-minimal thermodynamic bottleneck set. + + The search is performed on the LP obtained by fixing the current MILP + binary decisions. It follows a reset/grow/shrink scheme: + + 1. Candidate set: active reactions whose current driving force is within + ``tol`` of the current MDF. + 2. Reset: relax all candidate driving-force constraints. If the MDF does + not improve, the current MDF is not thermodynamically limited. + 3. Grow: re-enforce candidate constraints one at a time until the MDF + returns to the original optimum (within ``tol``). The last reaction + added is the trigger; the enforced set contains at least one complete + bottleneck. + 4. Shrink: repeatedly test removal of every non-trigger member. Keep a + reaction only when its removal lets the MDF rise above the original + optimum. Iterate to a fixed point so the returned set is inclusion + minimal. + + The returned set is *one* inclusion-minimal bottleneck set; its composition + can depend on the order in which candidates are grown. Reactions that + were already permanently relaxed by the caller should be excluded before + entering this function. + + The solver is restored to the original constraints and solved at the end, + regardless of whether the search succeeds or raises. + """ + + def active_constraint(rid: str): + flux = fluxes.get(rid, 0.0) + if abs(flux) <= flux_tol: + return None + direction = "fwd" if flux > 0 else "rev" + return df_constraints.get(rid, {}).get(direction) + + # Only genuinely active reactions tied at the MDF are candidates. This + # matters for zero-flux reactions, whose inactive-direction thermodynamic + # constraint is gated off and therefore cannot be part of the bottleneck. + candidate_constraints = {} + for rid, df in driving_forces.items(): + if df <= mdf + tol: + con = active_constraint(rid) + if con is not None: + candidate_constraints[rid] = con + + if not candidate_constraints: + return [] + + original_lb = {rid: con.lb for rid, con in candidate_constraints.items()} + relaxed_lb = -1e9 + + def set_relaxed(rids: Iterable[str]) -> None: + for rid in rids: + candidate_constraints[rid].lb = relaxed_lb + + def set_enforced(rids: Iterable[str]) -> None: + for rid in rids: + candidate_constraints[rid].lb = original_lb[rid] + + def optimize_mdf() -> Optional[float]: + status = solver.optimize() + if status != "optimal": + return None + return float(solver.objective.value) + + def improved(value: Optional[float]) -> bool: + return value is not None and value > mdf + tol + + def restored(value: Optional[float]) -> bool: + # The original feasible solution guarantees the MDF of a subset of + # the original constraints cannot be below mdf except for numerical + # noise. Use a symmetric tolerance around the original value. + return value is not None and value <= mdf + tol + + try: + candidates = list(candidate_constraints) + + # ------------------------------------------------------------------ + # 1) RESET: remove every candidate thermodynamic constraint. + # ------------------------------------------------------------------ + set_relaxed(candidates) + reset_mdf = optimize_mdf() + if not improved(reset_mdf): + # The MDF is capped elsewhere (or numerically unchanged). + return [] + + # ------------------------------------------------------------------ + # 2) GROW: restore constraints until the MDF returns to the original + # optimum. The last restored reaction is the trigger. + # ------------------------------------------------------------------ + enforced: List[str] = [] + trigger: Optional[str] = None + for rid in candidates: + set_enforced([rid]) + enforced.append(rid) + current_mdf = optimize_mdf() + if restored(current_mdf): + trigger = rid + break + + # This should only be possible because of numerical tolerance / an + # unexpected solver status: all candidates restored must reproduce the + # original model. Treat the full candidate set as the working set if + # no trigger was detected. + if trigger is None: + enforced = candidates[:] + set_enforced(enforced) + current_mdf = optimize_mdf() + if not restored(current_mdf): + return [] + + # ------------------------------------------------------------------ + # 3) SHRINK: remove bystanders while keeping the trigger enforced. + # Iterate until no further member can be removed. + # ------------------------------------------------------------------ + kept = list(dict.fromkeys(enforced)) + if trigger not in kept: + kept.append(trigger) + + # Ensure the working state corresponds exactly to the current kept set. + set_relaxed(candidates) + set_enforced(kept) + optimize_mdf() + + changed = True + while changed: + changed = False + for rid in list(kept): + if rid == trigger: + continue + + # Temporarily remove this member. If the MDF rises above the + # original optimum, this reaction is necessary for this set and + # must be restored. Otherwise it is a bystander and stays out. + candidate_kept = [x for x in kept if x != rid] + set_relaxed([rid]) + trial_mdf = optimize_mdf() + if improved(trial_mdf): + # Necessary: restore it immediately. + set_enforced([rid]) + else: + # Redundant: leave it relaxed and permanently discard it + # from this candidate bottleneck set. + kept = candidate_kept + changed = changed or (rid not in kept) + + # A final direct check makes the mathematical contract explicit: every + # returned member must be necessary, i.e. deleting it must recover an + # MDF strictly above the original value. This also catches unusual + # solver degeneracy where a single pass can be misleading. + for rid in list(kept): + if rid == trigger: + continue + set_relaxed([rid]) + trial_mdf = optimize_mdf() + if improved(trial_mdf): + set_enforced([rid]) + else: + kept.remove(rid) + + # If the trigger itself can be removed without improving the MDF, the + # grow order encountered a different sufficient subset. Re-run the + # shrink condition without privileging it so that the result remains + # inclusion-minimal. + if trigger in kept: + set_relaxed([trigger]) + trial_mdf = optimize_mdf() + if not improved(trial_mdf): + kept.remove(trigger) + + set_relaxed(candidates) + set_enforced(kept) + final_mdf = optimize_mdf() + if final_mdf is None or not restored(final_mdf): + # Defensive fallback: if numerical degeneracy made the final set + # fail to reproduce the original optimum, return the last enforced + # working set rather than a false claim of a bottleneck. + set_enforced(candidates) + optimize_mdf() + return [] + + return kept + + finally: + # Always restore every candidate constraint exactly and return the LP + # to the original optimum state. + set_enforced(candidate_constraints) + solver.optimize() + + +# --------------------------------------------------------------------------- +# OptMDFAnalysis: stateful solver for (iterative) OptMDFpathway +# --------------------------------------------------------------------------- + +class OptMDFAnalysis: + """ + Stateful OptMDFpathway solver, for cases where you want to solve more + than once against the same model/constraints -- most importantly, + iterative thermodynamic-bottleneck analysis: solve, find the current + minimal bottleneck, permanently relax exactly its driving-force + constraint(s), and repeat. + + Building the model copy, running FVA preprocessing, and constructing + every constraint of the MILP is done exactly once, in __init__ (see + optMDFpathway for what all of this means -- the modelling is identical; + this class only changes *when* work happens, not what is computed). + Repeated calls to solve() just re-optimize the same solver object; + finding a bottleneck and relaxing it only ever touches the specific + driving-force constraint(s) involved, never rebuilding anything. + + Typical usage, an unbounded number of iterations decided as you go: + + analysis = OptMDFAnalysis(model, scenarios=[...]) + for result, bottleneck in analysis.run(): + print(result.mdf, bottleneck) + if result.mdf >= target or not bottleneck: + break + + or with manual control over each step: + + analysis = OptMDFAnalysis(model, scenarios=[...]) + result = analysis.solve() + while True: + bottleneck = analysis.find_bottleneck() + if not bottleneck: + break # mdf is no longer thermodynamically limited + analysis.relax(bottleneck) + result = analysis.solve() + + optMDFpathway(...) is a thin one-shot wrapper around this class, for + callers that just want a single solve (optionally with shadow prices + and/or one bottleneck set) without managing the object themselves. + """ + + def __init__( + self, + model: cobra.Model, + Cmin: Union[Number, Dict[str, Number]] = 1e-6, + Cmax: Union[Number, Dict[str, Number]] = 1e-2, + RT: float = 8.314e-3 * 298.15, + scenarios: Optional[Union[ScenarioConstraint, Iterable[ScenarioConstraint]]] = None, + concentration_ratios: Optional[Sequence[Sequence[Any]]] = None, + reaction_subsets: Optional[Sequence[Sequence[Tuple[str, Number]]]] = None, + ignore_reactions: Optional[Iterable[str]] = None, + flux_bound_M: Optional[Number] = None, + dG_bound_M: Number = 1e4, + B_bounds: Tuple[Number, Number] = (-1e4, 1e4), + use_fva_preprocessing: Optional[bool] = None, + precomputed_fva: Optional[Dict[str, Tuple[float, float]]] = None, + blocked_flux_tol: float = 1e-9, + bottleneck_tol: float = 1e-6, + verbose: bool = False, + ): + """ + Build the model copy, run FVA preprocessing, and construct the full + MILP -- everything solve() will need, done exactly once here. + + Parameters + ---------- + model : cobra.Model + The metabolic model. Not modified (an internal copy is used). + Reactions get their thermodynamic data from annotation['dG0'] (a + number, or a (value, uncertainty) pair); reactions without this + annotation may still carry flux but get no driving-force + constraint. Metabolites get their concentration bounds from + annotation['Cmin']/['Cmax'] when present, falling back to the + Cmin/Cmax arguments below otherwise. + Cmin, Cmax : float or dict {metabolite_id: value} + Default concentration bounds in M (mol/L), used for any + metabolite that doesn't carry annotation['Cmin']/['Cmax']. A + scalar applies the same bound to every such metabolite; a dict + may supply per-metabolite defaults. + RT : float + Gas constant * temperature, in energy units matching dG0 + (default: RT at 25 deg C in kJ/mol). + scenarios : optional scenario constraint(s) + Additional linear constraints over reaction fluxes -- e.g. a + fixed uptake rate, a fixed growth rate, or a minimal yield + requirement. Each entry may be a raw + ({reaction_id: coefficient}, constraint_type, rhs) triple with + constraint_type in {'=', '<=', '>='}, or a Scenario-like + reaction_id -> (lb, ub) mapping (core.make_scenario_feasible / + element_exchange_balance's shape; any reactions it needs are + added via .add_scenario_reactions_to_model if present). See + scenario_constraints_to_triples for full details. Used both as + the FVA preprocessing step's ``constraints`` argument and as + ordinary linear constraints added to the MILP. + concentration_ratios : optional list of tuples + Constrains concentration_i / concentration_j for pairs of + metabolites, e.g. to couple cofactor pairs such as ATP/ADP. + Each entry is (met_i, met_j, ratio) for a fixed ratio, or + (met_i, met_j, ratio_min, ratio_max) for a range. + reaction_subsets : optional list of lists of (reaction_id, factor) + Strictly (fully) flux-coupled reaction groups -- e.g. from flux + coupling analysis -- where flux(reaction_id) == (factor / + factor_of_first_entry) * flux(first entry's reaction_id) holds + in every feasible steady state. Speeds up both FVA + preprocessing (run on a compressed model, one reaction per + group -- see _compress_for_fva) and the MILP (one pair of + binaries and one flux-linking constraint per group instead of + per reaction; each member's own driving-force constraint is + still built individually since thermodynamics can't be + shared). Each reaction may appear in at most one subset. + ignore_reactions : optional iterable of reaction ids + Reactions to exclude from driving-force constraints even if + they carry a dG0 annotation. + flux_bound_M : optional float + Upper cap on the big-M used to link flux to its binary + "reaction is active" indicator; see optMDFpathway. + dG_bound_M : float + Big-M used to relax the driving-force constraint when a + reaction's indicator is 0; see optMDFpathway. + B_bounds : (float, float) + Lower/upper bound for the MDF variable itself. + use_fva_preprocessing : optional bool + None (default): try FVA, fall back to static bounds if + core.py's dependencies aren't importable. True: require it. + False: always use static bounds. Ignored if precomputed_fva is + given. See optMDFpathway. + precomputed_fva : optional dict {reaction_id: (min_flux, max_flux)} + Reuse an FVA result the caller already has (e.g. from a GUI's + own FVA computation/cache) instead of running FVA here. Expected + to cover the *uncompressed* model, i.e. one entry per reaction + of `model` -- since every such entry is already the tightest + achievable range, no reaction_subsets compression/expansion is + needed regardless of reaction_subsets, unlike the internal FVA + path. Reactions missing from the dict fall back to static + bounds, same as a NaN FVA result would. When given, this is + used instead of running FVA internally (use_fva_preprocessing + is ignored). + blocked_flux_tol : float + Absolute flux tolerance below which an FVA-computed bound is + treated as zero (that direction is blocked). + bottleneck_tol : float + Absolute driving-force tolerance used by find_bottleneck() to + decide whether a reaction is "tied" at mdf and whether relaxing + a candidate set counts as an improvement. + verbose : bool + Print setup information now, and progress information from + solve()/find_bottleneck()/relax() later. + """ + start_time = time.monotonic() + m = model.copy() + solver = m.solver + if verbose: + m.problem.verbosity = 1 + # Use the solver-specific optlang classes (m.problem), not the generic + # optlang.interface ones -- optlang models refuse to add variables / + # constraints built from a different interface than their own backend. + Variable, Constraint, Objective = m.problem.Variable, m.problem.Constraint, m.problem.Objective + + # Capture the model's own objective (e.g. biomass) before it gets + # overwritten below with "maximize B" -- solve_fba() uses this. + native_objective_coeffs = { + rxn.id: rxn.objective_coefficient for rxn in m.reactions + if rxn.objective_coefficient != 0 + } + native_objective_direction = m.objective_direction + + # -- 0) normalize scenario constraints into (expression, type, rhs) + # triples -- any reactions a Scenario-like entry needs are added to + # `m` here (via .add_scenario_reactions_to_model), so this must + # happen before anything below reads m.reactions / m.metabolites. + scenario_triples = scenario_constraints_to_triples(scenarios, m) + subset_ratio = _normalize_reaction_subsets(reaction_subsets) + + Cmin_d, Cmax_d = _resolve_concentration_bounds(m, Cmin, Cmax) + ignore = set(ignore_reactions or []) + ratio_specs = _normalize_concentration_ratios(concentration_ratios) + + # -- FVA preprocessing: compute the tightest achievable per-reaction + # flux range given the whole network + scenario constraints, for use + # as a tight big-M and to prune structurally blocked reactions. If + # reaction_subsets were given, this runs on a compressed model (see + # _compress_for_fva) and the result is expanded back afterwards. + fva_bounds: Optional[Dict[str, Tuple[float, float]]] = None + if precomputed_fva is not None: + # Caller already ran FVA on the (uncompressed) model -- every + # reaction has its own directly-computed bound, so there is + # nothing to compress/expand via reaction_subsets here, unlike + # the internal FVA path below. Reactions the caller didn't + # cover fall back to static bounds via the NaN path used below. + fva_bounds = { + rxn.id: precomputed_fva.get(rxn.id, (math.nan, math.nan)) + for rxn in m.reactions + } + if verbose: + n_missing = sum(1 for rxn in m.reactions if rxn.id not in precomputed_fva) + msg = "Using precomputed FVA result." + if n_missing: + msg += f" ({n_missing} reaction(s) missing from it; using static bounds for those.)" + print(msg) + elif use_fva_preprocessing is not False: + m_fva, fva_triples = _compress_for_fva(m, subset_ratio, scenario_triples) + fva_lb, fva_ub, n_bad = multi_threaded_HiGHS_FVA(m_fva, constraints=fva_triples) + fva_bounds_compressed = {rxn.id: (lo, hi) for rxn, lo, hi in zip(m_fva.reactions, fva_lb, fva_ub)} + # Expand back to every reaction of the (uncompressed) `m`: a + # representative (or any reaction outside reaction_subsets) + # reads its bound directly; a subset member derives it from its + # representative's bound scaled by their ratio. + fva_bounds = {} + for rxn in m.reactions: + rep_id, ratio = subset_ratio.get(rxn.id, (rxn.id, 1.0)) + lo_rep, hi_rep = fva_bounds_compressed[rep_id] + if math.isnan(lo_rep) or math.isnan(hi_rep): + fva_bounds[rxn.id] = (lo_rep, hi_rep) + elif ratio > 0: + fva_bounds[rxn.id] = (ratio * lo_rep, ratio * hi_rep) + else: + fva_bounds[rxn.id] = (ratio * hi_rep, ratio * lo_rep) + if verbose: + msg = (f"FVA preprocessing done " + f"({n_bad} reaction(s) had solver trouble)" if n_bad + else "FVA preprocessing done.") + if m_fva is not m: + msg += (f" [{len(m.reactions)} reactions compressed to " + f"{len(m_fva.reactions)} via reaction_subsets]") + print(msg) + + # -- 1) log-concentration variables and the MDF variable B -------------- + # (logc is equivalent to c_vars in setup_driving_force_constraints.m) + logc: Dict[str, Variable] = {} + for mid in Cmin_d: + lb, ub = math.log(Cmin_d[mid]), math.log(Cmax_d[mid]) + if lb > ub: + raise ValueError(f"Cmin > Cmax for metabolite {mid!r}") + logc[mid] = Variable(f"logc_{mid}", lb=lb, ub=ub) + B = Variable("MDF", lb=B_bounds[0], ub=B_bounds[1]) + solver.add(list(logc.values()) + [B]) + + # From here on, every constraint is created with a placeholder ``Zero`` + # expression (cheap: no variable-dependent sympy tree at all) and its + # real coefficients are collected in a matching dict, to be pushed into + # the solver later in one batch via set_linear_coefficients -- see the + # module docstring's "Performance note". + all_cons: List[Constraint] = [] + all_coeffs: List[Dict[Variable, float]] = [] + + # -- 2) concentration ratio constraints (fixed or ranged) ---------------- + for i, (mi, mj, lo, hi) in enumerate(ratio_specs): + all_cons.append(Constraint( + Zero, lb=math.log(lo), ub=math.log(hi), + name=f"conc_ratio_{i}_{mi}_{mj}")) + all_coeffs.append({logc[mi]: 1.0, logc[mj]: -1.0}) + + # -- 3) per-subset driving-force machinery ------------------------------- + # Reactions are grouped by representative (every reaction not covered by + # reaction_subsets is its own singleton group, ratio 1.0), so that + # strictly-coupled reactions share one pair of "group active + # forward/reverse" binaries and one flux/indicator-linking constraint + # (on the representative only) instead of paying for that per reaction. + # Each member with its own dG0 annotation still gets its own + # driving-force constraint -- thermodynamics is inherently per-reaction + # and can't be shared -- gated by the group's shared binaries, sign- + # flipped for members whose ratio to the representative is negative + # (they then run opposite it). + # + # Fallback big-M for when FVA preprocessing isn't available: the + # largest finite static reaction bound in the model (or 1000). + static_default_M = flux_bound_M + if static_default_M is None: + finite_bounds = [abs(b) for r in m.reactions + for b in (r.lower_bound, r.upper_bound) + if math.isfinite(b) and b != 0] + static_default_M = max(finite_bounds) if finite_bounds else 1000.0 + + groups: Dict[str, List[Tuple[cobra.Reaction, float]]] = {} + for rxn in m.reactions: + rep_id, ratio = subset_ratio.get(rxn.id, (rxn.id, 1.0)) + groups.setdefault(rep_id, []).append((rxn, ratio)) + + stoich: Dict[str, Dict[str, float]] = {} # rxn.id -> {met.id: coeff} + G0_eff: Dict[str, float] = {} # rxn.id -> worst-case delta_r G0 + df_constraints: Dict[str, Dict[str, Constraint]] = {} # rxn.id -> {"fwd"/"rev": constraint} + + new_bin_vars: List[Variable] = [] + n_fwd = n_rev = n_blocked = n_no_dG0 = 0 + + for rep_id, members in groups.items(): + rep_rxn = m.reactions.get_by_id(rep_id) + + # Which (non-ignored) members actually need a driving-force + # constraint? Skip the whole group's binaries/linking if none do. + active_members: List[Tuple[cobra.Reaction, float, float, float]] = [] + for member_rxn, ratio in members: + if member_rxn.id in ignore: + continue + dG0 = _reaction_dG0(member_rxn) + if dG0 is None: + n_no_dG0 += 1 + continue + if not member_rxn.metabolites: + continue + val, unc = dG0 + active_members.append((member_rxn, ratio, val, unc)) + if not active_members: + continue + + # Representative's achievable flux range: the FVA-tightened one + # when available (falling back to the static bounds if FVA had + # solver trouble), otherwise the static bounds. + if fva_bounds is not None: + fva_lo, fva_hi = fva_bounds[rep_id] + if math.isnan(fva_lo) or math.isnan(fva_hi): + fva_lo, fva_hi = rep_rxn.lower_bound, rep_rxn.upper_bound + else: + fva_lo, fva_hi = rep_rxn.lower_bound, rep_rxn.upper_bound + + can_fwd = fva_hi > blocked_flux_tol + can_rev = fva_lo < -blocked_flux_tol + if not can_fwd and not can_rev: + # Structurally blocked under the current network/constraints -- + # nothing in this group can ever be "used", so none of its + # members need an MDF constraint or binary at all. + n_blocked += len(active_members) + continue + + z_fwd = z_rev = None + if can_fwd: + M_r = fva_hi if flux_bound_M is None else min(flux_bound_M, fva_hi) + z_fwd = Variable(f"z_fwd_{rep_id}", type="binary") + new_bin_vars.append(z_fwd) + all_cons.append(Constraint(Zero, ub=0, name=f"link_fwd_{rep_id}")) + all_coeffs.append({rep_rxn.forward_variable: 1.0, z_fwd: -M_r}) + if can_rev: + M_r = abs(fva_lo) if flux_bound_M is None else min(flux_bound_M, abs(fva_lo)) + z_rev = Variable(f"z_rev_{rep_id}", type="binary") + new_bin_vars.append(z_rev) + all_cons.append(Constraint(Zero, ub=0, name=f"link_rev_{rep_id}")) + all_coeffs.append({rep_rxn.reverse_variable: 1.0, z_rev: -M_r}) + + for member_rxn, ratio, val, unc in active_members: + met_coeffs = {met.id: coeff for met, coeff in member_rxn.metabolites.items()} + stoich[member_rxn.id] = met_coeffs + # conservative (worst-case) standard Gibbs energy: adding the + # uncertainty guarantees df_fwd >= B for every value the true + # dG0 could take in [val - unc, val + unc]. + G0_eff[member_rxn.id] = val + unc + + if member_rxn.id != rep_id: + # Safety net: strict coupling means the network's own mass + # balance already implies flux(member) == ratio*flux(rep) + # in every feasible flux vector, so this is mathematically + # redundant -- but pinning it down explicitly and cheaply + # (a linear equality, not a binary) makes the shared-binary + # sharing above correct by construction rather than by + # trusting reaction_subsets to exactly match the model. + all_cons.append(Constraint( + Zero, lb=0, ub=0, name=f"couple_{member_rxn.id}_{rep_id}")) + all_coeffs.append({ + member_rxn.forward_variable: 1.0, member_rxn.reverse_variable: -1.0, + rep_rxn.forward_variable: -ratio, rep_rxn.reverse_variable: ratio, + }) + + # A positive ratio means the member runs forward exactly when + # the representative does (and reverse exactly when it does); + # a negative ratio flips that correspondence. + fwd_indicator = z_fwd if ratio > 0 else z_rev + rev_indicator = z_rev if ratio > 0 else z_fwd + + # driving force if the reaction runs forward: + # df_fwd = -G0_eff - RT * sum_m S[m] * logc[m] + # Constraint enforced only when the indicator = 1 (df_fwd >= B): + # -RT*sum(S[m]*logc[m]) - B - dG_bound_M*z >= G0_eff - dG_bound_M + # (the constant G0_eff is folded straight into the constraint's + # lower bound, since set_linear_coefficients only ever sets + # *variable* coefficients, never a constant offset) + if fwd_indicator is not None: + df_coeffs = {logc[mid]: -RT * coeff for mid, coeff in met_coeffs.items()} + df_coeffs[B] = -1.0 + df_coeffs[fwd_indicator] = -dG_bound_M + df_fwd_con = Constraint( + Zero, lb=G0_eff[member_rxn.id] - dG_bound_M, name=f"df_fwd_{member_rxn.id}") + all_cons.append(df_fwd_con) + all_coeffs.append(df_coeffs) + df_constraints.setdefault(member_rxn.id, {})["fwd"] = df_fwd_con + n_fwd += 1 + + # running in reverse negates the effective stoichiometry, so + # the reverse driving force is simply -df_fwd: + # RT*sum(S[m]*logc[m]) - B - dG_bound_M*z >= -G0_eff - dG_bound_M + if rev_indicator is not None: + df_coeffs = {logc[mid]: RT * coeff for mid, coeff in met_coeffs.items()} + df_coeffs[B] = -1.0 + df_coeffs[rev_indicator] = -dG_bound_M + df_rev_con = Constraint( + Zero, lb=-G0_eff[member_rxn.id] - dG_bound_M, name=f"df_rev_{member_rxn.id}") + all_cons.append(df_rev_con) + all_coeffs.append(df_coeffs) + df_constraints.setdefault(member_rxn.id, {})["rev"] = df_rev_con + n_rev += 1 + + # -- 4) scenario constraints (fixed uptake rates, yield requirements, + # ...), as ordinary linear constraints on the reactions' flux + # expressions -- reuses the same triples the FVA preprocessing step + # above was given, so both see exactly the same constraints. --------- + for i, (expr, ctype, rhs) in enumerate(scenario_triples): + coeffs: Dict[Variable, float] = {} + try: + rxns = {rid: m.reactions.get_by_id(rid) for rid in expr} + except KeyError as exc: + print(f"Skipping scenario constraint referencing a reaction not " + f"in the model ({exc}): {expr}") + continue + for rid, coeff in expr.items(): + rxn = rxns[rid] + coeffs[rxn.forward_variable] = coeffs.get(rxn.forward_variable, 0.0) + coeff + coeffs[rxn.reverse_variable] = coeffs.get(rxn.reverse_variable, 0.0) - coeff + if ctype == "=": + con = Constraint(Zero, lb=rhs, ub=rhs, name=f"scenario_{i}") + elif ctype == "<=": + con = Constraint(Zero, ub=rhs, name=f"scenario_{i}") + else: # ">=" + con = Constraint(Zero, lb=rhs, name=f"scenario_{i}") + all_cons.append(con) + all_coeffs.append(coeffs) + + # -- 5) push every binary + constraint into the solver in one batch, + # flush it, then patch in the real coefficients ------------------------ + solver.add(new_bin_vars) + solver.add(all_cons) + solver.update() # flush pending additions so set_linear_coefficients can see them + for con, coeffs in zip(all_cons, all_coeffs): + con.set_linear_coefficients(coeffs) + + # -- 6) objective: maximise the max-min driving force -------------------- + solver.objective = Objective(Zero, direction="max") + solver.update() + solver.objective.set_linear_coefficients({B: 1.0}) + print(time.monotonic() - start_time) + + if verbose: + n_groups_used = len({subset_ratio.get(rid, (rid, 1.0))[0] for rid in stoich}) + subset_note = (f", sharing {n_groups_used} group(s)' binaries via " + "reaction_subsets" if subset_ratio else "") + print(f"OptMDFpathway: {len(stoich)} reactions with dG0 annotations " + f"({n_fwd} forward + {n_rev} reverse directions, " + f"{n_blocked} blocked & skipped, {n_no_dG0} without a dG0 " + f"annotation), {len(new_bin_vars)} binaries" + subset_note + + ("" if fva_bounds is not None else + f", default big-M = {static_default_M:g}")) + + # -- everything solve()/shadow_prices()/find_bottleneck()/relax() + # need going forward, preserved for reuse across iterations. ---- + self.model = m + self.solver = solver + self.RT = RT + self.logc = logc + self.B = B + self.stoich = stoich + self.G0_eff = G0_eff + self.df_constraints = df_constraints + self.bin_vars = new_bin_vars + self._bin_var_bounds = {z: (z.lb, z.ub) for z in new_bin_vars} + self.subset_ratio = subset_ratio + self.bottleneck_tol = bottleneck_tol + self.verbose = verbose + self._native_objective_coeffs = native_objective_coeffs + self._native_objective_direction = native_objective_direction + + #: One OptMDFResult per solve() call, in order. + self.history: List[OptMDFResult] = [] + #: One bottleneck-set-that-was-relaxed per relax() call, in order + #: (mirrors self.history when driven via step()/run()). + self.relaxed_log: List[List[str]] = [] + #: Reaction ids whose driving-force constraint(s) have been + #: permanently relaxed via relax() so far. + self._relaxed: Set[str] = set() + + # -- internal: temporarily solve as a pure LP with every binary fixed -- + @contextlib.contextmanager + def _fixed_binary_lp(self) -> Iterator[Optional[str]]: + """ + Context manager: fix every binary at its currently-solved value + (rounded to 0/1) and switch it to continuous, re-solve (so duals + and constraint-removal comparisons are meaningful -- see + optMDFpathway's compute_shadow_prices docstring for why), yield the + resulting status, then always restore every binary to its original + (type='binary', bounds=[0, 1]) state and re-solve again before + returning control -- so the model is back in proper MILP form for + the next solve()/find_bottleneck() call regardless of what the + caller did in between, even if it raised an exception. + """ + if not self.bin_vars: + yield self.history[-1].status if self.history else None + return + z_values = [round(z.primal) for z in self.bin_vars] # read all before making modifications + for z, z_val in zip(self.bin_vars, z_values): + z.type = "continuous" + z.lb = z.ub = z_val + status = self.solver.optimize() + try: + yield status + finally: + for z in self.bin_vars: + lb, ub = self._bin_var_bounds[z] + z.lb, z.ub = lb, ub + z.type = "binary" + self.solver.optimize() + + # -- public API ----------------------------------------------------------- + + def _extract_result(self, status: str) -> OptMDFResult: + """Build an OptMDFResult from the solver's current (just-solved) + state. Shared by solve() and solve_fba() -- everything except + .mdf and .objective_value is identical between them.""" + result = OptMDFResult(status=status) + if status != "optimal": + return result + for rxn in self.model.reactions: + result.fluxes[rxn.id] = rxn.flux + for mid, v in self.logc.items(): + result.concentrations[mid] = math.exp(v.primal) + for rid, coeffs in self.stoich.items(): + dG_fwd = self.G0_eff[rid] + self.RT * sum( + coeff * math.log(result.concentrations[mid]) + for mid, coeff in coeffs.items()) + df_fwd = -dG_fwd + # Report the driving force of the direction the reaction + # actually ran in this solution (df_fwd if flux >= 0, else + # -df_fwd, i.e. the reverse driving force): a reaction running + # in reverse can have a very negative df_fwd (that's *why* it + # runs in reverse) even though the direction it actually took + # satisfies df >= mdf. + result.driving_forces[rid] = df_fwd if result.fluxes[rid] >= 0 else -df_fwd + return result + + def solve(self) -> OptMDFResult: + """ + Solve the MILP as it currently stands (including any driving-force + constraints previously relaxed via relax()) and return an + OptMDFResult, exactly as optMDFpathway does. Appended to + self.history. + """ + start_time = time.monotonic() + status = self.solver.optimize() + print(time.monotonic() - start_time) + result = self._extract_result(status) + if status != "optimal": + if self.verbose: + print(f"Solve finished with status: {status}") + self.history.append(result) + return result + + result.mdf = self.B.primal + if self.verbose: + print(f"MDF = {result.mdf:.4g} (same energy units as dG0/RT)") + self.history.append(result) + return result + + @contextlib.contextmanager + def _temporary_objective(self, coeffs: Dict[Any, float], direction: str) -> Iterator[None]: + """Swap in a different objective (built from {variable: coefficient} + pairs) for the duration of the block, then always restore "maximize + B" -- this class's normal, permanent objective -- afterwards.""" + Objective = self.model.problem.Objective + self.solver.objective = Objective(Zero, direction=direction) + self.solver.update() + self.solver.objective.set_linear_coefficients(coeffs) + try: + yield + finally: + self.solver.objective = Objective(Zero, direction="max") + self.solver.update() + self.solver.objective.set_linear_coefficients({self.B: 1.0}) + + def solve_fba( + self, + objective: Optional[Dict[str, Number]] = None, + direction: Optional[str] = None, + ) -> OptMDFResult: + """ + Thermodynamically-constrained FBA: instead of maximising the MDF + itself, optimise the model's own objective (e.g. biomass) subject + to every constraint already in the MILP -- including B's own + bounds, so B_bounds[0] (set when constructing this analysis) acts + as a required minimum MDF for whichever flux distribution is + found. Everything else (scenarios, concentration_ratios, + reaction_subsets, any reactions already relax()ed) applies exactly + as in solve(). If B_bounds[0] is set higher than any flux + distribution can actually achieve, this is simply infeasible -- + same as any other over-constrained MILP. + + Parameters + ---------- + objective : optional dict {reaction_id: coefficient} + Defaults to the model's own objective (read from + reaction.objective_coefficient when this analysis was + constructed). Supply this to optimise something else instead + (e.g. a different reaction, or a linear combination). + direction : optional 'max' or 'min' + Defaults to the model's own objective_direction. Only used + together with an explicit ``objective``. + + Returns + ------- + OptMDFResult + Same fields as solve(), plus .objective_value: the optimised + objective's value (e.g. the growth rate). .mdf is B's value at + this solution (>= B_bounds[0] by construction). + """ + coeffs = objective if objective is not None else self._native_objective_coeffs + obj_direction = direction if direction is not None else self._native_objective_direction + if not coeffs: + raise ValueError( + "No objective to optimise: the model has no objective set " + "(and none was given). Set model.objective before " + "constructing OptMDFAnalysis, or pass objective= explicitly." + ) + var_coeffs: Dict[Any, float] = {} + for rid, coeff in coeffs.items(): + rxn = self.model.reactions.get_by_id(rid) + var_coeffs[rxn.forward_variable] = var_coeffs.get(rxn.forward_variable, 0.0) + coeff + var_coeffs[rxn.reverse_variable] = var_coeffs.get(rxn.reverse_variable, 0.0) - coeff + + with self._temporary_objective(var_coeffs, obj_direction): + status = self.solver.optimize() + result = self._extract_result(status) + if status == "optimal": + result.mdf = self.B.primal + result.objective_value = self.solver.objective.value + if self.verbose: + print(f"Thermodynamic FBA: objective = {result.objective_value:.6g}, " + f"mdf = {result.mdf:.4g} (>= B_bounds[0])") + elif self.verbose: + print(f"Thermodynamic FBA finished with status: {status}") + self.history.append(result) + return result + + def shadow_prices(self) -> Dict[str, float]: + """ + Shadow prices d(mdf)/d(dG0_r) of every reaction's driving-force + constraint, for the most recent solve() result: how much the MDF + would improve per unit its dG0 were relaxed. Nonzero only for + reactions whose constraint is exactly binding (df_r == mdf) in that + solution -- see optMDFpathway's compute_shadow_prices docstring for + the full explanation (including its caveat: with redundant/tied + reactions, e.g. isozymes, a nonzero dual on one of them doesn't + necessarily mean relaxing *that one alone* would help -- use + find_bottleneck() to confirm which reactions are actually, jointly, + necessary). + + Requires a prior solve() with status "optimal"; raises RuntimeError + otherwise. + """ + if not self.history or self.history[-1].status != "optimal": + raise RuntimeError("shadow_prices() requires a prior solve() " + "that returned status 'optimal'.") + result = self.history[-1] + duals: Dict[str, float] = {} + with self._fixed_binary_lp() as lp_status: + if lp_status == "optimal": + if self.verbose and abs(self.B.primal - result.mdf) > 1e-6: + print(f"Note: shadow-price LP resolve landed on " + f"mdf={self.B.primal:.6g}, slightly different " + f"from the original {result.mdf:.6g} (likely " + "solution degeneracy); duals are still valid for " + "the LP that was actually solved.") + duals_sum = 0 + for rid in self.stoich: + direction = "fwd" if result.fluxes[rid] >= 0 else "rev" + con = self.df_constraints.get(rid, {}).get(direction) + if con is not None: + duals[rid] = con.dual + ad = abs(con.dual) + if ad > 1e-6: + print(rid, con.dual) + duals_sum += ad + print("duals_sum", duals_sum) + elif self.verbose: + print(f"Shadow-price LP resolve finished with status " + f"{lp_status!r} (expected 'optimal'); skipping shadow prices.") + return duals + + def find_bottleneck(self) -> List[str]: + """ + A minimal thermodynamic bottleneck set for the most recent solve() + result -- see _find_minimal_bottleneck for the algorithm (a + Grow-Shrink search: relax every reaction currently tied at mdf, + confirm that helps at all, then re-enforce them one at a time + until mdf drops back down -- trapping one complete bottleneck set + -- before relaxing bystanders back out one at a time to isolate + exactly the necessary members; repeats over any remaining tied + reactions to find further independent bottleneck sets at this same + mdf). This correctly handles reactions coupled through a shared + limiting metabolite, where relaxing just one of them can already + permit some improvement without capturing the full achievable + improvement -- a plain one-at-a-time deletion filter would + under-report in that case. An empty list means mdf isn't currently + limited by any reaction's thermodynamics (e.g. it's capped by + B_bounds or a scenario constraint instead), or -- after enough + relax() calls -- that nothing thermodynamically limiting remains. + + Reactions already relaxed by a previous relax() call are excluded + from consideration (their constraint is already permanently + non-binding, so they can never be part of a *new* bottleneck). + + Uses the same fixed-binary LP as shadow_prices() (a handful of + extra re-solves, restored afterwards either way); requires a prior + solve() with status "optimal". + """ + if not self.history or self.history[-1].status != "optimal": + raise RuntimeError("find_bottleneck() requires a prior solve() " + "that returned status 'optimal'.") + result = self.history[-1] + driving_forces = {rid: df for rid, df in result.driving_forces.items() + if rid not in self._relaxed} + with self._fixed_binary_lp() as lp_status: + if lp_status != "optimal": + if self.verbose: + print(f"Bottleneck-search LP resolve finished with status " + f"{lp_status!r} (expected 'optimal'); skipping.") + return [] + start_time = time.monotonic() + bottleneck = _find_minimal_bottleneck( + self.solver, self.df_constraints, result.fluxes, + driving_forces, result.mdf, tol=self.bottleneck_tol) + print(time.monotonic() - start_time) + if self.verbose: + print(f"Bottleneck: {bottleneck}" if bottleneck else + "No thermodynamic bottleneck (mdf isn't limited by any " + "reaction's driving force here).") + return bottleneck + + def relax(self, reactions: Iterable[str]) -> None: + """ + Permanently relax (exempt from df_r >= mdf entirely) the + driving-force constraint(s) of the given reactions, so every future + solve() no longer enforces them. Idempotent -- reactions already + relaxed, or without a driving-force constraint at all (e.g. no + dG0 annotation), are silently skipped and not recorded in + relaxed_log. Their driving force is still computed and reported by + solve() (it's simply no longer constrained), and they're + automatically excluded from future find_bottleneck() candidate + sets. + """ + actually_relaxed = [] + for rid in reactions: + cons = self.df_constraints.get(rid) + if not cons: + continue + for con in cons.values(): + con.lb = -1e9 + self._relaxed.add(rid) + actually_relaxed.append(rid) + if actually_relaxed: + self.relaxed_log.append(actually_relaxed) + if self.verbose: + print(f"Relaxed: {actually_relaxed}") + + def step(self) -> Tuple[OptMDFResult, List[str]]: + """ + One iteration of the bottleneck-relaxation loop: solve(), find the + bottleneck for that result, relax() it (if non-empty), and return + (result, bottleneck). An empty bottleneck means mdf is no longer + thermodynamically limited -- further calls won't improve it. + """ + result = self.solve() + if result.status != "optimal": + return result, [] + bottleneck = self.find_bottleneck() + if bottleneck: + self.relax(bottleneck) + return result, bottleneck + + def run(self, max_iterations: Optional[int] = None) -> Iterator[Tuple[OptMDFResult, List[str]]]: + """ + Generator: repeatedly call step(), yielding (result, bottleneck) + each time, until either the bottleneck comes back empty (mdf is no + longer thermodynamically limited, or the solve failed) or + max_iterations have been yielded. Since it's a generator, the + caller decides how many iterations to actually consume -- e.g. + + for result, bottleneck in analysis.run(): + print(result.mdf, bottleneck) + if result.mdf >= target: + break + """ + n = 0 + while max_iterations is None or n < max_iterations: + result, bottleneck = self.step() + yield result, bottleneck + n += 1 + if result.status != "optimal" or not bottleneck: + return + + +# --------------------------------------------------------------------------- +# optMDFpathway: one-shot convenience wrapper around OptMDFAnalysis +# --------------------------------------------------------------------------- + +def optMDFpathway( + model: cobra.Model, + Cmin: Union[Number, Dict[str, Number]] = 1e-6, + Cmax: Union[Number, Dict[str, Number]] = 1e-2, + RT: float = 8.314e-3 * 298.15, + scenarios: Optional[Union[ScenarioConstraint, Iterable[ScenarioConstraint]]] = None, + concentration_ratios: Optional[Sequence[Sequence[Any]]] = None, + reaction_subsets: Optional[Sequence[Sequence[Tuple[str, Number]]]] = None, + ignore_reactions: Optional[Iterable[str]] = None, + flux_bound_M: Optional[Number] = None, + dG_bound_M: Number = 1e4, + B_bounds: Tuple[Number, Number] = (-1e4, 1e4), + use_fva_preprocessing: Optional[bool] = None, + precomputed_fva: Optional[Dict[str, Tuple[float, float]]] = None, + blocked_flux_tol: float = 1e-9, + bottleneck_tol: float = 1e-6, + compute_shadow_prices: bool = False, + find_minimal_bottleneck: bool = False, + verbose: bool = False, +) -> OptMDFResult: + """ + Compute a single OptMDFpathway solution: the flux distribution and + metabolite concentration profile that jointly maximise the max-min + driving force. A thin one-shot wrapper around OptMDFAnalysis(model, + ...).solve() -- see that class's docstring (and its __init__'s, for the + full parameter reference) for the underlying model and for iterative + use (solving more than once against the same model/constraints, most + importantly repeated bottleneck-relaxation analysis, without paying to + rebuild the model/FVA/MILP on every call). + + Parameters not listed below (Cmin, Cmax, RT, scenarios, + concentration_ratios, reaction_subsets, ignore_reactions, flux_bound_M, + dG_bound_M, B_bounds, use_fva_preprocessing, precomputed_fva, + blocked_flux_tol) are exactly OptMDFAnalysis.__init__'s. + + bottleneck_tol : float + Only relevant when find_minimal_bottleneck=True; see + OptMDFAnalysis.__init__. + compute_shadow_prices : bool + If True, also compute OptMDFAnalysis.shadow_prices() and attach it + as result.driving_force_duals: d(mdf)/d(dG0_r) for every reaction, + nonzero only where the driving-force constraint is exactly binding. + See OptMDFAnalysis.shadow_prices's docstring for the important + caveat about redundant/tied reactions. + find_minimal_bottleneck : bool + If True, also compute OptMDFAnalysis.find_bottleneck() and attach + it as result.bottleneck_reactions: a minimal set of reactions whose + driving-force constraints, relaxed together, would allow mdf to + improve (empty if mdf isn't thermodynamically limited here). + verbose : bool + Print setup and solve information. + + Note + ---- + If nothing forces the pathway to carry flux (no scenario/bound forcing + nonzero flux anywhere), the all-zero flux vector trivially satisfies + "every *active* reaction respects the MDF bound" (there are none), and + mdf will just come back at B_bounds[1]. Always pair this with a + scenario constraint that forces flux through the pathway, as in the + demo below. + + Returns + ------- + OptMDFResult + .status optlang solver status ("optimal" if solved) + .mdf the maximal max-min driving force + .fluxes {reaction_id: flux} + .concentrations {metabolite_id: concentration in M} + .driving_forces {reaction_id: driving force}, only for + reactions that had a dG0 annotation and were + not found to be structurally blocked by FVA + .driving_force_duals {reaction_id: d(mdf)/d(dG0_r)}, only if + compute_shadow_prices=True + .bottleneck_reactions minimal bottleneck set, only if + find_minimal_bottleneck=True + """ + analysis = OptMDFAnalysis( + model, Cmin=Cmin, Cmax=Cmax, RT=RT, scenarios=scenarios, + concentration_ratios=concentration_ratios, reaction_subsets=reaction_subsets, + ignore_reactions=ignore_reactions, flux_bound_M=flux_bound_M, + dG_bound_M=dG_bound_M, B_bounds=B_bounds, + use_fva_preprocessing=use_fva_preprocessing, + precomputed_fva=precomputed_fva, + blocked_flux_tol=blocked_flux_tol, bottleneck_tol=bottleneck_tol, + verbose=verbose, + ) + result = analysis.solve() + if result.status == "optimal": + if compute_shadow_prices: + result.driving_force_duals = analysis.shadow_prices() + if find_minimal_bottleneck: + result.bottleneck_reactions = analysis.find_bottleneck() + return result + + +# --------------------------------------------------------------------------- +# driving-force ranges (no flux coupling) -- analogous to driving_forces.m +# --------------------------------------------------------------------------- + +def driving_force_ranges( + model: cobra.Model, + Cmin: Union[Number, Dict[str, Number]] = 1e-6, + Cmax: Union[Number, Dict[str, Number]] = 1e-2, + RT: float = 8.314e-3 * 298.15, + concentration_ratios: Optional[Sequence[Sequence[Any]]] = None, + ignore_reactions: Optional[Iterable[str]] = None, +) -> Dict[str, Tuple[float, float]]: + """ + For every reaction with a dG0 annotation, compute the achievable range + of its driving force from the concentration bounds (and + concentration_ratios) alone, with no coupling to the flux distribution + -- mirrors CNA's driving_forces.m. + + Like driving_forces.m, a reaction only needs an LP when *two or more* + of its own metabolites participate in a concentration_ratios constraint + (with each other or with something outside the reaction) -- only then + can a ratio constraint actually couple the extremes achievable within + that one reaction. Otherwise each metabolite of the reaction can + independently be pushed to whichever bound (Cmin or Cmax) favours the + direction being optimised, which is computed directly with no solver + call at all. In particular, if concentration_ratios is empty (the + common case), no LPs are run at all. + + Parameters + ---------- + model, Cmin, Cmax, RT, concentration_ratios, ignore_reactions : + Same meaning as in optMDFpathway (dG0/Cmin/Cmax read from + annotations first, falling back to the Cmin/Cmax arguments). + + Returns + ------- + dict {reaction_id: (min_driving_force, max_driving_force)} + """ + Cmin_d, Cmax_d = _resolve_concentration_bounds(model, Cmin, Cmax) + ignore = set(ignore_reactions or []) + ratio_specs = _normalize_concentration_ratios(concentration_ratios) + + log_cmin = {mid: math.log(v) for mid, v in Cmin_d.items()} + log_cmax = {mid: math.log(v) for mid, v in Cmax_d.items()} + + has_ratio = set() + for mi, mj, _, _ in ratio_specs: + has_ratio.add(mi) + has_ratio.add(mj) + + ranges: Dict[str, Tuple[float, float]] = {} + # reactions where a ratio constraint links >=2 of their own metabolites + # -- the only ones that actually need an LP: + lp_needed: List[Tuple[str, Dict[str, float], float]] = [] + + for rxn in model.reactions: + if rxn.id in ignore: + continue + dG0 = _reaction_dG0(rxn) + if dG0 is None: + continue + val, unc = dG0 + met_coeffs = {met.id: coeff for met, coeff in rxn.metabolites.items()} + if not met_coeffs: + continue + G0_eff = val + unc + + n_ratio_linked = sum(1 for mid in met_coeffs if mid in has_ratio) + if n_ratio_linked <= 1: + # Analytic extremes (see driving_forces.m): to minimise + # df = -G0_eff - RT*sum(coeff*logc), maximise sum(coeff*logc) by + # pushing positive-coefficient metabolites to logCmax and + # negative-coefficient ones to logCmin (and vice versa for the + # maximum) -- valid whenever no ratio constraint ties this + # reaction's own metabolites' extremes together. + pos_at_max = sum(c * log_cmax[mid] for mid, c in met_coeffs.items() if c > 0) + pos_at_min = sum(c * log_cmin[mid] for mid, c in met_coeffs.items() if c > 0) + neg_at_min = sum(c * log_cmin[mid] for mid, c in met_coeffs.items() if c < 0) + neg_at_max = sum(c * log_cmax[mid] for mid, c in met_coeffs.items() if c < 0) + min_df = -G0_eff - RT * (pos_at_max + neg_at_min) + max_df = -G0_eff - RT * (pos_at_min + neg_at_max) + ranges[rxn.id] = (min_df, max_df) + else: + lp_needed.append((rxn.id, met_coeffs, G0_eff)) + + if not lp_needed: + return ranges + + # Only reached when concentration_ratios actually couples two or more + # metabolites within the same reaction -- build the optlang model + # lazily, and only now. + m = model.copy() + solver = m.solver + Variable, Constraint, Objective = m.problem.Variable, m.problem.Constraint, m.problem.Objective + + logc: Dict[str, Variable] = { + mid: Variable(f"logc_{mid}", lb=log_cmin[mid], ub=log_cmax[mid]) + for mid in Cmin_d + } + solver.add(list(logc.values())) + + ratio_cons: List[Constraint] = [] + ratio_coeffs: List[Dict[Variable, float]] = [] + for i, (mi, mj, lo, hi) in enumerate(ratio_specs): + ratio_cons.append(Constraint( + Zero, lb=math.log(lo), ub=math.log(hi), + name=f"conc_ratio_{i}_{mi}_{mj}")) + ratio_coeffs.append({logc[mi]: 1.0, logc[mj]: -1.0}) + solver.add(ratio_cons) + solver.update() + for con, coeffs in zip(ratio_cons, ratio_coeffs): + con.set_linear_coefficients(coeffs) + + # A single reusable objective: coefficients are patched per reaction via + # set_linear_coefficients rather than rebuilding the Objective (and + # parsing a fresh sympy expression) on every iteration. + solver.objective = Objective(Zero, direction="min") + solver.update() + + prev_coeffs: Dict[Variable, float] = {} + for rid, met_coeffs, G0_eff in lp_needed: + term_coeffs = {logc[mid]: -RT * coeff for mid, coeff in met_coeffs.items()} + reset = {v: 0.0 for v in prev_coeffs if v not in term_coeffs} + solver.objective.set_linear_coefficients({**reset, **term_coeffs}) + prev_coeffs = term_coeffs + + solver.objective.direction = "min" + solver.optimize() + lo_df = -G0_eff + solver.objective.value + solver.objective.direction = "max" + solver.optimize() + hi_df = -G0_eff + solver.objective.value + ranges[rid] = (lo_df, hi_df) + + return ranges + + +# --------------------------------------------------------------------------- +# self-test / demo +# --------------------------------------------------------------------------- + +if __name__ == "__main__": + # A tiny toy pathway: EX_A -> A -R1-> B -R2-> C -> EX_C + # R1 is thermodynamically favourable at standard conditions, R2 is not + # (it needs a concentration gradient to become favourable) -- a good + # minimal test that the MILP actually engages the concentration + # variables rather than just doing plain FBA. + from optlang import highs_interface + cobra.Configuration.solver = highs_interface + toy = cobra.Model("toy_pathway") + + A = cobra.Metabolite("A", compartment="c") + B = cobra.Metabolite("B", compartment="c") + C = cobra.Metabolite("C", compartment="c") + + EX_A = cobra.Reaction("EX_A", lower_bound=-10, upper_bound=0) + EX_A.add_metabolites({A: -1}) + + R1 = cobra.Reaction("R1", lower_bound=0, upper_bound=1000) + R1.add_metabolites({A: -1, B: 1}) + R1.annotation["dG0"] = -5.0 # kJ/mol, favourable at standard conditions + + R2 = cobra.Reaction("R2", lower_bound=0, upper_bound=1000) + R2.add_metabolites({B: -1, C: 1}) + R2.annotation["dG0"] = 3.0 # kJ/mol, needs help from concentrations + + EX_C = cobra.Reaction("EX_C", lower_bound=0, upper_bound=1000) + EX_C.add_metabolites({C: -1}) + + toy.add_reactions([EX_A, R1, R2, EX_C]) + + # A minimal Scenario stand-in -- only needs to behave like a + # {reaction_id: (lb, ub)} mapping (a real cnapy Scenario may + # additionally implement .add_scenario_reactions_to_model; see + # scenario_constraints_to_triples' docstring). Combined here with a + # genuine multi-reaction constraint given directly as a triple: a + # minimal yield requirement of >= 0.5 mol C per mol A consumed + # (EX_A is negative while importing, so -EX_A is the uptake magnitude). + scenario = {"EX_C": (1.0, 1000.0)} # force >= 1 unit of net C production + yield_constraint = ({"EX_C": 1.0, "EX_A": 0.5}, ">=", 0.0) # EX_C >= 0.5 * (-EX_A) + + result = optMDFpathway( + toy, + Cmin=1e-6, + Cmax=1e-2, + scenarios=[scenario, yield_constraint], + # use_fva_preprocessing defaults to "try it, fall back quietly if + # core.py's dependencies (cnapy/highspy/...) aren't importable" -- + # explicitly set True/False to require it / always skip it. + verbose=True, + ) + + print(result) + print("fluxes: ", result.fluxes) + print("concentrations:", result.concentrations) + print("driving forces:", result.driving_forces) + + print("\ndriving_force_ranges, no concentration_ratios " + "(purely analytic, no LPs run at all):") + for rid, (lo, hi) in driving_force_ranges(toy).items(): + print(f" {rid}: [{lo:.3g}, {hi:.3g}] kJ/mol") + + print("\ndriving_force_ranges with a concentration ratio *range* on " + "A/B (couples two metabolites of R1, so R1 now needs an LP; R2 " + "still doesn't, since only B -- one of its two metabolites -- " + "is ratio-linked):") + ranges = driving_force_ranges( + toy, concentration_ratios=[("A", "B", 0.5, 2.0)]) + for rid, (lo, hi) in ranges.items(): + print(f" {rid}: [{lo:.3g}, {hi:.3g}] kJ/mol") + + print("\nSame toy pathway, but telling optMDFpathway that R1 and R2 are " + "strictly (1:1) flux-coupled -- true here since B has no other " + "source or sink. This collapses their 2 binaries down to 1 and " + "gives the identical MDF:") + result_subset = optMDFpathway( + toy, + Cmin=1e-6, + Cmax=1e-2, + scenarios=[scenario, yield_constraint], + reaction_subsets=[[("R1", 1.0), ("R2", 1.0)]], + verbose=True, + ) + print(result_subset) + + print("\nThermodynamic bottleneck analysis (shadow prices of the " + "driving-force constraints): which reaction is actually limiting " + "the MDF, and how much would relaxing its dG0 help?") + result_dual = optMDFpathway( + toy, Cmin=1e-6, Cmax=1e-2, scenarios=[scenario], + compute_shadow_prices=True, find_minimal_bottleneck=True, + use_fva_preprocessing=False, + ) + for rid, dual in result_dual.driving_force_duals.items(): + tag = "<- nonzero dual" if dual != 0 else " (has slack)" + print(f" {rid}: d(mdf)/d(dG0) = {dual:+.3f} {tag}") + print(f" verified minimal bottleneck set: {result_dual.bottleneck_reactions}") + + print("\nIterative bottleneck relaxation via OptMDFAnalysis: builds the " + "model/FVA/MILP once, then repeatedly solves, finds the current " + "bottleneck, and permanently relaxes it -- an unbounded number of " + "iterations, decided on the fly, with no rebuilding in between:") + chain = cobra.Model("chain") + W, X, Y, Zm = (cobra.Metabolite(n, compartment="c") for n in "WXYZ") + EX_W = cobra.Reaction("EX_W", lower_bound=-10, upper_bound=0) + EX_W.add_metabolites({W: -1}) + Ra = cobra.Reaction("Ra", lower_bound=0, upper_bound=1000) + Ra.add_metabolites({W: -1, X: 1}) + Ra.annotation["dG0"] = -20.0 # favourable, never the bottleneck + Rb = cobra.Reaction("Rb", lower_bound=0, upper_bound=1000) + Rb.add_metabolites({X: -1, Y: 1}) + Rb.annotation["dG0"] = 8.0 # unfavourable -- the first bottleneck + Rc = cobra.Reaction("Rc", lower_bound=0, upper_bound=1000) + Rc.add_metabolites({Y: -1, Zm: 1}) + Rc.annotation["dG0"] = 6.0 # unfavourable -- the second bottleneck + EX_Z = cobra.Reaction("EX_Z", lower_bound=0, upper_bound=1000) + EX_Z.add_metabolites({Zm: -1}) + chain.add_reactions([EX_W, Ra, Rb, Rc, EX_Z]) + + analysis = OptMDFAnalysis( + chain, Cmin=1e-6, Cmax=1e-2, + scenarios={"EX_Z": (1.0, 1000.0)}, + use_fva_preprocessing=False, + ) + for result, bottleneck in analysis.run(max_iterations=10): + print(f" mdf={result.mdf:.3f} bottleneck_relaxed={bottleneck}") + print(f" {len(analysis.history)} solves total, model/FVA/MILP built once") diff --git a/cnapy/plot_trace_highs.py b/cnapy/plot_trace_highs.py new file mode 100644 index 00000000..225fd7a2 --- /dev/null +++ b/cnapy/plot_trace_highs.py @@ -0,0 +1,1076 @@ +"""cnapy/plot_trace_highs.py (prototype) + +HiGHS-native reimplementation of lptools._trace_polygon_rate_rate (exact +convex polygon boundary for two *rate* axes), built on +highs_lp_cache.HighsRateSolver instead of straindesign's fba(). Two +variants: + + - trace_polygon_rate_rate(): sequential, same recursive normal-bisection + algorithm as lptools.py, same result up to floating-point tie-breaking + in which vertex HiGHS returns for a degenerate (multi-optimal) face. + Already benefits from build-once/incremental reuse even with zero + threading (~13x per the person's own benchmark of the underlying + optimize() pattern). + + - trace_polygon_rate_rate_parallel(): same algorithm restructured as + breadth-first "waves" instead of depth-first recursion -- each wave + refines every currently-too-coarse edge concurrently via a small + thread pool, then decides which sub-edges need another wave, until + stable. Each worker thread gets its own HighsRateSolver (built from + the same read-only HighsLp) via threading.local(), never shares one + across threads. Produces the same vertex set as the sequential + version, in the same polygon order. + +Both expect the caller to have already built `lp` via +highs_lp_cache.build_rate_lp(cobra_model, constraints=, num_fix_slots=0) -- baseline +constraints are baked into the LP at build time, not passed per call, +since they're constant for the whole trace (this is exactly the +build-once contract highs_lp_cache is designed around). +""" + +import math +import threading as th +from typing import Dict, List, Optional, Tuple + +import numpy + +from cnapy.highs_lp_cache import HighsRateSolver, HighsYieldSolver, worker_pool + + +def _project(status: str, fluxes: Optional[Dict[str, float]], + ax0: Dict[str, float], ax1: Dict[str, float]) -> Optional[Tuple[float, float]]: + if status != "optimal" or fluxes is None: + return None + x0 = sum(c * fluxes.get(r, 0.0) for r, c in ax0.items()) + x1 = sum(c * fluxes.get(r, 0.0) for r, c in ax1.items()) + return (round(x0, 9), round(x1, 9)) + + +def _dedupe(points: List[Tuple[float, float]], tol: float) -> List[Tuple[float, float]]: + unique_pts: List[Tuple[float, float]] = [] + for p in points: + if not any(abs(p[0] - q[0]) < tol and abs(p[1] - q[1]) < tol for q in unique_pts): + unique_pts.append(p) + return unique_pts + + +def _order_ccw(points: List[Tuple[float, float]]) -> List[Tuple[float, float]]: + cx = sum(p[0] for p in points) / len(points) + cy = sum(p[1] for p in points) / len(points) + return sorted(points, key=lambda p: math.atan2(p[1] - cy, p[0] - cx)), cx, cy + + +def _refine_step(vi, vj, cx, cy, ax0, ax1, opt_fn): + """One normal-bisection refinement attempt on edge (vi, vj). + Returns ('done', vi) if the edge is already tight enough / degenerate, + or ('split', (vi, p_new), (p_new, vj)) if a new vertex was found.""" + dx, dy = vj[0] - vi[0], vj[1] - vi[1] + nx, ny = dy, -dx # rotate 90 degrees + mid_x, mid_y = (vi[0] + vj[0]) / 2, (vi[1] + vj[1]) / 2 + if nx * (mid_x - cx) + ny * (mid_y - cy) < 0: + nx, ny = -nx, -ny + obj: Dict[str, float] = {} + for r, c in ax0.items(): + obj[r] = obj.get(r, 0.0) + nx * c + for r, c in ax1.items(): + obj[r] = obj.get(r, 0.0) + ny * c + p_new = opt_fn(obj, "maximize") + return vi, vj, nx, ny, p_new + + +def trace_polygon_rate_rate(solver: HighsRateSolver, ax0: Dict[str, float], ax1: Dict[str, float], + tol: float = 1e-8, max_depth: int = 50) -> List[Tuple[float, float]]: + """Sequential, depth-first version -- direct port of + lptools._trace_polygon_rate_rate onto a build-once HighsRateSolver.""" + + def opt(obj, sense): + status, _, fluxes = solver.optimize(obj, sense) + return _project(status, fluxes, ax0, ax1) + + extremes = [] + for coeff, sense in [(ax0, "maximize"), (ax0, "minimize"), (ax1, "maximize"), (ax1, "minimize")]: + p = opt(coeff, sense) + if p is not None: + extremes.append(p) + + if len(extremes) < 2: + return extremes if extremes else [(0.0, 0.0)] + + unique_pts = _dedupe(extremes, tol) + if len(unique_pts) == 1: + return unique_pts + + unique_pts, cx, cy = _order_ccw(unique_pts) + + diameter = max(((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** 0.5 for a in unique_pts for b in unique_pts) + min_edge = 1e-10 * diameter if diameter > 0 else 1e-10 + + def _refine(vi, vj, depth): + if depth > max_depth: + return [vi] + edge_len = ((vi[0] - vj[0]) ** 2 + (vi[1] - vj[1]) ** 2) ** 0.5 + if edge_len < min_edge: + return [vi] + vi_, vj_, nx, ny, p_new = _refine_step(vi, vj, cx, cy, ax0, ax1, opt) + if p_new is None: + return [vi] + dist = nx * (p_new[0] - vi[0]) + ny * (p_new[1] - vi[1]) + norm_len = (nx ** 2 + ny ** 2) ** 0.5 + if norm_len > 0: + dist /= norm_len + if dist > tol and edge_len > min_edge: + if ((abs(p_new[0] - vi[0]) < tol and abs(p_new[1] - vi[1]) < tol) or + (abs(p_new[0] - vj[0]) < tol and abs(p_new[1] - vj[1]) < tol)): + return [vi] + return _refine(vi, p_new, depth + 1) + _refine(p_new, vj, depth + 1) + return [vi] + + refined = [] + n = len(unique_pts) + for i in range(n): + refined.extend(_refine(unique_pts[i], unique_pts[(i + 1) % n], 0)) + + if refined and refined[0] != refined[-1]: + refined.append(refined[0]) + return refined + + +def trace_polygon_rate_rate_parallel(lp, reaction_ids: List[str], rxn_index: Dict[str, int], + ax0: Dict[str, float], ax1: Dict[str, float], + n_workers: int = 4, tol: float = 1e-8, + max_depth: int = 50) -> List[Tuple[float, float]]: + """Same algorithm as trace_polygon_rate_rate(), restructured as + breadth-first waves: every currently-too-coarse edge is refined + concurrently each pass. `lp` is the shared, read-only HighsLp from + build_rate_lp() -- each worker thread lazily builds its own + HighsRateSolver from it the first time that thread is used, and + reuses it for the rest of the trace (so warm-starting still applies + within a thread's own sequence of solves, just not across threads).""" + + thread_local = th.local() + + def get_solver() -> HighsRateSolver: + s = getattr(thread_local, "solver", None) + if s is None: + s = HighsRateSolver(lp, reaction_ids, rxn_index, []) + thread_local.solver = s + return s + + def opt(obj, sense): + status, _, fluxes = get_solver().optimize(obj, sense) + return _project(status, fluxes, ax0, ax1) + + with worker_pool(n_workers) as ex: + extremes = list(ex.map( + lambda args: opt(*args), + [(ax0, "maximize"), (ax0, "minimize"), (ax1, "maximize"), (ax1, "minimize")])) + extremes = [p for p in extremes if p is not None] + + if len(extremes) < 2: + return extremes if extremes else [(0.0, 0.0)] + + unique_pts = _dedupe(extremes, tol) + if len(unique_pts) == 1: + return unique_pts + + unique_pts, cx, cy = _order_ccw(unique_pts) + diameter = max(((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** 0.5 + for a in unique_pts for b in unique_pts) + min_edge = 1e-10 * diameter if diameter > 0 else 1e-10 + n = len(unique_pts) + + # state: ordered list of ('vertex', v) [finalized] or + # ('edge', vi, vj) [still needs refining] -- order here IS the + # polygon traversal order, preserved across waves. + state = [("edge", unique_pts[i], unique_pts[(i + 1) % n]) for i in range(n)] + + def refine_once(edge): + vi, vj = edge + edge_len = ((vi[0] - vj[0]) ** 2 + (vi[1] - vj[1]) ** 2) ** 0.5 + if edge_len < min_edge: + return ("done", vi) + vi_, vj_, nx, ny, p_new = _refine_step(vi, vj, cx, cy, ax0, ax1, opt) + if p_new is None: + return ("done", vi) + dist = nx * (p_new[0] - vi[0]) + ny * (p_new[1] - vi[1]) + norm_len = (nx ** 2 + ny ** 2) ** 0.5 + if norm_len > 0: + dist /= norm_len + if dist > tol and edge_len > min_edge: + if ((abs(p_new[0] - vi[0]) < tol and abs(p_new[1] - vi[1]) < tol) or + (abs(p_new[0] - vj[0]) < tol and abs(p_new[1] - vj[1]) < tol)): + return ("done", vi) + return ("split", (vi, p_new), (p_new, vj)) + return ("done", vi) + + depth = 0 + while depth <= max_depth and any(s[0] == "edge" for s in state): + edge_entries = [s for s in state if s[0] == "edge"] + outcomes = list(ex.map(lambda s: refine_once((s[1], s[2])), edge_entries)) + new_state = [] + oi = 0 + for s in state: + if s[0] == "vertex": + new_state.append(s) + else: + o = outcomes[oi] + oi += 1 + if o[0] == "done": + new_state.append(("vertex", o[1])) + else: + _, e1, e2 = o + new_state.append(("edge", e1[0], e1[1])) + new_state.append(("edge", e2[0], e2[1])) + state = new_state + depth += 1 + + refined = [s[1] for s in state] # 'vertex' -> v ; leftover 'edge' -> vi (matches max_depth fallback) + if refined and refined[0] != refined[-1]: + refined.append(refined[0]) + return refined + + +def polygon_area(vertices: List[Tuple[float, float]]) -> float: + """Shoelace formula; used for cross-checking traced polygons against + a known reference area, independent of exact vertex order/count.""" + pts = vertices[:-1] if len(vertices) > 1 and vertices[0] == vertices[-1] else vertices + n = len(pts) + if n < 3: + return 0.0 + s = 0.0 + for i in range(n): + x0, y0 = pts[i] + x1, y1 = pts[(i + 1) % n] + s += x0 * y1 - x1 * y0 + return abs(s) / 2.0 + + +# --------------------------------------------------------------------------- +# 3D, one sliced axis (rate or yield) + two rate axes traced per slice. +# --------------------------------------------------------------------------- + +def fix_rate_axis(coeffs: Dict[str, float]): + """make_fix for trace_3d_slice_polygon when the sliced axis is a plain + rate: pin `coeffs` itself to `value` (coeffs constant across slices, + only the RHS varies -- one changeRowBounds call per slice, no + changeCoeff needed after the first).""" + return lambda value: (coeffs, value) + + +def fix_yield_axis(num_coeffs: Dict[str, float], den_coeffs: Dict[str, float]): + """make_fix for trace_3d_slice_polygon when the sliced axis is a + yield num/den: pinning yield to `value` is the plain linear equality + `num - value*den = 0` on the rate variables (see + lptools._make_fix_constraint's 'yield' branch) -- no Charnes-Cooper + LFP needed for this, only for finding the yield axis's own min/max + range elsewhere in plot_flux_space.""" + def make_fix(value): + merged = dict(num_coeffs) + for r, c in den_coeffs.items(): + merged[r] = merged.get(r, 0.0) - c * value + return merged, 0.0 + return make_fix + + +def trace_3d_slice_polygon(lp, reaction_ids: List[str], rxn_index: Dict[str, int], + fix_slot_rows: List[int], make_fix, rate_ax0: Dict[str, float], + rate_ax1: Dict[str, float], values: List[float], + n_workers: int = 4) -> List[Tuple[float, List[Tuple[float, float]]]]: + """3D surface for 'one sliced axis + two rate axes': for each target + value in `values`, pin the sliced axis via `make_fix(value) -> + (coeffs, rhs)` (see fix_rate_axis / fix_yield_axis) using one + reserved fix-slot row, then trace the 2D rate-rate polygon on + (rate_ax0, rate_ax1) within that constrained LP. + + Slices are fully independent, so parallelism happens *across slices*, + each running a complete sequential trace_polygon_rate_rate() (tens of + solves) -- deliberately not trace_polygon_rate_rate_parallel() nested + inside, since refining edges *within* one polygon turned out to be + too fine-grained a unit of work for a thread pool to pay for itself + (see the real-model benchmark this was built to follow up on). + `lp` must have been built with at least one fix slot + (build_rate_lp(..., num_fix_slots=1) or more). + + Returns [(value, polygon_vertices), ...], one entry per input value, + in the same order as `values` (ex.map preserves input order). + """ + + if n_workers <= 1 or len(values) <= 1: + return trace_3d_slice_polygon_sequential(lp, reaction_ids, rxn_index, fix_slot_rows, + make_fix, rate_ax0, rate_ax1, values) + + # one solver per worker thread, reused across that thread's slices + # (so it keeps its warm-started basis between neighbouring slices, + # like the sequential version) rather than one passModel per slice + thread_local = th.local() + + def one_slice(value): + solver = getattr(thread_local, "solver", None) + if solver is None: + solver = HighsRateSolver(lp, reaction_ids, rxn_index, fix_slot_rows) + thread_local.solver = solver + coeffs, rhs = make_fix(value) + solver.set_fix_row(0, coeffs, rhs) + return value, trace_polygon_rate_rate(solver, rate_ax0, rate_ax1) + + with worker_pool(min(n_workers, len(values))) as ex: + return list(ex.map(one_slice, values)) + + +def trace_3d_slice_polygon_sequential(lp, reaction_ids: List[str], rxn_index: Dict[str, int], + fix_slot_rows: List[int], make_fix, rate_ax0: Dict[str, float], + rate_ax1: Dict[str, float], + values: List[float]) -> List[Tuple[float, List[Tuple[float, float]]]]: + """Same as trace_3d_slice_polygon but single-threaded -- the baseline + to benchmark the threaded version against, reusing one solver (and + its warm-started basis) across all slices instead of building a fresh + one per slice.""" + solver = HighsRateSolver(lp, reaction_ids, rxn_index, fix_slot_rows) + out = [] + for value in values: + coeffs, rhs = make_fix(value) + solver.set_fix_row(0, coeffs, rhs) + out.append((value, trace_polygon_rate_rate(solver, rate_ax0, rate_ax1))) + return out + + +# --------------------------------------------------------------------------- +# 2D adaptive boundary tracing for yield / mixed axes (HiGHS-native port of +# lptools._trace_boundary_adaptive). Unlike the all-rate polygon case, the +# boundary isn't traced as an exact convex hull -- the true feasible region +# in yield coordinates needn't be convex or even polygonal, so this instead +# adaptively bisects the x-axis and asks "what's the max/min y achievable at +# this x", refining wherever linear interpolation between known points +# isn't a good enough approximation yet. +# --------------------------------------------------------------------------- + +class RateAxis: + """A plain-rate axis for use with trace_boundary_adaptive. Reads/pins + values via `rate_solver` -- pass the *same* HighsRateSolver instance + (with its fix slot 0 reserved for axis 0) to every axis object used in + one trace_boundary_adaptive() call, so pinning axis 0 is visible to + whatever axis 1 optimizes against.""" + + def __init__(self, rate_solver: HighsRateSolver, coeffs: Dict[str, float]): + self.rate_solver = rate_solver + self.coeffs = coeffs + + def optimize(self, sense: str) -> Optional[float]: + status, value, _ = self.rate_solver.optimize(self.coeffs, sense) + return value if status == "optimal" else None + + def apply_fix(self, coeffs: Dict[str, float], rhs: float): + """Only relevant when this axis is used as axis 1: pins axis 0's + value on every solver this axis reads from. For a plain rate axis + that's just the shared rate_solver (the same instance the fix was + already conceptually "on"); this exists mainly so + trace_boundary_adaptive can treat RateAxis and YieldAxis + uniformly rather than special-casing which solver(s) need it.""" + self.rate_solver.set_fix_row(0, coeffs, rhs) + + def clear_fix(self): + """Reset fix slot 0 to inert on every solver this axis touches. + trace_boundary_adaptive calls this on axis 1 at the very start of + every run, so calling it repeatedly on the same (reused) solver + instances is safe -- without this, a solver reused across + multiple trace_boundary_adaptive() calls (e.g. benchmarking with + several repeats, or plot_flux_space reusing solvers across + several plots) would silently start each later run with + whatever fix value the *previous* run's deepest refinement probe + happened to leave behind, constraining x_min/x_max to something + wrong from the very first step.""" + self.rate_solver.clear_fix_row(0) + + def make_fix(self): + return fix_rate_axis(self.coeffs) + + +class YieldAxis: + """A yield (Charnes-Cooper) axis for use with trace_boundary_adaptive. + `rate_solver` (shared with the rest of the trace, same instance as any + RateAxis's) is used only to check which denominator sign(s) are + achievable *under whatever fix is currently active on it* -- that + check has to be redone at every x, since pinning axis 0 can change + which signs are reachable, unlike the 3D-slicing case where the + sliced axis was always plain-rate and this never came up. + `yield_solver` carries this axis's own Charnes-Cooper structure (built + with *this* axis's denominator) and must not be shared with a + different YieldAxis that has a different denominator.""" + + def __init__(self, rate_solver: HighsRateSolver, yield_solver, num: Dict[str, float], + den: Dict[str, float], den_tol: float = 1e-9, yield_lp_bundle=None, + rate_lp_bundle=None): + self.rate_solver = rate_solver + self.yield_solver = yield_solver + self.num = num + self.den = den + self.den_tol = den_tol + # Diagnostic-only knobs, both None by default (normal behavior: + # reuse self.yield_solver / self.rate_solver throughout, warm + # start included). Set either to test whether reusing that + # particular solver across the many sequential calls one boundary + # trace makes is where a correctness issue is coming from, as + # opposed to the achievable-sign logic or the LP construction + # itself (which a single freshly-built, lightly-used solver -- + # e.g. --boundary-probe-x's -- can't reveal a staleness problem + # in, since it only ever sees a handful of calls). + # + # yield_lp_bundle: optimize() rebuilds a fresh HighsYieldSolver + # from (lp, reaction_ids, rxn_index, den_row, t_col, + # fix_slot_rows) on every call instead of reusing self.yield_solver. + # + # rate_lp_bundle: _achievable_signs() rebuilds a fresh + # HighsRateSolver from (lp, reaction_ids, rxn_index, + # fix_slot_rows) on every call instead of reusing self.rate_solver + # for the denominator-sign check specifically. This is the one + # that --boundary-cold did NOT test: that diagnostic only + # cold-rebuilt the yield solver, leaving the rate solver used for + # the sign check as the same long-lived, many-times-reused + # instance in both its "warm" and "cold" runs. + self.yield_lp_bundle = yield_lp_bundle + self.rate_lp_bundle = rate_lp_bundle + self._pending_fix = None + + def _achievable_signs(self) -> List[float]: + rate_solver = self.rate_solver + if self.rate_lp_bundle is not None: + lp, reaction_ids, rxn_index, fix_slot_rows = self.rate_lp_bundle + rate_solver = HighsRateSolver(lp, reaction_ids, rxn_index, fix_slot_rows) + if self._pending_fix is not None: + coeffs, rhs = self._pending_fix + rate_solver.set_fix_row(0, coeffs, rhs) + status_min, min_den, _ = rate_solver.optimize(self.den, "minimize") + status_max, max_den, _ = rate_solver.optimize(self.den, "maximize") + signs = [] + if status_min == "unbounded" or (status_min == "optimal" and min_den < -self.den_tol): + signs.append(-1.0) + if status_max == "unbounded" or (status_max == "optimal" and max_den > self.den_tol): + signs.append(1.0) + return signs + + def optimize(self, sense: str) -> Optional[float]: + signs = self._achievable_signs() + if not signs: + return None + yield_solver = self.yield_solver + if self.yield_lp_bundle is not None: + lp, reaction_ids, rxn_index, den_row, t_col, fix_slot_rows = self.yield_lp_bundle + yield_solver = HighsYieldSolver(lp, reaction_ids, rxn_index, den_row, t_col, fix_slot_rows) + if self._pending_fix is not None: + coeffs, rhs = self._pending_fix + yield_solver.set_fix_row(0, coeffs, rhs) + best = None + for sign in signs: + status, value, fluxes, scalable = yield_solver.optimize(self.num, sense, sign) + if status == "optimal" and ( + best is None or + (sense == "maximize" and value > best) or + (sense == "minimize" and value < best)): + best = value + return best + + def apply_fix(self, coeffs: Dict[str, float], rhs: float): + """Only relevant when this axis is used as axis 1: pins axis 0's + value on *every* solver this axis reads from -- both the shared + rate_solver (used for the achievable-denominator-sign check) and + this axis's own yield_solver (used for the actual optimization). + Missing the second one would silently optimize over the whole + feasible region instead of the x-pinned slice of it while the + sign-check still (correctly) saw the pin -- exactly the kind of + two-solvers-drifting-out-of-sync bug this method exists to rule + out by construction.""" + self._pending_fix = (coeffs, rhs) # used by _achievable_signs() when rate_lp_bundle is set + if self.rate_lp_bundle is None: + self.rate_solver.set_fix_row(0, coeffs, rhs) + if self.yield_lp_bundle is None: + self.yield_solver.set_fix_row(0, coeffs, rhs) + + def clear_fix(self): + """See RateAxis.clear_fix -- same reasoning, both solvers this + axis can touch (only relevant for the non-bundle/reused-solver + case; a bundle rebuilds fresh and inert every call already).""" + self._pending_fix = None + if self.rate_lp_bundle is None: + self.rate_solver.clear_fix_row(0) + if self.yield_lp_bundle is None: + self.yield_solver.clear_fix_row(0) + + def make_fix(self): + return fix_yield_axis(self.num, self.den) + + +def trace_boundary_adaptive(rate_solver: HighsRateSolver, ax0, ax1, max_depth: int = 15, + tol: float = 1e-8, debug: Optional[dict] = None): + """HiGHS-native port of lptools._trace_boundary_adaptive: adaptively + traces the upper and lower boundary of {(x, y) : x = ax0's value, y = + ax1's value} by bisecting the x-axis wherever linear interpolation + between known points isn't accurate enough yet. Unlike + trace_polygon_rate_rate, this is an *approximation* (matching + plot_flux_space's own "approximate" labelling for any plot involving + a yield axis) -- it's not guaranteed to find every extreme point of a + non-convex or non-polygonal feasible region, only to refine until + linear interpolation between what it has found is within tolerance. + + `ax0`, `ax1`: RateAxis or YieldAxis instances (ax0 provides + make_fix(), ax1 provides optimize()). `rate_solver` must be the same + instance every axis object was built against, with fix slot 0 + reserved for pinning axis 0. + + `debug`, if given an empty dict, is filled in with diagnostics + (corner values, abs_tol used, and how many fix_and_opt calls came + back None on each boundary) -- pass one when a returned boundary + looks suspiciously sparse, since a small vertex count is consistent + with either genuine convergence *or* every refinement probe silently + failing (returning None, silently treated the same as "this edge is + already fine"), and those look identical from the return value alone. + + Returns (upper_boundary, lower_boundary), each a list of (x, y) + tuples ordered by x. + """ + + # Defensive check: both axis objects are documented to share + # `rate_solver`, but nothing in RateAxis/YieldAxis's own construction + # enforces that -- a caller could accidentally build ax0 or ax1 + # against a *different* solver instance, which would silently pin + # axis 0 on one solver while reading it back from another. Fail loudly + # here instead of producing a quietly-wrong boundary. + for name, ax in (("ax0", ax0), ("ax1", ax1)): + if getattr(ax, "rate_solver", None) is not rate_solver: + raise ValueError( + f"{name}.rate_solver is not the same instance as the `rate_solver` argument -- " + f"every axis object passed to trace_boundary_adaptive must share it.") + + ax1.clear_fix() # start from known-inert state -- see RateAxis/YieldAxis.clear_fix's + # docstring for why this matters when a solver is reused across calls + + none_counts = {"upper": 0, "lower": 0} + + def fix_and_opt(x_val, sense, which=None): + coeffs, rhs = ax0.make_fix()(x_val) + ax1.apply_fix(coeffs, rhs) + value = ax1.optimize(sense) + if value is None and which is not None: + none_counts[which] += 1 + return value + + x_min = ax0.optimize("minimize") + x_max = ax0.optimize("maximize") + if x_min is None or x_max is None: + if debug is not None: + debug["error"] = "ax0.optimize() returned None for x_min or x_max" + return [], [] + + y_min_at_xmin = fix_and_opt(x_min, "minimize", "lower") + y_max_at_xmin = fix_and_opt(x_min, "maximize", "upper") + y_min_at_xmax = fix_and_opt(x_max, "minimize", "lower") + y_max_at_xmax = fix_and_opt(x_max, "maximize", "upper") + + upper = [(x, y) for x, y in [(x_min, y_max_at_xmin), (x_max, y_max_at_xmax)] if y is not None] + lower = [(x, y) for x, y in [(x_min, y_min_at_xmin), (x_max, y_min_at_xmax)] if y is not None] + + if debug is not None: + debug["x_min"], debug["x_max"] = x_min, x_max + debug["corners"] = { + "y_min_at_xmin": y_min_at_xmin, "y_max_at_xmin": y_max_at_xmin, + "y_min_at_xmax": y_min_at_xmax, "y_max_at_xmax": y_max_at_xmax, + } + + if not upper and not lower: + return [], [] + + all_y = [y for _, y in upper + lower] + y_range = max(all_y) - min(all_y) if all_y else 0.0 + abs_tol = max(1e-6, 1e-3 * abs(y_range)) + if debug is not None: + debug["y_range"] = y_range + debug["abs_tol"] = abs_tol + + if debug is not None: + debug["probes_upper"] = [] + debug["probes_lower"] = [] + + def refine(boundary, sense, depth, which): + if depth > max_depth or len(boundary) < 2: + return boundary + new_boundary = [boundary[0]] + changed = False + for i in range(len(boundary) - 1): + x_left, y_left = boundary[i] + x_right, y_right = boundary[i + 1] + if abs(x_right - x_left) < tol: + new_boundary.append(boundary[i + 1]) + continue + x_mid = (x_left + x_right) / 2 + y_actual = fix_and_opt(x_mid, sense, which) + y_interp = (y_left + y_right) / 2 + if debug is not None: + debug[f"probes_{which}"].append({ + "depth": depth, "x_mid": x_mid, "y_actual": y_actual, "y_interp": y_interp, + "deviation": None if y_actual is None else abs(y_actual - y_interp), + "split": False if y_actual is None else abs(y_actual - y_interp) > abs_tol, + }) + if y_actual is None: + new_boundary.append(boundary[i + 1]) + continue + if abs(y_actual - y_interp) > abs_tol: + new_boundary.append((x_mid, y_actual)) + changed = True + new_boundary.append(boundary[i + 1]) + if changed: + return refine(new_boundary, sense, depth + 1, which) + return new_boundary + + upper = refine(upper, "maximize", 0, "upper") + lower = refine(lower, "minimize", 0, "lower") + if debug is not None: + debug["none_count_upper"] = none_counts["upper"] + debug["none_count_lower"] = none_counts["lower"] + return upper, lower + + +# --------------------------------------------------------------------------- +# 3D, all-rate polytope via face-normal refinement (HiGHS-native port of +# lptools._trace_polytope_3d_rate / _hull_face_polygons). Exact, like +# trace_polygon_rate_rate -- not an approximation, unlike anything +# involving a yield axis. +# --------------------------------------------------------------------------- + +def _ceil_dec(v: float, n: int) -> float: + """Round v UP to n decimals. Matches lptools.py's own ceil_dec: a + consistent rounding *direction* (not round-to-nearest) makes two LP + solutions that are really the same vertex, but differ by tiny + floating-point noise in either direction, much more likely to land on + the exact same rounded value and get deduplicated correctly.""" + return math.ceil(v * (10 ** n)) / (10 ** n) + + +def _project3(fluxes: Dict[str, float], coeffs3) -> Tuple[float, float, float]: + return tuple( + _ceil_dec(sum(c * fluxes.get(r, 0.0) for r, c in coeffs.items()), 9) + for coeffs in coeffs3 + ) + + +def _is_dup3(pt, pts, tol: float) -> bool: + return any(all(abs(pt[k] - q[k]) < tol for k in range(3)) for q in pts) + + +def hull_face_polygons(hull): + """HiGHS-independent port of lptools._hull_face_polygons: merges + coplanar ConvexHull simplices into polygon faces (vertex-index lists, + ordered counterclockwise viewed from outside), so a 3D plot can be + rendered without visible seams along the interior of each flat face. + """ + from collections import defaultdict + + face_groups = defaultdict(set) + for i, eq in enumerate(hull.equations): + key = tuple(round(v, 5) for v in eq) + for vi in hull.simplices[i]: + face_groups[key].add(vi) + + faces = [] + for eq_key, vertex_indices in face_groups.items(): + verts = list(vertex_indices) + if len(verts) < 3: + continue + normal = numpy.array(eq_key[:3]) + pts = hull.points[verts] + centroid = pts.mean(axis=0) + ref = numpy.array([1.0, 0.0, 0.0]) if abs(normal[0]) < 0.9 else numpy.array([0.0, 1.0, 0.0]) + u = ref - normal * normal.dot(ref) + u = u / (u.dot(u) ** 0.5) + v = numpy.array([ + normal[1] * u[2] - normal[2] * u[1], + normal[2] * u[0] - normal[0] * u[2], + normal[0] * u[1] - normal[1] * u[0], + ]) + angles = [math.atan2((pt - centroid).dot(v), (pt - centroid).dot(u)) for pt in pts] + order = sorted(range(len(verts)), key=lambda i: angles[i]) + faces.append([verts[i] for i in order]) + return faces + + +def trace_polytope_3d_rate(solver: HighsRateSolver, ax0: Dict[str, float], ax1: Dict[str, float], + ax2: Dict[str, float], tol: float = 1e-8, max_iters: int = 20): + """Sequential, direct port of lptools._trace_polytope_3d_rate: exact + 3D convex polytope for three rate axes. Finds 6 initial extremes + (min/max of each axis), builds a convex hull, then repeatedly + optimizes along each current face's outward normal and adds any + genuinely new vertex found, until a pass adds nothing. + + Returns (vertices, triangles, face_polygons): + vertices: list of (x, y, z) tuples + triangles: hull.simplices as a list of 3-tuples of vertex indices + face_polygons: merged coplanar faces (see hull_face_polygons), or + None if fewer than 4 vertices were found (degenerate) + """ + import scipy.spatial as sp_spatial + + coeffs3 = (ax0, ax1, ax2) + + def opt_point(obj, sense="maximize"): + status, _, fluxes = solver.optimize(obj, sense) + return None if status != "optimal" else _project3(fluxes, coeffs3) + + vertices = [] + for coeffs in coeffs3: + for sense in ("maximize", "minimize"): + pt = opt_point(coeffs, sense) + if pt is not None and not _is_dup3(pt, vertices, tol): + vertices.append(pt) + + if len(vertices) < 4: + return vertices, [], None + + for _ in range(max_iters): + try: + hull = sp_spatial.ConvexHull(numpy.array(vertices)) + except sp_spatial.QhullError: + break + new_found = False + for eq in hull.equations: + normal = eq[:3] + obj: Dict[str, float] = {} + for k in range(3): + for r, c in coeffs3[k].items(): + obj[r] = obj.get(r, 0.0) + normal[k] * c + pt = opt_point(obj, "maximize") + if pt is not None and not _is_dup3(pt, vertices, tol): + vertices.append(pt) + new_found = True + if not new_found: + break + + try: + hull = sp_spatial.ConvexHull(numpy.array(vertices)) + triang = hull.simplices.tolist() + face_polys = hull_face_polygons(hull) + except sp_spatial.QhullError: + triang, face_polys = [], None + return vertices, triang, face_polys + + +def trace_polytope_3d_rate_parallel(lp, reaction_ids: List[str], rxn_index: Dict[str, int], + ax0: Dict[str, float], ax1: Dict[str, float], + ax2: Dict[str, float], n_workers: int = 4, + tol: float = 1e-8, max_iters: int = 20): + """Same as trace_polytope_3d_rate, but each refinement pass solves + every current hull face concurrently via a thread pool (each worker + thread lazily builds and reuses its own HighsRateSolver from the + shared, read-only `lp`). This is the right granularity for threading + here: a hull's face count grows with vertex count and each face is a + full LP solve, the same reasoning that made 3D slicing pay off where + refining edges *within* one 2D polygon didn't (too few, too cheap + tasks per wave to amortize thread-pool overhead against).""" + + coeffs3 = (ax0, ax1, ax2) + thread_local = th.local() + + def get_solver() -> HighsRateSolver: + s = getattr(thread_local, "solver", None) + if s is None: + s = HighsRateSolver(lp, reaction_ids, rxn_index, []) + thread_local.solver = s + return s + + def opt_point(obj, sense="maximize"): + status, _, fluxes = get_solver().optimize(obj, sense) + return None if status != "optimal" else _project3(fluxes, coeffs3) + + with worker_pool(n_workers) as ex: + extreme_args = [(coeffs, sense) for coeffs in coeffs3 for sense in ("maximize", "minimize")] + vertices = [] + for pt in ex.map(lambda a: opt_point(*a), extreme_args): + if pt is not None and not _is_dup3(pt, vertices, tol): + vertices.append(pt) + + if len(vertices) < 4: + return vertices, [], None + + import scipy.spatial as sp_spatial + for _ in range(max_iters): + try: + hull = sp_spatial.ConvexHull(numpy.array(vertices)) + except sp_spatial.QhullError: + break + objs = [] + for eq in hull.equations: + normal = eq[:3] + obj: Dict[str, float] = {} + for k in range(3): + for r, c in coeffs3[k].items(): + obj[r] = obj.get(r, 0.0) + normal[k] * c + objs.append(obj) + new_found = False + for pt in ex.map(lambda o: opt_point(o, "maximize"), objs): + if pt is not None and not _is_dup3(pt, vertices, tol): + vertices.append(pt) + new_found = True + if not new_found: + break + + try: + hull = sp_spatial.ConvexHull(numpy.array(vertices)) + triang = hull.simplices.tolist() + face_polys = hull_face_polygons(hull) + except sp_spatial.QhullError: + triang, face_polys = [], None + return vertices, triang, face_polys + + +# --------------------------------------------------------------------------- +# 3D, 2+ yield axes: grid-scanning fallback (HiGHS-native port of +# plot_flux_space's least-precise, least-used 3D case -- used only when +# neither the exact all-rate polytope tracer nor the 1-yield slicing +# tracer applies). Approximate, like everything else touching a yield +# axis, and coarser than either of those: this is plot_flux_space's own +# fallback of last resort, not something to reach for by choice. +# --------------------------------------------------------------------------- + +class GridAxis: + """Generalizes RateAxis/YieldAxis for trace_grid_3d, which (unlike + trace_boundary_adaptive's 2-axis pattern) needs up to TWO + simultaneous fixes active at once: axis 0's value AND axis 1's value, + both pinned when reading axis 2. RateAxis/YieldAxis hardcode fix slot + 0; this takes an explicit slot number on every fix operation instead, + so the same axis object can have two independent pins applied (one + per slot) without them overwriting each other. + + For a rate axis: pass ax_type='rate', coeffs=. For a yield axis: ax_type='yield', coeffs=, + den=, yield_solver=.""" + + def __init__(self, ax_type: str, rate_solver: HighsRateSolver, coeffs: Dict[str, float], + den: Optional[Dict[str, float]] = None, yield_solver=None, den_tol: float = 1e-9): + self.ax_type = ax_type + self.rate_solver = rate_solver + self.coeffs = coeffs + self.den = den + self.yield_solver = yield_solver + self.den_tol = den_tol + + def make_fix(self): + if self.ax_type == "rate": + return fix_rate_axis(self.coeffs) + return fix_yield_axis(self.coeffs, self.den) + + def apply_fix(self, slot: int, coeffs: Dict[str, float], rhs: float): + # Applying to a slot that doesn't exist must stay LOUD: silently + # skipping it would mean this axis quietly optimizes without one + # of the pins it was supposed to have -- a wrong answer that + # looks like a right one. + if self.ax_type == "yield" and slot >= len(self.yield_solver.fix_slot_rows): + raise ValueError( + f"GridAxis (yield) was built with only {len(self.yield_solver.fix_slot_rows)} " + f"fix slot(s) on its yield solver but apply_fix(slot={slot}) was requested -- " + f"build it with num_fix_slots >= {slot + 1}.") + self.rate_solver.set_fix_row(slot, coeffs, rhs) + if self.ax_type == "yield": + self.yield_solver.set_fix_row(slot, coeffs, rhs) + + def clear_fix(self, slot: int): + """Unlike apply_fix, tolerant of a slot this axis's yield solver + doesn't have: nothing could ever have been pinned there, so + "clearing" it is trivially already true (ax1 in trace_grid_3d + legitimately has only slot 0 on its yield solver, but shares + the rate solver -- which does have slot 1 -- with ax2).""" + self.rate_solver.clear_fix_row(slot) + if self.ax_type == "yield" and slot < len(self.yield_solver.fix_slot_rows): + self.yield_solver.clear_fix_row(slot) + + def optimize(self, sense: str) -> Optional[float]: + if self.ax_type == "rate": + status, value, _ = self.rate_solver.optimize(self.coeffs, sense) + return value if status == "optimal" else None + status_min, min_den, _ = self.rate_solver.optimize(self.den, "minimize") + status_max, max_den, _ = self.rate_solver.optimize(self.den, "maximize") + signs = [] + if status_min == "unbounded" or (status_min == "optimal" and min_den < -self.den_tol): + signs.append(-1.0) + if status_max == "unbounded" or (status_max == "optimal" and max_den > self.den_tol): + signs.append(1.0) + if not signs: + return None + best = None + for sign in signs: + status, value, fluxes, scalable = self.yield_solver.optimize(self.coeffs, sense, sign) + if status == "optimal" and ( + best is None or + (sense == "maximize" and value > best) or + (sense == "minimize" and value < best)): + best = value + return best + + +def _triangulate_strips(strip_a: List[int], strip_b: List[int], datapoints: List[List[float]], + triang: List[List[int]], flip_winding: bool = False): + """Verbatim port of lptools._triangulate_strips -- pure geometry/ + indexing, no solver dependency at all. Connects two ordered index + strips (a "top" contour and a "bottom" contour, say) into a triangle + strip by always advancing whichever side has the shorter diagonal.""" + if len(strip_a) < 1 or len(strip_b) < 1: + return + i, j = 0, 0 + while i < len(strip_a) - 1 or j < len(strip_b) - 1: + if i >= len(strip_a) - 1: + tri = [strip_a[i], strip_b[j], strip_b[j + 1]] + j += 1 + elif j >= len(strip_b) - 1: + tri = [strip_a[i], strip_b[j], strip_a[i + 1]] + i += 1 + else: + pa_next, pb_next = datapoints[strip_a[i + 1]], datapoints[strip_b[j + 1]] + pa_curr, pb_curr = datapoints[strip_a[i]], datapoints[strip_b[j]] + d1 = sum((pa_next[k] - pb_curr[k]) ** 2 for k in range(len(pa_next))) + d2 = sum((pa_curr[k] - pb_next[k]) ** 2 for k in range(len(pa_curr))) + if d1 < d2: + tri = [strip_a[i], strip_b[j], strip_a[i + 1]] + i += 1 + else: + tri = [strip_a[i], strip_b[j], strip_b[j + 1]] + j += 1 + if flip_winding: + tri = [tri[0], tri[2], tri[1]] + triang.append(tri) + + +def _grid_x_slice(ax0: GridAxis, ax1: GridAxis, ax2: GridAxis, x_val: float, + points: int, y_range_global: float): + """One x-slice of trace_grid_3d: pin ax0 = x_val, find ax1's range, + sample it, and read ax2's min/max at each sample. Self-contained + with respect to fix state (clears slot 1, overwrites slot 0), so + slices can run in any order or on different axis/solver sets. + Returns (x_val, upper_slice, lower_slice) or None if infeasible.""" + # Slot 1 may still be pinned to the previous slice's last y_val + # (ax2.apply_fix(1, ...) below); when ax1 and ax2 share a rate + # solver, ax1's range-finding reads that slot, so clear it first. + ax1.clear_fix(1) + ax2.clear_fix(1) + + coeffs0, rhs0 = ax0.make_fix()(x_val) + ax1.apply_fix(0, coeffs0, rhs0) + ax2.apply_fix(0, coeffs0, rhs0) + + y_lo = ax1.optimize("minimize") + y_hi = ax1.optimize("maximize") + if y_lo is None or y_hi is None: + return None + + if abs(y_hi - y_lo) < 1e-10: + y_space = [y_lo] + else: + # small slack so solver noise in y_hi - y_lo (e.g. 1.999999999999999 + # for a full-width slice) can't truncate the count one point short + n_pts = max(3, int(points * abs(y_hi - y_lo) / y_range_global + 1e-6)) + n_pts = min(n_pts, points) + y_space = [y_lo + (y_hi - y_lo) * i / (n_pts - 1) for i in range(n_pts)] if n_pts > 1 else [y_lo] + + upper_slice, lower_slice = [], [] + for y_val in y_space: + coeffs1, rhs1 = ax1.make_fix()(y_val) + ax2.apply_fix(1, coeffs1, rhs1) + z_lo = ax2.optimize("minimize") + z_hi = ax2.optimize("maximize") + if z_lo is not None and z_hi is not None: + upper_slice.append((y_val, z_hi)) + lower_slice.append((y_val, z_lo)) + return (x_val, upper_slice, lower_slice) if upper_slice else None + + +def trace_grid_3d(ax0: GridAxis, ax1: GridAxis, ax2: GridAxis, + x_limits: Tuple[float, float], y_range_global: float, + points: int = 25, n_workers: int = 1, make_axes=None): + """HiGHS-native port of plot_flux_space's 3D 2+-yield grid-scanning + fallback: scans a grid of ax0 values, finds ax1's achievable range at + each, samples a grid of ax1 values there, and for each (ax0, ax1) + pair finds ax2's achievable range -- building top and bottom + surfaces stitched into a triangle mesh via _triangulate_strips. + + ax1 must accept a fix on slot 0 (for pinning ax0). ax2 must accept + fixes on slots 0 AND 1 simultaneously (ax0 and ax1 both pinned when + reading ax2's range) -- so if ax1 and/or ax2 are yield-typed, their + own HighsYieldSolvers must have been built with num_fix_slots=2 (or + more), not the single slot trace_boundary_adaptive needs. + + x_limits: ax0's own unconstrained (min, max) range to scan over. + y_range_global: ax1's own unconstrained value range (max - min), + used only to scale how many y-points to sample per x-slice (a wide + x-slice's y-range gets proportionally more points, capped at + `points`) -- pass max(1e-10, abs(ax1_max - ax1_min)) to avoid a + division by zero if ax1 happens to be degenerate. + + Threading: x-slices are independent (see _grid_x_slice), so with + n_workers > 1 and a `make_axes() -> (ax0, ax1, ax2)` factory they + are scanned concurrently, each worker thread calling make_axes() + once for its own private solvers (never shared across threads). + Without make_axes, or with n_workers <= 1, the passed axes are used + sequentially. Results are returned in x order either way. + + Returns (datapoints, triang) -- datapoints as [x, y, z] lists. + """ + x_lo, x_hi = x_limits + x_space = [x_lo] if points <= 1 else [x_lo + (x_hi - x_lo) * i / (points - 1) for i in range(points)] + + # Start from a known-inert state on both slots, for every solver + # either axis reads from. Strictly redundant with the per-slice reset + # below (slot 1 is cleared at the top of every iteration and slot 0 is + # overwritten immediately), so this doesn't change behavior -- it just + # makes the "starts clean" precondition explicit rather than dependent + # on the loop body's ordering, matching trace_boundary_adaptive. + def reset(a1, a2): + # known-inert fix state on both slots for every solver involved + for slot in (0, 1): + a1.clear_fix(slot) + a2.clear_fix(slot) + + if n_workers > 1 and make_axes is not None and len(x_space) > 1: + thread_local = th.local() + + def scan(x_val): + axs = getattr(thread_local, "axes", None) + if axs is None: + axs = make_axes() + reset(axs[1], axs[2]) + thread_local.axes = axs + return _grid_x_slice(axs[0], axs[1], axs[2], x_val, points, y_range_global) + + with worker_pool(min(n_workers, len(x_space))) as ex: + results = list(ex.map(scan, x_space)) + else: + reset(ax1, ax2) + results = [_grid_x_slice(ax0, ax1, ax2, x_val, points, y_range_global) for x_val in x_space] + slices = [r for r in results if r is not None] + + if not slices: + return [], [] + + datapoints: List[List[float]] = [] + datapoints_top: List[List[int]] = [] + datapoints_bottom: List[List[int]] = [] + for x_val, upper_slice, lower_slice in slices: + top_ids, bot_ids = [], [] + for (y_val, z_hi), (_, z_lo) in zip(upper_slice, lower_slice): + top_ids.append(len(datapoints)) + datapoints.append([x_val, y_val, z_hi]) + bot_ids.append(len(datapoints)) + datapoints.append([x_val, y_val, z_lo]) + datapoints_top.append(top_ids) + datapoints_bottom.append(bot_ids) + + triang: List[List[int]] = [] + for s in range(len(slices) - 1): + _triangulate_strips(datapoints_top[s], datapoints_top[s + 1], datapoints, triang) + _triangulate_strips(datapoints_bottom[s], datapoints_bottom[s + 1], datapoints, triang, flip_winding=True) + front_top = [t[0] for t in datapoints_top] + front_bot = [b[0] for b in datapoints_bottom] + _triangulate_strips(front_top, front_bot, datapoints, triang, flip_winding=True) + back_top = [t[-1] for t in datapoints_top] + back_bot = [b[-1] for b in datapoints_bottom] + _triangulate_strips(back_top, back_bot, datapoints, triang) + _triangulate_strips(datapoints_top[0], datapoints_bottom[0], datapoints, triang) + _triangulate_strips(datapoints_top[-1], datapoints_bottom[-1], datapoints, triang, flip_winding=True) + + return datapoints, triang diff --git a/cnapy/scenario_spreadsheet.py b/cnapy/scenario_spreadsheet.py new file mode 100644 index 00000000..dd236838 --- /dev/null +++ b/cnapy/scenario_spreadsheet.py @@ -0,0 +1,324 @@ +"""Reading and writing CNApy scenarios as spreadsheets (.xlsx, works with Excel and LibreOffice). + +The workbook has the sheets General, Fluxes, Reactions, Constraints, Annotations and Help. +In each data sheet row 1 is a header, rows whose first cell is empty or starts with '#' +are skipped and missing sheets are treated as empty. read_scenario_xlsx returns a dictionary +in the same format as the JSON content of a .scen file so that it can be loaded the same way. +""" +from math import isinf +from typing import Dict, List + +import cobra +import openpyxl +from openpyxl.styles import Font, Alignment +from openpyxl.worksheet.datavalidation import DataValidation +from openpyxl.utils import get_column_letter +from straindesign.parse_constr import linexpr2dict, linexprdict2str, lineq2list + +GENERAL = "General" +FLUXES = "Fluxes" +REACTIONS = "Reactions" +CONSTRAINTS = "Constraints" +ANNOTATIONS = "Annotations" +HELP = "Help" + +DESCRIPTION = "Description" +OBJECTIVE = "Objective" +DIRECTION = "Direction" +USE_OBJECTIVE = "Use scenario objective" + +HEADERS = { + GENERAL: ["Setting", "Value"], + FLUXES: ["Reaction ID", "Lower bound", "Upper bound", "Pinned"], + REACTIONS: ["Reaction ID", "Equation", "Lower bound", "Upper bound"], + CONSTRAINTS: ["Constraint"], + ANNOTATIONS: ["Reaction ID", "Key", "Value"], +} +COLUMN_WIDTHS = { + GENERAL: [24, 80], + FLUXES: [24, 14, 14, 10], + REACTIONS: [24, 80, 14, 14], + CONSTRAINTS: [80], + ANNOTATIONS: [24, 24, 40], +} + +EXAMPLES = { + FLUXES: [["# EX_glc__D_e", -10, -10, "x"], ["# EX_o2_e", -5, 0, ""], ["# BIOMASS", "", "", "x"]], + REACTIONS: [["# EX_prod", "prod_c -->", 0, 1000], ["# PROD", "2 pyr_c + nadh_c --> prod_c + nad_c", "", ""]], + CONSTRAINTS: [["# EX_prod + 0.5 EX_glc__D_e >= 0"]], + ANNOTATIONS: [["# PROD", "ec-code", "1.1.1.1"]], +} + +HELP_TEXT = """\ +How to use this CNApy scenario spreadsheet + +Load it in CNApy with Scenario > Load scenario... (choose the .xlsx file). +When editing in LibreOffice keep the .xlsx format when saving. + +General rules +- Row 1 of each sheet is a header and is not read. +- Rows whose first cell is empty or starts with # are ignored (use # for comments or to disable a row). +- Sheets that you do not need can be left empty or deleted. This Help sheet is not read. +- Bounds may be numbers or inf / -inf. + +Fluxes: scenario flux values of model reactions and of the reactions from the Reactions sheet. +- Give lower and upper bound; if only one of them is given the flux is fixed to that value. +- Pinned: x (or yes/true/1) pins the reaction to the top of the reaction list. +- A row with a reaction ID, no bounds and a mark in Pinned only pins the reaction. + +General: settings in column B, identified by the name in column A. +- Description: free text describing the scenario. +- Objective: linear expression of reaction IDs, e.g. BIOMASS or 2 R1 - 0.5 R3. +- Direction: max or min. +- Use scenario objective: yes or no; when empty, yes if an objective is given. + +Reactions: additional reactions that are only part of this scenario. +- Equation in COBRApy notation, e.g. 2 pyr_c + nadh_c --> prod_c + nad_c (also <=> and <--). +- Unknown metabolites are created when the scenario is applied. +- If the bounds are left empty they are derived from the arrow in the equation. + +Constraints: one linear (in)equality per row, e.g. EX_prod + 0.5 EX_glc__D_e >= 0 (also <= and =). + +Annotations: reaction annotations (key and value) that are set while the scenario is active. +""" + +_TRUE_STRINGS = {"x", "yes", "y", "true", "1"} +_FALSE_STRINGS = {"no", "n", "false", "0"} + + +class ScenarioSpreadsheetError(Exception): + def __init__(self, messages: List[str]): + super().__init__("\n".join(messages)) + self.messages = messages + + +def _number_cell(value: float): + # infinite values cannot be stored as numbers in xlsx files + if isinf(value): + return "inf" if value > 0 else "-inf" + return value + + +def _add_sheet(wb: openpyxl.Workbook, title: str, rows: List[List]): + ws = wb.create_sheet(title) + ws.append(HEADERS[title]) + for cell in ws[1]: + cell.font = Font(bold=True) + ws.freeze_panes = "A2" + for i, width in enumerate(COLUMN_WIDTHS[title], start=1): + ws.column_dimensions[get_column_letter(i)].width = width + for row in rows: + ws.append(row) + return ws + + +def write_scenario_xlsx(scenario, filename: str, include_examples: bool = False): + wb = openpyxl.Workbook() + wb.remove(wb.active) + + rows = [[reac_id, _number_cell(lb), _number_cell(ub), "x" if reac_id in scenario.pinned_reactions else ""] + for reac_id, (lb, ub) in scenario.items()] + rows += [[reac_id, "", "", "x"] for reac_id in sorted(scenario.pinned_reactions) if reac_id not in scenario] + _add_sheet(wb, FLUXES, (EXAMPLES[FLUXES] if include_examples else []) + rows) + + ws = _add_sheet(wb, GENERAL, [ + [DESCRIPTION, scenario.description], + [OBJECTIVE, linexprdict2str(scenario.objective_coefficients)], + [DIRECTION, scenario.objective_direction], + [USE_OBJECTIVE, "yes" if scenario.use_scenario_objective else "no"], + [], + ["# Instructions can be found in the Help sheet."]]) + ws["B2"].alignment = Alignment(wrap_text=True, vertical="top") + for cell, choices in (("B4", '"max,min"'), ("B5", '"yes,no"')): + dv = DataValidation(type="list", formula1=choices, allow_blank=True) + ws.add_data_validation(dv) + dv.add(cell) + + rows = [] + for reac_id, (metabolites, lb, ub) in scenario.reactions.items(): + reaction = cobra.Reaction(reac_id, lower_bound=lb, upper_bound=ub) + reaction.add_metabolites({cobra.Metabolite(met_id): coeff for met_id, coeff in metabolites.items()}) + rows.append([reac_id, reaction.build_reaction_string(), _number_cell(lb), _number_cell(ub)]) + _add_sheet(wb, REACTIONS, (EXAMPLES[REACTIONS] if include_examples else []) + rows) + + rows = [[linexprdict2str(constr[0]) + " " + constr[1] + " " + str(constr[2])] + for constr in scenario.constraints if constr[0] is not None] + _add_sheet(wb, CONSTRAINTS, (EXAMPLES[CONSTRAINTS] if include_examples else []) + rows) + + rows = [[a["reaction_id"], a["key"], a["value"]] for a in scenario.annotations] + _add_sheet(wb, ANNOTATIONS, (EXAMPLES[ANNOTATIONS] if include_examples else []) + rows) + + ws = wb.create_sheet(HELP) + ws.column_dimensions["A"].width = 120 + for line in HELP_TEXT.splitlines(): + ws.append([line]) + ws["A1"].font = Font(bold=True, size=14) + + wb.active = 0 + wb.save(filename) + + +def _text(value) -> str: + if value is None: + return "" + if isinstance(value, float) and value.is_integer(): + value = int(value) + return str(value).strip() + + +class _SheetReader: + """Collects the error messages of all sheets so that they can be reported together.""" + + def __init__(self, filename: str): + self.wb = openpyxl.load_workbook(filename, read_only=True, data_only=True) + self.sheets = {name.casefold(): name for name in self.wb.sheetnames} + self.errors: List[str] = [] + self.sheet = "" + + def rows(self, title: str): + """yields (row number, row values) of the data rows, padded to the header length""" + self.sheet = title + name = self.sheets.get(title.casefold()) + if name is None: + return + num_columns = len(HEADERS[title]) + for row_number, row in enumerate(self.wb[name].iter_rows(min_row=2, values_only=True), start=2): + row = list(row[:num_columns]) + [None]*(num_columns - len(row)) + first = _text(row[0]) + if len(first) > 0 and not first.startswith("#"): + yield row_number, row + + def error(self, row_number: int, column: int, message: str): + self.errors.append(f"{self.sheet}!{get_column_letter(column + 1)}{row_number}: {message}") + + def number(self, row_number: int, row: List, column: int): + value = row[column] + if value is None or isinstance(value, bool): + if value is not None: + self.error(row_number, column, f"'{value}' is not a number") + return None + if isinstance(value, (int, float)): + return float(value) + text = _text(value) + if len(text) == 0: + return None + try: + return float(text.replace(",", ".")) + except ValueError: + self.error(row_number, column, f"'{text}' is not a number") + return None + + def flag(self, row_number: int, row: List, column: int, default=False) -> bool: + value = row[column] + if isinstance(value, bool): + return value + text = _text(value).casefold() + if len(text) == 0: + return default + if text in _TRUE_STRINGS: + return True + if text not in _FALSE_STRINGS: + self.error(row_number, column, f"'{_text(value)}' is not understood, use yes or no") + return False + + +def read_scenario_xlsx(filename: str, model_reaction_ids) -> Dict: + """Reads a scenario spreadsheet and returns it in the format of a .scen JSON dictionary. + model_reaction_ids are needed to parse the objective and the constraints; all errors + found are collectively raised as ScenarioSpreadsheetError.""" + reader = _SheetReader(filename) + + fluxes = {} + pinned_reactions = [] + for row_number, row in reader.rows(FLUXES): + reac_id = _text(row[0]) + num_errors = len(reader.errors) + lb = reader.number(row_number, row, 1) + ub = reader.number(row_number, row, 2) + pinned = reader.flag(row_number, row, 3) + if lb is None and ub is None: + if not pinned and len(reader.errors) == num_errors: + reader.error(row_number, 1, f"no flux value given for {reac_id}") + else: + if reac_id in fluxes: + reader.error(row_number, 0, f"flux value for {reac_id} is given more than once") + if lb is None: + lb = ub + elif ub is None: + ub = lb + if lb > ub: + reader.error(row_number, 1, f"lower bound {lb} is greater than upper bound {ub}") + fluxes[reac_id] = (lb, ub) + if pinned: + pinned_reactions.append(reac_id) + + reactions = {} + model = cobra.Model() # only used to parse the equations + for row_number, row in reader.rows(REACTIONS): + reac_id = _text(row[0]) + if reac_id in reactions: + reader.error(row_number, 0, f"reaction {reac_id} is defined more than once") + continue + reaction = cobra.Reaction(reac_id) + model.add_reactions([reaction]) + try: + reaction.build_reaction_from_string(_text(row[1]), verbose=False) + except Exception as e: + reader.error(row_number, 1, f"cannot parse equation '{_text(row[1])}' ({e})") + lb = reader.number(row_number, row, 2) + ub = reader.number(row_number, row, 3) + reactions[reac_id] = [{m.id: c for m, c in reaction.metabolites.items()}, + reaction.lower_bound if lb is None else lb, + reaction.upper_bound if ub is None else ub] + + reaction_ids = list(model_reaction_ids) + [r for r in reactions if r not in model_reaction_ids] + + constraints = [] + for row_number, row in reader.rows(CONSTRAINTS): + try: + constraints.append(list(lineq2list([_text(row[0])], reaction_ids)[0])) + except Exception as e: + reader.error(row_number, 0, f"cannot parse constraint '{_text(row[0])}' ({e})") + + annotations = [] + for row_number, row in reader.rows(ANNOTATIONS): + key = _text(row[1]) + if len(key) == 0: + reader.error(row_number, 1, "annotation key is missing") + annotations.append({"reaction_id": _text(row[0]), "key": key, "value": _text(row[2])}) + + general = {} + for row_number, row in reader.rows(GENERAL): + general[_text(row[0]).casefold()] = (row_number, row) + description = "" + if DESCRIPTION.casefold() in general: + value = general[DESCRIPTION.casefold()][1][1] + description = "" if value is None else str(value) + objective_coefficients = {} + if OBJECTIVE.casefold() in general: + row_number, row = general[OBJECTIVE.casefold()] + try: + objective_coefficients = linexpr2dict(_text(row[1]), reaction_ids) + except Exception as e: + reader.error(row_number, 1, f"cannot parse objective '{_text(row[1])}' ({e})") + objective_direction = "max" + if DIRECTION.casefold() in general: + row_number, row = general[DIRECTION.casefold()] + direction = _text(row[1]).casefold() + if direction in ("min", "minimize", "minimise"): + objective_direction = "min" + elif direction not in ("", "max", "maximize", "maximise"): + reader.error(row_number, 1, f"'{_text(row[1])}' is not a valid direction, use max or min") + use_scenario_objective = len(objective_coefficients) > 0 + if USE_OBJECTIVE.casefold() in general: + row_number, row = general[USE_OBJECTIVE.casefold()] + use_scenario_objective = reader.flag(row_number, row, 1, default=use_scenario_objective) + + reader.wb.close() + if len(reader.errors) > 0: + raise ScenarioSpreadsheetError(reader.errors) + + return {'fluxes': fluxes, 'pinned_reactions': pinned_reactions, 'description': description, + 'objective_direction': objective_direction, 'objective_coefficients': objective_coefficients, + 'use_scenario_objective': use_scenario_objective, 'reactions': reactions, + 'constraints': constraints, 'annotations': annotations, 'version': 3} diff --git a/cnapy/tests/README.md b/cnapy/tests/README.md new file mode 100644 index 00000000..0e377f78 --- /dev/null +++ b/cnapy/tests/README.md @@ -0,0 +1,158 @@ +# CNApy FBA/FVA CI tests + +Two layers of coverage for `MainWindow.fba()` / `MainWindow.fva()` +(cnapy/gui_elements/main_window.py): + +- `test_core_units.py` — fast, Qt-free unit tests of the actual functions + those slots delegate to (`cnapy.core_gui.model_optimization_with_exceptions`, + `cnapy.core.multi_threaded_HiGHS_FVA`), checked against cobrapy's own + `optimize()` / `flux_variability_analysis()` as ground truth on the ECC2 + model. +- `test_mainwindow_slots.py` — calls the real, unbound `MainWindow.fba` / + `MainWindow.fva` methods against a minimal duck-typed stand-in object + (`fake_main_window` fixture) that only implements the attributes those + methods touch (`.appdata`, `.centralWidget()`, `.statusBar()`, + `.solver_status_display`). This exercises the actual shipped scenario + handling / comp_values population / infinite-bound conversion without + constructing the full MainWindow (menus, map view, embedded console, + QWebEngineView, ...). + +Further functional tests, all without a full MainWindow: + +- `test_scenario_files.py` — `Scenario.save`/`load` for `.scen` (JSON) and + `.val` files: round trip of every field, `merge=True`, unknown IDs and + the `R_` prefix, legacy flux-only files, `parse_scenario`. +- `test_scenario_undo_redo.py` — the scenario history in `AppData` and the + unbound `MainWindow.undo_scenario_edit` / `redo_scenario_edit` / + `update_undo_redo_actions` / `discard_failed_scenario_load`. +- `test_project_files.py` — writing and reopening `.cna` projects + (`write_project_file` / `open_project`, SBML and JSON model) with a CNApy + and an Escher map, broken project files, SBML/JSON export round trips. +- `test_core_functions.py` — `highs_yopt`, `make_scenario_feasible` (LP and + OSQP), `element_exchange_balance`, `check_biomass_weight`, `replace_ids` + and `FluxVectorContainer`. The optlang-based functions run with GLPK, + because the pip versions of Gurobi/CPLEX have size-limited licenses. +- `test_efm_mcs.py` — EFMs (efmtool, needs Java) and MCSs + (optlang_enumerator) of a toy network with known results. + +## Setup + +These tests assume CNApy's own dependencies (per `pyproject.toml`) are +installed — `cobra`, `gurobipy`, `optlang_enumerator`, `efmtool_link`, +`straindesign`, `highspy`, `qtpy` + a Qt binding, etc. — plus `pytest` and +`pytest-qt`: + +```bash +pip install -e /path/to/CNApy # cnapy itself, editable +pip install pytest pytest-qt +QT_QPA_PLATFORM=offscreen pytest tests/ -v +``` + +## A design detail worth knowing + +The ECC2 fixture is loaded via `CNApyModel.read_sbml_model` +(`cnapy.cobra_cnapy`) rather than plain `cobra.io.read_sbml_model`, so that +it matches how CNApy itself loads a project's model. The stoichiometry hash +used for the results caches (`cnapy.cobra_cnapy.stoichiometry_hash`) is +recomputed on every access, so no hash setup is needed at load time. + +## Extending + +- `test_mainwindow_slots.py` ends with a skipped + `test_fba_via_real_main_window` sketch for a full end-to-end test against + an actual constructed `MainWindow` — un-skip and fill in the constructor + call if you want that heavier layer too; I didn't have `MainWindow.__init__` + in front of me to get the signature right. +- The bundled fixture (`tests/data/ECC2_model.sbml`) is the SBML model from + `cnapy-org/CNApy-projects`' ECC2 release asset (500 reactions, 487 + metabolites) — a genome-scale-ish model, good for catching real numerical + issues that a toy 3-reaction model would miss. + +## Cached FVA reference data + +`test_fva_unconstrained_matches_cobrapy` and +`test_fva_with_growth_constraint_matches_cobrapy` check +`multi_threaded_HiGHS_FVA` against cobrapy's own `flux_variability_analysis` +as ground truth. Since the ECC2 fixture model never changes, that reference +is precomputed once and cached in `tests/data/ecc2_fva_reference.json` +(~1–2s of cobrapy FVA, twice, saved on every test run) rather than +recomputed by the tests themselves. + +- `tests/generate_fva_reference_data.py` is the (manually run, not part of + `pytest`) script that produced it. +- The cached JSON embeds a sha256 of `ECC2_model.sbml`. The `fva_reference` + fixture in `conftest.py` checks that hash on every test run and fails + loudly — rather than silently comparing against a stale reference — if + the fixture model has changed without regenerating the cache. +- If you ever swap in a different/updated fixture model, regenerate with: + + ```bash + python tests/generate_fva_reference_data.py + ``` + +## Flux/yield space plotting (HiGHS path) + +Tests for the HiGHS-native replacement of straindesign's `plot_flux_space` +(`cnapy/highs_lp_cache.py`, `cnapy/plot_trace_highs.py`, +`cnapy/lptools_highs.py`), in two layers: + +- Small hand-built LPs with exactly known shapes (pentagon, cut cube and + rescaled versions of it), one file per module: + `test_highs_lp_cache.py`, `test_plot_trace_highs.py`, + `test_trace_3d_slice.py`, `test_trace_boundary_adaptive.py`, + `test_trace_grid_3d.py`, `test_trace_polytope_3d.py`, + `test_slice_mesh.py`, `test_lptools_highs.py`, + `test_lptools_highs_render.py`. They include regression tests for bugs + found while building this: solver fix-row state leaking between repeated + traces and between grid x-slices, folded slice stitching, and + threaded-vs-sequential equality. +- `test_flux_space_ecc2.py` on the ECC2 fixture (anaerobic, 8 rate/yield + axis combinations in 2D and 3D): extents against straindesign's own + `plot_flux_space`, closed and outward-oriented 3D meshes, threaded runs + (`n_workers=4` and the default `"auto"`) equal to sequential ones, flat + faces uniformly coloured, and headless rendering. Also the HiGHS thread + handling (next section). + +Why the ECC2 layer matters: `PlotSpaceDialog.compute()` falls back to +straindesign's `plot_flux_space` on *any* exception from the HiGHS path, so +a broken HiGHS path never shows up in the GUI (plots still appear, just +slowly). These tests call `plot_flux_space_data_highs` directly. + +### HiGHS thread scheduler + +HiGHS keeps one task scheduler per OS thread. The first `run()` on a thread +creates it, sized by that run's `threads` option (default 0 = half the +cores), and it is never resized: later runs on that thread must use +`threads=0` or exactly that size, otherwise `run()` returns `kError`. FBA, +FVA and cobra/optlang solve with the default on the GUI thread, so +`plot_flux_space_data_highs` runs all its solves on fresh +`highs_lp_cache.worker_pool` threads with `threads=1`, and leaves the GUI +thread's scheduler untouched. That way the plot works after any other solve, +and a later solve on the GUI thread (e.g. a MILP) still gets a +multi-threaded scheduler. Tests: `test_highs_lp_cache.py::TestThreadScheduler` +and section 4 of `test_flux_space_ecc2.py` (no solver on the calling thread, +plotting after a default-threads solve, and an OS-thread count check that +the caller's scheduler is still unset). + +No fakes: everything runs against the real cobra, straindesign (only used +for parsing axis/constraint expressions) and highspy. Rendering tests switch +pyplot to Agg only for their own duration (or draw on an explicit Agg +canvas), so the Qt tests' backend is untouched. + +### Cached straindesign reference data + +straindesign takes ~1 min for all cases (20 s for one 2-yield 3D case, with +~1 GB peak memory at higher resolution), so its results are precomputed into +`tests/data/ecc2_flux_space_reference.json`, the same way as the FVA +reference. The JSON also holds the case definitions (axes, constraints, +points) -- add or change a case in the generator only. + +- `tests/generate_flux_space_reference_data.py` (manually run, not part of + `pytest`) produces it: `python cnapy/tests/generate_flux_space_reference_data.py [--solver ...]` +- It stores a sha256 of the *decompressed* `ECC2_model.sbml.gz` (gzip + headers carry a timestamp, so re-gzipping an unchanged model doesn't + invalidate the cache). The `flux_space_reference` fixture fails loudly + if the model changed without regenerating. +- Extents are compared with a relative tolerance of 1e-5 of each axis range + (observed differences: <= 5e-8), so regenerating with a different + straindesign solver is fine. diff --git a/cnapy/tests/conftest.py b/cnapy/tests/conftest.py new file mode 100644 index 00000000..52d0e725 --- /dev/null +++ b/cnapy/tests/conftest.py @@ -0,0 +1,212 @@ +""" +Shared fixtures for CNApy's FBA/FVA CI tests. + +Design notes +------------ +CNApy's `MainWindow.fba()` / `MainWindow.fva()` (cnapy/gui_elements/main_window.py) +are thin Qt slots wrapped around real, non-GUI logic: + + fba() -> cnapy.core_gui.model_optimization_with_exceptions(model) (== model.optimize()) + fva() -> cnapy.core.multi_threaded_HiGHS_FVA(model, constraints) + +Rather than re-implementing that logic in tests (which would test our +re-implementation, not CNApy's), or instantiating the full MainWindow (which +drags in menus, the map view, QWebEngineView, the Jupyter console widget, +etc. - heavy and fragile in CI), these tests call the *actual* unbound +`MainWindow.fba` / `MainWindow.fva` methods on a minimal stand-in object +("FakeMainWindow") that only implements the surface those two methods +touch: `.appdata`, `.centralWidget()`, `.statusBar()`, +`.solver_status_display`, and the `set_status_*` helpers. + +This exercises the exact shipped slot code (scenario loading, comp_values +population, infinite-bound handling, exception handling) while staying fast +and independent of the rest of the widget tree. + +Requires: pytest, pytest-qt, cobra, qtpy (+ a Qt binding), and CNApy itself +importable (`pip install -e .` from the CNApy repo root). Run headless with: + + QT_QPA_PLATFORM=offscreen pytest tests/ +""" +import hashlib +import json +import os +import sys +import types +from pathlib import Path +from unittest.mock import MagicMock + +import pytest +import cobra + +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") + +DATA_DIR = Path(__file__).parent / "data" +ECC2_SBML = DATA_DIR / "ECC2_model.sbml.gz" +FVA_REFERENCE_JSON = DATA_DIR / "ecc2_fva_reference.json" + + +# --------------------------------------------------------------------------- +# Qt / import setup +# --------------------------------------------------------------------------- + +@pytest.fixture(scope="session") +def qapp(): + """A single headless QApplication for the whole test session.""" + from qtpy.QtWidgets import QApplication + app = QApplication.instance() or QApplication(sys.argv) + yield app + + +@pytest.fixture(autouse=True) +def no_blocking_dialogs(monkeypatch): + """Prevent any QMessageBox from popping up (and hanging) during tests.""" + from qtpy.QtWidgets import QMessageBox + monkeypatch.setattr(QMessageBox, "warning", lambda *a, **k: None) + monkeypatch.setattr(QMessageBox, "information", lambda *a, **k: None) + monkeypatch.setattr(QMessageBox, "exec", lambda *a, **k: None) + monkeypatch.setattr(QMessageBox, "exec_", lambda *a, **k: None) + + +# --------------------------------------------------------------------------- +# Model fixtures +# --------------------------------------------------------------------------- + +@pytest.fixture(scope="session") +def ecc2_reference_model() -> cobra.Model: + """ + A pristine model, loaded once per session, used both as the model + CNApy's slots operate on and as the basis for computing ground-truth + FBA/FVA values to check them against. + + Loaded via `CNApyModel.read_sbml_model` (cnapy.cobra_cnapy) rather than + plain `cobra.io.read_sbml_model`, matching how CNApy itself loads a + project's model. + + Never mutated directly by tests - use ecc2_reference_model.copy() if a + test needs to change bounds. + """ + from cnapy.cobra_cnapy import CNApyModel + return CNApyModel.read_sbml_model(str(ECC2_SBML)) + + +@pytest.fixture +def ecc2_model(ecc2_reference_model) -> cobra.Model: + """A fresh copy of the ECC2 model for a test to mutate freely.""" + return ecc2_reference_model.copy() + + +# --------------------------------------------------------------------------- +# Cached FVA ground truth +# --------------------------------------------------------------------------- + +@pytest.fixture(scope="session") +def fva_reference() -> dict: + """ + Cobrapy's own FVA, precomputed once (via generate_fva_reference_data.py) + and cached in tests/data/ecc2_fva_reference.json, rather than recomputed + on every test run - full FVA on a 500-reaction model is ~1-2s per call + and this file's fixture model never changes, so recomputing it every run + (twice - unconstrained and growth-coupled) was pure waste. + + Returns a dict with keys: source_sbml_sha256, biomass_reaction_id, + biomass_objective_value, unconstrained (rxn_id -> [min, max]), + growth_coupled (rxn_id -> [min, max]). + + The cached file embeds a sha256 of the SBML fixture it was computed + from; if that no longer matches the live fixture, this fails loudly + (rather than silently comparing new code against a stale reference) + with instructions to regenerate it. + """ + if not FVA_REFERENCE_JSON.exists(): + pytest.fail( + f"{FVA_REFERENCE_JSON} does not exist. Generate it once with:\n" + f" python tests/generate_fva_reference_data.py" + ) + + data = json.loads(FVA_REFERENCE_JSON.read_text()) + live_hash = hashlib.sha256(ECC2_SBML.read_bytes()).hexdigest() + if data["source_sbml_sha256"] != live_hash: + pytest.fail( + f"{FVA_REFERENCE_JSON} was computed from a different version of " + f"{ECC2_SBML.name} (sha256 mismatch) - the cached FVA reference " + "is stale. Regenerate it with:\n" + f" python tests/generate_fva_reference_data.py" + ) + return data + + +# --------------------------------------------------------------------------- +# Minimal AppData / ProjectData harness +# --------------------------------------------------------------------------- + +@pytest.fixture +def appdata(qapp, ecc2_model): + """ + A real `cnapy.appdata.AppData` + `ProjectData`, with `cobra_py_model` + swapped for our loaded ECC2 (CNApyModel) instance. Caching is disabled + (use_results_cache = False); this only affects whether FVA results get + read from / written to disk, not whether reactions have hash values + available (see ecc2_reference_model's docstring). + """ + from cnapy.appdata import AppData + + data = AppData() + data.project.cobra_py_model = ecc2_model + data.project.reaction_ids.set_ids( + ecc2_model.reactions.list_attr("id"), + data.project.scen_values.reactions.keys(), + ) + data.use_results_cache = False + return data + + +@pytest.fixture +def mainwindow_cls(): + """The real MainWindow class, imported lazily so importing it (which + pulls in the full cnapy.gui_elements package) only happens for tests + that actually need it.""" + from cnapy.gui_elements.main_window import MainWindow + return MainWindow + + +@pytest.fixture +def fake_main_window(appdata, mainwindow_cls): + """ + A duck-typed stand-in for MainWindow, implementing only the attributes + that MainWindow.fba() / MainWindow.fva() touch: + + self.appdata + self.centralWidget() -> object with .console, .update() + self.statusBar() -> object with .showMessage() + self.solver_status_display -> object with .setText() + self.set_status_optimal() / set_status_infeasible() / set_status_unknown() + + Everything is a MagicMock except .appdata, so assertions can check + `appdata.project.comp_values` (the real effect of these slots) while + ignoring pure-UI side effects (console text, cursor, status bar). + + One important wrinkle: fba() (and fba_optimize_reaction()) don't + populate comp_values themselves - they delegate to + `self.process_fba_solution()`. Similarly, fva() delegates to + `self.compute_fva_result()`. Since `self` here is a MagicMock instance, + leaving those attributes auto-mocked would make them return un-bound + MagicMock objects instead of executing real logic. + + Therefore, `process_fba_solution` and `compute_fva_result` are explicitly + bound onto this fake instance via `types.MethodType`. + """ + fake = MagicMock(name="FakeMainWindow") + fake.appdata = appdata + + central_widget = MagicMock(name="CentralWidget") + central_widget.console = MagicMock(name="console") + fake.centralWidget.return_value = central_widget + + fake.statusBar.return_value = MagicMock(name="statusBar") + fake.solver_status_display = MagicMock(name="solver_status_display") + + fake.process_fba_solution = types.MethodType(mainwindow_cls.process_fba_solution, fake) + fake.compute_fva_result = types.MethodType(mainwindow_cls.compute_fva_result, fake) + + return fake + \ No newline at end of file diff --git a/cnapy/tests/data/ECC2_model.sbml.gz b/cnapy/tests/data/ECC2_model.sbml.gz new file mode 100644 index 00000000..0bcf6b51 Binary files /dev/null and b/cnapy/tests/data/ECC2_model.sbml.gz differ diff --git a/cnapy/tests/data/ecc2_flux_space_reference.json b/cnapy/tests/data/ecc2_flux_space_reference.json new file mode 100644 index 00000000..14fdf384 --- /dev/null +++ b/cnapy/tests/data/ecc2_flux_space_reference.json @@ -0,0 +1,191 @@ +{ + "source_sbml_sha256": "f4dec01e6ce6fa175a15da4491ecfa172e1d02ad1d946d27c38683c5ab2facf5", + "straindesign_version": "1.19.1", + "straindesign_solver": "glpk", + "constraints": "EX_o2_e = 0", + "points": 12, + "cases": { + "2d_rr": { + "axes": [ + "1.0 Ec_biomass_iJO1366_core_53p95M", + "1.0 EX_etoh_e" + ], + "bbox": [ + [ + 0.0, + 0.28858945 + ], + [ + 0.0, + 20.0 + ] + ], + "area": 4.468193839749935 + }, + "2d_ry": { + "axes": [ + "1.0 Ec_biomass_iJO1366_core_53p95M", + [ + "1.0 EX_etoh_e", + "-1.0 EX_glc_e" + ] + ], + "bbox": [ + [ + 0.0, + 0.28858944 + ], + [ + -0.0, + 2.0 + ] + ] + }, + "2d_yy": { + "axes": [ + [ + "1.0 EX_etoh_e", + "-1.0 EX_glc_e" + ], + [ + "1.0 EX_succ_e", + "-1.0 EX_glc_e" + ] + ], + "bbox": [ + [ + -0.0, + 2.0 + ], + [ + -0.0, + 1.689523809 + ] + ] + }, + "3d_rrr": { + "axes": [ + "1.0 Ec_biomass_iJO1366_core_53p95M", + "1.0 EX_etoh_e", + "1.0 EX_succ_e" + ], + "bbox": [ + [ + 0.0, + 0.28858945 + ], + [ + 0.0, + 20.0 + ], + [ + 0.0, + 16.895238096 + ] + ], + "hull_volume": 28.51061728702626 + }, + "3d_rry": { + "axes": [ + "1.0 Ec_biomass_iJO1366_core_53p95M", + "1.0 EX_etoh_e", + [ + "1.0 EX_succ_e", + "-1.0 EX_glc_e" + ] + ], + "bbox": [ + [ + 0.0, + 0.28858945 + ], + [ + 0.0, + 20.0 + ], + [ + 0.0, + 1.6895238 + ] + ] + }, + "3d_yrr": { + "axes": [ + [ + "1.0 EX_succ_e", + "-1.0 EX_glc_e" + ], + "1.0 Ec_biomass_iJO1366_core_53p95M", + "1.0 EX_etoh_e" + ], + "bbox": [ + [ + 0.0, + 1.6895238 + ], + [ + 0.0, + 0.28858945 + ], + [ + 0.0, + 20.0 + ] + ] + }, + "3d_ryy": { + "axes": [ + "1.0 Ec_biomass_iJO1366_core_53p95M", + [ + "1.0 EX_etoh_e", + "-1.0 EX_glc_e" + ], + [ + "1.0 EX_succ_e", + "-1.0 EX_glc_e" + ] + ], + "bbox": [ + [ + 0.0, + 0.28858944 + ], + [ + 0.0, + 2.0 + ], + [ + 0.0, + 1.689523809 + ] + ] + }, + "3d_yry": { + "axes": [ + [ + "1.0 EX_etoh_e", + "-1.0 EX_glc_e" + ], + "1.0 Ec_biomass_iJO1366_core_53p95M", + [ + "1.0 EX_succ_e", + "-1.0 EX_glc_e" + ] + ], + "bbox": [ + [ + 0.0, + 2.0 + ], + [ + 0.0, + 0.282342041 + ], + [ + 0.0, + 1.689523809 + ] + ] + } + } +} diff --git a/cnapy/tests/data/ecc2_fva_reference.json b/cnapy/tests/data/ecc2_fva_reference.json new file mode 100644 index 00000000..e1f46ea3 --- /dev/null +++ b/cnapy/tests/data/ecc2_fva_reference.json @@ -0,0 +1,4009 @@ +{ +"biomass_objective_value": 0.9820949907392358, +"biomass_reaction_id": "Ec_biomass_iJO1366_core_53p95M", +"growth_coupled": { +"3OAR140": [ +0.07643056055929029, +0.07643056055929029 +], +"3OAS140": [ +0.07643056055929029, +0.07643056055929029 +], +"5DOAN": [ +0.0006589857387860272, +0.0006589857387860272 +], +"A5PISO": [ +0.038215280279645145, +0.038215280279645145 +], +"AACPS3": [ +0.1253428194780672, +0.1253428194780672 +], +"AACPS4": [ +0.14773458526692176, +0.14773458526692176 +], +"AACPS7": [ +0.07643056055929029, +0.07643056055929029 +], +"ACACT1r": [ +0.3495079653042793, +0.3495079653042793 +], +"ACACT2r": [ +0.3495079653042793, +0.3495079653042793 +], +"ACACT3r": [ +0.3495079653042793, +0.3495079653042793 +], +"ACACT4r": [ +0.3495079653042793, +0.3495079653042793 +], +"ACACT5r": [ +0.3495079653042793, +0.3495079653042793 +], +"ACACT6r": [ +0.273077404744989, +0.273077404744989 +], +"ACACT7r": [ +0.273077404744989, +0.273077404744989 +], +"ACALD": [ +0.0, +0.0 +], +"ACCOAC": [ +0.07643645312923474, +0.07643645312923474 +], +"ACGK": [ +0.29049387731075854, +0.29049387731075854 +], +"ACGS": [ +0.29049387731075854, +0.29049387731075854 +], +"ACHBS": [ +0.28532805765947017, +0.28532805765947017 +], +"ACKr": [ +0.5721469355149855, +0.572146935515371 +], +"ACLS": [ +0.8586122591736285, +0.8586122591736294 +], +"ACOAD1f": [ +-0.3495079653042793, +-0.3495079653042793 +], +"ACOAD2f": [ +-0.3495079653042793, +-0.3495079653042793 +], +"ACOAD3f": [ +-0.3495079653042793, +-0.3495079653042793 +], +"ACOAD4f": [ +-0.3495079653042793, +-0.3495079653042793 +], +"ACOAD5f": [ +-0.3495079653042793, +-0.3495079653042793 +], +"ACOAD6f": [ +-0.273077404744989, +-0.273077404744989 +], +"ACOAD7f": [ +-0.1253428194780672, +-0.1253428194780672 +], +"ACODA": [ +0.29049387731075854, +0.29049387731075854 +], +"ACONTa": [ +4.841403665163166, +4.8414036651647425 +], +"ACONTb": [ +4.841403665162931, +4.841403665164725 +], +"ACOTA": [ +-0.29049387731075854, +-0.29049387731075854 +], +"ACPPAT160": [ +0.0626714097390336, +0.0626714097390336 +], +"ACPPAT161": [ +0.07386729263346088, +0.07386729263346088 +], +"ACt2rpp": [ +0.0, +0.0 +], +"ACtex": [ +0.0, +0.0 +], +"ADCL": [ +0.0006570215488048703, +0.0006570215488046205 +], +"ADCS": [ +0.0006570215488048703, +0.0006570215488046205 +], +"ADK1": [ +2.6040405814649734, +2.6040405814670646 +], +"ADNK1": [ +0.02679646182232005, +0.02679646182232005 +], +"ADSK": [ +0.24343777792447874, +0.24343777792447985 +], +"ADSL1r": [ +0.293652294800995, +0.2936522948009666 +], +"ADSL2r": [ +0.439966770711308, +0.4399667707112794 +], +"ADSS": [ +0.293652294800995, +0.2936522948009666 +], +"AGPAT160": [ +0.0626714097390336, +0.0626714097390336 +], +"AGPAT161": [ +0.07386729263346088, +0.07386729263346088 +], +"AGPR": [ +-0.29049387731075854, +-0.29049387731075854 +], +"AHCYSNS": [ +0.0004399785558511812, +0.0004399785558511812 +], +"AICART": [ +0.5330084859439618, +0.5330084859439331 +], +"AIRC2": [ +0.439966770711308, +0.4399667707112794 +], +"AIRC3": [ +-0.439966770711308, +-0.4399667707112794 +], +"AKGDH": [ +3.7854600415912576, +3.7854600415968958 +], +"ALAALAr": [ +0.027290455602661884, +0.027290455602661884 +], +"ALAR": [ +0.040935683403992826, +0.040935683403992826 +], +"ALCD2x": [ +0.0, +0.0 +], +"AMAOTr": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"AMPMS2": [ +0.00021900718293484957, +0.00021900718293484957 +], +"ANPRT": [ +0.055825225558590374, +0.055825225558590374 +], +"ANS": [ +0.055825225558590374, +0.055825225558590374 +], +"AOXSr2": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"APG3PAT160": [ +0.0626714097390336, +0.0626714097390336 +], +"APG3PAT161": [ +0.07386729263346088, +0.07386729263346088 +], +"APRAUR": [ +0.0004380143658695103, +0.0004380143658695103 +], +"ARGSL": [ +0.29049387731075854, +0.29049387731075854 +], +"ARGSS": [ +0.29049387731075854, +0.29049387731075854 +], +"ASAD": [ +-1.0499410589844744, +-1.0499410589844744 +], +"ASNS2": [ +0.23673399751769278, +0.23673399751769278 +], +"ASP1DC": [ +0.0005656867146666134, +0.0005656867146668354 +], +"ASPCT": [ +0.3248701482716039, +0.3248701482716039 +], +"ASPK": [ +1.0499410589844744, +1.0499410589844744 +], +"ASPO5": [ +0.002237212388903842, +0.002237212388903842 +], +"ASPTA": [ +-2.8751950442180005, +-2.8751950442180396 +], +"ATPM": [ +3.15, +3.150000000000958 +], +"ATPPRT": [ +0.09304171523265373, +0.09304171523265373 +], +"ATPS4rpp": [ +55.86625322739454, +55.86625322739914 +], +"BMOCOS": [ +0.00011981558887018676, +0.00011981558887018676 +], +"BMOGDS1": [ +0.00011981558887018676, +0.00011981558887018676 +], +"BMOGDS2": [ +0.00011981558887018676, +0.00011981558887018676 +], +"BPNT": [ +0.24343777792447985, +0.24343777792447985 +], +"BTS5": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"CA2tex": [ +0.005111804426797722, +0.005111804426797722 +], +"CAt6pp": [ +-0.005111804426797722, +-0.005111804426797722 +], +"CBMKr": [ +0.6153640255823625, +0.6153640255823625 +], +"CDPMEK": [ +0.0023462249328760534, +0.0023462249328760534 +], +"CHORM": [ +0.31760264534013366, +0.31760264534013366 +], +"CHORS": [ +0.3743038996304635, +0.3743038996304635 +], +"CHRPL": [ +0.00021900718293484957, +0.00021900718293484957 +], +"CLt3_2pp": [ +0.002555902213398861, +0.002555902213398861 +], +"CLtex": [ +0.005111804426797722, +0.005111804426797722 +], +"CO2tex": [ +-19.684608774253178, +-19.684608774252677 +], +"CO2tpp": [ +-19.684608774253178, +-19.684608774252915 +], +"COBALT2tex": [ +2.4552374768480895e-05, +2.4552374768480895e-05 +], +"COBALT2tpp": [ +2.4552374768480895e-05, +2.4552374768480895e-05 +], +"CPMPS": [ +0.00023963117774037352, +0.00023963117774037352 +], +"CPPPGO2": [ +0.00021900718293484957, +0.00021900718293484957 +], +"CS": [ +4.841403665162305, +4.841403665164767 +], +"CTECOAI7": [ +-0.14773458526692176, +-0.14773458526692176 +], +"CTPS2": [ +0.15765276257839722, +0.15765276257839722 +], +"CU2tex": [ +0.0006963053484341182, +0.0006963053484341182 +], +"CU2tpp": [ +0.0006963053484341182, +0.0006963053484341182 +], +"CYSS": [ +0.24343777792447985, +0.24343777792447985 +], +"CYSTL": [ +0.1511591504996295, +0.1511591504996295 +], +"CYTBO3_4pp": [ +35.17336008644143, +35.17336008644156 +], +"CYTK1": [ +0.17766589429968144, +0.17766589429968255 +], +"DADK": [ +-0.02569749752768284, +-0.02569749752768284 +], +"DALAt2pp": [ +0.013645227801330942, +0.013645227801330942 +], +"DAPDC": [ +0.33701571702207617, +0.33701571702207617 +], +"DAPE": [ +0.3643061726247381, +0.3643061726247381 +], +"DASYN160": [ +0.0626714097390336, +0.0626714097390336 +], +"DASYN161": [ +0.07386729263346088, +0.07386729263346088 +], +"DB4PS": [ +0.0008760287317390206, +0.0008760287317390206 +], +"DBTS": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"DDPA": [ +0.3743038996304635, +0.3743038996304635 +], +"DHAD1": [ +0.8586122591736296, +0.8586122591736296 +], +"DHAD2": [ +0.28532805765947017, +0.28532805765947017 +], +"DHDPRy": [ +0.3643061726247381, +0.3643061726247381 +], +"DHDPS": [ +0.3643061726247381, +0.3643061726247381 +], +"DHFR": [ +0.026354519076487462, +0.026354519076487462 +], +"DHFS": [ +0.0006570215488046205, +0.0006570215488046205 +], +"DHNPA2r": [ +0.0006570215488046205, +0.0006570215488046205 +], +"DHORDfum": [ +0.3248701482716039, +0.3248701482716039 +], +"DHORTS": [ +-0.3248701482716039, +-0.3248701482716039 +], +"DHPPDA2": [ +0.0004380143658695103, +0.0004380143658695103 +], +"DHPS2": [ +0.0006570215488046205, +0.0006570215488046205 +], +"DHPTDCs2": [ +0.0004399785558511812, +0.0004399785558511812 +], +"DHQS": [ +0.3743038996304635, +0.3743038996304635 +], +"DHQTi": [ +0.3743038996304635, +0.3743038996304635 +], +"DMATT": [ +0.0002730224074255093, +0.0002730224074255093 +], +"DM_4CRSOL": [ +0.00021900718293484957, +0.00021900718293484957 +], +"DM_5DRIB": [ +0.0006589857387860272, +0.0006589857387860272 +], +"DM_AMOB": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"DM_MTHTHF": [ +0.0004399785558511812, +0.0004399785558511812 +], +"DNMPPA": [ +0.0006570215488046205, +0.0006570215488046205 +], +"DNTPPA": [ +0.0006570215488046205, +0.0006570215488046205 +], +"DPCOAK": [ +0.0005656867146668354, +0.0005656867146668354 +], +"DPR": [ +0.0005656867146668354, +0.0005656867146668354 +], +"DTMPK": [ +0.02569749752768284, +0.02569749752768284 +], +"DURIK1": [ +0.02569749752768284, +0.02569749752768284 +], +"DURIPP": [ +-0.02569749752768284, +-0.02569749752768284 +], +"DXPRIi": [ +0.0023462249328760534, +0.0023462249328760534 +], +"DXPS": [ +0.0027842392987457525, +0.0027842392987457525 +], +"D_LACt2pp": [ +0.0, +0.0 +], +"D_LACtex": [ +0.0, +0.0 +], +"E4PD": [ +0.00021900718293484957, +0.00021900718293484957 +], +"ECOAH1": [ +0.3495079653042793, +0.3495079653042793 +], +"ECOAH2": [ +0.3495079653042793, +0.3495079653042793 +], +"ECOAH3": [ +0.3495079653042793, +0.3495079653042793 +], +"ECOAH4": [ +0.3495079653042793, +0.3495079653042793 +], +"ECOAH5": [ +0.3495079653042793, +0.3495079653042793 +], +"ECOAH6": [ +0.273077404744989, +0.273077404744989 +], +"ECOAH7": [ +0.273077404744989, +0.273077404744989 +], +"EDA": [ +0.0, +2.0099477637813834e-12 +], +"EDD": [ +0.0, +1.2079226507921703e-12 +], +"EGMEACPR": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"ENO": [ +14.567781753326756, +14.567781753328873 +], +"EPMEACPR": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"ETOHtex": [ +0.0, +0.0 +], +"ETOHtrpp": [ +0.0, +0.0 +], +"EX_Biomass": [ +0.9820949907392358, +0.9820949907392358 +], +"EX_ac_e": [ +0.0, +0.0 +], +"EX_ca2_e": [ +-0.005111804426797722, +-0.005111804426797722 +], +"EX_cl_e": [ +-0.005111804426797722, +-0.005111804426797722 +], +"EX_co2_e": [ +19.684608774252677, +19.684608774253647 +], +"EX_cobalt2_e": [ +-2.4552374768480895e-05, +-2.4552374768480895e-05 +], +"EX_cu2_e": [ +-0.0006963053484341182, +-0.0006963053484341182 +], +"EX_etoh_e": [ +0.0, +0.0 +], +"EX_fe2_e": [ +-0.008105229958570912, +-0.008105229958570912 +], +"EX_fe3_e": [ +-0.007668197687691953, +-0.007668197687691953 +], +"EX_for_e": [ +0.0, +0.0 +], +"EX_glc_e": [ +-10.0, +-9.999999999999947 +], +"EX_glyc_e": [ +0.0, +0.0 +], +"EX_h2_e": [ +0.0, +8.588685318500211e-13 +], +"EX_h2o_e": [ +45.617341622496774, +45.6173416224974 +], +"EX_h_e": [ +9.031289555423495, +9.031289555424092 +], +"EX_k_e": [ +-0.19169512124239144, +-0.19169512124239144 +], +"EX_lac__D_e": [ +0.0, +0.0 +], +"EX_meoh_e": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"EX_mg2_e": [ +-0.00851967404466287, +-0.00851967404466287 +], +"EX_mn2_e": [ +-0.0006786276386008119, +-0.0006786276386008119 +], +"EX_mobd_e": [ +-0.0001266902538053614, +-0.0001266902538053614 +], +"EX_nh4_e": [ +-10.607295688767461, +-10.607295688767383 +], +"EX_ni2_e": [ +-0.0003172166820087731, +-0.0003172166820087731 +], +"EX_o2_e": [ +-17.586680043220532, +-17.586680043220714 +], +"EX_pi_e": [ +-0.9472424037076341, +-0.9472424037079283 +], +"EX_so4_e": [ +-0.24769810599430664, +-0.24769810599430553 +], +"EX_succ_e": [ +0.0, +0.0 +], +"EX_zn2_e": [ +-0.0003348943918420794, +-0.0003348943918420794 +], +"Ec_biomass_iJO1366_core_53p95M": [ +0.9820949907392358, +0.9820949907392358 +], +"F6PA": [ +0.0, +0.0 +], +"FACOAE120": [ +0.07643056055929029, +0.07643056055929029 +], +"FACOAE160": [ +0.1253428194780672, +0.1253428194780672 +], +"FACOAE161": [ +0.14773458526692176, +0.14773458526692176 +], +"FADRx": [ +2.1467516193069884, +2.1467516193069884 +], +"FBA": [ +7.764231363107961, +7.7642313631144075 +], +"FBP": [ +0.0, +0.0 +], +"FCLT": [ +0.00021900718293484957, +0.00021900718293484957 +], +"FE2tex": [ +0.008105229958570912, +0.008105229958570912 +], +"FE2tpp": [ +0.008105229958570912, +0.008105229958570912 +], +"FE3abcpp": [ +0.007668197687691953, +0.007668197687691953 +], +"FE3tex": [ +0.007668197687691953, +0.007668197687691953 +], +"FHL": [ +0.0, +8.588685318500211e-13 +], +"FLDR2": [ +0.10680774071784559, +0.10680774071784559 +], +"FMNAT": [ +0.0002190071829346607, +0.0002190071829346607 +], +"FORtex": [ +0.0, +0.0 +], +"FORtppi": [ +0.0, +0.0 +], +"FRD2": [ +0.0, +995.8861185546455 +], +"FRD3": [ +0.0006570215488046205, +0.000657021548804565 +], +"FTHFLi": [ +0.0024090790122828507, +0.002409079012282795 +], +"FUM": [ +4.809572984417355, +4.809572984413835 +], +"G1PACT": [ +0.09279619148496891, +0.09279619148496891 +], +"G1SAT": [ +0.003504114926957593, +0.003504114926957593 +], +"G3PD2": [ +-0.13653870237271448, +-0.1365387023724945 +], +"G3PD5": [ +0.0, +-7.290890113864634e-12 +], +"G5SADs": [ +0.21710191865281547, +0.21710191865281547 +], +"G5SD": [ +0.21710191865281547, +0.21710191865281547 +], +"G6PDH2r": [ +4.14169372409742, +4.141693724104378 +], +"GAPD": [ +16.25482698365585, +16.25482698365223 +], +"GARFT": [ +0.4401857778942144, +0.4401857778942004 +], +"GCALDD": [ +0.0006570215488046205, +0.000657021548804565 +], +"GF6PTA": [ +0.09279619148496891, +0.09279619148496891 +], +"GHMT2r": [ +1.0954709827551323, +1.0954709827551115 +], +"GK1": [ +0.23935619114296644, +0.23935619114296647 +], +"GLCptspp": [ +9.999999999998078, +10.0 +], +"GLCt2pp": [ +0.0, +2.8910207561239076e-12 +], +"GLCtex": [ +9.999999999999973, +10.0 +], +"GLNS": [ +1.7781487810977474, +1.77814878109777 +], +"GLU5K": [ +0.21710191865281547, +0.21710191865281547 +], +"GLUDy": [ +-8.39849825423048, +-8.398498254230516 +], +"GLUPRT": [ +0.4401857778942144, +0.44018577789422547 +], +"GLUR": [ +-0.027290455602661884, +-0.027290455602661884 +], +"GLUTRR": [ +0.003504114926957593, +0.003504114926957593 +], +"GLUTRS": [ +0.003504114926957593, +0.003504114926957593 +], +"GLXCL": [ +0.00032851077440231025, +0.00032851077440243515 +], +"GLYCDx": [ +0.0, +0.0 +], +"GLYCK": [ +0.00032851077440231025, +0.00032851077440243515 +], +"GLYCL": [ +0.05361452973443259, +0.0536145297344377 +], +"GLYCTO4": [ +0.0006570215488046205, +0.0006570215488048703 +], +"GLYCtex": [ +0.0, +0.0 +], +"GLYCtpp": [ +0.0, +0.0 +], +"GLYK": [ +0.0, +0.0 +], +"GLYOX3": [ +0.0, +-4.60786964140425e-12 +], +"GMPS2": [ +0.23935619114296644, +0.2393561911429667 +], +"GND": [ +4.14169372409742, +4.141693724098238 +], +"GRTT": [ +0.0002730224074255093, +0.0002730224074255093 +], +"GRXR": [ +0.24343777792447985, +0.24343777792447985 +], +"GTHOr": [ +0.24343777792447985, +0.24343777792447985 +], +"GTPCI": [ +0.0006570215488046205, +0.0006570215488045652 +], +"GTPCII2": [ +0.0004380143658695103, +0.0004380143658695103 +], +"H2Otex": [ +-45.61734162249769, +-45.617341622496916 +], +"H2Otpp": [ +-45.61734162249769, +-45.617341622496404 +], +"H2tex": [ +-8.588685318500211e-13, +0.0 +], +"H2tpp": [ +0.0, +0.0 +], +"HACD1": [ +0.3495079653042793, +0.3495079653042793 +], +"HACD2": [ +0.3495079653042793, +0.3495079653042793 +], +"HACD3": [ +0.3495079653042793, +0.3495079653042793 +], +"HACD4": [ +0.3495079653042793, +0.3495079653042793 +], +"HACD5": [ +0.3495079653042793, +0.3495079653042793 +], +"HACD6": [ +0.273077404744989, +0.273077404744989 +], +"HACD7": [ +0.273077404744989, +0.273077404744989 +], +"HBZOPT": [ +0.00021900718293484957, +0.00021900718293484957 +], +"HCO3E": [ +0.516403223840529, +0.5164032238405003 +], +"HEX1": [ +0.0, +2.8910207561239076e-12 +], +"HISTD": [ +0.09304171523265373, +0.09304171523265373 +], +"HISTP": [ +0.09304171523265373, +0.09304171523265373 +], +"HMBS": [ +0.00043801436586969915, +0.00043801436586969915 +], +"HPPK2": [ +0.0006570215488045372, +0.0006570215488048703 +], +"HSDy": [ +-0.6856348863597364, +-0.6856348863597364 +], +"HSK": [ +0.5344757358601069, +0.5344757358601069 +], +"HSST": [ +0.1511591504996295, +0.1511591504996295 +], +"HSTPT": [ +0.09304171523265373, +0.09304171523265373 +], +"Htex": [ +-9.031289555424092, +-9.03128955542358 +], +"I2FE2SR": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"I2FE2SS": [ +0.00028087916735142145, +0.00028087916735142145 +], +"I2FE2SS2": [ +0.0002553446975922013, +0.0002553446975922013 +], +"I2FE2ST": [ +2.7498659740698602e-05, +2.7498659740698602e-05 +], +"I4FE4SR": [ +0.0002553446975922013, +0.0002553446975922013 +], +"I4FE4ST": [ +0.0002553446975922013, +0.0002553446975922013 +], +"ICDHyr": [ +4.841403665153264, +4.841403665163309 +], +"ICL": [ +0.0, +-8.664180484174722e-12 +], +"ICYSDS": [ +0.0017726814582843206, +0.0017726814582843206 +], +"IG3PS": [ +0.09304171523265373, +0.09304171523265373 +], +"IGPDH": [ +0.09304171523265373, +0.09304171523265373 +], +"IGPS": [ +0.055825225558590374, +0.055825225558590374 +], +"ILETA": [ +-0.28532805765947017, +-0.28532805765947017 +], +"IMPC": [ +-0.5330084859439334, +-0.5330084859439335 +], +"IMPD": [ +0.23935619114296644, +0.23935619114296647 +], +"IPDDI": [ +0.0002730224074255093, +0.0002730224074255093 +], +"IPDPS": [ +0.0023462249328760534, +0.0023462249328760534 +], +"IPMD": [ +0.4424632561777479, +0.4424632561777479 +], +"IPPMIa": [ +-0.4424632561777479, +-0.4424632561777479 +], +"IPPMIb": [ +-0.4424632561777479, +-0.4424632561777479 +], +"IPPS": [ +0.4424632561777479, +0.4424632561777479 +], +"K2L4Aabcpp": [ +0.019107640139822572, +0.019107640139822572 +], +"K2L4Aabctex": [ +0.019107640139822572, +0.019107640139822572 +], +"KARA1": [ +-0.8586122591736296, +-0.85861225917363 +], +"KARA2": [ +0.28532805765947017, +0.28532805765947017 +], +"KDOCT2": [ +0.038215280279645145, +0.038215280279645145 +], +"KDOPP": [ +0.038215280279645145, +0.038215280279645145 +], +"KDOPS": [ +0.038215280279645145, +0.038215280279645145 +], +"Kt2pp": [ +0.19169512124239144, +0.19169512124239144 +], +"Ktex": [ +0.19169512124239144, +0.19169512124239144 +], +"LDH_D": [ +0.0, +-4.511946372076636e-12 +], +"LEUTAi": [ +0.4424632561777479, +0.4424632561777479 +], +"LPADSS": [ +0.019107640139822572, +0.019107640139822572 +], +"MALCOAMT": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"MALS": [ +0.0, +-8.837375276016246e-12 +], +"MCOATA": [ +0.07643448893925325, +0.07643448893925325 +], +"MCTP1App": [ +0.013645227801330942, +0.013645227801330942 +], +"MDH": [ +4.809572984416912, +4.809572984423765 +], +"ME1": [ +0.0, +-4.3085535139653075e-12 +], +"ME2": [ +0.0, +3.8441472227646045e-12 +], +"MECDPDH5": [ +0.0023462249328760534, +0.0023462249328760534 +], +"MECDPS": [ +0.0023462249328760534, +0.0023462249328760534 +], +"MEOHtex": [ +-1.9641899814784713e-06, +-1.9641899814784713e-06 +], +"MEOHtrpp": [ +-1.9641899814784713e-06, +-1.9641899814784713e-06 +], +"MEPCT": [ +0.0023462249328760534, +0.0023462249328760534 +], +"METAT": [ +0.0013199356675535363, +0.0013199356675535623 +], +"METS": [ +0.15159912905548067, +0.15159912905548067 +], +"MG2t3_2pp": [ +0.00851967404466287, +0.00851967404466287 +], +"MG2tex": [ +0.00851967404466287, +0.00851967404466287 +], +"MGSA": [ +0.0, +-4.60786964140425e-12 +], +"MN2tpp": [ +0.0006786276386008119, +0.0006786276386008119 +], +"MNtex": [ +0.0006786276386008119, +0.0006786276386008119 +], +"MOADSUx": [ +0.00047926235548074705, +0.00047926235548074705 +], +"MOAT": [ +0.019107640139822572, +0.019107640139822572 +], +"MOAT2": [ +0.019107640139822572, +0.019107640139822572 +], +"MOBDabcpp": [ +0.0001266902538053614, +0.0001266902538053614 +], +"MOBDtex": [ +0.0001266902538053614, +0.0001266902538053614 +], +"MOCOS": [ +0.00011981558887018676, +0.00011981558887018676 +], +"MOHMT": [ +0.0005656867146668354, +0.0005656867146668354 +], +"MPTAT": [ +0.00023963117774037352, +0.00023963117774037352 +], +"MPTG": [ +0.013645227801330942, +0.013645227801330942 +], +"MPTS": [ +0.00023963117774037352, +0.00023963117774037352 +], +"MPTSS": [ +0.00047926235548074705, +0.00047926235548074705 +], +"MTHFC": [ +0.9710041920087997, +0.9710041920087981 +], +"MTHFD": [ +0.9710041920087997, +0.9710041920087981 +], +"MTHFR2": [ +0.15159912905548067, +0.15159912905548065 +], +"MTHTHFSs": [ +0.0004399785558511812, +0.0004399785558511812 +], +"NADH16pp": [ +-964.8266399135545, +31.059478641089466 +], +"NADH17pp": [ +0.0, +995.8861185546444 +], +"NADK": [ +0.00043899646086265065, +0.0004389964608613184 +], +"NADS1": [ +0.0022372123889060624, +0.00223721238889496 +], +"NADTRHD": [ +0.0, +-4.1813663642642496e-11 +], +"NDPK1": [ +0.7764462638684275, +0.7764462638683991 +], +"NDPK2": [ +0.39196884222889, +0.39196884222889 +], +"NDPK3": [ +0.17766589429968233, +0.17766589429968255 +], +"NDPK4": [ +0.02569749752768284, +0.02569749752768284 +], +"NDPK8": [ +-0.02569749752768284, +-0.02569749752768284 +], +"NH4tex": [ +10.607295688767461, +10.607295688767294 +], +"NH4tpp": [ +10.607295688767461, +10.607295688767294 +], +"NI2tex": [ +0.0003172166820087731, +0.0003172166820087731 +], +"NI2tpp": [ +0.0003172166820087731, +0.0003172166820087731 +], +"NNATr": [ +0.0022372123889060624, +0.002237212388893184 +], +"NNDPR": [ +0.0022372123889060624, +0.002237212388893184 +], +"NTD2": [ +0.02569749752768284, +0.02569749752768284 +], +"NTD6": [ +0.02569749752768284, +0.02569749752768284 +], +"O2tex": [ +17.586680043220706, +17.58668004322095 +], +"O2tpp": [ +17.586680043220685, +17.58668004322095 +], +"OCBT": [ +0.29049387731075854, +0.29049387731075854 +], +"OCTDPS": [ +0.00021900718293484957, +0.00021900718293484957 +], +"OGMEACPD": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"OGMEACPR": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"OGMEACPS": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"OHPBAT": [ +0.00021900718293484957, +0.00021900718293484957 +], +"OMCDC": [ +0.4424632561777479, +0.4424632561777479 +], +"OMPDC": [ +0.3248701482716039, +0.3248701482716039 +], +"OPHBDC": [ +0.00021900718293484957, +0.00021900718293484957 +], +"OPHHX3": [ +0.00021900718293484957, +0.00021900718293484957 +], +"OPMEACPD": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"OPMEACPR": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"OPMEACPS": [ +1.9641899814820674e-06, +1.9641899814820674e-06 +], +"ORPT": [ +-0.3248701482716039, +-0.3248701482716039 +], +"P5CR": [ +0.21710191865281547, +0.21710191865281547 +], +"PANTS": [ +0.0005656867146657252, +0.0005656867146668354 +], +"PAPPT3": [ +0.027290455602661884, +0.027290455602661884 +], +"PAPSR2": [ +0.24343777792447874, +0.24343777792447985 +], +"PDH": [ +8.05808688699023, +8.058086886984697 +], +"PDX5PO2": [ +0.00021900718293484957, +0.00021900718293484957 +], +"PDX5PS": [ +0.00021900718293484957, +0.00021900718293484957 +], +"PE160abcpp": [ +0.045123336444504926, +0.045123336444504926 +], +"PE161abcpp": [ +0.020682920504968304, +0.020682920504968304 +], +"PERD": [ +0.00021900718293484957, +0.00021900718293484957 +], +"PFK": [ +7.7642313631077435, +7.764231363111591 +], +"PFL": [ +0.0, +9.059419880941277e-13 +], +"PGAMT": [ +-0.09279619148496891, +-0.09279619148496891 +], +"PGCD": [ +1.6873737411037264, +1.6873737411037089 +], +"PGI": [ +5.858306275894932, +5.858306275884626 +], +"PGK": [ +-16.254826983658223, +-16.254826983656148 +], +"PGL": [ +4.141693724097467, +4.14169372408619 +], +"PGM": [ +-14.567781753328859, +-14.567781753326823 +], +"PHETA1": [ +-0.1819527389342582, +-0.1819527389342582 +], +"PIt2rpp": [ +0.9472424037078708, +0.9472424037078402 +], +"PItex": [ +0.9472424037078708, +0.9472424037078402 +], +"PMDPHT": [ +0.0004380143658695103, +0.0004380143658695103 +], +"PMEACPE": [ +1.9641899814784713e-06, +1.9641899814784713e-06 +], +"PMPK": [ +0.00021900718293484957, +0.00021900718293484957 +], +"PNTK": [ +0.0005656867146668354, +0.0005656867146668354 +], +"POX": [ +0.0, +0.0 +], +"PPBNGS": [ +0.0017520574634787966, +0.0017520574634787966 +], +"PPC": [ +2.907025724953887, +2.907025724953625 +], +"PPCDC": [ +0.0005656867146668354, +0.0005656867146668354 +], +"PPCK": [ +0.0, +9.947598300641403e-13 +], +"PPKr": [ +-3.4155662963075475, +-3.4155662963075555 +], +"PPM": [ +-0.0010989642946372079, +-0.0010989642946372079 +], +"PPNCL2": [ +0.0005656867146668354, +0.0005656867146668354 +], +"PPND": [ +0.13564990640587546, +0.13564990640587546 +], +"PPNDH": [ +0.1819527389342582, +0.1819527389342582 +], +"PPPGO3": [ +0.00021900718293484957, +0.00021900718293484957 +], +"PPS": [ +0.0, +1.3171685964152857e-12 +], +"PRAGSr": [ +0.4401857778942089, +0.4401857778942128 +], +"PRAIS": [ +0.4401857778942089, +0.4401857778942128 +], +"PRAIi": [ +0.055825225558590374, +0.055825225558590374 +], +"PRAMPC": [ +0.09304171523265373, +0.09304171523265373 +], +"PRASCSi": [ +0.43996677071127405, +0.439966770711278 +], +"PRATPP": [ +0.09304171523265373, +0.09304171523265373 +], +"PRFGS": [ +0.4401857778942089, +0.4401857778942128 +], +"PRMICI": [ +0.09304171523265373, +0.09304171523265373 +], +"PROt2rpp": [ +-0.005111804426797722, +-0.005111804426797722 +], +"PROt4pp": [ +0.005111804426797722, +0.005111804426797722 +], +"PRPPS": [ +0.9161600793459566, +0.9161600793459647 +], +"PSCVT": [ +0.3743038996304634, +0.3743038996304634 +], +"PSD160": [ +0.0626714097390336, +0.0626714097390336 +], +"PSD161": [ +0.07386729263346088, +0.07386729263346088 +], +"PSERT": [ +1.6873737411037184, +1.6873737411037242 +], +"PSP_L": [ +1.6873737411037184, +1.6873737411037242 +], +"PSSA160": [ +0.0626714097390336, +0.0626714097390336 +], +"PSSA161": [ +0.07386729263346088, +0.07386729263346088 +], +"PTAr": [ +-0.572146935515371, +-0.572146935514386 +], +"PTPATi": [ +0.0005656867146666134, +0.0005656867146675015 +], +"PUNP1": [ +-0.02679646182232005, +-0.02679646182232005 +], +"PUNP2": [ +0.02569749752768284, +0.02569749752768284 +], +"PYK": [ +0.8466424932204069, +0.8466424932248628 +], +"PYNP2r": [ +0.02569749752768284, +0.02569749752768284 +], +"QULNS": [ +0.002237212388903842, +0.0022372123889024542 +], +"RBFK": [ +0.0002190071829346607, +0.0002190071829346607 +], +"RBFSa": [ +0.0008760287317390206, +0.0008760287317390206 +], +"RBFSb": [ +0.0004380143658695103, +0.0004380143658695103 +], +"RHCCE": [ +0.0004399785558511812, +0.0004399785558511812 +], +"RNTR1c2": [ +0.05139499505536568, +0.05139499505536568 +], +"RNTR2c2": [ +0.02653326036480193, +0.02653326036480193 +], +"RNTR3c2": [ +0.02653326036480193, +0.02653326036480193 +], +"RPE": [ +1.9987212786924902, +1.9987212786905963 +], +"RPI": [ +-2.1038811363960517, +-2.10388113640305 +], +"SADT2": [ +0.24343777792447985, +0.24343777792447985 +], +"SDPDS": [ +0.3643061726247381, +0.3643061726247381 +], +"SDPTA": [ +-0.3643061726247381, +-0.3643061726247381 +], +"SERAT": [ +0.24343777792447985, +0.24343777792447985 +], +"SHCHD2": [ +0.00021900718293484957, +0.00021900718293484957 +], +"SHCHF": [ +0.00021900718293484957, +0.00021900718293484957 +], +"SHK3Dr": [ +0.3743038996304635, +0.3743038996304634 +], +"SHKK": [ +0.3743038996304635, +0.3743038996304634 +], +"SHSL1": [ +0.1511591504996295, +0.1511591504996295 +], +"SO4t2pp": [ +0.24769810599430664, +0.24769810599430664 +], +"SO4tex": [ +0.24769810599430664, +0.24769810599430664 +], +"SUCCt2_2pp": [ +0.0, +1.476744652488075e-12 +], +"SUCCt3pp": [ +0.0, +-2.323807812842915e-12 +], +"SUCCtex": [ +0.0, +0.0 +], +"SUCDi": [ +4.1138814453519235, +1000.0 +], +"SUCOAS": [ +-3.269994718472528, +-3.269994718464707 +], +"SULRi": [ +0.24343777792447985, +0.2434377779244803 +], +"TALA": [ +1.1866220927529594, +1.186622092754889 +], +"TDSK": [ +0.019107640139822572, +0.019107640139822572 +], +"THD2pp": [ +0.0, +-4.1675107809169276e-11 +], +"THDPS": [ +0.3643061726247381, +0.3643061726247381 +], +"THRD_L": [ +0.28532805765947017, +0.28532805765947017 +], +"THRS": [ +0.5344757358601069, +0.5344757358601069 +], +"THZPSN3": [ +0.00021900718293484957, +0.00021900718293484957 +], +"TKT1": [ +1.1866220927529443, +1.186622092749152 +], +"TKT2": [ +0.812099185939561, +0.8120991859418729 +], +"TMDS": [ +0.02569749752768284, +0.02569749752768284 +], +"TMPK": [ +0.00021900718293484957, +0.00021900718293484957 +], +"TMPPP": [ +0.00021900718293484957, +0.00021900718293484957 +], +"TPI": [ +7.6254554483463455, +7.625455448342886 +], +"TRPAS2": [ +-0.055825225558590374, +-0.055825225558590374 +], +"TRPS3": [ +0.055825225558590374, +0.055825225558590374 +], +"TRSARr": [ +0.00032851077440231025, +0.0003285107744022825 +], +"TYRL": [ +0.00021900718293484957, +0.00021900718293484957 +], +"TYRTA": [ +-0.13564990640587546, +-0.13564990640587546 +], +"U23GAAT": [ +0.038215280279645145, +0.038215280279645145 +], +"UAAGDS": [ +0.027290455602661884, +0.027290455602661884 +], +"UAGAAT": [ +0.038215280279645145, +0.038215280279645145 +], +"UAGCVT": [ +0.027290455602661884, +0.027290455602661884 +], +"UAGDP": [ +0.09279619148496891, +0.09279619148496891 +], +"UAGPT3": [ +0.027290455602661884, +0.027290455602661884 +], +"UAMAGS": [ +0.027290455602661884, +0.027290455602661884 +], +"UAMAS": [ +0.027290455602661884, +0.027290455602661884 +], +"UAPGR": [ +0.027290455602661884, +0.027290455602661884 +], +"UDCPDP": [ +0.027290455602661884, +0.027290455602661884 +], +"UDCPDPS": [ +5.401522449065971e-05, +5.401522449065971e-05 +], +"UGMDDS": [ +0.027290455602661884, +0.027290455602661884 +], +"UHGADA": [ +0.038215280279645145, +0.038215280279645145 +], +"UMPK": [ +0.3455707464864055, +0.3455707464864055 +], +"UPP3MT": [ +0.00021900718293484957, +0.00021900718293484957 +], +"UPP3S": [ +0.00043801436586969915, +0.00043801436586969915 +], +"UPPDC1": [ +0.00021900718293484957, +0.00021900718293484957 +], +"USHD": [ +0.019107640139822572, +0.019107640139822572 +], +"VALTA": [ +-0.9865311138124047, +-0.9865311138124047 +], +"VPAMTr": [ +-0.5709477975311897, +-0.5709477975311897 +], +"ZN2tpp": [ +0.0003348943918420794, +0.0003348943918420794 +], +"Zn2tex": [ +0.0003348943918420794, +0.0003348943918420794 +] +}, +"source_sbml_sha256": "913f1543439b434ee3cbe66d7c4ad88a1ea949b618967bf14d091de2a5d4795d", +"unconstrained": { +"3OAR140": [ +0.0, +0.07643056055929029 +], +"3OAS140": [ +0.0, +0.07643056055929029 +], +"5DOAN": [ +0.0, +0.0006589857387860272 +], +"A5PISO": [ +0.0, +0.038215280279645145 +], +"AACPS3": [ +0.0, +0.12534281947806716 +], +"AACPS4": [ +0.0, +0.14773458526692176 +], +"AACPS7": [ +0.0, +0.07643056055929029 +], +"ACACT1r": [ +0.0, +0.3495079653042792 +], +"ACACT2r": [ +0.0, +0.3495079653042792 +], +"ACACT3r": [ +0.0, +0.3495079653042792 +], +"ACACT4r": [ +0.0, +0.3495079653042792 +], +"ACACT5r": [ +0.0, +0.3495079653042792 +], +"ACACT6r": [ +0.0, +0.2730774047449889 +], +"ACACT7r": [ +0.0, +0.2730774047449889 +], +"ACALD": [ +-20.0, +0.0 +], +"ACCOAC": [ +0.0, +0.07643645312923474 +], +"ACGK": [ +0.0, +0.29049387731075854 +], +"ACGS": [ +0.0, +0.29049387731075854 +], +"ACHBS": [ +0.0, +0.28532805765947017 +], +"ACKr": [ +-20.0, +40.0 +], +"ACLS": [ +0.0, +0.8586122591736293 +], +"ACOAD1f": [ +-0.3495079653042792, +0.0 +], +"ACOAD2f": [ +-0.3495079653042798, +0.0 +], +"ACOAD3f": [ +-0.3495079653042798, +0.0 +], +"ACOAD4f": [ +-0.3495079653042798, +0.0 +], +"ACOAD5f": [ +-0.3495079653042798, +0.0 +], +"ACOAD6f": [ +-0.27307740474498937, +0.0 +], +"ACOAD7f": [ +-0.12534281947806739, +0.0 +], +"ACODA": [ +0.0, +0.29049387731075815 +], +"ACONTa": [ +0.0, +20.0 +], +"ACONTb": [ +0.0, +20.0 +], +"ACOTA": [ +-0.29049387731075843, +0.0 +], +"ACPPAT160": [ +0.0, +0.06267140973903366 +], +"ACPPAT161": [ +0.0, +0.07386729263346088 +], +"ACt2rpp": [ +-20.0, +0.0 +], +"ACtex": [ +-20.0, +0.0 +], +"ADCL": [ +0.0, +0.0006570215488045652 +], +"ADCS": [ +0.0, +0.0006570215488045652 +], +"ADK1": [ +0.0, +231.85 +], +"ADNK1": [ +0.0, +0.026796461822320084 +], +"ADSK": [ +0.0, +0.24343777792447807 +], +"ADSL1r": [ +0.0, +0.29365229480098615 +], +"ADSL2r": [ +0.0, +0.43996677071129914 +], +"ADSS": [ +0.0, +0.29365229480098615 +], +"AGPAT160": [ +0.0, +0.06267140973903358 +], +"AGPAT161": [ +0.0, +0.07386729263346088 +], +"AGPR": [ +-0.2904938773107592, +0.0 +], +"AHCYSNS": [ +0.0, +0.0004399785558511782 +], +"AICART": [ +0.0, +0.5330084859439529 +], +"AIRC2": [ +0.0, +0.43996677071129914 +], +"AIRC3": [ +-0.43996677071130386, +0.0 +], +"AKGDH": [ +0.0, +20.0 +], +"ALAALAr": [ +0.0, +0.027290455602661832 +], +"ALAR": [ +0.0, +0.040935683403992826 +], +"ALCD2x": [ +-20.000000000000007, +0.0 +], +"AMAOTr": [ +0.0, +1.9641899814784713e-06 +], +"AMPMS2": [ +0.0, +0.00021900718293484957 +], +"ANPRT": [ +0.0, +0.055825225558590374 +], +"ANS": [ +0.0, +0.055825225558590374 +], +"AOXSr2": [ +0.0, +1.9641899814784713e-06 +], +"APG3PAT160": [ +0.0, +0.06267140973903358 +], +"APG3PAT161": [ +0.0, +0.07386729263346088 +], +"APRAUR": [ +0.0, +0.00043801436586978187 +], +"ARGSL": [ +0.0, +0.29049387731075854 +], +"ARGSS": [ +0.0, +0.29049387731075854 +], +"ASAD": [ +-1.0499410589844744, +0.0 +], +"ASNS2": [ +0.0, +0.23673399751769306 +], +"ASP1DC": [ +0.0, +0.0005656867146650571 +], +"ASPCT": [ +0.0, +0.32487014827160404 +], +"ASPK": [ +0.0, +1.0499410589844744 +], +"ASPO5": [ +0.0, +0.002237212388903731 +], +"ASPTA": [ +-2.875195044218068, +0.0 +], +"ATPM": [ +3.15, +235.0 +], +"ATPPRT": [ +0.0, +0.09304171523265384 +], +"ATPS4rpp": [ +-36.84999999999995, +209.99999999999994 +], +"BMOCOS": [ +0.0, +0.00011981558887018653 +], +"BMOGDS1": [ +0.0, +0.00011981558887018676 +], +"BMOGDS2": [ +0.0, +0.00011981558887018676 +], +"BPNT": [ +0.0, +0.24343777792447807 +], +"BTS5": [ +0.0, +1.9641899814784713e-06 +], +"CA2tex": [ +0.0, +0.005111804426797722 +], +"CAt6pp": [ +-0.005111804426797729, +0.0 +], +"CBMKr": [ +0.0, +0.6153640255823618 +], +"CDPMEK": [ +0.0, +0.0023462249328760286 +], +"CHORM": [ +0.0, +0.31760264534013366 +], +"CHORS": [ +0.0, +0.37430389963046345 +], +"CHRPL": [ +0.0, +0.00021900718293484957 +], +"CLt3_2pp": [ +0.0, +0.002555902213398861 +], +"CLtex": [ +0.0, +0.005111804426797722 +], +"CO2tex": [ +-60.0, +10.0 +], +"CO2tpp": [ +-60.0, +10.0 +], +"COBALT2tex": [ +0.0, +2.455237476848094e-05 +], +"COBALT2tpp": [ +0.0, +2.4552374768480895e-05 +], +"CPMPS": [ +0.0, +0.00023963117774037352 +], +"CPPPGO2": [ +0.0, +0.00021900718293484957 +], +"CS": [ +0.0, +20.0 +], +"CTECOAI7": [ +-0.14773458526692207, +0.0 +], +"CTPS2": [ +0.0, +0.15765276257839742 +], +"CU2tex": [ +0.0, +0.0006963053484341182 +], +"CU2tpp": [ +0.0, +0.0006963053484341182 +], +"CYSS": [ +0.0, +0.24343777792447807 +], +"CYSTL": [ +0.0, +0.15115915049962947 +], +"CYTBO3_4pp": [ +0.0, +120.0 +], +"CYTK1": [ +0.0, +0.1776658942996805 +], +"DADK": [ +-0.025697497527682838, +0.0 +], +"DALAt2pp": [ +0.0, +0.013645227801330971 +], +"DAPDC": [ +0.0, +0.33701571702207617 +], +"DAPE": [ +0.0, +0.364306172624738 +], +"DASYN160": [ +0.0, +0.06267140973903358 +], +"DASYN161": [ +0.0, +0.07386729263346088 +], +"DB4PS": [ +0.0, +0.0008760287317395637 +], +"DBTS": [ +0.0, +1.9641899814784713e-06 +], +"DDPA": [ +0.0, +0.37430389963046345 +], +"DHAD1": [ +0.0, +0.8586122591736279 +], +"DHAD2": [ +0.0, +0.28532805765947017 +], +"DHDPRy": [ +0.0, +0.364306172624738 +], +"DHDPS": [ +0.0, +0.364306172624738 +], +"DHFR": [ +0.0, +0.026354519076487407 +], +"DHFS": [ +0.0, +0.0006570215488045652 +], +"DHNPA2r": [ +0.0, +0.0006570215488045652 +], +"DHORDfum": [ +0.0, +0.32487014827160404 +], +"DHORTS": [ +-0.32487014827160393, +0.0 +], +"DHPPDA2": [ +0.0, +0.00043801436586934667 +], +"DHPS2": [ +0.0, +0.0006570215488045652 +], +"DHPTDCs2": [ +0.0, +0.0004399785558511782 +], +"DHQS": [ +0.0, +0.37430389963046345 +], +"DHQTi": [ +0.0, +0.37430389963046345 +], +"DMATT": [ +0.0, +0.00027302240742550706 +], +"DM_4CRSOL": [ +0.0, +0.00021900718293484995 +], +"DM_5DRIB": [ +0.0, +0.0006589857387860284 +], +"DM_AMOB": [ +0.0, +1.9641899814784747e-06 +], +"DM_MTHTHF": [ +0.0, +0.00043997855585120593 +], +"DNMPPA": [ +0.0, +0.0006570215488045652 +], +"DNTPPA": [ +0.0, +0.0006570215488045652 +], +"DPCOAK": [ +0.0, +0.0005656867146650571 +], +"DPR": [ +0.0, +0.0005656867146650571 +], +"DTMPK": [ +0.0, +0.02569749752768284 +], +"DURIK1": [ +0.0, +0.02569749752768284 +], +"DURIPP": [ +-0.025697497527682838, +0.0 +], +"DXPRIi": [ +0.0, +0.0023462249328760395 +], +"DXPS": [ +0.0, +0.0027842392987457282 +], +"D_LACt2pp": [ +-20.0, +0.0 +], +"D_LACtex": [ +-20.0, +0.0 +], +"E4PD": [ +0.0, +0.00021900718293484957 +], +"ECOAH1": [ +0.0, +0.3495079653042792 +], +"ECOAH2": [ +0.0, +0.3495079653042792 +], +"ECOAH3": [ +0.0, +0.3495079653042792 +], +"ECOAH4": [ +0.0, +0.3495079653042792 +], +"ECOAH5": [ +0.0, +0.3495079653042792 +], +"ECOAH6": [ +0.0, +0.2730774047449889 +], +"ECOAH7": [ +0.0, +0.2730774047449889 +], +"EDA": [ +0.0, +86.45 +], +"EDD": [ +0.0, +86.45 +], +"EGMEACPR": [ +0.0, +1.892353635803934e-06 +], +"ENO": [ +-66.44999999999999, +20.0 +], +"EPMEACPR": [ +0.0, +1.8946667502406367e-06 +], +"ETOHtex": [ +-20.0, +0.0 +], +"ETOHtrpp": [ +-20.0, +0.0 +], +"EX_Biomass": [ +0.0, +0.9820949907392347 +], +"EX_ac_e": [ +0.0, +20.0 +], +"EX_ca2_e": [ +-0.005111804426797734, +0.0 +], +"EX_cl_e": [ +-0.005111804426797722, +0.0 +], +"EX_co2_e": [ +-10.0, +60.0 +], +"EX_cobalt2_e": [ +-2.455237476848092e-05, +0.0 +], +"EX_cu2_e": [ +-0.0006963053484341182, +0.0 +], +"EX_etoh_e": [ +0.0, +20.0 +], +"EX_fe2_e": [ +-0.008105229958570912, +0.0 +], +"EX_fe3_e": [ +-0.007668197687691953, +0.0 +], +"EX_for_e": [ +0.0, +40.0 +], +"EX_glc_e": [ +-10.0, +-0.13404255319148936 +], +"EX_glyc_e": [ +0.0, +0.0 +], +"EX_h2_e": [ +0.0, +40.0 +], +"EX_h2o_e": [ +-12.000000000000746, +60.0 +], +"EX_h_e": [ +0.0, +40.00000000000001 +], +"EX_k_e": [ +-0.19169512124239138, +0.0 +], +"EX_lac__D_e": [ +0.0, +20.0 +], +"EX_meoh_e": [ +0.0, +1.9502739225143263e-06 +], +"EX_mg2_e": [ +-0.008519674044662862, +0.0 +], +"EX_mn2_e": [ +-0.0006786276386008131, +0.0 +], +"EX_mobd_e": [ +-0.00012669025380536162, +0.0 +], +"EX_nh4_e": [ +-10.60729568876746, +0.0 +], +"EX_ni2_e": [ +-0.00031721668200877367, +0.0 +], +"EX_o2_e": [ +-60.0, +0.0 +], +"EX_pi_e": [ +-0.9472424037079001, +0.0 +], +"EX_so4_e": [ +-0.24769810599430528, +0.0 +], +"EX_succ_e": [ +0.0, +17.096428571428554 +], +"EX_zn2_e": [ +-0.00033489439184207996, +0.0 +], +"Ec_biomass_iJO1366_core_53p95M": [ +0.0, +0.9820949907392375 +], +"F6PA": [ +0.0, +0.0 +], +"FACOAE120": [ +0.0, +0.07643056055929028 +], +"FACOAE160": [ +0.0, +0.12534281947806716 +], +"FACOAE161": [ +0.0, +0.14773458526692176 +], +"FADRx": [ +0.0, +2.146751619306988 +], +"FBA": [ +-76.44999999999995, +10.0 +], +"FBP": [ +0.0, +231.85 +], +"FCLT": [ +0.0, +0.00021900718293484984 +], +"FE2tex": [ +0.0, +0.008105229958570912 +], +"FE2tpp": [ +0.0, +0.008105229958570912 +], +"FE3abcpp": [ +0.0, +0.007668197687691953 +], +"FE3tex": [ +0.0, +0.007668197687691953 +], +"FHL": [ +0.0, +40.0 +], +"FLDR2": [ +0.0, +0.10680774071784556 +], +"FMNAT": [ +0.0, +0.00021900718293493232 +], +"FORtex": [ +-40.0, +0.0 +], +"FORtppi": [ +0.0, +40.0 +], +"FRD2": [ +0.0, +1000.0 +], +"FRD3": [ +0.0, +0.0006570215488045652 +], +"FTHFLi": [ +0.0, +0.0024090790122836096 +], +"FUM": [ +-16.681632653061225, +20.0 +], +"G1PACT": [ +0.0, +0.09279619148496902 +], +"G1SAT": [ +0.0, +0.003504114926957593 +], +"G3PD2": [ +-203.47999999999962, +0.0 +], +"G3PD5": [ +0.0, +203.48 +], +"G5SADs": [ +0.0, +0.21710191865281595 +], +"G5SD": [ +0.0, +0.21710191865281547 +], +"G6PDH2r": [ +0.0, +124.78333333333329 +], +"GAPD": [ +-66.45, +20.000000000000117 +], +"GARFT": [ +0.0, +0.44018577789424573 +], +"GCALDD": [ +0.0, +0.0006570215488045652 +], +"GF6PTA": [ +0.0, +0.09279619148496891 +], +"GHMT2r": [ +0.0, +1.0954709827551603 +], +"GK1": [ +0.0, +0.23935619114296666 +], +"GLCptspp": [ +0.0, +10.0 +], +"GLCt2pp": [ +0.0, +10.0 +], +"GLCtex": [ +0.13404255319148936, +10.0 +], +"GLNS": [ +0.0, +1.778148781097824 +], +"GLU5K": [ +0.0, +0.21710191865281547 +], +"GLUDy": [ +-8.398498254230542, +0.0 +], +"GLUPRT": [ +0.0, +0.4401857778942335 +], +"GLUR": [ +-0.02729045560266192, +0.0 +], +"GLUTRR": [ +0.0, +0.003504114926957597 +], +"GLUTRS": [ +0.0, +0.003504114926957593 +], +"GLXCL": [ +0.0, +0.0003285107744022826 +], +"GLYCDx": [ +0.0, +0.0 +], +"GLYCK": [ +0.0, +0.0003285107744023086 +], +"GLYCL": [ +0.0, +0.05361452973444125 +], +"GLYCTO4": [ +0.0, +0.0006570215488045652 +], +"GLYCtex": [ +0.0, +0.0 +], +"GLYCtpp": [ +0.0, +0.0 +], +"GLYK": [ +0.0, +0.0 +], +"GLYOX3": [ +0.0, +83.11666666666667 +], +"GMPS2": [ +0.0, +0.23935619114296722 +], +"GND": [ +0.0, +59.999999999999986 +], +"GRTT": [ +0.0, +0.00027302240742550587 +], +"GRXR": [ +0.0, +0.24343777792447807 +], +"GTHOr": [ +0.0, +0.24343777792447807 +], +"GTPCI": [ +0.0, +0.0006570215488045652 +], +"GTPCII2": [ +0.0, +0.00043801436586978187 +], +"H2Otex": [ +-60.0, +12.0 +], +"H2Otpp": [ +-60.0, +12.0 +], +"H2tex": [ +-40.0, +0.0 +], +"H2tpp": [ +-40.0, +0.0 +], +"HACD1": [ +0.0, +0.34950796530427947 +], +"HACD2": [ +0.0, +0.3495079653042792 +], +"HACD3": [ +0.0, +0.3495079653042792 +], +"HACD4": [ +0.0, +0.3495079653042792 +], +"HACD5": [ +0.0, +0.3495079653042792 +], +"HACD6": [ +0.0, +0.2730774047449889 +], +"HACD7": [ +0.0, +0.2730774047449889 +], +"HBZOPT": [ +0.0, +0.00021900718293484957 +], +"HCO3E": [ +0.0, +0.5164032238405338 +], +"HEX1": [ +0.0, +10.0 +], +"HISTD": [ +0.0, +0.09304171523265337 +], +"HISTP": [ +0.0, +0.09304171523265373 +], +"HMBS": [ +0.0, +0.00043801436586969915 +], +"HPPK2": [ +0.0, +0.0006570215488045652 +], +"HSDy": [ +-0.6856348863597362, +0.0 +], +"HSK": [ +0.0, +0.5344757358601075 +], +"HSST": [ +0.0, +0.15115915049962947 +], +"HSTPT": [ +0.0, +0.09304171523265373 +], +"Htex": [ +-40.0, +0.0 +], +"I2FE2SR": [ +0.0, +1.957246100660027e-06 +], +"I2FE2SS": [ +0.0, +0.0002808791673514203 +], +"I2FE2SS2": [ +0.0, +0.0002553446975922013 +], +"I2FE2ST": [ +0.0, +2.7498659740698602e-05 +], +"I4FE4SR": [ +0.0, +0.0002553446975922013 +], +"I4FE4ST": [ +0.0, +0.0002553446975922013 +], +"ICDHyr": [ +0.0, +20.00000000000006 +], +"ICL": [ +0.0, +20.00000000000001 +], +"ICYSDS": [ +0.0, +0.0017726814582843225 +], +"IG3PS": [ +0.0, +0.09304171523265373 +], +"IGPDH": [ +0.0, +0.09304171523265373 +], +"IGPS": [ +0.0, +0.055825225558590374 +], +"ILETA": [ +-0.285328057659471, +0.0 +], +"IMPC": [ +-0.5330084859439529, +0.0 +], +"IMPD": [ +0.0, +0.23935619114296655 +], +"IPDDI": [ +0.0, +0.00027302240742550706 +], +"IPDPS": [ +0.0, +0.0023462249328760286 +], +"IPMD": [ +0.0, +0.4424632561777479 +], +"IPPMIa": [ +-0.44246325617774884, +0.0 +], +"IPPMIb": [ +-0.4424632561777479, +0.0 +], +"IPPS": [ +0.0, +0.4424632561777484 +], +"K2L4Aabcpp": [ +0.0, +0.019107640139822572 +], +"K2L4Aabctex": [ +0.0, +0.019107640139822572 +], +"KARA1": [ +-0.8586122591736297, +0.0 +], +"KARA2": [ +0.0, +0.2853280576594705 +], +"KDOCT2": [ +0.0, +0.038215280279645145 +], +"KDOPP": [ +0.0, +0.038215280279645145 +], +"KDOPS": [ +0.0, +0.038215280279645145 +], +"Kt2pp": [ +0.0, +0.19169512124239144 +], +"Ktex": [ +0.0, +0.19169512124239144 +], +"LDH_D": [ +-20.0, +83.11666666666667 +], +"LEUTAi": [ +0.0, +0.44246325617774884 +], +"LPADSS": [ +0.0, +0.019107640139822572 +], +"MALCOAMT": [ +0.0, +1.964189981480436e-06 +], +"MALS": [ +0.0, +20.00000000000034 +], +"MCOATA": [ +0.0, +0.07643448893925325 +], +"MCTP1App": [ +0.0, +0.013645227801330942 +], +"MDH": [ +-104.675, +40.0 +], +"ME1": [ +0.0, +134.67499999999933 +], +"ME2": [ +0.0, +134.67500000000132 +], +"MECDPDH5": [ +0.0, +0.0023462249328760286 +], +"MECDPS": [ +0.0, +0.0023462249328760286 +], +"MEOHtex": [ +-1.8485461166856418e-06, +0.0 +], +"MEOHtrpp": [ +-1.8485461166856481e-06, +0.0 +], +"MEPCT": [ +0.0, +0.002346224932876037 +], +"METAT": [ +0.0, +0.0013199356675535352 +], +"METS": [ +0.0, +0.15159912905548065 +], +"MG2t3_2pp": [ +0.0, +0.00851967404466287 +], +"MG2tex": [ +0.0, +0.00851967404466287 +], +"MGSA": [ +0.0, +83.11666666666667 +], +"MN2tpp": [ +0.0, +0.0006786276386008134 +], +"MNtex": [ +0.0, +0.0006786276386008119 +], +"MOADSUx": [ +0.0, +0.00047926235548074705 +], +"MOAT": [ +0.0, +0.019107640139822572 +], +"MOAT2": [ +0.0, +0.019107640139822572 +], +"MOBDabcpp": [ +0.0, +0.0001266902538053614 +], +"MOBDtex": [ +0.0, +0.0001266902538053614 +], +"MOCOS": [ +0.0, +0.00011981558887018676 +], +"MOHMT": [ +0.0, +0.0005656867146650571 +], +"MPTAT": [ +0.0, +0.00023963117774037352 +], +"MPTG": [ +0.0, +0.013645227801330942 +], +"MPTS": [ +0.0, +0.00023963117774037352 +], +"MPTSS": [ +0.0, +0.00047926235548074705 +], +"MTHFC": [ +0.0, +0.971004192008838 +], +"MTHFD": [ +0.0, +0.971004192008838 +], +"MTHFR2": [ +0.0, +0.15159912905548065 +], +"MTHTHFSs": [ +0.0, +0.0004399785558511782 +], +"NADH16pp": [ +-1000.0, +120.0 +], +"NADH17pp": [ +0.0, +1000.0 +], +"NADK": [ +0.0, +0.000438996460859374 +], +"NADS1": [ +0.0, +0.002237212388903731 +], +"NADTRHD": [ +0.0, +508.70000000000897 +], +"NDPK1": [ +0.0, +0.7764462638684309 +], +"NDPK2": [ +0.0, +0.39196884222889017 +], +"NDPK3": [ +0.0, +0.1776658942996807 +], +"NDPK4": [ +0.0, +0.02569749752768284 +], +"NDPK8": [ +-0.025697497527682852, +0.0 +], +"NH4tex": [ +0.0, +10.607295688767467 +], +"NH4tpp": [ +0.0, +10.607295688767483 +], +"NI2tex": [ +0.0, +0.0003172166820087731 +], +"NI2tpp": [ +0.0, +0.0003172166820087731 +], +"NNATr": [ +0.0, +0.002237212388903731 +], +"NNDPR": [ +0.0, +0.002237212388903731 +], +"NTD2": [ +0.0, +0.02569749752768284 +], +"NTD6": [ +0.0, +0.02569749752768284 +], +"O2tex": [ +0.0, +60.0 +], +"O2tpp": [ +0.0, +60.0 +], +"OCBT": [ +0.0, +0.2904938773107589 +], +"OCTDPS": [ +0.0, +0.00021900718293484957 +], +"OGMEACPD": [ +0.0, +1.964189981480436e-06 +], +"OGMEACPR": [ +0.0, +1.964189981480436e-06 +], +"OGMEACPS": [ +0.0, +1.964189981480436e-06 +], +"OHPBAT": [ +0.0, +0.00021900718293484957 +], +"OMCDC": [ +0.0, +0.4424632561777479 +], +"OMPDC": [ +0.0, +0.32487014827160404 +], +"OPHBDC": [ +0.0, +0.00021900718293484957 +], +"OPHHX3": [ +0.0, +0.00021900718293484957 +], +"OPMEACPD": [ +0.0, +1.964189981480436e-06 +], +"OPMEACPR": [ +0.0, +1.964189981480436e-06 +], +"OPMEACPS": [ +0.0, +1.964189981480436e-06 +], +"ORPT": [ +-0.324870148271604, +0.0 +], +"P5CR": [ +0.0, +0.21710191865281572 +], +"PANTS": [ +0.0, +0.0005656867146650571 +], +"PAPPT3": [ +0.0, +0.027290455602661884 +], +"PAPSR2": [ +0.0, +0.24343777792447807 +], +"PDH": [ +0.0, +40.0 +], +"PDX5PO2": [ +0.0, +0.00021900718293484984 +], +"PDX5PS": [ +0.0, +0.00021900718293484957 +], +"PE160abcpp": [ +0.0, +0.045123336444504926 +], +"PE161abcpp": [ +0.0, +0.020682920504968304 +], +"PERD": [ +0.0, +0.00021900718293484957 +], +"PFK": [ +0.0, +241.85 +], +"PFL": [ +0.0, +39.99999999999986 +], +"PGAMT": [ +-0.09279619148496913, +0.0 +], +"PGCD": [ +0.0, +1.6873737411037526 +], +"PGI": [ +-114.78333333333329, +10.0 +], +"PGK": [ +-20.0, +66.45 +], +"PGL": [ +0.0, +124.78333333333327 +], +"PGM": [ +-20.0, +66.45 +], +"PHETA1": [ +-0.18195273893425817, +0.0 +], +"PIt2rpp": [ +0.0, +0.9472424037078611 +], +"PItex": [ +0.0, +0.9472424037079095 +], +"PMDPHT": [ +0.0, +0.00043801436586978187 +], +"PMEACPE": [ +0.0, +1.9641899814784713e-06 +], +"PMPK": [ +0.0, +0.00021900718293484957 +], +"PNTK": [ +0.0, +0.0005656867146650571 +], +"POX": [ +0.0, +40.0 +], +"PPBNGS": [ +0.0, +0.0017520574634787966 +], +"PPC": [ +0.0, +231.85 +], +"PPCDC": [ +0.0, +0.0005656867146650577 +], +"PPCK": [ +0.0, +231.85 +], +"PPKr": [ +-3.4155662963075777, +0.0 +], +"PPM": [ +-0.0010989642946372113, +0.0 +], +"PPNCL2": [ +0.0, +0.0005656867146650577 +], +"PPND": [ +0.0, +0.13564990640587546 +], +"PPNDH": [ +0.0, +0.1819527389342582 +], +"PPPGO3": [ +0.0, +0.00021900718293484957 +], +"PPS": [ +0.0, +231.85 +], +"PRAGSr": [ +0.0, +0.44018577789422847 +], +"PRAIS": [ +0.0, +0.44018577789423396 +], +"PRAIi": [ +0.0, +0.055825225558590374 +], +"PRAMPC": [ +0.0, +0.09304171523265373 +], +"PRASCSi": [ +0.0, +0.43996677071129914 +], +"PRATPP": [ +0.0, +0.09304171523265373 +], +"PRFGS": [ +0.0, +0.44018577789423396 +], +"PRMICI": [ +0.0, +0.09304171523265373 +], +"PROt2rpp": [ +-0.005111804426797721, +0.0 +], +"PROt4pp": [ +0.0, +0.005111804426797728 +], +"PRPPS": [ +0.0, +0.9161600793459858 +], +"PSCVT": [ +0.0, +0.3743038996304635 +], +"PSD160": [ +0.0, +0.06267140973903358 +], +"PSD161": [ +0.0, +0.07386729263346088 +], +"PSERT": [ +0.0, +1.6873737411037526 +], +"PSP_L": [ +0.0, +1.6873737411037526 +], +"PSSA160": [ +0.0, +0.06267140973903358 +], +"PSSA161": [ +0.0, +0.07386729263346088 +], +"PTAr": [ +-40.0, +20.0 +], +"PTPATi": [ +0.0, +0.0005656867146654953 +], +"PUNP1": [ +-0.02679646182232002, +0.0 +], +"PUNP2": [ +0.0, +0.02569749752768287 +], +"PYK": [ +0.0, +249.35 +], +"PYNP2r": [ +0.0, +0.02569749752768282 +], +"QULNS": [ +0.0, +0.002237212388903731 +], +"RBFK": [ +0.0, +0.00021900718293493232 +], +"RBFSa": [ +0.0, +0.0008760287317395637 +], +"RBFSb": [ +0.0, +0.00043801436586978187 +], +"RHCCE": [ +0.0, +0.0004399785558511782 +], +"RNTR1c2": [ +0.0, +0.05139499505536568 +], +"RNTR2c2": [ +0.0, +0.02653326036480193 +], +"RNTR3c2": [ +0.0, +0.02653326036480193 +], +"RPE": [ +-0.7570062990739757, +39.99999999999999 +], +"RPI": [ +-19.999999999999957, +0.0 +], +"SADT2": [ +0.0, +0.2434377779244782 +], +"SDPDS": [ +0.0, +0.364306172624738 +], +"SDPTA": [ +-0.36430617262473836, +0.0 +], +"SERAT": [ +0.0, +0.24343777792447832 +], +"SHCHD2": [ +0.0, +0.00021900718293484957 +], +"SHCHF": [ +0.0, +0.00021900718293484957 +], +"SHK3Dr": [ +0.0, +0.3743038996304635 +], +"SHKK": [ +0.0, +0.3743038996304635 +], +"SHSL1": [ +0.0, +0.15115915049962947 +], +"SO4t2pp": [ +0.0, +0.24769810599430486 +], +"SO4tex": [ +0.0, +0.24769810599430486 +], +"SUCCt2_2pp": [ +0.0, +309.1333333333332 +], +"SUCCt3pp": [ +0.0, +309.1333333333332 +], +"SUCCtex": [ +-17.09642857142859, +0.0 +], +"SUCDi": [ +0.0, +1000.0 +], +"SUCOAS": [ +-20.0, +0.5129978586096694 +], +"SULRi": [ +0.0, +0.24343777792447713 +], +"TALA": [ +-0.19256842188079681, +19.999999999999996 +], +"TDSK": [ +0.0, +0.01910764013982258 +], +"THD2pp": [ +0.0, +463.70000000000806 +], +"THDPS": [ +0.0, +0.364306172624738 +], +"THRD_L": [ +0.0, +0.28532805765947017 +], +"THRS": [ +0.0, +0.5344757358601069 +], +"THZPSN3": [ +0.0, +0.00021900718293484957 +], +"TKT1": [ +-0.19256842188079104, +19.999999999999996 +], +"TKT2": [ +-0.5644378771931817, +19.999999999999996 +], +"TMDS": [ +0.0, +0.025697497527682835 +], +"TMPK": [ +0.0, +0.00021900718293484957 +], +"TMPPP": [ +0.0, +0.00021900718293484957 +], +"TPI": [ +-76.44999999999999, +10.0 +], +"TRPAS2": [ +-0.055825225558590354, +0.0 +], +"TRPS3": [ +0.0, +0.055825225558590395 +], +"TRSARr": [ +0.0, +0.0003285107744022826 +], +"TYRL": [ +0.0, +0.00021900718293484957 +], +"TYRTA": [ +-0.13564990640587532, +0.0 +], +"U23GAAT": [ +0.0, +0.038215280279645186 +], +"UAAGDS": [ +0.0, +0.027290455602661884 +], +"UAGAAT": [ +0.0, +0.038215280279645145 +], +"UAGCVT": [ +0.0, +0.027290455602661884 +], +"UAGDP": [ +0.0, +0.09279619148496891 +], +"UAGPT3": [ +0.0, +0.027290455602661884 +], +"UAMAGS": [ +0.0, +0.027290455602661884 +], +"UAMAS": [ +0.0, +0.027290455602661884 +], +"UAPGR": [ +0.0, +0.027290455602661884 +], +"UDCPDP": [ +0.0, +0.027290455602661884 +], +"UDCPDPS": [ +0.0, +5.401522449065747e-05 +], +"UGMDDS": [ +0.0, +0.027290455602661884 +], +"UHGADA": [ +0.0, +0.038215280279645145 +], +"UMPK": [ +0.0, +0.3455707464864057 +], +"UPP3MT": [ +0.0, +0.00021900718293484957 +], +"UPP3S": [ +0.0, +0.00043801436586969915 +], +"UPPDC1": [ +0.0, +0.00021900718293484957 +], +"USHD": [ +0.0, +0.019107640139822572 +], +"VALTA": [ +-0.9865311138124037, +0.0 +], +"VPAMTr": [ +-0.5709477975311907, +0.0 +], +"ZN2tpp": [ +0.0, +0.0003348943918420798 +], +"Zn2tex": [ +0.0, +0.0003348943918420794 +] +} +} \ No newline at end of file diff --git a/cnapy/tests/generate_flux_space_reference_data.py b/cnapy/tests/generate_flux_space_reference_data.py new file mode 100644 index 00000000..54b4a608 --- /dev/null +++ b/cnapy/tests/generate_flux_space_reference_data.py @@ -0,0 +1,146 @@ +"""Generates tests/data/ecc2_flux_space_reference.json, the straindesign +ground truth for test_flux_space_ecc2.py (manually run, not part of pytest). + +Same idea as generate_fva_reference_data.py: straindesign's own +plot_flux_space is the independent reference for CNApy's HiGHS-native +lptools_highs.plot_flux_space_data_highs, but it is slow (and memory-hungry +on the 2-yield grid cases), so it is run once here and cached instead of on +every test run. + +The JSON also holds the case definitions themselves (axes, constraints, +points), so this file is the single place to add or change a case; the +test reads them back from the JSON. + +What is stored per case, and why: + bbox per-axis (min, max) over straindesign's datapoints -- every + path, exact or approximate, has to reproduce the plot's + extents (these are LP optima, independent of meshing) + area 2D rate/rate only: exact polygon, so its area must match too + hull_volume 3D rate/rate/rate only: exact polytope, convex-hull volume +Facet counts and triangulations are deliberately NOT stored: the two +implementations don't have to mesh the same way. + +The fixture hash is taken over the *decompressed* SBML, so re-gzipping the +same model (gzip headers carry a timestamp) doesn't invalidate the cache; +editing the model does. + +Run from the repo root: + python cnapy/tests/generate_flux_space_reference_data.py [--solver glpk|cplex|gurobi|scip] +""" +import argparse +import gzip +import hashlib +import json +import time +from pathlib import Path + +DATA_DIR = Path(__file__).parent / "data" +ECC2_SBML = DATA_DIR / "ECC2_model.sbml.gz" +REFERENCE_JSON = DATA_DIR / "ecc2_flux_space_reference.json" + +BM, ETOH, SUCC, GLC = "Ec_biomass_iJO1366_core_53p95M", "EX_etoh_e", "EX_succ_e", "EX_glc_e" +CONSTRAINTS = "EX_o2_e = 0" # anaerobic, as in the ECC2anaerob benchmarks +POINTS = 12 # low enough for CI; the dialog default is 20 + + +def _rate(rid): + return "1.0 " + rid + + +def _yield(rid): + return ["1.0 " + rid, "-1.0 " + GLC] # list, not tuple: survives the JSON round trip unchanged + + +# axes in the form PlotSpaceDialog passes them: a string for a rate axis, +# a (numerator, denominator) pair for a yield axis +CASES = { + "2d_rr": [_rate(BM), _rate(ETOH)], # exact polygon + "2d_ry": [_rate(BM), _yield(ETOH)], # adaptive boundary + "2d_yy": [_yield(ETOH), _yield(SUCC)], # adaptive boundary, two yields + "3d_rrr": [_rate(BM), _rate(ETOH), _rate(SUCC)], # exact polytope + "3d_rry": [_rate(BM), _rate(ETOH), _yield(SUCC)], # slices along the yield axis + "3d_yrr": [_yield(SUCC), _rate(BM), _rate(ETOH)], # same, yield as axis 0 (mesh handedness flip) + "3d_ryy": [_rate(BM), _yield(ETOH), _yield(SUCC)], # grid scan + "3d_yry": [_yield(ETOH), _rate(BM), _yield(SUCC)], # grid scan, axis 0 pinned as a yield +} + + +def fixture_sha256(path: Path) -> str: + data = gzip.decompress(path.read_bytes()) if path.suffix == ".gz" else path.read_bytes() + return hashlib.sha256(data).hexdigest() + + +def load_model(path: Path): + try: # same loader as conftest.py's ecc2_reference_model + from cnapy.cobra_cnapy import CNApyModel + return CNApyModel.read_sbml_model(str(path)) + except ImportError: + import cobra + return cobra.io.read_sbml_model(str(path)) + + +def bbox(points): + pts = [[float(v) for v in p] for p in points] + return [[min(p[k] for p in pts), max(p[k] for p in pts)] for k in range(len(pts[0]))] + + +def polygon_area(points): + pts = [(float(p[0]), float(p[1])) for p in points] + if len(pts) > 1 and pts[0] == pts[-1]: + pts = pts[:-1] + return abs(sum(pts[i][0] * pts[(i + 1) % len(pts)][1] - pts[(i + 1) % len(pts)][0] * pts[i][1] + for i in range(len(pts)))) / 2.0 + + +def _package_version(name): + try: + from importlib.metadata import version + return version(name) + except Exception: + return "unknown" + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--solver", default=None, help="straindesign solver (default: straindesign's own choice)") + args = ap.parse_args() + + import matplotlib + matplotlib.use("Agg") # standalone script: switching the backend globally is fine here + import matplotlib.pyplot as plt + import numpy as np + from scipy.spatial import ConvexHull + from straindesign import plot_flux_space, select_solver + + model = load_model(ECC2_SBML) + solver = select_solver(args.solver, model) + extra = {"solver": solver} + + out = { + "source_sbml_sha256": fixture_sha256(ECC2_SBML), + "straindesign_version": _package_version("straindesign"), + "straindesign_solver": solver, + "constraints": CONSTRAINTS, + "points": POINTS, + "cases": {}, + } + for name, axes in CASES.items(): + t0 = time.perf_counter() + sd_axes = [ax if isinstance(ax, str) else tuple(ax) for ax in axes] + datapoints, _, _ = plot_flux_space(model, sd_axes, points=POINTS, constraints=CONSTRAINTS, + show=False, **extra) + plt.close("all") + entry = {"axes": axes, "bbox": bbox(datapoints)} + if name == "2d_rr": + entry["area"] = polygon_area(datapoints) + if name == "3d_rrr": + entry["hull_volume"] = float(ConvexHull(np.asarray(datapoints, dtype=float)).volume) + out["cases"][name] = entry + print(f"{name}: {time.perf_counter() - t0:.1f}s") + + REFERENCE_JSON.write_text(json.dumps(out, indent=2) + "\n") + print(f"wrote {REFERENCE_JSON}") + + +if __name__ == "__main__": + main() diff --git a/cnapy/tests/generate_fva_reference_data.py b/cnapy/tests/generate_fva_reference_data.py new file mode 100644 index 00000000..9f31c58a --- /dev/null +++ b/cnapy/tests/generate_fva_reference_data.py @@ -0,0 +1,104 @@ +""" +(Re)generates tests/data/ecc2_fva_reference.json - the cobrapy ground-truth +FVA bounds that test_core_units.py checks multi_threaded_HiGHS_FVA against. + +This is NOT run automatically by pytest. Computing full FVA on a 500-reaction +model twice per test run (once per test, unconstrained and growth-coupled) +adds real time to every CI run for a result that never changes, since the +ECC2 fixture model itself never changes - so it's computed once, here, and +committed as data. + +Run this manually whenever tests/data/ECC2_model.sbml is replaced with a +different model: + + python tests/generate_fva_reference_data.py + +The output JSON embeds a sha256 of the source SBML file; conftest.py's +`fva_reference` fixture checks that hash against the live fixture file on +every test run and fails loudly (rather than silently comparing against a +stale reference) if they no longer match - that's the signal this script +needs to be re-run. +""" +import hashlib +import json +import sys +import time +from pathlib import Path + +import cobra + +DATA_DIR = Path(__file__).parent / "data" +SBML_PATH = DATA_DIR / "ECC2_model.sbml.gz" +OUTPUT_PATH = DATA_DIR / "ecc2_fva_reference.json" + + +def _finite_bounds_model(model: cobra.Model) -> cobra.Model: + """Same conversion MainWindow.fva() applies before calling + multi_threaded_HiGHS_FVA, and that test_core_units.py's own + _finite_bounds_model applies - kept in sync manually since this script + intentionally has no dependency on the cnapy package (it only needs + cobra), so it can be run even without a CNApy dev environment set up.""" + cfg = cobra.Configuration() + m = model.copy() + for r in m.reactions: + if r.lower_bound == -float("inf"): + r.lower_bound = cfg.lower_bound + if r.upper_bound == float("inf"): + r.upper_bound = cfg.upper_bound + return m + + +def main() -> None: + print(f"Loading {SBML_PATH} ...") + model = _finite_bounds_model(cobra.io.read_sbml_model(str(SBML_PATH))) + + biomass_candidates = [r for r in model.reactions if r.objective_coefficient != 0] + if len(biomass_candidates) != 1: + sys.exit( + f"Expected exactly one objective reaction, found " + f"{len(biomass_candidates)}: {[r.id for r in biomass_candidates]}. " + "Update this script if the fixture model's objective changed shape." + ) + biomass = biomass_candidates[0] + + print("Computing unconstrained (fraction_of_optimum=0.0) reference FVA ...") + t0 = time.time() + unconstrained = cobra.flux_analysis.flux_variability_analysis( + model, fraction_of_optimum=0.0, processes=1 + ) + print(f" done in {time.time() - t0:.1f}s") + + opt = model.copy().optimize().objective_value + print(f"Biomass reaction: {biomass.id}, optimum: {opt!r}") + + print("Computing growth-coupled (biomass fixed at optimum) reference FVA ...") + t0 = time.time() + growth_model = model.copy() + growth_model.reactions.get_by_id(biomass.id).bounds = (opt, opt) + growth_coupled = cobra.flux_analysis.flux_variability_analysis( + growth_model, fraction_of_optimum=0.0, processes=1 + ) + print(f" done in {time.time() - t0:.1f}s") + + sha256 = hashlib.sha256(SBML_PATH.read_bytes()).hexdigest() + + data = { + "source_sbml_sha256": sha256, + "biomass_reaction_id": biomass.id, + "biomass_objective_value": opt, + "unconstrained": { + rid: [float(row["minimum"]), float(row["maximum"])] + for rid, row in unconstrained.iterrows() + }, + "growth_coupled": { + rid: [float(row["minimum"]), float(row["maximum"])] + for rid, row in growth_coupled.iterrows() + }, + } + OUTPUT_PATH.write_text(json.dumps(data, indent=0, sort_keys=True)) + print(f"Wrote {OUTPUT_PATH} ({len(data['unconstrained'])} reactions, " + f"sha256={sha256[:12]}...)") + + +if __name__ == "__main__": + main() diff --git a/cnapy/tests/test_cobra_cnapy.py b/cnapy/tests/test_cobra_cnapy.py new file mode 100644 index 00000000..b4f823b8 --- /dev/null +++ b/cnapy/tests/test_cobra_cnapy.py @@ -0,0 +1,63 @@ +"""Tests for cnapy/cobra_cnapy.py: the stoichiometry hash must follow every change of +reaction IDs, bounds and stoichiometries, also when made directly on the model.""" +import cobra + +from cnapy.cobra_cnapy import CNApyModel, stoichiometry_hash + + +def small_model() -> CNApyModel: + model = CNApyModel("small") + a, b = cobra.Metabolite("A"), cobra.Metabolite("B") + r1, r2 = cobra.Reaction("R1", lower_bound=-10, upper_bound=10), cobra.Reaction("R2") + model.add_reactions([r1, r2]) + r1.add_metabolites({a: -1, b: 1}) + r2.add_metabolites({b: -1}) + return model + + +def test_unchanged_model_same_hash(): + model = small_model() + assert stoichiometry_hash(model).digest() == stoichiometry_hash(model).digest() + assert model.stoichiometry_hash_object.hexdigest() == stoichiometry_hash(small_model()).hexdigest() + + +def test_changes_alter_hash(): + model = small_model() + reference = stoichiometry_hash(model).digest() + model.reactions.R1.upper_bound = 5 + assert stoichiometry_hash(model).digest() != reference + model.reactions.R1.upper_bound = 10 + assert stoichiometry_hash(model).digest() == reference + model.reactions.R2.add_metabolites({cobra.Metabolite("C"): 2}) + assert stoichiometry_hash(model).digest() != reference + + model = small_model() + model.reactions.R2.id = "R3" + assert stoichiometry_hash(model).digest() != reference + + +def test_context_undo_restores_hash(): + model = small_model() + reference = stoichiometry_hash(model).digest() + with model: + model.reactions.R1.bounds = (0, 1) + assert stoichiometry_hash(model).digest() != reference + assert stoichiometry_hash(model).digest() == reference + + +def test_coefficient_number_type_does_not_matter(): + model1, model2 = small_model(), small_model() + model2.reactions.R1.add_metabolites({model2.metabolites.A: 1.0, model2.metabolites.B: -1.0}, combine=False) + model2.reactions.R1.add_metabolites({model2.metabolites.A: -1.0, model2.metabolites.B: 1.0}, combine=False) + assert stoichiometry_hash(model1).digest() == stoichiometry_hash(model2).digest() + + +def test_copy_and_optlang_enumerator_compatibility(): + model = small_model() + assert isinstance(model.copy(), CNApyModel) + # these are called by optlang_enumerator.mcs_computation + model.set_reaction_hashes() + model.set_stoichiometry_hash_object() + model._stoichiometry_hash_object = None + model.restore_stoichiometry_hash_object(None) + assert model.stoichiometry_hash_object.copy().hexdigest() == stoichiometry_hash(model).hexdigest() diff --git a/cnapy/tests/test_core_functions.py b/cnapy/tests/test_core_functions.py new file mode 100644 index 00000000..6f46b25f --- /dev/null +++ b/cnapy/tests/test_core_functions.py @@ -0,0 +1,301 @@ +"""Tests for functions in cnapy/core.py that had no tests yet (yield optimization, making a scenario +feasible, element balances, biomass weight, replacing IDs) and for FluxVectorContainer. + +The optlang-based functions are run with GLPK, because the pip versions of Gurobi/CPLEX that may be +the default solver have size-limited licenses. + +run: pytest cnapy/tests/test_core_functions.py +""" +import math + +import cobra +import numpy +import pytest + +from cnapy.appdata import Scenario +from cnapy.core import (check_biomass_weight, element_exchange_balance, highs_yopt, make_scenario_feasible, + make_scenario_feasible_osqp, replace_ids) +from cnapy.flux_vector_container import FluxVectorContainer + +BIOMASS = "Ec_biomass_iJO1366_core_53p95M" + + +@pytest.fixture(scope="module") +def model(ecc2_reference_model): + """shared by the tests of this module, which must not change it (switching the solver takes ~2 s)""" + model = ecc2_reference_model.copy() + model.solver = "glpk" + return model + + +@pytest.fixture(scope="module") +def max_growth(model): + """maximal growth rate with glucose uptake fixed at 10""" + with model: + model.reactions.EX_glc_e.bounds = (-10.0, -10.0) + model.objective = BIOMASS + return model.slim_optimize() + + +# --- highs_yopt ------------------------------------------------------------------------------- + +def test_yopt_biomass_yield_matches_fba(model, max_growth): + assert model.reactions.EX_glc_e.lower_bound == -10.0 # yield is highest at maximal uptake (maintenance) + sol = highs_yopt(model, {BIOMASS: 1.0}, {"EX_glc_e": -1.0}) + assert sol.status == "optimal" and not sol.scalable + assert sol.objective_value == pytest.approx(max_growth / 10.0, rel=1e-6) + assert sol.fluxes[BIOMASS] / -sol.fluxes["EX_glc_e"] == pytest.approx(sol.objective_value, rel=1e-6) + + +def test_yopt_minimize_and_scenario_constraints(model): + sol = highs_yopt(model, {"EX_ac_e": 1.0}, {"EX_glc_e": -1.0}, obj_sense="minimize", + constraints=[({"EX_ac_e": 1.0}, ">=", 2.0)]) + assert sol.status == "optimal" + assert sol.fluxes["EX_ac_e"] >= 2.0 - 1e-6 + assert sol.objective_value == pytest.approx(sol.fluxes["EX_ac_e"] / -sol.fluxes["EX_glc_e"], rel=1e-6) + assert sol.objective_value == pytest.approx(0.2, rel=1e-6) # 2 / 10: the minimum at maximal uptake + sol_max = highs_yopt(model, {"EX_ac_e": 1.0}, {"EX_glc_e": -1.0}) + assert sol_max.status == "optimal" and sol_max.objective_value > sol.objective_value + + +def test_yopt_infeasible_constraints(model): + sol = highs_yopt(model, {BIOMASS: 1.0}, {"EX_glc_e": -1.0}, constraints=[({BIOMASS: 1.0}, ">=", 2.0)]) + assert sol.status == "infeasible" and math.isnan(sol.objective_value) + + +def test_yopt_denominator_fixed_at_zero(model): + sol = highs_yopt(model, {BIOMASS: 1.0}, {"EX_ac_e": 1.0}, constraints=[({"EX_ac_e": 1.0}, "=", 0.0)]) + assert sol.status == "infeasible" and math.isnan(sol.objective_value) + + +def test_yopt_undefined_yield(model): + # acetate secretion can be zero while there is growth, so the yield is not bounded + sol = highs_yopt(model, {BIOMASS: 1.0}, {"EX_ac_e": 1.0}) + assert sol.status == "unbounded" and math.isnan(sol.objective_value) + + +def test_yopt_unknown_reaction(model): + with pytest.raises(KeyError): + highs_yopt(model, {"UNKNOWN": 1.0}, {"EX_glc_e": -1.0}) + + +# --- make_scenario_feasible ------------------------------------------------------------------- + +def infeasible_scenario(): + scen = Scenario() + scen[BIOMASS] = (1.2, 1.2) # more than the maximal growth rate with this glucose uptake + scen["EX_glc_e"] = (-10.0, -10.0) + return scen + + +def assert_fluxes_feasible(model, fluxes, reactions): + with model: + for r in reactions: + model.reactions.get_by_id(r).bounds = (fluxes[r] - 1e-6, fluxes[r] + 1e-6) + assert model.slim_optimize(error_value=None) is not None + + +def test_make_scenario_feasible_lp(model, max_growth): + solution, reactions_in_objective, bm_mod, _, _ = make_scenario_feasible(model, infeasible_scenario()) + assert solution.status == "optimal" + assert sorted(reactions_in_objective) == sorted([BIOMASS, "EX_glc_e"]) + assert solution.objective_value == pytest.approx(1.2 - max_growth, abs=1e-6) # weighted L1 change + assert solution.fluxes[BIOMASS] == pytest.approx(max_growth, abs=1e-6) + assert solution.fluxes["EX_glc_e"] == pytest.approx(-10.0, abs=1e-6) + assert bm_mod == {} + assert_fluxes_feasible(model, solution.fluxes, [BIOMASS, "EX_glc_e"]) + + +@pytest.mark.parametrize("osqp_directly", [True, False], ids=["osqp", "use_QP"]) +def test_make_scenario_feasible_qp(model, max_growth, osqp_directly): + if osqp_directly: + solution, reactions_in_objective, _, _, _ = make_scenario_feasible_osqp(model, infeasible_scenario()) + else: # with GLPK as solver, use_QP=True must delegate to OSQP + solution, reactions_in_objective, _, _, _ = make_scenario_feasible(model, infeasible_scenario(), + use_QP=True) + assert solution.status == "optimal" + assert sorted(reactions_in_objective) == sorted([BIOMASS, "EX_glc_e"]) + assert solution.objective_value == pytest.approx((1.2 - max_growth) ** 2, abs=1e-5) # squared change + assert solution.fluxes[BIOMASS] == pytest.approx(max_growth, abs=1e-4) + assert solution.fluxes["EX_glc_e"] == pytest.approx(-10.0, abs=1e-4) + + +def test_make_scenario_feasible_keeps_feasible_scenario(model): + scen = infeasible_scenario() + scen[BIOMASS] = (0.5, 0.5) + solution, _, _, _, _ = make_scenario_feasible(model, scen) + assert solution.status == "optimal" + assert solution.objective_value == pytest.approx(0.0, abs=1e-9) + assert solution.fluxes[BIOMASS] == pytest.approx(0.5, abs=1e-9) + + +@pytest.mark.parametrize("osqp", [False, True], ids=["lp", "osqp"]) +def test_make_scenario_feasible_adjusted_biomass_scenario_reaction(model, max_growth, osqp): + # setup as created by the dialog: the adjusted biomass reaction is a scenario reaction with the bounds of the + # original one and the growth rate as scenario value, the original biomass reaction is switched off + biomass = model.reactions.get_by_id(BIOMASS) + adjusted = "adjusted_" + BIOMASS + scen = infeasible_scenario() + scen.reactions = {adjusted: [{m.id: c for m, c in biomass.metabolites.items()}, *biomass.bounds]} + scen[adjusted] = scen.pop(BIOMASS) + scen[BIOMASS] = (0.0, 0.0) + if osqp: + solution, reactions_in_objective, _, _, _ = make_scenario_feasible_osqp(model, scen) + abs_tol = 1e-4 + else: + solution, reactions_in_objective, _, _, _ = make_scenario_feasible(model, scen) + abs_tol = 1e-6 + assert solution.status == "optimal" + assert sorted(reactions_in_objective) == sorted([adjusted, "EX_glc_e"]) + assert solution.fluxes[adjusted] == pytest.approx(max_growth, abs=abs_tol) + assert solution.fluxes[BIOMASS] == pytest.approx(0.0, abs=abs_tol) + assert adjusted not in model.reactions + + +@pytest.mark.parametrize("osqp", [False, True], ids=["lp", "osqp"]) +@pytest.mark.parametrize("scenario_reaction", [False, True], ids=["model_reaction", "scenario_reaction"]) +def test_make_scenario_feasible_weights_from_scenario_annotations(model, max_growth, osqp, scenario_reaction): + scen = infeasible_scenario() + reac_id = BIOMASS + if scenario_reaction: + biomass = model.reactions.get_by_id(BIOMASS) + reac_id = "adjusted_" + BIOMASS + scen.reactions = {reac_id: [{m.id: c for m, c in biomass.metabolites.items()}, *biomass.bounds]} + scen[reac_id] = scen.pop(BIOMASS) + scen[BIOMASS] = (0.0, 0.0) + # the later scenario annotation takes precedence + scen.annotations = [{"reaction_id": reac_id, "key": "weight", "value": "3"}, + {"reaction_id": reac_id, "key": "weight", "value": "2"}] + if osqp: + solution, _, _, _, _ = make_scenario_feasible_osqp(model, scen, weights_key="weight") + assert solution.objective_value == pytest.approx((1.2 - max_growth) ** 2 / 2, abs=1e-5) + else: + solution, _, _, _, _ = make_scenario_feasible(model, scen, weights_key="weight") + assert solution.objective_value == pytest.approx((1.2 - max_growth) / 2, abs=1e-6) + assert solution.status == "optimal" + assert "weight" not in model.reactions.get_by_id(BIOMASS).annotation + + +def test_make_scenario_feasible_leaves_model_unchanged(model): + bounds = {r.id: r.bounds for r in model.reactions} + objective = str(model.objective.expression) + make_scenario_feasible(model, infeasible_scenario()) + assert {r.id: r.bounds for r in model.reactions} == bounds + assert str(model.objective.expression) == objective + + +# --- element_exchange_balance / check_biomass_weight ------------------------------------------ + +def test_element_exchange_balance(model): + scen = Scenario() + scen["EX_glc_e"] = (-10.0, -10.0) # C6H12O6 + scen["EX_ac_e"] = (5.0, 5.0) # C2H3O2 + scen["EX_co2_e"] = (20.0, 20.0) # CO2 + messages = [] + influx, efflux = element_exchange_balance(model, scen, [], organic_elements_only=True, + print_func=lambda *args: messages.append(args)) + assert dict(influx) == pytest.approx({"C": 60.0, "H": 120.0, "O": 60.0}) + assert dict(efflux) == pytest.approx({"C": -30.0, "H": -15.0, "O": -50.0}) + assert messages # the balance table + + +def test_element_exchange_balance_non_boundary_reactions(model): + scen = Scenario() + scen["PGI"] = (2.0, 2.0) # internal reaction, balanced: only counted if requested + messages = [] + influx, efflux = element_exchange_balance(model, scen, ["PGI", "PFK"], + print_func=lambda *args: messages.append(" ".join(map(str, args)))) + # g6p_c -> f6p_c: both C6, so in- and outflux of each element cancel + assert set(influx) == set(efflux) and all(influx[el] + efflux[el] == pytest.approx(0.0) for el in influx) + assert influx["C"] == pytest.approx(12.0) + assert any("PFK" in m and "fixed" in m for m in messages) # PFK has no fixed flux value + + +def test_check_biomass_weight(model): + # the iJO1366 core biomass is normalized to 1 g/mmol + assert check_biomass_weight(model, BIOMASS) == pytest.approx(1.0, abs=1e-3) + + +# --- replace_ids ------------------------------------------------------------------------------ + +def test_replace_metabolite_ids_from_annotation(ecc2_model): + old = {m.id: m.annotation.get("metanetx.chemical") for m in ecc2_model.metabolites} + replace_ids(ecc2_model.metabolites, "metanetx.chemical") + by_original = {m.annotation.get("original ID", m.id): m for m in ecc2_model.metabolites} + assert set(by_original) == set(old) + for old_id, mnx in old.items(): + met = by_original[old_id] + if mnx is None: + assert met.id == old_id and "original ID" not in met.annotation + else: + assert met.id == mnx + "_" + met.compartment + assert len({m.id for m in ecc2_model.metabolites}) == len(ecc2_model.metabolites) + ecc2_model.metabolites.get_by_id(next(iter(by_original.values())).id) # DictList index was updated + + +def test_replace_reaction_ids_unique_only(ecc2_model): + mnx = {r.id: r.annotation.get("metanetx.reaction") for r in ecc2_model.reactions} + counts = {} + for v in mnx.values(): + counts[v] = counts.get(v, 0) + 1 + replace_ids(ecc2_model.reactions, "metanetx.reaction") + shared = 0 + for r in ecc2_model.reactions: + old_id = r.annotation.get("original ID", r.id) + value = mnx[old_id] + if value is None or counts[value] > 1: # shared annotations are not used + assert r.id == old_id + shared += value is not None + else: + assert r.id == value + assert shared > 0 # ECC2 has reactions that share a MetaNetX ID + + +def test_replace_ids_candidates_and_ambiguity(): + model = cobra.Model() # the DictList index is only updated for reactions that belong to a model + for rid, annotation in [("R1", "x;y"), ("R2", "z"), ("R3", "z2;R2"), ("R4", "")]: + r = cobra.Reaction(rid) + if annotation: + r.annotation["key"] = annotation + model.add_reactions([r]) + reactions = model.reactions + replace_ids(reactions, "key", unambiguous_only=True, candidates_separator=";") + assert [r.id for r in reactions] == ["R1", "z", "R3", "R4"] # R1 and R3 have two candidates + replace_ids(reactions, "key", candidates_separator=";") + assert [r.id for r in reactions] == ["x", "z", "z2", "R4"] + assert reactions.get_by_id("x").annotation["original ID"] == "R1" + + +# --- FluxVectorContainer ---------------------------------------------------------------------- + +def test_flux_vector_container_save_load(tmp_path): + fv_mat = numpy.array([[1.0, 0.0, -2.5], [0.0, 3.0, 1.0]]) + fvc = FluxVectorContainer(fv_mat, reac_id=["R1", "R2", "R3"], irreversible=numpy.array([1, 0]), + unbounded=numpy.array([0, 1])) + path = str(tmp_path / "modes.npz") + fvc.save(path) + loaded = FluxVectorContainer(path) + numpy.testing.assert_array_equal(loaded.fv_mat, fv_mat) + assert loaded.reac_id == ["R1", "R2", "R3"] + numpy.testing.assert_array_equal(loaded.irreversible, [1, 0]) + numpy.testing.assert_array_equal(loaded.unbounded, [0, 1]) + assert len(loaded) == 2 + assert loaded[0] == {"R1": 1.0, "R3": -2.5} + assert loaded.is_integer_vector_rounded(1, decimals=3) and not loaded.is_integer_vector_rounded(0, decimals=3) + loaded.clear() + assert loaded.reac_id == [] and loaded.fv_mat.shape == (0, 0) + + +def test_flux_vector_container_requires_reac_id(): + with pytest.raises(TypeError): + FluxVectorContainer(numpy.zeros((1, 2))) + + +def test_flux_vector_container_invalid_file(tmp_path, monkeypatch): + messages = [] + monkeypatch.setattr("cnapy.flux_vector_container.QMessageBox.critical", lambda *args: messages.append(args)) + path = tmp_path / "bad.npz" + path.write_text("not an npz file") + fvc = FluxVectorContainer(str(path)) + assert len(messages) == 1 + assert not hasattr(fvc, "fv_mat") diff --git a/cnapy/tests/test_core_units.py b/cnapy/tests/test_core_units.py new file mode 100644 index 00000000..770b7471 --- /dev/null +++ b/cnapy/tests/test_core_units.py @@ -0,0 +1,134 @@ +""" +Unit-level tests for the functions that back MainWindow.fba() / .fva(): + + cnapy.core_gui.model_optimization_with_exceptions + cnapy.core.multi_threaded_HiGHS_FVA + +These don't touch Qt widgets at all (beyond core_gui's QMessageBox import), +so they're the fastest, most robust layer of coverage - a good place to +catch a regression in the solve logic itself, independent of whether the +slot wiring around it is also correct (see test_mainwindow_slots.py for +that layer). +""" +import math + +import cobra +import pytest + +from cnapy.core_gui import model_optimization_with_exceptions +from cnapy.core import multi_threaded_HiGHS_FVA + + +def test_model_optimization_matches_cobrapy_optimize(ecc2_model): + """model_optimization_with_exceptions is (for the non-error path) + exactly model.optimize() - verify it doesn't alter that result.""" + reference = ecc2_model.copy().optimize() + result = model_optimization_with_exceptions(ecc2_model) + + assert result.status == "optimal" + assert result.status == reference.status + assert result.objective_value == pytest.approx(reference.objective_value, rel=1e-6) + + +def test_model_optimization_infeasible_scenario(ecc2_model): + """Forcing an infeasible scenario (uptake blocked but growth required) + should come back with status 'infeasible', not raise.""" + biomass = next(r for r in ecc2_model.reactions if r.objective_coefficient != 0) + biomass.lower_bound = 1.0 # force nonzero growth + for ex in ecc2_model.exchanges: + ex.lower_bound = 0.0 # block every uptake + + result = model_optimization_with_exceptions(ecc2_model) + assert result.status == "infeasible" + + +def _finite_bounds_model(model: cobra.Model) -> cobra.Model: + """Mirror what MainWindow.fva() does before calling multi_threaded_HiGHS_FVA: + replace +/-inf reaction bounds with cobra's configured defaults, since + the HiGHS-backed FVA path does not accept unbounded rows.""" + cfg = cobra.Configuration() + m = model.copy() + for r in m.reactions: + if r.lower_bound == -float("inf"): + r.lower_bound = cfg.lower_bound + if r.upper_bound == float("inf"): + r.upper_bound = cfg.upper_bound + return m + + +def test_fva_unconstrained_matches_cobrapy(ecc2_model, fva_reference): + """With no additional scenario constraints, multi_threaded_HiGHS_FVA's + per-reaction min/max should match cobrapy's own (structural, + fraction_of_optimum=0) FVA, precomputed in fva_reference (see + generate_fva_reference_data.py) rather than recomputed here.""" + model = _finite_bounds_model(ecc2_model) + + lb, ub, n_bad = multi_threaded_HiGHS_FVA(model, constraints=[]) + assert n_bad == 0 + + reference = fva_reference["unconstrained"] + for i, rxn in enumerate(model.reactions): + ref_lo, ref_hi = reference[rxn.id] + assert lb[i] == pytest.approx(ref_lo, abs=1e-5), f"lower bound mismatch for {rxn.id}" + assert ub[i] == pytest.approx(ref_hi, abs=1e-5), f"upper bound mismatch for {rxn.id}" + + +def test_fva_with_growth_constraint_matches_cobrapy(ecc2_model, fva_reference): + """With an explicit constraint fixing growth at its optimum (the + "growth-coupled" FVA a user gets by first fixing biomass in a + scenario), results should match the growth_coupled entries in + fva_reference (cobrapy's fraction_of_optimum=1.0 FVA, precomputed).""" + model = _finite_bounds_model(ecc2_model) + biomass_id = fva_reference["biomass_reaction_id"] + biomass = model.reactions.get_by_id(biomass_id) + + opt = model.copy().optimize().objective_value + assert opt == pytest.approx(fva_reference["biomass_objective_value"], rel=1e-6), ( + "live model's optimum no longer matches the value the cached " + "growth_coupled FVA reference was computed at - regenerate with " + "generate_fva_reference_data.py" + ) + constraints = [({biomass.id: 1.0}, "=", opt)] + + lb, ub, n_bad = multi_threaded_HiGHS_FVA(model, constraints=constraints) + assert n_bad == 0 + + reference = fva_reference["growth_coupled"] + for i, rxn in enumerate(model.reactions): + if rxn.id == biomass_id: + continue # pinned reaction; trivially equal to opt on both sides + ref_lo, ref_hi = reference[rxn.id] + assert lb[i] == pytest.approx(ref_lo, abs=1e-4), f"lower bound mismatch for {rxn.id}" + assert ub[i] == pytest.approx(ref_hi, abs=1e-4), f"upper bound mismatch for {rxn.id}" + + +def test_fva_infeasible_scenario_raises(ecc2_model): + """multi_threaded_HiGHS_FVA is documented (and relied on by + MainWindow.fva()) to raise cobra.exceptions.Infeasible rather than + return garbage bounds when the scenario itself has no feasible flux.""" + model = _finite_bounds_model(ecc2_model) + biomass = next(r for r in model.reactions if r.objective_coefficient != 0) + constraints = [({biomass.id: 1.0}, ">=", 1e6)] # unachievable growth + + with pytest.raises(cobra.exceptions.Infeasible): + multi_threaded_HiGHS_FVA(model, constraints=constraints) + + +def test_fva_bounds_are_internally_consistent(ecc2_model): + """Sanity property that should hold regardless of the reference + implementation: lower <= upper for every reaction, and both bounds lie + within the reaction's own (possibly scenario-tightened) bounds.""" + model = _finite_bounds_model(ecc2_model) + lb, ub, n_bad = multi_threaded_HiGHS_FVA(model, constraints=[]) + assert n_bad == 0 + + for i, rxn in enumerate(model.reactions): + assert not math.isnan(lb[i]) and not math.isnan(ub[i]) + # 1e-6, not 1e-7: matches the tolerance used for the two checks + # below, and blocked/near-zero-flux reactions can come back as e.g. + # -1.5e-07 instead of exactly 0.0 depending on the solver's own + # feasibility tolerance and platform-specific floating-point noise + # (observed on Linux but not Windows/macOS for this exact model). + assert lb[i] <= ub[i] + 1e-6 + assert lb[i] >= rxn.lower_bound - 1e-6 + assert ub[i] <= rxn.upper_bound + 1e-6 diff --git a/cnapy/tests/test_efm_mcs.py b/cnapy/tests/test_efm_mcs.py new file mode 100644 index 00000000..33d0ac8e --- /dev/null +++ b/cnapy/tests/test_efm_mcs.py @@ -0,0 +1,97 @@ +"""Tests for elementary flux modes (cnapy.core.efm_computation, runs efmtool via Java) and minimal cut sets +(optlang_enumerator's compute_mcs, called like the MCS dialog does) on a toy network whose EFMs and MCSs +are known. + +Toy network: R1: -> A R2: A -> B R3: A -> C R4: B <-> C R5: B -> R6: C -> + +EFMs (4): R1 R2 R5 | R1 R3 R6 | R1 R2 R4 R6 | R1 R3 -R4 R5 +MCSs for the target R6 >= 1: {R1} | {R6} | {R2, R3} | {R3, R4} + +run: pytest cnapy/tests/test_efm_mcs.py +""" +import cobra +import numpy +import pytest + +from cnapy.appdata import Scenario +from cnapy.core import efm_computation + +EXPECTED_EFMS = {(("R1", 1), ("R2", 1), ("R5", 1)), + (("R1", 1), ("R3", 1), ("R6", 1)), + (("R1", 1), ("R2", 1), ("R4", 1), ("R6", 1)), + (("R1", 1), ("R3", 1), ("R4", -1), ("R5", 1))} + + +def toy_model(r6_backwards=False): + model = cobra.Model("toy") + a, b, c = (cobra.Metabolite(m, compartment="c") for m in "ABC") + reactions = {"R1": ({a: 1}, 0), "R2": ({a: -1, b: 1}, 0), "R3": ({a: -1, c: 1}, 0), + "R4": ({b: -1, c: 1}, -1000), "R5": ({b: -1}, 0), "R6": ({c: -1}, 0)} + for rid, (stoichiometry, lb) in reactions.items(): + r = cobra.Reaction(rid, lower_bound=lb, upper_bound=1000) + r.add_metabolites(stoichiometry) + model.add_reactions([r]) + if r6_backwards: # written as the uptake of C, so it can only run backwards + model.reactions.R6.add_metabolites({c: 2}, combine=True) + model.reactions.R6.bounds = (-1000, 0) + return model + + +def efm_set(ems, flip=()): + """EFMs as sets of (reaction, sign) with the flux of R1 normalized to 1""" + result = set() + for row in numpy.asarray(ems.fv_mat): + r1 = row[ems.reac_id.index("R1")] + assert r1 > 0 + mode = [] + for rid, v in zip(ems.reac_id, row / r1): + if abs(v) > 1e-9: + assert v == pytest.approx(round(v)) # all EFMs of the toy network have unit fluxes + mode.append((rid, -int(round(v)) if rid in flip else int(round(v)))) + result.add(tuple(mode)) + return result + + +@pytest.fixture +def in_tmp_dir(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) # efmtool writes its temporary files into the current directory + + +def test_efms_of_toy_network(in_tmp_dir): + ems, scenario = efm_computation(toy_model(), Scenario(), False) + assert ems is not None, "efmtool failed, see the console output (is Java available?)" + assert scenario == {} + assert ems.reac_id == ["R1", "R2", "R3", "R4", "R5", "R6"] + assert len(ems) == 4 + assert efm_set(ems) == EXPECTED_EFMS + assert numpy.all(ems.irreversible) + + +def test_efms_with_reaction_knocked_out_by_scenario(in_tmp_dir): + scen = Scenario() + scen["R5"] = (0.0, 0.0) + scen["R2"] = (0.0, 5.0) # only (0, 0) is applied + ems, scenario = efm_computation(toy_model(), scen, True) + assert scenario == {"R5": (0, 0)} + assert "R5" not in ems.reac_id + assert efm_set(ems) == {m for m in EXPECTED_EFMS if ("R5", 1) not in m} + + +def test_efms_with_irreversible_backwards_reaction(in_tmp_dir): + ems, _ = efm_computation(toy_model(r6_backwards=True), Scenario(), False) + # same EFMs, but R6 carries a negative flux + assert efm_set(ems, flip={"R6"}) == EXPECTED_EFMS + + +def test_mcs_of_toy_network(): + from optlang_enumerator import mcs_computation + model = toy_model() + model.solver = "glpk" + reac_id = model.reactions.list_attr("id") + reac_id_symbols = mcs_computation.get_reac_id_symbols(reac_id) + targets = [mcs_computation.relations2leq_matrix( + mcs_computation.parse_relations([("R6", ">=", 1.0)], reac_id_symbols=reac_id_symbols), reac_id)] + mcs, err_val = mcs_computation.compute_mcs(model, targets=targets, max_mcs_size=3, timeout=60) + assert err_val == 0 + assert {frozenset(reac_id[i] for i in m) for m in mcs} == { + frozenset({"R1"}), frozenset({"R6"}), frozenset({"R2", "R3"}), frozenset({"R3", "R4"})} diff --git a/cnapy/tests/test_flux_space_ecc2.py b/cnapy/tests/test_flux_space_ecc2.py new file mode 100644 index 00000000..fd421a94 --- /dev/null +++ b/cnapy/tests/test_flux_space_ecc2.py @@ -0,0 +1,417 @@ +""" +Integration tests for CNApy's HiGHS-native flux/yield space plotting +(cnapy.lptools_highs) on the ECC2 fixture model, anaerobic. + +Why this layer matters: PlotSpaceDialog.compute() tries +plot_flux_space_highs first and silently falls back to straindesign's +plot_flux_space on *any* exception. A broken HiGHS path is therefore +invisible in the GUI -- plots still appear, just slowly. These tests call +plot_flux_space_data_highs directly, so an exception fails the test. + +What is checked, per axis combination (rate/yield, 2D/3D): + * extents match straindesign's plot_flux_space (cached reference, see + generate_flux_space_reference_data.py), plus area / hull volume for + the two exact cases (2D rate/rate polygon, 3D rate/rate/rate polytope) + * 3D meshes are closed, consistently and outwardly oriented; for the + slice path the enclosed volume also matches the integral of the slice + areas -- the check that exposed the folded stitching of slices whose + rate axes differ ~100x in scale (biomass vs ethanol) + * threaded results (n_workers=4 and the dialog's default "auto") match + the sequential ones + * no HiGHS solve runs on the calling thread (its scheduler stays + untouched, see "HiGHS thread scheduler handling" in highs_lp_cache.py), + and every worker-thread Highs() instance gets threads=1 / + output_flag=False / log_to_console=False *before* passModel() + * plotting still works after a default-threads HiGHS solve on the + calling thread (as FBA/FVA/cobra run them in the GUI), and leaves that + thread's scheduler unset for later solves (e.g. a multi-threaded MILP) + * plot_flux_space_highs renders every case headlessly + +Requires real cobra, straindesign (parsing only) and highspy. +""" +import gzip +import hashlib +import json +import math +import os +import threading as th +from collections import Counter +from pathlib import Path + +import numpy as np +import pytest + +from cnapy import lptools_highs as lh +from cnapy.lptools_highs import plot_flux_space_data_highs, orient_closed_mesh +from test_highs_lp_cache import default_threads_solve, in_fresh_thread + +DATA_DIR = Path(__file__).parent / "data" +ECC2_SBML = DATA_DIR / "ECC2_model.sbml.gz" +REFERENCE_JSON = DATA_DIR / "ecc2_flux_space_reference.json" + +CASE_NAMES = ["2d_rr", "2d_ry", "2d_yy", "3d_rrr", "3d_rry", "3d_yrr", "3d_ryy", "3d_yry"] +CASES_3D = [c for c in CASE_NAMES if c.startswith("3d")] +SLICE_CASES = ["3d_rry", "3d_yrr"] # exactly one yield axis -> per-slice polygons +GRID_CASES = ["3d_ryy", "3d_yry"] # two yield axes -> grid scan + +# Relative to each axis's range. Observed differences on ECC2 are <= 5e-8; +# the margin is for other straindesign solvers / HiGHS versions. +EXTENT_RTOL = 1e-5 + +REGENERATE = "Regenerate it with:\n python cnapy/tests/generate_flux_space_reference_data.py" + + +# --------------------------------------------------------------------------- +# fixtures +# --------------------------------------------------------------------------- + +def _fixture_sha256(path: Path) -> str: + # over the decompressed SBML, as in the generator (gzip headers carry a + # timestamp, so re-gzipping an unchanged model must not count as a change) + data = gzip.decompress(path.read_bytes()) if path.suffix == ".gz" else path.read_bytes() + return hashlib.sha256(data).hexdigest() + + +@pytest.fixture(scope="module") +def flux_space_reference() -> dict: + """straindesign's plot_flux_space results plus the case definitions, + cached in ecc2_flux_space_reference.json. Fails loudly (instead of + comparing against a stale reference) if the fixture model changed.""" + if not REFERENCE_JSON.exists(): + pytest.fail(f"{REFERENCE_JSON} does not exist. {REGENERATE}") + data = json.loads(REFERENCE_JSON.read_text()) + if data["source_sbml_sha256"] != _fixture_sha256(ECC2_SBML): + pytest.fail(f"{REFERENCE_JSON.name} was computed from a different version of " + f"{ECC2_SBML.name} (sha256 mismatch) - the cached reference is stale. {REGENERATE}") + missing = [c for c in CASE_NAMES if c not in data["cases"]] + if missing: + pytest.fail(f"{REFERENCE_JSON.name} has no entry for {missing}. {REGENERATE}") + return data + + +@pytest.fixture(scope="module") +def flux_model(ecc2_reference_model): + """One private copy for this module: the functions under test don't + mutate the model (anaerobiosis is passed as a constraint string), but + the session-wide reference model must never be at risk.""" + return ecc2_reference_model.copy() + + +@pytest.fixture(scope="module") +def compute(flux_model, flux_space_reference): + """compute(case, n_workers=1) -> plot_flux_space_data_highs result, + memoized per (case, n_workers) for the whole module.""" + cache = {} + + def _compute(case, n_workers=1): + key = (case, n_workers) + if key not in cache: + cache[key] = plot_flux_space_data_highs( + flux_model, _dialog_axes(flux_space_reference["cases"][case]["axes"]), + points=flux_space_reference["points"], + constraints=flux_space_reference["constraints"], + n_workers=n_workers) + return cache[key] + return _compute + + +def _dialog_axes(axes): + # JSON stores yield axes as [num, den] lists; the dialog passes tuples + return [ax if isinstance(ax, str) else tuple(ax) for ax in axes] + + +# --------------------------------------------------------------------------- +# helpers +# --------------------------------------------------------------------------- + +def _bbox(points): + p = np.asarray(points, dtype=float) + return np.stack([p.min(axis=0), p.max(axis=0)], axis=1) + + +def _polygon_area(points): + p = np.asarray(points, dtype=float)[:, :2] + if len(p) > 1 and np.allclose(p[0], p[-1]): + p = p[:-1] + x, y = p[:, 0], p[:, 1] + return 0.5 * abs(np.dot(x, np.roll(y, -1)) - np.dot(y, np.roll(x, -1))) + + +def _signed_volume(datapoints, triang): + p = np.asarray(datapoints, dtype=float) + t = np.asarray(triang) + a, b, c = p[t[:, 0]], p[t[:, 1]], p[t[:, 2]] + return float(np.einsum("ij,ij->i", a, np.cross(b, c)).sum() / 6.0) + + +def _edge_stats(triang): + undirected = Counter(frozenset(e) for t in triang for e in ((t[0], t[1]), (t[1], t[2]), (t[2], t[0]))) + directed = Counter(e for t in triang for e in ((t[0], t[1]), (t[1], t[2]), (t[2], t[0]))) + return undirected, sum(1 for v in directed.values() if v > 1) + + +def _slices(d): + """[(yield value, ring area)] of a slice-path result, in slice order.""" + p = np.asarray(d["datapoints"], dtype=float) + yi = d["ax_type"].index("yield") + ri = [i for i in range(3) if i != yi] + return [(p[ring[0], yi], _polygon_area(p[ring][:, ri])) for ring in d["slice_outlines"]] + + +# --------------------------------------------------------------------------- +# 1. agreement with straindesign +# --------------------------------------------------------------------------- + +def _assert_extents_match(case, d, flux_space_reference): + ref = flux_space_reference["cases"][case] + assert d["datapoints"], f"{case}: no datapoints" + ours, theirs = _bbox(d["datapoints"]), np.asarray(ref["bbox"], dtype=float) + assert ours.shape == theirs.shape + scale = np.maximum(np.abs(theirs[:, 1] - theirs[:, 0]), 1e-9)[:, None] + worst = float(np.max(np.abs(ours - theirs) / scale)) + assert worst <= EXTENT_RTOL, ( + f"{case}: extents differ from straindesign's by up to {worst:.2e} of the axis range\n" + f" HiGHS: {ours.tolist()}\n straindesign: {theirs.tolist()}") + + +@pytest.mark.parametrize("case", CASE_NAMES) +def test_extents_match_straindesign(case, compute, flux_space_reference): + _assert_extents_match(case, compute(case), flux_space_reference) + + +def test_rate_rate_polygon_area_matches_straindesign(compute, flux_space_reference): + ref = flux_space_reference["cases"]["2d_rr"]["area"] + assert _polygon_area(compute("2d_rr")["datapoints"]) == pytest.approx(ref, rel=1e-6) + + +def test_rate_polytope_volume_matches_straindesign(compute, flux_space_reference): + from scipy.spatial import ConvexHull + ref = flux_space_reference["cases"]["3d_rrr"]["hull_volume"] + vol = ConvexHull(np.asarray(compute("3d_rrr")["datapoints"], dtype=float)).volume + assert vol == pytest.approx(ref, rel=1e-6) + + +# --------------------------------------------------------------------------- +# 2. 3D mesh invariants on real output +# --------------------------------------------------------------------------- + +@pytest.mark.parametrize("case", CASES_3D) +def test_mesh_is_closed_and_outward(case, compute): + d = compute(case) + assert d["degen"] == "full" + triang, closed = orient_closed_mesh(d["datapoints"], d["triang"]) + assert closed, f"{case}: mesh is not a closed surface (rendering would fall back to no culling)" + undirected, conflicts = _edge_stats(triang) + assert set(undirected.values()) == {2} + assert conflicts == 0 + assert _signed_volume(d["datapoints"], triang) > 0 + + +@pytest.mark.parametrize("case", SLICE_CASES) +def test_slice_mesh_needs_no_reorientation_and_has_no_folds(case, compute): + """build_slice_mesh must produce the closed, outward mesh by itself + (in either axis order) -- and its volume must match the trapezoid + integral of the slice areas. A fold through the interior keeps the + surface closed but breaks this volume balance.""" + d = compute(case) + undirected, conflicts = _edge_stats(d["triang"]) + assert set(undirected.values()) == {2}, f"{case}: open or non-manifold edges" + assert conflicts == 0, f"{case}: inconsistent winding straight out of build_slice_mesh" + volume = _signed_volume(d["datapoints"], d["triang"]) + assert volume > 0, f"{case}: mesh faces inward" + s = _slices(d) + integral = sum((s[k + 1][0] - s[k][0]) * (s[k][1] + s[k + 1][1]) / 2 for k in range(len(s) - 1)) + assert volume == pytest.approx(integral, rel=0.01) + + +# --------------------------------------------------------------------------- +# 3. threading must not change the result +# --------------------------------------------------------------------------- + +@pytest.mark.parametrize("n_workers", [4, "auto"]) +@pytest.mark.parametrize("case", GRID_CASES) +def test_grid_threaded_matches_sequential(case, n_workers, compute): + """Same grid samples in the same order; the surface compared by + triangle count, closedness and enclosed volume, not by triangle + indices: the strip triangulation chooses each cell's diagonal by + comparing two distances that are often exactly equal on a regular + grid, so ~1e-11 noise from a different warm-start basis can flip a + diagonal without changing the surface.""" + seq, par = compute(case, 1), compute(case, n_workers) + np.testing.assert_allclose(np.asarray(par["datapoints"], dtype=float), + np.asarray(seq["datapoints"], dtype=float), rtol=0, atol=1e-8) + assert len(par["triang"]) == len(seq["triang"]) + (t_par, closed_par), (t_seq, closed_seq) = (orient_closed_mesh(d["datapoints"], d["triang"]) + for d in (par, seq)) + assert closed_par and closed_seq + assert _signed_volume(par["datapoints"], t_par) == pytest.approx( + _signed_volume(seq["datapoints"], t_seq), rel=1e-6) + + +@pytest.mark.parametrize("n_workers", [4, "auto"]) +@pytest.mark.parametrize("case", SLICE_CASES) +def test_slices_threaded_match_sequential(case, n_workers, compute): + """Compared per slice by yield value and area, not vertex lists: which + optimal vertex HiGHS returns on a tied face depends on the warm-start + basis, so a slice can gain or lose a collinear point on an edge while + being exactly the same polygon.""" + seq, par = _slices(compute(case, 1)), _slices(compute(case, n_workers)) + assert len(par) == len(seq) + for (y_par, a_par), (y_seq, a_seq) in zip(par, seq): + assert y_par == pytest.approx(y_seq, abs=1e-12) + assert a_par == pytest.approx(a_seq, rel=1e-9, abs=1e-12) + + +# --------------------------------------------------------------------------- +# 4. HiGHS threads: calling thread untouched, workers threads=1 and silent +# --------------------------------------------------------------------------- + +def test_every_highs_instance_configured_before_pass_model(flux_model, flux_space_reference, monkeypatch): + """No solver may run on the calling thread (a threads=1 solve there + would fix its HiGHS scheduler at one thread). Worker-thread solvers use + threads=1 (the default 0 oversubscribes badly under N worker threads), + and the logging options have to be set before passModel(). Records the + options at the moment of every passModel() call across a sequential and + a threaded run of the 2D, the slice and the grid path.""" + import highspy + caller = th.current_thread() + seen = [] + original = highspy.Highs.passModel + + def option(h, name): + value = h.getOptionValue(name) + return value[1] if isinstance(value, tuple) else value # (status, value) in newer highspy + + def recording_pass_model(self, *args, **kwargs): + seen.append((id(self), th.current_thread() is caller, option(self, "threads"), + option(self, "output_flag"), option(self, "log_to_console"))) + return original(self, *args, **kwargs) + + monkeypatch.setattr(highspy.Highs, "passModel", recording_pass_model) + ref = flux_space_reference + for case in ("2d_ry", "3d_rry", "3d_ryy"): + for n_workers in (1, 4): + plot_flux_space_data_highs(flux_model, _dialog_axes(ref["cases"][case]["axes"]), + points=5, constraints=ref["constraints"], n_workers=n_workers) + + assert seen, "passModel was never called -- did highspy.Highs move?" + assert len({s[0] for s in seen}) > 6, "expected per-thread solver instances in the threaded runs" + on_caller = [s for s in seen if s[1]] + assert not on_caller, f"{len(on_caller)} of {len(seen)} Highs instances built on the calling thread" + bad = [s for s in seen if s[2:] != (1, False, False)] + assert not bad, f"{len(bad)} of {len(seen)} Highs instances not configured before passModel: {bad[:3]}" + + +@pytest.mark.parametrize("n_workers", [1, 4]) +@pytest.mark.parametrize("case", ["2d_ry", "3d_rry"]) +def test_plot_after_default_threads_solve_on_same_thread(case, n_workers, flux_model, flux_space_reference): + """In the GUI, FBA/FVA/cobra solve with default threads on the GUI + thread, which creates its HiGHS scheduler with the default size. The + plot afterwards must not fail (it used to build threads=1 solvers + there, whose runs then returned kError).""" + ref = flux_space_reference + + def scenario(): + default_threads_solve() + return plot_flux_space_data_highs( + flux_model, _dialog_axes(ref["cases"][case]["axes"]), points=ref["points"], + constraints=ref["constraints"], n_workers=n_workers) + _assert_extents_match(case, in_fresh_thread(scenario), ref) + + +@pytest.mark.skipif(not os.path.isdir("/proc/self/task") or (os.cpu_count() or 1) < 4, + reason="counts OS threads via /proc; needs >= 4 cores for a multi-threaded default scheduler") +def test_plot_leaves_caller_scheduler_untouched(flux_model, flux_space_reference): + """After a plot, the first default-threads solve on the calling thread + must still create a multi-threaded scheduler (HiGHS starts its worker + threads then). If the plot had already created a one-thread scheduler + there, later solves on that thread (e.g. a MILP) would stay single-threaded.""" + ref = flux_space_reference + + def scenario(): + plot_flux_space_data_highs(flux_model, _dialog_axes(ref["cases"]["3d_rry"]["axes"]), + points=5, constraints=ref["constraints"], n_workers=1) + before = len(os.listdir("/proc/self/task")) + default_threads_solve() + return before, len(os.listdir("/proc/self/task")) + before, after = in_fresh_thread(scenario) + assert after > before, f"no HiGHS worker threads started ({before} -> {after} OS threads)" + + +def test_auto_n_workers_bounds(): + assert lh.auto_n_workers(0, 1) == 1 + assert lh.auto_n_workers(1, 1) == 1 + assert lh.auto_n_workers(100, 1, max_workers=4) <= 4 + assert lh.auto_n_workers(100, 1) <= lh._available_cpus() + assert lh.auto_n_workers(10, 5, max_workers=64) <= 2 + + +# --------------------------------------------------------------------------- +# 5. rendering smoke test on real data +# --------------------------------------------------------------------------- + +@pytest.fixture +def agg_pyplot(): + """pyplot on Agg for one test, previous backend restored afterwards.""" + import matplotlib + import matplotlib.pyplot as plt + previous = matplotlib.get_backend() + plt.switch_backend("Agg") + yield plt + plt.close("all") + try: + plt.switch_backend(previous) + except Exception: + pass + + +@pytest.mark.parametrize("case", CASE_NAMES) +def test_plot_flux_space_highs_renders(case, flux_model, flux_space_reference, agg_pyplot): + ref = flux_space_reference + datapoints, _, artist = lh.plot_flux_space_highs( + flux_model, _dialog_axes(ref["cases"][case]["axes"]), points=ref["points"], + constraints=ref["constraints"], show=False) + assert datapoints + artist.axes.figure.canvas.draw() # actually rasterize, incl. the culled 3D collections + if case.startswith("3d"): + from mpl_toolkits.mplot3d.art3d import Poly3DCollection + meshes = [c for c in artist.axes.collections if isinstance(c, Poly3DCollection)] + assert meshes, f"{case}: no 3D surface drawn" + if case in SLICE_CASES + GRID_CASES: + # closed triangle meshes are drawn with back-face culling: some, not + # all, triangles visible (3d_rrr is drawn from its hull's face + # polygons instead, without culling) + culled = [c for c in meshes if hasattr(c, "_all_verts")] + assert culled, f"{case}: closed mesh not drawn with the culled collection" + assert 0 < len(culled[0].get_paths()) < len(culled[0]._all_verts) + + +@pytest.mark.parametrize("case", GRID_CASES) +def test_flat_faces_are_uniformly_coloured(case, flux_model, flux_space_reference, agg_pyplot): + """The 2-yield meshes have a large flat bottom (yield axis at 0). + Its normals are (0, 0, -1) up to ~1e-17 noise; a face colour built + on atan2(n_y, n_x) turned that noise into random hues (a speckled + face). All triangles of one flat face must get the same colour.""" + ref = flux_space_reference + _, _, coll = lh.plot_flux_space_highs( + flux_model, _dialog_axes(ref["cases"][case]["axes"]), points=ref["points"], + constraints=ref["constraints"], show=False) + verts = np.asarray(coll._all_verts, dtype=float) # (n, 3, 3), culled collection + colours = np.asarray(coll._all_fc, dtype=float) + span = np.ptp(verts.reshape(-1, 3), axis=0) + n = np.cross(verts[:, 1] - verts[:, 0], verts[:, 2] - verts[:, 0]) / np.maximum(span, 1e-12)[[1, 2, 0]] \ + / np.maximum(span, 1e-12)[[2, 0, 1]] # normals in axis-normalized coordinates + length = np.linalg.norm(n, axis=1) + n = n[length > 0] / length[length > 0, None] + colours = colours[length > 0] + checked = 0 + for k in range(3): + for sign in (-1, 1): + flat = sign * n[:, k] > 1 - 1e-9 + if flat.sum() >= 10: + checked += 1 + spread = np.ptp(colours[flat, :3], axis=0).max() + assert spread < 1e-6, (f"{case}: {flat.sum()} triangles of the flat face normal to axis {k} " + f"({'+' if sign > 0 else '-'}) span colours by {spread:.3f}") + assert checked, f"{case}: expected at least one large flat face" + diff --git a/cnapy/tests/test_highs_lp_cache.py b/cnapy/tests/test_highs_lp_cache.py new file mode 100644 index 00000000..a07373ef --- /dev/null +++ b/cnapy/tests/test_highs_lp_cache.py @@ -0,0 +1,280 @@ +"""Correctness tests for highs_lp_cache.py, run against the real highspy package. + +Toy network (steady state, S * v = 0): + + EX_A --r0--> A --r1--> B --r2--> C --r3--> EX_C + (r0 bounds (-10, 0): negative = import of A) + (r1, r2, r3 bounds (0, 1000)) + +Mass balance forces v0 = -v1 = -v2 = -v3 at every feasible point (not +just the optimum), which makes hand-verifiable reference values easy to +write down for both the rate and yield tests. + +Run: pytest tests/test_highs_lp_cache.py +""" + +import threading as th +import unittest + +import numpy as np +import scipy.sparse as sp + + +from cnapy import highs_lp_cache as hlc + + +REACTION_IDS = ["r0", "r1", "r2", "r3"] +RXN_INDEX = {rid: i for i, rid in enumerate(REACTION_IDS)} +COL_LOWER = [-10.0, 0.0, 0.0, 0.0] +COL_UPPER = [0.0, 1000.0, 1000.0, 1000.0] + + +def toy_S(): + # rows: A, B, C ; cols: r0, r1, r2, r3 + S = sp.lil_matrix((3, 4)) + S[0, 0] = -1 # A: -r0 + S[0, 1] = -1 # A: -r1 + S[1, 1] = 1 # B: +r1 + S[1, 2] = -1 # B: -r2 + S[2, 2] = 1 # C: +r2 + S[2, 3] = -1 # C: -r3 + return S + + +class TestRateSolver(unittest.TestCase): + def _fresh_solver(self, num_fix_slots=0): + lp, fix_slot_rows = hlc._build_rate_lp_arrays( + toy_S(), COL_LOWER, COL_UPPER, num_fix_slots=num_fix_slots) + return hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_slot_rows) + + def test_max_output_matches_hand_solution(self): + solver = self._fresh_solver() + status, value, fluxes = solver.optimize({"r3": 1.0}, "maximize") + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 10.0, places=6) + self.assertAlmostEqual(fluxes["r0"], -10.0, places=6) + self.assertAlmostEqual(fluxes["r3"], 10.0, places=6) + + def test_min_matches_hand_solution(self): + solver = self._fresh_solver() + status, value, fluxes = solver.optimize({"r3": 1.0}, "minimize") + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 0.0, places=6) + + def test_fix_row_matches_manual_rebuild(self): + # build-once + set_fix_row(rhs varying) vs. a fresh solver built + # with the fix baked in as an ordinary constraint every time -- + # results must agree for every rhs. + solver = self._fresh_solver(num_fix_slots=1) + for rhs in (-1.0, -3.5, -7.0, -10.0, 0.0): + solver.set_fix_row(0, {"r0": 1.0}, rhs) + status, value, fluxes = solver.optimize({"r3": 1.0}, "maximize") + + lp_ref, fix_rows_ref = hlc._build_rate_lp_arrays( + toy_S(), COL_LOWER, COL_UPPER, + extra_rows=[({0: 1.0}, rhs, rhs)]) + ref_solver = hlc.HighsRateSolver(lp_ref, REACTION_IDS, RXN_INDEX, fix_rows_ref) + ref_status, ref_value, ref_fluxes = ref_solver.optimize({"r3": 1.0}, "maximize") + + self.assertEqual(status, ref_status) + self.assertAlmostEqual(value, ref_value, places=6, msg=f"mismatch at rhs={rhs}") + self.assertAlmostEqual(value, -rhs, places=6) # r3 == -r0 at every feasible point + + def test_fix_row_retargeted_to_different_expression(self): + # exercise the support-diffing path in set_fix_row: point the same + # slot at r0 first, then re-point it at r1 -- the old r0 coefficient + # must actually get cleared (changeCoeff back to 0), not just + # "shadowed" by an unrelated new nonzero somewhere. + solver = self._fresh_solver(num_fix_slots=1) + solver.set_fix_row(0, {"r0": 1.0}, -4.0) + solver.optimize({"r3": 1.0}, "maximize") + solver.set_fix_row(0, {"r1": 1.0}, 6.0) # forget r0 entirely, pin r1 instead + status, value, fluxes = solver.optimize({"r3": 1.0}, "maximize") + self.assertEqual(status, "optimal") + self.assertAlmostEqual(fluxes["r1"], 6.0, places=6) + self.assertAlmostEqual(value, 6.0, places=6) + # if the old r0==-4 coefficient had leaked, r0 would be pinned to + # -4 too and the max would still work out to 4, not 6 -- so this + # also indirectly checks r0 is free again: + self.assertAlmostEqual(fluxes["r0"], -6.0, places=6) + + def test_clear_fix_row_restores_unconstrained_optimum(self): + solver = self._fresh_solver(num_fix_slots=1) + solver.set_fix_row(0, {"r0": 1.0}, -2.0) + _, capped_value, _ = solver.optimize({"r3": 1.0}, "maximize") + self.assertAlmostEqual(capped_value, 2.0, places=6) + solver.clear_fix_row(0) + _, free_value, _ = solver.optimize({"r3": 1.0}, "maximize") + self.assertAlmostEqual(free_value, 10.0, places=6) + + +class TestYieldSolver(unittest.TestCase): + def _fresh_solver(self, den_weight=1.0, num_fix_slots=0): + lp, reaction_ids, rxn_index, den_row, t_col, fix_slot_rows = self._build(den_weight, num_fix_slots) + return hlc.HighsYieldSolver(lp, reaction_ids, rxn_index, den_row, t_col, fix_slot_rows) + + def _build(self, den_weight, num_fix_slots): + den_coeffs = {RXN_INDEX["r0"]: -den_weight} # den = -den_weight * r0 = den_weight * (uptake) + lp, den_row, t_col, fix_slot_rows = hlc._build_yopt_lp_arrays( + toy_S(), COL_LOWER, COL_UPPER, den_coeffs, num_fix_slots=num_fix_slots) + return lp, REACTION_IDS, RXN_INDEX, den_row, t_col, fix_slot_rows + + def test_constant_ratio_yield(self): + # r3 == -r0 identically (mass balance), so yield r3 / (w * (-r0)) + # is the constant 1/w everywhere the denominator is achievable. + for w in (1.0, 2.0, 0.5): + solver = self._fresh_solver(den_weight=w) + status, value, fluxes, scalable = solver.optimize({"r3": 1.0}, "maximize", denominator_sign=1.0) + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 1.0 / w, places=6, msg=f"w={w}") + + def test_fix_row_in_yield_space_matches_rate_axis(self): + solver = self._fresh_solver(den_weight=1.0, num_fix_slots=1) + for target in (1.0, 3.0, 7.5, 10.0): + solver.set_fix_row(0, {"r1": 1.0}, target) + status, value, fluxes, scalable = solver.optimize( + {"r3": 1.0}, "maximize", denominator_sign=1.0) + self.assertEqual(status, "optimal") + self.assertAlmostEqual(fluxes["r1"], target, places=6) + # r1 == r3 identically, and yield value is still 1.0 (den_weight=1) + self.assertAlmostEqual(value, 1.0, places=6) + self.assertAlmostEqual(fluxes["r3"], target, places=6) + + def test_retargeting_fix_row_clears_t_coefficient(self): + solver = self._fresh_solver(den_weight=1.0, num_fix_slots=1) + solver.set_fix_row(0, {"r1": 1.0}, 4.0) + solver.optimize({"r3": 1.0}, "maximize", denominator_sign=1.0) + solver.clear_fix_row(0) + status, value, fluxes, scalable = solver.optimize( + {"r3": 1.0}, "maximize", denominator_sign=1.0) + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 1.0, places=6) # back to the unconstrained (constant) yield + + + def test_yield_matches_hand_derivation_on_2d_case(self): + # toy: X, Y in [0,10], X+Y<=12; yield = Y/(X+Y). + # Analytically (not the toy_S chain's degenerate constant-ratio + # case): d/dX[Y/(X+Y)] = -Y/(X+Y)^2 < 0 for Y>0, so the ratio is + # maximized by minimizing X -> X=0 gives Y/(0+Y)=1 for any Y>0 + # (e.g. X=0,Y=10 is feasible) => max=1. Minimized by Y->0 with + # X>0 (e.g. X=5,Y=0 is feasible, den=5>0) => ratio=0/5=0 => min=0. + # Denominator X+Y is >=0 everywhere here (X,Y>=0), so only + # sign=+1 is achievable. + reaction_ids = ["X", "Y"] + rxn_index = {"X": 0, "Y": 1} + S = sp.lil_matrix((0, 2)) + extra_rows = [({0: 1.0, 1: 1.0}, '<=', 12.0)] # X + Y <= 12 (yield builder's ctype-string format) + lp, den_row, t_col, fix_rows = hlc._build_yopt_lp_arrays( + S, [0.0, 0.0], [10.0, 10.0], den_coeffs={0: 1.0, 1: 1.0}, extra_rows=extra_rows) + solver = hlc.HighsYieldSolver(lp, reaction_ids, rxn_index, den_row, t_col, fix_rows) + + status, value, fluxes, scalable = solver.optimize({"Y": 1.0}, "maximize", denominator_sign=1.0) + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 1.0, places=6) + + status, value, fluxes, scalable = solver.optimize({"Y": 1.0}, "minimize", denominator_sign=1.0) + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 0.0, places=6) + + +class TestThreadIsolation(unittest.TestCase): + def test_two_threads_share_lp_without_cross_contamination(self): + lp, fix_slot_rows = hlc._build_rate_lp_arrays( + toy_S(), COL_LOWER, COL_UPPER, num_fix_slots=1) + + results = {} + + def worker(name, rhs_values): + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_slot_rows) + out = [] + for rhs in rhs_values: + solver.set_fix_row(0, {"r0": 1.0}, rhs) + _, value, _ = solver.optimize({"r3": 1.0}, "maximize") + out.append((rhs, value)) + results[name] = out + + t1 = th.Thread(target=worker, args=("A", [-1.0, -4.0, -9.0])) + t2 = th.Thread(target=worker, args=("B", [-2.0, -6.0, -10.0])) + t1.start(); t2.start() + t1.join(); t2.join() + + for rhs, value in results["A"]: + self.assertAlmostEqual(value, -rhs, places=6) + for rhs, value in results["B"]: + self.assertAlmostEqual(value, -rhs, places=6) + + +def in_fresh_thread(fn): + """fn() on a new plain thread, i.e. with a HiGHS scheduler that nothing + has created yet; re-raises fn's exception.""" + out = {} + + def run(): + try: + out["value"] = fn() + except BaseException as e: + out["error"] = e + + t = th.Thread(target=run) + t.start() + t.join() + if "error" in out: + raise out["error"] + return out.get("value") + + +def default_threads_solve(): + """A HiGHS solve with default options, as cobra/optlang and cnapy.core + run them: creates the calling thread's scheduler with the default size.""" + import highspy + h = highspy.Highs() + h.setOptionValue("output_flag", False) + h.addVar(0.0, 1.0) + h.changeColCost(0, -1.0) + assert h.run() == highspy.HighsStatus.kOk + + +def threads_option(h): + value = h.getOptionValue("threads") + return value[1] if isinstance(value, tuple) else value # (status, value) in newer highspy + + +class TestThreadScheduler(unittest.TestCase): + """HiGHS has one scheduler per OS thread, sized by the first run() on + it; a later run with a different explicit threads value fails (see + "HiGHS thread scheduler handling" in highs_lp_cache.py).""" + + def _solver(self): + lp, fix_slot_rows = hlc._build_rate_lp_arrays(toy_S(), COL_LOWER, COL_UPPER) + return hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_slot_rows) + + def test_solver_works_after_default_threads_solve_on_same_thread(self): + def scenario(): + default_threads_solve() + return self._solver().optimize({"r3": 1.0}, "maximize") + status, value, _ = in_fresh_thread(scenario) + self.assertEqual(status, "optimal") + self.assertAlmostEqual(value, 10.0, places=6) + + def test_threads_pinned_to_one_only_on_worker_pool_threads(self): + self.assertEqual(threads_option(self._solver().h), 0) + with hlc.worker_pool(2) as ex: + in_pool = list(ex.map(lambda _: threads_option(self._solver().h), range(4))) + self.assertEqual(in_pool, [1, 1, 1, 1]) + + def test_failed_run_raises_instead_of_returning_stale_solution(self): + import os + + def scenario(): + solver = self._solver() + status, _, _ = solver.optimize({"r3": 1.0}, "maximize") + self.assertEqual(status, "optimal") + # can't match the scheduler the first run created (half the cores) + solver.h.setOptionValue("threads", os.cpu_count() + 1) + with self.assertRaises(RuntimeError): + solver.optimize({"r3": 1.0}, "maximize") + in_fresh_thread(scenario) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_lptools_highs.py b/cnapy/tests/test_lptools_highs.py new file mode 100644 index 00000000..b8599f52 --- /dev/null +++ b/cnapy/tests/test_lptools_highs.py @@ -0,0 +1,257 @@ +"""Data-layer tests for lptools_highs.plot_flux_space_data_highs on small +hand-built cobra models (real cobra, straindesign parsing, highspy), +checked against shapes whose area/volume is known exactly. See +test_flux_space_ecc2.py for the same entry point on the ECC2 model.""" +import math +import unittest + +import cobra + +from cnapy import plot_trace_highs as pth +from cnapy import lptools_highs as lh + + +def FakeModel(reaction_bounds): + """A real cobra.Model with one metabolite-free reaction per + (id, lb, ub): no mass-balance rows, so the feasible set is exactly + the bounds box cut by whatever `constraints` a test passes -- the + same toy LPs the hand-derived reference shapes (pentagon, cut cube) + are worked out for. Named after the stand-in it replaces, so the + test bodies read unchanged.""" + model = cobra.Model("toy") + reactions = [] + for rid, lb, ub in reaction_bounds: + r = cobra.Reaction(rid) + r.bounds = (lb, ub) + reactions.append(r) + model.add_reactions(reactions) + return model + + +class Test2D(unittest.TestCase): + def test_full_rate_rate_pentagon(self): + # X, Y in [0,10], X+Y<=12 -- same pentagon as test_plot_trace_highs.py + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs(model, ["X", "Y"], constraints="X + Y <= 12") + self.assertEqual(data["ax_type"], ["rate", "rate"]) + # NOT "full" -- see the note in lptools_highs._fill_2d: a fully + # non-degenerate 2D case is labelled "plane" by _detect_degeneracy + # (inherited from lptools.py itself), harmlessly, since neither + # dispatch branch checks for "full" explicitly. + self.assertEqual(data["degen"], "plane") + self.assertEqual(data["val_limits"], [[0.0, 10.0], [0.0, 10.0]]) + area = pth.polygon_area(data["datapoints"]) + self.assertAlmostEqual(area, 68.0, places=3) + + def test_degenerate_point(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs(model, ["X", "Y"], constraints="X = 3, Y = 4") + self.assertEqual(data["degen"], "point") + self.assertEqual(data["datapoints"], [[3.0, 4.0]]) + self.assertEqual(data["triang"], []) + + def test_degenerate_line(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs(model, ["X", "Y"], constraints="X = 3") + self.assertEqual(data["degen"], "line") + self.assertEqual(data["degen_axes"], [True, False]) + pts = data["datapoints"] + self.assertEqual([p[0] for p in pts], [3.0, 3.0]) + self.assertEqual(sorted(p[1] for p in pts), [0.0, 10.0]) + + def test_full_mixed_rate_yield_kinked_boundary(self): + # same setup as test_trace_boundary_adaptive.py's kinked case: + # X,P in [0,10], S in [1,10], X+P<=12; x=X (rate), y=P/S (yield) + model = FakeModel([("X", 0.0, 10.0), ("P", 0.0, 10.0), ("S", 1.0, 10.0)]) + data = lh.plot_flux_space_data_highs( + model, ["X", ["P", "S"]], constraints="X + P <= 12") + self.assertEqual(data["ax_type"], ["rate", "yield"]) + self.assertEqual(data["degen"], "plane") # see note above -- expected for 2D + self.assertTrue(data["is_approximate"]) + for x, y in data["datapoints"]: + expected = min(10.0, 12.0 - x) if x <= 10.0 else 0.0 + # datapoints alternate upper-then-reversed-lower; only check + # points that came from the upper boundary (y > 0 region is + # ambiguous at the shared endpoints, so just check the + # formula holds for every point that matches the upper curve) + if abs(y - expected) > 1e-3 and abs(y - 0.0) > 1e-3: + self.fail(f"point (x={x}, y={y}) matches neither upper ({expected}) nor lower (0) curve") + + +class Test3D(unittest.TestCase): + def test_full_0yield_cut_cube(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs( + model, ["X", "Y", "Z"], constraints="X + Y + Z <= 25") + self.assertEqual(data["ax_type"], ["rate", "rate", "rate"]) + self.assertEqual(data["degen"], "full") + self.assertEqual(len(data["datapoints"]), 10) + self.assertIsNotNone(data["face_polys"]) + self.assertEqual(len(data["face_polys"]), 7) + self.assertNotIn("is_approximate", data) # exact, unlike the yield cases + + def test_degenerate_point_3d(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs( + model, ["X", "Y", "Z"], constraints="X = 1, Y = 2, Z = 3") + self.assertEqual(data["degen"], "point") + self.assertEqual(data["datapoints"], [[1.0, 2.0, 3.0]]) + + def test_degenerate_line_3d(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs( + model, ["X", "Y", "Z"], constraints="X = 1, Y = 2") + self.assertEqual(data["degen"], "line") + self.assertEqual(len(data["datapoints"]), 2) + for p in data["datapoints"]: + self.assertAlmostEqual(p[0], 1.0) + self.assertAlmostEqual(p[1], 2.0) + self.assertEqual(sorted(p[2] for p in data["datapoints"]), [0.0, 10.0]) + + def test_degenerate_plane_3d_matches_2d_pentagon(self): + # X,Y in [0,10], X+Y<=12 (the pentagon again), Z pinned to 3 -> + # plane case should reduce to exactly the same pentagon, offset + # into the Z=3 plane. + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + data = lh.plot_flux_space_data_highs( + model, ["X", "Y", "Z"], constraints="X + Y <= 12, Z = 3") + self.assertEqual(data["degen"], "plane") + for p in data["datapoints"]: + self.assertAlmostEqual(p[2], 3.0, places=6) + pts_2d = [(p[0], p[1]) for p in data["datapoints"]] + area = pth.polygon_area(pts_2d) + self.assertAlmostEqual(area, 68.0, places=3) + + def test_degenerate_axis_below_one_detected(self): + # Rounding a fixed |v| < 1 inward to 8 decimals inverts its limits by + # 1e-8 (0.84743374 > 0.84743373), and the degeneracy check then + # depended on float noise: this value used to be classified "full", + # so the all-rate tracer returned 3 points and no triangles (empty plot). + v = 0.8474337369372327 + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", v, v)]) + data = lh.plot_flux_space_data_highs(model, ["X", "Y", "Z"], constraints="X + Y <= 12") + self.assertEqual(data["degen"], "plane") + self.assertEqual(data["degen_axes"], [False, False, True]) + self.assertEqual(data["val_limits"][2], [v, v]) + for p in data["datapoints"]: + self.assertAlmostEqual(p[2], v, places=9) + self.assertAlmostEqual(pth.polygon_area([(p[0], p[1]) for p in data["datapoints"]]), + 68.0, places=3) + # same for a fixed yield axis and in 2D + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", v, v), ("S", 1.0, 1.0)]) + data = lh.plot_flux_space_data_highs(model, ["X", "Y", ["Z", "S"]], constraints="X + Y <= 12") + self.assertEqual(data["degen"], "plane") + data = lh.plot_flux_space_data_highs(model, [["Z", "S"], "X"]) + self.assertEqual(data["degen"], "line") + self.assertEqual(data["degen_axes"], [True, False]) + + def test_full_1yield_structural(self): + # X, Y rate; yield = P/S as the third (sliced) axis + model = FakeModel([ + ("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("P", 0.0, 10.0), ("S", 1.0, 10.0), + ]) + data = lh.plot_flux_space_data_highs( + model, ["X", "Y", ["P", "S"]], constraints="X + Y + P <= 20", points=5) + self.assertEqual(data["ax_type"], ["rate", "rate", "yield"]) + self.assertEqual(data["degen"], "full") + self.assertTrue(data["is_approximate"]) + self.assertIsNotNone(data["slice_outlines"]) + self.assertGreater(len(data["slice_outlines"]), 0) + self.assertGreater(len(data["datapoints"]), 0) + # every datapoint's 3rd coordinate (the yield value) should fall + # within the axis's own computed val_limits + y_lo, y_hi = data["val_limits"][2] + for p in data["datapoints"]: + self.assertGreaterEqual(p[2], y_lo - 1e-6) + self.assertLessEqual(p[2], y_hi + 1e-6) + + # --- 2+ yield axes: grid-scanning fallback ------------------------- + # Denominator S pinned to a constant (bounds [5,5]) makes Y/S, Z/S (and + # X/S) plain linear rescalings of the already-verified cut cube + # (X,Y,Z in [0,10], X+Y+Z<=25, exact volume 1000 - 125/6), so the + # expected hull volume of the grid mesh is that volume divided by + # 5 per rescaled axis. + CUT_CUBE_VOLUME = 1000.0 - 125.0 / 6.0 + + def _hull_volume(self, datapoints): + import numpy as np + from scipy.spatial import ConvexHull + return ConvexHull(np.array(datapoints)).volume + + def test_two_yield_axes_grid_matches_rescaled_cut_cube(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0), ("S", 5.0, 5.0)]) + data = lh.plot_flux_space_data_highs( + model, ["X", ["Y", "S"], ["Z", "S"]], constraints="X + Y + Z <= 25", points=9) + self.assertEqual(data["ax_type"], ["rate", "yield", "yield"]) + self.assertTrue(data["is_approximate"]) + self.assertIsNone(data["face_polys"]) + self.assertTrue(data["triang"]) + self.assertAlmostEqual(self._hull_volume(data["datapoints"]), + self.CUT_CUBE_VOLUME / 25.0, places=3) + # every triangle index must refer to a real datapoint + self.assertLess(max(i for t in data["triang"] for i in t), len(data["datapoints"])) + + def test_three_yield_axes_grid_matches_rescaled_cut_cube(self): + # all three axes yields over the same constant denominator: also + # exercises axis 0 being a yield, i.e. its pin going through + # fix_yield_axis (X - x*S = 0) rather than a plain rate pin. + # Explicit worker counts: with 2 or 3 workers a cloned solver once + # returned a full-width y range a hair short of 2.0, which truncated + # that slice's point count and missed the cut-cube vertices. + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0), ("S", 5.0, 5.0)]) + for w in (1, 2, 3, 4): + with self.subTest(n_workers=w): + data = lh.plot_flux_space_data_highs( + model, [["X", "S"], ["Y", "S"], ["Z", "S"]], constraints="X + Y + Z <= 25", + points=9, n_workers=w) + self.assertEqual(data["ax_type"], ["yield", "yield", "yield"]) + self.assertEqual(len(data["datapoints"]), 2 * 9 * 9) + self.assertAlmostEqual(self._hull_volume(data["datapoints"]), + self.CUT_CUBE_VOLUME / 125.0, places=3) + + # --- threading: n_workers must not change the result ----------------- + def test_grid_path_threaded_matches_sequential(self): + import numpy as np + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0), ("S", 5.0, 5.0)]) + args = (model, ["X", ["Y", "S"], ["Z", "S"]]) + kw = dict(constraints="X + Y + Z <= 25", points=7) + ref = lh.plot_flux_space_data_highs(*args, n_workers=1, **kw) + for w in (2, 4): + d = lh.plot_flux_space_data_highs(*args, n_workers=w, **kw) + # same samples in the same order; triangle indices not compared + # (equal cell diagonals are a tie rounding noise can break) + np.testing.assert_allclose(np.array(d["datapoints"]), np.array(ref["datapoints"]), atol=1e-9) + self.assertEqual(len(d["triang"]), len(ref["triang"])) + self.assertAlmostEqual(self._hull_volume(d["datapoints"]), + self._hull_volume(ref["datapoints"]), places=6) + + def test_slice_path_threaded_matches_sequential(self): + model = FakeModel([ + ("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("P", 0.0, 10.0), ("S", 1.0, 10.0), + ]) + args = (model, ["X", "Y", ["P", "S"]]) + kw = dict(constraints="X + Y + P <= 20", points=6) + ref = lh.plot_flux_space_data_highs(*args, n_workers=1, **kw) + for w in (2, 4): + d = lh.plot_flux_space_data_highs(*args, n_workers=w, **kw) + self.assertEqual(len(d["slice_outlines"]), len(ref["slice_outlines"])) + for a, b in zip(d["slice_outlines"], ref["slice_outlines"]): + pa = [d["datapoints"][i] for i in a] + pb = [ref["datapoints"][i] for i in b] + self.assertAlmostEqual(pth.polygon_area([(p[0], p[1]) for p in pa]), + pth.polygon_area([(p[0], p[1]) for p in pb]), places=9) + self.assertAlmostEqual(pa[0][2], pb[0][2], places=12) + + + def test_two_yield_axes_infeasible_slices_raise(self): + # X + Y + Z <= -1 with all three >= 0 is infeasible, so the axis + # limits themselves already fail -- the point is it surfaces as an + # error rather than an empty/garbage mesh. + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0), ("S", 5.0, 5.0)]) + with self.assertRaises(Exception): + lh.plot_flux_space_data_highs( + model, ["X", ["Y", "S"], ["Z", "S"]], constraints="X + Y + Z <= -1", points=5) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_lptools_highs_render.py b/cnapy/tests/test_lptools_highs_render.py new file mode 100644 index 00000000..29121148 --- /dev/null +++ b/cnapy/tests/test_lptools_highs_render.py @@ -0,0 +1,120 @@ +"""Smoke tests for plot_flux_space_highs's rendering layer: confirms it +runs without crashing (matplotlib's Agg backend, no display needed) +across every dispatch branch plot_flux_space_data_highs's own tests +cover. This does NOT verify the figures look right -- only a real look +at real output can do that -- but a rendering crash on any of these +branches would be caught here rather than only on first real use. +""" + +import unittest + +import cobra +import matplotlib.pyplot as plt + +from cnapy import lptools_highs as lh + + +def FakeModel(reaction_bounds): + """A real cobra.Model with one metabolite-free reaction per + (id, lb, ub): no mass-balance rows, so the feasible set is exactly + the bounds box cut by whatever `constraints` a test passes -- the + same toy LPs the hand-derived reference shapes (pentagon, cut cube) + are worked out for. Named after the stand-in it replaces, so the + test bodies read unchanged.""" + model = cobra.Model("toy") + reactions = [] + for rid, lb, ub in reaction_bounds: + r = cobra.Reaction(rid) + r.bounds = (lb, ub) + reactions.append(r) + model.add_reactions(reactions) + return model + + +class TestRenderingSmoke(unittest.TestCase): + # pyplot is switched to Agg only for this class and switched back + # afterwards, instead of matplotlib.use("Agg") at import time, which + # would change the backend for the whole pytest session. + @classmethod + def setUpClass(cls): + import matplotlib + cls._previous_backend = matplotlib.get_backend() + plt.switch_backend("Agg") + + @classmethod + def tearDownClass(cls): + plt.close("all") + try: + plt.switch_backend(cls._previous_backend) + except Exception: # e.g. a GUI backend that can't be re-selected here + pass + + def tearDown(self): + plt.close("all") + + def _run(self, model, axes, **kwargs): + kwargs.setdefault("show", False) + return lh.plot_flux_space_highs(model, axes, **kwargs) + + def test_2d_point(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y"], constraints="X = 3, Y = 4") + self.assertEqual(len(dp), 1) + + def test_2d_line(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y"], constraints="X = 3") + self.assertEqual(len(dp), 2) + + def test_2d_full_rate_rate(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y"], constraints="X + Y <= 12") + self.assertGreater(len(dp), 2) + self.assertEqual(type(plot1).__name__, "Polygon") + + def test_2d_full_mixed(self): + model = FakeModel([("X", 0.0, 10.0), ("P", 0.0, 10.0), ("S", 1.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", ["P", "S"]], constraints="X + P <= 12") + self.assertGreater(len(dp), 2) + + def test_3d_point(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y", "Z"], constraints="X = 1, Y = 2, Z = 3") + self.assertEqual(len(dp), 1) + + def test_3d_line(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y", "Z"], constraints="X = 1, Y = 2") + self.assertEqual(len(dp), 2) + + def test_3d_plane(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y", "Z"], constraints="X + Y <= 12, Z = 3") + from mpl_toolkits.mplot3d.art3d import Poly3DCollection + self.assertIsInstance(plot1, Poly3DCollection) + + def test_3d_full_0yield(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y", "Z"], constraints="X + Y + Z <= 25") + self.assertEqual(len(dp), 10) + from mpl_toolkits.mplot3d.art3d import Poly3DCollection + self.assertIsInstance(plot1, Poly3DCollection) + + def test_3d_full_1yield(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("P", 0.0, 10.0), ("S", 1.0, 10.0)]) + dp, triang, plot1 = self._run(model, ["X", "Y", ["P", "S"]], + constraints="X + Y + P <= 20", points=5) + self.assertGreater(len(dp), 0) + + + def test_3d_full_2yield_grid(self): + model = FakeModel([("X", 0.0, 10.0), ("Y", 0.0, 10.0), ("Z", 0.0, 10.0), ("S", 5.0, 5.0)]) + dp, triang, plot1 = self._run(model, ["X", ["Y", "S"], ["Z", "S"]], + constraints="X + Y + Z <= 25", points=5) + self.assertGreater(len(dp), 0) + from mpl_toolkits.mplot3d.art3d import Poly3DCollection + self.assertIsInstance(plot1, Poly3DCollection) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_mainwindow_slots.py b/cnapy/tests/test_mainwindow_slots.py new file mode 100644 index 00000000..8be2f39f --- /dev/null +++ b/cnapy/tests/test_mainwindow_slots.py @@ -0,0 +1,278 @@ +""" +Tests that exercise the real, shipped `MainWindow.fba()` and +`MainWindow.fva()` slot implementations (cnapy/gui_elements/main_window.py), +rather than the bare computational functions they wrap (see +test_core_units.py for that layer). + +Approach: call the *unbound* method with a minimal "FakeMainWindow" object +(see conftest.py's `fake_main_window` fixture) that implements only the +attributes these two methods touch. This runs CNApy's actual scenario +handling / comp_values population / infinite-bound conversion / exception +handling exactly as shipped, without constructing the full MainWindow +(menus, map view, embedded Jupyter console, QWebEngineView, ...), which is +both slow and drags in optional dependencies unrelated to FBA/FVA. + +If your CI environment can afford it, `test_mainwindow_slots_real_window.py` +(sketched at the bottom of this file, skipped by default) shows how to do +the same thing against a fully constructed MainWindow instead, for a +higher-fidelity (but much heavier and more fragile) end-to-end check. +""" +import pytest +import qtpy.QtWebEngineWidgets + +def test_fba_slot_populates_comp_values(fake_main_window, mainwindow_cls): + mainwindow_cls.fba(fake_main_window) + + appdata = fake_main_window.appdata + reference = appdata.project.cobra_py_model.copy().optimize() + + assert appdata.project.solution is not None, ( + "appdata.project.solution is None - model_optimization_with_exceptions() " + "swallowed an exception from model.optimize() (see cnapy/core_gui.py: it " + "catches Exception broadly and only re-surfaces community-license errors; " + "any other solver exception is silently discarded). Temporarily patch " + "cnapy.gui_elements.main_window.model_optimization_with_exceptions with " + "a version that re-raises to see the real traceback." + ) + assert appdata.project.solution.status == "optimal", ( + f"unexpected solver status: {appdata.project.solution.status}" + ) + assert appdata.project.comp_values, "fba() left comp_values empty" + for rxn_id, flux in reference.fluxes.items(): + lo, hi = appdata.project.comp_values[rxn_id] + assert lo == hi == pytest.approx(flux, abs=1e-6) + + # process_fba_solution() is expected to trigger exactly one redraw + fake_main_window.centralWidget().update.assert_called() + + +def test_fba_slot_surfaces_optimize_exception(fake_main_window, mainwindow_cls, monkeypatch): + """ + Diagnostic test: bypasses model_optimization_with_exceptions()'s broad + except-and-swallow (cnapy/core_gui.py) so that if model.optimize() is + actually raising in this environment, the real traceback shows up here + instead of silently producing an empty comp_values later. + + If this test fails, the printed traceback is the true root cause of any + other fba()-related failures in this file - fix that first, independent + of anything scenario- or comp_values-related. + """ + import cnapy.gui_elements.main_window as mw + + def reraising_optimize(model): + return model.optimize() # no try/except - let it raise + + monkeypatch.setattr(mw, "model_optimization_with_exceptions", reraising_optimize) + + mainwindow_cls.fba(fake_main_window) # should not raise if optimize() truly succeeds + assert fake_main_window.appdata.project.solution.status == "optimal" + + +def test_fba_slot_reports_infeasible_scenario(fake_main_window, mainwindow_cls): + appdata = fake_main_window.appdata + biomass = next( + r for r in appdata.project.cobra_py_model.reactions + if r.objective_coefficient != 0 + ) + # An infeasible scenario: force growth but block every exchange. + appdata.project.scen_values[biomass.id] = (1.0, 1000.0) + for ex in appdata.project.cobra_py_model.exchanges: + appdata.project.scen_values[ex.id] = (0.0, 0.0) + + mainwindow_cls.fba(fake_main_window) + + assert appdata.project.comp_values == {}, ( + "fba() should clear comp_values on an infeasible scenario" + ) + + +def test_fba_slot_respects_scenario_bounds(fake_main_window, mainwindow_cls): + """A scenario that fixes one exchange reaction's flux should be + reflected in the optimized solution CNApy reports.""" + appdata = fake_main_window.appdata + model = appdata.project.cobra_py_model + glc_ex = next((r for r in model.exchanges if "glc" in r.id.lower()), None) + if glc_ex is None: + pytest.skip("ECC2 fixture has no glucose exchange reaction to pin") + + fixed_uptake = -5.0 + appdata.project.scen_values[glc_ex.id] = (fixed_uptake, fixed_uptake) + + mainwindow_cls.fba(fake_main_window) + + assert appdata.project.comp_values, ( + "comp_values is empty - fba() did not produce a solution at all " + "(see test_fba_slot_surfaces_optimize_exception for why)" + ) + lo, hi = appdata.project.comp_values[glc_ex.id] + assert lo == hi == pytest.approx(fixed_uptake, abs=1e-6) + + +def test_fva_slot_populates_comp_values_as_bounds(fake_main_window, mainwindow_cls): + mainwindow_cls.fva(fake_main_window) + + appdata = fake_main_window.appdata + assert appdata.project.comp_values_type == 1 # 1 == bounds/FVA result + assert appdata.project.comp_values, "fva() left comp_values empty" + + for rxn in appdata.project.cobra_py_model.reactions: + lo, hi = appdata.project.comp_values[rxn.id] + assert lo <= hi + 1e-7 + + # fva_values is documented to be a persisted copy of the FVA comp_values + assert appdata.project.fva_values == appdata.project.comp_values + + +def test_fva_slot_matches_core_units_reference(fake_main_window, mainwindow_cls, ecc2_reference_model): + """Cross-check the slot's output against the same cobrapy ground truth + used in test_core_units.py, closing the loop between the two test + layers on at least one model.""" + import cobra + from test_core_units import _finite_bounds_model + + mainwindow_cls.fva(fake_main_window) + appdata = fake_main_window.appdata + + ref_model = _finite_bounds_model(ecc2_reference_model) + reference = cobra.flux_analysis.flux_variability_analysis( + ref_model, fraction_of_optimum=0.0, processes=1 + ) + + for rxn_id in reference.index: + lo, hi = appdata.project.comp_values[rxn_id] + assert lo == pytest.approx(reference.loc[rxn_id, "minimum"], abs=1e-4) + assert hi == pytest.approx(reference.loc[rxn_id, "maximum"], abs=1e-4) + + +def test_fva_slot_reports_infeasible_scenario(fake_main_window, mainwindow_cls): + appdata = fake_main_window.appdata + biomass = next( + r for r in appdata.project.cobra_py_model.reactions + if r.objective_coefficient != 0 + ) + appdata.project.scen_values.constraints.append( + ({biomass.id: 1.0}, ">=", 1e6) # unachievable + ) + + # Should not raise out of the slot - fva() is expected to catch + # cobra.exceptions.Infeasible internally and show a QMessageBox instead + # (suppressed by the no_blocking_dialogs autouse fixture). + mainwindow_cls.fva(fake_main_window) + + assert appdata.project.comp_values == {} + + +def test_replace_ids_renames_reactions_and_syncs_project(fake_main_window, mainwindow_cls): + appdata = fake_main_window.appdata + model = appdata.project.cobra_py_model + reaction = model.reactions[0] + old_id = reaction.id + reaction.annotation["test.id"] = "renamed_reaction" + appdata.project.scen_values[old_id] = (0.0, 1.0) + appdata.project.comp_values[old_id] = (0.5, 0.5) + + renamed = mainwindow_cls.replace_ids(fake_main_window, "reactions", "test.id") + + assert renamed == [(old_id, "renamed_reaction")] + assert reaction.id == "renamed_reaction" + assert reaction.annotation["original ID"] == old_id + assert appdata.project.scen_values["renamed_reaction"] == (0.0, 1.0) + assert old_id not in appdata.project.scen_values + assert appdata.project.comp_values["renamed_reaction"] == (0.5, 0.5) + central_widget = fake_main_window.centralWidget() + central_widget.handle_changed_reaction.assert_called_once_with(old_id, reaction) + central_widget.update.assert_called_once_with(rebuild_all_tabs=True) + + +def test_replace_ids_renames_metabolites_with_compartment_suffix(fake_main_window, mainwindow_cls): + model = fake_main_window.appdata.project.cobra_py_model + metabolite = model.metabolites[0] + old_id = metabolite.id + metabolite.annotation["test.id"] = "renamed_metabolite" + + renamed = mainwindow_cls.replace_ids(fake_main_window, "metabolites", "test.id") + + new_id = "renamed_metabolite_" + metabolite.compartment + assert renamed == [(old_id, new_id)] + assert metabolite.id == new_id + fake_main_window.centralWidget().handle_changed_metabolite.assert_called_once_with( + metabolite, metabolite.reactions, old_id) + + +def _scenario_with_scenario_reaction(appdata): + scen = appdata.project.scen_values + met_id = appdata.project.cobra_py_model.metabolites[0].id + scen.reactions = {"S_new": [{met_id: -1.0, "new_met": 2.0}, -5.0, 7.0]} + scen["S_new"] = (1.0, 1.0) + appdata.add_scenario_to_history(scen) + appdata.record_current_scenario() # two history entries + return met_id + + +@pytest.mark.parametrize("answer", ["Yes", "No"]) +def test_add_scenario_reactions_to_model(fake_main_window, mainwindow_cls, monkeypatch, answer): + from qtpy.QtWidgets import QMessageBox + questions = [] + monkeypatch.setattr(QMessageBox, "question", + lambda parent, title, text, *a: questions.append(text) or getattr(QMessageBox.StandardButton, answer)) + appdata = fake_main_window.appdata + model = appdata.project.cobra_py_model + met_id = _scenario_with_scenario_reaction(appdata) + num_reactions = len(model.reactions) + + mainwindow_cls.add_scenario_reactions_to_model(fake_main_window) + + assert len(questions) == 1 and "scenario history" in questions[0] and "S_new" in questions[0] + scen = appdata.project.scen_values + assert scen["S_new"] == (1.0, 1.0) # the scenario value is kept in any case + if answer == "No": + assert "S_new" not in model.reactions and len(model.reactions) == num_reactions + assert "S_new" in scen.reactions + assert len(appdata.scenario_history) == 2 + fake_main_window.unsaved_changes.assert_not_called() + else: + reaction = model.reactions.get_by_id("S_new") + assert reaction.bounds == (-5.0, 7.0) + assert {m.id: c for m, c in reaction.metabolites.items()} == {met_id: -1.0, "new_met": 2.0} + assert scen.reactions == {} + assert len(appdata.scenario_history) == 1 and appdata.scenario_history[0] is scen + assert appdata.current_scenario_index == 0 + fake_main_window.update_undo_redo_actions.assert_called_once() + fake_main_window.unsaved_changes.assert_called_once() + fake_main_window.centralWidget().update.assert_called_once_with(rebuild_all_tabs=True) + assert "S_new" in appdata.project.reaction_ids.id_list + + +def test_add_scenario_reactions_to_model_without_scenario_reactions(fake_main_window, mainwindow_cls, monkeypatch): + from qtpy.QtWidgets import QMessageBox + monkeypatch.setattr(QMessageBox, "question", lambda *a: pytest.fail("no confirmation expected")) + num_reactions = len(fake_main_window.appdata.project.cobra_py_model.reactions) + mainwindow_cls.add_scenario_reactions_to_model(fake_main_window) + assert len(fake_main_window.appdata.project.cobra_py_model.reactions) == num_reactions + + +def test_replace_ids_rejects_unknown_items(fake_main_window, mainwindow_cls): + with pytest.raises(ValueError): + mainwindow_cls.replace_ids(fake_main_window, "genes", "test.id") + + +# --------------------------------------------------------------------------- +# Optional, heavier end-to-end variant against a real MainWindow instance. +# Skipped by default: constructing MainWindow pulls in the full gui_elements +# package (menus, MapView, EscherMapView/QWebEngineView, the embedded +# Jupyter console, StrainDesign dialogs, ...) and its constructor signature +# isn't reproduced here. Un-skip and fill in the constructor call once you +# know what MainWindow.__init__ needs in your checked-out version (it takes +# at least an AppData; check the current signature in main_window.py). +# --------------------------------------------------------------------------- + +@pytest.mark.skip(reason="requires the full MainWindow constructor call to be filled in") +def test_fba_via_real_main_window(qapp, appdata): + from cnapy.gui_elements.main_window import MainWindow + + window = MainWindow(appdata) # <- adjust to the real constructor signature + window.fba() + reference = appdata.project.cobra_py_model.copy().optimize() + for rxn_id, flux in reference.fluxes.items(): + lo, hi = appdata.project.comp_values[rxn_id] + assert lo == hi == pytest.approx(flux, abs=1e-6) diff --git a/cnapy/tests/test_plot_trace_highs.py b/cnapy/tests/test_plot_trace_highs.py new file mode 100644 index 00000000..945a7767 --- /dev/null +++ b/cnapy/tests/test_plot_trace_highs.py @@ -0,0 +1,83 @@ +"""Tests plot_trace_highs.py against a hand-verifiable 2D pentagon: + + X, Y in [0, 10], X + Y <= 12 + +Vertices (0,0), (10,0), (10,2), (2,10), (0,10); shoelace area = 68 +(100 for the full square minus the 32 for the corner triangle cut off +by X+Y<=12 -- both computed independently as a cross-check on the +traced result, since matching a shoelace-formula area is far more +robust than trying to match exact vertex order/positions when HiGHS's +choice of optimal vertex on a degenerate face isn't fully determined). +""" + +import unittest + +import scipy.sparse as sp + + +from cnapy import highs_lp_cache as hlc +from cnapy import plot_trace_highs as pth + +REACTION_IDS = ["X", "Y"] +RXN_INDEX = {"X": 0, "Y": 1} +COL_LOWER = [0.0, 0.0] +COL_UPPER = [10.0, 10.0] +TRUE_AREA = 68.0 + + +def pentagon_lp(): + S = sp.lil_matrix((0, 2)) # no mass-balance rows needed for this toy case + extra_rows = [({0: 1.0, 1: 1.0}, float("-inf"), 12.0)] # X + Y <= 12 + lp, fix_rows = hlc._build_rate_lp_arrays(S, COL_LOWER, COL_UPPER, extra_rows=extra_rows) + return lp, fix_rows + + +class TestPolygonTracer(unittest.TestCase): + def test_sequential_area_matches_reference(self): + lp, fix_rows = pentagon_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices = pth.trace_polygon_rate_rate(solver, {"X": 1.0}, {"Y": 1.0}) + area = pth.polygon_area(vertices) + self.assertAlmostEqual(area, TRUE_AREA, places=3, msg=f"vertices={vertices}") + + def test_parallel_area_matches_reference(self): + lp, fix_rows = pentagon_lp() + vertices = pth.trace_polygon_rate_rate_parallel( + lp, REACTION_IDS, RXN_INDEX, {"X": 1.0}, {"Y": 1.0}, n_workers=3) + area = pth.polygon_area(vertices) + self.assertAlmostEqual(area, TRUE_AREA, places=3, msg=f"vertices={vertices}") + + def test_sequential_and_parallel_agree(self): + lp, fix_rows = pentagon_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + seq = pth.trace_polygon_rate_rate(solver, {"X": 1.0}, {"Y": 1.0}) + par = pth.trace_polygon_rate_rate_parallel( + lp, REACTION_IDS, RXN_INDEX, {"X": 1.0}, {"Y": 1.0}, n_workers=4) + self.assertAlmostEqual(pth.polygon_area(seq), pth.polygon_area(par), places=6) + # same vertex *set* (order/starting point may legitimately differ) + seq_set = {tuple(round(c, 6) for c in p) for p in seq} + par_set = {tuple(round(c, 6) for c in p) for p in par} + self.assertEqual(seq_set, par_set, msg=f"seq={seq}\npar={par}") + + def test_all_vertices_feasible(self): + lp, fix_rows = pentagon_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices = pth.trace_polygon_rate_rate(solver, {"X": 1.0}, {"Y": 1.0}) + for x, y in vertices: + self.assertGreaterEqual(x, -1e-6) + self.assertGreaterEqual(y, -1e-6) + self.assertLessEqual(x, 10 + 1e-6) + self.assertLessEqual(y, 10 + 1e-6) + self.assertLessEqual(x + y, 12 + 1e-6) + + def test_expected_vertex_set(self): + lp, fix_rows = pentagon_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices = pth.trace_polygon_rate_rate(solver, {"X": 1.0}, {"Y": 1.0}) + expected = {(0.0, 0.0), (10.0, 0.0), (10.0, 2.0), (2.0, 10.0), (0.0, 10.0)} + got = {tuple(round(c, 6) for c in p) for p in vertices} + self.assertEqual(got, expected, msg=f"vertices={vertices}") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_project_files.py b/cnapy/tests/test_project_files.py new file mode 100644 index 00000000..c99f216c --- /dev/null +++ b/cnapy/tests/test_project_files.py @@ -0,0 +1,151 @@ +"""Tests for writing and opening CNApy projects (.cna ZIP files) via the real +MainWindow.write_project_file/save_model/open_project, called unbound on the fake_main_window +from conftest.py, and for SBML/JSON model export round trips. + +run: pytest cnapy/tests/test_project_files.py +""" +import json +import types +from zipfile import ZipFile + +import cobra +import pytest + +from cnapy.appdata import CnaMap, MAP_VIEW_ESCHER +from cnapy.cobra_cnapy import CNApyModel, stoichiometry_hash + +SVG = '' +ESCHER_MAP_DATA = json.dumps([{"map_name": "test"}, {"reactions": {}, "nodes": {}}]) + + +def model_hash(model): + # independent of the reaction order, which SBML/JSON need not preserve + return sorted((r.id, r.lower_bound, r.upper_bound, + sorted((m.id, float(c)) for m, c in r.metabolites.items())) + for r in model.reactions) + + +@pytest.fixture +def window(fake_main_window, mainwindow_cls, monkeypatch): + fake = fake_main_window + for name in ("write_project_file", "save_model", "open_project"): + setattr(fake, name, types.MethodType(getattr(mainwindow_cls, name), fake)) + fake.centralWidget().splitter2.getRange.return_value = (0, 1000) + fake.messages = [] + for kind in ("critical", "warning"): + monkeypatch.setattr("cnapy.gui_elements.main_window.QMessageBox." + kind, + lambda *args, kind=kind: fake.messages.append((kind,) + args[1:])) + return fake + + +@pytest.fixture +def project_with_maps(window, tmp_path): + project = window.appdata.project + svg_path = tmp_path / "background.svg" + svg_path.write_text(SVG) + cna_map = CnaMap("CNApy map") + cna_map["background"] = str(svg_path) + cna_map["boxes"] = {"EX_glc_e": [10.0, 20.0, False, True], "PGI": [30.0, 40.0, True, False]} + cna_map["zoom"] = 2 + escher_map = CnaMap("Escher map") + escher_map["view"] = MAP_VIEW_ESCHER + escher_map["escher_map_data"] = ESCHER_MAP_DATA + project.maps = {"CNApy map": cna_map, "Escher map": escher_map} + project.meta_data = {"description": "test project"} + project.name = str(tmp_path / "project.cna") + return project + + +@pytest.mark.parametrize("as_json", [False, True], ids=["sbml", "json"]) +def test_project_round_trip(window, project_with_maps, as_json): + window.appdata.save_model_as_json = as_json + project = project_with_maps + original_hash = model_hash(project.cobra_py_model) + filename = project.name + + assert window.write_project_file() is True + with ZipFile(filename) as zip_ref: + names = set(zip_ref.namelist()) + boxes = json.loads(zip_ref.read("box_positions.json")) + meta = json.loads(zip_ref.read("meta.json")) + svg = zip_ref.read("map1.svg").decode() + assert names == {"model.json" if as_json else "model.sbml", "box_positions.json", "meta.json", "map1.svg"} + assert boxes["CNApy map"]["background"] == "map1.svg" # archive name, not the local path + assert meta == {"description": "test project", "format version": window.appdata.format_version} + assert svg == SVG + window.nounsaved_changes.assert_called_once() + + # replace the project, then open the file again + project.reset() + window.open_project(filename) + assert window.messages == [] + window.reset_gui_for_new_project.assert_called_once() + window.set_current_filename.assert_called_once_with(filename) + assert model_hash(project.cobra_py_model) == original_hash + assert isinstance(project.cobra_py_model, CNApyModel) + assert project.meta_data["description"] == "test project" + maps = project.maps + assert list(maps) == ["CNApy map", "Escher map"] + assert maps["CNApy map"]["boxes"] == {"EX_glc_e": [10.0, 20.0, False, True], "PGI": [30.0, 40.0, True, False]} + assert maps["CNApy map"]["zoom"] == 2 + with open(maps["CNApy map"]["background"]) as fp: # extracted into a temporary directory + assert fp.read() == SVG + assert maps["Escher map"]["view"] == MAP_VIEW_ESCHER + assert maps["Escher map"]["escher_map_data"] == ESCHER_MAP_DATA + + +def test_failed_save_keeps_existing_project_file(window, project_with_maps, tmp_path): + filename = project_with_maps.name + assert window.write_project_file() is True + with open(filename, "rb") as fp: + saved = fp.read() + project_with_maps.maps["CNApy map"]["background"] = str(tmp_path / "missing.svg") + with pytest.raises(FileNotFoundError): + window.write_project_file() + with open(filename, "rb") as fp: + assert fp.read() == saved + assert not (tmp_path / "project.cna.tmp").exists() + + +def assert_project_unchanged(window, model): + assert window.appdata.project.cobra_py_model is model + window.reset_gui_for_new_project.assert_not_called() + + +@pytest.mark.parametrize("arc_names, expected_kind", [ + (["model.sbml", "meta.json"], "critical"), # no box_positions.json + (["model.sbml", "box_positions.json"], "warning"), # no meta.json + (["box_positions.json", "meta.json"], "warning"), # no model +], ids=["no_box_positions", "no_meta", "no_model"]) +def test_open_incomplete_project(window, tmp_path, arc_names, expected_kind): + model = window.appdata.project.cobra_py_model + filename = str(tmp_path / "broken.cna") + with ZipFile(filename, "w") as zip_obj: + for name in arc_names: + zip_obj.writestr(name, "{}") + window.open_project(filename) + assert [m[0] for m in window.messages] == [expected_kind] + assert_project_unchanged(window, model) + + +def test_open_non_zip_file(window, tmp_path): + model = window.appdata.project.cobra_py_model + filename = tmp_path / "not_a_zip.cna" + filename.write_text("this is not a ZIP file") + window.open_project(str(filename)) + assert [m[0] for m in window.messages] == ["critical"] + assert_project_unchanged(window, model) + + +@pytest.mark.parametrize("suffix", ["xml", "json"]) +def test_model_export_round_trip(window, tmp_path, suffix): + model = window.appdata.project.cobra_py_model + path = str(tmp_path / ("model." + suffix)) + window.save_model(path, save_as_json=(suffix == "json")) + assert window.messages == [] # ECC2 has no undefined compartments + reread = CNApyModel.read_sbml_model(path) if suffix == "xml" else cobra.io.load_json_model(path) + assert model_hash(reread) == model_hash(model) + if suffix == "xml": + # JSON export sorts the metabolites of each reaction alphabetically, which changes + # stoichiometry_hash (it depends on the metabolite order), so only SBML is compared here + assert stoichiometry_hash(reread).digest() == stoichiometry_hash(model).digest() diff --git a/cnapy/tests/test_scenario_files.py b/cnapy/tests/test_scenario_files.py new file mode 100644 index 00000000..0a284843 --- /dev/null +++ b/cnapy/tests/test_scenario_files.py @@ -0,0 +1,209 @@ +"""Tests for saving/loading scenarios as .scen (JSON) and .val files (Scenario.save/load in cnapy/appdata.py) +and for parse_scenario. The spreadsheet format is covered by test_scenario_spreadsheet.py. + +run: pytest cnapy/tests/test_scenario_files.py +""" +import json +import math + +import cobra +import pytest + +from cnapy.appdata import AppData, Scenario, parse_scenario +from test_scenario_spreadsheet import MODEL_REACTIONS, isobutanol_scenario + + +@pytest.fixture +def appdata(qapp): + appdata = AppData() + model = cobra.Model("test") + model.add_reactions([cobra.Reaction(r) for r in MODEL_REACTIONS]) + appdata.project.cobra_py_model = model + return appdata + + +def flux_tuples(scen): + # values loaded from JSON are lists, compare them as tuples + return {r: tuple(v) for r, v in scen.items()} + + +def write_json(path, json_dict): + with open(path, "w") as fp: + json.dump(json_dict, fp) + return str(path) + + +def scen_json(**fields): + json_dict = {"fluxes": {}, "pinned_reactions": [], "description": "", "objective_direction": "max", + "objective_coefficients": {}, "use_scenario_objective": False, "reactions": {}, + "constraints": [], "annotations": [], "version": 3} + json_dict.update(fields) + return json_dict + + +def test_scen_round_trip(tmp_path, appdata): + path = str(tmp_path / "scen.scen") + expected = isobutanol_scenario() + expected.save(path) + assert not expected.has_unsaved_changes + unknown_ids, incompatible_constraints, skipped = appdata.project.scen_values.load(path, appdata) + assert unknown_ids == [] and incompatible_constraints == [] and skipped == [] + scen = appdata.project.scen_values # replaced by a copy when the flux values are set + assert scen.file_name == path + assert flux_tuples(scen) == dict(expected) # also checks that -inf survives + assert scen.pinned_reactions == expected.pinned_reactions + assert scen.description == expected.description + assert scen.objective_direction == "min" + assert scen.objective_coefficients == expected.objective_coefficients + assert scen.use_scenario_objective is True + assert scen.reactions == expected.reactions + assert scen.constraints == expected.constraints + assert scen.annotations == expected.annotations + assert {r: tuple(v) for r, v in appdata.project.comp_values.items()} == dict(expected) + + +def test_scen_unknown_ids_are_reported_and_dropped(tmp_path, appdata): + path = write_json(tmp_path / "unknown.scen", scen_json( + # the scenario reaction R_new can have a flux value, its R_ prefix must not be stripped + fluxes={"PGI": [1.0, 2.0], "R_BIOMASS": [0.1, 0.1], "UNKNOWN": [3.0, 3.0], "R_UNKNOWN": [1.0, 1.0], + "R_new": [4.0, 4.0]}, + pinned_reactions=["PGI", "UNKNOWN_PINNED"], + objective_coefficients={"BIOMASS": 1.0, "UNKNOWN_OBJ": 2.0}, + reactions={"PGI": [{"a_c": -1.0}, 0.0, 10.0], "R_new": [{"a_c": -1.0}, 0.0, 10.0]}, + constraints=[[{"PGI": 1.0, "R_new": 1.0}, "<=", 5.0], [{"UNKNOWN": 1.0}, ">=", 1.0], [None, "", ""]])) + unknown_ids, incompatible_constraints, skipped = appdata.project.scen_values.load(path, appdata) + # reported with the IDs given in the file, also when they start with R_ + assert sorted(unknown_ids) == ["R_UNKNOWN", "UNKNOWN", "UNKNOWN_OBJ", "UNKNOWN_PINNED"] + assert incompatible_constraints == [[{"UNKNOWN": 1.0}, ">=", 1.0]] + assert skipped == ["PGI"] # a scenario reaction must not replace a model reaction + scen = appdata.project.scen_values + # R_ prefix stripped from R_BIOMASS + assert flux_tuples(scen) == {"PGI": (1.0, 2.0), "BIOMASS": (0.1, 0.1), "R_new": (4.0, 4.0)} + assert tuple(appdata.project.comp_values["R_new"]) == (4.0, 4.0) + assert scen.pinned_reactions == {"PGI"} + assert scen.objective_coefficients == {"BIOMASS": 1.0} + assert scen.reactions == {"R_new": [{"a_c": -1.0}, 0.0, 10.0]} # bounds unchanged + assert scen.constraints == [[{"PGI": 1.0, "R_new": 1.0}, "<=", 5.0], Scenario.empty_constraint] + + +def test_scen_merge_keeps_metadata(tmp_path, appdata): + scen = appdata.project.scen_values + scen["PGI"] = (1.0, 2.0) + scen["EX_o2_e"] = (-5.0, 0.0) + scen.pinned_reactions = {"PGI"} + scen.description = "keep me" + scen.objective_coefficients = {"BIOMASS": 1.0} + scen.constraints = [[{"PGI": 1.0}, "<=", 3.0]] + path = write_json(tmp_path / "merge.scen", scen_json( + fluxes={"PGI": [3.0, 3.0], "EX_glc__D_e": [-10.0, -10.0]}, pinned_reactions=["BIOMASS"], + description="other", objective_direction="min", objective_coefficients={"PGI": 1.0}, + constraints=[[{"EX_o2_e": 1.0}, ">=", -1.0]])) + unknown_ids, _, _ = scen.load(path, appdata, merge=True) + assert unknown_ids == [] + merged = appdata.project.scen_values + assert flux_tuples(merged) == {"PGI": (3.0, 3.0), "EX_o2_e": (-5.0, 0.0), "EX_glc__D_e": (-10.0, -10.0)} + assert merged.pinned_reactions == {"PGI", "BIOMASS"} + assert merged.description == "keep me" + assert merged.objective_direction == "max" + assert merged.objective_coefficients == {"BIOMASS": 1.0} + assert merged.constraints == [[{"PGI": 1.0}, "<=", 3.0]] + + +def test_scen_merge_values_of_scenario_reactions(tmp_path, appdata): + scen = appdata.project.scen_values + scen.reactions = {"S1": [{"a_c": -1.0}, 0.0, 10.0]} + path = write_json(tmp_path / "merge.scen", scen_json( + fluxes={"S1": [2.0, 3.0], "S2": [1.0, 1.0]}, reactions={"S2": [{"b_c": -1.0}, 0.0, 10.0]})) + unknown_ids, _, _ = scen.load(path, appdata, merge=True) + # the scenario reactions of a merged file are not taken over, hence neither the value of S2 + assert unknown_ids == ["S2"] + merged = appdata.project.scen_values + assert flux_tuples(merged) == {"S1": (2.0, 3.0)} + assert merged.reactions == {"S1": [{"a_c": -1.0}, 0.0, 10.0]} + + +def test_load_scenario_into_model_with_scenario_reaction_values(appdata): + scen = appdata.project.scen_values + scen.reactions = {"S1": [{"a_c": -1.0}, 0.0, 10.0], "S2": [{"a_c": 1.0}, -5.0, 5.0]} + scen["S1"] = (2.0, 3.0) # overrides the bounds of the scenario reaction + scen["PGI"] = (1.0, 1.0) + model = appdata.project.cobra_py_model + with model as m: + appdata.project.load_scenario_into_model(m) + assert m.reactions.S1.bounds == (2.0, 3.0) + assert m.reactions.S2.bounds == (-5.0, 5.0) # without scenario value it keeps its own bounds + assert m.reactions.PGI.bounds == (1.0, 1.0) + assert "S1" not in model.reactions and "S2" not in model.reactions + + +def test_load_scenario_into_model_annotations_are_reverted(appdata): + scen = appdata.project.scen_values + scen.reactions = {"S1": [{"a_c": -1.0}, 0.0, 10.0]} + scen.annotations = [{"reaction_id": "PGI", "key": "existing", "value": "scenario"}, + {"reaction_id": "PGI", "key": "new", "value": "x"}, + {"reaction_id": "S1", "key": "new", "value": "y"}] + model = appdata.project.cobra_py_model + pgi = model.reactions.PGI + pgi.annotation["existing"] = "model" + assert scen.reaction_annotation(pgi, "existing") == "scenario" + assert scen.reaction_annotation(pgi, "missing", "default") == "default" + with model as m: + appdata.project.load_scenario_into_model(m) + assert m.reactions.PGI.annotation == {"existing": "scenario", "new": "x"} + assert m.reactions.S1.annotation == {"new": "y"} + assert pgi.annotation == {"existing": "model"} + + +def test_scen_load_without_merge_replaces_everything(tmp_path, appdata): + scen = appdata.project.scen_values + scen["PGI"] = (1.0, 2.0) + scen.pinned_reactions = {"PGI"} + scen.description = "old" + path = write_json(tmp_path / "new.scen", scen_json(fluxes={"BIOMASS": [0.5, 0.5]})) + scen.load(path, appdata) + loaded = appdata.project.scen_values + assert flux_tuples(loaded) == {"BIOMASS": (0.5, 0.5)} + assert loaded.pinned_reactions == set() and loaded.description == "" + + +def test_legacy_flux_only_scen(tmp_path, appdata): + path = write_json(tmp_path / "legacy.scen", {"PGI": [1.0, 2.0], "UNKNOWN": [0.0, 0.0]}) + unknown_ids, _, _ = appdata.project.scen_values.load(path, appdata) + assert unknown_ids == ["UNKNOWN"] + assert flux_tuples(appdata.project.scen_values) == {"PGI": (1.0, 2.0)} + + +def test_val_file(tmp_path, appdata): + path = tmp_path / "scenario.val" + path.write_text("## CellNetAnalyzer scenario\nPGI 1.5\n\nR_BIOMASS 0.2\nnot a valid line\nUNKNOWN 1\n") + unknown_ids, _, _ = appdata.project.scen_values.load(str(path), appdata) + assert unknown_ids == ["UNKNOWN"] + scen = appdata.project.scen_values + assert dict(scen) == {"PGI": (1.5, 1.5), "BIOMASS": (0.2, 0.2)} + assert scen.file_name == "" # .val files are not saved back + + +def test_load_missing_file_raises(tmp_path, appdata): + with pytest.raises(FileNotFoundError): + appdata.project.scen_values.load(str(tmp_path / "missing.scen"), appdata) + + +@pytest.mark.parametrize("text, expected", [ + ("5", (5.0, 5.0)), + ("-1.5", (-1.5, -1.5)), + ("(-1, 2)", (-1, 2)), + ("(0.5,0.5)", (0.5, 0.5)), +]) +def test_parse_scenario(text, expected): + assert parse_scenario(text) == expected + + +def test_parse_scenario_inf(): + lb, ub = parse_scenario("-inf") + assert lb == ub == -math.inf + + +@pytest.mark.parametrize("text", ["abc", "(1, 2"]) +def test_parse_scenario_invalid(text): + with pytest.raises((ValueError, SyntaxError)): + parse_scenario(text) diff --git a/cnapy/tests/test_scenario_spreadsheet.py b/cnapy/tests/test_scenario_spreadsheet.py new file mode 100644 index 00000000..a5413bc7 --- /dev/null +++ b/cnapy/tests/test_scenario_spreadsheet.py @@ -0,0 +1,140 @@ +"""Tests for reading/writing scenarios as .xlsx spreadsheets (cnapy/scenario_spreadsheet.py). + +run: pytest cnapy/tests/test_scenario_spreadsheet.py +""" +import cobra +import openpyxl +import pytest + +from cnapy.appdata import AppData, Scenario +from cnapy.scenario_spreadsheet import (ScenarioSpreadsheetError, read_scenario_xlsx, write_scenario_xlsx, + FLUXES, REACTIONS, CONSTRAINTS, ANNOTATIONS, GENERAL, HEADERS) + +MODEL_REACTIONS = ["EX_glc__D_e", "EX_o2_e", "BIOMASS", "PGI"] + + +def isobutanol_scenario() -> Scenario: + scen = Scenario() + scen["EX_glc__D_e"] = (-10.0, -10.0) + scen["EX_o2_e"] = (-float("inf"), 0.0) + scen["EX_iBuOH"] = (0.5, 2.0) # scenario value of a scenario reaction + scen.pinned_reactions = {"EX_glc__D_e", "PGI"} + scen.description = "isobutanol production\nwith a minimal yield" + scen.objective_direction = "min" + scen.objective_coefficients = {"EX_iBuOH": 1.0, "BIOMASS": -0.5} + scen.use_scenario_objective = True + scen.reactions = {"EX_iBuOH": [{"iBuOH_c": -1.0}, 0.0, 1000.0], + "iBuOH": [{"h_c": -4.0, "nadh_c": -2.0, "pyr_c": -2.0, "co2_c": 2.0, "h2o_c": 1.0, + "iBuOH_c": 1.0, "nad_c": 2.0}, -5.0, 1000.0]} + scen.constraints = [[{"EX_iBuOH": 1.0, "EX_glc__D_e": 1.0}, ">=", 0.0], [{"PGI": 2.0}, "<=", 3.5]] + scen.annotations = [{"reaction_id": "iBuOH", "key": "biocyc.reaction", "value": "RXN-1"}] + return scen + + +def make_workbook(path, sheets): + wb = openpyxl.Workbook() + wb.remove(wb.active) + for title, rows in sheets.items(): + ws = wb.create_sheet(title) + ws.append(HEADERS[title]) + for row in rows: + ws.append(row) + wb.save(path) + return path + + +def test_round_trip(tmp_path): + scen = isobutanol_scenario() + path = tmp_path / "scen.xlsx" + write_scenario_xlsx(scen, path) + d = read_scenario_xlsx(path, MODEL_REACTIONS) + assert d["fluxes"] == dict(scen) + assert set(d["pinned_reactions"]) == scen.pinned_reactions + assert d["description"] == scen.description + assert d["objective_direction"] == "min" + assert d["objective_coefficients"] == scen.objective_coefficients + assert d["use_scenario_objective"] is True + assert d["reactions"] == scen.reactions + assert d["constraints"] == scen.constraints + assert d["annotations"] == scen.annotations + + +def test_empty_scenario_and_template(tmp_path): + path = tmp_path / "template.xlsx" + write_scenario_xlsx(Scenario(), path, include_examples=True) # example rows are comments + d = read_scenario_xlsx(path, MODEL_REACTIONS) + assert d["fluxes"] == {} and d["reactions"] == {} and d["constraints"] == [] and d["annotations"] == [] + assert d["pinned_reactions"] == [] and d["objective_coefficients"] == {} + assert d["use_scenario_objective"] is False + + +def test_hand_written_sheet(tmp_path): + path = make_workbook(tmp_path / "hand.xlsx", { + FLUXES: [["EX_glc__D_e", -10, None, None], + ["EX_o2_e", None, "-5,5", "no"], + ["# EX_foo", "abc", 1, None], + [None, None, None, None], + ["BIOMASS", None, None, "x"], + ["PGI", "-inf", "inf", True]], + REACTIONS: [["R_new", "a_c <=> 2 b_c", None, 10]], + GENERAL: [["objective", "R_new", None], ["Direction", "Maximize", None]], + }) # no Constraints and Annotations sheets + d = read_scenario_xlsx(path, MODEL_REACTIONS) + assert d["fluxes"] == {"EX_glc__D_e": (-10.0, -10.0), "EX_o2_e": (-5.5, -5.5), + "PGI": (-float("inf"), float("inf"))} + assert d["pinned_reactions"] == ["BIOMASS", "PGI"] + assert d["reactions"] == {"R_new": [{"a_c": -1, "b_c": 2}, cobra.Configuration().lower_bound, 10.0]} + assert d["objective_coefficients"] == {"R_new": 1.0} + assert d["objective_direction"] == "max" + assert d["use_scenario_objective"] is True # objective given and no explicit setting + + +def test_errors_are_collected(tmp_path): + path = make_workbook(tmp_path / "bad.xlsx", { + FLUXES: [["EX_glc__D_e", "abc", None, None], ["EX_o2_e", 5, 1, None], ["BIOMASS", None, None, "maybe"]], + REACTIONS: [["R_new", "a_c b_c", None, None], ["R_new", "a_c -->", None, None]], + CONSTRAINTS: [["unknown_reac >= 1"], ["R_new + PGI"]], + ANNOTATIONS: [["PGI", None, "value"]], + GENERAL: [["Direction", "sideways", None]], + }) + with pytest.raises(ScenarioSpreadsheetError) as e: + read_scenario_xlsx(path, MODEL_REACTIONS) + messages = e.value.messages + for location in ["Fluxes!B2", "Fluxes!B3", "Fluxes!D4", "Reactions!B2", "Reactions!A3", + "Constraints!A2", "Constraints!A3", "Annotations!B2", "General!B2"]: + assert any(m.startswith(location + ":") for m in messages), location + assert len(messages) == 9 + + +@pytest.fixture +def appdata(qapp): + appdata = AppData() + model = cobra.Model("test") + model.add_reactions([cobra.Reaction(r) for r in MODEL_REACTIONS]) + appdata.project.cobra_py_model = model + return appdata + + +def test_scenario_load_and_save(tmp_path, appdata): + path = str(tmp_path / "scen.xlsx") + isobutanol_scenario().save(path) + unknown_ids, incompatible_constraints, skipped = appdata.project.scen_values.load(path, appdata) + assert unknown_ids == [] and incompatible_constraints == [] and skipped == [] + scen = appdata.project.scen_values # replaced by a copy when the flux values are set + assert scen.file_name == path + expected = isobutanol_scenario() + assert dict(scen) == dict(expected) + assert scen.reactions == expected.reactions + assert scen.constraints == expected.constraints + assert scen.objective_coefficients == expected.objective_coefficients + assert scen.pinned_reactions == expected.pinned_reactions + + +def test_scenario_load_error_keeps_scenario(tmp_path, appdata): + scen = appdata.project.scen_values + scen["PGI"] = (1.0, 2.0) + scen.description = "keep me" + path = make_workbook(tmp_path / "bad.xlsx", {FLUXES: [["EX_glc__D_e", "abc", None, None]]}) + with pytest.raises(ScenarioSpreadsheetError): + scen.load(str(path), appdata) + assert dict(scen) == {"PGI": (1.0, 2.0)} and scen.description == "keep me" diff --git a/cnapy/tests/test_scenario_undo_redo.py b/cnapy/tests/test_scenario_undo_redo.py new file mode 100644 index 00000000..e450baa3 --- /dev/null +++ b/cnapy/tests/test_scenario_undo_redo.py @@ -0,0 +1,138 @@ +"""Tests for the scenario undo/redo history: AppData's scen_values_* methods record the history, +MainWindow.undo_scenario_edit/redo_scenario_edit/update_undo_redo_actions (called unbound on the +fake_main_window from conftest.py) move through it and enable the toolbar actions. + +run: pytest cnapy/tests/test_scenario_undo_redo.py +""" +import types + +import pytest + +from cnapy.appdata import Scenario + + +@pytest.fixture +def window(fake_main_window, mainwindow_cls): + fake = fake_main_window + for name in ("undo_scenario_edit", "redo_scenario_edit", "update_undo_redo_actions", + "discard_failed_scenario_load"): + setattr(fake, name, types.MethodType(getattr(mainwindow_cls, name), fake)) + # like MainWindow.__init__ and reset_gui_for_new_project + fake.appdata.scenarioHistoryChanged.connect(fake.update_undo_redo_actions) + fake.appdata.clear_scenario_history() + fake.appdata.add_scenario_to_history(fake.appdata.project.scen_values) + return fake + + +def scen(window): + return dict(window.appdata.project.scen_values) + + +def actions_enabled(window): + return (window.undo_scenario_action.setEnabled.call_args.args[0], + window.redo_scenario_action.setEnabled.call_args.args[0]) + + +def test_fresh_history_has_nothing_to_undo(window): + assert actions_enabled(window) == (False, False) + window.undo_scenario_edit() + window.redo_scenario_edit() + assert scen(window) == {} + assert window.appdata.current_scenario_index == 0 + + +def test_undo_redo_restore_exact_states(window): + appdata = window.appdata + appdata.scen_values_set("EX_glc_e", (-10.0, -10.0)) + appdata.scen_values_set_multiple(["EX_o2_e", "EX_glc_e"], [(-5.0, 0.0), (-8.0, -8.0)]) + appdata.scen_values_pop("EX_o2_e") + states = [{}, {"EX_glc_e": (-10.0, -10.0)}, + {"EX_glc_e": (-8.0, -8.0), "EX_o2_e": (-5.0, 0.0)}, {"EX_glc_e": (-8.0, -8.0)}] + assert scen(window) == states[3] + assert actions_enabled(window) == (True, False) + + for expected in reversed(states[:3]): + window.undo_scenario_edit() + assert scen(window) == expected + assert actions_enabled(window) == (False, True) + window.undo_scenario_edit() # nothing left to undo + assert scen(window) == {} + + for expected in states[1:]: + window.redo_scenario_edit() + assert scen(window) == expected + assert actions_enabled(window) == (True, False) + window.redo_scenario_edit() # nothing left to redo + assert scen(window) == states[3] + + central = window.centralWidget() + central.scenario_tab.recreate_scenario_items.assert_called() + window.fba.assert_not_called() # auto_fba is off + + +def test_undo_keeps_hash_and_metadata(window): + appdata = window.appdata + appdata.project.scen_values.description = "before" + appdata.scen_values_set("EX_glc_e", (-10.0, -10.0)) + hash_after = appdata.project.scen_values.hash_value + appdata.project.scen_values.description = "after" # in-place edit of the current entry + window.undo_scenario_edit() + assert appdata.project.scen_values.description == "before" + assert appdata.project.scen_values.hash_value == Scenario().hash_value + window.redo_scenario_edit() + assert appdata.project.scen_values.description == "after" + assert appdata.project.scen_values.hash_value == hash_after + + +def test_new_edit_after_undo_drops_redo_tail(window): + appdata = window.appdata + appdata.scen_values_set("EX_glc_e", (-10.0, -10.0)) + appdata.scen_values_set("EX_o2_e", (-5.0, 0.0)) + window.undo_scenario_edit() + window.undo_scenario_edit() + assert actions_enabled(window) == (False, True) + appdata.scen_values_set("EX_ac_e", (1.0, 1.0)) + assert scen(window) == {"EX_ac_e": (1.0, 1.0)} + assert len(appdata.scenario_history) == 2 + assert actions_enabled(window) == (True, False) + window.redo_scenario_edit() + assert scen(window) == {"EX_ac_e": (1.0, 1.0)} + window.undo_scenario_edit() + assert scen(window) == {} + + +def test_clear_is_undoable(window): + appdata = window.appdata + appdata.scen_values_set_multiple(["EX_glc_e", "EX_o2_e"], [(-10.0, -10.0), (-5.0, 0.0)]) + appdata.scen_values_clear() + assert scen(window) == {} + window.undo_scenario_edit() + assert scen(window) == {"EX_glc_e": (-10.0, -10.0), "EX_o2_e": (-5.0, 0.0)} + + +def test_auto_fba_runs_after_undo_and_redo(window): + window.appdata.auto_fba = True + window.appdata.scen_values_set("EX_glc_e", (-10.0, -10.0)) + window.undo_scenario_edit() + window.redo_scenario_edit() + assert window.fba.call_count == 2 + + +def test_clear_history_disables_actions(window): + window.appdata.scen_values_set("EX_glc_e", (-10.0, -10.0)) + window.undo_scenario_edit() + assert actions_enabled(window) == (False, True) + window.appdata.clear_scenario_history() + assert actions_enabled(window) == (False, False) + + +def test_discard_failed_scenario_load(window): + appdata = window.appdata + appdata.scen_values_set("EX_glc_e", (-10.0, -10.0)) + # what load_scenario_file does before a load that then fails + appdata.record_current_scenario() + appdata.project.scen_values.clear() + window.discard_failed_scenario_load() + assert scen(window) == {"EX_glc_e": (-10.0, -10.0)} + assert len(appdata.scenario_history) == 2 + assert actions_enabled(window) == (True, False) diff --git a/cnapy/tests/test_slice_mesh.py b/cnapy/tests/test_slice_mesh.py new file mode 100644 index 00000000..ed4da547 --- /dev/null +++ b/cnapy/tests/test_slice_mesh.py @@ -0,0 +1,172 @@ +"""Tests for the 1-yield slice mesh (build_slice_mesh), the generic +orientation pass (orient_closed_mesh) and back-face culled rendering.""" +import math +import unittest +from collections import Counter +from cnapy.lptools_highs import build_slice_mesh, orient_closed_mesh + + +def square(s, shift=0.0): + return [(-s + shift, -s), (s + shift, -s), (s + shift, s), (-s + shift, s)] + + +def edge_stats(tri): + und = Counter(frozenset(p) for t in tri for p in ((t[0], t[1]), (t[1], t[2]), (t[2], t[0]))) + dirc = Counter(p for t in tri for p in ((t[0], t[1]), (t[1], t[2]), (t[2], t[0]))) + return und, sum(1 for v in dirc.values() if v > 1) + + +def signed_volume(dp, tri): + v = 0.0 + for a, b, c in tri: + A, B, C = dp[a], dp[b], dp[c] + v += (A[0] * (B[1] * C[2] - B[2] * C[1]) - A[1] * (B[0] * C[2] - B[2] * C[0]) + + A[2] * (B[0] * C[1] - B[1] * C[0])) / 6 + return v + + +def assert_closed_outward(tc, dp, tri): + und, conflicts = edge_stats(tri) + tc.assertTrue(all(c == 2 for c in und.values()), dict(Counter(und.values()))) + tc.assertEqual(conflicts, 0) + tc.assertGreater(signed_volume(dp, tri), 0) + + +def tapering_slices(n=12, sx=0.03, sy=5.0): + """Anisotropic, shrinking-to-a-segment stack like the real + biomass/EtOH/succ-yield case (rate axes ~100x apart in scale).""" + out = [] + for k in range(n): + f = 1 - k / (n - 1) + m = 3 + (k * 5) % 6 # varying vertex counts + ring = [(sx * f * math.cos(2 * math.pi * j / m) + sx, + sy * max(f, 0.05) * math.sin(2 * math.pi * j / m) + sy) for j in range(m)] + if k == n - 1: + ring = [(sx, sy), (sx, sy)] # collapsed duplicate tip + out.append((k * 0.1, ring)) + return out + + +class TestSliceMesh(unittest.TestCase): + def test_closed_outward_prism(self): + sl = [(0.0, square(1)), + (1.0, square(1)[2:] + square(1)[:2]), + (2.0, square(1) + [square(1)[0]])] + dp, tri, outl = build_slice_mesh(sl, [0, 1], 2) + self.assertEqual(len(outl), 3) + assert_closed_outward(self, dp, tri) + self.assertAlmostEqual(signed_volume(dp, tri), 8.0, places=9) + + def test_cw_input_rings_still_outward(self): + sl = [(0.0, square(1)[::-1]), (1.0, square(2)[::-1])] + dp, tri, _ = build_slice_mesh(sl, [0, 1], 2) + assert_closed_outward(self, dp, tri) + + def test_anisotropic_taper_no_fold(self): + # regression: the greedy shortest-diagonal walker folded here + dp, tri, outl = build_slice_mesh(tapering_slices(), [0, 1], 2) + assert_closed_outward(self, dp, tri) + self.assertEqual(len(outl[-1]), 1) # duplicate tip collapsed + + def test_all_axis_orders_outward(self): + for ri, yi in (([0, 1], 2), ([1, 2], 0), ([0, 2], 1), ([2, 0], 1), ([1, 0], 2)): + dp, tri, _ = build_slice_mesh(tapering_slices(), ri, yi) + with self.subTest(rate_indices=ri, yield_idx=yi): + assert_closed_outward(self, dp, tri) + + def test_slices_are_connected(self): + dp, tri, _ = build_slice_mesh([(0.0, square(1)), (1.0, square(2))], [0, 1], 2) + self.assertTrue([t for t in tri if len({dp[i][2] for i in t}) == 2]) + + def test_unsorted_slices(self): + dp, tri, _ = build_slice_mesh([(1.0, square(2)), (0.0, square(1))], [0, 1], 2) + assert_closed_outward(self, dp, tri) + + def test_skips_empty(self): + self.assertEqual(build_slice_mesh([(0, [])], [0, 1], 2), ([], [], [])) + dp, tri, outl = build_slice_mesh([(0, []), (1, square(1))], [0, 1], 2) + self.assertEqual(len(outl), 1) + + +class TestOrientClosedMesh(unittest.TestCase): + CUBE = [[x, y, z] for x in (0, 1) for y in (0, 1) for z in (0, 1)] + QUADS = [(0, 1, 3, 2), (4, 6, 7, 5), (0, 4, 5, 1), (2, 3, 7, 6), (0, 2, 6, 4), (1, 5, 7, 3)] + + def cube_tris(self): + return [t for a, b, c, d in self.QUADS for t in ([a, b, c], [a, c, d])] + + def test_scrambled_cube_fixed(self): + tri = [t if k % 3 else [t[0], t[2], t[1]] for k, t in enumerate(self.cube_tris())] + out, closed = orient_closed_mesh(self.CUBE, tri) + self.assertTrue(closed) + assert_closed_outward(self, self.CUBE, out) + + def test_inward_cube_flipped(self): + tri = [[t[0], t[2], t[1]] for t in self.cube_tris()] + out, closed = orient_closed_mesh(self.CUBE, tri) + self.assertTrue(closed) + self.assertGreater(signed_volume(self.CUBE, out), 0) + + def test_open_mesh_untouched(self): + tri = self.cube_tris()[:-2] + out, closed = orient_closed_mesh(self.CUBE, tri) + self.assertFalse(closed) + self.assertEqual(out, tri) + + +class TestCulledRender(unittest.TestCase): + def test_culling_hides_back_faces(self): + # Rendered on an explicit Agg canvas rather than via + # matplotlib.use("Agg"), so this doesn't switch the global backend + # for the rest of the pytest session (Qt tests share it). + try: + import numpy as np + from matplotlib.figure import Figure + from matplotlib.backends.backend_agg import FigureCanvasAgg + except ImportError: + self.skipTest("matplotlib not available") + from cnapy import lptools_highs as L + L._ensure_plotting() + cube = TestOrientClosedMesh.CUBE + tri, _ = orient_closed_mesh(cube, TestOrientClosedMesh().cube_tris()) + + # one distinct colour per cube face, keyed by its outward normal + face_colours = {(0, 0, -1): (1, 0, 0), (0, 0, 1): (0, 1, 0), (0, -1, 0): (0, 0, 1), + (0, 1, 0): (1, 1, 0), (-1, 0, 0): (1, 0, 1), (1, 0, 0): (0, 1, 1)} + normals = [] + for t in tri: + a, b, c = (np.array(cube[i], float) for i in t) + n = np.cross(b - a, c - a) + normals.append(tuple(int(v) for v in np.sign(n))) + colours = [list(face_colours[n]) + [1.0] for n in normals] + + fig = Figure(figsize=(3, 3), dpi=100) + FigureCanvasAgg(fig) + ax = fig.add_subplot(projection="3d") + ax.set_axis_off() + coll = L.CulledTriCollection([[cube[i] for i in t] for t in tri], colours, linewidths=0) + ax.add_collection3d(coll) + ax.view_init(30, 45) # camera on the +x, +y, +z side + fig.canvas.draw() + # 3 faces = 6 triangles; 12 would mean culling silently fell back to + # drawing everything (e.g. a matplotlib upgrade changed the mplot3d + # internals it hooks) + self.assertEqual(len(coll.get_paths()), 6) + + # ...and they must be the right 3: front faces present in the + # image, back faces absent (catches a flipped front/back test, + # which would also leave exactly 6 triangles) + img = np.asarray(fig.canvas.buffer_rgba())[:, :, :3].astype(int) + + def pixels(rgb): + return int((np.abs(img - np.array(rgb) * 255).max(axis=2) <= 3).sum()) + for normal, rgb in face_colours.items(): + front = sum(normal) > 0 + with self.subTest(face=normal, front=front): + if front: + self.assertGreater(pixels(rgb), 200) + else: + self.assertEqual(pixels(rgb), 0) + +if __name__ == "__main__": + unittest.main() diff --git a/cnapy/tests/test_trace_3d_slice.py b/cnapy/tests/test_trace_3d_slice.py new file mode 100644 index 00000000..633868d2 --- /dev/null +++ b/cnapy/tests/test_trace_3d_slice.py @@ -0,0 +1,92 @@ +"""Tests trace_3d_slice_polygon / trace_3d_slice_polygon_sequential +against a known family of cross-sections: + + X, Y, Z in [0, 10], X + Y + Z <= 15 + +Slicing on Z = z fixes X + Y <= 15 - z =: C, giving: + C in (0, 10]: a triangle, area = C^2 / 2 + C in [10, 20): a clipped pentagon, area = 100 - (20 - C)^2 / 2 +(both derived independently; C=12 i.e. z=3 reduces to the pentagon +already used in test_plot_trace_highs.py, area 68, as a cross-check). +""" + +import unittest + +import scipy.sparse as sp + + +from cnapy import highs_lp_cache as hlc +from cnapy import plot_trace_highs as pth + +REACTION_IDS = ["X", "Y", "Z"] +RXN_INDEX = {"X": 0, "Y": 1, "Z": 2} +COL_LOWER = [0.0, 0.0, 0.0] +COL_UPPER = [10.0, 10.0, 10.0] + + +def expected_area(z): + C = 15.0 - z + if C <= 0: + return 0.0 + if C <= 10: + return C ** 2 / 2.0 + return 100.0 - (20.0 - C) ** 2 / 2.0 + + +def toy_lp(num_fix_slots=1): + S = sp.lil_matrix((0, 3)) + extra_rows = [({0: 1.0, 1: 1.0, 2: 1.0}, float("-inf"), 15.0)] # X+Y+Z <= 15 + return hlc._build_rate_lp_arrays(S, COL_LOWER, COL_UPPER, extra_rows=extra_rows, + num_fix_slots=num_fix_slots) + + +class TestSlicePolygon(unittest.TestCase): + def test_sequential_matches_formula(self): + lp, fix_rows = toy_lp() + z_values = [1.0, 3.0, 5.0, 7.0, 9.0] + results = pth.trace_3d_slice_polygon_sequential( + lp, REACTION_IDS, RXN_INDEX, fix_rows, + pth.fix_rate_axis({"Z": 1.0}), {"X": 1.0}, {"Y": 1.0}, z_values) + self.assertEqual([z for z, _ in results], z_values) # order preserved + for z, polygon in results: + area = pth.polygon_area(polygon) + self.assertAlmostEqual(area, expected_area(z), places=3, msg=f"z={z}, polygon={polygon}") + + def test_parallel_matches_formula_and_sequential(self): + lp, fix_rows = toy_lp() + z_values = [0.5, 2.0, 4.0, 6.0, 8.0, 9.5] + seq = pth.trace_3d_slice_polygon_sequential( + lp, REACTION_IDS, RXN_INDEX, fix_rows, + pth.fix_rate_axis({"Z": 1.0}), {"X": 1.0}, {"Y": 1.0}, z_values) + par = pth.trace_3d_slice_polygon( + lp, REACTION_IDS, RXN_INDEX, fix_rows, + pth.fix_rate_axis({"Z": 1.0}), {"X": 1.0}, {"Y": 1.0}, z_values, n_workers=3) + self.assertEqual([z for z, _ in par], z_values) + for (z_s, poly_s), (z_p, poly_p) in zip(seq, par): + self.assertEqual(z_s, z_p) + area_s, area_p = pth.polygon_area(poly_s), pth.polygon_area(poly_p) + self.assertAlmostEqual(area_p, expected_area(z_p), places=3) + self.assertAlmostEqual(area_s, area_p, places=6, msg=f"z={z_s}: seq={poly_s} par={poly_p}") + + def test_fix_yield_axis_formula(self): + # pure arithmetic check of fix_yield_axis's (coeffs, rhs) output + # against the merged = num - value*den formula directly, since + # exercising it through an actual yield-ratio LP needs a 4th + # reaction to serve as a real denominator flux -- not worth the + # extra toy-network complexity when the formula itself is a + # direct, easily-checked port of lptools._make_fix_constraint's + # 'yield' branch. + make_fix = pth.fix_yield_axis({"P": 2.0}, {"S": -3.0}) + coeffs, rhs = make_fix(0.5) + self.assertAlmostEqual(coeffs["P"], 2.0) + self.assertAlmostEqual(coeffs["S"], 1.5) # 0 - 0.5*(-3.0) + self.assertEqual(rhs, 0.0) + + coeffs0, rhs0 = make_fix(0.0) + self.assertAlmostEqual(coeffs0["P"], 2.0) + self.assertAlmostEqual(coeffs0.get("S", 0.0), 0.0) + self.assertEqual(rhs0, 0.0) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_trace_boundary_adaptive.py b/cnapy/tests/test_trace_boundary_adaptive.py new file mode 100644 index 00000000..dc059f39 --- /dev/null +++ b/cnapy/tests/test_trace_boundary_adaptive.py @@ -0,0 +1,174 @@ +"""Tests trace_boundary_adaptive (RateAxis / YieldAxis) against +hand-derivable boundaries. + +Toy network: X, P in [0,10], S in [1,10] (S never 0, sidesteps +denominator-sign edge cases), constraint X + P <= 12. + +Case A (flat sanity check): x-axis = X (rate), y-axis = P/S (yield), +*without* the X+P<=12 constraint -- X and P/S are fully decoupled, so +the true boundary is exactly flat: upper y = 10 (P=10, S=1) and +lower y = 0 (P=0) for every x in [0,10]. A correct adaptive tracer +should find this with no extra refinement points, since linear +interpolation between the two endpoints is already exact everywhere. + +Case B (real curvature): same axes, *with* X+P<=12 active. Now +P_max(x) = min(10, 12-x), giving a piecewise-linear upper boundary +with a kink at x=2 (10 for x<=2, 12-x for x>2) that pure endpoint +interpolation would get wrong in the middle -- forcing the tracer to +actually refine and find the kink. Lower boundary stays flat at 0 +(P can always be driven to 0 regardless of x, since X<=10<12). +""" + +import unittest + +import scipy.sparse as sp + + +from cnapy import highs_lp_cache as hlc +from cnapy import plot_trace_highs as pth + +REACTION_IDS = ["X", "P", "S"] +RXN_INDEX = {"X": 0, "P": 1, "S": 2} +COL_LOWER = [0.0, 0.0, 1.0] +COL_UPPER = [10.0, 10.0, 10.0] + + +def build_solvers(with_budget_constraint: bool): + Smat = sp.lil_matrix((0, 3)) + rate_extra = [({0: 1.0, 1: 1.0}, float("-inf"), 12.0)] if with_budget_constraint else [] + yield_extra = [({0: 1.0, 1: 1.0}, "<=", 12.0)] if with_budget_constraint else [] + + rate_lp, rate_fix_rows = hlc._build_rate_lp_arrays( + Smat, COL_LOWER, COL_UPPER, extra_rows=rate_extra, num_fix_slots=1) + rate_solver = hlc.HighsRateSolver(rate_lp, REACTION_IDS, RXN_INDEX, rate_fix_rows) + + yield_lp, den_row, t_col, yield_fix_rows = hlc._build_yopt_lp_arrays( + Smat, COL_LOWER, COL_UPPER, den_coeffs={2: 1.0}, extra_rows=yield_extra, num_fix_slots=1) + yield_solver = hlc.HighsYieldSolver(yield_lp, REACTION_IDS, RXN_INDEX, den_row, t_col, yield_fix_rows) + + return rate_solver, yield_solver + + +class TestBoundaryAdaptive(unittest.TestCase): + def test_flat_boundary_no_coupling(self): + rate_solver, yield_solver = build_solvers(with_budget_constraint=False) + ax0 = pth.RateAxis(rate_solver, {"X": 1.0}) + ax1 = pth.YieldAxis(rate_solver, yield_solver, num={"P": 1.0}, den={"S": 1.0}) + + upper, lower = pth.trace_boundary_adaptive(rate_solver, ax0, ax1) + + self.assertTrue(len(upper) >= 2 and len(lower) >= 2) + for x, y in upper: + self.assertAlmostEqual(y, 10.0, places=5, msg=f"upper at x={x}") + for x, y in lower: + self.assertAlmostEqual(y, 0.0, places=5, msg=f"lower at x={x}") + xs_upper = [x for x, _ in upper] + self.assertAlmostEqual(min(xs_upper), 0.0, places=5) + self.assertAlmostEqual(max(xs_upper), 10.0, places=5) + + def test_kinked_upper_boundary_matches_formula(self): + rate_solver, yield_solver = build_solvers(with_budget_constraint=True) + ax0 = pth.RateAxis(rate_solver, {"X": 1.0}) + ax1 = pth.YieldAxis(rate_solver, yield_solver, num={"P": 1.0}, den={"S": 1.0}) + + upper, lower = pth.trace_boundary_adaptive(rate_solver, ax0, ax1) + + for x, y in upper: + expected = min(10.0, 12.0 - x) + self.assertAlmostEqual(y, expected, places=4, msg=f"upper at x={x}: got {y}, expected {expected}") + for x, y in lower: + self.assertAlmostEqual(y, 0.0, places=5, msg=f"lower at x={x}") + + # the kink at x=2 should actually have been found, not smoothed over + xs_upper = sorted(x for x, _ in upper) + self.assertTrue(any(abs(x - 2.0) < 0.5 for x in xs_upper), + msg=f"expected a refinement point near the x=2 kink, got x values {xs_upper}") + + def test_yield_yield_case(self): + # both axes yield, sharing the same denominator S: x = X/S, y = P/S, + # with X+P<=12 still active. At any achievable x = X/S, P_max is + # still capped by X+P<=12 i.e. P <= 12-X = 12-x*S, so y_max = + # (12-x*S)/S = 12/S - x, S in [1,10] -- tightest (smallest) cap is + # at S=10: y_max <= 1.2-x. But P is also capped at 10 directly + # (P<=10/S i.e. y<=10/S, loosest at S=1: y<=10). The achievable + # upper boundary is whichever of these actually binds; rather than + # re-deriving the exact piecewise formula by hand, cross-check + # each returned point by directly re-solving the same LP (max P + # s.t. S=1, X = x*1) via the rate solver -- an independent + # reference computed a different way. + rate_solver, yield_solver_x = None, None + Smat = sp.lil_matrix((0, 3)) + extra = [({0: 1.0, 1: 1.0}, float("-inf"), 12.0)] + extra_y = [({0: 1.0, 1: 1.0}, "<=", 12.0)] + rate_lp, rate_fix_rows = hlc._build_rate_lp_arrays( + Smat, COL_LOWER, COL_UPPER, extra_rows=extra, num_fix_slots=1) + rate_solver = hlc.HighsRateSolver(rate_lp, REACTION_IDS, RXN_INDEX, rate_fix_rows) + + yield_lp_x, den_row_x, t_col_x, fix_x = hlc._build_yopt_lp_arrays( + Smat, COL_LOWER, COL_UPPER, den_coeffs={2: 1.0}, extra_rows=extra_y, num_fix_slots=1) + yield_solver_x = hlc.HighsYieldSolver(yield_lp_x, REACTION_IDS, RXN_INDEX, den_row_x, t_col_x, fix_x) + + yield_lp_y, den_row_y, t_col_y, fix_y = hlc._build_yopt_lp_arrays( + Smat, COL_LOWER, COL_UPPER, den_coeffs={2: 1.0}, extra_rows=extra_y, num_fix_slots=1) + yield_solver_y = hlc.HighsYieldSolver(yield_lp_y, REACTION_IDS, RXN_INDEX, den_row_y, t_col_y, fix_y) + + ax0 = pth.YieldAxis(rate_solver, yield_solver_x, num={"X": 1.0}, den={"S": 1.0}) + ax1 = pth.YieldAxis(rate_solver, yield_solver_y, num={"P": 1.0}, den={"S": 1.0}) + + upper, lower = pth.trace_boundary_adaptive(rate_solver, ax0, ax1) + self.assertTrue(len(upper) >= 2) + + # independent cross-check: at each returned x, pin X = x*S with + # S free in [1,10] (i.e. pin X - x*S = 0 directly on the rate + # solver) and maximize P/S the *same way* (a second, independently + # built yield solver with a fresh fix row), then compare. + check_lp, check_den_row, check_t_col, check_fix = hlc._build_yopt_lp_arrays( + Smat, COL_LOWER, COL_UPPER, den_coeffs={2: 1.0}, extra_rows=extra_y, num_fix_slots=1) + check_solver = hlc.HighsYieldSolver(check_lp, REACTION_IDS, RXN_INDEX, check_den_row, check_t_col, check_fix) + check_rate = hlc.HighsRateSolver(rate_lp, REACTION_IDS, RXN_INDEX, rate_fix_rows) + for x, y in upper: + coeffs, rhs = pth.fix_yield_axis({"X": 1.0}, {"S": 1.0})(x) + check_rate.set_fix_row(0, coeffs, rhs) + check_solver.set_fix_row(0, coeffs, rhs) + check_ax1 = pth.YieldAxis(check_rate, check_solver, num={"P": 1.0}, den={"S": 1.0}) + expected = check_ax1.optimize("maximize") + self.assertIsNotNone(expected, msg=f"reference solve infeasible at x={x}") + self.assertAlmostEqual(y, expected, places=4, msg=f"upper at x={x}") + + + def test_repeated_calls_on_same_solvers_give_identical_results(self): + # Regression test for a real bug found via real-model benchmarking: + # calling trace_boundary_adaptive() more than once on the *same* + # rate_solver/axis objects (e.g. benchmarking with several + # repeats, or any caller that reuses solvers across multiple + # traces) used to silently inherit whatever fix-row value the + # previous call's deepest refinement probe left behind, since + # nothing reset it at the start of the next call -- constraining + # x_min/x_max to something wrong from the very first step of + # every call after the first. trace_boundary_adaptive now clears + # its own fix state up front specifically to rule this out. + rate_solver, yield_solver = build_solvers(with_budget_constraint=True) + ax0 = pth.RateAxis(rate_solver, {"X": 1.0}) + ax1 = pth.YieldAxis(rate_solver, yield_solver, num={"P": 1.0}, den={"S": 1.0}) + + results = [pth.trace_boundary_adaptive(rate_solver, ax0, ax1) for _ in range(4)] + first = results[0] + for i, (upper, lower) in enumerate(results[1:], start=1): + self.assertEqual(len(upper), len(first[0]), msg=f"run {i}: upper point count changed") + self.assertEqual(len(lower), len(first[1]), msg=f"run {i}: lower point count changed") + for (x0, y0), (x1, y1) in zip(sorted(upper), sorted(first[0])): + self.assertAlmostEqual(x0, x1, places=6, msg=f"run {i}") + self.assertAlmostEqual(y0, y1, places=6, msg=f"run {i}") + + + def test_mismatched_solver_raises(self): + rate_solver, yield_solver = build_solvers(with_budget_constraint=True) + other_rate_solver, _ = build_solvers(with_budget_constraint=True) # a different instance + ax0 = pth.RateAxis(other_rate_solver, {"X": 1.0}) # built against the WRONG solver + ax1 = pth.YieldAxis(rate_solver, yield_solver, num={"P": 1.0}, den={"S": 1.0}) + with self.assertRaises(ValueError): + pth.trace_boundary_adaptive(rate_solver, ax0, ax1) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_trace_grid_3d.py b/cnapy/tests/test_trace_grid_3d.py new file mode 100644 index 00000000..68a57263 --- /dev/null +++ b/cnapy/tests/test_trace_grid_3d.py @@ -0,0 +1,195 @@ +"""Tests trace_grid_3d / GridAxis. + +Reference shape: the 10x10x10 cube with one corner cut off +(X, Y, Z in [0,10], X+Y+Z <= 25), whose exact volume 1000 - 125/6 = +979.1667 is already verified independently in test_trace_polytope_3d.py. + +Two ways of reusing it: + * all three axes RATE: the grid tracer's convex-hull volume should + match 979.1667. It's an approximate method in general, but for a + convex polytope whose corner x-values (0, 5, 10) land exactly on the + scan grid, the sampled boundary points include every true vertex, + so the match is essentially exact -- and tight enough that a + collapsed or partial mesh can't hide. + * Y and Z as YIELDS over a denominator S pinned to a constant (bounds + [5,5]): Y/S = Y/5 and Z/5 are then plain linear rescalings, so the + expected hull volume is 979.1667 / 25 = 39.1667. + +(An earlier version of this file checked only feasibility and "reaches +> 1.5 at x=0", and passed while a stale-fix-slot bug had collapsed every +x-slice after the first to a single point. These tests are written to +fail on exactly that.) +""" + +import unittest + +import numpy as np +import scipy.sparse as sp +from scipy.spatial import ConvexHull + + +from cnapy import highs_lp_cache as hlc +from cnapy import plot_trace_highs as pth + +REACTION_IDS = ["X", "Y", "Z", "S"] +RXN_INDEX = {"X": 0, "Y": 1, "Z": 2, "S": 3} +EXACT_VOLUME = 1000.0 - 125.0 / 6.0 + + +def _rate_only_axes(): + col_lower = [0.0, 0.0, 0.0, 5.0] + col_upper = [10.0, 10.0, 10.0, 5.0] + lp, fix_rows = hlc._build_rate_lp_arrays( + sp.lil_matrix((0, 4)), col_lower, col_upper, + extra_rows=[({0: 1.0, 1: 1.0, 2: 1.0}, float("-inf"), 25.0)], num_fix_slots=2) + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + return (pth.GridAxis("rate", solver, {"X": 1.0}), + pth.GridAxis("rate", solver, {"Y": 1.0}), + pth.GridAxis("rate", solver, {"Z": 1.0})) + + +def _yield_axes(num_fix_slots_ax1=1, num_fix_slots_ax2=2): + col_lower = [0.0, 0.0, 0.0, 5.0] + col_upper = [10.0, 10.0, 10.0, 5.0] # S pinned to a constant via its bounds + Smat = sp.lil_matrix((0, 4)) + rate_lp, rate_fix = hlc._build_rate_lp_arrays( + Smat, col_lower, col_upper, + extra_rows=[({0: 1.0, 1: 1.0, 2: 1.0}, float("-inf"), 25.0)], num_fix_slots=2) + rate_solver = hlc.HighsRateSolver(rate_lp, REACTION_IDS, RXN_INDEX, rate_fix) + + def yield_solver(slots): + lp, den_row, t_col, fix_rows = hlc._build_yopt_lp_arrays( + Smat, col_lower, col_upper, den_coeffs={3: 1.0}, + extra_rows=[({0: 1.0, 1: 1.0, 2: 1.0}, "<=", 25.0)], num_fix_slots=slots) + return hlc.HighsYieldSolver(lp, REACTION_IDS, RXN_INDEX, den_row, t_col, fix_rows) + + ax0 = pth.GridAxis("rate", rate_solver, {"X": 1.0}) + # ax1 = Y/S only ever needs slot 0 (pinning ax0); ax2 = Z/S needs 0 AND 1 + ax1 = pth.GridAxis("yield", rate_solver, {"Y": 1.0}, den={"S": 1.0}, + yield_solver=yield_solver(num_fix_slots_ax1)) + ax2 = pth.GridAxis("yield", rate_solver, {"Z": 1.0}, den={"S": 1.0}, + yield_solver=yield_solver(num_fix_slots_ax2)) + return ax0, ax1, ax2 + + +def _hull_volume(datapoints): + return ConvexHull(np.array(datapoints)).volume + + +class TestGrid3DRateOnly(unittest.TestCase): + def test_volume_matches_exact_polytope(self): + for points in (5, 9, 17): + ax0, ax1, ax2 = _rate_only_axes() + dp, triang = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 10.0, points=points) + self.assertAlmostEqual(_hull_volume(dp), EXACT_VOLUME, places=3, + msg=f"points={points}") + + def test_every_x_slice_spans_full_y_range(self): + # Regression test for the stale-slot bug: after the first + # x-slice, ax1's range used to collapse to the previous slice's + # last y_val. At every x in [0,10], y should span all of [0,10]. + ax0, ax1, ax2 = _rate_only_axes() + dp, _ = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 10.0, points=9) + by_x = {} + for x, y, z in dp: + by_x.setdefault(round(x, 6), []).append(y) + self.assertEqual(len(by_x), 9) + for x, ys in by_x.items(): + self.assertAlmostEqual(min(ys), 0.0, places=6, msg=f"x={x}") + self.assertAlmostEqual(max(ys), 10.0, places=6, msg=f"x={x}") + + def test_repeated_calls_on_same_solvers_give_identical_results(self): + ax0, ax1, ax2 = _rate_only_axes() + runs = [pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 10.0, points=5) for _ in range(3)] + for dp, triang in runs[1:]: + self.assertEqual(len(dp), len(runs[0][0])) + self.assertEqual(triang, runs[0][1]) + for a, b in zip(dp, runs[0][0]): + for u, v in zip(a, b): + self.assertAlmostEqual(u, v, places=6) + + def test_triangles_reference_valid_points(self): + ax0, ax1, ax2 = _rate_only_axes() + dp, triang = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 10.0, points=5) + self.assertTrue(triang) + self.assertLess(max(i for tri in triang for i in tri), len(dp)) + self.assertGreaterEqual(min(i for tri in triang for i in tri), 0) + + +class TestGrid3DWithYields(unittest.TestCase): + def test_volume_matches_rescaled_exact_polytope(self): + ax0, ax1, ax2 = _yield_axes() + dp, triang = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 2.0, points=9) + self.assertAlmostEqual(_hull_volume(dp), EXACT_VOLUME / 25.0, places=3) + + def test_points_are_feasible_in_original_coordinates(self): + ax0, ax1, ax2 = _yield_axes() + dp, _ = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 2.0, points=9) + for x, y, z in dp: + self.assertGreaterEqual(y, -1e-6) + self.assertLessEqual(y, 2.0 + 1e-6) + self.assertGreaterEqual(z, -1e-6) + self.assertLessEqual(z, 2.0 + 1e-6) + self.assertLessEqual(x + 5 * y + 5 * z, 25.0 + 1e-3) + + def test_repeated_calls_on_same_solvers_give_identical_results(self): + ax0, ax1, ax2 = _yield_axes() + first = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 2.0, points=5) + for _ in range(2): + again = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 2.0, points=5) + self.assertEqual(len(again[0]), len(first[0])) + for a, b in zip(again[0], first[0]): + for u, v in zip(a, b): + self.assertAlmostEqual(u, v, places=6) + + def test_missing_fix_slot_fails_loudly(self): + # ax2 (needs slots 0 AND 1) built with only one slot: pinning + # ax1's value on it would be silently skipped if this weren't an + # error, producing a plausible-looking wrong mesh. + ax0, ax1, ax2 = _yield_axes(num_fix_slots_ax2=1) + with self.assertRaises(ValueError): + pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 2.0, points=5) + + + +class TestGridThreaded(unittest.TestCase): + """Threaded scan (n_workers > 1 with a make_axes factory giving each + worker thread its own solvers) must give the sequential result, x + order included.""" + + def _check(self, factory, y_range, points): + ax0, ax1, ax2 = factory() + seq = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), y_range, points=points) + made = [] + + def make_axes(): + axs = factory() + made.append(axs) + return axs + for w in (2, 3, 16): + ax0, ax1, ax2 = factory() + par = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), y_range, points=points, + n_workers=w, make_axes=make_axes) + # samples identical and in the same order; triangle indices + # aren't compared (equal-length cell diagonals are a tie that + # rounding noise can break either way), the hull volume is + np.testing.assert_allclose(np.array(par[0]), np.array(seq[0]), atol=1e-9) + self.assertEqual(len(par[1]), len(seq[1])) + self.assertAlmostEqual(_hull_volume(par[0]), _hull_volume(seq[0]), places=6) + self.assertTrue(made) # the factory was really used + + def test_rate_axes(self): + self._check(_rate_only_axes, 10.0, 9) + + def test_yield_axes(self): + self._check(_yield_axes, 2.0, 9) + + def test_without_factory_runs_sequentially(self): + ax0, ax1, ax2 = _rate_only_axes() + seq = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 10.0, points=5) + ax0, ax1, ax2 = _rate_only_axes() + par = pth.trace_grid_3d(ax0, ax1, ax2, (0.0, 10.0), 10.0, points=5, n_workers=4) + self.assertEqual(par, seq) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/tests/test_trace_polytope_3d.py b/cnapy/tests/test_trace_polytope_3d.py new file mode 100644 index 00000000..2a4af4c1 --- /dev/null +++ b/cnapy/tests/test_trace_polytope_3d.py @@ -0,0 +1,118 @@ +"""Tests trace_polytope_3d_rate / trace_polytope_3d_rate_parallel against +a hand-derivable shape: a 10x10x10 cube with one corner cut off. + + X, Y, Z in [0, 10], X + Y + Z <= 25 + +The cube alone has volume 1000 and 8 corners. The plane X+Y+Z=25 slices +through the single corner (10,10,10) (since 10+10+10=30 > 25), cutting +it into a triangular face and replacing that one vertex with three new +ones along the edges leading to it: + + edge (10,10,10)-(0,10,10): X+20=25 -> X=5 -> (5,10,10) + edge (10,10,10)-(10,0,10): Y+20=25 -> Y=5 -> (10,5,10) + edge (10,10,10)-(10,10,0): Z+20=25 -> Z=5 -> (10,10,5) + +giving 10 vertices total (7 original cube corners + 3 new ones). The +cut-off piece is a right tetrahedron with three mutually perpendicular +edges of length 5 from (10,10,10), volume = 5*5*5/6 = 125/6, so the +remaining polytope's volume is 1000 - 125/6 = 5875/6 ~= 979.1667. +""" + +import unittest + +import numpy as np +import scipy.sparse as sp +from scipy.spatial import ConvexHull + + +from cnapy import highs_lp_cache as hlc +from cnapy import plot_trace_highs as pth + +REACTION_IDS = ["X", "Y", "Z"] +RXN_INDEX = {"X": 0, "Y": 1, "Z": 2} +COL_LOWER = [0.0, 0.0, 0.0] +COL_UPPER = [10.0, 10.0, 10.0] + +EXPECTED_VERTICES = { + (0.0, 0.0, 0.0), (10.0, 0.0, 0.0), (0.0, 10.0, 0.0), (0.0, 0.0, 10.0), + (10.0, 10.0, 0.0), (10.0, 0.0, 10.0), (0.0, 10.0, 10.0), + (5.0, 10.0, 10.0), (10.0, 5.0, 10.0), (10.0, 10.0, 5.0), +} +EXPECTED_VOLUME = 1000.0 - 125.0 / 6.0 + + +def cut_cube_lp(num_fix_slots=0): + Smat = sp.lil_matrix((0, 3)) + extra_rows = [({0: 1.0, 1: 1.0, 2: 1.0}, float("-inf"), 25.0)] # X+Y+Z <= 25 + return hlc._build_rate_lp_arrays(Smat, COL_LOWER, COL_UPPER, extra_rows=extra_rows, + num_fix_slots=num_fix_slots) + + +AX0, AX1, AX2 = {"X": 1.0}, {"Y": 1.0}, {"Z": 1.0} + + +class TestPolytope3D(unittest.TestCase): + def test_sequential_finds_exact_vertex_set(self): + lp, fix_rows = cut_cube_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices, triang, face_polys = pth.trace_polytope_3d_rate(solver, AX0, AX1, AX2) + got = {tuple(round(c, 6) for c in v) for v in vertices} + self.assertEqual(got, EXPECTED_VERTICES, msg=f"vertices={vertices}") + + def test_sequential_volume_matches_formula(self): + lp, fix_rows = cut_cube_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices, triang, face_polys = pth.trace_polytope_3d_rate(solver, AX0, AX1, AX2) + hull = ConvexHull(np.array(vertices)) + self.assertAlmostEqual(hull.volume, EXPECTED_VOLUME, places=4) + + def test_parallel_matches_sequential(self): + lp, fix_rows = cut_cube_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + seq_vertices, _, _ = pth.trace_polytope_3d_rate(solver, AX0, AX1, AX2) + + par_vertices, par_triang, par_face_polys = pth.trace_polytope_3d_rate_parallel( + lp, REACTION_IDS, RXN_INDEX, AX0, AX1, AX2, n_workers=3) + + seq_set = {tuple(round(c, 6) for c in v) for v in seq_vertices} + par_set = {tuple(round(c, 6) for c in v) for v in par_vertices} + self.assertEqual(seq_set, par_set, msg=f"seq={seq_vertices}\npar={par_vertices}") + + hull_par = ConvexHull(np.array(par_vertices)) + self.assertAlmostEqual(hull_par.volume, EXPECTED_VOLUME, places=4) + + def test_triangulation_and_face_polygons_cover_all_vertices(self): + lp, fix_rows = cut_cube_lp() + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices, triang, face_polys = pth.trace_polytope_3d_rate(solver, AX0, AX1, AX2) + + self.assertTrue(len(triang) > 0) + tri_vertex_ids = set(i for tri in triang for i in tri) + self.assertEqual(tri_vertex_ids, set(range(len(vertices)))) + + self.assertIsNotNone(face_polys) + # a cut cube has 6 faces (3 full squares, 3 pentagons where the + # corner was cut) + 1 new triangular face from the cut = 7 faces + self.assertEqual(len(face_polys), 7, msg=f"face_polys={face_polys}") + face_vertex_ids = set(i for face in face_polys for i in face) + self.assertEqual(face_vertex_ids, set(range(len(vertices)))) + # every merged face should be a simple polygon with no repeats + for face in face_polys: + self.assertEqual(len(face), len(set(face)), msg=f"face={face}") + + def test_degenerate_too_few_vertices(self): + # a single point: pin all three axes to force volume 0 + Smat = sp.lil_matrix((0, 3)) + extra_rows = [ + ({0: 1.0}, 3.0, 3.0), ({1: 1.0}, 3.0, 3.0), ({2: 1.0}, 3.0, 3.0), + ] + lp, fix_rows = hlc._build_rate_lp_arrays(Smat, COL_LOWER, COL_UPPER, extra_rows=extra_rows) + solver = hlc.HighsRateSolver(lp, REACTION_IDS, RXN_INDEX, fix_rows) + vertices, triang, face_polys = pth.trace_polytope_3d_rate(solver, AX0, AX1, AX2) + self.assertEqual(len(vertices), 1) + self.assertEqual(triang, []) + self.assertIsNone(face_polys) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/cnapy/utils.py b/cnapy/utils.py index c48f1c16..1bcdce3b 100644 --- a/cnapy/utils.py +++ b/cnapy/utils.py @@ -12,6 +12,10 @@ def format_scenario_constraint(constraint): def update_selected(string: str, with_annotations: bool, model_elements, element_list): + ''' element_list is expected to be a QTableWidget whose column 0 holds the + element's id in each row. ''' + row_count = element_list.rowCount() + element_list.setUpdatesEnabled(False) if len(string) >= 2: regex = re.compile(".*".join(map(re.escape, string.split("*"))), re.IGNORECASE) found_ids = [] @@ -20,25 +24,20 @@ def update_selected(string: str, with_annotations: bool, model_elements, element (any(regex.search(x) for x in el.annotation.keys()) or any(regex.search(str(x)) for x in el.annotation.values()))): found_ids.append(el.id) - root = element_list.invisibleRootItem() - child_count = root.childCount() - for i in range(child_count): - item = root.child(i) - item.setHidden(True) - - for found_id in found_ids: - for item in element_list.findItems(found_id, Qt.MatchExactly, 0): - item.setHidden(False) + found_id_set = set(found_ids) + for row in range(row_count): + id_item = element_list.item(row, 0) + is_found = id_item is not None and id_item.text() in found_id_set + element_list.setRowHidden(row, not is_found) else: found_ids = [x.id for x in model_elements] - root = element_list.invisibleRootItem() - for child_counter in range(root.childCount()): - root.child(child_counter).setHidden(False) + for row in range(row_count): + element_list.setRowHidden(row, False) current_item = element_list.currentItem() - if current_item is not None and not current_item.isHidden(): + if current_item is not None and not element_list.isRowHidden(current_item.row()): element_list.scrollToItem(current_item) - + element_list.setUpdatesEnabled(True) return found_ids @@ -62,21 +61,21 @@ def FONT_COLOR(HEX): # string that defines style sheet for changing the color o def show_unknown_error_box(exstr): msgBox = QMessageBox() msgBox.setWindowTitle("Unknown Error!") - msgBox.setTextFormat(Qt.RichText) + msgBox.setTextFormat(Qt.TextFormat.RichText) msgBox.setText( f"

{exstr}

Please report the problem to:

"+\ "

"+\ "https://github.com/cnapy-org/CNApy/issues

" ) - msgBox.setIcon(QMessageBox.Warning) + msgBox.setIcon(QMessageBox.Icon.Warning) msgBox.exec() def turn_red(item): palette = item.palette() role = item.foregroundRole() - palette.setColor(role, Qt.black) + palette.setColor(role, Qt.GlobalColor.black) item.setPalette(palette) item.setStyleSheet("background: #ff9999") @@ -129,7 +128,7 @@ class QComplReceivLineEdit(QLineEdit): def __init__(self, parent, wordlist, is_in_dark_mode: bool = False, check=True, is_constr=False, reject_empty_string=True): super().__init__("", parent) self.completer: QCompleter = QCompleter() - self.completer.setCaseSensitivity(Qt.CaseInsensitive) + self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive) if isinstance(wordlist, IDList): self.wordlist: list = wordlist.id_list self.completer.setModel(wordlist.ids_model) @@ -228,7 +227,7 @@ def __init__(self, *args, **kwargs): def keyPressEvent(self, event): super().keyPressEvent(event) - if event.key() == Qt.Key_C and (event.modifiers() & Qt.ControlModifier): + if event.key() == Qt.Key.Key_C and (event.modifiers() & Qt.KeyboardModifier.ControlModifier): copied_cells = sorted(self.selectedIndexes()) copy_text = '' max_column = copied_cells[-1].column() @@ -249,23 +248,23 @@ def __init__(self, *args, **kwargs): def setEditable(self, b): f = self.flags() if b: - self.setFlags(f | Qt.ItemIsEditable) + self.setFlags(f | Qt.ItemFlag.ItemIsEditable) else: - self.setFlags(f & ~Qt.ItemIsEditable) + self.setFlags(f & ~Qt.ItemFlag.ItemIsEditable) def setSelectable(self, b): f = self.flags() if b: - self.setFlags(f | Qt.ItemIsSelectable) + self.setFlags(f | Qt.ItemFlag.ItemIsSelectable) else: - self.setFlags(f & ~Qt.ItemIsSelectable) + self.setFlags(f & ~Qt.ItemFlag.ItemIsSelectable) def setEnabled(self, b): f = self.flags() if b: - self.setFlags(f | Qt.ItemIsEnabled) + self.setFlags(f | Qt.ItemFlag.ItemIsEnabled) else: - self.setFlags(f & ~Qt.ItemIsEnabled) + self.setFlags(f & ~Qt.ItemFlag.ItemIsEnabled) class QHSeperationLine(QFrame): @@ -277,9 +276,9 @@ def __init__(self): super().__init__() self.setMinimumWidth(1) self.setFixedHeight(20) - self.setFrameShape(QFrame.HLine) - self.setFrameShadow(QFrame.Sunken) - self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum) + self.setFrameShape(QFrame.Shape.HLine) + self.setFrameShadow(QFrame.Shadow.Sunken) + self.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Minimum) return @@ -292,7 +291,7 @@ def __init__(self): super().__init__() self.setFixedWidth(20) self.setMinimumHeight(1) - self.setFrameShape(QFrame.VLine) - self.setFrameShadow(QFrame.Sunken) - self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred) + self.setFrameShape(QFrame.Shape.VLine) + self.setFrameShadow(QFrame.Shadow.Sunken) + self.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Preferred) return diff --git a/cnapy/utils_for_cnapy_api.py b/cnapy/utils_for_cnapy_api.py index 5103869b..4b40fadb 100644 --- a/cnapy/utils_for_cnapy_api.py +++ b/cnapy/utils_for_cnapy_api.py @@ -14,7 +14,7 @@ class IdentifiersOrgResult: def check_in_identifiers_org(widget: QTreeWidgetItem): - widget.setCursor(Qt.BusyCursor) + widget.setCursor(Qt.CursorShape.BusyCursor) rows = widget.annotation_widget.annotation.rowCount() invalid_red = QColor(255, 0, 0) for i in range(0, rows): @@ -42,9 +42,9 @@ def check_in_identifiers_org(widget: QTreeWidgetItem): if identifiers_org_result.connection_error: msgBox = QMessageBox() msgBox.setWindowTitle("Connection error!") - msgBox.setTextFormat(Qt.RichText) + msgBox.setTextFormat(Qt.TextFormat.RichText) msgBox.setText("

identifiers.org could not be accessed. Either the internet connection isn't working or the server is currently down.

") - msgBox.setIcon(QMessageBox.Warning) + msgBox.setIcon(QMessageBox.Icon.Warning) msgBox.exec() break @@ -61,7 +61,7 @@ def check_in_identifiers_org(widget: QTreeWidgetItem): if not identifiers_org_result.is_key_value_pair_valid: break - widget.setCursor(Qt.ArrowCursor) + widget.setCursor(Qt.CursorShape.ArrowCursor) def check_identifiers_org_entry(key: str, value: str) -> IdentifiersOrgResult: diff --git a/environment.yml b/environment.yml index 9b73c6e2..4e01c87f 100644 --- a/environment.yml +++ b/environment.yml @@ -13,11 +13,12 @@ dependencies: - matplotlib-base>=3.6 - pip=23 - python>=3.10,<3.14 - - qtpy>=2.3 - - pyqtwebengine>=5.15 + - qtpy>=2.4 + - pyqt=6.8 + - pyqtwebengine=6.8 - appdirs>=1.4 - cobra>=0.30 - - qtconsole=5.4 + - qtconsole>=5.6 - requests>=2.28 - psutil>=5.9 - efmtool_link>=0.0.8 diff --git a/installers/install_cnapy_here.bat b/installers/install_cnapy_here.bat index 51d06374..1cef640b 100644 --- a/installers/install_cnapy_here.bat +++ b/installers/install_cnapy_here.bat @@ -1,47 +1,156 @@ @echo off setlocal +:: Optional first argument: a git branch of https://github.com/cnapy-org/CNApy to install +:: instead of the pinned PyPI release. Example: install_cnapy_here.bat cnapy2 +set "BRANCH=%~1" + +if /i "%BRANCH%"=="-h" goto :usage +if /i "%BRANCH%"=="--help" goto :usage +if /i "%BRANCH%"=="/?" goto :usage +goto :afterusage + +:usage +echo Usage: %~n0.bat [branch] +echo branch Optional. A branch of https://github.com/cnapy-org/CNApy to install +echo instead of the latest release from PyPI, e.g. "cnapy2". +exit /b 0 + +:afterusage + +if defined BRANCH ( + where git >nul 2>nul + if errorlevel 1 ( + echo ERROR: installing from a specific branch requires Git to be installed and available on PATH. + echo Download Git for Windows from https://git-scm.com/download/win and try again. + pause + exit /b 1 + ) + echo Checking that branch "%BRANCH%" exists in https://github.com/cnapy-org/CNApy.git ... + git ls-remote --exit-code --heads https://github.com/cnapy-org/CNApy.git "%BRANCH%" >nul 2>nul + if errorlevel 1 ( + echo ERROR: branch "%BRANCH%" was not found in https://github.com/cnapy-org/CNApy.git + pause + exit /b 1 + ) + set "INSTALL_LABEL=%BRANCH:/=-%" +) else ( + set "INSTALL_LABEL=1.2.8" +) + :: Set the PowerShell script file name set "psFile=install_cnapy.ps1" -:: Write the PowerShell script to a file -echo # Adapted from https://raw.githubusercontent.com/mamba-org/micromamba-releases/main/install.ps1 > "%psFile%" +:: Write the PowerShell script to a file. +:: +:: Uses the Miniforge installer (https://github.com/conda-forge/miniforge) instead of a bare +:: micromamba.exe. Downloading a standalone, unsigned exe and immediately running it is exactly +:: the pattern many corporate EDR/AV products silently block. The Miniforge installer is signed +:: and still bundles mamba, so environment creation stays fast. +echo $ErrorActionPreference = "Stop" > "%psFile%" echo. >> "%psFile%" -echo $CNAPY_VERSION = "1.2.8" ^# Replace with the actual version if needed >> "%psFile%" -echo $RELEASE_URL="https://github.com/mamba-org/micromamba-releases/releases/latest/download/micromamba-win-64" >> "%psFile%" +echo function Invoke-WithRetry { >> "%psFile%" +echo param( >> "%psFile%" +echo [ScriptBlock]$Action, >> "%psFile%" +echo [string]$Description, >> "%psFile%" +echo [int]$MaxAttempts = 3 >> "%psFile%" +echo ) >> "%psFile%" +echo for ($attempt = 1; $attempt -le $MaxAttempts; $attempt++) { >> "%psFile%" +echo ^& $Action >> "%psFile%" +echo if ($LASTEXITCODE -eq 0) { return } >> "%psFile%" +echo Write-Output "$Description failed (exit code $LASTEXITCODE) on attempt $attempt of $MaxAttempts. This is often caused by antivirus software briefly locking newly created files; retrying in 5 seconds..." >> "%psFile%" +echo if ($attempt -lt $MaxAttempts) { Start-Sleep -Seconds 5 } >> "%psFile%" +echo } >> "%psFile%" +echo Write-Output "ERROR: $Description failed after $MaxAttempts attempts (exit code $LASTEXITCODE)." >> "%psFile%" +echo pause >> "%psFile%" +echo exit 1 >> "%psFile%" +echo } >> "%psFile%" echo. >> "%psFile%" -echo Write-Output "Downloading micromamba from $RELEASE_URL" >> "%psFile%" -echo curl.exe -L -o micromamba.exe $RELEASE_URL >> "%psFile%" +echo ^# Set by install_cnapy_here.bat: either the pinned release version, or a sanitized >> "%psFile%" +echo ^# branch name if one was passed as an argument to install_cnapy_here.bat. >> "%psFile%" +echo $InstallLabel = "%INSTALL_LABEL%" >> "%psFile%" +echo $RELEASE_URL = "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Windows-x86_64.exe" >> "%psFile%" echo. >> "%psFile%" echo ^# Get the directory where the script is located >> "%psFile%" echo $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path >> "%psFile%" echo. >> "%psFile%" -echo $InstallDir = Join-Path -Path $ScriptDir -ChildPath "cnapy-$CNAPY_VERSION" >> "%psFile%" +echo $InstallDir = Join-Path -Path $ScriptDir -ChildPath "cnapy-$InstallLabel" >> "%psFile%" echo New-Item -ItemType Directory -Force -Path $InstallDir ^| out-null >> "%psFile%" echo. >> "%psFile%" -echo $MAMBA_INSTALL_PATH = Join-Path -Path $InstallDir -ChildPath "micromamba.exe" >> "%psFile%" +echo $InstallerPath = Join-Path -Path $InstallDir -ChildPath "miniforge_installer.exe" >> "%psFile%" +echo $MiniforgeDir = Join-Path -Path $InstallDir -ChildPath "miniforge3" >> "%psFile%" +echo $EnvDir = Join-Path -Path $InstallDir -ChildPath "cnapy-environment" >> "%psFile%" +echo $PythonExe = Join-Path -Path $EnvDir -ChildPath "python.exe" >> "%psFile%" +echo $MambaBat = Join-Path -Path $MiniforgeDir -ChildPath "condabin\mamba.bat" >> "%psFile%" echo. >> "%psFile%" -echo Write-Output "`nInstalling micromamba to $InstallDir`n" >> "%psFile%" -echo Move-Item -Force micromamba.exe $MAMBA_INSTALL_PATH ^| out-null >> "%psFile%" +echo Write-Output "Downloading the Miniforge installer from $RELEASE_URL" >> "%psFile%" +echo curl.exe -L -o $InstallerPath $RELEASE_URL >> "%psFile%" +echo if ($LASTEXITCODE -ne 0) { >> "%psFile%" +echo Write-Output "ERROR: failed to download the Miniforge installer (exit code $LASTEXITCODE)." >> "%psFile%" +echo pause >> "%psFile%" +echo exit 1 >> "%psFile%" +echo } >> "%psFile%" echo. >> "%psFile%" -echo ^# Use ^& to execute the micromamba commands stored in the variable >> "%psFile%" -echo ^& $MAMBA_INSTALL_PATH create -y -p "./cnapy-$CNAPY_VERSION/cnapy-environment" python=3.10 pip openjdk -r "./cnapy-$CNAPY_VERSION/" -c conda-forge >> "%psFile%" -echo Copy-Item -Path "cnapy-1.2.8/condabin/mamba.bat" -Destination "cnapy-1.2.8/condabin/micromamba.bat" >> "%psFile%" -echo ^& $MAMBA_INSTALL_PATH run -p "./cnapy-$CNAPY_VERSION/cnapy-environment" -r "./cnapy-$CNAPY_VERSION/" pip install --no-cache-dir uv >> "%psFile%" -echo ^& $MAMBA_INSTALL_PATH run -p "./cnapy-$CNAPY_VERSION/cnapy-environment" -r "./cnapy-$CNAPY_VERSION/" uv --no-cache pip install --no-cache-dir cnapy >> "%psFile%" +echo Write-Output "`nInstalling Miniforge to $MiniforgeDir (this can take a minute)`n" >> "%psFile%" +echo ^# Standard silent-install flags for the NSIS-based Miniconda/Miniforge Windows installer. >> "%psFile%" +echo ^# /NoRegistry=1 asks it to skip the Add/Remove Programs entry in the first place -- this is >> "%psFile%" +echo ^# a documented constructor (the tool that builds these installers) option, but not every >> "%psFile%" +echo ^# installer build is guaranteed to honor it, so it is backed up by an explicit cleanup below. >> "%psFile%" +echo $InstallArgs = "/InstallationType=JustMe /AddToPath=0 /RegisterPython=0 /NoRegistry=1 /S /D=$MiniforgeDir" >> "%psFile%" +echo $InstallProc = Start-Process -FilePath $InstallerPath -ArgumentList $InstallArgs -Wait -PassThru >> "%psFile%" +echo if ($InstallProc.ExitCode -ne 0 -or -not (Test-Path "$MambaBat")) { >> "%psFile%" +echo Write-Output "ERROR: Miniforge installation did not complete successfully (exit code $($InstallProc.ExitCode))." >> "%psFile%" +echo Write-Output "This can happen if antivirus/EDR software blocked the installer, or if a Miniforge install already exists at $MiniforgeDir." >> "%psFile%" +echo pause >> "%psFile%" +echo exit 1 >> "%psFile%" +echo } >> "%psFile%" echo. >> "%psFile%" -echo ^# Create a new PowerShell file called "cnapy_runner_helper.ps1" >> "%psFile%" -echo $PsFilePath = Join-Path -Path $InstallDir -ChildPath "cnapy_runner_helper.ps1" >> "%psFile%" -echo $PsFileContent = "$MAMBA_INSTALL_PATH run -p `"$InstallDir\cnapy-environment`" -r `"$InstallDir\`" cnapy" >> "%psFile%" -echo Set-Content -Path $PsFilePath -Value $PsFileContent >> "%psFile%" +echo ^# Best-effort cleanup of anything the installer may have left OUTSIDE $InstallDir, so that >> "%psFile%" +echo ^# deleting the cnapy-$InstallLabel folder later is a complete uninstall. Wrapped so a failure >> "%psFile%" +echo ^# here (e.g. restricted registry access) never aborts the install itself. >> "%psFile%" +echo try { >> "%psFile%" +echo $UninstallRoot = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall" >> "%psFile%" +echo Get-ChildItem -Path $UninstallRoot -ErrorAction SilentlyContinue ^| ForEach-Object { >> "%psFile%" +echo $Entry = Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue >> "%psFile%" +echo if ($Entry -and $Entry.InstallLocation -and ($Entry.InstallLocation.TrimEnd('\') -eq $MiniforgeDir.TrimEnd('\'))) { >> "%psFile%" +echo Remove-Item -Path $_.PSPath -Recurse -Force -ErrorAction SilentlyContinue >> "%psFile%" +echo } >> "%psFile%" +echo } >> "%psFile%" +echo } catch {} >> "%psFile%" +echo try { >> "%psFile%" +echo ^# Default Start Menu folder name used by Miniforge installers; only removed if this >> "%psFile%" +echo ^# specific install created it (Test-Path guards against removing a pre-existing one >> "%psFile%" +echo ^# from an unrelated Miniforge/Miniconda install on the same machine). >> "%psFile%" +echo $StartMenuDir = Join-Path -Path $Env:APPDATA -ChildPath "Microsoft\Windows\Start Menu\Programs\Miniforge3" >> "%psFile%" +echo if (Test-Path $StartMenuDir) { >> "%psFile%" +echo Remove-Item -Path $StartMenuDir -Recurse -Force -ErrorAction SilentlyContinue >> "%psFile%" +echo } >> "%psFile%" +echo } catch {} >> "%psFile%" +echo. >> "%psFile%" +echo Invoke-WithRetry -Description "Creating the cnapy environment" -Action { ^& $MambaBat create -y -p "$EnvDir" python=3.10 pip openjdk -c conda-forge } >> "%psFile%" +echo if (-not (Test-Path "$PythonExe")) { >> "%psFile%" +echo Write-Output "ERROR: environment creation did not produce a Python interpreter at $PythonExe." >> "%psFile%" +echo Write-Output "This is usually caused by a network problem while downloading packages from conda-forge." >> "%psFile%" +echo pause >> "%psFile%" +echo exit 1 >> "%psFile%" +echo } >> "%psFile%" +echo. >> "%psFile%" +echo Invoke-WithRetry -Description "Installing uv" -Action { ^& "$PythonExe" -m pip install --no-cache-dir uv } >> "%psFile%" +if defined BRANCH ( + echo Invoke-WithRetry -Description "Installing cnapy" -Action { ^& "$PythonExe" -m uv --no-cache pip install --no-cache-dir "git+https://github.com/cnapy-org/CNApy.git@%BRANCH%" } >> "%psFile%" +) else ( + echo Invoke-WithRetry -Description "Installing cnapy" -Action { ^& "$PythonExe" -m uv --no-cache pip install --no-cache-dir cnapy } >> "%psFile%" +) echo. >> "%psFile%" -echo ^# Create a new batch file called "RUN_CNApy.bat" >> "%psFile%" +echo ^# cnapy-environment is a self-contained conda environment: its own Scripts\cnapy.exe entry >> "%psFile%" +echo ^# point is launched directly, so miniforge3\ and conda/mamba are not needed again at runtime. >> "%psFile%" echo $BatchFilePath = Join-Path -Path $InstallDir -ChildPath "RUN_CNApy.bat" >> "%psFile%" -echo $BatchFileContent = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$InstallDir\cnapy_runner_helper.ps1`"" >> "%psFile%" +echo $CnapyExe = Join-Path -Path $EnvDir -ChildPath "Scripts\cnapy.exe" >> "%psFile%" +echo $BatchFileContent = "@echo off`r`n`"$CnapyExe`"" >> "%psFile%" echo Set-Content -Path $BatchFilePath -Value $BatchFileContent >> "%psFile%" echo. >> "%psFile%" echo ^# Create desktop icon using PowerShell >> "%psFile%" -echo $ShortcutPath = [System.IO.Path]::Combine($Env:USERPROFILE, "Desktop", "CNApy-$CNAPY_VERSION.lnk") >> "%psFile%" +echo $ShortcutPath = [System.IO.Path]::Combine($Env:USERPROFILE, "Desktop", "CNApy-$InstallLabel.lnk") >> "%psFile%" echo $WScriptShell = New-Object -ComObject WScript.Shell >> "%psFile%" echo $Shortcut = $WScriptShell.CreateShortcut($ShortcutPath) >> "%psFile%" echo $Shortcut.TargetPath = $BatchFilePath >> "%psFile%" @@ -68,9 +177,9 @@ if exist "%psFile%" ( :: Congratulate the user echo Congratulations! CNApy was successfully installed! - echo To run CNApy, double-click on the newly created CNApy-1.2.8 desktop icon or, - echo alternatively, double-click on the RUN_CNApy.bat file in the newly created cnapy-1.2.8 subfolder. - echo To deinstall CNApy later, simply delete the newly created cnapy-1.2.8 subfolder. + echo To run CNApy, double-click on the newly created CNApy-%INSTALL_LABEL% desktop icon or, + echo alternatively, double-click on the RUN_CNApy.bat file in the newly created cnapy-%INSTALL_LABEL% subfolder. + echo To deinstall CNApy later, simply delete the newly created cnapy-%INSTALL_LABEL% subfolder. pause ) else ( echo PowerShell script file not found: %psFile% diff --git a/installers/install_cnapy_here.sh b/installers/install_cnapy_here.sh index 641cd9e4..12595959 100755 --- a/installers/install_cnapy_here.sh +++ b/installers/install_cnapy_here.sh @@ -3,11 +3,35 @@ set -eu -# CNApy version -CNAPY_VERSION="1.2.8" +# Optional first argument: a git branch of https://github.com/cnapy-org/CNApy to install +# instead of the pinned PyPI release. Example: ./install_cnapy_here.sh cnapy2 +BRANCH="${1:-}" + +if [ "${BRANCH}" = "-h" ] || [ "${BRANCH}" = "--help" ]; then + echo "Usage: $0 [branch]" + echo " branch Optional. A branch of https://github.com/cnapy-org/CNApy to install" + echo " instead of the latest release from PyPI, e.g. \"cnapy2\"." + exit 0 +fi + +if [ -n "${BRANCH}" ]; then + if ! hash git >/dev/null 2>&1; then + echo "ERROR: installing from a specific branch requires git to be installed and available on PATH." >&2 + exit 1 + fi + echo "Checking that branch '${BRANCH}' exists in https://github.com/cnapy-org/CNApy.git ..." + if ! git ls-remote --exit-code --heads https://github.com/cnapy-org/CNApy.git "${BRANCH}" >/dev/null 2>&1; then + echo "ERROR: branch '${BRANCH}' was not found in https://github.com/cnapy-org/CNApy.git" >&2 + exit 1 + fi + # sanitize for use as a folder name (branch names may contain "/") + INSTALL_LABEL="$(echo "${BRANCH}" | tr '/' '-')" +else + INSTALL_LABEL="1.2.8" +fi # Folders -BIN_FOLDER="${BIN_FOLDER:-./cnapy-${CNAPY_VERSION}}" +BIN_FOLDER="${BIN_FOLDER:-./cnapy-${INSTALL_LABEL}}" CONDA_FORGE_YES="${CONDA_FORGE_YES:-yes}" # Computing artifact location @@ -39,6 +63,25 @@ esac RELEASE_URL="https://github.com/mamba-org/micromamba-releases/releases/latest/download/micromamba-${PLATFORM}-${ARCH}" +install_with_retry() { + description="$1" + shift + max_attempts=3 + attempt=1 + while [ "$attempt" -le "$max_attempts" ]; do + if "$@"; then + return 0 + fi + if [ "$attempt" -lt "$max_attempts" ]; then + echo "${description} failed on attempt ${attempt} of ${max_attempts}, retrying in 5 seconds. This is sometimes caused by antivirus/EDR software briefly locking newly created files..." >&2 + sleep 5 + fi + attempt=$((attempt + 1)) + done + echo "ERROR: ${description} failed after ${max_attempts} attempts." >&2 + exit 1 +} + # Downloading artifact mkdir -p "${BIN_FOLDER}" if hash curl >/dev/null 2>&1; then @@ -51,13 +94,34 @@ else fi chmod +x "${BIN_FOLDER}/micromamba" -./cnapy-${CNAPY_VERSION}/micromamba create -y -p ./cnapy-${CNAPY_VERSION}/cnapy-environment python=3.10 pip openjdk -r ./cnapy-${CNAPY_VERSION}/ -c conda-forge -./cnapy-${CNAPY_VERSION}/micromamba run -p ./cnapy-${CNAPY_VERSION}/cnapy-environment -r ./cnapy-${CNAPY_VERSION}/ pip install --no-cache-dir uv -./cnapy-${CNAPY_VERSION}/micromamba run -p ./cnapy-${CNAPY_VERSION}/cnapy-environment -r ./cnapy-${CNAPY_VERSION}/ uv --no-cache pip install --no-cache-dir cnapy +ENV_DIR="${BIN_FOLDER}/cnapy-environment" +ENV_PYTHON="${ENV_DIR}/bin/python" -cat << 'EOF' > ./cnapy-${CNAPY_VERSION}/run_cnapy.sh +"${BIN_FOLDER}/micromamba" create -y -p "${ENV_DIR}" python=3.10 pip openjdk -r "${BIN_FOLDER}" -c conda-forge + +if [ ! -x "${ENV_PYTHON}" ]; then + echo "ERROR: environment creation did not produce a Python interpreter at ${ENV_PYTHON}." >&2 + echo "This is usually caused by a network problem while downloading packages from conda-forge." >&2 + exit 1 +fi + +# Invoke pip/uv via the environment's own interpreter (absolute path) rather than by +# bare name, so this can never accidentally resolve to some other Python found earlier +# on PATH (e.g. an already-active conda base environment, a Homebrew Python, etc.) +install_with_retry "Installing uv" "${ENV_PYTHON}" -m pip install --no-cache-dir uv +if [ -n "${BRANCH}" ]; then + install_with_retry "Installing cnapy" "${ENV_PYTHON}" -m uv --no-cache pip install --no-cache-dir "git+https://github.com/cnapy-org/CNApy.git@${BRANCH}" +else + install_with_retry "Installing cnapy" "${ENV_PYTHON}" -m uv --no-cache pip install --no-cache-dir cnapy +fi + +cat << 'EOF' > "${BIN_FOLDER}/run_cnapy.sh" #!/bin/bash +# Always run relative to this script's own location, regardless of the caller's +# current working directory. +cd "$(dirname "$0")" + # Add CPLEX variable here, e.g. # export PYTHONPATH=/path_to_cplex/cplex/python/3.10/x86-64_linux @@ -67,8 +131,8 @@ export DYLD_LIBRARY_PATH="./cnapy-environment/lib/" # For MacOS EOF # Make the shell script executable -chmod +x ./cnapy-${CNAPY_VERSION}/run_cnapy.sh +chmod +x "${BIN_FOLDER}/run_cnapy.sh" echo CNApy was succesfully installed! -echo You can now run CNApy by executing run_cnapy.sh in the newly created cnapy-${CNAPY_VERSION} subfolder. -echo To deinstall CNApy later, simply delete the cnapy-${CNAPY_VERSION} subfolder. +echo You can now run CNApy by executing run_cnapy.sh in the newly created cnapy-${INSTALL_LABEL} subfolder. +echo To deinstall CNApy later, simply delete the cnapy-${INSTALL_LABEL} subfolder. diff --git a/pyproject.toml b/pyproject.toml index 1f36ac66..cff0550a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,10 +9,10 @@ authors = [ ] description = "An integrated environment for metabolic network analysis." readme = "README.md" +license = "Apache-2.0" requires-python = "==3.10.*" classifiers = [ "Programming Language :: Python :: 3", - "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", ] dependencies = [ @@ -23,9 +23,10 @@ dependencies = [ "efmtool_link>=0.0.8", "optlang_enumerator>=0.0.15", "straindesign>=1.18", - "qtpy>=2.3", - "pyqtwebengine>=5.15", - "qtconsole==5.4", + "qtpy>=2.4", + "pyqt6>=6.8,<6.9", + "pyqt6-webengine>=6.8,<6.9", + "qtconsole>=5.6", "gurobipy>=12.0", "cplex", "docplex", @@ -35,7 +36,9 @@ dependencies = [ "jpype1==1.5.0", "setuptools", "cobrak==0.0.10", - "highspy>=1.14.0" + "highspy>=1.14.0", + "osqp>=1.1.3", + "nest-asyncio2>=1.7.0" ] [project.scripts] diff --git a/setup.py b/setup.py index bf47b0d0..4ac50e8e 100644 --- a/setup.py +++ b/setup.py @@ -21,8 +21,9 @@ description='An integrated environment for metabolic network analysis.', long_description=open('README.md', encoding="utf8").read(), long_description_content_type="text/asciidoc", - packages=['cnapy', 'cnapy.gui_elements'], + packages=['cnapy', 'cnapy.gui_elements', 'cnapy.data'], package_dir={'cnapy': 'cnapy'}, package_data={'cnapy': [ - 'data/*.svg', 'data/escher_cnapy.html', 'data/escher.min.js']} + 'data/*.svg', 'data/escher_cnapy.html', 'data/escher.min.js', + 'data/scenario_template.xlsx']} ) diff --git a/test_optlang_highs_interface.py b/test_optlang_highs_interface.py new file mode 100644 index 00000000..4ae0d39f --- /dev/null +++ b/test_optlang_highs_interface.py @@ -0,0 +1,563 @@ +""" +Tests for HiGHS QP support in optlang. +""" +import unittest +import numpy as np +import cobra + +try: + import highspy +except ImportError: + raise ImportError("The highs_interface requires highspy: pip install highspy") + +# Assuming the interface code is in a file named highs_interface.py +# If it is in the same directory, you can just import it. +# For this test script to run standalone, we assume the class definitions +# are available or imported from the module where you saved the previous code. +import sys +import os + +import cnapy.optlang_highs_interface + +# Add the current directory to the path to import the module if needed +sys.path.insert(0, os.path.dirname(__file__)) +cobra.Configuration.solver = cnapy.optlang_highs_interface +from cnapy.optlang_highs_interface import Model, Variable, Constraint, Objective, _get_quadratic_terms_from_expr + +# try: +# from cnapy.optlang_highs_interface import Model, Variable, Constraint, Objective, _get_quadratic_terms_from_expr +# except ImportError: +# # If the file is named differently, adjust here +# print("Could not import highs_interface. Ensure it is in the path.") +# sys.exit(1) +# from optlang import symbolics + +class TestHiGHSQP(unittest.TestCase): + def setUp(self): + self.model = Model() + + def test_simple_qp_unconstrained(self): + """Test solving a simple unconstrained QP: min x^2 + y^2.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # Objective: x^2 + y^2 + # In HiGHS formulation: 0.5 * x^T * Q * x + # x^2 implies Q_xx = 2.0 + obj_expr = x**2 + y**2 + self.model.objective = Objective(obj_expr, direction="min") + + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Solution should be x=0, y=0 + self.assertAlmostEqual(self.model.objective.value, 0.0) + self.assertAlmostEqual(x.primal, 0.0, places=5) + self.assertAlmostEqual(y.primal, 0.0, places=5) + + def test_simple_qp_constrained(self): + """Test solving a constrained QP: min x^2 + y^2 s.t. x + y >= 1.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # Constraint: x + y >= 1 + self.model.add(Constraint(x + y, lb=1)) + + # Objective: x^2 + y^2 + self.model.objective = Objective(x**2 + y**2, direction="min") + + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Analytical solution for min x^2 + y^2 s.t. x + y = 1 is x=0.5, y=0.5 + self.assertAlmostEqual(x.primal, 0.5, places=4) + self.assertAlmostEqual(y.primal, 0.5, places=4) + self.assertAlmostEqual(self.model.objective.value, 0.5, places=4) + + def test_qp_with_linear_term(self): + """Test QP with linear terms: min (x-1)^2 + (y-2)^2.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # (x-1)^2 + (y-2)^2 = x^2 - 2x + 1 + y^2 - 4y + 4 + # Objective: x^2 + y^2 - 2x - 4y + 5 + self.model.objective = Objective(x**2 + y**2 - 2*x - 4*y + 5, direction="min") + + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Solution should be x=1, y=2 + self.assertAlmostEqual(x.primal, 1.0, places=4) + self.assertAlmostEqual(y.primal, 2.0, places=4) + self.assertAlmostEqual(self.model.objective.value, 0.0, places=4) + + def test_qp_cross_term(self): + """Test QP with cross terms: min x^2 + y^2 + xy.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # Objective: x^2 + y^2 + x*y + # Note: HiGHS Hessian is symmetric. x*y contributes to Q_xy and Q_yx. + # Our implementation handles the symmetry. + self.model.objective = Objective(x**2 + y**2 + x*y, direction="min") + + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Solution should be x=0, y=0 + self.assertAlmostEqual(x.primal, 0.0, places=5) + self.assertAlmostEqual(y.primal, 0.0, places=5) + + def test_qp_to_lp_transition(self): + """Test changing objective from QP to LP.""" + x = Variable("x", lb=0, ub=10) + y = Variable("y", lb=0, ub=10) + + self.model.add(x) + self.model.add(y) + + # 1. Solve QP + self.model.objective = Objective(x**2 + y**2, direction="min") + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 0.0) + + # 2. Change to LP objective + self.model.objective = Objective(x + y, direction="max") + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 10.0) + self.assertAlmostEqual(y.primal, 10.0) + + def test_lp_to_qp_transition(self): + """Test changing objective from LP to QP.""" + x = Variable("x", lb=0, ub=10) + y = Variable("y", lb=0, ub=10) + + self.model.add(x) + self.model.add(y) + + # 1. Solve LP + self.model.objective = Objective(x + y, direction="max") + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 10.0) + + # 2. Change to QP objective + self.model.objective = Objective((x-5)**2 + (y-5)**2, direction="min") + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 5.0, places=4) + self.assertAlmostEqual(y.primal, 5.0, places=4) + + def test_infeasible_qp(self): + """Test an infeasible QP.""" + x = Variable("x", lb=0, ub=10) + y = Variable("y", lb=0, ub=10) + + self.model.add(x) + self.model.add(y) + + # Contradictory constraints + self.model.add(Constraint(x + y, lb=30, ub=30)) + + self.model.objective = Objective(x**2 + y**2, direction="min") + status = self.model.optimize() + self.assertEqual(status, "infeasible") + + def test_unbounded_qp(self): + """Test an unbounded QP (if applicable, though QP is often bounded by curvature).""" + x = Variable("x", lb=-10, ub=10) # Bounded variable + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # Objective: -x^2 - y^2 (Concave, max problem) + # With bounds, it should be solvable at the bounds. + self.model.objective = Objective(-(x**2) - (y**2), direction="max") + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Maximize -x^2 - y^2 is same as minimize x^2 + y^2. + # Wait, if we maximize -x^2, the max is at x=0. + # If we had -x^2 + x, it might be unbounded if x is unbounded. + # Here x is bounded, so it should be optimal. + self.assertAlmostEqual(x.primal, 0.0, places=5) + + def test_quadratic_coefficient_extraction(self): + """Test the internal helper for extracting quadratic coefficients.""" + # Updated import name + + x = Variable("x") + y = Variable("y") + + # Case 1: x^2 + # We need to pass a SymPy expression, not an Objective instance + obj_expr = x**2 + terms,rest = _get_quadratic_terms_from_expr(obj_expr) + self.assertEqual(terms, {('x', 'x'): 1.0}) + self.assertEqual(rest, 0) + + # Case 2: x*y + obj_expr = x*y + terms,rest = _get_quadratic_terms_from_expr(obj_expr) + self.assertEqual(terms, {('x', 'y'): 1.0}) + self.assertEqual(rest, 0) + + # Case 3: 2*x^2 + 3*x*y + obj_expr = 2*x**2 + 3*x*y + terms,rest = _get_quadratic_terms_from_expr(obj_expr) + self.assertEqual(terms, {('x', 'x'): 2.0, ('x', 'y'): 3.0}) + self.assertEqual(rest, 0) + + def test_rebuild_with_qp(self): + """Test that removing a variable triggers a rebuild and preserves QP objective.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + z = Variable("z", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + self.model.add(z) + + self.model.objective = Objective(x**2 + y**2 + z**2, direction="min") + + # Optimize initial + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Remove z + self.model.remove(z) + + # Optimize again + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Check that x and y are still 0 + self.assertAlmostEqual(x.primal, 0.0, places=5) + self.assertAlmostEqual(y.primal, 0.0, places=5) + + # Check that z is gone + # The interface returns None for variables not in the problem + self.assertIsNone(z.primal) + + + def test_qp_solution_value_accuracy(self): + """Test that the reported objective value matches the calculated value.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # Constraint: x + y = 2 + self.model.add(Constraint(x + y, lb=2, ub=2)) + + # Objective: x^2 + y^2 + self.model.objective = Objective(x**2 + y**2, direction="min") + + self.model.optimize() + + # Analytical solution: x=1, y=1. Obj = 1^2 + 1^2 = 2. + expected_val = 1.0**2 + 1.0**2 + + # The solver might return slightly different due to tolerances + self.assertAlmostEqual(self.model.objective.value, expected_val, places=4) + + # Verify manually using primals + manual_val = x.primal**2 + y.primal**2 + self.assertAlmostEqual(self.model.objective.value, manual_val, places=6) + + def test_set_coefficients_linear(self): + """Test setting linear objective coefficients via dictionary.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + self.model.objective = Objective(0) + + # Use Variable objects as keys + self.model.objective.set_coefficients(linear={x: 1.0, y: 1.0}) + self.model.objective.direction = "max" + + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 10.0) + self.assertAlmostEqual(y.primal, 10.0) + + def test_set_coefficients_quadratic(self): + """Test setting quadratic objective coefficients via dictionary.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + self.model.objective = Objective(0) + + # Use Variable objects as keys + self.model.objective.set_coefficients(quadratic={(x, x): 1.0, (y, y): 1.0}) + self.model.objective.direction = "min" + + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 0.0, places=5) + self.assertAlmostEqual(y.primal, 0.0, places=5) + + def test_set_coefficients_mixed(self): + """Test setting both linear and quadratic coefficients via dictionaries.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + self.model.objective = Objective(0, direction="min") + + # Use Variable objects as keys + self.model.objective.set_coefficients( + linear={x: -2.0, y: -4.0}, + quadratic={(x, x): 1.0, (y, y): 1.0} + ) + + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 1.0, places=4) + self.assertAlmostEqual(y.primal, 2.0, places=4) + self.assertAlmostEqual(self.model.objective.value, -5.0, places=4) + + def test_clear_quadratic_terms(self): + """Test clearing quadratic terms by setting an empty dictionary.""" + x = Variable("x", lb=0, ub=10) + y = Variable("y", lb=0, ub=10) + + self.model.add(x) + self.model.add(y) + + # Start with QP + self.model.objective = Objective(x**2 + y**2, direction="min") + status = self.model.optimize() + self.assertEqual(status, "optimal") + self.assertAlmostEqual(x.primal, 0.0) + + # Clear quadratic terms and set linear objective + self.model.objective.set_coefficients( + linear={x: 1.0, y: 1.0}, + quadratic={} + ) + self.model.objective.direction = "max" + + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # Solution should be at bounds + self.assertAlmostEqual(x.primal, 10.0) + self.assertAlmostEqual(y.primal, 10.0) + + def test_expression_reconstruction(self): + """Test that the expression property correctly reconstructs from dictionaries.""" + x = Variable("x", lb=-10, ub=10) + y = Variable("y", lb=-10, ub=10) + + self.model.add(x) + self.model.add(y) + + # Set objective via dictionaries + self.model.objective = Objective(0) + self.model.objective.set_coefficients( + linear={x: 2.0, y: 3.0}, + quadratic={(x, x): 1.0, (x, y): 0.5} + ) + + # Get the expression + expr = self.model.objective.expression + self.assertEqual(str(expr), "1.0*x**2 + 0.5*x*y + 2.0*x + 3.0*y") + # use the assertions below if the one above is too strong + # # Verify it is a SymPy expression and not zero + # self.assertTrue(expr != 0) + # self.assertTrue(hasattr(expr, "is_Add") or hasattr(expr, "is_Mul")) + + # # Verify x and y are present + # self.assertIn(x, expr.atoms(symbolics.Symbol)) + # self.assertIn(y, expr.atoms(symbolics.Symbol)) + + def test_constraint_set_linear_coefficients(self): + """Test setting constraint coefficients directly via dictionary.""" + x = Variable("x", lb=0, ub=10) + y = Variable("y", lb=0, ub=10) + + self.model.add(x) + self.model.add(y) + + # Initial constraint: x + y <= 5 + # With objective max x + y, solution should be x=5, y=0 (or any combination summing to 5) + # But usually solvers pick one. Let's use a more distinct objective. + # Objective: max x + self.model.objective = Objective(x, direction="max") + + constraint = Constraint(x + y, lb=0, ub=5) + self.model.add(constraint) + + status = self.model.optimize() + self.assertEqual(status, "optimal") + + # With max x and x + y <= 5, x should be 5, y should be 0. + self.assertAlmostEqual(x.primal, 5.0) + self.assertAlmostEqual(y.primal, 0.0) + + # Now update the constraint coefficients using the dictionary method + # Change constraint to: 2*x + y <= 5 + # With max x, 2*x <= 5 => x <= 2.5. + # So x should be 2.5, y should be 0. + constraint.set_linear_coefficients({x: 2.0, y: 1.0}) + status = self.model.optimize() + self.assertEqual(status, "optimal") + + self.assertAlmostEqual(x.primal, 2.5, places=4) + self.assertAlmostEqual(y.primal, 0.0, places=4) + + # Verify the expression property reconstructs correctly + expr = constraint.expression + self.assertEqual(str(expr), "2.0*x + 1.0*y") + # # The expression should be 2.0*x + 1.0*y + # # We check if x and y are present + # self.assertIn(x, expr.atoms(symbolics.Symbol)) + # self.assertIn(y, expr.atoms(symbolics.Symbol)) + + def test_cobrapy_context_objective_rollback(self): + """Test that rolling back objective changes works correctly.""" + import cobra + from cobra import Model as CobraModel + + cobra_model = CobraModel('test_model') + + # Make metabolites boundary metabolites so they can accumulate/dissipate + # This allows flux to be non-zero. + met_a = cobra.Metabolite('A', compartment='e') # e for extracellular + met_b = cobra.Metabolite('B', compartment='e') + cobra_model.add_metabolites([met_a, met_b]) + + reaction = cobra.Reaction('R1') + reaction.add_metabolites({met_a: -1, met_b: 1}) + cobra_model.add_reactions([reaction]) + cobra_model.add_boundary(met_a, type="exchange") + cobra_model.add_boundary(met_b, type="exchange") + + # Set objective to maximize R1 + cobra_model.objective = 'R1' + cobra_model.objective_direction = 'max' + + # Optimize + sol = cobra_model.optimize() + self.assertEqual(sol.objective_value, 1000.0) + + # ... rest of test + + def test_cobrapy_context_bounds_rollback(self): + """Test that rolling back variable bound changes works correctly.""" + import cobra + from cobra import Model as CobraModel + + cobra_model = CobraModel('test_model') + cobra_model.add_metabolites([cobra.Metabolite('A', compartment='e')]) + reaction = cobra.Reaction('R1') + reaction.add_metabolites({cobra_model.metabolites.A: -1}) + cobra_model.add_reactions([reaction]) + cobra_model.add_boundary(cobra_model.metabolites.A, type="exchange") + cobra_model.objective = 'R1' + + # Initial bounds + initial_lb = cobra_model.reactions.get_by_id('R1').lower_bound + initial_ub = cobra_model.reactions.get_by_id('R1').upper_bound + + with cobra_model: + # Change bounds + cobra_model.reactions.get_by_id('R1').lower_bound = 10 + cobra_model.reactions.get_by_id('R1').upper_bound = 20 + + # Verify change + self.assertEqual(cobra_model.reactions.get_by_id('R1').lower_bound, 10) + + # Verify rollback + self.assertEqual(cobra_model.reactions.get_by_id('R1').lower_bound, initial_lb) + self.assertEqual(cobra_model.reactions.get_by_id('R1').upper_bound, initial_ub) + + # this test cannot work because for the rollback to happen one would need + # to set the objective via cobra_model.objective and this only supports a + # dictionary of linear coefficients, not quadratic ones. + # def test_cobrapy_context_qp_rollback(self): + # """Test that rolling back quadratic objective changes works correctly.""" + # import cobra + # from cobra import Model as CobraModel + + # cobra_model = CobraModel('test_model') + + # # Make metabolites boundary metabolites + # met_a = cobra.Metabolite('A', compartment='e') + # met_b = cobra.Metabolite('B', compartment='e') + # cobra_model.add_metabolites([met_a, met_b]) + + # reaction = cobra.Reaction('R1') + # reaction.add_metabolites({met_a: -1, met_b: 1}) + # cobra_model.add_reactions([reaction]) + # cobra_model.add_boundary(met_a, type="exchange") + # cobra_model.add_boundary(met_b, type="exchange") + + # # Set a linear objective initially + # cobra_model.objective = 'R1' + # cobra_model.objective_direction = 'max' + + # # Optimize initial state to ensure we are in a clean LP state + # sol_init = cobra_model.optimize() + # self.assertGreater(sol_init.fluxes['R1'], 0) + + # with cobra_model: + # # Change to a quadratic objective using the HiGHS interface method + # # cobra_model.solver is the Model instance + # # cobra_model.solver.objective is the Objective instance + + # r1 = cobra_model.reactions.get_by_id('R1') + + # # Set quadratic coefficients: (R1 - 0.5)^2 = R1^2 - R1 + 0.25 + # # Note: must use r1.forward_variable (the actual optlang Variable), + # # not the cobra Reaction object itself - Reaction.name is its + # # human-readable descriptive name (defaults to ""), not the + # # solver variable name, so passing r1 directly silently resolves + # # to the wrong (nonexistent) column. + # r1_var = r1.forward_variable + # cobra_model.solver.objective.set_coefficients( + # quadratic={(r1_var, r1_var): 1.0}, + # linear={r1_var: -1.0}, + # constant=0.25 + # ) + # cobra_model.solver.objective.direction = 'min' + + # sol_temp = cobra_model.optimize() + # # Solution should be close to 0.5 + # self.assertAlmostEqual(sol_temp.fluxes['R1'], 0.5, places=4) + + # # Verify rollback: The objective should be linear again + # self.assertTrue(cobra_model.objective.is_Linear) + + # # Optimize to ensure solver is in the correct state + # sol_final = cobra_model.optimize() + # # With linear objective max R1, it should go to upper bound (1000) + # self.assertAlmostEqual(sol_final.fluxes['R1'], 1000.0) + +if __name__ == '__main__': + unittest.main() diff --git a/testscript.py b/testscript.py index d724af0b..9b0395cb 100644 --- a/testscript.py +++ b/testscript.py @@ -70,8 +70,8 @@ def open_project(cna, filename): cna.recreate_maps() cna.centralWidget().mode_navigator.clear() cna.appdata.project.scen_values.clear() - cna.appdata.scenario_past.clear() - cna.appdata.scenario_future.clear() +# cna.appdata.scenario_past.clear() +# cna.appdata.scenario_future.clear() for r in cna.appdata.project.cobra_py_model.reactions: if 'cnapy-default' in r.annotation.keys(): cna.centralWidget().update_reaction_value(