IMP  2.3.1
The Integrative Modeling Platform
merge_tree_utils.h
Go to the documentation of this file.
1 /**
2  * \file IMP/multifit/merge_tree_utils.h
3  *
4  * Copyright 2007-2014 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPMULTIFIT_MERGE_TREE_UTILS_H
8 #define IMPMULTIFIT_MERGE_TREE_UTILS_H
9 
10 #include <boost/functional/hash.hpp>
11 #include <boost/graph/adjacency_matrix.hpp>
12 #include <boost/graph/adjacency_list.hpp>
13 #include <boost/pending/disjoint_sets.hpp>
14 #include <boost/graph/graph_utility.hpp>
15 #include <IMP/multifit/multifit_config.h>
16 #include <IMP/atom/Hierarchy.h>
17 #include <boost/unordered_map.hpp>
18 #include <boost/graph/kruskal_min_spanning_tree.hpp>
19 #include <boost/graph/prim_minimum_spanning_tree.hpp>
20 
21 IMPMULTIFIT_BEGIN_NAMESPACE
22 
23 namespace MTU {
24 typedef boost::adjacency_matrix<boost::undirectedS, boost::no_property,
25  boost::property<boost::edge_weight_t, double> >
27 typedef boost::graph_traits<DependencyGraph>::edge_descriptor DGEdge;
28 typedef DependencyGraph::edge_property_type DGWeight;
29 typedef boost::graph_traits<DependencyGraph>::vertex_descriptor DGVertex;
30 typedef boost::unordered_map<kernel::Particle *, DGVertex> PVMAP;
31 typedef boost::unordered_map<DGVertex, Particle *> VPMAP;
32 };
33 
34 //! A simple Restraint that always returns a score of zero.
35 class IMPMULTIFITEXPORT DummyRestraint : public kernel::Restraint {
36  public:
38  : kernel::Restraint(a->get_model(), "DummyRestraint%1%"),
39  p1_(a),
40  p2_(b) {}
41  virtual double unprotected_evaluate(IMP::kernel::DerivativeAccumulator *accum)
42  const IMP_OVERRIDE;
45 
46  protected:
47  kernel::Particle *p1_, *p2_;
48 };
49 
50 //! Utility class for building merge trees.
51 class IMPMULTIFITEXPORT MergeTreeBuilder {
52  public:
53  MergeTreeBuilder(const atom::Hierarchies &mhs) : g_(mhs.size()), mhs_(mhs) {
54  // add mapping from mol to node
55 
56  typedef boost::graph_traits<MTU::DependencyGraph>::vertex_iterator
57  VertexIterator;
58  VertexIterator v_it, v_it_end;
59  boost::tie(v_it, v_it_end) = boost::vertices(g_);
60  int ind = 0;
61  for (; v_it != v_it_end; ++v_it) {
62  mol2node_[mhs_[ind]] = *v_it;
63  node2mol_[*v_it] = mhs_[ind];
64  ind = ind + 1;
65  }
66  } // end constructor
67 
68  // add +1 to the edge weight, creates the edge if does not exist
69  void increase_edge(atom::Hierarchy mh1, atom::Hierarchy mh2) {
70  // do not add self edges
71  if (mh1.get_particle() == mh2.get_particle()) return;
72  // get the corresponding nodes
73  MTU::DGVertex u, v;
74  u = mol2node_[mh1.get_particle()];
75  v = mol2node_[mh2.get_particle()];
76  if (!boost::edge(u, v, g_).second) { // edge does not exist
77  boost::add_edge(u, v, MTU::DGWeight(0.), g_);
78  }
79  // increase edge by one
80  MTU::DGEdge e;
81  e = boost::edge(u, v, g_).first;
82  boost::put(boost::edge_weight_t(), g_, e,
83  boost::get(boost::edge_weight_t(), g_, e) - 1);
84  }
85  void show(std::ostream &out = std::cout) const {
86  out << "vertices:";
87  typedef boost::graph_traits<MTU::DependencyGraph>::vertex_iterator
88  vertex_iter;
89  std::pair<vertex_iter, vertex_iter> vp;
90  for (vp = vertices(g_); vp.first != vp.second; ++vp.first) {
91  out << node2mol_.find(*vp.first)->second->get_name() << " ";
92  }
93  out << std::endl;
94  out << "edges:";
95  boost::graph_traits<MTU::DependencyGraph>::edge_iterator ei, ei_end;
96  for (boost::tie(ei, ei_end) = edges(g_); ei != ei_end; ++ei)
97  out << "(" << node2mol_.find(source(*ei, g_))->second->get_name() << ","
98  << node2mol_.find(target(*ei, g_))->second->get_name() << ","
99  << boost::get(boost::edge_weight_t(), g_, *ei) << ")" << std::endl;
100  out << std::endl;
101  }
102  kernel::ParticlePairsTemp get_mst_dependency() const {
103  std::vector<MTU::DGEdge> mst;
104  boost::kruskal_minimum_spanning_tree(g_, std::back_inserter(mst));
105  // go over the edges and get the pairs
107  for (int i = 0; i < (int)mst.size(); i++) {
109  pp[0] = node2mol_.find(boost::source(mst[i], g_))->second;
110  pp[1] = node2mol_.find(boost::target(mst[i], g_))->second;
111  ret.push_back(pp);
112  }
113  return ret;
114  }
115 
116  protected:
117  // the graph
118  MTU::DependencyGraph g_;
119  atom::Hierarchies mhs_;
120  MTU::PVMAP mol2node_;
121  MTU::VPMAP node2mol_;
122 };
123 
124 IMPMULTIFIT_END_NAMESPACE
125 
126 #endif /* IMPMULTIFIT_MERGE_TREE_UTILS_H */
boost::graph DependencyGraph
A directed graph on the interactions between the various objects in the model.
Class for adding derivatives from restraints to the model.
Particle * get_particle() const
Returns the particle decorated by this decorator.
Utility class for building merge trees.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
A class to store an fixed array of same-typed values.
Definition: Array.h:33
Decorator for helping deal with a hierarchy of molecules.
The standard decorator for manipulating molecular structures.
A restraint is a term in an IMP ScoringFunction.
Class to handle individual model particles.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
virtual ModelObjectsTemp do_get_inputs() const =0
IMP::kernel::Restraint Restraint
A simple Restraint that always returns a score of zero.
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.