forked from jwiarda/scRNAseq_ChronicMastitis_MilkBlood
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_CellGeneFiltering.R
More file actions
141 lines (121 loc) · 4.58 KB
/
08_CellGeneFiltering.R
File metadata and controls
141 lines (121 loc) · 4.58 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
#20221129 - Jayne Wiarda
# Cell & gene filtering
library(Seurat)
library(SeuratDisk)
library(dplyr)
library(ggplot2)
#library(ggridges)
seu <- LoadH5Seurat('/ssd/mastitis_scRNAseq/outputs/pre_QC.h5seurat')
seu[['RNA']] <- seu[['originalexp']]
DefaultAssay(seu) <- 'RNA'
seu[['originalexp']] <- NULL
seu@meta.data %>%
ggplot(aes(x=nCount_RNA)) +
geom_histogram(bins=100)+
scale_x_log10()+
facet_wrap(~tissue+individual) + #xlim(0,5000) +
geom_vline(xintercept = c(500)) +
ggtitle('UMIs per cell (log scale)', 'cutoff = 500')
seu@meta.data %>%
ggplot(aes(x=nFeature_RNA)) +
geom_histogram(bins = 100)+
scale_x_log10()+
facet_wrap(~tissue+individual) + #xlim(0,2000) +
geom_vline(xintercept = c(250)) +
ggtitle('Genes per cell (log scale)', 'cutoff = 250')
seu@meta.data %>%
ggplot(aes(x=subsets_mitochondria_percent)) +
geom_histogram(bins=100)+
facet_wrap(~tissue+individual) + xlim(0,25) +
geom_vline(xintercept = c(12.5))+
ggtitle('Percent mitochondrial reads per cell', 'cutoff = 12.5%')
seu@meta.data %>%
ggplot(aes(y=nFeature_RNA,
x=nCount_RNA,
color=subsets_mitochondria_percent)) +
geom_point() +
scale_colour_gradient(low = "tan", high = "red4") +
stat_smooth(method=lm) +
scale_x_log10() +
scale_y_log10() +
theme_classic() +
geom_vline(xintercept = 500) +
geom_hline(yintercept = 250)+
facet_wrap(~tissue+individual)
seu@meta.data %>%
ggplot(aes(y=nFeature_RNA,
x=nCount_RNA,
color=subsets_mitochondria_percent)) +
geom_point(color = dplyr::case_when(seu@meta.data$scDblFinder.class == 'singlet' ~ "grey70",
seu@meta.data$scDblFinder.class == 'doublet' ~ "red4")) +
stat_smooth(method=lm) +
scale_x_log10() +
scale_y_log10() +
theme_classic() +
geom_vline(xintercept = 500) +
geom_hline(yintercept = 250)+
facet_wrap(~tissue+individual)
table(seu$scDblFinder.class, seu$sample_ID)
seu@meta.data %>%
ggplot(aes(y=nFeature_RNA,
x=nCount_RNA,
color=subsets_mitochondria_percent)) +
geom_point(color = dplyr::case_when(seu@meta.data$subsets_mitochondria_percent < 12.5 &
seu@meta.data$nCount_RNA > 500 &
seu@meta.data$nFeature_RNA > 250 &
seu@meta.data$scDblFinder.class == 'singlet' ~ "grey70",
TRUE ~ "red4")) +
stat_smooth(method=lm) +
scale_x_log10() +
scale_y_log10() +
theme_classic() +
geom_vline(xintercept = 500) +
geom_hline(yintercept = 250)+
facet_wrap(~tissue+individual)
seu@meta.data %>%
ggplot(aes(x=individual, y=subsets_rRNA_percent, fill=tissue)) + geom_violin() +
ggtitle('rRNA depletion appears to have failed for one sample')
# num features
seu@meta.data %>%
ggplot(aes(x=individual, y=nFeature_originalexp, fill=tissue)) +
geom_violin()
# num features by doublet status
seu@meta.data %>%
ggplot(aes(x=scDblFinder.class, y=nFeature_originalexp, fill=tissue)) +
geom_violin()
# this one for singlets vs doublets
seu@meta.data %>%
group_by(sample_ID, tissue, individual, scDblFinder.class) %>%
tally() %>%
ggplot(aes(x=scDblFinder.class, y=n, fill=tissue)) +
geom_col(position = position_dodge()) +
facet_wrap(~individual)
Idents(seu) <- seu$sample_ID
RidgePlot(seu, 'log10GenesPerUMI', same.y.lims = TRUE)
seu@meta.data <-
seu@meta.data %>%
mutate(REMOVE=case_when(
scDblFinder.class == 'doublet' ~ 'DOUBLET',
subsets_mitochondria_percent > 12.5 ~ 'PCT_MIT_ABOVE_12.5',
nFeature_originalexp < 250 ~ 'GENE_COUNT_BELOW_250',
nCount_originalexp < 500 ~ 'UMIs_BELOW_500',
TRUE ~ 'KEEP'),
REMOVE=factor(REMOVE, levels = c('KEEP', 'DOUBLET', 'GENE_COUNT_BELOW_250',
'PCT_MIT_ABOVE_12.5', 'UMIs_BELOW_500')))
seu@meta.data %>%
group_by(tissue, individual) %>%
count(REMOVE) %>%
ggplot(aes(y=REMOVE, x=n, fill=REMOVE)) +
geom_col(color='black') +
geom_text(aes(label=n),hjust=0 )+
facet_wrap(~individual+tissue, ncol=2) +
xlab('number of cells') +
theme(legend.position = 'none') +
xlim(0,10000) +
ggtitle('Cells removed by different QC metrics')
# remove bad cells
seu_filt <- subset(seu, subset = REMOVE == 'KEEP')
# remove genes with no expression in remaining cells
non_zero_Features <- names(which(!rowSums(seu_filt) == 0))
seu_filt <- subset(seu_filt, features=non_zero_Features)
SaveH5Seurat(seu_filt, '/home/Jayne.Wiarda/scRNAseqMastitisMilkBlood/Seurat/20230127_JEW_FilteredSeurat.h5seurat', overwrite = TRUE)