|
| 1 | +#' runAstir_celltype |
| 2 | +#' |
| 3 | +#' @title runAstir_celltype |
| 4 | +#' @description Predict cell types using Astir. This package requires the python library `astir` and `reticulate` to work. |
| 5 | +#' @param fcd Flow cytometry dataset. |
| 6 | +#' @param data_slot Data slot to use for the analysis (e.g. "orig" or "norm"). |
| 7 | +#' @param analysis_path Full path to the output folder of astir analysis. |
| 8 | +#' @param manifest_name Filename of the manifest file, this file must be located in the `analysis_path` folder. |
| 9 | +#' @param max_epochs Maximum number of epochs. |
| 10 | +#' @param learning_rate Learning Rate. |
| 11 | +#' @param initial_epochs Initial Epochs. |
| 12 | +#' @import reticulate |
| 13 | +#' @return runAstir_celltype |
| 14 | +#' |
| 15 | +#' @export |
| 16 | +runAstir_celltype <- function(fcd, |
| 17 | + data_slot, |
| 18 | + analysis_path, |
| 19 | + manifest_name, |
| 20 | + max_epochs, |
| 21 | + learning_rate, |
| 22 | + initial_epochs) { |
| 23 | + |
| 24 | + # Save the expression matrix as csv |
| 25 | + write.csv(x = fcd$expr[[data_slot]], file = paste0(analysis_path, "expr.csv")) |
| 26 | + |
| 27 | + # Import the astir python package |
| 28 | + ast_fun <- reticulate::import("astir") |
| 29 | + |
| 30 | + # Set the directories for the analysis |
| 31 | + expr_path <- paste0(analysis_path, "expr.csv") |
| 32 | + |
| 33 | + manifest_path <- paste0(analysis_path, manifest_name) |
| 34 | + |
| 35 | + ast <- ast_fun$from_csv_yaml(csv_input = expr_path, marker_yaml = manifest_path) |
| 36 | + |
| 37 | + batch_size = dim(ast$get_type_dataset()$get_exprs_df())[1]/100 |
| 38 | + |
| 39 | + ast$fit_type(max_epochs = as.integer(max_epochs), |
| 40 | + batch_size = as.integer(batch_size), |
| 41 | + learning_rate = learning_rate, |
| 42 | + n_init_epochs = as.integer(initial_epochs)) |
| 43 | + |
| 44 | + print(table(ast$get_celltypes())) |
| 45 | + |
| 46 | + probabilities <- ast$get_celltype_probabilities() |
| 47 | + |
| 48 | + diagnostic <- ast$diagnostics_celltype() |
| 49 | + |
| 50 | + cell_types <- ast$get_celltypes() |
| 51 | + |
| 52 | + write.csv(x = probabilities, file = paste0(analysis_path, "probabilities.csv")) |
| 53 | + |
| 54 | + write.csv(x = diagnostic, file = paste0(analysis_path, "diagnostic.csv")) |
| 55 | + |
| 56 | + write.csv(x = cell_types, file = paste0(analysis_path, "cell_types.csv")) |
| 57 | + |
| 58 | + # Add the cell type prediction in the condor object |
| 59 | + |
| 60 | + # Prepare the dataframe |
| 61 | + df <- data.frame(cell_type = cell_types, |
| 62 | + Description = paste0("Astir_cell_type_", data_slot, |
| 63 | + "_Max_Epoc_", max_epochs, |
| 64 | + "Learning_Rate_", learning_rate, |
| 65 | + "_Initial_Epochs_", initial_epochs)) |
| 66 | + |
| 67 | + fcd[["astir"]][[paste0("Astir_cell_type_", data_slot)]] <- df |
| 68 | + |
| 69 | + return(fcd) |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +#' runAstir_cellstates |
| 74 | +#' |
| 75 | +#' @title runAstir_cellstates |
| 76 | +#' @description Predict cell states using Astir. This package requires the python library `astir` and `reticulate` to work. |
| 77 | +#' @param fcd Flow cytometry dataset. |
| 78 | +#' @param data_slot Data slot to use for the analysis (e.g. "orig" or "norm"). |
| 79 | +#' @param analysis_path Full path to the output folder of astir analysis. |
| 80 | +#' @param manifest_name Filename of the manifest file, this file must be located in the `analysis_path` folder. |
| 81 | +#' @param max_epochs Maximum number of epochs. |
| 82 | +#' @param learning_rate Learning Rate. |
| 83 | +#' @param initial_epochs Initial Epochs. |
| 84 | +#' @import reticulate |
| 85 | +#' @importFrom utils write.csv |
| 86 | +#' @return runAstir_cellstates |
| 87 | +#' |
| 88 | +#' @export |
| 89 | +runAstir_cellstates <- function(fcd, |
| 90 | + data_slot, |
| 91 | + analysis_path, |
| 92 | + manifest_name, |
| 93 | + max_epochs, |
| 94 | + learning_rate, |
| 95 | + initial_epochs) { |
| 96 | + |
| 97 | + # Save the expression matrix as csv |
| 98 | + write.csv(x = fcd$expr[[data_slot]], file = paste0(analysis_path, "expr.csv")) |
| 99 | + |
| 100 | + # Import the astir python package |
| 101 | + ast_fun <- reticulate::import("astir") |
| 102 | + |
| 103 | + # Set the directories for the analysis |
| 104 | + expr_path <- paste0(analysis_path, "expr.csv") |
| 105 | + |
| 106 | + manifest_path <- paste0(analysis_path, manifest_name) |
| 107 | + |
| 108 | + # Parpare to run astir |
| 109 | + ast <- ast_fun$from_csv_yaml(csv_input = expr_path, marker_yaml = manifest_path) |
| 110 | + |
| 111 | + batch_size = dim(ast$get_type_dataset()$get_exprs_df())[1]/100 |
| 112 | + |
| 113 | + ast$fit_state(max_epochs = as.integer(max_epochs), |
| 114 | + batch_size = as.integer(batch_size), |
| 115 | + learning_rate = learning_rate, |
| 116 | + n_init_epochs = as.integer(initial_epochs)) |
| 117 | + |
| 118 | + diagnostic <- ast$diagnostics_cellstate() |
| 119 | + |
| 120 | + cell_states <- ast$get_cellstates() |
| 121 | + |
| 122 | + write.csv(x = diagnostic, file = paste0(analysis_path, "diagnostic.csv")) |
| 123 | + |
| 124 | + write.csv(x = cell_states, file = paste0(analysis_path, "cell_states.csv")) |
| 125 | + |
| 126 | + # Add the cell type prediction in the condor object |
| 127 | + |
| 128 | + # Prepare the dataframe |
| 129 | + df <- data.frame(cell_states, |
| 130 | + Description = paste0("Astir_cell_state_", data_slot, |
| 131 | + "_Max_Epoc_", max_epochs, |
| 132 | + "Learning_Rate_", learning_rate, |
| 133 | + "_Initial_Epochs_", initial_epochs)) |
| 134 | + |
| 135 | + fcd[["astir"]][[paste0("Astir_cell_state_", data_slot)]] <- df |
| 136 | + |
| 137 | + return(fcd) |
| 138 | + |
| 139 | +} |
0 commit comments