This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTG_report_large_screen.Rmd
241 lines (205 loc) · 8.81 KB
/
CTG_report_large_screen.Rmd
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
---
params:
data_file: "~/Downloads/CTG_Calico_Low/CAL_ctg_viability_data.Rds"
title: "`r basename(dirname(params$data_file))`"
author: "Andrew Boghossian"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F, include = F, warning = F, error = F, message = F)
library(plyr)
library(tidyverse)
library(ggthemes)
library(magrittr)
library(future.apply)
library(psych)
library(ComplexHeatmap)
library(viridis)
library(circlize)
library(ggridges)
theme_set(theme_pander(base_size = 12))
suppressMessages(source("src/helperFunctions.R"))
suppressMessages(source("src/make_dose_curves.R", chdir = T))
project <- word(basename(dirname(params$data_file)), 2, -1, sep = fixed("_"))
```
```{r load data}
normalized_data <- read_rds(params$data_file)
if (all(is.na(normalized_data$plate_map_name)) &
"Source Barcode" %in% colnames(normalized_data)) {
normalized_data %<>% dplyr::mutate(plate_map_name = `Source Barcode`)
}
```
# Introduction
This is a QC of the `r project` CTG plates.
# Luminescence and viability distributions {.tabset .tabset-pills}
## Luminescence distributions {.tabset .tabset-fade}
```{r luminescence, results = 'asis', include = T, fig.height = 20, fig.width=12}
for (cur_plate in normalized_data$plate_map_name %>% unique()) {
cat(sprintf('\n\n### %s {.tabset .tabset-fade}\n\n', cur_plate, '\n\n'))
df <- normalized_data %>%
dplyr::filter(plate_map_name == cur_plate) %>%
dplyr::mutate(lum = lum + 1)
# p <- df %>%
# ggplot(aes(x = lum, color = pert_type)) +
# geom_density() +
# geom_histogram(aes( y = ..density.., fill = pert_type), alpha = .3, position = 'identity') +
# scale_x_log10() +
# labs(x = "Luminescence", y = "Density", color = "Type") +
# ggtitle('Distribution of luminescence, separated by treatment type') +
# scale_color_tableau(guide = "none") + scale_fill_tableau(name = "pert_type") +
# facet_grid(ccle_name ~ replicate, scales = "free_y")
#
# print(p)
p <- df %>%
ggplot(aes(y = ccle_name, x = lum, color = pert_type, fill = pert_type)) +
geom_density_ridges(alpha = .3, scale = 1) +
scale_x_log10() +
labs(x = "Luminescence", y = "Cell Line", color = "Type", fill = "Type") +
ggtitle('Distribution of luminescence, separated by treatment type') +
scale_color_tableau(guide = "none") + scale_fill_tableau(name = "Type") +
facet_grid(. ~ replicate, scales = "free_y")
print(p)
cat("\n\n")
}
```
## Viability distributions (poscon normalized fold-change) {.tabset .tabset-fade}
```{r, results= 'asis', include = T, fig.height = 20, fig.width=12}
for (cur_plate in normalized_data$plate_map_name %>% unique()) {
cat(sprintf('\n\n### %s {.tabset .tabset-fade}\n\n', cur_plate, '\n\n'))
df <- normalized_data %>%
dplyr::filter(plate_map_name == cur_plate, is.finite(viability), viability > 0)
p <- df %>%
ggplot(aes(y = ccle_name, x = viability, color = pert_type, fill = pert_type)) +
geom_density_ridges(alpha = .3, scale = 1) +
scale_x_log10() +
labs(x = "Viability (poscon normalized)", y = "Cell Line", color = "Type", fill = "Type") +
ggtitle('Distribution of viability, separated by treatment type') +
scale_color_tableau(guide = "none") + scale_fill_tableau(name = "Type") +
facet_grid(. ~ replicate, scales = "free_y")
print(p)
cat("\n\n")
}
```
## Viability distributions (fold-change) {.tabset .tabset-fade}
```{r, results = 'asis', include = T, fig.height = 20, fig.width=12}
for (cur_plate in normalized_data$plate_map_name %>% unique()) {
cat(sprintf('\n\n### %s {.tabset .tabset-fade}\n\n', cur_plate, '\n\n'))
df <- normalized_data %>%
dplyr::filter(plate_map_name == cur_plate, is.finite(log_fc))
p <- df %>%
ggplot(aes(y = ccle_name, x = 2^log_fc, color = pert_type, fill = pert_type)) +
geom_density_ridges(alpha = .3, scale = 1) +
labs(x = "Viability", y = "Cell Line", color = "Type", fill = "Type") +
scale_x_log10() +
ggtitle('Distribution of viability, separated by treatment type') +
scale_color_tableau(guide = "none") + scale_fill_tableau(name = "Type") +
facet_grid(. ~ replicate, scales = "free_y")
print(p)
cat("\n\n")
}
```
# SSMDs
```{r calc ssmd}
ssmds <- normalized_data %>%
dplyr::group_by(arp_barcode, ccle_name, plate_map_name, replicate) %>%
dplyr::summarise(ssmd = calculate_ssmd_robust(log_fc[pert_type == 'poscon'],
log_fc[pert_type == 'negcon'])) %>%
dplyr::ungroup()
```
```{r ssmd plot, include = T, fig.width=12, fig.height=8}
ssmds %>%
DT::datatable()
ssmds %>%
ggplot(aes(x = ssmd, fill = paste(word(ccle_name, 1, sep = fixed("_")), plate_map_name, replicate))) +
geom_histogram() +
labs(x = "SSMD", y = "Count", fill = "Cell Line Replicate") +
theme(legend.position = "none") +
scale_y_continuous(limits = c(0, NA)) +
geom_vline(aes(xintercept = -2), linetype = "dashed")
```
# Replicate correlations {.tabset .tabset-pills}
## Viability (poscon normalized)
```{r, include = T, fig.height=12, fig.width=12}
normalized_data %>%
dplyr::filter(pert_type != "treatment" | !is.na(dose),
is.finite(viability)) %>%
tidyr::pivot_wider(id_cols = c("ccle_name", "pert_type", "dose", "broad_id"),
names_from = c("plate_map_name", "replicate"),
values_from = "viability",
values_fn = median) %>%
dplyr::select(-ccle_name, -dose, -pert_type, -broad_id) %>%
psych::pairs.panels(ellipses = F, lm = T, hist.col = tableau_color_pal()(1))
```
## Viability (log fold-change)
```{r, include = T, fig.height=12, fig.width=12}
normalized_data %>%
dplyr::filter(!is.na(dose), is.finite(log_fc)) %>%
tidyr::pivot_wider(id_cols = c("ccle_name", "pert_type", "dose", "broad_id"),
names_from = c("plate_map_name", "replicate"),
values_from = "log_fc",
values_fn = median) %>%
dplyr::select(-ccle_name, -dose, -pert_type, -broad_id) %>%
psych::pairs.panels(ellipses = F, lm = T, hist.col = tableau_color_pal()(1))
```
# Does viability decrease with dose?
## Spearman correlation
We can use spearman correlation to measure the monotonicity of dose vs viability.
Negative correlations indicate dose-response behavior.
```{r correlations, include = TRUE, fig.height=10, fig.width=24}
spearman_correlations <- tryCatch(
expr = {normalized_data %>%
dplyr::group_by(arp_barcode, broad_id, ccle_name, plate_map_name, replicate) %>%
dplyr::filter(length(unique(dose)) > 3) %>%
dplyr::summarise(spearman = cor(viability, dose, method = 'spearman', use = 'p'), .groups = "drop") %>%
dplyr::ungroup()
}, error = function(e) {return(NA)}
)
if (!is.na(spearman_correlations)) {
ggplot(spearman_correlations, aes(y = spearman, x = replicate)) +
geom_boxplot(aes(fill = replicate)) +
geom_hline(yintercept = 0, linetype = "dashed") +
theme(axis.text.x = element_text(angle = 45)) +
scale_fill_tableau(guide = "none") +
facet_grid(plate_map_name ~ ccle_name, scales = 'free') +
labs(x = "Replicate", y = "Spearman Correlation")
} else {
print("Unable to calculate correlations not enough doses")
}
```
# Heatmaps {.tabset .tabset-pills}
```{r, results='asis', include = T, fig.height=3, fig.width=8}
for (cell_line in (normalized_data %>%
dplyr::filter(is.finite(lum)))$ccle_name %>% unique()) {
cat(sprintf('\n\n## %s {.tabset .tabset-pills}\n\n', cell_line, '\n\n'))
df <- normalized_data %>% dplyr::filter(ccle_name == cell_line) %>%
dplyr::mutate(plate_row = str_sub(Well_Position, 1, 1),
plate_col = str_sub(Well_Position, 2, -1)) %>%
dplyr::arrange(replicate)
plots <- list(); ix <- 1
for (plate in df$arp_barcode %>% unique) {
plot_df <- df %>%
dplyr::filter(arp_barcode == plate) %>%
dplyr::distinct(replicate, plate_row, plate_col, lum, pert_type) %>%
dplyr::arrange(plate_col, plate_row)
if (all(is.na(plot_df$lum))) next
rep <- unique(plot_df$replicate)
plot_mat <- plot_df %>%
reshape2::acast(plate_row ~ plate_col, value.var = "lum")
type_mat <- plot_df %>%
dplyr::mutate(pert_type = str_sub(pert_type, 1, 1)) %>%
reshape2::acast(plate_row ~ plate_col, value.var = "pert_type")
p <- ComplexHeatmap::Heatmap(plot_mat, cluster_rows = F, cluster_columns = F,
heatmap_legend_param = list(title = "Luminescence"),
row_title = paste(plate, rep),
col = viridis(1000),
cell_fun = function(j, i, x, y, w, h, col) {grid.text(type_mat[i, j], x, y)})
# plots[[ix]] <- p; ix <- ix +1
print(p)
cat("\n\n")
}
# ht_list <- reduce(plots, `%v%`)
# draw(ht_list, merge_legend = F, show_heatmap_legend = F)
# cat("\n\n")
}
```