|
a |
|
b/Functions/Integrate.scRNA.scATAC.R |
|
|
1 |
#ref: Single cell transcriptional and chromatin accessibility profiling redefine cellular heterogeneity in the adult human kidney |
|
|
2 |
|
|
|
3 |
#'@param ref.npcs = 30, dims = 30, are the parameters in the FindTransferAnchors function, used to control the number of dimensions used by reference data and anchor search space respectively |
|
|
4 |
#'@param scATAC.assay = "ACTIVITY", scATAC.normalize = T, PC = 30, is used to process scATAC data. |
|
|
5 |
# scATAC.assay indicates the name of the assay where the gene activity is stored in the scATAC number |
|
|
6 |
# scATAC.normalize Whether the gene activity has been logNormalized |
|
|
7 |
# PC indicates the dimension of scATAC dimensionality reduction, which comes from the lsi of scATAC.object itself. Note that the default dimensionality reduction assay of scATAC.object is lsi |
|
|
8 |
|
|
|
9 |
Integrate.scRNA.scATAC <- function(scATAC.object, scRNA.object, class.label = "cellType", scRNA.assay = "RNA", ref.npcs = 30, dims = 30, scATAC.assay = "ACTIVITY", scATAC.normalize = T, PC = 30, nfeatures = 3000, SCT = F){ |
|
|
10 |
|
|
|
11 |
##1. load data |
|
|
12 |
#scRNA-seq data |
|
|
13 |
DefaultAssay(scRNA.object) <- scRNA.assay |
|
|
14 |
scRNA.object <- NormalizeData(scRNA.object) |
|
|
15 |
if(SCT){ |
|
|
16 |
#Need to use sctransform to normalize data in advance |
|
|
17 |
var.genes <- scRNA.object@assays$SCT@var.features[1:nfeatures] |
|
|
18 |
}else{ |
|
|
19 |
var.genes <- VariableFeatures(scRNA.object)[1:nfeatures] |
|
|
20 |
} |
|
|
21 |
#scATAC-seq data |
|
|
22 |
DefaultAssay(scATAC.object) <- scATAC.assay |
|
|
23 |
if(!scATAC.normalize){ |
|
|
24 |
scATAC.object <- NormalizeData(scATAC.object) |
|
|
25 |
} |
|
|
26 |
scATAC.object <- ScaleData(scATAC.object, features = rownames(scATAC.object)) |
|
|
27 |
|
|
|
28 |
##2. Identify anchors |
|
|
29 |
#npcs = 30; dim = 1:30 |
|
|
30 |
transfer.anchors <- FindTransferAnchors(reference = scRNA.object, |
|
|
31 |
query = scATAC.object, |
|
|
32 |
features = var.genes, #SCT model:scRNA.merge@assays$SCT@var.features |
|
|
33 |
reference.assay = scRNA.assay, |
|
|
34 |
query.assay = scATAC.assay, |
|
|
35 |
reduction = "cca", |
|
|
36 |
npcs = ref.npcs, # Number of PCs to compute on reference if reference |
|
|
37 |
dims = 1:dims) # Which dimensions to use from the reduction to specify the neighbor search space |
|
|
38 |
#saveRDS(transfer.anchors, file = file.path(saveDir, "transfer.anchors.rds")) |
|
|
39 |
|
|
|
40 |
#The default dims = NULL, it is consistent with FindTransferAnchors |
|
|
41 |
celltype.predictions <- TransferData(anchorset = transfer.anchors, |
|
|
42 |
refdata = scRNA.object@meta.data[,class.label], |
|
|
43 |
weight.reduction = scATAC.object[["lsi"]], |
|
|
44 |
dims = 2:PC) |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
scATAC.object <- AddMetaData(scATAC.object, metadata = celltype.predictions) |
|
|
48 |
scATAC.object <- AddMetaData(scATAC.object, metadata = celltype.predictions$predicted.id, class.label) |
|
|
49 |
|
|
|
50 |
p <- gghistogram(scATAC.object@meta.data, x = "prediction.score.max", y = "..density..", main = "Prediction Score for scATAC", add_density = TRUE) |
|
|
51 |
print(p) |
|
|
52 |
p <- DimPlot(scATAC.object, group.by = class.label, label = TRUE, repel = TRUE, reduction = "umap") + |
|
|
53 |
ggtitle("scATAC-seq Predicted Celltypes Before Harmony") + NoLegend() |
|
|
54 |
print(p) |
|
|
55 |
p <- DimPlot(scATAC.object, group.by = class.label, label = TRUE, repel = TRUE, reduction = "umap") + |
|
|
56 |
ggtitle("scATAC-seq Predicted Celltypes Before Harmony") |
|
|
57 |
print(p) |
|
|
58 |
p <- DimPlot(scATAC.object, group.by = class.label, label = TRUE, repel = TRUE, reduction = "tsne") + |
|
|
59 |
ggtitle("scATAC-seq Predicted Celltypes Before Harmony") + NoLegend() |
|
|
60 |
print(p) |
|
|
61 |
p <- DimPlot(scATAC.object, group.by = class.label, label = TRUE, repel = TRUE, reduction = "umap") + |
|
|
62 |
ggtitle("scATAC-seq Predicted Celltypes Before Harmony") |
|
|
63 |
print(p) |
|
|
64 |
return(list(transfer.anchors = transfer.anchors, scATAC.object = scATAC.object)) |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
#' @param dims Indicates the dimensionality reduction used when coembedding |
|
|
68 |
Coembedding.scATAC.scRNA <- function(transfer.anchors, scATAC.object, scRNA.assay = "RNA", scRNA.object, class.label = "cellType", PC = 30, dims = 30){ |
|
|
69 |
|
|
|
70 |
####Co-embedding scRNA-seq and scATAC-seq datasets |
|
|
71 |
# note that we restrict the imputation to variable genes from scRNA-seq, but could impute the |
|
|
72 |
# full transcriptome if we wanted to |
|
|
73 |
genes.use <- transfer.anchors@anchor.features |
|
|
74 |
DefaultAssay(scRNA.object) <- scRNA.assay |
|
|
75 |
scRNA.object <- NormalizeData(scRNA.object) |
|
|
76 |
refdata <- GetAssayData(scRNA.object, assay = scRNA.assay, slot = "data")[genes.use, ] |
|
|
77 |
# refdata (input) contains a scRNA-seq expression matrix for the scRNA-seq cells. imputation |
|
|
78 |
# (output) will contain an imputed scRNA-seq matrix for each of the ATAC cells |
|
|
79 |
imputation <- TransferData(anchorset = transfer.anchors, refdata = refdata, weight.reduction = scATAC.object[["lsi"]], dims = 2:PC) |
|
|
80 |
scATAC.object[["RNA"]] <- imputation |
|
|
81 |
DefaultAssay(scATAC.object) <- "RNA" |
|
|
82 |
|
|
|
83 |
scRNA.object <- AddMetaData(scRNA.object, metadata = rep("scRNA", nrow(scRNA.object@meta.data)), col.name = "type") |
|
|
84 |
scATAC.object <- AddMetaData(scATAC.object, metadata = rep("scATAC", nrow(scATAC.object@meta.data)), col.name = "type") |
|
|
85 |
|
|
|
86 |
coembed <- merge(x = scRNA.object, y = scATAC.object) |
|
|
87 |
DefaultAssay(coembed) <- "RNA" |
|
|
88 |
# Finally, we run PCA and UMAP on this combined object, to visualize the co-embedding of both |
|
|
89 |
# datasets |
|
|
90 |
coembed <- ScaleData(coembed, features = genes.use, do.scale = FALSE) |
|
|
91 |
coembed <- RunPCA(coembed, features = genes.use, verbose = FALSE) |
|
|
92 |
coembed <- RunHarmony(object = coembed, group.by.vars = "orig.ident", verbose = FALSE) |
|
|
93 |
coembed <- RunUMAP(coembed, dims = 1:dims, reduction = 'harmony', verbose = FALSE) |
|
|
94 |
coembed <- RunTSNE(coembed, dims = 1:dims, reduction = 'harmony', check_duplicates = FALSE, verbose = FALSE) |
|
|
95 |
#saveRDS(coembed, file = file.path(saveDir, "coembed.rds")) |
|
|
96 |
|
|
|
97 |
p <- ElbowPlot(object = coembed, ndims = dims) |
|
|
98 |
print(p) |
|
|
99 |
p <- DimPlot(coembed, group.by = c("orig.ident"), reduction = "umap", label = T) + NoLegend() |
|
|
100 |
print(p) |
|
|
101 |
p <- DimPlot(coembed, group.by = c("orig.ident"), reduction = "tsne", label = T) + NoLegend() |
|
|
102 |
print(p) |
|
|
103 |
p <- DimPlot(coembed, group.by = c("type"), reduction = "umap", label = T) + NoLegend() |
|
|
104 |
print(p) |
|
|
105 |
p <- DimPlot(coembed, group.by = class.label, reduction = "umap", label = T) + NoLegend() |
|
|
106 |
print(p) |
|
|
107 |
p <- DimPlot(coembed, group.by = c("type"), reduction = "tsne", label = T) + NoLegend() |
|
|
108 |
print(p) |
|
|
109 |
p <- DimPlot(coembed, group.by = class.label, reduction = "tsne", label = T) + NoLegend() |
|
|
110 |
print(p) |
|
|
111 |
|
|
|
112 |
p <- DimPlot(coembed, group.by = c("orig.ident"), reduction = "umap", label = T) |
|
|
113 |
print(p) |
|
|
114 |
p <- DimPlot(coembed, group.by = c("orig.ident"), reduction = "tsne", label = T) |
|
|
115 |
print(p) |
|
|
116 |
p <- DimPlot(coembed, group.by = c("type"), reduction = "umap", label = T) |
|
|
117 |
print(p) |
|
|
118 |
p <- DimPlot(coembed, group.by = class.label, reduction = "umap", label = T) |
|
|
119 |
print(p) |
|
|
120 |
p <- DimPlot(coembed, group.by = c("type"), reduction = "tsne", label = T) |
|
|
121 |
print(p) |
|
|
122 |
p <- DimPlot(coembed, group.by = class.label, reduction = "tsne", label = T) |
|
|
123 |
print(p) |
|
|
124 |
return(list(coembed = coembed, scATAC.object = scATAC.object)) |
|
|
125 |
} |