-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFUN_scRNAQC.R
263 lines (223 loc) · 11.7 KB
/
FUN_scRNAQC.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
scRNAQC <- function(PBMC.combined, nUMIFilter= 500, nGeneFilter = 250 ,
logGPUFilter= 0.8 , mitRFilter=0.1 ,
PtSize=0, SpeciSet = "Mouse",
AddMitInf = "Yes", # Add mitochondria information
CheckOnly="No", # CheckOnly = "Yes": Just plot the Fig
FileName = "QC",NAno=1){
## https://hbctraining.github.io/scRNA-seq/lessons/04_SC_quality_control.html
## QC for scRNA Data
##### QC in Seruat #####
PBMC.combined_QC <- PBMC.combined
# QC and selecting cells for further analysis
# The [[ operator can add columns to object metadata. This is a great place to stash QC stats
## PBMC.combined_QC[["percent.mt"]] <- PercentageFeatureSet(PBMC.combined_QC, pattern = "^MT-") # For Human
## PBMC.combined_QC[["percent.mt"]] <- PercentageFeatureSet(PBMC.combined_QC, pattern = "^mt-") # For Mouse
if(AddMitInf == "Yes"){
PBMC.combined_QC <- scRNAMit(PBMC.combined_QC, Species = SpeciSet)
}else{
PBMC.combined_QC <- PBMC.combined_QC
}
metadata_ori <- PBMC.combined_QC@meta.data
# Visualize QC metrics as a violin plot
# VlnPlot(PBMC.combined_QC, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3) # from Seruat
VlnPlot.PBMC <- VlnPlot(PBMC.combined_QC, features = c("nGene", "nUMI", "mitoRatio"), ncol = 2, pt.size= PtSize,
group.by=colnames(metadata_ori)[3+NAno])
FSplot1.PBMC <- FeatureScatter(PBMC.combined_QC, feature1 = "nUMI", feature2 = "nGene",
group.by=colnames(metadata_ori)[3+NAno])
VlnPlot.PBMC + FSplot1.PBMC
#########################################################################################################
##### QC in Harvard Chan Bioinformatics Core (HBC) #####
## https://hbctraining.github.io/scRNA-seq/lessons/04_SC_quality_control.html
# Add number of genes per UMI for each cell to metadata
PBMC.combined_QC$log10GenesPerUMI <- log10(PBMC.combined_QC$nGene) / log10(PBMC.combined_QC$nUMI)
metadata <- PBMC.combined_QC@meta.data
## Cell counts
# Visualize the number of cell counts per sample
metadata %>%
ggplot(aes(x=metadata[,3+NAno], fill=metadata[,3+NAno])) +
geom_bar() +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),aspect.ratio=1) +
theme(plot.title = element_text(hjust=0.5, face="bold")) +
ggtitle("NCells")+
labs(x = colnames(metadata)[3+NAno],
fill=colnames(metadata)[3+NAno]) -> NCells.BarPlot
NCells.BarPlot
## UMI counts (transcripts) per cell
# Visualize the number UMIs/transcripts per cell
metadata %>%
ggplot(aes(color=metadata[,3+NAno], x=nUMI, fill= metadata[,3+NAno])) +
geom_density(alpha = 0.2) +
scale_x_log10() +
theme_classic() +
ylab("Cell density") +
geom_vline(xintercept = nUMIFilter) +
labs(fill=colnames(metadata)[3+NAno],
colour=colnames(metadata)[3+NAno]) -> Cell_density.UMIs.Plot
Cell_density.UMIs.Plot
## Genes detected per cell
# Visualize the distribution of genes detected per cell via histogram
metadata %>%
ggplot(aes(color = metadata[,3+NAno], x=nGene, fill = metadata[,3+NAno])) +
geom_density(alpha = 0.2) +
theme_classic() +
scale_x_log10() +
ylab("Cell density") +
geom_vline(xintercept = nGeneFilter)+
labs(fill=colnames(metadata)[3+NAno],
colour=colnames(metadata)[3+NAno]) -> Cell_density.NGenes.Plot
Cell_density.NGenes.Plot
# Visualize the distribution of genes detected per cell via boxplot
metadata %>%
ggplot(aes(x = metadata[,3+NAno], y=log10(nGene), fill = metadata[,3+NAno])) +
geom_boxplot() +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),aspect.ratio=1) +
theme(plot.title = element_text(hjust=0.5, face="bold")) +
ggtitle("NCells vs NGenes") +
labs(fill=colnames(metadata)[3+NAno],
colour=colnames(metadata)[3+NAno]) -> NCvsNG.BarPlot
NCvsNG.BarPlot
## UMIs vs. genes detected
# Visualize the correlation between genes detected and number of UMIs and determine whether strong presence of cells with low numbers of genes/UMIs
metadata %>%
ggplot(aes(x=nUMI, y=nGene, color=mitoRatio)) +
geom_point() +
scale_colour_gradient(low = "gray90", high = "black") +
stat_smooth(method=lm) +
scale_x_log10() +
scale_y_log10() +
theme_classic() +
geom_vline(xintercept = nUMIFilter) +
geom_hline(yintercept = nGeneFilter) +
theme(aspect.ratio=1)+
scale_fill_continuous(limits = c(0, 1)) +
theme(legend.title = element_text(size=17),
legend.text = element_text(size=14, face="bold"))+
theme(plot.title = element_text(size=14, face="bold.italic"),
axis.title.x = element_text(color="black", size=20, face="bold"),
axis.title.y = element_text(color="black", size=20, face="bold"),
axis.text.x=element_text(size=14, face="bold"),
axis.text.y=element_text(size=14, face="bold")) +
facet_wrap(colnames(metadata)[3+NAno])+
theme(strip.text.x = element_text(size=17, color="black",
face="bold.italic")) -> FSplot
FSplot
# Add p-Value and r-Value
# https://stackoverflow.com/questions/39335005/add-p-value-and-r-on-ggplot-follow-up
# https://stackoverflow.com/questions/39333151/add-p-value-and-r2-ggplot-follow-up
formula <- y ~ x
FSplot2 <- FSplot +
stat_poly_eq(aes(label = paste(..rr.label..)),
label.x.npc = "right",
label.y.npc = 0.15,
formula = formula,
parse=TRUE,
size = 5) +
stat_fit_glance(method = 'lm', method.args = list(formula = formula),
geom='text', aes(label=ifelse(..p.value..< 0.001, "p<0.001**",
ifelse(..p.value..>=0.001 & ..p.value..<0.05, "p<0.05*", "p>0.05"))),
label.x.npc = 'right',
label.y.npc = 0.4,
size = 5)
FSplot2
## Mitochondrial counts ratio
# Visualize the distribution of mitochondrial gene expression detected per cell
metadata %>%
ggplot(aes(color=metadata[,3+NAno], x=mitoRatio, fill=metadata[,3+NAno])) +
geom_density(alpha = 0.2) +
#scale_x_log10() +
theme_classic() +
ylab("Cell density") +
geom_vline(xintercept = 0.1) +
labs(fill=colnames(metadata)[3+NAno],
colour=colnames(metadata)[3+NAno]) -> Cell_density.mitR.Plot
Cell_density.mitR.Plot
## Complexity
# Visualize the overall complexity of the gene expression by visualizing the genes detected per UMI
metadata %>%
ggplot(aes(x=log10GenesPerUMI, color = metadata[,3+NAno], fill=metadata[,3+NAno])) +
geom_density(alpha = 0.2) +
theme_classic() +
geom_vline(xintercept = logGPUFilter)+
labs(fill=colnames(metadata)[3+NAno],
colour=colnames(metadata)[3+NAno]) -> Cell_density.log10GenesPerUMI.Plot
Cell_density.log10GenesPerUMI.Plot
# Cell_density
Cell_density.UMIs.Plot + Cell_density.NGenes.Plot +
Cell_density.mitR.Plot + Cell_density.log10GenesPerUMI.Plot
# BarPlot
NCells.BarPlot + NCvsNG.BarPlot
# FeatureScatter
FSplot
# https://stackoverflow.com/questions/19288101/r-pdf-usage-inside-a-function/19288874
pdf(
file = paste0(getwd(),"/",FileName,".pdf"),
width = 10, height = 7
)
print(VlnPlot.PBMC + FSplot1.PBMC)
# Cell_density
print(Cell_density.UMIs.Plot + Cell_density.NGenes.Plot + Cell_density.mitR.Plot + Cell_density.log10GenesPerUMI.Plot)
# BarPlot
print(NCells.BarPlot + NCvsNG.BarPlot)
# FeatureScatter
print(FSplot2)
dev.off()
# graphics.off()
if(CheckOnly == "No"){
##### Filtering #####
## Cell-level filtering
# Filter out low quality reads using selected thresholds - these will change with experiment
PBMC.combined_QC_Filter <- subset(x = PBMC.combined_QC,
subset= (nUMI >= nUMIFilter) &
(nGene >= nGeneFilter) &
(log10GenesPerUMI > logGPUFilter) &
(mitoRatio < mitRFilter))
# ## Gene-level filtering
# # Output a logical vector for every gene on whether the more than zero counts per cell
# # Extract counts
# # counts <- GetAssayData(object = PBMC.combined_QC_Filter, slot = "counts")
# counts <- PBMC.combined_QC_Filter@assays[["RNA"]]@counts
#
#
# # Output a logical vector for every gene on whether the more than zero counts per cell
# nonzero <- counts > 0
#
# # Sums all TRUE values and returns TRUE if more than 10 TRUE values per gene
# keep_genes <- Matrix::rowSums(nonzero) >= 10
#
# # Only keeping those genes expressed in more than 10 cells
# filtered_counts <- counts[keep_genes, ]
#
# # Reassign to filtered Seurat object
# PBMC.combined_QC_Filter2 <- CreateSeuratObject(filtered_counts, meta.data = PBMC.combined_QC_Filter@meta.data)
# scRNAQC(PBMC.combined_QC_Filter2,AddMitInf = "No",FileName = paste0(FileName,"_CheckTry"), CheckOnly="Yes")
scRNAQC(PBMC.combined_QC_Filter,AddMitInf = "No",FileName = paste0(FileName,"_Check"), CheckOnly="Yes")
}else{
PBMC.combined_QC_Filter <- PBMC.combined_QC
}
return(PBMC.combined_QC_Filter)
}
##### QC Analysis for single Sample #####
# # TN138
# PBMC.TN138.combined_QC <- PBMC.TN138
# # QC and selecting cells for further analysis
# # The [[ operator can add columns to object metadata. This is a great place to stash QC stats
# PBMC.TN138.combined_QC[["percent.mt"]] <- PercentageFeatureSet(PBMC.TN138.combined_QC, pattern = "^MT-")
# PBMC.TN138.combined_QC[["percent.mt"]] <- PercentageFeatureSet(PBMC.TN138.combined_QC, pattern = "^mt-") # OK, and same as mitoRatio
#
# # Visualize QC metrics as a violin plot
# v1 <- VlnPlot(PBMC.TN138.combined_QC, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
# p1 <- FeatureScatter(PBMC.TN138, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
# p2 <- FeatureScatter(PBMC.TN138.combined_QC, feature1 = "nCount_RNA", feature2 = "percent.mt")
# p3 <- p1+p2
#
#
# # Test by funciton
# PBMC.TN138.combined_QC2 <- scRNAMit(PBMC.TN138)
# v1T <- VlnPlot(PBMC.TN138.combined_QC2, features = c("nGene", "nUMI", "mitoRatio"), ncol = 3)
# v1/v1T
# p1T <- FeatureScatter(PBMC.TN138.combined_QC2, feature1 = "nUMI", feature2 = "nGene")
# p2T <- FeatureScatter(PBMC.TN138.combined_QC2, feature1 = "nUMI", feature2 = "mitoRatio")
# p3T <- p1T+p2T
# p3/p3T