Ymir  .9
Fast\C++toolforcomputationofassemblingprobabilities,statisticalinferenceofassemblingstatisticalmodelandgenerationofartificialsequencesofT-cellreceptorsdata.
multimatrixchain.h
1 /*
2  * Ymir <imminfo.github.io/ymir>
3  *
4  * This file is part of Ymir, a fast C++ tool for computation of assembling
5  * probabilities, statistical inference of assembling statistical model
6  * and generation of artificial sequences of T-cell receptors data.
7  *
8  *
9  * Copyright 2015 Vadim Nazarov <vdn at mailbox dot com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 #ifndef _MULTIMATRIXCHAIN_H_
25 #define _MULTIMATRIXCHAIN_H_
26 
27 
28 #include <vector>
29 
30 #include "types.h"
31 
32 
33 #define YDEBUG
34 
35 
36 namespace ymir {
37 
38  template <typename _Scalar>
40 
41 
48 
49  typedef unique_ptr<EventIndMMC> pEventIndMMC;
50 
51 
58 
59  typedef unique_ptr<ProbMMC> pProbMMC;
60 
61 
67  typedef seq_len_t error_num_t;
69 
70  typedef unique_ptr<ErrMMC> pErrMMC;
71 
72 
79  template <typename _Scalar>
80  class MultiMatrixChain {
81 
82  friend class MAAGForwardBackwardAlgorithm; // ):
83 
84  public:
85 
91  typedef uint8_t node_ind_t;
92 
93 
99  typedef uint8_t matrix_ind_t;
100 
101 
107 // typedef size_t dim_t;
108  typedef seq_len_t dim_t;
109 
110 
116 // typedef Matrix<_Scalar, Dynamic, Dynamic> matrix_t;
118 
119  protected:
120 
126  struct Node {
127  public:
128 
129  Node() : _n(0), _start_index(0), _rows(0), _cols(0)
130  { }
131 
132 
133  void init(size_t start_index, matrix_ind_t n, dim_t rows, dim_t cols) {
134  _n = n;
135  _rows = rows;
136  _cols = cols;
137  _start_index = start_index;
138  }
139 
140 
141  size_t operator() (matrix_ind_t mat, dim_t row, dim_t column) const {
142 #ifndef DNDEBUG
143  if (!(mat >= 0 && mat < _n)) { throw(std::runtime_error("Matrix index check failed!")); }
144 
145  if (!(row >= 0 && row < _rows && column >= 0 && column < _cols)) { throw(std::runtime_error("Rows / columns number check failed!")); }
146 #endif
147  return _start_index + mat * (_rows * _cols) + row * _cols + column;
148  }
149 
150 
151  matrix_ind_t size() const { return _n; }
152 
153  dim_t rows() const { return _rows; }
154 
155  dim_t cols() const { return _cols; }
156 
157  size_t start() const { return _start_index; }
158 
159  size_t n_values() const { return _n * _rows * _cols; }
160 
161 
162  protected:
163 
164  size_t _start_index;
165  matrix_ind_t _n;
166  dim_t _rows, _cols;
167 
168  };
169 
170  public:
171 
172  MultiMatrixChain() {
173  _values.reserve(5000);
174  }
175 
176 
177 // MultiMatrixChain(MultiMatrixChain &) = default;
178 //
179 //
180 // MultiMatrixChain(MultiMatrixChain &&) = default;
181 //
182 //
183 // MultiMatrixChain& operator=(MultiMatrixChain &) = default;
184 //
185 //
186 // MultiMatrixChain& operator=(MultiMatrixChain &&) = default;
187 
188 
189  virtual ~MultiMatrixChain()
190  {
191  }
192 
193 
194  void resize(node_ind_t n_nodes) {
195  _chain.resize(n_nodes);
196  }
197 
198 
199  node_ind_t chainSize() const { return _chain.size(); }
200 
201 
203  matrix_ind_t nodeSize(node_ind_t node_i) const { return _chain[node_i].size(); }
204 
205  dim_t nodeRows(node_ind_t node_i) const { return _chain[node_i].rows(); }
206 
207  dim_t nodeColumns(node_ind_t node_i) const { return _chain[node_i].cols(); }
209 
210 
221  _Scalar& operator()(node_ind_t node_i, matrix_ind_t mat_i, dim_t row, dim_t col) {
223 #ifndef DNDEBUG
224  if (node_i >= _chain.size()) { throw(std::runtime_error("Number of the Node is out of bounds."));}
225 #endif
226  return _values[_chain[node_i](mat_i, row, col)];
227  }
228 
229 // const _Scalar& operator()(node_ind_t node_i, matrix_ind_t mat_i, dim_t row, dim_t col) const {
230  _Scalar operator()(node_ind_t node_i, matrix_ind_t mat_i, dim_t row, dim_t col) const {
231 #ifndef DNDEBUG
232  if (node_i >= _chain.size()) { throw (std::runtime_error("Number of the Node is out of bounds.")); }
233 #endif
234  return _values[_chain[node_i](mat_i, row, col)];
235  }
236 
237  _Scalar& at(node_ind_t node_i, matrix_ind_t mat_i, dim_t row, dim_t col) {
238 #ifndef DNDEBUG
239  if (node_i >= _chain.size()) { throw(std::runtime_error("Number of the Node is out of bounds."));}
240 #endif
241  return _values[_chain[node_i](mat_i, row, col)];
242  }
243 
244 // const _Scalar& operator()(node_ind_t node_i, matrix_ind_t mat_i, dim_t row, dim_t col) const {
245  _Scalar at(node_ind_t node_i, matrix_ind_t mat_i, dim_t row, dim_t col) const {
246 #ifndef DNDEBUG
247  if (node_i >= _chain.size()) { throw(std::runtime_error("Number of the Node is out of bounds."));}
248 #endif
249  return _values[_chain[node_i](mat_i, row, col)];
250  }
252 
253 
257  node_ind_t addNode() {
259  _chain.push_back(Node());
260  return _chain.size() - 1;
261  }
262 
263  node_ind_t addNode(matrix_ind_t n_matrices, dim_t rows, dim_t cols) {
264  _chain.push_back(Node());
265  this->initNode(_chain.size() - 1, n_matrices, rows, cols);
266  return _chain.size() - 1;
267  }
269 
270 
271  void initNode(node_ind_t node_i, matrix_ind_t n_matrices, dim_t rows, dim_t cols) {
272  size_t prev_node_size = _chain[node_i].n_values();
273  _chain[node_i].init(_values.size(), n_matrices, rows, cols);
274  _values.resize(_values.size() + _chain[node_i].n_values() - prev_node_size, 0);
275  }
276 
277 
278  void swap(MultiMatrixChain &other) {
279  _chain.swap(other._chain);
280  _values.swap(other._values);
281  }
282 
283 
284  void finish() {
285  _chain.shrink_to_fit();
286  _values.shrink_to_fit();
287 // cout << _values.size() << endl;
288 // cout << _values.capacity() << endl;
289  }
290 
291 
292  void fill(node_ind_t node, matrix_ind_t mat, _Scalar val = 0) {
293  for (dim_t r = 0; r < _chain[node].rows(); ++r) {
294  for (dim_t c = 0; c < _chain[node].cols(); ++c) {
295  _values[_chain[node](mat, r, c)] = val;
296  }
297  }
298  }
299 
300 
301  matrix_t matrix(node_ind_t node, matrix_ind_t mat) const {
302  matrix_t res(_chain[node].rows(), _chain[node].cols(), 0);
303  for (dim_t r = 0; r < _chain[node].rows(); ++r) {
304  for (dim_t c = 0; c < _chain[node].cols(); ++c) {
305  res(r, c) = (*this)(node, mat, r, c);
306  }
307  }
308  return res;
309  }
310 
311 
312  _Scalar operator[](size_t index) const { return _values[index]; }
313 
314 
315  size_t values_size() const { return _values.size(); }
316 
317 
318  protected:
319 
320  std::vector<Node> _chain;
321  std::vector<_Scalar> _values;
322 
323  };
324 
325 }
326 
327 #endif
Definition: aligner.h:37
Node in the chain. Stores one or more matrices with equal size.
Definition: multimatrixchain.h:126
node_ind_t addNode()
Add new node with pattern matrix.
Definition: multimatrixchain.h:258
Definition: maagforwardbackwardalgorithm.h:20
Simple matrix class.
Definition: matrix.h:21
uint8_t node_ind_t
Node index type.
Definition: multimatrixchain.h:91
seq_len_t dim_t
Type of dimensions of matrices (rows and columns).
Definition: multimatrixchain.h:108
Class for storing lists of matrices, where one node in the list (called "chain") could contain more t...
Definition: multimatrixchain.h:39
uint8_t matrix_ind_t
Matrix index type.
Definition: multimatrixchain.h:99
Matrix< _Scalar, dim_t > matrix_t
Type of matrices in the chain.
Definition: multimatrixchain.h:117