Debian Codegraph @

src/utils/utils.h

raw
1 //FastQTL: Fast and efficient QTL mapper for molecular phenotypes
2 //Copyright (C) 2015 Olivier DELANEAU, Alfonso BUIL, Emmanouil DERMITZAKIS & Olivier DELANEAU
3 //
4 //This program is free software: you can redistribute it and/or modify
5 //it under the terms of the GNU General Public License as published by
6 //the Free Software Foundation, either version 3 of the License, or
7 //(at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 //GNU General Public License for more details.
13 //
14 //You should have received a copy of the GNU General Public License
15 //along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef _UTILS_H
18 #define _UTILS_H
19
20 #define IA 16807
21 #define IM 2147483647
22 #define AM (1.0/IM)
23 #define IQ 127773
24 #define IR 2836
25 #define NTAB 32
26 #define NDIV (1+(IM-1)/NTAB)
27 #define EPS 1.2e-7
28 #define RNMX (1.0-EPS)
29 #define PI 3.14159265358979323846
30
31 #define LOW_POS_DOUBLE 1e-300
32 #define BIG_POS_DOUBLE 1e300
33 #define LOW_NEG_DOUBLE -1e-300
34 #define BIG_NEG_DOUBLE -1e300
35 #define LOW_POS_FLOAT 1e-8
36 #define BIG_POS_FLOAT 1e8
37 #define LOW_NEG_FLOAT -1e-8
38 #define BIG_NEG_FLOAT -1e8
39 #define BIG_POS_INT 1000000000
40 #define BIG_NEG_INT -1000000000
41
42 #define ___NA___ (0.0/0.0)
43
44 #include <string>
45 #include <complex>
46 #include <vector>
47 #include <queue>
48 #include <map>
49 #include <numeric>
50 #include <bitset>
51 #include <list>
52 #include <bitset>
53 #include <cmath>
54 #include <algorithm>
55 #include <iostream>
56 #include <sstream>
57 #include <fstream>
58 #include <iomanip>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <assert.h>
62 #include <time.h>
63 #include <sys/time.h>
64 #include <pthread.h>
65 #include <exception>
66 #include <boost/algorithm/string.hpp>
67 #include <boost/program_options.hpp>
68 #include <boost/iostreams/filtering_stream.hpp>
69 #include <boost/iostreams/filter/gzip.hpp>
70 #include <boost/iostreams/filter/bzip2.hpp>
71 #include <boost/uuid/uuid.hpp>
72 #include <boost/uuid/uuid_generators.hpp>
73 #include <boost/uuid/uuid_io.hpp>
74 #include <boost/lexical_cast.hpp>
75
76
77 using namespace std;
78 namespace bio = boost::iostreams;
79 namespace bpo = boost::program_options;
80 namespace bid = boost::uuids;
81
82
83 /******************************************************/
84 /* UTILS RUNNING STATS */
85 /******************************************************/
86
87 class RunningStat {
88 public:
89 RunningStat() : m_n(0) {}
90
91 RunningStat(vector < double > & X): m_n(0) {
92 for (unsigned int e = 0 ; e < X.size() ; e ++) Push(X[e]);
93 }
94
95 RunningStat(vector < float > & X): m_n(0) {
96 for (unsigned int e = 0 ; e < X.size() ; e ++) Push((double)X[e]);
97 }
98
99 void Clear() { m_n = 0; }
100
101 void Push(double x) {
102 m_n++;
103
104 if (m_n == 1) {
105 m_oldM = m_newM = x;
106 m_oldS = 0.0;
107 } else {
108 m_newM = m_oldM + (x - m_oldM)/m_n;
109 m_newS = m_oldS + (x - m_oldM)*(x - m_newM);
110 m_oldM = m_newM;
111 m_oldS = m_newS;
112 }
113 }
114
115 int NumDataValues() const {
116 return m_n;
117 }
118
119 double Mean() const {
120 return (m_n > 0) ? m_newM : 0.0;
121 }
122
123 double Variance() const {
124 return ( (m_n > 1) ? m_newS/(m_n - 1) : 0.0 );
125 }
126
127 double StandardDeviation() const {
128 return sqrt( Variance() );
129 }
130
131 void MeanStandardDeviation (double & mean, double & sd) {
132 mean = Mean();
133 sd = StandardDeviation();
134 }
135
136 void MeanVariance (double & mean, double & variance) {
137 mean = Mean();
138 variance = Variance();
139 }
140
141 private:
142 int m_n;
143 double m_oldM, m_newM, m_oldS, m_newS;
144 };
145
146
147 /******************************************************/
148 /* UTILS STATISTICS */
149 /******************************************************/
150 namespace putils {
151 double mean(vector < double > &);
152 double variance(vector < double > &, double );
153 double mean(vector < float > &);
154 double variance(vector < float > &, double );
155 bool isVariable(vector < float > &);
156 bool isVariable(vector < double > &);
157 void initRandom(long s);
158 double getRandom();
159 string getRandomID();
160 int getRandom(int);
161 void bootstrap(int, vector < int > &);
162 long getSeed();
163 void normalise(vector < double > & v);
164 void random_shuffle(vector < vector < float > > &);
165 int sample(vector< double > & v, double sum);
166 double entropy(vector < double > & v);
167 double KLdistance(vector < double > & P, vector < double > & Q);
168 double qnorm(double p, double mu, double sigma, int lower_tail, int log_p);
169 };
170
171 /******************************************************/
172 /* UTILS ALGORITHM */
173 /******************************************************/
174 namespace autils {
175 int max(vector < double > & v);
176 int max(vector < int > & v);
177 void findUniqueSet(vector < bool > & B, vector < int > & U);
178 void reorder(vector < float > &, vector < unsigned int > const & ) ;
179 void decompose(int min, vector < vector < int > > & B, vector < vector < vector < int > > > & BB);
180 int checkDuo (int pa1, int pa2, int ca1, int ca2);
181 int checkTrio (int fa1, int fa2, int ma1, int ma2, int ca1, int ca2);
182 };
183
184 /******************************************************/
185 /* UTILS STRING */
186 /******************************************************/
187 namespace sutils {
188 int tokenize(string &, vector < string > &);
189 int tokenize(string &, vector < string > &, int);
190 int tokenize(string &, vector < string > &, string);
191 string int2str(int n);
192 string int2str(vector < int > & v);
193 string long2str(long int n);
194 string double2str(double n);
195 string double2str(double n, int prc);
196 string double2str(vector < double > &v);
197 string double2str(vector < double > &v, int prc);
198 string double2str(vector < float > &v);
199 string double2str(vector < float > &v, int prc);
200 string bool2str(vector<bool> & v);
201 string date2str(time_t * t, string format);
202 bool isNumeric(string &);
203 string remove_spaces(const string &);
204 };
205
206 /******************************************************/
207 /* UTILS FILE */
208 /******************************************************/
209 namespace futils {
210 bool isFile(string f);
211 bool createFile(string f);
212 void checkFile(string f, bool);
213 string extensionFile(string & filename);
214 void bool2binary(vector < bool > & V, ostream &fd);
215 bool binary2bool(vector < bool > & V, istream & fd);
216 };
217
218
219 /******************************************************/
220 /* EXCEPTIONS */
221 /******************************************************/
222 class myException : public exception {
223 public:
224 explicit myException(std::string msg) : msg_(msg) {}
225
226 virtual ~myException() throw() {}
227
228 virtual const char* what() const throw() {
229 return msg_.c_str();
230 }
231
232 private:
233 std::string msg_;
234 };
235
236 /******************************************************/
237 /* INPUT FILE */
238 /******************************************************/
239 class ifile : public bio::filtering_istream {
240 private:
241 string file;
242 ifstream fd;
243
244 public:
245 ifile();
246 ifile(string filename , bool binary = false);
247 ~ifile();
248 string name();
249 bool open(string filename, bool binary = false);
250 bool readString(string &);
251 void close();
252 };
253
254 /******************************************************/
255 /* OUTPUT FILE */
256 /******************************************************/
257 class ofile : public bio::filtering_ostream {
258 private:
259 string file;
260 ofstream fd;
261
262 public:
263 ofile();
264 ofile(string filename , bool binary = false);
265 ~ofile();
266 string name();
267 bool open(string filename, bool binary = false);
268 void writeString(string &);
269 void close();
270 };
271
272 /******************************************************/
273 /* LOG FILE */
274 /******************************************************/
275 class lfile {
276 private:
277 string file;
278 ofstream fd;
279 bool verboseC;
280 bool verboseL;
281
282 public:
283 lfile();
284 ~lfile();
285 string name();
286 bool open(string filename = "file.log");
287 void close();
288 string getPrefix();
289 void muteL();
290 void unmuteL();
291 void muteC();
292 void unmuteC();
293 void print(string s);
294 void printC(string s);
295 void printL(string s);
296 void println(string s);
297 void printlnC(string s);
298 void printlnL(string s);
299 void warning(string s);
300 void error(string s);
301 };
302
303 #ifdef _GLOBAL
304 #define _EXTERN
305 #else
306 #define _EXTERN extern
307 #endif
308
309 _EXTERN lfile LOG;
310 _EXTERN time_t START_DATE;
311
312 #endif