forked from OpenGene/fastp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
274 lines (241 loc) · 6.37 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
#ifndef UTIL_H
#define UTIL_H
#include <stdlib.h>
#include <string>
#include <iostream>
#include <vector>
#include <sys/stat.h>
#include <algorithm>
#include <time.h>
#include <mutex>
using namespace std;
inline char complement(char base) {
switch(base){
case 'A':
case 'a':
return 'T';
case 'T':
case 't':
return 'A';
case 'C':
case 'c':
return 'G';
case 'G':
case 'g':
return 'C';
default:
return 'N';
}
}
inline bool starts_with( string const & value, string const & starting)
{
if (starting.size() > value.size()) return false;
return equal(starting.begin(), starting.end(), value.begin());
}
inline bool ends_with( string const & value, string const & ending)
{
if (ending.size() > value.size()) return false;
return equal(ending.rbegin(), ending.rend(), value.rbegin());
}
inline string trim(const string& str)
{
string::size_type pos = str.find_first_not_of(' ');
if (pos == string::npos)
{
return string("");
}
string::size_type pos2 = str.find_last_not_of(' ');
if (pos2 != string::npos)
{
return str.substr(pos, pos2 - pos + 1);
}
return str.substr(pos);
}
inline int split(const string& str, vector<string>& ret_, string sep = ",")
{
if (str.empty())
{
return 0;
}
string tmp;
string::size_type pos_begin = str.find_first_not_of(sep);
string::size_type comma_pos = 0;
while (pos_begin != string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}
ret_.push_back(tmp);
tmp.clear();
}
return 0;
}
inline string replace(const string& str, const string& src, const string& dest)
{
string ret;
string::size_type pos_begin = 0;
string::size_type pos = str.find(src);
while (pos != string::npos)
{
ret.append(str.data() + pos_begin, pos - pos_begin);
ret += dest;
pos_begin = pos + 1;
pos = str.find(src, pos_begin);
}
if (pos_begin < str.length())
{
ret.append(str.begin() + pos_begin, str.end());
}
return ret;
}
inline string reverse(const string& str) {
string ret(str.length(), 0);
for(int pos=0; pos<str.length(); pos++) {
ret[pos] = str[str.length() - pos - 1];
}
return ret;
}
inline string basename(const string& filename){
string::size_type pos = filename.find_last_of('/');
if (pos == string::npos)
return filename;
else if(pos == filename.length()-1)
return ""; // a bad filename
else
return filename.substr(pos+1, filename.length() - pos - 1);
}
inline string dirname(const string& filename){
string::size_type pos = filename.find_last_of('/');
if (pos == string::npos) {
return "./";
} else
return filename.substr(0, pos+1);
}
inline string joinpath(const string& dirname, const string& basename){
if(dirname[dirname.length()-1] == '/'){
return dirname + basename;
} else {
return dirname + "/" + basename;
}
}
//Check if a string is a file or directory
inline bool file_exists(const string& s)
{
bool exists = false;
if(s.length() > 0) {
struct stat status;
int result = stat( s.c_str(), &status );
if(result == 0) {
exists = true;
}
}
return exists;
}
// check if a string is a directory
inline bool is_directory(const string& path)
{
bool isdir = false;
struct stat status;
// visual studion use _S_IFDIR instead of S_IFDIR
// http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx
#ifdef _MSC_VER
#define S_IFDIR _S_IFDIR
#endif
stat( path.c_str(), &status );
if ( status.st_mode & S_IFDIR ) {
isdir = true;
}
// #endif
return isdir;
}
inline void check_file_valid(const string& s) {
if(!file_exists(s)){
cerr << "ERROR: file '" << s << "' doesn't exist, quit now" << endl;
exit(-1);
}
if(is_directory(s)){
cerr << "ERROR: '" << s << "' is a folder, not a file, quit now" << endl;
exit(-1);
}
}
inline void check_file_writable(const string& s) {
string dir = dirname(s);
if(!file_exists(dir)) {
cerr << "ERROR: '" << dir << " doesn't exist. Create this folder and run this command again." << endl;
exit(-1);
}
if(is_directory(s)){
cerr << "ERROR: '" << s << "' is not a writable file, quit now" << endl;
exit(-1);
}
}
// Remove non alphabetic characters from a string
inline string str_keep_alpha(const string& s)
{
string new_str;
for( size_t it =0; it < s.size(); it++) {
if( isalpha(s[it]) ) {
new_str += s[it];
}
}
return new_str;
}
// Remove invalid sequence characters from a string
inline void str_keep_valid_sequence( string& s, bool forceUpperCase = false)
{
size_t total = 0;
const char case_gap = 'a' - 'A';
for( size_t it =0; it < s.size(); it++) {
char c = s[it];
if(forceUpperCase && c>='a' && c<='z') {
c -= case_gap;
}
if( isalpha(c) || c == '-' || c == '*' ) {
s[total] = c;
total ++;
}
}
s.resize(total);
}
inline int find_with_right_pos(const string& str, const string& pattern, int start=0) {
int pos = str.find(pattern, start);
if (pos < 0)
return -1;
else
return pos + pattern.length();
}
inline void str2upper(string& s){
transform(s.begin(), s.end(), s.begin(), (int (*)(int))toupper);
}
inline void str2lower(string& s){
transform(s.begin(), s.end(), s.begin(), (int (*)(int))tolower);
}
inline char num2qual(int num) {
if(num > 127 - 33)
num = 127 - 33;
if(num < 0)
num = 0;
char c = num + 33;
return c;
}
inline void error_exit(const string& msg) {
cerr << "ERROR: " << msg << endl;
exit(-1);
}
extern mutex logmtx;
inline void loginfo(const string s){
logmtx.lock();
time_t tt = time(NULL);
tm* t= localtime(&tt);
cerr<<"["<<t->tm_hour<<":"<<t->tm_min<<":"<<t->tm_sec<<"] "<<s<<endl;
logmtx.unlock();
}
#endif /* UTIL_H */