Mercurial > repos > bimib > cobraxy
comparison COBRAxy/ras_generator.py @ 531:1fc5c1edb0ef draft default tip
Uploaded
author | francesco_lapi |
---|---|
date | Wed, 22 Oct 2025 13:30:18 +0000 |
parents | 352c51a39e23 |
children |
comparison
equal
deleted
inserted
replaced
530:352c51a39e23 | 531:1fc5c1edb0ef |
---|---|
280 self.count_df_filtered = dataset_normalized.loc[self.genes] | 280 self.count_df_filtered = dataset_normalized.loc[self.genes] |
281 | 281 |
282 def compute(self, | 282 def compute(self, |
283 or_expression=np.sum, # type of operation to do in case of an or expression (sum, max, mean) | 283 or_expression=np.sum, # type of operation to do in case of an or expression (sum, max, mean) |
284 and_expression=np.min, # type of operation to do in case of an and expression(min, sum) | 284 and_expression=np.min, # type of operation to do in case of an and expression(min, sum) |
285 drop_na_rows=True, # if True remove the nan rows of the ras matrix | 285 drop_na_rows=False, # if True remove the nan rows of the ras matrix |
286 drop_duplicates=False, # if true, remove duplicates rows | 286 drop_duplicates=False, # if true, remove duplicates rows |
287 ignore_nan=True, # if True, ignore NaN values in GPR evaluation (e.g., A or NaN -> A) | 287 ignore_nan=True, # if True, ignore NaN values in GPR evaluation (e.g., A or NaN -> A) |
288 print_progressbar=True, # if True, print the progress bar | 288 print_progressbar=True, # if True, print the progress bar |
289 add_count_metadata=True, # if True add metadata of cells in the ras adata | 289 add_count_metadata=True, # if True add metadata of cells in the ras adata |
290 add_met_metadata=True, # if True add metadata from the metabolic model (gpr and compartments of reactions) | 290 add_met_metadata=True, # if True add metadata from the metabolic model (gpr and compartments of reactions) |
325 # Some genes missing and we don't ignore NaN - set to NaN | 325 # Some genes missing and we don't ignore NaN - set to NaN |
326 pass | 326 pass |
327 else: | 327 else: |
328 # Evaluate the GPR expression using AST | 328 # Evaluate the GPR expression using AST |
329 # For single gene, AST handles it fine: ast.parse("GENE_A") works | 329 # For single gene, AST handles it fine: ast.parse("GENE_A") works |
330 try: | 330 # more genes in the formula |
331 check_only_and=("and" in rule and "or" not in rule) #only and | |
332 check_only_or=("or" in rule and "and" not in rule) #only or | |
333 if check_only_and or check_only_or: | |
334 #or/and sequence | |
335 matrix = self.count_df_filtered.loc[genes_present].values | |
336 #compute for all cells | |
337 if check_only_and: | |
338 ras_df[ind] = self.and_function(matrix, axis=0) | |
339 else: | |
340 ras_df[ind] = self.or_function(matrix, axis=0) | |
341 else: | |
342 # complex expression (e.g. A or (B and C)) | |
343 data = self.count_df_filtered.loc[genes_present] # dataframe of genes in the GPRs | |
331 tree = ast.parse(rule, mode="eval").body | 344 tree = ast.parse(rule, mode="eval").body |
332 data = self.count_df_filtered.loc[genes_present] | 345 values_by_cell = [dict(zip(data.index, data[col].values)) for col in data.columns] |
333 | 346 for j, values in enumerate(values_by_cell): |
334 # Evaluate for each cell/sample | 347 ras_df[ind, j] =self._evaluate_ast(tree, values, self.or_function, self.and_function, ignore_nan) |
335 for j, col in enumerate(data.columns): | 348 |
336 gene_values = dict(zip(data.index, data[col].values)) | |
337 ras_df[ind, j] = self._evaluate_ast(tree, gene_values, self.or_function, self.and_function, ignore_nan) | |
338 except: | |
339 # If parsing fails, keep as NaN | |
340 pass | |
341 | |
342 if print_progressbar: | 349 if print_progressbar: |
343 pbar.update(ind + 1) | 350 pbar.update(ind + 1) |
344 | 351 |
345 if print_progressbar: | 352 if print_progressbar: |
346 pbar.finish() | 353 pbar.finish() |