Skip to content

Commit 399e7cc

Browse files
committed
frip plot working
1 parent c1323ca commit 399e7cc

File tree

4 files changed

+65
-65
lines changed

4 files changed

+65
-65
lines changed

workflow/rules/bed.smk

-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ if config["peak_calling_perl"]["run"]:
184184
"-b stdin | "
185185
"awk '{{i+=$NF}}END{{print i}}' > "
186186
"{output.peak_read_count} "
187-
"{log}"
188187

189188

190189
rule plot_fraction_of_reads_in_peaks:

workflow/rules/peak_calling.smk

+55-57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from snakemake.logging import logger
2-
31
if config["peak_calling_perl"]["run"]:
42
fdr = config["peak_calling_perl"]["fdr"]
53
rule peak_calling_perl:
@@ -71,7 +69,7 @@ if config["peak_calling_macs2"]["run"]:
7169
resources:
7270
runtime=config["resources"]["deeptools"]["time"]
7371
log:
74-
"logs/consensus_peaks_macs2_narrow_fdr{fdr}/{bg_sample}.log"
72+
"logs/consensus_peaks_macs2_narrow/fdr{fdr}/{bg_sample}.log"
7573
conda:
7674
"../envs/peak_calling.yaml"
7775
shell:
@@ -113,7 +111,7 @@ if config["peak_calling_macs2"]["run"]:
113111
resources:
114112
runtime=config["resources"]["plotting"]["time"]
115113
log:
116-
"logs/plotting/macs2_narrow_peak_annotation_plots_fdr{fdr}.log"
114+
"logs/plotting/macs2_narrow_peak_annotation_plots/fdr{fdr}.log"
117115
conda:
118116
"../envs/R.yaml"
119117
script:
@@ -130,7 +128,7 @@ if config["peak_calling_macs2"]["run"]:
130128
params:
131129
extra=""
132130
log:
133-
"logs/annotate_peaks_macs2_narrow_fdr{fdr}/{bg_sample}.log"
131+
"logs/annotate_peaks_macs2_narrow/fdr{fdr}/{bg_sample}.log"
134132
threads: config["resources"]["deeptools"]["cpu"]
135133
resources:
136134
runtime=config["resources"]["deeptools"]["time"]
@@ -293,7 +291,7 @@ if config["peak_calling_macs2"]["run"]:
293291
resources:
294292
runtime=config["resources"]["deeptools"]["time"]
295293
log:
296-
"logs/consensus_peaks_macs2_broad_fdr{fdr}/{bg_sample}.log"
294+
"logs/consensus_peaks_macs2_broad/fdr{fdr}/{bg_sample}.log"
297295
conda:
298296
"../envs/peak_calling.yaml"
299297
shell:
@@ -421,55 +419,55 @@ if config["peak_calling_macs2"]["run"]:
421419
"../scripts/plot_enrichment.R"
422420

423421

424-
rule count_reads_in_peaks:
425-
# Adapted from https://www.biostars.org/p/337872/#337890
426-
input:
427-
bam="results/bam/{dir}/{bg_sample}.bam",
428-
b="results/macs2_broad/fdr{fdr}/{dir}/{bg_sample}_peaks.broadPeak",
429-
output:
430-
total_read_count="results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count",
431-
peak_read_count="results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count",
432-
params:
433-
extra="",
434-
threads: config["resources"]["deeptools"]["cpu"]
435-
resources:
436-
runtime=config["resources"]["deeptools"]["time"]
437-
log:
438-
"logs/bedtools_intersect/fdr{fdr}/{dir}/{bg_sample}.log"
439-
conda:
440-
"../envs/peak_calling.yaml"
441-
shell:
442-
"bedtools bamtobed "
443-
"{params.extra} "
444-
"-i {input.bam} | "
445-
"sort -k1,1 -k2,2n | "
446-
"tee >(wc -l > {output.total_read_count}) | "
447-
"bedtools intersect "
448-
"{params.extra} "
449-
"-sorted "
450-
"-c "
451-
"-a {input.b} "
452-
"-b stdin | "
453-
"awk '{{i+=$NF}}END{{print i}}' > "
454-
"{output.peak_read_count} "
455-
"{log}"
456-
422+
rule count_reads_in_peaks:
423+
# Adapted from https://www.biostars.org/p/337872/#337890
424+
input:
425+
bam="results/bam/{dir}/{bg_sample}.bam",
426+
b="results/macs2_broad/fdr{fdr}/{dir}/{bg_sample}_peaks.broadPeak",
427+
output:
428+
total_read_count="results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count",
429+
peak_read_count="results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count",
430+
params:
431+
extra="",
432+
threads: config["resources"]["deeptools"]["cpu"]
433+
resources:
434+
runtime=config["resources"]["deeptools"]["time"]
435+
log:
436+
"logs/bedtools_intersect/fdr{fdr}/{dir}/{bg_sample}.log"
437+
conda:
438+
"../envs/peak_calling.yaml"
439+
shell:
440+
"bedtools bamtobed "
441+
"{params.extra} "
442+
"-i {input.bam} | "
443+
"sort -k1,1 -k2,2n | "
444+
"tee >(wc -l > {output.total_read_count}) | "
445+
"bedtools intersect "
446+
"{params.extra} "
447+
"-sorted "
448+
"-c "
449+
"-a {input.b} "
450+
"-b stdin | "
451+
"awk '{{i+=$NF}}END{{print i}}' > "
452+
"{output.peak_read_count} "
453+
#"{log}"
454+
457455

