-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorrect_ambiance.R
177 lines (136 loc) · 4.33 KB
/
correct_ambiance.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
# Creates a cell_data_set with an additional assay `soupx_counts`, containing
# decontaminated counts as determined by SoupX.
#
# @DEPI data_raw/rna_seq
# @DEPI rna_integrated_monocle.rds
# @DEPO rna_decontaminated.rds
library(SingleCellExperiment)
library(DropletUtils)
library(monocle3)
library(SoupX)
library(tidyverse)
library(fs)
library(ggpmisc)
library(patchwork)
source("common_functions.R")
# Parameters --------------------------------------------------------------
# folder that contains cellranger results
data_dir <- "data_raw/rna_seq"
# integrated dataset
in_file <- "data_generated/rna_integrated_monocle.rds"
# dataset with decontaminated counts
out_file <- "data_generated/rna_decontaminated.rds"
# data frame with three columns 'order', 'sample_name', and 'sample_file'
samples <-
read_csv("metadata/sample_groups.csv", comment = "#") %>%
arrange(bsf_order) %>%
select(order = bsf_order, sample_file = geo_rna_raw, sample_name = sample)
# Load data ---------------------------------------------------------------
nb <- readRDS(in_file)
nb_metadata <- readRDS("data_generated/metadata.rds")
# Analysis ----------------------------------------------------------------
remove_soup <- function(bsf_order) {
info("Removing soup for ID {bsf_order}")
# subset filtered cells
sce_cells <- nb[, str_ends(colnames(nb), str_glue("-{bsf_order}"))]
sce_cells
# load raw droplet data
raw_file <-
samples %>%
filter(order == {{bsf_order}}) %>%
pull(sample_file)
raw_counts <- Seurat::Read10X_h5(path_join(c(data_dir, raw_file)))
# calculate contamination fraction
soup_channel <-
SoupChannel(tod = raw_counts, toc = counts(sce_cells)) %>%
setClusters(
nb_metadata %>%
select(cell, cluster_50) %>%
semi_join(tibble(cell = rownames(colData(sce_cells))), by = "cell") %>%
deframe() %>%
as.character()
) %>%
setDR(
nb_metadata %>%
select(cell, umap_1_monocle, umap_2_monocle) %>%
semi_join(tibble(cell = rownames(colData(sce_cells))), by = "cell") %>%
column_to_rownames("cell"),
reductName = "UMAP"
) %>%
autoEstCont()
# return adjusted counts
list(
soup_channel = soup_channel,
adjusted_counts = adjustCounts(soup_channel)
)
}
soupx_results <- map(samples$order, remove_soup)
soupx_counts <-
soupx_results %>%
map(~.x$adjusted_counts) %>%
reduce(cbind)
waldo::compare(rownames(soupx_counts), rownames(nb))
waldo::compare(colnames(soupx_counts), colnames(nb))
assays(nb)$soupx_counts <- soupx_counts
# Plots -------------------------------------------------------------------
plot_change_map <- function(sce, genes, filename = NULL) {
coordinates <- reducedDim(sce, "UMAP")
sce_filtered <- sce[genes, rownames(coordinates), drop = FALSE]
old <-
counts(sce_filtered) %>%
colSums()
new <-
assay(sce_filtered, "soupx_counts") %>%
colSums()
rel_change <- (old - new) / old
p <-
coordinates %>%
magrittr::set_colnames(c("UMAP_1", "UMAP_2")) %>%
as_tibble() %>%
bind_cols(rel_change = rel_change) %>%
arrange(!is.na(rel_change), rel_change) %>%
ggplot(aes(UMAP_1, UMAP_2)) +
geom_point(aes(color = rel_change), size = .1) +
annotate(
"text_npc", npcx = 0.5, npcy = 1,
label = genes, fontface = "bold", hjust = 0.5
) +
scale_color_distiller(
"relative\nchange",
palette = "YlOrRd",
direction = 1,
na.value = "gray80",
limits = c(0, 1)
) +
coord_fixed() +
theme_classic() +
theme(panel.grid = element_blank())
ggsave_default(filename)
p
}
# plot_change_map(nb, "IL32")
map(
c("HBB", "IGKC", "LYZ", "GNLY", "GAL", "IL32"),
plot_change_map,
sce = nb
) %>%
wrap_plots(guides = "collect")
ggsave_default("ambiance/relative_change", width = 420, height = 297)
tibble(
gene = rownames(nb),
n_cells_orig = rowSums(counts(nb) > 0),
n_cells_corr = rowSums(assay(nb, "soupx_counts") > 0)
) %>%
mutate(
abs_change = n_cells_orig - n_cells_corr,
rel_change = abs_change / n_cells_orig
) %>%
arrange(desc(abs_change)) %>%
select(!rel_change) %>%
column_to_rownames("gene") %>%
head(20) %>%
gridExtra::tableGrob() %>%
wrap_plots()
ggsave_default("ambiance/table")
# Save results ------------------------------------------------------------
nb %>% saveRDS(out_file)