| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : MG_species.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include "merge.hxx" |
|---|
| 12 | #include "MG_adapt_ali.hxx" |
|---|
| 13 | |
|---|
| 14 | #include <AW_rename.hxx> |
|---|
| 15 | #include <db_query.h> |
|---|
| 16 | #include <db_scanner.hxx> |
|---|
| 17 | #include <item_sel_list.h> |
|---|
| 18 | |
|---|
| 19 | #include <xfergui.h> |
|---|
| 20 | #include <xferset.h> |
|---|
| 21 | |
|---|
| 22 | #include <aw_awars.hxx> |
|---|
| 23 | #include <aw_msg.hxx> |
|---|
| 24 | #include <arb_progress.h> |
|---|
| 25 | #include <aw_root.hxx> |
|---|
| 26 | #include <aw_question.hxx> |
|---|
| 27 | |
|---|
| 28 | #include <arb_str.h> |
|---|
| 29 | #include <arb_strbuf.h> |
|---|
| 30 | #include <arb_global_defs.h> |
|---|
| 31 | #include <config_manager.hxx> |
|---|
| 32 | #include <selection_admin.h> |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | #define AWAR_SPECIES_SRC AWAR_MERGE_TMP_SRC "name" |
|---|
| 36 | #define AWAR_FIELD_SRC AWAR_MERGE_TMP_SRC "field" |
|---|
| 37 | #define AWAR_TAG_SRC AWAR_MERGE_TMP_SRC "tag" |
|---|
| 38 | |
|---|
| 39 | #define AWAR_SPECIES_DST AWAR_MERGE_TMP_DST "name" |
|---|
| 40 | #define AWAR_FIELD_DST AWAR_MERGE_TMP_DST "field" |
|---|
| 41 | #define AWAR_TAG_DST AWAR_MERGE_TMP_DST "tag" |
|---|
| 42 | |
|---|
| 43 | #define AWAR_TAG_DEL AWAR_MERGE_TMP "tagdel" |
|---|
| 44 | #define AWAR_APPEND AWAR_MERGE_TMP "append" |
|---|
| 45 | |
|---|
| 46 | #define AWAR_SPECIES_XFER_TYPE AWAR_MERGE_SAV "xferType" |
|---|
| 47 | #define AWAR_SPECIES_XFER_SCOPE AWAR_MERGE_SAV "xferScope" |
|---|
| 48 | #define AWAR_SPECIES_XFER_FTS AWAR_MERGE_SAV "fts" |
|---|
| 49 | |
|---|
| 50 | static DbScanner *scanner_src = NULp; |
|---|
| 51 | static DbScanner *scanner_dst = NULp; |
|---|
| 52 | |
|---|
| 53 | enum SpeciesXferType { |
|---|
| 54 | SXT_WHOLE_SPECIES, |
|---|
| 55 | SXT_SINGLE_FIELD, // (skips alignment data) |
|---|
| 56 | SXT_USING_FTS, // field transfer set |
|---|
| 57 | SXT_USING_FTS_SKIP_ALI, // field transfer set (skips alignment data) |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | enum SpeciesXferScope { |
|---|
| 61 | SXS_SELECTED_SPECIES, |
|---|
| 62 | SXS_LISTED_SPECIES, |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | const char *MG_left_AWAR_SPECIES_NAME() { return AWAR_SPECIES_SRC; } |
|---|
| 66 | |
|---|
| 67 | void MG_create_species_awars(AW_root *aw_root, AW_default aw_def) { |
|---|
| 68 | aw_root->awar_string(AWAR_SPECIES_SRC, "", aw_def); |
|---|
| 69 | aw_root->awar_string(AWAR_SPECIES_DST, "", aw_def); |
|---|
| 70 | |
|---|
| 71 | aw_root->awar_string(AWAR_FIELD_SRC, NO_FIELD_SELECTED, aw_def); |
|---|
| 72 | aw_root->awar_string(AWAR_FIELD_DST, NO_FIELD_SELECTED, aw_def); |
|---|
| 73 | |
|---|
| 74 | aw_root->awar_int(AWAR_APPEND, 0, aw_def); |
|---|
| 75 | |
|---|
| 76 | aw_root->awar_int(AWAR_SPECIES_XFER_TYPE, SXT_WHOLE_SPECIES, aw_def); |
|---|
| 77 | aw_root->awar_int(AWAR_SPECIES_XFER_SCOPE, SXS_LISTED_SPECIES, aw_def); |
|---|
| 78 | |
|---|
| 79 | aw_root->awar_string(AWAR_SPECIES_XFER_FTS, "", aw_def); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | static void MG_map_species(AW_root *aw_root, DbSel db) { |
|---|
| 83 | GBDATA *gb_main = get_gb_main(db); |
|---|
| 84 | const char *awar_name = db == SRC_DB ? AWAR_SPECIES_SRC : AWAR_SPECIES_DST; |
|---|
| 85 | DbScanner *scanner = db == SRC_DB ? scanner_src : scanner_dst; |
|---|
| 86 | |
|---|
| 87 | GB_transaction ta(gb_main); |
|---|
| 88 | char *selected_species = aw_root->awar(awar_name)->read_string(); |
|---|
| 89 | GBDATA *gb_species = GBT_find_species(gb_main, selected_species); |
|---|
| 90 | if (gb_species) { |
|---|
| 91 | scanner->Map(gb_species, awar_name); // @@@ why is awar_name passed here? (normally a changekey-path is used) |
|---|
| 92 | // @@@ undocumented feature in DbScanner or bug? |
|---|
| 93 | } |
|---|
| 94 | free(selected_species); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | static GB_ERROR MG_transfer_fields_info(char *fieldname = NULp) { |
|---|
| 98 | GBDATA *gb_key_data = GB_search(GLOBAL_gb_src, CHANGE_KEY_PATH, GB_CREATE_CONTAINER); |
|---|
| 99 | GB_ERROR error = NULp; |
|---|
| 100 | |
|---|
| 101 | if (!gb_key_data) error = GB_await_error(); |
|---|
| 102 | else { |
|---|
| 103 | if (!GB_search(GLOBAL_gb_dst, CHANGE_KEY_PATH, GB_CREATE_CONTAINER)) error = GB_await_error(); |
|---|
| 104 | else { |
|---|
| 105 | for (GBDATA *gb_key = GB_entry(gb_key_data, CHANGEKEY); |
|---|
| 106 | gb_key && !error; |
|---|
| 107 | gb_key = GB_nextEntry(gb_key)) |
|---|
| 108 | { |
|---|
| 109 | GBDATA *gb_key_name = GB_entry(gb_key, CHANGEKEY_NAME); |
|---|
| 110 | if (gb_key_name) { |
|---|
| 111 | GBDATA *gb_key_type = GB_entry(gb_key, CHANGEKEY_TYPE); |
|---|
| 112 | if (gb_key_type) { |
|---|
| 113 | char *name = GB_read_string(gb_key_name); |
|---|
| 114 | |
|---|
| 115 | if (!fieldname || strcmp(fieldname, name) == 0) { |
|---|
| 116 | error = GBT_add_new_species_changekey(GLOBAL_gb_dst, name, GB_TYPES(GB_read_int(gb_key_type))); |
|---|
| 117 | } |
|---|
| 118 | free(name); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | return error; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | static GB_ERROR MG_transfer_one_species(AW_root *aw_root, MG_remaps& remap, |
|---|
| 128 | GBDATA *gb_src_species, GBDATA *gb_dst_species_data, |
|---|
| 129 | bool src_is_genome, bool dst_is_genome, |
|---|
| 130 | GB_HASH *source_organism_hash, GB_HASH *dest_species_hash, |
|---|
| 131 | GB_HASH *error_suppressor) |
|---|
| 132 | { |
|---|
| 133 | // copies species 'gb_src_species' from source to destination DB. |
|---|
| 134 | // |
|---|
| 135 | // 'source_organism_hash' may be NULp, otherwise it's used to search for source organisms (when merging from genome DB) |
|---|
| 136 | // 'dest_species_hash' may be NULp, otherwise created species is stored there |
|---|
| 137 | |
|---|
| 138 | mg_assert(gb_src_species); |
|---|
| 139 | |
|---|
| 140 | GB_ERROR error = NULp; |
|---|
| 141 | const char *src_name = GBT_get_name(gb_src_species); |
|---|
| 142 | bool transfer_fields = false; |
|---|
| 143 | |
|---|
| 144 | if (!src_name) { |
|---|
| 145 | error = "refused to transfer unnamed species"; |
|---|
| 146 | } |
|---|
| 147 | else { |
|---|
| 148 | if (src_is_genome) { |
|---|
| 149 | if (dst_is_genome) { // genome -> genome |
|---|
| 150 | if (GEN_is_pseudo_gene_species(gb_src_species)) { |
|---|
| 151 | const char *origin = GEN_origin_organism(gb_src_species); |
|---|
| 152 | GBDATA *dest_organism = dest_species_hash |
|---|
| 153 | ? (GBDATA*)GBS_read_hash(dest_species_hash, origin) |
|---|
| 154 | : GEN_find_organism(GLOBAL_gb_dst, origin); |
|---|
| 155 | |
|---|
| 156 | if (dest_organism) transfer_fields = true; |
|---|
| 157 | else { |
|---|
| 158 | error = GBS_global_string("Destination DB does not contain '%s's origin-organism '%s'", |
|---|
| 159 | src_name, origin); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | // else: merge organism ok |
|---|
| 163 | } |
|---|
| 164 | else { // genome -> non-genome |
|---|
| 165 | if (GEN_is_pseudo_gene_species(gb_src_species)) transfer_fields = true; |
|---|
| 166 | else { |
|---|
| 167 | error = GBS_global_string("You can't merge organisms (%s) into a non-genome DB.\n" |
|---|
| 168 | "Only pseudo-species are possible", src_name); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | else { |
|---|
| 173 | if (dst_is_genome) { // non-genome -> genome |
|---|
| 174 | error = GBS_global_string("You can't merge non-genome species (%s) into a genome DB", src_name); |
|---|
| 175 | } |
|---|
| 176 | // else: non-genome -> non-genome ok |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | GBDATA *gb_dst_species = NULp; |
|---|
| 181 | if (!error) { |
|---|
| 182 | gb_dst_species = dest_species_hash |
|---|
| 183 | ? (GBDATA*)GBS_read_hash(dest_species_hash, src_name) |
|---|
| 184 | : GBT_find_species_rel_species_data(gb_dst_species_data, src_name); |
|---|
| 185 | |
|---|
| 186 | if (gb_dst_species) error = GB_delete(gb_dst_species); |
|---|
| 187 | } |
|---|
| 188 | if (!error) { |
|---|
| 189 | gb_dst_species = GB_create_container(gb_dst_species_data, "species"); |
|---|
| 190 | if (!gb_dst_species) error = GB_await_error(); |
|---|
| 191 | } |
|---|
| 192 | if (!error) error = GB_copy_dropProtectMarksAndTempstate(gb_dst_species, gb_src_species); |
|---|
| 193 | if (!error && transfer_fields) { |
|---|
| 194 | mg_assert(src_is_genome); |
|---|
| 195 | error = MG_export_fields(aw_root, gb_src_species, gb_dst_species, error_suppressor, source_organism_hash); |
|---|
| 196 | } |
|---|
| 197 | if (!error) GB_write_flag(gb_dst_species, 1); |
|---|
| 198 | if (!error) error = MG_adaptAllCopiedAlignments(remap, gb_src_species, gb_dst_species); |
|---|
| 199 | if (!error && dest_species_hash) GBS_write_hash(dest_species_hash, src_name, (long)gb_dst_species); |
|---|
| 200 | |
|---|
| 201 | return error; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | static const char *get_reference_species_names(AW_root *awr) { |
|---|
| 205 | return awr->awar(AWAR_REMAP_SPECIES_LIST)->read_char_pntr(); |
|---|
| 206 | } |
|---|
| 207 | static bool adaption_enabled(AW_root *awr) { |
|---|
| 208 | return awr->awar(AWAR_REMAP_ENABLE)->read_int() != 0; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | static void mg_transfer_selected_species(AW_window *aww) { |
|---|
| 212 | if (MG_copy_and_check_alignments() != 0) return; |
|---|
| 213 | |
|---|
| 214 | AW_root *aw_root = aww->get_root(); |
|---|
| 215 | char *src_name = aw_root->awar(AWAR_SPECIES_SRC)->read_string(); |
|---|
| 216 | GB_ERROR error = NULp; |
|---|
| 217 | |
|---|
| 218 | if (!src_name || !src_name[0]) { |
|---|
| 219 | error = "Please select a species in the left list"; |
|---|
| 220 | } |
|---|
| 221 | else { |
|---|
| 222 | arb_progress progress("Transferring selected species"); |
|---|
| 223 | |
|---|
| 224 | error = GB_begin_transaction(GLOBAL_gb_src); |
|---|
| 225 | if (!error) error = GB_begin_transaction(GLOBAL_gb_dst); |
|---|
| 226 | |
|---|
| 227 | if (!error) { |
|---|
| 228 | MG_remaps rm(GLOBAL_gb_src, GLOBAL_gb_dst, adaption_enabled(aw_root), get_reference_species_names(aw_root)); |
|---|
| 229 | |
|---|
| 230 | GBDATA *gb_src_species_data = GBT_get_species_data(GLOBAL_gb_src); |
|---|
| 231 | GBDATA *gb_dst_species_data = GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 232 | |
|---|
| 233 | bool src_is_genome = GEN_is_genome_db(GLOBAL_gb_src, -1); |
|---|
| 234 | bool dst_is_genome = GEN_is_genome_db(GLOBAL_gb_dst, -1); |
|---|
| 235 | |
|---|
| 236 | GBDATA *gb_src_species = GBT_find_species_rel_species_data(gb_src_species_data, src_name); |
|---|
| 237 | if (!gb_src_species) { |
|---|
| 238 | error = GBS_global_string("Could not find species '%s' in source database", src_name); |
|---|
| 239 | } |
|---|
| 240 | else { |
|---|
| 241 | error = MG_transfer_one_species(aw_root, rm, |
|---|
| 242 | gb_src_species, gb_dst_species_data, |
|---|
| 243 | src_is_genome, dst_is_genome, |
|---|
| 244 | NULp, NULp, |
|---|
| 245 | NULp); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | if (!error) error = MG_transfer_fields_info(); |
|---|
| 250 | |
|---|
| 251 | error = GB_end_transaction(GLOBAL_gb_src, error); |
|---|
| 252 | error = GB_end_transaction(GLOBAL_gb_dst, error); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | if (error) aw_message(error); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | static void mg_transfer_listed_species(AW_window *aww) { |
|---|
| 259 | if (MG_copy_and_check_alignments() != 0) return; |
|---|
| 260 | |
|---|
| 261 | GB_ERROR error = NULp; |
|---|
| 262 | GB_begin_transaction(GLOBAL_gb_src); |
|---|
| 263 | GB_begin_transaction(GLOBAL_gb_dst); |
|---|
| 264 | |
|---|
| 265 | bool src_is_genome = GEN_is_genome_db(GLOBAL_gb_src, -1); |
|---|
| 266 | bool dst_is_genome = GEN_is_genome_db(GLOBAL_gb_dst, -1); |
|---|
| 267 | |
|---|
| 268 | GB_HASH *error_suppressor = GBS_create_hash(50, GB_IGNORE_CASE); |
|---|
| 269 | GB_HASH *dest_species_hash = GBT_create_species_hash(GLOBAL_gb_dst); |
|---|
| 270 | GB_HASH *source_organism_hash = src_is_genome ? GBT_create_organism_hash(GLOBAL_gb_src) : NULp; |
|---|
| 271 | |
|---|
| 272 | AW_root *aw_root = aww->get_root(); |
|---|
| 273 | MG_remaps rm(GLOBAL_gb_src, GLOBAL_gb_dst, adaption_enabled(aw_root), get_reference_species_names(aw_root)); |
|---|
| 274 | |
|---|
| 275 | GBDATA *gb_src_species; |
|---|
| 276 | arb_progress progress("Transferring listed species", mg_count_queried(GLOBAL_gb_src)); |
|---|
| 277 | |
|---|
| 278 | for (gb_src_species = GBT_first_species(GLOBAL_gb_src); |
|---|
| 279 | gb_src_species; |
|---|
| 280 | gb_src_species = GBT_next_species(gb_src_species)) |
|---|
| 281 | { |
|---|
| 282 | if (IS_QUERIED_SPECIES(gb_src_species)) { |
|---|
| 283 | GBDATA *gb_dst_species_data = GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 284 | |
|---|
| 285 | error = MG_transfer_one_species(aw_root, rm, |
|---|
| 286 | gb_src_species, gb_dst_species_data, |
|---|
| 287 | src_is_genome, dst_is_genome, |
|---|
| 288 | source_organism_hash, dest_species_hash, |
|---|
| 289 | error_suppressor); |
|---|
| 290 | |
|---|
| 291 | progress.inc_and_check_user_abort(error); |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | GBS_free_hash(dest_species_hash); |
|---|
| 296 | if (source_organism_hash) GBS_free_hash(source_organism_hash); |
|---|
| 297 | GBS_free_hash(error_suppressor); |
|---|
| 298 | |
|---|
| 299 | if (!error) error = MG_transfer_fields_info(); |
|---|
| 300 | |
|---|
| 301 | error = GB_end_transaction(GLOBAL_gb_src, error); |
|---|
| 302 | GB_end_transaction_show_error(GLOBAL_gb_dst, error, aw_message); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | static GB_ERROR transfer_single_field(GBDATA *const gb_src_species, GBDATA *const gb_dst_species, const char *const field, GBDATA *const gb_src_field, const bool append, const bool transfer_of_alignment, const MG_remaps& rm) { |
|---|
| 306 | GBDATA *gb_dst_field = GB_search(gb_dst_species, field, GB_FIND); |
|---|
| 307 | GB_TYPES src_type = GB_read_type(gb_src_field); |
|---|
| 308 | GB_TYPES dst_type = gb_dst_field ? GB_read_type(gb_dst_field) : src_type; |
|---|
| 309 | |
|---|
| 310 | bool use_copy = true; |
|---|
| 311 | GB_ERROR error = NULp; |
|---|
| 312 | |
|---|
| 313 | if (append) { |
|---|
| 314 | if (src_type != GB_STRING || dst_type != GB_STRING) { |
|---|
| 315 | error = "You can use 'Append data' with text fields only!"; |
|---|
| 316 | } |
|---|
| 317 | else if (gb_dst_field) { // append possible (otherwise use_copy!) |
|---|
| 318 | char *src_val = GB_read_string(gb_src_field); |
|---|
| 319 | char *dst_val = GB_read_string(gb_dst_field); |
|---|
| 320 | |
|---|
| 321 | if (!src_val || !dst_val) error = GB_await_error(); |
|---|
| 322 | else { |
|---|
| 323 | if (!GBS_find_string(dst_val, src_val, 0)) { // do not append if dst_val contains src_val |
|---|
| 324 | error = GB_write_string(gb_dst_field, GBS_global_string("%s %s", dst_val, src_val)); |
|---|
| 325 | if (!error) GB_write_flag(gb_dst_species, 1); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | free(src_val); |
|---|
| 330 | free(dst_val); |
|---|
| 331 | |
|---|
| 332 | use_copy = false; |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | if (!error && use_copy) { |
|---|
| 337 | if (dst_type != src_type) error = "type mismatch"; // abort early (previously failed in call to MG_transfer_fields_info below) |
|---|
| 338 | if (!error && src_type == GB_DB && gb_dst_field) { |
|---|
| 339 | // if type is container -> delete before copy |
|---|
| 340 | // (otherwise it would generate a mix of both containers; if wanted provide optionally) |
|---|
| 341 | error = GB_delete(gb_dst_field); |
|---|
| 342 | gb_dst_field = NULp; // trigger recreation+copy |
|---|
| 343 | } |
|---|
| 344 | if (!error && !gb_dst_field) { // create missing target field (otherwise simply overwrite) |
|---|
| 345 | gb_dst_field = GB_search(gb_dst_species, field, src_type); |
|---|
| 346 | if (!gb_dst_field) error = GB_await_error(); // failed to create? |
|---|
| 347 | } |
|---|
| 348 | if (!error) error = GB_copy_dropProtectMarksAndTempstate(gb_dst_field, gb_src_field); // [Note: if transfer_of_alignment -> copies 'data' field of one alignment] |
|---|
| 349 | if (!error && transfer_of_alignment) error = MG_adaptAllCopiedAlignments(rm, gb_src_species, gb_dst_species); // [Note: adapts all copied alignments, if adapt toggle is checked] |
|---|
| 350 | if (!error) GB_write_flag(gb_dst_species, 1); |
|---|
| 351 | } |
|---|
| 352 | return error; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | inline bool fieldContainsAlignment(const char *field) { |
|---|
| 356 | return GBS_string_matches(field, "ali_*", GB_MIND_CASE); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | static void transfer_field_of_listed_cb(AW_window *aww) { |
|---|
| 360 | if (MG_copy_and_check_alignments() != 0) return; |
|---|
| 361 | |
|---|
| 362 | AW_root *aw_root = aww->get_root(); |
|---|
| 363 | char *field = aw_root->awar(AWAR_FIELD_SRC)->read_string(); |
|---|
| 364 | const bool append = aw_root->awar(AWAR_APPEND)->read_int(); |
|---|
| 365 | GB_ERROR error = NULp; |
|---|
| 366 | |
|---|
| 367 | if (strcmp(field, NO_FIELD_SELECTED) == 0) { |
|---|
| 368 | error = "Please select the field you like to transfer"; |
|---|
| 369 | } |
|---|
| 370 | else if (strcmp(field, "name") == 0) { |
|---|
| 371 | error = "Transferring the 'name' field is forbidden"; |
|---|
| 372 | } |
|---|
| 373 | else { |
|---|
| 374 | const bool transfer_of_alignment = fieldContainsAlignment(field); |
|---|
| 375 | |
|---|
| 376 | if (transfer_of_alignment && append) { |
|---|
| 377 | error = "Appending alignment data is not permitted\n" |
|---|
| 378 | "(please refer to ARB/Sequence/Concatenate.. to do so)"; |
|---|
| 379 | } |
|---|
| 380 | else { |
|---|
| 381 | error = GB_begin_transaction(GLOBAL_gb_src); |
|---|
| 382 | if (!error) error = GB_begin_transaction(GLOBAL_gb_dst); |
|---|
| 383 | |
|---|
| 384 | if (!error) { |
|---|
| 385 | arb_progress progress("Transferring fields of listed", mg_count_queried(GLOBAL_gb_src)); |
|---|
| 386 | |
|---|
| 387 | MG_remaps rm(GLOBAL_gb_src, GLOBAL_gb_dst, adaption_enabled(aw_root), get_reference_species_names(aw_root)); // @@@ unused if transfer_of_alignment == false |
|---|
| 388 | |
|---|
| 389 | GBDATA *gb_dest_species_data = GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 390 | if (!gb_dest_species_data) error = GB_await_error(); |
|---|
| 391 | |
|---|
| 392 | for (GBDATA *gb_src_species = GBT_first_species(GLOBAL_gb_src); |
|---|
| 393 | gb_src_species && !error; |
|---|
| 394 | gb_src_species = GBT_next_species(gb_src_species)) |
|---|
| 395 | { |
|---|
| 396 | if (IS_QUERIED_SPECIES(gb_src_species)) { |
|---|
| 397 | const char *src_name = GBT_get_name(gb_src_species); |
|---|
| 398 | if (src_name) { |
|---|
| 399 | GBDATA *gb_dst_species = GB_find_string(gb_dest_species_data, "name", src_name, GB_IGNORE_CASE, SEARCH_GRANDCHILD); |
|---|
| 400 | |
|---|
| 401 | if (!gb_dst_species) { |
|---|
| 402 | gb_dst_species = GB_create_container(gb_dest_species_data, "species"); |
|---|
| 403 | if (!gb_dst_species) error = GB_await_error(); |
|---|
| 404 | else error = GBT_write_string(gb_dst_species, "name", src_name); |
|---|
| 405 | } |
|---|
| 406 | else { |
|---|
| 407 | gb_dst_species = GB_get_father(gb_dst_species); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | if (!error) { |
|---|
| 411 | // @@@ DRY vs inner part of body of transfer_field_of_selected_cb .@DRY_TRANSFER_FIELD |
|---|
| 412 | GBDATA *gb_src_field = GB_search(gb_src_species, field, GB_FIND); |
|---|
| 413 | |
|---|
| 414 | if (gb_src_field) { |
|---|
| 415 | error = transfer_single_field(gb_src_species, gb_dst_species, field, gb_src_field, append, transfer_of_alignment, rm); |
|---|
| 416 | } |
|---|
| 417 | } |
|---|
| 418 | if (error) error = GBS_global_string("%s (species=%s)", error, src_name); // mention name of species in error message |
|---|
| 419 | } |
|---|
| 420 | else { |
|---|
| 421 | error = "refusing to transfer from unnamed species"; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | progress.inc_and_check_user_abort(error); |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | if (!error) error = MG_transfer_fields_info(field); |
|---|
| 430 | |
|---|
| 431 | error = GB_end_transaction(GLOBAL_gb_src, error); |
|---|
| 432 | error = GB_end_transaction(GLOBAL_gb_dst, error); |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | if (error) aw_message(error); |
|---|
| 437 | free(field); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | static void transfer_field_of_selected_cb(AW_window *aww) { |
|---|
| 441 | if (MG_copy_and_check_alignments() != 0) return; |
|---|
| 442 | |
|---|
| 443 | AW_root *aw_root = aww->get_root(); |
|---|
| 444 | char *field = aw_root->awar(AWAR_FIELD_SRC)->read_string(); |
|---|
| 445 | const bool append = false; // @@@ provide via GUI? |
|---|
| 446 | GB_ERROR error = NULp; |
|---|
| 447 | |
|---|
| 448 | if (strcmp(field, NO_FIELD_SELECTED) == 0) { |
|---|
| 449 | error = "Please select the field you like to transfer"; |
|---|
| 450 | } |
|---|
| 451 | else if (strcmp(field, "name") == 0) { |
|---|
| 452 | error = "Transferring the 'name' field is forbidden"; |
|---|
| 453 | } |
|---|
| 454 | else { |
|---|
| 455 | const bool transfer_of_alignment = fieldContainsAlignment(field); |
|---|
| 456 | |
|---|
| 457 | if (transfer_of_alignment && append) { // @@@ append always false (see above) |
|---|
| 458 | error = "Appending alignment data is not permitted\n" |
|---|
| 459 | "(please refer to ARB/Sequence/Concatenate.. to do so)"; |
|---|
| 460 | } |
|---|
| 461 | else { |
|---|
| 462 | error = GB_begin_transaction(GLOBAL_gb_src); |
|---|
| 463 | if (!error) error = GB_begin_transaction(GLOBAL_gb_dst); |
|---|
| 464 | |
|---|
| 465 | if (!error) { |
|---|
| 466 | arb_progress progress("Cross copy field"); |
|---|
| 467 | |
|---|
| 468 | MG_remaps rm(GLOBAL_gb_src, GLOBAL_gb_dst, adaption_enabled(aw_root), get_reference_species_names(aw_root)); // @@@ unused if transfer_of_alignment == false |
|---|
| 469 | |
|---|
| 470 | GBDATA *gb_src_species; |
|---|
| 471 | GBDATA *gb_dst_species; |
|---|
| 472 | { |
|---|
| 473 | char *src_name = aw_root->awar(AWAR_SPECIES_SRC)->read_string(); |
|---|
| 474 | char *dst_name = aw_root->awar(AWAR_SPECIES_DST)->read_string(); |
|---|
| 475 | |
|---|
| 476 | gb_src_species = GBT_find_species(GLOBAL_gb_src, src_name); |
|---|
| 477 | gb_dst_species = GBT_find_species(GLOBAL_gb_dst, dst_name); |
|---|
| 478 | |
|---|
| 479 | free(dst_name); |
|---|
| 480 | free(src_name); |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | if (!gb_src_species) error = "Please select a species in left hitlist"; |
|---|
| 484 | if (!gb_dst_species) error = "Please select a species in right hitlist"; |
|---|
| 485 | |
|---|
| 486 | if (!error) { |
|---|
| 487 | // @@@ DRY vs loop-body of transfer_field_of_listed_cb .@DRY_TRANSFER_FIELD |
|---|
| 488 | GBDATA *gb_src_field = GB_search(gb_src_species, field, GB_FIND); |
|---|
| 489 | if (!gb_src_field) error = GBS_global_string("Source species has no field '%s'", field); |
|---|
| 490 | |
|---|
| 491 | if (!error) { |
|---|
| 492 | error = transfer_single_field(gb_src_species, gb_dst_species, field, gb_src_field, append, transfer_of_alignment, rm); |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | } |
|---|
| 496 | if (!error) error = MG_transfer_fields_info(field); |
|---|
| 497 | |
|---|
| 498 | error = GB_end_transaction(GLOBAL_gb_src, error); |
|---|
| 499 | error = GB_end_transaction(GLOBAL_gb_dst, error); |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | if (error) aw_message(error); |
|---|
| 504 | |
|---|
| 505 | free(field); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | static void popup_single_field_transfer_window(AW_root *aw_root, SpeciesXferScope scope) { |
|---|
| 509 | static AW_window *aww[2] = { NULp, NULp }; // its ok to store windows here (not handled by popper()) |
|---|
| 510 | |
|---|
| 511 | if (!aww[scope]) { |
|---|
| 512 | GB_transaction ta(GLOBAL_gb_src); |
|---|
| 513 | AW_window_simple *aws = new AW_window_simple; |
|---|
| 514 | |
|---|
| 515 | bool listed = scope == SXS_LISTED_SPECIES; |
|---|
| 516 | mg_assert(implicated(!listed, scope == SXS_SELECTED_SPECIES)); |
|---|
| 517 | |
|---|
| 518 | if (listed) aws->init(aw_root, "MERGE_XFER_FIELD_OF_LISTED", "Transfer field of listed"); |
|---|
| 519 | else aws->init(aw_root, "MERGE_XFER_SINGLE_FIELD", "Transfer field of selected"); |
|---|
| 520 | |
|---|
| 521 | aws->load_xfig("merge/mg_transfield.fig"); |
|---|
| 522 | aws->button_length(10); |
|---|
| 523 | |
|---|
| 524 | aws->callback(AW_POPDOWN); |
|---|
| 525 | aws->at("close"); |
|---|
| 526 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 527 | |
|---|
| 528 | aws->at("help"); |
|---|
| 529 | aws->callback(makeHelpCallback(listed ? "mg_xfer_field_of_listed.hlp" : "mg_xfer_field_of_sel.hlp")); |
|---|
| 530 | aws->create_button("HELP", "HELP"); |
|---|
| 531 | |
|---|
| 532 | if (listed) { |
|---|
| 533 | aws->at("append"); |
|---|
| 534 | aws->label("Append data?"); |
|---|
| 535 | aws->create_toggle(AWAR_APPEND); |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | long typeFilter = listed ? FIELD_FILTER_ANY_FIELD : FIELD_UNFILTERED; |
|---|
| 539 | create_itemfield_selection_button(aws, FieldSelDef(AWAR_FIELD_SRC, GLOBAL_gb_src, SPECIES_get_selector(), typeFilter, "source/target field"), "scandb"); |
|---|
| 540 | |
|---|
| 541 | aws->at("go"); |
|---|
| 542 | aws->callback(listed ? transfer_field_of_listed_cb : transfer_field_of_selected_cb); |
|---|
| 543 | aws->create_button("GO", "GO"); |
|---|
| 544 | |
|---|
| 545 | aww[scope] = aws; |
|---|
| 546 | } |
|---|
| 547 | aww[scope]->activate(); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | static void MG_merge_tagged_field_cb(AW_window *aww) { |
|---|
| 551 | GB_transaction ta_merge(GLOBAL_gb_src); |
|---|
| 552 | GB_ERROR error = GB_begin_transaction(GLOBAL_gb_dst); |
|---|
| 553 | |
|---|
| 554 | if (!error) { |
|---|
| 555 | AW_root *awr = aww->get_root(); |
|---|
| 556 | |
|---|
| 557 | char *src_field = awr->awar(AWAR_FIELD_SRC)->read_string(); |
|---|
| 558 | char *dst_field = awr->awar(AWAR_FIELD_DST)->read_string(); |
|---|
| 559 | |
|---|
| 560 | if (strcmp(src_field, NO_FIELD_SELECTED) == 0 || |
|---|
| 561 | strcmp(dst_field, NO_FIELD_SELECTED) == 0) |
|---|
| 562 | { |
|---|
| 563 | error = "Please select source- and target-fields."; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | if (!error) { |
|---|
| 567 | char *src_tag = awr->awar(AWAR_TAG_SRC)->read_string(); |
|---|
| 568 | char *dst_tag = awr->awar(AWAR_TAG_DST)->read_string(); |
|---|
| 569 | char *del_tag = awr->awar(AWAR_TAG_DEL)->read_string(); |
|---|
| 570 | |
|---|
| 571 | arb_progress progress("Merging tagged fields", mg_count_queried(GLOBAL_gb_src)); |
|---|
| 572 | |
|---|
| 573 | GBDATA *gb_dest_species_data = GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 574 | if (!gb_dest_species_data) error = GB_await_error(); |
|---|
| 575 | else { |
|---|
| 576 | for (GBDATA *gb_src_species = GBT_first_species(GLOBAL_gb_src); |
|---|
| 577 | gb_src_species && !error; |
|---|
| 578 | gb_src_species = GBT_next_species(gb_src_species)) |
|---|
| 579 | { |
|---|
| 580 | if (IS_QUERIED_SPECIES(gb_src_species)) { |
|---|
| 581 | char *name = GBT_read_string(gb_src_species, "name"); |
|---|
| 582 | if (!name) error = GB_await_error(); |
|---|
| 583 | else { |
|---|
| 584 | GBDATA *gb_dst_species = GBT_find_or_create_species_rel_species_data(gb_dest_species_data, name, true); |
|---|
| 585 | if (!gb_dst_species) error = GB_await_error(); |
|---|
| 586 | else { |
|---|
| 587 | char *src_val = GBT_readOrCreate_string(gb_src_species, src_field, ""); |
|---|
| 588 | char *dst_val = GBT_readOrCreate_string(gb_dst_species, dst_field, ""); |
|---|
| 589 | |
|---|
| 590 | if (!src_val || !dst_val) error = GB_await_error(); |
|---|
| 591 | else { |
|---|
| 592 | char *sum = GBS_merge_tagged_strings(src_val, src_tag, del_tag, |
|---|
| 593 | dst_val, dst_tag, del_tag); |
|---|
| 594 | |
|---|
| 595 | if (!sum) error = GB_await_error(); |
|---|
| 596 | else error = GBT_write_string(gb_dst_species, dst_field, sum); |
|---|
| 597 | free(sum); |
|---|
| 598 | } |
|---|
| 599 | free(dst_val); |
|---|
| 600 | free(src_val); |
|---|
| 601 | } |
|---|
| 602 | } |
|---|
| 603 | free(name); |
|---|
| 604 | progress.inc_and_check_user_abort(error); |
|---|
| 605 | } |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | if (error) progress.done(); |
|---|
| 610 | |
|---|
| 611 | free(del_tag); |
|---|
| 612 | free(dst_tag); |
|---|
| 613 | free(src_tag); |
|---|
| 614 | } |
|---|
| 615 | free(dst_field); |
|---|
| 616 | free(src_field); |
|---|
| 617 | } |
|---|
| 618 | GB_end_transaction_show_error(GLOBAL_gb_dst, error, aw_message); |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | static AW_window *create_mg_merge_tagged_fields_window(AW_root *aw_root) { |
|---|
| 622 | static AW_window_simple *aws = NULp; |
|---|
| 623 | if (aws) return aws; |
|---|
| 624 | |
|---|
| 625 | GB_transaction ta(GLOBAL_gb_src); |
|---|
| 626 | |
|---|
| 627 | aw_root->awar_string(AWAR_TAG_SRC, "S"); |
|---|
| 628 | aw_root->awar_string(AWAR_TAG_DST, "D"); |
|---|
| 629 | |
|---|
| 630 | aw_root->awar_string(AWAR_TAG_DEL, "S*"); |
|---|
| 631 | |
|---|
| 632 | aws = new AW_window_simple; |
|---|
| 633 | aws->init(aw_root, "MERGE_TAGGED_FIELD", "Merge tagged field"); |
|---|
| 634 | aws->load_xfig("merge/mg_mergetaggedfield.fig"); |
|---|
| 635 | aws->button_length(13); |
|---|
| 636 | |
|---|
| 637 | aws->callback(AW_POPDOWN); |
|---|
| 638 | aws->at("close"); |
|---|
| 639 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 640 | |
|---|
| 641 | aws->at("go"); |
|---|
| 642 | aws->callback(MG_merge_tagged_field_cb); |
|---|
| 643 | aws->create_button("GO", "GO"); |
|---|
| 644 | |
|---|
| 645 | aws->at("help"); |
|---|
| 646 | aws->callback(makeHelpCallback("mergetaggedfield.hlp")); |
|---|
| 647 | aws->create_button("HELP", "HELP"); |
|---|
| 648 | |
|---|
| 649 | aws->at("tag1"); aws->create_input_field(AWAR_TAG_SRC, 5); |
|---|
| 650 | aws->at("tag2"); aws->create_input_field(AWAR_TAG_DST, 5); |
|---|
| 651 | |
|---|
| 652 | aws->at("del1"); aws->create_input_field(AWAR_TAG_DEL, 5); |
|---|
| 653 | |
|---|
| 654 | create_itemfield_selection_button(aws, FieldSelDef(AWAR_FIELD_SRC, GLOBAL_gb_src, SPECIES_get_selector(), FIELD_FILTER_STRING, "source-field"), "fields1"); |
|---|
| 655 | create_itemfield_selection_button(aws, FieldSelDef(AWAR_FIELD_DST, GLOBAL_gb_dst, SPECIES_get_selector(), FIELD_FILTER_STRING, "target-field"), "fields2"); |
|---|
| 656 | |
|---|
| 657 | return aws; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | static GB_ERROR MG_equal_alignments(bool autoselect_equal_alignment_name) { |
|---|
| 661 | //! Make the alignment names equal |
|---|
| 662 | ConstStrArray S_alignment_names; |
|---|
| 663 | ConstStrArray D_alignment_names; |
|---|
| 664 | GBT_get_alignment_names(S_alignment_names, GLOBAL_gb_src); |
|---|
| 665 | GBT_get_alignment_names(D_alignment_names, GLOBAL_gb_dst); |
|---|
| 666 | |
|---|
| 667 | GB_ERROR error = NULp; |
|---|
| 668 | if (!S_alignment_names[0]) { |
|---|
| 669 | error = GB_export_error("No source sequences found"); |
|---|
| 670 | } |
|---|
| 671 | else { |
|---|
| 672 | char *src_type = GBT_get_alignment_type_string(GLOBAL_gb_src, S_alignment_names[0]); |
|---|
| 673 | const char *dest = NULp; |
|---|
| 674 | |
|---|
| 675 | for (int d = D_alignment_names.size()-1; d>0; --d) { |
|---|
| 676 | char *dst_type = GBT_get_alignment_type_string(GLOBAL_gb_dst, D_alignment_names[d]); |
|---|
| 677 | if (strcmp(src_type, dst_type) != 0) D_alignment_names.remove(d--); |
|---|
| 678 | free(dst_type); |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | int d = D_alignment_names.size(); |
|---|
| 682 | switch (d) { |
|---|
| 683 | case 0: |
|---|
| 684 | error = GB_export_errorf("Cannot find a target alignment with a type of '%s'\n" |
|---|
| 685 | "You should create one first or select a different alignment type\n" |
|---|
| 686 | "during sequence import", src_type); |
|---|
| 687 | break; |
|---|
| 688 | case 1: |
|---|
| 689 | dest = D_alignment_names[0]; |
|---|
| 690 | break; |
|---|
| 691 | |
|---|
| 692 | default: |
|---|
| 693 | if (autoselect_equal_alignment_name) { |
|---|
| 694 | for (int i = 0; i<d; ++i) { |
|---|
| 695 | if (ARB_stricmp(S_alignment_names[0], D_alignment_names[i]) == 0) { |
|---|
| 696 | dest = D_alignment_names[i]; |
|---|
| 697 | break; |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | if (!dest) { |
|---|
| 703 | GBS_strstruct buttonstr(100); |
|---|
| 704 | |
|---|
| 705 | for (int i=0; i<d; i++) { |
|---|
| 706 | buttonstr.cat(D_alignment_names[i]); |
|---|
| 707 | buttonstr.put(','); |
|---|
| 708 | } |
|---|
| 709 | buttonstr.cat("ABORT"); |
|---|
| 710 | |
|---|
| 711 | int aliid = aw_question(NULp, |
|---|
| 712 | "There are more than one possible alignment targets\n" |
|---|
| 713 | "Choose one destination alignment or ABORT", |
|---|
| 714 | buttonstr.get_data()); |
|---|
| 715 | |
|---|
| 716 | if (aliid >= d) { |
|---|
| 717 | error = "Operation Aborted"; |
|---|
| 718 | break; |
|---|
| 719 | } |
|---|
| 720 | dest = D_alignment_names[aliid]; |
|---|
| 721 | } |
|---|
| 722 | break; |
|---|
| 723 | } |
|---|
| 724 | if (!error && dest && strcmp(S_alignment_names[0], dest) != 0) { |
|---|
| 725 | error = GBT_rename_alignment(GLOBAL_gb_src, S_alignment_names[0], dest); |
|---|
| 726 | if (error) { |
|---|
| 727 | error = GBS_global_string("Failed to rename alignment '%s' to '%s' (Reason: %s)", |
|---|
| 728 | S_alignment_names[0], dest, error); |
|---|
| 729 | } |
|---|
| 730 | else { |
|---|
| 731 | error = GBT_add_alignment_changekeys(GLOBAL_gb_src, dest); |
|---|
| 732 | } |
|---|
| 733 | } |
|---|
| 734 | free(src_type); |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | return error; |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | GB_ERROR MERGE_sequences_simple(AW_root *awr) { |
|---|
| 741 | //! Merge the sequences of two databases |
|---|
| 742 | |
|---|
| 743 | static char *s_name = NULp; |
|---|
| 744 | |
|---|
| 745 | GB_HASH *D_species_hash = NULp; |
|---|
| 746 | |
|---|
| 747 | GB_topSecurityLevel srcUnsecured(GLOBAL_gb_src); |
|---|
| 748 | GB_topSecurityLevel dstUnsecured(GLOBAL_gb_dst); |
|---|
| 749 | GB_transaction taSrc(GLOBAL_gb_src); |
|---|
| 750 | GB_transaction taDst(GLOBAL_gb_dst); |
|---|
| 751 | |
|---|
| 752 | GB_ERROR error = MG_equal_alignments(true); |
|---|
| 753 | if (!error) { |
|---|
| 754 | GBDATA *S_species_data = GBT_get_species_data(GLOBAL_gb_src); |
|---|
| 755 | GBDATA *D_species_data = GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 756 | |
|---|
| 757 | freenull(s_name); |
|---|
| 758 | |
|---|
| 759 | { |
|---|
| 760 | long S_species_count = GB_number_of_subentries(S_species_data); |
|---|
| 761 | long D_species_count = GB_number_of_subentries(D_species_data); |
|---|
| 762 | |
|---|
| 763 | // create hash containing all species from gb_dst, |
|---|
| 764 | // but sized to hold all species from both DBs: |
|---|
| 765 | D_species_hash = GBT_create_species_hash_sized(GLOBAL_gb_dst, S_species_count+D_species_count); |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | bool overwriteall = false; |
|---|
| 769 | bool autorenameall = false; |
|---|
| 770 | |
|---|
| 771 | for (GBDATA *S_species = GB_entry(S_species_data, "species"); S_species; S_species = GB_nextEntry(S_species)) { |
|---|
| 772 | GBDATA *S_name = GB_search(S_species, "name", GB_STRING); |
|---|
| 773 | |
|---|
| 774 | freeset(s_name, GB_read_string(S_name)); |
|---|
| 775 | |
|---|
| 776 | int count = 1; |
|---|
| 777 | bool retry; |
|---|
| 778 | do { |
|---|
| 779 | retry = false; |
|---|
| 780 | count++; |
|---|
| 781 | |
|---|
| 782 | GBDATA *D_species = (GBDATA*)GBS_read_hash(D_species_hash, s_name); |
|---|
| 783 | if (D_species) { |
|---|
| 784 | if (overwriteall) { |
|---|
| 785 | error = GB_delete(D_species); |
|---|
| 786 | } |
|---|
| 787 | else if (autorenameall) { |
|---|
| 788 | GB_ERROR dummy; |
|---|
| 789 | char *newname = AWTC_create_numbered_suffix(D_species_hash, s_name, dummy); |
|---|
| 790 | |
|---|
| 791 | mg_assert(newname); |
|---|
| 792 | freeset(s_name, newname); |
|---|
| 793 | } |
|---|
| 794 | else { |
|---|
| 795 | switch (aw_question("merge_existing_species", |
|---|
| 796 | GBS_global_string("Warning: There is an ID conflict for species '%s'\n" |
|---|
| 797 | " You may:\n" |
|---|
| 798 | " - Overwrite existing species\n" |
|---|
| 799 | " - Overwrite all species with ID conflicts\n" |
|---|
| 800 | " - Not import species\n" |
|---|
| 801 | " - Enter ID for imported species\n" |
|---|
| 802 | " - Automatically create unique species IDs (append .NUM)\n" |
|---|
| 803 | " - Abort everything", s_name), |
|---|
| 804 | "overwrite, overwrite all, don't import, create ID, auto-create IDs, abort")) { |
|---|
| 805 | case 1: |
|---|
| 806 | overwriteall = true; |
|---|
| 807 | FALLTHROUGH; |
|---|
| 808 | case 0: |
|---|
| 809 | GB_delete(D_species); |
|---|
| 810 | break; |
|---|
| 811 | |
|---|
| 812 | case 2: |
|---|
| 813 | continue; |
|---|
| 814 | |
|---|
| 815 | case 3: { |
|---|
| 816 | GB_ERROR warning; // duplicated species warning (does not apply here) |
|---|
| 817 | char *autoname = AWTC_create_numbered_suffix(D_species_hash, s_name, warning); |
|---|
| 818 | |
|---|
| 819 | if (!autoname) autoname = ARB_strdup(s_name); |
|---|
| 820 | freeset(s_name, aw_input("Species ID", "Enter new species ID", autoname)); |
|---|
| 821 | free(autoname); |
|---|
| 822 | retry = true; |
|---|
| 823 | break; |
|---|
| 824 | } |
|---|
| 825 | case 4: |
|---|
| 826 | autorenameall = true; |
|---|
| 827 | retry = true; |
|---|
| 828 | break; |
|---|
| 829 | |
|---|
| 830 | case 5: |
|---|
| 831 | error = "Operation aborted"; |
|---|
| 832 | break; |
|---|
| 833 | } |
|---|
| 834 | } |
|---|
| 835 | } |
|---|
| 836 | } while (retry); |
|---|
| 837 | |
|---|
| 838 | if (!error) { |
|---|
| 839 | GBDATA *D_species = GB_create_container(D_species_data, "species"); |
|---|
| 840 | if (!D_species) error = GB_await_error(); |
|---|
| 841 | else { |
|---|
| 842 | error = GB_copy_dropMarksAndTempstate(D_species, S_species); |
|---|
| 843 | |
|---|
| 844 | if (!error) { |
|---|
| 845 | GB_write_flag(D_species, 1); // mark species |
|---|
| 846 | GB_raise_user_flag(D_species, GB_USERFLAG_QUERY); // put in search&query hitlist |
|---|
| 847 | error = GBT_write_string(D_species, "name", s_name); |
|---|
| 848 | } |
|---|
| 849 | } |
|---|
| 850 | |
|---|
| 851 | GBS_write_hash(D_species_hash, s_name, (long)D_species); |
|---|
| 852 | } |
|---|
| 853 | |
|---|
| 854 | if (error) break; |
|---|
| 855 | } |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | if (D_species_hash) GBS_free_hash(D_species_hash); |
|---|
| 859 | |
|---|
| 860 | if (!error) error = MG_transfer_fields_info(); |
|---|
| 861 | if (!error) awr->awar(AWAR_SPECIES_NAME)->write_string(s_name); |
|---|
| 862 | |
|---|
| 863 | error = taSrc.close(error); |
|---|
| 864 | error = taDst.close(error); |
|---|
| 865 | |
|---|
| 866 | aw_message_if(error); |
|---|
| 867 | return error; |
|---|
| 868 | } |
|---|
| 869 | |
|---|
| 870 | // ----------------------------- |
|---|
| 871 | // MG_species_selector |
|---|
| 872 | |
|---|
| 873 | static void mg_select_species1(GBDATA*, AW_root *aw_root, const char *item_name) { |
|---|
| 874 | aw_root->awar(AWAR_SPECIES_SRC)->write_string(item_name); |
|---|
| 875 | } |
|---|
| 876 | static void mg_select_species2(GBDATA*, AW_root *aw_root, const char *item_name) { |
|---|
| 877 | aw_root->awar(AWAR_SPECIES_DST)->write_string(item_name); |
|---|
| 878 | } |
|---|
| 879 | |
|---|
| 880 | static GBDATA *mg_get_first_species_data1(GBDATA*, AW_root*, QUERY_RANGE) { |
|---|
| 881 | return GBT_get_species_data(GLOBAL_gb_src); |
|---|
| 882 | } |
|---|
| 883 | static GBDATA *mg_get_first_species_data2(GBDATA*, AW_root*, QUERY_RANGE) { |
|---|
| 884 | return GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 885 | } |
|---|
| 886 | |
|---|
| 887 | static GBDATA *mg_get_selected_species1(GBDATA*, AW_root *aw_root) { |
|---|
| 888 | GB_transaction ta(GLOBAL_gb_src); |
|---|
| 889 | const char *species_name = aw_root->awar(AWAR_SPECIES_SRC)->read_char_pntr(); |
|---|
| 890 | return species_name[0] ? GBT_find_species(GLOBAL_gb_src, species_name) : NULp; |
|---|
| 891 | } |
|---|
| 892 | static GBDATA *mg_get_selected_species2(GBDATA*, AW_root *aw_root) { |
|---|
| 893 | GB_transaction ta(GLOBAL_gb_dst); |
|---|
| 894 | const char *species_name = aw_root->awar(AWAR_SPECIES_DST)->read_char_pntr(); |
|---|
| 895 | return species_name[0] ? GBT_find_species(GLOBAL_gb_dst, species_name) : NULp; |
|---|
| 896 | } |
|---|
| 897 | |
|---|
| 898 | static MutableItemSelector MG_species_selector[2]; |
|---|
| 899 | |
|---|
| 900 | static void mg_initialize_species_selectors() { |
|---|
| 901 | static bool initialized = false; |
|---|
| 902 | if (!initialized) { |
|---|
| 903 | MG_species_selector[0] = MG_species_selector[1] = SPECIES_get_selector(); |
|---|
| 904 | |
|---|
| 905 | for (int s = 0; s <= 1; ++s) { |
|---|
| 906 | MutableItemSelector& sel = MG_species_selector[s]; |
|---|
| 907 | |
|---|
| 908 | sel.update_item_awars = s ? mg_select_species2 : mg_select_species1; |
|---|
| 909 | sel.get_first_item_container = s ? mg_get_first_species_data2 : mg_get_first_species_data1; |
|---|
| 910 | sel.get_selected_item = s ? mg_get_selected_species2 : mg_get_selected_species1; |
|---|
| 911 | |
|---|
| 912 | sel.items_name = sel.item_name = s ? "target species" : "source species"; |
|---|
| 913 | } |
|---|
| 914 | |
|---|
| 915 | initialized = true; |
|---|
| 916 | } |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | class ScopedTransporter { // @@@ later also use for old merge types (SXT_WHOLE_SPECIES + SXT_SINGLE_FIELD) |
|---|
| 920 | static QUERY::DbQuery *query2update; |
|---|
| 921 | |
|---|
| 922 | GBDATA *expect_selected_species(GBDATA *gb_main, const char *awarname_selected, const char *species_role, const char *listLocation, GB_ERROR& error, bool acceptNone) { |
|---|
| 923 | GBDATA *gb_species = NULp; |
|---|
| 924 | if (!error) { |
|---|
| 925 | const char *species_name = AW_root::SINGLETON->awar(awarname_selected)->read_char_pntr(); |
|---|
| 926 | if (species_name[0]) { |
|---|
| 927 | gb_species = GBT_expect_species(gb_main, species_name); |
|---|
| 928 | if (!gb_species) { |
|---|
| 929 | error = GBS_global_string("%s (selected in %s hitlist)", GB_await_error(), listLocation); |
|---|
| 930 | } |
|---|
| 931 | } |
|---|
| 932 | else { |
|---|
| 933 | if (!acceptNone) { |
|---|
| 934 | error = GBS_global_string("Please select a %s species in %s hitlist", species_role, listLocation); |
|---|
| 935 | } |
|---|
| 936 | } |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | mg_assert(implicated(!acceptNone, contradicted(error, gb_species))); |
|---|
| 940 | mg_assert(!(error && gb_species)); |
|---|
| 941 | |
|---|
| 942 | return gb_species; |
|---|
| 943 | } |
|---|
| 944 | |
|---|
| 945 | GB_ERROR transferExistingOrNew(GBDATA *gb_src_item, GBDATA *gb_dst_item_container) { |
|---|
| 946 | GB_ERROR error = NULp; |
|---|
| 947 | const char *name = GBT_get_name(gb_src_item); |
|---|
| 948 | |
|---|
| 949 | if (!name) { |
|---|
| 950 | error = "unnamed item"; |
|---|
| 951 | } |
|---|
| 952 | else { |
|---|
| 953 | GBDATA *gb_dst_item = GBT_find_species_rel_species_data(gb_dst_item_container, name); |
|---|
| 954 | if (gb_dst_item) { // found existing species |
|---|
| 955 | error = transferOne(gb_src_item, gb_dst_item, NULp); // use it as target |
|---|
| 956 | } |
|---|
| 957 | else { // not found |
|---|
| 958 | if (GB_have_error()) { // due to error |
|---|
| 959 | error = GB_await_error(); |
|---|
| 960 | } |
|---|
| 961 | else { // otherwise create new species |
|---|
| 962 | error = transferOne(gb_src_item, NULp, gb_dst_item_container); |
|---|
| 963 | } |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | return error; |
|---|
| 967 | } |
|---|
| 968 | |
|---|
| 969 | protected: |
|---|
| 970 | static void markTarget(GBDATA *gb_dst_item) { |
|---|
| 971 | mg_assert(GB_is_ancestor_of(GLOBAL_gb_dst, gb_dst_item)); // only modify userflag of destination(!) database |
|---|
| 972 | |
|---|
| 973 | GB_write_flag(gb_dst_item, 1); // mark species |
|---|
| 974 | GB_raise_user_flag(gb_dst_item, GB_USERFLAG_QUERY); // put in search&query (destination) hitlist |
|---|
| 975 | } |
|---|
| 976 | |
|---|
| 977 | public: |
|---|
| 978 | virtual ~ScopedTransporter() {} |
|---|
| 979 | |
|---|
| 980 | static void set_query_to_update(QUERY::DbQuery *query) { query2update = query; } |
|---|
| 981 | |
|---|
| 982 | virtual GB_ERROR transferOne(GBDATA *gb_src_item, GBDATA *gb_dst_item, GBDATA *gb_dst_item_container) = 0; |
|---|
| 983 | /*! transfer one species (used when transferring selected/listed species) |
|---|
| 984 | * @param gb_src_item source of transfer |
|---|
| 985 | * @param gb_dst_item target of transfer (if not NULp) |
|---|
| 986 | * @param gb_dst_item_container transfer target will be created inside this container (if gb_dst_item == NULp) |
|---|
| 987 | */ |
|---|
| 988 | |
|---|
| 989 | GB_ERROR transferAllIn(SpeciesXferScope scope, const char *progressTitle) { |
|---|
| 990 | GB_ERROR error = GB_begin_transaction(GLOBAL_gb_src); |
|---|
| 991 | error = GB_begin_transaction(GLOBAL_gb_dst); |
|---|
| 992 | |
|---|
| 993 | { |
|---|
| 994 | unsigned long scopeSize = 0; |
|---|
| 995 | switch (scope) { |
|---|
| 996 | case SXS_SELECTED_SPECIES: scopeSize = 1; break; |
|---|
| 997 | case SXS_LISTED_SPECIES: scopeSize = mg_count_queried(GLOBAL_gb_src); break; |
|---|
| 998 | } |
|---|
| 999 | |
|---|
| 1000 | arb_progress progress(progressTitle, scopeSize); |
|---|
| 1001 | |
|---|
| 1002 | GBDATA *gb_dst_species_container = GBT_get_species_data(GLOBAL_gb_dst); |
|---|
| 1003 | if (!error) { |
|---|
| 1004 | switch (scope) { |
|---|
| 1005 | case SXS_SELECTED_SPECIES: { |
|---|
| 1006 | GBDATA *gb_src_species = expect_selected_species(GLOBAL_gb_src, AWAR_SPECIES_SRC, "source", "left", error, false); |
|---|
| 1007 | GBDATA *gb_dst_species = expect_selected_species(GLOBAL_gb_dst, AWAR_SPECIES_DST, "target", "right", error, true); |
|---|
| 1008 | |
|---|
| 1009 | if (!error) { |
|---|
| 1010 | if (gb_dst_species) { // source and target explicitely selected |
|---|
| 1011 | error = transferOne(gb_src_species, gb_dst_species, NULp); |
|---|
| 1012 | } |
|---|
| 1013 | else { |
|---|
| 1014 | error = transferExistingOrNew(gb_src_species, gb_dst_species_container); |
|---|
| 1015 | } |
|---|
| 1016 | progress.inc_and_check_user_abort(error); |
|---|
| 1017 | } |
|---|
| 1018 | break; |
|---|
| 1019 | } |
|---|
| 1020 | |
|---|
| 1021 | case SXS_LISTED_SPECIES: { |
|---|
| 1022 | for (GBDATA *gb_src_species = GBT_first_species(GLOBAL_gb_src); |
|---|
| 1023 | gb_src_species && !error; |
|---|
| 1024 | gb_src_species = GBT_next_species(gb_src_species)) |
|---|
| 1025 | { |
|---|
| 1026 | if (IS_QUERIED_SPECIES(gb_src_species)) { |
|---|
| 1027 | error = transferExistingOrNew(gb_src_species, gb_dst_species_container); |
|---|
| 1028 | if (error) { // mention name of species in error message: |
|---|
| 1029 | error = GBS_global_string("%s (species=%s)", error, GBT_get_name_or_description(gb_src_species)); |
|---|
| 1030 | } |
|---|
| 1031 | progress.inc_and_check_user_abort(error); |
|---|
| 1032 | } |
|---|
| 1033 | } |
|---|
| 1034 | break; |
|---|
| 1035 | } |
|---|
| 1036 | } |
|---|
| 1037 | } |
|---|
| 1038 | |
|---|
| 1039 | if (error) progress.done(); |
|---|
| 1040 | } |
|---|
| 1041 | error = GB_end_transaction(GLOBAL_gb_src, error); |
|---|
| 1042 | error = GB_end_transaction(GLOBAL_gb_dst, error); |
|---|
| 1043 | |
|---|
| 1044 | if (!error && query2update) DbQuery_update_list(query2update); // no need to update in case of error |
|---|
| 1045 | |
|---|
| 1046 | return error; |
|---|
| 1047 | } |
|---|
| 1048 | }; |
|---|
| 1049 | |
|---|
| 1050 | QUERY::DbQuery *ScopedTransporter::query2update = NULp; |
|---|
| 1051 | |
|---|
| 1052 | using namespace FieldTransfer; |
|---|
| 1053 | |
|---|
| 1054 | typedef SmartPtr<MG_remaps> MG_remapsPtr; |
|---|
| 1055 | |
|---|
| 1056 | class AdaptedAlignmentTransporter : public AlignmentTransporter { |
|---|
| 1057 | MG_remapsPtr remaps; |
|---|
| 1058 | |
|---|
| 1059 | public: |
|---|
| 1060 | AdaptedAlignmentTransporter(MG_remapsPtr remaps_) : remaps(remaps_) {} |
|---|
| 1061 | bool shallCopyBefore() const OVERRIDE { |
|---|
| 1062 | return true; // ItemClonedByRuleSet::copyAlignments() shall be called b4 calling transport() |
|---|
| 1063 | } |
|---|
| 1064 | GB_ERROR transport(GBDATA *gb_src_item, GBDATA *gb_dst_item) const OVERRIDE { |
|---|
| 1065 | return MG_adaptAllCopiedAlignments(*remaps, gb_src_item, gb_dst_item); |
|---|
| 1066 | } |
|---|
| 1067 | }; |
|---|
| 1068 | |
|---|
| 1069 | struct DontTransportAlignment : public AlignmentTransporter { |
|---|
| 1070 | // this AlignmentTransporter simply does not transport anything |
|---|
| 1071 | bool shallCopyBefore() const OVERRIDE { return false; } // do not call copyAlignments() b4 calling transport() |
|---|
| 1072 | GB_ERROR transport(GBDATA *, GBDATA *) const OVERRIDE { return NULp; } // do not transport alignment data |
|---|
| 1073 | }; |
|---|
| 1074 | |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | class ViaFtsTransporter : public ScopedTransporter { |
|---|
| 1078 | RuleSetPtr fts; |
|---|
| 1079 | AlignmentTransporterPtr aliTransporter; |
|---|
| 1080 | |
|---|
| 1081 | public: |
|---|
| 1082 | ViaFtsTransporter(RuleSetPtr fts_, AlignmentTransporterPtr transp) : |
|---|
| 1083 | fts(fts_), |
|---|
| 1084 | aliTransporter(transp) |
|---|
| 1085 | {} |
|---|
| 1086 | GB_ERROR transferOne(GBDATA *gb_src_item, GBDATA *gb_dst_item, GBDATA *gb_dst_item_container) OVERRIDE { |
|---|
| 1087 | mg_assert(fts.isSet()); |
|---|
| 1088 | mg_assert(contradicted(gb_dst_item, gb_dst_item_container)); // exactly one of both shall be set |
|---|
| 1089 | |
|---|
| 1090 | GB_ERROR error = NULp; |
|---|
| 1091 | GBDATA *gb_transferred = NULp; |
|---|
| 1092 | { |
|---|
| 1093 | ItemClonedByRuleSet transfer(gb_src_item, CLONE_ITEM_SPECIES, fts, |
|---|
| 1094 | gb_dst_item ? CLONE_INTO_EXISTING : REAL_CLONE, |
|---|
| 1095 | gb_dst_item ? gb_dst_item : gb_dst_item_container, |
|---|
| 1096 | &*aliTransporter); |
|---|
| 1097 | |
|---|
| 1098 | if (transfer.has_error()) { |
|---|
| 1099 | error = transfer.get_error(); |
|---|
| 1100 | } |
|---|
| 1101 | else { |
|---|
| 1102 | gb_transferred = transfer.get_clone(); |
|---|
| 1103 | } |
|---|
| 1104 | } |
|---|
| 1105 | |
|---|
| 1106 | mg_assert(contradicted(error, gb_transferred)); |
|---|
| 1107 | if (!error) markTarget(gb_transferred); |
|---|
| 1108 | |
|---|
| 1109 | return error; |
|---|
| 1110 | } |
|---|
| 1111 | }; |
|---|
| 1112 | |
|---|
| 1113 | static void mg_xfer_via_fts(AW_root *aw_root, SpeciesXferScope scope, bool xfer_seq_data) { |
|---|
| 1114 | if (MG_copy_and_check_alignments() != 0) return; |
|---|
| 1115 | |
|---|
| 1116 | GB_ERROR error = NULp; |
|---|
| 1117 | const char *ftsname = XFER_getFullFTS(aw_root->awar(AWAR_SPECIES_XFER_FTS)->read_char_pntr()); |
|---|
| 1118 | |
|---|
| 1119 | if (!ftsname[0]) { |
|---|
| 1120 | error = "No FTS selected"; |
|---|
| 1121 | } |
|---|
| 1122 | else { |
|---|
| 1123 | ErrorOrRuleSetPtr loaded = RuleSet::loadFrom(ftsname); |
|---|
| 1124 | if (loaded.hasError()) { |
|---|
| 1125 | ARB_ERROR lerror = loaded.getError(); |
|---|
| 1126 | error = lerror.deliver(); |
|---|
| 1127 | } |
|---|
| 1128 | else { |
|---|
| 1129 | AlignmentTransporterPtr transport_ali; |
|---|
| 1130 | if (xfer_seq_data) { |
|---|
| 1131 | MG_remapsPtr remaps; |
|---|
| 1132 | { |
|---|
| 1133 | GB_transaction ta_src(GLOBAL_gb_src); |
|---|
| 1134 | GB_transaction ta_dst(GLOBAL_gb_dst); |
|---|
| 1135 | remaps = new MG_remaps(GLOBAL_gb_src, GLOBAL_gb_dst, adaption_enabled(aw_root), get_reference_species_names(aw_root)); // @@@ use factory to make MG_remaps / MG_remapsPtr |
|---|
| 1136 | // @@@ can MG_remaps report error? (e.g. if wrong SAI/species specified as reference) |
|---|
| 1137 | } |
|---|
| 1138 | |
|---|
| 1139 | transport_ali = new AdaptedAlignmentTransporter(remaps); |
|---|
| 1140 | } |
|---|
| 1141 | else { |
|---|
| 1142 | transport_ali = new DontTransportAlignment; |
|---|
| 1143 | } |
|---|
| 1144 | |
|---|
| 1145 | ViaFtsTransporter transporter(loaded.getValue(), transport_ali); |
|---|
| 1146 | error = transporter.transferAllIn(scope, "Transfer using FTS"); |
|---|
| 1147 | } |
|---|
| 1148 | } |
|---|
| 1149 | aw_message_if(error); |
|---|
| 1150 | } |
|---|
| 1151 | |
|---|
| 1152 | struct MergeFieldScanner : public AvailableFieldScanner { |
|---|
| 1153 | void scanFields(StrArray& fields, FieldsToScan whatToScan) const OVERRIDE { |
|---|
| 1154 | if (whatToScan & SCAN_INPUT_FIELDS) { |
|---|
| 1155 | collectKeysRegisteredInDatabase(fields, GLOBAL_gb_src, SPECIES_get_selector(), true, false); |
|---|
| 1156 | } |
|---|
| 1157 | if (whatToScan & SCAN_OUTPUT_FIELDS) { |
|---|
| 1158 | collectKeysRegisteredInDatabase(fields, GLOBAL_gb_dst, SPECIES_get_selector(), true, false); |
|---|
| 1159 | } |
|---|
| 1160 | } |
|---|
| 1161 | }; |
|---|
| 1162 | |
|---|
| 1163 | static MergeFieldScanner fieldScanner; |
|---|
| 1164 | |
|---|
| 1165 | static void MG_transfer_species(AW_window *aww) { |
|---|
| 1166 | AW_root *awr = aww->get_root(); |
|---|
| 1167 | |
|---|
| 1168 | SpeciesXferType type = SpeciesXferType (awr->awar(AWAR_SPECIES_XFER_TYPE) ->read_int()); |
|---|
| 1169 | SpeciesXferScope scope = SpeciesXferScope(awr->awar(AWAR_SPECIES_XFER_SCOPE)->read_int()); |
|---|
| 1170 | |
|---|
| 1171 | switch (type) { |
|---|
| 1172 | case SXT_WHOLE_SPECIES: |
|---|
| 1173 | switch (scope) { |
|---|
| 1174 | case SXS_LISTED_SPECIES: mg_transfer_listed_species(aww); break; |
|---|
| 1175 | case SXS_SELECTED_SPECIES: mg_transfer_selected_species(aww); break; |
|---|
| 1176 | } |
|---|
| 1177 | break; |
|---|
| 1178 | |
|---|
| 1179 | case SXT_SINGLE_FIELD: |
|---|
| 1180 | popup_single_field_transfer_window(awr, scope); |
|---|
| 1181 | break; |
|---|
| 1182 | |
|---|
| 1183 | case SXT_USING_FTS: |
|---|
| 1184 | case SXT_USING_FTS_SKIP_ALI: { |
|---|
| 1185 | bool xfer_seq_data = (type == SXT_USING_FTS); |
|---|
| 1186 | mg_xfer_via_fts(awr, scope, xfer_seq_data); |
|---|
| 1187 | break; |
|---|
| 1188 | } |
|---|
| 1189 | } |
|---|
| 1190 | } |
|---|
| 1191 | |
|---|
| 1192 | AW_window *MG_create_merge_species_window(AW_root *awr, bool dst_is_new) { |
|---|
| 1193 | GB_ERROR error = MG_expect_renamed(); |
|---|
| 1194 | if (error) { |
|---|
| 1195 | aw_message(error); |
|---|
| 1196 | return NULp; // deny to open window before user has renamed species |
|---|
| 1197 | } |
|---|
| 1198 | |
|---|
| 1199 | awr->awar_string(AWAR_REMAP_SPECIES_LIST, "ecoli", GLOBAL_gb_dst); |
|---|
| 1200 | awr->awar_int(AWAR_REMAP_ENABLE, 0, GLOBAL_gb_dst); |
|---|
| 1201 | |
|---|
| 1202 | AW_window_simple_menu *aws = new AW_window_simple_menu; |
|---|
| 1203 | aws->init(awr, "MERGE_TRANSFER_SPECIES", "TRANSFER SPECIES"); |
|---|
| 1204 | aws->load_xfig("merge/species.fig"); |
|---|
| 1205 | |
|---|
| 1206 | aws->at("close"); |
|---|
| 1207 | aws->callback(AW_POPDOWN); |
|---|
| 1208 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 1209 | |
|---|
| 1210 | aws->at("help"); |
|---|
| 1211 | aws->callback(makeHelpCallback("mg_species.hlp")); |
|---|
| 1212 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 1213 | |
|---|
| 1214 | mg_initialize_species_selectors(); |
|---|
| 1215 | |
|---|
| 1216 | // define macro IDs and window titles for both config managers used in transfer window: |
|---|
| 1217 | AWT_predef_config_manager("MERGE_TRANSFER_SOURCE_SPECIES_config", "Configurations for source species query"); |
|---|
| 1218 | AWT_predef_config_manager("MERGE_TRANSFER_TARGET_SPECIES_config", "Configurations for target species query"); |
|---|
| 1219 | |
|---|
| 1220 | { |
|---|
| 1221 | QUERY::query_spec awtqs(MG_species_selector[0]); |
|---|
| 1222 | aws->create_menu("Source-DB", "S"); |
|---|
| 1223 | |
|---|
| 1224 | awtqs.gb_main = GLOBAL_gb_src; |
|---|
| 1225 | awtqs.gb_ref = GLOBAL_gb_dst; |
|---|
| 1226 | awtqs.expect_hit_in_ref_list = false; |
|---|
| 1227 | awtqs.species_name = AWAR_SPECIES_SRC; |
|---|
| 1228 | awtqs.tree_name = NULp; // no selected tree here -> can't use tree related ACI commands without specifying a tree |
|---|
| 1229 | awtqs.select_bit = GB_USERFLAG_QUERY; |
|---|
| 1230 | awtqs.ere_pos_fig = "ere1"; |
|---|
| 1231 | awtqs.by_pos_fig = "by1"; |
|---|
| 1232 | awtqs.qbox_pos_fig = "qbox1"; |
|---|
| 1233 | awtqs.key_pos_fig = NULp; |
|---|
| 1234 | awtqs.query_pos_fig = "content1"; |
|---|
| 1235 | awtqs.result_pos_fig = "result1"; |
|---|
| 1236 | awtqs.count_pos_fig = "count1"; |
|---|
| 1237 | awtqs.do_query_pos_fig = "doquery1"; |
|---|
| 1238 | awtqs.config_pos_fig = "doconfig1"; |
|---|
| 1239 | awtqs.do_mark_pos_fig = NULp; |
|---|
| 1240 | awtqs.do_unmark_pos_fig = NULp; |
|---|
| 1241 | awtqs.do_delete_pos_fig = "dodelete1"; |
|---|
| 1242 | awtqs.do_set_pos_fig = "doset1"; |
|---|
| 1243 | awtqs.do_refresh_pos_fig = "dorefresh1"; |
|---|
| 1244 | |
|---|
| 1245 | create_query_box(aws, &awtqs, "db1"); |
|---|
| 1246 | |
|---|
| 1247 | DbScanner *scanner = DbScanner::create(GLOBAL_gb_src, "merge_spec1", aws, "box1", NULp, NULp, DB_SCANNER, NULp, awtqs.get_queried_itemtype()); |
|---|
| 1248 | scanner_src = scanner; |
|---|
| 1249 | aws->get_root()->awar(AWAR_SPECIES_SRC)->add_callback(makeRootCallback(MG_map_species, SRC_DB)); |
|---|
| 1250 | } |
|---|
| 1251 | { |
|---|
| 1252 | QUERY::query_spec awtqs(MG_species_selector[1]); |
|---|
| 1253 | aws->create_menu("Target-DB", "T"); |
|---|
| 1254 | |
|---|
| 1255 | awtqs.gb_main = GLOBAL_gb_dst; |
|---|
| 1256 | awtqs.gb_ref = GLOBAL_gb_src; |
|---|
| 1257 | awtqs.expect_hit_in_ref_list = true; |
|---|
| 1258 | awtqs.species_name = AWAR_SPECIES_DST; |
|---|
| 1259 | awtqs.select_bit = GB_USERFLAG_QUERY; |
|---|
| 1260 | awtqs.ere_pos_fig = "ere2"; |
|---|
| 1261 | awtqs.by_pos_fig = "by2"; |
|---|
| 1262 | awtqs.qbox_pos_fig = "qbox2"; |
|---|
| 1263 | awtqs.key_pos_fig = NULp; |
|---|
| 1264 | awtqs.query_pos_fig = "content2"; |
|---|
| 1265 | awtqs.result_pos_fig = "result2"; |
|---|
| 1266 | awtqs.count_pos_fig = "count2"; |
|---|
| 1267 | awtqs.do_query_pos_fig = "doquery2"; |
|---|
| 1268 | awtqs.config_pos_fig = "doconfig2"; |
|---|
| 1269 | awtqs.do_mark_pos_fig = NULp; |
|---|
| 1270 | awtqs.do_unmark_pos_fig = NULp; |
|---|
| 1271 | awtqs.do_delete_pos_fig = "dodelete2"; |
|---|
| 1272 | awtqs.do_set_pos_fig = "doset2"; |
|---|
| 1273 | awtqs.do_refresh_pos_fig = "dorefresh2"; |
|---|
| 1274 | |
|---|
| 1275 | ScopedTransporter::set_query_to_update(create_query_box(aws, &awtqs, "db2")); |
|---|
| 1276 | |
|---|
| 1277 | DbScanner *scanner = DbScanner::create(GLOBAL_gb_dst, "merge_spec2", aws, "box2", NULp, NULp, DB_SCANNER, NULp, awtqs.get_queried_itemtype()); |
|---|
| 1278 | scanner_dst = scanner; |
|---|
| 1279 | aws->get_root()->awar(AWAR_SPECIES_DST)->add_callback(makeRootCallback(MG_map_species, DST_DB)); |
|---|
| 1280 | } |
|---|
| 1281 | |
|---|
| 1282 | #define AWAR_MARKED_LABEL_NAME(db) awar_name_tmp(db, "marked_label") |
|---|
| 1283 | |
|---|
| 1284 | aws->at("marked1"); create_species_selection_button(aws, makeWindowCallback(MG_popup_selection_admin, SRC_DB), "select1", AWAR_MARKED_LABEL_NAME(SRC_DB), GLOBAL_gb_src); |
|---|
| 1285 | aws->at("marked2"); create_species_selection_button(aws, makeWindowCallback(MG_popup_selection_admin, DST_DB), "select2", AWAR_MARKED_LABEL_NAME(DST_DB), GLOBAL_gb_dst); |
|---|
| 1286 | |
|---|
| 1287 | // --------------------- |
|---|
| 1288 | // middle area |
|---|
| 1289 | |
|---|
| 1290 | // top icon |
|---|
| 1291 | aws->button_length(0); |
|---|
| 1292 | aws->at("icon"); |
|---|
| 1293 | aws->callback(makeHelpCallback("mg_species.hlp")); |
|---|
| 1294 | aws->create_button("HELP_MERGE", "#merge/icon.xpm"); |
|---|
| 1295 | |
|---|
| 1296 | // adapt alignments |
|---|
| 1297 | { |
|---|
| 1298 | if (dst_is_new) { |
|---|
| 1299 | aws->sens_mask(AWM_DISABLED); // if dest DB is new = > adaption impossible |
|---|
| 1300 | awr->awar(AWAR_REMAP_ENABLE)->write_int(0); // disable adaption |
|---|
| 1301 | } |
|---|
| 1302 | |
|---|
| 1303 | aws->at("adapt"); |
|---|
| 1304 | aws->create_toggle(AWAR_REMAP_ENABLE); |
|---|
| 1305 | |
|---|
| 1306 | aws->at("reference"); |
|---|
| 1307 | aws->create_text_field(AWAR_REMAP_SPECIES_LIST); |
|---|
| 1308 | |
|---|
| 1309 | aws->at("pres_sel"); |
|---|
| 1310 | aws->callback(MG_create_preserves_selection_window); |
|---|
| 1311 | aws->create_autosize_button("SELECT", "SELECT", "S"); |
|---|
| 1312 | |
|---|
| 1313 | aws->sens_mask(AWM_ALL); |
|---|
| 1314 | } |
|---|
| 1315 | |
|---|
| 1316 | // transfer options: |
|---|
| 1317 | aws->at("xferType"); |
|---|
| 1318 | aws->create_option_menu(AWAR_SPECIES_XFER_TYPE); |
|---|
| 1319 | aws->insert_option("whole species", "w", SXT_WHOLE_SPECIES); |
|---|
| 1320 | aws->insert_option("single field", "s", SXT_SINGLE_FIELD); |
|---|
| 1321 | aws->insert_option("using FTS (with seq)", "F", SXT_USING_FTS); |
|---|
| 1322 | aws->insert_option("using FTS (w/o seq)", "o", SXT_USING_FTS_SKIP_ALI); |
|---|
| 1323 | aws->update_option_menu(); |
|---|
| 1324 | |
|---|
| 1325 | aws->at("fts"); |
|---|
| 1326 | aws->callback(makeWindowCallback(XFER_select_RuleSet, AWAR_SPECIES_XFER_FTS, static_cast<AvailableFieldScanner*>(&fieldScanner))); |
|---|
| 1327 | aws->create_button("SELECT_FTS", AWAR_SPECIES_XFER_FTS); |
|---|
| 1328 | |
|---|
| 1329 | aws->at("xferWhat"); |
|---|
| 1330 | aws->create_option_menu(AWAR_SPECIES_XFER_SCOPE); |
|---|
| 1331 | aws->insert_option("listed species", "l", SXS_LISTED_SPECIES); |
|---|
| 1332 | aws->insert_option("selected species", "s", SXS_SELECTED_SPECIES); |
|---|
| 1333 | aws->update_option_menu(); |
|---|
| 1334 | |
|---|
| 1335 | { |
|---|
| 1336 | aws->shadow_width(3); |
|---|
| 1337 | |
|---|
| 1338 | aws->at("transfer"); |
|---|
| 1339 | aws->callback(MG_transfer_species); |
|---|
| 1340 | aws->create_button("TRANSFER", "Transfer species"); |
|---|
| 1341 | |
|---|
| 1342 | aws->shadow_width(1); |
|---|
| 1343 | } |
|---|
| 1344 | |
|---|
| 1345 | aws->create_menu("Source->Target", "g"); |
|---|
| 1346 | aws->insert_menu_topic("compare_field_of_listed", "Compare a field of listed species ...", "C", "checkfield.hlp", AWM_ALL, create_mg_check_fields_window); |
|---|
| 1347 | aws->insert_menu_topic("merge_field_of_listed_to_new_field", "Merge tagged field ...", "M", "mergetaggedfield.hlp", AWM_ALL, create_mg_merge_tagged_fields_window); |
|---|
| 1348 | aws->sep______________(); |
|---|
| 1349 | aws->insert_menu_topic("def_gene_species_field_xfer", "Define field transfer for gene-species", "g", "gene_species_field_transfer.hlp", AWM_ALL, MG_gene_species_create_field_transfer_def_window); |
|---|
| 1350 | |
|---|
| 1351 | |
|---|
| 1352 | return aws; |
|---|
| 1353 | } |
|---|
| 1354 | |
|---|