-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse_gatk_json.py
executable file
·1669 lines (1494 loc) · 72 KB
/
parse_gatk_json.py
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
#!/usr/bin/env python
# Potentially make List[String] types a repeat xml element. Otherwise, it appears these are
# separated by commas on the command line.
# Annotation options need macros.
# Macros for read filter options? These have their own json files it seems...
# gVCF not currently set to be different than VCF.
from lxml import etree
from string import Template
from xml.sax.saxutils import escape
import argparse
import json
import pypandoc
VERSION = "0.4.1"
def supply_args():
parser = argparse.ArgumentParser(description='')
parser.add_argument('json', help='Input JSON')
parser.add_argument('--xml_out', help='Output Directory')
parser.add_argument('--version', action='version', version='%(prog)s ' + VERSION)
args = parser.parse_args()
return args
def main():
"""
Not including (Picard):
CREATE_INDEX - Handled by Galaxy internally.
help - duh
QUIET - can't think of a good reason to include this, people can just ignore it
version - Should be included, if anywhere, under the version tag
:return:
"""
args = supply_args()
tool_info = JsonTool(args.json)
myshell = XmlFiller(tool_info)
XmlWriter(myshell.to_write, args.xml_out, tool_info.tool_name).write_me()
class Mappings:
"""
Hold all of the mappings we need to provide for parameters.
"""
def __init__(self):
# These are parameters that are already covered in the macros.xml file. They also may be options that we
# don't want included in any GATK wrapper.
# REFERENCE_SEQUENCE: If reference is denoted optional then it should go in the optional section. Build function to place it.
# Should be able to utilize self.blob['required'] to determine section (either optional or main), then look at self.macro_to_chth to determine
# the macro that REFERENCE_SEQUENCE is associated to.
self.xml_json_type_map = {
'ArrayList[String]': 'text',
'boolean': 'boolean',
'Boolean': 'boolean',
'byte': 'integer',
'ClippingRepresentation': 'select',
'CopyNumberPosteriorExpectationMode': 'select',
'CountPileupType': 'select',
'DEPTH_OF_COVERAGE_OUTPUT_FORMAT': 'select',
'double': 'float',
'Double': 'float',
'DuplicateTaggingPolicy': 'select',
'EnumSet[Partition]': 'text',
'FeatureInput[Feature]': 'data',
'FeatureInput[VariantContext]': 'data',
'File': 'data',
'float': 'float',
'Float': 'float',
'Format': 'select',
'GatherType': 'select',
'GATKPath': 'text',
'GenotypeAssignmentMethod': 'select',
'GenotypingOutputMode': 'select',
'Implementation': 'select',
'int': 'integer',
'Integer': 'integer',
'IntervalMergingRule': 'select',
'IntervalSetRule': 'select',
'List[File]': 'data',
'List[Double]': 'text',
'List[FeatureInput[Feature]]': 'data',
'List[FeatureInput[VariantContext]]': 'data',
'List[GATKPath]': 'text',
'List[Integer]': 'integer',
'List[PairOrientation]': 'text',
'List[String]': 'text',
'List[Type]': 'text',
'LogLevel': 'select',
'long': 'integer',
'Long': 'integer',
'MarkDuplicatesScoringStrategy': 'select',
'Mode': 'select',
'NumberAlleleRestriction': 'select',
'OutputMode': 'select',
'PCRErrorModel': 'select',
'PrimaryAlignmentStrategy': 'select',
'ReferenceConfidenceMode': 'select',
'RunMode': 'select',
'Set[File]': 'data',
'Set[MetricAccumulationLevel]': 'text',
'Set[PairOrientation]': 'text',
'Set[String]': 'text',
'SortOrder': 'select',
'StrandSpecificity': 'select',
'Strategy': 'select',
'String': 'text',
'UnmappingReadStrategy': 'select',
'ValidationStringency': 'select',
'WriterType': 'select'
}
self.xml_json_num_map = {'Infinity': '', '-Infinity': '', 'NA': ''}
# If the parameter is in here, it won't be included in either the cheetah section or the param section.
# self.common_args = ("version", "showHidden", "help", "arguments_file", "VERBOSITY",
# "VALIDATION_STRINGENCY", "USE_JDK_INFLATER", "USE_JDK_DEFLATER", "TMP_DIR", "QUIET",
# "MAX_RECORDS_IN_RAM", "GA4GH_CLIENT_SECRETS", "CREATE_MD5_FILE", "CREATE_INDEX",
# "COMPRESSION_LEVEL", "REFERENCE_SEQUENCE", "OUTPUT", "SEQUENCE_DICTIONARY", "INPUT",
# "input", "reference", "output", "annotation", "annotation_group", "annotations_to_exclude",
# "intervals", "exclude_intervals", "read_index", "interval_padding",
# "interval_exclusion_padding", "output_prefix", "sequence_dictionary", "variant")
self.read_filter_args = ('')
# There seems to be a significant amount of information that can't be retrieved from the provided json.
# Make this in to a config file.
# Might be able to classify a bunch of this based on input output files for a particular tool. For instance, vcf_tabix
# is applicable if the input is a VCF.
# Optional status of a partiular parameters could be connected to macros as well. For instance, ref_sel is the macro for the reference genome.
# So, the 'ref_sel': 'reference' relation could be used along with 'required' status to place these.
# One challenge with this is the macros would have to either be prebuilt with the necessary section variable, or
# would have to be rewritten on the fly.
# For a parameter to be included here, it must have a consistent file format throughout all GATK4 JSONs.
# Otherwise, this will need to be specified as a macro, and macro will need to be connected to tool.
self.gen_out_fmt = {
'activity_profile_out': 'tabular',
'assembly_region_out': 'tabular',
'bam_output': 'bam',
'debug_assembly': 'txt',
'f1r2_tar_gz': 'txt',
'graph_output': 'txt',
'output_statistics': 'txt',
'SECOND_END_FASTQ': 'fastqsanger',
'tumor_segmentation': 'txt',
'UNPAIRED_FASTQ': 'fastqsanger'
}
# This is meant to be for those parameters that are booleans, but instruct for an output file to be created.
# This is a work in progress, will probably ignore these parameters for the time being.
self.out_create_params = {}
# If you put a parameter name here, and in the macro section, you will get both the macro and the
# cheetah for the parameter.
self.gen_in_fmt = {
'ALIGNED_BAM': 'sam',
'alleles': 'vcf',
'allelic_counts': 'tabular',
'annotated_intervals': 'gatk_interval',
'BAIT_INTERVALS': 'picard_interval_list',
'bam_shard': 'data',
'bam_with_header': 'sam',
'bqsr_recal_file': 'gatk_recal',
'calculate_coverage_over_genes': 'ncbi',
'clip_sequences_file': 'fasta',
'comp': 'vcf',
'comparison': 'vcf',
'concordance': 'vcf',
'contamination_fraction_per_sample_file': 'tabular',
'count_panel_of_normals': 'h5',
'dbsnp': 'vcf',
'DB_SNP': 'vcf',
'denoised_copy_ratios': 'tabular',
'discordance': 'vcf',
'feature_file': 'vcf',
'gatk_config_file': 'txt',
'germline_resource': 'vcf',
'intervals': 'gatk_interval',
'known_sites': 'vcf',
'mask': 'vcf',
'normal_allelic_counts': 'tabular',
'pedigree': 'tabular',
'population_callset': 'vcf',
'panel_of_normals': 'vcf',
'read_index': 'idx',
'recal_file': 'vcf',
'segments': 'tabular',
'stats': 'txt',
'TARGET_INTERVALS': 'picard_interval_list',
'tranches_file': 'txt',
'UNMAPPED_BAM': 'sam'
}
# Parameter will be treated similarly as gen_out_fmt params, but will need to be forced in to a file with given
# suffix via from_work_dir tag.
self.gen_out_fmt_from_work_dir = {
'f1r2_tar_gz': 'tar.gz',
}
# This is for the parameters in the XML
self.param_to_macro_xml = {'exclude_intervals': ['gatk_excl_ints'],
'intervals': ['gatk_ints'],
'REFERENCE_SEQUENCE': ['ref_sel'],
'REFERENCE': ['ref_sel'],
'reference': ['ref_sel'],
'resource': ['gatk_resource'],
'SEQUENCE_DICTIONARY': ['seq_dict_sel'],
'sequence_dictionary': ['seq_dict_sel']
}
# Template {<pname>: ((<main_chth>), (<pre_chth>))}
self.param_to_macro_tmpl = {'exclude_intervals': ['gatk_excl_ints_chth'],
'intervals': ['gatk_ints_chth'],
'REFERENCE_SEQUENCE': ['picard_ref_opts'],
'REFERENCE': ['picard_ref_opts_plain'],
'reference': ['ref_opts'],
'resource': ['gatk_resource_chth'],
'SEQUENCE_DICTIONARY': ['picard_seqdict_opts'],
'sequence_dictionary': ['gatk_seqdict']
}
self.param_to_macro_pretmpl = {'exclude_intervals': ['pre_gatk_excl_ints_chth'],
'intervals': ['pre_gatk_ints_chth'],
'resource': ['gatk_resource_pre_chth']
}
# For case where there is no required output parameter (output goes to stdout).
self.stdout_tools = ['CountBases', 'CountBasesSpark', 'CountReads', 'FlagStat', 'GermlineCNVCaller',
'DetermineGermlineContigPloidy']
self.stdout_chth = ['stdout_to_output']
self.stdout_macros = ['stdout_to_output_params']
# This simply creates an additional input that does nothing, it is only used to instruct Galaxy it is ok to
# start the next step in the workflow.
self.dummy_tools = ['GermlineCNVCaller']
self.dummy_macro = ['dummy_params']
# Actually we are not sanitizing, we are instead allowing more characters to pass through for these params.
self.sanitize = ['selectExpressions', 'filter_expression', 'genotype_filter_expression',
'invert_filter_expression', 'invert_genotype_filter_expression']
self.path_sanitize = {'GermlineCNVCaller': ['contig_ploidy_calls', 'output', 'model'],
'DetermineGermlineContigPloidy': ['output', 'model'],
'PostprocessGermlineCNVCalls': ['calls_shard_path', 'contig_ploidy_calls', 'model_shard_path']}
# Define which fields are associated with which xml data types.
self.param_tmpls = {'integer': ['name', 'argument', 'type', 'optional', 'value', 'min', 'max', 'label', 'help'],
'float': ['name', 'argument', 'type', 'optional', 'value', 'min', 'max', 'label', 'help'],
'text': ['name', 'argument', 'type', 'optional', 'value', 'label', 'help'],
'data': ['name', 'argument', 'type', 'optional', 'format', 'label', 'help'],
'select': ['name', 'argument', 'type', 'optional', 'label', 'help'],
'boolean': ['name', 'argument', 'type', 'truevalue', 'falsevalue', 'optional', 'checked', 'label', 'help'],
'output': ['format', 'name', 'label']}
# self.param_tmpls = {'integer': ['name', 'argument', 'type', 'optional', 'min', 'max', 'label', 'help'],
# 'float': ['name', 'argument', 'type', 'optional', 'min', 'max', 'label', 'help'],
# 'text': ['name', 'argument', 'type', 'optional', 'label', 'help'],
# 'data': ['name', 'argument', 'type', 'optional', 'format', 'label', 'help'],
# 'select': ['name', 'argument', 'type', 'optional', 'label', 'help'],
# 'boolean': ['name', 'argument', 'type', 'truevalue', 'falsevalue', 'optional', 'checked', 'label', 'help'],
# 'output': ['format', 'name', 'label']}
# Data type is wrong, correct it here.
self.wrong_type = {'GermlineCNVCaller': {'contig_ploidy_calls': 'text',
'output': 'text',
'model': 'text'},
'DetermineGermlineContigPloidy': {'model': 'text',
'output': 'text'},
'PostprocessGermlineCNVCalls': {'model_shard_path': 'text',
'contig_ploidy_calls': 'text',
'calls_shard_path': 'text'}
}
# Many parameters will take multiple Galaxy datatypes. Providing a mapping will give flexibility to add or
# subtract types when wanted.
self.file_type_map = {'vcf': 'vcf,vcf_bgzip',
'idx': 'tabix,bai',
'gatk_interval': 'gatk_interval,bed,vcf',
'sam': 'sam,bam',
'picard_interval_list': 'picard_interval_list,tabular',
'tabular': 'tabular,tsv',
'h5': 'h5,tabular,tsv'}
# Relate tool name to a common argument name, such as input (which can be connected to different file types),
# and a file type.
self.tool_file_type = {
'AnnotatePairOrientation': {'input': 'sam'},
'ApplyBQSR': {'input': 'sam'},
'ApplyVQSR': {'variant': 'vcf',
'recal_file': 'vcf',
'input': 'sam'},
'BaseRecalibrator': {'input': 'sam'},
'BwaMemIndexImageCreator': {'input': 'fasta'},
'CalculateContamination': {'input': 'txt'},
'ClipReads': {'input': 'sam'},
'CollectAlignmentSummaryMetrics': {'INPUT': 'sam'},
'CollectReadCounts': {'input': 'sam'},
'CollectRnaSeqMetrics': {'INPUT': 'sam',
'REF_FLAT': 'tabular'},
'CollectSequencingArtifactMetrics': {'INPUT': 'sam'},
'CombineGVCFs': {'variant': 'vcf',
'input': 'sam'},
'CountBases': {'input': 'sam'},
'CountBasesSpark': {'input': 'sam'},
'CountReads': {'input': 'sam'},
'DepthOfCoverage': {'input': 'sam'},
'DetermineGermlineContigPloidy': {'input': 'h5'},
'FilterMutectCalls': {'variant': 'vcf',
'input': 'sam'},
'FixCallSetSampleOrdering': {'input': 'sam'},
'FixMisencodedBaseQualityReads': {'input': 'sam'},
'FlagStat': {'input': 'sam'},
'GatherVcfsCloud': {'input': 'vcf'},
'GenotypeGVCFs': {'variant': 'vcf',
'input': 'sam'},
'GermlineCNVCaller': {'input': 'h5'},
'GetPileupSummaries': {'input': 'sam',
'variant': 'vcf'},
'GetSampleName': {'input': 'sam'},
'HaplotypeCaller': {'input': 'sam'},
'IntervalListToBed': {'INPUT': 'picard_interval_list'},
'LearnReadOrientationModel': {'input': 'txt'},
'LeftAlignIndels': {'input': 'sam'},
'MarkDuplicatesSpark': {'input': 'sam'},
'MergeBamAlignment': {''},
'Mutect2': {'input': 'sam'},
'PrintReads': {'input': 'sam'},
'SamToFastq': {'INPUT': 'sam'},
'SelectVariants': {'variant': 'vcf',
'input': 'sam'},
'SortSam': {'INPUT': 'sam'},
'SplitNCigarReads': {'input': 'sam'},
'SplitReads': {'input': 'sam'},
'VariantFiltration': {'variant': 'vcf',
'input': 'sam'},
'VariantRecalibrator': {'variant': 'vcf',
'input': 'sam'},
'VariantsToTable': {'variant': 'vcf'}
}
self.tool_output_file_type = {
'AnnotatePairOrientation': {'output': 'vcf'},
'ApplyBQSR': {'output': 'bam'},
'ApplyVQSR': {'output': 'vcf'},
'BaseRecalibrator': {'output': 'gatk_recal'},
'BwaMemIndexImageCreator': {'output': 'data'},
'CalculateContamination': {'output': 'txt'},
'ClipReads': {'output': 'sam'},
'CollectAlignmentSummaryMetrics': {'OUTPUT': 'txt'},
'CollectReadCounts': {'output': 'tabular'},
'CollectRnaSeqMetrics': {'OUTPUT': 'tabular'},
'CollectSequencingArtifactMetrics': {'OUTPUT': 'txt'},
'CombineGVCFs': {'output': 'vcf'},
'ConvertHeaderlessHadoopBamShardToBam': {'output': 'sam'},
'DepthOfCoverage': {'output': 'tsv'},
'FilterMutectCalls': {'output': 'vcf'},
'FixCallSetSampleOrdering': {'output': 'vcf'},
'FixMisencodedBaseQualityReads': {'output': 'sam'},
'GatherVcfsCloud': {'output': 'vcf'},
'GenotypeGVCFs': {'output': 'vcf'},
'GetPileupSummaries': {'output': 'txt'},
'GetSampleName': {'output': 'txt'},
'HaplotypeCaller': {'output': 'vcf'},
'IndexFeatureFile': {'output': 'data'},
'IntervalListToBed': {'OUTPUT': 'bed'},
'LearnReadOrientationModel': {'output': 'txt'},
'LeftAlignIndels': {'OUTPUT': 'sam'},
'MarkDuplicatesSpark': {'output': 'bam'},
'MergeBamAlignment': {'OUTPUT': 'bam'},
'Mutect2': {'output': 'vcf'},
'PlotModeledSegments': {'output': 'png'},
'PostprocessGermlineCNVCalls': {'output_genotyped_segments': 'vcf',
'output_genotyped_intervals': 'vcf',
'output_denoised_copy_ratios': 'tsv'},
'PrintReads': {'output': 'sam'},
'SamToFastq': {'FASTQ': 'fastqsanger'},
'SelectVariants': {'output': 'vcf'},
'SortSam': {'OUTPUT': 'bam'},
'SplitNCigarReads': {'output': 'bam'},
'VariantFiltration': {'output': 'vcf'},
'VariantRecalibrator': {'output': 'vcf',
'tranches_file': 'txt'},
'VariantsToTable': {'output': 'txt'}
}
# Relate the name of the argument to the selection of macros that goes with it.
self.macro_to_param = {
'input': {'sam': {'pre_chth': ['bam_index_pre_chth_req'],
'main_chth': ['gatk_bam_input_req'],
'main_xml': ['gatk_bam_req_params']},
'fasta': {'main_chth': ['ref_opts_input'],
'main_xml': ['ref_sel']},
'vcf': {'pre_chth': [],
'main_chth': [],
'main_xml': []}
},
'variant': {'vcf': {'pre_chth': ['gatk_tabix_multi'],
'main_chth': ['gatk_input_multi'],
'main_xml': ['vcf_input_params_multi']}
},
'INPUT': {'sam': {'pre_chth': ['bam_index_pre_chth_req'],
'main_chth': ['picard_bam_input'],
'main_xml': ['gatk_bam_req_params']}
},
'output': {'vcf': {'main_chth': ['vcf_output_opts'],
'main_xml': ['gzip_vcf_params'],
'out_xml': ['gzip_vcf_output_params']},
'png': {'main_chth': ['plotmodeledsegments_chth'],
'out_xml': ['plotmodeledsegments_output']}
}
}
# If the pattern is not followed, we will override with a per-tool definition.
self.special_macros = {
'ApplyVQSR': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
},
'CombineGVCFs': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
},
'DepthOfCoverage': {'output': {'pre_chth': [],
'main_chth': ['doc_output_chth'],
'out_xml': ['doc_outputs']}
},
'FilterMutectCalls': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
},
'GenotypeGVCFs': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
},
'Mutect2': {'output': {'main_chth': ['vcf_output_opts'],
'main_xml': ['gzip_vcf_params'],
'out_xml': ['mutect_stats']}
},
# 'PlotModeledSegments': {'output': {'main_chth': ['plotmodeledsegments_chth'],
# 'out_xml': ['plotmodeledsegments_output']}
# },
'SelectVariants': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
},
'SortSam': {'INPUT': {'pre_chth': ['bam_index_pre_chth_no_index'],
'main_chth': ['picard_bam_input'],
'main_xml': ['gatk_bam_req_params']}
},
'MarkDuplicatesSpark': {'input': {'pre_chth': ['bam_index_pre_chth_no_index'],
'main_chth': ['gatk_bam_input_req'],
'main_xml': ['gatk_bam_req_params']}
},
'VariantFiltration': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
},
'VariantRecalibrator': {'input': {'pre_chth': ['bam_index_pre_chth'],
'main_chth': ['gatk_bam_input'],
'main_xml': ['gatk_bam_req_params_opt']}
}
}
class XmlWriter:
"""
"""
def __init__(self, to_write, outdir, tool_name, id_prefix='gatk4_auto_'):
self.to_write = to_write
self.outdir = outdir
self.id_prefix = id_prefix
self.tool_name = tool_name.lower().split(' ')[0]
def write_me(self):
handle_out = open(self._create_output_loc(), 'w')
handle_out.write(self.to_write)
handle_out.close()
def _create_output_loc(self):
"""
Create the output file name for writing, based on input folder.
:return:
"""
self.output_name = [self.outdir, self.id_prefix + self.tool_name + '.xml']
if not self.outdir.endswith('/'):
return '/'.join(self.output_name)
else:
return ''.join(self.output_name)
class XmlEtrees:
"""
Hold all of the etrees we need for the structure.
"""
def __init__(self, args):
"""
Provide templates for the shell of the XML file.
:return:
"""
self.args = args
self.shell_dict = self.args.shell_dict
self.tool = etree.Element('tool', id=self.args.id_prefix + self.shell_dict['id'],
name=self.shell_dict['name'], version="@WRAPPER_VERSION@0",
profile=self.args.profile)
description = etree.SubElement(self.tool, 'description')
description.text = '- ' + self.shell_dict['description']
macros = etree.SubElement(self.tool, 'macros')
macros_imp = etree.SubElement(macros, 'import')
macros_imp.text = 'macros.xml'
exp_reqs = etree.SubElement(self.tool, 'expand', macro='requirements')
exp_vers = etree.SubElement(self.tool, 'expand', macro='version_cmd')
self.command = etree.SubElement(self.tool, 'command', detect_errors='exit_code')
self.inputs = etree.SubElement(self.tool, 'inputs')
#self.req_inputs = self._section_write('required')
self.opt_inputs = self._section_write('optional')
self.adv_inputs = self._section_write('advanced')
self.comm_inputs = self._section_write('common')
self.dep_inputs = self._section_write('deprecated')
self.output_sect = etree.SubElement(self.inputs, 'section', name='output_opt',
title='Additional Output Parameters', expanded='False')
self.outputs = etree.SubElement(self.tool, 'outputs')
tests = etree.SubElement(self.tool, 'tests')
help = etree.SubElement(self.tool, 'help')
help.text = etree.CDATA(self.shell_dict['summary'])
citations = etree.SubElement(self.tool, 'citations')
exp_cit = etree.SubElement(citations, 'expand', macro='citations')
def _section_write(self, section):
"""
Put section together
:param section:
:param xml_sect:
:return:
"""
title = ' '.join([section.title(), 'Parameters'])
return etree.SubElement(self.inputs, 'section', name=section, title=title, expanded='False')
def _build_conditional(self):
"""
Put the conditional together that acts as a section.
Example:
<conditional name="output_opt">
<param name="output_opt_sel" type="select" label="Additional output parameters?">
<option value="yes">yes</option>
<option value="no" selected="true">no</option>
</param>
<when value="yes">
<param argument="--graph_output_sel" checked="false" falsevalue="" help="Write debug assembly graph information to this\
file" label="Graph Output" name="graph_output_sel" optional="true" truevalue="--graph_output_sel" type="boolean"/>
</when>
</conditional>
:return:
"""
# HERE
self.output_sect = etree.SubElement(self.inputs, 'conditional', name='output_opt')
self.output_sect_sel = etree.SubElement(self.output_sect, 'param', name='output_opt_sel', type='select', label='Additional output parameters?')
self.opt_yes = etree.SubElement(self.output_sect_sel, 'option', value='yes')
self.opt_yes.text = 'yes'
self.opt_no = etree.SubElement(self.output_sect_sel, 'option', value='no', selected='true')
self.opt_no.text = 'no'
self.when_yes = etree.SubElement(self.output_sect, 'when', value='yes')
def build_inputs_out_sel(self, params, parent):
"""
Build extra options under the additional outputs section to provide booleans for choosing optional outputs.
<param name="sites-only-vcf-output" argument="--sites-only-vcf-output" type="boolean" truevalue="--sites-only-vcf-output" falsevalue="" optional="true" checked="false" label="Sites Only Vcf Output" help="If true, don&apos;t emit genotype fields when writing vcf file output."/>
:return:
"""
for param in params:
new_name = param['name'] + '_sel'
new_label = param['name'].replace('_', ' ').title()
new_arg = '--' + new_name
this_param = etree.SubElement(parent, 'param', name=new_name, argument=new_arg, type='boolean',
truevalue=new_arg, falsevalue='', optional='true', checked='false',
label=new_label, help=param['help'])
def build_from_work_dir_out(self, params, parent):
"""
Work with the output parameters that come in as booleans. These should be set in the output_opt section
using the given parameter name, and then set in the output section with a "from_work_dir" value.
"""
for param in params:
new_label = param['name'].replace('_', ' ').title()
new_arg = '--' + param['name'].replace('_', '-')
this_param = etree.SubElement(parent, 'param', name=param['name'], argument=new_arg, type='boolean',
truevalue=new_arg, falsevalue='', optional='true', checked='false',
label=new_label, help=param['help'])
def build_sel_opt(self, this_param, sel_blob):
"""
Add select option statements if this is of select type.
[{'summary': '', 'name': 'ALL'}, {'summary': '', 'name': 'OVERLAPPING_ONLY'}]
:return:
"""
for sel in sel_blob:
this_sel = etree.SubElement(this_param, 'option', selected=sel['selected'], value=sel['value'])
this_sel.text = sel['value']
class XmlTemplates(object):
def __init__(self):
# Cheetah section
self.chth_tmpl = PercentTemplate('#include source=$%macro#')
# Required argument.
self.req_out_chth = PercentTemplate('%argument $%name')
# Required boolean argument.
self.req_out_chth_bool = PercentTemplate('$%name')
# May want to move the below templates out to the macros section. These are a little more general
# since they may apply to any given non-required vcf argument.
# for $num, $txt in enumerate($optional.selectExpressions_rpt)
# if $txt.selectExpressions
# --selectExpressions '$txt.selectExpressions'
# end if
# end for
self.rpt_opt = PercentTemplate('#for $num, $txt in enumerate($%section.%{name}_rpt)'
'\n#if $txt.%name'
'\n%argument \'$txt.%name\''
'\n#end if'
'\n#end for')
self.rpt_req = PercentTemplate('#for $num, $txt in enumerate($%{name}_rpt)'
'\n#if $txt.%name'
'\n%argument \'$txt.%name\''
'\n#end if'
'\n#end for')
self.vcf_choose = PercentTemplate('#if $%section.%name'
'\n#if $%section.%name.is_of_type("vcf_bgzip")'
'\n%argument %name.vcf.gz'
'\n#else'
'\n%argument %name.vcf'
'\n#end if'
'\n#end if')
self.vcf_choose_req = PercentTemplate('#if $%name'
'\n#if $%name.is_of_type("vcf_bgzip")'
'\n%argument %name.vcf.gz'
'\n#else'
'\n%argument %name.vcf'
'\n#end if'
'\n#end if')
self.vcf_tabix = PercentTemplate('#if $%section.%name'
'\n#set datatype = $%section.%name.datatype'
'\n#if $%section.%name.is_of_type("vcf_bgzip")'
'\nln -s $%section.%name %name.vcf.gz &&'
'\ntabix %name.vcf.gz &&'
'\n#else'
'\nln -s $%section.%name %name.vcf &&'
'\ngatk IndexFeatureFile -I %name.vcf &&'
'\n#end if'
'\n#end if')
self.vcf_tabix_req = PercentTemplate('#if $%name'
'\n#set datatype = $%name.datatype'
'\n#if $%name.is_of_type("vcf_bgzip")'
'\nln -s $%name %name.vcf.gz &&'
'\ntabix %name.vcf.gz &&'
'\n#else'
'\nln -s $%name %name.vcf &&'
'\ngatk IndexFeatureFile -I %name.vcf &&'
'\n#end if'
'\n#end if')
self.file_chth = PercentTemplate('#if $%section.%out_sel_name\n%argument $%name\n#end if')
self.file_chth_fwd = PercentTemplate('#if $%section.%out_sel_name\n%argument %name\n#end if')
self.ext_arg = PercentTemplate('#if $%section.%name\n %argument $%section.%name\n#end if\n')
self.ext_arg_bool = PercentTemplate('#if $%section.%name\n $%section.%name\n#end if\n')
self.reg_arg = '#if $%name\n %argument $%name\n#end if\n'
# XML section. Most of this is handled via etrees, but some cases are more easily handled here.
self.xml_tmpl = Template('<expand macro="$macro_name" />')
self.sel_out = Template('<option value="$value" selected="$selected">$value</option>')
self.out_label_tmpl = Template('$${tool.name} on $${on_string}: $name $format')
self.out_tmpl = Template('<data format="$format" name="$name" label="$${tool.name} on $${on_string}: $format" />')
class XmlFiller(XmlEtrees, XmlTemplates, Mappings):
"""
"""
def __init__(self, tool_args):
XmlEtrees.__init__(self, tool_args)
XmlTemplates.__init__(self)
Mappings.__init__(self)
self.tool_args = tool_args.args
self.command.text = etree.CDATA(self._command_fill())
self._fill_macros()
self._fill_output_params()
self._fill_params()
rm_sect = self._blank_sect_rm()
self.to_write = etree.tostring(self.tool, pretty_print=True, encoding="unicode")
def _file_type_map(self):
"""
Define the actual file types we should include for an element.
:return:
"""
return {'vcf': 'vcf,vcf_bgzip',
'idx': 'tabix,bai',
'gatk_interval': 'gatk_interval,bed,vcf',
'sam': 'sam,bam',
'picard_interval_list': 'picard_interval_list,tabular',
'tabular': 'tabular,tsv',
'h5': 'h5,tabular,tsv'}
def _fill_output_params(self):
"""
Same as _fill_params, but for the outputs element.
:return:
"""
for arg in self.tool_args:
if not arg.macros['out_xml']:
if arg.is_output:
# Set a better label for output types.
arg.xml_param['label'] = self.out_label_tmpl.substitute(arg.xml_param)
if arg.is_req:
self._set_param(arg, self.outputs, 'data')
else:
this_param = self._set_param(arg, self.outputs, 'data')
filt = etree.SubElement(this_param, 'filter')
filt.text = 'output_opt[\'' + arg.xml_out['name'] + '_sel\']'
def _fill_params(self):
"""
Get all of the parameters placed in the correct sections.
# self.build_inputs(self.tool_xml, self.inputs, 'param')
if ftype in self.file_type_map:
return self.file_type_map[ftype]
:return:
"""
for arg in self.tool_args:
if not arg.macros['main_xml']:
# Expand file types to include additional.
the_map = self._file_type_map()
if arg.ftype in the_map:
arg.xml_param['format'] = the_map[arg.ftype]
tag = 'param'
if arg.kind == 'required' and not arg.is_output:
self._set_param(arg, self.inputs, tag, first=True)
elif arg.kind == 'optional':
self._set_param(arg, self.opt_inputs, tag)
elif arg.kind == 'advanced':
self._set_param(arg, self.adv_inputs, tag)
elif arg.kind == 'common':
self._set_param(arg, self.comm_inputs, tag)
elif arg.kind == 'deprecated':
self._set_param(arg, self.dep_inputs, tag)
elif arg.kind == 'output_opt':
self._build_inputs_out_sel(arg.xml_out, self.output_sect)
elif arg.is_output:
pass
else:
raise Exception("Unrecognized section type %s." % arg.kind)
def _build_inputs_out_sel(self, param, parent):
"""
Build extra options under the additional outputs section to provide booleans for choosing optional outputs.
<param name="sites-only-vcf-output" argument="--sites-only-vcf-output" type="boolean" truevalue="--sites-only-vcf-output" falsevalue="" optional="true" checked="false" label="Sites Only Vcf Output" help="If true, don&apos;t emit genotype fields when writing vcf file output."/>
:return:
"""
new_name = param['name'] + '_sel'
new_label = param['name'].replace('_', ' ').title()
new_arg = '--' + new_name
return etree.SubElement(parent, 'param', name=new_name, argument=new_arg, type='boolean',
truevalue=new_arg, falsevalue='', optional='true', checked='false',
label=new_label, help=param['help'])
def _set_param(self, param, parent, elem, first=False):
"""
:param ToolArgXml
:return:
"""
if param.is_rpt:
rpt_name = '_'.join([param.pname, 'rpt'])
# rpt_title = ' '.join([param.pname, 'Repeat'])
rpt_title = param.xml_param['label']
rpt_param = etree.SubElement(parent, 'repeat', name=rpt_name, default="1", title=rpt_title)
this_param = etree.SubElement(rpt_param, elem)
if first:
parent.insert(0, rpt_param)
else:
this_param = etree.SubElement(parent, elem)
if first:
parent.insert(0, this_param)
for k, v in param.xml_param.items():
if (k == 'min') and (v == ''):
pass
elif (k == 'max') and (v == ''):
pass
else:
this_param.set(k, v)
if param.is_sel:
self._build_sel_opt(this_param, param.sel_blob)
if param.sani:
self._build_sani(this_param)
if param.path_sani:
self._build_path_sani(this_param)
if param.fwd_file:
this_param.set('from_work_dir', param.fwd_file)
return this_param
def _build_path_sani(self, param):
"""
Create XML for path sanitizer tags.
<sanitizer>
<valid initial="string.ascii_letters,string.digits">
<add value="/" />
<add value="_" />
<add value="-" />
<add value="." />
<add value="$" />
</valid>
</sanitizer>
:param param:
:return:
"""
this_sani = etree.SubElement(param, 'sanitizer')
sani_valid = etree.SubElement(this_sani, 'valid', initial="string.ascii_letters,string.digits")
etree.SubElement(sani_valid, 'add', value="/")
etree.SubElement(sani_valid, 'add', value="_")
etree.SubElement(sani_valid, 'add', value="-")
etree.SubElement(sani_valid, 'add', value=".")
etree.SubElement(sani_valid, 'add', value="$")
def _build_sani(self, param):
"""
Create the XML for sanitizer tags.
:return:
"""
this_sani = etree.SubElement(param, 'sanitizer')
sani_valid = etree.SubElement(this_sani, 'valid', initial="string.printable")
def _build_sel_opt(self, this_param, sel_blob):
"""
Add select option statements if this is of select type.
[{'summary': '', 'name': 'ALL'}, {'summary': '', 'name': 'OVERLAPPING_ONLY'}]
:return:
"""
for sel in sel_blob:
if 'selected' in sel:
this_sel = etree.SubElement(this_param, 'option', selected=sel['selected'], value=sel['value'])
else:
this_sel = etree.SubElement(this_param, 'option', value=sel['value'])
this_sel.text = sel['value']
def _fill_macros(self):
"""
Get the macro tags placed correctly in each section.
macros = {'main_chth': [],
'pre_chth': [],
'main_xml': [],
'out_xml': []}
:return:
"""
for arg in self.tool_args:
main_macs = arg.macros['main_xml']
out_macs = arg.macros['out_xml']
for macro in main_macs:
if arg.kind == 'required':
self.inputs.insert(0, etree.SubElement(self.inputs, 'expand', macro=macro))
elif arg.kind == 'optional':
etree.SubElement(self.opt_inputs, 'expand', macro=macro)
elif arg.kind == 'advanced':
etree.SubElement(self.adv_inputs, 'expand', macro=macro)
elif arg.kind == 'common':
etree.SubElement(self.comm_inputs, 'expand', macro=macro)
elif arg.kind == 'deprecated':
etree.SubElement(self.dep_inputs, 'expand', macro=macro)
else:
raise Exception("Unrecognized section type %s." % arg.kind)
for macro in out_macs:
etree.SubElement(self.outputs, 'expand', macro=macro)
if arg.tool_name in self.stdout_tools:
for entry in self.stdout_macros:
etree.SubElement(self.outputs, 'expand', macro=entry)
if arg.tool_name in self.dummy_tools:
for entry in self.dummy_macro:
self.inputs.insert(0, etree.SubElement(self.inputs, 'expand', macro=entry))
def _blank_sect_rm(self):
"""
Remove section elements that don't have any children. Provide list of removed elements.
:return:
"""
removed = []
for element in self.inputs.findall('section'):
if not element.getchildren():
self.inputs.remove(element)
removed.append(element)
return removed
def _command_fill(self):
"""
:return:
"""
command = []
command.append(self.chth_tmpl.substitute(macro='set_sections'))
for arg in self.tool_args:
command.extend(arg.chth_pre)
command.append(Template('@CMD_BEGIN@ $short_name').substitute(self.shell_dict))
for arg in self.tool_args:
command.extend(arg.chth)
# This is the only reason we are inheriting from Mappings...
tname = self.tool_args[0].tool_name
if tname in self.stdout_tools:
for entry in self.stdout_chth:
command.append(self.chth_tmpl.substitute(macro=entry))
return '\n'.join(command)
class CheetahPrep(XmlTemplates):
"""
Take arguments, then prep the Cheetah string.
Rules:
is_req (req_out_chth) (no section)
is_req && has_macro (chth_tmpl) (no section)
is_req && has_pre_macro (chth_tmpl) (no section)
all non-req args go in sections
not_req (ext_arg)
not_req && has_macro (chth_tmpl) (refer to section macro)
not_req && has_pre_macro (chth_tmpl) (refer to section macro)
Need pname, macro name
"""
def __init__(self, pname, argname, section, is_req=False, is_input_vcf=False, mname=None, pre_mname=None,
is_bool=False, out_sel_name=None, is_rpt=False, fwd_file=None):
XmlTemplates.__init__(self)
# Output is required, and there is a macro in my_xml.macros_tmpl.
self.pname = pname
self.argname = argname
self.section = section
self.mname = mname
self.pre_mname = pre_mname
self.is_req = is_req
self.is_input_vcf = is_input_vcf
self.is_bool = is_bool
self.out_sel_name = out_sel_name
self.is_rpt = is_rpt
self.fwd_file = fwd_file
self.chth = self._chth_create()
def _create_section_macro(self, macro):
"""
Create a macro name appropriate for macros listed in sections.
:param macro:
:return:
"""
return '_'.join([macro, self.section])
def _chth_create(self):
"""
Logic to decide which Cheetah template to return.
# self.chth_pre.append(CheetahPrep(self.pname, self.xml_out['argument'], self.kind, self.is_req,
# self.is_input_vcf, pre_mname='blah').chth)
:return:
"""
if self.is_input_vcf and not self.is_req and not self.pre_mname:
return self.vcf_choose.substitute(section=self.section, name=self.pname, argument=self.argname)
if self.is_input_vcf and self.is_req and not self.pre_mname and not self.mname:
return self.vcf_choose_req.substitute(section=self.section, name=self.pname, argument=self.argname)
if self.is_input_vcf and not self.is_req and not self.mname:
return self.vcf_tabix.substitute(section=self.section, name=self.pname)
if self.is_input_vcf and self.is_req and self.pre_mname and not self.mname:
return self.vcf_tabix_req.substitute(name=self.pname)
if not self.is_req and self.is_rpt and not self.mname and not self.pre_mname:
return self.rpt_opt.substitute(section=self.section, argument=self.argname, name=self.pname)
if self.is_req and self.is_rpt and not self.mname and not self.pre_mname:
return self.rpt_req.substitute(argument=self.argname, name=self.pname)
if self.is_req and self.mname and not self.pre_mname:
return self.chth_tmpl.substitute(macro=self.mname)
if self.is_req and not self.mname and not self.pre_mname and self.is_bool:
return self.req_out_chth_bool.substitute(name=self.pname)
if self.is_req and not self.mname and not self.pre_mname and not self.is_bool:
return self.req_out_chth.substitute(argument=self.argname, name=self.pname)
if self.is_req and not self.mname and self.pre_mname:
return self.chth_tmpl.substitute(macro=self.pre_mname)
if not self.is_req and self.out_sel_name and not self.fwd_file: