Mercurial > repos > bimib > cobraxy
comparison COBRAxy/ras_to_bounds.py @ 216:b162b98f9de5 draft
Uploaded
| author | luca_milaz | 
|---|---|
| date | Fri, 13 Dec 2024 18:46:12 +0000 | 
| parents | 00a66b9bc29e | 
| children | 8d1988935e1f | 
   comparison
  equal
  deleted
  inserted
  replaced
| 215:5cc4a367ef70 | 216:b162b98f9de5 | 
|---|---|
| 114 if len(dataset.columns) < 2: | 114 if len(dataset.columns) < 2: | 
| 115 sys.exit('Execution aborted: wrong format of ' + name + '\n') | 115 sys.exit('Execution aborted: wrong format of ' + name + '\n') | 
| 116 return dataset | 116 return dataset | 
| 117 | 117 | 
| 118 | 118 | 
| 119 def apply_ras_bounds(model, ras_row): | 119 def apply_ras_bounds(bounds, ras_row): | 
| 120 """ | 120 """ | 
| 121 Adjust the bounds of reactions in the model based on RAS values. | 121 Adjust the bounds of reactions in the model based on RAS values. | 
| 122 | 122 | 
| 123 Args: | 123 Args: | 
| 124 model (cobra.Model): The metabolic model to be modified. | 124 bounds (pd.DataFrame): Model bounds. | 
| 125 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds. | 125 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds. | 
| 126 Returns: | 126 Returns: | 
| 127 None | 127 new_bounds (pd.DataFrame): integrated bounds. | 
| 128 """ | 128 """ | 
| 129 new_bounds = bounds.copy() | |
| 129 for reaction in ras_row.index: | 130 for reaction in ras_row.index: | 
| 130 scaling_factor = ras_row[reaction] | 131 scaling_factor = ras_row[reaction] | 
| 131 lower_bound=model.reactions.get_by_id(reaction).lower_bound | 132 lower_bound=bounds.loc[reaction, "lower_bound"] | 
| 132 upper_bound=model.reactions.get_by_id(reaction).upper_bound | 133 upper_bound=bounds.loc[reaction, "upper_bound"] | 
| 133 valMax=float((upper_bound)*scaling_factor) | 134 valMax=float((upper_bound)*scaling_factor) | 
| 134 valMin=float((lower_bound)*scaling_factor) | 135 valMin=float((lower_bound)*scaling_factor) | 
| 135 if upper_bound!=0 and lower_bound==0: | 136 if upper_bound!=0 and lower_bound==0: | 
| 136 model.reactions.get_by_id(reaction).upper_bound=valMax | 137 new_bounds.loc[reaction, "upper_bound"] = valMax | 
| 137 if upper_bound==0 and lower_bound!=0: | 138 if upper_bound==0 and lower_bound!=0: | 
| 138 model.reactions.get_by_id(reaction).lower_bound=valMin | 139 new_bounds.loc[reaction, "lower_bound"] = valMin | 
| 139 if upper_bound!=0 and lower_bound!=0: | 140 if upper_bound!=0 and lower_bound!=0: | 
| 140 model.reactions.get_by_id(reaction).lower_bound=valMin | 141 new_bounds.loc[reaction, "lower_bound"] = valMin | 
| 141 model.reactions.get_by_id(reaction).upper_bound=valMax | 142 new_bounds.loc[reaction, "upper_bound"] = valMax | 
| 142 pass | 143 return new_bounds | 
| 143 | 144 | 
| 144 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder): | 145 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder): | 
| 145 """ | 146 """ | 
| 146 Process a single RAS cell, apply bounds, and save the bounds to a CSV file. | 147 Process a single RAS cell, apply bounds, and save the bounds to a CSV file. | 
| 147 | 148 | 
| 153 output_folder (str): Folder path where the output CSV file will be saved. | 154 output_folder (str): Folder path where the output CSV file will be saved. | 
| 154 | 155 | 
| 155 Returns: | 156 Returns: | 
| 156 None | 157 None | 
| 157 """ | 158 """ | 
| 158 model_new = model.copy() | 159 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 
| 159 apply_ras_bounds(model_new, ras_row) | 160 new_bounds = apply_ras_bounds(bounds, ras_row) | 
| 160 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 161 new_bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True) | 
| 161 bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True) | |
| 162 pass | 162 pass | 
| 163 | 163 | 
| 164 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame: | 164 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame: | 
| 165 """ | 165 """ | 
| 166 Generate reaction bounds for a metabolic model based on medium conditions and optional RAS adjustments. | 166 Generate reaction bounds for a metabolic model based on medium conditions and optional RAS adjustments. | 
| 195 model.reactions.get_by_id(reaction).upper_bound = float(df_FVA.loc[reaction, "maximum"]) | 195 model.reactions.get_by_id(reaction).upper_bound = float(df_FVA.loc[reaction, "maximum"]) | 
| 196 | 196 | 
| 197 if ras is not None: | 197 if ras is not None: | 
| 198 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows()) | 198 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows()) | 
| 199 else: | 199 else: | 
| 200 model_new = model.copy() | 200 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 
| 201 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids)) | 201 newBounds = apply_ras_bounds(bounds, pd.Series([1]*len(rxns_ids), index=rxns_ids)) | 
| 202 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 202 newBounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) | 
| 203 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) | |
| 204 pass | 203 pass | 
| 205 | 204 | 
| 206 | 205 | 
| 207 | 206 | 
| 208 ############################# main ########################################### | 207 ############################# main ########################################### | 
