File: _util.py

package info (click to toggle)
python-cooler 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,596 kB
  • sloc: python: 10,555; makefile: 198; sh: 31
file content (218 lines) | stat: -rw-r--r-- 6,624 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import errno
import os
import os.path as op
import sys
from contextlib import contextmanager
from functools import wraps

import click
import multiprocess as mp
import numpy as np
import pandas as pd

from .. import util


class DelimitedTuple(click.types.ParamType):
    def __init__(self, sep=",", type=str):
        self.sep = sep
        self.type = click.types.convert_type(type)

    @property
    def name(self):
        return "separated[%s]" % self.sep

    def convert(self, value, param, ctx):
        # needs to pass through value = None unchanged
        # needs to be idempotent
        # needs to be able to deal with param and context being None
        if value is None:
            return value
        elif isinstance(value, str):
            parts = value.split(",")
        else:
            parts = value
        return tuple(self.type(x, param, ctx) for x in parts)


def parse_kv_list_param(arg, item_sep=",", kv_sep="="):
    from io import StringIO

    import yaml

    if item_sep != ",":
        arg = arg.replace(item_sep, ",")
    arg = "{" + arg.replace(kv_sep, ": ") + "}"
    try:
        result = yaml.safe_load(StringIO(arg))
    except yaml.YAMLError as e:
        raise click.BadParameter(f"Error parsing key-value pairs: {arg}") from e
    return result


def parse_field_param(arg, includes_colnum=True, includes_agg=True):
    parts = arg.split(":")
    prefix = parts[0]
    if len(parts) == 1:
        props = None
    elif len(parts) == 2:
        props = parts[1]
    else:
        raise click.BadParameter(arg)

    if includes_colnum:
        parts = prefix.split("=")
        name = parts[0]
        if len(parts) == 1:
            colnum = None
        elif len(parts) == 2:
            try:
                colnum = int(parts[1]) - 1
            except ValueError as e:
                raise click.BadParameter(
                    f"Not a number: '{parts[1]}'", param_hint=arg
                ) from e
            if colnum < 0:
                raise click.BadParameter(
                    "Field numbers start at 1.", param_hint=arg
                )
        else:
            raise click.BadParameter(arg)
    else:
        name = parts[0]
        colnum = None

    dtype = None
    agg = None
    if props is not None:
        for item in props.split(","):
            try:
                prop, value = item.split("=")
            except ValueError as e:
                raise click.BadParameter(arg) from e
            if prop == "dtype":
                dtype = np.dtype(value)
            elif prop == "agg" and includes_agg:
                agg = value
            else:
                raise click.BadParameter(
                    f"Invalid property: '{prop}'.", param_hint=arg
                )
    return name, colnum, dtype, agg


def parse_bins(arg):
    # Provided chromsizes and binsize
    if ":" in arg:
        chromsizes_file, binsize = arg.split(":")
        if not op.exists(chromsizes_file):
            raise ValueError(f'File "{chromsizes_file}" not found')
        try:
            binsize = int(binsize)
        except ValueError as e:
            raise ValueError(
                f'Expected integer binsize argument (bp), got "{binsize}"'
            ) from e
        chromsizes = util.read_chromsizes(chromsizes_file, all_names=True)
        bins = util.binnify(chromsizes, binsize)

    # Provided bins
    elif op.exists(arg):
        try:
            bins = pd.read_csv(
                arg,
                sep="\t",
                names=["chrom", "start", "end"],
                usecols=[0, 1, 2],
                dtype={"chrom": str},
            )
        except pd.parser.CParserError as e:
            raise ValueError(
                f'Failed to parse bins file "{arg}": {str(e)}'
            ) from e

        chromtable = (
            bins.drop_duplicates(["chrom"], keep="last")[["chrom", "end"]]
            .reset_index(drop=True)
            .rename(columns={"chrom": "name", "end": "length"})
        )
        chroms, lengths = list(chromtable["name"]), list(chromtable["length"])
        chromsizes = pd.Series(index=chroms, data=lengths)

    else:
        raise ValueError(
            "Expected BINS to be either <Path to bins file> or "
            "<Path to chromsizes file>:<binsize in bp>."
        )

    return chromsizes, bins


def check_ncpus(arg_value):
    arg_value = int(arg_value)

    if arg_value <= 0:
        raise click.BadParameter("n_cpus must be >= 1")
    else:
        return min(arg_value, mp.cpu_count())


@contextmanager
def on_broken_pipe(handler):
    try:
        yield
    except OSError as e:
        if e.errno == errno.EPIPE:
            handler(e)
        else:
            # Not a broken pipe error. Bubble up.
            raise


def exit_on_broken_pipe(exit_code):
    """
    Decorator to catch a broken pipe (EPIPE) error and exit cleanly.

    Use this decorator to prevent the "[Errno 32] Broken pipe" output message.

    Notes
    -----
    A SIGPIPE signal is sent to a process writing to a pipe while the other
    end has been closed. For example, this happens when piping output to
    programs like head(1). Python traps this signal and translates it into an
    exception. It is presented as an IOError in PY2, and a subclass of OSError
    in PY3 (aliased by IOError), both using POSIX error number 32 (EPIPE).

    Some programs exit with 128 + signal.SIGPIPE == 141. However, according to
    the example in the docs, Python exits with the generic error code of 1
    on EPIPE.

    The equivalent system error when trying to write on a socket which has
    been shutdown for writing is ESHUTDOWN (108). It also raises
    BrokenPipeError on PY3.

    [1] https://docs.python.org/3.7/library/signal.html#note-on-sigpipe
    [2] https://www.quora.com/How-can-you-avoid-a-broken-pipe-error-on-Python

    """

    def decorator(func):
        @wraps(func)
        def decorated(*args, **kwargs):
            try:
                func(*args, **kwargs)
            except OSError as e:
                if e.errno == errno.EPIPE:
                    # We caught a broken pipe error.
                    # Python flushes standard streams on exit; redirect remaining
                    # output to devnull to avoid another BrokenPipeError at shutdown.
                    devnull = os.open(os.devnull, os.O_WRONLY)
                    os.dup2(devnull, sys.stdout.fileno())
                    sys.exit(exit_code)
                else:
                    # Not a broken pipe error. Bubble up.
                    raise

        return decorated

    return decorator