Mercurial > repos > bimib > cobraxy
changeset 506:ffc234ec80db draft
Uploaded
author | francesco_lapi |
---|---|
date | Wed, 01 Oct 2025 13:19:03 +0000 |
parents | 96f512dff490 |
children | 20e135a73cad |
files | COBRAxy/metabolicModel2Tabular.py COBRAxy/ras_to_bounds.py COBRAxy/utils/model_utils.py |
diffstat | 3 files changed, 34 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/COBRAxy/metabolicModel2Tabular.py Wed Oct 01 11:15:00 2025 +0000 +++ b/COBRAxy/metabolicModel2Tabular.py Wed Oct 01 13:19:03 2025 +0000 @@ -332,8 +332,7 @@ medium = modelUtils.get_medium(model) objective_function = modelUtils.extract_objective_coefficients(model) - if ARGS.name == "ENGRO2": - compartments = modelUtils.generate_compartments(model) + compartments = modelUtils.generate_compartments(model) df_rules = pd.DataFrame(list(rules.items()), columns = ["ReactionID", "GPR"]) df_reactions = pd.DataFrame(list(reactions.items()), columns = ["ReactionID", "Formula"]) @@ -351,7 +350,7 @@ merged = df_reactions.merge(df_rules, on = "ReactionID", how = "outer") merged = merged.merge(df_bounds, on = "ReactionID", how = "outer") merged = merged.merge(objective_function, on = "ReactionID", how = "outer") - if ARGS.name == "ENGRO2": + if compartments is not None: merged = merged.merge(compartments, on = "ReactionID", how = "outer") merged = merged.merge(df_medium, on = "ReactionID", how = "left")
--- a/COBRAxy/ras_to_bounds.py Wed Oct 01 11:15:00 2025 +0000 +++ b/COBRAxy/ras_to_bounds.py Wed Oct 01 13:19:03 2025 +0000 @@ -178,10 +178,7 @@ bounds = modelUtils.generate_bounds(model) medium = modelUtils.get_medium(model) - try: - compartments = modelUtils.generate_compartments(model) - except: - compartments = None + compartments = modelUtils.generate_compartments(model) df_rules = pd.DataFrame(list(rules.items()), columns = ["ReactionID", "Rule"]) df_reactions = pd.DataFrame(list(reactions.items()), columns = ["ReactionID", "Reaction"]) @@ -191,9 +188,8 @@ merged = df_reactions.merge(df_rules, on = "ReactionID", how = "outer") merged = merged.merge(df_bounds, on = "ReactionID", how = "outer") - - # Add compartments only if they exist and model name is ENGRO2 - if compartments is not None and hasattr(ARGS, 'name') and ARGS.name == "ENGRO2": + # Add compartments only if they exist + if compartments is not None: merged = merged.merge(compartments, on = "ReactionID", how = "outer") merged = merged.merge(df_medium, on = "ReactionID", how = "left")
--- a/COBRAxy/utils/model_utils.py Wed Oct 01 11:15:00 2025 +0000 +++ b/COBRAxy/utils/model_utils.py Wed Oct 01 13:19:03 2025 +0000 @@ -223,37 +223,54 @@ def generate_compartments(model: cobraModel) -> pd.DataFrame: """ - Generates a DataFrame containing compartment information for each reaction. - Creates columns for each compartment position (Compartment_1, Compartment_2, etc.) + Generates a DataFrame containing pathway information for each reaction. + Creates columns for each pathway position (Pathway_1, Pathway_2, etc.) only if pathways exist. Args: - model: the COBRA model to extract compartment data from. + model: the COBRA model to extract pathway data from. Returns: - pd.DataFrame: DataFrame with ReactionID and compartment columns + pd.DataFrame: DataFrame with ReactionID and pathway columns (if any pathways exist) """ pathway_data = [] # First pass: determine the maximum number of pathways any reaction has max_pathways = 0 reaction_pathways = {} + has_any_pathways = False for reaction in model.reactions: # Get unique pathways from all metabolites in the reaction - if 'pathways' in reaction.annotation: + if 'pathways' in reaction.annotation and reaction.annotation['pathways']: if type(reaction.annotation['pathways']) == list: - reaction_pathways[reaction.id] = reaction.annotation['pathways'] - max_pathways = max(max_pathways, len(reaction.annotation['pathways'])) + # Filter out empty/None values + valid_pathways = [p for p in reaction.annotation['pathways'] if p] + if valid_pathways: + reaction_pathways[reaction.id] = valid_pathways + max_pathways = max(max_pathways, len(valid_pathways)) + has_any_pathways = True + else: + reaction_pathways[reaction.id] = [] else: - reaction_pathways[reaction.id] = [reaction.annotation['pathways']] + # Single pathway value + if reaction.annotation['pathways']: + reaction_pathways[reaction.id] = [reaction.annotation['pathways']] + max_pathways = max(max_pathways, 1) + has_any_pathways = True + else: + reaction_pathways[reaction.id] = [] else: # No pathway annotation - use empty list reaction_pathways[reaction.id] = [] - # Create column names for pathways + # If no pathways exist in the model, return DataFrame with only ReactionID + if not has_any_pathways: + return None + + # Create column names for pathways only if they exist pathway_columns = [f"Pathway_{i+1}" for i in range(max_pathways)] - # Second pass: create the data + # Second pass: create the data with pathway columns for reaction_id, pathways in reaction_pathways.items(): row = {"ReactionID": reaction_id} @@ -263,7 +280,7 @@ if i < len(pathways): row[col_name] = pathways[i] else: - row[col_name] = None # or "" if you prefer empty strings + row[col_name] = None pathway_data.append(row) @@ -289,7 +306,7 @@ pathways.append(str(row[col]).strip()) if pathways: - + reaction = model.reactions.get_by_id(reaction_id) if len(pathways) == 1: reaction.annotation['pathways'] = pathways[0]