Debian Codegraph @

samtools/stats_isize.c.pysam.c

raw symbols SCIP
1 #include "samtools.pysam.h"
2
3 /* stats_isize.c -- generalised insert size calculation for samtools stats.
4
5 Copyright (C) 2014, 2018 Genome Research Ltd.
6
7 Author: Nicholas Clarke <[email protected]>
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE. */
26
27 #include <config.h>
28
29 #include <stdio.h>
30 #include "stats_isize.h"
31 #include <htslib/khash.h>
32
33 typedef enum {IN,OUT,OTHER} isize_insert_t;
34
35 static int max(int a, int b) {
36 if (a < b) {
37 return b;
38 } else {
39 return a;
40 }
41 }
42
43 static isize_sparse_record_t * sparse_get_f(isize_data_t data, int at) {
44 isize_sparse_data_t *a = data.sparse;
45 khash_t(m32) *h = a->array;
46
47 khint_t k = kh_get(m32, h, at);
48 if (k != kh_end(h)) {
49 return kh_value(h, k);
50 } else {
51 return NULL;
52 }
53 }
54
55 static uint64_t sparse_in_f(isize_data_t data, int at) {
56 isize_sparse_record_t* a = sparse_get_f(data, at);
57 if (a != NULL) {
58 return a->isize_inward;
59 } else {
60 return 0;
61 }
62 }
63 static uint64_t sparse_out_f(isize_data_t data, int at) {
64 isize_sparse_record_t* a = sparse_get_f(data, at);
65 if (a != NULL) {
66 return a->isize_outward;
67 } else {
68 return 0;
69 }
70 }
71 static uint64_t sparse_other_f(isize_data_t data, int at) {
72 isize_sparse_record_t* a = sparse_get_f(data, at);
73 if (a != NULL) {
74 return a->isize_other;
75 } else {
76 return 0;
77 }
78 }
79
80 static void sparse_set_f(isize_data_t data, int at, isize_insert_t field, uint64_t value) {
81 isize_sparse_data_t *a = data.sparse;
82 khash_t(m32) *h = a->array;
83
84 khint_t k = kh_get(m32, h, at);
85 isize_sparse_record_t *rec;
86 if (k != kh_end(h)) {
87 rec = kh_value(h, k);
88 } else if (value != 0) {
89 rec = malloc(sizeof(isize_sparse_record_t));
90 if (rec != NULL) {
91 rec->isize_inward = 0;
92 rec->isize_outward = 0;
93 rec->isize_other = 0;
94 int stupid = 0;
95 khint_t it = kh_put(m32, h, at, & stupid);
96 kh_value(h, it) = rec;
97 a->max = max(at, a->max);
98 } else {
99 fprintf(samtools_stderr, "%s\n", "Failed to allocate memory for isize_sparse_record_t");
100 samtools_exit(11);
101 }
102 } else {
103 return;
104 }
105 if (field == IN) {
106 rec->isize_inward = value;
107 } else if (field == OUT) {
108 rec->isize_outward = value;
109 } else {
110 rec->isize_other = value;
111 }
112
113 }
114
115 static void sparse_set_in_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, IN, value); }
116 static void sparse_set_out_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OUT, value); }
117 static void sparse_set_other_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OTHER, value); }
118
119 static void sparse_inc_in_f(isize_data_t data, int at) { sparse_set_in_f(data, at, sparse_in_f(data, at) + 1); }
120 static void sparse_inc_out_f(isize_data_t data, int at) { sparse_set_out_f(data, at, sparse_out_f(data, at) + 1); }
121 static void sparse_inc_other_f(isize_data_t data, int at) { sparse_set_other_f(data, at, sparse_other_f(data, at) + 1); }
122
123 static void sparse_isize_free(isize_data_t data) {
124 isize_sparse_data_t *a = data.sparse;
125 khint_t k;
126 for (k = 0; k < kh_end(a->array); ++k)
127 if (kh_exist(a->array, k)) free(kh_val(a->array, k));
128 kh_destroy(m32, a->array);
129 free(a);
130 }
131
132 static int sparse_nitems(isize_data_t data) {
133 isize_sparse_data_t *a = data.sparse;
134 return a->max + 1;
135 }
136
137 static uint64_t dense_in_f(isize_data_t data, int at) { return data.dense->isize_inward[at]; }
138 static uint64_t dense_out_f(isize_data_t data, int at) { return data.dense->isize_outward[at]; }
139 static uint64_t dense_other_f(isize_data_t data, int at) { return data.dense->isize_other[at]; }
140
141 static void dense_set_in_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_inward[at] = value; }
142 static void dense_set_out_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_outward[at] = value; }
143 static void dense_set_other_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_other[at] = value; }
144
145 static void dense_inc_in_f(isize_data_t data, int at) { data.dense->isize_inward[at] += 1; }
146 static void dense_inc_out_f(isize_data_t data, int at) { data.dense->isize_outward[at] += 1; }
147 static void dense_inc_other_f(isize_data_t data, int at) { data.dense->isize_other[at] += 1; }
148
149 static void dense_isize_free(isize_data_t data) {
150 isize_dense_data_t *a = data.dense;
151 free(a->isize_inward);
152 free(a->isize_outward);
153 free(a->isize_other);
154 free(a);
155 }
156
157 static int dense_nitems(isize_data_t data) {
158 isize_dense_data_t *a = data.dense;
159 return a->total;
160 }
161
162 // Construct a relevant isize_t given the bound.
163 isize_t *init_isize_t(int bound) {
164 if (bound <= 0) {
165 // Use sparse data structure.
166 isize_sparse_data_t *data = (isize_sparse_data_t *) malloc(sizeof(isize_sparse_data_t));
167 if (!data)
168 return NULL;
169
170 // Initialise
171 data->max = 0;
172 data->array = kh_init(m32);
173 if (!data->array) {
174 free(data);
175 return NULL;
176 }
177
178 isize_t *isize = (isize_t *)malloc(sizeof(isize_t));
179 if (!isize) {
180 kh_destroy(m32, data->array);
181 free(data);
182 return NULL;
183 }
184
185 isize->data.sparse = data;
186 isize->nitems = & sparse_nitems;
187
188 isize->inward = & sparse_in_f;
189 isize->outward = & sparse_out_f;
190 isize->other = & sparse_other_f;
191
192 isize->set_inward = & sparse_set_in_f;
193 isize->set_outward = & sparse_set_out_f;
194 isize->set_other = & sparse_set_other_f;
195
196 isize->inc_inward = & sparse_inc_in_f;
197 isize->inc_outward = & sparse_inc_out_f;
198 isize->inc_other = & sparse_inc_other_f;
199
200 isize->isize_free = & sparse_isize_free;
201
202 return isize;
203 } else {
204 uint64_t* in = calloc(bound,sizeof(uint64_t));
205 uint64_t* out = calloc(bound,sizeof(uint64_t));
206 uint64_t* other = calloc(bound,sizeof(uint64_t));
207 isize_dense_data_t *rec = (isize_dense_data_t *)malloc(sizeof(isize_dense_data_t));
208 isize_t *isize = (isize_t *)malloc(sizeof(isize_t));
209 if (!in || !out || !other || !rec || !isize) {
210 free(in);
211 free(out);
212 free(other);
213 free(rec);
214 free(isize);
215 return NULL;
216 }
217 rec->isize_inward = in;
218 rec->isize_outward = out;
219 rec->isize_other = other;
220 rec->total=bound;
221
222 isize->data.dense = rec;
223 isize->nitems = & dense_nitems;
224
225 isize->inward = & dense_in_f;
226 isize->outward = & dense_out_f;
227 isize->other = & dense_other_f;
228
229 isize->set_inward = & dense_set_in_f;
230 isize->set_outward = & dense_set_out_f;
231 isize->set_other = & dense_set_other_f;
232
233 isize->inc_inward = & dense_inc_in_f;
234 isize->inc_outward = & dense_inc_out_f;
235 isize->inc_other = & dense_inc_other_f;
236
237 isize->isize_free = & dense_isize_free;
238
239 return isize;
240 }
241 }