1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "geno.h" |
---|
6 | #include "genoconv.h" |
---|
7 | #include <common/loggers/loggers.h> |
---|
8 | #include <common/util-string.h> |
---|
9 | #include <frams/model/model.h> |
---|
10 | |
---|
11 | THREAD_LOCAL_DEF_PTR(Geno::Validators, geno_validators); |
---|
12 | THREAD_LOCAL_DEF_PTR(GenoConvManager, geno_converters); |
---|
13 | |
---|
14 | Geno::Validators* Geno::getValidators() { return tlsGetPtr(geno_validators); } |
---|
15 | GenoConvManager* Geno::getConverters() { return tlsGetPtr(geno_converters); } |
---|
16 | |
---|
17 | Geno::Validators* Geno::useValidators(Validators* val) |
---|
18 | { |
---|
19 | return tlsSetPtr(geno_validators, val); |
---|
20 | } |
---|
21 | GenoConvManager* Geno::useConverters(GenoConvManager* gcm) |
---|
22 | { |
---|
23 | return tlsSetPtr(geno_converters, gcm); |
---|
24 | } |
---|
25 | |
---|
26 | const SString Geno::INVALID_FORMAT = "invalid"; |
---|
27 | const SString Geno::UNKNOWN_FORMAT = ""; |
---|
28 | |
---|
29 | void Geno::init(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment) |
---|
30 | { |
---|
31 | refcount = 1; |
---|
32 | owner = 0; |
---|
33 | f0gen = 0; |
---|
34 | isvalid = -1; |
---|
35 | name = genname; |
---|
36 | txt = comment; |
---|
37 | setGenesAndFormat(genstring, genformat); |
---|
38 | } |
---|
39 | |
---|
40 | static SString trimAndValidateFormat(const SString& input) //the new requirement for genotype format name: no whitespace |
---|
41 | { |
---|
42 | SString format = trim(input); |
---|
43 | if (format.len() == 0 || strContainsOneOf(format.c_str(), " \r\n\t")) |
---|
44 | return Geno::INVALID_FORMAT; |
---|
45 | return format; |
---|
46 | } |
---|
47 | |
---|
48 | void Geno::setGenesAndFormat(const SString& genstring, const SString& in_genformat) |
---|
49 | { |
---|
50 | mapinshift = 0; |
---|
51 | mapoutshift = 0; |
---|
52 | SString gencopy(genstring); |
---|
53 | SString genformat = in_genformat; |
---|
54 | if (genformat == UNKNOWN_FORMAT) |
---|
55 | { // unknown format |
---|
56 | genformat = "1"; |
---|
57 | if (genstring.charAt(0) == '/') |
---|
58 | { |
---|
59 | int end, error_end = -1; |
---|
60 | switch (genstring.charAt(1)) |
---|
61 | { |
---|
62 | case '/': |
---|
63 | if ((end = genstring.indexOf('\n')) >= 0) |
---|
64 | { |
---|
65 | genformat = trimAndValidateFormat(genstring.substr(2, end - 2)); |
---|
66 | mapinshift = end + 1; |
---|
67 | gencopy = genstring.substr(end + 1); |
---|
68 | if ((end > 0) && (genstring[end - 1] == '\r')) end--; |
---|
69 | error_end = end; |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | genformat = trimAndValidateFormat(genstring.substr(2)); |
---|
74 | gencopy = ""; |
---|
75 | mapinshift = genstring.len(); |
---|
76 | } |
---|
77 | break; |
---|
78 | case '*': |
---|
79 | if ((end = genstring.indexOf("*/")) >= 0) |
---|
80 | { |
---|
81 | genformat = trimAndValidateFormat(genstring.substr(2, end - 2)); |
---|
82 | error_end = end + 2; |
---|
83 | gencopy = genstring.substr(end + 2); |
---|
84 | mapinshift = end + 2; |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | genformat = trimAndValidateFormat(genstring.substr(2)); |
---|
89 | gencopy = ""; |
---|
90 | mapinshift = genstring.len(); |
---|
91 | } |
---|
92 | break; |
---|
93 | } |
---|
94 | if (genformat == INVALID_FORMAT) |
---|
95 | { |
---|
96 | SString cut; |
---|
97 | if (error_end < 0) error_end = genstring.len(); |
---|
98 | static const int MAX_ERROR = 20; |
---|
99 | if (error_end > MAX_ERROR) |
---|
100 | cut = genstring.substr(0, MAX_ERROR) + "..."; |
---|
101 | else |
---|
102 | cut = genstring.substr(0, error_end); |
---|
103 | int lf = cut.indexOf('\n'); |
---|
104 | if (lf >= 0) { if ((lf > 0) && (cut[lf - 1] == '\r')) lf--; cut = cut.substr(0, lf); } |
---|
105 | sstringQuote(cut); |
---|
106 | logPrintf("Geno", "init", LOG_ERROR, "Invalid genotype format declaration: '%s'%s", cut.c_str(), name.len() ? SString::sprintf(" in '%s'", name.c_str()).c_str() : ""); |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|
110 | } |
---|
111 | gen = gencopy; |
---|
112 | multiline = (strchr(gen.c_str(), '\n') != 0); |
---|
113 | format = genformat; |
---|
114 | freeF0(); |
---|
115 | isvalid = -1; |
---|
116 | // mapoutshift...? |
---|
117 | } |
---|
118 | |
---|
119 | void Geno::freeF0() |
---|
120 | { |
---|
121 | if (f0gen) { delete f0gen; f0gen = 0; } |
---|
122 | } |
---|
123 | |
---|
124 | Geno::Geno(const char *genstring, const char* genformat, const char *genname, const char *comment) |
---|
125 | { |
---|
126 | init(SString(genstring), SString(genformat), SString(genname), SString(comment)); |
---|
127 | } |
---|
128 | |
---|
129 | Geno::Geno(const char *genstring, char genformat, const char *genname, const char *comment) |
---|
130 | { |
---|
131 | SString genformat_string; |
---|
132 | if (genformat > 0) |
---|
133 | genformat_string = SString(&genformat, 1); |
---|
134 | init(genstring, genformat_string, genname, comment); |
---|
135 | } |
---|
136 | |
---|
137 | Geno::Geno(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment) |
---|
138 | { |
---|
139 | init(genstring, genformat, genname, comment); |
---|
140 | } |
---|
141 | |
---|
142 | Geno::Geno(const Geno& src) |
---|
143 | :gen(src.gen), name(src.name), format(src.format), txt(src.txt), isvalid(src.isvalid), |
---|
144 | f0gen(0), mapinshift(src.mapinshift), mapoutshift(src.mapinshift), |
---|
145 | multiline(src.multiline), owner(0) |
---|
146 | { |
---|
147 | f0gen = src.f0gen ? new Geno(*src.f0gen) : 0; refcount = 1; |
---|
148 | } |
---|
149 | |
---|
150 | void Geno::operator=(const Geno& src) |
---|
151 | { |
---|
152 | freeF0(); |
---|
153 | gen = src.gen; |
---|
154 | name = src.name; |
---|
155 | format = src.format; |
---|
156 | txt = src.txt; |
---|
157 | isvalid = src.isvalid; |
---|
158 | mapinshift = src.mapinshift; |
---|
159 | mapoutshift = src.mapinshift; |
---|
160 | multiline = src.multiline; |
---|
161 | f0gen = src.f0gen ? new Geno(*src.f0gen) : 0; |
---|
162 | owner = 0; |
---|
163 | } |
---|
164 | |
---|
165 | Geno::Geno(const SString& src) |
---|
166 | { |
---|
167 | init(src, UNKNOWN_FORMAT, SString::empty(), SString::empty()); |
---|
168 | } |
---|
169 | |
---|
170 | void Geno::setGenesAssumingSameFormat(const SString& g) |
---|
171 | { |
---|
172 | gen = g; |
---|
173 | isvalid = -1; |
---|
174 | freeF0(); |
---|
175 | } |
---|
176 | |
---|
177 | void Geno::setString(const SString& g) |
---|
178 | { |
---|
179 | freeF0(); |
---|
180 | init(g, UNKNOWN_FORMAT, SString::empty(), SString::empty()); |
---|
181 | } |
---|
182 | |
---|
183 | void Geno::setName(const SString& n) |
---|
184 | { |
---|
185 | name = n; |
---|
186 | } |
---|
187 | |
---|
188 | void Geno::setComment(const SString& c) |
---|
189 | { |
---|
190 | txt = c; |
---|
191 | } |
---|
192 | |
---|
193 | SString Geno::getGenesAndFormat(void) const |
---|
194 | { |
---|
195 | SString out; |
---|
196 | if (format != "1") |
---|
197 | { |
---|
198 | if (multiline) |
---|
199 | out += "//"; |
---|
200 | else |
---|
201 | out += "/*"; |
---|
202 | out += format; |
---|
203 | if (multiline) |
---|
204 | out += "\n"; |
---|
205 | else |
---|
206 | out += "*/"; |
---|
207 | } |
---|
208 | out += gen; |
---|
209 | return out; |
---|
210 | } |
---|
211 | |
---|
212 | int Geno::mapGenToString(int genpos) const |
---|
213 | { |
---|
214 | if (genpos > gen.len()) return -2; |
---|
215 | if (genpos < 0) return -1; |
---|
216 | return mapinshift + genpos; |
---|
217 | } |
---|
218 | |
---|
219 | int Geno::mapStringToGen(int stringpos) const |
---|
220 | { |
---|
221 | stringpos -= mapinshift; |
---|
222 | if (stringpos > gen.len()) return -2; |
---|
223 | if (stringpos < 0) return -1; |
---|
224 | return stringpos; |
---|
225 | } |
---|
226 | |
---|
227 | SString Geno::getGenes(void) const { return gen; } |
---|
228 | SString Geno::getName(void) const { return name; } |
---|
229 | SString Geno::getFormat(void) const { return format; } |
---|
230 | SString Geno::getComment(void) const { return txt; } |
---|
231 | |
---|
232 | int ModelGenoValidator::testGenoValidity(Geno& g) |
---|
233 | { |
---|
234 | if (g.getFormat() == "0") |
---|
235 | { |
---|
236 | Model mod(g); |
---|
237 | return mod.isValid(); |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | bool converter_missing; |
---|
242 | Geno f0geno = g.getConverted("0", NULL, false, &converter_missing); |
---|
243 | if (converter_missing) |
---|
244 | return -1;//no result |
---|
245 | return f0geno.isValid(); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | void Geno::validate() |
---|
250 | { |
---|
251 | if (isvalid >= 0) return; |
---|
252 | if (gen.len() == 0) { isvalid = 0; return; } |
---|
253 | if (format == INVALID_FORMAT) { isvalid = 0; return; } |
---|
254 | Validators* vals = getValidators(); |
---|
255 | if (vals != NULL) |
---|
256 | { |
---|
257 | #ifdef WARN_VALIDATION_INCONSISTENCY |
---|
258 | vector<int> results; |
---|
259 | int first_result = -1; |
---|
260 | FOREACH(GenoValidator*, v, (*vals)) |
---|
261 | { |
---|
262 | int r = v->testGenoValidity(*this); |
---|
263 | if (first_result < 0) first_result = r; |
---|
264 | results.push_back(r); |
---|
265 | } |
---|
266 | int N = vals->size(); |
---|
267 | for (int i = 1; i < N; i++) |
---|
268 | if (results[i] != results[0]) |
---|
269 | { |
---|
270 | SString txt = "Inconsistent validation results"; |
---|
271 | for (int i = 0; i < N; i++) |
---|
272 | txt += SString::sprintf(" %d", results[i]); |
---|
273 | txt += " for genotype '"; |
---|
274 | txt += getGene(); |
---|
275 | txt += "'"; |
---|
276 | logPrintf("Geno", "validate", LOG_WARN, txt.c_str()); |
---|
277 | break; |
---|
278 | } |
---|
279 | isvalid = first_result; |
---|
280 | if (isvalid >= 0) |
---|
281 | return; |
---|
282 | #else |
---|
283 | FOREACH(GenoValidator*, v, (*vals)) |
---|
284 | if ((isvalid = v->testGenoValidity(*this)) >= 0) |
---|
285 | return; |
---|
286 | #endif |
---|
287 | } |
---|
288 | isvalid = 0; |
---|
289 | logPrintf("Geno", "validate", LOG_WARN, "Wrong configuration? No genotype validators defined for genetic format 'f%s'.", format.c_str()); |
---|
290 | } |
---|
291 | |
---|
292 | bool Geno::isValid(void) |
---|
293 | { |
---|
294 | if (isvalid < 0) |
---|
295 | { |
---|
296 | LoggerToMemory err(LoggerBase::Enable | LoggerToMemory::StoreAllMessages, LOG_INFO); |
---|
297 | validate(); |
---|
298 | err.disable(); |
---|
299 | string msg = err.getCountSummary(); |
---|
300 | if (msg.size() > 0) |
---|
301 | { |
---|
302 | msg += ssprintf(" while checking validity of '%s'", getName().c_str()); |
---|
303 | msg += "\n"; |
---|
304 | msg += err.getMessages(); |
---|
305 | logMessage("Geno", "isValid", err.getErrorLevel(), msg.c_str()); |
---|
306 | } |
---|
307 | } |
---|
308 | return isvalid > 0; |
---|
309 | } |
---|
310 | |
---|
311 | Geno Geno::getConverted(SString otherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing) |
---|
312 | { |
---|
313 | if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; } |
---|
314 | #ifndef NO_GENOCONVMANAGER |
---|
315 | GenoConvManager *converters = getConverters(); |
---|
316 | if (converters) |
---|
317 | { |
---|
318 | if ((otherformat == "0") && (!m) && (!using_checkpoints)) |
---|
319 | { |
---|
320 | if (!f0gen) |
---|
321 | f0gen = new Geno(converters->convert(*this, otherformat, NULL, using_checkpoints, converter_missing)); |
---|
322 | else |
---|
323 | { |
---|
324 | if (converter_missing) *converter_missing = false; |
---|
325 | } |
---|
326 | return *f0gen; |
---|
327 | } |
---|
328 | else |
---|
329 | return converters->convert(*this, otherformat, m, using_checkpoints, converter_missing); |
---|
330 | } |
---|
331 | #endif |
---|
332 | if (converter_missing) *converter_missing = true; |
---|
333 | return (otherformat == getFormat()) ? *this : Geno("", "", "", "GenConvManager not available"); |
---|
334 | } |
---|
335 | |
---|
336 | Geno::~Geno() |
---|
337 | { |
---|
338 | if (f0gen) delete f0gen; |
---|
339 | } |
---|