-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUStringUtils.cpp
executable file
·165 lines (149 loc) · 4.11 KB
/
UStringUtils.cpp
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
/**
* Taken mostly from 'vcflib'
*/
#include "UStringUtils.h"
#include <string.h>
#include <iostream>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
return split(s, delim, elems);
}
std::vector<std::string> &split(const std::string &s, const std::string &delims, std::vector<std::string> &elems) {
char* tok;
char cchars [s.size()+1];
char* cstr = &cchars[0];
strcpy(cstr, s.c_str());
tok = strtok(cstr, delims.c_str());
while (tok != NULL) {
elems.push_back(tok);
tok = strtok(NULL, delims.c_str());
}
return elems;
}
std::vector<std::string> split(const std::string &s, const std::string &delims) {
std::vector<std::string> elems;
return split(s, delims, elems);
}
std::vector<std::string> splitWithEmpty(const std::string &record, const std::string &token) {
vector<string> results;
size_t startPos = 0;
size_t pos = 0;
// Step: If either argument is empty then return an empty vector.
if (record.length() == 0 || token.length() == 0) {
return results;
}
// Step: Go through the record and split up the data.
while(startPos < record.length()) {
pos = record.find(token, startPos);
if (pos == string::npos) {
break;
}
results.push_back(record.substr(startPos, pos - startPos));
startPos = pos + token.length();
}
// Step: Get the last (or only bit).
results.push_back(record.substr(startPos, record.length() - startPos));
// Step: Return the results of the split.
return results;
}
std::vector<std::string> splitStrDelim(const std::string &s, const std::string& delimiter) {
std::vector<std::string> elems;
size_t pos = 0;
std::string token;
std::string s_copy = s;
if( s_copy.find(delimiter) == std::string::npos ) { elems.push_back(s); }
else {
while ((pos = s_copy.find(delimiter)) != std::string::npos) {
token = s_copy.substr(0, pos);
elems.push_back(token);
s_copy.erase(0, pos + delimiter.length());
}
}
return elems;
}
VariantFieldType str2VariantFieldType(const std::string &typeStr) {
if (typeStr == "Integer") {
return FIELD_INTEGER;
} else if (typeStr == "Float") {
return FIELD_FLOAT;
} else if (typeStr == "String") {
return FIELD_STRING;
} else if (typeStr == "Flag") {
return FIELD_FLAG;
} else {
return FIELD_UNKNOWN;
}
}
const std::string variantFieldType2str(const VariantFieldType &type) {
if ( type == FIELD_INTEGER ) {
return "Integer";
}
else if ( type == FIELD_FLOAT ) {
return "Float";
}
else if ( type == FIELD_FLAG ) {
return "Flag";
}
else if ( type == FIELD_STRING ) {
return "String";
}
else {
return "Uknown";
}
}
int str2VariantFieldNumber(const std::string &numStr) {
if (numStr == "A") {
return ALLELE_NUMBER;
} else if (numStr == "G") {
return GENOTYPE_NUMBER;
} else if (numStr == ".") {
return UNDEFINED;
} else {
int retVal;
convert(numStr, retVal);
return retVal;
}
}
const std::string variantFieldNumber2str(const int &num) {
if ( num == ALLELE_NUMBER ) {
return "A";
}
else if ( num == GENOTYPE_NUMBER ) {
return "G";
}
else if ( num == UNDEFINED ) {
return ".";
}
else {
return convert(num);
}
}
const std::string variantZygosity2str(const VariantZygosity &zygosity) {
if ( zygosity == HOMOZYGOUS ) {
return "Homozygote";
} else if ( zygosity == HETEROZYGOUS ) {
return "Heterozygote";
} else if ( zygosity == TRIZYGOUS ) {
return "Trizygote";
} else if ( zygosity == WILDTYPE ) {
return "Wildtype";
} else if ( zygosity == WILDTYPE_FILTERED ) {
return "Filtered Wildtype";
} else if ( zygosity == HOMOZYGOUS_FILTERED ) {
return "Filtered Homozygote";
} else if ( zygosity == HETEROZYGOUS_FILTERED ) {
return "Filtered Heterozygote";
} else if ( zygosity == TRIZYGOUS_FILTERED ) {
return "Filtered Trizygote";
} else {
return "Uknown";
}
}