-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.Rmd
More file actions
290 lines (233 loc) · 9.45 KB
/
expression.Rmd
File metadata and controls
290 lines (233 loc) · 9.45 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
---
title: "Regulatory Activity of Selected Sequences — Yeast & Human"
author: "Emmanuel Cazottes"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
toc: true
toc_float: true
theme: united
highlight: tango
code_folding: show
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.width = 10,
fig.height = 6,
fig.align = "center"
)
library(stringr)
library(tidyverse)
library(reshape2)
library(ggpubr)
library(ggsci)
library(gridExtra)
publication_theme <- theme_pubr() +
theme(
axis.text = element_text(size = 7, color = "black"),
axis.title = element_text(size = 10, face = "plain"),
plot.title = element_text(size = 10, face = "plain", hjust = 0.5),
legend.title = element_text(size = 10, face = "plain"),
legend.text = element_text(size = 7),
axis.line = element_line(size = 0.5, color = "black"),
axis.ticks = element_line(size = 0.5, color = "black"),
axis.ticks.length = unit(0.2, "cm"),
legend.position = "right",
legend.box.background = element_rect(color = "white"),
legend.background = element_rect(fill = "white", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white")
)
theme_set(publication_theme)
```
Comparison of regulatory activity (expression / log2 fold-change) between
control sequences (top-n-bottom for diversity and uncertainty) and the AL
pool. A random subsample of 100 000 AL pool sequences provides a visual
baseline.
Expression data sources:
- **Yeast**: GPRA data from de Boer et al.
- **Human**: MPRA data from Gosai et al. (K562 only)
# Shared utility functions
```{r shared-functions}
#' Read a 3-column sequence TSV and return IDs + method.
load.sequences <- function(path) {
seq.df <- read.table(path, header = FALSE, sep = "\t",
col.names = c("IDs", "sequence", "method"))
seq.df$sequence <- NULL
seq.df
}
#' Rename raw control method labels to human-readable names.
#'
#' Uses regex so the same function works for both yeast (suffix _bottom1 /
#' _top101) and human (suffix _bottom10 / _top1010).
rename_tnb_labels <- function(method_vec) {
method_vec <- gsub("uncertainty_((1x60)|(3x20))_bottom\\d+", "Rarely uncertain", method_vec)
method_vec <- gsub("uncertainty_((1x60)|(3x20))_top\\d+", "Often uncertain", method_vec)
method_vec <- gsub("diversity_((1x60)|(3x20))_bottom\\d+", "Rarely diverse", method_vec)
method_vec <- gsub("diversity_((1x60)|(3x20))_top\\d+", "Often diverse", method_vec)
method_vec
}
#' Mann–Whitney U test of each group vs the AL pool reference.
#'
#' @param data Data.frame with columns `al.method` (factor) and `value`.
#' @param ref.label Label of the reference group (default "AL pool").
#' @return Data.frame with method, formatted p-value, and test statistic.
mann_whitney_vs_pool <- function(data, ref.label = "AL pool") {
ref.vals <- subset(data, al.method == ref.label)$value
map_dfr(levels(data$al.method), function(lvl) {
grp <- subset(data, al.method == lvl)$value
mwt <- wilcox.test(grp, ref.vals)
data.frame(
al.method = lvl,
pvalue = ifelse(mwt$p.value <= 2.2e-16,
"p<2.2e-16",
paste0("p=", signif(mwt$p.value, 2))),
statistic = mwt$statistic
)
})
}
#' Violin + boxplot of expression by method, with Mann–Whitney p-values.
#'
#' @param data Data.frame with `al.method` (factor) and `value`.
#' @param pval.df Data.frame from mann_whitney_vs_pool.
#' @param ylab.text Y-axis label.
#' @param pval.y Y position for the p-value text.
#' @param title Plot title.
#' @param violin.adjust Bandwidth adjustment for the violin (default 1).
plot_expression_violin <- function(data, pval.df, ylab.text, pval.y,
title = NULL, violin.adjust = 1) {
pval.df$y.axis <- pval.y
ggplot(data, aes(x = al.method, y = value)) +
geom_violin(width = 1.25, outliers = FALSE, adjust = violin.adjust) +
geom_boxplot(width = 0.1, outliers = FALSE) +
geom_text(data = pval.df,
aes(x = al.method, y = y.axis, label = pvalue), size = 3) +
xlab("") + ylab(ylab.text) +
{ if (!is.null(title)) ggtitle(title) } +
theme(aspect.ratio = 0.75,
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
plot.margin = margin(5, 5, 5, 5, "mm"),
axis.text = element_text(size = 7),
axis.title = element_text(size = 10))
}
LEVEL_ORDER <- c("AL pool", "Random selection",
"Rarely uncertain", "Often uncertain",
"Rarely diverse", "Often diverse")
```
# Yeast {.tabset}
## Data loading
```{r yeast-load}
# Expression data: headerless 2-column file (sequence, expression)
yeast.expr <- read.table("yeast/reference/GPRA_Rafi2024.txt", header = FALSE,
sep = "\t", col.names = c("Seq", "expression"))
yeast.expr$IDs <- seq(0, nrow(yeast.expr) - 1)
yeast.expr$Seq <- NULL
# Sequences
yeast.alpool <- load.sequences("yeast/sequences/yeast_pool.trim.tsv")
yeast.alpool$al.method <- "AL pool"
yeast.tnb <- load.sequences("yeast/sequences/yeast_top_n_bottom_1.trim.tsv")
yeast.tnb$al.method <- rename_tnb_labels(yeast.tnb$method)
# Split by pool size
yeast.tnb$rounds <- ifelse(grepl("1x60", yeast.tnb$method), "1x60k", "3x20k")
yeast.tnb.3x20 <- subset(yeast.tnb, rounds == "3x20k")
yeast.tnb.1x60 <- subset(yeast.tnb, rounds == "1x60k")
```
## Merge with expression & assemble
```{r yeast-merge}
yeast.tnb.3x20.expr <- merge(yeast.tnb.3x20, yeast.expr, by = "IDs")
yeast.tnb.1x60.expr <- merge(yeast.tnb.1x60, yeast.expr, by = "IDs")
yeast.alpool.expr <- merge(yeast.alpool, yeast.expr, by = "IDs")
set.seed(42)
yeast.rnd.expr <- yeast.alpool.expr[sample(nrow(yeast.alpool.expr), 100000), ]
yeast.rnd.expr$al.method <- "Random selection"
# Rename value column for the shared plotting function
yeast.data.3x20 <- rbind(yeast.tnb.3x20.expr[,-4], yeast.rnd.expr, yeast.alpool.expr)
yeast.data.3x20$value <- yeast.data.3x20$expression
yeast.data.3x20$al.method <- factor(yeast.data.3x20$al.method, levels = LEVEL_ORDER)
yeast.data.1x60 <- rbind(yeast.tnb.1x60.expr[,-4], yeast.rnd.expr, yeast.alpool.expr)
yeast.data.1x60$value <- yeast.data.1x60$expression
yeast.data.1x60$al.method <- factor(yeast.data.1x60$al.method, levels = LEVEL_ORDER)
```
## Plots
```{r yeast-plots, fig.width=4, fig.height=4}
yeast.pval.3x20 <- mann_whitney_vs_pool(yeast.data.3x20)
yeast.pval.1x60 <- mann_whitney_vs_pool(yeast.data.1x60)
p.yeast.3x20 <- plot_expression_violin(
yeast.data.3x20, yeast.pval.3x20,
ylab.text = "Expression", pval.y = 18, violin.adjust = 3
)
p.yeast.1x60 <- plot_expression_violin(
yeast.data.1x60, yeast.pval.1x60,
ylab.text = "Expression", pval.y = 18, violin.adjust = 3
)
p.yeast.3x20
p.yeast.1x60
```
```{r yeast-save}
dir.create("fig", showWarnings = FALSE, recursive = TRUE)
ggsave("fig/yeast.boxplot.expression.3x20.pdf", p.yeast.3x20,
width = 4, height = 4, dpi = 320)
ggsave("fig/yeast.boxplot.expression.1x60.pdf", p.yeast.1x60,
width = 4, height = 4, dpi = 320)
```
# Human {.tabset}
## Data loading
```{r human-load}
# MPRA data from Gosai et al. — keep IDs and K562 columns only
human.mpra <- read.table("human/reference/41586_2024_8070_MOESM4_ESM.txt",
header = TRUE, sep = "\t")
human.mpra <- human.mpra[, c("IDs", "K562_log2FC")] # IDs + K562-related columns
# Sequences
human.alpool <- load.sequences("human/sequences/pool.tsv")
human.alpool$al.method <- "AL pool"
human.tnb <- load.sequences("human/sequences/human_top_n_bottom_10.tsv")
human.tnb$al.method <- rename_tnb_labels(human.tnb$method)
# Split by pool size
human.tnb.3x20 <- human.tnb[grep("1x60", human.tnb$method, invert = TRUE), ]
human.tnb.1x60 <- human.tnb[grep("1x60", human.tnb$method), ]
```
## Merge with expression & assemble
```{r human-merge}
human.tnb.3x20.mpra <- merge(human.tnb.3x20, human.mpra, by = "IDs")
human.tnb.1x60.mpra <- merge(human.tnb.1x60, human.mpra, by = "IDs")
human.alpool.mpra <- merge(human.alpool, human.mpra, by = "IDs")
set.seed(42)
human.rnd.mpra <- human.alpool.mpra[sample(nrow(human.alpool.mpra), 100000), ]
human.rnd.mpra$al.method <- "Random selection"
# Rename value column for the shared plotting function
human.data.3x20 <- rbind(human.tnb.3x20.mpra, human.rnd.mpra, human.alpool.mpra)
human.data.3x20$value <- human.data.3x20$K562_log2FC
human.data.3x20$al.method <- factor(human.data.3x20$al.method, levels = LEVEL_ORDER)
human.data.1x60 <- rbind(human.tnb.1x60.mpra, human.rnd.mpra, human.alpool.mpra)
human.data.1x60$value <- human.data.1x60$K562_log2FC
human.data.1x60$al.method <- factor(human.data.1x60$al.method, levels = LEVEL_ORDER)
```
## Plots
```{r human-plots, fig.width=4, fig.height=4}
human.pval.3x20 <- mann_whitney_vs_pool(human.data.3x20)
human.pval.1x60 <- mann_whitney_vs_pool(human.data.1x60)
p.human.3x20 <- plot_expression_violin(
human.data.3x20, human.pval.3x20,
ylab.text = "log2FC(K562)", pval.y = 10
)
p.human.1x60 <- plot_expression_violin(
human.data.1x60, human.pval.1x60,
ylab.text = "log2FC(K562)", pval.y = 10
)
p.human.3x20
p.human.1x60
```
```{r human-save}
ggsave("fig/human.boxplot.expression.log2FCK562.3x20.pdf", p.human.3x20,
width = 4, height = 4, dpi = 320)
ggsave("fig/human.boxplot.expression.log2FCK562.1x60.pdf", p.human.1x60,
width = 4, height = 4, dpi = 320)
```
```{r session-info}
sessionInfo()
```