| 1 |
""" |
| 2 |
Additional utilities for running command-line apps - most of these do not apply |
| 3 |
to tool-contract-driven programs. (Ported from pbsmrtpipe) |
| 4 |
""" |
| 5 |
|
| 6 |
import argparse |
| 7 |
import logging |
| 8 |
import os |
| 9 |
import platform |
| 10 |
import time |
| 11 |
import traceback |
| 12 |
|
| 13 |
from pbcommand.validators import validate_file, validate_fofn |
| 14 |
from pbcommand.utils import setup_log |
| 15 |
|
| 16 |
log = logging.getLogger(__name__) |
| 17 |
|
| 18 |
|
| 19 |
def subparser_builder(subparser, subparser_id, |
| 20 |
description, options_func, exe_func): |
| 21 |
""" |
| 22 |
Util to add subparser options |
| 23 |
|
| 24 |
:param subparser: |
| 25 |
:param subparser_id: |
| 26 |
:param description: |
| 27 |
:param options_func: Function that will add args and options to Parser instance F(subparser) -> None |
| 28 |
:param exe_func: Function to run F(args) -> Int |
| 29 |
:return: |
| 30 |
""" |
| 31 |
p = subparser.add_parser(subparser_id, help=description) |
| 32 |
options_func(p) |
| 33 |
p.set_defaults(func=exe_func) |
| 34 |
return p |
| 35 |
|
| 36 |
|
| 37 |
def add_debug_option(p): |
| 38 |
p.add_argument('--debug', action='store_true', |
| 39 |
help="Send logging info to stdout.") |
| 40 |
return p |
| 41 |
|
| 42 |
|
| 43 |
def _validate_output_dir_or_get_default(value): |
| 44 |
if value is None: |
| 45 |
return os.getcwd() |
| 46 |
else: |
| 47 |
if os.path.exists(value): |
| 48 |
return os.path.abspath(value) |
| 49 |
else: |
| 50 |
os.mkdir(value) |
| 51 |
return os.path.abspath(value) |
| 52 |
|
| 53 |
|
| 54 |
def add_output_dir_option(p): |
| 55 |
p.add_argument( |
| 56 |
'-o', |
| 57 |
'--output-dir', |
| 58 |
type=_validate_output_dir_or_get_default, |
| 59 |
default=os.getcwd(), |
| 60 |
help="Output directory.") |
| 61 |
return p |
| 62 |
|
| 63 |
|
| 64 |
def _add_input_file(args_label, type_, help_): |
| 65 |
def _wrapper(p): |
| 66 |
p.add_argument(args_label, type=type_, help=help_) |
| 67 |
return p |
| 68 |
return _wrapper |
| 69 |
|
| 70 |
|
| 71 |
add_fasta_output = _add_input_file( |
| 72 |
"fasta_out", str, "Path to output Fasta File") |
| 73 |
add_fasta_input = _add_input_file( |
| 74 |
"fasta_in", |
| 75 |
validate_file, |
| 76 |
"Path to Input FASTA File") |
| 77 |
|
| 78 |
add_fastq_output = _add_input_file( |
| 79 |
"fastq_out", str, "Path to output Fastq File") |
| 80 |
add_fastq_input = _add_input_file( |
| 81 |
"fastq_in", |
| 82 |
validate_file, |
| 83 |
"Path to Input FASTQ File") |
| 84 |
|
| 85 |
add_fofn_input = _add_input_file( |
| 86 |
"fofn_in", |
| 87 |
validate_fofn, |
| 88 |
"Path to Input FOFN (File of file names) File") |
| 89 |
add_fofn_output = _add_input_file("fofn_out", str, "Path to output FOFN.") |
| 90 |
|
| 91 |
add_report_output = _add_input_file( |
| 92 |
"json_report", str, "Path to PacBio JSON Report") |
| 93 |
|
| 94 |
add_subread_input = _add_input_file( |
| 95 |
"subread_ds", |
| 96 |
validate_file, |
| 97 |
"Path to PacBio Subread DataSet XML") |
| 98 |
|
| 99 |
add_ds_reference_input = _add_input_file( |
| 100 |
"reference_ds", |
| 101 |
validate_file, |
| 102 |
"Path to PacBio Subread DataSet XML") |
| 103 |
|
| 104 |
|
| 105 |
def args_executer(args): |
| 106 |
""" |
| 107 |
|
| 108 |
|
| 109 |
:rtype int |
| 110 |
""" |
| 111 |
try: |
| 112 |
return_code = args.func(args) |
| 113 |
except Exception as e: |
| 114 |
log.error(e, exc_info=True) |
| 115 |
import sys |
| 116 |
traceback.print_exc(sys.stderr) |
| 117 |
if isinstance(e, IOError): |
| 118 |
return_code = 1 |
| 119 |
else: |
| 120 |
return_code = 2 |
| 121 |
|
| 122 |
return return_code |
| 123 |
|
| 124 |
|
| 125 |
def main_runner(argv, parser, exe_runner_func, setup_log_func, alog): |
| 126 |
""" |
| 127 |
Fundamental interface to commandline applications |
| 128 |
""" |
| 129 |
started_at = time.time() |
| 130 |
args = parser.parse_args(argv) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
_have_log_setup = False |
| 135 |
if hasattr(args, 'quiet') and args.quiet: |
| 136 |
setup_log_func(alog, level=logging.ERROR) |
| 137 |
elif getattr(args, 'verbosity', None) is not None and args.verbosity > 0: |
| 138 |
if args.verbosity >= 2: |
| 139 |
setup_log_func(alog, level=logging.DEBUG) |
| 140 |
else: |
| 141 |
setup_log_func(alog, level=logging.INFO) |
| 142 |
elif hasattr(args, 'debug') and args.debug: |
| 143 |
setup_log_func(alog, level=logging.DEBUG) |
| 144 |
else: |
| 145 |
alog.addHandler(logging.NullHandler()) |
| 146 |
|
| 147 |
log.debug(args) |
| 148 |
alog.info("Starting tool version {v}".format(v=parser.version)) |
| 149 |
rcode = exe_runner_func(args) |
| 150 |
|
| 151 |
run_time = time.time() - started_at |
| 152 |
_d = dict(r=rcode, s=run_time) |
| 153 |
alog.info("exiting with return code {r} in {s:.2f} sec.".format(**_d)) |
| 154 |
return rcode |
| 155 |
|
| 156 |
|
| 157 |
def main_runner_default(argv, parser, alog): |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
setup_log_func = setup_log |
| 162 |
return main_runner(argv, parser, args_executer, setup_log_func, alog) |