-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_individually_processing_per_emulsion.R
More file actions
169 lines (153 loc) · 6.78 KB
/
01_individually_processing_per_emulsion.R
File metadata and controls
169 lines (153 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
###### Loading required packages ########
suppressPackageStartupMessages({
library(tidyverse)
library(Seurat)
library(SeuratDisk)
library(SeuratWrappers)
library(BiocParallel)
library(scDblFinder)
library(qs)
})
#### File/Data Path Preps ####
# We downloaded the multimodal reference pbmc dataset from here (https://atlas.fredhutch.org/data/nygc/multimodal/pbmc_multimodal.h5seurat)
# This dataset is published in this article https://doi.org/10.1016/j.cell.2021.04.048
#
ref_pbmc_path <- "/mnt/SERVER-CRCT-STORAGE/CRCT13/Ruchan/01_NK_project/99_back_up/reference_databases/pbmc_multimodal_reference.h5seurat"
sample_paths <- "/mnt/SERVER-CRCT-STORAGE/CRCT13/Ruchan/01_NK_project/99_back_up/cellranger_counts_human/"
# Loading up the reference pbmc dataset
ref_pbmc <- LoadH5Seurat(file = ref_pbmc_path)
Idents(ref_pbmc) <- ref_pbmc$celltype.l1
# Reading the samples of our cohort
name_of_samples <- list.files(sample_paths)
#### Main Task ####
# Handling each sample individually for ref-based cell-type annotation,
# doublet detection, velocity embedding and saving the processed file accodingly
for (i in 1:length(name_of_samples)) {
## Preprocessing steps
filt.matrix <- Read10X(data.dir = paste0(sample_paths, name_of_samples[i],"/outs/filtered_feature_bc_matrix/"),strip.suffix = T)
tmp <- CreateSeuratObject(counts = filt.matrix$`Gene Expression`, project = name_of_samples[i])
tmp <- NormalizeData(tmp) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA(reduction.name = "pca", verbose = F) %>%
RunUMAP(dims = 1:20, verbose = F) %>% FindNeighbors(dims = 1:20, verbose = F) %>%
FindClusters(algorithm = 3, verbose = T)
####
#### ScDblFinder is a package generating artificial doublets by given clusters.
#### and then training a model to classify singlets and doublets.
#### https://f1000research.com/articles/10-979
####
tmp_sce <- as.SingleCellExperiment(tmp) #ScdblFinder is compatible within SingleCellExperiment ecosysten!;
set.seed(22)
tmp_sce <-
scDblFinder(tmp_sce,
clusters = tmp_sce$seurat_clusters,
BPPARAM = MulticoreParam(16))
table(tmp_sce$scDblFinder.class)
rm(tmp); gc()
tmp <-
as.Seurat(tmp_sce, project = (name_of_samples[i]))
rm(tmp_sce); gc()
#### Metadata ####
tmp$id <- ifelse(tmp$orig.ident %in% c("T23491m_NK", "T23491m_T", "T23491b_NK", "T23491b_T"),
"T23491", str_sub(name_of_samples[i],1,nchar(name_of_samples[i])-1))
tmp$percent.mt <- PercentageFeatureSet(tmp, pattern = "^MT-") # calculate and add mito read percentage
tmp$percent.rb <- PercentageFeatureSet(tmp, pattern = "^RP[SL]") # calculate and add ribo read precentage
# If the donor was Healthy, sample name was starting with "H",
# else sample name was starting with "T"
# HD: Healthy Donor, MM: Multiple Myeloma
tmp$condition = ifelse(grepl(pattern="H", x = name_of_samples[i]), "HD", "MM")
# If the sample was taken from Bone marrow, it was encoded at the end of sample name with "m",
# else name was ending with "b" which reflects peripheral blood
# BMMC: Bone Marrow Mononuclear Cells, PBMC: Peripheral Blood Mononuclear Cells
tmp$source = ifelse(grepl(pattern="m", x = name_of_samples[i]), "BMMC", "PBMC") #
####
DefaultAssay(tmp) <- "RNA"
tmp@reductions$PCA <- NULL
tmp <-
NormalizeData(tmp) %>% FindVariableFeatures() %>% SCTransform(
method = "glmGamPoi",
vst.flavor = "v2",
vars.to.regress = "percent.mt",
assay = "RNA",
do.scale = T
) %>% RunPCA(reduction.name = "pca")
tmp[["ADT"]] <-
CreateAssayObject(filt.matrix$`Antibody Capture`[, colnames(x = tmp)])
DefaultAssay(tmp) <- "ADT"
VariableFeatures(tmp) <- rownames(tmp[["ADT"]])
tmp <-
NormalizeData(tmp, normalization.method = "CLR", margin = 2) %>%
ScaleData() %>% RunPCA(reduction.name = "apca") ## Normalize protein data across cells
DefaultAssay(tmp) <- "SCT"
#Due to the characteristic of the used reference dataset,
#cell type annotation accuracy is getting better with multimodal cell type label transfer approach.
# To identify multimodal neighbors. These will be stored in the neighbors slot,
# and can be accessed using sc_obj[['weighted.nn']]
# The WNN graph can be accessed at sc_obj[["wknn"]],
# and the SNN graph used for clustering at sc_obj[["wsnn"]]
# Cell-specific modality weights can be accessed at sc_obj$RNA.weight
tmp <- FindMultiModalNeighbors(tmp,
reduction.list = list("pca", "apca"),
dims.list = list(1:20, 1:10))
tmp <-
RunUMAP(
tmp,
nn.name = "weighted.nn",
reduction.name = "wnn.umap",
reduction.key = "wnnUMAP_"
)
tmp <-
FindClusters(tmp,
graph.name = "wsnn",
algorithm = 3,
verbose = FALSE)
anchors <- FindTransferAnchors(
reference = ref_pbmc,
query = tmp,
normalization.method = "SCT",
reference.reduction = "spca",
dims = 1:50
)
tmp <- TransferData(
anchorset = anchors,
reference = ref_pbmc,
query = tmp,
refdata = list(
celltype.l1 = "celltype.l1",
celltype.l2 = "celltype.l2",
predicted_ADT = "ADT"
)
)
head(colnames(tmp))
tmp <-
RenameCells(object = tmp,
new.names = paste0(name_of_samples[i], ":", colnames(x = tmp)))
head(colnames(tmp))
rm(anchors)
gc()
# Reading calculated RNA velocity information into the data object
# Velocity assay embeddings, echo=TRUE, message=FALSE, warning=FALSE, paged.print=TRUE}
tmp_velo <-
ReadVelocity(file = paste0(sample_paths, name_of_samples[i], "/velocyto/", name_of_samples[i], ".loom"))
tmp_velo <- as.Seurat(tmp_velo)
tmp_velo$orig.ident <- as.factor(name_of_samples[i])
#This "Renaming the cells" for uniform formatting is required to merge two object by their common cell ids.
# Gene expression Seurat Object and Velocity Seurat objects can be merged thanks to this. #
tmp_velo <- RenameCells(object = tmp_velo,
new.names = substring(colnames(tmp_velo), 1, nchar(colnames(tmp_velo)) -
1))
tmp[["spliced"]] <-
CreateAssayObject(tmp_velo[["spliced"]][, colnames(x = tmp_velo)])
tmp[["unspliced"]] <-
CreateAssayObject(tmp_velo[["unspliced"]][, colnames(x = tmp_velo)])
tmp[["ambiguous"]] <-
CreateAssayObject(tmp_velo[["ambiguous"]][, colnames(x = tmp_velo)])
rm(tmp_velo)
gc()
print(name_of_samples[i])
SaveH5Seurat(object = tmp, filename = paste0("/data/velo_embedded_wnn/",name_of_samples[i]))
rm(tmp)
gc()
}
#### Footnote ####
# Next step will be inference of Nuclear RNA fraction from Velocyto CLI outputs, directly.
# Then reducing this metric into a metadata level information and merging all
# processed samples into one single data object for the downstream of the analysis.