1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
// Copyright Maarten L. Hekkelman, Radboud University 2008-2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// substitution matrix for multiple sequence alignments
#pragma once
#include "mas.h"
#include "align-2d.h"
#include <string>
#include <istream>
#include <cassert>
#include <stdexcept>
#include <algorithm>
// --------------------------------------------------------------------
// uBlas compatible matrix types
// matrix is m x n, addressing i,j is 0 <= i < m and 0 <= j < n
// element m i,j is mapped to [i * n + j] and thus storage is row major
template<typename T>
class matrix_base
{
public:
typedef T value_type;
virtual ~matrix_base() {}
virtual uint32 dim_m() const = 0;
virtual uint32 dim_n() const = 0;
virtual value_type& operator()(uint32 i, uint32 j) { throw std::runtime_error("unimplemented method"); }
virtual value_type operator()(uint32 i, uint32 j) const = 0;
matrix_base& operator*=(const value_type& rhs);
matrix_base& operator-=(const value_type& rhs);
};
template<typename T>
matrix_base<T>& matrix_base<T>::operator*=(const T& rhs)
{
for (uint32 i = 0; i < dim_m(); ++i)
{
for (uint32 j = 0; j < dim_n(); ++j)
{
operator()(i, j) *= rhs;
}
}
return *this;
}
template<typename T>
matrix_base<T>& matrix_base<T>::operator-=(const T& rhs)
{
for (uint32 i = 0; i < dim_m(); ++i)
{
for (uint32 j = 0; j < dim_n(); ++j)
{
operator()(i, j) -= rhs;
}
}
return *this;
}
template<typename T>
std::ostream& operator<<(std::ostream& lhs, const matrix_base<T>& rhs)
{
lhs << '[' << rhs.dim_m() << ',' << rhs.dim_n() << ']' << '(';
for (uint32 i = 0; i < rhs.dim_m(); ++i)
{
lhs << '(';
for (uint32 j = 0; j < rhs.dim_n(); ++j)
{
if (j > 0)
lhs << ',';
lhs << rhs(i,j);
}
lhs << ')';
}
lhs << ')';
return lhs;
}
template<typename T>
class matrix : public matrix_base<T>
{
public:
typedef T value_type;
template<typename T2>
matrix(const matrix_base<T2>& m)
: m_m(m.dim_m())
, m_n(m.dim_n())
{
m_data = new value_type[m_m * m_n];
for (uint32 i = 0; i < m_m; ++i)
{
for (uint32 j = 0; j < m_n; ++j)
operator()(i, j) = m(i, j);
}
}
matrix(const matrix& m)
: m_m(m.m_m)
, m_n(m.m_n)
{
m_data = new value_type[m_m * m_n];
std::copy(m.m_data, m.m_data + (m_m * m_n), m_data);
}
matrix& operator=(const matrix& m)
{
value_type t = new value_type[m.m_m * m.m_n];
std::copy(m.m_data, m.m_data + (m_m * m_n), t);
delete[] m_data;
m_data = t;
m_m = m.m_m;
m_n = m.m_n;
return *this;
}
matrix(uint32 m, uint32 n, T v = T())
: m_m(m)
, m_n(n)
{
m_data = new value_type[m_m * m_n];
std::fill(m_data, m_data + (m_m * m_n), v);
}
virtual ~matrix()
{
delete [] m_data;
}
virtual uint32 dim_m() const { return m_m; }
virtual uint32 dim_n() const { return m_n; }
virtual value_type operator()(uint32 i, uint32 j) const
{
assert(i < m_m); assert(j < m_n);
return m_data[i * m_n + j];
}
virtual value_type& operator()(uint32 i, uint32 j)
{
assert(i < m_m); assert(j < m_n);
return m_data[i * m_n + j];
}
private:
value_type* m_data;
uint32 m_m, m_n;
};
// --------------------------------------------------------------------
template<typename T>
class symmetric_matrix : public matrix_base<T>
{
public:
typedef typename matrix_base<T>::value_type value_type;
symmetric_matrix(uint32 n)
: m_owner(true)
, m_n(n)
{
uint32 N = (m_n * (m_n + 1)) / 2;
m_data = new value_type[N];
std::fill(m_data, m_data + N, T(0));
}
symmetric_matrix(const T* data, uint32 n)
: m_owner(false)
, m_data(const_cast<T*>(data))
, m_n(n)
{
}
virtual ~symmetric_matrix()
{
if (m_owner)
delete[] m_data;
}
virtual uint32 dim_m() const { return m_n; }
virtual uint32 dim_n() const { return m_n; }
T operator()(uint32 i, uint32 j) const;
virtual T& operator()(uint32 i, uint32 j);
// erase two rows, add one at the end (for neighbour joining)
void erase_2(uint32 i, uint32 j);
private:
bool m_owner;
value_type* m_data;
uint32 m_n;
};
template<typename T>
inline
T symmetric_matrix<T>::operator()(uint32 i, uint32 j) const
{
return i < j
? m_data[(j * (j + 1)) / 2 + i]
: m_data[(i * (i + 1)) / 2 + j];
// if (i > j)
// std::swap(i, j);
// assert(j < m_n);
// return m_data[(j * (j + 1)) / 2 + i];
}
template<typename T>
inline
T& symmetric_matrix<T>::operator()(uint32 i, uint32 j)
{
if (i > j)
std::swap(i, j);
assert(j < m_n);
return m_data[(j * (j + 1)) / 2 + i];
}
template<typename T>
void symmetric_matrix<T>::erase_2(uint32 di, uint32 dj)
{
uint32 s = 0, d = 0;
for (uint32 i = 0; i < m_n; ++i)
{
for (uint32 j = 0; j < i; ++j)
{
if (i != di and j != dj and i != dj and j != di)
{
if (s != d)
m_data[d] = m_data[s];
++d;
}
++s;
}
}
--m_n;
}
template<typename T>
class identity_matrix : public matrix_base<T>
{
public:
typedef typename matrix_base<T>::value_type value_type;
identity_matrix(uint32 n)
: m_n(n)
{
}
virtual uint32 dim_m() const { return m_n; }
virtual uint32 dim_n() const { return m_n; }
virtual value_type operator()(uint32 i, uint32 j) const
{
value_type result = 0;
if (i == j)
result = 1;
return result;
}
private:
uint32 m_n;
};
// --------------------------------------------------------------------
// matrix functions
template<typename T>
matrix<T> operator*(const matrix_base<T>& lhs, const matrix_base<T>& rhs)
{
matrix<T> result(min(lhs.dim_m(), rhs.dim_m()), min(lhs.dim_n(), rhs.dim_n()));
for (uint32 i = 0; i < result.dim_m(); ++i)
{
for (uint32 j = 0; j < result.dim_n(); ++j)
{
for (uint32 li = 0, rj = 0; li < lhs.dim_m() and rj < rhs.dim_n(); ++li, ++rj)
result(i, j) += lhs(li, j) * rhs(i, rj);
}
}
return result;
}
template<typename T>
matrix<T> operator*(const matrix_base<T>& lhs, T rhs)
{
matrix<T> result(lhs);
result *= rhs;
return result;
}
template<typename T>
matrix<T> operator-(const matrix_base<T>& lhs, const matrix_base<T>& rhs)
{
matrix<T> result(std::min(lhs.dim_m(), rhs.dim_m()), std::min(lhs.dim_n(), rhs.dim_n()));
for (uint32 i = 0; i < result.dim_m(); ++i)
{
for (uint32 j = 0; j < result.dim_n(); ++j)
{
result(i, j) = lhs(i, j) - rhs(i, j);
}
}
return result;
}
template<typename T>
matrix<T> operator-(const matrix_base<T>& lhs, T rhs)
{
matrix<T> result(lhs.dim_m(), lhs.dim_n());
result -= rhs;
return result;
}
// --------------------------------------------------------------------
class substitution_matrix
{
public:
substitution_matrix(const std::string& name);
substitution_matrix(
const substitution_matrix& m, bool positive);
virtual ~substitution_matrix() {}
int8 operator()(aa a, aa b) const
{
return m_matrix(a, b);
}
int8 operator()(char a, char b) const
{
return m_matrix(encode(a), encode(b));
}
float mismatch_average() const { return m_mismatch_average; }
float scale_factor() const { return m_scale_factor; }
private:
substitution_matrix(
const substitution_matrix&);
substitution_matrix&
operator=(const substitution_matrix&);
void read(std::istream& is);
matrix<int8> m_matrix;
float m_mismatch_average;
float m_scale_factor;
};
class substitution_matrix_family
{
public:
substitution_matrix_family(
const std::string& name);
~substitution_matrix_family();
const substitution_matrix&
operator()(float distance, bool positive) const
{
const substitution_matrix* result;
uint32 ix = 0;
while (distance < m_cutoff[ix] and ix < 3)
++ix;
if (positive)
result = m_pos_smat[ix];
else
result = m_smat[ix];
return *result;
}
private:
substitution_matrix_family(
const substitution_matrix_family&);
substitution_matrix_family&
operator=(const substitution_matrix_family&);
float m_cutoff[4];
substitution_matrix*
m_smat[4];
substitution_matrix*
m_pos_smat[4];
};
//ostream& operator<<(ostream& os, substitution_matrix& m)
//{
// // print header
// os << ' ';
// for (uint32 i = 0; i < sizeof(kAA); ++i)
// os << " " << kAA[i];
// os << endl;
//
// // print matrix
// for (uint32 r = 0; r < sizeof(kAA); ++r)
// {
// os << kAA[r];
//
// for (uint32 c = 0; c < sizeof(kAA); ++c)
// os << setw(3) << m(r, c);
//
// os << endl;
// }
//
// return os;
//}
|