-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFigure4.R
192 lines (176 loc) · 10.8 KB
/
Figure4.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
# Reproduce Figure 4 from Katsevich, Barry, and Roeder (2020).
args <- commandArgs(trailingOnly = TRUE)
code_dir <- if (is.na(args[1])) "~/research_code/sceptre-manuscript/" else args[1]
require(katsevich2020)
require(ggrepel)
require(cowplot)
source(paste0(code_dir, "/sceptre_paper/plotting/load_data_for_plotting.R"))
fig4_dir <- paste0(manuscript_figure_dir, "/Figure4")
# subfigure a: old vs. new rejections
annotations <- tibble(gene_short_name = c("B3GNT2", "PTPN1", "TOP1", "AGFG1", "EIF1"),
eQTL = c(2.7e-26, NA, NA, 5.2e-8,NA),
eRNA = c(NA, 2e-18, 6.6e-5,NA, 1.2e-6))
promising_new_rejections <- resampling_results_gasp %>%
filter(gene_short_name %in% c("B3GNT2", "PTPN1", "TOP1", "AGFG1", "EIF1"), rejected) %>%
inner_join(original_results_gasp %>% filter(pvalue.empirical.adjusted > 0.1) %>%
select(gene_id, grna_group, pvalue.empirical), by = c("gene_id", "grna_group")) %>%
left_join(annotations, by = "gene_short_name") %>%
arrange(desc(quality_rank_grna), p_value) %>%
mutate(pair_number = row_number()) %>%
select(pair_number, pair_id, gene_short_name, target_site, p_value,
pvalue.empirical, eQTL, eRNA, quality_rank_grna)
thresh_new <- resampling_results_gasp %>% filter(rejected) %>% summarise(max(p_value)) %>% pull()
thresh_old <- original_results_gasp %>% filter(rejected) %>% summarise(max(pvalue.empirical)) %>% pull()
df_a <- resampling_results_gasp %>%
rename(new_pvalue = p_value, new_rejected = rejected) %>%
select(pair_id, gene_id, grna_group, new_pvalue, site_type, quality_rank_grna, new_rejected) %>%
left_join(original_results_gasp %>%
rename(old_pvalue = pvalue.empirical, old_rejected = rejected) %>%
select(gene_id, grna_group, old_pvalue, old_rejected, beta, outlier_gene), by = c("gene_id", "grna_group")) %>%
filter(site_type == "DHS", quality_rank_grna == "top_two") %>%
select(-c("site_type", "quality_rank_grna")) %>%
mutate(reason_not_rejected = ifelse(outlier_gene,
"Outlier gene",
ifelse(beta > 0,
"Positive effect",
ifelse(!old_rejected & old_pvalue <= thresh_old, "Low confidence", "none"))),
reason_not_rejected = factor(reason_not_rejected, levels = c("none", "Positive effect", "Outlier gene", "Low confidence")),
rejected_by = ifelse(old_rejected,
ifelse(new_rejected, "Both methods", "Gasperini only"),
ifelse(new_rejected, "SCEPTRE only", "Neither method"))) %>%
left_join(promising_new_rejections %>%
select(pair_number, pair_id) %>%
mutate(pair_number = as.character(pair_number)), by = "pair_id") %>%
mutate(pair_number = ifelse(is.na(pair_number), "", pair_number)) %>%
arrange(old_rejected, new_rejected) %>%
filter(old_pvalue < 1e-2 |
new_pvalue < 1e-2 |
reason_not_rejected != "none" |
row_number() %% 10 == 0)
n_rejected <- table(df_a$rejected_by)
n_rejected_annotated <- ifelse(names(n_rejected) == "Neither method", "Neither method", paste0(names(n_rejected), " (", n_rejected,")"))
matches <- match(x = df_a$rejected_by, table = names(n_rejected))
rejected_by_annotated <- n_rejected_annotated[matches]
my_labs <- n_rejected_annotated[match(c("Neither method", "Both methods", "SCEPTRE only", "Gasperini only"), names(n_rejected))]
df_a$rejected_by_annotated <- factor(x = rejected_by_annotated, levels = my_labs, labels = my_labs)
df_a$outlier_gene <- as.logical(df_a$outlier_gene)
filter(df_a, outlier_gene, new_rejected) %>% pull(gene_id) %>% unique() %>% length()
filter(df_a, outlier_gene, new_rejected) %>% nrow()
p_a <- ggplot(data = arrange(df_a, rejected_by_annotated), aes(x = old_pvalue, y = new_pvalue,
color = rejected_by_annotated, label = pair_number, shape = outlier_gene)) + geom_point(alpha = 0.9) +
geom_text_repel(colour = "black", force = 0.8, point.padding = 0.1, box.padding = 0.3, size = 3, min.segment.length = 0, max.overlaps = 50) +
geom_vline(xintercept = thresh_old, linetype = "dashed") + geom_hline(yintercept = thresh_new, linetype = "dashed") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
scale_color_manual(values = c("grey60", "darkorchid1", plot_colors[["sceptre"]], plot_colors[["gasperini_nb"]]), name = "Discovered by") +
scale_shape_manual(values = c("circle", "triangle"), labels = c("Non-outlier", "Outlier"),name = "Gasperini analysis") +
scale_x_log10() + scale_y_log10() +
xlab("Gasperini empirical p-value for gene-gRNA pair") +
guides(color = guide_legend(order = 1)) +
guides(shape = guide_legend(order = 2)) +
ylab("SCEPTRE p-value for gene-gRNA pair") + ggtitle("SCEPTRE vs. Gasperini p-values") +
theme_bw() + theme(
legend.position = c(0.77, 0.22),
legend.key = element_rect(colour = "transparent", fill = "transparent"),
legend.background = element_rect(color = "transparent", fill = "white"),
legend.margin = margin(-0.25,0,0,0, unit="cm"),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
plot.title = element_text(hjust = 0.5))
# subfigure b (distances)
df = original_results_gasp %>%
rename(old_rejected = rejected, old_pvalue = pvalue.empirical) %>%
filter(site_type == "DHS", quality_rank_grna == "top_two") %>%
left_join(resampling_results_gasp %>%
select(gene_id, grna_group, rejected) %>%
rename(new_rejected = rejected),
by = c("gene_id", "grna_group")) %>%
mutate(strand = ifelse(target_gene.start == TSS, "+", "-"),
enhancer_location = 0.5*(target_site.start + target_site.stop),
TSS_dist = ifelse(strand == "+", enhancer_location - TSS, TSS - enhancer_location))
df1 = df %>% filter(old_rejected) %>% mutate(group = "old_rejections")
df2 = df %>% filter(new_rejected) %>% mutate(group = "new_rejections")
dist_tab <- rbind(
df1 %>%
filter(TSS_dist <= 0) %>%
summarise(`Mean distance (kb)` = mean(TSS_dist)/1000,
`Median distance (kb)` = median(TSS_dist)/1000) %>%
mutate(method = "Original"),
df2 %>%
filter(TSS_dist <= 0) %>%
summarise(`Mean distance (kb)` = mean(TSS_dist)/1000, `Median distance (kb)` = median(TSS_dist)/1000) %>%
mutate(method = "SCEPTRE")
) %>%
gather(metric, measure, -method) %>%
spread(method, measure) # %>% kable(format = "latex", booktabs = TRUE, escape = FALSE, linesep = "",digits = 1, col.names = c("", "Original", "SCEPTRE"))
p_b <- rbind(df1, df2) %>%
filter(TSS_dist <= 0) %>%
mutate(group = factor(group,
levels = c("new_rejections", "old_rejections"),
labels = c("SCEPTRE", "Original"))) %>%
mutate(TSS_dist = TSS_dist/1000) %>%
filter(TSS_dist >= -300) %>%
ggplot(aes(x = TSS_dist, group = desc(group), fill = group)) +
geom_histogram(position = position_identity(), alpha = 0.75, bins = 14, aes(y = ..density..)) +
scale_fill_manual(values = c(plot_colors[["sceptre"]], plot_colors[["gasperini_nb"]])) +
scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) +
xlab("Enhancer to TSS distance (kb)") + ylab("Frequency") + ggtitle("Gene-enhancer distances") +
theme_bw() + theme(panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
legend.title = element_blank(),
legend.background = element_blank(),
legend.position = c(0.15, 0.72),
plot.title = element_text(hjust = 0.5))
# subfigure c (TADs)
old_scores = rejected_pairs_HIC %>% filter(rejected_old, !is.na(score_rank)) %>% pull(score_rank)
new_scores = rejected_pairs_HIC %>% filter(rejected_new, !is.na(score_rank)) %>% pull(score_rank)
sceptre_percent <- length(new_scores)/(df_a$new_rejected %>% sum)
gasp_percent <- length(old_scores)/(df_a$old_rejected %>% sum)
x = seq(0, 1 + 1e-10, length.out = 1000)
old_ecdf = ecdf(old_scores)(x)
new_ecdf = ecdf(new_scores)(x)
p_c <- tibble(x, old_ecdf, new_ecdf) %>%
gather(method, ecdf, -x) %>%
mutate(method = factor(method, levels = c("new_ecdf", "old_ecdf"), labels = c("SCEPTRE", "Original"))) %>%
ggplot(aes(x = x, y = ecdf, group = method, colour = method)) +
geom_line(lwd = 1.2) + geom_abline(slope = 1, linetype = "dashed") +
scale_colour_manual(values = c( plot_colors[["sceptre"]], plot_colors[["gasperini_nb"]]), name = "Loci pairs") + ggtitle("Gene-enhancer HIC interactions") +
xlab("Rank of pair by TAD interaction frequency") +
ylab("Cumulative fraction of pairs") +
theme_bw() + theme(legend.position = c(0.15, 0.72),
legend.title = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
legend.background = element_blank(),
axis.line = element_line(),
plot.title = element_text(hjust = 0.5))
# subfigure d (Chip-seq)
my_order <- TF_enrichments %>% filter(method == "Proposed") %>% pull(enrichment) %>% order()
ordered_labs <- (TF_enrichments %>% filter(method == "Proposed") %>% pull(TF))[my_order]
p_d <- TF_enrichments %>% arrange(desc(method)) %>%
mutate(method = factor(method, levels = c("Original", "Proposed"), labels = c("Original", "SCEPTRE")),
TF = factor(TF,
levels = ordered_labs,
labels = ordered_labs)) %>%
ggplot(aes(x = TF, y = enrichment, fill = method)) +
geom_col(position = "dodge", width = 1.25) +
xlab("ChIP-seq target") + ylab("Enrichment (odds ratio)") + ggtitle("Enhancer ChIP-seq enrichment") +
scale_fill_manual(values = c(plot_colors[["gasperini_nb"]], plot_colors[["sceptre"]])) +
scale_y_continuous(expand = c(0, 0)) +
geom_hline(yintercept = 1, linetype = "dashed") +
theme_bw() + coord_flip() + theme(
legend.position = "none",
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 0, vjust = 0.2, hjust=0))
# subfigure e (GTEx eQTL FANTOM)
# make a table of the selected pairs with title
p_e <- ggplot() + theme_bw() + ggtitle("eQTL and eRNA validation") + theme(plot.title = element_text(hjust = 0.5), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank())
# combine plots: option 1
left_col <- plot_grid(p_a, p_d, align = "v", labels = c("a", "d"), ncol = 1, rel_heights = c(2,1))
right_col <- plot_grid(p_b, p_c, p_e, align = "vh", labels = c("b", "c", "e"), ncol = 1)
final_plot <- plot_grid(left_col, right_col, align = "h", ncol = 2, rel_widths = c(1.1,1))
ggsave(filename = paste0(fig4_dir, "/subfigures_a_thru_e.pdf"), plot = final_plot, device = "pdf", scale = 1, width = 8, height = 8)