-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSnakefile
237 lines (208 loc) · 9.03 KB
/
Snakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
##########################################################################################################
# Benchmarking of ChIP-Seq peak Callers
# Author: Skyler Kuhn (NIH/NCI) [C]
# CCR Collaborative Bioinformatics Resource
# Version 1.0.0
# See README.MD for more information
# USAGE:
# sbatch --cpus-per-task=8 --mem=16g snakemake.sh
##########################################################################################################
import sys
shell.prefix("set -eo pipefail; ")
configfile: "config.yaml"
localrules: all
controls = config["controls"]
if controls is None:
sys.exit("Controls are needed")
samples_narrow = config["samples_narrow"]
if samples_narrow is None:
samples_narrow = []
samples_broad = config["samples_broad"]
if samples_broad is None:
samples_broad = []
ALL_SAMPLES = samples_narrow + samples_broad + controls
ALL_BAM = expand("bam/{sample}.sorted.Q5DD.bam", sample = ALL_SAMPLES)
ALL_BAM.extend(expand("bam/{sample}.sorted.Q5DD.bam.bai", sample = ALL_SAMPLES))
ALL_BAM.extend(expand("bam/{sample}.sorted.Q5DD.bam.flagstat", sample = ALL_SAMPLES))
ALL_BAM.extend(("bam/control.sorted.Q5DD.bam", "bam/control.sorted.Q5DD.bam.bai", "bam/control.sorted.Q5DD.bam.flagstat"))
ALL_PEAKS = expand("peaks/mac2/narrow/{sample}_peaks.narrowPeak", sample = samples_narrow) + \
expand("peaks/mac2/broad/{sample}_peaks.broadPeak", sample = samples_broad) + \
expand("peaks/ranger/{sample}_summit.bed", sample = samples_narrow) + \
expand("peaks/bcp/{sample}_region.bed", sample = samples_broad) + \
expand("peaks/ccat/{sample}_summit.bed", sample = samples_broad) + \
expand("peaks/sicer/narrow/{sample}.sorted.Q5DD-W100-normalized.wig", sample = samples_narrow) + \
expand("peaks/sicer/broad/{sample}.sorted.Q5DD-W200-normalized.wig", sample = samples_broad)
ALL_BED = expand("bed/{sample}.sorted.Q5DD.bed", sample = ALL_SAMPLES)
ALL_BED.extend(expand("bed/{sample}.sorted.bed", sample = ALL_SAMPLES))
ALL_NGSQC = expand("ngsqc/{sample}/NGSQC_report.txt", sample = ALL_SAMPLES)
ALL_NGSQC.extend(expand("ngsqc/{sample}Q5DD/NGSQC_report.txt", sample = ALL_SAMPLES))
rule all:
input: ALL_PEAKS + ALL_BAM + ALL_BED + ALL_NGSQC
rule merge_controls:
input: bam = expand("bam/{sample}.sorted.Q5DD.bam", sample = controls),
bai = expand("bam/{sample}.sorted.Q5DD.bam.bai", sample = controls),
flagstat = expand("bam/{sample}.sorted.Q5DD.bam.flagstat", sample = controls)
output: bam = "bam/control.sorted.Q5DD.bam",
bai = "bam/control.sorted.Q5DD.bam.bai",
flagstat = "bam/control.sorted.Q5DD.bam.flagstat"
log: "log/merge_controls"
threads: 2
shell:
'''
inbam=( {input.bam} )
if [[ ${{#inbam[@]}} -eq 1 ]]; then
ln -s $(cd $(dirname {input.bam}) && pwd)/$(basename {input.bam}) {output.bam}
ln -s $(cd $(dirname {input.bai}) && pwd)/$(basename {input.bai}) {output.bai}
ln -s $(cd $(dirname {input.flagstat}) && pwd)/$(basename {input.flagstat}) {output.flagstat}
else
module load samtools/1.2
samtools merge -r -@{threads} {output.bam} {input.bam}
fi
'''
rule MAC2_narrowPeaks:
input: "bam/{sample}.sorted.Q5DD.bam", "bam/control.sorted.Q5DD.bam", "bam/{sample}.sorted.Q5DD.ppqt"
output: "peaks/mac2/narrow/{sample}_model.r", "peaks/mac2/narrow/{sample}_peaks.narrowPeak",
"peaks/mac2/narrow/{sample}_peaks.xls", "peaks/mac2/narrow/{sample}_summits.bed",
"peaks/mac2/narrow/{sample}_model.pdf"
log: "log/mac2/{sample}.find_narrow_peaks"
threads: 2
run:
fh = open(input[2])
fragsize = 150
for line in fh:
try:
linelist = line.strip().split("\t")
fragsize = int(linelist[2].split(",")[0])
except IndexError:
pass
fh.close()
# took out -B
shell('''
module load macs/2.1.0.20150420 R
macs2 callpeak -t {input[0]} \
-c {input[1]} -f BAM -g {config[macs_g]} \
--outdir peaks/mac2/narrow -n {wildcards.sample} \
--nomodel --extsize {fragsize} -q 0.01 2> {log}
cd peaks/mac2/narrow && Rscript {wildcards.sample}_model.r
''')
rule MAC2_broadPeaks:
input: "bam/{sample}.sorted.Q5DD.bam", "bam/control.sorted.Q5DD.bam", "bam/{sample}.sorted.Q5DD.ppqt"
output: "peaks/mac2/broad/{sample}_peaks.xls", "peaks/mac2/broad/{sample}_peaks.broadPeak"
log: "log/mac2/{sample}.find_broad_peaks"
threads: 2
run:
fh = open(input[2])
fragsize = 150
for line in fh:
try:
linelist = line.strip().split("\t")
fragsize = int(linelist[2].split(",")[0])
except IndexError:
pass
fh.close()
shell('''
module load macs/2.1.0.20150420
macs2 callpeak -t {input[0]} \
-c {input[1]} -f BAM -g {config[macs_g]} \
--broad --broad-cutoff 0.1 --nomodel --extsize {fragsize} \
--outdir peaks/mac2/broad -n {wildcards.sample} -q 0.001 2> {log}
''')
rule bams2beds:
input: "bam/{sample}.sorted.bam", "bam/{sample}.sorted.Q5DD.bam"
output: "bed/{sample}.sorted.bed", "bed/{sample}.sorted.Q5DD.bed"
log: "log/bed2bam/"
threads: 2
shell:
'''
module load bedtools
bedtools bamtobed -i {input[0]} > {output[0]} 2> {log}{wildcards.sample}.bam2bed
bedtools bamtobed -i {input[1]} > {output[1]} 2> {log}{wildcards.sample}.Q5DD.bam2bed
'''
rule ngsqc:
input: "bed/{sample}.sorted.bed", "bed/{sample}.sorted.Q5DD.bed"
output: "ngsqc/{sample}/NGSQC_report.txt", "ngsqc/{sample}Q5DD/NGSQC_report.txt"
log: "log/ngsqc/"
threads: 2
shell:
'''
module load bedtools
/scratch/ChIPSeqBenchmarking/NGSQC_linux_x86_64 -v -o ngsqc/{wildcards.sample} {input[0]} /scratch/ChIPSeqBenchmarking/genomes/hg19.genome 2> {log}{wildcards.sample}.ngsqc
/scratch/ChIPSeqBenchmarking/NGSQC_linux_x86_64 -v -o ngsqc/{wildcards.sample}Q5DD {input[1]} /scratch/ChIPSeqBenchmarking/genomes/hg19.genome 2> {log}{wildcards.sample}Q5DD.ngsqc
'''
rule ranger:
input: "bam/{sample}.sorted.Q5DD.bam","bam/control.sorted.Q5DD.bam"
output: "peaks/ranger/{sample}_summit.bed", "peaks/ranger/{sample}_region.bed"
log: "log/ranger/"
threads: 4
shell:
'''
module load peakranger
mkdir --p peaks/ranger
peakranger ranger --format bam --data {input[0]} --control {input[1]} --output peaks/ranger/{wildcards.sample}
'''
rule bcp:
input: "bam/{sample}.sorted.Q5DD.bam","bam/control.sorted.Q5DD.bam"
output: "peaks/bcp/{sample}_region.bed"
log: "log/bcp/"
threads: 4
shell:
'''
module load peakranger
mkdir --p peaks/bcp
peakranger bcp --format bam --data {input[0]} --control {input[1]} --output peaks/bcp/{wildcards.sample}
'''
rule ccat:
input: "bam/{sample}.sorted.Q5DD.bam","bam/control.sorted.Q5DD.bam"
output: "peaks/ccat/{sample}_summit.bed", "peaks/ccat/{sample}_region.bed"
log: "log/ccat/"
threads: 4
shell:
'''
module load peakranger
mkdir --p peaks/ccat
peakranger ccat --format bam --data {input[0]} --control {input[1]} --output peaks/ccat/{wildcards.sample}
'''
rule sicer_narrow:
input: "bed/{sample}.sorted.Q5DD.bed", "bam/{sample}.sorted.Q5DD.ppqt"
output: "peaks/sicer/narrow/{sample}.sorted.Q5DD-W100-normalized.wig"
log: "log/sicer/{sample}.find_narrow_peaks"
threads: 2
run:
fh = open(input[1])
fragsize = 150
for line in fh:
try:
linelist = line.strip().split("\t")
fragsize = int(linelist[2].split(",")[0])
except IndexError:
pass
fh.close()
shell('''
module load sicer bedtools
if [ ! -f bed/control.sorted.Q5DD.bed ]; then
bedtools bamtobed -i bam/control.sorted.Q5DD.bam > bed/control.sorted.Q5DD.bed
fi
bash SICER.sh ./bed {wildcards.sample}.sorted.Q5DD.bed control.sorted.Q5DD.bed ./peaks/sicer/narrow hg19 1 100 {fragsize} 0.79 200 0.01
''')
rule sicer_broad:
input: "bed/{sample}.sorted.Q5DD.bed", "bam/{sample}.sorted.Q5DD.ppqt"
output: "peaks/sicer/broad/{sample}.sorted.Q5DD-W200-normalized.wig"
log: "log/sicer/{sample}.find_broad_peaks"
threads: 2
run:
fh = open(input[1])
fragsize = 150
for line in fh:
try:
linelist = line.strip().split("\t")
fragsize = int(linelist[2].split(",")[0])
except IndexError:
pass
fh.close()
shell('''
module load sicer bedtools
if [ ! -f bed/control.sorted.Q5DD.bed ]; then
bedtools bamtobed -i bam/control.sorted.Q5DD.bam > bed/control.sorted.Q5DD.bed
fi
bash SICER.sh ./bed {wildcards.sample}.sorted.Q5DD.bed control.sorted.Q5DD.bed ./peaks/sicer/broad hg19 1 200 {fragsize} 0.79 400 0.01
''')