source: branches/stable/MULTI_PROBE/MP_noclass.cxx

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