# HG changeset patch # User francesco_lapi # Date 1759250370 0 # Node ID 4e7e67693ce7568460c8ee84c18906f138857b1a # Parent a2f7a6dd9d0b6f5861d90345488580e55b3284d3 Uploaded diff -r a2f7a6dd9d0b -r 4e7e67693ce7 COBRAxy/utils/model_utils.py --- a/COBRAxy/utils/model_utils.py Tue Sep 30 16:19:55 2025 +0000 +++ b/COBRAxy/utils/model_utils.py Tue Sep 30 16:39:30 2025 +0000 @@ -374,29 +374,29 @@ # metabolites.update(matches) # return metabolites -import re -from typing import Set -# Estrae tutti gli ID metaboliti nella formula (gestisce prefissi numerici + underscore e [comp]) def extract_metabolites_from_reaction(reaction_formula: str) -> Set[str]: """ - Estrae gli ID dei metaboliti da una formula di reazione. - Gestisce: - - coefficienti stechiometrici opzionali (interi o decimali) - - compartimenti sia in forma [c] sia _c, sempre a fine metabolita - Restituisce gli ID includendo il suffisso di compartimento così come appare. + Extract metabolite IDs from a reaction formula. + + Handles: + - optional stoichiometric coefficients (integers or decimals) + - compartment tags at the end of the metabolite, either [c] or _c + + Returns the IDs including the compartment suffix exactly as written. """ pattern = re.compile( - r'(?:^|(?<=\s)|(?<=\+)|(?<=,)|(?<==)|(?<=:))' # confine a sinistra - r'(?:\d+(?:\.\d+)?\s*)?' # coefficiente opzionale - r'([A-Za-z0-9_]+(?:\[[A-Za-z0-9]+\]|_[A-Za-z0-9]+))' # metabolita + compartimento + r'(?:^|(?<=\s)|(?<=\+)|(?<=,)|(?<==)|(?<=:))' # left boundary (start, space, +, comma, =, :) + r'(?:\d+(?:\.\d+)?\s*)?' # optional coefficient + r'([A-Za-z0-9_]+(?:\[[A-Za-z0-9]+\]|_[A-Za-z0-9]+))' # metabolite + compartment ) return {m.group(1) for m in pattern.finditer(reaction_formula)} + def extract_compartment_from_metabolite(metabolite_id: str) -> str: """Extract the compartment from a metabolite ID.""" - if '_' in metabolite_id: + if '_' == metabolite_id[-2]: return metabolite_id.split('_')[-1] if metabolite_id[-1] == ']' and metabolite_id[-3] == '[': return metabolite_id[-2]