[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[955] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include <common/nonstd.h> |
---|
| 6 | |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | #include <math.h> |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <string.h> |
---|
| 11 | #include <ctype.h> |
---|
| 12 | #include <time.h> |
---|
| 13 | #include <errno.h> |
---|
| 14 | |
---|
| 15 | #include "genoconv.h" |
---|
| 16 | #include <frams/util/multimap.h> |
---|
[841] | 17 | #include <common/util-string.h> |
---|
[109] | 18 | |
---|
| 19 | /////////////////////////////////////////////////////////////////////////// |
---|
| 20 | |
---|
[150] | 21 | GenoConvParam::GenoConvParam(GenoConvManager *g) :Param(0), gcm(g) |
---|
[109] | 22 | { |
---|
[150] | 23 | updatetab(); |
---|
[109] | 24 | } |
---|
| 25 | |
---|
| 26 | void GenoConvParam::freetab() |
---|
| 27 | { |
---|
[150] | 28 | if (tab) free(tab); |
---|
| 29 | tab = 0; |
---|
[109] | 30 | } |
---|
| 31 | |
---|
| 32 | const char *GenoConvParam::id(int i) |
---|
| 33 | { |
---|
[150] | 34 | if (i >= gcm->converters.size()) return 0; |
---|
| 35 | sprintf(tmp_id, "genkonw%d", i); |
---|
| 36 | return tmp_id; |
---|
[109] | 37 | } |
---|
| 38 | |
---|
| 39 | void GenoConvParam::updatetab() |
---|
| 40 | { |
---|
[150] | 41 | int i; |
---|
| 42 | GenoConverter *gk; |
---|
| 43 | ParamEntry *pe; |
---|
| 44 | int ile = gcm->converters.size(); |
---|
| 45 | freetab(); |
---|
| 46 | tab = (ParamEntry*)calloc(2 + ile, sizeof(ParamEntry)); |
---|
| 47 | tab[0].id = "Genetics: Conversions"; |
---|
| 48 | tab[0].group = 1; |
---|
[483] | 49 | tab[0].flags = (paInt)ile; |
---|
[150] | 50 | tab[0].name = "gkparam:"; |
---|
| 51 | gcnames.clear(); |
---|
[783] | 52 | gcnames.reserve(gcm->converters.size()); //avoid reallocations in the loop below, since we externally store pointers to objects saved in this vector |
---|
[150] | 53 | for (i = 0, pe = tab + 1; gk = (GenoConverter *)gcm->converters(i); pe++, i++) |
---|
[109] | 54 | { |
---|
[150] | 55 | pe->id = "?"; |
---|
| 56 | pe->group = 0; |
---|
| 57 | pe->flags = 0; |
---|
| 58 | std::string descr = "f"; |
---|
[955] | 59 | descr += gk->in_format.c_str(); |
---|
[783] | 60 | descr += " --> f"; |
---|
[955] | 61 | descr += gk->out_format.c_str(); |
---|
[783] | 62 | descr += " : "; |
---|
[150] | 63 | descr += gk->name; |
---|
| 64 | gcnames.push_back(descr); |
---|
[783] | 65 | pe->name = gcnames.back().c_str(); //externally store a pointer to the object just saved in the vector |
---|
[150] | 66 | pe->type = "d 0 1"; |
---|
[109] | 67 | } |
---|
[150] | 68 | pe->id = 0; |
---|
[109] | 69 | } |
---|
| 70 | |
---|
| 71 | GenoConvParam::~GenoConvParam() |
---|
| 72 | { |
---|
[150] | 73 | freetab(); |
---|
[109] | 74 | } |
---|
| 75 | |
---|
| 76 | void *GenoConvParam::getTarget(int i) |
---|
| 77 | { |
---|
[150] | 78 | GenoConverter *gk = (GenoConverter *)gcm->converters(i); |
---|
| 79 | return &gk->enabled; |
---|
[109] | 80 | } |
---|
| 81 | |
---|
| 82 | GenoConvManager::GenoConvManager() |
---|
[732] | 83 | :param(this) |
---|
[109] | 84 | { |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | GenoConvManager::~GenoConvManager() |
---|
| 88 | { |
---|
[150] | 89 | FOREACH(GenoConverter*, gc, converters) delete gc; |
---|
[109] | 90 | } |
---|
| 91 | |
---|
| 92 | void GenoConvManager::addConverter(GenoConverter *gc) |
---|
| 93 | { |
---|
[150] | 94 | converters += gc; |
---|
| 95 | param.updatetab(); |
---|
[109] | 96 | } |
---|
| 97 | void GenoConvManager::removeConverter(GenoConverter *gc) |
---|
| 98 | { |
---|
[150] | 99 | converters -= gc; |
---|
| 100 | param.updatetab(); |
---|
[109] | 101 | } |
---|
| 102 | |
---|
[955] | 103 | GenoConverter *GenoConvManager::findConverters(SListTempl<GenoConverter*>* result, const SString& in, const SString& out, int enabled, char* name) |
---|
[109] | 104 | { |
---|
[150] | 105 | GenoConverter *gk, *retval = 0; |
---|
| 106 | int i = 0; |
---|
| 107 | for (; gk = (GenoConverter*)converters(i); i++) |
---|
[109] | 108 | { |
---|
[988] | 109 | if ((in != Geno::FORMAT_UNKNOWN) && (in != gk->in_format)) continue; |
---|
| 110 | if ((out != Geno::FORMAT_UNKNOWN) && (out != gk->out_format)) continue; |
---|
[150] | 111 | if ((enabled != -1) && (enabled != gk->enabled)) continue; |
---|
| 112 | if ((name) && (strcmp(name, gk->name))) continue; |
---|
| 113 | if (!retval) { retval = gk; if (!result) break; } |
---|
| 114 | if (result) result->append(gk); |
---|
[109] | 115 | } |
---|
[150] | 116 | return retval; |
---|
[109] | 117 | } |
---|
| 118 | |
---|
[150] | 119 | /// Writes path into 'path'. |
---|
[109] | 120 | /// return the last path element (return >= path) |
---|
| 121 | /// null -> path not found |
---|
| 122 | /// @param mapavailable will receive 1 if conversion map is supported by all converters in path |
---|
| 123 | /// (can be NULL if you don't need this information) |
---|
| 124 | |
---|
[972] | 125 | GenoConverter **GenoConvManager::getPath(const SString& in_format, const SString& out_format_list, GenoConverter **path, int maxlen, int *mapavailable) |
---|
[109] | 126 | { |
---|
[150] | 127 | if (!maxlen) return 0; |
---|
| 128 | GenoConverter *gk; |
---|
| 129 | int i = 0; |
---|
| 130 | for (; gk = (GenoConverter*)converters(i); i++) |
---|
[109] | 131 | { |
---|
[972] | 132 | if ((gk->enabled) && (gk->in_format == in_format)) |
---|
[109] | 133 | { |
---|
[955] | 134 | *path = gk; |
---|
[972] | 135 | if (Geno::formatIsOneOf(gk->out_format, out_format_list)) |
---|
[109] | 136 | { |
---|
[150] | 137 | if (mapavailable) |
---|
| 138 | *mapavailable = gk->mapsupport; |
---|
| 139 | return path; |
---|
[109] | 140 | } |
---|
[150] | 141 | else |
---|
[109] | 142 | { |
---|
[150] | 143 | int mapavail; |
---|
[972] | 144 | GenoConverter **ret = getPath(gk->out_format, out_format_list, path + 1, maxlen - 1, &mapavail); |
---|
[150] | 145 | if (ret) |
---|
[109] | 146 | { |
---|
[150] | 147 | if (mapavailable) |
---|
| 148 | *mapavailable = gk->mapsupport && mapavail; |
---|
| 149 | return ret; |
---|
[109] | 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
[150] | 154 | return 0; |
---|
[109] | 155 | } |
---|
| 156 | |
---|
[972] | 157 | Geno GenoConvManager::convert(Geno &in, SString format_list, MultiMap *map, bool using_checkpoints, bool *converter_missing) |
---|
[109] | 158 | { |
---|
[972] | 159 | if (Geno::formatIsOneOf(in.getFormat(), format_list)) |
---|
| 160 | { |
---|
[981] | 161 | if (converter_missing) *converter_missing = false; |
---|
| 162 | return in; |
---|
[972] | 163 | } |
---|
[955] | 164 | GenoConverter *path[10]; |
---|
[150] | 165 | int dep; |
---|
[955] | 166 | GenoConverter **ret; |
---|
[981] | 167 | if (in.isInvalid()) |
---|
| 168 | { |
---|
| 169 | if (converter_missing) *converter_missing = false; |
---|
[988] | 170 | return Geno("", Geno::FORMAT_INVALID, "", "Invalid genotype cannot be converted"); |
---|
[981] | 171 | } |
---|
[150] | 172 | int mapavail; |
---|
| 173 | for (dep = 1; dep < (int)sizeof(path); dep++) //iterative deepening |
---|
[972] | 174 | if (ret = getPath(in.getFormat(), format_list, path, dep, &mapavail)) break; |
---|
[981] | 175 | if (!ret) |
---|
| 176 | { |
---|
| 177 | if (converter_missing) *converter_missing = true; |
---|
[988] | 178 | return Geno("", Geno::FORMAT_INVALID, "", "Converter not found"); |
---|
[981] | 179 | } |
---|
[150] | 180 | if (converter_missing) *converter_missing = false; |
---|
| 181 | if (!map) mapavail = 0; |
---|
[955] | 182 | GenoConverter **t = path; |
---|
[972] | 183 | SString tmp, out_format; |
---|
[534] | 184 | tmp = in.getGenes(); |
---|
[150] | 185 | MultiMap lastmap, tmpmap; |
---|
| 186 | int firstmap = 1; |
---|
| 187 | for (; t <= ret; t++) |
---|
[109] | 188 | { |
---|
[955] | 189 | GenoConverter *gk = *t; |
---|
[732] | 190 | tmp = gk->convert(tmp, mapavail ? &tmpmap : 0, using_checkpoints); |
---|
[972] | 191 | out_format = gk->out_format; |
---|
| 192 | if (!tmp.length()) |
---|
[109] | 193 | { |
---|
[955] | 194 | string t = ssprintf("f%s->f%s conversion failed (%s)", gk->in_format.c_str(), gk->out_format.c_str(), gk->name); |
---|
[988] | 195 | return Geno("", Geno::FORMAT_INVALID, "", t.c_str()); |
---|
[109] | 196 | } |
---|
[150] | 197 | if (mapavail) |
---|
[109] | 198 | { |
---|
[150] | 199 | if (firstmap) |
---|
[109] | 200 | { |
---|
[150] | 201 | lastmap = tmpmap; |
---|
| 202 | firstmap = 0; |
---|
[109] | 203 | } |
---|
[150] | 204 | else |
---|
[109] | 205 | { |
---|
[150] | 206 | MultiMap m; |
---|
| 207 | m.addCombined(lastmap, tmpmap); |
---|
| 208 | lastmap = m; |
---|
[109] | 209 | } |
---|
[150] | 210 | tmpmap.clear(); |
---|
[109] | 211 | } |
---|
| 212 | } |
---|
[150] | 213 | if (map) |
---|
| 214 | *map = lastmap; |
---|
[972] | 215 | return Geno(tmp, out_format, in.getName(), in.getComment()); |
---|
[109] | 216 | } |
---|