Mercurial > repos > bimib > cobraxy
comparison COBRAxy/ras_to_bounds.py @ 48:fac6930e6385 draft
Uploaded
| author | luca_milaz |
|---|---|
| date | Sat, 12 Oct 2024 19:57:09 +0000 |
| parents | 41f35c2f0c7b |
| children | 47153fe3d59e |
comparison
equal
deleted
inserted
replaced
| 47:7b07b05bdb5d | 48:fac6930e6385 |
|---|---|
| 57 parser.add_argument('-rs', '--ras_selector', | 57 parser.add_argument('-rs', '--ras_selector', |
| 58 required = True, | 58 required = True, |
| 59 type=utils.Bool("using_RAS"), | 59 type=utils.Bool("using_RAS"), |
| 60 help = 'ras selector') | 60 help = 'ras selector') |
| 61 | 61 |
| 62 parser.add_argument('-c', '--classes', | |
| 63 type = str, | |
| 64 help = 'input classes') | |
| 65 | |
| 66 parser.add_argument('-cc', '--cell_class', | |
| 67 type = str, | |
| 68 help = 'output of cell class') | |
| 69 | |
| 62 ARGS = parser.parse_args() | 70 ARGS = parser.parse_args() |
| 63 return ARGS | 71 return ARGS |
| 64 | 72 |
| 65 ########################### warning ########################################### | 73 ########################### warning ########################################### |
| 66 def warning(s :str) -> None: | 74 def warning(s :str) -> None: |
| 113 | 121 |
| 114 Returns: | 122 Returns: |
| 115 None | 123 None |
| 116 """ | 124 """ |
| 117 for reaction in rxns_ids: | 125 for reaction in rxns_ids: |
| 118 if reaction in ras_row.index and pd.notna(ras_row[reaction]): | 126 if reaction in ras_row.index: |
| 119 rxn = model.reactions.get_by_id(reaction) | |
| 120 scaling_factor = ras_row[reaction] | 127 scaling_factor = ras_row[reaction] |
| 121 rxn.lower_bound *= scaling_factor | 128 lower_bound=model.reactions.get_by_id(reaction).lower_bound |
| 122 rxn.upper_bound *= scaling_factor | 129 upper_bound=model.reactions.get_by_id(reaction).upper_bound |
| 130 valMax=float((upper_bound)*scaling_factor) | |
| 131 valMin=float((lower_bound)*scaling_factor) | |
| 132 if upper_bound!=0 and lower_bound==0: | |
| 133 model.reactions.get_by_id(reaction).upper_bound=valMax | |
| 134 if upper_bound==0 and lower_bound!=0: | |
| 135 model.reactions.get_by_id(reaction).lower_bound=valMin | |
| 136 if upper_bound!=0 and lower_bound!=0: | |
| 137 model.reactions.get_by_id(reaction).lower_bound=valMin | |
| 138 model.reactions.get_by_id(reaction).upper_bound=valMax | |
| 139 pass | |
| 123 | 140 |
| 124 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder): | 141 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder): |
| 125 """ | 142 """ |
| 126 Process a single RAS cell, apply bounds, and save the bounds to a CSV file. | 143 Process a single RAS cell, apply bounds, and save the bounds to a CSV file. |
| 127 | 144 |
| 137 """ | 154 """ |
| 138 model_new = model.copy() | 155 model_new = model.copy() |
| 139 apply_ras_bounds(model_new, ras_row, rxns_ids) | 156 apply_ras_bounds(model_new, ras_row, rxns_ids) |
| 140 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 157 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) |
| 141 bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True) | 158 bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True) |
| 159 pass | |
| 142 | 160 |
| 143 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame: | 161 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame: |
| 144 """ | 162 """ |
| 145 Generate reaction bounds for a metabolic model based on medium conditions and optional RAS adjustments. | 163 Generate reaction bounds for a metabolic model based on medium conditions and optional RAS adjustments. |
| 146 | 164 |
| 147 Args: | 165 Args: |
| 148 model (cobra.Model): The metabolic model for which bounds will be generated. | 166 model (cobra.Model): The metabolic model for which bounds will be generated. |
| 149 medium (dict): A dictionary where keys are reaction IDs and values are the medium conditions. | 167 medium (dict): A dictionary where keys are reaction IDs and values are the medium conditions. |
| 150 ras (pd.DataFrame, optional): A DataFrame with RAS scaling factors for different cell types. Defaults to None. | 168 ras (pd.DataFrame, optional): RAS pandas dataframe. Defaults to None. |
| 151 output_folder (str, optional): Folder path where output CSV files will be saved. Defaults to 'output/'. | 169 output_folder (str, optional): Folder path where output CSV files will be saved. Defaults to 'output/'. |
| 152 | 170 |
| 153 Returns: | 171 Returns: |
| 154 pd.DataFrame: DataFrame containing the bounds of reactions in the model. | 172 pd.DataFrame: DataFrame containing the bounds of reactions in the model. |
| 155 """ | 173 """ |
| 174 else: | 192 else: |
| 175 model_new = model.copy() | 193 model_new = model.copy() |
| 176 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids) | 194 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids) |
| 177 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) | 195 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) |
| 178 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) | 196 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) |
| 197 pass | |
| 198 | |
| 179 | 199 |
| 180 | 200 |
| 181 ############################# main ########################################### | 201 ############################# main ########################################### |
| 182 def main() -> None: | 202 def main() -> None: |
| 183 """ | 203 """ |
| 194 ARGS = process_args(sys.argv) | 214 ARGS = process_args(sys.argv) |
| 195 | 215 |
| 196 ARGS.output_folder = 'ras_to_bounds/' | 216 ARGS.output_folder = 'ras_to_bounds/' |
| 197 | 217 |
| 198 if(ARGS.ras_selector == True): | 218 if(ARGS.ras_selector == True): |
| 199 ras = read_dataset(ARGS.input_ras, "ras dataset") | 219 ras_file_list = ARGS.ras_selector.split(",") |
| 200 ras.replace("None", None, inplace=True) | 220 if(len(ras_list)>1): |
| 201 ras.set_index("Reactions", drop=True, inplace=True) | 221 ras_class_names = [cls.strip() for cls in ARGS.classes.split(',')] |
| 202 ras = ras.T | 222 else: |
| 203 ras = ras.astype(float) | 223 ras_class_names = ["placeHolder"] |
| 224 ras_list = [] | |
| 225 class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"]) | |
| 226 for ras_matrix, ras_class_name in ras_file_list, ras_class_names: | |
| 227 ras = read_dataset(ras_matrix, "ras dataset") | |
| 228 ras.replace("None", None, inplace=True) | |
| 229 ras.set_index("Reactions", drop=True, inplace=True) | |
| 230 ras = ras.T | |
| 231 ras = ras.astype(float) | |
| 232 ras_list.append(ras) | |
| 233 for patient_id in ras.index: | |
| 234 class_assignments = class_assignments.append({"Patient_ID": patient_id, "Class": ras_class_name}, ignore_index=True) | |
| 235 | |
| 236 # Concatenate all ras DataFrames into a single DataFrame | |
| 237 ras_combined = pd.concat(ras_list, axis=1) | |
| 238 # Normalize the RAS values by max RAS | |
| 239 ras_combined = ras_combined.div(ras_combined.max(axis=0)) | |
| 240 ras_combined = ras_combined.fillna(0) | |
| 241 | |
| 242 | |
| 204 | 243 |
| 205 model_type :utils.Model = ARGS.model_selector | 244 model_type :utils.Model = ARGS.model_selector |
| 206 if model_type is utils.Model.Custom: | 245 if model_type is utils.Model.Custom: |
| 207 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext) | 246 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext) |
| 208 else: | 247 else: |
| 218 ARGS.medium_selector = ARGS.medium_selector.replace("_", " ") | 257 ARGS.medium_selector = ARGS.medium_selector.replace("_", " ") |
| 219 medium = df_mediums[[ARGS.medium_selector]] | 258 medium = df_mediums[[ARGS.medium_selector]] |
| 220 medium = medium[ARGS.medium_selector].to_dict() | 259 medium = medium[ARGS.medium_selector].to_dict() |
| 221 | 260 |
| 222 if(ARGS.ras_selector == True): | 261 if(ARGS.ras_selector == True): |
| 223 generate_bounds(model, medium, ras = ras, output_folder=ARGS.output_folder) | 262 generate_bounds(model, medium, ras = ras_combined, output_folder=ARGS.output_folder) |
| 263 if(len(ras_list)>1): | |
| 264 class_assignments.to_csv(ARGS.cell_class, sep = '\t', index = False) | |
| 224 else: | 265 else: |
| 225 generate_bounds(model, medium, output_folder=ARGS.output_folder) | 266 generate_bounds(model, medium, output_folder=ARGS.output_folder) |
| 226 | 267 |
| 227 pass | 268 pass |
| 228 | 269 |
