1 /*
2 Copyright (c) 2013, 2018 Genome Research Ltd.
3 Author: James Bonfield <jkb@sanger.ac.uk>
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8    1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 
11    2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 
15    3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
16 Institute nor the names of its contributors may be used to endorse or promote
17 products derived from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #ifndef CRAM_INDEX_H
32 #define CRAM_INDEX_H
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /*
39  * Loads a CRAM .crai index into memory.
40  * Returns 0 for success
41  *        -1 for failure
42  */
43 int cram_index_load(cram_fd *fd, const char *fn, const char *fn_idx);
44 
45 void cram_index_free(cram_fd *fd);
46 
47 /*
48  * Searches the index for the first slice overlapping a reference ID
49  * and position.
50  *
51  * Returns the cram_index pointer on success
52  *         NULL on failure
53  */
54 cram_index *cram_index_query(cram_fd *fd, int refid, hts_pos_t pos, cram_index *frm);
55 cram_index *cram_index_last(cram_fd *fd, int refid, cram_index *from);
56 cram_index *cram_index_query_last(cram_fd *fd, int refid, hts_pos_t end);
57 
58 /*
59  * Skips to a container overlapping the start coordinate listed in
60  * cram_range.
61  *
62  * Returns 0 on success
63  *        -1 on failure
64  */
65 int cram_seek_to_refpos(cram_fd *fd, cram_range *r);
66 
67 void cram_index_free(cram_fd *fd);
68 
69 /*
70  * Skips to a container overlapping the start coordinate listed in
71  * cram_range.
72  *
73  * In theory we call cram_index_query multiple times, once per slice
74  * overlapping the range. However slices may be absent from the index
75  * which makes this problematic. Instead we find the left-most slice
76  * and then read from then on, skipping decoding of slices and/or
77  * whole containers when they don't overlap the specified cram_range.
78  *
79  * Returns 0 on success
80  *        -1 on failure
81  */
82 int cram_seek_to_refpos(cram_fd *fd, cram_range *r);
83 
84 /*
85  * Builds an index file.
86  *
87  * fd is a newly opened cram file that we wish to index.
88  * fn_base is the filename of the associated CRAM file.
89  * fn_idx is the filename of the index file to be written;
90  * if NULL, we add ".crai" to fn_base to get the index filename.
91  *
92  * Returns 0 on success,
93  *         negative on failure (-1 for read failure, -4 for write failure)
94  */
95 int cram_index_build(cram_fd *fd, const char *fn_base, const char *fn_idx);
96 
97 /*
98  * Adds a single slice to the index.
99  *
100  * Returns 0 on success,
101  *        -1 on failure
102  */
103 int cram_index_slice(cram_fd *fd,
104                      cram_container *c,
105                      cram_slice *s,
106                      BGZF *fp,
107                      off_t cpos,
108                      off_t spos, // relative to cpos
109                      off_t sz);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif
116