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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*
* Copyright (c) 2013 Genome Research Ltd.
* Author(s): James Bonfield
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
* Institute nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
* LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Author: James Bonfield, Wellcome Trust Sanger Institute. 2013
*/
#ifdef HAVE_CONFIG_H
#include "io_lib_config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <ctype.h>
#include "io_lib/cram.h"
#include "io_lib/os.h"
cram_stats *cram_stats_create(void) {
return calloc(1, sizeof(cram_stats));
}
void cram_stats_add(cram_stats *st, int64_t val) {
st->nsamp++;
if (val < MAX_STAT_VAL && val >= 0) {
st->freqs[val]++;
} else {
HashItem *hi;
if (!st->h) {
st->h = HashTableCreate(2048, HASH_DYNAMIC_SIZE|HASH_NONVOLATILE_KEYS|HASH_INT_KEYS);
}
if ((hi = HashTableSearch(st->h, (char *)(size_t)val, 8))) {
hi->data.i++;
} else {
HashData hd;
hd.i = 1;
HashTableAdd(st->h, (char *)(size_t)val, 8, hd, NULL);
}
}
}
void cram_stats_del(cram_stats *st, int64_t val) {
st->nsamp--;
if (val < MAX_STAT_VAL && val >= 0) {
st->freqs[val]--;
assert(st->freqs[val] >= 0);
} else if (st->h) {
HashItem *hi;
if ((hi = HashTableSearch(st->h, (char *)(size_t)val, 8))) {
if (--hi->data.i == 0)
HashTableDel(st->h, hi, 0);
} else {
fprintf(stderr, "Failed to remove val %"PRId64" from cram_stats\n", val);
st->nsamp++;
}
} else {
fprintf(stderr, "Failed to remove val %"PRId64" from cram_stats\n", val);
st->nsamp++;
}
}
void cram_stats_dump(cram_stats *st) {
int i;
fprintf(stderr, "cram_stats:\n");
for (i = 0; i < MAX_STAT_VAL; i++) {
if (!st->freqs[i])
continue;
fprintf(stderr, "\t%d\t%d\n", i, st->freqs[i]);
}
if (st->h) {
HashIter *iter= HashTableIterCreate();
HashItem *hi;
while ((hi = HashTableIterNext(st->h, iter))) {
fprintf(stderr, "\t%d\t%d\n", (int)(size_t)hi->key,
(int)hi->data.i);
}
HashTableIterDestroy(iter);
}
}
/*
* Computes entropy from integer frequencies for various encoding methods and
* picks the best encoding.
*
* FIXME: we could reuse some of the code here for the actual encoding
* parameters too. Eg the best 'k' for SUBEXP or the code lengths for huffman.
*
* Returns the best codec to use.
*/
enum cram_encoding cram_stats_encoding(cram_fd *fd, cram_stats *st) {
int nvals, i, ntot = 0, max_val = 0, min_val = INT_MAX;
int *vals = NULL, *freqs = NULL, vals_alloc = 0;
//cram_stats_dump(st);
/* Count number of unique symbols */
for (nvals = i = 0; i < MAX_STAT_VAL; i++) {
if (!st->freqs[i])
continue;
if (nvals >= vals_alloc) {
vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
vals = realloc(vals, vals_alloc * sizeof(int));
freqs = realloc(freqs, vals_alloc * sizeof(int));
if (!vals || !freqs) {
if (vals) free(vals);
if (freqs) free(freqs);
return E_HUFFMAN; // Cannot do much else atm
}
}
vals[nvals] = i;
freqs[nvals] = st->freqs[i];
ntot += freqs[nvals];
if (max_val < i) max_val = i;
if (min_val > i) min_val = i;
nvals++;
}
if (st->h) {
HashIter *iter= HashTableIterCreate();
HashItem *hi;
int i;
while ((hi = HashTableIterNext(st->h, iter))) {
if (nvals >= vals_alloc) {
vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
vals = realloc(vals, vals_alloc * sizeof(int));
freqs = realloc(freqs, vals_alloc * sizeof(int));
if (!vals || !freqs)
return E_HUFFMAN; // Cannot do much else atm
}
i = (size_t)hi->key;
vals[nvals]=i;
freqs[nvals] = hi->data.i;
ntot += freqs[nvals];
if (max_val < i) max_val = i;
if (min_val > i) min_val = i;
nvals++;
}
HashTableIterDestroy(iter);
}
st->nvals = nvals;
assert(ntot == st->nsamp);
free(vals);
free(freqs);
// Crude and simple alternative.
return nvals > 1 ? E_EXTERNAL : E_HUFFMAN;
#ifdef RANDOMISER
// RANDOMISER
switch(random()%10) {
case 0: return E_HUFFMAN;
case 1: return E_HUFFMAN;
//case 1: return E_BETA; // Java doesn't support E_BETA for BYTE vals
default: return E_EXTERNAL;
}
#endif
// Single value items are best served in HUFFMAN as this takes
// zero bits to store (it's only needs the compression header).
if (nvals <= 1) {
if (fd->verbose > 1)
fprintf(stderr, "0 values => 0 bits\n");
return E_HUFFMAN;
}
if (fd->verbose > 1)
fprintf(stderr, "Range = %d..%d, nvals=%d, ntot=%d\n",
min_val, max_val, nvals, ntot);
// Massive cull of old entropy analysis.
// If you're interested in code to compare huffman vs beta
// bit streams, see svn revision r4051
return E_EXTERNAL;
}
void cram_stats_free(cram_stats *st) {
if (st->h)
HashTableDestroy(st->h, 0);
free(st);
}
|