|
| 1 | +requireNamespace("flowCore", quietly = TRUE) |
| 2 | +requireNamespace("anndata", quietly = TRUE) |
| 3 | +requireNamespace("Biobase", quietly = TRUE) |
| 4 | +requireNamespace("CytoNorm", quietly = TRUE) |
| 5 | + |
| 6 | +## VIASH START |
| 7 | +par <- list( |
| 8 | + input = "resources_test/task_cyto_batch_integration/cyto_spleen_subset/unintegrated_censored.h5ad", |
| 9 | + output = "resources_test/output.h5ad", |
| 10 | + som_grid_size = 10, |
| 11 | + num_metacluster = 10, |
| 12 | + n_quantiles = 99 |
| 13 | +) |
| 14 | +meta <- list( |
| 15 | + name = "cytonorm_control", |
| 16 | + temp_dir = "resources_test/task_cyto_batch_integration/tmp", |
| 17 | + resources_dir = "src/utils" |
| 18 | +) |
| 19 | +## VIASH END |
| 20 | + |
| 21 | +source(paste0(meta$resources_dir, "/anndata_to_fcs.R")) |
| 22 | + |
| 23 | +tmp_path <- meta[["temp_dir"]] |
| 24 | + |
| 25 | +cat("Reading input files\n") |
| 26 | +adata <- anndata::read_h5ad(par[["input"]]) |
| 27 | + |
| 28 | +cat("Preparing training data\n") |
| 29 | + |
| 30 | +# get the control samples to be used for training the model |
| 31 | +fset_train <- anndata_to_fcs(adata[adata$obs$is_control != 0, ]) |
| 32 | +# every sample, including the controls, pretty much the entire unintegrated data |
| 33 | +# will be corrected. |
| 34 | +fset_all <- anndata_to_fcs(adata) |
| 35 | + |
| 36 | +cat("Setting up some variables for training the model\n") |
| 37 | + |
| 38 | +# get batch label for the training data |
| 39 | +batch_lab_train <- vapply(sampleNames(fset_train), function(samp) { |
| 40 | + as.character( |
| 41 | + unique( |
| 42 | + adata[adata$obs$sample == samp]$obs$batch |
| 43 | + )[1] |
| 44 | + ) |
| 45 | +}, FUN.VALUE = character(1)) |
| 46 | + |
| 47 | +# get batch label for the all data |
| 48 | +batch_labs <- vapply(sampleNames(fset_all), function(samp) { |
| 49 | + as.character( |
| 50 | + unique( |
| 51 | + adata[adata$obs$sample == samp]$obs$batch |
| 52 | + )[1] |
| 53 | + ) |
| 54 | +}, FUN.VALUE = character(1)) |
| 55 | + |
| 56 | +markers_to_correct <- as.vector(adata$var$channel[adata$var$to_correct]) |
| 57 | + |
| 58 | +lineage_markers <- as.vector(adata$var$channel[adata$var$marker_type == "lineage"]) |
| 59 | + |
| 60 | +# get number of cells for clustering. |
| 61 | +# we will define this as the minimum of the smallest sample and 1,000,000. |
| 62 | +# and multiply this by how many samples we have - because internally, |
| 63 | +# this number is divided by the number of files to determine the amount to select from |
| 64 | +# each individual file. |
| 65 | +n_cells_per_control_sample <- flowCore::fsApply(fset_train, function(ff) nrow(exprs(ff))) |
| 66 | +n_cells_for_clustering <- min(n_cells_per_control_sample, 1000000) * length(n_cells_per_control_sample) |
| 67 | + |
| 68 | +cat("Training Cytonorm model using all control samples\n") |
| 69 | + |
| 70 | +# FlowSOM.params and normParams are the default parameters in cytonorm |
| 71 | +model <- CytoNorm::CytoNorm.train( |
| 72 | + files = fset_train, |
| 73 | + labels = batch_lab_train, |
| 74 | + channels = markers_to_correct, |
| 75 | + outputDir = tmp_path, |
| 76 | + FlowSOM.params = list( |
| 77 | + nCells = n_cells_for_clustering, |
| 78 | + xdim = par[["som_grid_size"]], |
| 79 | + ydim = par[["som_grid_size"]], |
| 80 | + nClus = par[["num_metacluster"]], |
| 81 | + scale = FALSE, |
| 82 | + colsToUse = lineage_markers |
| 83 | + ), |
| 84 | + transformList = NULL, |
| 85 | + normParams = list( |
| 86 | + nQ = par[["n_quantiles"]], |
| 87 | + goal = "1" |
| 88 | + ), |
| 89 | + seed = 42, |
| 90 | + verbose = FALSE, |
| 91 | + recompute = TRUE |
| 92 | +) |
| 93 | + |
| 94 | +cat("Normalising using Cytonorm model trained using all control samples\n") |
| 95 | + |
| 96 | +norm_fset_all <- CytoNorm::CytoNorm.normalize( |
| 97 | + model = model, |
| 98 | + files = fset_all, |
| 99 | + labels = batch_labs, |
| 100 | + transformList = NULL, |
| 101 | + transformList.reverse = NULL, |
| 102 | + outputDir = tmp_path, |
| 103 | + prefix = "Norm_", |
| 104 | + clean = TRUE, |
| 105 | + write = FALSE, |
| 106 | + verbose = FALSE |
| 107 | +) |
| 108 | + |
| 109 | +cat("Preparing output anndata\n") |
| 110 | +# cytonorm will return all markers corrected or not in the same order as the input data. |
| 111 | +# so we can just directly replace the colnames with var_names |
| 112 | +norm_mat <- flowCore::fsApply(norm_fset_all, exprs) |
| 113 | +colnames(norm_mat) <- adata$var_names |
| 114 | + |
| 115 | +norm_mat <- anndata::AnnData( |
| 116 | + obs = adata$obs[, integer(0)], |
| 117 | + var = adata$var[colnames(norm_mat), integer(0)], |
| 118 | + layers = list(integrated = norm_mat), |
| 119 | + uns = list( |
| 120 | + dataset_id = adata$uns$dataset_id, |
| 121 | + method_id = meta$name, |
| 122 | + parameters = list() |
| 123 | + ) |
| 124 | +) |
| 125 | + |
| 126 | +cat("Write output AnnData to file\n") |
| 127 | +norm_mat$write_h5ad(par[["output"]], compression = "gzip") |
0 commit comments