annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/pysam/utils.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 from typing import (
jpayne@68 2 Callable,
jpayne@68 3 List,
jpayne@68 4 Optional,
jpayne@68 5 Tuple,
jpayne@68 6 Iterable,
jpayne@68 7 Union,
jpayne@68 8 )
jpayne@68 9
jpayne@68 10 from pysam.libcutils import _pysam_dispatch
jpayne@68 11
jpayne@68 12
jpayne@68 13 class SamtoolsError(Exception):
jpayne@68 14 '''exception raised in case of an error incurred in the samtools
jpayne@68 15 library.'''
jpayne@68 16
jpayne@68 17 def __init__(self, value):
jpayne@68 18 self.value = value
jpayne@68 19
jpayne@68 20 def __str__(self):
jpayne@68 21 return repr(self.value)
jpayne@68 22
jpayne@68 23
jpayne@68 24 class PysamDispatcher(object):
jpayne@68 25 '''The dispatcher emulates the samtools/bctools command line.
jpayne@68 26
jpayne@68 27 Captures stdout and stderr.
jpayne@68 28
jpayne@68 29 Raises a :class:`pysam.SamtoolsError` exception in case samtools
jpayne@68 30 exits with an error code other than 0.
jpayne@68 31
jpayne@68 32 Some command line options are associated with parsers. For
jpayne@68 33 example, the samtools command "pileup -c" creates a tab-separated
jpayne@68 34 table on standard output. In order to associate parsers with
jpayne@68 35 options, an optional list of parsers can be supplied. The list
jpayne@68 36 will be processed in order checking for the presence of each
jpayne@68 37 option.
jpayne@68 38
jpayne@68 39 If no parser is given or no appropriate parser is found, the
jpayne@68 40 stdout output of samtools/bcftools commands will be returned.
jpayne@68 41
jpayne@68 42 '''
jpayne@68 43
jpayne@68 44 dispatch = None
jpayne@68 45 parsers = None
jpayne@68 46 collection = None
jpayne@68 47
jpayne@68 48 def __init__(
jpayne@68 49 self,
jpayne@68 50 collection: str,
jpayne@68 51 dispatch: str,
jpayne@68 52 parsers: Optional[Iterable[Tuple[str, Callable[[Union[str, List[str]]], Union[str, List[str]]]]]] = None,
jpayne@68 53 ):
jpayne@68 54 self.collection = collection
jpayne@68 55 self.dispatch = dispatch
jpayne@68 56 self.parsers = parsers
jpayne@68 57 self.stderr = []
jpayne@68 58
jpayne@68 59 def __call__(self, *args: str, **kwargs) -> Union[str, List[str]]:
jpayne@68 60 '''
jpayne@68 61 execute a samtools command.
jpayne@68 62
jpayne@68 63 Keyword arguments:
jpayne@68 64 catch_stdout -- redirect stdout from the samtools command and
jpayne@68 65 return as variable (default True)
jpayne@68 66 save_stdout -- redirect stdout to a filename.
jpayne@68 67 raw -- ignore any parsers associated with this samtools command.
jpayne@68 68 split_lines -- return stdout (if catch_stdout is True and stderr
jpayne@68 69 as a list of strings.
jpayne@68 70 '''
jpayne@68 71 retval, stderr, stdout = _pysam_dispatch(
jpayne@68 72 self.collection,
jpayne@68 73 self.dispatch,
jpayne@68 74 args,
jpayne@68 75 catch_stdout=kwargs.get("catch_stdout", True),
jpayne@68 76 save_stdout=kwargs.get("save_stdout", None))
jpayne@68 77
jpayne@68 78 if kwargs.get("split_lines", False):
jpayne@68 79 stdout = stdout.splitlines()
jpayne@68 80 if stderr:
jpayne@68 81 stderr = stderr.splitlines()
jpayne@68 82
jpayne@68 83 if retval:
jpayne@68 84 raise SamtoolsError(
jpayne@68 85 "%s returned with error %i: "
jpayne@68 86 "stdout=%s, stderr=%s" %
jpayne@68 87 (self.collection,
jpayne@68 88 retval,
jpayne@68 89 stdout,
jpayne@68 90 stderr))
jpayne@68 91
jpayne@68 92 self.stderr = stderr
jpayne@68 93
jpayne@68 94 # call parser for stdout:
jpayne@68 95 if not kwargs.get("raw") and stdout and self.parsers:
jpayne@68 96 for options, parser in self.parsers:
jpayne@68 97 for option in options:
jpayne@68 98 if option not in args:
jpayne@68 99 break
jpayne@68 100 else:
jpayne@68 101 return parser(stdout)
jpayne@68 102
jpayne@68 103 return stdout
jpayne@68 104
jpayne@68 105 def get_messages(self):
jpayne@68 106 return self.stderr
jpayne@68 107
jpayne@68 108 def usage(self):
jpayne@68 109 '''return the samtools usage information for this command'''
jpayne@68 110 retval, stderr, stdout = _pysam_dispatch(
jpayne@68 111 self.collection,
jpayne@68 112 self.dispatch,
jpayne@68 113 is_usage=True,
jpayne@68 114 catch_stdout=True)
jpayne@68 115 # some tools write usage to stderr, such as mpileup
jpayne@68 116 if stderr:
jpayne@68 117 return stderr
jpayne@68 118 else:
jpayne@68 119 return stdout
jpayne@68 120
jpayne@68 121
jpayne@68 122 class unquoted_str(str):
jpayne@68 123 '''Tag a value as an unquoted string. Meta-information in the VCF
jpayne@68 124 header takes the form of key=value pairs. By default, pysam will
jpayne@68 125 enclose the value in quotation marks. Tagging that value with
jpayne@68 126 unquoted_str will prevent this quoting.'''