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