|
1 | 1 | #!/opt/common/CentOS_6-dev/python/python-2.7.10/bin/python
|
2 | 2 |
|
3 | 3 | import argparse, os, sys, re, subprocess, itertools, glob
|
| 4 | +from operator import attrgetter |
| 5 | +import textwrap as _textwrap |
4 | 6 | import cmo
|
5 | 7 |
|
| 8 | +# Custom help formatter to display args in alphabetical order, and fitted line wrap for sphinx |
| 9 | +class SortingHelpFormatter(argparse.ArgumentDefaultsHelpFormatter): |
| 10 | + def add_arguments(self, actions): |
| 11 | + actions = sorted(actions, key=attrgetter('option_strings')) |
| 12 | + super(SortingHelpFormatter, self).add_arguments(actions) |
| 13 | + def _split_lines(self, text, width): |
| 14 | + text = self._whitespace_matcher.sub(' ', text).strip() |
| 15 | + return _textwrap.wrap(text, 78) |
| 16 | + |
| 17 | +# Function that runs --help on the tool we've wrapped, and extracts documentation |
6 | 18 | def parse_script_help(script_path):
|
7 | 19 | perl = cmo.util.programs['perl']['default']
|
8 |
| - help_text = subprocess.Popen(" ".join([perl, script_path, "-h"]),stdout=subprocess.PIPE,shell=True).communicate()[0] |
9 |
| - valid_args = re.findall(r"\s+(--[\S_]+)\s+([\S \t]+)\n?", help_text, re.M) |
10 |
| - return dict(valid_args) |
| 20 | + help_text = subprocess.Popen(" ".join([perl, script_path, "-h"]), stdout=subprocess.PIPE, shell=True).communicate()[0] |
| 21 | + valid_args = re.findall(r"^\s*(--\S+)\s+([^\[\n]+)", help_text, re.M) |
| 22 | + defaults = re.findall(r"^\s*(--\S+)\s+[\S ]+\[([\S ]+)\]$", help_text, re.M) |
| 23 | + return dict(valid_args), dict(defaults) |
11 | 24 |
|
12 | 25 | if __name__ =='__main__':
|
13 | 26 | # We'll first need to figure out which version to run with "-h" to parse the help text
|
14 |
| - preparser = argparse.ArgumentParser(description="run maf2vcf", add_help=False) |
15 |
| - preparser.add_argument("--version", choices=cmo.util.programs['vcf2maf'].keys(), default="default") |
| 27 | + preparser = argparse.ArgumentParser(description="Run maf2vcf", add_help=False, formatter_class=SortingHelpFormatter) |
| 28 | + preparser.add_argument("--version", help="Version of tool to run", choices=cmo.util.programs['vcf2maf'].keys(), default="default") |
| 29 | + preparser.add_argument("--ncbi-build", help="Genome build of variants in input", choices=["GRCh37","GRCh38","GRCm38"], default="GRCh37") |
16 | 30 | options, _ = preparser.parse_known_args()
|
| 31 | + |
| 32 | + # Figure out the path to the actual Perl script that this Python wrapper will run |
17 | 33 | script_path = cmo.util.programs['vcf2maf'][options.version] + "maf2vcf.pl"
|
18 |
| - args_dict = parse_script_help(script_path) |
19 |
| - parser = argparse.ArgumentParser(parents = [preparser], add_help=True) |
| 34 | + # Extract arguments and their defaults, by parsing the --help output |
| 35 | + args_dict, defaults_dict = parse_script_help(script_path) |
| 36 | + |
| 37 | + # With arguments and defaults set, let's construct an argparse instance |
| 38 | + parser = argparse.ArgumentParser(parents = [preparser], add_help=True, formatter_class=SortingHelpFormatter) |
20 | 39 | for arg, description in args_dict.items():
|
21 |
| - if arg == "--help": |
| 40 | + # Hide a few arguments from the user, because we'll determine them ourselves |
| 41 | + if arg in ["--help","--man","--ref-fasta"]: |
22 | 42 | continue
|
23 |
| - parser.add_argument(arg,action="store", metavar='', help=description) |
24 |
| - cmo.util.add_logging_options(parser) |
| 43 | + if arg in defaults_dict and arg not in ["--output-maf"]: |
| 44 | + parser.add_argument(arg, action="store", metavar='', help=description, default=defaults_dict[arg]) |
| 45 | + else: |
| 46 | + parser.add_argument(arg, action="store", metavar='', help=description) |
| 47 | + |
| 48 | + # Now run the argparse instance, which will parse and execute, or print help text if requested |
25 | 49 | args = parser.parse_args()
|
26 | 50 | args_dict = vars(args)
|
| 51 | + |
| 52 | + # Locate the reference for this genome build |
| 53 | + args_dict['ref_fasta'] = cmo.util.genomes[args.ncbi_build]['fasta'] |
| 54 | + |
| 55 | + # Remove arguments that the actual wrapped tool won't recognize |
27 | 56 | for key in ["version"]:
|
28 | 57 | del args_dict[key]
|
| 58 | + |
| 59 | + # Build the command we're going to run |
29 | 60 | cmd = [cmo.util.programs['perl']['default'], script_path]
|
30 |
| - stderr = args.stderr |
31 |
| - stdout = args.stdout |
| 61 | + # Trim out arguments without values |
32 | 62 | args_dict = dict((k, v) for k, v in args_dict.iteritems() if v)
|
33 |
| - cmo.util.remove_logging_options_from_dict(args_dict) |
| 63 | + |
| 64 | + # Make sure the arguments are in a format that the script will accept, and kick it off |
34 | 65 | for arg, value in args_dict.items():
|
35 | 66 | arg = arg.replace("_","-")
|
36 | 67 | cmd = cmd + ["--"+arg, value]
|
37 |
| - cmo.util.call_cmd(" ".join(cmd), stdout=stdout, stderr=stderr) |
| 68 | + sys.stderr.write( "RUNNING: " + " ".join( cmd ) + "\n" ) |
| 69 | + cmo.util.call_cmd( " ".join( cmd )) |
0 commit comments