458-
rule plot_fraction_of_reads_in_peaks:
459-
input:
460-
total_read_count=expand("results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
461-
peak_read_count=expand("results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
462-
output:
463-
plot="results/plots/macs2_broad/fdr{fdr}/frip.pdf",
464-
csv="results/macs2_broad/fdr{fdr}/frip.csv",
465-
params:
466-
extra="",
467-
threads: config["resources"]["plotting"]["cpu"]
468-
resources:
469-
runtime=config["resources"]["plotting"]["time"]
470-
log:
471-
"logs/plot_frip/fdr{fdr}.log"
472-
conda:
473-
"../envs/R.yaml"
474-
script:
475-
"../scripts/plot_frip.R"
456+
rule plot_fraction_of_reads_in_peaks:
457+
input:
458+
total_read_count=expand("results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
459+
peak_read_count=expand("results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
460+
output:
461+
plot="results/plots/macs2_broad/fdr{fdr}/frip.pdf",
462+
csv="results/macs2_broad/fdr{fdr}/frip.csv",
463+
params:
464+
extra="",
465+
threads: config["resources"]["plotting"]["cpu"]
466+
resources:
467+
runtime=config["resources"]["plotting"]["time"]
468+
log:
469+
"logs/plot_frip/fdr{fdr}.log"
470+
conda:
471+
"../envs/R.yaml"
472+
script:
473+
"../scripts/plot_frip.R"

workflow/scripts/general_functions.smk

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def targets():
3434
expand("results/peaks/fdr{fdr}/consensus_peaks/{bg_sample}.annotated.txt", fdr=fdr, bg_sample=BG_SAMPLES),
3535
expand("results/peaks/fdr{fdr}/consensus_peaks/{bg_sample}.geneIDs.txt", fdr=fdr,bg_sample=BG_SAMPLES),
3636
expand("results/plots/peaks/fdr{fdr}/frip.pdf", fdr=fdr),
37-
expand("results/plots/peaks/fdr{fdr}/frip.csv", fdr=fdr),
37+
expand("results/peaks/fdr{fdr}/frip.csv", fdr=fdr),
3838
])
3939
if config["consensus_peaks"]["enrichment_analysis"]["run"]:
4040
TARGETS.extend([

workflow/scripts/plot_frip.R

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ library(tidyverse)
77
library(cowplot)
88

99
# Plot bar graph of FRiP values
10-
total_read_counts <- snakemake@input[["total_read_counts"]]
10+
total_read_counts <- snakemake@input[["total_read_count"]]
1111
peak_read_counts <- snakemake@input[["peak_read_count"]]
1212

13-
# df to store FRiP values
13+
# Create df to store FRiP values
1414
df <- data.frame(sample = character(),
1515
read_in_peaks = numeric(),
1616
total_reads = numeric(),
1717
fraction_of_reads_in_peaks = numeric())
1818

1919
# Add FRiP values to df
2020
for (i in seq_along(total_read_counts)) {
21-
sample <- snakemake@wildcards[["bg_sample"]][i]
21+
sample <- basename(total_read_counts[i]) %>% str_remove(".total.count")
22+
sample <- paste(basename(dirname(total_read_counts[i])), sample, sep="_")
2223
total_reads <- as.numeric(read_lines(total_read_counts[i]))
2324
peak_reads <- as.numeric(read_lines(peak_read_counts[i]))
2425
frip <- peak_reads / total_reads
@@ -30,6 +31,9 @@ for (i in seq_along(total_read_counts)) {
3031
fraction_of_reads_in_peaks = frip)
3132
}
3233

34+
# Save df to file
35+
write.csv(df, snakemake@output[["csv"]], row.names = FALSE)
36+
3337
# Create plot
3438
p <- ggplot(df, aes(x = sample,
3539
y = fraction_of_reads_in_peaks)) +
@@ -43,11 +47,10 @@ p <- ggplot(df, aes(x = sample,
4347
limits = c(0, 1)) +
4448
labs(title = NULL,
4549
x = NULL,
46-
y = "Fraction of reads in peaks") +
47-
theme(plot.title = element_text(hjust = 0.5))
50+
y = "Fraction of reads in peaks")
4851

4952
# Save plot
50-
ggsave(snakemake@output[[1]], p)
53+
ggsave(snakemake@output[["plot"]], p)
5154

5255
# Close redirection of output/messages
5356
sink(log, type = "output")

0 commit comments

Comments
 (0)