-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.h
executable file
·419 lines (369 loc) · 8.16 KB
/
defs.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
#ifndef DEFS_H
#define DEFS_H
#include<cassert>
#include<cmath>
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<cstdarg>
#include<algorithm>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
using namespace std;
template<class T, class U>
ostream & operator << (ostream &out, const map<T,U> & m) {
typename map<T,U>::const_iterator it;
for (it = m.begin(); it != m.end(); it++) {
out << it->first << " -> " << it->second << endl;
}
return out;
}
//print vector of vectors
template<class T>
ostream & operator << (ostream & out, const vector<vector<T > > & v) {
out << v.size() << endl;
if (v.size() != 0) {
out << v[0];
for (int i = 1; i < v.size(); i++) out << endl << v[i];
}
return out;
}
//read vector of vectors
template<class T>
istream & operator >> (istream & in, vector<vector<T > > & v) {
int count;
vector<T> row;
in >> count;
v.reserve(count);
for (int i = 0; i < count; i++) {
row.clear();
in >> row;
v.push_back(row);
}
return in;
}
//print vector of T
template<class T>
ostream & operator << (ostream & out, const vector<T> & v) {
//out << v.size() << '\t';
if (v.size() != 0) {
out << v[0];
for (int i = 1; i < v.size(); i++) out << '\t' << v[i];
}
return out;
}
//read vector of T
template<class T>
istream & operator >> (istream & in, vector<T> & v) {
int count;
T val;
in >> count;
v.reserve(count);
for (int i = 0; i < count; i++) {
in >> val;
v.push_back(val);
}
return in;
}
void flip( bool &val) { val = !val; }
inline int randNumber (int min, int max) { return min + int((((double) rand()) / RAND_MAX ) * (max - min + 1) ); }
int nt2num(char c) {
if (c=='A') return 0;
if (c=='C') return 1;
if (c=='G') return 2;
if (c=='T') return 3;
if (c=='a') return 0;
if (c=='c') return 1;
if (c=='g') return 2;
if (c=='t') return 3;
cerr << "Invalid character " << c << " in nt2num." << endl;
exit(1);
}
char num2nt(int x) {
if (x==0) return 'A';
if (x==1) return 'C';
if (x==2) return 'G';
if (x==3) return 'T';
cerr << "Invalid number " << x << " in num2nt.\n";
exit(1);
}
void open_file(ifstream & inFile, string filename) {
inFile.open(filename.c_str());
if (!inFile) {
cerr << "Cannot open input file: " << filename << endl;
assert(0);
exit(1);
}
}
void open_file(ofstream & file, string filename) {
file.open(filename.c_str());
if (!file) {
cerr << "Cannot open input file: " << filename << endl;
assert(0);
exit(1);
}
}
void open_file_binary(ofstream & file, string filename) {
file.open(filename.c_str(), ios::out | ios::binary);
if (!file) {
cerr << "Cannot open input file: " << filename << endl;
assert(0);
exit(1);
}
}
string read_genome(string filename) {
string genome;
ifstream inFile;
open_file(inFile, filename);
char buf[257];
//read the file into genome
inFile.get(buf, 256);
char c;
while ((c = inFile.rdbuf()->sbumpc()) != EOF) {
if (c != '\n') {
genome.push_back(c);
}
}
inFile.close();
return genome;
}
/*
//assumes each line is a separate contig
string read_pseudo_genome(istream inFile) {
string genome;
//read the file into genome
char c;
while ((c = inFile.rdbuf()->sbumpc()) != EOF) {
if (c != '\n') {
genome.push_back(c);
} else {
genome.push_back('$');
}
}
return genome;
}
*/
template<class T>
int argmin(const vector<T> & data) {
if (data.size() == 0) return -1;
T maxVal;
int maxPos = 0;
maxVal = data[0];
for (int i = 1; i < data.size(); i++) {
if (data[i] < maxVal) {
maxVal = data[i];
maxPos = i;
}
}
return maxPos;
}
template<class T>
int argmin(T data[], int size) {
if (size == 0) return -1;
T maxVal;
int maxPos = 0;
maxVal = data[0];
for (int i = 1; i < size; i++) {
if (data[i] < maxVal) {
maxVal = data[i];
maxPos = i;
}
}
return maxPos;
}
template<class T>
int argmax(const vector<T> & data) {
if (data.size() == 0) return -1;
T maxVal;
int maxPos = 0;
maxVal = data[0];
for (int i = 1; i < data.size(); i++) {
if (data[i] > maxVal) {
maxVal = data[i];
maxPos = i;
}
}
return maxPos;
}
template<class T>
int argmax(T data[], int size) {
if (size == 0) return -1;
T maxVal;
int maxPos = 0;
maxVal = data[0];
for (int i = 1; i < size; i++) {
if (data[i] > maxVal) {
maxVal = data[i];
maxPos = i;
}
}
return maxPos;
}
int nscore(vector<int> & data, int genLen, int nval) {
sort(data.begin(), data.end());
int totLen=0;
for (int i = 0; i < data.size(); i++) {
totLen += data[i];
if (totLen >= genLen * double(nval / 100.0)) return data[i];
}
cerr << "trouble in nscore\n";
exit(1);
}
bool get_row(istream & inFile, vector<string> & row, char delim = '\t') { //read a tab delimited line from file
string line, s;
row.clear();
getline(inFile, line);
if (inFile.eof()) return false;
istringstream lineStream(line);
while (getline(lineStream, s, delim)) {
row.push_back(s);
}
return true;
}
bool get_row_whitespace (istream & inFile, vector<string> & row ) { //read a tab delimited line from file
string line, s;
row.clear();
getline(inFile, line);
if (inFile.eof()) return false;
istringstream lineStream(line);
while (lineStream >> s) {
row.push_back(s);
}
return true;
}
char revcomp (char s) {
if (s == 'A') return 'T';
else if (s == 'C') return 'G';
else if (s == 'G') return 'C';
else if (s == 'T') return 'A';
else if (s == 'a') return 't';
else if (s == 'c') return 'g';
else if (s == 'g') return 'c';
else if (s == 't') return 'a';
return 'X';
}
string revcomp (string s) {
string rc;
for (int i = s.length() - 1; i >= 0; i--) rc+=revcomp(s[i]);
return rc;
}
string rcnorm (string &s) {
string rc;
for (int i = 0; i < s.length(); i++) {
char c = revcomp(s[s.length() - 1 - i]);
if (s[i] < c) {
return s;
} else if (s[i] > c) {
return revcomp(s);
}
}
return revcomp(s);
}
string rcnorm (string &s, bool & flipped) {
string rc;
for (int i = 0; i < s.length(); i++) {
char c = revcomp(s[s.length() - 1 - i]);
if (s[i] < c) {
flipped = false;
return s;
} else if (s[i] > c) {
flipped = true;
return revcomp(s);
}
}
return revcomp(s);
}
enum HAMDIST_OPTS {SAME_STRAND, DIFF_STRAND, ANY_STRAND};
int hamdist(const string & x, const string & y, HAMDIST_OPTS mode, int maxAllowed = -1) {
if (mode == ANY_STRAND) {
string rcy = revcomp(y);
return min(hamdist(x,y, SAME_STRAND), hamdist(x,rcy,SAME_STRAND));
} else if (mode == DIFF_STRAND) {
string rcy = revcomp(y);
return hamdist(x,rcy,SAME_STRAND);
}
assert (mode == SAME_STRAND);
assert (x.length() == y.length());
int dist = 0;
if (maxAllowed == -1) maxAllowed = x.length();
for (int i = 0; i < x.length(); i++) {
if (x[i] != y[i]) {
dist++;
if (dist > maxAllowed) return dist;
}
}
return dist;
}
int hamdist(string & x, string & y, HAMDIST_OPTS mode, bool &flipped) {
assert (mode == ANY_STRAND);
int dist, dist2;
dist = hamdist(x,y, SAME_STRAND);
dist2 = hamdist(x,y,DIFF_STRAND);
if (dist2 < dist) {
flipped = true;
return dist2;
}
flipped = false;
return dist;
}
void maskStrings(vector<string> & strings) {
int minLen = 10000000;
for (int i = 0; i < strings.size(); i++) {
if (strings[i].length() < minLen) minLen = strings[i].length();
}
for (int j = 0; j < minLen; j++) {
bool same = true;;
char lastchar = strings[0].at(j);
for (int i = 1; i < strings.size(); i++) {
if (strings[i].at(j) != lastchar) {
same = false;
break;
}
}
if (same) {
for (int i = 0; i < strings.size(); i++) {
strings[i].at(j) = '-';
}
}
}
}
template<class T>
string make_string(const T & s) {
ostringstream o;
o << s;
return o.str();
}
string add_commas(int num) {
string s, retval;
s = make_string(num);
for (int i = 0; i < s.length(); i++) {
retval.push_back(s.at(i));
int j = s.length() - 1 - i;
if (((j % 3) == 0) && (j != 0)) {
retval.push_back(',');
}
}
return retval;
}
long get_filesize(string filename) {
int file=0;
if((file=open(filename.c_str(),O_RDONLY)) < -1) {
cerr << "File not found: " << filename;
exit(1);
}
struct stat fileStat;
if(fstat(file,&fileStat) < 0) {
cerr << "Can't fstat file: " << filename;
exit(1);
}
return fileStat.st_size;
}
#endif