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