32 #define di_assert(cond) arb_assert(cond)
37 #define AWAR_CLUSTER_PREFIX AWAR_DIST_PREFIX "cluster/"
38 #define AWAR_CLUSTER_PREFIX_TEMP "/tmp/" AWAR_DIST_PREFIX
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"
47 #define AWAR_CLUSTER_GRP_PREFIX AWAR_CLUSTER_PREFIX "group/"
48 #define AWAR_CLUSTER_GRP_PREFIX_TMP "/tmp/" AWAR_CLUSTER_GRP_PREFIX
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_PARTSEP AWAR_CLUSTER_GRP_PREFIX "separator"
58 #define AWAR_CLUSTER_GROUP_EXAMPLE AWAR_CLUSTER_GRP_PREFIX_TMP "example"
60 #define AWAR_CLUSTER_SELECTED AWAR_CLUSTER_PREFIX_TEMP "selected" // ID of currently selected cluster (or zero)
61 #define AWAR_CLUSTER_RESTORE_LABEL AWAR_CLUSTER_PREFIX_TEMP "rlabel" // label of restore button
124 if (!subtree->is_leaf()) {
154 :
new AP_sequence_parsimony(aliview);
159 tree =
new ClusterTreeRoot(aliview, seq, maxDistance/100, minClusterSize);
170 error = tree->loadFromDB(tree_name);
174 if (!error) error = tree->linkToDB(
NULp,
NULp);
179 error = tree->find_clusters();
208 else if (warn_if_none_affected) {
217 for (
ClusterIDsIter cli = clusters.begin(); cli != cli_end; ++cli) {
223 else if (warn_if_none_affected) {
224 aw_message(
"There are no clusters in the list");
235 for (
DBItemSetIter sp = members.begin(); sp != sp_end; ++sp) {
236 bool is_rep = *sp == representative;
240 case CMM_ALL: mark =
true;
break;
313 unsigned tagged_count;
315 void update_tag_counters();
316 unsigned get_leaf_count() const OVERRIDE {
return leaf_count; }
319 friend class GroupTreeRoot;
323 : ARB_countedTree(root),
336 unsigned update_leaf_counters();
346 float tagged_rate()
const {
return float(get_tagged_count())/get_leaf_count(); }
349 GroupTreeRoot::GroupTreeRoot(
AliView *aliView,
AP_sequence *seqTempl,
bool add_delete_callbacks)
352 inline TreeNode *GroupTreeRoot::makeNode()
const {
return new GroupTree(const_cast<GroupTreeRoot*>(
this)); }
353 inline void GroupTreeRoot::destroyNode(
TreeNode *
node)
const {
delete DOWNCAST(GroupTree*,node); }
356 unsigned GroupTree::update_leaf_counters() {
358 else leaf_count = get_leftson()->update_leaf_counters() + get_rightson()->update_leaf_counters();
362 void GroupTree::clear_tags() {
363 if (!
is_leaf() && tagged_count) {
364 get_leftson()->clear_tags();
365 get_rightson()->clear_tags();
370 void GroupTree::map_species2tip(
Species2Tip& mapping) {
372 if (name) mapping[name] =
this;
375 get_leftson()->map_species2tip(mapping);
376 get_rightson()->map_species2tip(mapping);
380 void GroupTree::update_tag_counters() {
382 GroupTree *
node =
this;
384 node->tagged_count = node->get_leftson()->get_tagged_count() + node->get_rightson()->get_tagged_count();
385 node = node->get_father();
396 void clear() { created = skipped = overwritten = deleted = restored = 0; }
409 msg =
string(
"Group changes: ")+msg;
424 GroupTreeRoot *tree_root;
430 unsigned existing_count;
434 string cluster_prefix;
435 string cluster_suffix_def;
438 bool del_match_prefixes;
440 GroupTree *find_group_position(GroupTree *subtree,
unsigned cluster_size);
441 float get_max_distance()
const {
return maxDist; }
444 DEFINE_DOWNCAST_ACCESSORS(GroupTree, get_root_node, tree_root->get_root_node());
446 bool shall_delete_group(
const char *name)
const {
482 return strstr(groupname, cluster_prefix.c_str()) == groupname;
486 void GroupBuilder::load_tree() {
489 tree_root =
new GroupTreeRoot(
new AliView(gb_main),
NULp,
false);
490 error = tree_root->loadFromDB(tree_name.c_str());
499 GroupTree *
tree = get_root_node();
500 tree->update_leaf_counters();
501 tree->map_species2tip(species2tip);
506 if (changes.
exist()) {
508 error = tree_root->saveToDB();
521 GroupTree *GroupBuilder::find_group_position(GroupTree *subtree,
unsigned cluster_size) {
524 GroupTree *groupPos =
NULp;
525 if (subtree->get_tagged_count() == cluster_size) {
526 groupPos = find_group_position(subtree->get_leftson(), cluster_size);
527 if (!groupPos) groupPos = find_group_position(subtree->get_rightson(), cluster_size);
530 if (subtree->tagged_rate() >= matchRatio) {
543 return !hasClusterPrefix;
551 for (list<string>::const_iterator p = namepart.begin(); p != namepart.end(); ++p) {
553 if (!concat.empty()) concat += separator;
565 list<string> namepart;
566 namepart.push_back(cluster_prefix);
568 string orgname_suffix;
570 char *old_name = group_node->name;
577 orgname_suffix =
string(
" {was:")+original+
"}";
584 for (
int i = 0; cluster_suffix_def[i]; ++i) {
585 if (cluster_suffix_def[i] ==
'%') {
587 if (cluster_suffix_def[i] ==
'%') {
592 switch(cluster_suffix_def[i]) {
598 case 'e': expanded = null2empty(group_node->name);
break;
600 int matchRate =
int(group_node->tagged_rate()*100+0.5);
606 text += cluster_suffix_def[i];
610 if (!expanded.empty()) {
611 namepart.push_back(text);
613 namepart.push_back(expanded);
618 text += cluster_suffix_def[i];
621 namepart.push_back(text);
627 GroupTree *group_node =
NULp;
629 if (!tree_root) load_tree();
637 for (
DBItemSetIter sp = members.begin(); sp != sp_end && !error; ++sp) {
641 Species2Tip::const_iterator found = species2tip.find(name);
642 if (found == species2tip.end()) {
643 error =
GBS_global_string(
"Species '%s' is not in '%s'", name, tree_name.c_str());
646 GroupTree *leaf = found->second;
655 group_node = find_group_position(get_root_node(), cluster->
get_member_count());
681 char *old_name = group_node->name;
696 group_node->name =
ARB_strdup(new_name.c_str());
699 if (!group_node->gb_node) {
700 GBDATA *gb_tree = group_node->get_tree_root()->get_gb_tree();
711 group_node->gb_node = gb_node;
714 if (group_node->gb_node && !error) {
726 if (group_node->name && shall_delete_group(group_node->name)) {
730 freeset(group_node->name, original);
731 if (group_node->gb_node) {
738 freenull(group_node->name);
739 group_node->gb_node =
NULp;
751 if (error) bad_cluster = cluster;
752 get_root_node()->clear_tags();
770 else if (group_node) value = builder.generate_group_name(selCluster, group_node);
771 else value =
"<no matching subtree found>";
773 else value =
"<no cluster selected>";
803 ClusterPtr bad = groupBuilder.get_bad_cluster();
806 aw_message(
"Problematic cluster has been highlighted");
810 error = groupBuilder.save_modified_tree();
815 bool accept = !
error;
824 static AW_window_simple *aws =
NULp;
829 aws =
new AW_window_simple;
830 aws->init(aw_root,
"cluster_groups",
"Cluster groups");
832 aws->auto_space(10, 10);
835 aws->create_button(
"CLOSE",
"CLOSE",
"C");
838 aws->create_button(
"HELP",
"HELP");
845 aws->insert_default_option(
"listed clusters",
"l",
GROUP_LISTED);
846 aws->update_option_menu();
850 aws->label(
"with a min. cluster/subtree identity (%) of");
855 aws->label(
"-> if no matching subtree found");
860 aws->update_option_menu();
865 aws->create_autosize_button(
"CREATE_GROUPS",
"create groups!");
867 aws->label(
"If group exists");
873 aws->update_option_menu();
878 aws->create_autosize_button(
"DELETE_GROUPS",
"delete groups!");
887 aws->button_length(60);
959 static AW_window_simple *aws =
NULp;
964 aws =
new AW_window_simple;
965 aws->init(aw_root,
"DETECT_CLUSTERS",
"Detect clusters in tree");
966 aws->load_xfig(
"di_clusters.fig");
975 aws->create_button(
"CLOSE",
"CLOSE");
979 aws->create_button(
"HELP",
"HELP");
989 aws->at(
"calculate");
991 aws->create_autosize_button(
"CALC",
"Detect clusters");
993 aws->button_length(20);
994 aws->at(
"tree_name");
1000 aws->button_length(10);
1005 aws->at(
"mark"); aws->callback(makeWindowCallback(
mark_clusters,
SEL_CLUSTER,
true)); aws->create_button(
"MARK",
"Mark");
1008 aws->at(
"mark_what");
1010 aws->insert_default_option(
"cluster w/o repr.",
"d",
CMM_ALL_BUT_REP);
1011 aws->insert_option (
"whole cluster",
"b",
CMM_ALL);
1012 aws->insert_option (
"only representative",
"s",
CMM_ONLY_REP);
1013 aws->update_option_menu();
1015 aws->at(
"mark_all"); aws->callback(makeWindowCallback(
mark_clusters,
ALL_CLUSTERS,
true)); aws->create_button(
"MARKALL",
"Mark all");
1019 aws->button_length(18);
1032 aws->update_option_menu();
1036 aws->at(
"store_all"); aws->callback(makeWindowCallback(
store_clusters,
ALL_CLUSTERS)); aws->create_button(
"STOREALL",
"Store all");
1037 aws->at(
"store"); aws->callback(makeWindowCallback(
store_clusters,
SEL_CLUSTER)); aws->create_button(
"STORESEL",
"Store selected");
1039 aws->at(
"swap"); aws->callback(
swap_clusters); aws->create_button(
"Swap",
"Swap stored");
1044 aws->at(
"delete"); aws->callback(makeWindowCallback(
delete_clusters,
SEL_CLUSTER)); aws->create_button(
"DEL",
"Delete selected");
1049 aws->at(
"cluster_list");
GBDATA * get_representative() const
GBDATA * get_gb_main() const
#define AWAR_CLUSTER_SELECTREP
const DBItemSet & get_members() const
static void mark_cluster(ClusterPtr cluster, ClusterMarkMode markRep)
~GroupTreeRoot() OVERRIDE
#define AWAR_CLUSTER_SELECTED
size_t count(ClusterSubset subset)
const char * get_list_display(const DisplayFormat *format) const
static void save_results(ClusterTreeRoot *tree)
string concatenate_name_parts(const list< string > &namepart, const string &separator)
char * originalGroupName(const char *groupname)
double get_mean_distance() const
static void update_all(AW_window *aww)
return string(buffer, length)
#define AWAR_CLUSTER_GROUP_PREFIX_MATCH
static void select_representative(ClusterPtr cluster)
long GBT_mark_all(GBDATA *gb_main, int flag)
#define AWAR_CLUSTER_GROUP_EXISTING
void changeSortOrder(ClusterOrder newOrder)
#define DEFINE_TREE_RELATIVES_ACCESSORS(TreeType)
GroupTree(GroupTreeRoot *root)
void update_cluster_selection_list()
static void select_cluster(ID id)
ID idAtPos(int pos, ClusterSubset subset)
#define AWAR_CLUSTER_MARKREP
#define AWAR_CLUSTER_GROUP_IDENTITY
char * ARB_strdup(const char *str)
AW_awar * set_minmax(float min, float max)
static void save_results_recursive(ClusterTree *subtree)
#define AWAR_DIST_ALIGNMENT
const char * GBS_global_string(const char *templat,...)
void init_tree() OVERRIDE
void AW_POPDOWN(AW_window *window)
#define AWAR_CLUSTER_GROUP_NOTFOUND
bool isNull() const
test if SmartPtr is NULp
float tagged_rate() const
static void delete_clusters(AW_window *aww, AffectedClusters affected)
bool GBT_is_alignment_protein(GBDATA *gb_main, const char *alignment_name)
void update_description(const ARB_countedTree *ct)
static void restore_clusters(AW_window *aww)
#define DOWNCAST(totype, expr)
static void update_cluster_sellist()
AW_awar * add_callback(const RootCallback &cb)
#define AWAR_CLUSTER_GROUP_PREFIX
void add(ClusterPtr clus, ClusterSubset subset)
const char * read_char_pntr() const
string generate_group_name(ClusterPtr cluster, const GroupTree *group_node)
POS_TREE1 * get_father() const
std::string get_upgroup_info(const ARB_countedTree *ct, const ARB_tree_predicate &keep_group_name, const std::string &separator)
GroupTree * find_best_matching_subtree(ClusterPtr cluster)
static void update_cluster_group(ClusterPtr cluster, GroupBuilder *groupBuilder)
GB_ERROR GB_await_error()
HasntCurrentClusterPrefix(const GroupBuilder &builder_)
GBDATA * GB_create_container(GBDATA *father, const char *key)
WindowCallback makeHelpCallback(const char *helpfile)
static void store_selected_cluster(ClusterPtr cluster)
static void update_example(AW_root *aw_root)
DECLARE_CBTYPE_FVV_AND_BUILDERS(ClusterCallback, void, ClusterPtr)
ARB_ERROR get_error() const
AliDataPtr concat(AliDataPtr left, AliDataPtr right)
#define AWAR_CLUSTER_GROUP_WHAT
std::set< GBDATA * > DBItemSet
AW_awar * awar_float(const char *var_name, float default_value=0.0, AW_default default_file=AW_ROOT_DEFAULT)
AW_root * get_aw_root() const
AW_window * DI_create_cluster_detection_window(AW_root *aw_root, WeightedFilter *weightedFilter)
#define AWAR_CLUSTER_GROUP_SUFFIX
static void select_cluster_cb(AW_root *aw_root)
const ClusterIDs & get_clusterIDs(ClusterSubset subset)
ClusterPtr get_bad_cluster() const
static void group_clusters(AW_window *, Group_Action action)
std::vector< ID > ClusterIDs
static void error(const char *msg)
#define AWAR_TREE_REFRESH
bool is_root_node() const
ARB_ERROR save_modified_tree()
#define AWAR_SPECIES_NAME
DBItemSet::const_iterator DBItemSetIter
#define AWAR_CLUSTER_GROUP_EXAMPLE
static SearchTree * tree[SEARCH_PATTERNS]
char * read_string() const
static void calculate_clusters(AW_window *aww)
AW_awar * awar(const char *awar)
static void store_clusters(AW_window *aww, AffectedClusters affected)
void clear(ClusterSubset subset)
static void swap_clusters(AW_window *aww)
void remove(ClusterPtr clus, ClusterSubset subset)
static int with_affected_clusters_do(AW_root *aw_root, AffectedClusters affected, bool warn_if_none_affected, ClusterCallback cb)
static void update_restore_label(AW_window *aww)
void accept_proposed(bool accept)
#define AWAR_DIST_TREE_CURR_NAME
#define AWAR_CLUSTER_GROUP_PARTSEP
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
GroupBuilder(GBDATA *gb_main_, Group_Action action_)
AliView * create_aliview(const char *aliname, GB_ERROR &error) const
void DI_create_cluster_awars(AW_root *aw_root, AW_default def, AW_default db)
GB_ERROR close(GB_ERROR error)
ClusterIDs::const_iterator ClusterIDsIter
void GB_write_flag(GBDATA *gbd, long flag)
static void mark_clusters(AW_window *, AffectedClusters affected, bool warn_if_none_affected)
ClusterPtr clusterWithID(ID id) const
void subtitle(const char *stitle)
#define AWAR_CLUSTER_ORDER
void update_group(ClusterPtr cluster)
void aw_message(const char *msg)
bool matches_current_prefix(const char *groupname) const
static void delete_selected_cluster(ClusterPtr cluster)
GB_ERROR GBT_write_name_to_groupData(GBDATA *gb_group, bool createNameEntry, const char *new_group_name, bool pedantic)
size_t get_member_count() const
#define AWAR_CLUSTER_RESTORE_LABEL
GB_ERROR write_string(const char *aw_string)
#define AWAR_CLUSTER_AUTOMARK
static ClustersData * global_data
const char * GBT_get_name(GBDATA *gb_item)
map< string, GroupTree * > Species2Tip
unsigned get_tagged_count() const
WeightedFilter & weighted_filter
static void sort_order_changed_cb(AW_root *aw_root)
GB_transaction ta(gb_var)
GB_ERROR GBT_write_int(GBDATA *gb_container, const char *fieldpath, long content)
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
AW_selection_list * clusterList
static void di_forget_global_data(AW_window *)
#define AWAR_CLUSTER_MINSIZE
const char * get_group_name() const
static void accept_proposed_names(ClusterPtr cluster, bool accept)
GB_ERROR write_int(long aw_int)
void aw_message_if(GB_ERROR error)
static void popup_group_clusters_window(AW_window *aw_clusterList)
#define AWAR_CLUSTER_MAXDIST
void mark_all_members(ClusterMarkMode mmode) const