25 Figure 4
25.1 Summary
This is the accessory documentation of Figure 4.
The Figure can be recreated by running the R script plot_F4.R
from a (bash
terminal):
cd $BASE_DIR
Rscript --vanilla R/fig/plot_F4.R \
\
2_analysis/astral/astral_5000x_5kb_v1_noS.tre 2_analysis/ibd/cM_converted/no_outgr_bed95_8.conv_filterd.tsv
25.2 Details of plot_F4.R
In the following, the individual steps of the R script are documented. It is an executable R script that depends on the accessory R package GenomicOriginsScripts, BAMMtools and on the package hypoimg.
25.2.1 Config
The scripts start with a header that contains copy & paste templates to execute interactively or debug the script:
#!/usr/bin/env Rscript
# run from terminal:
# Rscript --vanilla R/fig/plot_F4.R \
# 2_analysis/astral/astral_5000x_5kb_v1_noS.tre \
# 2_analysis/ibd/cM_converted/no_outgr_bed95_8.conv_filterd.tsv
# ===============================================================
# This script produces Figure 4 of the study "Rapid radiation in a highly
# diverse marine environment" by Hench, Helmkampf, McMillan and Puebla
# ---------------------------------------------------------------
# ===============================================================
# args <- c("2_analysis/astral/astral_5000x_5kb_v1_noS.tre",
# "2_analysis/ibd/cM_converted/no_outgr_bed95_8.conv_filterd.tsv")
# script_name <- "R/fig/plot_F4.R"
<- commandArgs(trailingOnly = FALSE) args
The next section processes the input from the command line.
It stores the arguments in the vector args
.
The needed R packages are loaded and the script name and the current working directory are stored inside variables (script_name
, plot_comment
).
This information will later be written into the meta data of the figure to help us tracing back the scripts that created the figures in the future.
Then we drop all the imported information besides the arguments following the script name and print the information to the terminal.
# setup -----------------------
::activate()
renvlibrary(GenomicOriginsScripts)
library(hypoimg)
library(hypogen)
library(ape)
library(ggtree)
library(tidygraph)
library(ggraph)
library(patchwork)
cat('\n')
<- args[5] %>%
script_name str_remove(., '--file=')
<- script_name %>%
plot_comment str_c('mother-script = ', getwd(), '/', .)
<- process_input(script_name, args) args
#> ── Script: R/fig/plot_F4.R ──────────────────────────────────────────────
#> Parameters read:
#> ★ 1: 2_analysis/astral/astral_5000x_5kb_v1_noS.tre
#> ★ 2: 2_analysis/ibd/cM_converted/no_outgr_bed95_8.conv_filterd.tsv
#> ─────────────────────────────────────────── /current/working/directory ──
The directories for the different data types are received and stored in respective variables. Also, we set a few parameters for the plot layout:
# config -----------------------
<- as.character(args[1])
tree_hypo_file <- as.character(args[2]) ibd_file
25.2.2 Actual Script Start
<- read.tree(tree_hypo_file)
tree $edge.length <- replace(tree$edge.length, tree$edge.length == "NaN", 0.05) # Set terminal branches to 0.05 tree
<- root(phy = tree, outgroup = "PL17_160floflo")
tree_rooted <- rgb(.2, .2, .2) clr_neutral
### Prepare tree and categorize support values
<- ggtree(tree_rooted) %>%
tree_plus ::rotate(node = 205) %>%
ggtree$data %>%
.mutate(spec = ifelse(isTip, str_sub(label, -6, -4), "ungrouped"),
loc = ifelse(isTip, str_sub(label, -3, -1), "ungrouped"),
support = as.numeric(label) * 100,
support_class = cut(support, c(0,50,70,90,100)) %>%
as.character() %>% factor(levels = c("(0,50]", "(50,70]", "(70,90]", "(90,100]"))
)
<- (ggtree(tree_plus, layout = 'fan', aes(color = spec), size = .2) %>%
t_plot ::rotate_tree(angle = -100)) +
ggtreegeom_tippoint(aes(color = spec,
shape = loc,
fill = after_scale(color)), size = .5) +
geom_nodepoint(data = tree_plus %>% filter(!isTip, support_class != "(0,50]"), # Apply to nodes with support >50 only
aes(fill = support_class,
size = support_class),
shape = 21,
color = clr_neutral) +
scale_color_manual(values = c(GenomicOriginsScripts::clr2, ungrouped = "gray60"),
guide = 'none') +
scale_shape_manual(values = c(bel = 21, flo = 24, hon = 22, pan = 23), labels = GenomicOriginsScripts::loc_names,
guide = 'none') +
scale_fill_manual(values = c(`(0,50]` = "transparent",
`(50,70]` = "white",
`(70,90]` = "gray",
`(90,100]` = "black"),
drop = FALSE) +
scale_size_manual(values = c(`(0,50]` = 0,
`(50,70]` = .8,
`(70,90]` = .8,
`(90,100]` = .8),
na.value = 0,
drop = FALSE) +
# Add scale bar:
::geom_treescale(width = .05,
ggtreex = .13, y = 130,
offset = -1,
linesize = .2,
fontsize = plot_text_size/.pt,
color = clr_neutral) +
scale_x_continuous(limits = c(-.05, .26), expand = c(0,0)) +
guides(fill = guide_legend(title = "Node Support Class", title.position = "top",
nrow = 2),
size = guide_legend(title = "Node Support Class", title.position = "top",
nrow = 2)) +
theme_void() +
theme(legend.title.align = 0.5,
legend.text = element_text(color = "gray20"),
legend.title = element_text(color = "gray20"))
<- .55
y_sep <- .5 x_shift
<- ggplot() +
p1 coord_equal(xlim = c(0, 1),
ylim = c(0, 1),
expand = 0) +
annotation_custom(grob = ggplotGrob(t_plot + theme(legend.position = "none")),
ymin = -.15 - y_sep ,
ymax = 1 + y_sep,
xmin = .25 - x_shift,
xmax = 1 + x_shift) +
theme_void()
<- read_tsv(ibd_file) %>%
data_ibd mutate(ibd_total = (ibd2_cM_m1 + 0.5*ibd1_cM_m1) / (ibd0_cM_m1 + ibd1_cM_m1 + ibd2_cM_m1))
set.seed(42)
<- data_ibd %>%
p2 as_tbl_graph() %>%
mutate(spec = factor(str_sub(name, -6, -4), levels = names(clr)[-10:-9]),
loc = factor(str_sub(name, -3, -1), levels = c("bel", "hon", "pan", "flo"))) %>%
ggraph( layout = 'fr', weights = ibd_total) +
geom_edge_link(aes(alpha = ibd_total, edge_width = ibd_total), color = rgb(.1,.1,.1)) +
geom_node_point(aes(fill = spec,
shape = loc, color = after_scale(clr_darken(fill,.3))), size = 1.2) +
scale_fill_manual("Species", values = GenomicOriginsScripts::clr2[!(names(GenomicOriginsScripts::clr2) %in% c( "tor", "tab"))],
labels = GenomicOriginsScripts::sp_labs, drop = FALSE)+
scale_edge_alpha_continuous(range = c(0, 1), guide = "none") +
scale_edge_width_continuous(range = c(.1, .4), guide = "none") +
scale_shape_manual("Site", values = c(bel = 21, flo = 24, hon = 22, pan = 23),
labels = GenomicOriginsScripts::loc_names, drop = FALSE) +
guides(fill = guide_legend(nrow = 2, override.aes = list(shape = 21, size = 2.5), title.position = "top",label.hjust = 0),
shape = guide_legend(nrow = 2, title.position = "top")) +
coord_fixed() +
theme(text = element_text(size = GenomicOriginsScripts::plot_text_size),
panel.background = element_blank())
<- cowplot::plot_grid(cowplot::plot_grid(p1, p2 + theme(legend.position = "none"), rel_widths = c(1,.9),
p_done labels = c("a", "b"), label_fontface = "plain", label_size = plot_text_size),
::plot_grid(cowplot::get_legend(t_plot +
cowplottheme_minimal(base_size = GenomicOriginsScripts::plot_text_size) +
theme(legend.key.width = unit(3,"pt"))),
::get_legend(p2 +
cowplottheme_minimal(base_size = GenomicOriginsScripts::plot_text_size) +
theme(legend.position = "bottom",
legend.key.width = unit(3,"pt"))),
rel_widths = c(.3,1)),
rel_heights = c(1,.15),
ncol = 1)
Finally, we can export Figure 4.
hypo_save(plot = p_done,
filename = "figures/F4.png",
width = GenomicOriginsScripts::f_width,
height = GenomicOriginsScripts::f_width * .65,
bg = "white",
type = "cairo",
dpi = 600,
comment = plot_comment)
system("convert figures/F4.png figures/F4.pdf")
system("rm figures/F4.png")
<- str_c("exiftool -overwrite_original -Description=\"", plot_comment, "\" figures/F4.pdf")
create_metadata system(create_metadata)