ToPS
|
00001 /* 00002 * DecodableModel.cpp 00003 * 00004 * Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br> 00005 * Ígor Bonádio <ibonadio@ime.usp.br> 00006 * Vitor Onuchic <vitoronuchic@gmail.com> 00007 * Alan Mitchell Durham <aland@usp.br> 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00022 * MA 02110-1301, USA. 00023 */ 00024 00025 #include "DecodableModel.hpp" 00026 00027 namespace tops { 00028 00029 00030 double DecodableModel::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const 00031 { 00032 Matrix alpha; 00033 Sequence s2; 00034 s2.resize(end - begin + 1); 00035 for (unsigned int i = begin; i <= end; i++) 00036 s2[i-begin] = s[i]; 00037 return forward(s2, alpha); 00038 } 00039 00040 Sequence & DecodableModel::choose(Sequence & h, int size) const 00041 { 00042 Sequence p; 00043 return choose(h,p,0,size); 00044 } 00045 00046 00047 Sequence & DecodableModel::choose(Sequence & h, Sequence & path, int size) const{ 00048 return choose(h, path, 0, size); 00049 } 00050 00051 void DecodableModel::choosePath(const Sequence &s, Sequence &path){ 00052 std::cerr << model_name() << ": not implemented choosePath" << std::endl; 00053 } 00054 00055 Sequence & DecodableModel::choose(Sequence & h, Sequence & path, int i, int size) const 00056 { 00057 assert(path.size() == h.size()); 00058 int state = chooseFirstState(); 00059 if (((i - 1) < (int) path.size()) && (i - 1) >= 0) 00060 state = path[i - 1]; 00061 int l = 0; 00062 while ((int) h.size() < (i + size)) { 00063 chooseObservation(h, i + l, state); 00064 for (int k = i + l; k < (int) h.size(); k++, l++) { 00065 if (k < (int) path.size()) 00066 path[k] = state; 00067 else 00068 path.push_back(state); 00069 } 00070 state = chooseState(state); // next state 00071 } 00072 h.resize(size); 00073 path.resize(size); 00074 return h; 00075 } 00076 00078 void DecodableModel::posteriorProbabilities (const Sequence &sequence, SparseMatrixPtr probabilities) const{ 00079 return; 00080 } 00081 00082 void DecodableModel::posteriorProbabilities (const Sequence &sequence, fMatrix &probabilities) const{ 00083 return; 00084 } 00085 00086 float DecodableModel::MEAPred(const Sequence &s, Sequence &path){ 00087 cerr << "This model does not implement MEAPred" << endl; 00088 exit(-1); 00089 } 00090 00091 float DecodableModel::MEAPred(const Sequence &s, Sequence &path, SparseMatrixPtr postProbs){ 00092 cerr << "This model does not implement MEAPred" << endl; 00093 exit(-1); 00094 } 00095 00096 void DecodableModel::posteriorProbabilities (const Sequence &sequence, Matrix & probabilities) const 00097 { 00098 int nstates = (int)getStateNames()->size(); 00099 int size = sequence.size(); 00100 Matrix p (nstates, size); 00101 00102 Matrix alpha; // forward 00103 Matrix beta; // backward 00104 00105 double full = forward(sequence, alpha); 00106 backward(sequence, beta); 00107 00108 for(int k = 0; k < nstates; k++) 00109 for(int i = 0; i < size; i++) 00110 p(k, i) = alpha(k, i) + beta(k, i) - full; 00111 00112 probabilities = p; 00113 } 00114 00115 00117 void DecodableModel::posteriorDecoding (const Sequence &sequence, Sequence &path, Matrix & probabilities) const{ 00118 int nstates = (int)getStateNames()->size(); 00119 int size = sequence.size(); 00120 00121 posteriorProbabilities(sequence, probabilities); 00122 00123 path.resize(size); 00124 00125 double max; 00126 for(int i = 0; i < size; i++) 00127 { 00128 max = probabilities(0, i); 00129 path[i] = 0; 00130 for(int k = 1; k < nstates; k++) 00131 { 00132 if(probabilities(k, i) > max) 00133 { 00134 max = probabilities(k, i); 00135 path[i] = k; 00136 } 00137 } 00138 } 00139 } 00140 00141 }