-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commonf.h
467 lines (371 loc) · 7.86 KB
/
Commonf.h
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#ifndef COMMONF_H_
#define COMMONF_H_
#include <limits.h>
#include <limits>
#include "CppUtilClasses/StringUtil.h"
#include "Nucleic.h"
#include <sstream>
#include <vector>
using namespace std;
int getActualPosGappedSeq(const char*seq, const int* sorted_pos, int *out_pos, int num_pos, char gap='-');
template<class P>
class less_ptr
{
public:
inline bool operator()(const P left, const P right)
{
return *left<*right;
}
};
inline static string toUpperAndNoSpecialDNA(const string& str)
{
int len=str.length();
char *tmp=new char[len+1];
register const char*cur=str.c_str();
register const char*terminal=cur+len;
register char *tmp2=tmp;
for(;cur<terminal;cur++,tmp2++)
{
*tmp2=toupper(*cur);
switch(*tmp2)
{
case A_SYM:case C_SYM:case T_SYM:case G_SYM:
break;
case U_SYM:
*tmp2=T_SYM;
break;
default:
*tmp2=N_SYM;
}
}
tmp[len]='\0';
string tstr=string(tmp);
delete[] tmp;
return tstr;
}
template <class T>
class SmartPtr
{
private:
T* ptr;
public:
inline SmartPtr(T* _ptr=NULL):ptr(_ptr){}
inline void operator=( T *_ptr)
{
ptr=_ptr;
}
inline operator T*() const
{
return ptr;
}
inline T& operator *() const
{
return *ptr;
}
inline T* operator ->() const
{
return ptr;
}
inline bool operator<(const SmartPtr<T>& right) const
{
return (**this)<(*right);
}
inline bool operator>(const SmartPtr<T>& right) const
{
return (**this)>(*right);
}
inline bool operator==(const SmartPtr<T>& right) const
{
return (**this)==(*right);
}
inline bool operator<=(const SmartPtr<T>& right) const
{
return !(**this>*right);
}
inline bool operator>=(const SmartPtr<T>& right) const
{
return !(**this<*right);
}
inline bool operator!=(const SmartPtr<T>& right) const
{
return !((**this)==(*right));
}
inline bool ptrEquals(const SmartPtr<T>& right) const
{
return ptr==right.ptr;
}
};
void die_clean_up();
void die_exit_clean_up();
#define die(msg) {if(strlen(msg)>0){ cerr<<"die:"<<(msg)<<endl;} die_clean_up(); return; }/*added die_clean_up defined to clean up memory*/
#define dier(msg,r) {if(strlen(msg)>0) { cerr<<"die:"<<(msg)<<endl;} die_clean_up(); return (r); }/*added reset everything to clean up memory*/
#define die_exit(msg) {if(strlen(msg)>0) { cerr<<"die:"<<(msg)<<endl;} die_exit_clean_up(); exit(1); }
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
inline KeyPair<int,int> b01tob00(const KeyPair<int,int>& b01)
{
return KeyPair<int,int>(b01.k1,b01.k2-1);
}
inline KeyPair<int,int> b00tob01(const KeyPair<int,int>& b00)
{
return KeyPair<int,int>(b00.k1,b00.k2+1);
}
inline int offset00(int a0,int len)
{
return a0+len-1;
}
inline int offset01(int a0,int len)
{
return a0+len;
}
inline int offset10(int a1,int len)
{
return a1+len-2;
}
inline int offset11(int a1,int len)
{
return offset00(a1,len);
}
inline bool isBound00Valid(const KeyPair<int,int>& b00)
{
return b00.k1>=0 && b00.k2<INT_MAX && b00.k1<=b00.k2;
}
inline bool isBound11Valid(const KeyPair<int,int>& b11)
{
return b11.k1>=1 && b11.k2<INT_MAX && b11.k1<=b11.k2;
}
inline bool isBound01Valid(const KeyPair<int,int>& b01)
{
return b01.k1>=0 && b01.k2<INT_MAX && b01.k1<b01.k2;
}
inline int len01(int start0,int end1)
{
int l=end1-start0;
return MAX(l,0);
}
inline int len00(int start0,int end0)
{
int l=end0-start0+1;
return MAX(l,0);
}
inline int len10(int start1,int end0)
{
int l=end0-start1+2;
return MAX(l,0);
}
inline int len11(int start1,int end1)
{
return len00( start1, end1);
}
inline int len01(const KeyPair<int,int>& bound01)
{
return len01(bound01.k1,bound01.k2);
}
inline int len11(const KeyPair<int,int>& bound11)
{
return len11(bound11.k1,bound11.k2);
}
inline int len10(const KeyPair<int,int>& bound10)
{
return len10(bound10.k1,bound10.k2);
}
inline int len00(const KeyPair<int,int>& bound00)
{
return len00(bound00.k1,bound00.k2);
}
inline int overlaplen1111(const KeyPair<int,int>& a11,const KeyPair<int,int> &b11)
{
if(!isBound11Valid(a11) || !isBound11Valid(b11))
return 0;
return len11(MAX(a11.k1,b11.k1),MIN(a11.k2,b11.k2));
//return MAX(lraw,0);
}
inline int overlaplen0101(const KeyPair<int,int>& a01,const KeyPair<int,int> &b01)
{
if(!isBound01Valid(a01) || !isBound01Valid(b01))
return 0;
return len01(MAX(a01.k1,b01.k1),MIN(a01.k2,b01.k2));
//return MAX(lraw,0);
}
template<class T>
class SerializableVector
{
public:
vector<T> _vector;
inline SerializableVector(string filename)
{
load(filename);
}
inline int load(string filename,bool clear=true)
{
if(clear)
{
_vector.clear();
}
int numRead=0;
ifstream fin(filename.c_str());
if(!fin.good())
{
fin.close();
return numRead;
}
while(!fin.eof())
{
T t;
fin>>t;
_vector.push_back(t);
numRead++;
}
fin.close();
return numRead;
}
inline int write(string filename,std::_Ios_Openmode nMode=ios::out|ios::trunc,string newLine="\n")
{
int numWrite=0;
ofstream fout(filename.c_str(),nMode);
if(!fout.good())
return numWrite;
for(typename vector<T>::iterator i=_vector.begin();i!=_vector.end();i++)
{
fout<<(*i);
if(newLine.length()>0)
fout<<newLine;
}
numWrite=_vector.size();
return numWrite;
}
};
template< class K, class V>
class One2OneMapping
{
public:
map<K,V> _map;
inline int load(string filename,bool clearMap=true)
{
if(clearMap)
{
_map.clear();
}
int numRead=0;
ifstream fin(filename.c_str());
if(!fin.good())
{
fin.close();
return numRead;
}
while(!fin.eof())
{
K k;
V v;
fin>>k;
if(fin.eof())
{
break;
}
fin>>v;
_map.insert(typename map<K,V>::value_type(k,v));
numRead++;
}
fin.close();
return numRead;
}
inline One2OneMapping(string filename)
{
load(filename);
}
};
typedef One2OneMapping<int,string> ChrMap;
typedef SerializableVector<string> FileList;
template< class T, class K, class I>
class buffered_ofstream
{
public:
string filename;
string endline;
//ofstream* fout;
K store;
int threshold;
int storedItemSize;
int lineOutputed;
typedef I ItemIterator;
inline buffered_ofstream(string _filename,bool clearFil=true,int _threshold=500,string _endline="\n"): filename(_filename), threshold(_threshold), store(_threshold), endline(_endline),storedItemSize(0),lineOutputed(0)
{
if(clearFil)
clearFile();
}
inline void clearFile()
{
//just to clear the content of the file
lineOutputed=0;
ofstream fout(filename.c_str());
fout.close();
}
inline void flush(std::_Ios_Openmode nMode=ios::out|ios::app,bool printEndLine=true)
{
ofstream fout(filename.c_str(),nMode);
if(!fout.good())
{
cerr<<"file error"<<endl;
fout.close();
return;
}
for(int i=0;i<storedItemSize;i++)
{
if(!fout.good())
{
cerr<<"file error"<<endl;
fout.close();
return;
}
if(printEndLine)
fout<<store[i]<<endline;
else
fout<<store[i];
//cerr<<(*i)<<endline;
}
lineOutputed+=storedItemSize;
//store.clear();
storedItemSize=0;
fout.close();
}
inline void push(const T& element,std::_Ios_Openmode nMode=ios::out|ios::app,bool printEndLine=true)
{
store[storedItemSize]=element;
storedItemSize++;
if(storedItemSize==threshold)
{
flush(nMode,printEndLine);
}
}
inline int getStoredItemSize()
{
return this->storedItemSize;
}
inline int getLineOutputed()
{
return this->lineOutputed;
}
};
#define READLINEBUFFERLENGTH 3
inline string consumeStreamUntilNewLine(istream &is)
{
/*char newLineBuff[READLINEBUFFERLENGTH];
stringstream strin;
bool cont=true;
while(cont)
{
is.getline(newLineBuff,READLINEBUFFERLENGTH);
strin<<newLineBuff;
cerr<<"read "<<newLineBuff<<" gcount="<<is.gcount()<<" flag="<<is.rdstate()<<endl;
if( (is.rdstate() & ios::failbit)!=0 && is.gcount()==READLINEBUFFERLENGTH )
{
cont=true;
}
else cont=false;
}
return strin.str();*/
string s;
std::getline(is,s);
return s;
}
#endif /*COMMONF_H_*/