Mercurial > repos > bimib > cobraxy
comparison COBRAxy/utils/general_utils.py @ 412:bdf4630ac1eb draft
Uploaded
author | francesco_lapi |
---|---|
date | Mon, 08 Sep 2025 21:24:32 +0000 |
parents | 6b015d3184ab |
children | 7a3ccf066b2c |
comparison
equal
deleted
inserted
replaced
411:6b015d3184ab | 412:bdf4630ac1eb |
---|---|
15 | 15 |
16 import zipfile | 16 import zipfile |
17 import gzip | 17 import gzip |
18 import bz2 | 18 import bz2 |
19 from io import StringIO | 19 from io import StringIO |
20 import rule_parsing as rulesUtils | 20 import utils.rule_parsing as rulesUtils |
21 import reaction_parsing as reactionUtils | 21 import utils.reaction_parsing as reactionUtils |
22 | 22 |
23 | 23 |
24 | 24 |
25 class ValueErr(Exception): | 25 class ValueErr(Exception): |
26 def __init__(self, param_name, expected, actual): | 26 def __init__(self, param_name, expected, actual): |
775 | 775 |
776 print(f"Aggiunti {len(metabolites_dict)} metaboliti e {len(compartments_dict)} compartimenti") | 776 print(f"Aggiunti {len(metabolites_dict)} metaboliti e {len(compartments_dict)} compartimenti") |
777 | 777 |
778 # Seconda passata: aggiungi le reazioni | 778 # Seconda passata: aggiungi le reazioni |
779 reactions_added = 0 | 779 reactions_added = 0 |
780 reactions_skipped = 0 | |
781 | 780 |
782 for idx, row in df.iterrows(): | 781 for idx, row in df.iterrows(): |
782 reaction_id = str(row['ReactionID']).strip() | |
783 reaction_formula = str(row['Reaction']).strip() | |
784 | |
785 # Salta reazioni senza formula | |
786 if not reaction_formula or reaction_formula == 'nan': | |
787 raise ValueError(f"Formula della reazione mancante {reaction_id}") | |
788 | |
789 # Crea la reazione | |
790 reaction = Reaction(reaction_id) | |
791 reaction.name = reaction_id | |
792 | |
793 # Imposta bounds | |
794 reaction.lower_bound = float(row['lower_bound']) if pd.notna(row['lower_bound']) else -1000.0 | |
795 reaction.upper_bound = float(row['upper_bound']) if pd.notna(row['upper_bound']) else 1000.0 | |
796 | |
797 # Aggiungi gene rule se presente | |
798 if pd.notna(row['Rule']) and str(row['Rule']).strip(): | |
799 reaction.gene_reaction_rule = str(row['Rule']).strip() | |
800 | |
801 # Parse della formula della reazione | |
783 try: | 802 try: |
784 reaction_id = str(row['ReactionID']).strip() | 803 parse_reaction_formula(reaction, reaction_formula, metabolites_dict) |
785 reaction_formula = str(row['Reaction']).strip() | |
786 | |
787 # Salta reazioni senza formula | |
788 if not reaction_formula or reaction_formula == 'nan': | |
789 reactions_skipped += 1 | |
790 continue | |
791 | |
792 # Crea la reazione | |
793 reaction = Reaction(reaction_id) | |
794 reaction.name = reaction_id | |
795 | |
796 # Imposta bounds | |
797 reaction.lower_bound = float(row['lower_bound']) if pd.notna(row['lower_bound']) else -1000.0 | |
798 reaction.upper_bound = float(row['upper_bound']) if pd.notna(row['upper_bound']) else 1000.0 | |
799 | |
800 # Aggiungi gene rule se presente | |
801 if pd.notna(row['Rule']) and str(row['Rule']).strip(): | |
802 reaction.gene_reaction_rule = str(row['Rule']).strip() | |
803 | |
804 # Parse della formula della reazione | |
805 try: | |
806 parse_reaction_formula(reaction, reaction_formula, metabolites_dict) | |
807 except Exception as e: | |
808 print(f"Errore nel parsing della reazione {reaction_id}: {e}") | |
809 reactions_skipped += 1 | |
810 continue | |
811 | |
812 # Aggiungi la reazione al modello | |
813 model.add_reactions([reaction]) | |
814 reactions_added += 1 | |
815 | |
816 except Exception as e: | 804 except Exception as e: |
817 print(f"Errore nell'aggiungere la reazione {reaction_id}: {e}") | 805 print(f"Errore nel parsing della reazione {reaction_id}: {e}") |
818 reactions_skipped += 1 | 806 reactions_skipped += 1 |
819 continue | 807 continue |
808 | |
809 # Aggiungi la reazione al modello | |
810 model.add_reactions([reaction]) | |
811 reactions_added += 1 | |
812 | |
820 | 813 |
821 print(f"Aggiunte {reactions_added} reazioni, saltate {reactions_skipped} reazioni") | 814 print(f"Aggiunte {reactions_added} reazioni, saltate {reactions_skipped} reazioni") |
822 | 815 |
823 # Imposta l'obiettivo di biomassa | 816 # Imposta l'obiettivo di biomassa |
824 set_biomass_objective(model) | 817 set_biomass_objective(model) |