|
| 1 | +library(glue) |
| 2 | +library(dplyr) |
| 3 | +library(arrow) |
| 4 | +library(SingleCellExperiment) |
| 5 | +library(tibble) |
| 6 | +library(SummarizedExperiment) |
| 7 | +library(glue) |
| 8 | +library(purrr) |
| 9 | +library(zellkonverter) |
| 10 | +library(tidyr) |
| 11 | +library(ggplot2) |
| 12 | +library(plotly) |
| 13 | +library(targets) |
| 14 | +library(stringr) |
| 15 | +library(CuratedAtlasQueryR) |
| 16 | +library(fs) |
| 17 | +library(HPCell) |
| 18 | +library(crew.cluster) |
| 19 | +directory = "/home/users/allstaff/shen.m/scratch/Census_rerun/split_h5ad_based_on_sample_id/" |
| 20 | +sample_anndata <- dir(glue("{directory}"), full.names = T) |
| 21 | +downloaded_samples_tbl <- read_parquet("/home/users/allstaff/shen.m/scratch/Census_rerun/census_samples_to_download_groups.parquet") |
| 22 | +downloaded_samples_tbl <- downloaded_samples_tbl |> |
| 23 | + rename(cell_number = list_length) |> |
| 24 | + mutate(cell_number = cell_number |> as.integer(), |
| 25 | + file_name = glue("{directory}{sample_2}.h5ad") |> as.character(), |
| 26 | + tier = case_when( |
| 27 | + cell_number < 500 ~ "tier_1", cell_number >= 500 & |
| 28 | + cell_number < 1000 ~ "tier_2", cell_number >= 1000 & |
| 29 | + cell_number < 10000 ~ "tier_3", cell_number >= 10000 ~ "tier_4" |
| 30 | + )) |
| 31 | + |
| 32 | +result_directory = "/vast/projects/cellxgene_curated/metadata_cellxgenedp_Apr_2024" |
| 33 | +sample_meta <- tar_read(metadata_dataset_id_common_sample_columns, store = glue("{result_directory}/_targets")) |
| 34 | +sample_tbl = downloaded_samples_tbl |> left_join(get_metadata() |> select(dataset_id, contains("norm")) |> |
| 35 | + distinct() |> filter(!is.na(x_normalization)) |> |
| 36 | + as_tibble(), by = "dataset_id") |
| 37 | + |
| 38 | + |
| 39 | +sample_tbl <- sample_tbl |> left_join(sample_meta, by = "dataset_id") |> distinct(file_name, tier, cell_number, dataset_id, sample_2, |
| 40 | + x_normalization, x_approximate_distribution) |> |
| 41 | + mutate(transform_method = case_when(str_like(x_normalization, "C%") ~ "log", |
| 42 | + x_normalization == "none" ~ "log", |
| 43 | + x_normalization == "normalized" ~ "log", |
| 44 | + is.na(x_normalization) & is.na(x_approximate_distribution) ~ "log", |
| 45 | + is.na(x_normalization) & x_approximate_distribution == "NORMAL" ~ "NORMAL", |
| 46 | + is.na(x_normalization) & x_approximate_distribution == "COUNT" ~ "COUNT", |
| 47 | + str_like(x_normalization, "%canpy%") ~ "log1p", |
| 48 | + TRUE ~ x_normalization)) |> |
| 49 | + |
| 50 | + mutate(method_to_apply = case_when(transform_method %in% c("log","LogNormalization","LogNormalize","log-normalization") ~ "exp", |
| 51 | + is.na(x_normalization) & is.na(x_approximate_distribution) ~ "exp", |
| 52 | + str_like(transform_method, "Counts%") ~ "exp", |
| 53 | + str_like(transform_method, "%log2%") ~ "exp", |
| 54 | + transform_method %in% c("log1p", "log1p, base e", "Scanpy", |
| 55 | + "scanpy.api.pp.normalize_per_cell method, scaling factor 10000") ~ "expm1", |
| 56 | + transform_method == "log1p, base 2" ~ "expm1", |
| 57 | + transform_method == "NORMAL" ~ "exp", |
| 58 | + transform_method == "COUNT" ~ "identity", |
| 59 | + is.na(transform_method) ~ "identity" |
| 60 | + ) ) |> |
| 61 | + mutate(comment = case_when(str_like(x_normalization, "Counts%") ~ "a checkpoint for max value of Assay must <= 50", |
| 62 | + is.na(x_normalization) & is.na(x_approximate_distribution) ~ "round negative value to 0", |
| 63 | + x_normalization == "normalized" ~ "round negative value to 0" |
| 64 | + )) |
| 65 | + |
| 66 | +sample_tbl <- sample_tbl |> mutate(transformation_function = map( |
| 67 | + method_to_apply, |
| 68 | + ~ ( function(data) { |
| 69 | + assay_name <- data@assays |> names() |> magrittr::extract2(1) |
| 70 | + counts <- assay(data, assay_name) |
| 71 | + density_est <- counts |> as.matrix() |> density() |
| 72 | + mode_value <- density_est$x[which.max(density_est$y)] |
| 73 | + if (mode_value < 0 ) counts <- counts + abs(mode_value) |
| 74 | + |
| 75 | + # Scale max counts to 20 to avoid any downstream failure |
| 76 | + if (.x != "identity" && (max(counts) > 20)) { |
| 77 | + scale_factor = 20 / max(counts) |
| 78 | + counts <- counts * scale_factor} |
| 79 | + |
| 80 | + counts <- transform_method(counts) |
| 81 | + # round counts to avoid potential substraction error due to different digits print out |
| 82 | + counts <- counts |> round(5) |
| 83 | + majority_gene_counts = names(which.max(table(as.vector(counts)))) |> as.numeric() |
| 84 | + if (majority_gene_counts != 0) { |
| 85 | + counts <- counts - majority_gene_counts |
| 86 | + } |
| 87 | + |
| 88 | + # Avoid downstream failures negative counts |
| 89 | + if((counts[,1:min(10000, ncol(counts))] |> min()) < 0) |
| 90 | + counts[counts < 0] <- 0 |
| 91 | + |
| 92 | + # Assign counts back to data |
| 93 | + assay(data, assay_name) <- counts |
| 94 | + |
| 95 | + col_sums <- colSums(counts) |
| 96 | + # Drop all zero cells |
| 97 | + data <- data[, col_sums > 0] |
| 98 | + |
| 99 | + # Avoid downstream binding error |
| 100 | + rowData(data) = NULL |
| 101 | + |
| 102 | + data |
| 103 | + |
| 104 | + }) |> |
| 105 | + # Meta programming, replacing the transformation programmatically |
| 106 | + substitute( env = list(transform_method = as.name(.x))) |> |
| 107 | + # Evaluate back to a working function |
| 108 | + eval() |
| 109 | + )) |
| 110 | + |
| 111 | +#sample_tbl |> saveRDS("~/scratch/Census_rerun/sample_tbl_input_for_hpcell.rds") |
| 112 | +sample_tbl <- readRDS("~/scratch/Census_rerun/sample_tbl_input_for_hpcell.rds") |
| 113 | + |
| 114 | +# Set the parent directory where the subdirectories will be created |
| 115 | +# parent_dir <- "~/scratch/Census_rerun/" |
| 116 | +# |
| 117 | +# # Directory names to create |
| 118 | +# dir_names <- paste0("run", 1:25) |
| 119 | +# |
| 120 | +# # Full paths of the directories |
| 121 | +# full_dir_paths <- file.path(parent_dir, dir_names) |
| 122 | +# |
| 123 | +# # Create each directory if it does not exist |
| 124 | +# for (dir_path in full_dir_paths) { |
| 125 | +# if (!dir_exists(dir_path)) { |
| 126 | +# dir_create(dir_path) |
| 127 | +# } |
| 128 | +# } |
| 129 | + |
| 130 | +# Run 1000 samples per run. Save log and result in the corresponding store |
| 131 | +store = "~/scratch/Census_rerun/run3/" |
| 132 | +setwd(glue("{store}")) |
| 133 | +sliced_sample_tbl = sample_tbl |> slice(2001:3000) |> select(file_name, tier, cell_number, dataset_id, |
| 134 | + sample_2, transformation_function) |
| 135 | + |
| 136 | +# Enable sample_names.rds to store sample names for the input |
| 137 | +sample_names <- sliced_sample_tbl |> pull(file_name) |> set_names(sliced_sample_tbl |> pull(sample_2)) |
| 138 | + |
| 139 | +sample_names |> |
| 140 | + initialise_hpc( |
| 141 | + gene_nomenclature = "ensembl", |
| 142 | + data_container_type = "anndata", |
| 143 | + store = store, |
| 144 | + tier = sliced_sample_tbl |> pull(tier), |
| 145 | + computing_resources = list( |
| 146 | + crew_controller_slurm( |
| 147 | + name = "tier_1", |
| 148 | + script_lines = "#SBATCH --mem 35G", |
| 149 | + slurm_cpus_per_task = 1, |
| 150 | + workers = 200, |
| 151 | + tasks_max = 1, |
| 152 | + verbose = T |
| 153 | + ), |
| 154 | + |
| 155 | + crew_controller_slurm( |
| 156 | + name = "tier_2", |
| 157 | + script_lines = "#SBATCH --mem 60G", |
| 158 | + slurm_cpus_per_task = 1, |
| 159 | + workers = 50, |
| 160 | + tasks_max = 1, |
| 161 | + verbose = T |
| 162 | + ), |
| 163 | + crew_controller_slurm( |
| 164 | + name = "tier_3", |
| 165 | + script_lines = "#SBATCH --mem 90G", |
| 166 | + slurm_cpus_per_task = 1, |
| 167 | + workers = 25, |
| 168 | + tasks_max = 1, |
| 169 | + verbose = T |
| 170 | + ), |
| 171 | + crew_controller_slurm( |
| 172 | + name = "tier_4", |
| 173 | + script_lines = "#SBATCH --mem 100G", |
| 174 | + slurm_cpus_per_task = 1, |
| 175 | + workers = 14, |
| 176 | + tasks_max = 1, |
| 177 | + verbose = T |
| 178 | + ) |
| 179 | + ) |
| 180 | + |
| 181 | + ) |> |
| 182 | + tranform_assay(fx = sliced_sample_tbl |> |
| 183 | + pull(transformation_function), |
| 184 | + target_output = "sce_transformed") |> |
| 185 | + |
| 186 | + # Remove empty outliers based on RNA count threshold per cell |
| 187 | + remove_empty_threshold(target_input = "sce_transformed", RNA_feature_threshold = 200) |> |
| 188 | + |
| 189 | + # Remove dead cells |
| 190 | + remove_dead_scuttle(target_input = "sce_transformed") |> |
| 191 | + |
| 192 | + # Score cell cycle |
| 193 | + score_cell_cycle_seurat(target_input = "sce_transformed") |> |
| 194 | + |
| 195 | + # Remove doublets |
| 196 | + remove_doublets_scDblFinder(target_input = "sce_transformed") |> |
| 197 | + |
| 198 | + # Annotation |
| 199 | + annotate_cell_type(target_input = "sce_transformed", azimuth_reference = "pbmcref") |> |
| 200 | + |
| 201 | + normalise_abundance_seurat_SCT( |
| 202 | + factors_to_regress = c("subsets_Mito_percent", "subsets_Ribo_percent", "G2M.Score"), |
| 203 | + target_input = "sce_transformed" |
| 204 | + ) |
| 205 | + |
| 206 | + |
0 commit comments