/usr/include/libmsnumpress/MSNumpress.hpp is in libmsnumpress-dev 1.0.0-2.
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 | /*
MSNumpress.hpp
johan.teleman@immun.lth.se
Copyright 2013 Johan Teleman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
==================== encodeInt ====================
Some of the encodings described below use a integer compression referred to simply as
encodeInt()
This encoding works on a 4 byte integer, by truncating initial zeros or ones.
If the initial (most significant) half byte is 0x0 or 0xf, the number of such
halfbytes starting from the most significant is stored in a halfbyte. This initial
count is then followed by the rest of the ints halfbytes, in little-endian order.
A count halfbyte c of
0 <= c <= 8 is interpreted as an initial c 0x0 halfbytes
9 <= c <= 15 is interpreted as an initial (c-8) 0xf halfbytes
Ex:
int c rest
0 => 0x8
-1 => 0xf 0xf
23 => 0x6 0x7 0x1
*/
#ifndef _MSNUMPRESS_HPP_
#define _MSNUMPRESS_HPP_
#include <cstddef>
#include <vector>
// defines whether to throw an exception when a number cannot be encoded safely
// with the given parameters
#ifndef THROW_ON_OVERFLOW
#define THROW_ON_OVERFLOW true
#endif
namespace ms {
namespace numpress {
namespace MSNumpress {
/**
* Compute the maximal linear fixed point that prevents integer overflow.
*
* @data pointer to array of double to be encoded (need memorycont. repr.)
* @dataSize number of doubles from *data to encode
*
* @return the linear fixed point safe to use
*/
double optimalLinearFixedPoint(
const double *data,
size_t dataSize);
/**
* Compute the optimal linear fixed point with a desired m/z accuracy.
*
* @note If the desired accuracy cannot be reached without overflowing 64
* bit integers, then a negative value is returned. You need to check for
* this and in that case abandon numpress or use optimalLinearFixedPoint
* which returns the largest safe value.
*
* @data pointer to array of double to be encoded (need memorycont. repr.)
* @dataSize number of doubles from *data to encode
* @mass_acc desired m/z accuracy in Th
*
* @return the linear fixed point that satisfies the accuracy requirement (or -1 in case of failure).
*/
double optimalLinearFixedPointMass(
const double *data,
size_t dataSize,
double mass_acc);
/**
* Encodes the doubles in data by first using a
* - lossy conversion to a 4 byte 5 decimal fixed point representation
* - storing the residuals from a linear prediction after first two values
* - encoding by encodeInt (see above)
*
* The resulting binary is maximally 8 + dataSize * 5 bytes, but much less if the
* data is reasonably smooth on the first order.
*
* This encoding is suitable for typical m/z or retention time binary arrays.
* On a test set, the encoding was empirically show to be accurate to at least 0.002 ppm.
*
* @data pointer to array of double to be encoded (need memorycont. repr.)
* @dataSize number of doubles from *data to encode
* @result pointer to where resulting bytes should be stored
* @fixedPoint the scaling factor used for getting the fixed point repr.
* This is stored in the binary and automatically extracted
* on decoding.
* @return the number of encoded bytes
*/
size_t encodeLinear(
const double *data,
const size_t dataSize,
unsigned char *result,
double fixedPoint);
/**
* Calls lower level encodeLinear while handling vector sizes appropriately
*
* @data vector of doubles to be encoded
* @result vector of resulting bytes (will be resized to the number of bytes)
*/
void encodeLinear(
const std::vector<double> &data,
std::vector<unsigned char> &result,
double fixedPoint);
/**
* Decodes data encoded by encodeLinear.
*
* result vector guaranteed to be shorter or equal to (|data| - 8) * 2
*
* Note that this method may throw a const char* if it deems the input data to be corrupt, i.e.
* that the last encoded int does not use the last byte in the data. In addition the last encoded
* int need to use either the last halfbyte, or the second last followed by a 0x0 halfbyte.
*
* @data pointer to array of bytes to be decoded (need memorycont. repr.)
* @dataSize number of bytes from *data to decode
* @result pointer to were resulting doubles should be stored
* @return the number of decoded doubles, or -1 if dataSize < 4 or 4 < dataSize < 8
*/
size_t decodeLinear(
const unsigned char *data,
const size_t dataSize,
double *result);
/**
* Calls lower level decodeLinear while handling vector sizes appropriately
*
* Note that this method may throw a const char* if it deems the input data to be corrupt, i.e..
* that the last encoded int does not use the last byte in the data. In addition the last encoded
* int need to use either the last halfbyte, or the second last followed by a 0x0 halfbyte.
*
* @data vector of bytes to be decoded
* @result vector of resulting double (will be resized to the number of doubles)
*/
void decodeLinear(
const std::vector<unsigned char> &data,
std::vector<double> &result);
/////////////////////////////////////////////////////////////
/**
* Encodes the doubles in data by storing the residuals from a linear prediction after first two values.
*
* The resulting binary is the same size as the input data.
*
* This encoding is suitable for typical m/z or retention time binary arrays, and is
* intended to be used before zlib compression to improve compression.
*
* @data pointer to array of doubles to be encoded (need memorycont. repr.)
* @dataSize number of doubles from *data to encode
* @result pointer to were resulting bytes should be stored
*/
size_t encodeSafe(
const double *data,
const size_t dataSize,
unsigned char *result);
/**
* Decodes data encoded by encodeSafe.
*
* result vector is the same size as the input data.
*
* Might throw const char* is something goes wrong during decoding.
*
* @data pointer to array of bytes to be decoded (need memorycont. repr.)
* @dataSize number of bytes from *data to decode
* @result pointer to were resulting doubles should be stored
* @return the number of decoded bytes
*/
size_t decodeSafe(
const unsigned char *data,
const size_t dataSize,
double *result);
/////////////////////////////////////////////////////////////
/**
* Encodes ion counts by simply rounding to the nearest 4 byte integer,
* and compressing each integer with encodeInt.
*
* The handleable range is therefore 0 -> 4294967294.
* The resulting binary is maximally dataSize * 5 bytes, but much less if the
* data is close to 0 on average.
*
* @data pointer to array of double to be encoded (need memorycont. repr.)
* @dataSize number of doubles from *data to encode
* @result pointer to were resulting bytes should be stored
* @return the number of encoded bytes
*/
size_t encodePic(
const double *data,
const size_t dataSize,
unsigned char *result);
/**
* Calls lower level encodePic while handling vector sizes appropriately
*
* @data vector of doubles to be encoded
* @result vector of resulting bytes (will be resized to the number of bytes)
*/
void encodePic(
const std::vector<double> &data,
std::vector<unsigned char> &result);
/**
* Decodes data encoded by encodePic
*
* result vector guaranteed to be shorter of equal to |data| * 2
*
* Note that this method may throw a const char* if it deems the input data to be corrupt, i.e.
* that the last encoded int does not use the last byte in the data. In addition the last encoded
* int need to use either the last halfbyte, or the second last followed by a 0x0 halfbyte.
*
* @data pointer to array of bytes to be decoded (need memorycont. repr.)
* @dataSize number of bytes from *data to decode
* @result pointer to were resulting doubles should be stored
* @return the number of decoded doubles
*/
size_t decodePic(
const unsigned char *data,
const size_t dataSize,
double *result);
/**
* Calls lower level decodePic while handling vector sizes appropriately
*
* Note that this method may throw a const char* if it deems the input data to be corrupt, i.e.
* that the last encoded int does not use the last byte in the data. In addition the last encoded
* int need to use either the last halfbyte, or the second last followed by a 0x0 halfbyte.
*
* @data vector of bytes to be decoded
* @result vector of resulting double (will be resized to the number of doubles)
*/
void decodePic(
const std::vector<unsigned char> &data,
std::vector<double> &result);
/////////////////////////////////////////////////////////////
double optimalSlofFixedPoint(
const double *data,
size_t dataSize);
/**
* Encodes ion counts by taking the natural logarithm, and storing a
* fixed point representation of this. This is calculated as
*
* unsigned short fp = log(d + 1) * fixedPoint + 0.5
*
* the result vector is exactly |data| * 2 + 8 bytes long
*
* @data pointer to array of double to be encoded (need memorycont. repr.)
* @dataSize number of doubles from *data to encode
* @result pointer to were resulting bytes should be stored
* @return the number of encoded bytes
*/
size_t encodeSlof(
const double *data,
const size_t dataSize,
unsigned char *result,
double fixedPoint);
/**
* Calls lower level encodeSlof while handling vector sizes appropriately
*
* @data vector of doubles to be encoded
* @result vector of resulting bytes (will be resized to the number of bytes)
*/
void encodeSlof(
const std::vector<double> &data,
std::vector<unsigned char> &result,
double fixedPoint);
/**
* Decodes data encoded by encodeSlof
*
* The return will include exactly (|data| - 8) / 2 doubles.
*
* Note that this method may throw a const char* if it deems the input data to be corrupt.
*
* @data pointer to array of bytes to be decoded (need memorycont. repr.)
* @dataSize number of bytes from *data to decode
* @result pointer to were resulting doubles should be stored
* @return the number of decoded doubles
*/
size_t decodeSlof(
const unsigned char *data,
const size_t dataSize,
double *result);
/**
* Calls lower level decodeSlof while handling vector sizes appropriately
*
* Note that this method may throw a const char* if it deems the input data to be corrupt.
*
* @data vector of bytes to be decoded
* @result vector of resulting double (will be resized to the number of doubles)
*/
void decodeSlof(
const std::vector<unsigned char> &data,
std::vector<double> &result);
} // namespace MSNumpress
} // namespace msdata
} // namespace pwiz
#endif // _MSNUMPRESS_HPP_
|