MDA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
Vector.hpp
1 /*
2  * Vector.hpp
3  *
4  * Created on: Oct 9, 2013
5  * Author: ckeme_01
6  */
7 
8 #ifndef VECTOR_HPP_
9 #define VECTOR_HPP_
10 
11 #include <vector>
12 #include <algorithm>
13 
14 #include "../utils/Matrix.hpp"
15 
16 
17 namespace MDAT
18 {
19 
20 template<typename DataType>
21 class Vector
22 {
23 
24 private:
25  std::vector<DataType> _vec;
26  size_t _id;
27 
28 public:
29  Vector(size_t length, size_t id_value=0):_vec(std::vector<DataType>(length)), _id(id_value)
30  {}
31 
32  virtual ~Vector()
33  {}
34 
35  DataType &operator[](unsigned int index)
36  {
37  return _vec[index];
38  }
39 
40  const DataType &operator[](unsigned int index) const
41  {
42  return _vec[index];
43  }
44 
45  size_t id()
46  {
47  return _id;
48  }
49 
50  void
51  id(size_t value)
52  {
53  _id=value;
54  }
55 
56  void
57  resize(size_t new_size)
58  {
59  _vec.resize(new_size);
60  }
61 
62 
63  float
64  dist(const Vector<DataType> &other)
65  {
66  typename std::vector<DataType>::const_iterator it1, it2=other._vec.begin(), it1_end=_vec.end();
67  DataType val=0;
68  DataType sum=0;
69  for (it1=_vec.begin(); it1!=it1_end; ++it1)
70  {
71  val+=std::min(*it1,*it2);
72  sum+=std::max(*it1, *it2);
73  ++it2;
74  }
75  return 100-(100.0*val/sum);
76  }
77 };
78 
79 
80 template<typename SequenceType>
82 calc_km_vec(const SequenceType &seq, std::vector<short> &coded_alphabet, size_t start=0, size_t end=0)
83 {
84 
85  Vector<int> *vec_p = new Vector<int>(400, seq.id());
86  Vector<int> &vec = *vec_p;
87 
88  unsigned int value = 20 * coded_alphabet[(int)seq[start]] + coded_alphabet[(int)seq[start+1]];
89  short bad=0;
90  if (coded_alphabet[(int)seq[start]]==20)
91  ++bad;
92  if (coded_alphabet[(int)seq[++start]]==20)
93  ++bad;
94  if (bad==0)
95  ++vec[value];
96  short c;
97  ++start;
98  for (size_t i = start; i<end+1; ++i)
99  {
100  c = coded_alphabet[(int)seq[i-2]];
101  if (c==20)
102  --bad;
103  value -= 20*c;
104  value *= 20;
105 
106  value += coded_alphabet[(int)seq[i]];
107  if (coded_alphabet[(int)seq[i]]==20)
108  ++bad;
109 
110  if (bad==0)
111  ++vec[value];
112  }
113  return vec_p;
114 }
115 
116 
117 
118 template<typename SequenceSetType>
119 Matrix<float> *
120 kmer_dist_mat(const SequenceSetType &set)
121 {
122  int n_seqs=set.n_seqs();
123 
124  if (n_seqs==1)
125  return NULL;
126 
127  std::vector<short> coded_alphabet(256);
128  int i,j;
129  for (i=0; i<256;++i)
130  coded_alphabet[i]=20;
131  char aa_alphabet[20]={'A','V','L','I','P','M','F','W','G','S','T','C','N','Q','Y','D','E','K','R','H'};
132  short val=-1;
133  for (i=0; i<20; ++i)
134  {
135  coded_alphabet[toupper(aa_alphabet[i])-0]=++val;
136  coded_alphabet[tolower(aa_alphabet[i])-0]=val;
137  }
138 
139  // turn sequences into vectors for guide tree
140  std::vector<Vector<int>* >vec_set(set.size());
141 
142  for (i=0; i<n_seqs; ++i)
143  vec_set[i]=calc_km_vec(set[i], coded_alphabet, 0, set[i].length());
144 
145 
146  Matrix<float> *dist_mat_p=new Matrix<float>(n_seqs, n_seqs, 0);
147  Matrix<float> &dist_mat=*dist_mat_p;
148  for (i=0; i<n_seqs; ++i)
149  {
150  dist_mat[i][i]=0;
151  for (j=i+1; j<n_seqs; ++j)
152  dist_mat[i][j]=dist_mat[j][i]=vec_set[i]->dist(*(vec_set[j]));
153  }
154  for (i=0; i<n_seqs; ++i)
155  delete vec_set[i];
156  return dist_mat_p;
157 }
158 
159 }
160 
161 
162 
163 #endif /* VECTOR_HPP_ */