-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
348 lines (312 loc) · 10.5 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Title: SnakeMake pipeline to analyze bulk, paired-end RNA-Seq data
Author: Garth Kong
"""
import os
import sys
from glob import glob
from pandas import read_csv
from snakemake.utils import min_version
from yaml import safe_load
min_version("5.11")
if sys.version_info < (3, 6):
sys.exit("Python version is less than 3.6. Your python version:", sys.version_info)
SAMPLES, = glob_wildcards("data/raw/{sample}_R1.fastq.gz")
configfile: "config.yaml"
# singularity: "/home/groups/MaxsonLab/software/singularity-containers/4.12.0_sha256.7302640e37d37af02dd48c812ddf9c540a7dfdbfc6420468923943651f795591.sif"
def message(msg):
sys.stderr.write("|--- " + msg + "\n")
for i in SAMPLES:
message("Processing " + i)
def define_reads():
READS = sorted(glob("data/raw/*.gz"))
READS = [ os.path.basename(i).split(".")[0] for i in READS ]
return READS
READS = define_reads()
def defect_mode(wildcards, attempt):
if attempt == 1:
return ""
elif attempt > 1:
return "-D"
def define_contrasts(file = config["CONTRASTS"]):
contrasts = read_csv(file, sep = "\t", header = None)
contrast1 = contrasts[0]
contrast2 = contrasts[1]
return contrast1, contrast2
contrast1, contrast2 = define_contrasts()
def detect_singularity():
from sys import argv
cmd = " ".join(sys.argv)
singularity_flag = "--use-singularity"
if cmd.find(singularity_flag) != -1:
return "true"
else:
return "false"
rule all:
input:
# quality control -----------------------------------------------------
# expand("data/fastp/{sample}_{read}.fastq.gz", sample = SAMPLES, read = ["R1", "R2"]),
expand("data/fastqc/{reads}_fastqc.html", reads = READS),
expand("data/fastq_screen/{reads}_screen.txt", reads = READS),
expand("data/preseq/estimates_{sample}.txt", sample = SAMPLES),
expand("data/preseq/lcextrap_{sample}", sample = SAMPLES),
"data/multiqc/multiqc_report.html",
# read alignment ------------------------------------------------------
expand([
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam",
"data/star/{sample}_bam/ReadsPerGene.out.tab",
"data/star/{sample}_bam/Log.final.out"
], sample = SAMPLES),
expand("data/bigwig/{sample}.bw", sample = SAMPLES),
"data/counts/{}-raw-counts.txt".format(config["PROJECT_ID"]),
"data/counts/{}-raw-filtered-counts.txt".format(config["PROJECT_ID"]),
# deseq2 --------------------------------------------------------------
"data/counts/{}-deseq2-norm.txt".format(config["PROJECT_ID"]),
"data/counts/{}-log2-deseq2-norm.txt".format(config["PROJECT_ID"]),
"data/deseq2/group",
expand(["data/deseq2/pairwise/{c1}-vs-{c2}-all.txt",
"data/deseq2/pairwise/{c1}-vs-{c2}-pca.pdf",
"data/deseq2/pairwise/{{c1}}-vs-{{c2}}-{p}.txt".format(p = config["PADJ"]),
], zip, c1 = contrast1, c2 = contrast2)
# pre-processing ----------------------------------------------------------------------------------
rule fastp:
input:
r1 = "data/raw/{sample}_R1.fastq.gz",
r2 = "data/raw/{sample}_R2.fastq.gz"
output:
r1 = temp("data/fastp/{sample}_R1.fastq.gz"),
r2 = temp("data/fastp/{sample}_R2.fastq.gz")
params:
adapter_fasta_file = config["ADAPTER_FASTA"]
conda:
"envs/fastp.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "fastp.sif")
log:
"data/logs/{sample}.fastp.json"
threads: 4
shell:
"fastp "
"-i {input.r1} "
"-I {input.r2} "
"-o {output.r1} "
"-O {output.r2} "
"--detect_adapter_for_pe "
"--trim_poly_g "
"--adapter_fasta {params.adapter_fasta_file} "
"--thread {threads} "
"-j {log} "
"-h /dev/null"
rule fastqc:
input:
"data/fastp/{read}.fastq.gz"
output:
"data/fastqc/{read}_fastqc.html"
conda:
"envs/fastqc.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "fastqc.sif")
log:
"data/logs/fastqc_{read}.log"
threads: 1
shell:
"fastqc -t {threads} --outdir data/fastqc {input} > {log} 2>&1"
rule fastq_screen:
input:
fastq = "data/fastp/{read}.fastq.gz",
config = config["FASTQ_SCREEN_CONFIG"]
output:
"data/fastq_screen/{read}_screen.txt"
conda:
"envs/fastq_screen.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "fastq_screen.sif")
log:
"data/logs/fastq_screen_{read}.txt"
threads: 8
shell:
"fastq_screen --aligner bowtie2 --threads {threads} --outdir data/fastq_screen --conf {input.config} --force {input.fastq} > {log} 2>&1"
# read alignment ----------------------------------------------------------------------------------
rule STAR:
input:
fwd = "data/fastp/{sample}_R1.fastq.gz",
rev = "data/fastp/{sample}_R2.fastq.gz"
output:
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam",
"data/star/{sample}_bam/ReadsPerGene.out.tab",
"data/star/{sample}_bam/Log.final.out"
threads: 8
params:
gtf=config["GTF"],
genome_index=config["STAR"]
conda:
"envs/star.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "star.sif")
shell:
"STAR "
"--runThreadN {threads} "
"--runMode alignReads "
"--genomeDir {params.genome_index} "
"--readFilesIn {input.fwd} {input.rev} "
"--outFileNamePrefix data/star/{wildcards.sample}_bam/ "
"--sjdbGTFfile {params.gtf} "
"--quantMode GeneCounts "
"--sjdbGTFtagExonParentGene gene_name "
"--outSAMtype BAM SortedByCoordinate "
"--readFilesCommand zcat "
"--twopassMode Basic"
rule index:
input:
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam"
output:
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam.bai"
conda:
"envs/samtools.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "samtools.sif")
threads: 2
shell:
"samtools index -@ {threads} {input}"
rule preseq:
input:
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam",
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam.bai"
output:
"data/preseq/estimates_{sample}.txt"
conda:
"envs/preseq.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "preseq.sif")
resources:
defect_mode = defect_mode
log:
"data/logs/preseq_{sample}.log"
shell:
"preseq c_curve -B {resources.defect_mode} -l 1000000000 -P -o {output} {input} > {log} 2>&1"
rule preseq_lcextrap:
input:
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam",
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam.bai"
output:
"data/preseq/lcextrap_{sample}"
conda:
"envs/preseq.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "preseq.sif")
resources:
defect_mode = defect_mode
log:
"data/logs/preseq_lcextrap_{sample}.log"
shell:
"preseq lc_extrap -B {resources.defect_mode} -l 1000000000 -P -e 1000000000 -o {output} {input} > {log} 2>&1"
rule bigwig:
input:
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam",
"data/star/{sample}_bam/Aligned.sortedByCoord.out.bam.bai"
output:
"data/bigwig/{sample}.bw"
conda:
"envs/deeptools.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "deeptools.sif")
threads: 4
shell:
"bamCoverage -b {input[0]} -o {output} -p {threads} --normalizeUsing CPM --binSize 10 --smoothLength 50"
rule multiqc:
input:
expand("data/fastp/{sample}_{reads}.fastq.gz", sample = SAMPLES, reads = ["R1", "R2"]),
expand("data/fastqc/{reads}_fastqc.html", reads = READS),
expand("data/fastq_screen/{reads}_screen.txt", reads = READS),
expand("data/star/{sample}_bam/Aligned.sortedByCoord.out.bam", sample = SAMPLES),
expand("data/preseq/estimates_{sample}.txt", sample = SAMPLES),
expand("data/preseq/lcextrap_{sample}", sample = SAMPLES)
output:
"data/multiqc/multiqc_report.html"
params:
singularity = detect_singularity()
conda:
"envs/multiqc.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "multiqc.sif")
shell:
"if [ '{params.singularity}' == 'true' ]; then export LC_ALL=C.UTF-8; export LANG=C.UTF-8; fi && "
"multiqc data -f --ignore data/tmp -o data/multiqc 2>&1"
# counts table ------------------------------------------------------------------------------------
rule compile_counts:
input:
expand("data/star/{sample}_bam/ReadsPerGene.out.tab", sample = SAMPLES)
output:
"data/counts/{}-raw-counts.txt".format(config["PROJECT_ID"])
params:
samples = SAMPLES
script:
"scripts/compile_star_counts.py"
rule filter_counts:
input:
counts = "data/counts/{}-raw-counts.txt".format(config["PROJECT_ID"]),
annotation = config["ANNOTATION"]
output:
"data/counts/{}-raw-filtered-counts.txt".format(config["PROJECT_ID"])
script:
"scripts/filter_counts.py"
# deseq2 ------------------------------------------------------------------------------------------
# output log2-transformed deseq2-normalized counts table.
rule deseq2_norm:
input:
counts = "data/counts/{}-raw-filtered-counts.txt".format(config["PROJECT_ID"]),
md = config["DESEQ2_CONFIG"]
output:
norm_counts = "data/counts/{}-deseq2-norm.txt".format(config["PROJECT_ID"]),
log2_norm_counts = "data/counts/{}-log2-deseq2-norm.txt".format(config["PROJECT_ID"])
params:
model = config["MODEL"]
conda:
"envs/deseq2.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "deseq2.sif")
script:
"scripts/deseq2-norm.R"
rule deseq2_pairwise:
input:
counts = "data/counts/{}-raw-filtered-counts.txt".format(config["PROJECT_ID"]),
md = config["DESEQ2_CONFIG"],
contrasts = config["CONTRASTS"]
output:
all_genes = "data/deseq2/pairwise/{c1}-vs-{c2}-all.txt",
sig_genes = "data/deseq2/pairwise/{{c1}}-vs-{{c2}}-{p}.txt".format(p = config["PADJ"]),
pca_plot = "data/deseq2/pairwise/{c1}-vs-{c2}-pca.pdf"
params:
use_singularity = detect_singularity(),
model = config["MODEL"],
padj = config["PADJ"],
c1 = "{c1}",
c2 = "{c2}",
outdir = "data/deseq2/pairwise"
conda:
"envs/deseq2.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "deseq2.sif")
threads: 1
log:
"data/logs/deseq2-pairwise-{c1}-vs-{c2}.log"
script:
"scripts/deseq2-pairwise.R"
rule deseq2_group:
input:
counts = "data/counts/{}-raw-filtered-counts.txt".format(config["PROJECT_ID"]),
md = config["DESEQ2_CONFIG"]
output:
outdir = directory("data/deseq2/group")
params:
use_singularity = detect_singularity(),
model = config["MODEL"],
padj = config["PADJ"]
conda:
"envs/deseq2.yaml"
singularity:
os.path.join(config["SINGULARITY_IMAGE_FOLDER"], "deseq2.sif")
log:
"data/logs/deseq2-group.log"
script:
"scripts/deseq2-group.R"