source: tags/ms_r16q3/MULTI_PROBE/MP_noclass.cxx

Last change on this file was 15176, checked in by westram, 8 years ago
  • 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      = NULL;
30static int   system3_tab_size = 0;
31
32unsigned char **hamming_tab   = NULL;
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++) ;
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{
51    AWT_canvas  *scr = mp_main->get_canvas();
52
53    if (mp_main->get_mp_window()->get_result_window())
54        mp_main->get_mp_window()->get_result_window()->hide();
55
56    GB_transaction ta(scr->gb_main);
57
58    AP_tree *ap_tree = AWT_TREE(scr)->get_root_node();
59    if (ap_tree) ap_tree->uncolorize();
60
61    if (scr->gb_main)
62        scr->gfx->update(scr->gb_main);
63
64    scr->refresh();
65
66    AW_POPDOWN(aww);
67    delete mp_main->get_p_eval();
68    mp_main->set_p_eval(NULL);
69    delete mp_main->get_stc();
70    mp_main->set_stc(NULL);
71
72    new_pt_server = true;
73}
74
75static 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
88void MP_gen_quality(AW_root *awr) {
89    bool firsttime = true; // @@@ was this static in the past?
90
91    if (firsttime)
92    {
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(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    const char *ptr2;
132    while ((ptr2 = selentry.get_value())) {
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    ptr2 = l->get_first();
156    while (ptr2)
157    {
158        l->remove_first();
159        selected_list->insert(ptr2, ptr2);
160        delete ptr2;
161        ptr2 = l->get_first();
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
173void 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(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
195static void init_system3_tab()
196{
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    {
213        system3_tab[j] = new int[3];
214        memset(system3_tab[j], 0, sizeof(int) * 3);
215    }
216
217    for (j=0; j< mp_gl_awars.no_of_probes; j++)
218    {
219        for (k=0; k<3; k++)
220        {
221            if (!j) system3_tab[j][k] = k;
222            else    system3_tab[j][k] = system3_tab[j-1][k] * 3;
223        }
224    }
225
226
227    // ** hamming_tab
228        if (hamming_tab)
229            delete [] hamming_tab;
230
231        size_hamming_tab = (int)pow(3.0, (double) mp_gl_awars.no_of_probes);
232        hamming_tab = new unsigned char*[size_hamming_tab];
233        dummy_int = new int*[size_hamming_tab];
234
235        for (i=0; i<size_hamming_tab; i++)
236        {
237            hamming_tab[i] = new unsigned char[size_hamming_tab];
238            memset(hamming_tab[i], 0, sizeof(unsigned char) * size_hamming_tab);
239
240            dummy_int[i] = new int[mp_gl_awars.no_of_probes];
241            memset(dummy_int[i], 0, sizeof(int) * mp_gl_awars.no_of_probes);
242        }
243
244        counter = 1;
245        for (i=0; i< mp_gl_awars.no_of_probes; i++)
246        {
247            for (j=0; j<size_hamming_tab; j++)
248            {
249                for (wert = 0; wert < 3; wert++)
250                {
251                    for (k=0; k<counter; k++)
252                    {
253                        dummy_int[j++][i] = wert;
254                    }
255                }
256                j--;
257            }
258            counter *= 3;
259        }
260
261        for (i=0; i<size_hamming_tab; i++)
262        {
263            for (j=0; j<size_hamming_tab; j++)
264            {
265                hamm_dist = 0;
266                for (k=0; k<mp_gl_awars.no_of_probes; k++)
267                {
268                    if ((dummy_int[i][k] == 2 && dummy_int[j][k] == 0) ||
269                        (dummy_int[i][k] == 0 && dummy_int[j][k] == 2))
270                        hamm_dist++;
271                }
272                hamming_tab[i][j] = hamm_dist;
273            }
274        }
275
276        for (i=0; i<size_hamming_tab; i++)
277        {
278            delete [] dummy_int[i];
279        }
280        delete [] dummy_int;
281
282
283
284}
285
286void MP_popup_result_window(AW_window */*aww*/) {
287    mp_main->get_mp_window()->create_result_window(mp_main->get_aw_root())->activate();
288    init_system3_tab();
289}
290
291bool MP_aborted(int gen_cnt, double avg_fit, double min_fit, double max_fit, arb_progress& progress) {
292    char view_text[150];
293
294    if (gen_cnt == 0)
295        sprintf(view_text, "Evaluating first generation");
296    else
297        sprintf(view_text, "Gen:%d Avg:%5i Min:%5i Max:%5i", gen_cnt, int(avg_fit), int(min_fit), int(max_fit));
298
299    progress.subtitle(view_text);
300    return progress.aborted();
301}
302
303
304void MP_compute(AW_window*, GBDATA *gb_main) {
305    AW_root         *aw_root = mp_main->get_aw_root();
306    AW_window       *aww2;
307    int              i       = 0;
308    int             *bew_array;
309    char            *ptr, *qual;
310    char           **probe_field;
311    int             *single_mismatch;
312    ProbeValuation  *p_eval  = NULL;
313
314    MO_Liste::set_gb_main(gb_main);
315
316    size_t selected_probes_count = selected_list->size();
317    if ((int)selected_probes_count < mp_gl_awars.no_of_probes) {
318        aw_message("Not enough probes selected for computation !!!");
319        return;
320    }
321
322    if (mp_main->get_stc())
323    {
324        delete mp_main->get_stc();
325        mp_main->set_stc(NULL);
326        new_pt_server = true;
327    }
328
329    init_system3_tab();
330
331    aww2 = mp_main->get_mp_window()->create_result_window(aw_root);
332
333    result_probes_list->clear();
334    result_probes_list->insert_default("", "");
335    result_probes_list->update();
336
337    AW_selection_list_iterator selentry(selected_list);
338    probe_field     = new char*[selected_probes_count];
339    bew_array       = new int[selected_probes_count];
340    single_mismatch = new int[selected_probes_count];
341
342    arb_progress progress("Computing multiprobes");
343
344    const char *ptr2;
345    while ((ptr2 = selentry.get_value())) {
346        ++selentry;
347        ptr = MP_get_probes(ptr2);      // hier sind es einfachsonden
348        if (ptr && ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') {
349            qual         = MP_get_comment(1, ptr2);
350            bew_array[i] = atoi(qual);
351            free(qual);
352
353            qual               = MP_get_comment(2, ptr2);
354            single_mismatch[i] = atoi(qual); // single mismatch kann zwar eingestellt werden, aber wird noch nicht uebergeben
355            free(qual);
356
357            probe_field[i++] = ptr;
358        }
359    }
360
361    p_eval = mp_main->new_probe_eval(probe_field, i, bew_array, single_mismatch);
362    p_eval->init_valuation();
363
364    if (pt_server_different)
365    {
366        pt_server_different = false;
367        aw_message("There are species in the tree which are\nnot included in the PT-Server");
368    }
369
370    mp_main->destroy_probe_eval();
371
372    aww2->activate();
373}
374
375static bool MP_is_probe(char *seq) {
376    bool    result=true;
377    char    *s,
378        *seq2;
379
380    if (! seq)
381        return false;
382
383    seq2 = MP_get_probes(seq);
384    if (!seq2 || ! seq2[0])
385        return false;
386
387    s = seq2;
388    while (*s && result)
389    {
390        result = result && MP_probe_tab[(unsigned char)*s];
391        s++;
392    }
393    free(seq2);
394    return result;
395}
396
397void MP_new_sequence(AW_window *aww) {
398    AW_root *aw_root = aww->get_root();
399    char    *seq     = aw_root->awar(MP_AWAR_SEQIN)->read_string();
400
401    if (MP_is_probe(seq)) {
402        char *new_seq = GBS_global_string_copy("%1ld#%1ld#%6d#%s", mp_gl_awars.probe_quality, mp_gl_awars.singlemismatches, 0, seq);
403
404        selected_list->insert(new_seq, new_seq);
405        selected_list->sort(false, true);
406        selected_list->update();
407
408        aw_root->awar(MP_AWAR_SELECTEDPROBES)->write_string(new_seq);
409        free(new_seq);
410    }
411    else {
412        aw_message(GBS_global_string("'%s' is no valid probe", seq));
413    }
414    free(seq);
415}
416
417void MP_cache_sonden(AW_window *) { new_pt_server = true; }
418void MP_cache_sonden2(AW_root *) { new_pt_server = true; }
419
420void MP_show_probes_in_tree_move(AW_window *aww, bool backward, AW_selection_list *resultProbesList) {
421    resultProbesList->move_selection(backward ? -1 : 1);
422    MP_show_probes_in_tree(aww);
423}
424
425void MP_show_probes_in_tree(AW_window */*aww*/) {
426    AWT_canvas *scr = mp_main->get_canvas();
427    char       *mism, *mism_temp;
428    char       *a_probe, *another_probe, *the_probe, *mism_temp2;
429    int         i, how_many_probes = 0;
430
431    {
432        char *sel = mp_main->get_aw_root()->awar(MP_AWAR_RESULTPROBES)->read_string();
433        a_probe   = MP_get_probes(sel); // haelt jetzt Sondenstring
434        if (! a_probe || ! a_probe[0]) {
435            free(a_probe);
436            free(sel);
437            return;
438        }
439
440        mism_temp2 = MP_get_comment(2, sel);
441        mism_temp  = mism_temp2;
442        free(sel);
443    }
444
445    char **probe_field = new char*[MAXMISMATCHES];
446    int   *mismatches  = new int[MAXMISMATCHES];
447
448    for (i=0; i<MAXMISMATCHES; i++)
449    {
450        probe_field[i] = NULL;
451        mismatches[i] = 0;
452    }
453
454    another_probe = a_probe;
455    for (i=0; i< MAXPROBECOMBIS; i++)       // generiert  probe_field und mismatches array
456    {
457        mism = strchr(mism_temp, ' ');
458        if (mism)
459            *(mism++) = 0;
460
461        mismatches[i] = atoi(mism_temp);
462        mism_temp = mism;
463
464
465        probe_field[i] = NULL;
466        the_probe = another_probe;
467        another_probe = strchr(another_probe, ' ');
468        if (another_probe)
469        {
470            *(another_probe++) = 0;
471            while (*another_probe == ' ' || *another_probe == '\t')
472                another_probe++;
473        }
474        else
475        {
476            probe_field[i] = ARB_strdup(the_probe);
477            how_many_probes ++;
478            break;
479        }
480
481        if (the_probe && the_probe[0])
482        {
483            probe_field[i] = ARB_strdup(the_probe);
484            how_many_probes ++;
485        }
486    }
487
488    free(a_probe);
489    free(mism_temp2);
490
491    if (new_pt_server)
492    {
493        new_pt_server = false;
494
495        if (mp_main->get_stc())
496            delete mp_main->get_stc();
497
498        mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE));
499        if (pt_server_different)
500        {
501            mp_main->set_stc(NULL);
502            new_pt_server = true;
503            aw_message("There are species in the tree which are\nnot included in the PT-Server");
504            pt_server_different = false;
505            return;
506        }
507    }
508
509    delete mp_main->get_stc()->sondentopf;
510    mp_main->get_stc()->sondentopf = new Sondentopf(mp_main->get_stc()->Bakterienliste, mp_main->get_stc()->Auswahlliste);
511
512    for (i=0; i<MAXMISMATCHES; i++) {
513        if (probe_field[i]) {
514            mp_main->get_stc()->sondentopf->put_Sonde(probe_field[i], mismatches[i], mismatches[i] + mp_gl_awars.outside_mismatches_difference);
515        }
516    }
517
518    mp_main->get_stc()->sondentopf->gen_color_hash(mp_gl_awars.no_of_probes);
519
520    GB_transaction ta(scr->gb_main);
521    AWT_TREE(scr)->get_root_node()->colorize(mp_main->get_stc()->sondentopf->get_color_hash());
522
523    if (scr->gb_main)
524        scr->gfx->update(scr->gb_main);
525
526    scr->refresh();
527
528    for (i=0; i<MAXMISMATCHES; i++)
529        free(probe_field[i]);
530
531    delete [] probe_field;
532    delete [] mismatches;
533}
534
535void MP_mark_probes_in_tree(AW_window *aww) {
536    AWT_canvas  *scr = mp_main->get_canvas();
537    char        *mism, *mism_temp;
538    char        *a_probe, *another_probe, *the_probe, *mism_temp2;
539    int          i, how_many_probes = 0;
540    GBDATA      *gb_species;
541
542    {
543        char *sel = mp_main->get_aw_root()->awar(MP_AWAR_RESULTPROBES)->read_string();
544        a_probe   = MP_get_probes(sel);             // haelt jetzt Sondenstring
545
546        if (! a_probe || ! a_probe[0]) {
547            free(a_probe);
548            free(sel);
549            return;
550        }
551
552        mism_temp2 = MP_get_comment(2, sel);
553        mism_temp  = mism_temp2;
554        free(sel);
555    }
556
557    char **probe_field = new char*[MAXMISMATCHES];
558    int   *mismatches  = new int[MAXMISMATCHES];
559
560    for (i=0; i<MAXMISMATCHES; i++)
561    {
562        probe_field[i] = NULL;
563        mismatches[i] = 0;
564    }
565
566    another_probe = a_probe;
567    for (i=0; i< MAXPROBECOMBIS; i++)       // generiert  probe_field und mismatches array
568    {
569        mism = strchr(mism_temp, ' ');
570        if (mism)
571            *(mism++) = 0;
572
573        mismatches[i] = atoi(mism_temp);
574        mism_temp = mism;
575
576        probe_field[i] = NULL;
577        the_probe = another_probe;
578        another_probe = strchr(another_probe, ' ');
579        if (another_probe) {
580            *(another_probe++) = 0;
581            while (*another_probe == ' ' || *another_probe == '\t')
582                another_probe++;
583        }
584        else {
585            probe_field[i] = ARB_strdup(the_probe);
586            how_many_probes ++;
587            break;
588        }
589
590        if (the_probe && the_probe[0]) {
591            probe_field[i] = ARB_strdup(the_probe);
592            how_many_probes ++;
593        }
594    }
595
596    free(a_probe);
597    free(mism_temp2);
598
599    if (new_pt_server) {
600        new_pt_server = false;
601
602        if (mp_main->get_stc())
603            delete mp_main->get_stc();
604
605        mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE));
606        if (pt_server_different)
607        {
608            mp_main->set_stc(NULL);
609            new_pt_server = true;
610            aw_message("There are species in the tree which are\nnot included in the PT-Server");
611            pt_server_different = false;
612            return;
613        }
614    }
615
616    delete mp_main->get_stc()->sondentopf;
617    mp_main->get_stc()->sondentopf = new Sondentopf(mp_main->get_stc()->Bakterienliste, mp_main->get_stc()->Auswahlliste);
618
619    for (i=0; i<MAXMISMATCHES; i++) {
620        if (probe_field[i]) {
621            mp_main->get_stc()->sondentopf->put_Sonde(probe_field[i], mismatches[i], mismatches[i] + mp_gl_awars.outside_mismatches_difference);
622        }
623    }
624    mp_main->get_stc()->sondentopf->gen_color_hash(mp_gl_awars.no_of_probes);
625
626    {
627        GB_push_transaction(scr->gb_main);
628        GB_HASH *col_hash = mp_main->get_stc()->sondentopf->get_color_hash();
629        for (gb_species = GBT_first_species(scr->gb_main); gb_species; gb_species = GBT_next_species(gb_species)) {
630            GB_write_flag(gb_species, GBS_read_hash(col_hash, GBT_read_name(gb_species)) > AWT_GC_BLACK);
631        }
632    }
633    GB_pop_transaction(scr->gb_main);
634
635    GB_transaction ta(scr->gb_main);
636
637    if (scr->gb_main)
638        scr->gfx->update(scr->gb_main);
639
640    scr->refresh();
641
642    for (i=0; i<MAXMISMATCHES; i++)
643        free(probe_field[i]);
644
645    delete [] probe_field;
646    delete [] mismatches;
647
648    MP_normal_colors_in_tree(aww);
649}
650
651void MP_Comment(AW_window *, const char *new_comment) {
652    // Comment fuer Auswahl eintragen
653
654    AW_root *awr       = mp_main->get_aw_root();
655    char    *selprobes = awr->awar(MP_AWAR_RESULTPROBES)->read_string();
656    char    *new_val   = MP_get_probes(selprobes);
657
658    if (new_val && new_val[0]) {
659        char *new_list_string;
660        {
661            char       *edited_comment = NULL;
662            const char *comment        = new_comment;
663
664            if (!comment) {
665                edited_comment = awr->awar(MP_AWAR_RESULTPROBESCOMMENT)->read_string();
666                for (int i = 0; edited_comment[i]; ++i) { // remove all '#' from new comment
667                    if (edited_comment[i] == SEPARATOR[0]) {
668                        edited_comment[i] = '|';
669                    }
670                }
671            }
672
673            char *misms = MP_get_comment(2, selprobes);
674            char *ecol  = MP_get_comment(3, selprobes);
675
676            new_list_string = GBS_global_string_copy("%-20s" SEPARATOR "%s" SEPARATOR "%s" SEPARATOR "%s",
677                                                     comment, misms, ecol, new_val);
678            free(ecol);
679            free(misms);
680            free(edited_comment);
681        }
682
683        {
684            bool autoadvance = awr->awar(MP_AWAR_AUTOADVANCE)->read_int();
685
686            if (autoadvance) result_probes_list->move_selection(1);
687
688            result_probes_list->delete_value(selprobes);
689            result_probes_list->insert(new_list_string, new_list_string);
690            result_probes_list->update();
691
692            if (!autoadvance) awr->awar(MP_AWAR_RESULTPROBES)->write_string(new_list_string);
693        }
694        free(new_list_string);
695    }
696
697    free(new_val);
698    free(selprobes);
699}
700
701void MP_selected_chosen(AW_root *aw_root) { // @@@ move to MP_Window.cxx
702    const char *selected = aw_root->awar(MP_AWAR_SELECTEDPROBES)->read_char_pntr();
703    if (selected && selected[0]) {
704        char *probe = MP_get_comment(3, selected);
705        if (probe) {
706            aw_root->awar(MP_AWAR_ECOLIPOS)->write_int(atoi(probe));
707            free(probe); // @@@ check/fix ressource handling of probe!
708
709            if (probe) {
710                probe = MP_get_comment(1, selected);
711                aw_root->awar(MP_AWAR_QUALITY)->write_int(atoi(probe));
712                free(probe);
713
714                if (probe) {
715                    probe = MP_get_comment(2, selected);
716                    aw_root->awar(MP_AWAR_SINGLEMISMATCHES)->write_int(atoi(probe));
717                    free(probe);
718
719                    if (probe) {
720                        probe = MP_get_probes(selected);
721                        aw_root->awar_string(MP_AWAR_SEQIN)->write_string(MP_get_probes(probe));
722                        free(probe);
723                        return;
724                    }
725                }
726            }
727        }
728        aw_message("can't parse entry");
729    }
730}
731
732void MP_group_all_except_marked(AW_window * /* aww */) {
733    AWT_canvas  *scr = mp_main->get_canvas();
734    NT_expand_marked_cb(0, scr);
735}
736
737void MP_normal_colors_in_tree(AW_window */*aww*/) {
738    AWT_canvas  *scr = mp_main->get_canvas();
739    GB_transaction ta(scr->gb_main);
740
741    AWT_graphic_tree *tree = AWT_TREE(scr);
742    if (tree) {
743        AP_tree *root = tree->get_root_node();
744        if (root) {
745            root->uncolorize();
746            if (scr->gb_main) scr->gfx->update(scr->gb_main);
747            scr->refresh();
748        }
749    }
750}
751
752void MP_delete_selected(UNFIXED, AW_selection_list *sellist) {
753    if (!sellist->default_is_selected()) {
754        int idx = sellist->get_index_of_selected();
755        sellist->delete_element_at(idx);
756        sellist->select_element_at(idx);
757        sellist->update();
758    }
759}
760
761
762char *MP_get_comment(int which, const char *str) {
763    // Parses information from strings like "val1#val2#val3#probes".
764    // 'which' == 1 -> 'val1'
765    // 'which' == 2 -> 'val2'
766    // ...
767    // Values may be present or not (i.e. 'probes', 'val1#probes', ... are also valid as 'str')
768
769    char       *result  = NULL;
770    const char *numsign = strchr(str, '#');
771
772    mp_assert(which >= 1);
773
774    if (numsign) {
775        if (which == 1) {
776            result = ARB_strpartdup(str, numsign-1);
777        }
778        else {
779            result = MP_get_comment(which-1, numsign+1);
780        }
781    }
782
783    return result;
784}
785
786void MP_result_combination_chosen(AW_root *aw_root) { // @@@ move to MP_Window.cxx
787    const char *str     = aw_root->awar(MP_AWAR_RESULTPROBES)->read_char_pntr();
788    char       *new_str = MP_get_comment(1, str);
789
790    aw_root->awar(MP_AWAR_RESULTPROBESCOMMENT)->write_string(new_str);
791
792    free(new_str);
793}
794
795
796
797//
798// functions concerning the server
799//
800
801int MP_init_local_com_struct()
802{
803    const char *user = GB_getenvUSER();
804
805    if (aisc_create(mp_pd_gl.link, PT_MAIN, mp_pd_gl.com,
806                    MAIN_LOCS, PT_LOCS, mp_pd_gl.locs,
807                    LOCS_USER, user,
808                    NULL)) {
809        return 1;
810    }
811    return 0;
812}
813
814const char *MP_probe_pt_look_for_server() {
815    // DRY vs  ../TOOLS/arb_probe.cxx@AP_probe_pt_look_for_server
816    // DRY vs  ../PROBE_DESIGN/probe_design.cxx@PD_probe_pt_look_for_server
817    const char *server_tag = GBS_ptserver_tag(mp_gl_awars.ptserver);
818    GB_ERROR    error      = arb_look_and_start_server(AISC_MAGIC_NUMBER, server_tag);
819
820    const char *result = NULL;
821    if (!error) {
822        result = GBS_read_arb_tcp(server_tag);
823        if (!result) error = GB_await_error();
824    }
825    if (error) aw_message(error);
826    return result;
827}
828
829// --------------------------------------------------------------------------------
830
831#ifdef UNIT_TESTS
832
833#include <test_unit.h>
834
835#define MP_GET_COMMENT_EQUAL(which, str, expect)  \
836    do {                                          \
837        char *got = MP_get_comment(which, str);   \
838        TEST_EXPECT_EQUAL(got, expect);           \
839        free(got);                                \
840    } while(0)                                    \
841       
842#define MP_GET_PROBES_EQUAL(str, expect)          \
843    do {                                          \
844        char *got = MP_get_probes(str);           \
845        TEST_EXPECT_EQUAL(got, expect);           \
846        free(got);                                \
847    } while(0)                                    \
848
849void TEST_MP_get_comment_and_probes() {
850    char *probes_only    = ARB_strdup("ACGT TGCA ATCG");
851    char *probes_comment = ARB_strdup("val1#val2#val3#ACGT TGCA ATCG");
852    char *probes_1 = ARB_strdup("one#     \t    ACGT TGCA ATCG");
853    char *probes_2 = ARB_strdup("one#two#\tACGT TGCA ATCG");
854
855    MP_GET_PROBES_EQUAL(probes_only,    "ACGT TGCA ATCG");
856    MP_GET_PROBES_EQUAL(probes_comment, "ACGT TGCA ATCG");
857    MP_GET_PROBES_EQUAL(probes_1,       "ACGT TGCA ATCG");
858    MP_GET_PROBES_EQUAL(probes_2,       "ACGT TGCA ATCG");
859   
860    MP_GET_COMMENT_EQUAL(1, probes_comment, "val1");
861    MP_GET_COMMENT_EQUAL(2, probes_comment, "val2");
862    MP_GET_COMMENT_EQUAL(3, probes_comment, "val3");
863
864    MP_GET_COMMENT_EQUAL(1, probes_only, (const char *)NULL);
865    MP_GET_COMMENT_EQUAL(2, probes_only, (const char *)NULL);
866    MP_GET_COMMENT_EQUAL(3, probes_only, (const char *)NULL);
867
868    MP_GET_COMMENT_EQUAL(1, probes_1, "one");
869    MP_GET_COMMENT_EQUAL(2, probes_1, (const char *)NULL);
870    MP_GET_COMMENT_EQUAL(3, probes_1, (const char *)NULL);
871   
872    MP_GET_COMMENT_EQUAL(1, probes_2, "one");
873    MP_GET_COMMENT_EQUAL(2, probes_2, "two");
874    MP_GET_COMMENT_EQUAL(3, probes_2, (const char *)NULL);
875
876    free(probes_2);
877    free(probes_1);
878    free(probes_comment);
879    free(probes_only);
880}
881
882#endif // UNIT_TESTS
Note: See TracBrowser for help on using the repository browser.