ToPS
|
00001 /* 00002 * MaximumDependenceDecomposition.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 "MaximumDependenceDecomposition.hpp" 00026 #include "Alphabet.hpp" 00027 #include "ProbabilisticModelParameter.hpp" 00028 #include "Symbol.hpp" 00029 #include "VariableLengthMarkovChainCreator.hpp" 00030 #include "ProbabilisticModelCreatorClient.hpp" 00031 00032 namespace tops{ 00033 00034 void restore_submodel(std::string & model_name, std::map<std::string, ProbabilisticModelPtr> &_models, const ProbabilisticModelParameters & parameters); 00035 00037 double MaximumDependenceDecomposition::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const 00038 { 00039 double result = 0.0; 00040 std::stringstream node; 00041 std::vector <int> rest; 00042 for(int i = 0; i < _max_size; i++){ 00043 rest.push_back(0); 00044 } 00045 node << "x"; 00046 while(1) { 00047 std::string node_id = node.str(); 00048 int pos = _positions.find(node_id)->second; 00049 result += (_submodels.find(node_id)->second)->inhomogeneous()->evaluatePosition(s, pos, pos); 00050 rest[pos] = 1; 00051 std::string last_node = node_id; 00052 if(s[pos] == _consensus.find(node_id)->second) { 00053 node << "l"; 00054 } else { 00055 node << "r"; 00056 } 00057 // if we reach the end of the tree, calculate the probabilities using the last model 00058 if(_positions.find(node.str()) == _positions.end()) { 00059 for(int r = 0; r < _max_size; r++) 00060 if(rest[r] == 0) { 00061 result += _submodels.find(last_node)->second->inhomogeneous()->evaluatePosition(s, r, r); 00062 } 00063 break; 00064 } 00065 } 00066 return result; 00067 } 00068 00069 Sequence & MaximumDependenceDecomposition::choose(Sequence & s, int size) const { 00070 std::stringstream node; 00071 std::vector <int> rest; 00072 for(int i = 0; i < _max_size; i++){ 00073 rest.push_back(0); 00074 } 00075 node << "x"; 00076 while(1) { 00077 std::string node_id = node.str(); 00078 int pos = _positions.find(node_id)->second; 00079 s[pos] = (_submodels.find(node_id)->second)->inhomogeneous()->choosePosition(s, pos, pos); 00080 rest[pos] = 1; 00081 std::string last_node = node_id; 00082 if(s[pos] == _consensus.find(node_id)->second) { 00083 node << "l"; 00084 } else { 00085 node << "r"; 00086 } 00087 // if we reach the end of the tree, complete the sequence using the last model 00088 if(_positions.find(node.str()) == _positions.end()) { 00089 for(int r = 0; r < _max_size; r++) 00090 if(rest[r] == 0) { 00091 s[r] = _submodels.find(last_node)->second->inhomogeneous()->choosePosition(s, r, r); 00092 } 00093 break; 00094 } 00095 } 00096 return s; 00097 } 00098 00099 00100 void MaximumDependenceDecomposition::initialize(const ProbabilisticModelParameters & p) { 00101 ProbabilisticModelParameterValuePtr maxsizepar = p.getMandatoryParameterValue("max_length"); 00102 ProbabilisticModelParameterValuePtr consensuspar = p.getMandatoryParameterValue("consensus"); 00103 ProbabilisticModelParameterValuePtr positionspar = p.getMandatoryParameterValue("positions"); 00104 _max_size = maxsizepar->getInt(); 00105 std::map<std::string,double> aux = consensuspar->getDoubleMap(); 00106 std::map<std::string,double>::const_iterator it; 00107 for (it = aux.begin(); it != aux.end(); it++) { 00108 _consensus [it->first] = (int)it->second; 00109 } 00110 aux = positionspar->getDoubleMap(); 00111 for (it = aux.begin(); it != aux.end(); it++) { 00112 _positions [it->first] = (int)it->second; 00113 } 00114 for (it = aux.begin(); it != aux.end(); it++) { 00115 std::string node = it->first; 00116 restore_submodel(node, _submodels, p); 00117 } 00118 _parameters = p; 00119 } 00120 00121 std::string MaximumDependenceDecomposition::str() const{ 00122 std::stringstream s; 00123 std::map <std::string, ProbabilisticModelParameterValuePtr> ::const_iterator it; 00124 std::map <std::string, ProbabilisticModelParameterValuePtr> p = _parameters.parameters(); 00125 for(it = p.begin(); it != p.end(); it++) 00126 { 00127 s << it->first << " = " << (it->second)->str() << std::endl; 00128 } 00129 return s.str(); 00130 } 00131 00132 ProbabilisticModelParameters MaximumDependenceDecomposition::parameters() const{ 00133 return _parameters; 00134 } 00135 } 00136