Debian Codegraph @

src/utils/utils.cpp

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 #include "utils.h"
18
19 long seed = -123456789;
20 pthread_mutex_t mutex_rng;
21
22 namespace putils {
23 void initRandom(long s) {
24 seed = - s;
25 pthread_mutex_init(&mutex_rng, NULL);
26 }
27
28 double mean(vector < double > & X) {
29 double mean = 0.0;
30 for (int x = 0 ; x < X.size() ; x ++) mean += X[x];
31 mean /= X.size();
32 return mean;
33 }
34
35 double variance(vector < double > & X, double mean) {
36 double variance = 0.0;
37 for (int x = 0 ; x < X.size() ; x++) variance += (X[x] - mean) * (X[x] - mean);
38 variance /= (X.size() - 1);
39 return variance;
40 }
41
42 double mean(vector < float > & X) {
43 double mean = 0.0;
44 for (int x = 0 ; x < X.size() ; x ++) mean += X[x];
45 mean /= X.size();
46 return mean;
47 }
48
49 double variance(vector < float > & X, double mean) {
50 double variance = 0.0;
51 for (int x = 0 ; x < X.size() ; x++) variance += (X[x] - mean) * (X[x] - mean);
52 variance /= (X.size() - 1);
53 return variance;
54 }
55
56 bool isVariable(vector < float > & v) {
57 for (int i = 1 ; i < v.size(); i++) if (v[i] != v[i-1]) return true;
58 return false;
59 }
60
61 bool isVariable(vector < double > & v) {
62 for (int i = 1 ; i < v.size(); i++) if (v[i] != v[i-1]) return true;
63 return false;
64 }
65
66 double getRandom() {
67 pthread_mutex_lock(&mutex_rng);
68 int j;
69 long k;
70 static long iy=0;
71 static long iv[NTAB];
72 double temp;
73 if (seed <= 0 || !iy) {
74 if (-(seed) < 1) seed=1;
75 else seed = -(seed);
76 for (j=NTAB+7;j>=0;j--) {
77 k=(seed)/IQ;
78 seed=IA*(seed-k*IQ)-IR*k;
79 if (seed < 0) seed += IM;
80 if (j < NTAB) iv[j] = seed;
81 }
82 iy=iv[0];
83 }
84 k=(seed)/IQ;
85 seed=IA*(seed-k*IQ)-IR*k;
86 if (seed < 0) seed += IM;
87 j=iy/NDIV;
88 iy=iv[j];
89 iv[j] = seed;
90 temp=AM*iy;
91 pthread_mutex_unlock(&mutex_rng);
92 if (temp > RNMX) return RNMX;
93 else return temp;
94 }
95
96 string getRandomID(){
97 return boost::lexical_cast<string>(boost::uuids::random_generator()());
98 }
99
100 long getSeed() {
101 return seed;
102 }
103
104 int getRandom(int n) {
105 return (int)floor(getRandom() * n);
106 }
107
108 void bootstrap(int N, vector < int > & sample) {
109 sample = vector < int > (N, 0);
110 for (int e = 0 ; e < N ; e ++) sample[e] = getRandom(N);
111 }
112
113 void normalise(vector < double > & v) {
114 double sum = 0.0;
115 for (int i = 0 ; i < v.size() ; i++) sum += v[i];
116 if (sum != 0.0) for (int i = 0 ; i < v.size() ; i++) v[i] /= sum;
117 }
118
119 int sample(vector< double > & v, double sum) {
120 double csum = v[0];
121 double u = getRandom() * sum;
122 for (int i = 0; i < v.size() - 1; ++i) {
123 if ( u < csum ) return i;
124 csum += v[i+1];
125 }
126 return v.size() - 1;
127 }
128
129 void random_shuffle(vector < vector < float > > & X) {
130 int n = X[0].size();
131 vector < int > O;
132 for (int i = 0; i < n; i++) O.push_back(i);
133 random_shuffle(O.begin(), O.end());
134 vector < vector < float > > Xtmp = X;
135 for (int c = 0 ; c < X.size() ; c++) for (int e = 0 ; e < n ; e++) X[c][e] = Xtmp[c][O[e]];
136 }
137
138 double entropy(vector < double > & v) {
139 double e = 0.0;
140 for (int i = 0 ; i < v.size() ; i ++) {
141 if (v[i] > 0.0) e += v[i] * log(v[i]);
142 }
143 return e;
144 }
145
146 double KLdistance(vector < double > & P, vector < double > & Q) {
147 assert(P.size() == Q.size());
148 double d = 0.0;
149 for (int i = 0 ; i < Q.size() ; i ++) {
150 if (Q[i] > 0.0 && P[i] > 0.0) d += P[i] * (log(P[i]) - log(Q[i]));
151 }
152 return d;
153 }
154
155 double qnorm(double p, double mu, double sigma, int lower_tail, int log_p) {
156 double q, r, val;
157 q = p - 0.5;
158 if (fabs(q) <= .425) {
159 r = .180625 - q * q;
160 val = q * (((((((r * 2509.0809287301226727 + 33430.575583588128105) * r + 67265.770927008700853) * r + 45921.953931549871457) * r + 13731.693765509461125) * r + 1971.5909503065514427) * r + 133.14166789178437745) * r + 3.387132872796366608) / (((((((r * 5226.495278852854561 + 28729.085735721942674) * r + 39307.89580009271061) * r + 21213.794301586595867) * r + 5394.1960214247511077) * r + 687.1870074920579083) * r + 42.313330701600911252) * r + 1.);
161 } else { /* closer than 0.075 from {0,1} boundary */
162 if (q > 0) r = 1 - p;
163 else r = p;
164
165 r = sqrt(- ((log_p && ((lower_tail && q <= 0) || (!lower_tail && q > 0))) ? p : log(r)));
166
167 if (r <= 5.) { /* <==> min(p,1-p) >= exp(-25) ~= 1.3888e-11 */
168 r += -1.6;
169 val = (((((((r * 7.7454501427834140764e-4 + .0227238449892691845833) * r + .24178072517745061177) * r + 1.27045825245236838258) * r + 3.64784832476320460504) * r + 5.7694972214606914055) * r + 4.6303378461565452959) * r + 1.42343711074968357734) / (((((((r * 1.05075007164441684324e-9 + 5.475938084995344946e-4) * r + .0151986665636164571966) * r + .14810397642748007459) * r + .68976733498510000455) * r + 1.6763848301838038494) * r + 2.05319162663775882187) * r + 1.);
170 } else { /* very close to 0 or 1 */
171 r += -5.;
172 val = (((((((r * 2.01033439929228813265e-7 + 2.71155556874348757815e-5) * r + .0012426609473880784386) * r + .026532189526576123093) * r + .29656057182850489123) * r + 1.7848265399172913358) * r + 5.4637849111641143699) * r + 6.6579046435011037772) / (((((((r * 2.04426310338993978564e-15 + 1.4215117583164458887e-7)* r + 1.8463183175100546818e-5) * r + 7.868691311456132591e-4) * r + .0148753612908506148525) * r + .13692988092273580531) * r + .59983220655588793769) * r + 1.);
173 }
174 if (q < 0.0) val = -val;
175 }
176 return mu + sigma * val;
177 }
178 };
179
180 /******************************************************/
181 /* UTILS ALGORITHM */
182 /******************************************************/
183 namespace autils {
184 int max(vector < double > & v) {
185 double max = -1e300;
186 int index_max = 0;
187 for (int i = 0 ; i < v.size() ; i ++)
188 if (v[i] > max) {
189 max = v[i];
190 index_max = i;
191 }
192 return index_max;
193 }
194
195 int max(vector < int > & v) {
196 int max = -1000000000;
197 int index_max = 0;
198 for (int i = 0 ; i < v.size() ; i ++)
199 if (v[i] > max) {
200 max = v[i];
201 index_max = i;
202 }
203 return index_max;
204 }
205
206 void reorder(vector < float > & v, vector < unsigned int > const & order ) {
207 vector < float > vtmp = v;
208 for (int e = 0 ; e < order.size() ; e ++) v[e] = vtmp[order[e]];
209 }
210
211 void findUniqueSet(vector < bool > & B, vector < int > & U) {
212 U.clear();
213 for (int b = 0 ; b < B.size() ; b ++)
214 if (B[b]) U.push_back(b);
215 //sort( U.begin(), U.end() );
216 //U.erase(unique( U.begin(), U.end() ), U.end());
217 }
218
219 void decompose(int min, vector < vector < int > > & B, vector < vector < vector < int > > > & BB) {
220 if (B.size() < 2 * min || B.size() == 2) {
221 BB.push_back(B);
222 return;
223 }
224
225 int l = putils::getRandom(B.size() - 2 * min) + min;
226 vector < vector < int > > LB = vector < vector < int > > (B.begin(), B.begin() + l + 1);
227 vector < vector < int > > RB = vector < vector < int > > (B.begin() + l - 1, B.end());
228 vector < vector < vector < int > > > LBB;
229 vector < vector < vector < int > > > RBB;
230 decompose(min, LB, LBB);
231 decompose(min, RB, RBB);
232 BB = LBB;
233 BB.insert(BB.end(), RBB.begin(), RBB.end());
234 }
235
236 int checkDuo (int pa1, int pa2, int ca1, int ca2) {
237 if (pa1 == 0 && pa2 == 0 && ca1 == 0 && ca2 == 0) { return 0; }
238 if (pa1 == 0 && pa2 == 0 && ca1 == 0 && ca2 == 1) { return 0; }
239 if (pa1 == 0 && pa2 == 0 && ca1 == 1 && ca2 == 0) { return 0; }
240 if (pa1 == 0 && pa2 == 0 && ca1 == 1 && ca2 == 1) { return -1; }
241 if (pa1 == 0 && pa2 == 1 && ca1 == 0 && ca2 == 0) { return 1; }
242 if (pa1 == 0 && pa2 == 1 && ca1 == 0 && ca2 == 1) { return 0; }
243 if (pa1 == 0 && pa2 == 1 && ca1 == 1 && ca2 == 0) { return 0; }
244 if (pa1 == 0 && pa2 == 1 && ca1 == 1 && ca2 == 1) { return 1; }
245 if (pa1 == 1 && pa2 == 0 && ca1 == 0 && ca2 == 0) { return 1; }
246 if (pa1 == 1 && pa2 == 0 && ca1 == 0 && ca2 == 1) { return 0; }
247 if (pa1 == 1 && pa2 == 0 && ca1 == 1 && ca2 == 0) { return 0; }
248 if (pa1 == 1 && pa2 == 0 && ca1 == 1 && ca2 == 1) { return 1; }
249 if (pa1 == 1 && pa2 == 1 && ca1 == 0 && ca2 == 0) { return -1; }
250 if (pa1 == 1 && pa2 == 1 && ca1 == 0 && ca2 == 1) { return 0; }
251 if (pa1 == 1 && pa2 == 1 && ca1 == 1 && ca2 == 0) { return 0; }
252 if (pa1 == 1 && pa2 == 1 && ca1 == 1 && ca2 == 1) { return 0; }
253 return 0;
254 }
255
256 int checkTrio (int fa1, int fa2, int ma1, int ma2, int ca1, int ca2) {
257 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return 0; }
258 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return -1; }
259 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return -1; }
260 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return -1; }
261 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return 1; }
262 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 1; }
263 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 1; }
264 if (fa1 == 0 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return -1; }
265 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return 1; }
266 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 1; }
267 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 1; }
268 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return -1; }
269 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return -1; }
270 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 0; }
271 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 0; }
272 if (fa1 == 0 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return -1; }
273 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return 1; }
274 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 1; }
275 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 1; }
276 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return -1; }
277 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return 2; }
278 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 0; }
279 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 0; }
280 if (fa1 == 0 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return 2; }
281 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return 2; }
282 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 0; }
283 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 0; }
284 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return 2; }
285 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return -1; }
286 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 1; }
287 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 1; }
288 if (fa1 == 0 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return 1; }
289 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return 1; }
290 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 1; }
291 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 1; }
292 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return -1; }
293 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return 2; }
294 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 0; }
295 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 0; }
296 if (fa1 == 1 && fa2 == 0 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return 2; }
297 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return 2; }
298 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 0; }
299 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 0; }
300 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return 2; }
301 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return -1; }
302 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 1; }
303 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 1; }
304 if (fa1 == 1 && fa2 == 0 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return 1; }
305 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return -1; }
306 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 0; }
307 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 0; }
308 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return -1; }
309 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return -1; }
310 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return 1; }
311 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return 1; }
312 if (fa1 == 1 && fa2 == 1 && ma1 == 0 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return 1; }
313 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 0 ) { return -1; }
314 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 0 && ca2 == 1 ) { return 1; }
315 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 0 ) { return 1; }
316 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 0 && ca1 == 1 && ca2 == 1 ) { return 1; }
317 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 0 ) { return -1; }
318 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 0 && ca2 == 1 ) { return -1; }
319 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 0 ) { return -1; }
320 if (fa1 == 1 && fa2 == 1 && ma1 == 1 && ma2 == 1 && ca1 == 1 && ca2 == 1 ) { return 0; }
321 return 0;
322 }
323 };
324
325 /******************************************************/
326 /* UTILS STRING */
327 /******************************************************/
328 namespace sutils {
329 int tokenize (string & str, vector < string > & tokens) {
330 tokens.clear();
331 string::size_type p_last = str.find_first_not_of(" ", 0);
332 string::size_type p_curr = str.find_first_of(" ", p_last);
333 while (string::npos != p_curr || string::npos != p_last) {
334 tokens.push_back(str.substr(p_last, p_curr - p_last));
335 p_last = str.find_first_not_of(" ", p_curr);
336 p_curr = str.find_first_of(" ", p_last);
337 }
338 if (tokens.back()[tokens.back().size()-1] == '\r')
339 tokens.back() = tokens.back().substr(0, tokens.back().size()-1);
340 return tokens.size();
341 }
342
343 int tokenize(string & str, vector < string > & tokens, int n_max_tokens) {
344 tokens.clear();
345 string::size_type p_last = str.find_first_not_of(" ", 0);
346 string::size_type p_curr = str.find_first_of(" ", p_last);
347 while ((string::npos != p_curr || string::npos != p_last) && tokens.size() < n_max_tokens) {
348 tokens.push_back(str.substr(p_last, p_curr - p_last));
349 p_last = str.find_first_not_of(" ", p_curr);
350 p_curr = str.find_first_of(" ", p_last);
351 }
352 return tokens.size();
353 }
354
355 int tokenize (string & str, vector < string > & tokens, string sep) {
356 tokens.clear();
357 string::size_type p_last = str.find_first_not_of(sep, 0);
358 string::size_type p_curr = str.find_first_of(sep, p_last);
359 while (string::npos != p_curr || string::npos != p_last) {
360 tokens.push_back(str.substr(p_last, p_curr - p_last));
361 p_last = str.find_first_not_of(sep, p_curr);
362 p_curr = str.find_first_of(sep, p_last);
363 }
364 if (tokens.back()[tokens.back().size()-1] == '\r')
365 tokens.back() = tokens.back().substr(0, tokens.back().size()-1);
366 return tokens.size();
367 }
368
369 bool isNumeric(string & str) {
370 float n;
371 std::istringstream in(str);
372 if (!(in >> n)) return false;
373 return true;
374 }
375
376 string int2str(int n) {
377 ostringstream s2( stringstream::out );
378 s2 << n;
379 return s2.str();
380 }
381
382 string int2str(vector < int > & v) {
383 ostringstream s2( stringstream::out );
384 for (int l = 0 ; l < v.size() ; l++) {
385 s2 << " " << v[l] ;
386 }
387 return s2.str();
388 }
389
390 string long2str(long int n) {
391 ostringstream s2( stringstream::out );
392 s2 << n;
393 return s2.str();
394 }
395
396 string double2str(double n) {
397 ostringstream s2;
398 s2 << n;
399 return s2.str();
400 }
401
402 string double2str(double n, int prc) {
403 ostringstream s2;
404 s2 << setiosflags( ios::fixed );
405 if ( prc > 0 ) s2.precision(prc);
406 s2 << n;
407 return s2.str();
408 }
409
410 string double2str(vector < double > &v) {
411 ostringstream s2;
412 for (int l = 0 ; l < v.size() ; l++) s2 << v[l] << ((l!=v.size()-1)?" ":"");
413 return s2.str();
414 }
415
416 string double2str(vector < double > &v, int prc) {
417 ostringstream s2;
418 s2 << setiosflags( ios::fixed );
419 if ( prc >= 0 ) s2.precision(prc);
420 for (int l = 0 ; l < v.size() ; l++) s2 << v[l] << ((l!=v.size()-1)?" ":"");
421 return s2.str();
422 }
423
424 string double2str(vector < float > &v) {
425 ostringstream s2;
426 for (int l = 0 ; l < v.size() ; l++) s2 << v[l] << ((l!=v.size()-1)?" ":"");
427 return s2.str();
428 }
429
430 string double2str(vector < float > &v, int prc) {
431 ostringstream s2;
432 s2 << setiosflags( ios::fixed );
433 if ( prc >= 0 ) s2.precision(prc);
434 for (int l = 0 ; l < v.size() ; l++) s2 << v[l] << ((l!=v.size()-1)?" ":"");
435 return s2.str();
436 }
437
438 string bool2str(vector<bool> & v) {
439 ostringstream s2( stringstream::out );
440 for (int l = 0 ; l < v.size() ; l++) {
441 if (v[l]) s2 << "1";
442 else s2 << "0";
443 }
444 return s2.str();
445 }
446
447 string date2str(time_t * t, string format) {
448 struct tm * timeinfo = localtime(t);
449 char buffer[128];
450 strftime(buffer, 128, format.c_str(), timeinfo);
451 ostringstream s2( stringstream::out );
452 s2 << buffer;
453 return s2.str();
454 }
455
456 string remove_spaces(const string &s) {
457 int last = s.size() - 1;
458 while (last >= 0 && s[last] == ' ') --last;
459 return s.substr(0, last + 1);
460 }
461 };
462
463 /******************************************************/
464 /* UTILS FILE */
465 /******************************************************/
466 namespace futils {
467 bool isFile(string f) {
468 ifile inp;
469 inp.open(f.c_str(), ifstream::in);
470 if(inp.fail()) {
471 inp.clear(ios::failbit);
472 inp.close();
473 return false;
474 }
475 inp.close();
476 return true;
477 }
478
479 bool createFile(string f) {
480 ofile out;
481 //out.open(f.c_str(), ofstream::out);
482 out.open(f.c_str());
483 if (out.fail()) {
484 out.clear(ios::failbit);
485 out.close();
486 return false;
487 } else out << "";
488 out.close();
489 return true;
490 }
491
492 void checkFile(string f, bool readmode) {
493 if (readmode && !isFile(f)) LOG.error(f + " is impossible to open, check file existence or reading permissions");
494 if (!readmode && !createFile(f)) LOG.error(f + " is impossible to open, check writing permissions");
495 }
496
497 string extensionFile(string & filename) {
498 if (filename.find_last_of(".") != string::npos)
499 return filename.substr(filename.find_last_of(".") + 1);
500 return "";
501 }
502
503
504 void bool2binary(vector < bool > & V, ostream &fd) {
505 int nb = V.size();
506 fd.write((char*)&nb, sizeof(int));
507 int cpt_byte = 0;
508 int cpt_bin = 0;
509 int nb_byte = (int)ceil( (V.size() * 1.0) / 8);
510 while (cpt_byte < nb_byte) {
511 bitset<8> byte_bitset;
512 for (int l = 0; l < 8 && cpt_bin < V.size() ; l++) {
513 byte_bitset[l] = V[cpt_bin];
514 cpt_bin ++;
515 }
516 char byte_char = (char)byte_bitset.to_ulong();
517 fd.write(&byte_char, 1);
518 cpt_byte++;
519 }
520 }
521
522 bool binary2bool(vector < bool > & V, istream & fd) {
523 int nb;
524 fd.read((char*)&nb, sizeof(int));
525 if (!fd) return false;
526 int cpt_byte = 0;
527 int cpt_bin = 0;
528 int nb_byte = (int)ceil( (nb * 1.0) / 8);
529 V = vector < bool >(nb);
530 while (cpt_byte < nb_byte) {
531 char byte_char;
532 fd.read(&byte_char, 1);
533 if (!fd) return false;
534 bitset<8> byte_bitset = byte_char;
535 for (int l = 0; l < 8 && cpt_bin < nb ; l++) {
536 V[cpt_bin] = byte_bitset[l];
537 cpt_bin++;
538 }
539 cpt_byte++;
540 }
541 return true;
542 }
543 };
544
545 /******************************************************/
546 /* INPUT FILE */
547 /******************************************************/
548 ifile::ifile() {
549 }
550
551 ifile::ifile(string filename , bool binary) {
552 open(filename, binary);
553 }
554
555 ifile::~ifile() {
556 close();
557 }
558
559 string ifile::name() {
560 return file;
561 }
562
563 bool ifile::open(string filename, bool binary) {
564 file = filename;
565 string ext = futils::extensionFile(filename);
566 if (ext == "gz") {
567 fd.open(file.c_str(), ios::in | ios::binary);
568 push(bio::gzip_decompressor());
569 } else if (ext == "bz2") {
570 fd.open(file.c_str(), ios::in | ios::binary);
571 push(bio::bzip2_decompressor());
572 } else if (binary) {
573 fd.open(file.c_str(), ios::in | ios::binary);
574 } else {
575 fd.open(file.c_str());
576 }
577 if (fd.fail()) return false;
578 push(fd);
579 return true;
580 }
581
582 bool ifile::readString(string & str) {
583 int s;
584 if (!read((char*)&s, sizeof(int))) return false;
585 char * buffer = new char [s + 1];
586 if (!read(buffer, s)) return false;
587 buffer[s] = '\0';
588 str = string(buffer);
589 delete [] buffer;
590 return true;
591 }
592
593 void ifile::close() {
594 if (!empty()) reset();
595 fd.close();
596 }
597
598 /******************************************************/
599 /* OUTPUT FILE */
600 /******************************************************/
601 ofile::ofile() {
602 }
603
604 ofile::ofile(string filename , bool binary) {
605 open(filename, binary);
606 }
607
608 ofile::~ofile() {
609 close();
610 }
611
612 string ofile::name() {
613 return file;
614 }
615
616 bool ofile::open(string filename, bool binary) {
617 file = filename;
618 string ext = futils::extensionFile(filename);
619 if (ext == "gz") {
620 fd.open(file.c_str(), ios::out | ios::binary);
621 push(bio::gzip_compressor());
622 } else if (ext == "bz2") {
623 fd.open(file.c_str(), ios::out | ios::binary);
624 push(bio::bzip2_compressor());
625 } else if (binary) {
626 fd.open(file.c_str(), ios::out | ios::binary);
627 } else {
628 fd.open(file.c_str());
629 }
630 if (fd.fail()) return false;
631 push(fd);
632 return true;
633 }
634
635 void ofile::writeString(string & str) {
636 int s = str.size();
637 write((char*)&s, sizeof(int));
638 write(str.c_str(), s * sizeof(char));
639 }
640
641 void ofile::close() {
642 if (!empty()) reset();
643 fd.close();
644 }
645
646 /******************************************************/
647 /* LOG FILE */
648 /******************************************************/
649 lfile::lfile() {
650 verboseC = true;
651 verboseL = true;
652 }
653
654 lfile::~lfile() {
655 close();
656 }
657
658 string lfile::name() {
659 return file;
660 }
661
662 bool lfile::open(string filename) {
663 file = filename;
664 if (futils::extensionFile(file) != "log") file += ".log";
665 fd.open(file.c_str());
666 if (fd.fail()) return false;
667 return true;
668 }
669
670 void lfile::close() {
671 fd.close();
672 }
673
674 string lfile::getPrefix() {
675 return file.substr(0, file.find_last_of("."));
676 }
677
678 void lfile::muteL() {
679 verboseL = false;
680 }
681
682 void lfile::unmuteL() {
683 verboseL = true;
684 }
685
686 void lfile::muteC() {
687 verboseC = false;
688 }
689
690 void lfile::unmuteC() {
691 verboseC = true;
692 }
693
694
695 void lfile::print(string s) {
696 if (verboseL) { fd << s; fd.flush(); }
697 if (verboseC) { cout << s; cout.flush(); }
698 }
699
700 void lfile::printC(string s) {
701 if (verboseC) { cout << s; cout.flush(); }
702 }
703
704 void lfile::printL(string s) {
705 if (verboseL) { fd << s; fd.flush(); }
706 }
707
708 void lfile::println(string s) {
709 if (verboseL) { fd << s << endl; }
710 if (verboseC) { cout << s << endl; }
711 }
712
713 void lfile::printlnC(string s) {
714 if (verboseC) { cout << s << endl; }
715 }
716
717 void lfile::printlnL(string s) {
718 if (verboseL) { fd << s << endl; }
719 }
720
721 void lfile::warning(string s) {
722 cout << endl << "\33[33mWARNING:\33[0m " << s << endl;
723 if (verboseL) fd << endl << "WARNING: " << s << endl;
724 }
725
726 void lfile::error(string s) {
727 cout << endl << "\33[33mERROR:\33[0m " << s << endl;
728 if (verboseL) fd << endl << "ERROR: " << s << endl;
729 close();
730 exit(1);
731 }