-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaf_trajectory_panel_plot.R
More file actions
79 lines (69 loc) · 2.03 KB
/
Copy pathaf_trajectory_panel_plot.R
File metadata and controls
79 lines (69 loc) · 2.03 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
#!/usr/bin/env Rscript
# Write stdout and stderr to log file
log <- file(snakemake@log[[1]], open = "wt")
sink(log, type = "message")
sink(log, type = "output")
set.seed(snakemake@params$random_color_seed) # seed for sampling colors
library(dplyr)
library(readr)
library(ggplot2)
library(glue)
library(logger)
library(logger)
log_threshold(INFO)
# Import file with plots style
source(snakemake@params[["design"]])
log_info("Reading formatted variants table")
variants <- read_tsv(snakemake@input$fmt_variants)
log_info("Reading subset of variants to represent")
selected.variants <- read_lines(snakemake@input$subset)
# Set plot height depending on the number of SNPs assuming 4 columns in the plot
plot.rows <- ceiling(
length(selected.variants) / snakemake@params$n_plot_columns
)
plot.height <- max(100, plot.rows * snakemake@params$plot_row_height_mm)
log_debug("Setting total plot height to {plot.height} mm with {plot.rows} rows")
log_info("Plotting {length(selected.variants)} SNPs allele frequency trajectories in time")
selected.colors <- sample(TRAJECTORY.PANEL.COLORS, length(selected.variants))
log_debug("Selected color: {selected.colors}")
p <- variants %>%
filter(VARIANT_NAME %in% selected.variants) %>%
mutate(
gPOS = paste0("g.", POS),
gPOS = reorder(gPOS, POS)
) %>%
ggplot() +
aes(
x = interval,
y = ALT_FREQ,
color = reorder(glue("{gPOS}\n{VARIANT_NAME}"), POS)
) +
scale_color_manual(values = selected.colors) +
geom_point() +
geom_line() +
theme(
legend.position = "bottom",
legend.text = element_text(size = 9),
legend.title = element_blank(),
legend.spacing.y = unit(3, "mm")
) +
labs(
x = "Days since first sample",
y = "Frequency"
) +
guides(color = guide_legend(ncol = 3))
if (length(selected.variants) > 1) {
p <- p +
facet_wrap(
vars(gPOS),
ncol = snakemake@params$n_plot_columns
)
}
ggsave(
filename = snakemake@output[["plot"]],
plot = p,
width = snakemake@params$plot_width_mm,
height = plot.height,
units = "mm",
dpi = 250
)