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