]> git.donarmstrong.com Git - samtools.git/blob - bam.h
* samtools-0.1.5-5 (r395)
[samtools.git] / bam.h
1 /* The MIT License
2
3    Copyright (c) 2008 Genome Research Ltd (GRL).
4
5    Permission is hereby granted, free of charge, to any person obtaining
6    a copy of this software and associated documentation files (the
7    "Software"), to deal in the Software without restriction, including
8    without limitation the rights to use, copy, modify, merge, publish,
9    distribute, sublicense, and/or sell copies of the Software, and to
10    permit persons to whom the Software is furnished to do so, subject to
11    the following conditions:
12
13    The above copyright notice and this permission notice shall be
14    included in all copies or substantial portions of the Software.
15
16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20    BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21    ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23    SOFTWARE.
24 */
25
26 /* Contact: Heng Li <lh3@sanger.ac.uk> */
27
28 #ifndef BAM_BAM_H
29 #define BAM_BAM_H
30
31 /*!
32   @header
33
34   BAM library provides I/O and various operations on manipulating files
35   in the BAM (Binary Alignment/Mapping) or SAM (Sequence Alignment/Map)
36   format. It now supports importing from or exporting to TAM, sorting,
37   merging, generating pileup, and quickly retrieval of reads overlapped
38   with a specified region.
39
40   @copyright Genome Research Ltd.
41  */
42
43 #include <stdint.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdio.h>
47
48 #ifndef BAM_LITE
49 #define BAM_VIRTUAL_OFFSET16
50 #include "bgzf.h"
51 /*! @abstract BAM file handler */
52 typedef BGZF *bamFile;
53 #define bam_open(fn, mode) bgzf_open(fn, mode)
54 #define bam_dopen(fd, mode) bgzf_fdopen(fd, mode)
55 #define bam_close(fp) bgzf_close(fp)
56 #define bam_read(fp, buf, size) bgzf_read(fp, buf, size)
57 #define bam_write(fp, buf, size) bgzf_write(fp, buf, size)
58 #define bam_tell(fp) bgzf_tell(fp)
59 #define bam_seek(fp, pos, dir) bgzf_seek(fp, pos, dir)
60 #else
61 #define BAM_TRUE_OFFSET
62 #include <zlib.h>
63 typedef gzFile bamFile;
64 #define bam_open(fn, mode) gzopen(fn, mode)
65 #define bam_dopen(fd, mode) gzdopen(fd, mode)
66 #define bam_close(fp) gzclose(fp)
67 #define bam_read(fp, buf, size) gzread(fp, buf, size)
68 /* no bam_write/bam_tell/bam_seek() here */
69 #endif
70
71 /*! @typedef
72   @abstract Structure for the alignment header.
73   @field n_targets   number of reference sequences
74   @field target_name names of the reference sequences
75   @field target_len  lengths of the referene sequences
76   @field hash        hash table for fast name lookup
77   @field rg2lib      hash table for @RG-ID -> LB lookup
78   @field l_text      length of the plain text in the header
79   @field text        plain text
80
81   @discussion Field hash points to null by default. It is a private
82   member.
83  */
84 typedef struct {
85         int32_t n_targets;
86         char **target_name;
87         uint32_t *target_len;
88         void *hash, *rg2lib;
89         int l_text;
90         char *text;
91 } bam_header_t;
92
93 /*! @abstract the read is paired in sequencing, no matter whether it is mapped in a pair */
94 #define BAM_FPAIRED        1
95 /*! @abstract the read is mapped in a proper pair */
96 #define BAM_FPROPER_PAIR   2
97 /*! @abstract the read itself is unmapped; conflictive with BAM_FPROPER_PAIR */
98 #define BAM_FUNMAP         4
99 /*! @abstract the mate is unmapped */
100 #define BAM_FMUNMAP        8
101 /*! @abstract the read is mapped to the reverse strand */
102 #define BAM_FREVERSE      16
103 /*! @abstract the mate is mapped to the reverse strand */
104 #define BAM_FMREVERSE     32
105 /*! @abstract this is read1 */
106 #define BAM_FREAD1        64
107 /*! @abstract this is read2 */
108 #define BAM_FREAD2       128
109 /*! @abstract not primary alignment */
110 #define BAM_FSECONDARY   256
111 /*! @abstract QC failure */
112 #define BAM_FQCFAIL      512
113 /*! @abstract optical or PCR duplicate */
114 #define BAM_FDUP        1024
115
116 /*! @abstract defautl mask for pileup */
117 #define BAM_DEF_MASK (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP)
118
119 #define BAM_CORE_SIZE   sizeof(bam1_core_t)
120
121 /**
122  * Describing how CIGAR operation/length is packed in a 32-bit integer.
123  */
124 #define BAM_CIGAR_SHIFT 4
125 #define BAM_CIGAR_MASK  ((1 << BAM_CIGAR_SHIFT) - 1)
126
127 /*
128   CIGAR operations.
129  */
130 /*! @abstract CIGAR: match */
131 #define BAM_CMATCH      0
132 /*! @abstract CIGAR: insertion to the reference */
133 #define BAM_CINS        1
134 /*! @abstract CIGAR: deletion from the reference */
135 #define BAM_CDEL        2
136 /*! @abstract CIGAR: skip on the reference (e.g. spliced alignment) */
137 #define BAM_CREF_SKIP   3
138 /*! @abstract CIGAR: clip on the read with clipped sequence present in qseq */
139 #define BAM_CSOFT_CLIP  4
140 /*! @abstract CIGAR: clip on the read with clipped sequence trimmed off */
141 #define BAM_CHARD_CLIP  5
142 /*! @abstract CIGAR: padding */
143 #define BAM_CPAD        6
144
145 /*! @typedef
146   @abstract Structure for core alignment information.
147   @field  tid     chromosome ID, defined by bam_header_t
148   @field  pos     0-based leftmost coordinate
149   @field  strand  strand; 0 for forward and 1 otherwise
150   @field  bin     bin calculated by bam_reg2bin()
151   @field  qual    mapping quality
152   @field  l_qname length of the query name
153   @field  flag    bitwise flag
154   @field  n_cigar number of CIGAR operations
155   @field  l_qseq  length of the query sequence (read)
156  */
157 typedef struct {
158         int32_t tid;
159         int32_t pos;
160         uint32_t bin:16, qual:8, l_qname:8;
161         uint32_t flag:16, n_cigar:16;
162         int32_t l_qseq;
163         int32_t mtid;
164         int32_t mpos;
165         int32_t isize;
166 } bam1_core_t;
167
168 /*! @typedef
169   @abstract Structure for one alignment.
170   @field  core       core information about the alignment
171   @field  l_aux      length of auxiliary data
172   @field  data_len   current length of bam1_t::data
173   @field  m_data     maximum length of bam1_t::data
174   @field  data       all variable-length data, concatenated; structure: cigar-qname-seq-qual-aux
175
176   @discussion Notes:
177  
178    1. qname is zero tailing and core.l_qname includes the tailing '\0'.
179    2. l_qseq is calculated from the total length of an alignment block
180       on reading or from CIGAR.
181  */
182 typedef struct {
183         bam1_core_t core;
184         int l_aux, data_len, m_data;
185         uint8_t *data;
186 } bam1_t;
187
188 #define bam1_strand(b) (((b)->core.flag&BAM_FREVERSE) != 0)
189 #define bam1_mstrand(b) (((b)->core.flag&BAM_FMREVERSE) != 0)
190
191 /*! @function
192   @abstract  Get the CIGAR array
193   @param  b  pointer to an alignment
194   @return    pointer to the CIGAR array
195
196   @discussion In the CIGAR array, each element is a 32-bit integer. The
197   lower 4 bits gives a CIGAR operation and the higher 28 bits keep the
198   length of a CIGAR.
199  */
200 #define bam1_cigar(b) ((uint32_t*)((b)->data + (b)->core.l_qname))
201
202 /*! @function
203   @abstract  Get the name of the query
204   @param  b  pointer to an alignment
205   @return    pointer to the name string, null terminated
206  */
207 #define bam1_qname(b) ((char*)((b)->data))
208
209 /*! @function
210   @abstract  Get query sequence
211   @param  b  pointer to an alignment
212   @return    pointer to sequence
213
214   @discussion Each base is encoded in 4 bits: 1 for A, 2 for C, 4 for G,
215   8 for T and 15 for N. Two bases are packed in one byte with the base
216   at the higher 4 bits having smaller coordinate on the read. It is
217   recommended to use bam1_seqi() macro to get the base.
218  */
219 #define bam1_seq(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname)
220
221 /*! @function
222   @abstract  Get query quality
223   @param  b  pointer to an alignment
224   @return    pointer to quality string
225  */
226 #define bam1_qual(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + ((b)->core.l_qseq + 1)/2)
227
228 /*! @function
229   @abstract  Get a base on read
230   @param  s  Query sequence returned by bam1_seq()
231   @param  i  The i-th position, 0-based
232   @return    4-bit integer representing the base.
233  */
234 #define bam1_seqi(s, i) ((s)[(i)/2] >> 4*(1-(i)%2) & 0xf)
235
236 /*! @function
237   @abstract  Get query sequence and quality
238   @param  b  pointer to an alignment
239   @return    pointer to the concatenated auxiliary data
240  */
241 #define bam1_aux(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (b)->core.l_qseq + ((b)->core.l_qseq + 1)/2)
242
243 #ifndef kroundup32
244 /*! @function
245   @abstract  Round an integer to the next closest power-2 integer.
246   @param  x  integer to be rounded (in place)
247   @discussion x will be modified.
248  */
249 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
250 #endif
251
252 /*!
253   @abstract Whether the machine is big-endian; modified only in
254   bam_header_init().
255  */
256 extern int bam_is_be;
257
258 /*! @abstract Table for converting a nucleotide character to the 4-bit encoding. */
259 extern unsigned char bam_nt16_table[256];
260
261 /*! @abstract Table for converting a 4-bit encoded nucleotide to a letter. */
262 extern char *bam_nt16_rev_table;
263
264 extern char bam_nt16_nt4_table[];
265
266 #ifdef __cplusplus
267 extern "C" {
268 #endif
269
270         /*! @abstract TAM file handler */
271         typedef struct __tamFile_t *tamFile;
272
273         /*!
274           @abstract   Open a SAM file for reading, either uncompressed or compressed by gzip/zlib.
275           @param  fn  SAM file name
276           @return     SAM file handler
277          */
278         tamFile sam_open(const char *fn);
279
280         /*!
281           @abstract   Close a SAM file handler
282           @param  fp  SAM file handler
283          */
284         void sam_close(tamFile fp);
285
286         /*!
287           @abstract      Read one alignment from a SAM file handler
288           @param  fp     SAM file handler
289           @param  header header information (ordered names of chromosomes)
290           @param  b      read alignment; all members in b will be updated
291           @return        0 if successful; otherwise negative
292          */
293         int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b);
294
295         /*!
296           @abstract       Read header information from a TAB-delimited list file.
297           @param  fn_list file name for the list
298           @return         a pointer to the header structure
299
300           @discussion Each line in this file consists of chromosome name and
301           the length of chromosome.
302          */
303         bam_header_t *sam_header_read2(const char *fn_list);
304
305         /*!
306           @abstract       Read header from a SAM file (if present)
307           @param  fp      SAM file handler
308           @return         pointer to header struct; 0 if no @SQ lines available
309          */
310         bam_header_t *sam_header_read(tamFile fp);
311
312         /*!
313           @abstract       Parse @SQ lines a update a header struct
314           @param  h       pointer to the header struct to be updated
315           @return         number of target sequences
316
317           @discussion bam_header_t::{n_targets,target_len,target_name} will
318           be destroyed in the first place.
319          */
320         int sam_header_parse(bam_header_t *h);
321
322         /*!
323           @abstract       Parse @RG lines a update a header struct
324           @param  h       pointer to the header struct to be updated
325           @return         number of @RG lines
326
327           @discussion bam_header_t::rg2lib will be destroyed in the first
328           place.
329          */
330         int sam_header_parse_rg(bam_header_t *h);
331
332 #define sam_write1(header, b) bam_view1(header, b)
333
334         int bam_strmap_put(void *strmap, const char *rg, const char *lib);
335         const char *bam_strmap_get(const void *strmap, const char *rg);
336         void *bam_strmap_dup(const void*);
337         void *bam_strmap_init();
338         void bam_strmap_destroy(void *strmap);
339
340         /*!
341           @abstract Initialize a header structure.
342           @return   the pointer to the header structure
343
344           @discussion This function also modifies the global variable
345           bam_is_be.
346          */
347         bam_header_t *bam_header_init();
348
349         /*!
350           @abstract        Destroy a header structure.
351           @param  header  pointer to the header
352          */
353         void bam_header_destroy(bam_header_t *header);
354
355         /*!
356           @abstract   Read a header structure from BAM.
357           @param  fp  BAM file handler, opened by bam_open()
358           @return     pointer to the header structure
359
360           @discussion The file position indicator must be placed at the
361           beginning of the file. Upon success, the position indicator will
362           be set at the start of the first alignment.
363          */
364         bam_header_t *bam_header_read(bamFile fp);
365
366         /*!
367           @abstract      Write a header structure to BAM.
368           @param  fp     BAM file handler
369           @param  header pointer to the header structure
370           @return        always 0 currently
371          */
372         int bam_header_write(bamFile fp, const bam_header_t *header);
373
374         /*!
375           @abstract   Read an alignment from BAM.
376           @param  fp  BAM file handler
377           @param  b   read alignment; all members are updated.
378           @return     number of bytes read from the file
379
380           @discussion The file position indicator must be
381           placed right before an alignment. Upon success, this function
382           will set the position indicator to the start of the next
383           alignment. This function is not affected by the machine
384           endianness.
385          */
386         int bam_read1(bamFile fp, bam1_t *b);
387
388         /*!
389           @abstract Write an alignment to BAM.
390           @param  fp       BAM file handler
391           @param  c        pointer to the bam1_core_t structure
392           @param  data_len total length of variable size data related to
393                            the alignment
394           @param  data     pointer to the concatenated data
395           @return          number of bytes written to the file
396
397           @discussion This function is not affected by the machine
398           endianness.
399          */
400         int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data);
401
402         /*!
403           @abstract   Write an alignment to BAM.
404           @param  fp  BAM file handler
405           @param  b   alignment to write
406           @return     number of bytes written to the file
407
408           @abstract It is equivalent to:
409             bam_write1_core(fp, &b->core, b->data_len, b->data)
410          */
411         int bam_write1(bamFile fp, const bam1_t *b);
412
413         /*! @function
414           @abstract  Initiate a pointer to bam1_t struct
415          */
416 #define bam_init1() ((bam1_t*)calloc(1, sizeof(bam1_t)))
417
418         /*! @function
419           @abstract  Free the memory allocated for an alignment.
420           @param  b  pointer to an alignment
421          */
422 #define bam_destroy1(b) do {            \
423                 free((b)->data); free(b);       \
424         } while (0)
425
426         /*!
427           @abstract       Format a BAM record in the SAM format
428           @param  header  pointer to the header structure
429           @param  b       alignment to print
430           @return         a pointer to the SAM string
431          */
432         char *bam_format1(const bam_header_t *header, const bam1_t *b);
433
434         /*! @typedef
435           @abstract Structure for one alignment covering the pileup position.
436           @field  b      pointer to the alignment
437           @field  qpos   position of the read base at the pileup site, 0-based
438           @field  indel  indel length; 0 for no indel, positive for ins and negative for del
439           @field  is_del 1 iff the base on the padded read is a deletion
440           @field  level  the level of the read in the "viewer" mode
441
442           @discussion See also bam_plbuf_push() and bam_lplbuf_push(). The
443           difference between the two functions is that the former does not
444           set bam_pileup1_t::level, while the later does. Level helps the
445           implementation of alignment viewers, but calculating this has some
446           overhead.
447          */
448         typedef struct {
449                 bam1_t *b;
450                 int32_t qpos;
451                 int indel, level;
452                 uint32_t is_del:1, is_head:1, is_tail:1;
453         } bam_pileup1_t;
454
455         struct __bam_plbuf_t;
456         /*! @abstract pileup buffer */
457         typedef struct __bam_plbuf_t bam_plbuf_t;
458
459         void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask);
460
461         /*! @typedef
462           @abstract    Type of function to be called by bam_plbuf_push().
463           @param  tid  chromosome ID as is defined in the header
464           @param  pos  start coordinate of the alignment, 0-based
465           @param  n    number of elements in pl array
466           @param  pl   array of alignments
467           @param  data user provided data
468           @discussion  See also bam_plbuf_push(), bam_plbuf_init() and bam_pileup1_t.
469          */
470         typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
471
472         /*!
473           @abstract     Reset a pileup buffer for another pileup process
474           @param  buf   the pileup buffer to be reset
475          */
476         void bam_plbuf_reset(bam_plbuf_t *buf);
477
478         /*!
479           @abstract     Initialize a buffer for pileup.
480           @param  func  fucntion to be called by bam_pileup_core()
481           @param  data  user provided data
482           @return       pointer to the pileup buffer
483          */
484         bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data);
485
486         /*!
487           @abstract    Destroy a pileup buffer.
488           @param  buf  pointer to the pileup buffer
489          */
490         void bam_plbuf_destroy(bam_plbuf_t *buf);
491
492         /*!
493           @abstract    Push an alignment to the pileup buffer.
494           @param  b    alignment to be pushed
495           @param  buf  pileup buffer
496           @see         bam_plbuf_init()
497           @return      always 0 currently
498
499           @discussion If all the alignments covering a particular site have
500           been collected, this function will call the user defined function
501           as is provided to bam_plbuf_init(). The coordinate of the site and
502           all the alignments will be transferred to the user defined
503           function as function parameters.
504          
505           When all the alignments are pushed to the buffer, this function
506           needs to be called with b equal to NULL. This will flush the
507           buffer. A pileup buffer can only be reused when bam_plbuf_reset()
508           is called.
509          */
510         int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf);
511
512         int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data);
513
514         struct __bam_lplbuf_t;
515         typedef struct __bam_lplbuf_t bam_lplbuf_t;
516
517         void bam_lplbuf_reset(bam_lplbuf_t *buf);
518
519         /*! @abstract  bam_plbuf_init() equivalent with level calculated. */
520         bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data);
521
522         /*! @abstract  bam_plbuf_destroy() equivalent with level calculated. */
523         void bam_lplbuf_destroy(bam_lplbuf_t *tv);
524
525         /*! @abstract  bam_plbuf_push() equivalent with level calculated. */
526         int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *buf);
527
528         struct __bam_index_t;
529         typedef struct __bam_index_t bam_index_t;
530
531         /*!
532           @abstract   Build index for a BAM file.
533           @discussion Index file "fn.bai" will be created.
534           @param  fn  name of the BAM file
535           @return     always 0 currently
536          */
537         int bam_index_build(const char *fn);
538
539         /*!
540           @abstract   Load index from file "fn.bai".
541           @param  fn  name of the BAM file (NOT the index file)
542           @return     pointer to the index structure
543          */
544         bam_index_t *bam_index_load(const char *fn);
545
546         /*!
547           @abstract    Destroy an index structure.
548           @param  idx  pointer to the index structure
549          */
550         void bam_index_destroy(bam_index_t *idx);
551
552         /*! @typedef
553           @abstract      Type of function to be called by bam_fetch().
554           @param  b     the alignment
555           @param  data  user provided data
556          */
557         typedef int (*bam_fetch_f)(const bam1_t *b, void *data);
558
559         /*!
560           @abstract Retrieve the alignments that are overlapped with the
561           specified region.
562
563           @discussion A user defined function will be called for each
564           retrieved alignment ordered by its start position.
565
566           @param  fp    BAM file handler
567           @param  idx   pointer to the alignment index
568           @param  tid   chromosome ID as is defined in the header
569           @param  beg   start coordinate, 0-based
570           @param  end   end coordinate, 0-based
571           @param  data  user provided data (will be transferred to func)
572           @param  func  user defined function
573          */
574         int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func);
575
576         /*!
577           @abstract       Parse a region in the format: "chr2:100,000-200,000".
578           @discussion     bam_header_t::hash will be initialized if empty.
579           @param  header  pointer to the header structure
580           @param  str     string to be parsed
581           @param  ref_id  the returned chromosome ID
582           @param  begin   the returned start coordinate
583           @param  end     the returned end coordinate
584           @return         0 on success; -1 on failure
585          */
586         int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end);
587
588         /*!
589           @abstract       Retrieve data of a tag
590           @param  b       pointer to an alignment struct
591           @param  tag     two-character tag to be retrieved
592
593           @return  pointer to the type and data. The first character is the
594           type that can be 'iIsScCdfAZH'.
595
596           @discussion  Use bam_aux2?() series to convert the returned data to
597           the corresponding type.
598         */
599         uint8_t *bam_aux_get(const bam1_t *b, const char tag[2]);
600
601         int32_t bam_aux2i(const uint8_t *s);
602         float bam_aux2f(const uint8_t *s);
603         double bam_aux2d(const uint8_t *s);
604         char bam_aux2A(const uint8_t *s);
605         char *bam_aux2Z(const uint8_t *s);
606
607         void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data);
608
609         uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2]); // an alias of bam_aux_get()
610
611         /*!  
612           @abstract Calculate the rightmost coordinate of an alignment on the
613           reference genome.
614
615           @param  c      pointer to the bam1_core_t structure
616           @param  cigar  the corresponding CIGAR array (from bam1_t::cigar)
617           @return        the rightmost coordinate, 0-based
618         */
619         uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar);
620
621         /*!
622           @abstract      Calculate the length of the query sequence from CIGAR.
623           @param  c      pointer to the bam1_core_t structure
624           @param  cigar  the corresponding CIGAR array (from bam1_t::cigar)
625           @return        length of the query sequence
626         */
627         int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar);
628
629         typedef struct {
630                 int32_t qbeg, qend;
631                 int32_t tbeg, tend;
632                 int32_t cbeg, cend;
633         } bam_segreg_t;
634
635         int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg);
636
637 #ifdef __cplusplus
638 }
639 #endif
640
641 /*!
642   @abstract    Calculate the minimum bin that contains a region [beg,end).
643   @param  beg  start of the region, 0-based
644   @param  end  end of the region, 0-based
645   @return      bin
646  */
647 static inline int bam_reg2bin(uint32_t beg, uint32_t end)
648 {
649         --end;
650         if (beg>>14 == end>>14) return 4681 + (beg>>14);
651         if (beg>>17 == end>>17) return  585 + (beg>>17);
652         if (beg>>20 == end>>20) return   73 + (beg>>20);
653         if (beg>>23 == end>>23) return    9 + (beg>>23);
654         if (beg>>26 == end>>26) return    1 + (beg>>26);
655         return 0;
656 }
657
658 /*!
659   @abstract     Copy an alignment
660   @param  bdst  destination alignment struct
661   @param  bsrc  source alignment struct
662   @return       pointer to the destination alignment struct
663  */
664 static inline bam1_t *bam_copy1(bam1_t *bdst, const bam1_t *bsrc)
665 {
666         uint8_t *data = bdst->data;
667         int m_data = bdst->m_data;   // backup data and m_data
668         if (m_data < bsrc->m_data) { // double the capacity
669                 m_data = bsrc->m_data; kroundup32(m_data);
670                 data = (uint8_t*)realloc(data, m_data);
671         }
672         memcpy(data, bsrc->data, bsrc->data_len); // copy var-len data
673         *bdst = *bsrc; // copy the rest
674         // restore the backup
675         bdst->m_data = m_data;
676         bdst->data = data;
677         return bdst;
678 }
679
680 /*!
681   @abstract     Duplicate an alignment
682   @param  src   source alignment struct
683   @return       pointer to the destination alignment struct
684  */
685 static inline bam1_t *bam_dup1(const bam1_t *src)
686 {
687         bam1_t *b;
688         b = bam_init1();
689         *b = *src;
690         b->m_data = b->data_len;
691         b->data = (uint8_t*)calloc(b->data_len, 1);
692         memcpy(b->data, src->data, b->data_len);
693         return b;
694 }
695
696 #endif