| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : ad_ext.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include "NT_local.h" |
|---|
| 12 | #include <db_scanner.hxx> |
|---|
| 13 | #include <awt_sel_boxes.hxx> |
|---|
| 14 | #include <aw_awars.hxx> |
|---|
| 15 | #include <aw_msg.hxx> |
|---|
| 16 | #include <aw_root.hxx> |
|---|
| 17 | #include <arbdbt.h> |
|---|
| 18 | #include <arb_strbuf.h> |
|---|
| 19 | |
|---|
| 20 | static void rename_SAI_cb(AW_window *aww) { |
|---|
| 21 | AW_awar *awar_sai = aww->get_root()->awar(AWAR_SAI_NAME); |
|---|
| 22 | char *sai_name = awar_sai->read_string(); |
|---|
| 23 | GB_ERROR error = 0; |
|---|
| 24 | |
|---|
| 25 | if (!sai_name || !sai_name[0]) error = "Select SAI to rename"; |
|---|
| 26 | else { |
|---|
| 27 | char *new_name = aw_input("Rename SAI", "Enter new name of SAI", sai_name); |
|---|
| 28 | if (new_name && new_name[0]) { |
|---|
| 29 | error = GB_begin_transaction(GLOBAL.gb_main); |
|---|
| 30 | if (!error) { |
|---|
| 31 | GBDATA *gb_sai = GBT_find_SAI(GLOBAL.gb_main, sai_name); |
|---|
| 32 | if (!gb_sai) error = GBS_global_string("can't find SAI '%s'", sai_name); |
|---|
| 33 | else { |
|---|
| 34 | GBDATA *gb_dest_exists = GBT_find_SAI(GLOBAL.gb_main, new_name); |
|---|
| 35 | if (gb_dest_exists) error = GBS_global_string("There is already a SAI named '%s'", new_name); |
|---|
| 36 | else { |
|---|
| 37 | error = GBT_write_string(gb_sai, "name", new_name); |
|---|
| 38 | if (!error) awar_sai->write_string(new_name); |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | error = GB_end_transaction(GLOBAL.gb_main, error); |
|---|
| 43 | } |
|---|
| 44 | free(new_name); |
|---|
| 45 | } |
|---|
| 46 | free(sai_name); |
|---|
| 47 | |
|---|
| 48 | if (error) aw_message(error); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | static void copy_SAI_cb(AW_window *aww) { |
|---|
| 52 | AW_awar *awar_sai = aww->get_root()->awar(AWAR_SAI_NAME); |
|---|
| 53 | char *source_name = awar_sai->read_string(); |
|---|
| 54 | GB_ERROR error = 0; |
|---|
| 55 | |
|---|
| 56 | if (!source_name || !source_name[0]) error = "Select SAI to duplicate"; |
|---|
| 57 | else { |
|---|
| 58 | char *dest_name = aw_input("Copy SAI", "Enter name of new SAI", source_name); |
|---|
| 59 | if (dest_name && dest_name[0]) { |
|---|
| 60 | error = GB_begin_transaction(GLOBAL.gb_main); |
|---|
| 61 | if (!error) { |
|---|
| 62 | GBDATA *gb_sai_data = GBT_get_SAI_data(GLOBAL.gb_main); |
|---|
| 63 | GBDATA *gb_source = GBT_find_SAI_rel_SAI_data(gb_sai_data, source_name); |
|---|
| 64 | if (!gb_source) error = GBS_global_string("can't find SAI '%s'", source_name); |
|---|
| 65 | else { |
|---|
| 66 | GBDATA *gb_dest_exists = GBT_find_SAI_rel_SAI_data(gb_sai_data, dest_name); |
|---|
| 67 | if (gb_dest_exists) error = GBS_global_string("There is already a SAI named '%s'", dest_name); |
|---|
| 68 | else { |
|---|
| 69 | GBDATA *gb_dest = GB_create_container(gb_sai_data, "extended"); |
|---|
| 70 | if (!gb_dest) error = GB_await_error(); |
|---|
| 71 | else { |
|---|
| 72 | error = GB_copy(gb_dest, gb_source); |
|---|
| 73 | if (!error) { |
|---|
| 74 | error = GBT_write_string(gb_dest, "name", dest_name); |
|---|
| 75 | if (!error) awar_sai->write_string(dest_name); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | error = GB_end_transaction(GLOBAL.gb_main, error); |
|---|
| 82 | } |
|---|
| 83 | free(dest_name); |
|---|
| 84 | } |
|---|
| 85 | free(source_name); |
|---|
| 86 | |
|---|
| 87 | if (error) aw_message(error); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | static void copy_SAI_to_species_cb(AW_window *aww) { |
|---|
| 91 | AW_root *aw_root = aww->get_root(); |
|---|
| 92 | char *sai_name = aw_root->awar(AWAR_SAI_NAME)->read_string(); |
|---|
| 93 | GB_ERROR error = 0; |
|---|
| 94 | |
|---|
| 95 | if (!sai_name || !sai_name[0]) error = "No SAI selected"; |
|---|
| 96 | else { |
|---|
| 97 | char *species_name = aw_input("Copy SAI to species", "Enter target species name:", sai_name); |
|---|
| 98 | |
|---|
| 99 | if (species_name && species_name[0]) { |
|---|
| 100 | error = GB_begin_transaction(GLOBAL.gb_main); |
|---|
| 101 | |
|---|
| 102 | if (!error) { |
|---|
| 103 | GBDATA *gb_species_data = GBT_get_species_data(GLOBAL.gb_main); |
|---|
| 104 | GBDATA *gb_dest = GBT_find_species_rel_species_data(gb_species_data, species_name); |
|---|
| 105 | |
|---|
| 106 | if (gb_dest) error = GBS_global_string("Species '%s' already exists", species_name); |
|---|
| 107 | else { |
|---|
| 108 | GBDATA *gb_sai = GBT_find_SAI(GLOBAL.gb_main, sai_name); |
|---|
| 109 | |
|---|
| 110 | if (!gb_sai) error = GBS_global_string("SAI '%s' not found", sai_name); |
|---|
| 111 | else { |
|---|
| 112 | gb_dest = GB_create_container(gb_species_data, "species"); |
|---|
| 113 | if (!gb_dest) error = GB_await_error(); |
|---|
| 114 | else { |
|---|
| 115 | error = GB_copy(gb_dest, gb_sai); |
|---|
| 116 | if (!error) { |
|---|
| 117 | error = GBT_write_string(gb_dest, "name", species_name); |
|---|
| 118 | if (!error) aw_root->awar(AWAR_SPECIES_NAME)->write_string(species_name); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | error = GB_end_transaction(GLOBAL.gb_main, error); |
|---|
| 125 | } |
|---|
| 126 | free(species_name); |
|---|
| 127 | } |
|---|
| 128 | free(sai_name); |
|---|
| 129 | if (error) aw_message(error); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | static void delete_SAI_cb(AW_window *aww) { |
|---|
| 133 | char *sai_name = aww->get_root()->awar(AWAR_SAI_NAME)->read_string(); |
|---|
| 134 | GB_ERROR error = GB_begin_transaction(GLOBAL.gb_main); |
|---|
| 135 | |
|---|
| 136 | if (!error) { |
|---|
| 137 | GBDATA *gb_sai = GBT_find_SAI(GLOBAL.gb_main, sai_name); |
|---|
| 138 | error = gb_sai ? GB_delete(gb_sai) : "Please select a SAI"; |
|---|
| 139 | } |
|---|
| 140 | GB_end_transaction_show_error(GLOBAL.gb_main, error, aw_message); |
|---|
| 141 | free(sai_name); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | static void map_SAI_to_scanner(AW_root *aw_root, DbScanner *scanner) { |
|---|
| 145 | GB_transaction ta(GLOBAL.gb_main); |
|---|
| 146 | char *sai_name = aw_root->awar(AWAR_SAI_NAME)->read_string(); |
|---|
| 147 | GBDATA *gb_sai = GBT_find_SAI(GLOBAL.gb_main, sai_name); |
|---|
| 148 | |
|---|
| 149 | map_db_scanner(scanner, gb_sai, CHANGE_KEY_PATH); |
|---|
| 150 | free(sai_name); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | static void edit_SAI_description(AW_window *aww) { |
|---|
| 154 | AW_root *awr = aww->get_root(); |
|---|
| 155 | char *sai_name = awr->awar(AWAR_SAI_NAME)->read_string(); |
|---|
| 156 | GB_ERROR error = 0; |
|---|
| 157 | |
|---|
| 158 | if (!sai_name || !sai_name[0]) error = "No SAI selected"; |
|---|
| 159 | else { |
|---|
| 160 | GBDATA *gb_ali = 0; |
|---|
| 161 | char *type = 0; |
|---|
| 162 | { |
|---|
| 163 | GB_transaction ta(GLOBAL.gb_main); |
|---|
| 164 | GBDATA *gb_sai = GBT_find_SAI(GLOBAL.gb_main, sai_name); |
|---|
| 165 | |
|---|
| 166 | if (!gb_sai) error = GBS_global_string("SAI '%s' not found", sai_name); |
|---|
| 167 | else { |
|---|
| 168 | char *ali_name = GBT_get_default_alignment(GLOBAL.gb_main); |
|---|
| 169 | |
|---|
| 170 | gb_ali = GB_entry(gb_sai, ali_name); |
|---|
| 171 | if (!gb_ali) error = GBS_global_string("SAI '%s' has no data in alignment '%s'", sai_name, ali_name); |
|---|
| 172 | else { |
|---|
| 173 | GB_clear_error(); |
|---|
| 174 | type = GBT_read_string(gb_ali, "_TYPE"); |
|---|
| 175 | if (!type) { |
|---|
| 176 | if (GB_have_error()) { |
|---|
| 177 | error = GB_await_error(); |
|---|
| 178 | } |
|---|
| 179 | else { |
|---|
| 180 | type = strdup(""); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | error = ta.close(error); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | if (!error) { |
|---|
| 189 | nt_assert(gb_ali); |
|---|
| 190 | char *new_type = aw_input("Change SAI description", type); |
|---|
| 191 | if (new_type) { |
|---|
| 192 | GB_transaction t2(GLOBAL.gb_main); |
|---|
| 193 | |
|---|
| 194 | if (new_type[0]) { |
|---|
| 195 | error = GBT_write_string(gb_ali, "_TYPE", new_type); |
|---|
| 196 | } |
|---|
| 197 | else { // empty description -> delete |
|---|
| 198 | GBDATA *gb_type = GB_entry(gb_ali, "_TYPE"); |
|---|
| 199 | if (gb_type) error = GB_delete(gb_type); |
|---|
| 200 | } |
|---|
| 201 | error = t2.close(error); |
|---|
| 202 | free(new_type); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | free(type); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | if (error) aw_message(error); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | static char *getExistingSAIgroups() { |
|---|
| 213 | // scan SAIs for existing groups. |
|---|
| 214 | // return a string of ';'-separated group names (or NULL) |
|---|
| 215 | |
|---|
| 216 | GB_HASH *groups = GBS_create_hash(GBT_get_SAI_count(GLOBAL.gb_main), GB_MIND_CASE); |
|---|
| 217 | GBS_strstruct *out = GBS_stropen(1000); |
|---|
| 218 | int count = 0; |
|---|
| 219 | GB_transaction ta(GLOBAL.gb_main); |
|---|
| 220 | |
|---|
| 221 | for (GBDATA *gb_sai = GBT_first_SAI(GLOBAL.gb_main); gb_sai; gb_sai = GBT_next_SAI(gb_sai)) { |
|---|
| 222 | const char *group = GBT_read_char_pntr(gb_sai, "sai_group"); |
|---|
| 223 | if (group && !GBS_read_hash(groups, group)) { |
|---|
| 224 | GBS_strcat(out, group); |
|---|
| 225 | GBS_chrcat(out, ';'); |
|---|
| 226 | GBS_write_hash(groups, group, 1); |
|---|
| 227 | count++; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | char *result = 0; |
|---|
| 232 | if (count>0) { |
|---|
| 233 | GBS_str_cut_tail(out, 1); // truncate final ';' |
|---|
| 234 | result = GBS_strclose(out); |
|---|
| 235 | } |
|---|
| 236 | else { |
|---|
| 237 | GBS_strforget(out); |
|---|
| 238 | } |
|---|
| 239 | GBS_free_hash(groups); |
|---|
| 240 | |
|---|
| 241 | return result; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | static void assign_SAI_to_group(AW_window *aww) { |
|---|
| 245 | AW_root *awr = aww->get_root(); |
|---|
| 246 | char *sai_name = awr->awar(AWAR_SAI_NAME)->read_string(); |
|---|
| 247 | GB_ERROR error = 0; |
|---|
| 248 | |
|---|
| 249 | if (!sai_name || !sai_name[0]) error = "No SAI selected"; |
|---|
| 250 | else { |
|---|
| 251 | GBDATA *gb_sai; |
|---|
| 252 | { |
|---|
| 253 | GB_transaction ta(GLOBAL.gb_main); |
|---|
| 254 | gb_sai = GBT_find_SAI(GLOBAL.gb_main, sai_name); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | if (!gb_sai) error = GBS_global_string("SAI '%s' not found", sai_name); |
|---|
| 258 | else { |
|---|
| 259 | bool has_group = true; |
|---|
| 260 | const char *group = GBT_read_char_pntr(gb_sai, "sai_group"); |
|---|
| 261 | if (!group) { |
|---|
| 262 | group = "default_group"; |
|---|
| 263 | has_group = false; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | char *new_group; |
|---|
| 267 | { |
|---|
| 268 | char *existingGroups = getExistingSAIgroups(); |
|---|
| 269 | if (existingGroups) { |
|---|
| 270 | new_group = aw_string_selection("Assign SAI to group", "Enter group name:", group, existingGroups, NULL); |
|---|
| 271 | free(existingGroups); |
|---|
| 272 | } |
|---|
| 273 | else { |
|---|
| 274 | new_group = aw_input("Assign SAI to group", "Enter group name:", group); |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | if (new_group) { |
|---|
| 279 | GB_transaction t2(GLOBAL.gb_main); |
|---|
| 280 | if (new_group[0]) error = GBT_write_string(gb_sai, "sai_group", new_group); |
|---|
| 281 | else if (has_group) { |
|---|
| 282 | GBDATA *gb_group = GB_entry(gb_sai, "sai_group"); |
|---|
| 283 | if (gb_group) error = GB_delete(gb_group); |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | if (error) aw_message(error); |
|---|
| 290 | free(sai_name); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | AW_window *NT_create_extendeds_window(AW_root *aw_root) |
|---|
| 294 | { |
|---|
| 295 | static AW_window_simple *aws = 0; |
|---|
| 296 | |
|---|
| 297 | if (!aws) { |
|---|
| 298 | aws = new AW_window_simple; |
|---|
| 299 | aws->init(aw_root, "INFO_OF_SAI", "SAI INFORMATION"); |
|---|
| 300 | aws->load_xfig("ad_ext.fig"); |
|---|
| 301 | |
|---|
| 302 | aws->callback((AW_CB0)AW_POPDOWN); |
|---|
| 303 | aws->at("close"); |
|---|
| 304 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 305 | |
|---|
| 306 | aws->callback(makeHelpCallback("ad_extended.hlp")); |
|---|
| 307 | aws->at("help"); |
|---|
| 308 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 309 | |
|---|
| 310 | aws->button_length(13); |
|---|
| 311 | |
|---|
| 312 | aws->at("delete"); |
|---|
| 313 | aws->callback(delete_SAI_cb); |
|---|
| 314 | aws->create_button("DELETE", "DELETE", "D"); |
|---|
| 315 | |
|---|
| 316 | aws->at("rename"); |
|---|
| 317 | aws->callback(rename_SAI_cb); |
|---|
| 318 | aws->create_button("RENAME", "RENAME", "R"); |
|---|
| 319 | |
|---|
| 320 | aws->at("copy"); |
|---|
| 321 | aws->callback(copy_SAI_cb); |
|---|
| 322 | aws->create_button("COPY", "COPY", "C"); |
|---|
| 323 | |
|---|
| 324 | aws->at("remark"); |
|---|
| 325 | aws->callback(edit_SAI_description); |
|---|
| 326 | aws->create_button("EDIT_COMMENT", "EDIT COMMENT", "R"); |
|---|
| 327 | |
|---|
| 328 | aws->at("group"); |
|---|
| 329 | aws->callback(assign_SAI_to_group); |
|---|
| 330 | aws->create_button("ASSIGN_GROUP", "ASSIGN GROUP", "R"); |
|---|
| 331 | |
|---|
| 332 | aws->at("makespec"); |
|---|
| 333 | aws->callback(copy_SAI_to_species_cb); |
|---|
| 334 | aws->create_button("COPY_TO_SPECIES", "COPY TO\nSPECIES", "C"); |
|---|
| 335 | |
|---|
| 336 | aws->at("list"); |
|---|
| 337 | awt_create_selection_list_on_sai(GLOBAL.gb_main, aws, AWAR_SAI_NAME, true); |
|---|
| 338 | |
|---|
| 339 | DbScanner *scanner = create_db_scanner(GLOBAL.gb_main, aws, "info", 0, 0, 0, DB_SCANNER, 0, 0, 0, SPECIES_get_selector()); |
|---|
| 340 | aws->get_root()->awar(AWAR_SAI_NAME)->add_callback(makeRootCallback(map_SAI_to_scanner, scanner)); |
|---|
| 341 | } |
|---|
| 342 | aws->show(); |
|---|
| 343 | return aws; |
|---|
| 344 | } |
|---|