Skip to content

Commit 390954b

Browse files
committed
improve sample annotation visualization
1 parent 6a89e65 commit 390954b

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

workflow/envs/ggplot.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ channels:
33
- bioconda
44
- nodefaults
55
dependencies:
6-
- r-tidyverse
7-
- r-data.table
8-
- r-patchwork
9-
- r-ggplot2
10-
- r-ggnewscale
11-
- r-plotly
12-
- r-htmlwidgets
6+
- r-tidyverse=2.0.0
7+
- r-data.table=1.17.6
8+
- r-patchwork=1.3.0
9+
- r-ggplot2=3.5.2
10+
- r-ggnewscale=0.5.1
11+
- r-plotly=4.11.0
12+
- r-htmlwidgets=1.6.4
13+
- r-stringr=1.5.1

workflow/rules/qc.smk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ rule plot_sample_annotation:
228228
log:
229229
"logs/rules/plot_sample_annotation.log",
230230
resources:
231-
mem_mb="1000",
231+
mem_mb="4000",
232232
conda:
233233
"../envs/ggplot.yaml"
234234
script:

workflow/scripts/plot_sample_annotation.R

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ library(tidyverse)
44
library(patchwork)
55
library(ggplot2)
66
library(ggnewscale)
7+
library(stringr)
78
# for interactive plotting
89
library(plotly)
910
library(htmlwidgets)
@@ -21,10 +22,11 @@ sample_annotation_html_path <- snakemake@output[["sample_annotation_html"]]
2122

2223
#### load & prepare data ####
2324
# load data
24-
sample_annotation <- data.frame(fread(file.path(sample_annotation_path), header=TRUE), row.names=1)
25+
sample_annotation <- data.frame(fread(file.path(sample_annotation_path), header=TRUE), row.names=1, check.names = FALSE)
2526
anno <- data.frame(fread(file.path(sample_annotation_w_QC_path), header=TRUE), row.names=1)
2627

2728
# determine QC (pipeline provided) columns
29+
names(sample_annotation) <- gsub(" +", "_", names(sample_annotation)) # replace empty space ` ` with underscore `_`
2830
qc_cols <- setdiff(names(anno), names(sample_annotation))
2931

3032
# determine metadata (user provided) columns by removing non-numeric columns that are unique for each row (e.g., bam_file)
@@ -146,11 +148,28 @@ if(length(meta_cols) > 0){
146148
barwidth = 0.15))
147149
} else { # categorical legend
148150
pal <- setNames(dat$col, dat$value)
151+
152+
# reduce legend in case of more than 10 levels
153+
max_items <- min(10, length(unique(dat$value)))
154+
all_levels <- unique(names(pal))
155+
show_levels <- all_levels[1:max_items]
156+
149157
p_meta <- p_meta +
150158
geom_tile(data = dat, aes(x = meta, y = sample, fill = value), colour = "grey60", linewidth = 0.1) +
151-
scale_fill_manual(values = pal, name = v,
152-
guide = guide_legend(keywidth = 0.25, # thinner
153-
keyheight = 0.4))
159+
scale_fill_manual(values = pal,
160+
# name = v,
161+
breaks = show_levels,
162+
guide = guide_legend(keywidth = 0.25,
163+
keyheight = 0.4,
164+
ncol=1,
165+
byrow = TRUE,
166+
title = ifelse(
167+
length(all_levels) <= max_items,
168+
v,
169+
paste0(v, " (showing ", max_items, "/", length(all_levels), ")")
170+
)
171+
)
172+
)
154173
}
155174
}
156175
}

0 commit comments

Comments
 (0)