| 1 | // ==================================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : MG_gene_species.cxx // |
|---|
| 4 | // Purpose : Transfer fields from organism and gene when // |
|---|
| 5 | // transferring gene species // |
|---|
| 6 | // // |
|---|
| 7 | // // |
|---|
| 8 | // Coded by Ralf Westram (coder@reallysoft.de) in July 2002 // |
|---|
| 9 | // Copyright Department of Microbiology (Technical University Munich) // |
|---|
| 10 | // // |
|---|
| 11 | // Visit our web site at: http://www.arb-home.de/ // |
|---|
| 12 | // // |
|---|
| 13 | // // |
|---|
| 14 | // ==================================================================== // |
|---|
| 15 | |
|---|
| 16 | #include "merge.hxx" |
|---|
| 17 | |
|---|
| 18 | #include <awt_config_manager.hxx> |
|---|
| 19 | #include <aw_awar.hxx> |
|---|
| 20 | #include <aw_root.hxx> |
|---|
| 21 | #include <aw_msg.hxx> |
|---|
| 22 | #include <aw_select.hxx> |
|---|
| 23 | #include <arbdbt.h> |
|---|
| 24 | #include <arb_str.h> |
|---|
| 25 | #include <gb_aci.h> |
|---|
| 26 | |
|---|
| 27 | #define AWAR_MERGE_GENE_SPECIES_SAV AWAR_MERGE_SAV "gene_species/" |
|---|
| 28 | #define AWAR_MERGE_GENE_SPECIES_TMP AWAR_MERGE_TMP "gene_species/" |
|---|
| 29 | |
|---|
| 30 | // for input : |
|---|
| 31 | |
|---|
| 32 | #define AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD AWAR_MERGE_GENE_SPECIES_TMP "current" |
|---|
| 33 | #define AWAR_MERGE_GENE_SPECIES_DEST AWAR_MERGE_GENE_SPECIES_TMP "dest" |
|---|
| 34 | #define AWAR_MERGE_GENE_SPECIES_SOURCE AWAR_MERGE_GENE_SPECIES_TMP "source" |
|---|
| 35 | #define AWAR_MERGE_GENE_SPECIES_METHOD AWAR_MERGE_GENE_SPECIES_TMP "method" |
|---|
| 36 | #define AWAR_MERGE_GENE_SPECIES_ACI AWAR_MERGE_GENE_SPECIES_TMP "aci" |
|---|
| 37 | #define AWAR_MERGE_GENE_SPECIES_EXAMPLE AWAR_MERGE_GENE_SPECIES_TMP "example" |
|---|
| 38 | #define AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE AWAR_MERGE_GENE_SPECIES_TMP "save" // only used to save/load config |
|---|
| 39 | |
|---|
| 40 | // saved awars : |
|---|
| 41 | #define AWAR_MERGE_GENE_SPECIES_CREATE_FIELDS AWAR_MERGE_GENE_SPECIES_SAV "activated" |
|---|
| 42 | #define AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS AWAR_MERGE_GENE_SPECIES_SAV "field_defs" |
|---|
| 43 | |
|---|
| 44 | enum CreationMethod { |
|---|
| 45 | MG_CREATE_COPY_ORGANISM, |
|---|
| 46 | MG_CREATE_COPY_GENE, |
|---|
| 47 | MG_CREATE_USING_ACI_ONLY |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | static AW_default MG_props = NULp; // pointer current applications properties database |
|---|
| 51 | |
|---|
| 52 | void MG_create_gene_species_awars(AW_root *aw_root, AW_default aw_def) { |
|---|
| 53 | aw_root->awar_int(AWAR_MERGE_GENE_SPECIES_METHOD, 0, aw_def); |
|---|
| 54 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_ACI, "", aw_def); |
|---|
| 55 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD, "", aw_def); |
|---|
| 56 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_DEST, "", aw_def); |
|---|
| 57 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_EXAMPLE, "", aw_def); |
|---|
| 58 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_SOURCE, "", aw_def); |
|---|
| 59 | |
|---|
| 60 | aw_root->awar_int(AWAR_MERGE_GENE_SPECIES_CREATE_FIELDS, 0, aw_def); |
|---|
| 61 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS, ";", aw_def); |
|---|
| 62 | aw_root->awar_string(AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE, "", aw_def); |
|---|
| 63 | |
|---|
| 64 | MG_props = aw_def; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | #define BUFSIZE 100 |
|---|
| 68 | |
|---|
| 69 | inline char *strcpydest(char *dest, const char *src) { |
|---|
| 70 | // like strcpy, but returns pointer to zero terminator |
|---|
| 71 | int i = 0; |
|---|
| 72 | while (src[i]) { |
|---|
| 73 | dest[i] = src[i]; |
|---|
| 74 | i++; |
|---|
| 75 | } |
|---|
| 76 | dest[i] = 0; |
|---|
| 77 | return dest+i; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | inline const char *field_awar(const char *field_name, const char *subfield) { |
|---|
| 81 | static char buffer[BUFSIZE]; |
|---|
| 82 | |
|---|
| 83 | char *end = strcpydest(strcpydest(buffer, AWAR_MERGE_GENE_SPECIES_SAV"def_"), field_name); |
|---|
| 84 | *end++ = '/'; |
|---|
| 85 | IF_DEBUG(end=) strcpydest(end, subfield); |
|---|
| 86 | |
|---|
| 87 | mg_assert((end-buffer)<BUFSIZE); |
|---|
| 88 | |
|---|
| 89 | return buffer; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | inline const char *current_field_awar(AW_root *aw_root, const char *subfield) { |
|---|
| 93 | static char *cur_field = NULp; |
|---|
| 94 | |
|---|
| 95 | freeset(cur_field, aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->read_string()); |
|---|
| 96 | |
|---|
| 97 | if (cur_field[0]) return field_awar(cur_field, subfield); |
|---|
| 98 | return NULp; // no field definition selected |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | static void create_awars_for_field(const char *cur_field) { |
|---|
| 102 | // Note : MG_current_field_def_changed_cb also creates these awars! |
|---|
| 103 | AW_root *aw_root = AW_root::SINGLETON; |
|---|
| 104 | aw_root->awar_string(field_awar(cur_field, "source"), cur_field, MG_props); |
|---|
| 105 | aw_root->awar_int(field_awar(cur_field, "method"), 1, MG_props); |
|---|
| 106 | aw_root->awar_string(field_awar(cur_field, "aci"), "", MG_props); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | static char *MG_create_field_content(GBDATA *gb_species, CreationMethod method, const char *origins_field, const char *aci, GB_ERROR& error, GB_HASH *organism_hash) { |
|---|
| 110 | // does not write to database (only creates the content) |
|---|
| 111 | mg_assert(GEN_is_pseudo_gene_species(gb_species)); |
|---|
| 112 | |
|---|
| 113 | char *result = NULp; |
|---|
| 114 | GBDATA *gb_origin = NULp; |
|---|
| 115 | |
|---|
| 116 | switch (method) { |
|---|
| 117 | case MG_CREATE_COPY_ORGANISM: |
|---|
| 118 | gb_origin = GEN_find_origin_organism(gb_species, organism_hash); |
|---|
| 119 | if (!gb_origin) { |
|---|
| 120 | error = GBS_global_string("Origin-Organism '%s' not found", GEN_origin_organism(gb_species)); |
|---|
| 121 | } |
|---|
| 122 | break; |
|---|
| 123 | case MG_CREATE_COPY_GENE: |
|---|
| 124 | gb_origin = GEN_find_origin_gene(gb_species, organism_hash); |
|---|
| 125 | if (!gb_origin) { |
|---|
| 126 | error = GBS_global_string("Origin-Gene '%s/%s' not found", |
|---|
| 127 | GEN_origin_organism(gb_species), GEN_origin_gene(gb_species)); |
|---|
| 128 | } |
|---|
| 129 | break; |
|---|
| 130 | case MG_CREATE_USING_ACI_ONLY: |
|---|
| 131 | break; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | if (gb_origin) { // read source field |
|---|
| 135 | if (origins_field[0]) { |
|---|
| 136 | GBDATA *gb_field = GB_entry(gb_origin, origins_field); |
|---|
| 137 | if (!gb_field) { |
|---|
| 138 | error = GBS_global_string("Field not found: '%s'", origins_field); |
|---|
| 139 | } |
|---|
| 140 | else { |
|---|
| 141 | result = GB_read_as_string(gb_field); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | else { |
|---|
| 145 | error = "Specify a 'Source field'"; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if (!error) { |
|---|
| 150 | char *aci_result = NULp; |
|---|
| 151 | |
|---|
| 152 | GBL_env env(GLOBAL_gb_src, NULp); |
|---|
| 153 | |
|---|
| 154 | if (method == MG_CREATE_USING_ACI_ONLY) { |
|---|
| 155 | mg_assert(!result); |
|---|
| 156 | GBL_call_env callEnv(gb_species, env); |
|---|
| 157 | aci_result = GB_command_interpreter_in_env("", aci, callEnv); |
|---|
| 158 | if (!aci_result) error = GB_await_error(); |
|---|
| 159 | } |
|---|
| 160 | else { |
|---|
| 161 | if (aci && aci[0]) { |
|---|
| 162 | GBL_call_env callEnv(gb_origin, env); |
|---|
| 163 | aci_result = GB_command_interpreter_in_env(null2empty(result), aci, callEnv); |
|---|
| 164 | if (!aci_result) error = GB_await_error(); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | if (aci_result) freeset(result, aci_result); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | if (error) freenull(result); |
|---|
| 172 | |
|---|
| 173 | mg_assert(result||error); |
|---|
| 174 | return result; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | GB_ERROR MG_export_fields(AW_root *aw_root, GBDATA *gb_src, GBDATA *gb_dst, GB_HASH *error_suppressor, GB_HASH *source_organism_hash) { |
|---|
| 178 | // Export fields from pseudo-species' source-organism to exported destination-species |
|---|
| 179 | // error_suppressor and source_organism_hash may be NULp |
|---|
| 180 | |
|---|
| 181 | int export_fields = aw_root->awar(AWAR_MERGE_GENE_SPECIES_CREATE_FIELDS)->read_int(); |
|---|
| 182 | |
|---|
| 183 | if (export_fields) { // should fields be exported ? |
|---|
| 184 | mg_assert(GEN_is_pseudo_gene_species(gb_src)); |
|---|
| 185 | |
|---|
| 186 | char *existing_definitions = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->read_string(); |
|---|
| 187 | char *start = existing_definitions+1; |
|---|
| 188 | |
|---|
| 189 | mg_assert(existing_definitions[0] == ';'); |
|---|
| 190 | |
|---|
| 191 | GB_ERROR error = NULp; |
|---|
| 192 | while (!error && start[0]) { // parse existing definitions and add them to selection list |
|---|
| 193 | char *end = strchr(start, ';'); |
|---|
| 194 | if (!end) end = strchr(start, 0); |
|---|
| 195 | int len = end-start; |
|---|
| 196 | if (len<1) break; |
|---|
| 197 | |
|---|
| 198 | mg_assert(end[0] == ';'); |
|---|
| 199 | end[0] = 0; |
|---|
| 200 | |
|---|
| 201 | // export one field (start contains destination field name) |
|---|
| 202 | { |
|---|
| 203 | create_awars_for_field(start); |
|---|
| 204 | CreationMethod method = (CreationMethod)aw_root->awar(field_awar(start, "method"))->read_int(); |
|---|
| 205 | char *source = aw_root->awar(field_awar(start, "source"))->read_string(); |
|---|
| 206 | char *aci = aw_root->awar(field_awar(start, "aci"))->read_string(); |
|---|
| 207 | |
|---|
| 208 | char *result = MG_create_field_content(gb_src, method, source, aci, error, source_organism_hash); |
|---|
| 209 | mg_assert(result || error); |
|---|
| 210 | |
|---|
| 211 | if (result) { |
|---|
| 212 | error = GBT_write_string(gb_dst, start, result); |
|---|
| 213 | free(result); |
|---|
| 214 | } |
|---|
| 215 | else { |
|---|
| 216 | long error_seen = error_suppressor ? GBS_read_hash(error_suppressor, error) : 0; |
|---|
| 217 | #define MAX_EQUAL_WARNINGS 10 |
|---|
| 218 | if (error_seen >= MAX_EQUAL_WARNINGS) { |
|---|
| 219 | if (error_seen == MAX_EQUAL_WARNINGS) { |
|---|
| 220 | aw_message(GBS_global_string("More than %i warnings about '%s' (suppressing)", MAX_EQUAL_WARNINGS, error)); |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | else { |
|---|
| 224 | aw_message(GBS_global_string("'%s' when exporting %s (continuing)", error, GBT_get_name_or_description(gb_src))); |
|---|
| 225 | } |
|---|
| 226 | if (error_suppressor) { |
|---|
| 227 | GBS_incr_hash(error_suppressor, error); |
|---|
| 228 | GBS_optimize_hash(error_suppressor); |
|---|
| 229 | } |
|---|
| 230 | error = NULp; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | free(aci); |
|---|
| 234 | free(source); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | start = end+1; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | aw_message_if(error); |
|---|
| 241 | free(existing_definitions); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | return NULp; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | static char *MG_create_current_field_content(AW_root *aw_root, GBDATA *gb_species, GB_ERROR& error) { |
|---|
| 248 | CreationMethod method = (CreationMethod)aw_root->awar(AWAR_MERGE_GENE_SPECIES_METHOD)->read_int(); |
|---|
| 249 | char *origins_field = aw_root->awar(AWAR_MERGE_GENE_SPECIES_SOURCE)->read_string(); |
|---|
| 250 | char *aci = aw_root->awar(AWAR_MERGE_GENE_SPECIES_ACI)->read_string(); |
|---|
| 251 | |
|---|
| 252 | char *result = MG_create_field_content(gb_species, method, origins_field, aci, error, NULp); |
|---|
| 253 | |
|---|
| 254 | free(aci); |
|---|
| 255 | free(origins_field); |
|---|
| 256 | |
|---|
| 257 | return result; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | static void MG_update_example(AW_root *aw_root) { |
|---|
| 261 | char *result = NULp; |
|---|
| 262 | GB_ERROR error = NULp; |
|---|
| 263 | char *curr_species = aw_root->awar(MG_left_AWAR_SPECIES_NAME())->read_string(); |
|---|
| 264 | |
|---|
| 265 | if (!curr_species || !curr_species[0]) error = "No species selected."; |
|---|
| 266 | else { |
|---|
| 267 | GB_transaction ta(GLOBAL_gb_src); |
|---|
| 268 | GBDATA *gb_species = GBT_find_species(GLOBAL_gb_src, curr_species); |
|---|
| 269 | |
|---|
| 270 | if (!gb_species) error = GB_export_errorf("No such species: '%s'", curr_species); |
|---|
| 271 | else if (!GEN_is_pseudo_gene_species(gb_species)) error = "Selected species is no gene-species"; |
|---|
| 272 | else { |
|---|
| 273 | result = MG_create_current_field_content(aw_root, gb_species, error); |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if (!error && !result) error = "no result"; |
|---|
| 278 | if (error) freeset(result, GBS_global_string_copy("<%s>", error)); |
|---|
| 279 | |
|---|
| 280 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_EXAMPLE)->write_string(result); |
|---|
| 281 | |
|---|
| 282 | free(result); |
|---|
| 283 | free(curr_species); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | static void check_and_correct_current_field(char*& cur_field) { |
|---|
| 287 | if (ARB_stricmp(cur_field, "name") == 0 || ARB_stricmp(cur_field, "acc") == 0) { |
|---|
| 288 | aw_message("rules writing to 'name' or 'acc' are not allowed."); |
|---|
| 289 | freeset(cur_field, GBS_global_string_copy("%s_not_allowed", cur_field)); |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | static bool allow_callbacks = true; |
|---|
| 295 | |
|---|
| 296 | static void MG_current_field_def_changed_cb(AW_root *aw_root) { |
|---|
| 297 | if (allow_callbacks) { |
|---|
| 298 | LocallyModify<bool> flag(allow_callbacks, false); |
|---|
| 299 | |
|---|
| 300 | char *cur_field = aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->read_string(); |
|---|
| 301 | check_and_correct_current_field(cur_field); |
|---|
| 302 | |
|---|
| 303 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->write_string(cur_field); |
|---|
| 304 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_DEST)->write_string(cur_field); |
|---|
| 305 | |
|---|
| 306 | if (cur_field[0]) { |
|---|
| 307 | const char *awar_name = field_awar(cur_field, "source"); |
|---|
| 308 | |
|---|
| 309 | // read stored source field (if undef default to new value of destination field) |
|---|
| 310 | char *source_field = aw_root->awar_string(awar_name, cur_field, MG_props)->read_string(); |
|---|
| 311 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_SOURCE)->write_string(source_field); |
|---|
| 312 | free(source_field); |
|---|
| 313 | |
|---|
| 314 | // read stored method (if undef then default to currently visible method) |
|---|
| 315 | awar_name = field_awar(cur_field, "method"); |
|---|
| 316 | int def_method = aw_root->awar(AWAR_MERGE_GENE_SPECIES_METHOD)->read_int(); |
|---|
| 317 | int method = aw_root->awar_int(awar_name, def_method, MG_props)->read_int(); |
|---|
| 318 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_METHOD)->write_int(method); |
|---|
| 319 | |
|---|
| 320 | // read stored aci (if undef then default to currently visible aci ) |
|---|
| 321 | awar_name = field_awar(cur_field, "aci"); |
|---|
| 322 | char *curr_aci = aw_root->awar(AWAR_MERGE_GENE_SPECIES_ACI)->read_string(); |
|---|
| 323 | char *aci = aw_root->awar_string(awar_name, curr_aci, MG_props)->read_string(); |
|---|
| 324 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_ACI)->write_string(aci); |
|---|
| 325 | free(aci); |
|---|
| 326 | free(curr_aci); |
|---|
| 327 | |
|---|
| 328 | MG_update_example(aw_root); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | free(cur_field); |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | static void MG_source_field_changed_cb(AW_root *aw_root) { |
|---|
| 336 | if (allow_callbacks) { |
|---|
| 337 | const char *awar_name = current_field_awar(aw_root, "source"); |
|---|
| 338 | if (awar_name) { // if a rule is selected |
|---|
| 339 | char *source_field = aw_root->awar(AWAR_MERGE_GENE_SPECIES_SOURCE)->read_string(); |
|---|
| 340 | int method = aw_root->awar(AWAR_MERGE_GENE_SPECIES_METHOD)->read_int(); |
|---|
| 341 | |
|---|
| 342 | if (source_field[0] && method == MG_CREATE_USING_ACI_ONLY) { |
|---|
| 343 | aw_message("Source field is not used with this method"); |
|---|
| 344 | source_field[0] = 0; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | aw_root->awar(awar_name)->write_string(source_field); |
|---|
| 348 | free(source_field); |
|---|
| 349 | |
|---|
| 350 | MG_update_example(aw_root); |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | static void MG_dest_field_changed_cb(AW_root *aw_root) { |
|---|
| 356 | if (allow_callbacks) { |
|---|
| 357 | // if this is changed -> a new definition will be generated |
|---|
| 358 | char *dest_field = aw_root->awar(AWAR_MERGE_GENE_SPECIES_DEST)->read_string(); |
|---|
| 359 | check_and_correct_current_field(dest_field); |
|---|
| 360 | |
|---|
| 361 | const char *search = GBS_global_string(";%s;", dest_field); |
|---|
| 362 | char *existing_definitions = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->read_string(); |
|---|
| 363 | |
|---|
| 364 | mg_assert(existing_definitions[0] == ';'); |
|---|
| 365 | |
|---|
| 366 | if (!strstr(existing_definitions, search)) { // not found -> create a new definition |
|---|
| 367 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS) |
|---|
| 368 | ->write_string(GBS_global_string("%s%s;", existing_definitions, dest_field)); |
|---|
| 369 | |
|---|
| 370 | } |
|---|
| 371 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->write_string(dest_field); |
|---|
| 372 | |
|---|
| 373 | free(existing_definitions); |
|---|
| 374 | free(dest_field); |
|---|
| 375 | |
|---|
| 376 | MG_update_example(aw_root); |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | static void MG_method_changed_cb(AW_root *aw_root) { |
|---|
| 381 | if (allow_callbacks) { |
|---|
| 382 | const char *awar_name = current_field_awar(aw_root, "method"); |
|---|
| 383 | |
|---|
| 384 | if (awar_name) { |
|---|
| 385 | int method = aw_root->awar(AWAR_MERGE_GENE_SPECIES_METHOD)->read_int(); |
|---|
| 386 | aw_root->awar(awar_name)->write_int(method); |
|---|
| 387 | if (method == MG_CREATE_USING_ACI_ONLY) { |
|---|
| 388 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_SOURCE)->write_string(""); // clear source field |
|---|
| 389 | } |
|---|
| 390 | MG_update_example(aw_root); |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | static void MG_delete_selected_field_def(AW_window *aww) { |
|---|
| 396 | AW_root *aw_root = aww->get_root(); |
|---|
| 397 | char *cur_field = aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->read_string(); |
|---|
| 398 | |
|---|
| 399 | if (cur_field[0]) { |
|---|
| 400 | char *existing_definitions = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->read_string(); |
|---|
| 401 | const char *search = GBS_global_string(";%s;", cur_field); |
|---|
| 402 | |
|---|
| 403 | mg_assert(existing_definitions[0] == ';'); |
|---|
| 404 | |
|---|
| 405 | char *found = strstr(existing_definitions, search); |
|---|
| 406 | mg_assert(found); |
|---|
| 407 | if (found) { |
|---|
| 408 | strcpy(found, found+strlen(cur_field)+1); // remove that config |
|---|
| 409 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->write_string(existing_definitions); |
|---|
| 410 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->write_string(""); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | free(existing_definitions); |
|---|
| 414 | } |
|---|
| 415 | else { |
|---|
| 416 | aw_message("No field selected."); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | free(cur_field); |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | static void MG_aci_changed_cb(AW_root *aw_root) { |
|---|
| 423 | if (allow_callbacks) { |
|---|
| 424 | const char *awar_name = current_field_awar(aw_root, "aci"); |
|---|
| 425 | if (awar_name) { // if a rule is selected |
|---|
| 426 | char *aci = aw_root->awar(AWAR_MERGE_GENE_SPECIES_ACI)->read_string(); |
|---|
| 427 | aw_root->awar(awar_name)->write_string(aci); |
|---|
| 428 | free(aci); |
|---|
| 429 | MG_update_example(aw_root); |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | static void MG_update_selection_list_on_field_transfers(AW_root *aw_root, AW_selection_list *geneSpecFieldList) { |
|---|
| 435 | geneSpecFieldList->clear(); |
|---|
| 436 | |
|---|
| 437 | { |
|---|
| 438 | char *existing_definitions = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->read_string(); |
|---|
| 439 | char *start = existing_definitions+1; |
|---|
| 440 | |
|---|
| 441 | mg_assert(existing_definitions[0] == ';'); |
|---|
| 442 | |
|---|
| 443 | while (start[0]) { // parse existing definitions and add them to selection list |
|---|
| 444 | char *end = strchr(start, ';'); |
|---|
| 445 | if (!end) end = strchr(start, 0); |
|---|
| 446 | int len = end-start; |
|---|
| 447 | if (len<1) break; |
|---|
| 448 | |
|---|
| 449 | mg_assert(end[0] == ';'); |
|---|
| 450 | end[0] = 0; |
|---|
| 451 | geneSpecFieldList->insert(start, start); |
|---|
| 452 | start = end+1; |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | free(existing_definitions); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | geneSpecFieldList->insert_default("", ""); |
|---|
| 459 | geneSpecFieldList->update(); |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | static void init_gene_species_xfer_fields_subconfig(AWT_config_definition& cdef, char *existing_definitions) { |
|---|
| 463 | char *start = existing_definitions+1; |
|---|
| 464 | mg_assert(existing_definitions[0] == ';'); |
|---|
| 465 | |
|---|
| 466 | for (int count = 0; start[0]; ++count) { // parse existing definitions and add them to config |
|---|
| 467 | char *end = strchr(start, ';'); |
|---|
| 468 | if (!end) end = strchr(start, 0); |
|---|
| 469 | int len = end-start; |
|---|
| 470 | if (len<1) break; |
|---|
| 471 | |
|---|
| 472 | mg_assert(end[0] == ';'); |
|---|
| 473 | end[0] = 0; |
|---|
| 474 | |
|---|
| 475 | // add config : |
|---|
| 476 | #define add_config(s, id) cdef.add(field_awar(s, id), id, count) |
|---|
| 477 | |
|---|
| 478 | create_awars_for_field(start); |
|---|
| 479 | |
|---|
| 480 | add_config(start, "source"); |
|---|
| 481 | add_config(start, "method"); |
|---|
| 482 | add_config(start, "aci"); |
|---|
| 483 | |
|---|
| 484 | #undef add_config |
|---|
| 485 | |
|---|
| 486 | end[0] = ';'; |
|---|
| 487 | |
|---|
| 488 | start = end+1; |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| 491 | static void init_gene_species_xfer_fields_config(AWT_config_definition& cdef) { |
|---|
| 492 | cdef.add(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS, "fields"); |
|---|
| 493 | cdef.add(AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE, "defs"); |
|---|
| 494 | } |
|---|
| 495 | static char *store_gene_species_xfer_fields() { |
|---|
| 496 | AW_root *aw_root = AW_root::SINGLETON; |
|---|
| 497 | { |
|---|
| 498 | char *existing_definitions = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->read_string(); |
|---|
| 499 | AWT_config_definition cdef; |
|---|
| 500 | |
|---|
| 501 | init_gene_species_xfer_fields_subconfig(cdef, existing_definitions); |
|---|
| 502 | |
|---|
| 503 | char *sub_config = cdef.read(); // save single configs to sub_config |
|---|
| 504 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE)->write_string(sub_config); |
|---|
| 505 | |
|---|
| 506 | free(sub_config); |
|---|
| 507 | free(existing_definitions); |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | // save AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE and AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS |
|---|
| 511 | AWT_config_definition sub_cdef; |
|---|
| 512 | init_gene_species_xfer_fields_config(sub_cdef); |
|---|
| 513 | return sub_cdef.read(); |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | static void load_or_reset_gene_species_xfer_fields(const char *stored_string, AW_selection_list *geneSpecFieldList) { |
|---|
| 517 | // if stored_string==NULp -> reset |
|---|
| 518 | AW_root *aw_root = AW_root::SINGLETON; |
|---|
| 519 | |
|---|
| 520 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->write_string(""); // de-select |
|---|
| 521 | |
|---|
| 522 | // Load 'AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS' and 'AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE' |
|---|
| 523 | { |
|---|
| 524 | AWT_config_definition sub_cdef; |
|---|
| 525 | init_gene_species_xfer_fields_config(sub_cdef); |
|---|
| 526 | if (stored_string) { |
|---|
| 527 | sub_cdef.write(stored_string); |
|---|
| 528 | } |
|---|
| 529 | else { |
|---|
| 530 | sub_cdef.reset(); |
|---|
| 531 | } |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | { |
|---|
| 535 | char *existing_definitions = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->read_string(); |
|---|
| 536 | char *sub_config = aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_SAVE)->read_string(); |
|---|
| 537 | |
|---|
| 538 | AWT_config_definition cdef; |
|---|
| 539 | init_gene_species_xfer_fields_subconfig(cdef, existing_definitions); |
|---|
| 540 | cdef.write(sub_config); |
|---|
| 541 | |
|---|
| 542 | free(sub_config); |
|---|
| 543 | free(existing_definitions); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | MG_update_selection_list_on_field_transfers(aw_root, geneSpecFieldList); // refresh mask |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | AW_window *MG_gene_species_create_field_transfer_def_window(AW_root *aw_root) { |
|---|
| 550 | static AW_window_simple *aws = NULp; |
|---|
| 551 | if (aws) return aws; |
|---|
| 552 | |
|---|
| 553 | aws = new AW_window_simple; |
|---|
| 554 | aws->init(aw_root, "DEFINE_GENE_SPECIES_FIELDS", "DEFINE FIELDS EXPORTED WITH GENE SPECIES"); |
|---|
| 555 | aws->load_xfig("merge/mg_def_gene_species_fields.fig"); |
|---|
| 556 | |
|---|
| 557 | aws->at("close"); |
|---|
| 558 | aws->callback(AW_POPDOWN); |
|---|
| 559 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 560 | |
|---|
| 561 | aws->at("help"); |
|---|
| 562 | aws->callback(makeHelpCallback("gene_species_field_transfer.hlp")); |
|---|
| 563 | aws->create_button("HELP", "HELP"); |
|---|
| 564 | |
|---|
| 565 | aws->at("active"); |
|---|
| 566 | aws->create_toggle(AWAR_MERGE_GENE_SPECIES_CREATE_FIELDS); |
|---|
| 567 | |
|---|
| 568 | aws->at("src"); |
|---|
| 569 | aws->create_input_field(AWAR_MERGE_GENE_SPECIES_SOURCE); // @@@ use field selection (either from organism or from gene); need custom window-popper + intermediate awars here |
|---|
| 570 | |
|---|
| 571 | aws->at("delete"); |
|---|
| 572 | aws->callback(MG_delete_selected_field_def); |
|---|
| 573 | aws->create_button("DELETE", "DELETE"); |
|---|
| 574 | |
|---|
| 575 | aws->at("sel_method"); |
|---|
| 576 | aws->create_toggle_field(AWAR_MERGE_GENE_SPECIES_METHOD); |
|---|
| 577 | aws->insert_toggle("Copy from organism", "O", MG_CREATE_COPY_ORGANISM); |
|---|
| 578 | aws->insert_toggle("Copy from gene", "G", MG_CREATE_COPY_GENE); |
|---|
| 579 | aws->insert_toggle("Only use ACI below", "A", MG_CREATE_USING_ACI_ONLY); |
|---|
| 580 | aws->update_toggle_field(); |
|---|
| 581 | |
|---|
| 582 | aws->at("aci"); |
|---|
| 583 | aws->create_input_field(AWAR_MERGE_GENE_SPECIES_ACI); |
|---|
| 584 | |
|---|
| 585 | aws->at("dest"); |
|---|
| 586 | aws->create_input_field(AWAR_MERGE_GENE_SPECIES_DEST); // @@@ use field selection (with SF_ALLOW_NEW), but create field instantly. Otherwise too complicated. |
|---|
| 587 | |
|---|
| 588 | aws->at("example"); |
|---|
| 589 | aws->create_text_field(AWAR_MERGE_GENE_SPECIES_EXAMPLE, 40, 3); |
|---|
| 590 | |
|---|
| 591 | aws->at("fields"); |
|---|
| 592 | AW_selection_list *geneSpecFieldList = aws->create_selection_list(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD, 10, 30); |
|---|
| 593 | |
|---|
| 594 | MG_update_selection_list_on_field_transfers(aw_root, geneSpecFieldList); |
|---|
| 595 | |
|---|
| 596 | aws->at("save"); |
|---|
| 597 | AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "gene_species_field_xfer", |
|---|
| 598 | makeStoreConfigCallback(store_gene_species_xfer_fields), |
|---|
| 599 | makeRestoreConfigCallback(load_or_reset_gene_species_xfer_fields, geneSpecFieldList)); |
|---|
| 600 | |
|---|
| 601 | // add callbacks for this window : |
|---|
| 602 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_FIELDS_DEFS)->add_callback(makeRootCallback(MG_update_selection_list_on_field_transfers, geneSpecFieldList)); |
|---|
| 603 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_CURRENT_FIELD)->add_callback(MG_current_field_def_changed_cb); |
|---|
| 604 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_SOURCE)->add_callback(MG_source_field_changed_cb); |
|---|
| 605 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_DEST)->add_callback(MG_dest_field_changed_cb); |
|---|
| 606 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_METHOD)->add_callback(MG_method_changed_cb); |
|---|
| 607 | aw_root->awar(AWAR_MERGE_GENE_SPECIES_ACI)->add_callback(MG_aci_changed_cb); |
|---|
| 608 | aw_root->awar(MG_left_AWAR_SPECIES_NAME())->add_callback(MG_update_example); |
|---|
| 609 | |
|---|
| 610 | return aws; |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | |
|---|