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 <frams/model/model.h> |
---|
8 | |
---|
9 | THREAD_LOCAL_DEF_PTR(Geno::Validators, geno_validators); |
---|
10 | THREAD_LOCAL_DEF_PTR(GenoConvManager, geno_converters); |
---|
11 | |
---|
12 | Geno::Validators* Geno::getValidators() {return tlsGetPtr(geno_validators);} |
---|
13 | GenoConvManager* Geno::getConverters() {return tlsGetPtr(geno_converters);} |
---|
14 | |
---|
15 | Geno::Validators* Geno::useValidators(Validators* val) |
---|
16 | {return tlsSetPtr(geno_validators,val);} |
---|
17 | GenoConvManager* Geno::useConverters(GenoConvManager* gcm) |
---|
18 | {return tlsSetPtr(geno_converters,gcm);} |
---|
19 | |
---|
20 | void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment) |
---|
21 | { |
---|
22 | refcount = 1; |
---|
23 | owner = 0; |
---|
24 | f0gen = 0; |
---|
25 | mapinshift = 0; |
---|
26 | mapoutshift = 0; |
---|
27 | isvalid = -1; |
---|
28 | SString gencopy(genstring); |
---|
29 | if (genformat == -1) |
---|
30 | { // unknown format |
---|
31 | genformat = '1'; |
---|
32 | if (genstring.charAt(0) == '/') |
---|
33 | { |
---|
34 | int end; |
---|
35 | SString newcomment; |
---|
36 | switch (genstring.charAt(1)) |
---|
37 | { |
---|
38 | case '/': |
---|
39 | genformat = genstring.charAt(2); |
---|
40 | if ((end = genstring.indexOf('\n')) >= 0) |
---|
41 | { |
---|
42 | newcomment = genstring.substr(2, end - 2); |
---|
43 | gencopy = genstring.substr(end + 1); |
---|
44 | mapinshift = end + 1; |
---|
45 | } |
---|
46 | else |
---|
47 | { |
---|
48 | gencopy = 0; |
---|
49 | mapinshift = genstring.len(); |
---|
50 | } |
---|
51 | break; |
---|
52 | case '*': |
---|
53 | genformat = genstring.charAt(2); |
---|
54 | if ((end = genstring.indexOf("*/")) >= 0) |
---|
55 | { |
---|
56 | newcomment = genstring.substr(2, end - 2); |
---|
57 | gencopy = genstring.substr(end + 2); |
---|
58 | mapinshift = end + 2; |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | gencopy = 0; |
---|
63 | mapinshift = genstring.len(); |
---|
64 | } |
---|
65 | break; |
---|
66 | } |
---|
67 | if (newcomment.len() > 0) |
---|
68 | { |
---|
69 | SString token; int pos = 0; |
---|
70 | if (newcomment.getNextToken(pos, token, ';')) |
---|
71 | if (newcomment.getNextToken(pos, token, ';')) |
---|
72 | { |
---|
73 | if (token.len()) txt = token; |
---|
74 | if (newcomment.getNextToken(pos, token, ';')) |
---|
75 | if (token.len()) name = token; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | gen = gencopy; |
---|
82 | format = genformat; |
---|
83 | if (!name.len()) name = genname; |
---|
84 | if (!txt.len()) txt = comment; |
---|
85 | multiline = (strchr(gen.c_str(), '\n') != 0); |
---|
86 | // mapoutshift...? |
---|
87 | } |
---|
88 | |
---|
89 | void Geno::freeF0() |
---|
90 | { |
---|
91 | if (f0gen) { delete f0gen; f0gen = 0; } |
---|
92 | } |
---|
93 | |
---|
94 | Geno::Geno(const char *genstring, char genformat, const char *genname, const char *comment) |
---|
95 | { |
---|
96 | init(SString(genstring), genformat, SString(genname), SString(comment)); |
---|
97 | } |
---|
98 | |
---|
99 | Geno::Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment) |
---|
100 | { |
---|
101 | init(genstring, genformat, genname, comment); |
---|
102 | } |
---|
103 | |
---|
104 | Geno::Geno(const Geno& src) |
---|
105 | :gen(src.gen), name(src.name), format(src.format), txt(src.txt), isvalid(src.isvalid), |
---|
106 | f0gen(0), mapinshift(src.mapinshift), mapoutshift(src.mapinshift), |
---|
107 | multiline(src.multiline), owner(0) |
---|
108 | { |
---|
109 | f0gen = src.f0gen ? new Geno(*src.f0gen) : 0; refcount = 1; |
---|
110 | } |
---|
111 | |
---|
112 | void Geno::operator=(const Geno& src) |
---|
113 | { |
---|
114 | freeF0(); |
---|
115 | gen = src.gen; |
---|
116 | name = src.name; |
---|
117 | format = src.format; |
---|
118 | txt = src.txt; |
---|
119 | isvalid = src.isvalid; |
---|
120 | mapinshift = src.mapinshift; |
---|
121 | mapoutshift = src.mapinshift; |
---|
122 | multiline = src.multiline; |
---|
123 | f0gen = src.f0gen ? new Geno(*src.f0gen) : 0; |
---|
124 | owner = 0; |
---|
125 | } |
---|
126 | |
---|
127 | Geno::Geno(const SString& src) |
---|
128 | { |
---|
129 | init(src, -1, SString::empty(), SString::empty()); |
---|
130 | } |
---|
131 | |
---|
132 | void Geno::setGene(const SString& g, char newformat) |
---|
133 | { |
---|
134 | gen = g; |
---|
135 | isvalid = -1; |
---|
136 | freeF0(); |
---|
137 | if (newformat >= 0) format = newformat; |
---|
138 | } |
---|
139 | |
---|
140 | void Geno::setString(const SString& g) |
---|
141 | { |
---|
142 | freeF0(); |
---|
143 | init(g, -1, SString::empty(), SString::empty()); |
---|
144 | } |
---|
145 | |
---|
146 | void Geno::setName(const SString& n) |
---|
147 | { |
---|
148 | name = n; |
---|
149 | } |
---|
150 | |
---|
151 | void Geno::setComment(const SString& c) |
---|
152 | { |
---|
153 | txt = c; |
---|
154 | } |
---|
155 | |
---|
156 | SString Geno::toString(void) const |
---|
157 | { |
---|
158 | SString out; |
---|
159 | int comment = 0; |
---|
160 | if ((format != '1') || (comment = (txt.len() || name.len()))) |
---|
161 | { |
---|
162 | if (multiline) |
---|
163 | out += "//"; |
---|
164 | else |
---|
165 | out += "/*"; |
---|
166 | out += format; |
---|
167 | if (comment) |
---|
168 | { |
---|
169 | if (txt.len()) { out += ";"; out += txt; } |
---|
170 | if (name.len()){ out += ";"; out += name; } |
---|
171 | } |
---|
172 | if (multiline) |
---|
173 | out += "\n"; |
---|
174 | else |
---|
175 | out += "*/"; |
---|
176 | } |
---|
177 | out += gen; |
---|
178 | return out; |
---|
179 | } |
---|
180 | |
---|
181 | SString Geno::shortString(void) const |
---|
182 | { |
---|
183 | SString out; |
---|
184 | if (format != '1') |
---|
185 | { |
---|
186 | if (multiline) |
---|
187 | out += "//"; |
---|
188 | else |
---|
189 | out += "/*"; |
---|
190 | if (format == 0) |
---|
191 | out += "invalid"; |
---|
192 | else |
---|
193 | out += format; |
---|
194 | if (multiline) |
---|
195 | out += "\n"; |
---|
196 | else |
---|
197 | out += "*/"; |
---|
198 | } |
---|
199 | out += gen; |
---|
200 | return out; |
---|
201 | } |
---|
202 | |
---|
203 | int Geno::mapGenToString(int genpos) const |
---|
204 | { |
---|
205 | if (genpos > gen.len()) return -2; |
---|
206 | if (genpos<0) return -1; |
---|
207 | return mapinshift + genpos; |
---|
208 | } |
---|
209 | |
---|
210 | int Geno::mapStringToGen(int stringpos) const |
---|
211 | { |
---|
212 | stringpos -= mapinshift; |
---|
213 | if (stringpos>gen.len()) return -2; |
---|
214 | if (stringpos < 0) return -1; |
---|
215 | return stringpos; |
---|
216 | } |
---|
217 | |
---|
218 | SString Geno::getGene(void) const { return gen; } |
---|
219 | SString Geno::getName(void) const { return name; } |
---|
220 | char Geno::getFormat(void) const { return format; } |
---|
221 | SString Geno::getComment(void) const { return txt; } |
---|
222 | |
---|
223 | int ModelGenoValidator::testGenoValidity(Geno& g) |
---|
224 | { |
---|
225 | if (g.getFormat() == '0') |
---|
226 | { |
---|
227 | Model mod(g); |
---|
228 | return mod.isValid(); |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | bool converter_missing; |
---|
233 | Geno f0geno = g.getConverted('0', NULL, &converter_missing); |
---|
234 | if (converter_missing) |
---|
235 | return -1;//no result |
---|
236 | return f0geno.isValid(); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | void Geno::validate() |
---|
241 | { |
---|
242 | if (isvalid >= 0) return; |
---|
243 | if (gen.len() == 0) { isvalid = 0; return; } |
---|
244 | Validators* vals=getValidators(); |
---|
245 | if (vals!=NULL) |
---|
246 | { |
---|
247 | FOREACH(GenoValidator*, v, (*vals)) |
---|
248 | if ((isvalid = v->testGenoValidity(*this)) >= 0) |
---|
249 | return; |
---|
250 | } |
---|
251 | isvalid = 0; |
---|
252 | FMprintf("Geno", "validate", FMLV_WARN, "Wrong configuration? No genotype validators defined for genetic format f%c.", format); |
---|
253 | } |
---|
254 | |
---|
255 | bool Geno::isValid(void) |
---|
256 | { |
---|
257 | if (isvalid<0) validate(); |
---|
258 | return isvalid>0; |
---|
259 | } |
---|
260 | |
---|
261 | Geno Geno::getConverted(char otherformat, MultiMap *m, bool *converter_missing) |
---|
262 | { |
---|
263 | if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; } |
---|
264 | #ifndef NO_GENOCONVMANAGER |
---|
265 | GenoConvManager *converters=getConverters(); |
---|
266 | if (converters) |
---|
267 | { |
---|
268 | if ((otherformat == '0') && (!m)) |
---|
269 | { |
---|
270 | if (!f0gen) |
---|
271 | f0gen = new Geno(converters->convert(*this, otherformat, NULL, converter_missing)); |
---|
272 | return *f0gen; |
---|
273 | } |
---|
274 | else |
---|
275 | return converters->convert(*this, otherformat, m, converter_missing); |
---|
276 | } |
---|
277 | #endif |
---|
278 | if (converter_missing) *converter_missing = true; |
---|
279 | return (otherformat == getFormat()) ? *this : Geno(0, 0, 0, "GenConvManager not available"); |
---|
280 | } |
---|
281 | |
---|
282 | Geno::~Geno() |
---|
283 | { |
---|
284 | if (f0gen) delete f0gen; |
---|
285 | } |
---|