-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfig_genomes.Rmd
595 lines (526 loc) · 21.3 KB
/
fig_genomes.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
---
title: "Data munging and Figure 4 preparation"
author: "Shyam Saladi ([email protected])"
date: "09/20/2016"
output: html_document
---
## Load libraries
Load all libraries necessary for subsequent data munging and plotting.
Some defaults are set here to make things easier throughout.
```{r libraries}
library(testthat)
library(datamart) # `uconv` for unit conversion
# library(plyr) # for `dlply`
library(magrittr)
library(tidyverse)
library(multidplyr)
library(dplyrExtras)
library(RColorBrewer)
library(ggbeeswarm)
library(cowplot)
library(doMC)
# registerDoMC(cores = 20L)
# create_cluster(20L) %>%
# set_default_cluster()
# devtools::install("myutils")
library(myutils)
```
### Load data from training process
```{r}
training_env <- new.env()
load("training.RData", training_env)
```
### Load model data for prediction function
```{r}
model_env <- new.env()
load("model.RData", model_env)
svmpredict <- model_env$svmpredict
```
### Load NYCOMPS data for prediction function
```{r}
nycomps_env <- new.env()
load("large-scale.RData", nycomps_env)
```
# p4 - forward predictions
```{r forward_data_prep, cache=TRUE}
# load allstats and scores
feat_col_spec = cols(.default = col_double(), title = col_character())
forward_preds <-
tibble(fn = c("celegans_mp_wormbase",
"hs_rnd1",
"mm_rnd1",
"sgd_orf_coding.mps",
"Picpa1_GeneCatalog_CDS_20130227.mps"),
group = c(rep("Metazoa", 3), rep("Eukaryota", 2)),
species = c("Caenorhabditis elegans",
"Homo sapiens",
"Mus musculus",
"Saccharomyces cerevisiae",
"Pichia pastoris")) %>%
mutate(fn = paste0("forward-genomes/", fn, ".fna.allstats.csv.gz")) %>%
group_by(fn, group, species) %>%
do(read_csv(.$fn, col_types = feat_col_spec)) %>%
ungroup %>%
select(-fn)
forward_preds <- "forward-genomes/microbial_query.tsv.gz" %>%
read_delim(delim = "\t") %>%
bind_cols(read_csv("forward-genomes/microbial_query.fna.allstats.csv.gz",
col_types = feat_col_spec), .) %>%
rename(group = grouping) %>%
# Remove duplicate E. coli genome
filter(species != "Escherichia coli (strain ATCC 33849 / DSM 4235 / NCIB 12045 / K12 / DH1)") %>%
select(one_of(colnames(forward_preds))) %>%
bind_rows(forward_preds) %>%
filter(numTMs > 0) %>%
# format the scientific name
separate(species, c("genus", "species")) %>%
mutate(species = paste(genus, species)) %>%
select(-genus)
forward_preds$score <- svmpredict(forward_preds)
```
# prepare data for plotting
```{r}
my_boxplot <- function(x) {
# 1.5 corresponds to the criteria for the whiskers of a Tukey boxplot
out <- boxplot.stats(x, coef = 1.5, do.conf = TRUE, do.out = TRUE)$stats
names(out) <-
c("whisker_low", "quartile1", "median", "quartile3", "whisker_high")
out[['count']] <- length(x)
enframe(out)
}
forward_tufte_boxplots <- forward_preds %>%
group_by(group, species) %>%
do(my_boxplot(.$score)) %>%
spread(name, value) %>%
mutate(species_count = paste0(species, " (", count, ")") ) %>%
arrange(species == "Escherichia coli", group == "Metazoa", median) %>%
ungroup
forward_ec_thresholds <-
filter(forward_tufte_boxplots, species == "Escherichia coli") %>%
select(-group, -species, -count, -species_count) %>% unlist
forward_preds_order <- c(
"Vibrio cholerae",
"Helicobacter pylori",
"Campylobacter jejuni",
"Mycobacterium tuberculosis",
"Staphylococcus aureus",
"Plasmodium falciparum",
"Aquifex aeolicus",
"Thermus thermophilus",
"Spirochaeta thermophila",
"Gloeobacter violaceus",
"Rhizobium loti",
"Methanococcus maripaludis",
"Haloarcula marismortui",
"Bacillus halodurans",
"Lactobacillus casei",
"Bacillus cereus",
"Bacillus subtilis",
"Escherichia coli",
"Pichia pastoris",
"Saccharomyces cerevisiae",
"Caenorhabditis elegans",
"Mus musculus",
"Homo sapiens")
thermophile_species <- c("Aquifex aeolicus",
"Spirochaeta thermophila",
"Thermus thermophilus")
p4_all_violin <- ggplot(forward_preds) +
geom_hline(yintercept = forward_ec_thresholds['median'], color = "darkgrey") +
geom_point(aes(x = species, y = median),
color = "darkgrey", data = forward_tufte_boxplots) +
geom_text(aes(x = species, y = -6, label = species_count,
color = species %in% thermophile_species),
vjust = 2, fontface = "italic", size = 7*5/14,
data = forward_tufte_boxplots) +
geom_linerange(aes(x = species, ymin = whisker_low, ymax = quartile1),
color = "darkgrey", data = forward_tufte_boxplots) +
geom_linerange(aes(x = species, ymin = quartile3, ymax = whisker_high),
color = "darkgrey", data = forward_tufte_boxplots) +
geom_violin2(aes(y = score, x = species),
color = "black", adjust = 0.3, alpha = 0) +
coord_flip(ylim = nycomps_env$nycomps_xscale) +
ylab(expression("IMProve score")) +
scale_y_continuous(breaks = nycomps_env$nycomps_breaks) +
ggplot2::scale_x_discrete(limits = forward_preds_order) +
scale_color_manual(values = c("TRUE" = "#ff6600",
"FALSE" = "black")) +
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.line.y = element_blank())
p4_all_violin
```
```{r}
selected_violin_genomes <- c("Plasmodium falciparum",
"Aquifex aeolicus",
"Thermus thermophilus",
"Spirochaeta thermophila",
"Bacillus subtilis",
"Escherichia coli",
"Saccharomyces cerevisiae",
"Homo sapiens")
p4_selected_violin <- forward_preds %>%
filter(species %in% selected_violin_genomes) %>%
ggplot() +
geom_hline(yintercept = forward_ec_thresholds['median'],
linetype = "dashed", color = "darkgrey") +
geom_point(aes(x = species, y = median), color = "darkgrey",
data = forward_tufte_boxplots %>%
filter(species %in% selected_violin_genomes)) +
geom_text(aes(x = species, y = -6, label = species_count,
color = species %in% thermophile_species),
vjust = 2, fontface = "italic", size = 7*5/14,
data = forward_tufte_boxplots %>%
filter(species %in% selected_violin_genomes)) +
geom_linerange(aes(x = species, ymin = whisker_low, ymax = quartile1),
color = "darkgrey",
data = forward_tufte_boxplots %>%
filter(species %in% selected_violin_genomes)) +
geom_linerange(aes(x = species, ymin = quartile3, ymax = whisker_high),
color = "darkgrey",
data = forward_tufte_boxplots %>%
filter(species %in% selected_violin_genomes)) +
geom_violin2(aes(y = score, x = species),
color = "black", adjust = 0.3, alpha = 0) +
coord_flip(ylim = nycomps_env$nycomps_xscale) +
ylab(expression("SVM" ^ "rank"*~"score")) +
scale_color_manual(values = c("TRUE" = "#ff6600",
"FALSE" = "black")) +
ggplot2::scale_x_discrete(limits = selected_violin_genomes,
breaks = selected_violin_genomes) +
scale_y_continuous(breaks = nycomps_env$nycomps_breaks) +
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.line.y = element_blank())
p4_selected_violin
```
```{r fig4_nycomps_genes_ppv}
p4_nvector_ppv_prep <- nycomps_env$nycomps_gene_outcomes %>%
# filter(plasmid_name == "N") %>%
mutate(outcome = gene_outcome == "pos") %>%
calc_auc_roc(method = "roc") %>%
prep_for_polygon() %>%
as_tibble
p4_nvector_ppv <- ggplot(p4_nvector_ppv_prep) +
geom_polygon(aes(x = thresholds, y = ppv, group = id, fill = y_greater_min,
color = y_greater_min), size = 0.1) +
geom_path(aes(x = thresholds, y = ppv), size = 0.5,
data = filter(p4_nvector_ppv_prep, type != "min_y") %>%
distinct(thresholds, ppv)) +
geom_hline(aes(yintercept = min(min_ppv))) +
annotate("text", x = -1.7, y = 55, size = 7*5/14,
label = "Expression in (N) His-FLAG-TEV-", fontface = "bold") +
# coord_cartesian(xlim = nycomps_env$nycomps_xscale) +
scale_x_continuous(breaks = nycomps_env$nycomps_breaks) +
scale_y_continuous(limits = c(0, .601), breaks = seq(0, 1, .1),
labels = function(x) x*100) +
xlab(expression("IMProve score")) +
ylab("Positive Predictive Value") +
scale_color_manual(values = c("#A4A5A5", "#A10F1C")) +
scale_fill_manual(values = c("#A4A5A5", "#A10F1C"))
p4_nvector_ppv
```
## deal with feature distributions
```{r feature_distributions, cache = TRUE}
all_features <- forward_preds %>%
mutate(set = "forward") %>%
select(-score) %>%
gather(feature, value, -title, -group, -species, -set) %>%
bind_rows(training_env$daley_allstats %>%
rename(title = id) %>%
mutate(group = "Training",
species = "Ec-Daley Training",
set = "training") %>%
gather(feature, value, -title, -group, -species, -set)) %>%
bind_rows(nycomps_env$nycomps_features %>%
#rename() %>%
mutate(title = as.character(id),
group = "NYCOMPS",
species = "nycomps all",
set = "nycomps") %>%
select(-score, -score_nornass, -cterm, -name) %>%
gather(feature, value, -title, -group, -species, -set))
all_features %<>%
semi_join(all_features %>% filter(set == "forward"), by = "feature") %>%
semi_join(all_features %>% filter(set == "training"), by = "feature") %>%
# make sure that value is not NA
filter(!is.na(value)) %>%
group_by(feature)
# total count should be 89 - 5 (no RNAss from NUPACK)
test_that("correct number of distinct features", {
expect_equal(all_features %>%
filter(set == "training") %>%
distinct(feature) %>%
nrow, 84)
})
overlap_wrap <- function(.data, x, labelx, ref, labelref) {
data_frame(
labelx = labelx,
labelref = labelref,
overlap = estimate_overlap(
# can't figure out how to implement this with NSE
.data[x,] %$% unlist(value, use.names = FALSE),
.data[ref,] %$% unlist(value, use.names = FALSE))
)
}
# since the subsequent calculation is time consuming,
# split over multiple cores
all_features %<>%
partition(feature)
# load libraries on each core
myclusterEvalQ <- function(cl, ...) {
clusterEvalQ(cl, ...)
cl
}
get_default_cluster() %>%
cluster_assign_value("overlap_wrap", overlap_wrap) %>%
cluster_assign_value("thermophile_species", thermophile_species) %>%
myclusterEvalQ(library(magrittr)) %>%
myclusterEvalQ(library(tidyverse)) %>%
myclusterEvalQ(library(myutils))
overlap_df <- bind_rows(
all_features %>%
do(overlap_wrap(., .$set == "nycomps", "nycomps",
.$set == "training", "training")) %>% collect,
all_features %>%
do(overlap_wrap(., .$set == "forward", "forward",
.$set == "training", "training")) %>% collect,
all_features %>%
do(overlap_wrap(., .$species %in% thermophile_species, "thermo",
.$set == "training", "training")) %>% collect,
all_features %>%
do(overlap_wrap(., .$species == "Plasmodium falciparum", "pf",
.$set == "training", "training")) %>% collect,
all_features %>%
do(overlap_wrap(., .$species == "Escherichia coli", "ec_whole",
.$set == "training", "training")) %>% collect
) %>%
spread(labelx, overlap) %>%
ungroup
all_features %<>% collect
mad_calc <- function(x, y) {
abs(x - y) %>%
mean(na.rm = TRUE) * 100
}
# Mean absolute deviations:
mad_vs_nycomps <- overlap_df %>%
summarize(forward = mad_calc(nycomps, forward),
pf = mad_calc(nycomps, pf),
thermo = mad_calc(nycomps, thermo)) %>%
unlist
```
```{r}
p4_ecoli_dist <- ggplot(overlap_df) +
geom_vline(xintercept = 0.75,
size = 0.7, linetype = "dotted", color = "#00b1b9") +
geom_point(aes(x = ec_whole, y = 1), size = 0.25,
position = position_quasirandom()) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 1),
labels = function(x) x*100) +
xlab("E. coli vs. E. coli (Training Set)") +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank())
p4_ecoli_dist %<>% ggplotGrob
p4_ecoli_dist$layout %<>%
mutate_rows(name == "panel", clip = "off")
ggdraw(p4_ecoli_dist)
```
```{r}
pext2_thermo <-
ggplot(overlap_df, aes(x = nycomps, y = thermo, label = feature)) +
geom_hline(yintercept = 0.75,
linetype = "dotted", size = 0.7, color = "#00b1b9") +
geom_vline(xintercept = 0.75,
linetype = "dotted", size = 0.7, color = "#00b1b9") +
geom_abline(color = "grey", linetype = "dashed") +
geom_point(size = 0.25) +
annotate(geom = "text",
label = paste0("MAD=", specify_decimal(mad_vs_nycomps['thermo'])),
x = 0.25, y = 0.9, size = 6*5/14) +
xlab("NYCOMPS vs. Training") +
ylab("Thermophiles vs. Training") +
scale_x_continuous(limits = c(0,1), expand = c(0,0),
labels = function(x) x*100) +
scale_y_continuous(limits = c(0,1), expand = c(0,0),
labels = function(x) x*100) +
theme(legend.position = "none",
legend.title = element_blank(),
aspect.ratio = 1)
pext2_thermo
```
```{r}
pext2_pf <- ggplot(overlap_df, aes(x = nycomps, y = pf, label = feature)) +
geom_hline(yintercept = 0.75,
linetype = "dotted", size = 0.7, color = "#00b1b9") +
geom_vline(xintercept = 0.75,
linetype = "dotted", size = 0.7, color = "#00b1b9") +
geom_abline(color = "grey", linetype = "dashed") +
geom_point(size = 0.25) +
annotate(geom = "text",
label = paste0("MAD=", specify_decimal(mad_vs_nycomps['pf'])),
x = 0.25, y = 0.9, size = 6*5/14) +
xlab("NYCOMPS vs. Training") +
ylab("P. falciparum vs. Training") +
scale_color_manual(values = brewer.pal(3, "Dark2")[2:1]) +
scale_x_continuous(limits = c(0,1), expand = c(0,0),
labels = function(x) x*100) +
scale_y_continuous(limits = c(0,1), expand = c(0,0),
labels = function(x) x*100) +
theme(legend.position = "none",
legend.title = element_blank(),
aspect.ratio = 1)
pext2_pf
```
```{r}
pext2_nycomps_forward <-
ggplot(overlap_df, aes(x = nycomps, y = forward, label = feature)) +
geom_hline(yintercept = 0.75,
linetype = "dotted", size = 0.7, color = "#00b1b9") +
geom_vline(xintercept = 0.75,
linetype = "dotted", size = 0.7, color = "#00b1b9") +
geom_abline(color = "grey", linetype = "dashed") +
geom_point(size = 0.25) +
annotate(geom = "text",
label = paste0("MAD=", specify_decimal(mad_vs_nycomps['forward'])),
x = 0.25, y = 0.9, size = 6*5/14) +
xlab("NYCOMPS vs. Training") +
ylab("Forward vs. Training") +
scale_x_continuous(limits = c(0,1), expand = c(0,0),
labels = function(x) x*100) +
scale_y_continuous(limits = c(0,1), expand = c(0,0),
labels = function(x) x*100) +
theme(aspect.ratio = 1)
pext2_nycomps_forward
```
Need to split up plotting since we want to have several on log axes
```{r}
selected <-
c(seqLen = "Sequence Length",
pI = "Isoelectric Point",
GC = "GC Content",
membrCont = "Membrane Residue Count",
tAI = "tRNA Adapation Index",
numPosNormCyt = "Positive Cytoplasmic Residues (normalized)",
CPS = "Codon Pair Score",
delGallTMs = "ΔG of Insertion (Hessa, et al.)",
relareaSD = "Shine-Dalgarno Sites (normalized)",
len1_2loop = "Length of TM1-TM2 Loop",
zeroto38avgRNAss = "RNA Secondary Structure (1st 40 codons)",
avgRONN = "Mean Disorder Prediction (RONN)")
log_scale <- c("seqLen", "membrCont", "len1_2loop")
make_indiv_plot <- function(df) {
library(tidyverse)
library(cowplot)
library(myutils)
feat <- unique(df$feature)
p_hist <- ggplot(df) +
geom_step_hist(aes(x = value, color = group),
alpha = 0.7,
position = "identity") +
xlab(selected[[feat]]) +
scale_color_manual(
values = c("training" = brewer.pal(3L, "Dark2")[[1L]],
"thermo" = "#ff6600",
"pf" = brewer.pal(3L, "Dark2")[[3L]])) +
scale_y_continuous(expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
theme(axis.title.y = element_blank(),
legend.position = "None",
plot.margin = unit(c(5, 5, 0, 0), "pt"))
# adjustments if log scale is desired
if (feat %in% log_scale) {
p_hist <- p_hist +
scale_x_log10(
expand = c(0, 0),
labels = scales::trans_format(
"log10", scales::math_format(10 ^ .x))) +
annotation_logticks2(sides = "b",
short = unit(-0.4, "mm"),
mid = unit(-0.4, "mm"),
long = unit(-0.6, "mm"),
size = 0.2)
p_hist %<>% ggplotGrob
p_hist$layout %<>%
mutate_rows(name == "panel", clip = "off")
}
# add labels in ggdraw coordinates
ggdraw(p_hist) +
draw_label(x = 0.85, y = 0.9,
label = overlap_df %>%
filter_("feature == '" %>% paste0(feat, "'")) %>%
select(thermo) %>% unlist(use.names = FALSE) %>%
specify_decimal(2L),
size = 5, colour = "#ff6600") +
draw_label(x = 0.85, y = 0.8,
label = overlap_df %>%
filter_("feature == '" %>% paste0(feat, "'")) %>%
select(pf) %>% unlist(use.names = FALSE) %>%
specify_decimal(2L),
size = 5, colour = brewer.pal(3L, "Dark2")[[3L]])
}
setdiff(names(selected), names(all_features))
p4_feat_hist <- all_features %>%
# necessary to filter for certain features of interest
ungroup %>%
filter(set == "training" |
species %in% thermophile_species |
species == "Plasmodium falciparum",
feature %in% names(selected)) %>%
mutate_rows(set == "training", group = "training") %>%
mutate_rows(species %in% thermophile_species, group = "thermo") %>%
mutate_rows(species == "Plasmodium falciparum", group = "pf") %>%
# to control plot order
mutate(feature = factor(feature, names(selected))) %>%
plyr::dlply(.(feature), make_indiv_plot, .parallel = TRUE) %>%
# render final plot
plot_grid(plotlist = ., align = "hv", ncol = 2L)
p4_feat_hist
```
```{r}
pext2 <- ggdraw() +
draw_plot(p4_all_violin, width = 0.65) +
draw_label("a", x = 0.01, y = .99, fontface = "bold", size = 8) +
draw_plot(p4_feat_hist, x = 0.67, width = 0.33, height = 0.97) +
draw_label("b", x = .66, y = .99, fontface = "bold", size = 8) +
draw_label("E. coli", x = .73, y = .98, fontface = "bold.italic",
size = 7, colour = brewer.pal(3L, "Dark2")[[1L]]) +
draw_label("Thermophiles", x = .83, y = .98, fontface = "bold",
size = 7, colour = "#ff6600") +
draw_label("P. falciparum", x = .93, y = .98, fontface = "bold.italic",
size = 7, colour = brewer.pal(3L, "Dark2")[[3L]]) +
draw_label("Counts", x = .655, y = .5,
fontface = "bold", size = 7, angle = 90)
# RGB by default
save_plot(filename = "plots/extfig2_names.pdf",
plot = pext2,
base_width = uconv(183, "mm", "in", "Length"), base_height = 7,
dpi = 300)
pext2
```
## Figure 4 Compilation
```{r}
p4_main <- ggdraw() +
draw_plot(plot_grid(p4_selected_violin, p4_nvector_ppv,
nrow = 2L, align = "hv", rel_heights = c(3, 1),
labels = letters[1:2], label_size = 8),
x = -.01, y = 0, width = .72, height = 1) +
draw_plot(plot_grid(p4_ecoli_dist, pext2_nycomps_forward,
pext2_thermo, pext2_pf,
labels = letters[3:6], label_size = 8,
ncol = 1L, align = "hv",
rel_heights = c(2, 3, 3, 3)),
x = .72, y = 0, width = .27, height = 1)
# RGB by default
save_plot(filename = "plots/fig4_fluman_thermo.pdf", plot = p4_main,
base_width = uconv(136, "mm", "in", "Length"),
base_height = 5.5)
p4_main
```
```{r session}
save(forward_preds, file = "genomes.RData")
sessionInfo()
```