-
Notifications
You must be signed in to change notification settings - Fork 0
/
B.2023_06_06_sc-by-batch.r
140 lines (109 loc) · 5.33 KB
/
B.2023_06_06_sc-by-batch.r
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
library(Seurat)
library(parallel)
library(dplyr)
library(ggplot2)
library(ggprism)
library(RColorBrewer)
setwd("~/salvage_tmp/final/")
set.seed(99)
load(file = './code_outputs/2024-05-18_AggrSeurat.rda')
batch1 = subset(seu, subset = Batch == 1)
batch2 = subset(seu, subset = Batch == 2)
rm(seu)
#Process Batch1============================================================
pbmc = batch1; rm(batch1)
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
plot0 <- VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot3 <- plot1 + plot2
plot3
filt <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 5000 & percent.mt < 7.5)
ncells_orig = ncol(pbmc); rm(pbmc)
plot4 <- FeatureScatter(filt, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot5 <- FeatureScatter(filt, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot6 <- plot4 + plot5
plot6
plot7 <- VlnPlot(filt, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
ncells_remaining = ncol(filt)
filt <- NormalizeData(filt, normalization.method = "LogNormalize", scale.factor = 10000)
filt <- FindVariableFeatures(filt, selection.method = 'vst', nfeatures = 5000)
top10 <- head(VariableFeatures(filt), 10)
plot8 <- VariableFeaturePlot(filt)
plot9 <- LabelPoints(plot = plot8, points = top10, repel = TRUE)
plot9
filt <- ScaleData(filt, features = VariableFeatures(filt))
filt <- RunPCA(filt, features = VariableFeatures(filt))
plot10 <- ElbowPlot(filt, ndims = 30)
N=25
filt<- RunUMAP(filt, dims = 1:N)
filt = filt[sample(c(1:nrow(filt)), nrow(filt), replace = F),] #shuffle cells
plot11 <- DimPlot(filt, reduction = 'umap', group.by = 'Sample', raster = FALSE)
plot12 <- DimPlot(filt, reduction = 'umap', raster = FALSE, split.by = 'Sample')
umap = filt@[email protected]
[email protected]$to_exclude = umap[,1] < -6 & umap[,2] < 0
filt2 = subset(filt, to_exclude == FALSE)
plot13 <- DimPlot(filt2, reduction = 'umap', group.by = 'Sample', raster = FALSE)
plot14 <- DimPlot(filt2, reduction = 'umap', raster = FALSE, split.by = 'Sample')
filt2 <- FindNeighbors(filt2, dims = 1:25) #low nDim because I'm just trying to catch the batch-specific cluster
filt2 <- FindClusters(filt2, resolution = 0.3)
save(filt2, file = paste0('./code_outputs/', Sys.Date(), '_batch1filt.rda'))
#Process Batch2============================================================
pbmc = batch2; rm(batch2)
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
plot0 <- VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot3 <- plot1 + plot2
plot3
filt <- subset(pbmc, subset = nFeature_RNA > 500 & nFeature_RNA < 5000 & percent.mt < 7.5)
ncells_orig = ncol(pbmc); rm(pbmc)
plot4 <- FeatureScatter(filt, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot5 <- FeatureScatter(filt, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot6 <- plot4 + plot5
plot6
plot7 <- VlnPlot(filt, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
ncells_remaining = ncol(filt)
filt <- NormalizeData(filt, normalization.method = "LogNormalize", scale.factor = 10000)
filt <- FindVariableFeatures(filt, selection.method = 'vst', nfeatures = 5000)
top10 <- head(VariableFeatures(filt), 10)
plot8 <- VariableFeaturePlot(filt)
plot9 <- LabelPoints(plot = plot8, points = top10, repel = TRUE)
plot9
filt <- ScaleData(filt, features = VariableFeatures(filt))
filt <- RunPCA(filt, features = VariableFeatures(filt))
plot10 <- ElbowPlot(filt, ndims = 50)
N=40
filt<- RunUMAP(filt, dims = 1:N)
filt = filt[sample(c(1:nrow(filt)), nrow(filt), replace = F),] #shuffle cells
plot11 <- DimPlot(filt, reduction = 'umap', group.by = 'Sample', raster = FALSE)
plot12 <- DimPlot(filt, reduction = 'umap', raster = FALSE, split.by = 'Sample')
#Based on UMAP let's try patient correction using Batchelor.
library(batchelor)
counts <- filt@assays$RNA@counts
counts <- counts[rownames(counts) %in% VariableFeatures(filt),]
counts = as.matrix(counts)
libsizes <- colSums(counts)
size.factors <- libsizes/mean(libsizes)
sce = SingleCellExperiment(assays = list(logcounts = log2(t(t(counts)/size.factors) + 1)))
meta = [email protected]
#reducedDims(sce) <- list(PCA = filt@[email protected], UMAP = filt@)
batch = [email protected]$Patient == 'J1994.002'
rm(counts)
set.seed(99)
sce = fastMNN(
sce,
batch = batch
)
#Jerryrig solution to use native seurat functions on Batchelor dimensional reduction
pca = filt@[email protected]
tmp = reducedDim(sce, "corrected")
colnames(tmp) = colnames(pca)
filt@reductions$corrected = filt@reductions$pca
filt@[email protected] = tmp
filt <- RunUMAP(filt, reduction.name = 'umap_corrected', reduction = 'corrected', dims = 1:N)
plot13 <- DimPlot(filt, reduction = 'umap_corrected', group.by = 'Sample', raster = FALSE)
plot14 <- DimPlot(filt, reduction = 'umap_corrected', raster = FALSE, split.by = 'Sample')
filt <- FindNeighbors(filt, dims = 1:40)
filt <- FindClusters(filt, resolution = 0.3)
save(filt, file = paste0('./code_outputs/', Sys.Date(), '_batch2filt.rda'))