File: libcbgzf.pyx

package info (click to toggle)
python-pysam 0.15.4%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,992 kB
  • sloc: ansic: 140,738; python: 7,881; sh: 265; makefile: 223; perl: 41
file content (238 lines) | stat: -rw-r--r-- 7,438 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""Functions that read and write block gzipped files.

The user of the file doesn't have to worry about the compression
and random access is allowed if an index file is present."""

# based on Python 3.5's gzip module

import io

from libc.stdint cimport int8_t, int16_t, int32_t, int64_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
from libc.stdlib cimport malloc, calloc, realloc, free

from cpython.object cimport PyObject
from cpython.bytes  cimport PyBytes_FromStringAndSize, _PyBytes_Resize

from pysam.libcutils   cimport force_bytes, encode_filename
from pysam.libchtslib  cimport bgzf_open, bgzf_index_build_init, bgzf_write, bgzf_read, \
                               bgzf_flush, bgzf_index_dump, bgzf_close, bgzf_seek, \
                               bgzf_tell, bgzf_getline, kstring_t, SEEK_SET, BGZF

__all__ = ["BGZFile"]


BUFFER_SIZE = io.DEFAULT_BUFFER_SIZE


cdef class BGZFile(object):
    """The BGZFile class simulates most of the methods of a file object with
    the exception of the truncate() method.

    This class only supports opening files in binary mode. If you need to open a
    compressed file in text mode, use the gzip.open() function.
    """
    cdef BGZF* bgzf
    cdef readonly object name, index

    def __init__(self, filename, mode=None, index=None):
        """Constructor for the BGZFile class.

        The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or
        'xb' depending on whether the file will be read or written.  The default
        is the mode of fileobj if discernible; otherwise, the default is 'rb'.
        A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and
        'wb', 'a' and 'ab', and 'x' and 'xb'.
        """
        if mode and ('t' in mode or 'U' in mode):
            raise ValueError("Invalid mode: {!r}".format(mode))
        if not mode:
            mode = 'rb'
        elif mode and 'b' not in mode:
            mode += 'b'

        mode = force_bytes(mode)

        self.name = encode_filename(filename)
        self.index = encode_filename(index) if index is not None else None

        self.bgzf = bgzf_open(self.name, mode)

        if self.bgzf.is_write and index is not None and bgzf_index_build_init(self.bgzf) < 0:
            raise IOError('Error building bgzf index')

    def __dealloc__(self):
        self.close()

    def write(self, data):
        if not self.bgzf:
            raise ValueError("write() on closed BGZFile object")

        if not self.bgzf.is_write:
            import errno
            raise IOError(errno.EBADF, "write() on read-only BGZFile object")

        if isinstance(data, bytes):
            length = len(data)
        else:
            # accept any data that supports the buffer protocol
            data = memoryview(data)
            length = data.nbytes

        if length > 0 and bgzf_write(self.bgzf, <char *>data, length) < 0:
            raise IOError('BGZFile write failed')

        return length

    def read(self, size=-1):
        cdef ssize_t read_size

        if not self.bgzf:
            raise ValueError("read() on closed BGZFile object")

        if self.bgzf.is_write:
            import errno
            raise IOError(errno.EBADF, "read() on write-only BGZFile object")

        if size < 0:
            chunks = []
            while 1:
                chunk = PyBytes_FromStringAndSize(NULL, BUFFER_SIZE)
                cdata = <bytes>chunk
                read_size = bgzf_read(self.bgzf, <char *>chunk, BUFFER_SIZE)
                if read_size < 0:
                    raise IOError('Error reading from BGZFile')
                elif not read_size:
                    break
                elif read_size < BUFFER_SIZE:
                    chunk = chunk[:read_size]
                chunks.append(chunk)
            return b''.join(chunks)

        elif size > 0:
            chunk = PyBytes_FromStringAndSize(NULL, size)
            read_size = bgzf_read(self.bgzf, <char *>chunk, size)
            if read_size < 0:
                raise IOError('Error reading from BGZFile')
            elif read_size < size:
                chunk = chunk[:size]
            return chunk
        else:
            return b''

    @property
    def closed(self):
        return self.bgzf == NULL

    def close(self):
        if not self.bgzf:
            return

        if self.bgzf.is_write and bgzf_flush(self.bgzf) < 0:
            raise IOError('Error flushing BGZFile object')

        if self.index and bgzf_index_dump(self.bgzf, self.index, NULL) < 0:
            raise IOError('Cannot write index')

        cdef ret = bgzf_close(self.bgzf)
        self.bgzf = NULL

        if ret < 0:
            raise IOError('Error closing BGZFile object')

    def __enter__(self):
        return self

    def __exit__(self, type, value, tb):
        self.close()

    def flush(self):
        if not self.bgzf:
            return

        if self.bgzf.is_write and bgzf_flush(self.bgzf) < 0:
            raise IOError('Error flushing BGZFile object')

    def fileno(self):
        """Invoke the underlying file object's fileno() method.

        This will raise AttributeError if the underlying file object
        doesn't support fileno().
        """
        raise AttributeError('fileno')

    def rewind(self):
        '''Return the uncompressed stream file position indicator to the
        beginning of the file'''
        if not self.bgzf:
            raise ValueError("rewind() on closed BGZFile object")
        if not self.bgzf.is_write:
            raise IOError("Can't rewind in write mode")
        if bgzf_seek(self.bgzf, 0, SEEK_SET) < 0:
            raise IOError('Error seeking BGZFFile object')

    def readable(self):
        if not self.bgzf:
            raise ValueError("readable() on closed BGZFile object")
        return self.bgzf != NULL and not self.bgzf.is_write

    def writable(self):
        return self.bgzf != NULL and self.bgzf.is_write

    def seekable(self):
        return True

    def tell(self):
        if not self.bgzf:
            raise ValueError("seek() on closed BGZFile object")
        cdef int64_t off = bgzf_tell(self.bgzf)
        if off < 0:
            raise IOError('Error in tell on BGZFFile object')

        return off

    def seek(self, offset, whence=io.SEEK_SET):
        if not self.bgzf:
            raise ValueError("seek() on closed BGZFile object")
        if whence is not io.SEEK_SET:
            raise ValueError('Seek from end not supported')

        cdef int64_t off = bgzf_seek(self.bgzf, offset, SEEK_SET)
        if off < 0:
            raise IOError('Error seeking BGZFFile object')

        return off

    def readline(self, size=-1):
        if not self.bgzf:
            raise ValueError("readline() on closed BGZFile object")

        cdef kstring_t line
        cdef char c

        line.l = line.m = 0
        line.s = NULL

        cdef int ret = bgzf_getline(self.bgzf, '\n', &line)
        if ret == -1:
            s = b''
        elif ret == -2:
            if line.m:
                free(line.s)
            raise IOError('Error reading line in BGZFFile object')
        else:
            s = line.s[:line.l]

        if line.m:
            free(line.s)

        return s

    def __iter__(self):
        return self

    def __next__(self):
        line = self.readline()
        if not line:
            raise StopIteration()
        return line