| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : MP_noclass.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include "MultiProbe.hxx" |
|---|
| 12 | #include "mp_proto.hxx" |
|---|
| 13 | #include "MP_externs.hxx" |
|---|
| 14 | #include "MP_probe.hxx" |
|---|
| 15 | |
|---|
| 16 | #include <aw_select.hxx> |
|---|
| 17 | #include <aw_msg.hxx> |
|---|
| 18 | #include <arb_progress.h> |
|---|
| 19 | #include <aw_root.hxx> |
|---|
| 20 | #include <TreeCallbacks.hxx> |
|---|
| 21 | #include <client.h> |
|---|
| 22 | #include <AP_TreeColors.hxx> |
|---|
| 23 | |
|---|
| 24 | #include <ctime> |
|---|
| 25 | |
|---|
| 26 | int get_random(int min, int max); // gibt eine Zufallszahl x mit der Eigenschaft : min <= x <= max |
|---|
| 27 | |
|---|
| 28 | int **system3_tab = NULp; |
|---|
| 29 | static int system3_tab_size = 0; |
|---|
| 30 | |
|---|
| 31 | unsigned char **hamming_tab = NULp; |
|---|
| 32 | bool new_pt_server = true; |
|---|
| 33 | |
|---|
| 34 | long k_chosenFrom_n(int k, int n) { // binomial coefficient. former german name was "k_aus_n". |
|---|
| 35 | int a = n, b = 1, i; |
|---|
| 36 | |
|---|
| 37 | if (k > (n / 2)) k = n - k; |
|---|
| 38 | if (k <= 0) return k == 0; |
|---|
| 39 | for (i = 2; i <= k; n--, a *= n, b *= i, i++) ; // IRRELEVANT_LOOP (gcc 9.x refuses to optimize) |
|---|
| 40 | return a / b; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | int get_random(int min, int max) { |
|---|
| 44 | mp_assert(min <= max); |
|---|
| 45 | return GB_random(max-min+1)+min; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | static void refresh_without_dbsync(AWT_canvas *scr) { |
|---|
| 49 | LocallyModify<int> allow_flag_modify(scr->gfx->exports.get_modifying_flag_ref(), -1); // allow flag updates |
|---|
| 50 | if (scr->gb_main) scr->gfx->notify_synchronized(scr->gb_main); |
|---|
| 51 | scr->instant_refresh(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void MP_close_main(AW_window *aww) { |
|---|
| 55 | TREE_canvas *scr = mp_main->get_canvas(); |
|---|
| 56 | |
|---|
| 57 | if (mp_main->get_mp_window()->get_result_window()) |
|---|
| 58 | mp_main->get_mp_window()->get_result_window()->hide(); |
|---|
| 59 | |
|---|
| 60 | GB_transaction ta(scr->gb_main); |
|---|
| 61 | |
|---|
| 62 | AP_tree *ap_tree = AWT_TREE(scr)->get_root_node(); |
|---|
| 63 | if (ap_tree) ap_tree->uncolorize(); |
|---|
| 64 | refresh_without_dbsync(scr); |
|---|
| 65 | |
|---|
| 66 | AW_POPDOWN(aww); |
|---|
| 67 | delete mp_main->get_p_eval(); |
|---|
| 68 | mp_main->set_p_eval(NULp); |
|---|
| 69 | delete mp_main->get_stc(); |
|---|
| 70 | mp_main->set_stc(NULp); |
|---|
| 71 | |
|---|
| 72 | new_pt_server = true; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | static char *MP_get_probes(const char *str) { |
|---|
| 76 | const char *result = strrchr(str, '#'); |
|---|
| 77 | |
|---|
| 78 | if (!result) { |
|---|
| 79 | result = str; |
|---|
| 80 | } |
|---|
| 81 | else { |
|---|
| 82 | ++result; |
|---|
| 83 | result += strspn(result, " \t"); // skip over whitespace |
|---|
| 84 | } |
|---|
| 85 | return ARB_strdup(result); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void MP_gen_quality(AW_root *awr) { |
|---|
| 89 | bool firsttime = true; // @@@ was this static in the past? |
|---|
| 90 | |
|---|
| 91 | if (firsttime) { |
|---|
| 92 | firsttime = false; // @@@ unused |
|---|
| 93 | return; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | char *probe, *new_qual, *ecol_pos; |
|---|
| 97 | char *selected = awr->awar(MP_AWAR_SELECTEDPROBES)->read_string(); |
|---|
| 98 | |
|---|
| 99 | if (!selected || !selected[0]) |
|---|
| 100 | return; |
|---|
| 101 | |
|---|
| 102 | probe = MP_get_probes(selected); |
|---|
| 103 | if (!probe || !probe[0]) |
|---|
| 104 | return; |
|---|
| 105 | |
|---|
| 106 | ecol_pos = MP_get_comment(3, selected); |
|---|
| 107 | |
|---|
| 108 | selected_list->delete_value(AW_scalar(selected)); |
|---|
| 109 | |
|---|
| 110 | new_qual = new char[5 + 7 + strlen(probe)]; // 5 = Zahl und Separator und Zahl und Separator und Nullzeichen |
|---|
| 111 | sprintf(new_qual, "%1ld#%1ld#%6d#%s", mp_gl_awars.probe_quality, mp_gl_awars.singlemismatches, atoi(ecol_pos), probe); |
|---|
| 112 | delete probe; |
|---|
| 113 | |
|---|
| 114 | selected_list->insert(new_qual, new_qual); |
|---|
| 115 | selected_list->insert_default("", ""); |
|---|
| 116 | selected_list->sort(false, true); |
|---|
| 117 | selected_list->update(); |
|---|
| 118 | awr->awar(MP_AWAR_SELECTEDPROBES)->write_string(new_qual); |
|---|
| 119 | delete new_qual; |
|---|
| 120 | delete ecol_pos; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void MP_modify_selected(AW_root *awr) { |
|---|
| 124 | // setzt den 2.Parameter in selected_list |
|---|
| 125 | char *com1, *com2, *com3, *probes, temp[120]; |
|---|
| 126 | List<char> *l = new List<char>; |
|---|
| 127 | |
|---|
| 128 | AW_selection_list_iterator selentry(selected_list); |
|---|
| 129 | |
|---|
| 130 | while (selentry) { |
|---|
| 131 | const char *ptr2 = selentry.get_value()->get_string(); |
|---|
| 132 | ++selentry; |
|---|
| 133 | |
|---|
| 134 | com1 = MP_get_comment(1, ptr2); |
|---|
| 135 | com2 = MP_get_comment(2, ptr2); |
|---|
| 136 | com3 = MP_get_comment(3, ptr2); |
|---|
| 137 | probes = MP_get_probes(ptr2); |
|---|
| 138 | |
|---|
| 139 | if (!probes || !probes[0]) |
|---|
| 140 | break; |
|---|
| 141 | |
|---|
| 142 | sprintf(temp, "%1d#%1ld#%6d#%s", atoi(com1), mp_gl_awars.no_of_mismatches, atoi(com3), probes); |
|---|
| 143 | |
|---|
| 144 | l->insert_as_last(ARB_strdup(temp)); |
|---|
| 145 | |
|---|
| 146 | delete probes; |
|---|
| 147 | delete com1; |
|---|
| 148 | delete com2; |
|---|
| 149 | delete com3; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | selected_list->clear(); |
|---|
| 153 | |
|---|
| 154 | { |
|---|
| 155 | const char *ptr2 = l->get_first(); |
|---|
| 156 | while (ptr2) { |
|---|
| 157 | l->remove_first(); |
|---|
| 158 | selected_list->insert(ptr2, ptr2); |
|---|
| 159 | delete ptr2; |
|---|
| 160 | ptr2 = l->get_first(); |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | selected_list->insert_default("", ""); |
|---|
| 165 | selected_list->sort(false, true); |
|---|
| 166 | selected_list->update(); |
|---|
| 167 | awr->awar(MP_AWAR_SELECTEDPROBES)->write_string(""); |
|---|
| 168 | |
|---|
| 169 | remembered_mismatches = mp_gl_awars.no_of_mismatches; |
|---|
| 170 | delete l; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void MP_gen_singleprobe(AW_root *awr) { |
|---|
| 174 | char *probe, *new_sing; |
|---|
| 175 | char *selected = awr->awar(MP_AWAR_SELECTEDPROBES)->read_string(); |
|---|
| 176 | |
|---|
| 177 | if (!selected || !selected[0]) |
|---|
| 178 | return; |
|---|
| 179 | |
|---|
| 180 | probe = MP_get_probes(selected); |
|---|
| 181 | selected_list->delete_value(AW_scalar(selected)); |
|---|
| 182 | |
|---|
| 183 | new_sing = new char[5 + 7 + strlen(probe)]; // 5 = Zahl und Separator und Zahl und Separator und Nullzeichen |
|---|
| 184 | sprintf(new_sing, "%1ld#%1ld#%6ld#%s", mp_gl_awars.probe_quality, mp_gl_awars.singlemismatches, mp_gl_awars.ecolipos, probe); |
|---|
| 185 | delete probe; |
|---|
| 186 | |
|---|
| 187 | selected_list->insert(new_sing, new_sing); |
|---|
| 188 | selected_list->insert_default("", ""); |
|---|
| 189 | selected_list->sort(false, true); |
|---|
| 190 | selected_list->update(); |
|---|
| 191 | awr->awar(MP_AWAR_SELECTEDPROBES)->write_string(new_sing); |
|---|
| 192 | delete new_sing; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | static void init_system3_tab() { |
|---|
| 196 | int i, j, k, |
|---|
| 197 | size_hamming_tab, |
|---|
| 198 | hamm_dist; |
|---|
| 199 | int **dummy_int; |
|---|
| 200 | int counter, wert; |
|---|
| 201 | |
|---|
| 202 | if (system3_tab) { |
|---|
| 203 | for (j=0; j< system3_tab_size; j++) |
|---|
| 204 | delete [] system3_tab[j]; |
|---|
| 205 | |
|---|
| 206 | delete [] system3_tab; |
|---|
| 207 | } |
|---|
| 208 | system3_tab = new int*[mp_gl_awars.no_of_probes]; |
|---|
| 209 | system3_tab_size = mp_gl_awars.no_of_probes; |
|---|
| 210 | for (j=0; j<mp_gl_awars.no_of_probes; j++) { |
|---|
| 211 | system3_tab[j] = new int[3]; |
|---|
| 212 | memset(system3_tab[j], 0, sizeof(int) * 3); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | for (j=0; j< mp_gl_awars.no_of_probes; j++) { |
|---|
| 216 | for (k=0; k<3; k++) { |
|---|
| 217 | if (!j) system3_tab[j][k] = k; |
|---|
| 218 | else system3_tab[j][k] = system3_tab[j-1][k] * 3; |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | // ** hamming_tab |
|---|
| 224 | if (hamming_tab) |
|---|
| 225 | delete [] hamming_tab; |
|---|
| 226 | |
|---|
| 227 | size_hamming_tab = (int)pow(3.0, (double) mp_gl_awars.no_of_probes); |
|---|
| 228 | hamming_tab = new unsigned char*[size_hamming_tab]; |
|---|
| 229 | dummy_int = new int*[size_hamming_tab]; |
|---|
| 230 | |
|---|
| 231 | for (i=0; i<size_hamming_tab; i++) { |
|---|
| 232 | hamming_tab[i] = new unsigned char[size_hamming_tab]; |
|---|
| 233 | memset(hamming_tab[i], 0, sizeof(unsigned char) * size_hamming_tab); |
|---|
| 234 | |
|---|
| 235 | dummy_int[i] = new int[mp_gl_awars.no_of_probes]; |
|---|
| 236 | memset(dummy_int[i], 0, sizeof(int) * mp_gl_awars.no_of_probes); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | counter = 1; |
|---|
| 240 | for (i=0; i< mp_gl_awars.no_of_probes; i++) { |
|---|
| 241 | for (j=0; j<size_hamming_tab; j++) { |
|---|
| 242 | for (wert = 0; wert < 3; wert++) { |
|---|
| 243 | for (k=0; k<counter; k++) { |
|---|
| 244 | dummy_int[j++][i] = wert; |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | j--; |
|---|
| 248 | } |
|---|
| 249 | counter *= 3; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | for (i=0; i<size_hamming_tab; i++) { |
|---|
| 253 | for (j=0; j<size_hamming_tab; j++) { |
|---|
| 254 | hamm_dist = 0; |
|---|
| 255 | for (k=0; k<mp_gl_awars.no_of_probes; k++) { |
|---|
| 256 | if ((dummy_int[i][k] == 2 && dummy_int[j][k] == 0) || |
|---|
| 257 | (dummy_int[i][k] == 0 && dummy_int[j][k] == 2)) |
|---|
| 258 | hamm_dist++; |
|---|
| 259 | } |
|---|
| 260 | hamming_tab[i][j] = hamm_dist; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | for (i=0; i<size_hamming_tab; i++) { |
|---|
| 265 | delete [] dummy_int[i]; |
|---|
| 266 | } |
|---|
| 267 | delete [] dummy_int; |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | void MP_popup_result_window(AW_window */*aww*/) { |
|---|
| 274 | mp_main->get_mp_window()->create_result_window(mp_main->get_aw_root())->activate(); |
|---|
| 275 | init_system3_tab(); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | bool MP_aborted(int gen_cnt, double avg_fit, double min_fit, double max_fit, arb_progress& progress) { |
|---|
| 279 | char view_text[150]; |
|---|
| 280 | |
|---|
| 281 | if (gen_cnt == 0) |
|---|
| 282 | sprintf(view_text, "Evaluating first generation"); |
|---|
| 283 | else |
|---|
| 284 | sprintf(view_text, "Gen:%d Avg:%5i Min:%5i Max:%5i", gen_cnt, int(avg_fit), int(min_fit), int(max_fit)); |
|---|
| 285 | |
|---|
| 286 | progress.subtitle(view_text); |
|---|
| 287 | return progress.aborted(); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | void MP_compute(AW_window*, GBDATA *gb_main) { |
|---|
| 292 | AW_root *aw_root = mp_main->get_aw_root(); |
|---|
| 293 | AW_window *aww2; |
|---|
| 294 | int i = 0; |
|---|
| 295 | int *bew_array; |
|---|
| 296 | char *ptr, *qual; |
|---|
| 297 | char **probe_field; |
|---|
| 298 | int *single_mismatch; |
|---|
| 299 | ProbeValuation *p_eval = NULp; |
|---|
| 300 | |
|---|
| 301 | MO_Liste::set_gb_main(gb_main); |
|---|
| 302 | |
|---|
| 303 | size_t selected_probes_count = selected_list->size(); |
|---|
| 304 | if ((int)selected_probes_count < mp_gl_awars.no_of_probes) { |
|---|
| 305 | aw_message("Not enough probes selected for computation !!!"); |
|---|
| 306 | return; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | if (mp_main->get_stc()) { |
|---|
| 310 | delete mp_main->get_stc(); |
|---|
| 311 | mp_main->set_stc(NULp); |
|---|
| 312 | new_pt_server = true; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | init_system3_tab(); |
|---|
| 316 | |
|---|
| 317 | aww2 = mp_main->get_mp_window()->create_result_window(aw_root); |
|---|
| 318 | |
|---|
| 319 | result_probes_list->clear(); |
|---|
| 320 | result_probes_list->insert_default("", ""); |
|---|
| 321 | result_probes_list->update(); |
|---|
| 322 | |
|---|
| 323 | AW_selection_list_iterator selentry(selected_list); |
|---|
| 324 | probe_field = new char*[selected_probes_count]; |
|---|
| 325 | bew_array = new int[selected_probes_count]; |
|---|
| 326 | single_mismatch = new int[selected_probes_count]; |
|---|
| 327 | |
|---|
| 328 | arb_progress progress("Computing multiprobes"); |
|---|
| 329 | |
|---|
| 330 | while (selentry) { |
|---|
| 331 | const char *ptr2 = selentry.get_value()->get_string(); |
|---|
| 332 | ++selentry; |
|---|
| 333 | ptr = MP_get_probes(ptr2); // hier sind es einfachsonden |
|---|
| 334 | if (ptr && ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') { |
|---|
| 335 | qual = MP_get_comment(1, ptr2); |
|---|
| 336 | bew_array[i] = atoi(qual); |
|---|
| 337 | free(qual); |
|---|
| 338 | |
|---|
| 339 | qual = MP_get_comment(2, ptr2); |
|---|
| 340 | single_mismatch[i] = atoi(qual); // single mismatch kann zwar eingestellt werden, aber wird noch nicht uebergeben |
|---|
| 341 | free(qual); |
|---|
| 342 | |
|---|
| 343 | probe_field[i++] = ptr; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | p_eval = mp_main->new_probe_eval(probe_field, i, bew_array, single_mismatch); |
|---|
| 348 | p_eval->init_valuation(); |
|---|
| 349 | |
|---|
| 350 | if (pt_server_different) { |
|---|
| 351 | pt_server_different = false; |
|---|
| 352 | aw_message("There are species in the tree which are\nnot included in the PT-Server"); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | mp_main->destroy_probe_eval(); |
|---|
| 356 | |
|---|
| 357 | aww2->activate(); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | static bool MP_is_probe(char *seq) { |
|---|
| 361 | bool result=true; |
|---|
| 362 | char *s, |
|---|
| 363 | *seq2; |
|---|
| 364 | |
|---|
| 365 | if (! seq) |
|---|
| 366 | return false; |
|---|
| 367 | |
|---|
| 368 | seq2 = MP_get_probes(seq); |
|---|
| 369 | if (!seq2 || ! seq2[0]) |
|---|
| 370 | return false; |
|---|
| 371 | |
|---|
| 372 | s = seq2; |
|---|
| 373 | while (*s && result) { |
|---|
| 374 | result = result && MP_probe_tab[(unsigned char)*s]; |
|---|
| 375 | s++; |
|---|
| 376 | } |
|---|
| 377 | free(seq2); |
|---|
| 378 | return result; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | void MP_new_sequence(AW_window *aww) { |
|---|
| 382 | AW_root *aw_root = aww->get_root(); |
|---|
| 383 | char *seq = aw_root->awar(MP_AWAR_SEQIN)->read_string(); |
|---|
| 384 | |
|---|
| 385 | if (MP_is_probe(seq)) { |
|---|
| 386 | char *new_seq = GBS_global_string_copy("%1ld#%1ld#%6d#%s", mp_gl_awars.probe_quality, mp_gl_awars.singlemismatches, 0, seq); |
|---|
| 387 | |
|---|
| 388 | selected_list->insert(new_seq, new_seq); |
|---|
| 389 | selected_list->sort(false, true); |
|---|
| 390 | selected_list->update(); |
|---|
| 391 | |
|---|
| 392 | aw_root->awar(MP_AWAR_SELECTEDPROBES)->write_string(new_seq); |
|---|
| 393 | free(new_seq); |
|---|
| 394 | } |
|---|
| 395 | else { |
|---|
| 396 | aw_message(GBS_global_string("'%s' is no valid probe", seq)); |
|---|
| 397 | } |
|---|
| 398 | free(seq); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | void MP_cache_sonden(AW_window *) { new_pt_server = true; } |
|---|
| 402 | void MP_cache_sonden2(AW_root *) { new_pt_server = true; } |
|---|
| 403 | |
|---|
| 404 | void MP_show_probes_in_tree_move(AW_window *aww, bool backward, AW_selection_list *resultProbesList) { |
|---|
| 405 | resultProbesList->move_selection(backward ? -1 : 1); |
|---|
| 406 | MP_show_probes_in_tree(aww); |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | void MP_show_probes_in_tree(AW_window */*aww*/) { |
|---|
| 410 | TREE_canvas *scr = mp_main->get_canvas(); |
|---|
| 411 | |
|---|
| 412 | char *mism, *mism_temp; |
|---|
| 413 | char *a_probe, *another_probe, *the_probe, *mism_temp2; |
|---|
| 414 | int i, how_many_probes = 0; |
|---|
| 415 | |
|---|
| 416 | { |
|---|
| 417 | char *sel = mp_main->get_aw_root()->awar(MP_AWAR_RESULTPROBES)->read_string(); |
|---|
| 418 | a_probe = MP_get_probes(sel); // haelt jetzt Sondenstring |
|---|
| 419 | if (! a_probe || ! a_probe[0]) { |
|---|
| 420 | free(a_probe); |
|---|
| 421 | free(sel); |
|---|
| 422 | return; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | mism_temp2 = MP_get_comment(2, sel); |
|---|
| 426 | mism_temp = mism_temp2; |
|---|
| 427 | free(sel); |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | char **probe_field = new char*[MAXMISMATCHES]; |
|---|
| 431 | int *mismatches = new int[MAXMISMATCHES]; |
|---|
| 432 | |
|---|
| 433 | for (i=0; i<MAXMISMATCHES; i++) { |
|---|
| 434 | probe_field[i] = NULp; |
|---|
| 435 | mismatches[i] = 0; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | another_probe = a_probe; |
|---|
| 439 | for (i=0; i< MAXPROBECOMBIS; i++) { // generiert probe_field und mismatches array |
|---|
| 440 | mism = strchr(mism_temp, ' '); |
|---|
| 441 | if (mism) |
|---|
| 442 | *(mism++) = 0; |
|---|
| 443 | |
|---|
| 444 | mismatches[i] = atoi(mism_temp); |
|---|
| 445 | mism_temp = mism; |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | probe_field[i] = NULp; |
|---|
| 449 | the_probe = another_probe; |
|---|
| 450 | another_probe = strchr(another_probe, ' '); |
|---|
| 451 | if (another_probe) { |
|---|
| 452 | *(another_probe++) = 0; |
|---|
| 453 | while (*another_probe == ' ' || *another_probe == '\t') another_probe++; |
|---|
| 454 | } |
|---|
| 455 | else { |
|---|
| 456 | probe_field[i] = ARB_strdup(the_probe); |
|---|
| 457 | how_many_probes ++; |
|---|
| 458 | break; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | if (the_probe && the_probe[0]) { |
|---|
| 462 | probe_field[i] = ARB_strdup(the_probe); |
|---|
| 463 | how_many_probes ++; |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | free(a_probe); |
|---|
| 468 | free(mism_temp2); |
|---|
| 469 | |
|---|
| 470 | if (new_pt_server) { |
|---|
| 471 | new_pt_server = false; |
|---|
| 472 | |
|---|
| 473 | if (mp_main->get_stc()) delete mp_main->get_stc(); |
|---|
| 474 | |
|---|
| 475 | mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE)); |
|---|
| 476 | if (pt_server_different) { |
|---|
| 477 | mp_main->set_stc(NULp); |
|---|
| 478 | new_pt_server = true; |
|---|
| 479 | aw_message("There are species in the tree which are\nnot included in the PT-Server"); |
|---|
| 480 | pt_server_different = false; |
|---|
| 481 | return; |
|---|
| 482 | } |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | delete mp_main->get_stc()->sondentopf; |
|---|
| 486 | mp_main->get_stc()->sondentopf = new Sondentopf(mp_main->get_stc()->Bakterienliste, mp_main->get_stc()->Auswahlliste); |
|---|
| 487 | |
|---|
| 488 | for (i=0; i<MAXMISMATCHES; i++) { |
|---|
| 489 | if (probe_field[i]) { |
|---|
| 490 | mp_main->get_stc()->sondentopf->put_Sonde(probe_field[i], mismatches[i], mismatches[i] + mp_gl_awars.outside_mismatches_difference); |
|---|
| 491 | } |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | mp_main->get_stc()->sondentopf->gen_color_hash(mp_gl_awars.no_of_probes); |
|---|
| 495 | |
|---|
| 496 | GB_transaction ta(scr->gb_main); |
|---|
| 497 | AWT_TREE(scr)->get_root_node()->colorize(mp_main->get_stc()->sondentopf->get_color_hash()); |
|---|
| 498 | refresh_without_dbsync(scr); |
|---|
| 499 | |
|---|
| 500 | for (i=0; i<MAXMISMATCHES; i++) |
|---|
| 501 | free(probe_field[i]); |
|---|
| 502 | |
|---|
| 503 | delete [] probe_field; |
|---|
| 504 | delete [] mismatches; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | void MP_mark_probes_in_tree(AW_window *aww) { |
|---|
| 508 | AWT_canvas *scr = mp_main->get_canvas(); |
|---|
| 509 | char *mism, *mism_temp; |
|---|
| 510 | char *a_probe, *another_probe, *the_probe, *mism_temp2; |
|---|
| 511 | int i, how_many_probes = 0; |
|---|
| 512 | GBDATA *gb_species; |
|---|
| 513 | |
|---|
| 514 | { |
|---|
| 515 | char *sel = mp_main->get_aw_root()->awar(MP_AWAR_RESULTPROBES)->read_string(); |
|---|
| 516 | a_probe = MP_get_probes(sel); // haelt jetzt Sondenstring |
|---|
| 517 | |
|---|
| 518 | if (! a_probe || ! a_probe[0]) { |
|---|
| 519 | free(a_probe); |
|---|
| 520 | free(sel); |
|---|
| 521 | return; |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | mism_temp2 = MP_get_comment(2, sel); |
|---|
| 525 | mism_temp = mism_temp2; |
|---|
| 526 | free(sel); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | char **probe_field = new char*[MAXMISMATCHES]; |
|---|
| 530 | int *mismatches = new int[MAXMISMATCHES]; |
|---|
| 531 | |
|---|
| 532 | for (i=0; i<MAXMISMATCHES; i++) { |
|---|
| 533 | probe_field[i] = NULp; |
|---|
| 534 | mismatches[i] = 0; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | another_probe = a_probe; |
|---|
| 538 | for (i=0; i< MAXPROBECOMBIS; i++) { // generiert probe_field und mismatches array |
|---|
| 539 | mism = strchr(mism_temp, ' '); |
|---|
| 540 | if (mism) *(mism++) = 0; |
|---|
| 541 | |
|---|
| 542 | mismatches[i] = atoi(mism_temp); |
|---|
| 543 | mism_temp = mism; |
|---|
| 544 | |
|---|
| 545 | probe_field[i] = NULp; |
|---|
| 546 | the_probe = another_probe; |
|---|
| 547 | another_probe = strchr(another_probe, ' '); |
|---|
| 548 | if (another_probe) { |
|---|
| 549 | *(another_probe++) = 0; |
|---|
| 550 | while (*another_probe == ' ' || *another_probe == '\t') |
|---|
| 551 | another_probe++; |
|---|
| 552 | } |
|---|
| 553 | else { |
|---|
| 554 | probe_field[i] = ARB_strdup(the_probe); |
|---|
| 555 | how_many_probes ++; |
|---|
| 556 | break; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | if (the_probe && the_probe[0]) { |
|---|
| 560 | probe_field[i] = ARB_strdup(the_probe); |
|---|
| 561 | how_many_probes ++; |
|---|
| 562 | } |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | free(a_probe); |
|---|
| 566 | free(mism_temp2); |
|---|
| 567 | |
|---|
| 568 | if (new_pt_server) { |
|---|
| 569 | new_pt_server = false; |
|---|
| 570 | |
|---|
| 571 | if (mp_main->get_stc()) |
|---|
| 572 | delete mp_main->get_stc(); |
|---|
| 573 | |
|---|
| 574 | mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE)); |
|---|
| 575 | if (pt_server_different) { |
|---|
| 576 | mp_main->set_stc(NULp); |
|---|
| 577 | new_pt_server = true; |
|---|
| 578 | aw_message("There are species in the tree which are not included in the PT-Server"); |
|---|
| 579 | pt_server_different = false; |
|---|
| 580 | return; |
|---|
| 581 | } |
|---|
| 582 | } |
|---|
| 583 | |
|---|
| 584 | delete mp_main->get_stc()->sondentopf; |
|---|
| 585 | mp_main->get_stc()->sondentopf = new Sondentopf(mp_main->get_stc()->Bakterienliste, mp_main->get_stc()->Auswahlliste); |
|---|
| 586 | |
|---|
| 587 | for (i=0; i<MAXMISMATCHES; i++) { |
|---|
| 588 | if (probe_field[i]) { |
|---|
| 589 | mp_main->get_stc()->sondentopf->put_Sonde(probe_field[i], mismatches[i], mismatches[i] + mp_gl_awars.outside_mismatches_difference); |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | mp_main->get_stc()->sondentopf->gen_color_hash(mp_gl_awars.no_of_probes); |
|---|
| 593 | |
|---|
| 594 | { |
|---|
| 595 | GB_push_transaction(scr->gb_main); |
|---|
| 596 | GB_HASH *col_hash = mp_main->get_stc()->sondentopf->get_color_hash(); |
|---|
| 597 | for (gb_species = GBT_first_species(scr->gb_main); gb_species; gb_species = GBT_next_species(gb_species)) { |
|---|
| 598 | GB_write_flag(gb_species, GBS_read_hash(col_hash, GBT_get_name_or_description(gb_species)) > AWT_GC_BLACK); |
|---|
| 599 | } |
|---|
| 600 | } |
|---|
| 601 | GB_pop_transaction(scr->gb_main); |
|---|
| 602 | |
|---|
| 603 | GB_transaction ta(scr->gb_main); |
|---|
| 604 | refresh_without_dbsync(scr); |
|---|
| 605 | |
|---|
| 606 | for (i=0; i<MAXMISMATCHES; i++) |
|---|
| 607 | free(probe_field[i]); |
|---|
| 608 | |
|---|
| 609 | delete [] probe_field; |
|---|
| 610 | delete [] mismatches; |
|---|
| 611 | |
|---|
| 612 | MP_normal_colors_in_tree(aww); |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | void MP_Comment(AW_window *, const char *new_comment) { |
|---|
| 616 | // Comment fuer Auswahl eintragen |
|---|
| 617 | |
|---|
| 618 | AW_root *awr = mp_main->get_aw_root(); |
|---|
| 619 | char *selprobes = awr->awar(MP_AWAR_RESULTPROBES)->read_string(); |
|---|
| 620 | char *new_val = MP_get_probes(selprobes); |
|---|
| 621 | |
|---|
| 622 | if (new_val && new_val[0]) { |
|---|
| 623 | char *new_list_string; |
|---|
| 624 | { |
|---|
| 625 | char *edited_comment = NULp; |
|---|
| 626 | const char *comment = new_comment; |
|---|
| 627 | |
|---|
| 628 | if (!comment) { |
|---|
| 629 | edited_comment = awr->awar(MP_AWAR_RESULTPROBESCOMMENT)->read_string(); |
|---|
| 630 | for (int i = 0; edited_comment[i]; ++i) { // remove all '#' from new comment |
|---|
| 631 | if (edited_comment[i] == SEPARATOR[0]) { |
|---|
| 632 | edited_comment[i] = '|'; |
|---|
| 633 | } |
|---|
| 634 | } |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | char *misms = MP_get_comment(2, selprobes); |
|---|
| 638 | char *ecol = MP_get_comment(3, selprobes); |
|---|
| 639 | |
|---|
| 640 | new_list_string = GBS_global_string_copy("%-20s" SEPARATOR "%s" SEPARATOR "%s" SEPARATOR "%s", |
|---|
| 641 | comment, misms, ecol, new_val); |
|---|
| 642 | free(ecol); |
|---|
| 643 | free(misms); |
|---|
| 644 | free(edited_comment); |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | { |
|---|
| 648 | bool autoadvance = awr->awar(MP_AWAR_AUTOADVANCE)->read_int(); |
|---|
| 649 | |
|---|
| 650 | if (autoadvance) result_probes_list->move_selection(1); |
|---|
| 651 | |
|---|
| 652 | result_probes_list->delete_value(AW_scalar(selprobes)); |
|---|
| 653 | result_probes_list->insert(new_list_string, new_list_string); |
|---|
| 654 | result_probes_list->update(); |
|---|
| 655 | |
|---|
| 656 | if (!autoadvance) awr->awar(MP_AWAR_RESULTPROBES)->write_string(new_list_string); |
|---|
| 657 | } |
|---|
| 658 | free(new_list_string); |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | free(new_val); |
|---|
| 662 | free(selprobes); |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | void MP_selected_chosen(AW_root *aw_root) { // @@@ move to MP_Window.cxx |
|---|
| 666 | const char *selected = aw_root->awar(MP_AWAR_SELECTEDPROBES)->read_char_pntr(); |
|---|
| 667 | if (selected && selected[0]) { |
|---|
| 668 | char *probe = MP_get_comment(3, selected); |
|---|
| 669 | if (probe) { |
|---|
| 670 | aw_root->awar(MP_AWAR_ECOLIPOS)->write_int(atoi(probe)); |
|---|
| 671 | free(probe); // @@@ check/fix ressource handling of probe! |
|---|
| 672 | |
|---|
| 673 | if (probe) { |
|---|
| 674 | probe = MP_get_comment(1, selected); |
|---|
| 675 | aw_root->awar(MP_AWAR_QUALITY)->write_int(atoi(probe)); |
|---|
| 676 | free(probe); |
|---|
| 677 | |
|---|
| 678 | if (probe) { |
|---|
| 679 | probe = MP_get_comment(2, selected); |
|---|
| 680 | aw_root->awar(MP_AWAR_SINGLEMISMATCHES)->write_int(atoi(probe)); |
|---|
| 681 | free(probe); |
|---|
| 682 | |
|---|
| 683 | if (probe) { |
|---|
| 684 | probe = MP_get_probes(selected); |
|---|
| 685 | aw_root->awar_string(MP_AWAR_SEQIN)->write_string(MP_get_probes(probe)); |
|---|
| 686 | free(probe); |
|---|
| 687 | return; |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | } |
|---|
| 691 | } |
|---|
| 692 | aw_message("can't parse entry"); |
|---|
| 693 | } |
|---|
| 694 | } |
|---|
| 695 | |
|---|
| 696 | void MP_group_all_except_marked(AW_window * /* aww */) { |
|---|
| 697 | TREE_canvas *scr = mp_main->get_canvas(); |
|---|
| 698 | NT_expand_marked_cb(NULp, scr); |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | void MP_normal_colors_in_tree(AW_window */*aww*/) { |
|---|
| 702 | TREE_canvas *scr = mp_main->get_canvas(); |
|---|
| 703 | GB_transaction ta(scr->gb_main); |
|---|
| 704 | |
|---|
| 705 | AWT_graphic_tree *tree = AWT_TREE(scr); |
|---|
| 706 | if (tree) { |
|---|
| 707 | AP_tree *root = tree->get_root_node(); |
|---|
| 708 | if (root) { |
|---|
| 709 | root->uncolorize(); |
|---|
| 710 | refresh_without_dbsync(scr); |
|---|
| 711 | } |
|---|
| 712 | } |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | void MP_delete_selected(UNFIXED, AW_selection_list *sellist) { |
|---|
| 716 | if (!sellist->default_is_selected()) { |
|---|
| 717 | int idx = sellist->get_index_of_selected(); |
|---|
| 718 | sellist->delete_element_at(idx); |
|---|
| 719 | sellist->select_element_at(idx); |
|---|
| 720 | sellist->update(); |
|---|
| 721 | } |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | char *MP_get_comment(int which, const char *str) { |
|---|
| 726 | // Parses information from strings like "val1#val2#val3#probes". |
|---|
| 727 | // 'which' == 1 -> 'val1' |
|---|
| 728 | // 'which' == 2 -> 'val2' |
|---|
| 729 | // ... |
|---|
| 730 | // Values may be present or not (i.e. 'probes', 'val1#probes', ... are also valid as 'str') |
|---|
| 731 | |
|---|
| 732 | char *result = NULp; |
|---|
| 733 | const char *numsign = strchr(str, '#'); |
|---|
| 734 | |
|---|
| 735 | mp_assert(which >= 1); |
|---|
| 736 | |
|---|
| 737 | if (numsign) { |
|---|
| 738 | if (which == 1) { |
|---|
| 739 | result = ARB_strpartdup(str, numsign-1); |
|---|
| 740 | } |
|---|
| 741 | else { |
|---|
| 742 | result = MP_get_comment(which-1, numsign+1); |
|---|
| 743 | } |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | return result; |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | void MP_result_combination_chosen(AW_root *aw_root) { // @@@ move to MP_Window.cxx |
|---|
| 750 | const char *str = aw_root->awar(MP_AWAR_RESULTPROBES)->read_char_pntr(); |
|---|
| 751 | char *new_str = MP_get_comment(1, str); |
|---|
| 752 | |
|---|
| 753 | aw_root->awar(MP_AWAR_RESULTPROBESCOMMENT)->write_string(new_str); |
|---|
| 754 | |
|---|
| 755 | free(new_str); |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | // |
|---|
| 761 | // functions concerning the server |
|---|
| 762 | // |
|---|
| 763 | |
|---|
| 764 | int MP_init_local_com_struct(mp_gl_struct& mp_pd_gl) { |
|---|
| 765 | const char *user = GB_getenvUSER(); |
|---|
| 766 | |
|---|
| 767 | if (aisc_create(mp_pd_gl.link, PT_MAIN, mp_pd_gl.com, |
|---|
| 768 | MAIN_LOCS, PT_LOCS, mp_pd_gl.locs, |
|---|
| 769 | LOCS_USER, user, |
|---|
| 770 | NULp)) { |
|---|
| 771 | return 1; |
|---|
| 772 | } |
|---|
| 773 | return 0; |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | // -------------------------------------------------------------------------------- |
|---|
| 777 | |
|---|
| 778 | #ifdef UNIT_TESTS |
|---|
| 779 | |
|---|
| 780 | #include <test_unit.h> |
|---|
| 781 | |
|---|
| 782 | #define MP_GET_COMMENT_EQUAL(which, str, expect) \ |
|---|
| 783 | do { \ |
|---|
| 784 | char *got = MP_get_comment(which, str); \ |
|---|
| 785 | TEST_EXPECT_EQUAL(got, expect); \ |
|---|
| 786 | free(got); \ |
|---|
| 787 | } while(0) \ |
|---|
| 788 | |
|---|
| 789 | #define MP_GET_PROBES_EQUAL(str, expect) \ |
|---|
| 790 | do { \ |
|---|
| 791 | char *got = MP_get_probes(str); \ |
|---|
| 792 | TEST_EXPECT_EQUAL(got, expect); \ |
|---|
| 793 | free(got); \ |
|---|
| 794 | } while(0) \ |
|---|
| 795 | |
|---|
| 796 | void TEST_MP_get_comment_and_probes() { |
|---|
| 797 | char *probes_only = ARB_strdup("ACGT TGCA ATCG"); |
|---|
| 798 | char *probes_comment = ARB_strdup("val1#val2#val3#ACGT TGCA ATCG"); |
|---|
| 799 | char *probes_1 = ARB_strdup("one# \t ACGT TGCA ATCG"); |
|---|
| 800 | char *probes_2 = ARB_strdup("one#two#\tACGT TGCA ATCG"); |
|---|
| 801 | |
|---|
| 802 | MP_GET_PROBES_EQUAL(probes_only, "ACGT TGCA ATCG"); |
|---|
| 803 | MP_GET_PROBES_EQUAL(probes_comment, "ACGT TGCA ATCG"); |
|---|
| 804 | MP_GET_PROBES_EQUAL(probes_1, "ACGT TGCA ATCG"); |
|---|
| 805 | MP_GET_PROBES_EQUAL(probes_2, "ACGT TGCA ATCG"); |
|---|
| 806 | |
|---|
| 807 | MP_GET_COMMENT_EQUAL(1, probes_comment, "val1"); |
|---|
| 808 | MP_GET_COMMENT_EQUAL(2, probes_comment, "val2"); |
|---|
| 809 | MP_GET_COMMENT_EQUAL(3, probes_comment, "val3"); |
|---|
| 810 | |
|---|
| 811 | MP_GET_COMMENT_EQUAL(1, probes_only, (const char *)NULp); |
|---|
| 812 | MP_GET_COMMENT_EQUAL(2, probes_only, (const char *)NULp); |
|---|
| 813 | MP_GET_COMMENT_EQUAL(3, probes_only, (const char *)NULp); |
|---|
| 814 | |
|---|
| 815 | MP_GET_COMMENT_EQUAL(1, probes_1, "one"); |
|---|
| 816 | MP_GET_COMMENT_EQUAL(2, probes_1, (const char *)NULp); |
|---|
| 817 | MP_GET_COMMENT_EQUAL(3, probes_1, (const char *)NULp); |
|---|
| 818 | |
|---|
| 819 | MP_GET_COMMENT_EQUAL(1, probes_2, "one"); |
|---|
| 820 | MP_GET_COMMENT_EQUAL(2, probes_2, "two"); |
|---|
| 821 | MP_GET_COMMENT_EQUAL(3, probes_2, (const char *)NULp); |
|---|
| 822 | |
|---|
| 823 | free(probes_2); |
|---|
| 824 | free(probes_1); |
|---|
| 825 | free(probes_comment); |
|---|
| 826 | free(probes_only); |
|---|
| 827 | } |
|---|
| 828 | |
|---|
| 829 | #endif // UNIT_TESTS |
|---|