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
|
from pysam.libcutils import _pysam_dispatch
class SamtoolsError(Exception):
'''exception raised in case of an error incurred in the samtools
library.'''
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class PysamDispatcher(object):
'''The dispatcher emulates the samtools/bctools command line.
Captures stdout and stderr.
Raises a :class:`pysam.SamtoolsError` exception in case samtools
exits with an error code other than 0.
Some command line options are associated with parsers. For
example, the samtools command "pileup -c" creates a tab-separated
table on standard output. In order to associate parsers with
options, an optional list of parsers can be supplied. The list
will be processed in order checking for the presence of each
option.
If no parser is given or no appropriate parser is found, the
stdout output of samtools/bcftools commands will be returned.
'''
dispatch = None
parsers = None
collection = None
def __init__(self, collection, dispatch, parsers):
self.collection = collection
self.dispatch = dispatch
self.parsers = parsers
self.stderr = []
def __call__(self, *args, **kwargs):
'''execute a samtools command.
Keyword arguments:
catch_stdout -- redirect stdout from the samtools command and
return as variable (default True)
save_stdout -- redirect stdout to a filename.
raw -- ignore any parsers associated with this samtools command.
split_lines -- return stdout (if catch_stdout is True and stderr
as a list of strings.
'''
retval, stderr, stdout = _pysam_dispatch(
self.collection,
self.dispatch,
args,
catch_stdout=kwargs.get("catch_stdout", True),
save_stdout=kwargs.get("save_stdout", None))
if kwargs.get("split_lines", False):
stdout = stdout.splitlines()
if stderr:
stderr = stderr.splitlines()
if retval:
raise SamtoolsError(
"%s returned with error %i: "
"stdout=%s, stderr=%s" %
(self.collection,
retval,
stdout,
stderr))
self.stderr = stderr
# call parser for stdout:
if not kwargs.get("raw") and stdout and self.parsers:
for options, parser in self.parsers:
for option in options:
if option not in args:
break
else:
return parser(stdout)
return stdout
def get_messages(self):
return self.stderr
def usage(self):
'''return the samtools usage information for this command'''
retval, stderr, stdout = _pysam_dispatch(
self.collection,
self.dispatch,
is_usage=True,
catch_stdout=True)
# some tools write usage to stderr, such as mpileup
if stderr:
return stderr
else:
return stdout
|