/usr/include/fast5/Huffman_Packer.hpp is in libfast5-dev 0.6.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | //
// Part of: https://github.com/mateidavid/fast5
//
// Copyright (c) 2015-2017 Matei David, Ontario Institute for Cancer Research
// MIT License
//
#ifndef __HUFFMAN_PACKER_HPP
#define __HUFFMAN_PACKER_HPP
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <limits>
#include <stdexcept>
#include <cassert>
#include <bitset>
#include "logger.hpp"
namespace fast5
{
class Huffman_Packer
{
public:
typedef std::vector< std::uint8_t > Code_Type;
typedef std::map< std::string, std::string > Code_Params_Type;
Huffman_Packer() = default;
Huffman_Packer(Huffman_Packer const &) = delete;
Huffman_Packer(Huffman_Packer &&) = default;
Huffman_Packer & operator = (Huffman_Packer const &) = delete;
Huffman_Packer & operator = (Huffman_Packer &&) = default;
Huffman_Packer(std::istream & is, std::string const & cwm_name)
{
load_codeword_map(is, cwm_name);
}
Huffman_Packer(std::vector< std::string > const & v, std::string const & cwm_name)
{
load_codeword_map(v.begin(), v.end(), cwm_name);
}
template < typename Iterator >
Huffman_Packer(Iterator it_begin, Iterator it_end, std::string const & cwm_name)
{
load_codeword_map(it_begin, it_end, cwm_name);
}
void load_codeword_map(std::istream & is, std::string const & cwm_name)
{
_cwm_name = cwm_name;
std::string v_s;
std::string cw_s;
while (is >> v_s >> cw_s)
{
add_codeword(v_s, cw_s);
}
}
template < typename Iterator >
void load_codeword_map(Iterator it_begin, Iterator it_end, std::string const & cwm_name)
{
_cwm_name = cwm_name;
for (auto it = it_begin; it != it_end and std::next(it) != it_end; it += 2)
{
add_codeword(*it, *next(it));
}
}
template < typename Int_Type >
std::pair< Code_Type, Code_Params_Type >
encode(std::vector< Int_Type > const & v, bool encode_diff = false) const
{
Code_Type res;
Code_Params_Type res_params = id();
res_params["code_diff"] = encode_diff? "1" : "0";
std::ostringstream oss;
oss << v.size();
res_params["size"] = oss.str();
uint64_t buff = 0;
uint8_t buff_len = 0;
bool reset = true;
Int_Type last = 0;
unsigned i = 0;
long long int val;
long long int x;
while (true)
{
assert(buff_len <= 64);
// flush buffer
while (buff_len >= 8)
{
res.push_back(buff & 0xFF);
buff >>= 8;
buff_len -= 8;
}
assert(buff_len < 8);
if (reset)
{
assert(buff_len == 0);
if (i == v.size()) break;
//LOG(debug) << "absolute value val=" << v[i] << std::endl;
for (unsigned j = 0; j < sizeof(Int_Type); ++j)
{
std::uint8_t y = (v[i] >> (8 * j)) & 0xFF;
//LOG(debug) << "byte " << j << ": " << std::bitset<8>(y) << std::endl;
res.push_back(y);
}
reset = false;
last = v[i];
++i;
}
else // not reset
{
if (i < v.size())
{
val = v[i];
x = encode_diff? val - last : val;
reset = _cwm.count(x) == 0;
//LOG(debug) << "relative value: val=" << v[i] << " last=" << last << " x=" << x << " reset=" << reset << std::endl;
}
else
{
reset = true;
//LOG(debug) << "end: reset=1" << std::endl;
}
auto p = (not reset? _cwm.at(x) : _cwm.at(break_cw()));
buff |= (p.first << buff_len);
buff_len += p.second;
if (not reset)
{
last = v[i];
++i;
}
else if ((buff_len % 8) > 0) // and reset
{
buff_len += 8 - (buff_len % 8);
}
}
}
oss.str("");
oss << std::fixed << std::setprecision(2) << (double)(res.size() * 8) / v.size();
res_params["avg_bits"] = oss.str();
return std::make_pair(std::move(res), std::move(res_params));
}
template < typename Int_Type >
std::vector< Int_Type >
decode(Code_Type const & v, Code_Params_Type const & v_params) const
{
check_params(v_params);
bool decode_diff = v_params.at("code_diff") == "1";
std::vector< Int_Type > res;
std::uint64_t buff = 0;
std::uint8_t buff_len = 0;
bool reset = true;
Int_Type last = 0;
unsigned i = 0;
while (i < v.size() or buff_len > 0)
{
assert(buff_len <= 64);
// fill buffer
while (i < v.size() and buff_len <= 56)
{
uint64_t y = v[i];
buff |= (y << buff_len);
buff_len += 8;
++i;
}
assert(buff_len <= 64);
if (reset)
{
assert((buff_len % 8) == 0);
assert(buff_len / 8 >= sizeof(Int_Type));
//LOG(debug) << "absolute value" << std::endl;
Int_Type x = 0;
for (unsigned j = 0; j < sizeof(Int_Type); ++j)
{
std::uint64_t y = (buff & 0xFF);
//LOG(debug) << "byte " << j << ": " << std::bitset<8>(y) << std::endl;
x |= (y << (8 * j));
buff >>= 8;
buff_len -= 8;
}
//LOG(debug) << "got: val=" << x << std::endl;
res.push_back(x);
last = x;
reset = false;
}
else // not reset
{
//LOG(debug) << "reading relative value" << std::endl;
// TODO: faster decoding
// currently, try all codewords one by one
auto it = _cwm.begin();
while (it != _cwm.end())
{
if ((buff & ((1llu << it->second.second) - 1)) == it->second.first)
{
break;
}
++it;
}
if (it == _cwm.end())
{
LOG_THROW
<< "codeword not found: buff=" << std::bitset<64>(buff);
}
auto x = it->first;
auto p = it->second;
assert(buff_len >= p.second);
buff >>= p.second;
buff_len -= p.second;
if (x != break_cw())
{
//LOG(debug) << "got: x=" << x << " last=" << last << " val=" << x + last << " cw_len=" << (int)p.second << std::endl;
if (decode_diff) x += last;
if (sizeof(Int_Type) < 8
and (x < (long long)std::numeric_limits< Int_Type >::min()
or x > (long long)std::numeric_limits< Int_Type >::max()))
{
LOG_THROW
<< "overflow";
}
res.push_back(x);
last = x;
}
else
{
//LOG(debug) << "got: break cw_len=" << (int)p.second << std::endl;
reset = true;
if ((buff_len % 8) > 0)
{
buff >>= (buff_len % 8);
buff_len -= (buff_len % 8);
}
}
}
}
return res;
}
//
// static coder access
//
static Huffman_Packer const &
get_coder(std::string const & cwm_name)
{
static_init();
if (cwm_m().count(cwm_name) == 0)
{
LOG_THROW
<< "missing codeword map: " + cwm_name;
}
return cwm_m().at(cwm_name);
}
private:
std::map< long long int, std::pair< std::uint64_t, std::uint8_t > > _cwm;
std::string _cwm_name;
static long long int break_cw()
{
static long long int const _break_cw = std::numeric_limits< long long int >::min();
return _break_cw;
}
Code_Params_Type id() const
{
Code_Params_Type res;
res["packer"] = "huffman_packer";
res["format_version"] = "2";
res["codeword_map_name"] = _cwm_name;
return res;
}
void check_params(Code_Params_Type const & params) const
{
auto _id = id();
if (params.at("packer") != _id.at("packer")
or params.at("format_version") != _id.at("format_version")
or params.at("codeword_map_name") != _id.at("codeword_map_name"))
{
LOG_THROW
<< "decode id mismatch";
}
}
void add_codeword(std::string const & v_s, std::string const & cw_s)
{
long long int v;
if (v_s != ".")
{
std::istringstream(v_s) >> v;
}
else
{
v = break_cw();
}
std::uint64_t cw = 0;
if (cw_s.size() > 57)
{
LOG_THROW
<< "codeword too long: " + v_s + " " + cw_s;
}
std::uint8_t cw_l = cw_s.size();
for (int i = cw_s.size() - 1; i >= 0; --i)
{
cw <<= 1;
cw |= (cw_s[i] == '1');
}
_cwm[v] = std::make_pair(cw, cw_l);
}
static std::map< std::string, Huffman_Packer > & cwm_m()
{
static std::map< std::string, Huffman_Packer > _cwm_m;
return _cwm_m;
}
static void static_init()
{
static bool inited = false;
if (inited) return;
std::deque< std::deque< std::string > > dd;
dd.push_back(
#include "cwmap.fast5_rw_1.inl"
);
dd.push_back(
#include "cwmap.fast5_ed_skip_1.inl"
);
dd.push_back(
#include "cwmap.fast5_ed_len_1.inl"
);
dd.push_back(
#include "cwmap.fast5_fq_bp_1.inl"
);
dd.push_back(
#include "cwmap.fast5_fq_qv_1.inl"
);
dd.push_back(
#include "cwmap.fast5_ev_rel_skip_1.inl"
);
dd.push_back(
#include "cwmap.fast5_ev_move_1.inl"
);
cwm_m().clear();
for (auto & d : dd)
{
auto cwm_name = d.front();
Huffman_Packer hc(d.begin() + 1, d.end(), cwm_name);
cwm_m()[cwm_name] = std::move(hc);
}
inited = true;
} // static_init()
}; // class Huffman_Packer
} // namespace fast5
#endif
|