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