-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.hpp
408 lines (353 loc) · 10.5 KB
/
utility.hpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
*
* reHC-*
* Haplotyping with Recombinations, Errors, and Missing Genotypes
*
* Copyright (C) 2010,2011 Yuri Pirola <yuri.pirola(-at-)gmail.com>
*
* Distributed under the terms of the GNU General Public License (GPL)
*
*
* This file is part of reHC-* (reHCstar),
* previously known as ZRHC-* (ZRHCstar).
*
* reHC-* is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* reHC-* is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with reHC-*. If not, see <http://www.gnu.org/licenses/>.
*
**/
/**
*
* utility.hpp
*
* Various utility functions and classes.
*
**/
#ifndef __UTILITY_HPP__
#define __UTILITY_HPP__
#include <functional>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/ptr_container/ptr_list.hpp>
#include <boost/version.hpp>
#include "assertion.hpp"
#ifndef EXIT_NO_reHC
#define EXIT_NO_reHC (2)
#endif
#ifndef EXIT_reHC_ERROR
#define EXIT_reHC_ERROR (4)
#endif
template <typename T>
std::string tostr(const std::vector<T>& x) {
std::ostringstream out;
bool first= true;
for (typename std::vector<T>::const_iterator it= x.begin();
it != x.end();
++it) {
if (!first) {
out << " ";
}
out << *it;
first= false;
}
return out.str();
};
template <typename T>
std::string tostr(const T& x) {
std::ostringstream ostr;
ostr << x;
return ostr.str();
};
template <class T>
class deleter_t
: public std::unary_function<T*, void> {
public:
void operator()(T* pt) {
delete pt;
}
};
class generic_raise_error_class_t
:public log_able_t<generic_raise_error_class_t>
{
public:
void raise_error(const std::string& reason="no detail specified") {
L_FATAL("Generic error. Reason: " << reason << ". Exiting...");
MY_FAIL;
};
};
template <int i, int default_index, class derived_enum, class raise_error_class_t>
class static_str_switch {
public:
static inline derived_enum EXEC(const std::string& s, raise_error_class_t& err) {
if (s == derived_enum::str_values[i]) {
return derived_enum::enum_values[i];
} else {
return static_str_switch< i-1, default_index, derived_enum, raise_error_class_t>::EXEC(s, err);
}
}
};
template <int default_index, class derived_enum, class raise_error_class_t>
class static_str_switch<0, default_index, derived_enum, raise_error_class_t> {
public:
static inline derived_enum EXEC(const std::string& s, raise_error_class_t& err) {
if (s == derived_enum::str_values[0]) {
return derived_enum::enum_values[0];
} else {
err.raise_error("string >" + s + "< not recognized");
return derived_enum::enum_values[default_index];
}
}
};
template <int i, class derived_enum>
class static_int_switch {
public:
static inline int EXEC(const int v) {
if (v == derived_enum::int_values[i]) {
return i;
} else {
return static_int_switch< i-1, derived_enum>::EXEC(v);
}
}
};
template <class derived_enum>
class static_int_switch<0, derived_enum> {
public:
static inline int EXEC(const int v) {
if (v == derived_enum::int_values[0]) {
return 0;
} else {
MY_FAIL;
return -1;
}
}
};
template <class derived_enum, size_t n_values, int default_index>
class enum_like_t {
private:
int _d;
protected:
class raise_input_error_t {
private:
std::istream& in;
public:
raise_input_error_t(std::istream& _in)
:in(_in)
{};
void raise_error(const std::string& reason="no deatail provided") {
in.setstate(std::ios::failbit);
};
};
enum_like_t(const size_t d)
:_d(d)
{
MY_ASSERT_DBG((0 <= d) && (d<n_values));
}
size_t get_ordinal_data() const {
return _d;
}
public:
enum_like_t(const derived_enum& copy)
: _d(copy.get_ordinal_data())
{
MY_ASSERT_DBG( copy.get_ordinal_data() <n_values );
};
const derived_enum&
operator=(const derived_enum& copy) {
_d= copy.get_ordinal_data();
return *this;
};
const static size_t N_VALUES= n_values;
friend bool operator==(const derived_enum& e1, const derived_enum& e2) {
return e1.get_ordinal_data() == e2.get_ordinal_data();
}
friend bool operator!=(const derived_enum& e1, const derived_enum& e2) {
return e1.get_ordinal_data() != e2.get_ordinal_data();
}
friend bool operator<=(const derived_enum& e1, const derived_enum& e2) {
return e1.get_ordinal_data() <= e2.get_ordinal_data();
}
friend bool operator<(const derived_enum& e1, const derived_enum& e2) {
return e1.get_ordinal_data() < e2.get_ordinal_data();
}
friend std::ostream& operator<<(std::ostream& out, const derived_enum& val) {
return (out << derived_enum::str_values[val.get_ordinal_data()]);
}
friend std::istream& operator>>(std::istream& in, derived_enum& val) {
std::string h;
typename derived_enum::raise_input_error_t err(in);
if (in >> h) {
val= static_str_switch< n_values-1, default_index, derived_enum, raise_input_error_t>::EXEC(h, err);
} else {
err.raise_error("generic error");
val= derived_enum::enum_values[default_index];
}
return in;
}
};
class file_utility:
public log_able_t<file_utility> {
private:
file_utility() {};
public:
typedef boost::shared_ptr<std::istream> pistream;
typedef boost::shared_ptr<std::ostream> postream;
static const file_utility&
get_file_utility() {
static const file_utility fu;
return fu;
};
pistream get_ifstream(const std::string& file_name,
const bool compressed=false) const {
L_DEBUG("Opening file '" << file_name << "' for reading...");
boost::iostreams::file_source source(file_name);
if (!source.is_open()) {
L_ERROR("Impossible to open file '" << file_name << "' for reading.");
throw std::logic_error(std::string("Impossible to open file '")
+ file_name + "' for reading.");
}
L_DEBUG("File '" << file_name << "' successfully opened.");
std::istream* is;
if (compressed) {
L_DEBUG("...setting file '" << file_name << "' as gzip-ed.");
boost::iostreams::filtering_istream* gis= new boost::iostreams::filtering_istream;
gis->push(boost::iostreams::gzip_decompressor());
gis->push(source);
is= gis;
} else {
is= new boost::iostreams::stream<boost::iostreams::file_source>(source);
}
return pistream(is);
};
postream get_ofstream(const std::string& file_name,
const bool compressed=false) const {
L_DEBUG("Opening file '" << file_name << "' for writing...");
boost::iostreams::file_sink sink(file_name);
if (!sink.is_open()) {
L_ERROR("Impossible to open file '" << file_name << "' for writing.");
throw std::logic_error(std::string("Impossible to open file '")
+ file_name + "' for writing.");
}
L_DEBUG("File '" << file_name << "' successfully opened.");
std::ostream* os;
if (compressed) {
L_DEBUG("...setting file '" << file_name << "' as gzip-ed.");
boost::iostreams::filtering_ostream* gos= new boost::iostreams::filtering_ostream;
gos->push(boost::iostreams::gzip_compressor());
gos->push(sink);
os= gos;
} else {
os= new boost::iostreams::stream<boost::iostreams::file_sink>(sink);
}
return postream(os);
};
postream get_tmp_ostream(const std::string file_template,
std::string& real_name,
const bool compressed=false) const {
L_DEBUG("Opening a temporary file with hint '" << file_template <<
"' for writing...");
MY_ASSERT(boost::ends_with(file_template, "XXXXXX"));
const size_t name_len= strlen(file_template.c_str());
char* name= (char*)calloc(name_len+1, sizeof(char));
strncpy(name, file_template.c_str(), name_len+1);
const int fd= mkstemp(name);
real_name= std::string(name);
free(name);
if (fd == -1) {
L_ERROR("Impossible to open a temporary file with hint '" << file_template
<< "' for writing.");
throw std::logic_error(std::string("Impossible to open a temporary file with hint '")
+ file_template + "' for writing.");
}
#if (BOOST_VERSION >= 104400)
boost::iostreams::file_descriptor_sink sink(fd, boost::iostreams::close_handle);
#else
boost::iostreams::file_descriptor_sink sink(fd);
#endif
if (!sink.is_open()) {
L_ERROR("Impossible to open file '" << real_name << "' for writing.");
throw std::logic_error(std::string("Impossible to open file '")
+ real_name + "' for writing.");
}
L_INFO("File '" << real_name << "' successfully opened.");
std::ostream* os;
if (compressed) {
L_DEBUG("...setting file '" << real_name << "' as gzip-ed.");
boost::iostreams::filtering_ostream* gos= new boost::iostreams::filtering_ostream;
gos->push(boost::iostreams::gzip_compressor());
gos->push(sink);
os= gos;
} else {
os= new boost::iostreams::stream<boost::iostreams::file_descriptor_sink>(sink);
}
postream pos(os);
return pos;
};
postream get_ostream_from_file(FILE* file) const {
MY_ASSERT(file != NULL);
const int fd = fileno(file);
if (fd == -1) {
L_ERROR("Impossible to open a stream associated to the given C-file.");
throw std::logic_error(std::string("Impossible to open a stream associated to the given C-file."));
}
#if (BOOST_VERSION >= 104400)
boost::iostreams::file_descriptor_sink sink(fd, boost::iostreams::close_handle);
#else
boost::iostreams::file_descriptor_sink sink(fd);
#endif
if (!sink.is_open()) {
L_ERROR("Impossible to open the given C-file.");
throw std::logic_error(std::string("Impossible to open the given C-file."));
}
std::ostream* os= new boost::iostreams::stream<boost::iostreams::file_descriptor_sink>(sink);
postream pos(os);
return pos;
};
};
class percent_t {
public:
const double val;
const double tot;
percent_t(const double val_,
const double tot_)
:val(val_), tot(tot_)
{};
};
std::ostream&
operator<<(std::ostream& out, const percent_t& val);
inline static size_t
pow2_of_floor_log2(const size_t n) {
size_t p= 1;
while ((p << 1) <= n) {
p= p << 1;
}
return p;
};
inline static size_t
pow2_of_ceiling_log2(const size_t n) {
size_t p= 1;
while (p < n) {
p= p << 1;
}
return p;
};
#endif // __UTILITY_HPP__