AnalyticalSubstitutionCount.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "AnalyticalSubstitutionCount.h"
00041
00042 #include <Bpp/Numeric/Matrix/MatrixTools.h>
00043
00044 using namespace bpp;
00045
00046
00047
00048 void AnalyticalSubstitutionCount::computeCounts(double length) const
00049 {
00050 RowMatrix<double> Q = model_->getGenerator();
00051
00052 unsigned int s = Q.getNumberOfRows();
00053 RowMatrix<double> QL(s, s);
00054 for (unsigned int i = 0; i < s; i++)
00055 {
00056 for (unsigned int j = 0; j < s; j++)
00057 {
00058 QL(i, j) = ((i == j) ? 0. : Q(i, j)) ;
00059 }
00060 }
00061
00062 MatrixTools::fill(m_, 0.);
00063 RowMatrix<double> M2(s, s);
00064 RowMatrix<double> M3(s, s);
00065 RowMatrix<double> M4(s, s);
00066 RowMatrix<double> M5(s, s);
00067 for (int n = 1; n < cutOff_; n++)
00068 {
00069 MatrixTools::fill(M2, 0.);
00070 for(int p = 0; p < n; p++)
00071 {
00072 MatrixTools::pow(Q, p, M3);
00073 MatrixTools::mult(M3, QL, M4);
00074 MatrixTools::pow(Q, n - p - 1, M3);
00075 MatrixTools::mult(M4, M3, M5);
00076 MatrixTools::add(M2, M5);
00077 }
00078 MatrixTools::scale(M2, pow(length, n) / NumTools::fact(n));
00079 MatrixTools::add(m_, M2);
00080 }
00081
00082
00083 RowMatrix<double> P = model_->getPij_t(length);
00084 for (unsigned int i = 0; i < s; i++)
00085 {
00086 for(unsigned int j = 0; j < s; j++)
00087 {
00088 m_(i, j) /= P(i, j);
00089 }
00090 }
00091 }
00092
00093
00094
00095 double AnalyticalSubstitutionCount::getNumberOfSubstitutions(unsigned int initialState, unsigned int finalState, double length, unsigned int type) const
00096 {
00097 if (length == currentLength_) return m_(initialState, finalState);
00098 if (length < 0.000001) return initialState == finalState ? 0. : 1.;
00099
00100 computeCounts(length);
00101
00102 currentLength_ = length;
00103 return m_(initialState, finalState);
00104 }
00105
00106
00107
00108 Matrix<double>* AnalyticalSubstitutionCount::getAllNumbersOfSubstitutions(double length, unsigned int type) const
00109 {
00110 if (length == currentLength_) return new RowMatrix<double>(m_);
00111 if (length < 0.000001)
00112 {
00113 unsigned int s = model_->getAlphabet()->getSize();
00114 for (unsigned int i = 0; i < s; i++)
00115 {
00116 for (unsigned int j = 0; j < s; j++)
00117 {
00118 m_(i, j) = i == j ? 0. : 1.;
00119 }
00120 }
00121 }
00122 else
00123 {
00124
00125 computeCounts(length);
00126 }
00127
00128 currentLength_ = length;
00129
00130 return new RowMatrix<double>(m_);
00131 }
00132
00133
00134
00135 void AnalyticalSubstitutionCount::setSubstitutionModel(const SubstitutionModel* model)
00136 {
00137 model_ = model;
00138 unsigned int n = model->getAlphabet()->getSize();
00139 m_.resize(n, n);
00140
00141 computeCounts(currentLength_);
00142 }
00143
00144
00145