Mercurial > repos > bimib > cobraxy
comparison COBRAxy/ras_to_bounds.py @ 113:1961a091484c draft
Uploaded
| author | luca_milaz |
|---|---|
| date | Sun, 13 Oct 2024 15:55:16 +0000 |
| parents | 4ea14da7043b |
| children | a7a079f6a4cd |
comparison
equal
deleted
inserted
replaced
| 112:4ea14da7043b | 113:1961a091484c |
|---|---|
| 109 if len(dataset.columns) < 2: | 109 if len(dataset.columns) < 2: |
| 110 sys.exit('Execution aborted: wrong format of ' + name + '\n') | 110 sys.exit('Execution aborted: wrong format of ' + name + '\n') |
| 111 return dataset | 111 return dataset |
| 112 | 112 |
| 113 | 113 |
| 114 def apply_ras_bounds(model, ras_row, rxns_ids): | 114 def apply_ras_bounds(model, ras_row, rxns_ids, mediumRxns_ids): |
| 115 """ | 115 """ |
| 116 Adjust the bounds of reactions in the model based on RAS values. | 116 Adjust the bounds of reactions in the model based on RAS values. |
| 117 | 117 |
| 118 Args: | 118 Args: |
| 119 model (cobra.Model): The metabolic model to be modified. | 119 model (cobra.Model): The metabolic model to be modified. |
| 120 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds. | 120 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds. |
| 121 rxns_ids (list of str): List of reaction IDs to which the scaling factors will be applied. | 121 rxns_ids (list of str): List of reaction IDs to which the scaling factors will be applied. |
| 122 | 122 mediumRxns_ids (list of str): List of reaction IDs in the medium. Their RAS is set to zero, but they are already set in the model. |
| 123 Returns: | 123 Returns: |
| 124 None | 124 None |
| 125 """ | 125 """ |
| 126 for reaction in rxns_ids: | 126 for reaction in rxns_ids: |
| 127 if reaction in ras_row.index: | 127 if reaction in ras_row.index and ras_row[reaction] not in mediumRxns_ids: |
| 128 scaling_factor = ras_row[reaction] | 128 scaling_factor = ras_row[reaction] |
| 129 lower_bound=model.reactions.get_by_id(reaction).lower_bound | 129 lower_bound=model.reactions.get_by_id(reaction).lower_bound |
| 130 upper_bound=model.reactions.get_by_id(reaction).upper_bound | 130 upper_bound=model.reactions.get_by_id(reaction).upper_bound |
| 131 valMax=float((upper_bound)*scaling_factor) | 131 valMax=float((upper_bound)*scaling_factor) |
| 132 valMin=float((lower_bound)*scaling_factor) | 132 valMin=float((lower_bound)*scaling_factor) |
| 137 if upper_bound!=0 and lower_bound!=0: | 137 if upper_bound!=0 and lower_bound!=0: |
| 138 model.reactions.get_by_id(reaction).lower_bound=valMin | 138 model.reactions.get_by_id(reaction).lower_bound=valMin |
| 139 model.reactions.get_by_id(reaction).upper_bound=valMax | 139 model.reactions.get_by_id(reaction).upper_bound=valMax |
| 140 pass | 140 pass |
| 141 | 141 |
| 142 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder): | 142 def process_ras_cell(cellName, ras_row, model, rxns_ids, mediumRxns_ids, output_folder): |
| 143 """ | 143 """ |
| 144 Process a single RAS cell, apply bounds, and save the bounds to a CSV file. | 144 Process a single RAS cell, apply bounds, and save the bounds to a CSV file. |
| 145 | 145 |
| 146 Args: | 146 Args: |
| 147 cellName (str): The name of the RAS cell (used for naming the output file). | 147 cellName (str): The name of the RAS cell (used for naming the output file). |
| 148 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds. | 148 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds. |
| 149 model (cobra.Model): The metabolic model to be modified. | 149 model (cobra.Model): The metabolic model to be modified. |
| 150 rxns_ids (list of str): List of reaction IDs to which the scaling factors will be applied. | 150 rxns_ids (list of str): List of reaction IDs to which the scaling factors will be applied. |
| 151 mediumRxns_ids (list of str): List of reaction IDs in the medium. Their RAS is set to zero, but they are already set in the model. | |
| 151 output_folder (str): Folder path where the output CSV file will be saved. | 152 output_folder (str): Folder path where the output CSV file will be saved. |
| 152 | 153 |
| 153 Returns: | 154 Returns: |
| 154 None | 155 None |
| 155 """ | 156 """ |
| 156 model_new = model.copy() | 157 model_new = model.copy() |
| 157 apply_ras_bounds(model_new, ras_row, rxns_ids) | 158 apply_ras_bounds(model_new, ras_row, rxns_ids, mediumRxns_ids) |
| 158 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 159 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) |
| 159 bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True) | 160 bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True) |
| 160 pass | 161 pass |
| 161 | 162 |
| 162 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame: | 163 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame: |
| 179 if value is not None: | 180 if value is not None: |
| 180 if(reaction != "EX_thbpt_e" and reaction != "EX_lac__L_e"): | 181 if(reaction != "EX_thbpt_e" and reaction != "EX_lac__L_e"): |
| 181 model.reactions.get_by_id(reaction).lower_bound = -float(value) | 182 model.reactions.get_by_id(reaction).lower_bound = -float(value) |
| 182 if(reaction == "EX_lac__L_e"): | 183 if(reaction == "EX_lac__L_e"): |
| 183 model.reactions.get_by_id(reaction).lower_bound = float(0.0) | 184 model.reactions.get_by_id(reaction).lower_bound = float(0.0) |
| 185 | |
| 186 mediumRxns_ids = medium.keys() | |
| 184 | 187 |
| 185 # Perform Flux Variability Analysis (FVA) | 188 # Perform Flux Variability Analysis (FVA) |
| 186 df_FVA = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8) | 189 df_FVA = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8) |
| 187 | 190 |
| 188 # Set FVA bounds | 191 # Set FVA bounds |
| 189 for reaction in rxns_ids: | 192 for reaction in rxns_ids: |
| 190 model.reactions.get_by_id(reaction).lower_bound = float(df_FVA.loc[reaction, "minimum"]) | 193 model.reactions.get_by_id(reaction).lower_bound = float(df_FVA.loc[reaction, "minimum"]) |
| 191 model.reactions.get_by_id(reaction).upper_bound = float(df_FVA.loc[reaction, "maximum"]) | 194 model.reactions.get_by_id(reaction).upper_bound = float(df_FVA.loc[reaction, "maximum"]) |
| 192 | 195 |
| 193 warning(str(model.reactions.get_by_id("EX_Lcystin_e").lower_bound)) | 196 #warning(str(model.reactions.get_by_id("EX_Lcystin_e").lower_bound)) |
| 194 | 197 |
| 195 if ras is not None: | 198 if ras is not None: |
| 196 #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 #Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows()) |
| 197 for cellName, ras_row in ras.iterrows(): | 200 for cellName, ras_row in ras.iterrows(): |
| 198 process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder) | 201 process_ras_cell(cellName, ras_row, model, rxns_ids, mediumRxns_ids, output_folder) |
| 199 break #just one cell for testing | 202 break #just one cell for testing |
| 200 else: | 203 else: |
| 201 model_new = model.copy() | 204 model_new = model.copy() |
| 202 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids) | 205 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids, mediumRxns_ids) |
| 203 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 206 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) |
| 204 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) | 207 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) |
| 205 pass | 208 pass |
| 206 | 209 |
| 207 | 210 |
