|
| 1 | +# !/usr/bin/env Rscript |
| 2 | + |
| 3 | +library(Seurat) |
| 4 | +library(dplyr) |
| 5 | +library(future) |
| 6 | + |
| 7 | +plan(multicore) |
| 8 | +options(future.globals.maxSize = 2000 * 1024^2) |
| 9 | + |
| 10 | +load("./tmp/base_image.RData") |
| 11 | + |
| 12 | +for (i in 1:nrow(samples)) { |
| 13 | + x <- Read10X( # pulling data with no filter |
| 14 | + data.dir = samples$dir[i] |
| 15 | + ) |
| 16 | + |
| 17 | + str_section_head("Raw Object") # logging |
| 18 | + |
| 19 | + raw <- x |
| 20 | + rna <- raw[[1]] |
| 21 | + adt <- raw[[2]] |
| 22 | + |
| 23 | + x <- CreateSeuratObject( # certain data will gen null matrix sans filters |
| 24 | + counts = rna, |
| 25 | + project = samples$project[i] |
| 26 | + # min.cells = params["min.cells", ], |
| 27 | + # min.features = params["min.features", ] |
| 28 | + ) |
| 29 | + |
| 30 | + x[["ADT"]] <- CreateAssayObject( |
| 31 | + counts = adt |
| 32 | + ) |
| 33 | + |
| 34 | + x[["percent.mt"]] <- PercentageFeatureSet( |
| 35 | + x, |
| 36 | + pattern = "(?i)^MT-" |
| 37 | + ) |
| 38 | + |
| 39 | + str_section_head("Base Seurat Object") # logging |
| 40 | + |
| 41 | + x <- subset( |
| 42 | + x, |
| 43 | + nCount_RNA > params["min.count", ] & |
| 44 | + nCount_RNA < params["max.count", ] & |
| 45 | + percent.mt < params["max.percent.mt", ] & |
| 46 | + percent.mt > params["min.percent.mt", ] |
| 47 | + ) |
| 48 | + |
| 49 | + x <- NormalizeData(x) |
| 50 | + x <- NormalizeData( # TODO determine validity of margin param |
| 51 | + x, |
| 52 | + normalization.method = "CLR", |
| 53 | + assay = "ADT" |
| 54 | + ) |
| 55 | + |
| 56 | + str_section_head("Subset, Normalized") # logging |
| 57 | + |
| 58 | + genes <- rownames(x) |
| 59 | + |
| 60 | + x <- ScaleData( |
| 61 | + x, |
| 62 | + verbose = F, |
| 63 | + features = genes |
| 64 | + ) |
| 65 | + |
| 66 | + x <- ScaleData( |
| 67 | + x, |
| 68 | + assay = "ADT" |
| 69 | + ) |
| 70 | + |
| 71 | + x <- FindVariableFeatures( |
| 72 | + x, |
| 73 | + selection.method = "vst", |
| 74 | + nfeatures = params["max.features", ] |
| 75 | + ) |
| 76 | + |
| 77 | + x@meta.data$object <- samples$name[i] |
| 78 | + x@meta.data$group <- samples$group[i] |
| 79 | + |
| 80 | + assign( # giving names to objects |
| 81 | + samples$name[i], |
| 82 | + x |
| 83 | + ) |
| 84 | + str_section_head("Scaled") # logging |
| 85 | +} |
| 86 | + |
| 87 | +# create and save list of seurat objects |
| 88 | +objects <- list() |
| 89 | +for (i in samples$name) { |
| 90 | + objects <- c( |
| 91 | + objects, |
| 92 | + get(i) # need get() to call object instead of string |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +# run check for single sample |
| 97 | +if (length(samples$name) == 1) { |
| 98 | + message("Single sample detected - skipping integration") |
| 99 | + saveRDS(x, file = "./tmp/tmp_object.RDS") |
| 100 | + saveRDS(objects, file = paste0(out_path, "individual.RDS")) |
| 101 | +} else { |
| 102 | + d <- params["dims", ] |
| 103 | + saveRDS(objects, file = paste0(out_path, "individual.RDS")) |
| 104 | + |
| 105 | + x <- FindIntegrationAnchors( |
| 106 | + object.list = objects, |
| 107 | + dims = 1:d |
| 108 | + ) |
| 109 | + |
| 110 | + x <- IntegrateData( |
| 111 | + anchorset = x, |
| 112 | + dims = 1:d |
| 113 | + ) |
| 114 | + |
| 115 | + DefaultAssay(x) <- "integrated" |
| 116 | + saveRDS(x, file = "./tmp/tmp_object.RDS") |
| 117 | + |
| 118 | + str_section_noloop("Integrated") # logging |
| 119 | +} |
| 120 | + |
| 121 | +print("End of create_object.R") |
0 commit comments