MDA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
consistency_aln.hpp
Go to the documentation of this file.
1 /*
2  * consistency_aln.hpp
3  *
4  * Created on: Sep 12, 2013
5  * Author: ckeme_01
6  */
7 
13 #ifndef CONSISTENCY_ALN_HPP_
14 #define CONSISTENCY_ALN_HPP_
15 
16 // C++ header
17 #include <string>
18 #include <algorithm>
19 #include <utility>
20 
21 // MDAT header
22 #include "Library.hpp"
23 #include "../utils/MatrixStack.hpp"
24 #include "../clustering/Tree.hpp"
25 
26 namespace MDAT{
27 
28 
38 void
39 nw_dyn_consistency(size_t dim1, size_t dim2, Matrix<std::pair<int, char> > &matrix);
40 
49 void
50 nw_dyn_consistency_traceback(size_t i, size_t j, const Matrix<std::pair<int, char> > &matrix, std::string &edit_string1, std::string &edit_string2);
51 
52 
63 void
64 gotoh_dyn_consistency(int dim1, int dim2, MatrixStack<3, std::pair<int, char> > &matrices, int gop, int gep);
65 
74 void
75 gotoh_dyn_consistency_traceback(size_t i, size_t j, MatrixStack<3,std::pair<int, char> > &matrices, std::string &edit_string1, std::string &edit_string2);
76 
77 
85 /*template<typename DataType>
86 void
87 enterDataIntoMatrix(MatrixStack<3, std::pair<DataType, char> > &matrices, size_t dim1, size_t dim2, const std::map<Match, DataType> &match_points)
88 {
89  std::cout << "AHA" << std::endl;
90  matrices.resize(dim1+1, dim2+1);
91  matrices.fill(std::pair<DataType, char>(0,'-'));
92  typename std::map<Match, DataType>::const_iterator it, it_end=match_points.end();
93  Matrix<std::pair<DataType, char> > &mat = matrices[0];
94  for (it=match_points.begin(); it!=it_end; ++it)
95  {
96 
97  mat[it->first.first+1][it->first.second+1].first=it->second;
98  }
99 }
100 */
101 
109 template<typename DataType>
110 void
111 enterDataIntoMatrix(Matrix<std::pair<DataType, char> > &matrix, size_t dim1, size_t dim2, const std::map<Match, DataType> &match_points)
112 {
113  matrix.resize(dim1+1, dim2+1);
114  matrix.fill(std::pair<DataType, char>(0,'-'));
115  typename std::map<Match, DataType>::const_iterator it, it_end=match_points.end();
116  for (it=match_points.begin(); it!=it_end; ++it)
117  matrix[it->first.first+1][it->first.second+1].first=it->second;
118 }
119 
120 
121 
129 template<typename DataType, typename GapFunction>
130 void
131 progressive_consistency_align(const Library<DataType > &lib, Tree &guide_tree, DataType &set, GapFunction gap_func)
132 {
133  size_t n_elems = set.size();
134  std::stack<std::pair<TreeNode*, unsigned int> > to_do;
135  to_do.push(std::pair<TreeNode*, int>(guide_tree.root(), 0));
136  std::map<Match, int> match_points;
137 
138  Matrix<std::pair<int, char> > matrix(10,10);
139  std::vector<std::pair<unsigned int, unsigned int> > gap1, gap2;
140 
141  Matrix<size_t> ids(n_elems, 1);
142  for (size_t i=0; i<n_elems; ++i)
143  ids[i][0]=i;
144 
145  size_t dim1, dim2, i;
146  std::string edit_string1, edit_string2;
147  while (!to_do.empty())
148  {
149  std::pair<TreeNode*, unsigned int> &current = to_do.top();
150  if (current.first->children.empty())
151  to_do.pop();
152  else if (current.second == current.first->children.size())
153  {
154  std::vector<size_t> &ids1 = ids[current.first->children[0]->id];
155  dim1=set[ids1[0]].length();
156  std::vector<size_t> &ids2 = ids[current.first->children[1]->id];
157  dim2=set[ids2[0]].length();
158  lib.get(ids1, ids2, match_points, gap_func);
159  enterDataIntoMatrix(matrix, dim1, dim2, match_points);
160  nw_dyn_consistency(dim1, dim2, matrix);
161  nw_dyn_consistency_traceback(dim1, dim2, matrix, edit_string1, edit_string2);
162  for (i=0; i<ids1.size(); ++i)
163  set[ids1[i]].insert_gaps(edit_string1);
164  for (i=0; i<ids2.size(); ++i)
165  set[ids2[i]].insert_gaps(edit_string2);
166  ids1.reserve( ids1.size() + ids2.size() );
167  ids1.insert(ids1.end(), ids2.begin(), ids2.end());
168  current.first->id = current.first->children[0]->id;
169  to_do.pop();
170  }
171  else
172  {
173  to_do.push(std::pair<TreeNode*, int>(&(*current.first->children[current.second]), 0));
174  ++current.second;
175  }
176  }
177 }
178 
179 
180 
181 
182 }
183 
184 
185 #endif /* CONSISTENCY_ALN_HPP_ */