Changeset 955 for cpp/frams/genetics/geno.cpp
- Timestamp:
- 06/25/20 00:34:29 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified cpp/frams/genetics/geno.cpp ¶
r841 r955 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 18Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 24 24 } 25 25 26 void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment) 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) 27 30 { 28 31 refcount = 1; … … 35 38 } 36 39 37 void Geno::setGenesAndFormat(const SString& genstring, char genformat) 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) 38 49 { 39 50 mapinshift = 0; 40 51 mapoutshift = 0; 41 52 SString gencopy(genstring); 42 if (genformat == -1) 53 SString genformat = in_genformat; 54 if (genformat == UNKNOWN_FORMAT) 43 55 { // unknown format 44 genformat = '1';56 genformat = "1"; 45 57 if (genstring.charAt(0) == '/') 46 58 { … … 49 61 { 50 62 case '/': 51 genformat = genstring.charAt(2);52 63 if ((end = genstring.indexOf('\n')) >= 0) 53 64 { 65 genformat = trimAndValidateFormat(genstring.substr(2, end - 2)); 54 66 mapinshift = end + 1; 55 67 gencopy = genstring.substr(end + 1); 56 68 if ((end > 0) && (genstring[end - 1] == '\r')) end--; 57 69 error_end = end; 58 if (end != 3) genformat = INVALID_FORMAT;59 70 } 60 71 else 61 72 { 62 if (genstring.len() != 3) genformat = INVALID_FORMAT;63 gencopy = 0;73 genformat = trimAndValidateFormat(genstring.substr(2)); 74 gencopy = ""; 64 75 mapinshift = genstring.len(); 65 76 } 66 77 break; 67 78 case '*': 68 genformat = genstring.charAt(2);69 79 if ((end = genstring.indexOf("*/")) >= 0) 70 80 { 81 genformat = trimAndValidateFormat(genstring.substr(2, end - 2)); 71 82 error_end = end + 2; 72 if (end != 3) genformat = INVALID_FORMAT;73 83 gencopy = genstring.substr(end + 2); 74 84 mapinshift = end + 2; … … 76 86 else 77 87 { 78 if (genstring.len() != 5) genformat = INVALID_FORMAT;79 gencopy = 0;88 genformat = trimAndValidateFormat(genstring.substr(2)); 89 gencopy = ""; 80 90 mapinshift = genstring.len(); 81 91 } 82 92 break; 83 93 } 84 if (!isalnum(genformat)) genformat = INVALID_FORMAT;85 94 if (genformat == INVALID_FORMAT) 86 95 { 87 96 SString cut; 88 if (error_end <0) error_end = genstring.len();97 if (error_end < 0) error_end = genstring.len(); 89 98 static const int MAX_ERROR = 20; 90 if (error_end >MAX_ERROR)99 if (error_end > MAX_ERROR) 91 100 cut = genstring.substr(0, MAX_ERROR) + "..."; 92 101 else … … 113 122 } 114 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 115 129 Geno::Geno(const char *genstring, char genformat, const char *genname, const char *comment) 116 130 { 117 init(SString(genstring), genformat, SString(genname), SString(comment)); 118 } 119 120 Geno::Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment) 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) 121 138 { 122 139 init(genstring, genformat, genname, comment); … … 148 165 Geno::Geno(const SString& src) 149 166 { 150 init(src, -1, SString::empty(), SString::empty());167 init(src, UNKNOWN_FORMAT, SString::empty(), SString::empty()); 151 168 } 152 169 … … 161 178 { 162 179 freeF0(); 163 init(g, -1, SString::empty(), SString::empty());180 init(g, UNKNOWN_FORMAT, SString::empty(), SString::empty()); 164 181 } 165 182 … … 177 194 { 178 195 SString out; 179 if (format != '1')196 if (format != "1") 180 197 { 181 198 if (multiline) … … 183 200 else 184 201 out += "/*"; 185 if (format == 0) 186 out += "invalid"; 187 else 188 out += format; 202 out += format; 189 203 if (multiline) 190 204 out += "\n"; … … 213 227 SString Geno::getGenes(void) const { return gen; } 214 228 SString Geno::getName(void) const { return name; } 215 charGeno::getFormat(void) const { return format; }229 SString Geno::getFormat(void) const { return format; } 216 230 SString Geno::getComment(void) const { return txt; } 217 231 218 232 int ModelGenoValidator::testGenoValidity(Geno& g) 219 233 { 220 if (g.getFormat() == '0')234 if (g.getFormat() == "0") 221 235 { 222 236 Model mod(g); … … 226 240 { 227 241 bool converter_missing; 228 Geno f0geno = g.getConverted( '0', NULL, false, &converter_missing);242 Geno f0geno = g.getConverted("0", NULL, false, &converter_missing); 229 243 if (converter_missing) 230 244 return -1;//no result … … 243 257 #ifdef WARN_VALIDATION_INCONSISTENCY 244 258 vector<int> results; 245 int first_result =-1;259 int first_result = -1; 246 260 FOREACH(GenoValidator*, v, (*vals)) 247 261 { 248 int r =v->testGenoValidity(*this);249 if (first_result <0) first_result=r;262 int r = v->testGenoValidity(*this); 263 if (first_result < 0) first_result = r; 250 264 results.push_back(r); 251 265 } 252 int N =vals->size();253 for (int i=1;i<N;i++)254 if (results[i] !=results[0])266 int N = vals->size(); 267 for (int i = 1; i < N; i++) 268 if (results[i] != results[0]) 255 269 { 256 SString txt="Inconsistent validation results";257 for(int i=0;i<N;i++)258 txt+=SString::sprintf(" %d",results[i]);259 txt+=" for genotype '";260 txt+=getGene();261 txt+="'";262 logPrintf("Geno","validate",LOG_WARN,txt.c_str());263 break;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; 264 278 } 265 isvalid =first_result;266 if (isvalid >=0)279 isvalid = first_result; 280 if (isvalid >= 0) 267 281 return; 268 282 #else … … 273 287 } 274 288 isvalid = 0; 275 logPrintf("Geno", "validate", LOG_WARN, "Wrong configuration? No genotype validators defined for genetic format 'f% c'.", format);289 logPrintf("Geno", "validate", LOG_WARN, "Wrong configuration? No genotype validators defined for genetic format 'f%s'.", format.c_str()); 276 290 } 277 291 … … 295 309 } 296 310 297 Geno Geno::getConverted( charotherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing)311 Geno Geno::getConverted(SString otherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing) 298 312 { 299 313 if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; } … … 302 316 if (converters) 303 317 { 304 if ((otherformat == '0') && (!m) && (!using_checkpoints))318 if ((otherformat == "0") && (!m) && (!using_checkpoints)) 305 319 { 306 320 if (!f0gen) … … 317 331 #endif 318 332 if (converter_missing) *converter_missing = true; 319 return (otherformat == getFormat()) ? *this : Geno( 0, 0, 0, "GenConvManager not available");333 return (otherformat == getFormat()) ? *this : Geno("", "", "", "GenConvManager not available"); 320 334 } 321 335
Note: See TracChangeset
for help on using the changeset viewer.