-
Notifications
You must be signed in to change notification settings - Fork 4
/
Snakefile
401 lines (357 loc) · 16.8 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
shell.prefix('export PS1=""; ml anaconda3; CONDA_BASE=$(conda info --base); source $CONDA_BASE/etc/profile.d/conda.sh; ml purge;')
#shell.prefix('export PS1="";source activate isoseq-pipeline;ml R/3.6.0;')
import pandas as pd
import glob
metadata = config['metadata']
metaDF = pd.read_csv(metadata, sep = '\t')
samples = metaDF['sample']
#print(samples)
rawFolder = config['rawFolder']
dataCode = config['dataCode']
referenceFa = config['referenceFa']
referenceGTF = config['referenceGTF']
primers = "reference/NEB_primers_01_2019.fa"
# short read junctions
junctionFolder = "/sc/hydra/projects/ad-omics/microglia_isoseq/short_read_junctions"
junctions = glob.glob(junctionFolder + "/*SJ.out.tab")
junctionArgs = " ".join(["-c " + f for f in junctions])
rule all:
input:
"TAMA_merge/tama_merge_config.txt",
expand("{sample}/TAMA/{sample}.bed", sample = samples),
# expand( "{sample}/cupcake/{sample}.cupcake.abundance.txt", sample = samples),
# expand( "{sample}/SQANTI2/{sample}.{method}_classification.txt", sample = samples, method = ["stringtie","cupcake"]),
expand( "{sample}/qc/{sample}.metrics.tsv", sample = samples),
"all_samples/SQANTI2/all_samples.chained_classification.txt",
# expand( "{sample}/stringtie/{sample}.stringtie.gtf", sample = samples)
"all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered_lite_classification.txt",
"all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered.sorted.gff.gz.tbi"
# expand(fastqFolder + "{sample}.classification.txt", sample = samples),
# "multiqc/multiqc_report.html",
#expand( "rnaseqc/{samp}.metrics.tsv", samp = samples),
#outFolder + "multiqc/multiqc_report.html",
#expand(outFolder + "rnaseqc/{samp}.metrics.tsv", samp = samples),
#reference + ".mmi",
#expand( "sorted/{samp}_sorted.bam", samp = samples)
# #expand("{outFolder}{samples}_sorted.bam", samples = samples, outFolder = outFolder)
# CCS - call circular consensus sequences from the SMRTcell movies
# todo: add chunking for paralellisation
#Input Filter Options:
# --min-passes INT Minimum number of full-length subreads required to generate CCS for a ZMW. [3]
# --min-snr FLOAT Minimum SNR of subreads to use for generating CCS [2.5]
#Draft Filter Options:
# --min-length INT Minimum draft length before polishing. [10]
# --max-length INT Maximum draft length before polishing. [50000]
#Output Filter Options:
# --min-rq FLOAT Minimum predicted accuracy in [0, 1]. [0.99]
rule CCS:
input:
rawFolder + "{sample}/{sample}.subreadset.xml"
output:
bam = "{sample}/isoseq3-ccs/{sample}.ccs.bam",
report = "{sample}/isoseq3-ccs/{sample}.ccs.report.txt"
shell:
"ccs -j 0 {input} {output.bam} --report-file {output.report}"
# primer removal and demultiplexing
rule isoseq_lima:
input:
"{sample}/isoseq3-ccs/{sample}.ccs.bam",
output:
"{sample}/isoseq3-lima/{sample}.fl.bam"
shell:
"lima --isoseq --different --min-passes 1 --split-bam-named --dump-clips --dump-removed -j 0 {input} {primers} {output}"
# trimming of polya tails and removal of concatemers
rule isoseq_refine:
input:
"{sample}/isoseq3-lima/{sample}.fl.bam"
output:
"{sample}/isoseq3-refine/{sample}.flnc.bam"
shell:
"isoseq3 refine --require-polya {input} {primers} {output}"
# polish
rule isoseq_cluster:
input:
"{sample}/isoseq3-refine/{sample}.flnc.bam"
params:
fasta_gz = "{sample}/isoseq3-cluster/{sample}.polished.hq.fasta.gz"
output:
fasta = "{sample}/isoseq3-cluster/{sample}.polished.hq.fasta",
report = "{sample}/isoseq3-cluster/{sample}.polished.cluster_report.csv"
shell:
"isoseq3 cluster --verbose --use-qvs -j 0 {input} {output.fasta};"
"gunzip {params.fasta_gz}"
#### MINIMAP
rule minimapIndex:
input: referenceFa + ".fa"
output: referenceFa + ".mmi"
shell:
"minimap2 -d {output} {input}"
rule minimap:
input:
fastq = "{sample}/isoseq3-cluster/{sample}.polished.hq.fasta",
ref = referenceFa + ".fa",
index = referenceFa + ".mmi"
params:
"-ax splice -t 4 -uf --secondary=no -C5"
#config['minimapParams']
output:
sam = "{sample}/minimap/{sample}.hq.sam",
sam_sorted = "{sample}/minimap/{sample}.hq.sorted.sam"
shell:
"minimap2 {params} {input.index} {input.fastq} > {output.sam};"
"sort -k 3,3 -k 4,4n {output.sam} > {output.sam_sorted}"
rule samtools:
input: "{sample}/minimap/{sample}.hq.sam"
output:
bam = "{sample}/minimap/{sample}_sorted.bam",
bai = "{sample}/minimap/{sample}_sorted.bam.bai",
flagstat = "{sample}/qc/{sample}.flagstat.txt",
idxstat = "{sample}/qc/{sample}.idxstat.txt"
shell:
"samtools view -bh {input} | samtools sort > {output.bam}; "
"samtools index {output.bam};"
"samtools flagstat {output.bam} > {output.flagstat};"
"samtools idxstats {output.bam} > {output.idxstat} "
## TAMA tools
rule TAMA_collapse:
input:
sam_sorted = "{sample}/minimap/{sample}.hq.sorted.sam",
genome = referenceFa + ".fa"
output:
bed = "{sample}/TAMA/{sample}.bed",
txt = "{sample}/TAMA/{sample}_read.txt"
params:
script = "/sc/hydra/projects/ad-omics/data/software/tama/tama_collapse.py"
shell:
'conda activate py2bio;'
'python {params.script} -s {input.sam_sorted} -f {input.genome} -p {wildcards.sample}/TAMA/{wildcards.sample} -x no_cap'
#annotation_capped.bed capped 1,1,1 caplib
#annotation_nocap.bed no_cap 2,1,1 nocaplib
rule create_TAMA_merge_config:
output:
config = "TAMA_merge/tama_merge_config.txt"
run:
tamaMergeRows = []
for s in samples:
entry = s + "/TAMA/" + s + ".bed\tno_cap\t1,1,1\t" + s
tamaMergeRows.append(entry)
with open(output.config, 'w') as filehandle:
for listitem in tamaMergeRows:
filehandle.write('%s\n' % listitem)
## Cupcake Tools
# collapse redundant reads
# deal with 5' truncated reads
# filter away
rule cupcake_collapse:
input:
fasta = "{sample}/isoseq3-cluster/{sample}.polished.hq.fasta",
sam_sorted = "{sample}/minimap/{sample}.hq.sorted.sam",
cluster_report = "{sample}/isoseq3-cluster/{sample}.polished.cluster_report.csv"
output:
gff = "{sample}/cupcake/{sample}.cupcake.collapsed.filtered.gff",
fasta = "{sample}/cupcake/{sample}.cupcake.collapsed.rep.fa",
group = "{sample}/cupcake/{sample}.cupcake.collapsed.group.txt",
stat = "{sample}/cupcake/{sample}.cupcake.collapsed.read_stat.txt",
count = "{sample}/cupcake/{sample}.cupcake.collapsed.abundance.txt",
fasta2 = "{sample}/cupcake/{sample}.cupcake.collapsed.filtered.rep.fa",
chain_gff = "{sample}/cupcake/chain/cupcake.collapsed.filtered.gff",
chain_group = "{sample}/cupcake/chain/cupcake.collapsed.group.txt",
chain_count = "{sample}/cupcake/chain/cupcake.collapsed.abundance.txt"
params:
prefix = "{sample}/cupcake/{sample}.cupcake",
prefix_collapsed = "{sample}/cupcake/{sample}.cupcake.collapsed"
shell:
"collapse_isoforms_by_sam.py --input {input.fasta} "
"-s {input.sam_sorted} --dun-merge-5-shorter -o {params.prefix};"
# get abundances
"get_abundance_post_collapse.py {params.prefix}.collapsed {input.cluster_report};"
# filter 5' truncated transcripts
"filter_away_subset.py {params.prefix}.collapsed ; "
# collapse again to create groups file
#"collapse_isoforms_by_sam.py --input {output.fasta2} "
#"-s {input.sam_sorted} --dun-merge-5-shorter -o {params.prefix}.collapsed.filtered;"
# get abundance counts
#"get_abundance_post_collapse.py {params.prefix}.collapsed.filtered.collapsed {input.cluster_report};"
# copy files to chain directory - omit sample name from file name
"cp {output.gff} {output.chain_gff};"
"cp {output.group} {output.chain_group};"
"cp {output.count} {output.chain_count}"
chainFileRows = ["GROUP_FILENAME=cupcake.collapsed.group.txt", "COUNT_FILENAME=cupcake.collapsed.abundance.txt", "GFF_FILENAME=cupcake.collapsed.filtered.gff"]
rule create_chain_config:
output:
config = "chain.config.txt"
run:
chainSampleRows = []
for i in samples:
l = "SAMPLE=" + i + ";" + i + "/cupcake/chain/"
chainSampleRows.append(l)
allRows = chainSampleRows + [""] + chainFileRows
with open(output.config, 'w') as filehandle:
for listitem in allRows:
filehandle.write('%s\n' % listitem)
rule chain_samples:
input:
chain_gff = expand("{sample}/cupcake/chain/cupcake.collapsed.filtered.gff", sample = samples),
chain_config = "chain.config.txt"
output:
gff = "all_samples/all_samples.chained.gff"
shell:
"chain_samples.py {input.chain_config} count_fl; "
"if [ ! -d all_samples/ ]; then mkdir all_samples; fi ;"
"mv all_samples.chained* all_samples/;"
"rm tmp* "
#### SQANTI
rule SQANTI:
input:
#fasta = "{sample}/isoseq3-cluster/{sample}.polished.hq.fasta"
gff = "{sample}/{method}/{sample}.{method}.collapsed.filtered.gff"
output:
report = "{sample}/SQANTI2/{sample}.{method}_classification.txt"
params:
sample = "{sample}.{method}",
outDir = "{sample}/SQANTI2/",
python = "/sc/arion/work/$USER/conda/envs/isoseq-pipeline/bin/python",
sqantiPath= "/sc/arion/projects/ad-omics/data/software/SQANTI2",
nCores = 12,
#abundance = "{sample}/cupcake/{sample}.{method}.abundance.txt",
gtf = referenceGTF,
genome = referenceFa + ".fa",
junctions = "\'" + junctionFolder + "/*SJ.out.tab\'" ,
intropolis = "/sc/hydra/projects/ad-omics/data/references/hg38_reference/SQANTI2/intropolis.v1.hg19_with_liftover_to_hg38.tsv.min_count_10.modified",
cage = "/sc/hydra/projects/ad-omics/data/references/hg38_reference/SQANTI2/hg38.cage_peak_phase1and2combined_coord.bed",
polya = "/sc/hydra/projects/ad-omics/data/references/hg38_reference/SQANTI2/human.polyA.list.txt"
shell:
#"export PATH=/sc/hydra/projects/ad-omics/data/software/UCSC/:$PATH;"
#"module unload gcc;ml R/3.6.0; "
"export PYTHONPATH=$PYTHONPATH:/hpc/users/humphj04/pipelines/cDNA_Cupcake/sequence/;"
"{params.python} {params.sqantiPath}/sqanti_qc2.py -t {params.nCores} --aligner_choice=minimap2"
" --dir {params.outDir} "
" --out {params.sample} "
" -c {params.junctions} "
" --cage_peak {params.cage} --polyA_motif_list {params.polya} -c {params.intropolis}"
#" --fl_count {params.abundance}"
" --gtf {input.gff} "
" {params.gtf} {params.genome} "
# sqanti filter - only works with Cupcake output for now
rule SQUANTI_filter:
input:
classification = "{sample}/SQANTI2/{sample}.{method}_classification.txt",
fasta = "{sample}/SQANTI2/{sample}.cupcake.collapsed_corrected.fasta",
#sam = "{sample}/cupcake/{sample}.renamed_corrected.sam",
gtf = "{sample}/SQANTI2/{sample}.cupcake.collapsed_corrected.gtf",
faa = "{sample}/SQANTI2/{sample}.cupcake.collapsed_corrected.faa"
output:
"{sample}/SQANTI2/{sample}_classification.filtered_lite_classification.txt"
params:
python = "/sc/hydra/work/$USER/conda/envs/isoseq-pipeline/bin/python",
sqantiPath= "/sc/hydra/projects/ad-omics/data/software/SQANTI2"
shell:
"{params.python} {params.sqantiPath}/sqanti_filter2.py "
" --faa {input.faa} " #--sam {input.sam} "
" {input.classification} {input.fasta} {input.gtf} "
rule SQANTI_all:
input:
gff = "all_samples/all_samples.chained.gff"
output:
report = "all_samples/SQANTI2/all_samples.chained_classification.txt"
params:
sample = "all_samples.chained",
outDir = "all_samples/SQANTI2/",
python = "/sc/hydra/work/$USER/conda/envs/isoseq-pipeline/bin/python",
sqantiPath= "/sc/hydra/projects/ad-omics/data/software/SQANTI2",
nCores = 12,
junctions = "\'" + junctionFolder + "/*SJ.out.tab\'" ,
abundance = "all_samples/all_samples.chained_count.txt",
gtf = referenceGTF,
genome = referenceFa + ".fa",
intropolis = "/sc/hydra/projects/ad-omics/data/references/hg38_reference/SQANTI2/intropolis.v1.hg19_with_liftover_to_hg38.tsv.min_count_10.modified",
cage = "/sc/hydra/projects/ad-omics/data/references/hg38_reference/SQANTI2/hg38.cage_peak_phase1and2combined_coord.bed",
polya = "/sc/hydra/projects/ad-omics/data/references/hg38_reference/SQANTI2/human.polyA.list.txt"
shell:
"export PYTHONPATH=$PYTHONPATH:/hpc/users/humphj04/pipelines/cDNA_Cupcake/sequence/;"
"{params.python} {params.sqantiPath}/sqanti_qc2.py -t {params.nCores} --aligner_choice=minimap2"
" --dir {params.outDir} "
" --out {params.sample} "
" -c {params.junctions} "
" --cage_peak {params.cage} --polyA_motif_list {params.polya} "
"--skipORF " # skipping ORF finding for now as it's very slow
#"-c {params.intropolis}"
" --fl_count {params.abundance}"
" --gtf {input.gff} "
" {params.gtf} {params.genome} "
rule SQUANTI_all_filter:
input:
classification = "all_samples/SQANTI2/all_samples.chained_classification.txt",
fasta = "all_samples/SQANTI2/all_samples.chained_corrected.fasta",
#sam = "{sample}/cupcake/{sample}.renamed_corrected.sam",
gtf = "all_samples/SQANTI2/all_samples.chained_corrected.gtf"
#faa = "all_samples/SQANTI2/all_samples.chained_corrected.faa"
output:
"all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered_lite_classification.txt",
"all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered_lite.gtf"
params:
python = "/sc/arion/work/$USER/conda/envs/isoseq-pipeline/bin/python",
sqantiPath= "/sc/arion/projects/ad-omics/data/software/SQANTI2"
shell:
"{params.python} {params.sqantiPath}/sqanti_filter2.py "
#" --faa {input.faa} " #--sam {input.sam} "
" {input.classification} {input.fasta} {input.gtf} ;"
" mv *png all_samples/SQANTI_filtered/"
#### STRINGTIE2
# assemble minimap-aligned reads into transcripts - alternative to cupcake_collapse
# some monoexon transcripts are given strand of "." - remove them in bioawk
rule stringtie:
input:
bam = "{sample}/minimap/{sample}_sorted.bam"
params:
gtf = referenceGTF,
stringtiePath = "/sc/hydra/projects/ad-omics/data/software/stringtie"
output:
gtf = "{sample}/stringtie/{sample}.stringtie.collapsed.gtf",
gff = "{sample}/stringtie/{sample}.stringtie.collapsed.gff"
shell:
"ml bioawk; "
"{params.stringtiePath}/stringtie --rf -G {params.gtf} -L -o {output.gtf} {input.bam};"
"gffread -E {output.gtf} -o- | awk \'$7 != \".\"\' > {output.gff}"
#### MISC
rule collapseAnnotation:
input: referenceGTF
output: referenceGTF + ".genes"
params: script = "scripts/collapse_annotation.py"
shell: "/sc/arion/work/humphj04/conda/envs/isoseq-pipeline/bin/python {params.script} {input} {output}"
rule rnaseqc:
input:
geneGTF = referenceGTF + ".genes",
bam = "{sample}/minimap/{sample}_sorted.bam"
params:
out = "{sample}/qc/"
output:
"{sample}/qc/{sample}.metrics.tsv"
shell:
"ml rnaseqc;"
"rnaseqc {input.geneGTF} {input.bam} {params.out} "
" --sample={wildcards.sample} "
" --unpaired --coverage --verbose --mapping-quality 0 --base-mismatch=1000 --detection-threshold=1"
rule multiQC:
input:
"{sample}/qc/{sample}.metrics.tsv",
"{sample}/qc/{sample}.flagstat.txt",
"{sample}/qc/{sample}.idxstat.txt"
output:
"multiqc/multiqc_report.html"
shell:
"export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8;"
"multiqc -f --outdir {outFolder}multiqc/ {outFolder}"
# sort and tabix index final GFF
rule indexGFF:
input:
"all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered_lite.gtf"
output:
gff = "all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered.sorted.gff.gz",
index = "all_samples/SQANTI2_filtered/all_samples.chained_classification.filtered.sorted.gff.gz.tbi"
params:
gff3sort = "/sc/hydra/projects/ad-omics/data/software/gff3sort/gff3sort.pl"
shell:
"{params.gff3sort} {input} | bgzip > {output.gff};"
"ml tabix;"
"tabix {output.gff} "