-
Notifications
You must be signed in to change notification settings - Fork 4
/
mocha.wdl
2424 lines (2227 loc) · 101 KB
/
mocha.wdl
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
version development
## Copyright (c) 2020-2024 Giulio Genovese
##
## Version 2024-09-27
##
## Contact Giulio Genovese <[email protected]>
##
## This WDL workflow runs MoChA on a cohort of samples genotyped with either Illumina or Affymetrix DNA microarrays
##
## Cromwell version support
## - Successfully tested on v87
##
## Distributed under terms of the MIT License
struct Reference {
String name
File fasta
Int min_chr_len
File? rules_file
String? mhc_reg
String? kir_reg
String? nonpar_reg
File? dup_file
File genetic_map_file
File? cnp_file
File cyto_file
String? panel_pfx
String? panel_sfx
String panel_idx
Int? n_panel_smpls
}
workflow mocha {
input {
String sample_set_id
String mode # idat gtc cel chp txt vcf pvcf
String target = "calls" # vcf pvcf calls pngs
Boolean realign = false
Boolean wgs = false
Boolean gtc_output = false # only for idat mode
Boolean chp_output = false # only for cel mode
Int idat_batch_size = 256
Int gtc_batch_size = 1024
Int chp_batch_size = 1024
Float max_win_size_cm = 50.0
Float overlap_size_cm = 5.0
Float sample_call_rate_thr = 0.97
Float variant_call_rate_thr = 0.97
Float baf_auto_thr = 0.03
String ext_string = "as"
String ref_name = "GRCh38"
String? ref_path
String? ref_fasta
Int? min_chr_len
File? ref_rules_file
String? mhc_reg
String? kir_reg
String? nonpar_reg
String? dup_file
String? genetic_map_file
String? cnp_file
String? cyto_file
String? panel_pfx
String? panel_sfx
String? panel_idx
Int? n_panel_smpls
String manifest_path = ""
File sample_tsv_file # sample_id batch_id green_idat red_idat gtc cel chp computed_gender call_rate
File batch_tsv_file # batch_id path bpm csv egt sam xml zip probeset_ids snp report calls confidences summary vcf vcf_index xcl_vcf xcl_vcf_index
String? data_path
File? pedigree_file
File? duplicate_samples_file
File? extra_xcl_vcf_file
String? gtc2vcf_extra_args
String? phase_extra_args
String? mocha_extra_args
String? mocha_plot_extra_args
String basic_bash_docker = "debian:stable-slim"
String pandas_docker = "amancevice/pandas:slim"
String docker_repository = "us.gcr.io/mccarroll-mocha"
String bcftools_docker = "bcftools:1.20-20240927"
String apt_docker = "apt:1.20-20240927"
String shapeit5_docker = "shapeit5:1.20-20240927"
String r_mocha_docker = "r_mocha:1.20-20240927"
Boolean? table_output
Boolean do_not_use_reference = false
Boolean use_shapeit5_ligate = false
String delim = "~"
Array[String]? chip_type
String tags = "GT,BAF,LRR"
Int? gc_window_size
}
Boolean mode_is_vcf = mode == "vcf" || mode == "pvcf" || wgs
String docker_repository_with_sep = docker_repository + if docker_repository != "" && docker_repository == sub(docker_repository, "/$", "") then "/" else ""
String ref_path_with_sep = select_first([ref_path, ""]) + if defined(ref_path) && select_first([ref_path]) == sub(select_first([ref_path]), "/$", "") then "/" else ""
Reference ref = object {
name: ref_name,
fasta: if defined(ref_fasta) then ref_path_with_sep + select_first([ref_fasta]) else if ref_name == "GRCh38" then ref_path_with_sep + "GCA_000001405.15_GRCh38_no_alt_analysis_set.fna" else if ref_name == "GRCh37" then ref_path_with_sep + "human_g1k_v37.fasta" else None,
min_chr_len: select_first([min_chr_len, 2000000]),
rules_file: if defined(ref_rules_file) then ref_path_with_sep + select_first([ref_rules_file]) else None,
mhc_reg: if defined(mhc_reg) then select_first([mhc_reg]) else if ref_name == "GRCh38" then "chr6:27518932-33480487" else if ref_name == "GRCh37" then "6:27486711-33448264" else None,
kir_reg: if defined(kir_reg) then select_first([kir_reg]) else if ref_name == "GRCh38" then "chr19:54071493-54992731" else if ref_name == "GRCh37" then "19:54574747-55504099" else None,
nonpar_reg: if defined(nonpar_reg) then select_first([nonpar_reg]) else if ref_name == "GRCh38" then "chrX:2781479-155700628" else if ref_name == "GRCh37" then "X:2699520-154930289" else None,
dup_file: if defined(dup_file) then ref_path_with_sep + select_first([dup_file]) else if ref_name == "GRCh38" || ref_name == "GRCh37" then ref_path_with_sep + "segdups.bed.gz" else None,
genetic_map_file: if defined(genetic_map_file) then ref_path_with_sep + select_first([genetic_map_file]) else if ref_name == "GRCh38" then ref_path_with_sep + "genetic_map_hg38_withX.txt.gz" else if ref_name == "GRCh37" then ref_path_with_sep + "genetic_map_hg19_withX.txt.gz" else None,
cnp_file: if defined(cnp_file) then ref_path_with_sep + select_first([cnp_file]) else if ref_name == "GRCh38" || ref_name == "GRCh37" then ref_path_with_sep + "cnps.bed" else None,
cyto_file: if defined(cyto_file) then ref_path_with_sep + select_first([cyto_file]) else if ref_name == "GRCh38" || ref_name == "GRCh37" then ref_path_with_sep + "cytoBand.txt.gz" else None,
panel_pfx: if defined(panel_pfx) then ref_path_with_sep + select_first([panel_pfx]) else if ref_name == "GRCh38" then ref_path_with_sep + "1kGP_high_coverage_Illumina." else if ref_name == "GRCh37" then ref_path_with_sep + "ALL.chr" else None,
panel_sfx: if defined(panel_sfx) then select_first([panel_sfx]) else if ref_name == "GRCh38" then ".bcf" else if ref_name == "GRCh37" then ".phase3_integrated.20130502.genotypes.bcf" else None,
panel_idx: select_first([panel_idx, ".csi"]),
n_panel_smpls: if defined(n_panel_smpls) then select_first([n_panel_smpls]) else if ref_name == "GRCh38" then 3202 else if ref_name == "GRCh37" then 2504 else None,
}
# read table with batches information (scatter could be avoided if there was a tail() function)
call tsv_sorted as batch_sorted_tsv { input: tsv_file = batch_tsv_file, column = "batch_id", check_dups = true, docker = basic_bash_docker }
Array[Array[String]] batch_tsv = read_tsv(batch_sorted_tsv.file)
Int n_batches = length(batch_tsv)-1
scatter (idx in range(n_batches)) { Array[String] batch_tsv_rows = batch_tsv[(idx+1)] }
Map[String, Array[String]] batch_tbl = as_map(zip(batch_tsv[0], transpose(batch_tsv_rows)))
Array[String] batches = batch_tbl["batch_id"]
# check which variables are in batch table (see http://github.com/openwdl/wdl/issues/305)
Boolean is_path_in_batch_tbl = length(collect_by_key(zip(flatten([keys(batch_tbl),["path"]]),range(length(keys(batch_tbl))+1)))["path"])>1
Boolean is_sam_in_batch_tbl = length(collect_by_key(zip(flatten([keys(batch_tbl),["sam"]]),range(length(keys(batch_tbl))+1)))["sam"])>1
Boolean is_probeset_ids_in_batch_tbl = length(collect_by_key(zip(flatten([keys(batch_tbl),["probeset_ids"]]),range(length(keys(batch_tbl))+1)))["probeset_ids"])>1
Boolean is_confidences_in_batch_tbl = length(collect_by_key(zip(flatten([keys(batch_tbl),["confidences"]]),range(length(keys(batch_tbl))+1)))["confidences"])>1
String manifest_path_with_sep = manifest_path + (if manifest_path == "" || sub(manifest_path, "/$", "") != manifest_path then "" else "/")
scatter (idx in range(n_batches)) {
String data_paths_with_sep = (if defined(data_path) then sub(select_first([data_path]), "/$", "") + "/" else "") +
(if is_path_in_batch_tbl then sub(batch_tbl["path"][idx], "/$", "") + "/" else "")
String pfxs = sample_set_id + (if n_batches == 1 then "" else "." + batches[idx])
}
# aligns manifest file to human genome reference if requested
if (realign && !mode_is_vcf) {
# hack due to lack of unique function
Array[String] csv_files = keys(collect_by_key(zip(batch_tbl["csv"], batches)))
scatter (csv_file in csv_files) {
call csv2bam {
input:
plugin = if mode == "idat" || mode == "gtc" then "gtc2vcf" else "affy2vcf",
csv_file = manifest_path_with_sep + csv_file,
ref_fasta = ref.fasta,
ref_fasta_idxs = prefix(ref.fasta + ".", ["amb", "ann", "bwt", "pac", "sa"]),
docker = docker_repository_with_sep + bcftools_docker,
}
}
Map[String, File] csv2sam = as_map(zip(csv_files, csv2bam.bam_file))
}
scatter (idx in range(n_batches)) {
File? sams = if realign && !mode_is_vcf then select_first([csv2sam])[(batch_tbl["csv"][idx])]
else if is_sam_in_batch_tbl then manifest_path_with_sep + batch_tbl["sam"][idx] else None
}
Array[Array[String]] ref_fasta_fai_tbl = transpose(read_tsv(ref.fasta + ".fai"))
scatter (idx in range(length(ref_fasta_fai_tbl[0]))) {
Int fai_len = ref_fasta_fai_tbl[1][idx]
if (fai_len > ref.min_chr_len && ref_fasta_fai_tbl[0][idx] != "Y" && ref_fasta_fai_tbl[0][idx] != "chrY") {
String chrs = ref_fasta_fai_tbl[0][idx]
Int lens = fai_len
}
}
if (!mode_is_vcf) {
# resort table with sample information and extract sample_id column
call tsv_sorted as sample_sorted_tsv { input: tsv_file = sample_tsv_file, column = "batch_id", docker = basic_bash_docker }
call tsv_column as sample_id_lines { input: tsv_file = sample_sorted_tsv.file, column = "sample_id", check_dups = true, docker = basic_bash_docker }
call tsv_column as batch_id_lines { input: tsv_file = sample_sorted_tsv.file, column = "batch_id", docker = basic_bash_docker }
# process Illumina data
if (mode == "idat") {
call tsv_column as green_idat_lines { input: tsv_file = sample_sorted_tsv.file, column = "green_idat", docker = basic_bash_docker }
call tsv_column as red_idat_lines { input: tsv_file = sample_sorted_tsv.file, column = "red_idat", docker = basic_bash_docker }
# group samples by IDAT batches
call batch_scatter as idat { input: batch_id_file = batch_id_lines.file, sub_batch_size = idat_batch_size, delim = delim, docker = basic_bash_docker }
Map[String, Array[String]] idat_batch2green_idat_files = collect_by_key(zip(read_lines(idat.sub_batch_id), read_lines(green_idat_lines.file)))
Map[String, Array[String]] idat_batch2red_idat_files = collect_by_key(zip(read_lines(idat.sub_batch_id), read_lines(red_idat_lines.file)))
scatter (idx in range(length(idat.sub_batches))) {
call idat2gtc {
input:
bpm_file = manifest_path_with_sep + batch_tbl["bpm"][(idat.idxs[idx])],
egt_file = manifest_path_with_sep + batch_tbl["egt"][(idat.idxs[idx])],
green_idat_files = prefix(data_paths_with_sep[(idat.idxs[idx])], idat_batch2green_idat_files[(idat.sub_batches[idx])]),
red_idat_files = prefix(data_paths_with_sep[(idat.idxs[idx])], idat_batch2red_idat_files[(idat.sub_batches[idx])]),
filebase = sample_set_id + "." + idat.sub_batches[idx],
docker = docker_repository_with_sep + bcftools_docker,
}
}
call tsv_concat as green_idat_tsv { input: tsv_files = idat2gtc.green_idat_tsv, filebase = sample_set_id + ".green_idat", docker = basic_bash_docker }
call tsv_concat as red_idat_tsv { input: tsv_files = idat2gtc.red_idat_tsv, filebase = sample_set_id + ".red_idat", docker = basic_bash_docker }
}
if (mode == "gtc") {
call tsv_column as gtc_lines { input: tsv_file = sample_sorted_tsv.file, column = "gtc", docker = basic_bash_docker }
}
if (mode == "idat" || mode == "gtc") {
# group samples by GTC batches
call batch_scatter as gtc { input: batch_id_file = batch_id_lines.file, sub_batch_size = gtc_batch_size, delim = delim, docker = basic_bash_docker }
call get_reheader_maps as gtc_reheader { input: batches = gtc.sub_batches, barcodes_file = select_first([green_idat_lines.file, gtc_lines.file]), sample_id_file = sample_id_lines.file, batch_id_file = gtc.sub_batch_id, docker = basic_bash_docker }
Array[String]+ input_gtc_files = if mode == "idat" then flatten(select_first([idat2gtc.gtc_files])) else read_lines(select_first([gtc_lines.file]))
Map[String, Array[String]] gtc_batch2gtc_files = collect_by_key(zip(read_lines(gtc.sub_batch_id), input_gtc_files))
scatter (idx in range(length(gtc.sub_batches))) {
call gtc2vcf {
input:
tags = tags,
bpm_file = manifest_path_with_sep + batch_tbl["bpm"][(gtc.idxs[idx])],
csv_file = manifest_path_with_sep + batch_tbl["csv"][(gtc.idxs[idx])],
egt_file = manifest_path_with_sep + batch_tbl["egt"][(gtc.idxs[idx])],
ref_fasta = ref.fasta,
ref_fasta_fai = ref.fasta + ".fai",
gc_window_size = gc_window_size,
gtc_files = prefix(if mode == "idat" then "" else data_paths_with_sep[(gtc.idxs[idx])], gtc_batch2gtc_files[(gtc.sub_batches[idx])]),
gtc2vcf_extra_args = gtc2vcf_extra_args,
sam_file = sams[(gtc.idxs[idx])],
reheader_file = gtc_reheader.map_files[idx],
filebase = sample_set_id + "." + gtc.sub_batches[idx],
docker = docker_repository_with_sep + bcftools_docker,
}
}
call tsv_concat as gtc_tsv { input: tsv_files = gtc2vcf.gtc_tsv, filebase = sample_set_id + ".gtc", docker = basic_bash_docker }
# this job can be long, so it is better to run as non-preemptible
Map[Int, Array[String]] idx2gtc2vcf_files = collect_by_key(zip(gtc.idxs, gtc2vcf.vcf_file))
Map[Int, Array[String]] idx2gtc2vcf_idxs = collect_by_key(zip(gtc.idxs, gtc2vcf.vcf_idx))
Map[Int, Array[String]] idx2gtc2vcf_n_smpls = collect_by_key(zip(gtc.idxs, gtc2vcf.n_smpls))
scatter (idx in range(n_batches)) {
if (length(idx2gtc2vcf_files[idx]) > 1) {
call vcf_merge as gtc2vcf_merge {
input:
vcf_files = idx2gtc2vcf_files[idx],
filebase = pfxs[idx],
docker = docker_repository_with_sep + bcftools_docker
}
}
File gtc2vcf_files = select_first([gtc2vcf_merge.vcf_file, idx2gtc2vcf_files[idx][0]])
File gtc2vcf_idxs = select_first([gtc2vcf_merge.vcf_idx, idx2gtc2vcf_idxs[idx][0]])
Int gtc2vcf_n_smpls = select_first([gtc2vcf_merge.n_smpls, idx2gtc2vcf_n_smpls[idx][0]])
}
}
if (mode == "cel" || mode == "txt") {
call tsv_column as cel_lines { input: tsv_file = sample_sorted_tsv.file, column = "cel", docker = basic_bash_docker }
}
# process Affymetrix data
if (mode == "cel") {
Map[String, Array[String]] batch2cel_files = collect_by_key(zip(read_lines(batch_id_lines.file), read_lines(select_first([cel_lines.file]))))
scatter (idx in range(n_batches)) {
call cel2affy as cel2chp {
input:
xml_file = manifest_path_with_sep + batch_tbl["xml"][idx],
zip_file = manifest_path_with_sep + batch_tbl["zip"][idx],
cel_files = prefix(data_paths_with_sep[idx], batch2cel_files[(batches[idx])]),
probeset_file = if is_probeset_ids_in_batch_tbl && batch_tbl["probeset_ids"][idx] != "" then manifest_path_with_sep + batch_tbl["probeset_ids"][idx] else None,
chip_type = chip_type,
table_output = table_output,
filebase = pfxs[idx],
docker = docker_repository_with_sep + apt_docker
}
}
call tsv_concat as cel_tsv { input: tsv_files = cel2chp.cel_tsv, filebase = sample_set_id + ".cel", docker = basic_bash_docker }
}
if (mode == "chp") {
call tsv_column as chp_lines { input: tsv_file = sample_sorted_tsv.file, column = "chp", docker = basic_bash_docker }
}
if (mode == "cel" || mode == "chp") {
# group samples by CHP batches
call batch_scatter as chp { input: batch_id_file = batch_id_lines.file, sub_batch_size = chp_batch_size, delim = delim, docker = basic_bash_docker }
call get_reheader_maps as chp_reheader { input: batches = chp.sub_batches, barcodes_file = select_first([cel_lines.file, chp_lines.file]), sample_id_file = sample_id_lines.file, batch_id_file = chp.sub_batch_id, docker = basic_bash_docker }
Array[String]+ input_chp_files = if mode == "cel" then flatten(select_first([cel2chp.chp_files])) else read_lines(select_first([chp_lines.file]))
Map[String, Array[String]] chp_batch2chp_files = collect_by_key(zip(read_lines(chp.sub_batch_id), input_chp_files))
scatter (idx in range(length(chp.sub_batches))) {
call chp2vcf {
input:
tags = tags,
csv_file = manifest_path_with_sep + batch_tbl["csv"][(chp.idxs[idx])],
ref_fasta = ref.fasta,
ref_fasta_fai = ref.fasta + ".fai",
gc_window_size = gc_window_size,
probeset_file = if is_probeset_ids_in_batch_tbl && batch_tbl["probeset_ids"][(chp.idxs[idx])] != "" then manifest_path_with_sep + batch_tbl["probeset_ids"][(chp.idxs[idx])] else None,
snp_file = if mode == "cel" then select_first([cel2chp.snp_file])[(chp.idxs[idx])] else data_paths_with_sep[(chp.idxs[idx])] + batch_tbl["snp"][(chp.idxs[idx])],
chp_files = prefix(if mode == "cel" then "" else data_paths_with_sep[(chp.idxs[idx])], chp_batch2chp_files[(chp.sub_batches[idx])]),
sam_file = sams[(chp.idxs[idx])],
reheader_file = chp_reheader.map_files[idx],
filebase = sample_set_id + "." + chp.sub_batches[idx],
docker = docker_repository_with_sep + bcftools_docker
}
}
# this job can be long, so it is better to run as non-preemptible
Map[Int, Array[String]] idx2chp2vcf_files = collect_by_key(zip(chp.idxs, chp2vcf.vcf_file))
Map[Int, Array[String]] idx2chp2vcf_idxs = collect_by_key(zip(chp.idxs, chp2vcf.vcf_idx))
Map[Int, Array[String]] idx2chp2vcf_n_smpls = collect_by_key(zip(chp.idxs, chp2vcf.n_smpls))
scatter (idx in range(n_batches)) {
if (length(idx2chp2vcf_files[idx]) > 1) {
call vcf_merge as chp2vcf_merge {
input:
vcf_files = idx2chp2vcf_files[idx],
filebase = pfxs[idx],
docker = docker_repository_with_sep + bcftools_docker
}
}
File chp2vcf_files = select_first([chp2vcf_merge.vcf_file, idx2chp2vcf_files[idx][0]])
File chp2vcf_idxs = select_first([chp2vcf_merge.vcf_idx, idx2chp2vcf_idxs[idx][0]])
Int chp2vcf_n_smpls = select_first([chp2vcf_merge.n_smpls, idx2chp2vcf_n_smpls[idx][0]])
}
}
if (mode == "txt") {
call get_reheader_maps as txt_reheader { input: batches = batches, barcodes_file = select_first([cel_lines.file]), sample_id_file = sample_id_lines.file, batch_id_file = batch_id_lines.file, docker = basic_bash_docker }
scatter (idx in range(n_batches)) {
# this job can be long, so it is better to run as non-preemptible
call txt2vcf {
input:
tags = tags,
csv_file = manifest_path_with_sep + batch_tbl["csv"][idx],
ref_fasta = ref.fasta,
ref_fasta_fai = ref.fasta + ".fai",
gc_window_size = gc_window_size,
probeset_file = if is_probeset_ids_in_batch_tbl && batch_tbl["probeset_ids"][idx] != ""
then manifest_path_with_sep + batch_tbl["probeset_ids"][idx] else None,
calls_file = data_paths_with_sep[idx] + batch_tbl["calls"][idx],
confidences_file = if is_confidences_in_batch_tbl then data_paths_with_sep[idx] + batch_tbl["confidences"][idx] else None,
summary_file = data_paths_with_sep[idx] + batch_tbl["summary"][idx],
report_file = data_paths_with_sep[idx] + batch_tbl["report"][idx],
snp_file = data_paths_with_sep[idx] + batch_tbl["snp"][idx],
sam_file = sams[idx],
reheader_file = txt_reheader.map_files[idx],
filebase = pfxs[idx],
docker = docker_repository_with_sep + bcftools_docker
}
}
}
if (mode == "cel" || mode == "chp" || mode == "txt") {
call tsv_concat as affy_tsv { input: tsv_files = select_first([chp2vcf.affy_tsv, txt2vcf.affy_tsv]), filebase = sample_set_id + ".affy", docker = basic_bash_docker }
}
Array[File] unphased_vcf_files = select_first([gtc2vcf_files, chp2vcf_files, txt2vcf.vcf_file])
Array[File] unphased_vcf_idxs = select_first([gtc2vcf_idxs, chp2vcf_idxs, txt2vcf.vcf_idx])
Array[Int] unphased_vcf_n_smpls = select_first([gtc2vcf_n_smpls, chp2vcf_n_smpls, txt2vcf.n_smpls])
call lst_flatten as flatten_sample_id_lines { input: lst_files = select_first([gtc2vcf.sample_id_lines, chp2vcf.sample_id_lines, txt2vcf.sample_id_lines]), filebase = "sample_id", docker = basic_bash_docker }
call tsv_column as override_computed_gender_lines { input: tsv_file = sample_sorted_tsv.file, column = "computed_gender", fail = false, docker = basic_bash_docker }
if (override_computed_gender_lines.failed) {
call tsv_column as computed_gender_lines { input: tsv_file = select_first([gtc_tsv.file, affy_tsv.file, sample_tsv_file]), column = "computed_gender", docker = basic_bash_docker }
}
call tsv_column as override_call_rate_lines { input: tsv_file = sample_sorted_tsv.file, column = "call_rate", fail = false, docker = basic_bash_docker }
if (override_call_rate_lines.failed) {
call tsv_column as call_rate_lines { input: tsv_file = select_first([gtc_tsv.file, affy_tsv.file, sample_tsv_file]), column = "call_rate", docker = basic_bash_docker }
}
call lst_paste as sample_tsv {
input:
lst_files = select_all([flatten_sample_id_lines.file, select_first([computed_gender_lines.file, override_computed_gender_lines.file]), select_first([call_rate_lines.file, override_call_rate_lines.file])]),
headers = ['sample_id', 'computed_gender', 'call_rate'],
filebase = sample_set_id + ".sample",
docker = basic_bash_docker
}
}
if (mode != "pvcf" && target != "vcf") {
call ref_scatter {
input:
chrs = select_all(chrs),
lens = select_all(lens),
genetic_map_file = ref.genetic_map_file,
max_win_size_cm = max_win_size_cm,
overlap_size_cm = overlap_size_cm,
docker = pandas_docker
}
scatter (idx in range(n_batches)) {
call vcf_scatter {
input:
vcf_file = if mode_is_vcf then data_paths_with_sep[idx] + batch_tbl["vcf"][idx] else select_first([unphased_vcf_files])[idx],
intervals_tsv = ref_scatter.intervals_tsv,
docker = docker_repository_with_sep + bcftools_docker
}
}
if (defined(extra_xcl_vcf_file)) {
call vcf_scatter as xcl_vcf_scatter {
input:
vcf_file = select_first([extra_xcl_vcf_file]),
intervals_tsv = ref_scatter.intervals_tsv,
docker = docker_repository_with_sep + bcftools_docker
}
}
call lst_concat as sample_id_split_tsv { input: lst_files = vcf_scatter.sample_id_lines, filebase = "split_sample_id", docker = basic_bash_docker }
Int n_smpls = length(flatten(read_tsv(sample_id_split_tsv.file)))
Boolean use_reference = if do_not_use_reference then false else n_smpls <= 2 * ref.n_panel_smpls
Array[Array[File]] interval_slices = transpose(vcf_scatter.vcf_files)
Array[Array[String]] intervals_tbl = transpose(read_tsv(ref_scatter.intervals_tsv))
if (defined(ref.mhc_reg)) {
String? mhc_chr = sub(select_first([ref.mhc_reg]), ":.*$", "")
Int mhc_beg = sub(sub(select_first([ref.mhc_reg]), "-.*$", ""), "^.*:", "")
Int mhc_end = sub(select_first([ref.mhc_reg]), "^.*-", "")
}
if (defined(ref.nonpar_reg)) {
String? nonpar_chr = sub(select_first([ref.nonpar_reg]), ":.*$", "")
Int nonpar_beg = sub(sub(select_first([ref.nonpar_reg]), "-.*$", ""), "^.*:", "")
Int nonpar_end = sub(select_first([ref.nonpar_reg]), "^.*-", "")
}
scatter (idx in range(length(intervals_tbl[0]))) {
if (length(interval_slices[idx]) > 1) {
call vcf_merge {
input:
vcf_files = interval_slices[idx],
filebase = sample_set_id + "." + idx,
docker = docker_repository_with_sep + bcftools_docker
}
}
call vcf_qc {
input:
vcf_file = select_first([vcf_merge.vcf_file, interval_slices[idx][0]]),
vcf_idx = vcf_merge.vcf_idx,
dup_file = ref.dup_file,
sample_tsv_file = select_first([sample_tsv.file, sample_tsv_file]),
sample_call_rate_thr = if wgs then None else sample_call_rate_thr,
variant_call_rate_thr = variant_call_rate_thr,
duplicate_samples_file = duplicate_samples_file,
extra_xcl_vcf_file = if defined(extra_xcl_vcf_file) then select_first([xcl_vcf_scatter.vcf_files])[idx] else None,
fix_ploidy = wgs,
docker = docker_repository_with_sep + bcftools_docker
}
Int buffer_beg = intervals_tbl[1][idx] # cast string to integer
Int buffer_end = intervals_tbl[2][idx] # cast string to integer
Int beg = intervals_tbl[3][idx] # cast string to integer
Int end = intervals_tbl[4][idx] # cast string to integer
call vcf_shapeit5 {
input:
n_smpls = n_smpls,
n_markers = vcf_qc.n_markers,
unphased_vcf_file = vcf_qc.qc_vcf_file,
unphased_vcf_idx = vcf_qc.qc_vcf_idx,
genetic_map_file = ref.genetic_map_file,
n_chrs = length(select_all(chrs)),
pedigree_file = pedigree_file,
sample_tsv_file = if defined(ref.nonpar_reg) && intervals_tbl[0][idx] == select_first([nonpar_chr]) && beg >= select_first([nonpar_beg]) && end <= select_first([nonpar_end]) then select_first([sample_tsv.file, sample_tsv_file]) else None,
n_panel_smpls = if use_reference then ref.n_panel_smpls else None,
ref_vcf_file = if use_reference then ref.panel_pfx + intervals_tbl[0][idx] + ref.panel_sfx else None,
ref_vcf_idx = if use_reference then ref.panel_pfx + intervals_tbl[0][idx] + ref.panel_sfx + ref.panel_idx else None,
ref_fasta_fai = ref.fasta + ".fai",
input_region = intervals_tbl[0][idx] + ":" + (1 + beg) + "-" + end,
scaffold_region = intervals_tbl[0][idx] + ":" + (1 + buffer_beg) + "-" + buffer_end,
chr = intervals_tbl[0][idx],
mhc = if defined(ref.mhc_reg) then intervals_tbl[0][idx] == select_first([mhc_chr]) && beg <= select_first([mhc_end]) && end > select_first([mhc_beg]) else false,
maf = if wgs && n_smpls > 2000 then 0.001 else 0.0, # see http://odelaneau.github.io/shapeit5/docs/documentation/phase_rare/
phase_extra_args = phase_extra_args,
docker = docker_repository_with_sep + shapeit5_docker
}
if (length(batches) > 1 || defined(pedigree_file)) {
call vcf_split {
input:
vcf_file = vcf_shapeit5.pgt_vcf_file,
batches = batches,
sample_id_file = sample_id_split_tsv.file,
docker = docker_repository_with_sep + bcftools_docker
}
}
}
call vcf_concat {
input:
vcf_files = vcf_qc.xcl_vcf_file,
filebase = sample_set_id + ".xcl",
docker = docker_repository_with_sep + bcftools_docker
}
Array[Array[File]] batch_slices = if (length(batches) > 1 || defined(pedigree_file)) then transpose(select_all(vcf_split.vcf_files)) else [vcf_shapeit5.pgt_vcf_file]
scatter (idx in range(n_batches)) {
call vcf_ligate {
input:
vcf_files = batch_slices[idx],
pedigree_file = if use_shapeit5_ligate then pedigree_file else None,
filebase = pfxs[idx] + ".pgt",
docker = docker_repository_with_sep + if use_shapeit5_ligate then shapeit5_docker else bcftools_docker
}
call vcf_import {
input:
pgt_vcf_file = vcf_ligate.vcf_file,
pgt_vcf_idx = vcf_ligate.vcf_idx,
unphased_vcf_file = if mode_is_vcf then data_paths_with_sep[idx] + batch_tbl["vcf"][idx] else select_first([unphased_vcf_files])[idx],
unphased_vcf_idx = if mode_is_vcf then data_paths_with_sep[idx] + batch_tbl["vcf_index"][idx] else select_first([unphased_vcf_idxs])[idx],
docker = docker_repository_with_sep + bcftools_docker
}
}
}
if (target == "calls" || target == "pngs") {
scatter (idx in range(n_batches)) {
call get_max_nrecords { input: vcf_idx = if mode == "pvcf" then data_paths_with_sep[idx] + batch_tbl["vcf_index"][idx] else select_first([vcf_import.vcf_idx])[idx], docker = docker_repository_with_sep + bcftools_docker }
call vcf_mocha {
input:
n_smpls = if mode == "pvcf" then batch_tbl["n_smpls"][idx] else select_first([vcf_import.n_smpls])[idx],
max_n_markers = get_max_nrecords.n,
assembly = ref.name,
rules_file = ref.rules_file,
pvcf_file = if mode == "pvcf" then data_paths_with_sep[idx] + batch_tbl["vcf"][idx] else select_first([vcf_import.vcf_file])[idx],
pvcf_idx = if mode == "pvcf" then data_paths_with_sep[idx] + batch_tbl["vcf_index"][idx] else select_first([vcf_import.vcf_idx])[idx],
sample_tsv_file = select_first([sample_tsv.file, sample_tsv_file]),
xcl_vcf_file = if mode == "pvcf" then data_paths_with_sep[idx] + batch_tbl["xcl_vcf"][idx] else vcf_concat.vcf_file,
xcl_vcf_idx = if mode == "pvcf" then data_paths_with_sep[idx] + batch_tbl["xcl_vcf_index"][idx] else vcf_concat.vcf_idx,
cnp_file = ref.cnp_file,
mhc_reg = ref.mhc_reg,
kir_reg = ref.kir_reg,
mocha_extra_args = mocha_extra_args,
ext_string = ext_string,
wgs = wgs,
docker = docker_repository_with_sep + bcftools_docker
}
if (target == "pngs") {
call mocha_plot {
input:
vcf_file = vcf_mocha.mocha_vcf_file,
vcf_idx = vcf_mocha.mocha_vcf_idx,
stats_tsv = vcf_mocha.stats_tsv,
calls_tsv = vcf_mocha.calls_tsv,
cyto_file = ref.cyto_file,
n_chrs = length(select_all(chrs)),
call_rate_thr = if wgs then None else sample_call_rate_thr,
baf_auto_thr = baf_auto_thr,
mocha_plot_extra_args = mocha_plot_extra_args,
wgs = wgs,
docker = docker_repository_with_sep + r_mocha_docker
}
}
}
call tsv_concat as mocha_stats_tsv { input: tsv_files = vcf_mocha.stats_tsv, filebase = sample_set_id + ".stats", docker = basic_bash_docker }
call tsv_concat as mocha_calls_tsv { input: tsv_files = vcf_mocha.calls_tsv, filebase = sample_set_id + ".calls", docker = basic_bash_docker }
call mocha_summary {
input:
calls_tsv = mocha_calls_tsv.file,
stats_tsv = mocha_stats_tsv.file,
ucsc_beds = vcf_mocha.ucsc_bed,
cyto_file = ref.cyto_file,
n_chrs = length(select_all(chrs)),
filebase = sample_set_id,
call_rate_thr = if wgs then None else sample_call_rate_thr,
baf_auto_thr = baf_auto_thr,
docker = docker_repository_with_sep + r_mocha_docker
}
}
# generate a table summarizing the main output files and serialize the table to disk
# vcf_files and vcf_idxs are defined in the output section
scatter (idx in range(n_batches)) {
String basename_vcf_files = basename(vcf_files[idx])
String basename_vcf_idxs = basename(vcf_idxs[idx])
String basename_xcl_vcf_files = basename(if mode == "pvcf" then batch_tbl["xcl_vcf"][idx] else select_first([vcf_concat.vcf_file, ""]))
String basename_xcl_vcf_idxs = basename(if mode == "pvcf" then batch_tbl["xcl_vcf_index"][idx] else select_first([vcf_concat.vcf_idx, ""]))
String basename_pgt_vcf_files = basename(if mode != "pvcf" && target != "vcf" then select_first([vcf_ligate.vcf_file])[idx] else "")
String basename_pgt_vcf_idxs = basename(if mode != "pvcf" && target != "vcf" then select_first([vcf_ligate.vcf_idx])[idx] else "")
}
Array[Pair[String,Array[String]]] output_pairs = flatten([[("batch_id", batch_tbl["batch_id"]),
("vcf", basename_vcf_files),
("vcf_index", basename_vcf_idxs)],
if mode == "pvcf" || target == "vcf" then [] else
[("n_smpls", select_first([vcf_import.n_smpls, unphased_vcf_n_smpls])),
("xcl_vcf", basename_xcl_vcf_files),
("xcl_vcf_index", basename_xcl_vcf_idxs),
("pgt_vcf", basename_pgt_vcf_files),
("pgt_vcf_index", basename_pgt_vcf_idxs)]])
Map[String, Array[String]] output_map = as_map(output_pairs)
# cannot use output_keys = keys(output_map) because of unresolved Cromwell bug
# http://github.com/broadinstitute/cromwell/issues/5559
scatter (p in output_pairs) {
String output_keys = p.left
Array[String] output_tsv_cols = output_map[p.left]
}
# this is run as a separate task rather than using write_tsv() as Cromwell can break the WDL specification
# http://support.terra.bio/hc/en-us/community/posts/360071465631-write-lines-write-map-write-tsv-write-json-fail-when-run-in-a-workflow-rather-than-in-a-task
call write_tsv {
input:
tsv = flatten([[output_keys], transpose(output_tsv_cols)]),
filebase = sample_set_id + ".mocha",
docker = basic_bash_docker
}
output {
File? green_idat_tsv_file = green_idat_tsv.file
File? red_idat_tsv_file = red_idat_tsv.file
File? gtc_tsv_file = gtc_tsv.file
File? cel_tsv_file = cel_tsv.file
File? affy_tsv_file = affy_tsv.file
File? stats_file = if defined(mocha_stats_tsv.file) || defined(sample_tsv.file) then select_first([mocha_stats_tsv.file, sample_tsv.file]) else None
File? calls_file = mocha_calls_tsv.file
File? ucsc_bed = mocha_summary.ucsc_bed
File? summary_pdf = mocha_summary.summary_pdf
File? pileup_pdf = mocha_summary.pileup_pdf
Array[File]? png_files = if target == "pngs" then flatten(select_all(select_first([mocha_plot.png_files]))) else None
Array[File]? gtc_files = if mode == "idat" && gtc_output then flatten(select_first([idat2gtc.gtc_files])) else None
Array[File]? chp_files = if mode == "cel" && chp_output then flatten(select_first([cel2chp.chp_files])) else None
Array[File]? snp_files = if mode == "cel" && chp_output then select_first([cel2chp.snp_file]) else None
Array[File] vcf_files = select_first([vcf_mocha.mocha_vcf_file, vcf_import.vcf_file, unphased_vcf_files])
Array[File] vcf_idxs = select_first([vcf_mocha.mocha_vcf_idx, vcf_import.vcf_idx, unphased_vcf_idxs])
File? xcl_vcf_file = vcf_concat.vcf_file
File? xcl_vcf_idx = vcf_concat.vcf_idx
Array[File]? pgt_vcf_files = vcf_ligate.vcf_file
Array[File]? pgt_vcf_idxs = vcf_ligate.vcf_idx
File mocha_tsv_file = write_tsv.file
}
meta {
author: "Giulio Genovese (with help from Chris Whelan)"
email: "[email protected]"
description: "See the [MoChA](http://github.com/freeseek/mocha) website for more information"
}
}
task tsv_sorted {
input {
File tsv_file
String column
Boolean check_dups = false
Boolean check_empty = true
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
String filebase = basename(tsv_file, ".tsv")
command <<<
set -euo pipefail
mv "~{tsv_file}" .
col=$(head -n1 "~{basename(tsv_file)}" | sed 's/\r$//' | tr '\t' '\n' | awk -F"\t" '$0=="~{column}" {print NR}')
if [ "$col" == "" ]; then
echo "Column \"~{column}\" does not exist" 1>&2
exit 1
fi
cat "~{basename(tsv_file)}" | sed 's/\r$//' | (read -r; printf "%s\n" "$REPLY"; sort -k $col,$col -s -t $'\t' -T .) > "~{filebase}.sorted.tsv"
~{if check_dups then
"dups=$(tail -n+2 \"" + filebase + ".sorted.tsv\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '{print $col}' | uniq --repeated)\n" +
"if [[ ! -z \"$dups\" ]]; then\n echo -e \"Duplicate " + column + "(s) found:\\n$dups\" 1>&2\n exit 1\nfi"
else ""}
~{if check_empty then
"empty=$(tail -n+2 \"" + basename(tsv_file) + "\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '$col==\"\"')\n" +
"if [[ ! -z \"$empty\" ]]; then\n echo -e \"Empty " + column + "(s) found at line:\\n$empty\" 1>&2\n exit 1\nfi"
else ""}
rm "~{basename(tsv_file)}"
>>>
output {
File file = filebase + ".sorted.tsv"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task tsv_column {
input {
File tsv_file
String column
Boolean fail = true
Boolean check_dups = false
Boolean check_empty = true
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
mv "~{tsv_file}" .
col=$(head -n1 "~{basename(tsv_file)}" | sed 's/\r$//' | tr '\t' '\n' | awk -F"\t" '$0=="~{column}" {print NR}')
if [ "$col" == "" ]; then
~{if fail then
" echo \"Column \\\"" + column + "\\\" does not exist\" 1>&2\n" +
" exit 1"
else
" touch \"" + column + ".lines\"\n" +
" echo \"true\""}
else
~{if check_dups then
" dups=$(tail -n+2 \"" + basename(tsv_file) + "\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '{print $col}' | sort | uniq --repeated)\n" +
" if [[ ! -z \"$dups\" ]]; then\n echo -e \"Duplicate " + column + "(s) found:\\n$dups\" 1>&2\n exit 1\nfi"
else ""}
~{if check_empty then
" empty=$(tail -n+2 \"" + basename(tsv_file) + "\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '$col==\"\"')\n" +
" if [[ ! -z \"$empty\" ]]; then\n echo -e \"Empty " + column + "(s) found at line:\\n$empty\" 1>&2\n exit 1\nfi"
else ""}
~{if column != "call_rate" then
" tail -n+2 \"" + basename(tsv_file) + "\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '{print $col}' > \"" + column + ".lines\""
else
" max_call_rate=$(tail -n+2 \"" + basename(tsv_file) + "\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '{print $col}' | sort -g -T . | tail -n1)\n" +
" tail -n+2 \"" + basename(tsv_file) + "\" | sed 's/\\r$//' | awk -F\"\\t\" -v col=\"$col\" '{print $col}' | if [[ $max_call_rate > 1.0 ]]; then awk '{print $0/100}'; else cat; fi > \"" + column + ".lines\"\n"}
echo "false"
fi
rm "~{basename(tsv_file)}"
>>>
output {
Boolean failed = read_boolean(stdout())
File file = column + ".lines"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task tsv_concat {
input {
Array[File]+ tsv_files
String filebase
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
tsv_files=~{write_lines(tsv_files)}
~{if length(tsv_files) > 1 then
"cat $tsv_files | tr '\\n' '\\0' | xargs -0 mv -t .\n" +
"sed -i 's/^.*\\///' $tsv_files\n" +
"(head -n1 \"" + basename(tsv_files[0]) + "\";\n" +
"cat $tsv_files | tr '\\n' '\\0' | xargs -0 tail -qn+2) | sed 's/\\r$//'> \"" + filebase + ".tsv\"\n" +
"cat $tsv_files | tr '\\n' '\\0' | xargs -0 rm"
else "mv \"" + tsv_files[0] + "\" \"" + filebase + ".tsv\""}
>>>
output {
File file = filebase + ".tsv"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task lst_flatten {
input {
Array[File]+ lst_files
String filebase
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
lst_files=~{write_lines(lst_files)}
cat $lst_files | tr '\n' '\0' | xargs -0 mv -t .
sed -i 's/^.*\///' $lst_files
cat $lst_files | tr '\n' '\0' | xargs -0 awk 1 > "~{filebase}.lines"
cat $lst_files | tr '\n' '\0' | xargs -0 rm
>>>
output {
File file = filebase + ".lines"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task lst_concat {
input {
Array[File]+ lst_files
String filebase
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
lst_files=~{write_lines(lst_files)}
cat $lst_files | tr '\n' '\0' | xargs -0 mv -t .
sed -i 's/^.*\///' $lst_files
cat $lst_files | tr '\n' '\0' | xargs -0 -n 1 awk '{printf $0"\t"} END {printf "\n"}' | sed 's/\t$//' > "~{filebase}.tsv"
cat $lst_files | tr '\n' '\0' | xargs -0 rm
>>>
output {
File file = filebase + ".tsv"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task lst_paste {
input {
Array[File]+ lst_files
Array[String]+ headers
String filebase
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
headers=~{write_lines(headers)}
lst_files=~{write_lines(lst_files)}
cat $lst_files | tr '\n' '\0' | xargs -0 mv -t .
sed -i 's/^.*\///' $lst_files
(cat $headers | tr '\n' '\t' | sed 's/\t$/\n/'; cat $lst_files | tr '\n' '\0' | xargs -0 paste) > "~{filebase}.tsv"
cat $lst_files | tr '\n' '\0' | xargs -0 rm
>>>
output {
File file = filebase + ".tsv"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
# this task generates sub batches from batches and then returns them in the same order as batches
# batch_id_file should contain batch names without the delim string, or else behavior is undefined
task batch_scatter {
input {
File batch_id_file
Int sub_batch_size
String delim
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
mv "~{batch_id_file}" .
awk -F"\t" 'NR==FNR {x[$0]++} NR>FNR {n=x[$0]/int((x[$0]-1)/~{sub_batch_size}+1);
if (x[$0]>n) print $0"~{delim}"int(y[$0]/n); else print $0; y[$0]++}' \
"~{basename(batch_id_file)}" "~{basename(batch_id_file)}" > sub_batch_ids.lines
uniq sub_batch_ids.lines
uniq sub_batch_ids.lines | cut -d"~{delim}" -f1 | awk '!x[$0]++ {idx++} {print idx-1}' 1>&2
rm "~{basename(batch_id_file)}"
>>>
output {
File sub_batch_id = "sub_batch_ids.lines"
Array[String] sub_batches = read_lines(stdout())
Array[Int] idxs = read_lines(stderr())
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task get_reheader_maps {
input {
Array[String]+ batches
File barcodes_file
File sample_id_file
File batch_id_file
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
batches=~{write_lines(batches)}
mv "~{barcodes_file}" .
mv "~{sample_id_file}" .
mv "~{batch_id_file}" .
mkdir maps
sed 's/^.*\///;s/.gz$//;s/_Grn.idat$//;s/.gtc$//;s/.CEL$//;s/.AxiomGT1.chp$//;s/.birdseed-v2.chp$//' "~{basename(barcodes_file)}" | \
paste -d$'\t' - "~{basename(sample_id_file)}" | \
paste -d$'\t' - "~{basename(batch_id_file)}" | \
awk -F"\t" -v OFS="\t" '{print $1,$2>"maps/"$3".map"}'
sed 's/^/maps\//;s/$/.map/' $batches
rm "~{basename(barcodes_file)}"
rm "~{basename(sample_id_file)}"
rm "~{basename(batch_id_file)}"
>>>
output {
Directory maps = "maps"
# cannot use suffix(batches, ".map") because of unresolved Cromwell bug
# http://github.com/broadinstitute/cromwell/issues/5549
Array[File] map_files = read_lines(stdout())
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}
}
task write_tsv {
input {
Array[Array[String]] tsv
String filebase
String docker
Int cpu = 1
Int disk_size = 10
Float memory = 3.5
Int preemptible = 1
Int maxRetries = 0
}
command <<<
set -euo pipefail
mv ~{write_tsv(tsv)} "~{filebase}.tsv"
>>>
output {
File file = filebase + ".tsv"
}
runtime {
docker: docker
cpu: cpu
disks: "local-disk " + disk_size + " HDD"
memory: memory + " GiB"
preemptible: preemptible
maxRetries: maxRetries
}