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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
// ==========================================================================
// Author: utro@math.unipa.it
// Author: srombo@deis.unical.it
// Author: Jonathan Göke
// ==========================================================================
//////////////////////////////////////////////////////////////////////////////
#ifndef SEQAN_STATISTICS_STATISTICS_MARKOV_MODEL_H_
#define SEQAN_STATISTICS_STATISTICS_MARKOV_MODEL_H_
#include <seqan/align.h>
#include <seqan/index.h>
namespace seqan2 {
/*!
* @class MarkovModel
* @headerfile <seqan/statistics.h>
* @brief Gives a suitable representation of a Marcov Chain.
*
* @signature template <typename TAlphabet[, typename TFloat[, typename TSpec]]>
* class MarkovModel;
*
*
* @tparam TAlphabet The type of the underlying alphabet.
* @tparam TFloat The type for storing counts, default is <tt>double</tt>.
* @tparam TSpec Tag for specialization.
*
* @section Examples
*
* @subsection Build a MarkovModel from Background
*
* @include demos/dox/statistics/build_markov_model.cpp
*
* The following example shows how to build a MarkovModel over a Dna alphabet from a set of background sequence. After
* build the model, we compute the zscore.
*
* @code{.console}
* zscore: 11.8323
* @endcode
*
* @subsection Load a MarkovModel from File
*
* We can also load the MarkovModel from a file (previously saved using @link MarkovModel::write @endlink). Since we do
* not have the background word set here but only the model, we compute the variance of a word using the function
* calculateVariance from the alignment_free module.
*
* @include demos/dox/statistics/load_markov_model.cpp
*
* @code{.console}
* variance: 0.267919
* @endcode
*
* @var unsigned MarkovModel::order;
* @brief The order of the MarkovModel.
*
* @var TMatrix MarkovModel::transition;
* @brief The transition matirx.
*
* @var TVector MarkovModel::stationaryDistribution;
* @brief The vector of characgter distribution (String of TFloat).
*
*
* @fn MarkovModel::MarkovModel
* @brief Constructor
*
* @signature MarkovModel::MarkovModel(order);
*
* @param[in] order The order of the model (<tt>unsigned</tt>).
*
*
* @fn MarkovModel::build
* @brief Compute the transition matrix from a training set.
*
* The character statitionary distribution and the auxiliary information that give raise to an instance of a
* Markov Model are also computed.
*
* @signature void MarkovModel::build(stringSet);
*
* @param[in] stringSet The StringSet to build the model for.
*
*
* @fn MarkovModel::set
* @brief Set transition matrix.
*
* Given e transition matrix, sets it as transition matrix of the MarkovModel and computes (if it is not available) the
* vector of character distributions and the auxiliary information.
*
* @signature void MarkovModel(transition[, stationaryDistribution]);
*
* @param[in,out] transition The transition matrix.
* @param[in] stationaryDistribution The vector of character distributions.
*
*
* @fn MarkovModel::emittedProbability
* @brief Computes the probability that a string or a set of strings is emitted by the MarkovModel.
*
* @signature TFloat MarkovModel::emittedProbability(s);
* @signature TFloat MarkovModel::emittedProbability(ss);
*
* @param[in] s The String to compute the emission probability for.
* @param[in] ss The StringSet to compute the emission probability for.
*
* @return TFloat The emission probability, TFloat is the TFloat from the MarkovModel.
*
*
* @fn MarkovModel::write
* @brief Stores an instance of a markovModel in a file.
*
* @signature void MarkovModel::write(file);
*
* @param[in,out] file The file to write the model to (type <tt>FILE *</tt>).
*
*
* @fn MarkovModel::read
* @brief Load an instance of MarkovModel from a file.
*
* @signature void MarkovModel::read(file);
*
* @param[in,out] file The file to read the model from (type <tt>FILE *</tt>).
*/
/*
with matrix calss matrices
*/
template <typename TAlphabet, typename TFloat = double, typename TSpec = Default>
class MarkovModel
{
public:
//Definition of matrix types, could be set to two dimensional as soon as implemented
typedef Matrix<TFloat, 2> TMatrix;
//typedef String<TFloat> TMatrix;
typedef String<TFloat> TVector;
unsigned int order;
TMatrix transition;
TVector stationaryDistribution;
//The following matrices are only for internal use of the class
TMatrix _q; //QPP^(morder-1)
TMatrix _qppp; //QPP^(morder-1)
TMatrix _qppqpp; //QPPQP^(morder-1)
MarkovModel(unsigned int order_):
order(order_)
{
unsigned int const alphabet_size = ValueSize<TAlphabet>::VALUE;
//unsigned int const column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
unsigned int column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
//Special case of order 0 marko model:
//A Bernoulli scheme is a special case of a Markov chain where the transition probability matrix has identical rows, which means that the next state is even independent of the current state (in addition to being independent of the past states).
if(order==0)
{
column_size=alphabet_size;
}
//resize the matrix
setLength(transition, 0, column_size);
setLength(transition, 1, column_size);
resize(transition,(TFloat) 0);
clear(stationaryDistribution);
resize(stationaryDistribution, column_size);
}
///////////////////////////////////////////////////////////////
///// BUILD THE MODEL
///////////////////////////////////////////////////////////////
typedef String<Dna> TText;
typedef Size<TText>::Type TSize;
typedef StringSet<TText > TStringSet;
//template <typename TAlphabet>
void build(StringSet<String<TAlphabet> > const &strings)
{
typedef String<TAlphabet> TText;
//typedef Size<TText>::Type TSize;
typedef StringSet<TText > TStringSet;
typedef Index<TStringSet, IndexQGram<SimpleShape> > TIndex;
typedef typename Fibre<TIndex, QGramDir>::Type TDir;
typedef typename Iterator<TDir, Standard>::Type TIter;
unsigned int const alphabet_size = ValueSize<TAlphabet>::VALUE;
//unsigned int const column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
unsigned int column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
//Special case of order 0 marko model:
//A Bernoulli scheme is a special case of a Markov chain where the transition probability matrix has identical rows, which means that the next state is even independent of the current state (in addition to being independent of the past states).
if(order==0)
{
column_size=alphabet_size;
}
TIndex ind(strings);
resize(indexShape(ind), order + 1);
indexRequire(ind, QGramSADir());
TIter itBegin = begin(indexDir(ind), Standard());
TIter itEnd = end(indexDir(ind), Standard()) - 1;
//Frequency of all q-grams for a markov model of order q-1 to calculate transition probabilitiesof (q-1) gram to next (q-1)gram
String<TAlphabet> qgram;
Shape<TAlphabet, SimpleShape> orderShape;
resize(orderShape, order);
if(order==0)
{
resize(orderShape, order+1);
}
//count transition of sequences according to model order
for(TIter itA = itBegin; itA != itEnd; ++itA)
{
unhash(qgram, itA - itBegin, weight(indexShape(ind)));
//std::cout<<"\n"<<qgram<<"\n"<<(*(itA+1) - *itA)<<"\n"<<hash(orderShape, begin(qgram));
//old for the array: value(transition, hash(orderShape, begin(qgram)) * column_size + hash(orderShape, begin(qgram) + 1)) = *(itA+1) - *itA;
if(order==0)
{
for(unsigned int row=0;row<column_size;++row)
{
value(transition, row,hash(orderShape, begin(qgram))) = static_cast<TFloat>(*(itA+1) - *itA);
}
}
else
{
//new for the matrix
value(transition, hash(orderShape, begin(qgram)),hash(orderShape, begin(qgram) + 1)) = static_cast<TFloat>(*(itA+1) - *itA);
}
}
//std::cout<<"\n"<<transition<<"\n\n";
//normalization, rows have to sum up to 1
for(unsigned int row = 0; row < column_size;++row)
{
TFloat sum = 0;
for(unsigned int col = 0; col < column_size;++col)
{
sum += value(transition, row,col); //sum of the rows
}
if (sum != 0)
{
for(unsigned int col = 0; col < column_size;++col)
{
value(transition, row, col) /= sum; //normalize by dividing by sum of rows
}
}
}
//std::cout<<transition<<"\n\n";
//Special case of order 0 marko model:
//A Bernoulli scheme is a special case of a Markov chain where the transition probability matrix has identical rows, which means that the next state is even independent of the current state (in addition to being independent of the past states).
if(order==0)
{
order=1;
}
//----Calculation of stationary Distribution-----
TMatrix temp = transition;
//initialise a variable t representing a good threshold to estimate the vector
//after multiplying t times the transition matrix with itself
unsigned int t=6;
for (unsigned int i=0; i<t; i++)
{
temp=temp*temp;
}
for (unsigned int i=0; i<column_size; i++){
value(stationaryDistribution,i)=value(temp, 0,i);
}
//std::cout<<temp<<"\n\n";
//for(unsigned int i=0;i<length(stationaryDistribution);++i){std::cout<<stationaryDistribution[i]<<"\t";}
//!is commented since I dont use it and it makes everything very slow for k>3 or 4
//!call ensureAuxMatrices(markovModel);
//_computeAuxiliaryMatrices();
}
///////////////////////////////////////////////////////////////
///// EMITTEDPROBABILITY
///////////////////////////////////////////////////////////////
template <typename TString, typename TSetSpec>
TFloat emittedProbability(StringSet<TString, TSetSpec > const &stringSet)
{
TFloat p = 0;
for(unsigned int i=0; i<length(stringSet); i++)
{
p+= emittedProbability(stringSet[i]);
}
return p;
}
template <typename TString>
TFloat emittedProbability(TString const &string)
{
Shape<TAlphabet, SimpleShape> orderShape;
resize(orderShape, order);
int row = hash(orderShape,begin(string));
TFloat p = value(stationaryDistribution,row);
for(unsigned int i=1; i<(length(string)-order+1); i++)
{
int column=hash(orderShape,begin(string)+i);
p*=value(transition,row,column);
row = column;
}
return p;
}
///////////////////////////////////////////////////////////////
///// SET THE MODEL
///////////////////////////////////////////////////////////////
void set(Matrix<TFloat,2> &iTransition)
{
unsigned int const alphabet_size = ValueSize<TAlphabet>::VALUE;
unsigned int const column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
transition = iTransition;
Matrix<TFloat,2> temp = transition;
//initialise a variable t representing a good threshold to estimate the vector
//after multiplying e times the transition matrix with itself
unsigned int t=6;
for (unsigned int i=0; i<t; i++){
temp=temp*temp;
}
for (unsigned int i=0; i<column_size; i++){
value(stationaryDistribution,i)=value(temp, 0,i); //Set stationary distribution to row of transition^6
}
//_computeAuxiliaryMatrices();
}
void set(Matrix<TFloat,2> &iTransition, String<TFloat> &iStationaryDistribution)
{
transition = iTransition;
stationaryDistribution = iStationaryDistribution;
//_computeAuxiliaryMatrices();
}
///////////////////////////////////////////////////////////////
///// WRITE
///////////////////////////////////////////////////////////////
void write(FILE *file)
{
ensureAuxMatrices(*this);
unsigned int const alphabet_size = ValueSize<TAlphabet>::VALUE;
unsigned int const column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
//write the transition matrix
for(unsigned int row=0; row<column_size; row++){
for(unsigned int col=0; col<column_size; col++){
fprintf(file,"%f ",value(transition, row,col));
}
fprintf(file,"\n");
}
//write the stationary distribution vector
for(unsigned int row=0; row<column_size; row++){
fprintf(file,"%f ",value(stationaryDistribution, row));
}
fprintf(file,"\n");
if(length(_q)){
//write the auxiliary matrix
for(unsigned int row=0; row<column_size; row++){
for(unsigned int col=0; col<column_size; col++){
fprintf(file,"%f ",value(_q, row,col));
}
fprintf(file,"\n");
}
for(unsigned int row=0; row<column_size; row++){
for(unsigned int col=0; col<column_size; col++){
fprintf(file,"%f ",value(_qppp,row,col));
}
fprintf(file,"\n");
}
for(unsigned int row=0; row<column_size; row++){
for(unsigned int col=0; col<column_size; col++){
fprintf(file,"%f ",value(_qppqpp, row,col));
}
fprintf(file,"\n");
}
}
}
///////////////////////////////////////////////////////////////
///// READ
///////////////////////////////////////////////////////////////
void read(FILE *file)
{
unsigned int const alphabet_size = ValueSize<TAlphabet>::VALUE;
unsigned int const column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
//read the transition matrix
for(unsigned int row=0; row<column_size; row++)
{
for(unsigned int col=0; col<column_size; col++)
{
int unused = fscanf(file,"%lf ", & value(transition, row,col));
(void)unused;
}
int unused = fscanf(file,"\n");
(void)unused;
}
//read the stationary distribution vector
for(unsigned int row=0; row<column_size; row++)
{
int unused = fscanf(file,"%lf ",&value(stationaryDistribution, row));
(void)unused;
}
int unused = fscanf(file,"\n");
(void)unused;
if (!feof(file))
{
setLength(_q, 0, column_size);
setLength(_q, 1, column_size);
resize(_q);
//read the auxiliary matrix
for(unsigned int row=0; row<column_size; row++)
{
for(unsigned int col=0; col<column_size; col++)
{
int unused = fscanf(file,"%lf ",&value(_q, row,col));
(void)unused;
}
int unused = fscanf(file,"\n");
(void)unused;
}
setLength(_qppp, 0, column_size);
setLength(_qppp, 1, column_size);
resize(_qppp);
for(unsigned int row=0; row<column_size; row++){
for(unsigned int col=0; col<column_size; col++){
int unused = fscanf(file,"%lf ",&value(_qppp, row,col));
(void)unused;
}
int unused = fscanf(file,"\n");
(void)unused;
}
setLength(_qppqpp, 0, column_size);
setLength(_qppqpp, 1, column_size);
resize(_qppqpp);
for(unsigned int row=0; row<column_size; row++){
for(unsigned int col=0; col<column_size; col++){
int unused = fscanf(file,"%lf ",&value(_qppqpp, row,col));
(void)unused;
}
int unused = fscanf(file,"\n");
(void)unused;
}
}
}
/////////////////////////////////////////////////////////////////////////////
///// COMPUTE THE AUXILIARY MATRICES FOR THE VARIANCE And Z-SCORE COMPUTATION
/////////////////////////////////////////////////////////////////////////////
void _computeAuxiliaryMatrices()
{
//std::cout<<"auxMat\n";
//clear(_q);
//clear(_qppp);
//clear(_qppqpp);
unsigned int const alphabet_size = ValueSize<TAlphabet>::VALUE;
unsigned int const column_size = (unsigned int) std::pow((double) alphabet_size, (int) order);
TMatrix I;
TMatrix Ip;
//resize the matrices
setLength(I, 0, column_size);
setLength(I, 1, column_size);
resize(I, 0.0);
setLength(Ip, 0, column_size);
setLength(Ip, 1, column_size);
resize(Ip, 0.0);
for(unsigned int i=0; i<column_size; i++){
value(I,i,i)=1.0;
for (unsigned int j=0; j<column_size; j++)
value(Ip,i,j)=value(stationaryDistribution,j);
}
_q=transition-I;
_q=_q+Ip;
_q=_computeInverseMatrix(_q);//works for simple non singular matrices, others not checked
//original code: _q=_computeInverseMatrix(_matricialSum(_matricialDifference(transition, I), Ip));
_qppp=_q*transition;
_qppqpp = _qppp*transition;
_qppqpp = _qppqpp*_q;
for(unsigned int i=1; i<order; i++)
{
_qppp=_qppp*transition;
_qppqpp=_qppqpp*transition;
}
}
};
//////////////////////////////////////////////////////////////////////////////
// _computeInverseMatrix
//////////////////////////////////////////////////////////////////////////////
/*
* @fn _computeInverseMatrix
* @brief Computes the inverse matrix of a given matrix.
*
* @signature TMatrix _computeInverseMatrix(matrix);
*
* @param[in] matrix The matrix in the input (2-dimensional Matrix).
*
* @return TMatrix The inverse matrix, the same type as matrix.
*/
template <typename TValue>
Matrix<TValue,2> _computeInverseMatrix(Matrix<TValue,2> &matrix)
{
typedef Matrix<TValue,2> TMatrix;
unsigned int n = length(matrix,0);
TMatrix result;
//resize the matrix
setLength(result, 0, n);
setLength(result, 1, n);
resize(result, 0.0);
//copy the matrix in result, since the procedure is in-place
TMatrix tmp = matrix;
//lu decomposition of a in-place
String<TValue> indx=_ludcmp(tmp);
String<TValue> col;
unsigned int i;
// inverse by columns
for (unsigned int j=0; j<n; j++)
{
resize(col,n,0);
if(j>0)
{
for(i=0; i<n; i++)
{
value(col,i)=0;
}
}
value(col,j) = 1;
_lubksb(tmp,indx,col);
for (i=0; i<n; i++)
{
value(result, i,j)= value(col,i);
}
}
return result;
}
/*
*
* LU decomposition
*
*/
template <typename TValue>
String<TValue> _ludcmp(Matrix<TValue,2> &result)
{
double const TINY = 1.0e-20;
int n = length(result,0);
int i, imax, j, k,d;
imax = std::numeric_limits<int>::min();
double big,dum,sum,temp;
String<TValue> vv;
resize(vv, n, 1.0);
d = 1;
for (i=1; i<=n; i++)
{
big = 0.0;
for (j=1; j<=n; j++)
{
if ((temp=fabs(value(result, i-1,j-1)))>big)
{
big = temp;
}
}
if (big==0.0)
{
std::cout<<"Singular matrix in routine ludcmp" << std::endl;
exit(1);
}
value(vv,i-1) = 1.0/big;
}
String<TValue> indx;
resize(indx,n);
for (j=1; j<=n; j++)
{
for (i=1; i<j; i++)
{
sum = value(result,i-1,j-1);
for (k=1; k<i; k++)
{
sum -= value(result, i-1,k-1)*value(result, k-1,j-1);
}
value(result, i-1,j-1) = sum;
}
big = 0.0;
for (i=j; i<=n; i++)
{
sum = value(result,i-1,j-1);
for (k=1; k<j; k++)
{
sum -= value(result, i-1,k-1)*value(result, k-1,j-1);
}
value(result, i-1,j-1) = sum;
if ((dum = value(vv, i-1)*fabs(sum))>=big)
{
big = dum;
imax = i;
}
}
if (j != imax)
{
for (k=1; k<=n; k++)
{
dum = value(result,imax-1,k-1);
value(result, imax-1,k-1) = value(result, j-1,k-1);
value(result,j-1,k-1) = dum;
}
d = -(d);
value(vv, imax-1)=value(vv,j-1);
}
value(indx, j-1) = imax;
if (value(result, j-1,j-1) == 0.0)
{
value(result, j-1,j-1) = TINY;
}
if (j!=n)
{
dum = 1.0/(value(result,j-1,j-1));
for (i=j+1; i<=n; i++)
{
value(result, i-1,j-1) *= dum;
}
}
}
return indx;
}
template <typename TValue>
void _lubksb(Matrix<TValue,2> &a, String<TValue> &indx, String<TValue> &b)
{
int n =length(a,0); //Number of columns in matrix a
int i, ii=0,ip,j;
double sum;
for (i=1; i<=n; i++)
{
ip = static_cast<int>(value(indx,i-1));
sum = value(b,ip-1);
value(b,ip-1) = value(b,i-1);
if (ii)
{
for (j=ii;j<=i-1;j++)
{
sum -=value(a,i-1,j-1)*value(b,j-1);
}
}
else
if (sum)
{
ii=i;
}
value(b,i-1) = sum;
}
for (i=n; i>=1; i--)
{
sum = value(b,i-1);
for (j=i+1; j<=n; j++)
{
sum -= value(a,i-1,j-1)*value(b,j-1);
}
value(b,i-1) = sum/value(a,i-1,i-1);
}
}
//////////////////////////////////////////////////////////////////////////////
// Interface
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
template <typename TAlphabet, typename TFloat, typename TSpec>
void buildMarkovModel(MarkovModel<TAlphabet, TFloat, TSpec> &mm,
StringSet<String<TAlphabet > > &stringSet)
{
mm.build(stringSet);
}
template <typename TAlphabet, typename TFloat, typename TSpec>
void buildMarkovModel(MarkovModel<TAlphabet, TFloat, TSpec> &mm,
StringSet<String<TAlphabet > > const &stringSet)
{
mm.build(stringSet);
}
///////////////////////////////////////////////////////////////
template <typename TAlphabet, typename TFloat, typename TSpec>
void setMarkovModel(MarkovModel<TAlphabet, TFloat, TSpec> & mm,
Matrix<TFloat,2> &iTransition)
{
mm.set(iTransition);
}
template <typename TAlphabet, typename TFloat, typename TSpec>
void setMarkovModel(MarkovModel<TAlphabet, TFloat, TSpec> & mm,
Matrix<TFloat,2> &iTransition,
String<TFloat> &iStationaryDistribution)
{
mm.set(iTransition, iStationaryDistribution);
}
///////////////////////////////////////////////////////////////
template <typename TAlphabet, typename TFloat, typename TSpec>
TFloat emittedProbability(MarkovModel<TAlphabet, TFloat, TSpec> & mm,
StringSet<String<TAlphabet > > & stringSet)
{
return mm.emittedProbability(stringSet);
}
template <typename TAlphabet, typename TFloat, typename TSpec>
TFloat emittedProbability(MarkovModel<TAlphabet, TFloat, TSpec> & mm,
String<TAlphabet> &string)
{
return mm.emittedProbability(string);
}
//const
template <typename TAlphabet, typename TFloat, typename TSpec, typename TString, typename TSetSpec>
TFloat emittedProbability(MarkovModel<TAlphabet, TFloat, TSpec> & mm,
StringSet<TString, TSetSpec> const &stringSet)
{
return mm.emittedProbability(stringSet);
}
template <typename TAlphabet, typename TFloat, typename TSpec, typename TString>
TFloat emittedProbability(MarkovModel<TAlphabet, TFloat, TSpec> & mm,
TString const &string)
{
return mm.emittedProbability(string);
}
///////////////////////////////////////////////////////////////
template <typename TAlphabet, typename TFloat, typename TSpec>
void write(FILE *file,
MarkovModel<TAlphabet, TFloat, TSpec> & mm )
{
//IOREV only defined for FILE* not for File(), uses custom io with fprintf
mm.write(file);
}
///////////////////////////////////////////////////////////////
template <typename TAlphabet, typename TFloat, typename TSpec>
void read(FILE *file,
MarkovModel<TAlphabet, TFloat, TSpec> & mm )
{
//IOREV only defined for FILE* not for File(), uses custom io with fscanf
mm.read(file);
}
//////////////////////////////////////////////////////////////////////////////
template <typename TAlphabet, typename TFloat, typename TSpec>
void ensureAuxMatrices(MarkovModel<TAlphabet, TFloat, TSpec> & mm )
{
if(empty(mm._q)){
mm._computeAuxiliaryMatrices();
}
}
}
#endif // #ifndef SEQAN_STATISTICS_STATISTICS_MARKOV_MODEL_H_
|