-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathUtil.h
334 lines (266 loc) · 10.4 KB
/
Util.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
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <cstddef>
#include <cstring>
#include <vector>
#include <limits>
#include <map>
#include "MMseqsMPI.h"
#ifndef EXIT
#define EXIT(exitCode) do { int __status = (exitCode); std::cerr.flush(); std::cout.flush(); exit(__status); } while(0)
#endif
#define BIT_SET(a,b) ((a) | (1ULL<<(b)))
#define BIT_CLEAR(a,b) ((a) & ~(1ULL<<(b)))
#define BIT_CHECK(a,b) (!!((a) & (1ULL<<(b))))
template<typename T>
struct assert_false : std::false_type
{ };
template<typename T>
std::string SSTR(T) {
static_assert(assert_false<T>::value , "Not implemented for requested type");
return "";
}
template<> std::string SSTR(const char*);
template<> std::string SSTR(char*);
template<> std::string SSTR(bool);
template<> std::string SSTR(const char[]);
template<> std::string SSTR(const std::string&);
template<> std::string SSTR(std::string);
template<> std::string SSTR(char);
template<> std::string SSTR(short);
template<> std::string SSTR(unsigned short);
template<> std::string SSTR(int);
template<> std::string SSTR(unsigned int);
template<> std::string SSTR(long);
template<> std::string SSTR(unsigned long);
template<> std::string SSTR(long long);
template<> std::string SSTR(unsigned long long);
template<> std::string SSTR(double);
template<> std::string SSTR(float);
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#if defined(__GNUC__) || __has_builtin(__builtin_expect)
#define LIKELY(x) __builtin_expect((x),1)
#define UNLIKELY(x) __builtin_expect((x),0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
#if defined(__GNUC__) || __has_attribute(__unused__)
# define MAYBE_UNUSED(x) x __attribute__((__unused__))
#else
# define MAYBE_UNUSED(x) x
#endif
class Util {
public:
static void decomposeDomain(size_t domain_size, size_t world_rank,
size_t world_size, size_t *subdomain_start,
size_t *subdomain_size);
static void rankedDescSort8(short *val, unsigned int *index);
static void rankedDescSort32(short *val, unsigned int *index);
static void rankedDescSort20(short *val, unsigned int *index);
static size_t getTotalSystemMemory();
static size_t getPageSize();
static size_t getTotalMemoryPages();
static uint64_t getL2CacheSize();
static char touchMemory(const char* memory, size_t size);
static size_t countLines(const char *data, size_t length);
static size_t ompCountLines(const char *data, size_t length, unsigned int threads);
static int readMapping(std::string mappingFilename, std::vector<std::pair<unsigned int, unsigned int> > & mapping);
template<typename T>
static inline T fast_atoi( const char * str )
{
T val = 0;
int sign=1;
if(std::numeric_limits<T>::is_signed == true){
if(*str == '-') {
sign = -1;
str++;
}
}
while (*str >= '0' && *str <= '9') {
val = val*10 + (*str++ - '0');
}
return sign * val;
}
static char* fastSeqIdToBuffer(float seqId, char* buffer);
static bool isNumber(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
static bool startWith(const std::string &prefix, const std::string &str, const size_t offset = 0){
if (str.length() < prefix.length()) {
return false;
}
return (!str.compare(offset, prefix.length(), prefix));
}
static bool endsWith(const std::string &suffix, const std::string &str){
if (str.length() < suffix.length()) {
return false;
}
return (!str.compare(str.length() - suffix.length(), suffix.length(), suffix));
}
static std::vector<std::string> split(const std::string &str, const std::string &sep);
static std::string& implode(const std::vector<std::string> &splits, char sep, std::string& str) {
for (std::vector<std::string>::const_iterator it = splits.begin(); it != splits.end(); ++it) {
str += (*it);
if (it + 1 != splits.end()) {
str += sep;
}
}
return str;
}
static std::string implode(const std::vector<std::string> &splits, char sep) {
std::string str;
implode(splits, sep, str);
return str;
}
static inline char * skipLine(char * data){
while( *data !='\n' ) { data++; }
return (data+1);
}
static inline char * seekToNextEntry(char * data){
while( *data !='\0' ) { data++; }
return (data+1);
}
static bool getLine(const char* data, size_t dataLength, char* buffer, size_t bufferLength);
static inline size_t skipWhitespace(const char* data){
size_t counter = 0;
while((data[counter] == ' ' || data[counter] == '\t') == true) {
counter++;
}
return counter;
}
// Return the index i such that data[i] <- '\0' makes a string terminated
// by a non Whitespace character
static inline size_t getLastNonWhitespace(char * data, size_t len){
size_t counter = len;
if (!counter)
return 0;
if (counter && data[counter] == '\0')
counter--;
while( (data[counter] == ' ' || data[counter] == '\t')) {
if(!counter)
return 0;
counter--;
}
return counter + 1;
}
static inline size_t skipNoneWhitespace(const char * data){
//A value different from zero (i.e., true) if indeed c is a white-space character. Zero (i.e., false) otherwise.
size_t counter = 0;
while((data[counter] == ' ' || data[counter] == '\t' || data[counter] == '\n' || data[counter] == '\0') == false) {
counter++;
}
return counter;
}
static std::pair<std::string, std::string> createTmpFileNames(const std::string &db,
const std::string &dbindex, int count){
// TODO take only db and not db and dbindex
// TODO check naming of old database paths
std::string suffix = std::string("_tmp_") + SSTR(count);
std::string data = db + suffix;
std::string index = "";
if (dbindex.compare(db + ".index") == 0) {
index.append(db + suffix + ".index");
} else {
//Debug(Debug::WARNING) << "Database index name is deprecated (" << dbindex << ")\n";
index.append(dbindex + suffix);
}
return std::make_pair(data, index);
}
static std::pair<std::string, std::string> databaseNames(const std::string &basename) {
std::string index = basename;
index.append(".index");
return std::make_pair(basename, index);
};
static inline size_t getWordsOfLine(const char* data, const char** words, size_t maxElement ){
size_t elementCounter = 0;
while(*data != '\n' && *data != '\0'){
data += skipWhitespace(data);
words[elementCounter] = data;
elementCounter++;
if(elementCounter >= maxElement)
return elementCounter;
data += skipNoneWhitespace(data);
}
if(elementCounter < maxElement)
words[elementCounter] = data;
return elementCounter;
}
static std::pair<ssize_t,ssize_t> getFastaHeaderPosition(const std::string & header);
static std::string parseFastaHeader(const char * header);
static inline char toUpper(char character){
character += ('a' <= character && character <= 'z') ? ('A' - 'a') : 0;
return character;
}
static void parseKey(const char *data, char * key);
static void parseByColumnNumber(char *data, char * key, int position);
static std::string base_name(std::string const & path, std::string const & delims)
{
return path.substr(path.find_last_of(delims) + 1);
}
static std::string remove_extension(std::string const & filename)
{
typename std::string::size_type const p(filename.find_last_of('.'));
return p > 0 && p != std::string::npos ? filename.substr(0, p) : filename;
}
static void checkAllocation(void *pointer, std::string message);
template <typename Iterator, typename Container>
static bool isLastIterator(Iterator iterator, const Container& container) {
return (iterator != container.end()) && (++iterator == container.end());
}
static std::string csvEscape(const std::string &s) {
size_t n = s.size(), wp = 0;
std::vector<char> result(n * 2);
for (size_t i = 0; i < n; i++) {
if (s[i] == '\n' || s[i] == '\t') {
result[wp++] = '\\';
result[wp++] = 'n';
continue;
}
result[wp++] = s[i];
}
return std::string(&result[0], &result[wp]);
}
static std::string removeAfterFirstSpace(std::string in) {
in.erase(in.find_first_of(" "));
return in;
}
static size_t overlappingKmers(int seqLen, unsigned int kmerSize) {
int kmersPerSize = seqLen - static_cast<int>(kmerSize);
return (kmersPerSize >= 0) ? kmersPerSize + 1 : 0;
}
template <typename T>
static size_t hash(T * x, size_t length){
const size_t INITIAL_VALUE = 0;
const size_t A = 31;
size_t h = INITIAL_VALUE;
for (size_t i = 0; i < length; ++i){
h = ((h*A) + x[i]);
}
return h;
}
static int omp_thread_count();
static std::string removeWhiteSpace(std::string in);
static std::map<unsigned int, std::string> readLookup(const std::string& lookupFile,
const bool removeSplit = false);
static bool canBeCovered(const float covThr, const int covMode, float queryLength, float targetLength);
static bool hasCoverage(float covThr, int covMode, float queryCov, float targetCov);
static int swapCoverageMode(int covMode);
static float computeSeqId(int seqIdMode, int aaIds, int qLen, int tLen, int alnLen);
static uint64_t revComplement(const uint64_t kmer, const int k);
static bool hasAlignmentLength(int alnLenThr, int alnLen) {
return alnLen >= alnLenThr;
}
static size_t computeMemory(size_t limit);
};
#endif