source: tags/arb-6.0/DIST/DI_clusters.cxx

Last change on this file was 12267, checked in by westram, 10 years ago
File size: 38.5 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : DI_clusters.cxx                                   //
4//   Purpose   : Detect clusters of homologous sequences in tree   //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in October 2009   //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include "di_clusters.hxx"
13#include "di_clustertree.hxx"
14#include "di_foundclusters.hxx"
15#include "di_awars.hxx"
16
17#include <AP_filter.hxx>
18#include <AP_seq_protein.hxx>
19#include <AP_seq_dna.hxx>
20
21#include <awt_sel_boxes.hxx>
22
23#include <aw_awars.hxx>
24#include <aw_msg.hxx>
25#include <arb_progress.h>
26#include <aw_root.hxx>
27
28#include <list>
29
30using namespace std;
31
32#define di_assert(cond) arb_assert(cond)
33
34// --------------
35//      awars
36
37#define AWAR_CLUSTER_PREFIX      AWAR_DIST_PREFIX "cluster/"
38#define AWAR_CLUSTER_PREFIX_TEMP "/tmp/" AWAR_DIST_PREFIX
39
40#define AWAR_CLUSTER_MAXDIST   AWAR_CLUSTER_PREFIX "maxdist"
41#define AWAR_CLUSTER_MINSIZE   AWAR_CLUSTER_PREFIX "minsize"
42#define AWAR_CLUSTER_AUTOMARK  AWAR_CLUSTER_PREFIX "automark"
43#define AWAR_CLUSTER_MARKREP   AWAR_CLUSTER_PREFIX "markrep"
44#define AWAR_CLUSTER_SELECTREP AWAR_CLUSTER_PREFIX "selrep"
45#define AWAR_CLUSTER_ORDER     AWAR_CLUSTER_PREFIX "order"
46
47#define AWAR_CLUSTER_GRP_PREFIX     AWAR_CLUSTER_PREFIX "group/"
48#define AWAR_CLUSTER_GRP_PREFIX_TMP "/tmp/" AWAR_CLUSTER_GRP_PREFIX
49
50#define AWAR_CLUSTER_GROUP_WHAT         AWAR_CLUSTER_GRP_PREFIX "all"
51#define AWAR_CLUSTER_GROUP_EXISTING     AWAR_CLUSTER_GRP_PREFIX "existing"
52#define AWAR_CLUSTER_GROUP_NOTFOUND     AWAR_CLUSTER_GRP_PREFIX "notfound"
53#define AWAR_CLUSTER_GROUP_IDENTITY     AWAR_CLUSTER_GRP_PREFIX "identity"
54#define AWAR_CLUSTER_GROUP_PREFIX       AWAR_CLUSTER_GRP_PREFIX "prefix"
55#define AWAR_CLUSTER_GROUP_PREFIX_MATCH AWAR_CLUSTER_GRP_PREFIX "prefix_match"
56#define AWAR_CLUSTER_GROUP_SUFFIX       AWAR_CLUSTER_GRP_PREFIX "suffix"
57#define AWAR_CLUSTER_GROUP_EXAMPLE      AWAR_CLUSTER_GRP_PREFIX_TMP "example"
58
59#define AWAR_CLUSTER_SELECTED      AWAR_CLUSTER_PREFIX_TEMP "selected" // ID of currently selected cluster (or zero)
60#define AWAR_CLUSTER_RESTORE_LABEL AWAR_CLUSTER_PREFIX_TEMP "rlabel" // label of restore button
61
62enum Group_What {
63    GROUP_SELECTED,
64    GROUP_LISTED,
65};
66
67enum Group_Action {
68    GROUP_CREATE,
69    GROUP_DELETE,
70};
71
72enum Group_NotFound {
73    NOTFOUND_ABORT,
74    NOTFOUND_WARN,
75    NOTFOUND_IGNORE,
76};
77
78enum Group_Existing {
79    EXISTING_GROUP_ABORT,
80    EXISTING_GROUP_SKIP,
81    EXISTING_GROUP_OVERWRITE,
82    EXISTING_GROUP_APPEND_ORG,
83};
84
85// ----------------------------------------
86
87enum AffectedClusters { ALL_CLUSTERS, SEL_CLUSTER };
88
89static ClustersData *global_data = 0;
90
91// ------------------------
92
93static void di_forget_global_data(AW_window *) {
94    di_assert(global_data);
95    global_data->free();
96    // do not delete 'global_data' itself, it will be reused when window is opened again
97}
98
99// ------------------------
100//      Update contents
101
102static void update_cluster_sellist() {
103    global_data->update_cluster_selection_list();
104    // @@@ update result info line
105}
106static void update_restore_label(AW_window *aww) {
107    AW_root    *aw_root = aww->get_root();
108    AW_awar    *awar    = aw_root->awar(AWAR_CLUSTER_RESTORE_LABEL);
109    unsigned    size    = global_data->count(STORED_CLUSTERS);
110    const char *label   = size ? GBS_global_string("Stored: %u", size) : "None stored";
111
112    awar->write_string(label);
113}
114static void update_all(AW_window *aww) {
115    update_cluster_sellist();
116    update_restore_label(aww);
117}
118
119static void save_results_recursive(ClusterTree *subtree) {
120    if (subtree->get_state() == CS_IS_CLUSTER) {
121        global_data->add(new Cluster(subtree), SHOWN_CLUSTERS);
122    }
123    if (!subtree->is_leaf) {
124        save_results_recursive(subtree->get_leftson());
125        save_results_recursive(subtree->get_rightson());
126    }
127}
128static void save_results(ClusterTreeRoot *tree) {
129    global_data->clear(SHOWN_CLUSTERS);
130    save_results_recursive(tree->get_root_node());
131    update_cluster_sellist();
132}
133
134static void calculate_clusters(AW_window *aww) {
135    GBDATA   *gb_main = global_data->get_gb_main();
136    GB_ERROR  error   = NULL;
137
138    arb_progress progress("Detecting clusters");
139
140    // calculate ClusterTree
141    ClusterTreeRoot *tree = NULL;
142    {
143        GB_transaction  ta(gb_main);
144        AW_root        *aw_root = aww->get_root();
145
146        {
147            char    *use     = aw_root->awar(AWAR_DIST_ALIGNMENT)->read_string();
148            AliView *aliview = global_data->weighted_filter.create_aliview(use, error);
149
150            if (aliview) {
151                AP_sequence *seq = GBT_is_alignment_protein(gb_main, use)
152                    ? (AP_sequence*)new AP_sequence_protein(aliview)
153                    : new AP_sequence_parsimony(aliview);
154
155                AP_FLOAT maxDistance    = aw_root->awar(AWAR_CLUSTER_MAXDIST)->read_float();
156                unsigned minClusterSize = aw_root->awar(AWAR_CLUSTER_MINSIZE)->read_int();
157
158                tree = new ClusterTreeRoot(aliview, seq, maxDistance/100, minClusterSize);
159
160                delete seq;
161            }
162            free(use);
163        }
164
165        if (!error) {
166            progress.subtitle("Loading tree");
167            {
168                char *tree_name = aw_root->awar(AWAR_DIST_TREE_CURR_NAME)->read_string();
169                error           = tree->loadFromDB(tree_name);
170                free(tree_name);
171            }
172
173            if (!error) error = tree->linkToDB(0, 0);
174        }
175    }
176
177    if (!error) {
178        error = tree->find_clusters();
179        if (!error) save_results(tree);
180    }
181
182    delete tree;
183
184    if (error) aw_message(error);
185}
186
187
188static int with_affected_clusters_do(AW_root *aw_root, AffectedClusters affected, bool warn_if_none_affected, AW_CL cd, void (*fun)(ClusterPtr, AW_CL)) {
189    // returns number of affected clusters
190    int affCount = 0;
191    if (affected == SEL_CLUSTER) {
192        AW_awar *awar  = aw_root->awar(AWAR_CLUSTER_SELECTED);
193        ID       selID = awar->read_int();
194
195        if (selID) {
196            ClusterPtr selCluster = global_data->clusterWithID(selID);
197            cl_assert(!selCluster.isNull());
198            fun(selCluster, cd);
199            affCount++;
200        }
201        else if (warn_if_none_affected) {
202            aw_message("No cluster is selected");
203        }
204    }
205    else {
206        unsigned shown = global_data->count(SHOWN_CLUSTERS);
207        if (shown) {
208            ClusterIDs     clusters(global_data->get_clusterIDs(SHOWN_CLUSTERS)); // intended copy!
209            ClusterIDsIter cli_end = clusters.end();
210            for (ClusterIDsIter cli = clusters.begin(); cli != cli_end; ++cli) {
211                ClusterPtr cluster = global_data->clusterWithID(*cli);
212                fun(cluster, cd);
213                affCount++;
214            }
215        }
216        else if (warn_if_none_affected) {
217            aw_message("There are no clusters in the list");
218        }
219    }
220    return affCount;
221}
222
223// -------------
224//      mark
225
226void Cluster::mark_all_members(bool mark_representative) const {
227    DBItemSetIter sp_end = members.end();
228    for (DBItemSetIter sp = members.begin(); sp != sp_end; ++sp) {
229        if (mark_representative || (*sp != representative)) {
230            GB_write_flag(*sp, 1);
231        }
232    }
233}
234
235enum {
236    MARK_REPRES   = 1,
237    SELECT_REPRES = 2,
238};
239
240static void mark_cluster(ClusterPtr cluster, AW_CL cl_mask) {
241    cluster->mark_all_members(cl_mask & MARK_REPRES);
242    if (cl_mask & SELECT_REPRES) {
243        GBDATA     *gb_species = cluster->get_representative();
244        const char *name       = GBT_get_name(gb_species);
245
246        global_data->get_aw_root()->awar(AWAR_SPECIES_NAME)->write_string(name);
247    }
248}
249static void mark_clusters(AW_window *, AffectedClusters affected, bool warn_if_none_affected) {
250    AW_root *aw_root = global_data->get_aw_root();
251    GBDATA  *gb_main = global_data->get_gb_main();
252
253    GB_transaction ta(gb_main);
254
255    bool  markRep = aw_root->awar(AWAR_CLUSTER_MARKREP)->read_int();
256    bool  selRep  = aw_root->awar(AWAR_CLUSTER_SELECTREP)->read_int();
257    AW_CL mask    = markRep * MARK_REPRES + selRep * SELECT_REPRES;
258
259    GBT_mark_all(gb_main, 0);                       // unmark all
260    int marked = with_affected_clusters_do(aw_root, affected, warn_if_none_affected, mask, mark_cluster);
261    if (!marked || !selRep) {
262        // nothing marked -> force tree refresh
263        if (selRep) {
264            aw_root->awar(AWAR_SPECIES_NAME)->write_string("");
265        }
266        else {
267            aw_root->awar(AWAR_TREE_REFRESH)->touch(); // @@ hm - no effect?!
268        }
269    }
270}
271
272static void select_cluster_cb(AW_root *aw_root) {
273    bool auto_mark = aw_root->awar(AWAR_CLUSTER_AUTOMARK)->read_int();
274    if (auto_mark) mark_clusters(NULL, SEL_CLUSTER, false);
275}
276
277static void select_cluster(ID id) {
278    global_data->get_aw_root()->awar(AWAR_CLUSTER_SELECTED)->write_int(id);
279}
280
281
282// -------------------
283//      Sort order
284
285
286static void sort_order_changed_cb(AW_root *aw_root) {
287    ClusterOrder order = (ClusterOrder)aw_root->awar(AWAR_CLUSTER_ORDER)->read_int();
288    global_data->changeSortOrder(order);
289    update_cluster_sellist();
290}
291
292// --------------
293//      group
294
295class GroupTree;
296typedef map<string, GroupTree*> Species2Tip;
297
298class GroupTree : public ARB_countedTree {
299    unsigned leaf_count;   // total number of leafs in subtree
300    unsigned tagged_count; // tagged leafs
301
302    void update_tag_counters();
303public:
304
305    explicit GroupTree(ARB_seqtree_root *root)
306        : ARB_countedTree(root),
307          leaf_count(0),
308          tagged_count(0)
309    {}
310
311    // ARB_countedTree interface
312    virtual void init_tree() OVERRIDE { update_leaf_counters(); }
313    // ARB_countedTree interface end
314
315    DEFINE_TREE_RELATIVES_ACCESSORS(GroupTree);
316
317    void map_species2tip(Species2Tip& mapping);
318
319    unsigned get_leaf_count() const OVERRIDE { return leaf_count; }
320    unsigned update_leaf_counters();
321
322    void tag_leaf() {
323        di_assert(is_leaf);
324        tagged_count = 1;
325        get_father()->update_tag_counters();
326    }
327    unsigned get_tagged_count() const { return tagged_count; }
328    void clear_tags();
329
330    double tagged_rate() const { return double(get_tagged_count())/get_leaf_count(); }
331};
332
333struct GroupTreeNodeFactory : public RootedTreeNodeFactory {
334    virtual RootedTree *makeNode(TreeRoot *root) const {
335        return new GroupTree(DOWNCAST(ARB_seqtree_root*, root));
336    }
337};
338
339unsigned GroupTree::update_leaf_counters() {
340    if (is_leaf) leaf_count = 1;
341    else leaf_count = get_leftson()->update_leaf_counters() + get_rightson()->update_leaf_counters();
342    return leaf_count;
343}
344
345void GroupTree::clear_tags() {
346    if (!is_leaf && tagged_count) {
347        get_leftson()->clear_tags();
348        get_rightson()->clear_tags();
349    }
350    tagged_count = 0;
351}
352
353void GroupTree::map_species2tip(Species2Tip& mapping) {
354    if (is_leaf) {
355        if (name) mapping[name] = this;
356    }
357    else {
358        get_leftson()->map_species2tip(mapping);
359        get_rightson()->map_species2tip(mapping);
360    }
361}
362
363void GroupTree::update_tag_counters() {
364    di_assert(!is_leaf);
365    GroupTree *node = this;
366    while (node) {
367        node->tagged_count = node->get_leftson()->get_tagged_count() + node->get_rightson()->get_tagged_count();
368        node               = node->get_father();
369    }
370}
371
372struct GroupChanges {
373    unsigned created;
374    unsigned skipped;
375    unsigned overwritten;
376    unsigned deleted;
377    unsigned restored;
378
379    void clear() { created = skipped = overwritten = deleted = restored = 0; }
380    bool exist() const { return created||overwritten||deleted||restored; }
381
382    void show_message() {
383        string msg;
384
385        if (created)     msg += GBS_global_string("%u created  ",     created);
386        if (overwritten) msg += GBS_global_string("%u overwritten  ", overwritten);
387        if (skipped)     msg += GBS_global_string("%u skipped  ",     skipped);
388        if (deleted)     msg += GBS_global_string("%u deleted  ",     deleted);
389        if (restored)    msg += GBS_global_string("%u restored  ",    restored);
390
391        if (!msg.empty()) {
392            msg = string("Group changes: ")+msg;
393            aw_message(msg.c_str());
394        }
395    }
396   
397    GroupChanges() { clear(); }
398};
399
400
401// ---------------------
402//      GroupBuilder
403
404typedef map<GroupTree*, ClusterPtr> Group2Cluster;
405
406class GroupBuilder : virtual Noncopyable {
407    GBDATA           *gb_main;
408    string            tree_name;
409    ARB_seqtree_root *tree_root;
410    Group_Action      action;                       // create or delete ?
411    Species2Tip       species2tip;                  // map speciesName -> leaf
412    ARB_ERROR         error;
413    ClusterPtr        bad_cluster;                  // error occurred here (is set)
414    Group_Existing    existing;
415    unsigned          existing_count;               // counts existing groups
416    Group_NotFound    notfound;
417    double            matchRatio;                   // needed identity of subtree and cluster
418    double            maxDist;                      // max. Distance used for calculation
419    string            cluster_prefix;               // prefix for cluster name
420    string            cluster_suffix_def;           // suffix-definition for cluster name
421    GroupChanges      changes;                      // count tree modifications
422    bool              del_match_prefixes;           // only delete groups, where prefix matches
423
424    GroupTree *find_group_position(GroupTree *subtree, unsigned cluster_size);
425
426public:
427    GroupBuilder(GBDATA *gb_main_, Group_Action action_)
428        : gb_main(gb_main_),
429          tree_root(NULL),
430          action(action_),
431          error(NULL),
432          existing_count(0)
433    {
434        AW_root *awr = global_data->get_aw_root();
435
436        tree_name          = awr->awar(AWAR_DIST_TREE_CURR_NAME)->read_char_pntr();
437        existing           = (Group_Existing)awr->awar(AWAR_CLUSTER_GROUP_EXISTING)->read_int();
438        notfound           = (Group_NotFound)awr->awar(AWAR_CLUSTER_GROUP_NOTFOUND)->read_int();
439        del_match_prefixes = awr->awar(AWAR_CLUSTER_GROUP_PREFIX_MATCH)->read_int();
440        matchRatio         = awr->awar(AWAR_CLUSTER_GROUP_IDENTITY)->read_int()/100.0;
441        maxDist            = awr->awar(AWAR_CLUSTER_MAXDIST)->read_float();
442        cluster_prefix     = awr->awar(AWAR_CLUSTER_GROUP_PREFIX)->read_char_pntr();
443        cluster_suffix_def = awr->awar(AWAR_CLUSTER_GROUP_SUFFIX)->read_char_pntr();
444    }
445    ~GroupBuilder() {
446        delete tree_root;
447    }
448
449    ARB_ERROR get_error() const { return error; }
450    ClusterPtr get_bad_cluster() const { return bad_cluster; }
451    Group_Existing with_existing() const { return existing; }
452    unsigned get_existing_count() const { return existing_count; }
453    double get_max_distance() const { return maxDist; }
454
455    ARB_ERROR save_modified_tree();
456    void      load_tree();
457
458    DEFINE_DOWNCAST_ACCESSORS(GroupTree, get_root_node, tree_root->get_root_node());
459
460    GroupTree *find_best_matching_subtree(ClusterPtr cluster);
461    void update_group(ClusterPtr cluster); // create or delete group for cluster
462    string generate_group_name(ClusterPtr cluster, const GroupTree *group_node);
463
464    bool matches_current_prefix(const char *groupname) const {
465        return strstr(groupname, cluster_prefix.c_str()) == groupname;
466    }
467
468    bool shall_delete_group(const char *name) const {
469        return !del_match_prefixes || matches_current_prefix(name);
470    }
471};
472
473void GroupBuilder::load_tree() {
474    di_assert(!tree_root);
475
476    tree_root = new ARB_seqtree_root(new AliView(gb_main), new GroupTreeNodeFactory, NULL, false);
477    error     = tree_root->loadFromDB(tree_name.c_str());
478
479    if (error) {
480        delete tree_root;
481        tree_root = NULL;
482    }
483    else {
484        changes.clear();
485
486        GroupTree *tree = get_root_node();
487        tree->update_leaf_counters();
488        tree->map_species2tip(species2tip);
489    }
490}
491ARB_ERROR GroupBuilder::save_modified_tree() {
492    di_assert(!error);
493    if (changes.exist()) {
494        di_assert(tree_root);
495        error = tree_root->saveToDB();
496
497        AW_root *awr = global_data->get_aw_root();
498        awr->awar(AWAR_TREE_REFRESH)->touch();
499
500        if (!error) {
501            changes.show_message();
502            changes.clear();
503        }
504    }
505    return error;
506}
507
508GroupTree *GroupBuilder::find_group_position(GroupTree *subtree, unsigned cluster_size) {
509    // searches for best group in subtree matching the cluster
510
511    GroupTree *groupPos = NULL;
512    if (subtree->get_tagged_count() == cluster_size) {
513        groupPos                = find_group_position(subtree->get_leftson(), cluster_size);
514        if (!groupPos) groupPos = find_group_position(subtree->get_rightson(), cluster_size);
515
516        if (!groupPos) {                            // consider 'subtree'
517            if (subtree->tagged_rate() >= matchRatio) {
518                groupPos = subtree;
519            }
520        }
521    }
522    return groupPos;
523}
524
525class HasntCurrentClusterPrefix : public ARB_tree_predicate {
526    const GroupBuilder& builder;
527public:
528    HasntCurrentClusterPrefix(const GroupBuilder& builder_) : builder(builder_) {}
529    bool selects(const ARB_seqtree& tree) const OVERRIDE {
530        const char *groupname        = tree.get_group_name();
531        bool        hasClusterPrefix = groupname && builder.matches_current_prefix(groupname);
532        return !hasClusterPrefix;
533    }
534};
535
536string concatenate_name_parts(const list<string>& namepart) {
537    string concat;
538    for (list<string>::const_iterator p = namepart.begin(); p != namepart.end(); ++p) {
539        if (!p->empty()) concat += '_'+*p;
540    }
541    return concat.erase(0, 1);
542}
543
544struct UseTreeRoot : public ARB_tree_predicate {
545    bool selects(const ARB_seqtree& tree) const OVERRIDE { return tree.is_root_node(); }
546};
547
548string GroupBuilder::generate_group_name(ClusterPtr cluster, const GroupTree *group_node) {
549    list<string> namepart;
550    namepart.push_back(cluster_prefix);
551
552    string  orgname_suffix;
553    if (existing == EXISTING_GROUP_APPEND_ORG) {
554        char *old_name = group_node->name;
555        if (old_name) {
556            char *original = originalGroupName(old_name);
557            if (!original && !matches_current_prefix(old_name)) {
558                original = strdup(old_name); // use existing name as original name
559            }
560            if (original) {
561                orgname_suffix = string(" {was:")+original+"}";
562                free(original);
563            }
564        }
565    }
566
567    string text;
568    for (int i = 0; cluster_suffix_def[i]; ++i) {
569        if (cluster_suffix_def[i] == '%') {
570            ++i;
571            if (cluster_suffix_def[i] == '%') {
572                text += '%';
573            }
574            else {
575                string expanded;
576                switch(cluster_suffix_def[i]) {
577                    case 'p': expanded = cluster->get_upgroup_info(group_node, HasntCurrentClusterPrefix(*this)); break;
578                    case 'P': expanded = cluster->get_upgroup_info(group_node, UseAnyTree()); break;
579                    case 't': expanded = cluster->get_upgroup_info(group_node, UseTreeRoot()); break;
580                    case 'd': expanded = GBS_global_string("~%.3f", cluster->get_mean_distance()); break;
581                    case 'D': expanded = GBS_global_string("<=%.1f%%", get_max_distance()); break;
582                    case 'e': expanded = group_node->name ? group_node->name : ""; break;
583                    case 'o': {
584                        int matchRate = int(group_node->tagged_rate()*100+0.5);
585                        if (matchRate<100) expanded = GBS_global_string("%i%%_of", matchRate);
586                        break;
587                    }
588                    default:
589                        text += '%';
590                        text += cluster_suffix_def[i];
591                        break;
592                }
593
594                if (!expanded.empty()) {
595                    namepart.push_back(text);
596                    text = "";
597                    namepart.push_back(expanded);
598                }
599            }
600        }
601        else {
602            text += cluster_suffix_def[i];
603        }
604    }
605    namepart.push_back(text);
606
607    return concatenate_name_parts(namepart)+orgname_suffix;
608}
609
610GroupTree *GroupBuilder::find_best_matching_subtree(ClusterPtr cluster) {
611    GroupTree *group_node = NULL;
612    if (!error) {
613        if (!tree_root) load_tree();
614        if (!error) {
615            const DBItemSet& members = cluster->get_members();
616
617            // mark cluster members in tree
618            {
619                GB_transaction ta(gb_main);
620                DBItemSetIter sp_end = members.end();
621                for (DBItemSetIter sp = members.begin(); sp != sp_end && !error; ++sp) {
622                    const char *name = GBT_get_name(*sp);
623                    di_assert(name);
624                    if (name) {
625                        Species2Tip::const_iterator found = species2tip.find(name);
626                        if (found == species2tip.end()) {
627                            error = GBS_global_string("Species '%s' is not in '%s'", name, tree_name.c_str());
628                        }
629                        else {
630                            GroupTree *leaf = found->second;
631                            leaf->tag_leaf();
632                        }
633                    }
634                }
635            }
636
637            if (!error) {
638                // top-down search for best matching node
639                group_node = find_group_position(get_root_node(), cluster->get_member_count());
640            }
641        }
642    }
643    return group_node;
644}
645
646void GroupBuilder::update_group(ClusterPtr cluster) {
647    if (!error) {
648        GroupTree *group_node = find_best_matching_subtree(cluster);
649        if (!error) {
650            if (!group_node) { // no matching subtree found
651                switch (notfound) {
652                    case NOTFOUND_WARN:
653                    case NOTFOUND_ABORT: {
654                        const char *msg = GBS_global_string("Could not find matching subtree for cluster '%s'", cluster->get_list_display(NULL));
655                        if (notfound == NOTFOUND_ABORT) error = msg;
656                        else aw_message(msg);
657                        break;
658                    }
659                    case NOTFOUND_IGNORE: break; // silently ignore
660                }
661            }
662            else { // found subtree for group
663                switch (action) {
664                    case GROUP_CREATE: {
665                        char *old_name = group_node->name;
666
667                        if (old_name && existing == EXISTING_GROUP_ABORT) {
668                            error = GBS_global_string("Existing group '%s' is in the way", old_name);
669                        }
670                        else {
671                            if (old_name && existing == EXISTING_GROUP_SKIP) {
672                                changes.skipped++;
673                            }
674                            else {
675                                string new_name = generate_group_name(cluster, group_node);
676
677                                if (old_name) changes.overwritten++; else changes.created++;
678
679                                free(old_name);
680                                group_node->name = strdup(new_name.c_str());
681
682                                // @@@ DRY that.. it's spread everywhere through libs :(
683                                if (!group_node->gb_node) {
684                                    GBDATA *gb_tree = group_node->get_tree_root()->get_gb_tree();
685                                    GB_transaction ta(gb_tree);
686                                    GBDATA *gb_node = GB_create_container(gb_tree, "node");
687                                    if (!gb_node) {
688                                        error = GB_await_error();
689                                    }
690                                    else {
691                                        error = GBT_write_int(gb_node, "id", 0);
692                                    }
693
694                                    if (!error) {
695                                        group_node->gb_node = gb_node;
696                                    }
697                                }
698                                if (group_node->gb_node && !error) {
699                                    GB_transaction ta(group_node->gb_node);
700                                    error = GBT_write_string(group_node->gb_node, "group_name", group_node->name);
701                                }
702
703                                cluster->update_description(group_node); // change list display
704                            }
705                        }
706                        break;
707                    }
708                    case GROUP_DELETE: {
709                        if (group_node->name && shall_delete_group(group_node->name)) {
710                            char *original = originalGroupName(group_node->name);
711
712                            if (original) {
713                                freeset(group_node->name, original); // restore original name
714                                if (group_node->gb_node) {
715                                    error = GBT_write_string(group_node->gb_node, "group_name", group_node->name);
716                                }
717                                changes.restored++;
718                            }
719                            else {
720                                freenull(group_node->name);
721                                group_node->gb_node = NULL; // forget ref to group data (@@@ need to delete group data from DB ? )
722                                changes.deleted++;
723                            }
724
725                            cluster->update_description(group_node); // change list display
726                        }
727                        break;
728                    }
729                }
730            }
731        }
732
733        if (error) bad_cluster = cluster;
734        get_root_node()->clear_tags();
735    }
736}
737
738static void update_example(AW_root *aw_root) {
739    ID     selID = aw_root->awar(AWAR_CLUSTER_SELECTED)->read_int();
740    string value;
741
742    if (selID) {
743        ClusterPtr selCluster = global_data->clusterWithID(selID);
744        cl_assert(!selCluster.isNull());
745
746        GroupBuilder  builder(global_data->get_gb_main(), GROUP_CREATE);
747        GroupTree    *group_node = builder.find_best_matching_subtree(selCluster);
748
749        GB_ERROR error = builder.get_error().deliver();
750       
751        if (error)           value = GBS_global_string("<error: %s>", error);
752        else if (group_node) value = builder.generate_group_name(selCluster, group_node);
753        else                 value = "<no matching subtree found>";
754    }
755    else value = "<no cluster selected>";
756    aw_root->awar(AWAR_CLUSTER_GROUP_EXAMPLE)->write_string(value.c_str());
757}
758
759static void update_cluster_group(ClusterPtr cluster, AW_CL cl_groupBuilder) {
760    GroupBuilder *groupBuilder = (GroupBuilder*)cl_groupBuilder;
761    if (!groupBuilder->get_error()) {
762        groupBuilder->update_group(cluster);
763    }
764}
765
766static void accept_proposed_names(ClusterPtr cluster, AW_CL cl_accept) {
767    bool accept(cl_accept);
768    cluster->accept_proposed(accept);
769}
770
771static void group_clusters(AW_window *, Group_Action action) {
772    AW_root          *aw_root  = global_data->get_aw_root();
773    Group_What        what     = (Group_What)aw_root->awar(AWAR_CLUSTER_GROUP_WHAT)->read_int();
774    AffectedClusters  affected = what == GROUP_LISTED ? ALL_CLUSTERS : SEL_CLUSTER;
775
776    GroupBuilder groupBuilder(global_data->get_gb_main(), action);
777
778    GB_transaction ta(global_data->get_gb_main());
779    with_affected_clusters_do(aw_root, affected, true, (AW_CL)&groupBuilder, update_cluster_group);
780
781    ARB_ERROR error = groupBuilder.get_error();
782    if (error) {
783        ClusterPtr bad = groupBuilder.get_bad_cluster();
784        if (!bad.isNull()) {
785            select_cluster(bad->get_ID());
786            aw_message("Problematic cluster has been highlighted");
787        }
788    }
789    else {
790        error = groupBuilder.save_modified_tree();
791    }
792
793    error = ta.close(error);
794
795    bool accept = !error;
796    aw_message_if(error);
797    // careful! the following code will invalidate error, so don't use below
798
799    with_affected_clusters_do(aw_root, affected, false, (AW_CL)accept, accept_proposed_names); // just affects display
800    global_data->update_cluster_selection_list();
801}
802
803static void popup_group_clusters_window(AW_window *aw_clusterList) {
804    static AW_window_simple *aws = 0;
805
806    if (!aws) {
807        AW_root *aw_root = aw_clusterList->get_root();
808
809        aws = new AW_window_simple;
810        aws->init(aw_root, "cluster_groups", "Cluster groups");
811
812        aws->auto_space(10, 10);
813
814        aws->callback((AW_CB0)AW_POPDOWN);
815        aws->create_button("CLOSE", "CLOSE", "C");
816        aws->callback(makeHelpCallback("cluster_group.hlp"));
817        aws->create_button("HELP", "HELP");
818
819        aws->at_newline();
820
821        aws->label("For");
822        aws->create_option_menu(AWAR_CLUSTER_GROUP_WHAT, true);
823        aws->insert_option        ("selected cluster", "s", GROUP_SELECTED);
824        aws->insert_default_option("listed clusters",  "l", GROUP_LISTED);
825        aws->update_option_menu();
826
827        aws->at_newline();
828
829        aws->label("with a min. cluster/subtree identity (%) of");
830        aws->create_input_field(AWAR_CLUSTER_GROUP_IDENTITY, 4);
831
832        aws->at_newline();
833
834        aws->label("-> if no matching subtree found");
835        aws->create_option_menu(AWAR_CLUSTER_GROUP_NOTFOUND, true);
836        aws->insert_default_option("abort",          "a", NOTFOUND_ABORT);
837        aws->insert_option        ("warn",           "w", NOTFOUND_WARN);
838        aws->insert_option        ("ignore",         "i", NOTFOUND_IGNORE);
839        aws->update_option_menu();
840
841        aws->at_newline();
842
843        aws->callback(makeWindowCallback(group_clusters, GROUP_CREATE));
844        aws->create_autosize_button("CREATE_GROUPS", "create groups!");
845
846        aws->label("If group exists");
847        aws->create_option_menu(AWAR_CLUSTER_GROUP_EXISTING, true);
848        aws->insert_default_option("abort",                "a", EXISTING_GROUP_ABORT);
849        aws->insert_option        ("skip",                 "s", EXISTING_GROUP_SKIP);
850        aws->insert_option        ("overwrite (caution!)", "o", EXISTING_GROUP_OVERWRITE);
851        aws->insert_option        ("append original",      "p", EXISTING_GROUP_APPEND_ORG);
852        aws->update_option_menu();
853
854        aws->at_newline();
855
856        aws->callback(makeWindowCallback(group_clusters, GROUP_DELETE));
857        aws->create_autosize_button("DELETE_GROUPS", "delete groups!");
858
859        aws->create_text_toggle(AWAR_CLUSTER_GROUP_PREFIX_MATCH, "(all)", "(where prefix matches)", 30);
860
861        aws->at_newline(); aws->label("Name prefix:"); aws->create_input_field(AWAR_CLUSTER_GROUP_PREFIX, 20);
862        aws->at_newline(); aws->label("Name suffix:"); aws->create_input_field(AWAR_CLUSTER_GROUP_SUFFIX, 20);
863
864        aws->at_newline();
865        aws->button_length(60);
866        aws->label("=>"); 
867        aws->create_button(NULL, AWAR_CLUSTER_GROUP_EXAMPLE);
868
869        aws->window_fit();
870    }
871
872    aws->activate();
873}
874
875// ---------------
876//      delete
877
878static void delete_selected_cluster(ClusterPtr cluster, AW_CL) {
879    int pos    = global_data->get_pos(cluster, SHOWN_CLUSTERS);
880    int nextId = global_data->idAtPos(pos+1, SHOWN_CLUSTERS);
881    select_cluster(nextId);
882    global_data->remove(cluster, SHOWN_CLUSTERS);
883}
884static void delete_clusters(AW_window *aww, AffectedClusters affected) {
885    switch (affected) {
886        case SEL_CLUSTER:
887            with_affected_clusters_do(aww->get_root(), affected, true, affected, delete_selected_cluster);
888            break;
889        case ALL_CLUSTERS:
890            select_cluster(0);
891            global_data->clear(SHOWN_CLUSTERS);
892            break;
893    }
894
895    update_cluster_sellist();
896}
897
898// ----------------------
899//      store/restore
900
901static void store_selected_cluster(ClusterPtr cluster, AW_CL) {
902    int pos    = global_data->get_pos(cluster, SHOWN_CLUSTERS);
903    int nextId = global_data->idAtPos(pos+1, SHOWN_CLUSTERS);
904
905    select_cluster(nextId);
906    global_data->store(cluster->get_ID());
907}
908static void store_clusters(AW_window *aww, AffectedClusters affected) {
909    switch (affected) {
910        case SEL_CLUSTER:
911            with_affected_clusters_do(aww->get_root(), affected, true, affected, store_selected_cluster);
912            break;
913        case ALL_CLUSTERS:
914            select_cluster(0);
915            global_data->store_all();
916            break;
917    }
918
919    update_all(aww);
920}
921
922
923static void restore_clusters(AW_window *aww) {
924    global_data->restore_all();
925    update_all(aww);
926}
927static void swap_clusters(AW_window *aww) {
928    global_data->swap_all();
929    select_cluster(0);
930    update_all(aww);
931}
932
933// ------------------
934//      save/load
935
936#if defined(WARN_TODO)
937#warning "implement save/load clusters"
938#endif
939static void save_clusters(AW_window *) {
940    aw_message("save_clusters not implemented");
941}
942static void load_clusters(AW_window *) {
943    aw_message("load_clusters not implemented");
944}
945
946// ---------------------------------
947//      cluster detection window
948
949AW_window *DI_create_cluster_detection_window(AW_root *aw_root, WeightedFilter *weightedFilter) {
950    static AW_window_simple *aws = 0;
951    if (!aws) {
952        cl_assert(!global_data);
953        global_data = new ClustersData(*weightedFilter);
954
955        aws = new AW_window_simple;
956        aws->init(aw_root, "DETECT_CLUSTERS", "Detect clusters in tree");
957        aws->load_xfig("di_clusters.fig");
958
959        aws->on_hide(di_forget_global_data);
960
961        // -------------------
962        //      upper area
963
964        aws->at("close");
965        aws->callback(AW_POPDOWN);
966        aws->create_button("CLOSE", "CLOSE");
967
968        aws->at("help");
969        aws->callback(makeHelpCallback("di_clusters.hlp"));
970        aws->create_button("HELP", "HELP");
971
972        aws->at("max_dist");
973        aws->d_callback(makeWindowCallback(calculate_clusters));
974        aws->create_input_field(AWAR_CLUSTER_MAXDIST, 12);
975
976        aws->at("min_size");
977        aws->d_callback(makeWindowCallback(calculate_clusters));
978        aws->create_input_field(AWAR_CLUSTER_MINSIZE, 5);
979
980        aws->at("calculate");
981        aws->callback(calculate_clusters);
982        aws->create_autosize_button("CALC", "Detect clusters");
983
984        aws->button_length(20);
985        aws->at("tree_name");
986        aws->create_button(NULL, AWAR_DIST_TREE_CURR_NAME);
987
988        // -------------------
989        //      lower area
990
991        aws->button_length(18);
992
993        // column 1
994
995        aws->at("mark_all"); aws->callback(makeWindowCallback(mark_clusters, ALL_CLUSTERS, true)); aws->create_button("MARKALL", "Mark all");
996
997        aws->at("auto_mark");  aws->create_toggle(AWAR_CLUSTER_AUTOMARK);
998        aws->at("mark_rep");   aws->create_toggle(AWAR_CLUSTER_MARKREP);
999        aws->at("select_rep"); aws->create_toggle(AWAR_CLUSTER_SELECTREP);
1000
1001        aws->at("mark"); aws->callback(makeWindowCallback(mark_clusters, SEL_CLUSTER, true)); aws->create_button("MARK", "Mark cluster");
1002
1003        // column 2
1004
1005        aws->at("group"); aws->callback(popup_group_clusters_window); aws->create_button("GROUP", "Cluster groups..");
1006
1007        aws->at("sort");
1008        aws->create_option_menu(AWAR_CLUSTER_ORDER, true);
1009        aws->insert_default_option("by mean distance",  "d", SORT_BY_MEANDIST);
1010        aws->insert_option        ("by min bases used", "b", SORT_BY_MIN_BASES);
1011        aws->insert_option        ("by size",           "s", SORT_BY_CLUSTERSIZE);
1012        aws->insert_option        ("by tree position",  "p", SORT_BY_TREEPOSITION);
1013        aws->insert_option        ("by min distance",   "i", SORT_BY_MIN_DIST);
1014        aws->insert_option        ("by max distance",   "x", SORT_BY_MAX_DIST);
1015        aws->insert_option        ("reverse",           "r", SORT_REVERSE);
1016        aws->update_option_menu();
1017
1018        // store/restore
1019
1020        aws->at("store_all"); aws->callback(makeWindowCallback(store_clusters, ALL_CLUSTERS)); aws->create_button("STOREALL", "Store all");
1021        aws->at("store");     aws->callback(makeWindowCallback(store_clusters, SEL_CLUSTER));  aws->create_button("STORESEL", "Store selected");
1022        aws->at("restore");   aws->callback(restore_clusters);                                 aws->create_button("RESTORE",  AWAR_CLUSTER_RESTORE_LABEL);
1023        aws->at("swap");      aws->callback(swap_clusters);                                    aws->create_button("Swap",     "Swap stored");
1024
1025        // column 4
1026
1027        aws->at("clear");  aws->callback(makeWindowCallback(delete_clusters, ALL_CLUSTERS)); aws->create_button("CLEAR", "Clear list");
1028        aws->at("delete"); aws->callback(makeWindowCallback(delete_clusters, SEL_CLUSTER));  aws->create_button("DEL",   "Delete selected");
1029        aws->sens_mask(AWM_DISABLED);
1030        aws->at("save"); aws->callback(save_clusters); aws->create_button("SAVE", "Save list");
1031        aws->at("load"); aws->callback(load_clusters); aws->create_button("LOAD", "Load list");
1032        aws->sens_mask(AWM_ALL);
1033
1034
1035        // --------------------
1036        //      clusterlist
1037
1038        aws->at("cluster_list");
1039        global_data->clusterList = aws->create_selection_list(AWAR_CLUSTER_SELECTED, true);
1040        update_cluster_sellist();
1041
1042        aw_root->awar(AWAR_CLUSTER_SELECTED)->add_callback(select_cluster_cb);
1043        aw_root->awar(AWAR_CLUSTER_ORDER)->add_callback(sort_order_changed_cb);
1044        sort_order_changed_cb(aw_root);
1045    }
1046
1047    return aws;
1048}
1049
1050void DI_create_cluster_awars(AW_root *aw_root, AW_default def, AW_default db) {
1051    aw_root->awar_float(AWAR_CLUSTER_MAXDIST,   3.0, def)->set_minmax(0.0, 100.0);
1052    aw_root->awar_int  (AWAR_CLUSTER_MINSIZE,   7,   def)->set_minmax(2, INT_MAX);
1053    aw_root->awar_int  (AWAR_CLUSTER_AUTOMARK,  1,   def);
1054    aw_root->awar_int  (AWAR_CLUSTER_MARKREP,   0,   def);
1055    aw_root->awar_int  (AWAR_CLUSTER_SELECTREP, 1,   def);
1056
1057    aw_root->awar_int   (AWAR_CLUSTER_ORDER,         SORT_BY_MEANDIST, def);
1058    aw_root->awar_string(AWAR_CLUSTER_RESTORE_LABEL, "None stored",    def);
1059
1060    aw_root->awar_int(AWAR_CLUSTER_GROUP_WHAT,     GROUP_LISTED,   def);
1061    aw_root->awar_int(AWAR_CLUSTER_GROUP_NOTFOUND, NOTFOUND_ABORT, def);
1062
1063    aw_root->awar_int   (AWAR_CLUSTER_GROUP_IDENTITY,     100, def)->set_minmax(1, 100);
1064    aw_root->awar_int   (AWAR_CLUSTER_GROUP_PREFIX_MATCH, 1,   def);
1065    aw_root->awar_string(AWAR_CLUSTER_GROUP_EXAMPLE,      "",  def);
1066
1067    aw_root->awar_int   (AWAR_CLUSTER_SELECTED,       0,                    def)->add_callback(update_example);
1068    aw_root->awar_int   (AWAR_CLUSTER_GROUP_EXISTING, EXISTING_GROUP_ABORT, def)->add_callback(update_example);
1069    aw_root->awar_string(AWAR_CLUSTER_GROUP_PREFIX,   "cluster",            def)->add_callback(update_example);
1070    aw_root->awar_string(AWAR_CLUSTER_GROUP_SUFFIX,   "%o%p",               def)->add_callback(update_example);
1071
1072    aw_root->awar_int(AWAR_TREE_REFRESH, 0, db);
1073
1074    update_example(aw_root);
1075}
1076
Note: See TracBrowser for help on using the repository browser.