Skip to content

Commit 8daeea0

Browse files
committed
Decouple of polymorphic sites over time plot from data
1 parent 4917f21 commit 8daeea0

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env Rscript
2+
3+
# Write stdout and stderr to log file
4+
log <- file(snakemake@log[[1]], open = "wt")
5+
sink(log, type = "message")
6+
sink(log, type = "output")
7+
8+
library(tidyverse)
9+
library(jsonlite)
10+
library(logger)
11+
log_threshold(INFO)
12+
13+
log_info("Reading variants")
14+
variants <- read_delim(snakemake@input$variants)
15+
16+
log_info("Reading metadata")
17+
metadata <- read_delim(snakemake@input$metadata)
18+
19+
log_info("Calculating heterozygous sites")
20+
sites <- variants %>%
21+
filter(ALT_FREQ <= snakemake@params$max_alt_freq) %>%
22+
left_join(
23+
metadata,
24+
by = c("SAMPLE" = "ID")
25+
) %>%
26+
group_by(SAMPLE) %>%
27+
summarise(
28+
CollectionDate = min(as.Date(CollectionDate)),
29+
n = n_distinct(POS)
30+
) %>%
31+
ungroup() %>%
32+
arrange(CollectionDate) %>%
33+
mutate(
34+
Day = as.numeric(
35+
difftime(CollectionDate, min(CollectionDate), units = "days")
36+
)
37+
)
38+
39+
if (nrow(sites) == 0) {
40+
log_warn("There are none, using an empty table and no linear regression")
41+
sites <- tibble(
42+
SAMPLE = date_order,
43+
REGION = as.character(NA),
44+
VARIANT_NAME = as.character(NA),
45+
ALT_FREQ = as.numeric(NA),
46+
EFFECT = as.character(NA),
47+
SYNONYMOUS = as.character(NA),
48+
POS = as.numeric(NA),
49+
ALT = as.character(NA),
50+
NV_class = as.character(NA),
51+
group = as.character(NA)
52+
)
53+
r_squared <- "none"
54+
p_value_string <- "none"
55+
} else if (nrow(sites) > 2) {
56+
log_info("Calculating linear regression")
57+
model <- lm(n ~ CollectionDate, data = sites)
58+
r_squared <- summary(model)$r.squared[[1]]
59+
p_value <- summary(model)$coefficients[2, 4]
60+
p_value_string <- ifelse(p_value < 0.001, "< 0.001", p_value)
61+
} else {
62+
log_warn("Not enough data points for a linear regression")
63+
r_squared <- "none"
64+
p_value_string <- "none"
65+
}
66+
67+
log_info("Writing JSON summary")
68+
list(
69+
"r2" = r_squared,
70+
"value" = p_value_string
71+
) %>%
72+
write_json(
73+
snakemake@output$json,
74+
auto_unbox = TRUE,
75+
digits = NA
76+
)
77+
78+
log_info("Writing processed table")
79+
write_csv(sites, snakemake@output$table)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env Rscript
2+
3+
# Write stdout and stderr to log file
4+
log <- file(snakemake@log[[1]], open = "wt")
5+
sink(log, type = "message")
6+
sink(log, type = "output")
7+
8+
library(tidyverse)
9+
library(logger)
10+
log_threshold(INFO)
11+
12+
# Import file with plots style
13+
source(snakemake@params[["design"]])
14+
15+
log_info("Reading plot data")
16+
df <- read_csv(snakemake@input$table)
17+
18+
log_info("Plotting")
19+
p <- df %>%
20+
ggplot() +
21+
aes(x = Day, y = n) +
22+
geom_smooth(
23+
method = "lm",
24+
fill = "gray95",
25+
alpha = 0.6,
26+
colour = "orange"
27+
) +
28+
geom_point(size = 1, shape = 1) +
29+
labs(
30+
x = "Days since the initial sampling",
31+
y = "No. of polimorphic sites"
32+
)
33+
34+
log_info("Saving plot")
35+
ggsave(
36+
filename = snakemake@output$plot,
37+
plot = p,
38+
width = 250,
39+
height = 119.4,
40+
units = "mm",
41+
dpi = 250
42+
)

0 commit comments

Comments
 (0)