Mercurial > repos > bimib > cobraxy
comparison COBRAxy/utils/model_utils.py @ 508:ca98c149ec61 draft
Uploaded
author | francesco_lapi |
---|---|
date | Wed, 01 Oct 2025 14:21:26 +0000 |
parents | ffc234ec80db |
children |
comparison
equal
deleted
inserted
replaced
507:20e135a73cad | 508:ca98c149ec61 |
---|---|
1153 added += 1 | 1153 added += 1 |
1154 | 1154 |
1155 logger.info(f"Model genes updated: removed {removed}, added {added}") | 1155 logger.info(f"Model genes updated: removed {removed}, added {added}") |
1156 | 1156 |
1157 | 1157 |
1158 def export_model_to_tabular(model: cobraModel, | |
1159 output_path: str, | |
1160 translation_issues: Dict = None, | |
1161 include_objective: bool = True, | |
1162 save_function = None) -> pd.DataFrame: | |
1163 """ | |
1164 Export a COBRA model to tabular format with optional components. | |
1165 | |
1166 Args: | |
1167 model: COBRA model to export | |
1168 output_path: Path where to save the tabular file | |
1169 translation_issues: Optional dict of {reaction_id: issues} from gene translation | |
1170 include_objective: Whether to include objective coefficient column | |
1171 save_function: Optional custom save function, if None uses pd.DataFrame.to_csv | |
1172 | |
1173 Returns: | |
1174 pd.DataFrame: The merged tabular data | |
1175 """ | |
1176 # Generate model data | |
1177 rules = generate_rules(model, asParsed=False) | |
1178 | |
1179 reactions = generate_reactions(model, asParsed=False) | |
1180 bounds = generate_bounds(model) | |
1181 medium = get_medium(model) | |
1182 compartments = generate_compartments(model) | |
1183 | |
1184 # Create base DataFrames | |
1185 df_rules = pd.DataFrame(list(rules.items()), columns=["ReactionID", "GPR"]) | |
1186 df_reactions = pd.DataFrame(list(reactions.items()), columns=["ReactionID", "Formula"]) | |
1187 df_bounds = bounds.reset_index().rename(columns={"index": "ReactionID"}) | |
1188 df_medium = medium.rename(columns={"reaction": "ReactionID"}) | |
1189 df_medium["InMedium"] = True | |
1190 | |
1191 # Start merging | |
1192 merged = df_reactions.merge(df_rules, on="ReactionID", how="outer") | |
1193 merged = merged.merge(df_bounds, on="ReactionID", how="outer") | |
1194 | |
1195 # Add objective coefficients if requested | |
1196 if include_objective: | |
1197 objective_function = extract_objective_coefficients(model) | |
1198 merged = merged.merge(objective_function, on="ReactionID", how="outer") | |
1199 | |
1200 # Add compartments/pathways if they exist | |
1201 if compartments is not None: | |
1202 merged = merged.merge(compartments, on="ReactionID", how="outer") | |
1203 | |
1204 # Add medium information | |
1205 merged = merged.merge(df_medium, on="ReactionID", how="left") | |
1206 | |
1207 # Add translation issues if provided | |
1208 if translation_issues: | |
1209 df_translation_issues = pd.DataFrame([ | |
1210 {"ReactionID": rxn_id, "TranslationIssues": issues} | |
1211 for rxn_id, issues in translation_issues.items() | |
1212 ]) | |
1213 if not df_translation_issues.empty: | |
1214 merged = merged.merge(df_translation_issues, on="ReactionID", how="left") | |
1215 merged["TranslationIssues"] = merged["TranslationIssues"].fillna("") | |
1216 | |
1217 # Final processing | |
1218 merged["InMedium"] = merged["InMedium"].fillna(False) | |
1219 merged = merged.sort_values(by="InMedium", ascending=False) | |
1220 | |
1221 # Save the file | |
1222 if save_function: | |
1223 save_function(merged, output_path) | |
1224 else: | |
1225 merged.to_csv(output_path, sep="\t", index=False) | |
1226 | |
1227 return merged | |
1228 | |
1229 | |
1158 def _log_translation_statistics(stats: Dict[str, int], | 1230 def _log_translation_statistics(stats: Dict[str, int], |
1159 unmapped_genes: List[str], | 1231 unmapped_genes: List[str], |
1160 multi_mapping_genes: List[Tuple[str, List[str]]], | 1232 multi_mapping_genes: List[Tuple[str, List[str]]], |
1161 original_genes: Set[str], | 1233 original_genes: Set[str], |
1162 final_genes, | 1234 final_genes, |