Skip to content

Commit 8e76ece

Browse files
committed
added frip plot
1 parent cb54dca commit 8e76ece

File tree

3 files changed

+171
-3
lines changed

3 files changed

+171
-3
lines changed

workflow/rules/bed.smk

+54-1
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,57 @@ if config["peak_calling_perl"]["run"]:
151151
conda:
152152
"../envs/R.yaml"
153153
script:
154-
"../scripts/plot_enrichment.R"
154+
"../scripts/plot_enrichment.R"
155+
156+
rule count_reads_in_peaks:
157+
# Adapted from https://www.biostars.org/p/337872/#337890
158+
input:
159+
bam="results/bam/{dir}/{bg_sample}.bam",
160+
b="results/peaks/fdr{fdr}/{dir}/{bg_sample}.sorted.bed",
161+
output:
162+
total_read_count="results/peaks/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count",
163+
peak_read_count="results/peaks/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count",
164+
params:
165+
extra="",
166+
threads: config["resources"]["deeptools"]["cpu"]
167+
resources:
168+
runtime=config["resources"]["deeptools"]["time"]
169+
log:
170+
"logs/bedtools_intersect/fdr{fdr}/{dir}/{bg_sample}.log"
171+
conda:
172+
"../envs/peak_calling.yaml"
173+
shell:
174+
"bedtools bamtobed "
175+
"{params.extra} "
176+
"-i {input.bam} | "
177+
"sort -k1,1 -k2,2n | "
178+
"tee >(wc -l > {output.total_read_count}) | "
179+
"bedtools intersect "
180+
"{params.extra} "
181+
"-sorted "
182+
"-c "
183+
"-a {input.b} "
184+
"-b stdin | "
185+
"awk '{{i+=$NF}}END{{print i}}' > "
186+
"{output.peak_read_count} "
187+
"{log}"
188+
189+
190+
rule plot_fraction_of_reads_in_peaks:
191+
input:
192+
total_read_count=expand("results/peaks/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
193+
peak_read_count=expand("results/peaks/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
194+
output:
195+
plot="results/plots/peaks/fdr{fdr}/frip.pdf",
196+
csv="results/peaks/fdr{fdr}/frip.csv",
197+
params:
198+
extra="",
199+
threads: config["resources"]["plotting"]["cpu"]
200+
resources:
201+
runtime=config["resources"]["plotting"]["time"]
202+
log:
203+
"logs/plot_frip/fdr{fdr}.log"
204+
conda:
205+
"../envs/R.yaml"
206+
script:
207+
"../scripts/plot_frip.R"

workflow/rules/peak_calling.smk

+110-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,60 @@ if config["peak_calling_macs2"]["run"]:
196196
"../envs/R.yaml"
197197
script:
198198
"../scripts/plot_enrichment.R"
199+
200+
201+
rule count_reads_in_peaks:
202+
# Adapted from https://www.biostars.org/p/337872/#337890
203+
input:
204+
bam="results/bam/{dir}/{bg_sample}.bam",
205+
b="results/macs2_narrow/fdr{fdr}/{dir}/{bg_sample}_peaks.narrowPeak",
206+
output:
207+
total_read_count="results/macs2_narrow/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count",
208+
peak_read_count="results/macs2_narrow/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count",
209+
params:
210+
extra="",
211+
threads: config["resources"]["deeptools"]["cpu"]
212+
resources:
213+
runtime=config["resources"]["deeptools"]["time"]
214+
log:
215+
"logs/bedtools_intersect/fdr{fdr}/{dir}/{bg_sample}.log"
216+
conda:
217+
"../envs/peak_calling.yaml"
218+
shell:
219+
"bedtools bamtobed "
220+
"{params.extra} "
221+
"-i {input.bam} | "
222+
"sort -k1,1 -k2,2n | "
223+
"tee >(wc -l > {output.total_read_count}) | "
224+
"bedtools intersect "
225+
"{params.extra} "
226+
"-sorted "
227+
"-c "
228+
"-a {input.b} "
229+
"-b stdin | "
230+
"awk '{{i+=$NF}}END{{print i}}' > "
231+
"{output.peak_read_count} "
232+
"{log}"
233+
234+
235+
rule plot_fraction_of_reads_in_peaks:
236+
input:
237+
total_read_count=expand("results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.total.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
238+
peak_read_count=expand("results/macs2_broad/fdr{fdr}/read_counts/{dir}/{bg_sample}.peak.count", dir=DIRS, fdr=fdr, bg_sample=BG_SAMPLES),
239+
output:
240+
plot="results/plots/macs2_broad/fdr{fdr}/frip.pdf",
241+
csv="results/macs2_broad/fdr{fdr}/frip.csv",
242+
params:
243+
extra="",
244+
threads: config["resources"]["plotting"]["cpu"]
245+
resources:
246+
runtime=config["resources"]["plotting"]["time"]
247+
log:
248+
"logs/plot_frip/fdr{fdr}.log"
249+
conda:
250+
"../envs/R.yaml"
251+
script:
252+
"../scripts/plot_frip.R"
199253

200254
elif config["peak_calling_macs2"]["mode"] == "broad":
201255
fdr = config["peak_calling_macs2"]["broad_cutoff"]
@@ -347,6 +401,7 @@ if config["peak_calling_macs2"]["run"]:
347401
script:
348402
"../scripts/enrichment_analysis.R"
349403

404+
350405
rule plot_enrichment:
351406
input:
352407
xlsx="results/macs2_broad/fdr{fdr}/enrichment_analysis/{bg_sample}.xlsx",
@@ -363,4 +418,58 @@ if config["peak_calling_macs2"]["run"]:
363418
conda:
364419
"../envs/R.yaml"
365420
script:
366-
"../scripts/plot_enrichment.R"
421+
"../scripts/plot_enrichment.R"
422+
423+
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+
457+
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"

workflow/scripts/general_functions.smk

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ def targets():
3232
expand("results/plots/peaks/fdr{fdr}/feature_distributions.pdf", fdr=fdr),
3333
expand("results/plots/peaks/fdr{fdr}/distance_to_tss.pdf", fdr=fdr),
3434
expand("results/peaks/fdr{fdr}/consensus_peaks/{bg_sample}.annotated.txt", fdr=fdr, bg_sample=BG_SAMPLES),
35-
expand("results/peaks/fdr{fdr}/consensus_peaks/{bg_sample}.geneIDs.txt", fdr=fdr,bg_sample=BG_SAMPLES)
35+
expand("results/peaks/fdr{fdr}/consensus_peaks/{bg_sample}.geneIDs.txt", fdr=fdr,bg_sample=BG_SAMPLES),
36+
expand(plot="results/plots/peaks/fdr{fdr}/frip.pdf", fdr=fdr),
37+
expand("results/plots/peaks/fdr{fdr}/frip.csv", fdr=fdr),
3638
])
3739
if config["consensus_peaks"]["enrichment_analysis"]["run"]:
3840
TARGETS.extend([
@@ -45,6 +47,8 @@ def targets():
4547
expand("results/plots/macs2_narrow/fdr{fdr}/feature_distributions.pdf", fdr=fdr),
4648
expand("results/plots/macs2_narrow/fdr{fdr}/distance_to_tss.pdf", fdr=fdr),
4749
expand("results/macs2_narrow/fdr{fdr}/{bg_sample}.geneIDs.txt", fdr=fdr, bg_sample=BG_SAMPLES),
50+
expand("results/plots/macs2_narrow/fdr{fdr}/frip.pdf", fdr=fdr),
51+
expand("results/macs2_narrow/fdr{fdr}/frip.csv", fdr=fdr),
4852
])
4953
if config["consensus_peaks"]["enrichment_analysis"]["run"]:
5054
TARGETS.extend([
@@ -55,6 +59,8 @@ def targets():
5559
expand("results/plots/macs2_broad/fdr{fdr}/feature_distributions.pdf", fdr=fdr),
5660
expand("results/plots/macs2_broad/fdr{fdr}/distance_to_tss.pdf", fdr=fdr),
5761
expand("results/macs2_broad/fdr{fdr}/{bg_sample}.geneIDs.txt", fdr=fdr, bg_sample=BG_SAMPLES),
62+
expand("results/plots/macs2_broad/fdr{fdr}/frip.pdf", fdr=fdr),
63+
expand("results/macs2_broad/fdr{fdr}/frip.csv", fdr=fdr),
5864
])
5965
if config["consensus_peaks"]["enrichment_analysis"]["run"]:
6066
TARGETS.extend([

0 commit comments

Comments
 (0)