Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

238 lignes
7.3 KiB

  1. # cython: language_level=3
  2. """Functions that read and write block gzipped files.
  3. The user of the file doesn't have to worry about the compression
  4. and random access is allowed if an index file is present."""
  5. # based on Python 3.5's gzip module
  6. import io
  7. from libc.stdint cimport int8_t, int16_t, int32_t, int64_t
  8. from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
  9. from libc.stdio cimport SEEK_SET
  10. from libc.stdlib cimport malloc, calloc, realloc, free
  11. from cpython.object cimport PyObject
  12. from cpython.bytes cimport PyBytes_FromStringAndSize, _PyBytes_Resize
  13. from pysam.libcutils cimport force_bytes, encode_filename
  14. from pysam.libchtslib cimport bgzf_open, bgzf_index_build_init, bgzf_write, bgzf_read, \
  15. bgzf_flush, bgzf_index_dump, bgzf_close, bgzf_seek, \
  16. bgzf_tell, bgzf_getline, kstring_t, BGZF
  17. __all__ = ["BGZFile"]
  18. BUFFER_SIZE = io.DEFAULT_BUFFER_SIZE
  19. cdef class BGZFile(object):
  20. """The BGZFile class simulates most of the methods of a file object with
  21. the exception of the truncate() method.
  22. This class only supports opening files in binary mode. If you need to open a
  23. compressed file in text mode, use the gzip.open() function.
  24. """
  25. def __init__(self, filename, mode=None, index=None):
  26. """Constructor for the BGZFile class.
  27. The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or
  28. 'xb' depending on whether the file will be read or written. The default
  29. is the mode of fileobj if discernible; otherwise, the default is 'rb'.
  30. A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and
  31. 'wb', 'a' and 'ab', and 'x' and 'xb'.
  32. """
  33. if mode and ('t' in mode or 'U' in mode):
  34. raise ValueError("Invalid mode: {!r}".format(mode))
  35. if not mode:
  36. mode = 'rb'
  37. elif mode and 'b' not in mode:
  38. mode += 'b'
  39. mode = force_bytes(mode)
  40. self.name = encode_filename(filename)
  41. self.index = encode_filename(index) if index is not None else None
  42. self.bgzf = bgzf_open(self.name, mode)
  43. if self.bgzf.is_write and index is not None and bgzf_index_build_init(self.bgzf) < 0:
  44. raise IOError('Error building bgzf index')
  45. def __dealloc__(self):
  46. self.close()
  47. def write(self, data):
  48. if not self.bgzf:
  49. raise ValueError("write() on closed BGZFile object")
  50. if not self.bgzf.is_write:
  51. import errno
  52. raise IOError(errno.EBADF, "write() on read-only BGZFile object")
  53. if isinstance(data, bytes):
  54. length = len(data)
  55. else:
  56. # accept any data that supports the buffer protocol
  57. data = memoryview(data)
  58. length = data.nbytes
  59. if length > 0 and bgzf_write(self.bgzf, <char *>data, length) < 0:
  60. raise IOError('BGZFile write failed')
  61. return length
  62. def read(self, size=-1):
  63. cdef ssize_t read_size
  64. if not self.bgzf:
  65. raise ValueError("read() on closed BGZFile object")
  66. if self.bgzf.is_write:
  67. import errno
  68. raise IOError(errno.EBADF, "read() on write-only BGZFile object")
  69. if size < 0:
  70. chunks = []
  71. while 1:
  72. chunk = PyBytes_FromStringAndSize(NULL, BUFFER_SIZE)
  73. cdata = <bytes>chunk
  74. read_size = bgzf_read(self.bgzf, <char *>chunk, BUFFER_SIZE)
  75. if read_size < 0:
  76. raise IOError('Error reading from BGZFile')
  77. elif not read_size:
  78. break
  79. elif read_size < BUFFER_SIZE:
  80. chunk = chunk[:read_size]
  81. chunks.append(chunk)
  82. return b''.join(chunks)
  83. elif size > 0:
  84. chunk = PyBytes_FromStringAndSize(NULL, size)
  85. read_size = bgzf_read(self.bgzf, <char *>chunk, size)
  86. if read_size < 0:
  87. raise IOError('Error reading from BGZFile')
  88. elif read_size < size:
  89. chunk = chunk[:read_size]
  90. return chunk
  91. else:
  92. return b''
  93. @property
  94. def closed(self):
  95. return self.bgzf == NULL
  96. def close(self):
  97. if not self.bgzf:
  98. return
  99. if self.bgzf.is_write and bgzf_flush(self.bgzf) < 0:
  100. raise IOError('Error flushing BGZFile object')
  101. if self.index and bgzf_index_dump(self.bgzf, self.index, NULL) < 0:
  102. raise IOError('Cannot write index')
  103. cdef ret = bgzf_close(self.bgzf)
  104. self.bgzf = NULL
  105. if ret < 0:
  106. raise IOError('Error closing BGZFile object')
  107. def __enter__(self):
  108. return self
  109. def __exit__(self, type, value, tb):
  110. self.close()
  111. def flush(self):
  112. if not self.bgzf:
  113. return
  114. if self.bgzf.is_write and bgzf_flush(self.bgzf) < 0:
  115. raise IOError('Error flushing BGZFile object')
  116. def fileno(self):
  117. """Invoke the underlying file object's fileno() method.
  118. This will raise AttributeError if the underlying file object
  119. doesn't support fileno().
  120. """
  121. raise AttributeError('fileno')
  122. def rewind(self):
  123. '''Return the uncompressed stream file position indicator to the
  124. beginning of the file'''
  125. if not self.bgzf:
  126. raise ValueError("rewind() on closed BGZFile object")
  127. if not self.bgzf.is_write:
  128. raise IOError("Can't rewind in write mode")
  129. if bgzf_seek(self.bgzf, 0, SEEK_SET) < 0:
  130. raise IOError('Error seeking BGZFFile object')
  131. def readable(self):
  132. if not self.bgzf:
  133. raise ValueError("readable() on closed BGZFile object")
  134. return self.bgzf != NULL and not self.bgzf.is_write
  135. def writable(self):
  136. return self.bgzf != NULL and self.bgzf.is_write
  137. def seekable(self):
  138. return True
  139. def tell(self):
  140. if not self.bgzf:
  141. raise ValueError("seek() on closed BGZFile object")
  142. cdef int64_t off = bgzf_tell(self.bgzf)
  143. if off < 0:
  144. raise IOError('Error in tell on BGZFFile object')
  145. return off
  146. def seek(self, offset, whence=io.SEEK_SET):
  147. if not self.bgzf:
  148. raise ValueError("seek() on closed BGZFile object")
  149. if whence is not io.SEEK_SET:
  150. raise ValueError('Seek from end not supported')
  151. cdef int64_t off = bgzf_seek(self.bgzf, offset, SEEK_SET)
  152. if off < 0:
  153. raise IOError('Error seeking BGZFFile object')
  154. return off
  155. def readline(self, size=-1):
  156. if not self.bgzf:
  157. raise ValueError("readline() on closed BGZFile object")
  158. cdef kstring_t line
  159. cdef char c
  160. line.l = line.m = 0
  161. line.s = NULL
  162. cdef int ret = bgzf_getline(self.bgzf, b'\n', &line)
  163. if ret == -1:
  164. s = b''
  165. elif ret == -2:
  166. if line.m:
  167. free(line.s)
  168. raise IOError('Error reading line in BGZFFile object')
  169. else:
  170. s = line.s[:line.l]
  171. if line.m:
  172. free(line.s)
  173. return s
  174. def __iter__(self):
  175. return self
  176. def __next__(self):
  177. line = self.readline()
  178. if not line:
  179. raise StopIteration()
  180. return line