Changeset 150 for cpp/frams/genetics/genoconv.cpp
- Timestamp:
- 03/01/14 22:05:02 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/genoconv.cpp
r145 r150 18 18 /////////////////////////////////////////////////////////////////////////// 19 19 20 GenoConvParam::GenoConvParam(GenoConvManager *g) :Param(0),gcm(g)21 { 22 updatetab();20 GenoConvParam::GenoConvParam(GenoConvManager *g) :Param(0), gcm(g) 21 { 22 updatetab(); 23 23 } 24 24 25 25 void GenoConvParam::freetab() 26 26 { 27 if (tab) free(tab);28 tab=0;27 if (tab) free(tab); 28 tab = 0; 29 29 } 30 30 31 31 const char *GenoConvParam::id(int i) 32 32 { 33 if (i>=gcm->converters.size()) return 0;34 sprintf(tmp_id,"genkonw%d",i);35 return tmp_id;33 if (i >= gcm->converters.size()) return 0; 34 sprintf(tmp_id, "genkonw%d", i); 35 return tmp_id; 36 36 } 37 37 38 38 void GenoConvParam::updatetab() 39 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=(short)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;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++) 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 68 } 69 69 70 70 GenoConvParam::~GenoConvParam() 71 71 { 72 freetab();72 freetab(); 73 73 } 74 74 75 75 void *GenoConvParam::getTarget(int i) 76 76 { 77 GenoConverter *gk=(GenoConverter *)gcm->converters(i);78 return &gk->enabled;77 GenoConverter *gk = (GenoConverter *)gcm->converters(i); 78 return &gk->enabled; 79 79 } 80 80 81 81 GenoConvManager::GenoConvManager() 82 82 :param(this) 83 83 { 84 84 } … … 86 86 GenoConvManager::~GenoConvManager() 87 87 { 88 GenoConverter *gc; 89 for (converters.start();gc=(GenoConverter*)converters();) delete gc; 88 FOREACH(GenoConverter*, gc, converters) delete gc; 90 89 } 91 90 92 91 void GenoConvManager::addConverter(GenoConverter *gc) 93 92 { 94 converters+=gc;95 param.updatetab();93 converters += gc; 94 param.updatetab(); 96 95 } 97 96 void GenoConvManager::removeConverter(GenoConverter *gc) 98 97 { 99 converters-=gc;100 param.updatetab();101 } 102 103 GenoConverter *GenoConvManager::findConverters(SListTempl<GenoConverter*>* result, char in,char out,int enabled,char* name)104 { 105 GenoConverter *gk,*retval=0;106 int i=0;107 for (;gk=(GenoConverter*)converters(i);i++)108 { 109 if ((in!=-1)&&(in!=gk->in_format)) continue;110 if ((out!=-1)&&(out!=gk->out_format)) continue;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);115 } 116 return retval;117 } 118 119 /// write path into 'path'98 converters -= gc; 99 param.updatetab(); 100 } 101 102 GenoConverter *GenoConvManager::findConverters(SListTempl<GenoConverter*>* result, char in, char out, int enabled, char* name) 103 { 104 GenoConverter *gk, *retval = 0; 105 int i = 0; 106 for (; gk = (GenoConverter*)converters(i); i++) 107 { 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); 114 } 115 return retval; 116 } 117 118 /// Writes path into 'path'. 120 119 /// return the last path element (return >= path) 121 120 /// null -> path not found … … 123 122 /// (can be NULL if you don't need this information) 124 123 125 char *GenoConvManager::getPath(char in, char out,char *path,int maxlen,int *mapavailable)126 { 127 if (!maxlen) return 0;128 GenoConverter *gk;129 int i=0;130 for (;gk=(GenoConverter*)converters(i);i++)131 { 132 if ((gk->enabled)&&(gk->in_format == in))124 char *GenoConvManager::getPath(char in, char out, char *path, int maxlen, int *mapavailable) 125 { 126 if (!maxlen) return 0; 127 GenoConverter *gk; 128 int i = 0; 129 for (; gk = (GenoConverter*)converters(i); i++) 130 { 131 if ((gk->enabled) && (gk->in_format == in)) 133 132 { 134 *path=(char)i;135 if (gk->out_format == out)136 { 137 if (mapavailable)138 *mapavailable=gk->mapsupport;139 return path;140 } 141 else142 { 143 int mapavail;144 char *ret=getPath(gk->out_format,out,path+1,maxlen-1,&mapavail);145 if (ret)133 *path = (char)i; 134 if (gk->out_format == out) 135 { 136 if (mapavailable) 137 *mapavailable = gk->mapsupport; 138 return path; 139 } 140 else 141 { 142 int mapavail; 143 char *ret = getPath(gk->out_format, out, path + 1, maxlen - 1, &mapavail); 144 if (ret) 146 145 { 147 if (mapavailable)148 *mapavailable=gk->mapsupport && mapavail;149 return ret;146 if (mapavailable) 147 *mapavailable = gk->mapsupport && mapavail; 148 return ret; 150 149 } 151 150 } 152 151 } 153 152 } 154 return 0; 155 } 156 157 char *GenoConvManager::getFormatPath(char in,char out,char *path,int maxlen,int *mapavailable) 158 { 159 char *ret=getPath(in,out,path,maxlen,mapavailable); 160 if (ret) 161 { 162 for (char*t=path;t<=ret;t++) 163 *t=((GenoConverter*)converters(*t))->out_format; 164 } 165 return ret; 166 } 167 168 Geno GenoConvManager::convert(Geno &in,char format,MultiMap *map) 169 { 170 if (in.getFormat()==format) return in; 171 char path[10]; 172 int dep; 173 char *ret; 174 if (in.isInvalid()) { return Geno("",0,"","invalid genotype cannot be converted"); } 175 int mapavail; 176 for (dep=1;dep<(int)sizeof(path);dep++) // sogenannte iteracyjne poglebianie... 177 if (ret=getPath(in.getFormat(),format,path,dep,&mapavail)) break; 178 if (!ret) { return Geno("",0,"","converter not found"); } 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++) 186 { 187 GenoConverter *gk=(GenoConverter*)converters(*t); 188 tmp=gk->convert(tmp,mapavail?&tmpmap:0); 189 if (!tmp.len()) 153 return 0; 154 } 155 156 char *GenoConvManager::getFormatPath(char in, char out, char *path, int maxlen, int *mapavailable) 157 { 158 char *ret = getPath(in, out, path, maxlen, mapavailable); 159 if (ret) 160 { 161 for (char*t = path; t <= ret; t++) 162 *t = ((GenoConverter*)converters(*t))->out_format; 163 } 164 return ret; 165 } 166 167 Geno GenoConvManager::convert(Geno &in, char format, MultiMap *map, bool *converter_missing) 168 { 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++) 186 { 187 GenoConverter *gk = (GenoConverter*)converters(*t); 188 tmp = gk->convert(tmp, mapavail ? &tmpmap : 0); 189 if (!tmp.len()) 190 190 { 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);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); 194 194 } 195 if (mapavail)195 if (mapavail) 196 196 { 197 if (firstmap)198 { 199 lastmap=tmpmap;200 firstmap=0;201 } 202 else203 { 204 MultiMap m;205 m.addCombined(lastmap,tmpmap);206 lastmap=m;207 } 208 tmpmap.clear();197 if (firstmap) 198 { 199 lastmap = tmpmap; 200 firstmap = 0; 201 } 202 else 203 { 204 MultiMap m; 205 m.addCombined(lastmap, tmpmap); 206 lastmap = m; 207 } 208 tmpmap.clear(); 209 209 } 210 210 } 211 if (map)212 *map=lastmap;213 return Geno(tmp, format, in.getName(), in.getComment());214 } 211 if (map) 212 *map = lastmap; 213 return Geno(tmp, format, in.getName(), in.getComment()); 214 }
Note: See TracChangeset
for help on using the changeset viewer.