Skip to content

Commit 578fb6f

Browse files
committed
Decouple demix plot from data
Fix plot design source Update sample sorting Update demix table column names Fix column names Update temp variable name Update structure
1 parent a6c2fb3 commit 578fb6f

5 files changed

Lines changed: 99 additions & 76 deletions

File tree

workflow/rules/demix.smk

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rule demix_update:
1+
rule demix_barcode_update:
22
threads: 1
33
shadow: "shallow"
44
conda:
@@ -14,7 +14,7 @@ rule demix_update:
1414
pathogens = OUTDIR/"demixing"/"freyja_data"/"pathogen_config.yml",
1515
usher_barcodes = OUTDIR/"demixing"/"freyja_data"/"usher_barcodes.feather"
1616
log:
17-
LOGDIR / "demix_update" / "log.txt"
17+
LOGDIR / "demix_barcode_update" / "log.txt"
1818
shell:
1919
"mkdir -p {output.folder:q} && "
2020
"freyja update --outdir {output.folder:q} --pathogen {params.pathogen:q} >{log} 2>&1"
@@ -63,7 +63,7 @@ rule demix:
6363
relaxed_mrca_thresh = config["DEMIX"]["RELAXED_MRCA_THRESH"],
6464
solver = config["DEMIX"]["SOLVER"],
6565
output:
66-
demix_file = OUTDIR/"demixing"/"{sample}/{sample}_demixed.tsv"
66+
demix_file = OUTDIR/"demixing"/"samples"/"{sample}/{sample}_demixed.tsv"
6767
log:
6868
LOGDIR / "demix" / "{sample}.log.txt"
6969
shell:
@@ -91,15 +91,15 @@ rule demix:
9191
">{log} 2>&1"
9292

9393

94-
rule summarise_demixing:
94+
rule summarise_demix:
9595
threads: 1
9696
conda: "../envs/renv.yaml"
9797
shadow: "shallow"
9898
input:
99-
tables = expand(OUTDIR/"demixing"/"{sample}/{sample}_demixed.tsv", sample=iter_samples())
99+
tables = expand(OUTDIR/"demixing"/"samples"/"{sample}/{sample}_demixed.tsv", sample=iter_samples())
100100
output:
101-
summary_df = report(OUTDIR/"summary_freyja_demixing.csv")
101+
summary_df = report(OUTDIR/"demixing"/"summary.csv")
102102
log:
103-
LOGDIR / "summarise_demixing" / "log.txt"
103+
LOGDIR / "summarise_demix" / "log.txt"
104104
script:
105-
"../scripts/summary_demixing.R"
105+
"../scripts/summarise_demix.R"

workflow/rules/report.smk

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,27 @@ rule diversity:
4949
"../scripts/report/diversity_plot.R"
5050

5151

52+
rule freyja_plot_data:
53+
conda: "../envs/renv.yaml"
54+
input:
55+
summary_demixing = OUTDIR/"demixing"/"summary.csv",
56+
metadata = config["METADATA"]
57+
output:
58+
data = report(REPORT_DIR_TABLES/"freyja.csv")
59+
log:
60+
LOGDIR / "freyja_plot_data" / "log.txt"
61+
script:
62+
"../scripts/report/freyja_plot_data.R"
63+
64+
5265
rule freyja_plot:
5366
conda: "../envs/renv.yaml"
5467
params:
5568
design = config["PLOTS"]
5669
input:
57-
summary_demixing = OUTDIR/"summary_freyja_demixing.csv",
58-
metadata = config["METADATA"]
70+
data = REPORT_DIR_TABLES/"freyja.csv"
5971
output:
60-
fig = report(REPORT_DIR_PLOTS/"figure_1.png"),
61-
table = report(REPORT_DIR_TABLES/"figure_1.csv")
72+
plot = report(REPORT_DIR_PLOTS/"freyja.png")
6273
log:
6374
LOGDIR / "freyja_plot" / "log.txt"
6475
script:
@@ -175,7 +186,7 @@ rule report:
175186
shadow: "shallow"
176187
input:
177188
qmd = Path(config["REPORT_QMD"]).resolve(),
178-
freyja = report(REPORT_DIR_PLOTS/"figure_1.png"),
189+
freyja = report(REPORT_DIR_PLOTS/"freyja.png"),
179190
tree_ml = report(REPORT_DIR_PLOTS/"figure_2.png"),
180191
diversity = report(REPORT_DIR_PLOTS/"figure_3.png"),
181192
fig_cor = report(REPORT_DIR_PLOTS/"figure_4.png"),

workflow/scripts/report/freyja_plot.R

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,17 @@ log_threshold(INFO)
1313
source(snakemake@params[["design"]])
1414

