-
Notifications
You must be signed in to change notification settings - Fork 0
/
hint.nf
862 lines (701 loc) · 17.5 KB
/
hint.nf
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/usr/bin/env nextflow
params.outDir = false
params.biosample = false
params.genewizMap = false
params.fastqDir = false
params.libraryID = false
params.genome = 'hg38'
params.resolutions = false
params.ABresolutions = false
params.HiChIP = false
params.help = false
def helpMessage() {
log.info"""
Usage:
The typical command for running the pipeline is as follows:
epiPipe --biosample bisample1,biosample2,biosample3 --outDir ~/ebs/ref_push/prodEpi/myExpName
or
epiPipe --fastqDir ~/ebs/ref_push/prodEpi/myExpName/fastqs/ --outDir ~/ebs/ref_push/prodEpi/myExpName
Mandatory arguments:
--outDir [pathOfDir] Path of where to publish the data, can be local or an S3 accessible bucket
Input (minimum one of these, can be mixed):
--biosample [str] Comma seperated list of BaseSpace biosamples.
--fastqDir [str] Path to a directoy of fastq files.
--genewizMap [str] CSV file descrbibing the LIMS id to S3 path on GeneWiz bucket. Field: LIMS_ID,LIBRAY_ID,S3_path_R1,S3_path_R2
Restriting to a set of library:
--libraryID [str] Comma seperated list of library prefixes. Need to be used in conjunction with --fastqDir.
Arrowhead parameters:
--resolutions [integers] Comma-seperated list of resolutions in kb for loops finding. Default: [5,10]
--ABresolutions [integers] Comma-seperated list of resolutions in kb. Default: [32,64,128]
References:
--genome [str] Name of the genome to use. Possible choice: hg38, mm10, dm3. Default: hg38.
""".stripIndent()
}
if (params.help){
helpMessage()
exit 0
}
if ( !(params.biosample || params.fastqDir || params.genewizMap) ){
exit 1, "You need to use either --biosample aBaseSpace-biosample or --fastqDir path/to/bam/files or --genewizMap mappingFiles.csv. Use --help to get the full usage."
}
if ( !(params.outDir) ){
exit 1, "--outDir is a required arguments. Use --help to get the full usage."
}
if (params.resolutions){
resolutions = params.resolutions.split(/,/,-1)
} else {
resolutions = [5,10]
}
if (params.ABresolutions){
ABresolutions = params.ABresolutions.toString().split(/,/,-1)
} else {
ABresolutions = [32,64,128]
}
if (!params.genome =~ /hg38|mm10|dm3/){
exit 1, "Only hg38, mm10 and dm3 genomes are currently offered"
} else {
Channel
.fromPath("${HOME}/ebs/genome/nextflow/${params.genome}", checkIfExists: true)
.ifEmpty { exit 1, "BWA index not found: ${params.genome}" }
.set { bwa_index }
Channel
.fromPath("${HOME}/ebs/genome/nextflow/${params.genome}/${params.genome}.fa", checkIfExists: true)
.ifEmpty { exit 1, "Genome not found: ${params.genome}" }
.set { abcomp_genome_ch }
}
if (params.biosample){
Channel
.from(params.biosample)
.splitCsv()
.flatten()
.set { biosample_ch }
} else {
Channel
.empty()
.into { biosample_ch;fastqs_ch }
}
if (params.fastqDir){
if (params.libraryID){
Channel
.from(params.libraryID)
.splitCsv()
.flatten()
.map {id -> file("${params.fastqDir}/${id}_*R1*.fastq.gz")}
.flatten()
.map {R1 ->
def m = R1.name.toString() =~ /(.+)_R1/
def id = m[0][1]
def R2 = file("${params.fastqDir}/${id}*_R2*.fastq.gz")
if (R2.size() > 1) {
return( "R2 has more than one entry")
} else {
return(tuple(id, R1, R2[0]) )
}
}
.set{fastqDir_ch}
} else {
Channel
.fromFilePairs("${params.fastqDir}/*_{R1,R2}*.fastq.gz",
chekIfExists: true,
flat: true)
.set{fastqDir_ch}
}
} else {
if (params.libraryID){
exit 1, "--libraryID needs to be used in conjuction with --fastqDir. Use --help to get details"
} else {
Channel
.empty()
.set{fastqDir_ch}
}
}
if (params.genewizMap) {
def i = 0
Channel
.fromPath(params.genewizMap)
.splitCsv()
.map{row ->
i=i+1
tuple(row[0]+"-rep"+i,file(row[2]),file(row[3]))
}
.into{keyMapping_ch;genewiz_ch}
process copy_mapping {
cpus 1
memory "2 GB"
container 'mblanche/basespace-cli'
publishDir "${params.outDir}",
mode: 'copy'
input:
val mapping from keyMapping_ch
.map { l = [it[0],it[1].toString(),it[2].toString()]
l.join(",")
}
.flatten()
.collect()
output:
path ("*.csv") into outMapping
script:
"""
echo "id,R1_fastq,R2_fastq" > keyMapping.csv
for l in ${mapping}; do
echo \${l::-1} >> keyMapping.csv
done
"""
}
process copy_manifest {
cpus 1
memory "2 GB"
container 'mblanche/basespace-cli'
publishDir "${params.outDir}",
mode: 'copy'
input:
path manifest from Channel.fromPath(params.genewizMap)
output:
path manifest into outManifest
script:
"""
cat ${manifest}
"""
}
}else{
Channel
.empty()
.set{genewiz_ch}
}
if (params.biosample){
process get_bs_files {
cpus 1
memory '1G'
container 'mblanche/basespace-cli'
input:
val bs from biosample_ch
output:
stdout into bs_id_ch
script:
"""
bs biosample content -n ${bs} -F Id -F FilePath -f csv | \
awk 'BEGIN{OFS =","} \
NR == 1 {print "biosample", \$0} \
NR > 1 {print "${bs}", \$0}'
"""
}
process get_bs_dataset {
cpus 1
memory '1G'
container 'mblanche/basespace-cli'
publishDir "${params.outDir}/fastqs",
mode: 'copy',
saveAs: {filename -> filename.endsWith('.error') ? filename : null}
input:
tuple file_name, bs, id from bs_id_ch
.splitCsv(header: true)
.map { row -> tuple(row.FilePath,row.biosample, row.Id )}
.groupTuple()
.map { tuple(it[0], it[1].unique()[0],it[2]) }
output:
path "bs_data_set.csv" optional true into bs_dataset_ch
path "*.error" optional true into error_ch
script:
if (id.size() >1){
"""
echo found more than one identical file names for biosample ${bs} > ${bs}.error
"""
} else {
"""
bs list dataset \
--like-type=illumina.fastq.v1.8 \
--input-biosample=${bs} \
--like-type=illumina.fastq.v1.8 \
-f csv | \
awk 'BEGIN{OFS =","} \
NR == 1 {print "biosample", \$0} \
NR > 1 {print "${bs}", \$0}' > bs_data_set.csv
"""
}
}
process download_bs {
label "movers"
cpus 4
memory '4G'
container 'mblanche/basespace-cli'
queue 'moversQ'
publishDir "${params.outDir}/fastqs",
mode: 'copy'
input:
tuple bs, val(bs_id) from bs_dataset_ch
.splitCsv(header: true)
.unique()
.map{row -> tuple(row.biosample, row.Id)}
output:
tuple bs, file("*_R1*.fastq.gz"), file("*_R2*.fastq.gz") into fastqs_ch
script:
"""
bs download dataset \
--id=${bs_id} \
-o .
"""
}
}
process hint_pre {
tag "_${id}"
cpus 48
memory '48 GB'
container 'suwangbio/hint'
input:
tuple val(id), file(R1s), file(R2s) from fastqs_ch
.mix(fastqDir_ch)
.mix(genewiz_ch)
path index from bwa_index.first()
output:
tuple id, path("*.bam"), path("*.tsv") into pairtools_parse_ch
script:
"""
hint pre -d <(zcat ${R1s|head -n 400000}),<(zcat ${R2s|head -n 400000}) \
-i ${index}/${params.genome} \
--refdir /path/to/refData/hg19 \
-g hg19 \
--informat fastq \
--outformat cooler \
-n test \
-o /path/to/outputdir \
--pairtoolspath /path/to/pairtools \
--samtoolspath /path/to/samtools \
--coolerpath /path/to/cooler\
"""
}
process pairtools_parse {
tag "_${id}"
cpus 14
memory '50 GB'
container 'mblanche/pairtools'
input:
tuple id, path(sam), path(chr_sizes) from pairtools_parse_ch
output:
tuple id, path("*.pairsam.gz") into pairsam_part_ch
script:
"""
pairtools parse \
--min-mapq 40 \
--walks-policy 5unique \
--max-inter-align-gap 30 \
--nproc-in ${task.cpus} --nproc-out ${task.cpus} \
--chroms-path ${chr_sizes} \
--output ${id}.pairsam.gz \
${sam}
"""
}
process pairtools_merge_lane {
tag "_${id}"
cpus 14
memory '50 GB'
container 'mblanche/pairtools'
input:
tuple id, path(sam) from pairsam_part_ch
.map {id, file ->
def key = id.tokenize('_').get(0)
return tuple(key, file)
}
.groupTuple()
output:
tuple id, path("*.pairsam.gz") into pairsam_ch
script:
if (sam.sort().size() >1) {
"""
pairtools merge -o ${id}.pairsam.gz --nproc ${task.cpus} ${sam}
"""
} else {
"""
ln -sf ${sam} ${id}_ML.pairsam.gz
"""
}
}
process pairtools_sort {
tag "_${id}"
cpus 14
memory '100 GB'
container 'mblanche/pairtools'
input:
tuple id, path(sam) from pairsam_ch
output:
tuple id, path("*_sorted.pairsam.gz") into sorted_ps_ch
script:
"""
mkdir -p tmp
pairtools sort --tmpdir ./tmp \
--nproc ${task.cpus} \
--output ${id}_sorted.pairsam.gz \
$sam
"""
}
process pairtools_dedup {
tag "_${id}"
cpus 14
memory '40 GB'
container 'mblanche/pairtools'
publishDir "${params.outDir}/pairtools_stat",
mode: 'copy',
saveAs: {filename -> filename.endsWith('.stats') ? filename : null}
input:
tuple id, path(sam) from sorted_ps_ch
output:
tuple id, path("*_dedup.pairsam.gz") into dedup_ps_ch
tuple id, path("*_unmapped.pairsam.gz") into unmapped_ps_ch
tuple id, path("*_pairtools.stats") into ps_stats_ch
script:
"""
pairtools dedup --nproc-in ${task.cpus} --nproc-out ${task.cpus} \
--mark-dups \
--output-stats ${id}_pairtools.stats \
--output ${id}_dedup.pairsam.gz \
--output-unmapped ${id}_unmapped.pairsam.gz \
${sam}
"""
}
process pairtools_stats_merge {
tag "_${id}"
cpus 1
memory '4 GB'
container 'mblanche/pt-stats'
publishDir "${params.outDir}",
mode: 'copy'
input:
path(stats) from ps_stats_ch
.map{it[1]}
.collect()
output:
path('pairtoolsStats.csv') into merged_stats_ch
script:
"""
pairtoolsStat.sh ${stats} > pairtoolsStats.csv
"""
}
process pairtools_split_dedup {
tag "_${id}"
cpus 14
memory '40 GB'
container 'mblanche/pairtools'
input:
tuple id, path(sam) from dedup_ps_ch
output:
tuple id, path("*.bam") into bam_parts_ch
tuple id, path("*.valid.pairs.gz") into pairs_parts_ch, pairs_parts_ch_test
script:
"""
pairtools split --nproc-in ${task.cpus} --nproc-out ${task.cpus} \
--output-sam ${id}_PT.bam \
--output-pairs ${id}_PT.valid.pairs.gz \
${sam}
"""
}
process merge_bam {
tag "_${id}"
cpus 48
memory '100 GB'
container 'mblanche/bwa-samtools'
input:
tuple id, path(bam_part) from bam_parts_ch
.map {id, file ->
if ( id.contains("-rep") ){
def key = id.tokenize('-rep').get(0)
return tuple(key, file)
} else {
return( tuple(id,file) )
}
}
.groupTuple()
output:
tuple id, path("*.bam") into merged_bam_sort_ch
script:
bam_files = bam_part.sort()
if (bam_files.size() >1) {
"""
samtools merge -@ ${task.cpus} ${id}_MB.bam ${bam_part}
"""
} else {
"""
ln -s ${bam_part} ${id}_MB.bam
"""
}
}
process bam_sort {
tag "bam_sort_${id}"
cpus 48
memory '150 GB'
container 'mblanche/bwa-samtools'
publishDir "${params.outDir}/bam",
mode: 'copy',
pattern: "${id}.bam"
input:
tuple id, path(bam) from merged_bam_sort_ch
output:
tuple id, path("${id}.bam"),path("${id}.bam.bai") into bam_bigwig_ch
script:
"""
samtools sort -m 2G \
-@ ${task.cpus} \
-o ${id}.bam \
${bam}
samtools index -@${task.cpus} ${id}.bam
"""
}
process pairtools_split_unmapped {
tag "_${id}"
cpus 14
memory '40 GB'
container 'mblanche/pairtools'
publishDir "${params.outDir}/unmapped",
mode: 'copy'
input:
tuple id, path(sam) from unmapped_ps_ch
output:
path "*_unmapped.bam" into unmapped_bam_ch
path "*_unmapped.valid.pairs.gz" into unmapped_pairs_ch
script:
"""
pairtools split --nproc-in ${task.cpus} --nproc-out ${task.cpus} \
--output-sam ${id}_unmapped.bam \
--output-pairs ${id}_unmapped.valid.pairs.gz \
${sam}
"""
}
process merge_pairs {
tag "_${id}"
cpus 14
memory '40 GB'
container 'mblanche/pairtools'
publishDir "${params.outDir}/validPairs",
mode: 'copy'
input:
tuple val(id), path(pairs) from pairs_parts_ch
.map {id, file ->
if ( id.contains("-rep") ){
def key = id.tokenize('-rep').get(0)
return tuple(key, file)
} else {
return( tuple(id,file) )
}
}
.groupTuple()
output:
tuple id, path("*.valid.pairs.gz"), path("*.px2") into pairs_chrSize_ch
script:
pair_files = pairs.sort()
if (pair_files.size() >1) {
"""
pairtools merge -o ${id}.valid.pairs.gz --nproc ${task.cpus} ${pairs}
pairix ${id}.valid.pairs.gz
"""
} else {
"""
ln -s ${pairs} ${id}.valid.pairs.gz
pairix ${id}.valid.pairs.gz
"""
}
}
process chr_size {
tag "_${id}"
cpus 4
memory '24 GB'
container 'mblanche/pairtools'
input:
tuple id, path(pairs), path(idx) from pairs_chrSize_ch
output:
tuple id, path(pairs), path(idx), path("*.tsv") into pairs_ch_cooler, pairs_ch_juicer
script:
"""
pairix -H -f ${pairs} \
| awk -v OFS='\t' '/^#chromsize/ {print \$2,\$3}' \
| sort -V -k1,1 \
> chr_size.tsv
"""
}
process cooler_cload {
tag "_${id}"
cpus 48
memory '100 GB'
container 'mblanche/cooler'
input:
tuple id, path(pairs), path(idx), path(chr_sizes) from pairs_ch_cooler
output:
tuple id, path("*.cool") into balance_cooler_ch
script:
"""
cooler cload pairix \
-p ${task.cpus} \
${chr_sizes}:1000 \
${pairs} \
${id}.cool
"""
}
process balance_cooler {
tag "_${id}"
cpus 48
memory '100 GB'
container 'mblanche/cooler'
publishDir "${params.outDir}/coolerFiles",
mode: 'copy'
input:
tuple id, path(cooler) from balance_cooler_ch
output:
tuple id, path(cooler) into zoomify_cooler_ch
script:
"""
cooler balance --force -p ${task.cpus} ${cooler}
"""
}
process cooler_zoomify {
tag "_${id}"
cpus 48
memory '100 GB'
container 'mblanche/cooler'
publishDir "${params.outDir}/coolerFiles",
mode: 'copy'
input:
tuple id, path(cooler) from zoomify_cooler_ch
output:
tuple id, path("*.mcool") into mustache_mcool_ch, abcomp_mcool_ch
script:
"""
cooler zoomify --balance -p ${task.cpus} ${cooler}
"""
}
process bam2bw {
tag "_${id}"
cpus 20
memory '175 GB'
container 'mblanche/r-cov'
publishDir "${params.outDir}/bigwigs",
mode: 'copy'
input:
tuple id, path(bam),path(idx) from bam_bigwig_ch
output:
tuple id, path ("*.bw") into bigwig_out_ch
script:
"""
bam2bw ${bam} ${id}.bw ${task.cpus}
"""
}
process juicer {
tag "_${id}"
cpus 24
memory '150 GB'
container 'mblanche/juicer'
publishDir "${params.outDir}/hicFiles",
mode: 'copy'
input:
tuple id, path(pairs), path(idx), path(chr_sizes) from pairs_ch_juicer
output:
tuple id, path("*.hic") into arrowhead_ch, hiccups_ch
script:
"""
java -Xmx96000m -Djava.awt.headless=true \
-jar /juicer_tools.jar pre \
--threads ${task.cpus} \
-j ${task.cpus} \
-k VC,VC_SQRT,KR,SCALE \
${pairs} \
${id}.hic \
${chr_sizes}
"""
}
process arrowhead {
tag "_${id}"
cpus 12
memory '40 GB'
container "mblanche/juicer"
publishDir "${params.outDir}/arrowHead",
mode: 'copy'
input:
tuple id, path(hic), val(res) from arrowhead_ch
.combine(Channel.from(resolutions))
output:
tuple id, path("${id}_${res}kb") into arrowhead_out_ch
script:
bpRes = res.toInteger() * 1000
"""
mkdir -p ${id}_${res}kb && touch ${id}_${res}kb/${bpRes}_blocks.bedpe
java -Xmx24000m \
-jar /juicer_tools.jar \
arrowhead \
--threads ${task.cpus} \
--ignore-sparsity \
-r ${bpRes} \
-k KR \
${hic} \
${id}_${res}kb
"""
}
process hiccups {
tag "_${id}"
label 'gpu'
accelerator 1
cpus 6
memory '30 GB'
container "mblanche/hiccups-gpu"
publishDir "${params.outDir}/hiccups/",
mode: 'copy'
input:
tuple id, path(hic), val(res) from hiccups_ch
.combine(Channel.from(resolutions.collect{it*1000}.join(',')))
output:
tuple id, path("${id}_loops") into hiccups_out_ch
script:
"""
java -Xmx24000m \
-jar /juicer_tools.jar \
hiccups \
--threads ${task.cpus} \
--ignore-sparsity \
-m 500 \
-r ${res} \
-k KR \
${hic} \
${id}_loops
"""
}
process mustache {
tag "_${id}"
cpus 24
memory '48 GB'
container "mblanche/mustache:1.1.3"
publishDir "${params.outDir}/mustache",
mode: 'copy'
input:
tuple id, path(mcool), val(res) from mustache_mcool_ch
.combine(Channel.from(1000,4000,16000))
output:
tuple id, path("*.tsv") into mustache_2_merge_ch
script:
"""
touch ${id}_${res}kb_loops.tsv
mustache -p ${task.cpus} \
-f ${mcool} \
-r ${res} \
-o ${id}_${res}kb_loops.tsv
"""
}
process ABcomp {
tag "_${id}"
cpus 1
memory '12 GB'
container "mblanche/fan-c"
publishDir "${params.outDir}/AB_comp",
mode: 'copy'
input:
tuple id, path(cool), val(resKB) from abcomp_mcool_ch
.combine(Channel.from(ABresolutions))
path(genome) from abcomp_genome_ch.first()
output:
tuple id, path("*.bed"), path("*.ab") into fanc_out_ch
script:
res = resKB.toInteger() * 1000
"""
fanc compartments \
-f \
-v ${id}_eigenV_${resKB}kb.bed \
-d ${id}_AB_${resKB}kb.bed \
-g ${genome} \
${cool}@${res} \
${id}_${resKB}kb.ab
"""
}