Mercurial > repos > bimib > cobraxy
comparison COBRAxy/utils/model_utils.py @ 506:ffc234ec80db draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Wed, 01 Oct 2025 13:19:03 +0000 |
| parents | 96f512dff490 |
| children | ca98c149ec61 |
comparison
equal
deleted
inserted
replaced
| 505:96f512dff490 | 506:ffc234ec80db |
|---|---|
| 221 | 221 |
| 222 | 222 |
| 223 | 223 |
| 224 def generate_compartments(model: cobraModel) -> pd.DataFrame: | 224 def generate_compartments(model: cobraModel) -> pd.DataFrame: |
| 225 """ | 225 """ |
| 226 Generates a DataFrame containing compartment information for each reaction. | 226 Generates a DataFrame containing pathway information for each reaction. |
| 227 Creates columns for each compartment position (Compartment_1, Compartment_2, etc.) | 227 Creates columns for each pathway position (Pathway_1, Pathway_2, etc.) only if pathways exist. |
| 228 | 228 |
| 229 Args: | 229 Args: |
| 230 model: the COBRA model to extract compartment data from. | 230 model: the COBRA model to extract pathway data from. |
| 231 | 231 |
| 232 Returns: | 232 Returns: |
| 233 pd.DataFrame: DataFrame with ReactionID and compartment columns | 233 pd.DataFrame: DataFrame with ReactionID and pathway columns (if any pathways exist) |
| 234 """ | 234 """ |
| 235 pathway_data = [] | 235 pathway_data = [] |
| 236 | 236 |
| 237 # First pass: determine the maximum number of pathways any reaction has | 237 # First pass: determine the maximum number of pathways any reaction has |
| 238 max_pathways = 0 | 238 max_pathways = 0 |
| 239 reaction_pathways = {} | 239 reaction_pathways = {} |
| 240 has_any_pathways = False | |
| 240 | 241 |
| 241 for reaction in model.reactions: | 242 for reaction in model.reactions: |
| 242 # Get unique pathways from all metabolites in the reaction | 243 # Get unique pathways from all metabolites in the reaction |
| 243 if 'pathways' in reaction.annotation: | 244 if 'pathways' in reaction.annotation and reaction.annotation['pathways']: |
| 244 if type(reaction.annotation['pathways']) == list: | 245 if type(reaction.annotation['pathways']) == list: |
| 245 reaction_pathways[reaction.id] = reaction.annotation['pathways'] | 246 # Filter out empty/None values |
| 246 max_pathways = max(max_pathways, len(reaction.annotation['pathways'])) | 247 valid_pathways = [p for p in reaction.annotation['pathways'] if p] |
| 248 if valid_pathways: | |
| 249 reaction_pathways[reaction.id] = valid_pathways | |
| 250 max_pathways = max(max_pathways, len(valid_pathways)) | |
| 251 has_any_pathways = True | |
| 252 else: | |
| 253 reaction_pathways[reaction.id] = [] | |
| 247 else: | 254 else: |
| 248 reaction_pathways[reaction.id] = [reaction.annotation['pathways']] | 255 # Single pathway value |
| 256 if reaction.annotation['pathways']: | |
| 257 reaction_pathways[reaction.id] = [reaction.annotation['pathways']] | |
| 258 max_pathways = max(max_pathways, 1) | |
| 259 has_any_pathways = True | |
| 260 else: | |
| 261 reaction_pathways[reaction.id] = [] | |
| 249 else: | 262 else: |
| 250 # No pathway annotation - use empty list | 263 # No pathway annotation - use empty list |
| 251 reaction_pathways[reaction.id] = [] | 264 reaction_pathways[reaction.id] = [] |
| 252 | 265 |
| 253 # Create column names for pathways | 266 # If no pathways exist in the model, return DataFrame with only ReactionID |
| 267 if not has_any_pathways: | |
| 268 return None | |
| 269 | |
| 270 # Create column names for pathways only if they exist | |
| 254 pathway_columns = [f"Pathway_{i+1}" for i in range(max_pathways)] | 271 pathway_columns = [f"Pathway_{i+1}" for i in range(max_pathways)] |
| 255 | 272 |
| 256 # Second pass: create the data | 273 # Second pass: create the data with pathway columns |
| 257 for reaction_id, pathways in reaction_pathways.items(): | 274 for reaction_id, pathways in reaction_pathways.items(): |
| 258 row = {"ReactionID": reaction_id} | 275 row = {"ReactionID": reaction_id} |
| 259 | 276 |
| 260 # Fill pathway columns | 277 # Fill pathway columns |
| 261 for i in range(max_pathways): | 278 for i in range(max_pathways): |
| 262 col_name = pathway_columns[i] | 279 col_name = pathway_columns[i] |
| 263 if i < len(pathways): | 280 if i < len(pathways): |
| 264 row[col_name] = pathways[i] | 281 row[col_name] = pathways[i] |
| 265 else: | 282 else: |
| 266 row[col_name] = None # or "" if you prefer empty strings | 283 row[col_name] = None |
| 267 | 284 |
| 268 pathway_data.append(row) | 285 pathway_data.append(row) |
| 269 | 286 |
| 270 return pd.DataFrame(pathway_data) | 287 return pd.DataFrame(pathway_data) |
| 271 | 288 |
| 287 for col in pathway_cols: | 304 for col in pathway_cols: |
| 288 if pd.notna(row[col]) and str(row[col]).strip(): | 305 if pd.notna(row[col]) and str(row[col]).strip(): |
| 289 pathways.append(str(row[col]).strip()) | 306 pathways.append(str(row[col]).strip()) |
| 290 | 307 |
| 291 if pathways: | 308 if pathways: |
| 292 | 309 |
| 293 reaction = model.reactions.get_by_id(reaction_id) | 310 reaction = model.reactions.get_by_id(reaction_id) |
| 294 if len(pathways) == 1: | 311 if len(pathways) == 1: |
| 295 reaction.annotation['pathways'] = pathways[0] | 312 reaction.annotation['pathways'] = pathways[0] |
| 296 else: | 313 else: |
| 297 reaction.annotation['pathways'] = pathways | 314 reaction.annotation['pathways'] = pathways |
