-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontamination_removal.R
167 lines (132 loc) · 7.86 KB
/
contamination_removal.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
source('Codes/Functions.R')
source('Codes/convert_human_to_ortholog_functions.R')
Initialize()
library(SoupX)
# note: DA_M_10WK_004_strained is the 200515_A00827_0155_AHLV55DRXX_McParland_Sonya reseq sample
sample_names = list.dirs('~/RatLiver/Data/SoupX_data/SoupX_inputs/',recursive = FALSE, full.names = FALSE)
for(i in 1:length(sample_names)){
sample_name = sample_names[i]
print(sample_name)
sc = load10X(paste0('~/RatLiver/Data/SoupX_data/SoupX_inputs/', sample_name, '/'))
sc = autoEstCont(sc, forceAccept = TRUE)
out = adjustCounts(sc)
#saveRDS(list(sc=sc,out=out),paste0('~/RatLiver/Data/SoupX_data/SoupX_outputs/', sample_name, '_soupX_out.rds'))
}
# rat_DA_01_reseq: mito=30, library size= 1500
# rat_DA_M_10WK_003: mito=20, library size=2000
# rat_Lew_01: mito=40, library size= 2000
# rat_Lew_02: mito=40, library size= 2000
######### importing the old samples raw data
## Define cut-off values
qc_info = list('MacParland_Sonya__RAT_SHAM_DA_M_10WK_003_strained'=list(LIB_SIZE_CUT_OFF=2000, MIT_CUT_OFF=20),
'McParland_Sonya__RAT_SHAM_DA_M_10WK_004_strained'=list(LIB_SIZE_CUT_OFF=1500, MIT_CUT_OFF=30),
'McParland_Sonya__RAT_SHAM_LEW_M_13WK_001_strained'=list(LIB_SIZE_CUT_OFF = 2000,MIT_CUT_OFF = 40),
'McParland_Sonya__RAT_SHAM_LEW_M_13WK_002_strained'=list(LIB_SIZE_CUT_OFF = 2000,MIT_CUT_OFF = 40))
data_norm_list = list(NA, NA, NA, NA)
names(data_norm_list) = sample_names
strain_info = c('DA', 'DA', 'LEW', 'LEW')
for(i in 1:length(sample_names)){
sample_name = sample_names[i]
#soupX_out = readRDS(paste0('~/RatLiver/Data/SoupX_data/SoupX_outputs/default_param/', sample_name, '_soupX_out.rds'))
soupX_out = readRDS(paste0('~/RatLiver/Data/SoupX_data/SoupX_outputs/paramPlus01/', sample_name, '_soupX_out_Rhoplus.0.1.rds'))
#soupX_out = readRDS(paste0('~/RatLiver/Data/SoupX_data/SoupX_outputs/paramPlus02/', sample_name, '_soupX_out_Rhoplus.0.2.rds'))
#soup_profile = soupX_out$sc$soupProfile
rownames(soupX_out$out) = gsub(rownames(soupX_out$out),pattern = '_', replacement = '-')
data = CreateSeuratObject(counts = soupX_out$out)
data$sample_name = sample_name
data$strain = strain_info[i]
MIT_PATTERN = '^Mt-'
mito_genes_index <- grep(pattern = MIT_PATTERN, rownames(data) )
data[["mito_perc"]] <- PercentageFeatureSet(data, features = mito_genes_index)
to_drop_mito = data$mito_perc > qc_info[[sample_name]]$MIT_CUT_OFF
to_drop_lib_size = data$nCount_RNA < qc_info[[sample_name]]$LIB_SIZE_CUT_OFF
to_drop_numgenes = data$nFeature_RNA < 100
summary(data$mito_perc )
summary(data$nFeature_RNA )
sum(!to_drop_mito & !to_drop_numgenes)
sum(!to_drop_mito & !to_drop_lib_size)
sum(to_drop_lib_size)
sum(to_drop_numgenes)
sum(to_drop_mito)
data <- data[,!to_drop_mito & !to_drop_numgenes]
data = SCTransform(data, vst.flavor = "v2", verbose = TRUE)
data_norm_list[[i]] = data
}
rm(soupX_out)
rm(data)
gc()
merged_data <- merge(data_norm_list[[1]], c(data_norm_list[[2]], data_norm_list[[3]], data_norm_list[[4]] ), #
add.cell.ids = names(data_norm_list),
project = names(data_norm_list),
merge.data = TRUE)
dim(merged_data)
Idents(merged_data) = merged_data$sample_name
rm(data_norm_list)
gc()
#saveRDS(merged_data, '~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.2.rds')
saveRDS(merged_data, '~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.1.rds')
merged_data <- readRDS( '~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.1.rds')
#merged_data <- readRDS('~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.2.rds')
DefaultAssay(merged_data) <- 'SCT'
merged_data@assays$RNA <- NULL
gc()
#merged_data_backup <- merged_data
#merged_data <- merged_data_backup
### run the pipeline without this line and see how it would differ - could not run pca and find variable genes
#### scaling the merged data instead of SCTransform does not solve the issue
### current pipeline: we are normlizing the individual samples first - merging them - normalizing the merged sample
merged_data = SCTransform(merged_data, vst.flavor = "v2", verbose = TRUE, assay = 'SCT', conserve.memory = TRUE)
#merged_data <- ScaleData(merged_data)
#merged_data <- FindVariableFeatures(merged_data) #SCT assay is comprised of multiple SCT models
#saveRDS(merged_data, '~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.2_normed.rds')
#saveRDS(merged_data, '~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.1_normed.rds')
#merged_data <- readRDS('~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_normed.rds')
#merged_data <- readRDS('~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.2_normed.rds')
merged_data = readRDS('~/RatLiver/Data/SoupX_data/TLH_decontaminated_merged_Rhoplus.0.1_normed.rds')
merged_data <- RunPCA(merged_data,verbose=T)
plot(100 * merged_data@reductions$pca@stdev^2 / merged_data@reductions$pca@misc$total.variance,
pch=20,xlab="Principal Component",ylab="% variance explained",log="y")
#### UMAP on PC components and clustering
#merged_data <- RunUMAP(merged_data, reduction = "pca", dims = 1:30, verbose = FALSE) %>%
# FindNeighbors(reduction = "pca", dims = 1:30, verbose = FALSE) %>%
# FindClusters(resolution = 0.7, verbose = FALSE)
#### UMAP on corrected PC components and clustering
merged_data <- RunHarmony(merged_data, group.by.vars = "sample_name")
merged_data <- RunUMAP(merged_data, reduction = "harmony", dims = 1:30, reduction.name = "umap_h") %>%
FindNeighbors(reduction = "harmony", dims = 1:30, verbose = FALSE) %>%
FindClusters(resolution = 0.7, verbose = FALSE)
Resolution = 0.2
merged_data <- FindClusters(merged_data, resolution = Resolution, verbose = FALSE)
gene_name = 'Cd68'
is_cluster = ifelse(merged_data$seurat_clusters== 7, 'cluster', 'other')
df_umap <- data.frame(#UMAP_1=getEmb(merged_data, 'umap')[,1],
#UMAP_2=getEmb(merged_data, 'umap')[,2],
UMAP_h_1=getEmb(merged_data, 'umap_h')[,1],
UMAP_h_2=getEmb(merged_data, 'umap_h')[,2],
library_size= merged_data$nCount_RNA,
mito_perc=merged_data$mito_perc,
n_expressed=merged_data$nFeature_RNA,
cluster=merged_data$seurat_clusters,
is_cluster=is_cluster,
#cell_status = merged_data$cell_status,
#nuclear_fraction=merged_data$nuclear_fraction,
Alb=GetAssayData(merged_data)['Alb',],
gene=GetAssayData(merged_data)[gene_name,],
sample_name = merged_data$sample_name,
strain = merged_data$strain)
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=is_cluster))+geom_point(alpha=0.8)+theme_classic()#+ggtitle(paste0('res: ', Resolution))
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=cluster))+geom_point(alpha=0.8)+theme_classic()#+ggtitle(paste0('res: ', Resolution))
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=sample_name))+geom_point(alpha=0.3)+theme_classic()#+ggtitle(paste0('res: ', Resolution))
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=gene))+geom_point(alpha=0.4)+theme_classic()+
scale_color_viridis(direction = -1)+ggtitle(gene_name)
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=Alb))+geom_point(alpha=0.4)+theme_classic()+
scale_color_viridis(direction = -1)+ggtitle('Alb')
genes = c('Alb', 'Tat', 'G6pc', 'Cps1', 'Tdo2')
genes = c('Lyve1', 'Id3') # Lsecs
genes = c('Igfbp7', 'Rbp1', 'Col3a1', 'Sparc') # stellate cells
genes = c('Nkg7', 'Cd7')
Idents(merged_data) = as.character(merged_data$seurat_clusters)
cluster7_markers = FindMarkers(merged_data, ident.1 = '7')
cluster7_markers <- cluster7_markers[order(cluster7_markers$avg_log2FC, decreasing = T),]
cluster7_markers <- cluster7_markers[order(cluster7_markers$p_val_adj, decreasing = F),]
head(cluster7_markers,30)