bgzf.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* The MIT License
  2. Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
  3. 2011 Attractive Chaos <attractor@live.co.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /* The BGZF library was originally written by Bob Handsaker from the Broad
  21. * Institute. It was later improved by the SAMtools developers. */
  22. #ifndef __BGZF_H
  23. #define __BGZF_H
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <zlib.h>
  27. #define BGZF_BLOCK_SIZE 0x10000
  28. #define BGZF_MAX_BLOCK_SIZE 0x10000
  29. #define BGZF_ERR_ZLIB 1
  30. #define BGZF_ERR_HEADER 2
  31. #define BGZF_ERR_IO 4
  32. #define BGZF_ERR_MISUSE 8
  33. typedef struct {
  34. int open_mode:8, compress_level:8, errcode:16;
  35. int cache_size;
  36. int block_length, block_offset;
  37. int64_t block_address;
  38. void *uncompressed_block, *compressed_block;
  39. void *cache; // a pointer to a hash table
  40. void *fp; // actual file handler; FILE* on writing; FILE* or knetFile* on reading
  41. } BGZF;
  42. #ifndef KSTRING_T
  43. #define KSTRING_T kstring_t
  44. typedef struct __kstring_t {
  45. size_t l, m;
  46. char *s;
  47. } kstring_t;
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /******************
  53. * Basic routines *
  54. ******************/
  55. /**
  56. * Open an existing file descriptor for reading or writing.
  57. *
  58. * @param fd file descriptor
  59. * @param mode mode matching /[rwu0-9]+/: 'r' for reading, 'w' for writing and a digit specifies
  60. * the zlib compression level; if both 'r' and 'w' are present, 'w' is ignored.
  61. * @return BGZF file handler; 0 on error
  62. */
  63. BGZF* bgzf_dopen(int fd, const char *mode);
  64. #define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
  65. /**
  66. * Open the specified file for reading or writing.
  67. */
  68. BGZF* bgzf_open(const char* path, const char *mode);
  69. /**
  70. * Close the BGZF and free all associated resources.
  71. *
  72. * @param fp BGZF file handler
  73. * @return 0 on success and -1 on error
  74. */
  75. int bgzf_close(BGZF *fp);
  76. /**
  77. * Read up to _length_ bytes from the file storing into _data_.
  78. *
  79. * @param fp BGZF file handler
  80. * @param data data array to read into
  81. * @param length size of data to read
  82. * @return number of bytes actually read; 0 on end-of-file and -1 on error
  83. */
  84. ssize_t bgzf_read(BGZF *fp, void *data, ssize_t length);
  85. /**
  86. * Write _length_ bytes from _data_ to the file.
  87. *
  88. * @param fp BGZF file handler
  89. * @param data data array to write
  90. * @param length size of data to write
  91. * @return number of bytes actually written; -1 on error
  92. */
  93. ssize_t bgzf_write(BGZF *fp, const void *data, ssize_t length);
  94. /**
  95. * Write the data in the buffer to the file.
  96. */
  97. int bgzf_flush(BGZF *fp);
  98. /**
  99. * Return a virtual file pointer to the current location in the file.
  100. * No interpetation of the value should be made, other than a subsequent
  101. * call to bgzf_seek can be used to position the file at the same point.
  102. * Return value is non-negative on success.
  103. */
  104. #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
  105. /**
  106. * Set the file to read from the location specified by _pos_.
  107. *
  108. * @param fp BGZF file handler
  109. * @param pos virtual file offset returned by bgzf_tell()
  110. * @param whence must be SEEK_SET
  111. * @return 0 on success and -1 on error
  112. */
  113. int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
  114. /**
  115. * Check if the BGZF end-of-file (EOF) marker is present
  116. *
  117. * @param fp BGZF file handler opened for reading
  118. * @return 1 if EOF is present; 0 if not or on I/O error
  119. */
  120. int bgzf_check_EOF(BGZF *fp);
  121. /**
  122. * Check if a file is in the BGZF format
  123. *
  124. * @param fn file name
  125. * @return 1 if _fn_ is BGZF; 0 if not or on I/O error
  126. */
  127. int bgzf_is_bgzf(const char *fn);
  128. /*********************
  129. * Advanced routines *
  130. *********************/
  131. /**
  132. * Set the cache size. Only effective when compiled with -DBGZF_CACHE.
  133. *
  134. * @param fp BGZF file handler
  135. * @param size size of cache in bytes; 0 to disable caching (default)
  136. */
  137. void bgzf_set_cache_size(BGZF *fp, int size);
  138. /**
  139. * Flush the file if the remaining buffer size is smaller than _size_
  140. */
  141. int bgzf_flush_try(BGZF *fp, ssize_t size);
  142. /**
  143. * Read one byte from a BGZF file. It is faster than bgzf_read()
  144. * @param fp BGZF file handler
  145. * @return byte read; -1 on end-of-file or error
  146. */
  147. int bgzf_getc(BGZF *fp);
  148. /**
  149. * Read one line from a BGZF file. It is faster than bgzf_getc()
  150. *
  151. * @param fp BGZF file handler
  152. * @param delim delimitor
  153. * @param str string to write to; must be initialized
  154. * @return length of the string; 0 on end-of-file; negative on error
  155. */
  156. int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
  157. /**
  158. * Read the next BGZF block.
  159. */
  160. int bgzf_read_block(BGZF *fp);
  161. #ifdef __cplusplus
  162. }
  163. #endif
  164. #endif