Mercurial > repos > bimib > cobraxy
comparison COBRAxy/utils/model_utils.py @ 499:a2f7a6dd9d0b draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Tue, 30 Sep 2025 16:19:55 +0000 |
| parents | a92d21f92956 |
| children | 4e7e67693ce7 |
comparison
equal
deleted
inserted
replaced
| 498:df90f40a156c | 499:a2f7a6dd9d0b |
|---|---|
| 356 | 356 |
| 357 return model | 357 return model |
| 358 | 358 |
| 359 | 359 |
| 360 # Estrae tutti gli ID metaboliti nella formula (gestisce prefissi numerici + underscore) | 360 # Estrae tutti gli ID metaboliti nella formula (gestisce prefissi numerici + underscore) |
| 361 #def extract_metabolites_from_reaction(reaction_formula: str) -> Set[str]: | |
| 362 # """ | |
| 363 # Extract metabolite IDs from a reaction formula. | |
| 364 # Robust pattern: tokens ending with _<compartment> (e.g., _c, _m, _e), | |
| 365 # allowing leading digits/underscores. | |
| 366 # """ | |
| 367 # metabolites = set() | |
| 368 # # optional coefficient followed by a token ending with _<letters> | |
| 369 # if reaction_formula[-1] == ']' and reaction_formula[-3] == '[': | |
| 370 # pattern = r'(?:\d+(?:\.\d+)?\s+)?([A-Za-z0-9_]+[[A-Za-z0-9]]+)' | |
| 371 # else: | |
| 372 # pattern = r'(?:\d+(?:\.\d+)?\s+)?([A-Za-z0-9_]+_[A-Za-z0-9]+)' | |
| 373 # matches = re.findall(pattern, reaction_formula) | |
| 374 # metabolites.update(matches) | |
| 375 # return metabolites | |
| 376 | |
| 377 import re | |
| 378 from typing import Set | |
| 379 | |
| 380 # Estrae tutti gli ID metaboliti nella formula (gestisce prefissi numerici + underscore e [comp]) | |
| 361 def extract_metabolites_from_reaction(reaction_formula: str) -> Set[str]: | 381 def extract_metabolites_from_reaction(reaction_formula: str) -> Set[str]: |
| 362 """ | 382 """ |
| 363 Extract metabolite IDs from a reaction formula. | 383 Estrae gli ID dei metaboliti da una formula di reazione. |
| 364 Robust pattern: tokens ending with _<compartment> (e.g., _c, _m, _e), | 384 Gestisce: |
| 365 allowing leading digits/underscores. | 385 - coefficienti stechiometrici opzionali (interi o decimali) |
| 366 """ | 386 - compartimenti sia in forma [c] sia _c, sempre a fine metabolita |
| 367 metabolites = set() | 387 Restituisce gli ID includendo il suffisso di compartimento così come appare. |
| 368 # optional coefficient followed by a token ending with _<letters> | 388 """ |
| 369 if reaction_formula[-1] == ']' and reaction_formula[-3] == '[': | 389 pattern = re.compile( |
| 370 pattern = r'(?:\d+(?:\.\d+)?\s+)?([A-Za-z0-9_]+[[A-Za-z0-9]]+)' | 390 r'(?:^|(?<=\s)|(?<=\+)|(?<=,)|(?<==)|(?<=:))' # confine a sinistra |
| 371 else: | 391 r'(?:\d+(?:\.\d+)?\s*)?' # coefficiente opzionale |
| 372 pattern = r'(?:\d+(?:\.\d+)?\s+)?([A-Za-z0-9_]+_[A-Za-z0-9]+)' | 392 r'([A-Za-z0-9_]+(?:\[[A-Za-z0-9]+\]|_[A-Za-z0-9]+))' # metabolita + compartimento |
| 373 matches = re.findall(pattern, reaction_formula) | 393 ) |
| 374 metabolites.update(matches) | 394 return {m.group(1) for m in pattern.finditer(reaction_formula)} |
| 375 return metabolites | |
| 376 | 395 |
| 377 | 396 |
| 378 def extract_compartment_from_metabolite(metabolite_id: str) -> str: | 397 def extract_compartment_from_metabolite(metabolite_id: str) -> str: |
| 379 """Extract the compartment from a metabolite ID.""" | 398 """Extract the compartment from a metabolite ID.""" |
| 380 if '_' in metabolite_id: | 399 if '_' in metabolite_id: |
