Ignore:
Timestamp:
06/25/20 00:34:29 (5 years ago)
Author:
Maciej Komosinski
Message:

Genetic format ID becomes a string (no longer limited to a single character)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified cpp/frams/genetics/geno.cpp

    r841 r955  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2424}
    2525
    26 void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment)
     26const SString Geno::INVALID_FORMAT = "invalid";
     27const SString Geno::UNKNOWN_FORMAT = "";
     28
     29void Geno::init(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment)
    2730{
    2831        refcount = 1;
     
    3538}
    3639
    37 void Geno::setGenesAndFormat(const SString& genstring, char genformat)
     40static 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
     48void Geno::setGenesAndFormat(const SString& genstring, const SString& in_genformat)
    3849{
    3950        mapinshift = 0;
    4051        mapoutshift = 0;
    4152        SString gencopy(genstring);
    42         if (genformat == -1)
     53        SString genformat = in_genformat;
     54        if (genformat == UNKNOWN_FORMAT)
    4355        { // unknown format
    44                 genformat = '1';
     56                genformat = "1";
    4557                if (genstring.charAt(0) == '/')
    4658                {
     
    4961                        {
    5062                        case '/':
    51                                 genformat = genstring.charAt(2);
    5263                                if ((end = genstring.indexOf('\n')) >= 0)
    5364                                {
     65                                        genformat = trimAndValidateFormat(genstring.substr(2, end - 2));
    5466                                        mapinshift = end + 1;
    5567                                        gencopy = genstring.substr(end + 1);
    5668                                        if ((end > 0) && (genstring[end - 1] == '\r')) end--;
    5769                                        error_end = end;
    58                                         if (end != 3) genformat = INVALID_FORMAT;
    5970                                }
    6071                                else
    6172                                {
    62                                         if (genstring.len() != 3) genformat = INVALID_FORMAT;
    63                                         gencopy = 0;
     73                                        genformat = trimAndValidateFormat(genstring.substr(2));
     74                                        gencopy = "";
    6475                                        mapinshift = genstring.len();
    6576                                }
    6677                                break;
    6778                        case '*':
    68                                 genformat = genstring.charAt(2);
    6979                                if ((end = genstring.indexOf("*/")) >= 0)
    7080                                {
     81                                        genformat = trimAndValidateFormat(genstring.substr(2, end - 2));
    7182                                        error_end = end + 2;
    72                                         if (end != 3) genformat = INVALID_FORMAT;
    7383                                        gencopy = genstring.substr(end + 2);
    7484                                        mapinshift = end + 2;
     
    7686                                else
    7787                                {
    78                                         if (genstring.len() != 5) genformat = INVALID_FORMAT;
    79                                         gencopy = 0;
     88                                        genformat = trimAndValidateFormat(genstring.substr(2));
     89                                        gencopy = "";
    8090                                        mapinshift = genstring.len();
    8191                                }
    8292                                break;
    8393                        }
    84                         if (!isalnum(genformat)) genformat = INVALID_FORMAT;
    8594                        if (genformat == INVALID_FORMAT)
    8695                        {
    8796                                SString cut;
    88                                 if (error_end<0) error_end = genstring.len();
     97                                if (error_end < 0) error_end = genstring.len();
    8998                                static const int MAX_ERROR = 20;
    90                                 if (error_end>MAX_ERROR)
     99                                if (error_end > MAX_ERROR)
    91100                                        cut = genstring.substr(0, MAX_ERROR) + "...";
    92101                                else
     
    113122}
    114123
     124Geno::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
    115129Geno::Geno(const char *genstring, char genformat, const char *genname, const char *comment)
    116130{
    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
     137Geno::Geno(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment)
    121138{
    122139        init(genstring, genformat, genname, comment);
     
    148165Geno::Geno(const SString& src)
    149166{
    150         init(src, -1, SString::empty(), SString::empty());
     167        init(src, UNKNOWN_FORMAT, SString::empty(), SString::empty());
    151168}
    152169
     
    161178{
    162179        freeF0();
    163         init(g, -1, SString::empty(), SString::empty());
     180        init(g, UNKNOWN_FORMAT, SString::empty(), SString::empty());
    164181}
    165182
     
    177194{
    178195        SString out;
    179         if (format != '1')
     196        if (format != "1")
    180197        {
    181198                if (multiline)
     
    183200                else
    184201                        out += "/*";
    185                 if (format == 0)
    186                         out += "invalid";
    187                 else
    188                         out += format;
     202                out += format;
    189203                if (multiline)
    190204                        out += "\n";
     
    213227SString Geno::getGenes(void) const { return gen; }
    214228SString Geno::getName(void) const { return name; }
    215 char Geno::getFormat(void) const { return format; }
     229SString Geno::getFormat(void) const { return format; }
    216230SString Geno::getComment(void) const { return txt; }
    217231
    218232int ModelGenoValidator::testGenoValidity(Geno& g)
    219233{
    220         if (g.getFormat() == '0')
     234        if (g.getFormat() == "0")
    221235        {
    222236                Model mod(g);
     
    226240        {
    227241                bool converter_missing;
    228                 Geno f0geno = g.getConverted('0', NULL, false, &converter_missing);
     242                Geno f0geno = g.getConverted("0", NULL, false, &converter_missing);
    229243                if (converter_missing)
    230244                        return -1;//no result
     
    243257#ifdef WARN_VALIDATION_INCONSISTENCY
    244258                vector<int> results;
    245                 int first_result=-1;
     259                int first_result = -1;
    246260                FOREACH(GenoValidator*, v, (*vals))
    247261                {
    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;
    250264                        results.push_back(r);
    251265                }
    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])
    255269                        {
    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;
    264278                        }
    265                 isvalid=first_result;
    266                 if (isvalid>=0)
     279                isvalid = first_result;
     280                if (isvalid >= 0)
    267281                        return;
    268282#else
     
    273287        }
    274288        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());
    276290}
    277291
     
    295309}
    296310
    297 Geno Geno::getConverted(char otherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing)
     311Geno Geno::getConverted(SString otherformat, MultiMap *m, bool using_checkpoints, bool *converter_missing)
    298312{
    299313        if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; }
     
    302316        if (converters)
    303317        {
    304                 if ((otherformat == '0') && (!m) && (!using_checkpoints))
     318                if ((otherformat == "0") && (!m) && (!using_checkpoints))
    305319                {
    306320                        if (!f0gen)
     
    317331#endif
    318332        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");
    320334}
    321335
Note: See TracChangeset for help on using the changeset viewer.