source: tags/ms_r16q3/PROBE_DESIGN/probe_design.cxx

Last change on this file was 15306, checked in by westram, 8 years ago
  • derive TREE_canvas from AWT_canvas
    • move awar_treeTREE_canvas
    • use TREE_canvas where required
    • remove dummy-param from AWT_canvas-ctor
  • rename NT_canvas_registryTREE_canvas_registry
  • remove unused param from AP_user_push_cb
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 110.2 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : probe_design.cxx                                  //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "SaiProbeVisualization.hxx"
12#include "probe_match_parser.hxx"
13
14#include <PT_com.h>
15#include <PT_server.h> // needed for DOMAIN_MIN_LENGTH
16#include <client.h>
17#include <servercntrl.h>
18#include <probe_gui.hxx>
19#include <probe_local.hxx>
20
21#include <GEN.hxx>
22#include <TreeCallbacks.hxx>
23
24#include <iupac.h>
25#include <awt_config_manager.hxx>
26#include <awt_sel_boxes.hxx>
27#include <awt_misc.hxx>
28
29#include <aw_awars.hxx>
30#include <aw_preset.hxx>
31#include <aw_select.hxx>
32#include <aw_msg.hxx>
33#include <aw_root.hxx>
34#include <aw_question.hxx>
35#include <aw_edit.hxx>
36#include <rootAsWin.h>
37
38#include <adGene.h>
39
40#include <arb_progress.h>
41#include <arb_strbuf.h>
42#include <arb_strarray.h>
43#include <arb_file.h>
44#include <arb_misc.h>
45
46#include "probe_collection.hxx"
47
48// general awars
49
50#define AWAR_PROBE_CREATE_GENE_SERVER "tmp/probe_admin/gene_server" // whether to create a gene pt server
51
52#define AWAR_ITARGET_STRING       "nt/itarget_string"
53
54// probe match awars
55
56// #define AWAR_PD_MATCH_ITEM     AWAR_SPECIES_NAME
57#define AWAR_PD_SELECTED_MATCH "tmp/probe_design/match"
58#define AWAR_PD_MATCH_RESOLVE  "tmp/probe_design/match_resolve" // for IUPAC resolution
59
60#define AWAR_PD_MATCH_SORTBY     "probe_match/sort_by" // weighted mismatches
61#define AWAR_PD_MATCH_COMPLEMENT "probe_match/complement" // check reverse complement too
62#define AWAR_PD_MATCH_MARKHITS   "probe_match/mark_hits" // mark hitten species in database
63#define AWAR_PD_MATCH_WRITE2TMP  "probe_match/write_2_tmp" // write result to field tmp
64#define AWAR_PD_MATCH_AUTOMATCH  "probe_match/auto_match" // auto match probes when target string changes
65
66#define AWAR_PD_MATCH_NHITS    "tmp/probe_match/nhits" // display the 'number of hits'
67
68// probe design awars
69
70#define AWAR_PD_DESIGN_CLIPRESULT "probe_design/CLIPRESULT" // 'length of output' (how many probes will get designed)
71#define AWAR_PD_DESIGN_MISHIT     "probe_design/MISHIT"     // 'non group hits'
72#define AWAR_PD_DESIGN_MAXBOND    "probe_design/MAXBOND"    // max hairpinbonds ?
73#define AWAR_PD_DESIGN_MINTARGETS "probe_design/MINTARGETS" // 'min. group hits (%)'
74
75#define AWAR_PD_DESIGN_MIN_LENGTH "probe_design/PROBELENGTH"      // min. length of probe
76#define AWAR_PD_DESIGN_MAX_LENGTH "probe_design/PROBEMAXLENGTH"   // max. length of probe (or empty)
77
78#define AWAR_PD_DESIGN_MIN_TEMP     "probe_design/MINTEMP"     // temperature (min)
79#define AWAR_PD_DESIGN_MAX_TEMP     "probe_design/MAXTEMP"     // temperature (max)
80#define AWAR_PD_DESIGN_MIN_GC       "probe_design/MINGC"       // GC content (min)
81#define AWAR_PD_DESIGN_MAX_GC       "probe_design/MAXGC"       // GC content (max)
82#define AWAR_PD_DESIGN_MIN_ECOLIPOS "probe_design/MINECOLI"    // ecolipos (min)
83#define AWAR_PD_DESIGN_MAX_ECOLIPOS "probe_design/MAXECOLI"    // ecolipos (max)
84
85#define AWAR_PD_DESIGN_GENE "probe_design/gene" // generate probes for genes ?
86
87// probe design/match (expert window)
88#define AWAR_PD_COMMON_EXP_BONDS_FORMAT "probe_design/bonds/pos%i"  // format to generate bond awar names
89#define AWAR_PD_COMMON_EXP_SPLIT        "probe_design/SPLIT"
90
91#define AWAR_PD_DESIGN_EXP_DTEDGE "probe_design/DTEDGE"
92#define AWAR_PD_DESIGN_EXP_DT     "probe_design/DT"
93
94#define AWAR_PD_MATCH_NMATCHES   "probe_match/nmatches"
95#define AWAR_PD_MATCH_LIM_NMATCH "probe_match/lim_nmatch"
96#define AWAR_PD_MATCH_MAX_RES    "probe_match/maxres"
97
98// --------------------------------
99// probe collection awars
100
101// probe collection window
102#define AWAR_PC_TARGET_STRING      "probe_collection/target_string"
103#define AWAR_PC_TARGET_NAME        "probe_collection/target_name"
104#define AWAR_PC_MATCH_WEIGHTS      "probe_collection/match_weights/pos"
105#define AWAR_PC_MATCH_WIDTH        "probe_collection/match_weights/width"
106#define AWAR_PC_MATCH_BIAS         "probe_collection/match_weights/bias"
107#define AWAR_PC_AUTO_MATCH         "probe_collection/auto"
108#define AWAR_PC_CURRENT_COLLECTION "probe_collection/current"
109
110#define AWAR_PC_SELECTED_PROBE     "tmp/probe_collection/probe"
111#define AWAR_PC_MATCH_NHITS        "tmp/probe_collection/nhits"
112
113// probe collection matching control parameters
114#define AWAR_PC_MISMATCH_THRESHOLD "probe_collection/mismatch_threshold"
115
116#define REPLACE_TARGET_CONTROL_CHARS ":#=_:\\:=_"
117
118// ----------------------------------------
119
120static saiProbeData *g_spd = 0;
121
122static struct {
123    aisc_com          *link;
124    T_PT_LOCS          locs;
125    T_PT_MAIN          com;
126    AW_window_simple  *win;
127    AW_selection_list *resultList; // @@@ replace by TypedSelectionList?
128} PD;
129
130struct ProbeMatchEventParam : virtual Noncopyable {
131    GBDATA            *gb_main;
132    AW_selection_list *selection_id;                // may be NULL
133    int               *counter;                     // may be NULL (if != NULL -> afterwards contains number of hits)
134
135    // results set by probe_match_event:
136    std::string hits_summary; // shown in probe match window
137    bool        refresh_sai_display;
138
139    void init_results() {
140        refresh_sai_display = false;
141    }
142
143    ProbeMatchEventParam(GBDATA *gb_main_, int *counter_)         : gb_main(gb_main_), selection_id(NULL), counter(counter_) { init_results(); }
144    ProbeMatchEventParam(GBDATA *gb_main_, AW_selection_list *id) : gb_main(gb_main_), selection_id(id),   counter(NULL)     { init_results(); }
145};
146
147struct AutoMatchSettings {
148    ProbeMatchEventParam *event_param; // not owned!
149    bool                  disable;
150
151    AutoMatchSettings(ProbeMatchEventParam *event_param_, bool disable_)
152        : event_param(event_param_), disable(disable_)
153    {}
154    AutoMatchSettings()
155        : event_param(NULL) , disable(true)
156    {}
157
158    bool initialized() const { return event_param != NULL; }
159};
160
161static AutoMatchSettings auto_match_cb_settings;
162
163// prototypes:
164static void probe_match_event_using_awars(AW_root *root, ProbeMatchEventParam *event_param);
165
166static void auto_match_cb(AW_root *root) {
167    if (!auto_match_cb_settings.disable) {
168        char *ts = root->awar(AWAR_TARGET_STRING)->read_string();
169        if (strlen(ts) > 0) {
170            probe_match_event_using_awars(root, auto_match_cb_settings.event_param);
171        }
172        free(ts);
173    }
174}
175
176static const char *auto_match_sensitive_awars[] = {
177    AWAR_TARGET_STRING,
178    AWAR_PT_SERVER,
179    AWAR_PD_MATCH_COMPLEMENT,
180    AWAR_MAX_MISMATCHES,
181    AWAR_PD_MATCH_SORTBY,
182    0
183};
184
185static void auto_match_changed(AW_root *root) {
186    static bool callback_active = false;
187    int         autoMatch       = root->awar(AWAR_PD_MATCH_AUTOMATCH)->read_int();
188
189    if (autoMatch) {
190        if (!callback_active) {
191            for (int i = 0; auto_match_sensitive_awars[i]; ++i) {
192                root->awar(auto_match_sensitive_awars[i])->add_callback(auto_match_cb);
193            }
194        }
195    }
196    else {
197        if (callback_active) {
198            for (int i = 0; auto_match_sensitive_awars[i]; ++i) {
199                root->awar(auto_match_sensitive_awars[i])->remove_callback(auto_match_cb);
200            }
201        }
202    }
203    callback_active = bool(autoMatch);
204}
205
206static void enable_auto_match_cb(AW_root *root, ProbeMatchEventParam *event_param) {
207    if (event_param == NULL && auto_match_cb_settings.initialized()) {
208        // "partially" enable (w/o ProbeMatchEventParam) is only done
209        // if not already "fully enabled"
210        return;
211    }
212
213    auto_match_cb_settings = AutoMatchSettings(event_param, false);
214    auto_match_changed(root);
215}
216
217static void popup_match_window_cb(AW_window *aww, GBDATA *gb_main) { // @@@ shall be called by auto_match_cb (if never opened b4)
218    AW_root   *root         = aww->get_root();
219    AW_window *match_window = create_probe_match_window(root, gb_main);
220    match_window->activate();
221    root->awar(AWAR_TARGET_STRING)->touch(); // force re-match
222}
223
224// --------------------------------------------------------------------------------
225
226static void popup_probe_design_result_window(AW_window *aww, GBDATA *gb_main) {
227    if (!PD.win) {
228        AW_root *root = aww->get_root();
229
230        PD.win = new AW_window_simple;
231        PD.win->init(root, "PD_RESULT", "PD RESULT");
232        PD.win->load_xfig("pd_reslt.fig");
233
234        PD.win->button_length(6);
235        PD.win->auto_space(10, 10);
236
237        PD.win->at("help");
238        PD.win->callback(makeHelpCallback("probedesignresult.hlp"));
239        PD.win->create_button("HELP", "HELP", "H");
240
241        PD.win->at("result");
242        PD.resultList = PD.win->create_selection_list(AWAR_TARGET_STRING, 40, 5, false);
243        const StorableSelectionList *storable_result_list = new StorableSelectionList(TypedSelectionList("prb", PD.resultList, "designed probes", "designed")); // @@@ make member of PD ?
244
245        PD.resultList->clear();
246        PD.resultList->insert_default("No probes designed yet", "");
247
248        PD.win->at("buttons");
249
250        PD.win->callback(AW_POPDOWN);
251        PD.win->create_button("CLOSE", "CLOSE", "C");
252
253        PD.win->callback(makeWindowCallback(awt_clear_selection_list_cb, PD.resultList));
254        PD.win->create_button("CLEAR", "CLEAR", "R");
255
256        PD.win->callback(makeCreateWindowCallback(create_load_box_for_selection_lists, storable_result_list));
257        PD.win->create_button("LOAD", "LOAD", "L");
258
259        PD.win->callback(makeCreateWindowCallback(create_save_box_for_selection_lists, storable_result_list));
260        PD.win->create_button("SAVE", "SAVE", "S");
261
262        PD.win->callback(makeWindowCallback(create_print_box_for_selection_lists, &storable_result_list->get_typedsellist()));
263        PD.win->create_button("PRINT", "PRINT", "P");
264
265        PD.win->callback(makeWindowCallback(popup_match_window_cb, gb_main));
266        PD.win->create_button("MATCH", "MATCH", "M");
267
268        PD.win->label("Auto match");
269        PD.win->create_toggle(AWAR_PD_MATCH_AUTOMATCH);
270
271        enable_auto_match_cb(root, NULL);
272    }
273    PD.win->activate();
274}
275
276static int init_local_com_struct() {
277    const char *user = GB_getenvUSER();
278
279    if (aisc_create(PD.link, PT_MAIN, PD.com,
280                    MAIN_LOCS, PT_LOCS, PD.locs,
281                    LOCS_USER, user,
282                    NULL)) {
283        return 1;
284    }
285    return 0;
286}
287
288static const char *PD_probe_pt_look_for_server(int serverNumber, GB_ERROR& error) {
289    // return PT server info string (see GBS_read_arb_tcp for details)
290    // or NULL (in this case 'error' is set)
291
292    // DRY vs  ../TOOLS/arb_probe.cxx@AP_probe_pt_look_for_server
293    // DRY vs  ../MULTI_PROBE/MP_noclass.cxx@MP_probe_pt_look_for_server
294
295    const char *result     = 0;
296    const char *server_tag = GBS_ptserver_tag(serverNumber);
297
298    error = arb_look_and_start_server(AISC_MAGIC_NUMBER, server_tag);
299    if (!error) {
300        result             = GBS_read_arb_tcp(server_tag);
301        if (!result) error = GB_await_error();
302    }
303    pd_assert(contradicted(result, error));
304    return result;
305}
306
307static GB_ERROR species_requires(GBDATA *gb_species, const char *whats_required) {
308    return GBS_global_string("Species '%s' needs %s", GBT_read_name(gb_species), whats_required);
309}
310
311static GB_ERROR gene_requires(GBDATA *gb_gene, const char *whats_required) {
312    GBDATA *gb_species = GB_get_grandfather(gb_gene);
313    pd_assert(gb_species);
314    return GBS_global_string("Gene '%s' of organism '%s' needs %s", GBT_read_name(gb_gene), GBT_read_name(gb_species), whats_required);
315}
316
317static GB_ERROR pd_get_the_names(GBDATA *gb_main, bytestring &bs, bytestring &checksum) {
318    GBS_strstruct *names     = GBS_stropen(1024);
319    GBS_strstruct *checksums = GBS_stropen(1024);
320    GB_ERROR       error     = 0;
321
322    GB_begin_transaction(gb_main);
323
324    char *use = GBT_get_default_alignment(gb_main);
325
326    for (GBDATA *gb_species = GBT_first_marked_species(gb_main); gb_species; gb_species = GBT_next_marked_species(gb_species)) {
327        GBDATA *gb_name = GB_entry(gb_species, "name");
328        if (!gb_name) { error = species_requires(gb_species, "name"); break; }
329
330        GBDATA *gb_data = GBT_find_sequence(gb_species, use);
331        if (!gb_data) { error = species_requires(gb_species, GBS_global_string("data in '%s'", use)); break; }
332
333        GBS_intcat(checksums, GBS_checksum(GB_read_char_pntr(gb_data), 1, ".-"));
334        GBS_strcat(names, GB_read_char_pntr(gb_name));
335        GBS_chrcat(checksums, '#');
336        GBS_chrcat(names, '#');
337    }
338
339    GBS_str_cut_tail(names, 1); // remove trailing '#'
340    GBS_str_cut_tail(checksums, 1); // remove trailing '#'
341
342    free(use);
343
344    bs.size = GBS_memoffset(names)+1;
345    bs.data = GBS_strclose(names);
346
347    checksum.size = GBS_memoffset(checksums)+1;
348    checksum.data = GBS_strclose(checksums);
349
350    GB_commit_transaction(gb_main);
351
352    return error;
353}
354
355static GB_ERROR pd_get_the_gene_names(GBDATA *gb_main, bytestring &bs, bytestring &checksum) {
356    GBS_strstruct *names     = GBS_stropen(1024);
357    GBS_strstruct *checksums = GBS_stropen(1024);
358    GB_ERROR       error     = 0;
359
360    GB_begin_transaction(gb_main);
361    const char *use = GENOM_ALIGNMENT; // gene pt server is always build on 'ali_genom'
362
363    for (GBDATA *gb_species = GEN_first_organism(gb_main); gb_species && !error; gb_species = GEN_next_organism(gb_species)) {
364        const char *species_name = 0;
365        {
366            GBDATA *gb_data = GBT_find_sequence(gb_species, use);
367            if (!gb_data) { error = species_requires(gb_species, GBS_global_string("data in '%s'", use)); break; }
368
369            GBDATA *gb_name = GB_search(gb_species, "name", GB_FIND);
370            if (!gb_name) { error = species_requires(gb_species, "name"); break; }
371            species_name = GB_read_char_pntr(gb_name);
372        }
373
374        for (GBDATA *gb_gene = GEN_first_marked_gene(gb_species); gb_gene && !error; gb_gene = GEN_next_marked_gene(gb_gene)) {
375            const char *gene_name = 0;
376            {
377                GBDATA *gb_gene_name = GB_entry(gb_gene, "name");
378                if (!gb_gene_name) { error = gene_requires(gb_gene, "name"); break; }
379                gene_name = GB_read_char_pntr(gb_gene_name);
380            }
381
382            {
383                char *gene_seq       = GBT_read_gene_sequence(gb_gene, false, 0);
384                if (!gene_seq) error = GB_await_error();
385                else {
386                    long        CheckSum = GBS_checksum(gene_seq, 1, ".-");
387                    const char *id       = GBS_global_string("%s/%s", species_name, gene_name);
388
389                    GBS_intcat(checksums, CheckSum);
390                    GBS_strcat(names, id);
391                    GBS_chrcat(checksums, '#');
392                    GBS_chrcat(names, '#');
393
394                    free(gene_seq);
395                }
396            }
397        }
398    }
399
400    GBS_str_cut_tail(names, 1); // remove trailing '#'
401    GBS_str_cut_tail(checksums, 1); // remove trailing '#'
402
403    if (error) {
404        GBS_strforget(names);
405        GBS_strforget(checksums);
406    }
407    else {
408        bs.size = GBS_memoffset(names)+1;
409        bs.data = GBS_strclose(names);
410
411        checksum.size = GBS_memoffset(checksums)+1;
412        checksum.data = GBS_strclose(checksums);
413    }
414    error = GB_end_transaction(gb_main, error);
415    return error;
416}
417
418inline const char *bond_awar_name(int i) {
419    return GBS_global_string(AWAR_PD_COMMON_EXP_BONDS_FORMAT, i);
420}
421
422struct ProbeCommonSettings {
423    float split;
424    float bonds[16];
425
426    explicit ProbeCommonSettings(AW_root *root)
427        : split(root->awar(AWAR_PD_COMMON_EXP_SPLIT)->read_float())
428    {
429        for (int i=0; i<16; i++) {
430            bonds[i] = root->awar(bond_awar_name(i))->read_float();
431        }
432    }
433};
434
435static int probe_common_send_data(const ProbeCommonSettings& commonSettings) {
436    // send data common to probe-design AND -match
437    if (aisc_put(PD.link, PT_LOCS, PD.locs,
438                 LOCS_SPLIT, (double)commonSettings.split,
439                 NULL))
440        return 1;
441
442    for (int i=0; i<16; i++) {
443        if (aisc_put(PD.link, PT_LOCS, PD.locs,
444                     PT_INDEX,     (long)i,
445                     LOCS_BONDVAL, (double)commonSettings.bonds[i],
446                     NULL))
447            return 1;
448    }
449    return 0;
450}
451static int probe_design_send_data(AW_root *root, const T_PT_PDC& pdc) {
452    if (aisc_put(PD.link, PT_PDC, pdc,
453                 PDC_DTEDGE,     (double)root->awar(AWAR_PD_DESIGN_EXP_DTEDGE)->read_float()*100.0,
454                 PDC_DT,         (double)root->awar(AWAR_PD_DESIGN_EXP_DT)->read_float()*100.0,
455                 PDC_CLIPRESULT, (long)root->awar(AWAR_PD_DESIGN_CLIPRESULT)->read_int(),
456                 NULL))
457        return 1;
458
459    return probe_common_send_data(ProbeCommonSettings(root));
460}
461
462struct ProbeMatchSettings : public ProbeCommonSettings {
463    int serverNumber;
464    int nmatches;
465    int nlimit;
466    int maxhits;
467    int complement;
468    int sortBy;
469    int maxMismatches;
470    int markHits;
471    int write2tmp;
472
473    std::string targetString;
474
475    explicit ProbeMatchSettings(AW_root *root)
476        : ProbeCommonSettings(root),
477          serverNumber(root->awar(AWAR_PT_SERVER)->read_int()),
478          nmatches(root->awar(AWAR_PD_MATCH_NMATCHES)->read_int()),
479          nlimit(root->awar(AWAR_PD_MATCH_LIM_NMATCH)->read_int()),
480          maxhits(root->awar(AWAR_PD_MATCH_MAX_RES)->read_int()),
481          complement(root->awar(AWAR_PD_MATCH_COMPLEMENT)->read_int()),
482          sortBy(root->awar(AWAR_PD_MATCH_SORTBY)->read_int()),
483          maxMismatches(root->awar(AWAR_MAX_MISMATCHES)->read_int()),
484          markHits(root->awar(AWAR_PD_MATCH_MARKHITS)->read_int()),
485          write2tmp(root->awar(AWAR_PD_MATCH_WRITE2TMP)->read_int()),
486          targetString(root->awar(AWAR_TARGET_STRING)->read_char_pntr())
487    {}
488};
489
490static int probe_match_send_data(const ProbeMatchSettings& matchSettings) {
491    if (aisc_put(PD.link, PT_LOCS, PD.locs,
492                 LOCS_MATCH_N_ACCEPT, (long)matchSettings.nmatches,
493                 LOCS_MATCH_N_LIMIT,  (long)matchSettings.nlimit,
494                 LOCS_MATCH_MAX_HITS, (long)matchSettings.maxhits,
495                 NULL))
496        return 1;
497
498    return probe_common_send_data(matchSettings);
499}
500
501static int ecolipos2int(const char *awar_val) {
502    int i = atoi(awar_val);
503    return i>0 ? i : -1;
504}
505
506static char *readableUnknownNames(const ConstStrArray& unames) {
507    GBS_strstruct readable(100);
508
509    int ulast = unames.size()-1;
510    int umax  = ulast <= 10 ? ulast : 10;
511    for (int u = 0; u <= umax; ++u) {
512        if (u) readable.cat(u == ulast ? " and " : ", ");
513        readable.cat(unames[u]);
514    }
515
516    if (umax<ulast) readable.nprintf(30, " (and %i other)", ulast-umax);
517
518    return readable.release();
519}
520
521static void probe_design_event(AW_window *aww, GBDATA *gb_main) {
522    AW_root     *root  = aww->get_root();
523    T_PT_PDC     pdc;
524    T_PT_TPROBE  tprobe;
525    bytestring   bs;
526    bytestring   check;
527    GB_ERROR     error = 0;
528
529    arb_progress progress("Probe design");
530    progress.subtitle("Connecting PT-server");
531
532    {
533        const char *servername = PD_probe_pt_look_for_server(root->awar(AWAR_PT_SERVER)->read_int(), error);
534        if (servername) {
535            PD.link = aisc_open(servername, PD.com, AISC_MAGIC_NUMBER, &error);
536            if (!PD.link) error = "can't contact PT server";
537            PD.locs.clear();
538        }
539    }
540
541    if (!error && init_local_com_struct()) {
542        error = "Cannot contact to probe server: Connection Refused";
543    }
544
545    bool design_gene_probes = root->awar(AWAR_PD_DESIGN_GENE)->read_int();
546    if (design_gene_probes) {
547        GB_transaction ta(gb_main);
548        if (!GEN_is_genome_db(gb_main, -1)) design_gene_probes = false;
549    }
550
551    if (!error) {
552        if (design_gene_probes) { // design probes for genes
553            error = pd_get_the_gene_names(gb_main, bs, check);
554        }
555        else {
556            error = pd_get_the_names(gb_main, bs, check);
557        }
558    }
559
560    if (error) {
561        aw_message(error);
562        return;
563    }
564
565    progress.subtitle("probe design running");
566
567    if (aisc_create(PD.link, PT_LOCS, PD.locs,
568                    LOCS_PROBE_DESIGN_CONFIG, PT_PDC, pdc,
569                    PDC_MIN_PROBELEN, (long)root->awar(AWAR_PD_DESIGN_MIN_LENGTH)->read_int(),
570                    PDC_MAX_PROBELEN, (long)root->awar(AWAR_PD_DESIGN_MAX_LENGTH)->read_int(),
571                    PDC_MINTEMP,      (double)root->awar(AWAR_PD_DESIGN_MIN_TEMP)->read_float(),
572                    PDC_MAXTEMP,      (double)root->awar(AWAR_PD_DESIGN_MAX_TEMP)->read_float(),
573                    PDC_MINGC,        (double)root->awar(AWAR_PD_DESIGN_MIN_GC)->read_float()/100.0,
574                    PDC_MAXGC,        (double)root->awar(AWAR_PD_DESIGN_MAX_GC)->read_float()/100.0,
575                    PDC_MAXBOND,      (double)root->awar(AWAR_PD_DESIGN_MAXBOND)->read_int(),
576                    PDC_MIN_ECOLIPOS, (long)ecolipos2int(root->awar(AWAR_PD_DESIGN_MIN_ECOLIPOS)->read_char_pntr()),
577                    PDC_MAX_ECOLIPOS, (long)ecolipos2int(root->awar(AWAR_PD_DESIGN_MAX_ECOLIPOS)->read_char_pntr()),
578                    PDC_MISHIT,       (long)root->awar(AWAR_PD_DESIGN_MISHIT)->read_int(),
579                    PDC_MINTARGETS,   (double)root->awar(AWAR_PD_DESIGN_MINTARGETS)->read_float()/100.0,
580                    NULL))
581    {
582        aw_message("Connection to PT_SERVER lost (1)");
583        return;
584    }
585
586    if (probe_design_send_data(root, pdc)) {
587        aw_message("Connection to PT_SERVER lost (1)");
588        return;
589    }
590
591    aisc_put(PD.link, PT_PDC, pdc,
592             PDC_NAMES, &bs,
593             PDC_CHECKSUMS, &check,
594             NULL);
595
596
597    // Get the unknown names
598    bytestring unknown_names;
599    if (aisc_get(PD.link, PT_PDC, pdc,
600                 PDC_UNKNOWN_NAMES, &unknown_names,
601                 NULL))
602    {
603        aw_message("Connection to PT_SERVER lost (1)");
604        return;
605    }
606
607    bool abort = false;
608
609    if (unknown_names.size>1) {
610        ConstStrArray unames_array;
611        GBT_split_string(unames_array, unknown_names.data, "#", true);
612
613        char *readable_unames = readableUnknownNames(unames_array);
614
615        if (design_gene_probes) { // updating sequences of missing genes is not possible with gene PT server
616            aw_message(GBS_global_string("Your PT server is not up to date or wrongly chosen!\n"
617                                         "It knows nothing about the following genes:\n"
618                                         "\n"
619                                         "  %s\n"
620                                         "\n"
621                                         "You have to rebuild the PT server.",
622                                         readable_unames));
623            abort = true;
624        }
625        else if (aw_question("ptserver_add_unknown",
626                             GBS_global_string("Your PT server is not up to date or wrongly chosen!\n"
627                                               "It knows nothing about the following species:\n"
628                                               "\n"
629                                               "  %s\n"
630                                               "\n"
631                                               "You may now temporarily add these sequences for probe design.\n",
632                                               readable_unames),
633                             "Add and continue,Abort"))
634        {
635            abort = true;
636        }
637        else {
638            GB_transaction ta(gb_main);
639
640            char *ali_name = GBT_get_default_alignment(gb_main);
641
642            for (size_t u = 0; !abort && u<unames_array.size(); ++u) {
643                const char *uname      = unames_array[u];
644                GBDATA     *gb_species = GBT_find_species(gb_main, uname);
645                if (!gb_species) {
646                    aw_message(GBS_global_string("Species '%s' not found", uname));
647                    abort = true;
648                }
649                else {
650                    GBDATA *data = GBT_find_sequence(gb_species, ali_name);
651                    if (!data) {
652                        aw_message(GB_export_errorf("Species '%s' has no sequence belonging to alignment '%s'", uname, ali_name));
653                        abort = true;
654                    }
655                    else {
656                        T_PT_SEQUENCE pts;
657
658                        bytestring bs_seq;
659                        bs_seq.data = (char*)GB_read_char_pntr(data);
660                        bs_seq.size = GB_read_string_count(data)+1;
661
662                        aisc_create(PD.link, PT_PDC, pdc,
663                                    PDC_SEQUENCE, PT_SEQUENCE, pts,
664                                    SEQUENCE_SEQUENCE, &bs_seq,
665                                    NULL);
666                    }
667                }
668            }
669            free(ali_name);
670        }
671        free(readable_unames);
672        free(unknown_names.data);
673    }
674
675    if (!abort) {
676        aisc_put(PD.link, PT_PDC, pdc,
677                 PDC_GO, (long)0,
678                 NULL);
679
680        progress.subtitle("Reading results from server");
681        {
682            char *locs_error = 0;
683            if (aisc_get(PD.link, PT_LOCS, PD.locs,
684                         LOCS_ERROR, &locs_error,
685                         NULL))
686            {
687                aw_message("Connection to PT_SERVER lost (1)");
688                abort = true;
689            }
690            else if (*locs_error) {
691                aw_message(locs_error);
692                abort = true;
693            }
694            else {
695                free(locs_error);
696            }
697        }
698    }
699
700    if (!abort) {
701        aisc_get(PD.link, PT_PDC, pdc,
702                 PDC_TPROBE, tprobe.as_result_param(),
703                 NULL);
704
705        popup_probe_design_result_window(aww, gb_main);
706        PD.resultList->clear();
707
708        {
709            char *match_info = 0;
710            aisc_get(PD.link, PT_PDC, pdc,
711                     PDC_INFO_HEADER, &match_info,
712                     NULL);
713
714            char *s = strtok(match_info, "\n");
715            while (s) {
716                PD.resultList->insert(s, "");
717                s = strtok(0, "\n");
718            }
719            free(match_info);
720        }
721
722        while (tprobe.exists()) {
723            T_PT_TPROBE  tprobe_next;
724            char        *match_info = 0;
725
726            if (aisc_get(PD.link, PT_TPROBE, tprobe,
727                         TPROBE_NEXT, tprobe_next.as_result_param(),
728                         TPROBE_INFO, &match_info,
729                         NULL)) break;
730
731            tprobe.assign(tprobe_next);
732
733            char *probe, *space;
734            probe = strpbrk(match_info, "acgtuACGTU");
735            if (probe) space = strchr(probe, ' ');
736            if (probe && space) {
737                *space = 0; probe = ARB_strdup(probe); *space=' ';
738            }
739            else {
740                probe = ARB_strdup("");
741            }
742            PD.resultList->insert(match_info, probe);
743            free(probe);
744            free(match_info);
745        }
746        PD.resultList->insert_default("", "");
747        PD.resultList->update();
748    }
749
750    aisc_close(PD.link, PD.com); PD.link = 0;
751    return;
752}
753
754static bool allow_probe_match_event = true;
755
756static __ATTR__USERESULT GB_ERROR probe_match_event(const ProbeMatchSettings& matchSettings, ProbeMatchEventParam *event_param) {
757    GB_ERROR error = NULL;
758    if (allow_probe_match_event) {
759#if defined(ASSERTION_USED)
760        // runtime check against awar usage when NOT called from probe match window!
761        typedef SmartPtr< LocallyModify<bool> > Maybe;
762
763        Maybe dontReadAwars;
764        Maybe dontWriteAwars;
765
766        if (!event_param || !event_param->selection_id) {
767            dontReadAwars = new LocallyModify<bool>(AW_awar::deny_read, true);
768            dontWriteAwars = new LocallyModify<bool>(AW_awar::deny_write, true);
769        }
770#endif
771
772        AW_selection_list *selection_id = event_param ? event_param->selection_id : NULL;
773        int               *counter      = event_param ? event_param->counter : NULL;
774        GBDATA            *gb_main      = event_param ? event_param->gb_main : NULL;
775        int                show_status  = 0;
776        int                extras       = 1;        // mark species and write to temp fields
777
778        if (!gb_main) { error = "Please open probe match window once to enable auto-match"; }
779
780        SmartPtr<arb_progress> progress;
781
782        if (!error) {
783            const char *servername = PD_probe_pt_look_for_server(matchSettings.serverNumber, error);
784
785            if (!error) {
786                if (selection_id) {
787                    selection_id->clear();
788                    pd_assert(!counter);
789                    show_status = 1;
790                }
791                else if (counter) {
792                    extras = 0;
793                }
794
795                if (show_status) {
796                    progress = new arb_progress("Probe match");
797                    progress->subtitle("Connecting PT-server");
798                }
799
800                PD.link = aisc_open(servername, PD.com, AISC_MAGIC_NUMBER, &error);
801                if (!error && !PD.link) error = "Cannot contact PT-server";
802                PD.locs.clear();
803            }
804        }
805
806        if (!error && init_local_com_struct())              error = "Cannot contact PT-server (2)";
807        if (!error && probe_match_send_data(matchSettings)) error = "Connection to PT_SERVER lost (2)";
808
809        const char *probe = matchSettings.targetString.c_str();
810        if (!error) {
811            if (show_status) progress->subtitle("Probe match running");
812
813            if (aisc_nput(PD.link, PT_LOCS, PD.locs,
814                          LOCS_MATCH_REVERSED,       (long)matchSettings.complement, // @@@ use of complement/reverse looks suspect -> check (all uses of LOCS_MATCH_REVERSED and LOCS_MATCH_COMPLEMENT)
815                          LOCS_MATCH_COMPLEMENT,     (long)0,
816                          LOCS_MATCH_SORT_BY,        (long)matchSettings.sortBy,
817                          LOCS_MATCH_MAX_MISMATCHES, (long)matchSettings.maxMismatches,
818                          LOCS_SEARCHMATCH,          probe,
819                          NULL))
820            {
821                error = "Connection to PT_SERVER lost (2)";
822            }
823            else {
824                delete(g_spd);              // delete previous probe data
825                g_spd = new saiProbeData;
826                transferProbeData(g_spd);
827
828                g_spd->setProbeTarget(probe);
829            }
830        }
831
832        bytestring bs;
833        bs.data = 0;
834
835        long matches_truncated = 0;
836        if (!error) {
837            if (show_status) progress->subtitle("Reading results");
838
839            T_PT_MATCHLIST  match_list;
840            long            match_list_cnt    = 0;
841            char           *locs_error        = 0;
842
843            if (aisc_get(PD.link, PT_LOCS, PD.locs,
844                         LOCS_MATCH_LIST,        match_list.as_result_param(),
845                         LOCS_MATCH_LIST_CNT,    &match_list_cnt,
846                         LOCS_MATCH_STRING,      &bs,
847                         LOCS_MATCHES_TRUNCATED, &matches_truncated,
848                         LOCS_ERROR,             &locs_error,
849                         NULL))
850            {
851                error = "Connection to PT_SERVER lost (3)";
852            }
853            else {
854                if (locs_error) {
855                    if (*locs_error) error = GBS_static_string(locs_error);
856                    free(locs_error);
857                }
858                else {
859                    error = "Missing status from server (connection aborted?)";
860                }
861            }
862
863            event_param->hits_summary = GBS_global_string(matches_truncated ? "[more than %li]" : "%li", match_list_cnt);
864            if (matches_truncated) {
865                aw_message("Too many matches, displaying a random digest.\n" // @@@ should better be handled by caller
866                           "Increase the limit in the expert window.");
867            }
868        }
869
870        long mcount                = 0;
871        long unknown_species_count = 0;
872        long unknown_gene_count    = 0;
873
874        if (!error) {
875            char        toksep[2]  = { 1, 0 };
876            char       *strtok_ptr = 0; // stores strtok position
877            const char *hinfo      = strtok_r(bs.data, toksep, &strtok_ptr);
878
879            bool              gene_flag = false;
880            ProbeMatchParser *parser    = 0;
881            char             *result    = ARB_alloc<char>(1024);
882
883
884            if (hinfo) {
885                g_spd->setHeadline(hinfo);
886                parser = new ProbeMatchParser(probe, hinfo);
887                error  = parser->get_error();
888                if (!error) gene_flag = parser->is_gene_result();
889            }
890
891            if (selection_id) {
892                int width         = 0;
893                if (parser && !error) width = parser->get_probe_region_offset()+2+10; // 2 cause headline is shorter and 10 for match prefix region
894
895                const char *searched = GBS_global_string("%-*s%s", width, "Searched for ", probe);
896                selection_id->insert(searched, probe);
897                if (hinfo) selection_id->insert(hinfo, "");
898            }
899
900
901            // clear all marks and delete all 'tmp' entries
902
903            int mark        = extras && matchSettings.markHits;
904            int write_2_tmp = extras && matchSettings.write2tmp;
905
906            GB_push_transaction(gb_main);
907
908            GBDATA *gb_species_data = GBT_get_species_data(gb_main);
909            if (mark && !error) {
910                if (show_status) progress->subtitle(gene_flag ? "Unmarking all species and genes" : "Unmarking all species");
911                for (GBDATA *gb_species = GBT_first_marked_species_rel_species_data(gb_species_data);
912                     gb_species;
913                     gb_species = GBT_next_marked_species(gb_species))
914                {
915                    GB_write_flag(gb_species, 0);
916                }
917
918                if (gene_flag) {
919                    // unmark genes of ALL species
920                    for (GBDATA *gb_species = GBT_first_species_rel_species_data(gb_species_data);
921                         gb_species;
922                         gb_species = GBT_next_species(gb_species))
923                    {
924                        GBDATA *genData = GEN_find_gene_data(gb_species);
925                        if (genData) {
926                            for (GBDATA *gb_gene = GEN_first_marked_gene(gb_species);
927                                 gb_gene;
928                                 gb_gene = GEN_next_marked_gene(gb_gene))
929                            {
930                                GB_write_flag(gb_gene, 0);
931                            }
932                        }
933                    }
934                }
935            }
936            if (write_2_tmp && !error) {
937                if (show_status) progress->subtitle("Deleting old 'tmp' fields");
938                for (GBDATA *gb_species = GBT_first_species_rel_species_data(gb_species_data);
939                     gb_species;
940                     gb_species = GBT_next_species(gb_species))
941                {
942                    GBDATA *gb_tmp = GB_entry(gb_species, "tmp");
943                    if (gb_tmp) GB_delete(gb_tmp);
944                    if (gene_flag) {
945                        for (GBDATA *gb_gene = GEN_first_gene(gb_species);
946                             gb_gene;
947                             gb_gene = GEN_next_gene(gb_species))
948                        {
949                            gb_tmp = GB_entry(gb_gene, "tmp");
950                            if (gb_tmp) GB_delete(gb_tmp);
951                        }
952                    }
953                }
954            }
955
956            // read results from pt-server :
957
958            if (!error) {
959                if (show_status) progress->subtitle("Parsing results");
960
961                g_spd->probeSpecies.clear();
962                g_spd->probeSeq.clear();
963
964                if (gene_flag) {
965                    if (!GEN_is_genome_db(gb_main, -1)) {
966                        error = "Wrong PT-Server chosen (selected one is built for genes)";
967                    }
968                }
969            }
970
971            const char *match_name = 0;
972            while (hinfo && !error && (match_name = strtok_r(0, toksep, &strtok_ptr))) {
973                const char *match_info = strtok_r(0, toksep, &strtok_ptr);
974                if (!match_info) break;
975
976                pd_assert(parser);
977                ParsedProbeMatch  ppm(match_info, *parser);
978                char             *gene_str = 0;
979
980                if (gene_flag) {
981                    gene_str = ppm.get_column_content("genename", true);
982                }
983
984                if (!error) {
985                    char flags[]       = "xx";
986                    GBDATA *gb_species = GBT_find_species_rel_species_data(gb_species_data, match_name);
987
988                    if (gb_species) {
989                        GBDATA *gb_gene = 0;
990                        if (gene_flag && strncmp(gene_str, "intergene_", 10) != 0) { // real gene
991                            gb_gene = GEN_find_gene(gb_species, gene_str);
992                            if (!gb_gene) {
993                                aw_message(GBS_global_string("Gene '%s' not found in organism '%s'", gene_str, match_name));
994                            }
995                        }
996
997                        if (mark) {
998                            GB_write_flag(gb_species, 1);
999                            flags[0] = '*';
1000                            if (gb_gene) {
1001                                GB_write_flag(gb_gene, 1);
1002                                flags[1] = '*';
1003                            }
1004                            else {
1005                                flags[1] = '?'; // no gene
1006                            }
1007                        }
1008                        else {
1009                            flags[0] = " *"[GB_read_flag(gb_species)];
1010                            flags[1] = " *?"[gb_gene ? GB_read_flag(gb_gene) : 2];
1011                        }
1012
1013                        if (write_2_tmp) {
1014                            // write or append to field 'tmp'
1015
1016                            GBDATA   *gb_tmp = 0;
1017                            GB_ERROR  error2 = 0;
1018                            bool      append = true;
1019                            {
1020                                GBDATA *gb_parent = gene_flag ? gb_gene : gb_species;
1021                                gb_tmp            = GB_search(gb_parent, "tmp", GB_FIND);
1022                                if (!gb_tmp) {
1023                                    append              = false;
1024                                    gb_tmp              = GB_search(gb_parent, "tmp", GB_STRING);
1025                                    if (!gb_tmp) error2 = GB_await_error();
1026                                }
1027                            }
1028
1029                            if (!error2) {
1030                                pd_assert(gb_tmp);
1031                                if (append) {
1032                                    char *prevContent = GB_read_string(gb_tmp);
1033                                    if (!prevContent) {
1034                                        error2 = GB_await_error();
1035                                    }
1036                                    else {
1037                                        char *concatenatedContent = ARB_alloc<char>(strlen(prevContent)+1+strlen(match_info)+1);
1038                                        sprintf(concatenatedContent, "%s\n%s", prevContent, match_info);
1039                                        error2 = GB_write_string(gb_tmp, concatenatedContent);
1040                                        free(concatenatedContent);
1041                                        free(prevContent);
1042                                    }
1043                                }
1044                                else {
1045                                    error2 = GB_write_string(gb_tmp, match_info);
1046                                }
1047                            }
1048
1049                            if (error2) aw_message(error2);
1050                        }
1051                    }
1052                    else {
1053                        flags[0] = flags[1] = '?'; // species does not exist
1054                        unknown_species_count++;
1055                    }
1056
1057
1058                    if (gene_flag) {
1059                        sprintf(result, "%s %s", flags, match_info+1); // both flags (skip 1 space from match info to keep alignment)
1060                        char *gene_match_name = new char[strlen(match_name) + strlen(gene_str)+2];
1061                        sprintf(gene_match_name, "%s/%s", match_name, gene_str);
1062                        if (selection_id) selection_id->insert(result, gene_match_name);   // @@@ wert fuer awar eintragen
1063                    }
1064                    else {
1065                        sprintf(result, "%c %s", flags[0], match_info); // only first flag ( = species related)
1066                        if (selection_id) selection_id->insert(result, match_name);   // @@@ wert fuer awar eintragen
1067
1068                        // storing probe data into linked lists
1069                        g_spd->probeSeq.push_back(std::string(match_info));
1070                        g_spd->probeSpecies.push_back(std::string(match_name));
1071                    }
1072                    mcount++;
1073                }
1074
1075                free(gene_str);
1076            }
1077
1078            if (error) error = GBS_static_string(error); // make static copy (error may be freed by delete parser)
1079            delete parser;
1080            free(result);
1081
1082            GB_pop_transaction(gb_main);
1083        }
1084
1085        if (error) {
1086            if (event_param) event_param->hits_summary = "[none]"; // clear hits
1087        }
1088        else {
1089            if (unknown_species_count>0) {
1090                error = GBS_global_string("%li matches hit unknown species -- PT-server is out-of-date or build upon a different database", unknown_species_count);
1091            }
1092            if (unknown_gene_count>0 && !error) {
1093                error = GBS_global_string("%li matches hit unknown genes -- PT-server is out-of-date or build upon a different database", unknown_gene_count);
1094            }
1095
1096            if (selection_id) {     // if !selection_id then probe match window is not opened
1097                pd_assert(g_spd);
1098                event_param->refresh_sai_display = true; // want refresh of SAI/Probe window
1099            }
1100        }
1101
1102        if (counter) *counter = mcount;
1103
1104        aisc_close(PD.link, PD.com);
1105        PD.link = 0;
1106
1107        if (selection_id) {
1108            const char *last_line                 = 0;
1109            if (error) last_line                  = GBS_global_string("****** Error: %s *******", error);
1110            else if (matches_truncated) last_line = "****** List is truncated *******";
1111            else        last_line                 = "****** End of List *******";
1112
1113            selection_id->insert_default(last_line, "");
1114            selection_id->update();
1115        }
1116
1117        free(bs.data);
1118    }
1119    return error;
1120}
1121
1122static void probe_match_event_using_awars(AW_root *root, ProbeMatchEventParam *event_param) {
1123    if (allow_probe_match_event) {
1124        GB_ERROR error = probe_match_event(ProbeMatchSettings(root), event_param);
1125        aw_message_if(error);
1126
1127        LocallyModify<bool> flag(allow_probe_match_event, false);
1128
1129        if (event_param) {
1130            if (event_param->refresh_sai_display) {
1131                root->awar(AWAR_SPV_DB_FIELD_NAME)->touch(); // force refresh of SAI/Probe window
1132            }
1133            if (event_param->selection_id) {
1134                root->awar(AWAR_PD_MATCH_NHITS)->write_string(event_param->hits_summary.c_str()); // update hits in probe match window
1135            }
1136        }
1137        root->awar(AWAR_TREE_REFRESH)->touch(); // refresh tree
1138    }
1139}
1140
1141static void probe_match_all_event(AW_window *aww, AW_selection_list *iselection_id, GBDATA *gb_main) {
1142    AW_selection_list_iterator selentry(iselection_id);
1143    arb_progress               progress("Matching all resolved strings", iselection_id->size());
1144    ProbeMatchSettings         matchSettings(aww->get_root());
1145    const bool                 got_result = selentry;
1146
1147    while (selentry) {
1148        const char *entry          = selentry.get_value(); // @@@ rename -> probe
1149        matchSettings.targetString = entry;
1150
1151        int                  counter = -1;
1152        ProbeMatchEventParam match_event(gb_main, &counter);
1153        GB_ERROR             error   = probe_match_event(matchSettings, &match_event);
1154
1155        // write # of hits to list entries:
1156        {
1157#define DISP_FORMAT "%7i %s"
1158            const char *displayed;
1159            if (error) {
1160                displayed = GBS_global_string(DISP_FORMAT " (Error: %s)", 0, entry, error);
1161            }
1162            else {
1163                displayed = GBS_global_string(DISP_FORMAT, counter, entry);
1164            }
1165            selentry.set_displayed(displayed);
1166#undef DISP_FORMAT
1167        }
1168
1169        ++selentry;
1170        progress.inc();
1171    }
1172
1173    if (got_result) {
1174        iselection_id->sort(true, true);
1175        iselection_id->update();
1176    }
1177}
1178
1179static void resolved_probe_chosen(AW_root *root) {
1180    char *string = root->awar(AWAR_PD_MATCH_RESOLVE)->read_string();
1181    root->awar(AWAR_TARGET_STRING)->write_string(string);
1182}
1183
1184static void selected_match_changed_cb(AW_root *root) {
1185    // this gets called when ever the selected probe match changes
1186    char *temp;
1187    char *selected_match = root->awar(AWAR_PD_SELECTED_MATCH)->read_string();
1188
1189    if (strchr(selected_match, '/')) { // "organism/gene"
1190        temp = strtok(selected_match, "/");
1191        root->awar(AWAR_SPECIES_NAME)->write_string(temp);
1192        temp = strtok(NULL, " /\n");
1193        root->awar(AWAR_GENE_NAME)->write_string(temp);
1194    }
1195    else {
1196        root->awar(AWAR_SPECIES_NAME)->write_string(selected_match);
1197    }
1198
1199    {
1200        LocallyModify<bool> flag(allow_probe_match_event, false); // avoid recursion
1201        root->awar(AWAR_TARGET_STRING)->touch(); // forces editor to jump to probe match in gene
1202    }
1203
1204    free(selected_match);
1205}
1206
1207static void probelength_changed_cb(AW_root *root, bool maxChanged) {
1208    static bool avoid_recursion = false;
1209    if (!avoid_recursion) {
1210        AW_awar *awar_minl = root->awar(AWAR_PD_DESIGN_MIN_LENGTH);
1211        AW_awar *awar_maxl = root->awar(AWAR_PD_DESIGN_MAX_LENGTH);
1212
1213        int minl = awar_minl->read_int();
1214        int maxl = awar_maxl->read_int();
1215
1216        if (minl>maxl) {
1217            if (maxChanged) awar_minl->write_int(maxl);
1218            else            awar_maxl->write_int(minl);
1219        }
1220    }
1221}
1222
1223static void minmax_awar_pair_changed_cb(AW_root *root, bool maxChanged, const char *minAwarName, const char *maxAwarName) {
1224    static bool avoid_recursion = false;
1225    if (!avoid_recursion) {
1226        LocallyModify<bool> flag(avoid_recursion, true);
1227
1228        AW_awar *awar_min = root->awar(minAwarName);
1229        AW_awar *awar_max = root->awar(maxAwarName);
1230
1231        float currMin = awar_min->read_float();
1232        float currMax = awar_max->read_float();
1233
1234        if (currMin>currMax) { // invalid -> correct
1235            if (maxChanged) awar_min->write_float(currMax);
1236            else            awar_max->write_float(currMin);
1237        }
1238    }
1239}
1240static void gc_minmax_changed_cb(AW_root *root, bool maxChanged) {
1241    minmax_awar_pair_changed_cb(root, maxChanged, AWAR_PD_DESIGN_MIN_GC, AWAR_PD_DESIGN_MAX_GC);
1242}
1243static void temp_minmax_changed_cb(AW_root *root, bool maxChanged) {
1244    minmax_awar_pair_changed_cb(root, maxChanged, AWAR_PD_DESIGN_MIN_TEMP, AWAR_PD_DESIGN_MAX_TEMP);
1245}
1246
1247void create_probe_design_variables(AW_root *root, AW_default props, AW_default db) {
1248    PD.win = 0;        // design result window not created
1249
1250    root->awar_string(AWAR_SPECIES_NAME,         "", props);
1251    root->awar_string(AWAR_PD_SELECTED_MATCH,    "", props)->add_callback(selected_match_changed_cb);
1252    root->awar_float (AWAR_PD_DESIGN_EXP_DTEDGE, .5, props);
1253    root->awar_float (AWAR_PD_DESIGN_EXP_DT,     .5, props);
1254
1255    double default_bonds[16] = {
1256        0.0, 0.0, 0.5, 1.1,
1257        0.0, 0.0, 1.5, 0.0,
1258        0.5, 1.5, 0.4, 0.9,
1259        1.1, 0.0, 0.9, 0.0
1260    };
1261
1262    for (int i=0; i<16; i++) {
1263        root->awar_float(bond_awar_name(i), default_bonds[i], props)->set_minmax(0, 3.0);
1264    }
1265    root->awar_float(AWAR_PD_COMMON_EXP_SPLIT,  .5, props);
1266    root->awar_float(AWAR_PD_DESIGN_EXP_DTEDGE, .5, props);
1267    root->awar_float(AWAR_PD_DESIGN_EXP_DT,     .5, props);
1268
1269    root->awar_int  (AWAR_PD_DESIGN_CLIPRESULT, 50,   props)->set_minmax(0, 1000);
1270    root->awar_int  (AWAR_PD_DESIGN_MISHIT,     0,    props)->set_minmax(0, 100000);
1271    root->awar_int  (AWAR_PD_DESIGN_MAXBOND,    4,    props)->set_minmax(0, 20);
1272    root->awar_float(AWAR_PD_DESIGN_MINTARGETS, 50.0, props)->set_minmax(0, 100);
1273
1274    AW_awar *awar_min_len = root->awar_int(AWAR_PD_DESIGN_MIN_LENGTH, 18, props);
1275    awar_min_len->set_minmax(DOMAIN_MIN_LENGTH, 100)->add_callback(makeRootCallback(probelength_changed_cb, false));
1276    root->awar_int(AWAR_PD_DESIGN_MAX_LENGTH, awar_min_len->read_int(), props)->set_minmax(DOMAIN_MIN_LENGTH, 100)->add_callback(makeRootCallback(probelength_changed_cb, true));
1277
1278    root->awar_float(AWAR_PD_DESIGN_MIN_TEMP,     30.0,   props)->set_minmax(0, 1000)->add_callback(makeRootCallback(temp_minmax_changed_cb, false));
1279    root->awar_float(AWAR_PD_DESIGN_MAX_TEMP,     100.0,  props)->set_minmax(0, 1000)->add_callback(makeRootCallback(temp_minmax_changed_cb, true));
1280
1281    root->awar_float(AWAR_PD_DESIGN_MIN_GC, 50.0,  props)->add_callback(makeRootCallback(gc_minmax_changed_cb, false));
1282    root->awar_float(AWAR_PD_DESIGN_MAX_GC, 100.0, props)->add_callback(makeRootCallback(gc_minmax_changed_cb, true));
1283
1284    gc_minmax_changed_cb(root, false);
1285    gc_minmax_changed_cb(root, true);
1286
1287    root->awar_string(AWAR_PD_DESIGN_MIN_ECOLIPOS, "", props);
1288    root->awar_string(AWAR_PD_DESIGN_MAX_ECOLIPOS, "", props);
1289
1290    root->awar_int(AWAR_PT_SERVER,      0, props);
1291    root->awar_int(AWAR_PD_DESIGN_GENE, 0, props);
1292
1293    root->awar_int(AWAR_PD_MATCH_MARKHITS,   1, props);
1294    root->awar_int(AWAR_PD_MATCH_SORTBY,     0, props);
1295    root->awar_int(AWAR_PD_MATCH_WRITE2TMP,  0, props);
1296    root->awar_int(AWAR_PD_MATCH_COMPLEMENT, 0, props);
1297
1298    root->awar_int   (AWAR_MAX_MISMATCHES, 0, db)->set_minmax(0, 200);
1299    root->awar_string(AWAR_TARGET_STRING,  0, db);
1300
1301    root->awar_string(AWAR_PD_MATCH_NHITS,      "[none]", props);
1302    root->awar_int   (AWAR_PD_MATCH_NMATCHES,   1,        props);
1303    root->awar_int   (AWAR_PD_MATCH_LIM_NMATCH, 4,        props);
1304    root->awar_int   (AWAR_PD_MATCH_MAX_RES,    1000000,  props);
1305
1306    root->awar_int(AWAR_PD_MATCH_AUTOMATCH, 0, props)->add_callback(auto_match_changed);
1307
1308    root->awar_string(AWAR_PD_MATCH_RESOLVE, "", props)->add_callback(resolved_probe_chosen);
1309    root->awar_string(AWAR_ITARGET_STRING,   "", db);
1310
1311    root->awar_int(AWAR_PROBE_ADMIN_PT_SERVER,    0, db);
1312    root->awar_int(AWAR_PROBE_CREATE_GENE_SERVER, 0, db);
1313
1314    root->awar_string(AWAR_SPV_SAI_2_PROBE,    "",     db); // name of SAI selected in list
1315    root->awar_string(AWAR_SPV_DB_FIELD_NAME,  "name", db); // name of displayed species field
1316    root->awar_int   (AWAR_SPV_DB_FIELD_WIDTH, 10,     db); // width of displayed species field
1317    root->awar_string(AWAR_SPV_ACI_COMMAND,    "",     db); // User defined or pre-defined ACI command to display
1318
1319    root->awar_int(AWAR_PC_MATCH_NHITS,     0, db);
1320    root->awar_int(AWAR_PC_AUTO_MATCH,      0, props);
1321
1322    root->awar_string(AWAR_PC_TARGET_STRING,  "", db)->set_srt(REPLACE_TARGET_CONTROL_CHARS);
1323    root->awar_string(AWAR_PC_TARGET_NAME,    "", db)->set_srt(REPLACE_TARGET_CONTROL_CHARS);
1324    root->awar_string(AWAR_PC_SELECTED_PROBE, "", db);
1325
1326    root->awar_float(AWAR_PC_MATCH_WIDTH, 1.0, db)->set_minmax(0.01, 100.0);
1327    root->awar_float(AWAR_PC_MATCH_BIAS,  0.0, db)->set_minmax(-1.0, 1.0);
1328
1329    root->awar_float(AWAR_PC_MISMATCH_THRESHOLD, 0.0, db)->set_minmax(0, 100); // Note: limits will be modified by probe_match_with_specificity_event
1330
1331    float default_weights[16] = {0.0};
1332    float default_width = 1.0;
1333    float default_bias  = 0.0;
1334
1335    ArbProbeCollection& g_probe_collection = get_probe_collection();
1336    g_probe_collection.getParameters(default_weights, default_width, default_bias);
1337    g_probe_collection.clear();
1338
1339    char buffer[256] = {0};
1340
1341    for (int i = 0; i < 16 ; i++) {
1342        sprintf(buffer, AWAR_PC_MATCH_WEIGHTS"%i", i);
1343        AW_awar *awar = root->awar_float(buffer, 0.0, db);
1344
1345        awar->set_minmax(0, 10);
1346        default_weights[i] = awar->read_float();
1347    }
1348
1349    g_probe_collection.setParameters(default_weights, default_width, default_bias);
1350
1351    // read probes from DB and add them to collection
1352    {
1353        AW_awar *awar_current = root->awar_string(AWAR_PC_CURRENT_COLLECTION, "", db);
1354        char    *current      = awar_current->read_string();
1355
1356        if (current && current[0]) {
1357            // Note: target sequences/names do not contain '#'/':' (see REPLACE_TARGET_CONTROL_CHARS)
1358            ConstStrArray probes;
1359            GBT_splitNdestroy_string(probes, current, "#", true);
1360
1361            for (size_t i = 0; i<probes.size(); ++i) {
1362                const char *probe = probes[i];
1363                const char *colon = strchr(probe, ':');
1364
1365                if (colon) {
1366                    char       *name = ARB_strpartdup(probe, colon-1);
1367                    const char *seq  = colon+1;
1368                    g_probe_collection.add(name, seq);
1369                    free(name);
1370                }
1371                else {
1372                    aw_message(GBS_global_string("Saved probe ignored: has wrong format ('%s', expected 'name:seq')", probe));
1373                }
1374            }
1375        }
1376        free(current);
1377    }
1378    root->awar_string(AWAR_SPV_SELECTED_PROBE, "",     db); // For highlighting the selected PROBE
1379}
1380
1381static AW_window *create_probe_expert_window(AW_root *root, bool for_design) {
1382    AW_window_simple *aws = new AW_window_simple;
1383    if (for_design) {
1384        aws->init(root, "PD_exp", "Probe Design (Expert)");
1385        aws->load_xfig("pd_spec.fig");
1386    }
1387    else {
1388        aws->init(root, "PM_exp", "Probe Match (Expert)");
1389        aws->load_xfig("pm_spec.fig");
1390    }
1391
1392    aws->label_length(30);
1393    aws->button_length(10);
1394
1395    aws->at("close");
1396    aws->callback(AW_POPDOWN);
1397    aws->create_button("CLOSE", "CLOSE", "C");
1398
1399    aws->callback(makeHelpCallback(for_design ? "pd_spec_param.hlp" : "pm_spec_param.hlp")); // uses_hlp_res("pm_spec_param.hlp", "pd_spec_param.hlp"); see ../SOURCE_TOOLS/check_resources.pl@uses_hlp_res
1400    aws->at("help");
1401    aws->create_button("HELP", "HELP", "C");
1402
1403    for (int i=0; i<16; i++) { // bond matrix
1404        char bondAt[3];
1405        sprintf(bondAt, "%i", i);
1406        aws->at(bondAt);
1407
1408        aws->create_input_field(bond_awar_name(i), 4);
1409    }
1410
1411    aws->sens_mask(AWM_EXP);
1412    aws->at("split"); aws->create_input_field(AWAR_PD_COMMON_EXP_SPLIT,  6);
1413
1414    if (for_design) {
1415        aws->at("dt_edge"); aws->create_input_field(AWAR_PD_DESIGN_EXP_DTEDGE, 6);
1416        aws->at("dt");      aws->create_input_field(AWAR_PD_DESIGN_EXP_DT,     6);
1417        aws->sens_mask(AWM_ALL);
1418    }
1419    else {
1420        aws->at("nmatches");   aws->create_input_field(AWAR_PD_MATCH_NMATCHES,   3);
1421        aws->at("lim_nmatch"); aws->create_input_field(AWAR_PD_MATCH_LIM_NMATCH, 3);
1422        aws->sens_mask(AWM_ALL);
1423        aws->at("max_res");    aws->create_input_field(AWAR_PD_MATCH_MAX_RES,    14);
1424    }
1425
1426    return aws;
1427}
1428
1429static AWT_config_mapping_def probe_design_mapping_def[] = {
1430    // main window:
1431    { AWAR_PD_DESIGN_CLIPRESULT,   "clip" },
1432    { AWAR_PD_DESIGN_MISHIT,       "mishit" },
1433    { AWAR_PD_DESIGN_MAXBOND,      "maxbond" },
1434    { AWAR_PD_DESIGN_MINTARGETS,   "mintarget" },
1435    { AWAR_PD_DESIGN_MIN_LENGTH,   "probelen" },
1436    { AWAR_PD_DESIGN_MAX_LENGTH,   "probemaxlen" },
1437    { AWAR_PD_DESIGN_MIN_TEMP,     "mintemp" },
1438    { AWAR_PD_DESIGN_MAX_TEMP,     "maxtemp" },
1439    { AWAR_PD_DESIGN_MIN_GC,       "mingc" },
1440    { AWAR_PD_DESIGN_MAX_GC,       "maxgc" },
1441    { AWAR_PD_DESIGN_MIN_ECOLIPOS, "minecoli" },
1442    { AWAR_PD_DESIGN_MAX_ECOLIPOS, "maxecoli" },
1443    { AWAR_PD_DESIGN_GENE,         "gene" },
1444
1445    // expert window:
1446    { AWAR_PD_DESIGN_EXP_DTEDGE,   "dtedge" },
1447    { AWAR_PD_DESIGN_EXP_DT,       "dt" },
1448
1449    { 0, 0 }
1450};
1451
1452static void setup_probe_config(AWT_config_definition& cdef, const AWT_config_mapping_def *mapping) {
1453    cdef.add(mapping);
1454
1455    // entries common for both expert windows (design + match)
1456    cdef.add(AWAR_PD_COMMON_EXP_SPLIT, "split");
1457    for (int i = 0; i<16; ++i) {
1458        cdef.add(bond_awar_name(i), "bond", i);
1459    }
1460}
1461
1462AW_window *create_probe_design_window(AW_root *root, GBDATA *gb_main) {
1463    bool is_genom_db;
1464    {
1465        GB_transaction ta(gb_main);
1466        is_genom_db = GEN_is_genome_db(gb_main, -1);
1467    }
1468
1469    AW_window_simple *aws = new AW_window_simple;
1470    aws->init(root, "PROBE_DESIGN", "PROBE DESIGN");
1471
1472    aws->load_xfig("pd_main.fig");
1473
1474    aws->at("close");
1475    aws->callback(AW_POPDOWN);
1476    aws->create_button("CLOSE", "CLOSE", "C");
1477
1478    aws->at("help");
1479    aws->callback(makeHelpCallback("probedesign.hlp"));
1480    aws->create_button("HELP", "HELP", "H");
1481
1482    aws->callback(makeWindowCallback(probe_design_event, gb_main));
1483    aws->at("design");
1484    aws->highlight();
1485    aws->create_button("GO", "GO", "G");
1486
1487    aws->callback(makeWindowCallback(popup_probe_design_result_window, gb_main));
1488    aws->at("result");
1489    aws->create_button("RESULT", "RESULT", "S");
1490
1491    aws->callback(makeCreateWindowCallback(create_probe_expert_window, true));
1492    aws->at("expert");
1493    aws->create_button("EXPERT", "EXPERT", "S");
1494
1495    aws->at("pt_server");
1496    aws->label("PT-Server:");
1497    awt_create_PTSERVER_selection_button(aws, AWAR_PT_SERVER);
1498
1499    aws->at("lenout");   aws->create_input_field(AWAR_PD_DESIGN_CLIPRESULT, 6);
1500    aws->at("mishit");   aws->create_input_field(AWAR_PD_DESIGN_MISHIT,     6);
1501    aws->sens_mask(AWM_EXP);
1502    aws->at("maxbonds"); aws->create_input_field(AWAR_PD_DESIGN_MAXBOND,    6);
1503    aws->sens_mask(AWM_ALL);
1504    aws->at("minhits");  aws->create_input_field(AWAR_PD_DESIGN_MINTARGETS, 6);
1505
1506    aws->at("minlen"); aws->create_input_field(AWAR_PD_DESIGN_MIN_LENGTH,   5);
1507    aws->at("maxlen"); aws->create_input_field(AWAR_PD_DESIGN_MAX_LENGTH,   5);
1508    aws->at("mint");   aws->create_input_field(AWAR_PD_DESIGN_MIN_TEMP,     5);
1509    aws->at("maxt");   aws->create_input_field(AWAR_PD_DESIGN_MAX_TEMP,     5);
1510    aws->at("mingc");  aws->create_input_field(AWAR_PD_DESIGN_MIN_GC,       5);
1511    aws->at("maxgc");  aws->create_input_field(AWAR_PD_DESIGN_MAX_GC,       5);
1512    aws->at("minpos"); aws->create_input_field(AWAR_PD_DESIGN_MIN_ECOLIPOS, 5);
1513    aws->at("maxpos"); aws->create_input_field(AWAR_PD_DESIGN_MAX_ECOLIPOS, 5);
1514
1515    if (is_genom_db) {
1516        aws->at("gene");
1517        aws->label("Gene probes?");
1518        aws->create_toggle(AWAR_PD_DESIGN_GENE);
1519    }
1520
1521    aws->at("save");
1522    AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "probe_design", makeConfigSetupCallback(setup_probe_config, probe_design_mapping_def));
1523
1524    return aws;
1525}
1526
1527// -------------------------------------------------------------------
1528
1529inline void my_strupr(char *s) {
1530    pd_assert(s);
1531    for (int i=0; s[i]; i++) {
1532        s[i] = toupper(s[i]);
1533    }
1534}
1535
1536static void resolve_IUPAC_target_string(AW_root *root, AW_selection_list *selection_id, GBDATA *gb_main) {
1537    selection_id->clear();
1538
1539    GB_alignment_type ali_type = GBT_get_alignment_type(gb_main, GBT_get_default_alignment(gb_main));
1540
1541    GB_ERROR err = 0;
1542    if (ali_type != GB_AT_RNA && ali_type!=GB_AT_DNA) {
1543        err = "Wrong alignment type";
1544        GB_clear_error();
1545    }
1546    else {
1547        int   index   = ali_type==GB_AT_RNA ? 1 : 0;
1548        char *istring = root->awar(AWAR_ITARGET_STRING)->read_string();
1549
1550        if (istring && istring[0]) { // contains sth?
1551            my_strupr(istring);
1552
1553            int bases_to_resolve = 0;
1554            char *istr = istring;
1555            int istring_length = strlen(istring);
1556
1557            for (;;) {
1558                char i = *istr++;
1559                if (!i) break;
1560                if (i=='?') continue; // ignore '?'
1561
1562                int idx = i-'A';
1563                if (idx<0 || idx>=26 || iupac::nuc_group[idx][index].members==0) {
1564                    err = GBS_global_string("Illegal character '%c' in IUPAC-String", i);
1565                    break;
1566                }
1567
1568                if (iupac::nuc_group[idx][index].count>1) {
1569                    bases_to_resolve++;
1570                }
1571            }
1572
1573            if (!err) {
1574                int *offsets_to_resolve = new int[bases_to_resolve];
1575                int resolutions = 1;
1576                {
1577                    istr = istring;
1578                    int offset = 0;
1579                    int offset_count = 0;
1580                    for (;;) {
1581                        char i = *istr++;
1582                        if (!i) break;
1583
1584                        if (i!='?') {
1585                            int idx = iupac::to_index(i);
1586                            pd_assert(iupac::nuc_group[idx][index].members);
1587
1588                            if (iupac::nuc_group[idx][index].count>1) {
1589                                offsets_to_resolve[offset_count++] = offset; // store string offsets of non-unique base-codes
1590                                resolutions *= iupac::nuc_group[idx][index].count; // calc # of resolutions
1591                            }
1592                        }
1593                        offset++;
1594                    }
1595                }
1596
1597                {
1598                    int *resolution_idx = new int[bases_to_resolve];
1599                    int *resolution_max_idx = new int[bases_to_resolve];
1600                    {
1601                        int i;
1602                        for (i=0; i<bases_to_resolve; i++) {
1603                            resolution_idx[i] = 0;
1604                            int idx = iupac::to_index(istring[offsets_to_resolve[i]]);
1605                            resolution_max_idx[i] = iupac::nuc_group[idx][index].count-1;
1606                        }
1607                    }
1608
1609                    char *buffer = new char[istring_length+1];
1610                    int not_last = resolutions-1;
1611
1612                    for (;;) {
1613                        // create string according to resolution_idx[]:
1614                        int i;
1615
1616                        memcpy(buffer, istring, istring_length+1);
1617                        for (i=0; i<bases_to_resolve; i++) {
1618                            int off = offsets_to_resolve[i];
1619                            int idx = iupac::to_index(istring[off]);
1620
1621                            pd_assert(iupac::nuc_group[idx][index].members);
1622                            buffer[off] = iupac::nuc_group[idx][index].members[resolution_idx[i]];
1623                        }
1624
1625                        selection_id->insert(buffer, buffer);
1626                        not_last--;
1627
1628                        // permute indices:
1629                        int nidx = bases_to_resolve-1;
1630                        int done = 0;
1631                        while (!done && nidx>=0) {
1632                            if (resolution_idx[nidx]<resolution_max_idx[nidx]) {
1633                                resolution_idx[nidx]++;
1634                                done = 1;
1635                                break;
1636                            }
1637                            nidx--;
1638                        }
1639                        if (!done) break; // we did all permutations!
1640
1641                        nidx++; // do not touch latest incremented index
1642                        while (nidx<bases_to_resolve) resolution_idx[nidx++] = 0; // zero all other indices
1643                    }
1644
1645                    delete [] buffer;
1646                    delete [] resolution_max_idx;
1647                    delete [] resolution_idx;
1648
1649                    pd_assert(!err);
1650                    selection_id->insert_default("", "");
1651                }
1652
1653                delete [] offsets_to_resolve;
1654            }
1655        }
1656        else { // empty input
1657            selection_id->insert_default("", "");
1658        }
1659    }
1660    if (err) selection_id->insert_default(err, "");
1661    selection_id->update();
1662}
1663
1664enum ModMode { TS_MOD_CLEAR, TS_MOD_REV_COMPL, TS_MOD_COMPL };
1665
1666static void modify_target_string(AW_window *aww, GBDATA *gb_main, ModMode mod_mode) {
1667    AW_root  *root          = aww->get_root();
1668    char     *target_string = root->awar(AWAR_TARGET_STRING)->read_string();
1669    GB_ERROR  error         = 0;
1670
1671    if (mod_mode == TS_MOD_CLEAR) target_string[0] = 0;
1672    else {
1673        GB_transaction    ta(gb_main);
1674        GB_alignment_type ali_type = GBT_get_alignment_type(gb_main, GBT_get_default_alignment(gb_main));
1675        if (ali_type == GB_AT_UNKNOWN) {
1676            error = GB_await_error();
1677        }
1678        else if (mod_mode == TS_MOD_REV_COMPL) {
1679            char T_or_U;
1680            error = GBT_determine_T_or_U(ali_type, &T_or_U, "reverse-complement");
1681            if (!error) GBT_reverseComplementNucSequence(target_string, strlen(target_string), T_or_U);
1682        }
1683        else if (mod_mode == TS_MOD_COMPL) {
1684            char T_or_U;
1685            error = GBT_determine_T_or_U(ali_type, &T_or_U, "complement");
1686            if (!error) freeset(target_string, GBT_complementNucSequence(target_string, strlen(target_string), T_or_U));
1687        }
1688    }
1689
1690    if (error) aw_message(error);
1691    else root->awar(AWAR_TARGET_STRING)->write_string(target_string);
1692    free(target_string);
1693}
1694
1695static void clear_itarget(AW_window *aww) {
1696    aww->get_root()->awar(AWAR_ITARGET_STRING)->write_string("");
1697}
1698
1699static AW_window *create_IUPAC_resolve_window(AW_root *root, GBDATA *gb_main) {
1700    AW_window_simple *aws = new AW_window_simple;
1701
1702    aws->init(root, "PROBE_MATCH_RESOLVE_IUPAC", "Resolve IUPAC for Probe Match");
1703    aws->load_xfig("pd_match_iupac.fig");
1704
1705    aws->button_length(11);
1706
1707    aws->at("close");
1708    aws->callback(AW_POPDOWN);
1709    aws->create_button("CLOSE", "CLOSE", "C");
1710
1711    aws->at("help");
1712    aws->callback(makeHelpCallback("pd_match_iupac.hlp"));
1713    aws->create_button("HELP", "HELP", "H");
1714
1715    aws->at("istring");
1716    aws->create_input_field(AWAR_ITARGET_STRING, 32);
1717
1718    aws->at("iresult");
1719    AW_selection_list *iselection_id;
1720    iselection_id = aws->create_selection_list(AWAR_PD_MATCH_RESOLVE, 32, 15, true);
1721    iselection_id->insert_default("---empty---", "");
1722
1723    // add callback for automatic decomposition of AWAR_ITARGET_STRING:
1724    RootCallback  upd_cb       = makeRootCallback(resolve_IUPAC_target_string, iselection_id, gb_main);
1725    AW_awar      *awar_itarget = root->awar(AWAR_ITARGET_STRING);
1726    awar_itarget->add_callback(upd_cb);
1727    root->awar(AWAR_DEFAULT_ALIGNMENT)->add_callback(upd_cb);
1728
1729    aws->at("match_all");
1730    aws->callback(makeWindowCallback(probe_match_all_event, iselection_id, gb_main));
1731    aws->create_button("MATCH_ALL", "Match all", "M");
1732
1733    aws->at("clear");
1734    aws->callback(clear_itarget);
1735    aws->create_button("CLEAR", "Clear", "r");
1736
1737    aws->at("iupac_info");
1738    aws->callback(AWT_create_IUPAC_info_window);
1739    aws->create_button("IUPAC_INFO", "IUPAC info", "I");
1740
1741    awar_itarget->touch(); // force initial refresh for saved value
1742
1743    return aws;
1744}
1745
1746static void popupSaiProbeMatchWindow(AW_window *aw, GBDATA *gb_main) {
1747    static AW_window *aw_saiProbeMatch = 0;
1748
1749    if (!aw_saiProbeMatch) aw_saiProbeMatch = createSaiProbeMatchWindow(aw->get_root(), gb_main);
1750    if (g_spd) transferProbeData(g_spd); // transferring probe data to saiProbeMatch function
1751
1752    aw_saiProbeMatch->activate();
1753}
1754
1755static AWT_config_mapping_def probe_match_mapping_def[] = {
1756    // main window:
1757    { AWAR_TARGET_STRING,       "target" },
1758    { AWAR_PD_MATCH_COMPLEMENT, "complement" },
1759    { AWAR_PD_MATCH_MARKHITS,   "markhits" },
1760    { AWAR_PD_MATCH_WRITE2TMP,  "writetmp" },
1761    { AWAR_PD_MATCH_AUTOMATCH,  "automatch" },
1762
1763    // expert window:
1764    { AWAR_PD_MATCH_NMATCHES,   "nmatches" },
1765    { AWAR_PD_MATCH_LIM_NMATCH, "limitnmatch" },
1766    { AWAR_PD_MATCH_MAX_RES,    "maxresults" },
1767
1768    { 0, 0 }
1769};
1770
1771
1772AW_window *create_probe_match_window(AW_root *root, GBDATA *gb_main) {
1773    static AW_window_simple *aws = 0; // the one and only probe match window
1774    if (!aws) {
1775        aws = new AW_window_simple;
1776
1777        aws->init(root, "PROBE_MATCH", "PROBE MATCH");
1778        aws->load_xfig("pd_match.fig");
1779
1780        aws->auto_space(5, 5);
1781
1782        aws->at("close");
1783        aws->callback(AW_POPDOWN);
1784        aws->create_button("CLOSE", "CLOSE", "C");
1785
1786        aws->callback(makeHelpCallback("probematch.hlp"));
1787        aws->at("help");
1788        aws->create_button("HELP", "HELP", "H");
1789
1790        aws->at("string");
1791        aws->create_input_field(AWAR_TARGET_STRING, 32);
1792
1793        AW_selection_list *selection_id;
1794        aws->at("result");
1795        selection_id = aws->create_selection_list(AWAR_PD_SELECTED_MATCH, 110, 15, true);
1796        selection_id->insert_default("****** No results yet *******", "");  // if list is empty -> crashed if new species was selected in ARB_EDIT4
1797
1798        static SmartPtr<TypedSelectionList> typed_selection = new TypedSelectionList("match", selection_id, "probe match", "probe_match");
1799        aws->at("print");
1800        aws->callback(makeWindowCallback(create_print_box_for_selection_lists, &*typed_selection));
1801        aws->create_button("PRINT", "PRINT", "P");
1802
1803        aws->at("matchSai");
1804        aws->help_text("saiProbe.hlp");
1805        aws->callback(makeWindowCallback(popupSaiProbeMatchWindow, gb_main));
1806        aws->create_button("MATCH_SAI", "Match SAI", "S");
1807
1808        aws->callback(makeCreateWindowCallback(create_probe_expert_window, false));
1809        aws->at("expert");
1810        aws->create_button("EXPERT", "EXPERT", "X");
1811
1812        aws->at("pt_server");
1813        awt_create_PTSERVER_selection_button(aws, AWAR_PT_SERVER);
1814
1815        aws->at("complement");
1816        aws->create_toggle(AWAR_PD_MATCH_COMPLEMENT);
1817
1818        aws->at("mark");
1819        aws->create_toggle(AWAR_PD_MATCH_MARKHITS);
1820
1821        aws->at("weighted");
1822        aws->create_toggle(AWAR_PD_MATCH_SORTBY);
1823
1824        aws->at("mismatches");
1825        aws->create_input_field_with_scaler(AWAR_MAX_MISMATCHES, 5, 200, AW_SCALER_EXP_LOWER);
1826
1827        aws->at("tmp");
1828        aws->create_toggle(AWAR_PD_MATCH_WRITE2TMP);
1829
1830        aws->at("nhits");
1831        aws->create_button(0, AWAR_PD_MATCH_NHITS);
1832
1833        aws->button_length(9);
1834
1835        ProbeMatchEventParam *event_param = new ProbeMatchEventParam(gb_main, selection_id);
1836        aws->callback(RootAsWindowCallback::simple(probe_match_event_using_awars, event_param));
1837        aws->at("match");
1838        aws->create_button("MATCH", "MATCH", "D");
1839
1840        aws->at("auto");
1841        aws->label("Auto");
1842        aws->create_toggle(AWAR_PD_MATCH_AUTOMATCH);
1843        enable_auto_match_cb(root, event_param);
1844
1845        aws->callback(makeWindowCallback(modify_target_string, gb_main, TS_MOD_CLEAR));
1846        aws->at("clear");
1847        aws->create_button("CLEAR", "Clear", "0");
1848
1849        aws->callback(makeWindowCallback(modify_target_string, gb_main, TS_MOD_REV_COMPL));
1850        aws->at("revcompl");
1851        aws->create_button("REVCOMPL", "RevCompl", "R");
1852
1853        aws->callback(makeWindowCallback(modify_target_string, gb_main, TS_MOD_COMPL));
1854        aws->at("compl");
1855        aws->create_button("COMPL", "Compl", "C");
1856
1857        aws->callback(makeCreateWindowCallback(create_IUPAC_resolve_window, gb_main));
1858        aws->at("iupac");
1859        aws->create_button("IUPAC", "IUPAC", "I");
1860
1861        aws->at("config");
1862        AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "probe_match", makeConfigSetupCallback(setup_probe_config, probe_match_mapping_def));
1863    }
1864    return aws;
1865}
1866
1867static void pd_start_pt_server(AW_window *aww) {
1868    const char *server_tag = GBS_ptserver_tag(aww->get_root()->awar(AWAR_PROBE_ADMIN_PT_SERVER)->read_int());
1869    arb_progress progress("Connecting PT-server");
1870    GB_ERROR error = arb_look_and_start_server(AISC_MAGIC_NUMBER, server_tag);
1871    if (error) aw_message(error);
1872}
1873
1874static void pd_kill_pt_server(AW_window *aww, bool kill_all) {
1875    if (aw_ask_sure("kill_ptserver",
1876                    GBS_global_string("Are you sure to stop %s", kill_all ? "all servers" : "that server")))
1877    {
1878        long min = 0;
1879        long max = 0;
1880
1881        if (kill_all) {
1882            const char * const *pt_servers = GBS_get_arb_tcp_entries("ARB_PT_SERVER*");
1883            while (pt_servers[max]) max++;
1884        }
1885        else {
1886            min = max = aww->get_root()->awar(AWAR_PROBE_ADMIN_PT_SERVER)->read_int(); // selected server
1887        }
1888
1889        arb_progress progress("Stopping PT-servers..", max-min+1);
1890        GB_ERROR     error = NULL;
1891
1892        for (int i = min; i <= max && !error;  i++) {
1893            char *choice = GBS_ptserver_id_to_choice(i, 0);
1894            if (!choice) {
1895                error = GB_await_error();
1896            }
1897            else {
1898                progress.subtitle(GBS_global_string("Trying to stop '%s'", choice));
1899
1900                const char *server_tag = GBS_ptserver_tag(i);
1901                GB_ERROR    kill_error = arb_look_and_kill_server(AISC_MAGIC_NUMBER, server_tag);
1902
1903                if (kill_error) aw_message(GBS_global_string("Could not stop '%s' (Reason: %s)", choice, kill_error));
1904                else aw_message(GBS_global_string("Stopped '%s'", choice));
1905
1906                free(choice);
1907            }
1908            progress.inc_and_check_user_abort(error);
1909        }
1910        progress.done();
1911        aw_message_if(error);
1912    }
1913}
1914
1915static const char *ptserver_directory_info_command(const char *dirname, const char *directory) {
1916    return GBS_global_string("echo 'Contents of directory %s:'; echo; (cd \"%s\"; ls -hl); echo; "
1917                             "echo 'Available disk space in %s:'; echo; df -h \"%s\"; echo; ",
1918                             dirname, directory,
1919                             dirname, directory);
1920
1921}
1922
1923static void pd_query_pt_server(AW_window *aww) {
1924    const char *server_tag = GBS_ptserver_tag(aww->get_root()->awar(AWAR_PROBE_ADMIN_PT_SERVER)->read_int());
1925
1926    GBS_strstruct query_cmd(1024);
1927    query_cmd.cat(ptserver_directory_info_command("ARBHOME/lib/pts", "$ARBHOME/lib/pts"));
1928
1929    const char *ARB_LOCAL_PTS = ARB_getenv_ignore_empty("ARB_LOCAL_PTS");
1930    if (ARB_LOCAL_PTS) query_cmd.cat(ptserver_directory_info_command("ARB_LOCAL_PTS", "$ARB_LOCAL_PTS")); // only defined if called via 'arb'-script
1931    else               query_cmd.cat(ptserver_directory_info_command("HOME/.arb_pts", "${HOME}/.arb_pts"));
1932
1933    query_cmd.cat("echo 'Running ARB programs:'; echo; ");
1934
1935    GB_ERROR error = NULL;
1936    {
1937        const char *socketid = GBS_read_arb_tcp(server_tag);
1938        if (!socketid) {
1939            error = GB_await_error();
1940        }
1941        else {
1942            char *arb_who = createCallOnSocketHost(socketid, "$ARBHOME/bin/", "arb_who", WAIT_FOR_TERMINATION, NULL);
1943            query_cmd.cat(arb_who);
1944            free(arb_who);
1945        }
1946    }
1947
1948    if (!error) error = GB_xcmd(query_cmd.get_data(), XCMD_ASYNC_WAITKEY);
1949    aw_message_if(error);
1950}
1951
1952static void pd_export_pt_server(AW_window *aww, GBDATA *gb_main) {
1953    AW_root  *awr   = aww->get_root();
1954    GB_ERROR  error = 0;
1955
1956    bool create_gene_server = awr->awar(AWAR_PROBE_CREATE_GENE_SERVER)->read_int();
1957    {
1958        GB_transaction ta(gb_main);
1959        if (create_gene_server && !GEN_is_genome_db(gb_main, -1)) create_gene_server = false;
1960
1961        // check alignment first
1962        if (create_gene_server) {
1963            GBDATA *gb_ali     = GBT_get_alignment(gb_main, GENOM_ALIGNMENT);
1964            if (!gb_ali) error = GB_await_error();
1965        }
1966        else { // normal pt server
1967            char              *use     = GBT_get_default_alignment(gb_main);
1968            GB_alignment_type  alitype = GBT_get_alignment_type(gb_main, use);
1969            if (alitype == GB_AT_AA) error = "The PT-server does only support RNA/DNA sequence data";
1970            free(use);
1971        }
1972    }
1973
1974    long        server_num = awr->awar(AWAR_PROBE_ADMIN_PT_SERVER)->read_int();
1975    const char *server_tag  = GBS_ptserver_tag(server_num);
1976
1977    if (!error &&
1978        aw_question("update_ptserver",
1979                    "This function will send your loaded database to the pt_server,\n"
1980                    "which will need a long time (up to several hours) to analyse the data.\n"
1981                    "Until the new server has analyzed all data, no server functions are available.\n\n"
1982                    "Note 1: You must have the write permissions to do that ($ARBHOME/lib/pts/xxx))\n"
1983                    "Note 2: The server will do the job in background,\n"
1984                    "        quitting this program won't affect the server",
1985                    "Cancel,Do it"))
1986    {
1987        arb_progress progress("Updating PT-server");
1988        progress.subtitle("Stopping PT-server");
1989        arb_look_and_kill_server(AISC_MAGIC_NUMBER, server_tag);
1990
1991        const char *ipPort = GBS_read_arb_tcp(server_tag);
1992        const char *file   = NULL;
1993        if (!ipPort) error = GB_await_error();
1994        else {
1995            file = GBS_scan_arb_tcp_param(ipPort, "-d");
1996            GBS_add_ptserver_logentry(GBS_global_string("Started build of '%s'", file));
1997
1998            char *db_name = awr->awar(AWAR_DB_PATH)->read_string();
1999            GBS_add_ptserver_logentry(GBS_global_string("Exporting DB '%s'", db_name));
2000            free(db_name);
2001        }
2002
2003        if (!error) {
2004            progress.subtitle("Exporting the database");
2005            {
2006                const char *mode = GB_supports_mapfile() ? "mbf" : "bf"; // save PT-server database with Fastload file (if supported)
2007                if (create_gene_server) {
2008                    char *temp_server_name = GBS_string_eval(file, "*.arb=*_temp.arb", 0);
2009                    error = GB_save_as(gb_main, temp_server_name, mode);
2010
2011                    if (!error) {
2012                        // convert database (genes -> species)
2013                        progress.subtitle("Preparing DB for gene PT server");
2014                        GBS_add_ptserver_logentry("Preparing DB for gene PT server");
2015                        char *command = GBS_global_string_copy("$ARBHOME/bin/arb_gene_probe %s %s", temp_server_name, file);
2016                        error = GBK_system(command);
2017                        if (error) error = GBS_global_string("Couldn't convert database for gene pt server\n(Reason: %s)", error);
2018                        free(command);
2019                    }
2020                    free(temp_server_name);
2021                }
2022                else { // normal pt_server
2023                    error = GB_save_as(gb_main, file, mode);
2024                }
2025            }
2026        }
2027
2028        if (!error) { // set pt-server database file to same permissions as pts directory
2029            char *dir = const_cast<char*>(strrchr(file, '/'));
2030            if (dir) {
2031                *dir = 0;
2032                long modi = GB_mode_of_file(file);
2033                *dir = '/';
2034                modi &= 0666;
2035                error = GB_set_mode_of_file(file, modi);
2036            }
2037        }
2038
2039        if (!error) {
2040            progress.subtitle("Start PT-server (builds in background)");
2041            error = arb_start_server(server_tag, 1);
2042        }
2043    }
2044    if (error) aw_message(error);
2045}
2046
2047static void pd_view_pt_log(AW_window *) {
2048    AW_edit(GBS_ptserver_logname());
2049}
2050
2051AW_window *create_probe_admin_window(AW_root *root, GBDATA *gb_main) {
2052    bool is_genom_db;
2053    {
2054        GB_transaction ta(gb_main);
2055        is_genom_db = GEN_is_genome_db(gb_main, -1);
2056    }
2057
2058    AW_window_simple *aws = new AW_window_simple;
2059    aws->init(root, "PT_SERVER_ADMIN", "PT_SERVER ADMIN");
2060
2061    aws->load_xfig("pd_admin.fig");
2062
2063
2064    aws->callback(makeHelpCallback("probeadmin.hlp"));
2065    aws->at("help");
2066    aws->create_button("HELP", "HELP", "H");
2067
2068    aws->at("close");
2069    aws->callback(AW_POPDOWN);
2070    aws->create_button("CLOSE", "CLOSE", "C");
2071
2072    aws->button_length(18);
2073
2074    aws->at("pt_server");
2075    awt_create_PTSERVER_selection_list(aws, AWAR_PROBE_ADMIN_PT_SERVER);
2076
2077    aws->at("start");
2078    aws->callback(pd_start_pt_server);
2079    aws->create_button("START_SERVER", "Start server");
2080
2081    aws->at("kill");
2082    aws->callback(makeWindowCallback(pd_kill_pt_server, false));
2083    aws->create_button("KILL_SERVER", "Stop server");
2084
2085    aws->at("query");
2086    aws->callback(pd_query_pt_server);
2087    aws->create_button("CHECK_SERVER", "Check server");
2088
2089    aws->at("kill_all");
2090    aws->callback(makeWindowCallback(pd_kill_pt_server, true));
2091    aws->create_button("KILL_ALL_SERVERS", "Stop all servers");
2092
2093    aws->at("edit");
2094    aws->callback(makeWindowCallback(awt_edit_arbtcpdat_cb, gb_main));
2095    aws->create_button("CREATE_TEMPLATE", "Configure");
2096
2097    aws->at("log");
2098    aws->callback(pd_view_pt_log);
2099    aws->create_button("EDIT_LOG", "View logfile");
2100
2101    aws->at("export");
2102    aws->callback(makeWindowCallback(pd_export_pt_server, gb_main));
2103    aws->create_button("UPDATE_SERVER", "Build server");
2104
2105    if (is_genom_db) {
2106        aws->at("gene_server");
2107        aws->label("Gene server?");
2108        aws->create_toggle(AWAR_PROBE_CREATE_GENE_SERVER);
2109    }
2110
2111    return aws;
2112}
2113
2114// ----------------------------------------------------------------------------
2115
2116static AW_window *create_probe_match_specificity_control_window(AW_root *root) {
2117    static AW_window_simple *aws = NULL;
2118
2119    if (!aws) {
2120        aws = new AW_window_simple;
2121
2122        aws->init(root, "MATCH_DISPLAYCONTROL", "MATCH DISPLAY CONTROL");
2123
2124        aws->auto_space(10, 10);
2125        aws->label_length(35);
2126
2127        const int FIELDSIZE  = 5;
2128        const int SCALERSIZE = 500;
2129
2130        aws->label("Mismatch threshold");
2131        aws->create_input_field_with_scaler(AWAR_PC_MISMATCH_THRESHOLD, FIELDSIZE, SCALERSIZE);
2132
2133        aws->at_newline();
2134
2135        aws->label("Clade marked threshold");
2136        aws->create_input_field_with_scaler(AWAR_DTREE_GROUP_MARKED_THRESHOLD, FIELDSIZE, SCALERSIZE);
2137
2138        aws->at_newline();
2139
2140        aws->label("Clade partially marked threshold");
2141        aws->create_input_field_with_scaler(AWAR_DTREE_GROUP_PARTIALLY_MARKED_THRESHOLD, FIELDSIZE, SCALERSIZE);
2142
2143        aws->at_newline();
2144
2145        aws->callback(TREE_create_marker_settings_window);
2146        aws->create_autosize_button("MARKER_SETTINGS", "Marker display settings", "d");
2147
2148        aws->at_newline();
2149    }
2150
2151    return aws;
2152}
2153
2154// ----------------------------------------------------------------------------
2155
2156struct ArbPM_Context {
2157    AW_selection_list *probes_id;
2158    TREE_canvas       *ntw;
2159
2160    ArbPM_Context()
2161        : probes_id(NULL),
2162          ntw(NULL)
2163    {}
2164};
2165
2166static ArbPM_Context PM_Context;
2167
2168static void       probe_match_update_probe_list(ArbPM_Context *pContext);
2169static AW_window *create_probe_collection_window(AW_root *root, ArbPM_Context *pContext);
2170
2171struct ArbWriteFile_Context {
2172    FILE         *pFile;
2173    arb_progress *pProgress;
2174    int           nLastPercent;
2175
2176    ArbWriteFile_Context()
2177        : pFile(NULL),
2178          pProgress(NULL),
2179          nLastPercent(0)
2180    {}
2181};
2182
2183// ----------------------------------------------------------------------------
2184
2185static bool probe_match_with_specificity_enum_callback(void *pVoidContext, const char *pResult, bool bIsComment, int nItem, int nItems) {
2186    ArbWriteFile_Context *pContext = (ArbWriteFile_Context*)pVoidContext;
2187    int                   nPercent = (int)(100.0 * nItem / nItems);
2188
2189    if (!bIsComment) {
2190        if (pContext->nLastPercent != nPercent) {
2191            pContext->nLastPercent = nPercent;
2192            pContext->pProgress->inc();
2193        }
2194    }
2195
2196    if (pContext->pFile != 0) {
2197        // Update status - put after matching cause matching writes its own status messages
2198        fprintf(pContext->pFile, "%s\n", pResult);
2199    }
2200
2201    return pContext->pProgress->aborted();
2202}
2203
2204// ----------------------------------------------------------------------------
2205
2206static void probe_match_with_specificity_edit_event() {
2207    AW_edit(get_results_manager().resultsFileName(), 0, 0, 0);
2208}
2209
2210// ----------------------------------------------------------------------------
2211
2212class GetMatchesContext { // @@@ merge with ProbeCollDisplay?
2213    float mismatchThreshold;
2214    int   nProbes;
2215
2216    typedef ArbMatchResultPtrByStringMultiMap          MatchMap;
2217    typedef ArbMatchResultPtrByStringMultiMapConstIter MatchMapIter;
2218
2219    const MatchMap& results;
2220public:
2221    GetMatchesContext(float misThres, int numProbes)
2222        : mismatchThreshold(misThres),
2223          nProbes(numProbes),
2224          results(get_results_manager().resultsMap())
2225    {}
2226
2227    void detect(std::string speciesName, NodeMarkers& matches) const {
2228        std::pair<MatchMapIter,MatchMapIter> iter = results.equal_range(speciesName);
2229
2230        for (; iter.first != iter.second ; ++iter.first) {
2231            const ArbMatchResult *pMatchResult = iter.first->second;
2232            if (pMatchResult->weight() <= mismatchThreshold) {
2233                int nProbe = pMatchResult->index();
2234                matches.incMarker(nProbe);
2235            }
2236        }
2237        matches.incNodeSize();
2238    }
2239};
2240
2241static SmartPtr<GetMatchesContext> getMatchesContext;
2242
2243class ProbeCollDisplay : public MarkerDisplay {
2244    ArbProbe *find_probe(int markerIdx) const {
2245        const ArbProbePtrList&   rProbeList = get_probe_collection().probeList();
2246        ArbProbePtrListConstIter wanted     = rProbeList.begin();
2247
2248        if (markerIdx>=0 && markerIdx<int(rProbeList.size())) advance(wanted, markerIdx);
2249        else wanted = rProbeList.end();
2250
2251        return wanted == rProbeList.end() ? NULL : *wanted;
2252    }
2253
2254public:
2255    ProbeCollDisplay(int numProbes)
2256        : MarkerDisplay(numProbes)
2257    {}
2258
2259    // MarkerDisplay interface
2260    const char *get_marker_name(int markerIdx) const OVERRIDE {
2261        ArbProbe *probe = find_probe(markerIdx);
2262        return probe ? probe->name().c_str() : GBS_global_string("<invalid probeindex %i>", markerIdx);
2263    }
2264    void retrieve_marker_state(const char *speciesName, NodeMarkers& matches) OVERRIDE {
2265        getMatchesContext->detect(speciesName, matches);
2266    }
2267
2268    void handle_click(int markerIdx, AW_MouseButton, AWT_graphic_exports&) OVERRIDE {
2269        // select probe in selection list
2270        ArbProbe *probe = find_probe(markerIdx);
2271        if (probe) AW_root::SINGLETON->awar(AWAR_PC_SELECTED_PROBE)->write_string(probe->sequence().c_str());
2272#if defined(DEBUG)
2273        else fprintf(stderr, "ProbeCollDisplay::handle_click: no probe found for markerIdx=%i\n", markerIdx);
2274#endif
2275    }
2276};
2277
2278inline bool displays_probeColl_markers(MarkerDisplay *md) { return dynamic_cast<ProbeCollDisplay*>(md); }
2279
2280static void refresh_matchedProbesDisplay_cb(AW_root *root, TREE_canvas *ntw) {
2281    // setup parameters for display of probe collection matches and trigger tree refresh
2282    LocallyModify<bool> flag(allow_probe_match_event, false);
2283
2284    AWT_graphic_tree *agt     = DOWNCAST(AWT_graphic_tree*, ntw->gfx);
2285    bool              display = get_results_manager().hasResults();
2286
2287    MarkerDisplay *markerDisplay = agt->get_marker_display();
2288    bool           redraw        = false;
2289    if (display) {
2290        size_t probesCount = get_probe_collection().probeList().size();
2291        getMatchesContext  = new GetMatchesContext(root->awar(AWAR_PC_MISMATCH_THRESHOLD)->read_float(), probesCount);
2292
2293        if (displays_probeColl_markers(markerDisplay) && probesCount == size_t(markerDisplay->size())) {
2294            markerDisplay->flush_cache();
2295        }
2296        else {
2297            agt->set_marker_display(new ProbeCollDisplay(get_probe_collection().probeList().size()));
2298        }
2299        redraw = true;
2300    }
2301    else {
2302        if (displays_probeColl_markers(markerDisplay)) {
2303            agt->hide_marker_display();
2304            redraw = true;
2305        }
2306    }
2307
2308    if (redraw) root->awar(AWAR_TREE_REFRESH)->touch();
2309}
2310
2311// ----------------------------------------------------------------------------
2312
2313static void probe_match_with_specificity_event(AW_root *root, TREE_canvas *ntw) {
2314    if (allow_probe_match_event) {
2315        GB_ERROR error = NULL;
2316
2317        ProbeMatchSettings matchSettings(root);
2318        matchSettings.markHits  = 0;
2319        matchSettings.write2tmp = 0;
2320
2321        ArbMatchResultsManager& g_results_manager = get_results_manager();
2322        g_results_manager.reset();
2323
2324        // Using g_probe_collection instance of ArbProbeCollection, need to loop
2325        // through all the probes in the collect and then call probe_match_event,
2326        // collating the results as we go. The results can be obtained from the
2327        // g_spd->probeSeq list.
2328        const ArbProbePtrList& rProbeList = get_probe_collection().probeList();
2329
2330        const int nItems      = rProbeList.size();
2331        int       nItem       = 1;
2332        int       nHits       = 0;
2333        int       nProbeIndex = 0;
2334
2335        // This extra scope needed to ensure the arb_progress object is released
2336        // prior to the next one being used to show progress on write results to file
2337        {
2338            arb_progress progress("Matching probe collection", nItems);
2339
2340            ArbProbeCollection& g_probe_collection = get_probe_collection();
2341
2342            for (ArbProbePtrListConstIter ProbeIter = rProbeList.begin() ; ProbeIter != rProbeList.end() && !error; ++ProbeIter) {
2343                const ArbProbe *pProbe = *ProbeIter;
2344
2345                if (pProbe != 0) {
2346                    int                nMatches;
2347                    ArbMatchResultSet *pResultSet = g_results_manager.addResultSet(pProbe);
2348
2349                    // Update status - put after matching cause matching writes its own status messages
2350                    progress.subtitle(GBS_global_string("Matching probe %i of %i", nItem, nItems));
2351
2352                    // Perform match on pProbe
2353                    matchSettings.targetString  = pProbe->sequence();
2354                    matchSettings.maxMismatches = pProbe->allowedMismatches();
2355
2356                    int                   counter = -1;
2357                    ProbeMatchEventParam  match_event(ntw->gb_main, &counter);
2358
2359                    error = probe_match_event(matchSettings, &match_event);
2360
2361                    pResultSet->initialise(pProbe, nProbeIndex);
2362                    nProbeIndex++;
2363
2364                    if (g_spd && g_spd->getHeadline() && !error) {
2365                        ProbeMatchParser parser(pProbe->sequence().c_str(), g_spd->getHeadline());
2366
2367                        if (parser.get_error()) {
2368                            error = parser.get_error();
2369                        }
2370                        else {
2371                            int nStartFullName  = 0;
2372                            int nEndFullName    = 0;
2373
2374                            parser.getColumnRange("fullname", &nStartFullName, &nEndFullName);
2375
2376                            pResultSet->headline(g_spd->getHeadline(), nEndFullName);
2377
2378                            // Collate match results
2379                            nMatches = g_spd->probeSeq.size();
2380
2381                            for (int cn = 0 ; cn < nMatches && !error ; cn++) {
2382                                const std::string& sResult = g_spd->probeSeq[cn];
2383
2384                                ParsedProbeMatch parsed(sResult.c_str(), parser);
2385
2386                                if (parsed.get_error()) {
2387                                    error = parsed.get_error();
2388                                }
2389                                else {
2390                                    char       *pName      = parsed.get_column_content("name", true);
2391                                    char       *pFullName  = parsed.get_column_content("fullname", true);
2392                                    const char *pMatchPart = parsed.get_probe_region();
2393
2394                                    pResultSet->add(pName,
2395                                                    pFullName,
2396                                                    pMatchPart,
2397                                                    sResult.c_str(),
2398                                                    g_probe_collection.matchWeighting());
2399
2400                                    free(pName);
2401                                    free(pFullName);
2402                                }
2403                            }
2404                        }
2405                    }
2406
2407                    if (error) error = GBS_global_string("while matching probe #%i: %s", nItem, error);
2408
2409                    nItem++;
2410                }
2411                progress.inc_and_check_user_abort(error);
2412            }
2413        }
2414
2415        if (!error) {
2416            ArbWriteFile_Context Context;
2417
2418            arb_progress progress("Writing results to file", 100);
2419            progress.subtitle(g_results_manager.resultsFileName());
2420
2421            Context.pFile         = fopen(g_results_manager.resultsFileName(), "w");
2422            Context.pProgress     = &progress;
2423            Context.nLastPercent  = 0;
2424
2425            nHits = g_results_manager.enumerate_results(probe_match_with_specificity_enum_callback, (void*)&Context);
2426
2427            fclose(Context.pFile);
2428
2429            if (!error) error = progress.error_if_aborted();
2430            progress.done();
2431        }
2432
2433        root->awar(AWAR_PC_MATCH_NHITS)->write_int(nHits);
2434
2435        if (error) {
2436            aw_message(error);
2437
2438            // Clear the results set
2439            g_results_manager.reset();
2440        }
2441        else {
2442            // Open the Probe Match Specificity dialog to interactively show how the
2443            // matches target the phylongeny
2444            create_probe_match_specificity_control_window(root)->show();
2445
2446            double oldMaxWeight = g_results_manager.maximumWeight();
2447            g_results_manager.updateResults();
2448            double newMaxWeight = g_results_manager.maximumWeight();
2449
2450            if (newMaxWeight != oldMaxWeight) {
2451                // set new limits for scaler and force current value into limits
2452                newMaxWeight = std::max(newMaxWeight, 0.000001); // avoid invalid limits
2453                root->awar(AWAR_PC_MISMATCH_THRESHOLD)->set_minmax(0.0, newMaxWeight)->touch();
2454            }
2455        }
2456
2457        refresh_matchedProbesDisplay_cb(root, ntw);
2458    }
2459}
2460
2461static void auto_match_cb(AW_root *root, TREE_canvas *ntw) {
2462    if (root->awar(AWAR_PC_AUTO_MATCH)->read_int()) {
2463        probe_match_with_specificity_event(root, ntw);
2464    }
2465}
2466static void trigger_auto_match(AW_root *root) {
2467    root->awar(AWAR_PC_AUTO_MATCH)->touch();
2468}
2469
2470// ----------------------------------------------------------------------------
2471
2472static void probe_forget_matches_event(AW_window *aww, ArbPM_Context *pContext) {
2473    AW_root *root = aww->get_root();
2474
2475    get_results_manager().reset();
2476    root->awar(AWAR_PC_MATCH_NHITS)->write_int(0);
2477    refresh_matchedProbesDisplay_cb(root, pContext->ntw);
2478}
2479
2480static void selected_probe_changed_cb(AW_root *root) {
2481    char *pSequence = root->awar(AWAR_PC_SELECTED_PROBE)->read_string();
2482    if (pSequence) {
2483        const ArbProbe *pProbe = get_probe_collection().find(pSequence);
2484        if (pProbe) {
2485            const char *seq = pProbe->sequence().c_str();
2486            root->awar(AWAR_PC_TARGET_STRING)->write_string(seq);
2487            root->awar(AWAR_PC_TARGET_NAME)  ->write_string(pProbe->name().c_str());
2488
2489            root->awar(AWAR_TARGET_STRING)->write_string(seq); // copy to probe match & match in edit4
2490        }
2491    }
2492    free(pSequence);
2493}
2494
2495static void target_string_changed_cb(AW_root *root) {
2496    char *pSequence = root->awar(AWAR_PC_TARGET_STRING)->read_string();
2497    if (pSequence && pSequence[0]) {
2498        const ArbProbe *pProbe = get_probe_collection().find(pSequence);
2499        if (pProbe) root->awar(AWAR_PC_SELECTED_PROBE)->write_string(pSequence);
2500    }
2501    free(pSequence);
2502}
2503
2504static void match_changed_cb(AW_root *root) {
2505    // automatically adapt to last probe matched
2506    // (also adapts to last selected designed probe)
2507    char *pSequence = root->awar(AWAR_TARGET_STRING)->read_string();
2508    if (pSequence && pSequence[0]) {
2509        AW_awar *awar_target = root->awar(AWAR_PC_TARGET_STRING);
2510        if (strcmp(awar_target->read_char_pntr(), pSequence) != 0) {
2511            root->awar(AWAR_PC_TARGET_NAME)->write_string(""); // clear name
2512        }
2513        awar_target->write_string(pSequence);
2514    }
2515    free(pSequence);
2516}
2517
2518// ----------------------------------------------------------------------------
2519
2520AW_window *create_probe_match_with_specificity_window(AW_root *root, TREE_canvas *ntw) {
2521    static AW_window_simple *aws = 0; // the one and only probeSpec window
2522
2523    if (!aws) {
2524        root->awar(AWAR_PC_MISMATCH_THRESHOLD)->add_callback(makeRootCallback(refresh_matchedProbesDisplay_cb, ntw));
2525
2526        aws = new AW_window_simple;
2527
2528        aws->init(root, "PROBE_MATCH_WITH_SPECIFICITY", "PROBE MATCH WITH SPECIFICITY");
2529
2530        aws->load_xfig("pd_match_with_specificity.fig");
2531
2532        aws->at("close");
2533        aws->callback(AW_POPDOWN);
2534        aws->create_button("CLOSE", "CLOSE", "C");
2535
2536        aws->callback(makeHelpCallback("probespec.hlp"));
2537        aws->at("help");
2538        aws->create_button("HELP", "HELP", "H");
2539
2540        AW_selection_list *probes_id;
2541
2542        aws->at("probes");
2543        probes_id = aws->create_selection_list(AWAR_PC_SELECTED_PROBE, 110, 10, false);
2544        probes_id->insert_default("", "");
2545
2546        PM_Context.probes_id = probes_id;
2547        PM_Context.ntw       = ntw;
2548
2549        aws->callback(makeWindowCallback(probe_match_with_specificity_edit_event));
2550        aws->at("results");
2551        aws->create_button("RESULTS", "RESULTS", "R");
2552
2553        aws->at("pt_server");
2554        awt_create_PTSERVER_selection_button(aws, AWAR_PT_SERVER);
2555
2556        aws->at("nhits");
2557        aws->create_button(0, AWAR_PC_MATCH_NHITS);
2558
2559        aws->callback(makeCreateWindowCallback(create_probe_collection_window, &PM_Context));
2560        aws->at("edit");
2561        aws->create_button("EDIT", "EDIT", "E");
2562
2563        aws->callback(RootAsWindowCallback::simple(probe_match_with_specificity_event, ntw));
2564        aws->at("match");
2565        aws->create_button("MATCH", "MATCH", "M");
2566
2567        aws->callback(makeWindowCallback(probe_forget_matches_event, &PM_Context));
2568        aws->at("forget");
2569        aws->create_button("FORGET", "FORGET", "F");
2570
2571        aws->at("auto");
2572        aws->label("Auto match");
2573        aws->create_toggle(AWAR_PC_AUTO_MATCH);
2574
2575        AW_awar *awar_automatch = root->awar(AWAR_PC_AUTO_MATCH);
2576        awar_automatch->add_callback(makeRootCallback(auto_match_cb, ntw));
2577
2578        aws->callback(makeCreateWindowCallback(create_probe_match_specificity_control_window));
2579        aws->at("control");
2580        aws->create_autosize_button("CONTROL", "Display control", "D");
2581
2582        probe_match_update_probe_list(&PM_Context);
2583
2584        root->awar(AWAR_PC_SELECTED_PROBE)->add_callback(makeRootCallback(selected_probe_changed_cb));
2585        root->awar(AWAR_PC_TARGET_STRING)->add_callback(makeRootCallback(target_string_changed_cb));
2586        root->awar(AWAR_TARGET_STRING)->add_callback(makeRootCallback(match_changed_cb));
2587
2588        awar_automatch->touch(); // automatically run match if 'auto-match' is checked at startup
2589    }
2590
2591    return aws;
2592}
2593
2594// ----------------------------------------------------------------------------
2595
2596struct ArbPC_Context {
2597    AW_selection_list *selection_id;
2598    ArbPM_Context     *PM_Context;
2599
2600    ArbPC_Context()
2601        : selection_id(NULL),
2602          PM_Context(NULL)
2603    {}
2604};
2605
2606static ArbPC_Context PC_Context;
2607
2608// ----------------------------------------------------------------------------
2609
2610static void save_probe_list_to_DB(const ArbProbePtrList& rProbeList, AW_root *root) {
2611    std::string saved;
2612    for (ArbProbePtrListConstIter ProbeIter = rProbeList.begin() ; ProbeIter != rProbeList.end() ; ++ProbeIter) {
2613        const ArbProbe *pProbe = *ProbeIter;
2614        if (pProbe) {
2615            // Note: target sequences/names do not contain '#'/':' (see REPLACE_TARGET_CONTROL_CHARS)
2616            saved = saved + '#'+pProbe->name()+':'+pProbe->sequence();
2617        }
2618    }
2619
2620    root->awar(AWAR_PC_CURRENT_COLLECTION)->write_string(saved.empty() ? "" : saved.c_str()+1);
2621}
2622
2623static void show_probes_in_sellist(const ArbProbePtrList& rProbeList, AW_selection_list *sellist) {
2624    sellist->clear();
2625    sellist->insert_default("", "");
2626    for (ArbProbePtrListConstIter ProbeIter = rProbeList.begin() ; ProbeIter != rProbeList.end() ; ++ProbeIter) {
2627        const ArbProbe *pProbe = *ProbeIter;
2628        if (pProbe) {
2629            sellist->insert(pProbe->displayName().c_str(), pProbe->sequence().c_str());
2630        }
2631    }
2632    sellist->update();
2633}
2634
2635static void load_probe_collection(AW_window *aww, ArbPC_Context *Context, const char * const *awar_filename) {
2636    char *pFileName = aww->get_root()->awar(*awar_filename)->read_string();
2637
2638    ArbProbeCollection ProbeCollection;
2639    std::string        sErrorMessage;
2640
2641    if (ProbeCollection.openXML(pFileName, sErrorMessage)) {
2642        int    cn;
2643        char   buffer[256] = {0};
2644        float  weights[16] = {0.0};
2645        float  dWidth      = 1.0;
2646        float  dBias       = 0.0;
2647
2648        ArbProbeCollection& g_probe_collection = get_probe_collection();
2649        g_probe_collection = ProbeCollection;
2650
2651        g_probe_collection.getParameters(weights, dWidth, dBias);
2652
2653        AW_root *root = AW_root::SINGLETON;
2654        root->awar(AWAR_PC_MATCH_WIDTH)->write_float(dWidth);
2655        root->awar(AWAR_PC_MATCH_BIAS)->write_float(dBias);
2656
2657        for (cn = 0; cn < 16 ; cn++) {
2658            sprintf(buffer, AWAR_PC_MATCH_WEIGHTS"%i", cn);
2659
2660            root->awar(buffer)->write_float(weights[cn]);
2661        }
2662
2663        const ArbProbePtrList& rProbeList = g_probe_collection.probeList();
2664
2665        save_probe_list_to_DB(rProbeList, root);
2666        show_probes_in_sellist(rProbeList, Context->selection_id);
2667
2668        probe_match_update_probe_list(Context->PM_Context);
2669        aww->hide();
2670        trigger_auto_match(root);
2671    }
2672    else {
2673        // Print error message
2674        aw_message(sErrorMessage.c_str());
2675    }
2676
2677    free(pFileName);
2678}
2679
2680// ----------------------------------------------------------------------------
2681
2682static void probe_collection_update_parameters() {
2683    AW_root *root = AW_root::SINGLETON;
2684
2685    int cn;
2686    char buffer[256] = {0};
2687
2688    float weights[16] = {0.0};
2689    float dWidth = root->awar(AWAR_PC_MATCH_WIDTH)->read_float();
2690    float dBias  = root->awar(AWAR_PC_MATCH_BIAS)->read_float();
2691
2692    for (cn = 0; cn < 16 ; cn++) {
2693        sprintf(buffer, AWAR_PC_MATCH_WEIGHTS"%i", cn);
2694
2695        weights[cn] = root->awar(buffer)->read_float();
2696    }
2697
2698    ArbProbeCollection& g_probe_collection = get_probe_collection();
2699    g_probe_collection.setParameters(weights, dWidth, dBias);
2700
2701    save_probe_list_to_DB(g_probe_collection.probeList(), root);
2702}
2703
2704// ----------------------------------------------------------------------------
2705
2706static void save_probe_collection(AW_window *aww, const char * const *awar_filename) {
2707    char *pFileName = aww->get_root()->awar(*awar_filename)->read_string();
2708
2709    struct stat   FileStatus;
2710    int           nResult = ::stat(pFileName, &FileStatus);
2711    bool          bWrite  = true;
2712
2713    if (nResult == 0) {
2714        bWrite = (aw_question("probe_collection_save", "File already exists. Overwrite?", "YES,NO") == 0);
2715    }
2716
2717    if (bWrite) {
2718        probe_collection_update_parameters();
2719        get_probe_collection().saveXML(pFileName);
2720
2721        aww->hide();
2722    }
2723
2724    free(pFileName);
2725}
2726
2727// ----------------------------------------------------------------------------
2728
2729static void add_probe_to_collection_event(AW_window *aww, ArbPC_Context *pContext) {
2730    AW_selection_list *selection_id = pContext->selection_id;
2731    if (selection_id) {
2732        AW_root *root      = aww->get_root();
2733        char    *pSequence = root->awar(AWAR_PC_TARGET_STRING)->read_string();
2734        char    *pName     = root->awar(AWAR_PC_TARGET_NAME)->read_string();
2735
2736        GB_ERROR error = NULL;
2737        if (!pSequence || !pSequence[0]) {
2738            error = "Please enter a target string";
2739        }
2740        else if (get_probe_collection().find(pSequence)) {
2741            error = "Target string already in collection";
2742        }
2743
2744        if (!error) {
2745            const ArbProbe *pProbe = 0;
2746
2747            if (get_probe_collection().add(pName, pSequence, &pProbe) && (pProbe != 0)) {
2748                selection_id->insert(pProbe->displayName().c_str(), pSequence);
2749                selection_id->update();
2750                probe_match_update_probe_list(pContext->PM_Context);
2751                probe_collection_update_parameters();
2752
2753                root->awar(AWAR_PC_SELECTED_PROBE)->write_string(pSequence);
2754                trigger_auto_match(root);
2755            }
2756            else {
2757                error = "failed to add probe";
2758            }
2759        }
2760
2761        aw_message_if(error);
2762
2763        free(pName);
2764        free(pSequence);
2765    }
2766}
2767
2768static void modify_probe_event(AW_window *aww, ArbPC_Context *pContext) {
2769    AW_selection_list *selection_id = pContext->selection_id;
2770    if (selection_id) {
2771        AW_root *root            = aww->get_root();
2772        AW_awar *awar_selected   = root->awar(AWAR_PC_SELECTED_PROBE);
2773        char    *oldSequence     = awar_selected->read_string();
2774        char    *pSequence       = root->awar(AWAR_PC_TARGET_STRING)->read_string();
2775        char    *pName           = root->awar(AWAR_PC_TARGET_NAME)->read_string();
2776        bool     sequenceChanged = false;
2777
2778        GB_ERROR error = NULL;
2779        if (!pSequence || !pSequence[0]) {
2780            error = "Please enter a target string";
2781        }
2782        else if (!oldSequence || !oldSequence[0]) {
2783            error = "Please select probe to modify";
2784        }
2785        else {
2786            sequenceChanged = strcmp(oldSequence, pSequence) != 0;
2787            if (sequenceChanged) { // sequence changed -> test vs duplicate
2788                if (get_probe_collection().find(pSequence)) {
2789                    error = "Target string already in collection";
2790                }
2791            }
2792        }
2793
2794        if (!error) {
2795            const ArbProbe *pProbe = 0;
2796
2797            if (get_probe_collection().replace(oldSequence, pName, pSequence, &pProbe) && (pProbe != 0)) {
2798                AW_selection_list_iterator entry(selection_id, selection_id->get_index_of_selected());
2799                entry.set_displayed(pProbe->displayName().c_str());
2800                entry.set_value(pSequence);
2801                selection_id->update();
2802
2803                probe_match_update_probe_list(pContext->PM_Context);
2804                probe_collection_update_parameters();
2805
2806                awar_selected->write_string(pSequence);
2807                trigger_auto_match(root);
2808            }
2809            else {
2810                error = "failed to replace probe";
2811            }
2812        }
2813
2814        aw_message_if(error);
2815
2816        free(pName);
2817        free(pSequence);
2818        free(oldSequence);
2819    }
2820}
2821
2822static void remove_probe_from_collection_event(AW_window *aww, ArbPC_Context *pContext) {
2823    AW_selection_list *selection_id = pContext->selection_id;
2824    if (selection_id) {
2825        AW_root *root      = aww->get_root();
2826        char    *pSequence = root->awar(AWAR_PC_SELECTED_PROBE)->read_string();
2827        if (pSequence) {
2828            const ArbProbe *pProbe = get_probe_collection().find(pSequence);
2829            if (pProbe) {
2830                int idx = selection_id->get_index_of_selected();
2831                selection_id->delete_element_at(idx);
2832
2833                if (selection_id->size() < 1) {
2834                    selection_id->insert_default("", "");
2835                }
2836                selection_id->update();
2837                selection_id->select_element_at(idx); // select next probe for deletion
2838
2839                get_probe_collection().remove(pSequence);
2840                probe_match_update_probe_list(pContext->PM_Context);
2841                probe_collection_update_parameters();
2842
2843                trigger_auto_match(root);
2844            }
2845            free(pSequence);
2846        }
2847    }
2848}
2849
2850// ----------------------------------------------------------------------------
2851
2852static AW_window *probe_collection_load_prompt(AW_root *root, ArbPC_Context *pContext) {
2853    static char *awar_name = NULL; // do not free, bound to callback
2854    return awt_create_load_box(root, "Load", "probe collection",
2855                               ".", "xpc", &awar_name,
2856                               makeWindowCallback(load_probe_collection, pContext, (const char*const*)&awar_name));
2857}
2858static AW_window *probe_collection_save_prompt(AW_root *root) {
2859    static char *awar_name = NULL; // do not free, bound to callback
2860    return awt_create_load_box(root, "Save", "probe collection",
2861                               ".", "xpc", &awar_name,
2862                               makeWindowCallback(save_probe_collection, (const char*const*)&awar_name));
2863}
2864
2865// ----------------------------------------------------------------------------
2866
2867static void probe_match_update_probe_list(ArbPM_Context *pContext) {
2868    if (pContext) {
2869        AW_selection_list *sellist = pContext->probes_id;
2870        if (sellist) show_probes_in_sellist(get_probe_collection().probeList(), sellist);
2871    }
2872}
2873static void clear_probe_collection_event(AW_window *aww, ArbPC_Context *pContext) {
2874    if (get_probe_collection().clear()) {
2875        probe_match_update_probe_list(pContext->PM_Context);
2876        show_probes_in_sellist(get_probe_collection().probeList(), pContext->selection_id);
2877        trigger_auto_match(aww->get_root());
2878    }
2879}
2880
2881// ----------------------------------------------------------------------------
2882
2883static void probe_collection_close(AW_window *aww) {
2884    probe_collection_update_parameters();
2885    aww->hide();
2886}
2887
2888// ----------------------------------------------------------------------------
2889
2890static AW_window *create_probe_collection_window(AW_root *root, ArbPM_Context *pContext) {
2891    static AW_window_simple *aws = 0;     // the one and only probe match window
2892    char                     buffer[256];
2893
2894    pd_assert(pContext);
2895    PC_Context.PM_Context = pContext;
2896
2897    if (!aws) {
2898        aws = new AW_window_simple;
2899
2900        aws->init(root, "PROBE_COLLECTION", "PROBE COLLECTION");
2901
2902        aws->load_xfig("pd_match_probe_collection.fig");
2903
2904        aws->callback(probe_collection_close);
2905        aws->at("close");
2906        aws->create_button("CLOSE", "CLOSE", "C");
2907
2908        aws->callback(makeHelpCallback("probespec.hlp"));
2909        aws->at("help");
2910        aws->create_button("HELP", "HELP", "H");
2911
2912        AW_selection_list *selection_id;
2913
2914        aws->at("probes");
2915        selection_id = aws->create_selection_list(AWAR_PC_SELECTED_PROBE, 110, 10, false);
2916        selection_id->insert_default("", "");
2917
2918        PC_Context.selection_id = selection_id;
2919
2920        aws->at("string");
2921        aws->create_input_field(AWAR_PC_TARGET_STRING, 32);
2922
2923        aws->at("name");
2924        aws->create_input_field(AWAR_PC_TARGET_NAME, 32);
2925
2926        aws->callback(makeWindowCallback(add_probe_to_collection_event, &PC_Context));
2927        aws->at("add");
2928        aws->create_button("ADD", "ADD", "A");
2929
2930        aws->callback(makeWindowCallback(modify_probe_event, &PC_Context));
2931        aws->at("modify");
2932        aws->create_button("MODIFY", "MODIFY", "M");
2933
2934        aws->callback(makeWindowCallback(remove_probe_from_collection_event, &PC_Context));
2935        aws->at("remove");
2936        aws->create_button("REMOVE", "REMOVE", "R");
2937
2938        aws->callback(makeCreateWindowCallback(probe_collection_load_prompt, &PC_Context));
2939        aws->at("open");
2940        aws->create_button("LOAD", "LOAD", "L");
2941
2942        aws->callback(makeCreateWindowCallback(probe_collection_save_prompt));
2943        aws->at("save");
2944        aws->create_button("SAVE", "SAVE", "S");
2945
2946        aws->callback(makeWindowCallback(clear_probe_collection_event, &PC_Context));
2947        aws->at("clear");
2948        aws->create_button("CLEAR", "CLEAR", "L");
2949
2950        for (int i = 0 ; i < 16 ; i++) {
2951            sprintf(buffer, "%i", i);
2952            aws->at(buffer);
2953            sprintf(buffer, AWAR_PC_MATCH_WEIGHTS"%i", i);
2954            aws->create_input_field(buffer, 4);
2955            root->awar(buffer)->add_callback(trigger_auto_match);
2956        }
2957
2958        aws->at("width");
2959        aws->create_input_field(AWAR_PC_MATCH_WIDTH, 5);
2960        root->awar(AWAR_PC_MATCH_WIDTH)->add_callback(trigger_auto_match);
2961
2962        aws->at("bias");
2963        aws->create_input_field(AWAR_PC_MATCH_BIAS, 5);
2964        root->awar(AWAR_PC_MATCH_BIAS)->add_callback(trigger_auto_match);
2965
2966        show_probes_in_sellist(get_probe_collection().probeList(), selection_id);
2967    }
2968    return aws;
2969}
Note: See TracBrowser for help on using the repository browser.