Debian Codegraph @

pysam/htslib_util.c

raw symbols SCIP
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 // set htslib verbosity level
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 // taken from samtools/bam_import.c
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 // update the variable length data within a bam1_t entry.
44 // Adds *nbytes_new* - *nbytes_old* into the variable length data of *src* at *pos*.
45 // Data within the bam1_t entry is moved so that it is
46 // consistent with the data field lengths.
47 // Return NULL on error (memory allocation)
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 // no change
59 if (d == 0)
60 return b;
61
62 // new size of total data
63 new_size = d + b->l_data;
64
65 // fields before field in data
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 // increase memory if required
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 // move data after field to new location
84 memmove(field_start + nbytes_new,
85 field_start + nbytes_old,
86 b->l_data - (nbytes_before + nbytes_old));
87
88 // adjust l_data
89 b->l_data = new_size;
90
91 return b;
92 }
93
94 // translate a nucleotide character to binary code
95 unsigned char pysam_translate_sequence(const unsigned char s)
96 {
97 return seq_nt16_table[s];
98 }
99
100 // Auxiliary functions for B support
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 // check that type is 'B'
113 if('B' != type) return;
114
115 ori_len = b->l_data;
116
117 l_data = len * aux_type2size(subtype);
118 // infer the data length from the sub-type
119 b->l_data += 8 + l_data;
120
121 // htslib: obsolete?
122 // b->l_aux += 8 + l_data;
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 // tag
133 b->data[ori_len + 2] = type;
134 // type
135 b->data[ori_len + 3] = subtype;
136 // subtype
137 (*(int32_t*)(b->data + ori_len + 4)) = len;
138 // size
139 memcpy(b->data + ori_len + 8, data, l_data);
140 // data
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 }