RNAblueprint-1.3.2
probability_matrix.h
Go to the documentation of this file.
1 
15 #ifndef PROBABILITY_MATRIX_H
16 #define PROBABILITY_MATRIX_H
17 
18 // include common header with graph definition and global variables
19 #include "common.h"
20 #include "pairing_matrix.h"
21 
22 // include standard library parts
23 #include <unordered_map>
24 #include <list>
25 #include <functional>
26 #include <iomanip>
27 #include <algorithm>
28 #include <iterator>
29 
30 // include boost components
31 #include <boost/functional/hash.hpp>
32 
33 
34 namespace design {
35  namespace detail {
36  // class definitions
37 
38  // Class with custom hash function for the ProbabilityMatrix
39  typedef std::map < int, int > ProbabilityKey;
40 
41  class ProbabilityKeyHash {
42  public:
43  std::size_t operator()(const ProbabilityKey& k) const;
44  };
45 
46  // Class to get Pairing numbers
47  typedef std::unordered_map< ProbabilityKey, SolutionSizeType, ProbabilityKeyHash> ProbabilityMap;
48 
49  class ProbabilityMatrix {
50  public:
51  ProbabilityMatrix();
52  // default copy constructor
53  //ProbabilityMatrix( const ProbabilityMatrix &pm ) = default;
54  // get probability for ProbabilityKeys... key of Aks (12/A) (4/C) ()...
55  //SolutionSizeType get(ProbabilityKey pk);
56  SolutionSizeType operator[](ProbabilityKey& pk);
57  // fill a nos for a certain key
58  void put(ProbabilityKey& pk, SolutionSizeType nos);
59  // get maximal number of sequences for the whole matrix/subgraph
60  SolutionSizeType mnos();
61  // return if this is a initialized PM or not
62 
63  bool is_initialized() {
64  return initialized;
65  }
66  // get set of articulation vertices
67  std::set< int > getArticulations() {
68  return articulations;
69  };
70  // get number of dimensions
71  unsigned int getDimensions() {
72  return articulations.size();
73  }
74  // sample one combination randomly given a ProbabilityKey with the constraints and a random number generator
75  // return a pair with the chosen ProbabilityKey and the number_of_sequences for the given input constraints
76  template <typename R>
77  std::pair<ProbabilityKey, ProbabilityFraction> sample(ProbabilityKey pk, R& rand);
78  // articulation case without probability key (and therefore constraints)
79  template <typename R>
80  std::pair<ProbabilityKey, ProbabilityFraction> sample(R& rand);
81  // multiplicator
82  ProbabilityMatrix operator*(ProbabilityMatrix& y);
83  // My custom hash key used for n
84  friend class ProbabilityKeyHash;
85  ~ProbabilityMatrix() = default;
86  private:
87  // map of possibilities saved by key
88  ProbabilityMap pmap;
89  // remember if initialized
90  bool initialized = false;
91  // remember all articulation points
92  std::set< int > articulations = {};
93  // TODO a function to "multiply" probability matrixes
94  };
95 
96  class PermuteKeyFactory {
97  public:
98  PermuteKeyFactory(ProbabilityKey pk);
99  ProbabilityKey* key();
100  bool next_permutation();
101  bool previous_permutation();
102  void reset();
103  private:
104  std::map<int, std::list<int> > storage;
105  std::map<int, std::list<int>::iterator> state;
106  bool make_next_step(std::map<int, std::list<int>::iterator>::iterator state_it);
107  bool make_previous_step(std::map<int, std::list<int>::iterator>::iterator state_it);
108  ProbabilityKey current;
109  inline void copy_current() {
110  for (auto s : state) {
111  current[s.first] = *s.second;
112  }
113  }
114  };
115 
116  // a function which creates a new ProbabilityMatrix where the given key is internal
117  ProbabilityMatrix make_internal(ProbabilityMatrix& pm, int v);
118 
119  // overload << operator to print ProbabilityKeys
120  std::ostream& operator<<(std::ostream& os, ProbabilityKey& m);
121 
122  // overload << operator to print ProbabilityMatrix
123  std::ostream& operator<<(std::ostream& os, ProbabilityMatrix& m);
124  }
125 }
126 
127 #endif /* PROBABILITY_MATRIX_H */
128 
129 /*
130  * \endcond INTERNAL
131  */
This file holds all global includes, definitions and variables.
All classes and functions for the RNA design library are under the design namespace.
Definition: RNAblueprint.h:101
This file holds the class definitions for the PairingMatrix.