| 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 |
#include <config.h> |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include "stats_isize.h" |
| 29 |
#include <htslib/khash.h> |
| 30 |
|
| 31 |
typedef enum {IN,OUT,OTHER} isize_insert_t; |
| 32 |
|
| 33 |
static int max(int a, int b) { |
| 34 |
if (a < b) { |
| 35 |
return b; |
| 36 |
} else { |
| 37 |
return a; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
static isize_sparse_record_t * sparse_get_f(isize_data_t data, int at) { |
| 42 |
isize_sparse_data_t *a = data.sparse; |
| 43 |
khash_t(m32) *h = a->array; |
| 44 |
|
| 45 |
khint_t k = kh_get(m32, h, at); |
| 46 |
if (k != kh_end(h)) { |
| 47 |
return kh_value(h, k); |
| 48 |
} else { |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static uint64_t sparse_in_f(isize_data_t data, int at) { |
| 54 |
isize_sparse_record_t* a = sparse_get_f(data, at); |
| 55 |
if (a != NULL) { |
| 56 |
return a->isize_inward; |
| 57 |
} else { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
} |
| 61 |
static uint64_t sparse_out_f(isize_data_t data, int at) { |
| 62 |
isize_sparse_record_t* a = sparse_get_f(data, at); |
| 63 |
if (a != NULL) { |
| 64 |
return a->isize_outward; |
| 65 |
} else { |
| 66 |
return 0; |
| 67 |
} |
| 68 |
} |
| 69 |
static uint64_t sparse_other_f(isize_data_t data, int at) { |
| 70 |
isize_sparse_record_t* a = sparse_get_f(data, at); |
| 71 |
if (a != NULL) { |
| 72 |
return a->isize_other; |
| 73 |
} else { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void sparse_set_f(isize_data_t data, int at, isize_insert_t field, uint64_t value) { |
| 79 |
isize_sparse_data_t *a = data.sparse; |
| 80 |
khash_t(m32) *h = a->array; |
| 81 |
|
| 82 |
khint_t k = kh_get(m32, h, at); |
| 83 |
isize_sparse_record_t *rec; |
| 84 |
if (k != kh_end(h)) { |
| 85 |
rec = kh_value(h, k); |
| 86 |
} else if (value != 0) { |
| 87 |
rec = malloc(sizeof(isize_sparse_record_t)); |
| 88 |
if (rec != NULL) { |
| 89 |
rec->isize_inward = 0; |
| 90 |
rec->isize_outward = 0; |
| 91 |
rec->isize_other = 0; |
| 92 |
int stupid = 0; |
| 93 |
khint_t it = kh_put(m32, h, at, & stupid); |
| 94 |
kh_value(h, it) = rec; |
| 95 |
a->max = max(at, a->max); |
| 96 |
} else { |
| 97 |
fprintf(stderr, "%s\n", "Failed to allocate memory for isize_sparse_record_t"); |
| 98 |
exit(11); |
| 99 |
} |
| 100 |
} else { |
| 101 |
return; |
| 102 |
} |
| 103 |
if (field == IN) { |
| 104 |
rec->isize_inward = value; |
| 105 |
} else if (field == OUT) { |
| 106 |
rec->isize_outward = value; |
| 107 |
} else { |
| 108 |
rec->isize_other = value; |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
static void sparse_set_in_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, IN, value); } |
| 114 |
static void sparse_set_out_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OUT, value); } |
| 115 |
static void sparse_set_other_f(isize_data_t data, int at, uint64_t value) { sparse_set_f(data, at, OTHER, value); } |
| 116 |
|
| 117 |
static void sparse_inc_in_f(isize_data_t data, int at) { sparse_set_in_f(data, at, sparse_in_f(data, at) + 1); } |
| 118 |
static void sparse_inc_out_f(isize_data_t data, int at) { sparse_set_out_f(data, at, sparse_out_f(data, at) + 1); } |
| 119 |
static void sparse_inc_other_f(isize_data_t data, int at) { sparse_set_other_f(data, at, sparse_other_f(data, at) + 1); } |
| 120 |
|
| 121 |
static void sparse_isize_free(isize_data_t data) { |
| 122 |
isize_sparse_data_t *a = data.sparse; |
| 123 |
khint_t k; |
| 124 |
for (k = 0; k < kh_end(a->array); ++k) |
| 125 |
if (kh_exist(a->array, k)) free(kh_val(a->array, k)); |
| 126 |
kh_destroy(m32, a->array); |
| 127 |
free(a); |
| 128 |
} |
| 129 |
|
| 130 |
static int sparse_nitems(isize_data_t data) { |
| 131 |
isize_sparse_data_t *a = data.sparse; |
| 132 |
return a->max + 1; |
| 133 |
} |
| 134 |
|
| 135 |
static uint64_t dense_in_f(isize_data_t data, int at) { return data.dense->isize_inward[at]; } |
| 136 |
static uint64_t dense_out_f(isize_data_t data, int at) { return data.dense->isize_outward[at]; } |
| 137 |
static uint64_t dense_other_f(isize_data_t data, int at) { return data.dense->isize_other[at]; } |
| 138 |
|
| 139 |
static void dense_set_in_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_inward[at] = value; } |
| 140 |
static void dense_set_out_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_outward[at] = value; } |
| 141 |
static void dense_set_other_f(isize_data_t data, int at, uint64_t value) { data.dense->isize_other[at] = value; } |
| 142 |
|
| 143 |
static void dense_inc_in_f(isize_data_t data, int at) { data.dense->isize_inward[at] += 1; } |
| 144 |
static void dense_inc_out_f(isize_data_t data, int at) { data.dense->isize_outward[at] += 1; } |
| 145 |
static void dense_inc_other_f(isize_data_t data, int at) { data.dense->isize_other[at] += 1; } |
| 146 |
|
| 147 |
static void dense_isize_free(isize_data_t data) { |
| 148 |
isize_dense_data_t *a = data.dense; |
| 149 |
free(a->isize_inward); |
| 150 |
free(a->isize_outward); |
| 151 |
free(a->isize_other); |
| 152 |
free(a); |
| 153 |
} |
| 154 |
|
| 155 |
static int dense_nitems(isize_data_t data) { |
| 156 |
isize_dense_data_t *a = data.dense; |
| 157 |
return a->total; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
isize_t *init_isize_t(int bound) { |
| 162 |
if (bound <= 0) { |
| 163 |
|
| 164 |
isize_sparse_data_t *data = (isize_sparse_data_t *) malloc(sizeof(isize_sparse_data_t)); |
| 165 |
if (!data) |
| 166 |
return NULL; |
| 167 |
|
| 168 |
|
| 169 |
data->max = 0; |
| 170 |
data->array = kh_init(m32); |
| 171 |
if (!data->array) { |
| 172 |
free(data); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
|
| 176 |
isize_t *isize = (isize_t *)malloc(sizeof(isize_t)); |
| 177 |
if (!isize) { |
| 178 |
kh_destroy(m32, data->array); |
| 179 |
free(data); |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
isize->data.sparse = data; |
| 184 |
isize->nitems = & sparse_nitems; |
| 185 |
|
| 186 |
isize->inward = & sparse_in_f; |
| 187 |
isize->outward = & sparse_out_f; |
| 188 |
isize->other = & sparse_other_f; |
| 189 |
|
| 190 |
isize->set_inward = & sparse_set_in_f; |
| 191 |
isize->set_outward = & sparse_set_out_f; |
| 192 |
isize->set_other = & sparse_set_other_f; |
| 193 |
|
| 194 |
isize->inc_inward = & sparse_inc_in_f; |
| 195 |
isize->inc_outward = & sparse_inc_out_f; |
| 196 |
isize->inc_other = & sparse_inc_other_f; |
| 197 |
|
| 198 |
isize->isize_free = & sparse_isize_free; |
| 199 |
|
| 200 |
return isize; |
| 201 |
} else { |
| 202 |
uint64_t* in = calloc(bound,sizeof(uint64_t)); |
| 203 |
uint64_t* out = calloc(bound,sizeof(uint64_t)); |
| 204 |
uint64_t* other = calloc(bound,sizeof(uint64_t)); |
| 205 |
isize_dense_data_t *rec = (isize_dense_data_t *)malloc(sizeof(isize_dense_data_t)); |
| 206 |
isize_t *isize = (isize_t *)malloc(sizeof(isize_t)); |
| 207 |
if (!in || !out || !other || !rec || !isize) { |
| 208 |
free(in); |
| 209 |
free(out); |
| 210 |
free(other); |
| 211 |
free(rec); |
| 212 |
free(isize); |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
rec->isize_inward = in; |
| 216 |
rec->isize_outward = out; |
| 217 |
rec->isize_other = other; |
| 218 |
rec->total=bound; |
| 219 |
|
| 220 |
isize->data.dense = rec; |
| 221 |
isize->nitems = & dense_nitems; |
| 222 |
|
| 223 |
isize->inward = & dense_in_f; |
| 224 |
isize->outward = & dense_out_f; |
| 225 |
isize->other = & dense_other_f; |
| 226 |
|
| 227 |
isize->set_inward = & dense_set_in_f; |
| 228 |
isize->set_outward = & dense_set_out_f; |
| 229 |
isize->set_other = & dense_set_other_f; |
| 230 |
|
| 231 |
isize->inc_inward = & dense_inc_in_f; |
| 232 |
isize->inc_outward = & dense_inc_out_f; |
| 233 |
isize->inc_other = & dense_inc_other_f; |
| 234 |
|
| 235 |
isize->isize_free = & dense_isize_free; |
| 236 |
|
| 237 |
return isize; |
| 238 |
} |
| 239 |
} |