-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakefile
More file actions
381 lines (355 loc) · 13.5 KB
/
Snakefile
File metadata and controls
381 lines (355 loc) · 13.5 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from os.path import join
import yaml, sys, os, csv
snakemake_dir = '/gpfs/commons/groups/nygcfaculty/knowles_phatnani/ALS_compartment_RNA_seq/'
configfile: join(snakemake_dir, 'config/config.yaml')
fastq_dir = config['fastq_dir']
def scheduler_time_hours(hour):
return hour*60
def scheduler_memory(memory_gb):
return memory_gb*1000
rule all:
input:
expand(join(snakemake_dir, "fastqc_out/{sample}_trimmed_R1_fastqc.html"), sample = config['samples']),
expand(join(snakemake_dir, "fastqc_out/{sample}_trimmed_R2_fastqc.html"), sample = config['samples']),
expand(join(snakemake_dir, "junction_counts/dedup/{sample}_junctions.out"), sample = config['samples']),
expand(join(snakemake_dir, "junction_counts/normal/{sample}_junctions.out"), sample = config['samples']),
expand(join(snakemake_dir, "kallisto_out/normal/{sample}/abundance.tsv"), sample = config['samples']),
expand(join(snakemake_dir, "bam_stats/deduped/{sample}_stats.txt"), sample = config['samples']),
expand(join(snakemake_dir, "bam_stats/normal/{sample}_stats.txt"), sample = config['samples']),
expand(join(snakemake_dir, "bam_stats/deduped/{sample}.metrics.tsv"), sample = config['samples']),
expand(join(snakemake_dir, "bam_stats/normal/{sample}.metrics.tsv"), sample = config['samples']),
expand(join(snakemake_dir, "labrat_quant/{sample}/quant.sf"), sample = config['samples'])
ruleorder: combine_files > trim_adapter
rule combine_files:
output:
R1 = temp(join(snakemake_dir, "fastq/{sample}_R1.fastq.gz")),
R2 = temp(join(snakemake_dir, "fastq/{sample}_R2.fastq.gz"))
params:
R1 = join(fastq_dir, "Sample_{sample}/fastq/{sample}*_001.R1.fastq.gz"),
R2 = join(fastq_dir, "Sample_{sample}/fastq/{sample}*_001.R2.fastq.gz")
shell:
"""
cat {params.R1} > {output.R1}
cat {params.R2} > {output.R2}
"""
rule trim_adapter:
input:
R1 = join(snakemake_dir, "fastq/{sample}_R1.fastq.gz"),
R2 = join(snakemake_dir, "fastq/{sample}_R2.fastq.gz")
output:
R1 = temp(join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R1.fastq.gz")),
R2 = temp(join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R2.fastq.gz"))
resources:
mem = scheduler_memory(20)
threads:
8
shell:
"""
cutadapt -a AGATCGGAAGAG -a CTGTCTCTTATACACATCT -A CTCTTCCGATCT -A TGTCTCTTATACACAT -m 40 -j {threads} -o {output.R1} -p {output.R2} {input.R1} {input.R2}
"""
rule fastqc_R1:
input:
join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R1.fastq.gz")
output:
join(snakemake_dir, "fastqc_out/{sample}_trimmed_R1_fastqc.zip"),
join(snakemake_dir, "fastqc_out/{sample}_trimmed_R1_fastqc.html")
params:
out_dir = join(snakemake_dir, 'fastqc_out/')
resources:
mem = scheduler_memory(20)
threads:
8
shell:
"""
fastqc {input} -t {threads} -o {params.out_dir}
"""
rule fastqc_R2:
input:
join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R2.fastq.gz")
output:
join(snakemake_dir, "fastqc_out/{sample}_trimmed_R2_fastqc.zip"),
join(snakemake_dir, "fastqc_out/{sample}_trimmed_R2_fastqc.html")
params:
out_dir = join(snakemake_dir, 'fastqc_out/')
resources:
mem = scheduler_memory(20)
threads:
8
shell:
"""
fastqc {input} -t {threads} -o {params.out_dir}
"""
rule star_map:
input:
R1 = join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R1.fastq.gz"),
R2 = join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R2.fastq.gz")
output:
bam_file = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam"),
log_out = join(snakemake_dir, "bam/{sample}_Log.out"),
final_out = join(snakemake_dir, "bam/{sample}_Log.final.out"),
progress_out = temp(join(snakemake_dir, "bam/{sample}_Log.progress.out")),
SJ_out = temp(join(snakemake_dir, "bam/{sample}_SJ.out.tab")),
genome_folder = temp(directory(join(snakemake_dir, "bam/{sample}__STARgenome"))),
pass_folder = temp(directory(join(snakemake_dir, "bam/{sample}__STARpass1")))
threads:
8
resources:
mem = scheduler_memory(41),
time = scheduler_time_hours(8)
message:
"mapping with star"
params:
genome_index = config['star_index'],
output_prefix = join(snakemake_dir, "bam/{sample}_"),
shell:
"""
STAR --genomeDir {params.genome_index} --readFilesIn {input.R1} {input.R2} --readFilesCommand zcat --runMode alignReads --outSAMattributes All --outSAMstrandField intronMotif --limitBAMsortRAM 40000000000 --outSAMtype BAM SortedByCoordinate --runThreadN {threads} --twopassMode Basic --outFileNamePrefix {params.output_prefix}
"""
rule bam_index:
input:
join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam")
output:
temp(join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam.bai"))
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"indexing bam file"
shell:
"""
samtools index -@ 7 {input}
"""
rule collate_bam:
input:
bam = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam"),
bai = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam.bai")
output:
temp(join(snakemake_dir, "bam/{sample}_collated.bam"))
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"collating bam file; first step of duplicate removal"
shell:
"""
samtools collate -@ 7 -o {output} {input.bam}
"""
rule fixmate_bam:
input:
join(snakemake_dir, "bam/{sample}_collated.bam")
output:
temp(join(snakemake_dir, "bam/{sample}_mate_fixed.bam"))
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"fixing mate of bam file; second step of duplicate removal"
shell:
"""
samtools fixmate -@ 7 -m {input} {output}
"""
rule sort_fixmate_bam:
input:
join(snakemake_dir, "bam/{sample}_mate_fixed.bam")
output:
temp(join(snakemake_dir, "bam/{sample}_mate_fixed_sorted.bam"))
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"sorting fixed mate bam file; third step of duplicate removal"
shell:
"""
samtools sort -@ 7 -o {output} {input}
"""
rule remove_duplicates:
input:
join(snakemake_dir, "bam/{sample}_mate_fixed_sorted.bam")
output:
bam = temp(join(snakemake_dir, "bam/{sample}_duplicate_removed.bam")),
stats = join(snakemake_dir, "dedup_stats/{sample}_stats.txt")
threads:
8
resources:
mem = scheduler_memory(40),
time = scheduler_time_hours(1)
message:
"sorting fixed mate bam file; third step of duplicate removal"
shell:
"""
samtools markdup -r -f {output.stats} -@ 7 {input} {output.bam}
"""
rule index_deduped_bam:
input:
join(snakemake_dir, "bam/{sample}_duplicate_removed.bam")
output:
temp(join(snakemake_dir, "bam/{sample}_duplicate_removed.bam.bai"))
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"indexing deduped bam file"
shell:
"""
samtools index -@ 7 {input}
"""
rule stats_deduped:
input:
bam = join(snakemake_dir, "bam/{sample}_duplicate_removed.bam"),
bai = join(snakemake_dir, "bam/{sample}_duplicate_removed.bam.bai")
output:
join(snakemake_dir, "bam_stats/deduped/{sample}_stats.txt")
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"getting bam stats for deduped bam"
shell:
"""
samtools stats -@ {threads} {input.bam} > {output}
"""
rule stats_normal:
input:
bam = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam"),
bai = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam.bai")
output:
join(snakemake_dir, "bam_stats/normal/{sample}_stats.txt")
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(1)
message:
"getting bam stats for normal bam"
shell:
"""
samtools stats -@ {threads} {input.bam} > {output}
"""
rule kallisto_quant:
input:
R1 = join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R1.fastq.gz"),
R2 = join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R2.fastq.gz")
output:
abundance_h5 = join(snakemake_dir, "kallisto_out/normal/{sample}/abundance.h5"),
abundance_tsv = join(snakemake_dir, "kallisto_out/normal/{sample}/abundance.tsv"),
run_info = join(snakemake_dir, "kallisto_out/normal/{sample}/run_info.json")
params:
kallisto_index = join(snakemake_dir, "references/kallisto.index"),
out_dir = join(snakemake_dir, "kallisto_out/normal/{sample}")
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(6)
message:
"quantifying with kallisto"
shell:
"""
kallisto quant --bias -b 100 -i {params.kallisto_index} -t {threads} -o {params.out_dir} {input.R1} {input.R2}
"""
rule junction_count_normal:
input:
bam = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam"),
bai = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam.bai")
output:
join(snakemake_dir, "junction_counts/normal/{sample}_junctions.out")
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(6)
message:
"Counting junctions"
shell:
"""
regtools junctions extract -a 8 -m 50 -M 500000 {input.bam} -o {output} -s XS
"""
rule junction_count_dedup:
input:
bam = join(snakemake_dir, "bam/{sample}_duplicate_removed.bam"),
bai = join(snakemake_dir, "bam/{sample}_duplicate_removed.bam.bai")
output:
join(snakemake_dir, "junction_counts/dedup/{sample}_junctions.out")
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(6)
message:
"Counting junctions deduped"
shell:
"""
regtools junctions extract -a 8 -m 50 -M 500000 {input.bam} -o {output} -s XS
"""
rule rnaseqc_normal:
input:
collapsed_gtf = join(snakemake_dir, "references/gencode.v43.basic.annotation_collapsed.gtf"),
bam = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam"),
bai = join(snakemake_dir, "bam/{sample}_Aligned.sortedByCoord.out.bam.bai")
output:
temp(join(snakemake_dir, "bam_stats/normal/{sample}.exon_reads.gct")),
temp(join(snakemake_dir, "bam_stats/normal/{sample}.gene_fragments.gct")),
temp(join(snakemake_dir, "bam_stats/normal/{sample}.gene_reads.gct")),
temp(join(snakemake_dir, "bam_stats/normal/{sample}.gene_tpm.gct")),
join(snakemake_dir, "bam_stats/normal/{sample}.metrics.tsv")
params:
out_dir = join(snakemake_dir, "bam_stats/normal/"),
sample_id = "{sample}"
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(2)
message:
"running rnaseqc normal samples"
shell:
"""
rnaseqc -s {params.sample_id} {input.collapsed_gtf} {input.bam} {params.out_dir}
"""
rule rnaseqc_dedup:
input:
collapsed_gtf = join(snakemake_dir, "references/gencode.v43.basic.annotation_collapsed.gtf"),
bam = join(snakemake_dir, "bam/{sample}_duplicate_removed.bam"),
bai = join(snakemake_dir, "bam/{sample}_duplicate_removed.bam.bai")
output:
temp(join(snakemake_dir, "bam_stats/deduped/{sample}.exon_reads.gct")),
temp(join(snakemake_dir, "bam_stats/deduped/{sample}.gene_fragments.gct")),
temp(join(snakemake_dir, "bam_stats/deduped/{sample}.gene_reads.gct")),
temp(join(snakemake_dir, "bam_stats/deduped/{sample}.gene_tpm.gct")),
join(snakemake_dir, "bam_stats/deduped/{sample}.metrics.tsv")
params:
out_dir = join(snakemake_dir, "bam_stats/deduped/"),
sample_id = "{sample}"
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(2)
message:
"running rnaseqc deduped samples"
shell:
"""
rnaseqc -s {params.sample_id} {input.collapsed_gtf} {input.bam} {params.out_dir}
"""
rule labrat_quant:
input:
R1 = join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R1.fastq.gz"),
R2 = join(snakemake_dir, "trimmed_fastq/{sample}_trimmed_R2.fastq.gz")
output:
join(snakemake_dir, "labrat_quant/{sample}/quant.sf")
params:
TF_ref = join(snakemake_dir, "references/TFseqs.fasta"),
out_dir = join(snakemake_dir, "labrat_quant/{sample}")
threads:
8
resources:
mem = scheduler_memory(20),
time = scheduler_time_hours(2)
shell:
"""
LABRAT.py --mode runSalmon --librarytype RNAseq --reads1 {input.R1} --reads2 {input.R2} --txfasta {params.TF_ref} --samplename {params.out_dir} --threads {threads}
"""