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