1515
# Read inputs
16-
demix <- read_csv(snakemake@input[["summary_demixing"]])
17-
18-
# DATA PROCESSING
19-
log_info("Obtaining main lineages")
20-
main_lineages <- demix %>%
21-
group_by(sample) %>%
22-
top_n(1, abundances) %>%
23-
ungroup() %>%
24-
pull(lineages) %>%
25-
unique()
26-
27-
# Obtain sample names ordered by CollectionDate
28-
metadata <- read_csv(snakemake@input[["metadata"]])
29-
date_order <- metadata %>%
30-
arrange(CollectionDate) %>%
31-
filter(ID %in% demix$sample) %>%
32-
pull(ID) %>%
33-
unique()
34-
35-
# PLOT
36-
log_info("Plotting summary demixing")
37-
demix_plot <- demix %>%
38-
mutate(
39-
lineages = case_when(
40-
lineages %in% main_lineages ~ lineages
41-
)
42-
) %>%
43-
group_by(lineages, sample) %>%
16+
log_info("Plotting")
17+
p <- read_csv(snakemake@input$data) %>%
4418
mutate(
45-
abundances = sum(abundances)
19+
sample = reorder(sample, CollectionDate),
20+
lineage = ifelse(is_main, lineage, NA)
4621
) %>%
47-
unique() %>%
4822
ggplot() +
4923
aes(
50-
x = factor(sample, date_order),
51-
y = as.numeric(abundances),
52-
fill = lineages
24+
x = sample,
25+
y = abundance,
26+
fill = lineage
5327
) +
5428
scale_fill_viridis_d(
5529
na.value = "gray50",
@@ -68,34 +42,12 @@ demix_plot <- demix %>%
6842
fill = "Lineage"
6943
)
7044

45+
log_info("Saving plot")
7146
ggsave(
72-
filename = snakemake@output[["fig"]],
73-
plot = demix_plot,
47+
filename = snakemake@output[["plot"]],
48+
plot = p,
7449
width = 159.2,
7550
height = 119.4,
7651
units = "mm",
7752
dpi = 250
7853
)
79-
80-
81-
# PLOT TABLES
82-
log_info("Saving plot table")
83-
demix %>%
84-
mutate(
85-
lineages = case_when(
86-
lineages %in% main_lineages ~ lineages,
87-
TRUE ~ "Other"
88-
)
89-
) %>%
90-
group_by(sample, lineages) %>%
91-
summarise(abundances = sum(abundances)) %>%
92-
ungroup() %>%
93-
left_join(
94-
select(
95-
metadata,
96-
ID,
97-
CollectionDate
98-
),
99-
by = c("sample" = "ID")
100-
) %>%
101-
write.csv(snakemake@output[["table"]], row.names = FALSE)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
# Read inputs
13+
demix <- read_csv(snakemake@input[["summary_demixing"]])
14+
15+
# Data processing
16+
log_info("Obtaining main lineages")
17+
main_lineages <- demix %>%
18+
group_by(sample) %>%
19+
top_n(1, abundance) %>%
20+
ungroup() %>%
21+
pull(lineage) %>%
22+
unique()
23+
24+
# Obtain sample names ordered by CollectionDate
25+
log_info("Sorting dates")
26+
metadata <- read_csv(snakemake@input[["metadata"]])
27+
date_order <- metadata %>%
28+
arrange(CollectionDate) %>%
29+
filter(ID %in% demix$sample) %>%
30+
pull(ID) %>%
31+
unique()
32+
33+
# Build plot data
34+
log_info("Building plot data")
35+
plot.data <- demix %>%
36+
group_by(lineage, sample) %>%
37+
summarize(abundance = sum(abundance)) %>%
38+
ungroup() %>%
39+
left_join(
40+
select(
41+
metadata,
42+
ID,
43+
CollectionDate
44+
),
45+
by = c("sample" = "ID")
46+
) %>%
47+
mutate(
48+
is_main = lineage %in% main_lineages
49+
)
50+
51+
# Save plot data
52+
log_info("Saving plot data")
53+
write_csv(
54+
plot.data,
55+
snakemake@output[["data"]]
56+
)
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ log_threshold(INFO)
1111

1212
# Empty dataframe to be filled with data
1313
demix <- data.frame(
14-
"lineages" = NA,
15-
"abundances" = NA,
14+
"lineage" = NA,
15+
"abundance" = NA,
1616
"sample" = NA
1717
) %>%
1818
filter(!is.na(sample))
@@ -23,21 +23,25 @@ lapply(
2323
function(tsv_file) {
2424
read_tsv(
2525
tsv_file,
26-
col_names = c("variable", "valor"),
26+
col_names = c("variable", "value"),
2727
show_col_types = FALSE
2828
) %>%
2929
filter(
30-
row_number() %in% c(3, 4)
30+
variable %in% c("lineages", "abundances")
3131
) %>%
3232
pivot_wider(
3333
names_from = variable,
34-
values_from = valor
34+
values_from = value
3535
) %>%
3636
separate_rows(
3737
lineages,
3838
abundances,
3939
sep = " "
4040
) %>%
41+
rename(
42+
lineage = lineages,
43+
abundance = abundances
44+
) %>%
4145
mutate(
4246
sample = str_extract(
4347
basename(tsv_file),

0 commit comments

Comments
 (0)