| 1 |
#include <ctype.h> |
| 2 |
#include <assert.h> |
| 3 |
#include "htslib/khash.h" |
| 4 |
#include "htslib/ksort.h" |
| 5 |
#include "htslib/sam.h" |
| 6 |
#include "htslib/hts.h" |
| 7 |
#include "htslib/knetfile.h" |
| 8 |
#include "htslib/kseq.h" |
| 9 |
#include "htslib_util.h" |
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#ifndef inline |
| 13 |
#define inline __inline |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
extern int hts_verbose; |
| 18 |
int hts_set_verbosity(int verbosity) |
| 19 |
{ |
| 20 |
int old_verbosity = hts_verbose; |
| 21 |
hts_verbose = verbosity; |
| 22 |
return old_verbosity; |
| 23 |
} |
| 24 |
|
| 25 |
int hts_get_verbosity(void) |
| 26 |
{ |
| 27 |
return hts_verbose; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
static inline uint8_t * alloc_data(bam1_t *b, size_t size) |
| 33 |
{ |
| 34 |
if (b->m_data < size) |
| 35 |
{ |
| 36 |
b->m_data = size; |
| 37 |
kroundup32(b->m_data); |
| 38 |
b->data = (uint8_t*)realloc(b->data, b->m_data); |
| 39 |
} |
| 40 |
return b->data; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
bam1_t * pysam_bam_update(bam1_t * b, |
| 49 |
const size_t nbytes_old, |
| 50 |
const size_t nbytes_new, |
| 51 |
uint8_t * field_start) |
| 52 |
{ |
| 53 |
int d = nbytes_new - nbytes_old; |
| 54 |
int new_size; |
| 55 |
size_t nbytes_before; |
| 56 |
uint8_t * retval = NULL; |
| 57 |
|
| 58 |
|
| 59 |
if (d == 0) |
| 60 |
return b; |
| 61 |
|
| 62 |
|
| 63 |
new_size = d + b->l_data; |
| 64 |
|
| 65 |
|
| 66 |
nbytes_before = field_start - b->data; |
| 67 |
|
| 68 |
if (b->l_data != 0) |
| 69 |
{ |
| 70 |
assert(nbytes_before >= 0); |
| 71 |
assert(nbytes_before <= b->l_data); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
if (d > 0) |
| 76 |
{ |
| 77 |
retval = alloc_data(b, new_size); |
| 78 |
if (retval == NULL) |
| 79 |
return NULL; |
| 80 |
field_start = b->data + nbytes_before; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
memmove(field_start + nbytes_new, |
| 85 |
field_start + nbytes_old, |
| 86 |
b->l_data - (nbytes_before + nbytes_old)); |
| 87 |
|
| 88 |
|
| 89 |
b->l_data = new_size; |
| 90 |
|
| 91 |
return b; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
unsigned char pysam_translate_sequence(const unsigned char s) |
| 96 |
{ |
| 97 |
return seq_nt16_table[s]; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
void bam_aux_appendB(bam1_t *b, |
| 102 |
const char tag[2], |
| 103 |
char type, |
| 104 |
char subtype, |
| 105 |
int len, |
| 106 |
uint8_t *data) |
| 107 |
{ |
| 108 |
|
| 109 |
int ori_len; |
| 110 |
int l_data; |
| 111 |
|
| 112 |
|
| 113 |
if('B' != type) return; |
| 114 |
|
| 115 |
ori_len = b->l_data; |
| 116 |
|
| 117 |
l_data = len * aux_type2size(subtype); |
| 118 |
|
| 119 |
b->l_data += 8 + l_data; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if (b->m_data < b->l_data) |
| 124 |
{ |
| 125 |
b->m_data = b->l_data; |
| 126 |
kroundup32(b->m_data); |
| 127 |
b->data = (uint8_t*)realloc(b->data, b->m_data); |
| 128 |
} |
| 129 |
|
| 130 |
b->data[ori_len] = tag[0]; |
| 131 |
b->data[ori_len + 1] = tag[1]; |
| 132 |
|
| 133 |
b->data[ori_len + 2] = type; |
| 134 |
|
| 135 |
b->data[ori_len + 3] = subtype; |
| 136 |
|
| 137 |
(*(int32_t*)(b->data + ori_len + 4)) = len; |
| 138 |
|
| 139 |
memcpy(b->data + ori_len + 8, data, l_data); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
int aux_type2size(uint8_t type) |
| 144 |
{ |
| 145 |
switch (type) { |
| 146 |
case 'A': case 'c': case 'C': |
| 147 |
return 1; |
| 148 |
case 's': case 'S': |
| 149 |
return 2; |
| 150 |
case 'i': case 'I': case 'f': |
| 151 |
return 4; |
| 152 |
case 'd': |
| 153 |
return 8; |
| 154 |
case 'Z': case 'H': case 'B': |
| 155 |
return type; |
| 156 |
default: |
| 157 |
return 0; |
| 158 |
} |
| 159 |
} |