45 Supplementary Figure 18
45.1 Summary
This is the accessory documentation of Figure S18.
The Figure can be recreated by running the R script plot_SF18.R
:
cd $BASE_DIR
Rscript --vanilla R/fig/plot_SF18.R \
2_analysis/summaries/fst_outliers_998.tsv
45.2 Details of plot_SF18.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, as well as on the packages hypoimg, hypogen and patchwork
45.2.1 Config
The scripts start with a header that contains copy & paste templates to execute or debug the script:
#!/usr/bin/env Rscript
# run from terminal:
# Rscript --vanilla R/fig/plot_SF18.R \
# 2_analysis/summaries/fst_outliers_998.tsv
# ===============================================================
# This script produces Suppl. Figure 18 of the study "Rapid radiation in a
# highly diverse marine environment" by Hench, Helmkampf, McMillan and Puebla
# ---------------------------------------------------------------
# ===============================================================
# args <- c("2_analysis/summaries/fst_outliers_998.tsv")
# script_name <- "R/fig/plot_SF18.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(tidygraph)
library(ggraph)
library(prismatic)
library(patchwork)
library(IRanges)
library(plyranges)
library(hypogen)
library(hypoimg)
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_SF18.R ────────────────────────────────────────────
#> Parameters read:
#> ★ 1: 2_analysis/summaries/fst_outliers_998.tsv
#> ────────────────────────────────────────── /current/working/directory ──
The directory containing the PCA data is received and stored in a variable. Also the default color scheme is updated and the size of the hamlet ann.
# config -----------------------
<- as.character(args[1])
outlier_file <- read_tsv(outlier_file) outlier_regions
<- 100 / (166 * 165)
hap_to_perc <- c(str_c("2/5*10^",6:3," BP"),"25/10 kb","10/5 kb","7-5/3 kb","15/7.5 kb")
iterations <- c(str_c("10-",6:3),"7","8","9","10") itteration_names
<- function(n_zeros, y_lim = c(0, 4), filtmode = "direct", y_lab = ""){
plot_ibd_gw <- vroom::vroom(glue::glue("2_analysis/ibd/no_outgr_{filtmode}_{itteration_names[n_zeros]}.segments.tsv"),
data_seg delim = "\t", col_types = "cccciidcdci") %>%
left_join(hypogen::hypo_chrom_start) %>%
mutate(start = POS * 10^6,
end = start + (LENGTH * 10^6),
gstart = GSTART + start,
gend = start + (LENGTH * 10^6),
ibd_hplo = str_remove(TYPE,"IBD") %>%
as.integer())
%>%
data_seg ::select(seqnames = CHROM,start,end,gstart,GSTART,TYPE,ibd_hplo,ID1,ID2) %>%
dplyrarrange(gstart) %>%
::select(-gstart) %>%
dplyras_granges() %>%
::coverage(weight = "ibd_hplo") %>%
GenomicRanges::as_ranges() %>%
plyrangesas_tibble() %>%
::select(CHROM = seqnames, start, end, width, score) %>%
dplyrleft_join(hypogen::hypo_chrom_start) %>%
mutate(gstart = GSTART + start, gend = GSTART + end) %>%
::select(CHROM, gstart, gend, score) %>%
dplyrpivot_longer(gstart:gend,values_to = "GPOS", names_to = "PART") %>%
ggplot() +
geom_hypo_LG() +
geom_vline(data = outlier_regions, aes(xintercept = gpos), color = rgb(1,0,0,.2), size = .3) +
geom_step(aes(x = GPOS, y = score * hap_to_perc, group = CHROM), color = rgb(.3,.3,.3), size = .3) +
geom_ribbon(aes(x = GPOS, ymin = 0, ymax = score * hap_to_perc, group = CHROM), fill = rgb(0,0,0,.5)) +
scale_hypobg_manual(values = c("transparent",rgb(.9,.9,.9,.9),"red","blue") %>%
set_names(nm = c("even", "odd", "a","b")), guide = "none")+
scale_x_hypo_LG() +
labs(y = str_c("IBD Score (" , iterations[n_zeros], ")")) +
coord_cartesian(ylim = y_lim, expand = 0) +
theme_hypo()
}
<- vroom::vroom(glue::glue("2_analysis/ibd/no_outgr_direct_10.segments.tsv"),
data_seg delim = "\t", col_types = "cccciidcdci") %>%
left_join(hypogen::hypo_chrom_start) %>%
mutate(start = POS * 10^6,
end = start + (LENGTH * 10^6),
gstart = GSTART + start,
gend = start + (LENGTH * 10^6),
ibd_hplo = str_remove(TYPE,"IBD") %>%
as.integer())
<- data_seg %>%
data_compact ::select(seqnames = CHROM,start,end,gstart,GSTART,TYPE,ibd_hplo,ID1,ID2) %>%
dplyrarrange(gstart) %>%
::select(-gstart) %>%
dplyras_granges() %>%
::coverage(weight = "ibd_hplo") %>%
GenomicRanges::as_ranges() %>%
plyrangesas_tibble() %>%
::select(CHROM = seqnames, start, end, width, score) %>%
dplyrleft_join(hypogen::hypo_chrom_start) %>%
mutate(gstart = GSTART + start, gend = GSTART + end) %>%
::select(CHROM, gstart, gend, score) dplyr
<- data_compact %>%
total_cov_lenght mutate(length = gend-gstart) %>% .$length %>% sum()
<- data_compact %>%
data_sorted mutate(length = gend-gstart) %>%
group_by(score) %>%
summarise(length = sum(length)) %>%
ungroup() %>%
mutate(length = if_else(score == 0,
+ hypo_karyotype$GEND[24]-total_cov_lenght, # attach uncovered chrom ends
length
length),gend = cumsum(length),
gstart = lag(gend,default = 0))
<- .95
perc_cutoff
<- data_sorted %>%
perc_score filter(gstart < hypo_karyotype$GEND[24] * perc_cutoff,
> hypo_karyotype$GEND[24] * perc_cutoff) %>%
gend $score .
<- c(5,8,6) %>%
plts map2(.y = list(c(0,26), # .55),
c(0,26), # 4),
c(0,26)),
filtmode = "direct") plot_ibd_gw,
<- (plts[[1]] +
p_done 2]] + geom_hline(yintercept = perc_score * hap_to_perc, color = "#11C269", size = .3, alpha = .7) +
plts[[3]] +
plts[[plot_layout(ncol = 1)) +
plot_annotation(tag_levels = "a") &
theme(text = element_text(size = plot_text_size),
plot.tag.position = c(0, 1),
legend.position = "bottom",
legend.key = element_blank(),
legend.direction = "horizontal",
legend.background = element_blank(),
legend.box = "horizontal",
legend.text.align = 0)
Finally, we can export Figure S18.
hypo_save(plot = p_done,
filename = "figures/SF18.png",
width = f_width,
height = .55 * f_width,
dpi = 600,
type = "cairo",
bg = "transparent",
comment = plot_comment)
system("convert figures/SF18.png figures/SF18.pdf")
system("rm figures/SF18.png")
<- str_c("exiftool -overwrite_original -Description=\"", plot_comment, "\" figures/SF18.pdf")
create_metadata system(create_metadata)