source: tags/arb-6.0/MULTI_PROBE/MP_noclass.cxx

Last change on this file was 12267, checked in by westram, 10 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
24#include <ctime>
25
26int  get_random(int min, int max); // gibt eine Zufallszahl x mit der Eigenschaft : min <= x <= max
27
28int        **system3_tab      = NULL;
29static int   system3_tab_size = 0;
30
31unsigned char **hamming_tab   = NULL;
32bool            new_pt_server = true;
33
34long k_aus_n(int k, int 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++) ;
40    return a / b;
41}
42
43int get_random(int min, int max) {
44    mp_assert(min <= max);
45    return GB_random(max-min+1)+min;
46}
47
48void MP_close_main(AW_window *aww)
49{
50    AWT_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)
61        scr->gfx->update(scr->gb_main);
62
63    scr->refresh();
64
65    AW_POPDOWN(aww);
66    delete mp_main->get_p_eval();
67    mp_main->set_p_eval(NULL);
68    delete mp_main->get_stc();
69    mp_main->set_stc(NULL);
70
71    new_pt_server = true;
72}
73
74static char *MP_get_probes(const char *str) {
75    const char *result = strrchr(str, '#');
76
77    if (!result) {
78        result = str;
79    }
80    else {
81        ++result;
82        result += strspn(result, " \t"); // skip over whitespace
83    }
84    return strdup(result);
85}
86
87void MP_gen_quality(AW_root *awr) {
88    bool firsttime = true; // @@@ was this static in the past?
89
90    if (firsttime)
91    {
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(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
123void 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    const char *ptr2;
131    while ((ptr2 = selentry.get_value())) {
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(strdup(temp));
145
146        delete probes;
147        delete com1;
148        delete com2;
149        delete com3;
150    }
151
152    selected_list->clear();
153
154    ptr2 = l->get_first();
155    while (ptr2)
156    {
157        l->remove_first();
158        selected_list->insert(ptr2, ptr2);
159        delete ptr2;
160        ptr2 = l->get_first();
161    }
162
163    selected_list->insert_default("", "");
164    selected_list->sort(false, true);
165    selected_list->update();
166    awr->awar(MP_AWAR_SELECTEDPROBES)->write_string("");
167
168    remembered_mismatches = mp_gl_awars.no_of_mismatches;
169    delete l;
170}
171
172void MP_gen_singleprobe(AW_root *awr) {
173    char *probe, *new_sing;
174    char *selected = awr->awar(MP_AWAR_SELECTEDPROBES)->read_string();
175
176    if (!selected || !selected[0])
177        return;
178
179    probe = MP_get_probes(selected);
180    selected_list->delete_value(selected);
181
182    new_sing = new char[5 + 7 + strlen(probe)];     // 5 = Zahl und Separator und Zahl und Separator und Nullzeichen
183    sprintf(new_sing, "%1ld#%1ld#%6ld#%s", mp_gl_awars.probe_quality, mp_gl_awars.singlemismatches, mp_gl_awars.ecolipos, probe);
184    delete probe;
185
186    selected_list->insert(new_sing, new_sing);
187    selected_list->insert_default("", "");
188    selected_list->sort(false, true);
189    selected_list->update();
190    awr->awar(MP_AWAR_SELECTEDPROBES)->write_string(new_sing);
191    delete new_sing;
192}
193
194static void init_system3_tab()
195{
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    {
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    {
218        for (k=0; k<3; k++)
219        {
220            if (!j) system3_tab[j][k] = k;
221            else    system3_tab[j][k] = system3_tab[j-1][k] * 3;
222        }
223    }
224
225
226    // ** hamming_tab
227        if (hamming_tab)
228            delete [] hamming_tab;
229
230        size_hamming_tab = (int)pow(3.0, (double) mp_gl_awars.no_of_probes);
231        hamming_tab = new unsigned char*[size_hamming_tab];
232        dummy_int = new int*[size_hamming_tab];
233
234        for (i=0; i<size_hamming_tab; i++)
235        {
236            hamming_tab[i] = new unsigned char[size_hamming_tab];
237            memset(hamming_tab[i], 0, sizeof(unsigned char) * size_hamming_tab);
238
239            dummy_int[i] = new int[mp_gl_awars.no_of_probes];
240            memset(dummy_int[i], 0, sizeof(int) * mp_gl_awars.no_of_probes);
241        }
242
243        counter = 1;
244        for (i=0; i< mp_gl_awars.no_of_probes; i++)
245        {
246            for (j=0; j<size_hamming_tab; j++)
247            {
248                for (wert = 0; wert < 3; wert++)
249                {
250                    for (k=0; k<counter; k++)
251                    {
252                        dummy_int[j++][i] = wert;
253                    }
254                }
255                j--;
256            }
257            counter *= 3;
258        }
259
260        for (i=0; i<size_hamming_tab; i++)
261        {
262            for (j=0; j<size_hamming_tab; j++)
263            {
264                hamm_dist = 0;
265                for (k=0; k<mp_gl_awars.no_of_probes; k++)
266                {
267                    if ((dummy_int[i][k] == 2 && dummy_int[j][k] == 0) ||
268                        (dummy_int[i][k] == 0 && dummy_int[j][k] == 2))
269                        hamm_dist++;
270                }
271                hamming_tab[i][j] = hamm_dist;
272            }
273        }
274
275        for (i=0; i<size_hamming_tab; i++)
276        {
277            delete [] dummy_int[i];
278        }
279        delete [] dummy_int;
280
281
282
283}
284
285void MP_popup_result_window(AW_window */*aww*/) {
286    mp_main->get_mp_window()->create_result_window(mp_main->get_aw_root())->activate();
287    init_system3_tab();
288}
289
290bool MP_aborted(int gen_cnt, double avg_fit, double min_fit, double max_fit, arb_progress& progress) {
291    char view_text[150];
292
293    if (gen_cnt == 0)
294        sprintf(view_text, "Evaluating first generation");
295    else
296        sprintf(view_text, "Gen:%d Avg:%5i Min:%5i Max:%5i", gen_cnt, int(avg_fit), int(min_fit), int(max_fit));
297
298    progress.subtitle(view_text);
299    return progress.aborted();
300}
301
302
303void MP_compute(AW_window *, AW_CL cl_gb_main) {
304    AW_root         *aw_root = mp_main->get_aw_root();
305    AW_window       *aww2;
306    int              i       = 0;
307    int             *bew_array;
308    char            *ptr, *qual;
309    char           **probe_field;
310    int             *single_mismatch;
311    ProbeValuation  *p_eval  = NULL;
312    GBDATA          *gb_main = (GBDATA*)cl_gb_main;
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, AW_CL cl_backward, AW_CL cl_result_probes_list) {
421    bool               backward           = bool(cl_backward);
422    AW_selection_list *resultProbesList = (AW_selection_list*)cl_result_probes_list;
423
424    resultProbesList->move_selection(backward ? -1 : 1);
425    MP_show_probes_in_tree(aww);
426}
427
428void MP_show_probes_in_tree(AW_window */*aww*/) {
429    AWT_canvas *scr = mp_main->get_canvas();
430    char       *mism, *mism_temp;
431    char       *a_probe, *another_probe, *the_probe, *mism_temp2;
432    int         i, how_many_probes = 0;
433
434    {
435        char *sel = mp_main->get_aw_root()->awar(MP_AWAR_RESULTPROBES)->read_string();
436        a_probe   = MP_get_probes(sel); // haelt jetzt Sondenstring
437        if (! a_probe || ! a_probe[0]) {
438            free(a_probe);
439            free(sel);
440            return;
441        }
442
443        mism_temp2 = MP_get_comment(2, sel);
444        mism_temp  = mism_temp2;
445        free(sel);
446    }
447
448    char **probe_field = new char*[MAXMISMATCHES];
449    int   *mismatches  = new int[MAXMISMATCHES];
450
451    for (i=0; i<MAXMISMATCHES; i++)
452    {
453        probe_field[i] = NULL;
454        mismatches[i] = 0;
455    }
456
457    another_probe = a_probe;
458    for (i=0; i< MAXPROBECOMBIS; i++)       // generiert  probe_field und mismatches array
459    {
460        mism = strchr(mism_temp, ' ');
461        if (mism)
462            *(mism++) = 0;
463
464        mismatches[i] = atoi(mism_temp);
465        mism_temp = mism;
466
467
468        probe_field[i] = NULL;
469        the_probe = another_probe;
470        another_probe = strchr(another_probe, ' ');
471        if (another_probe)
472        {
473            *(another_probe++) = 0;
474            while (*another_probe == ' ' || *another_probe == '\t')
475                another_probe++;
476        }
477        else
478        {
479            probe_field[i] = strdup(the_probe);
480            how_many_probes ++;
481            break;
482        }
483
484        if (the_probe && the_probe[0])
485        {
486            probe_field[i] = strdup(the_probe);
487            how_many_probes ++;
488        }
489    }
490
491    free(a_probe);
492    free(mism_temp2);
493
494    if (new_pt_server)
495    {
496        new_pt_server = false;
497
498        if (mp_main->get_stc())
499            delete mp_main->get_stc();
500
501        mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE));
502        if (pt_server_different)
503        {
504            mp_main->set_stc(NULL);
505            new_pt_server = true;
506            aw_message("There are species in the tree which are\nnot included in the PT-Server");
507            pt_server_different = false;
508            return;
509        }
510    }
511
512    delete mp_main->get_stc()->sondentopf;
513    mp_main->get_stc()->sondentopf = new Sondentopf(mp_main->get_stc()->Bakterienliste, mp_main->get_stc()->Auswahlliste);
514
515    for (i=0; i<MAXMISMATCHES; i++) {
516        if (probe_field[i]) {
517            mp_main->get_stc()->sondentopf->put_Sonde(probe_field[i], mismatches[i], mismatches[i] + mp_gl_awars.outside_mismatches_difference);
518        }
519    }
520
521    mp_main->get_stc()->sondentopf->gen_color_hash(mp_gl_awars.no_of_probes);
522
523    GB_transaction ta(scr->gb_main);
524    AWT_TREE(scr)->get_root_node()->colorize(mp_main->get_stc()->sondentopf->get_color_hash());
525
526    if (scr->gb_main)
527        scr->gfx->update(scr->gb_main);
528
529    scr->refresh();
530
531    for (i=0; i<MAXMISMATCHES; i++)
532        free(probe_field[i]);
533
534    delete [] probe_field;
535    delete [] mismatches;
536}
537
538void MP_mark_probes_in_tree(AW_window *aww) {
539    AWT_canvas  *scr = mp_main->get_canvas();
540    char        *mism, *mism_temp;
541    char        *a_probe, *another_probe, *the_probe, *mism_temp2;
542    int          i, how_many_probes = 0;
543    GBDATA      *gb_species;
544
545    {
546        char *sel = mp_main->get_aw_root()->awar(MP_AWAR_RESULTPROBES)->read_string();
547        a_probe   = MP_get_probes(sel);             // haelt jetzt Sondenstring
548
549        if (! a_probe || ! a_probe[0]) {
550            free(a_probe);
551            free(sel);
552            return;
553        }
554
555        mism_temp2 = MP_get_comment(2, sel);
556        mism_temp  = mism_temp2;
557        free(sel);
558    }
559
560    char **probe_field = new char*[MAXMISMATCHES];
561    int   *mismatches  = new int[MAXMISMATCHES];
562
563    for (i=0; i<MAXMISMATCHES; i++)
564    {
565        probe_field[i] = NULL;
566        mismatches[i] = 0;
567    }
568
569    another_probe = a_probe;
570    for (i=0; i< MAXPROBECOMBIS; i++)       // generiert  probe_field und mismatches array
571    {
572        mism = strchr(mism_temp, ' ');
573        if (mism)
574            *(mism++) = 0;
575
576        mismatches[i] = atoi(mism_temp);
577        mism_temp = mism;
578
579        probe_field[i] = NULL;
580        the_probe = another_probe;
581        another_probe = strchr(another_probe, ' ');
582        if (another_probe) {
583            *(another_probe++) = 0;
584            while (*another_probe == ' ' || *another_probe == '\t')
585                another_probe++;
586        }
587        else {
588            probe_field[i] = strdup(the_probe);
589            how_many_probes ++;
590            break;
591        }
592
593        if (the_probe && the_probe[0]) {
594            probe_field[i] = strdup(the_probe);
595            how_many_probes ++;
596        }
597    }
598
599    free(a_probe);
600    free(mism_temp2);
601
602    if (new_pt_server) {
603        new_pt_server = false;
604
605        if (mp_main->get_stc())
606            delete mp_main->get_stc();
607
608        mp_main->set_stc(new ST_Container(MAXSONDENHASHSIZE));
609        if (pt_server_different)
610        {
611            mp_main->set_stc(NULL);
612            new_pt_server = true;
613            aw_message("There are species in the tree which are\nnot included in the PT-Server");
614            pt_server_different = false;
615            return;
616        }
617    }
618
619    delete mp_main->get_stc()->sondentopf;
620    mp_main->get_stc()->sondentopf = new Sondentopf(mp_main->get_stc()->Bakterienliste, mp_main->get_stc()->Auswahlliste);
621
622    for (i=0; i<MAXMISMATCHES; i++) {
623        if (probe_field[i]) {
624            mp_main->get_stc()->sondentopf->put_Sonde(probe_field[i], mismatches[i], mismatches[i] + mp_gl_awars.outside_mismatches_difference);
625        }
626    }
627    mp_main->get_stc()->sondentopf->gen_color_hash(mp_gl_awars.no_of_probes);
628
629    {
630        GB_push_transaction(scr->gb_main);
631        GB_HASH *col_hash = mp_main->get_stc()->sondentopf->get_color_hash();
632        for (gb_species = GBT_first_species(scr->gb_main); gb_species; gb_species = GBT_next_species(gb_species)) {
633            GB_write_flag(gb_species, GBS_read_hash(col_hash, GBT_read_name(gb_species)) > AWT_GC_BLACK);
634        }
635    }
636    GB_pop_transaction(scr->gb_main);
637
638    GB_transaction ta(scr->gb_main);
639
640    if (scr->gb_main)
641        scr->gfx->update(scr->gb_main);
642
643    scr->refresh();
644
645    for (i=0; i<MAXMISMATCHES; i++)
646        free(probe_field[i]);
647
648    delete [] probe_field;
649    delete [] mismatches;
650
651    MP_normal_colors_in_tree(aww);
652}
653
654void MP_Comment(AW_window *, AW_CL cl_com) {
655    // Comment fuer Auswahl eintragen
656
657    AW_root *awr       = mp_main->get_aw_root();
658    char    *selprobes = awr->awar(MP_AWAR_RESULTPROBES)->read_string();
659    char    *new_val   = MP_get_probes(selprobes);
660
661    if (new_val && new_val[0]) {
662        char *new_list_string;
663        {
664            char       *edited_comment = NULL;
665            const char *comment        = NULL;
666
667            if (cl_com) {
668                comment = (char*)cl_com;
669            }
670            else {
671                edited_comment = awr->awar(MP_AWAR_RESULTPROBESCOMMENT)->read_string();
672                for (int i = 0; edited_comment[i]; ++i) { // remove all '#' from new comment
673                    if (edited_comment[i] == SEPARATOR[0]) {
674                        edited_comment[i] = '|';
675                    }
676                }
677            }
678
679            char *misms = MP_get_comment(2, selprobes);
680            char *ecol  = MP_get_comment(3, selprobes);
681
682            new_list_string = GBS_global_string_copy("%-20s" SEPARATOR "%s" SEPARATOR "%s" SEPARATOR "%s",
683                                                     comment, misms, ecol, new_val);
684            free(ecol);
685            free(misms);
686            free(edited_comment);
687        }
688
689        {
690            bool autoadvance = awr->awar(MP_AWAR_AUTOADVANCE)->read_int();
691
692            if (autoadvance) result_probes_list->move_selection(1);
693
694            result_probes_list->delete_value(selprobes);
695            result_probes_list->insert(new_list_string, new_list_string);
696            result_probes_list->update();
697
698            if (!autoadvance) awr->awar(MP_AWAR_RESULTPROBES)->write_string(new_list_string);
699        }
700        free(new_list_string);
701    }
702
703    free(new_val);
704    free(selprobes);
705}
706
707void MP_selected_chosen(AW_window */*aww*/) {
708    AW_root *aw_root  = mp_main->get_aw_root();
709    char    *selected = aw_root->awar(MP_AWAR_SELECTEDPROBES)->read_string();
710    if (selected && selected[0]) {
711        char *probe = MP_get_comment(3, selected);
712        if (probe) {
713            aw_root->awar(MP_AWAR_ECOLIPOS)->write_int(atoi(probe));
714            free(probe);
715
716            if (probe) {
717                probe = MP_get_comment(1, selected);
718                aw_root->awar(MP_AWAR_QUALITY)->write_int(atoi(probe));
719                free(probe);
720
721                if (probe) {
722                    probe = MP_get_comment(2, selected);
723                    aw_root->awar(MP_AWAR_SINGLEMISMATCHES)->write_int(atoi(probe));
724                    free(probe);
725
726                    if (probe) {
727                        probe = MP_get_probes(selected);
728                        aw_root->awar_string(MP_AWAR_SEQIN)->write_string(MP_get_probes(probe));
729                        free(probe);
730                        return;
731                    }
732                }
733            }
734        }
735        aw_message("can't parse entry");
736    }
737}
738
739void MP_group_all_except_marked(AW_window * /* aww */) {
740    AWT_canvas  *scr = mp_main->get_canvas();
741    NT_group_not_marked_cb(0, scr);
742}
743
744void MP_normal_colors_in_tree(AW_window */*aww*/) {
745    AWT_canvas  *scr = mp_main->get_canvas();
746    GB_transaction ta(scr->gb_main);
747
748    AWT_graphic_tree *tree = AWT_TREE(scr);
749    if (tree) {
750        AP_tree *root = tree->get_root_node();
751        if (root) {
752            root->uncolorize();
753            if (scr->gb_main) scr->gfx->update(scr->gb_main);
754            scr->refresh();
755        }
756    }
757}
758
759void MP_delete_selected(UNFIXED, AW_selection_list *sellist) {
760    if (!sellist->default_is_selected()) {
761        int idx = sellist->get_index_of_selected();
762        sellist->delete_element_at(idx);
763        sellist->select_element_at(idx);
764        sellist->update();
765    }
766}
767
768
769char *MP_get_comment(int which, const char *str) {
770    // Parses information from strings like "val1#val2#val3#probes".
771    // 'which' == 1 -> 'val1'
772    // 'which' == 2 -> 'val2'
773    // ...
774    // Values may be present or not (i.e. 'probes', 'val1#probes', ... are also valid as 'str')
775
776    char       *result  = NULL;
777    const char *numsign = strchr(str, '#');
778
779    mp_assert(which >= 1);
780
781    if (numsign) {
782        if (which == 1) {
783            result = GB_strpartdup(str, numsign-1);
784        }
785        else {
786            result = MP_get_comment(which-1, numsign+1);
787        }
788    }
789
790    return result;
791}
792
793void MP_result_chosen(AW_window */*aww*/) {
794    AW_root *aw_root = mp_main->get_aw_root();
795    char    *str     = aw_root->awar(MP_AWAR_RESULTPROBES)->read_as_string();
796    char    *new_str;
797
798    new_str = MP_get_comment(1, str);
799
800    aw_root->awar(MP_AWAR_RESULTPROBESCOMMENT)->write_string(new_str);
801    free(str);
802    free(new_str);
803}
804
805
806
807//
808// functions concerning the server
809//
810
811int MP_init_local_com_struct()
812{
813    const char *user = GB_getenvUSER();
814
815    if (aisc_create(mp_pd_gl.link, PT_MAIN, mp_pd_gl.com,
816                    MAIN_LOCS, PT_LOCS, mp_pd_gl.locs,
817                    LOCS_USER, user,
818                    NULL)) {
819        return 1;
820    }
821    return 0;
822}
823
824const char *MP_probe_pt_look_for_server() {
825    const char *server_tag = GBS_ptserver_tag(mp_gl_awars.ptserver);
826    GB_ERROR    error      = arb_look_and_start_server(AISC_MAGIC_NUMBER, server_tag);
827
828    if (error) {
829        aw_message(error);
830        return 0;
831    }
832    return GBS_read_arb_tcp(server_tag);
833}
834
835// --------------------------------------------------------------------------------
836
837#ifdef UNIT_TESTS
838
839#include <test_unit.h>
840
841#define MP_GET_COMMENT_EQUAL(which, str, expect)  \
842    do {                                          \
843        char *got = MP_get_comment(which, str);   \
844        TEST_EXPECT_EQUAL(got, expect);           \
845        free(got);                                \
846    } while(0)                                    \
847       
848#define MP_GET_PROBES_EQUAL(str, expect)          \
849    do {                                          \
850        char *got = MP_get_probes(str);           \
851        TEST_EXPECT_EQUAL(got, expect);           \
852        free(got);                                \
853    } while(0)                                    \
854
855void TEST_MP_get_comment_and_probes() {
856    char *probes_only    = strdup("ACGT TGCA ATCG");
857    char *probes_comment = strdup("val1#val2#val3#ACGT TGCA ATCG");
858    char *probes_1 = strdup("one#     \t    ACGT TGCA ATCG");
859    char *probes_2 = strdup("one#two#\tACGT TGCA ATCG");
860
861    MP_GET_PROBES_EQUAL(probes_only,    "ACGT TGCA ATCG");
862    MP_GET_PROBES_EQUAL(probes_comment, "ACGT TGCA ATCG");
863    MP_GET_PROBES_EQUAL(probes_1,       "ACGT TGCA ATCG");
864    MP_GET_PROBES_EQUAL(probes_2,       "ACGT TGCA ATCG");
865   
866    MP_GET_COMMENT_EQUAL(1, probes_comment, "val1");
867    MP_GET_COMMENT_EQUAL(2, probes_comment, "val2");
868    MP_GET_COMMENT_EQUAL(3, probes_comment, "val3");
869
870    MP_GET_COMMENT_EQUAL(1, probes_only, (const char *)NULL);
871    MP_GET_COMMENT_EQUAL(2, probes_only, (const char *)NULL);
872    MP_GET_COMMENT_EQUAL(3, probes_only, (const char *)NULL);
873
874    MP_GET_COMMENT_EQUAL(1, probes_1, "one");
875    MP_GET_COMMENT_EQUAL(2, probes_1, (const char *)NULL);
876    MP_GET_COMMENT_EQUAL(3, probes_1, (const char *)NULL);
877   
878    MP_GET_COMMENT_EQUAL(1, probes_2, "one");
879    MP_GET_COMMENT_EQUAL(2, probes_2, "two");
880    MP_GET_COMMENT_EQUAL(3, probes_2, (const char *)NULL);
881
882    free(probes_2);
883    free(probes_1);
884    free(probes_comment);
885    free(probes_only);
886}
887
888#endif // UNIT_TESTS
Note: See TracBrowser for help on using the repository browser.