1 /* @include ajbamindex ********************************************************
2 **
3 ** AJAX BAM files indexing/querying functions
4 **
5 ** @version $Revision: 1.4 $
6 ** @modified 2011 Mahmut Uludag ported from samtools (samtools.sf.net)
7 ** @modified $Date: 2012/04/26 17:36:15 $ by $Author: mks $
8 ** @@
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 ** MA  02110-1301,  USA.
24 **
25 ******************************************************************************/
26 
27 /* The MIT License
28 **
29 **   Copyright (c) 2008 Genome Research Ltd (GRL).
30 **
31 **   Permission is hereby granted, free of charge, to any person obtaining
32 **   a copy of this software and associated documentation files (the
33 **   "Software"), to deal in the Software without restriction, including
34 **   without limitation the rights to use, copy, modify, merge, publish,
35 **   distribute, sublicense, and/or sell copies of the Software, and to
36 **   permit persons to whom the Software is furnished to do so, subject to
37 **   the following conditions:
38 **
39 **   The above copyright notice and this permission notice shall be
40 **   included in all copies or substantial portions of the Software.
41 **
42 **   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43 **   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44 **   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45 **   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
46 **   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
47 **   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
48 **   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
49 **   SOFTWARE.
50 */
51 
52 #ifndef AJBAMINDEX_H
53 #define AJBAMINDEX_H
54 
55 #include "ajdefine.h"
56 #include "ajseqbam.h"
57 
58 
59 /* @data pair64_t *************************************************************
60 **
61 ** A [start,stop) file pointer pairing into the BAM file, stored
62 ** as a BAM file index.  A chunk is represented as a single 64-bit
63 ** value where the high-order 48 bits point to the location of the
64 ** start of a compressed BGZF block within a BGZF file and the
65 ** low-order 16 bits point to a position within the decompressed
66 ** data in the BGZF block.
67 ** ref: Chunk.java in picard project
68 **
69 ** @attr u [ajulong] virtual file offset of the start of the chunk
70 ** @attr v [ajulong] virtual file offset of the end of the chunk
71 ******************************************************************************/
72 
73 typedef struct pair64_t
74 {
75     ajulong u;
76     ajulong v;
77 } pair64_t;
78 
79 
80 /* @data bam_binlist_t ********************************************************
81 **
82 ** bin object in a binning index for a reference sequence
83 **
84 ** @attr m [ajuint] allocated size of the chunk array
85 **                  and is always >= n
86 ** @attr n [ajuint] number of chunks
87 ** @attr list [pair64_t*] array of BAM file chunk start/stop offsets
88 ******************************************************************************/
89 
90 typedef struct bam_binlist_t
91 {
92     ajuint m;
93     ajuint n;
94     pair64_t *list;
95 } bam_binlist_t;
96 
97 
98 /* @data bam_lidx_t ***********************************************************
99 **
100 ** linear index for a reference sequence
101 **
102 ** @attr n [ajint] number of chunks
103 ** @attr m [ajint] allocated size of the list or offset array
104 **                 and is always >= n
105 ** @attr offset [ajulong*] Array of offsets
106 **
107 ******************************************************************************/
108 
109 typedef struct bam_lidx_t
110 {
111     ajint n;
112     ajint m;
113     ajulong *offset;
114 } bam_lidx_t;
115 
116 
117 
118 
119 /* @data AjPBamIndex ********************************************************
120 **
121 ** Structure for BAM indexes. (samtools name: bam_index_t)
122 **
123 ** @attr  Padding [ajuint] Padding to alignment boundary
124 ** @attr  n     [ajint]  number of reference sequences
125 ** @attr  n_no_coor [ajulong]  unmapped reads without coordinate
126 ** @attr  bindex    [AjPTable*]  list of binning indices
127 ** @attr  index2    [bam_lidx_t*]  list of intervals for the linear index
128 ******************************************************************************/
129 
130 typedef struct AjSBamIndex
131 {
132     ajuint Padding;
133     ajint n;
134     ajulong n_no_coor;
135     AjPTable* bindex;
136     bam_lidx_t *index2;
137 } AjOBamIndex;
138 
139 #define AjPBamIndex AjOBamIndex*
140 
141 
142 
143 
144 /*
145  * Return a virtual file pointer to the current location in the file.
146  * No interpretation of the value should be made, other than a subsequent
147  * call to ajSeqBamBgzfSeek can be used to position the file at the same point.
148  * Return value is non-negative on success.
149  * Returns -1 on error.
150  */
151 #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
152 #define bam_tell(fp) bgzf_tell(fp)
153 
154 
155 
156 
157 /* @datatype bam_fetch_f ******************************************************
158 **
159 **  Type of function to be called by ajBamFetch().
160 **
161 **  @attr typedef [int] Value returned
162 **
163 ******************************************************************************/
164 typedef int (*bam_fetch_f)(AjPSeqBam b, void *data);
165 
166 
167 
168 int ajBamFetch(AjPSeqBamBgzf fp, const AjPBamIndex idx, int tid,
169 	       int beg, int end, void *data, bam_fetch_f func);
170 
171 int ajBamIndexBuild(const char *fn);
172 void ajBamIndexDel(AjPBamIndex* idx);
173 AjPBamIndex ajBamIndexLoad(const char *fn);
174 
175 
176 #endif /* !AJBAMINDEX_H */
177