| 1 | /* |
| 2 | tmp_file.h - write to and read from a temporary binary file |
| 3 | for fast storage plus added compression. |
| 4 | |
| 5 | Copyright (C) 2017, 2018 Genome Research Ltd. |
| 6 | |
| 7 | Author: Andrew Whitwham <[email protected]> |
| 8 | |
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | of this software and associated documentation files (the "Software"), to deal |
| 11 | in the Software without restriction, including without limitation the rights |
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | copies of the Software, and to permit persons to whom the Software is |
| 14 | furnished to do so, subject to the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be included in |
| 17 | all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 | DEALINGS IN THE SOFTWARE |
| 26 | */ |
| 27 | |
| 28 | #ifndef _TMP_SAM_FILE_H_ |
| 29 | #define _TMP_SAM_FILE_H_ |
| 30 | |
| 31 | #include <lz4.h> |
| 32 | #include "htslib/sam.h" |
| 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | // Group size that seems to give reasonable compression. |
| 39 | #define TMP_SAM_GROUP_SIZE 100 |
| 40 | |
| 41 | // Arbitrary initial size values but growable. |
| 42 | #define TMP_SAM_MAX_DATA 1024 |
| 43 | #define TMP_SAM_RING_SIZE 1048576 |
| 44 | |
| 45 | // Error numbers. |
| 46 | #define TMP_SAM_OK 0 |
| 47 | #define TMP_SAM_MEM_ERROR -1 |
| 48 | #define TMP_SAM_FILE_ERROR -2 |
| 49 | #define TMP_SAM_LZ4_ERROR -3 |
| 50 | #define TMP_SAM_INPUT_ERROR -4 |
| 51 | |
| 52 | typedef struct { |
| 53 | FILE *fp; |
| 54 | LZ4_stream_t *stream; |
| 55 | LZ4_streamDecode_t *dstream; |
| 56 | size_t data_size; |
| 57 | size_t max_data_size; |
| 58 | size_t ring_buffer_size; |
| 59 | size_t comp_buffer_size; |
| 60 | size_t offset; |
| 61 | uint8_t *ring_buffer; |
| 62 | uint8_t *ring_index; |
| 63 | char *comp_buffer; |
| 64 | char *name; |
| 65 | size_t group_size; |
| 66 | size_t input_size; |
| 67 | size_t read_size; |
| 68 | size_t output_size; |
| 69 | size_t entry_number; |
| 70 | int verbose; |
| 71 | char *dict; |
| 72 | size_t groups_written; |
| 73 | } tmp_file_t; |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * Opens the temp file and initialises memory. |
| 78 | * Verbose mode prints out error messages to stderr. |
| 79 | * Returns 0 on success, a negative number on failure. |
| 80 | */ |
| 81 | int tmp_file_open_write(tmp_file_t *tmp, char *tmp_name, int verbose); |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * Stores an in memory bam structure for writing and if enough are gathered together writes |
| 86 | * it to a file. Multiple alignments compress better that single ones though after a certain number |
| 87 | * there is a law of diminishing returns. |
| 88 | * Returns 0 on success, a negative number on failure. |
| 89 | */ |
| 90 | int tmp_file_write(tmp_file_t *tmp, bam1_t *inbam); |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | * Marks the end of file writing. Adds a size_t 0 to mark the end of |
| 95 | * the file. Companion function to tmp_file_begin_read below. |
| 96 | * Returns 0 on success, a negative number on failure. |
| 97 | */ |
| 98 | int tmp_file_end_write(tmp_file_t *tmp); |
| 99 | |
| 100 | /* |
| 101 | * Prepares the file for reading. |
| 102 | * Companion function to tmp_file_end_write above. |
| 103 | * Returns 0 on success, a negative number on failure. |
| 104 | */ |
| 105 | int tmp_file_begin_read(tmp_file_t *tmp); |
| 106 | |
| 107 | /* |
| 108 | * Read the next alignment, either from memory or from a file. |
| 109 | * Returns size of entry on success, 0 on end of file or a negative on error. |
| 110 | */ |
| 111 | int tmp_file_read(tmp_file_t *tmp, bam1_t *inbam); |
| 112 | |
| 113 | |
| 114 | /* |
| 115 | * Frees up memory, closes the file and deletes it. |
| 116 | * Returns 0 on success or EOF on failure. |
| 117 | */ |
| 118 | int tmp_file_destroy(tmp_file_t *tmp); |
| 119 | |
| 120 | #ifdef __cplusplus |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #endif /* _TMP_SAM_FILE_H_ */ |