source: tags/ms_r18q1/MULTI_PROBE/MP_noclass.cxx

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