Skip to content

Commit 4aa8af1

Browse files
committed
make heatmap versions scaled based on both counts and relative abundance. Also make the cutoff zone a bit more opaque and scale it to the total length of the primers
1 parent 5b5fe27 commit 4aa8af1

2 files changed

Lines changed: 69 additions & 43 deletions

File tree

‎bin/MergeCheck_Plot.R‎

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
suppressPackageStartupMessages(library(tidyverse))
33
suppressPackageStartupMessages(library(optparse))
44

5-
# A very simple plot script for generating a 'heatmap' checking overlaps.
5+
# A very simple plot script for generating a 'heatmap' checking overlaps.
66
option_list = list(
7-
# make_option(c("--forward_clip"), type="character", default=NULL, help="Forward (5') trim"),
8-
# make_option(c("--reverse_clip"), type="character", default=NULL, help="Reverse (5') trim"),
9-
# make_option(c("--minMergedLen"), type="numeric", default=0, help="cpus"),
7+
make_option("--forward",
8+
type = "character",
9+
default = "",
10+
help = "Forward (5') primer"),
11+
make_option("--reverse",
12+
type = "character",
13+
default = "",
14+
help = "Reverse (5') primer")
1015
)
1116

12-
opt <- parse_args(OptionParser(option_list=option_list))
13-
len_files <- list.files(".",
14-
pattern = "*.lengthstats.txt",
17+
opt <- parse_args(OptionParser(option_list = option_list))
18+
19+
len_files <- list.files(".",
20+
pattern = "*.lengthstats.txt",
1521
full.names = TRUE)
1622

1723
lens_tmp <- lapply(len_files,
@@ -20,50 +26,69 @@ lens_tmp <- lapply(len_files,
2026

2127
names(lens_tmp) <- gsub("\\S+/(\\S+).lengthstats.txt", "\\1", len_files)
2228

23-
# bind all the data, then group by Sample, add in relative abundance per Sample and binning info, then group by Sample + Bin and summarize counts and RelAb per bin
24-
# It's a bit of a hack but it generally works; however it's not perfect
25-
lens_all <- bind_rows(lens_tmp, .id = "Sample") %>%
26-
group_by(Sample) %>%
27-
mutate(RelAb=Count/sum(Count),
28-
Bin = cut(Length,
29-
seq(min(Length),
30-
max(Length),5),
31-
include.lowest = TRUE)) %>%
32-
group_by(Sample, Bin) %>%
33-
mutate(ReadCountPerBin=sum(Count),
34-
RelAbPerBin=sum(RelAb))
29+
# bind all the data,
30+
# group by Sample,
31+
# add in relative abundance per Sample and binning info,
32+
# group by Sample + Bin,
33+
# summarize counts and RelAb per bin.
34+
lens_all <- bind_rows(lens_tmp, .id = "Sample") |>
35+
group_by(Sample) |>
36+
mutate(RelAb = Count / sum(Count),
37+
Bin = cut(Length,
38+
seq(min(Length),
39+
max(Length), 5),
40+
include.lowest = TRUE)) |>
41+
group_by(Sample, Bin) |>
42+
mutate(ReadCountPerBin = sum(Count),
43+
RelAbPerBin = sum(RelAb))
3544

3645
lens_all$Sample <- factor(lens_all$Sample)
3746

3847
# This will become settable, but essentially anything 50nt or less is not kept
39-
cutoff <- 50
48+
cutoff <- nchar(opt$forward) + nchar(opt$reverse)
49+
50+
if (cutoff == 0) {
51+
cutoff <- 50
52+
}
4053

41-
gg <- lens_all |> ggplot(aes(x=Length, y=Sample, fill=ReadCountPerBin)) +
54+
cat("Cutoff is ", cutoff)
55+
56+
gg <- lens_all |>
57+
ggplot(aes(x = Length, y = Sample, fill = ReadCountPerBin)) +
4258
geom_tile(stat = "identity") +
43-
scale_fill_viridis_c(option="plasma", direction = -1) +
59+
scale_fill_viridis_c(option = "plasma", direction = -1) +
4460
annotate("rect",
45-
xmin = 0,
46-
xmax = cutoff,
47-
ymin = 0.5,
48-
ymax = Inf,
49-
alpha=0.3, fill="blue") +
61+
xmin = 0,
62+
xmax = cutoff,
63+
ymin = 0.5,
64+
ymax = Inf,
65+
alpha = 0.1,
66+
fill = "blue") +
5067
theme_minimal()
5168

52-
if (nlevels(lens_all$Sample) > 40) {
53-
gg <- gg + theme(axis.text.y=element_blank())
69+
if (nlevels(lens_all$Sample) > 50) {
70+
gg <- gg + theme(axis.text.y = element_blank())
5471
}
5572

56-
# if(fprimer > 0) {
57-
# gg <- gg+ geom_vline(xintercept=fprimer, color = "red", alpha = 0.5)
58-
# }
73+
ggsave("MergedCheck_heatmap.counts.pdf", gg)
5974

60-
# if(rprimer > 0) {
61-
# gg <- gg+ geom_vline(xintercept=rprimer, color = "black", alpha = 0.5)
62-
# }
75+
gg2 <- lens_all |>
76+
ggplot(aes(x = Length, y = Sample, fill = RelAbPerBin)) +
77+
geom_tile(stat = "identity") +
78+
scale_fill_viridis_c(option = "plasma", direction = -1) +
79+
annotate("rect",
80+
xmin = 0,
81+
xmax = cutoff,
82+
ymin = 0.5,
83+
ymax = Inf,
84+
alpha = 0.1,
85+
fill = "blue") +
86+
theme_minimal()
87+
88+
if (nlevels(lens_all$Sample) > 50) {
89+
gg2 <- gg2 + theme(axis.text.y = element_blank())
90+
}
6391

64-
# if(maxsizeprimers > 0) {
65-
# gg <- gg+ geom_vline(xintercept=maxsizeprimers, color = "green", alpha = 0.5)
66-
# }
92+
ggsave("MergedCheck_heatmap.RelAb.pdf", gg2)
6793

68-
ggsave("MergedCheck_heatmap.pdf")
69-
saveRDS(gg, "MergedCheck_heatmap.RDS")
94+
saveRDS(lens_all, "stats.RDS")

‎modules/local/overlapheatmap.nf‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ process OVERLAP_HEATMAP {
88
path(merged_tables)
99

1010
output:
11-
path("MergedCheck_heatmap.pdf"), emit: overlap_check_pdf
12-
path("MergedCheck_heatmap.RDS"), emit: overlap_check_rds
11+
path("MergedCheck_heatmap*.pdf"), emit: overlap_check_pdf
12+
path("stats.RDS"), emit: overlap_check_rds
1313
// path "versions.yml" , emit: versions
1414

1515
when:
@@ -18,12 +18,13 @@ process OVERLAP_HEATMAP {
1818
script:
1919
def args = task.ext.args ?: ''
2020
"""
21-
MergeCheck_Plot.R
21+
MergeCheck_Plot.R --forward ${params.for_primer} --reverse ${params.rev_primer}
2222
"""
2323

2424
stub:
2525
def args = task.ext.args ?: ''
2626

2727
"""
28+
touch
2829
"""
2930
}

0 commit comments

Comments
 (0)