-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mskcc/cmo
- Loading branch information
Showing
4 changed files
with
155 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,69 @@ | ||
#!/opt/common/CentOS_6-dev/python/python-2.7.10/bin/python | ||
|
||
import argparse, os, sys, re, subprocess, itertools, glob | ||
from operator import attrgetter | ||
import textwrap as _textwrap | ||
import cmo | ||
|
||
# Custom help formatter to display args in alphabetical order, and fitted line wrap for sphinx | ||
class SortingHelpFormatter(argparse.ArgumentDefaultsHelpFormatter): | ||
def add_arguments(self, actions): | ||
actions = sorted(actions, key=attrgetter('option_strings')) | ||
super(SortingHelpFormatter, self).add_arguments(actions) | ||
def _split_lines(self, text, width): | ||
text = self._whitespace_matcher.sub(' ', text).strip() | ||
return _textwrap.wrap(text, 78) | ||
|
||
# Function that runs --help on the tool we've wrapped, and extracts documentation | ||
def parse_script_help(script_path): | ||
perl = cmo.util.programs['perl']['default'] | ||
help_text = subprocess.Popen(" ".join([perl, script_path, "-h"]),stdout=subprocess.PIPE,shell=True).communicate()[0] | ||
valid_args = re.findall(r"\s+(--[\S_]+)\s+([\S \t]+)\n?", help_text, re.M) | ||
return dict(valid_args) | ||
help_text = subprocess.Popen(" ".join([perl, script_path, "-h"]), stdout=subprocess.PIPE, shell=True).communicate()[0] | ||
valid_args = re.findall(r"^\s*(--\S+)\s+([^\[\n]+)", help_text, re.M) | ||
defaults = re.findall(r"^\s*(--\S+)\s+[\S ]+\[([\S ]+)\]$", help_text, re.M) | ||
return dict(valid_args), dict(defaults) | ||
|
||
if __name__ =='__main__': | ||
# We'll first need to figure out which version to run with "-h" to parse the help text | ||
preparser = argparse.ArgumentParser(description="run maf2vcf", add_help=False) | ||
preparser.add_argument("--version", choices=cmo.util.programs['vcf2maf'].keys(), default="default") | ||
preparser = argparse.ArgumentParser(description="Run maf2vcf", add_help=False, formatter_class=SortingHelpFormatter) | ||
preparser.add_argument("--version", help="Version of tool to run", choices=cmo.util.programs['vcf2maf'].keys(), default="default") | ||
preparser.add_argument("--ncbi-build", help="Genome build of variants in input", choices=["GRCh37","GRCh38","GRCm38"], default="GRCh37") | ||
options, _ = preparser.parse_known_args() | ||
|
||
# Figure out the path to the actual Perl script that this Python wrapper will run | ||
script_path = cmo.util.programs['vcf2maf'][options.version] + "maf2vcf.pl" | ||
args_dict = parse_script_help(script_path) | ||
parser = argparse.ArgumentParser(parents = [preparser], add_help=True) | ||
# Extract arguments and their defaults, by parsing the --help output | ||
args_dict, defaults_dict = parse_script_help(script_path) | ||
|
||
# With arguments and defaults set, let's construct an argparse instance | ||
parser = argparse.ArgumentParser(parents = [preparser], add_help=True, formatter_class=SortingHelpFormatter) | ||
for arg, description in args_dict.items(): | ||
if arg == "--help": | ||
# Hide a few arguments from the user, because we'll determine them ourselves | ||
if arg in ["--help","--man","--ref-fasta"]: | ||
continue | ||
parser.add_argument(arg,action="store", metavar='', help=description) | ||
cmo.util.add_logging_options(parser) | ||
if arg in defaults_dict and arg not in ["--output-maf"]: | ||
parser.add_argument(arg, action="store", metavar='', help=description, default=defaults_dict[arg]) | ||
else: | ||
parser.add_argument(arg, action="store", metavar='', help=description) | ||
|
||
# Now run the argparse instance, which will parse and execute, or print help text if requested | ||
args = parser.parse_args() | ||
args_dict = vars(args) | ||
|
||
# Locate the reference for this genome build | ||
args_dict['ref_fasta'] = cmo.util.genomes[args.ncbi_build]['fasta'] | ||
|
||
# Remove arguments that the actual wrapped tool won't recognize | ||
for key in ["version"]: | ||
del args_dict[key] | ||
|
||
# Build the command we're going to run | ||
cmd = [cmo.util.programs['perl']['default'], script_path] | ||
stderr = args.stderr | ||
stdout = args.stdout | ||
# Trim out arguments without values | ||
args_dict = dict((k, v) for k, v in args_dict.iteritems() if v) | ||
cmo.util.remove_logging_options_from_dict(args_dict) | ||
|
||
# Make sure the arguments are in a format that the script will accept, and kick it off | ||
for arg, value in args_dict.items(): | ||
arg = arg.replace("_","-") | ||
cmd = cmd + ["--"+arg, value] | ||
cmo.util.call_cmd(" ".join(cmd), stdout=stdout, stderr=stderr) | ||
sys.stderr.write( "RUNNING: " + " ".join( cmd ) + "\n" ) | ||
cmo.util.call_cmd( " ".join( cmd )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/opt/common/CentOS_6-dev/python/python-2.7.10/bin/python | ||
|
||
import argparse, os, sys, re, subprocess, itertools, glob | ||
from operator import attrgetter | ||
import textwrap as _textwrap | ||
import cmo | ||
|
||
# Custom help formatter to display args in alphabetical order, and fitted line wrap for sphinx | ||
class SortingHelpFormatter(argparse.ArgumentDefaultsHelpFormatter): | ||
def add_arguments(self, actions): | ||
actions = sorted(actions, key=attrgetter('option_strings')) | ||
super(SortingHelpFormatter, self).add_arguments(actions) | ||
def _split_lines(self, text, width): | ||
text = self._whitespace_matcher.sub(' ', text).strip() | ||
return _textwrap.wrap(text, 78) | ||
|
||
# Function that runs --help on the tool we've wrapped, and extracts documentation | ||
def parse_script_help(script_path): | ||
perl = cmo.util.programs['perl']['default'] | ||
help_text = subprocess.Popen(" ".join([perl, script_path, "-h"]), stdout=subprocess.PIPE, shell=True).communicate()[0] | ||
valid_args = re.findall(r"^\s*(--\S+)\s+([^\[\n]+)", help_text, re.M) | ||
defaults = re.findall(r"^\s*(--\S+)\s+[\S ]+\[([\S ]+)\]$", help_text, re.M) | ||
return dict(valid_args), dict(defaults) | ||
|
||
if __name__ =='__main__': | ||
# We'll first need to figure out which version to run with "-h" to parse the help text | ||
preparser = argparse.ArgumentParser(description="Run vcf2vcf", add_help=False, formatter_class=SortingHelpFormatter) | ||
preparser.add_argument("--version", help="Version of tool to run", choices=cmo.util.programs['vcf2maf'].keys(), default="default") | ||
preparser.add_argument("--ncbi-build", help="Genome build of variants in input", choices=["GRCh37","GRCh38","GRCm38"], default="GRCh37") | ||
options, _ = preparser.parse_known_args() | ||
|
||
# Figure out the path to the actual Perl script that this Python wrapper will run | ||
script_path = cmo.util.programs['vcf2maf'][options.version] + "vcf2vcf.pl" | ||
# Extract arguments and their defaults, by parsing the --help output | ||
args_dict, defaults_dict = parse_script_help(script_path) | ||
|
||
# With arguments and defaults set, let's construct an argparse instance | ||
parser = argparse.ArgumentParser(parents = [preparser], add_help=True, formatter_class=SortingHelpFormatter) | ||
for arg, description in args_dict.items(): | ||
# Hide a few arguments from the user, because we'll determine them ourselves | ||
if arg in ["--help","--man","--ref-fasta"]: | ||
continue | ||
if arg in defaults_dict and arg not in ["--output-maf"]: | ||
parser.add_argument(arg, action="store", metavar='', help=description, default=defaults_dict[arg]) | ||
else: | ||
parser.add_argument(arg, action="store", metavar='', help=description) | ||
|
||
# Now run the argparse instance, which will parse and execute, or print help text if requested | ||
args = parser.parse_args() | ||
args_dict = vars(args) | ||
|
||
# Locate the reference for this genome build | ||
args_dict['ref_fasta'] = cmo.util.genomes[args.ncbi_build]['fasta'] | ||
|
||
# Remove arguments that the actual wrapped tool won't recognize | ||
for key in ["version"]: | ||
del args_dict[key] | ||
|
||
# Build the command we're going to run | ||
cmd = [cmo.util.programs['perl']['default'], script_path] | ||
# Trim out arguments without values | ||
args_dict = dict((k, v) for k, v in args_dict.iteritems() if v) | ||
|
||
# Make sure the arguments are in a format that the script will accept, and kick it off | ||
for arg, value in args_dict.items(): | ||
arg = arg.replace("_","-") | ||
cmd = cmd + ["--"+arg, value] | ||
sys.stderr.write( "RUNNING: " + " ".join( cmd ) + "\n" ) | ||
cmo.util.call_cmd( " ".join( cmd )) |