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