Skip to content

Commit 8025462

Browse files
authored
fix: catch errors on AF-time correlation test (#39)
* fix: write file as tab-separated file for coherence * refactor: add horizontal space in trees * fix: handle variants with fewer than necessary time points * fix: add back snakemake option reverts bug introduced in 8bc79b0 * refactor: update af-time correlation data logging * refactor: update logging
1 parent ce47836 commit 8025462

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

workflow/scripts/report/af_time_correlation_data.R

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,24 @@ variants <- left_join(variants, metadata, by = c("SAMPLE" = "ID")) %>%
4848
CollectionDate
4949
) %>%
5050
mutate(
51-
interval = as.numeric(difftime(CollectionDate, min(CollectionDate), units = "days"))
51+
interval = as.numeric(difftime(
52+
CollectionDate,
53+
min(CollectionDate),
54+
units = "days"
55+
))
5256
)
5357

5458
# Save processed input
5559
log_info("Writing dated and frequency-filled variants")
56-
write_csv(variants, snakemake@output$fmt_variants)
60+
write_tsv(variants, snakemake@output$fmt_variants)
5761

5862
log_info("Calculating correlations")
5963
log_debug("Calculating unique SNPs")
6064
# Get list with all different polymorphisms
6165
unique.snps <- unique(variants$VARIANT_NAME)
6266

6367
# Create an empty dataframe to be filled
68+
log_debug("Initializing empty results")
6469
cor.df <- data.frame(
6570
variant = "",
6671
min_af = 0,
@@ -71,21 +76,30 @@ cor.df <- data.frame(
7176
) %>%
7277
filter(p.value != 0)
7378

74-
log_debug("Calculating correlation using method = {snakemake@params$cor_method} and exact p-value = {snakemake@params$cor_exact}")
79+
log_info("Calculating correlation with method={snakemake@params$cor_method} and exact={snakemake@params$cor_exact}")
7580
correlations <- lapply(
7681
unique.snps,
7782
function(snp) {
83+
log_debug("Processing {snp}")
7884
# Select SNP
7985
df <- filter(
8086
variants,
8187
VARIANT_NAME == snp
8288
)
8389
# Perform calculation
84-
test <- cor.test(
85-
df$ALT_FREQ,
86-
df$interval,
87-
method = snakemake@params$cor_method,
88-
exact = snakemake@params$cor_exact
90+
test <- tryCatch(
91+
{
92+
cor.test(
93+
df$ALT_FREQ,
94+
df$interval,
95+
method = snakemake@params$cor_method,
96+
exact = snakemake@params$cor_exact
97+
)
98+
},
99+
error = function(e) {
100+
log_warn("Cannot run cor.test for {snp}: {e}")
101+
list(p.value = NA, estimate = NA)
102+
}
89103
)
90104
# Adjust p-value
91105
p.value.adj <- p.adjust(
@@ -122,8 +136,7 @@ significant.variants <- correlations %>%
122136
) %>%
123137
pull(variant) %>%
124138
unique()
125-
126-
log_info("Significant: {significant.variants}")
139+
log_debug("Significant: {significant.variants}")
127140

128141
log_info("Selecting variants in positions with more than one alternative allele")
129142
mult.alt.variants <- variants %>%
@@ -137,13 +150,11 @@ mult.alt.variants <- variants %>%
137150
ungroup() %>%
138151
pull(VARIANT_NAME) %>%
139152
unique()
140-
141-
log_info("Mult all: {mult.alt.variants}")
153+
log_debug("Mult all: {mult.alt.variants}")
142154

143155
# Build selected subset to represent
144156
variant.selection <- unique(c(significant.variants, mult.alt.variants))
145-
146-
log_info("Selection: {variant.selection}")
157+
log_debug("Selection: {variant.selection}")
147158

148159
log_info("Writing selected variants subset")
149160
write_lines(variant.selection, snakemake@output$subset)

workflow/scripts/report/af_trajectory_panel_plot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ log_threshold(INFO)
1919
source(snakemake@params[["design"]])
2020

2121
log_info("Reading formatted variants table")
22-
variants <- read_csv(snakemake@input$fmt_variants)
22+
variants <- read_tsv(snakemake@input$fmt_variants)
2323

2424
log_info("Reading subset of variants to represent")
2525
selected.variants <- read_lines(snakemake@input$subset)

workflow/scripts/report/allele_freq_tree_plot.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ max.tip.length <- max(
7777
p <- ggtree(tree) %<+% tree_tiplab +
7878
geom_tiplab(aes(label = tip_label)) +
7979
geom_treescale(1.1 * max.tip.length) +
80-
geom_rootedge(0.05 * max.tip.length)
80+
geom_rootedge(0.05 * max.tip.length) +
81+
hexpand(0.2)
8182

8283
ggsave(
8384
filename = snakemake@output$plot,

0 commit comments

Comments
 (0)