Skip to content

Commit 6dfb966

Browse files
committed
refactor: calculate AF correlation matrix before rendering report
1 parent 02a78e4 commit 6dfb966

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

template.qmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ library(tidyr)
6565
library(dplyr)
6666
library(heatmaply)
6767
library(readr)
68+
library(tibble)
6869
6970
# Diversity
7071
div_values <- fromJSON(params$div_value)
@@ -89,10 +90,9 @@ table <- read.csv(params$table)
8990
n.samples <- table %>% pull(Sample) %>% unique() %>% length()
9091
9192
# Heatmap
92-
heatmap.df <- read.csv(params$heat_tab)
93-
row.names(heatmap.df) <- heatmap.df$`...1`
94-
heatmap.df[1] <- NULL
95-
cor.mat <- cor(heatmap.df, method = params$cor_method, use = "pairwise.complete.obs")
93+
cor.mat <- read_csv(params$heat_tab) %>%
94+
column_to_rownames("...1") %>%
95+
as.matrix()
9696
if (params$cor_method == "pearson") {
9797
cor.method.name <- "Pearson's"
9898
} else if (params$cor_method == "spearman") {

workflow/rules/report.smk

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,19 @@ rule af_trajectory_panel_plot:
394394

395395
rule pairwise_trajectory_correlation_data:
396396
conda: "../envs/renv.yaml"
397+
params:
398+
cor_method = config["COR"]["METHOD"],
399+
cor_use = "pairwise.complete.obs",
397400
input:
398401
variants = OUTDIR/f"{OUTPUT_NAME}.variants.all_sites.tsv",
399402
metadata = config["METADATA"],
400403
output:
401-
table = report(REPORT_DIR_TABLES/"pairwise_trajectory_correlation.csv"),
404+
table = REPORT_DIR_TABLES/"pairwise_trajectory_frequency_data.csv",
405+
matrix = report(REPORT_DIR_TABLES/"pairwise_trajectory_correlation_matrix.csv"),
402406
log:
403407
LOGDIR / "pairwise_trajectory_correlation_data" / "log.txt"
404408
script:
405-
"../scripts/pairwise_trajectory_correlation_data.R"
409+
"../scripts/report/pairwise_trajectory_correlation_data.R"
406410

407411

408412
rule summary_table:
@@ -436,7 +440,7 @@ rule report:
436440
temest = report(REPORT_DIR_PLOTS/"time_signal.png"),
437441
evo = report(REPORT_DIR_PLOTS/"dn_and_ds.png"),
438442
omega_plot = report(REPORT_DIR_PLOTS/"dnds.png"),
439-
heat_table = report(REPORT_DIR_TABLES/"pairwise_trajectory_correlation.csv"),
443+
heat_table = report(REPORT_DIR_TABLES/"pairwise_trajectory_correlation_matrix.csv"),
440444
freyja_ts = OUTDIR/"demixing"/"freyja_data"/"last_barcode_update.txt",
441445
value = REPORT_DIR_TABLES/"diversity.json",
442446
stats_lm = REPORT_DIR_TABLES/"time_signal.json",

workflow/scripts/pairwise_trajectory_correlation_data.R renamed to workflow/scripts/report/pairwise_trajectory_correlation_data.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ date_order <- read_csv(snakemake@input[["metadata"]]) %>%
2323
pull(ID) %>%
2424
unique()
2525

26+
log_info("Formatting variants")
2627
all_variants_wider <- variants %>%
2728
select(SAMPLE, VARIANT_NAME, ALT_FREQ) %>%
2829
pivot_wider(
@@ -35,5 +36,15 @@ all_variants_wider <- variants %>%
3536
rename_with(~ str_replace(., "^([^|]+)\\|.*$", "\\1(...)"), -SAMPLE) %>%
3637
column_to_rownames(var = "SAMPLE")
3738

38-
log_info("Saving table")
39+
log_info("Saving table of frequencies")
3940
write.csv(all_variants_wider, snakemake@output[["table"]])
41+
42+
log_info("Calculating correlations")
43+
cor.mat <- cor(
44+
all_variants_wider,
45+
method = snakemake@params$cor_method,
46+
use = snakemake@params$cor_use
47+
)
48+
49+
log_info("Saving correlation matrix")
50+
write.csv(cor.mat, snakemake@output[["matrix"]])

0 commit comments

Comments
 (0)