| 1 |
#ifndef BAMLITE_H_ |
| 2 |
#define BAMLITE_H_ |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <zlib.h> |
| 6 |
|
| 7 |
#ifdef USE_MALLOC_WRAPPERS |
| 8 |
# include "malloc_wrap.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#define USE_VERBOSE_ZLIB_WRAPPERS |
| 12 |
|
| 13 |
typedef gzFile bamFile; |
| 14 |
#ifdef USE_VERBOSE_ZLIB_WRAPPERS |
| 15 |
/* These print error messages on failure */ |
| 16 |
# define bam_open(fn, mode) bamlite_gzopen(fn, mode) |
| 17 |
# define bam_dopen(fd, mode) gzdopen(fd, mode) |
| 18 |
# define bam_close(fp) bamlite_gzclose(fp) |
| 19 |
# define bam_read(fp, buf, size) bamlite_gzread(fp, buf, size) |
| 20 |
#else |
| 21 |
# define bam_open(fn, mode) gzopen(fn, mode) |
| 22 |
# define bam_dopen(fd, mode) gzdopen(fd, mode) |
| 23 |
# define bam_close(fp) gzclose(fp) |
| 24 |
# define bam_read(fp, buf, size) gzread(fp, buf, size) |
| 25 |
#endif /* USE_VERBOSE_ZLIB_WRAPPERS */ |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
int32_t n_targets; |
| 29 |
char **target_name; |
| 30 |
uint32_t *target_len; |
| 31 |
size_t l_text, n_text; |
| 32 |
char *text; |
| 33 |
} bam_header_t; |
| 34 |
|
| 35 |
#define BAM_FPAIRED 1 |
| 36 |
#define BAM_FPROPER_PAIR 2 |
| 37 |
#define BAM_FUNMAP 4 |
| 38 |
#define BAM_FMUNMAP 8 |
| 39 |
#define BAM_FREVERSE 16 |
| 40 |
#define BAM_FMREVERSE 32 |
| 41 |
#define BAM_FREAD1 64 |
| 42 |
#define BAM_FREAD2 128 |
| 43 |
#define BAM_FSECONDARY 256 |
| 44 |
#define BAM_FQCFAIL 512 |
| 45 |
#define BAM_FDUP 1024 |
| 46 |
|
| 47 |
#define BAM_CIGAR_SHIFT 4 |
| 48 |
#define BAM_CIGAR_MASK ((1 << BAM_CIGAR_SHIFT) - 1) |
| 49 |
|
| 50 |
#define BAM_CMATCH 0 |
| 51 |
#define BAM_CINS 1 |
| 52 |
#define BAM_CDEL 2 |
| 53 |
#define BAM_CREF_SKIP 3 |
| 54 |
#define BAM_CSOFT_CLIP 4 |
| 55 |
#define BAM_CHARD_CLIP 5 |
| 56 |
#define BAM_CPAD 6 |
| 57 |
|
| 58 |
typedef struct { |
| 59 |
int32_t tid; |
| 60 |
int32_t pos; |
| 61 |
uint32_t bin:16, qual:8, l_qname:8; |
| 62 |
uint32_t flag:16, n_cigar:16; |
| 63 |
int32_t l_qseq; |
| 64 |
int32_t mtid; |
| 65 |
int32_t mpos; |
| 66 |
int32_t isize; |
| 67 |
} bam1_core_t; |
| 68 |
|
| 69 |
typedef struct { |
| 70 |
bam1_core_t core; |
| 71 |
int l_aux, data_len, m_data; |
| 72 |
uint8_t *data; |
| 73 |
} bam1_t; |
| 74 |
|
| 75 |
#ifndef kroundup32 |
| 76 |
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) |
| 77 |
#endif |
| 78 |
|
| 79 |
#define bam1_strand(b) (((b)->core.flag&BAM_FREVERSE) != 0) |
| 80 |
#define bam1_mstrand(b) (((b)->core.flag&BAM_FMREVERSE) != 0) |
| 81 |
#define bam1_cigar(b) ((uint32_t*)((b)->data + (b)->core.l_qname)) |
| 82 |
#define bam1_qname(b) ((char*)((b)->data)) |
| 83 |
#define bam1_seq(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname) |
| 84 |
#define bam1_qual(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (((b)->core.l_qseq + 1)>>1)) |
| 85 |
#define bam1_seqi(s, i) ((s)[(i)/2] >> 4*(1-(i)%2) & 0xf) |
| 86 |
#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) |
| 87 |
|
| 88 |
#define bam_init1() ((bam1_t*)calloc(1, sizeof(bam1_t))) |
| 89 |
#define bam_destroy1(b) do { \ |
| 90 |
if (b) { free((b)->data); free(b); } \ |
| 91 |
} while (0) |
| 92 |
|
| 93 |
extern int bam_is_be; |
| 94 |
|
| 95 |
#ifdef __cplusplus |
| 96 |
extern "C" { |
| 97 |
#endif |
| 98 |
|
| 99 |
bam_header_t *bam_header_init(void); |
| 100 |
void bam_header_destroy(bam_header_t *header); |
| 101 |
bam_header_t *bam_header_read(bamFile fp); |
| 102 |
int bam_read1(bamFile fp, bam1_t *b); |
| 103 |
|
| 104 |
#ifdef USE_VERBOSE_ZLIB_WRAPPERS |
| 105 |
gzFile bamlite_gzopen(const char *fn, const char *mode); |
| 106 |
int bamlite_gzread(gzFile file, void *ptr, unsigned int len); |
| 107 |
int bamlite_gzclose(gzFile file); |
| 108 |
#endif /* USE_VERBOSE_ZLIB_WRAPPERS */ |
| 109 |
|
| 110 |
#ifdef __cplusplus |
| 111 |
} |
| 112 |
#endif |
| 113 |
|
| 114 |
#endif |