Mercurial > repos > bimib > cobraxy
comparison COBRAxy/utils/model_utils.py @ 505:96f512dff490 draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Wed, 01 Oct 2025 11:15:00 +0000 |
| parents | 8dd07e59f631 |
| children | ffc234ec80db |
comparison
equal
deleted
inserted
replaced
| 504:ef3df9b697b1 | 505:96f512dff490 |
|---|---|
| 267 | 267 |
| 268 pathway_data.append(row) | 268 pathway_data.append(row) |
| 269 | 269 |
| 270 return pd.DataFrame(pathway_data) | 270 return pd.DataFrame(pathway_data) |
| 271 | 271 |
| 272 | 272 def set_annotation_pathways_from_data(model: cobraModel, df: pd.DataFrame): |
| 273 """Set reaction pathways based on 'Pathway_1', 'Pathway_2', ... columns in the dataframe.""" | |
| 274 pathway_cols = [col for col in df.columns if col.startswith('Pathway_')] | |
| 275 if not pathway_cols: | |
| 276 print("No 'Pathway_' columns found, skipping pathway annotation") | |
| 277 return | |
| 278 | |
| 279 pathway_data = defaultdict(list) | |
| 280 | |
| 281 for idx, row in df.iterrows(): | |
| 282 reaction_id = str(row['ReactionID']).strip() | |
| 283 if reaction_id not in model.reactions: | |
| 284 continue | |
| 285 | |
| 286 pathways = [] | |
| 287 for col in pathway_cols: | |
| 288 if pd.notna(row[col]) and str(row[col]).strip(): | |
| 289 pathways.append(str(row[col]).strip()) | |
| 290 | |
| 291 if pathways: | |
| 292 | |
| 293 reaction = model.reactions.get_by_id(reaction_id) | |
| 294 if len(pathways) == 1: | |
| 295 reaction.annotation['pathways'] = pathways[0] | |
| 296 else: | |
| 297 reaction.annotation['pathways'] = pathways | |
| 298 | |
| 299 pathway_data[reaction_id] = pathways | |
| 300 | |
| 301 print(f"Annotated {len(pathway_data)} reactions with pathways.") | |
| 273 | 302 |
| 274 def build_cobra_model_from_csv(csv_path: str, model_id: str = "new_model") -> cobraModel: | 303 def build_cobra_model_from_csv(csv_path: str, model_id: str = "new_model") -> cobraModel: |
| 275 """ | 304 """ |
| 276 Build a COBRApy model from a tabular file with reaction data. | 305 Build a COBRApy model from a tabular file with reaction data. |
| 277 | 306 |
| 364 | 393 |
| 365 # set objective function | 394 # set objective function |
| 366 set_objective_from_csv(model, df, obj_col="ObjectiveCoefficient") | 395 set_objective_from_csv(model, df, obj_col="ObjectiveCoefficient") |
| 367 | 396 |
| 368 set_medium_from_data(model, df) | 397 set_medium_from_data(model, df) |
| 398 | |
| 399 set_annotation_pathways_from_data(model, df) | |
| 369 | 400 |
| 370 print(f"Model completed: {len(model.reactions)} reactions, {len(model.metabolites)} metabolites") | 401 print(f"Model completed: {len(model.reactions)} reactions, {len(model.metabolites)} metabolites") |
| 371 | 402 |
| 372 return model | 403 return model |
| 373 | 404 |
