-
Notifications
You must be signed in to change notification settings - Fork 12
/
Snakefile_fastq_singlelane
executable file
·322 lines (288 loc) · 11.6 KB
/
Snakefile_fastq_singlelane
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
include:
'configs/config_HuR.mouse.py'
workdir: OUT_DIR
from itertools import chain, combinations
from os.path import join
import glob
import re
SAMPLES = glob.glob('{}**/*.fastq.gz'.format(RAWDATA_DIR), recursive=True)
SAMPLES = [sample.replace('{}/'.format(RAWDATA_DIR),'').replace('.fastq.gz','') for sample in SAMPLES]
SAMPLES = [('_').join(sample.split('_')[:-2]) for sample in SAMPLES]
PLOT_PREFIXES = combinations(SAMPLES, 2)
PLOT_PREFIXES_1 = [x[0] for x in PLOT_PREFIXES]
PLOT_PREFIXES_2 = [x[1] for x in PLOT_PREFIXES]
rule all:
input:
STAR_INDEX,
expand('qc/{sample}_R1_001_fastqc.html', sample=SAMPLES),
expand('qc/{sample}_R2_001_fastqc.html', sample=SAMPLES),
expand('mapped/counts_strict/{sample}.counts.tsv', sample=SAMPLES),
expand('mapped/plots/tpm_scatter/{sample1}_VS_{sample2}.png', sample1=PLOT_PREFIXES_1, sample2=PLOT_PREFIXES_2),
#expand('mapped/integenic_reads/{sample}.intergenic.bam', sample=SAMPLES),
#expand('mapped/fpkm/{sample}.FPKM.xls', sample=SAMPLES),
'mapped/tpm/HTSeq/masterTPM.tsv',
'mapped/tpm/featureCounts/masterTPM.tsv',
'mapped/DE_analysis/HTSeq/'+GENOME_BUILD+'.HTSeq.DESeq2.all.tsv',
'mapped/DE_analysis/featureCounts/'+GENOME_BUILD+'.featureCounts.DESeq2.sig.tsv',
'multiqc_report/multiqc_report.html',
rule create_index:
input:
GENOME_FASTA,
GTF
output: STAR_INDEX
threads: 16
shell:
r'''mkdir -p {output} && STAR --runThreadN 16\
--runMode genomeGenerate \
--genomeDir {output} \
--genomeFastaFiles {input[0]}\
--sjdbGTFfile {input[1]}'''
rule perform_qc:
input:
R1=RAWDATA_DIR+'/{sample}_R1_001.fastq.gz',
R2=RAWDATA_DIR+'/{sample}_R2_001.fastq.gz'
params:
out_dir = 'qc'
output:
'qc/{sample}_R1_001_fastqc.html',
'qc/{sample}_R1_001_fastqc.zip',
'qc/{sample}_R2_001_fastqc.html',
'qc/{sample}_R2_001_fastqc.zip',
shell:
r'''
fastqc -o {params.out_dir} -f fastq {input.R1} {input.R2}
'''
rule perfom_trimming:
input:
R1=RAWDATA_DIR+'/{sample}_R1_001.fastq.gz',
R2=RAWDATA_DIR+'/{sample}_R2_001.fastq.gz'
params:
out_dir='preprocessed/{sample}',
phred_cutoff=5
output:
'preprocessed/{sample}/{sample}_R1_001_val_1.fq.gz',
'preprocessed/{sample}/{sample}_R2_001_val_2.fq.gz',
shell:
r'''
trim_galore --paired -o {params.out_dir} -q {params.phred_cutoff} {input.R1} {input.R2}
'''
"""
rule map_star:
input:
R1='preprocessed/{sample}/{sample}_R1_001_val_1.fq.gz',
R2='preprocessed/{sample}/{sample}_R2_001_val_2.fq.gz',
index=STAR_INDEX
output: 'mapped/bams/{sample}.bam'
params:
prefix = 'mapped/bams/{sample}',
unmapped = 'unmapped/fastq/{sample}',
starlogs = 'mapped/starlogs'
threads: 16
shell:
r'''
STAR --runThreadN {threads}\
--genomeDir {input.index}\
--outFileNamePrefix {params.prefix} --readFilesIn {input.R1} {input.R2}\
--outSAMtype BAM SortedByCoordinate\
--outFilterMatchNmin 50\
--outFilterMismatchNmax 100\
--readFilesCommand zcat\
--outReadsUnmapped {params.unmapped} && mv {params.prefix}Aligned.sortedByCoord.out.bam {output} && mkdir -p {params.starlogs} && mv {params.prefix}Log.final.out {params.prefix}Log.out {params.prefix}Log.progress.out {params.starlogs}
'''
"""
rule map_starlong:
input:
R1='preprocessed/{sample}/{sample}_R1_001_val_1.fq.gz',
R2='preprocessed/{sample}/{sample}_R2_001_val_2.fq.gz',
index=STAR_INDEX
output: 'mapped/bams/{sample}.bam'
params:
prefix = 'mapped/bams/{sample}',
unmapped = 'unmapped/fastq/{sample}',
starlogs = 'mapped/starlogs'
threads: 16
shell:
r'''
STARlong --runThreadN {threads}\
--genomeDir {input.index}\
--outFileNamePrefix {params.prefix} --readFilesIn {input.R1} {input.R2}\
--outSAMtype BAM SortedByCoordinate\
--readFilesCommand zcat\
--outReadsUnmapped {params.unmapped}\
--outFilterMultimapScoreRange 20\
--outFilterScoreMinOverLread 0\
--outFilterMatchNminOverLread 0.66\
--outFilterMismatchNmax 100\
--winAnchorMultimapNmax 200\
--seedPerReadNmax 100000 --seedPerWindowNmax 100\
&& mv {params.prefix}Aligned.sortedByCoord.out.bam {output} &&\
mkdir -p {params.starlogs} && mv {params.prefix}Log.final.out\
{params.prefix}Log.out {params.prefix}Log.progress.out {params.starlogs}
'''
rule sort_by_name:
input: 'mapped/bams/{sample}.bam'
output: 'mapped/bams/{sample}.sortedByName.bam'
shell:
r'''
samtools sort -on {input} -T /tmp/ -o {output}
'''
rule count:
input: 'mapped/bams/{sample}.sortedByName.bam'
params:
annotation=GTF,
phred_cutoff=5
output: 'mapped/counts_strict/{sample}.counts.tsv'
shell:
r'''
source activate clipseq2 && htseq-count --order=name --format=bam --mode=intersection-strict --stranded=no --minaqual={params.phred_cutoff} --type=exon --idattr=gene_id {input} {params.annotation} > {output}
'''
rule format_counts:
input: 'mapped/counts_strict/{sample}.counts.tsv'
output: 'mapped/counts_strict/{sample}.counts.noversion.tsv'
shell:
r'''
cat {input} | sed -E 's/\.[0-9]+//' > {output}
'''
rule featurecounts:
input: expand('mapped/bams/{sample}.sortedByName.bam', sample=set(SAMPLES))
params:
annotation=GTF
output: 'mapped/featureCounts/fcounts.tsv'
threads: 16
shell:
r'''featureCounts -a {params.annotation} -o {output} -t exon -g gene_id -Q 4 -T {threads} {input}'''
rule format_fcounts:
input: 'mapped/featureCounts/fcounts.tsv'
output: 'mapped/featureCounts/fcounts.noversion.tsv'
shell:
r'''
cat {input} | sed -E 's/\.[0-9]+//' > {output}
'''
output: 'mapped/featureCounts/fcounts.noversion.tsv'
rule run_deseq:
input: expand('mapped/counts_strict/{sample}.counts.noversion.tsv', sample=SAMPLES)
output:
'mapped/DE_analysis/HTSeq/'+GENOME_BUILD+'.HTSeq.DESeq2.all.tsv',
'mapped/DE_analysis/HTSeq/'+GENOME_BUILD+'.HTSeq.DESeq2.sig.tsv'
params:
basedir = 'mapped/counts_strict',
inprefix = 'counts.noversion',
gene_annotations = GENE_NAMES,
outprefix = 'mapped/DE_analysis/HTSeq/'+GENOME_BUILD+'.HTSeq'
shell:
r'''
Rscript {SRC_DIR}/do_DE_analysis.R --basedir={params.basedir} \
--gene_annotations={params.gene_annotations} \
--design_file={DESIGN_FILE} \
--outprefix={params.outprefix} \
--inprefix={params.inprefix}
'''
rule run_deseq_featureCounts:
input: 'mapped/featureCounts/fcounts.noversion.tsv'
output:
'mapped/DE_analysis/featureCounts/'+GENOME_BUILD+'.featureCounts.DESeq2.all.tsv',
'mapped/DE_analysis/featureCounts/'+GENOME_BUILD+'.featureCounts.DESeq2.sig.tsv'
params:
gene_annotations = GENE_NAMES,
outprefix = 'mapped/DE_analysis/featureCounts/'+GENOME_BUILD+'.featureCounts'
shell:
r'''
Rscript {SRC_DIR}/do_DE_analysis_featureCounts.R --counts={input} \
--gene_annotations={params.gene_annotations} \
--design_file={DESIGN_FILE} \
--outprefix={params.outprefix}
'''
rule run_picardmetrics:
input: 'mapped/bams/{sample}.bam'
output: 'mapped/bam_metrics/{sample}.metrics'
shell:
r'''
picard CollectInsertSizeMetrics I={input} H={output}.insertsize.pdf O={output}
'''
rule create_insertsize_tsv:
input: 'mapped/bam_metrics/{sample}.metrics'
output: 'mapped/bam_metrics/{sample}.insertsizes.tsv'
shell:
r'''
python {SRC_DIR}/collect_picard_metrics.py {input} {output}
'''
rule counts_to_tpm:
input:
count = expand('mapped/counts_strict/{sample}.counts.noversion.tsv', sample=SAMPLES),
insert_size = expand('mapped/bam_metrics/{sample}.insertsizes.tsv', sample=SAMPLES),
output: 'mapped/tpm/HTSeq/masterTPM.tsv'
params:
gene_lengths=GENE_LENGTHS,
name=expand('{sample}', sample=set(SAMPLES)),
outprefix='mapped/tpm/HTSeq',
gene_map=GENE_NAMES
run:
counts_input = (',').join(input.count)
sizes_input = (',').join(input.insert_size)
names = (',').join(params.name)
shell('Rscript {SRC_DIR}/counts_to_tpm.R --counts={counts_input} --insert_sizes={sizes_input} --gene_lengths={params.gene_lengths} --inprefix={names} --gene_map={params.gene_map} --outprefix={params.outprefix}')
rule featurecounts_to_tpm:
input:
insert_size = expand('mapped/bam_metrics/{sample}.insertsizes.tsv', sample=set(SAMPLES)),
fcounts = 'mapped/featureCounts/fcounts.noversion.tsv'
output: 'mapped/tpm/featureCounts/masterTPM.tsv'
params:
gene_lengths=GENE_LENGTHS,
name=expand('{sample}', sample=set(SAMPLES)),
outprefix='mapped/tpm/featureCounts',
gene_map=GENE_NAMES
run:
sizes_input = (',').join(input.insert_size)
names = (',').join(params.name)
shell('Rscript {SRC_DIR}/featurecounts_to_tpm.R --counts={input.fcounts} --insert_sizes={sizes_input} --gene_lengths={params.gene_lengths} --inprefix={names} --gene_map={params.gene_map} --outprefix={params.outprefix}')
rule plot_tpm:
input: 'mapped/tpm/HTSeq/masterTPM.tsv'
output: 'mapped/plots/tpm_scatter/{sample1}_VS_{sample2}.png'
params:
prefix = 'mapped/plots/tpm_scatter/'
shell:
r'''
python {SRC_DIR}/plot_tpm_scatter.py --master {input} --outprefix {params.prefix}
'''
rule perform_qualimap_qc:
input: 'mapped/bams/{sample}.bam',
output: 'mapped/post_mapping_qualimap/{sample}/qualimapReport.html',
params:
outdir='mapped/post_mapping_qualimap/{sample}',
gtf=GTF
shell:
r'''
qualimap rnaseq -bam {input} -gtf {params.gtf} --outdir {params.outdir} --java-mem-size=8G
'''
rule get_duplication_estimate:
input: 'mapped/bams/{sample}.bam'
output: 'mapped/post_mapping_deduplication/{sample}/output.DupRate_plot.r'
params:
outprefix='mapped/post_mapping_deduplication/{sample}/output'
shell:
r'''
source activate clipseq2 && read_duplication.py -i {input} -o {params.outprefix}
'''
rule run_multiqc:
input:
expand('qc/{sample_name}_R1_001_fastqc.html', sample_name=SAMPLES),
expand('qc/{sample_name}_R2_001_fastqc.html', sample_name=SAMPLES),
expand('mapped/post_mapping_deduplication/{sample}/output.DupRate_plot.r', sample=SAMPLES),
expand('mapped/bam_metrics/{sample}.metrics', sample=SAMPLES),
expand('mapped/post_mapping_qualimap/{sample}/qualimapReport.html', sample=SAMPLES),
expand('mapped/bams/{sample}.bam', sample=SAMPLES),
'mapped/DE_analysis/HTSeq/'+GENOME_BUILD+'.HTSeq.DESeq2.sig.tsv',
'mapped/DE_analysis/featureCounts/'+GENOME_BUILD+'.featureCounts.DESeq2.sig.tsv',
output:
'multiqc_report/multiqc_report.html'
shell:
'export LC_ALL=en_US.UTF-8 && multiqc -f --outdir multiqc_report .'
rule calc_fpkm:
input: 'mapped/bams/{sample}.bam'
output: 'mapped/fpkm/{sample}.FPKM.xls'
params:
prefix='mapped/fpkm/{sample}'
shell:
r'''
source activate clipseq2 && FPKM_count.py -i {input} -o {params.prefix} -r {GENE_BED}
'''