comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/pysam/utils.py @ 69:33d812a61356

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