forked from samtools/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_isize.c
200 lines (166 loc) · 6.35 KB
/
stats_isize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Author: Nicholas Clarke ([email protected])
Generalises insert size calculation for samtools stats.
*/
#include <stdio.h>
#include "stats_isize.h"
#include <htslib/khash.h>
typedef enum {IN,OUT,OTHER} isize_insert_t;
static int max(int a, int b) {
if (a < b) {
return b;
} else {
return a;
}
}
static isize_sparse_record_t * sparse_get_f(isize_data_t data, int at) {
isize_sparse_data_t *a = data.sparse;
khash_t(m32) *h = a->array;
khint_t k = kh_get(m32, h, at);
if (k != kh_end(h)) {
return kh_value(h, k);
} else {
return NULL;
}
}
static uint64_t sparse_in_f(isize_data_t data, int at) {
isize_sparse_record_t* a = sparse_get_f(data, at);
if (a != NULL) {
return a->isize_inward;
} else {
return 0;
}
}
static uint64_t sparse_out_f(isize_data_t data, int at) {
isize_sparse_record_t* a = sparse_get_f(data, at);
if (a != NULL) {
return a->isize_outward;
} else {
return 0;
}
}
static uint64_t sparse_other_f(isize_data_t data, int at) {
isize_sparse_record_t* a = sparse_get_f(data, at);
if (a != NULL) {
return a->isize_other;
} else {
return 0;
}
}
static void sparse_set_f(isize_data_t data, int at, isize_insert_t field, uint64_t value) {
isize_sparse_data_t *a = data.sparse;
khash_t(m32) *h = a->array;
khint_t k = kh_get(m32, h, at);
isize_sparse_record_t *rec;
if (k != kh_end(h)) {
rec = kh_value(h, k);
} else if (value != 0) {
rec = malloc(sizeof(isize_sparse_record_t));
if (rec != NULL) {
rec->isize_inward = 0;
rec->isize_outward = 0;
rec->isize_other = 0;
int stupid = 0;
khint_t it = kh_put(m32, h, at, & stupid);
kh_value(h, it) = rec;
a->max = max(at, a->max);
} else {
fprintf(stderr, "%s\n", "Failed to allocate memory for isize_sparse_record_t");
exit(11);
}
} else {
return;
}
if (field == IN) {
rec->isize_inward = value;
} else if (field == OUT) {
rec->isize_outward = value;
} else {
rec->isize_other = value;
}
}
static void sparse_set_in_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, IN, value); }
static void sparse_set_out_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OUT, value); }
static void sparse_set_other_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OTHER, value); }
static void sparse_inc_in_f(isize_data_t data, int at) { sparse_set_in_f(data, at, sparse_in_f(data, at) + 1); }
static void sparse_inc_out_f(isize_data_t data, int at) { sparse_set_out_f(data, at, sparse_out_f(data, at) + 1); }
static void sparse_inc_other_f(isize_data_t data, int at) { sparse_set_other_f(data, at, sparse_other_f(data, at) + 1); }
static void sparse_isize_free(isize_data_t data) {
isize_sparse_data_t *a = data.sparse;
khint_t k;
for (k = 0; k < kh_end(a->array); ++k)
if (kh_exist(a->array, k)) free(kh_val(a->array, k));
kh_destroy(m32, a->array);
free(a);
}
static int sparse_nitems(isize_data_t data) {
isize_sparse_data_t *a = data.sparse;
return a->max + 1;
}
static uint64_t dense_in_f(isize_data_t data, int at) { return data.dense->isize_inward[at]; }
static uint64_t dense_out_f(isize_data_t data, int at) { return data.dense->isize_outward[at]; }
static uint64_t dense_other_f(isize_data_t data, int at) { return data.dense->isize_other[at]; }
static void dense_set_in_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_inward[at] = value; }
static void dense_set_out_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_outward[at] = value; }
static void dense_set_other_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_other[at] = value; }
static void dense_inc_in_f(isize_data_t data, int at) { data.dense->isize_inward[at] += 1; }
static void dense_inc_out_f(isize_data_t data, int at) { data.dense->isize_outward[at] += 1; }
static void dense_inc_other_f(isize_data_t data, int at) { data.dense->isize_other[at] += 1; }
static void dense_isize_free(isize_data_t data) {
isize_dense_data_t *a = data.dense;
free(a->isize_inward);
free(a->isize_outward);
free(a->isize_other);
free(a);
}
static int dense_nitems(isize_data_t data) {
isize_dense_data_t *a = data.dense;
return a->total;
}
// Construct a relevant isize_t given the bound.
isize_t *init_isize_t(int bound) {
if (bound <= 0) {
// Use sparse data structure.
isize_sparse_data_t *data = (isize_sparse_data_t *) malloc(sizeof(isize_sparse_data_t));
// Initialise
data->max = 0;
data->array = kh_init(m32);
isize_t *isize = (isize_t *)malloc(sizeof(isize_t));
isize->data.sparse = data;
isize->nitems = & sparse_nitems;
isize->inward = & sparse_in_f;
isize->outward = & sparse_out_f;
isize->other = & sparse_other_f;
isize->set_inward = & sparse_set_in_f;
isize->set_outward = & sparse_set_out_f;
isize->set_other = & sparse_set_other_f;
isize->inc_inward = & sparse_inc_in_f;
isize->inc_outward = & sparse_inc_out_f;
isize->inc_other = & sparse_inc_other_f;
isize->isize_free = & sparse_isize_free;
return isize;
} else {
uint64_t* in = calloc(bound,sizeof(uint64_t));
uint64_t* out = calloc(bound,sizeof(uint64_t));
uint64_t* other = calloc(bound,sizeof(uint64_t));
isize_dense_data_t *rec = (isize_dense_data_t *)malloc(sizeof(isize_dense_data_t));
rec->isize_inward = in;
rec->isize_outward = out;
rec->isize_other = other;
rec->total=bound;
isize_t *isize = (isize_t *)malloc(sizeof(isize_t));
isize->data.dense = rec;
isize->nitems = & dense_nitems;
isize->inward = & dense_in_f;
isize->outward = & dense_out_f;
isize->other = & dense_other_f;
isize->set_inward = & dense_set_in_f;
isize->set_outward = & dense_set_out_f;
isize->set_other = & dense_set_other_f;
isize->inc_inward = & dense_inc_in_f;
isize->inc_outward = & dense_inc_out_f;
isize->inc_other = & dense_inc_other_f;
isize->isize_free = & dense_isize_free;
return isize;
}
}