|
bpp-phyl
2.1.0
|
00001 // 00002 // File: AbstractTreeLikelihood.h 00003 // Created by: Julien Dutheil 00004 // Created on: Fri Oct 17 17:57:21 2003 00005 // 00006 00007 /* 00008 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004) 00009 00010 This software is a computer program whose purpose is to provide classes 00011 for phylogenetic data analysis. 00012 00013 This software is governed by the CeCILL license under French law and 00014 abiding by the rules of distribution of free software. You can use, 00015 modify and/ or redistribute the software under the terms of the CeCILL 00016 license as circulated by CEA, CNRS and INRIA at the following URL 00017 "http://www.cecill.info". 00018 00019 As a counterpart to the access to the source code and rights to copy, 00020 modify and redistribute granted by the license, users are provided only 00021 with a limited warranty and the software's author, the holder of the 00022 economic rights, and the successive licensors have only limited 00023 liability. 00024 00025 In this respect, the user's attention is drawn to the risks associated 00026 with loading, using, modifying and/or developing or reproducing the 00027 software by the user in light of its specific status of free software, 00028 that may mean that it is complicated to manipulate, and that also 00029 therefore means that it is reserved for developers and experienced 00030 professionals having in-depth computer knowledge. Users are therefore 00031 encouraged to load and test the software's suitability as regards their 00032 requirements in conditions enabling the security of their systems and/or 00033 data to be ensured and, more generally, to use and operate it in the 00034 same conditions as regards security. 00035 00036 The fact that you are presently reading this means that you have had 00037 knowledge of the CeCILL license and that you accept its terms. 00038 */ 00039 00040 #ifndef _ABSTRACTTREELIKELIHOOD_H_ 00041 #define _ABSTRACTTREELIKELIHOOD_H_ 00042 00043 #include "TreeLikelihood.h" 00044 #include "../Tree.h" 00045 #include "../TreeTemplate.h" 00046 00047 #include <Bpp/Numeric/AbstractParametrizable.h> 00048 00049 //From SeqLib: 00050 #include <Bpp/Seq/Container/SiteContainer.h> 00051 00052 namespace bpp 00053 { 00054 00069 class AbstractTreeLikelihood : 00070 public virtual TreeLikelihood, 00071 public AbstractParametrizable 00072 { 00073 public: 00079 class SimpleBranchIterator : 00080 public BranchIterator 00081 { 00082 private: 00083 std::vector<int> nodesId_; 00084 size_t index_; 00085 00086 public: 00087 SimpleBranchIterator(const std::vector<int>& nodesId) : 00088 nodesId_(nodesId), index_(0) {} 00089 00090 public: 00091 int next() throw (Exception) 00092 { 00093 if (!hasNext()) 00094 throw Exception("AbstractTreeLikelihood::SimpleBranchIterator::next(). No more branch in the set."); 00095 return nodesId_[index_++]; 00096 } 00097 00098 bool hasNext() const { return index_ < nodesId_.size(); } 00099 00100 }; 00101 00109 class SimpleSiteIterator : 00110 public SiteIterator 00111 { 00112 private: 00113 size_t maxIndex_; 00114 size_t index_; 00115 size_t offset_; 00116 00117 public: 00118 SimpleSiteIterator(size_t nbSites, size_t offset = 0) : 00119 maxIndex_(nbSites), index_(0), offset_(offset) {} 00120 00121 public: 00122 size_t next() throw (Exception) 00123 { 00124 if (!hasNext()) 00125 throw Exception("AbstractTreeLikelihood::SimpleSiteIterator::next(). No more site in the set."); 00126 return offset_ + index_++; 00127 } 00128 00129 bool hasNext() const { return index_ < maxIndex_; } 00130 00131 }; 00132 00138 class ConstNoPartitionBranchModelDescription : 00139 public ConstBranchModelDescription 00140 { 00141 private: 00142 const SubstitutionModel* model_; 00143 size_t nbSites_; 00144 00145 public: 00146 ConstNoPartitionBranchModelDescription(const SubstitutionModel* model, size_t nbSites) : 00147 model_(model), nbSites_(nbSites) {} 00148 00149 ConstNoPartitionBranchModelDescription(const ConstNoPartitionBranchModelDescription& bmd) : 00150 model_(bmd.model_), 00151 nbSites_(bmd.nbSites_) 00152 {} 00153 00154 ConstNoPartitionBranchModelDescription& operator=(const ConstNoPartitionBranchModelDescription& bmd) 00155 { 00156 model_ = bmd.model_; 00157 nbSites_ = bmd.nbSites_; 00158 return *this; 00159 } 00160 00161 public: 00162 const SubstitutionModel* getModel() const { return model_; } 00163 00164 SiteIterator* getNewSiteIterator() const { return new SimpleSiteIterator(nbSites_); } 00165 }; 00166 00167 class ConstNoPartitionBranchModelIterator : 00168 public ConstBranchModelIterator 00169 { 00170 private: 00171 ConstNoPartitionBranchModelDescription branchModelDescription_; 00172 size_t index_; 00173 00174 public: 00175 ConstNoPartitionBranchModelIterator(const SubstitutionModel* model, size_t nbSites) : 00176 branchModelDescription_(model, nbSites), index_(0) {} 00177 00178 public: 00179 ConstNoPartitionBranchModelDescription* next() throw (Exception) 00180 { 00181 if (!hasNext()) 00182 throw Exception("AbstractHomogeneousTreeLikelihood::ConstHomogeneousBranchModelIterator::next(). No more branch in the set."); 00183 index_++; 00184 return &branchModelDescription_; 00185 } 00186 00187 bool hasNext() const { return index_ == 0; } 00188 }; 00189 00190 class ConstNoPartitionSiteModelDescription : 00191 public ConstSiteModelDescription 00192 { 00193 private: 00194 const SubstitutionModel* model_; 00195 std::vector<int> nodesId_; 00196 00197 public: 00198 ConstNoPartitionSiteModelDescription(const SubstitutionModel* model, const std::vector<int> nodesId) : 00199 model_(model), nodesId_(nodesId) {} 00200 00201 ConstNoPartitionSiteModelDescription(const ConstNoPartitionSiteModelDescription& smd) : 00202 model_(smd.model_), 00203 nodesId_(smd.nodesId_) 00204 {} 00205 00206 ConstNoPartitionSiteModelDescription& operator=(const ConstNoPartitionSiteModelDescription& smd) 00207 { 00208 model_ = smd.model_; 00209 nodesId_ = smd.nodesId_; 00210 return *this; 00211 } 00212 00213 public: 00214 const SubstitutionModel* getModel() const { return model_; } 00215 00216 BranchIterator* getNewBranchIterator() const { return new SimpleBranchIterator(nodesId_); } 00217 }; 00218 00224 protected: 00225 const SiteContainer* data_; 00226 mutable TreeTemplate<Node>* tree_; 00227 bool computeFirstOrderDerivatives_; 00228 bool computeSecondOrderDerivatives_; 00229 bool initialized_; 00230 00231 public: 00232 AbstractTreeLikelihood(): 00233 AbstractParametrizable(""), 00234 data_(0), 00235 tree_(0), 00236 computeFirstOrderDerivatives_(true), 00237 computeSecondOrderDerivatives_(true), 00238 initialized_(false) {} 00239 00240 AbstractTreeLikelihood(const AbstractTreeLikelihood & lik): 00241 AbstractParametrizable(lik), 00242 data_(0), 00243 tree_(0), 00244 computeFirstOrderDerivatives_(lik.computeFirstOrderDerivatives_), 00245 computeSecondOrderDerivatives_(lik.computeSecondOrderDerivatives_), 00246 initialized_(lik.initialized_) 00247 { 00248 if (lik.data_) data_ = dynamic_cast<SiteContainer*>(lik.data_->clone()); 00249 if (lik.tree_) tree_ = lik.tree_->clone(); 00250 } 00251 00252 AbstractTreeLikelihood & operator=(const AbstractTreeLikelihood& lik) 00253 { 00254 AbstractParametrizable::operator=(lik); 00255 if (data_) delete data_; 00256 if (lik.data_) data_ = dynamic_cast<SiteContainer*>(lik.data_->clone()); 00257 else data_ = 0; 00258 if (tree_) delete tree_; 00259 if (lik.tree_) tree_ = lik.tree_->clone(); 00260 else tree_ = 0; 00261 computeFirstOrderDerivatives_ = lik.computeFirstOrderDerivatives_; 00262 computeSecondOrderDerivatives_ = lik.computeSecondOrderDerivatives_; 00263 initialized_ = lik.initialized_; 00264 return *this; 00265 } 00266 00272 virtual ~AbstractTreeLikelihood() 00273 { 00274 if (data_) delete data_; 00275 if (tree_) delete tree_; 00276 } 00277 00278 public: 00284 const SiteContainer* getData() const { return data_; } 00285 const Alphabet* getAlphabet() const { return data_->getAlphabet(); } 00286 Vdouble getLikelihoodForEachSite() const; 00287 Vdouble getLogLikelihoodForEachSite() const; 00288 VVdouble getLikelihoodForEachSiteForEachState() const; 00289 VVdouble getLogLikelihoodForEachSiteForEachState() const; 00290 size_t getNumberOfSites() const { return data_->getNumberOfSites(); } 00291 size_t getNumberOfStates() const { return data_->getAlphabet()->getSize(); } 00292 const Tree& getTree() const { return *tree_; } 00293 void enableDerivatives(bool yn) { computeFirstOrderDerivatives_ = computeSecondOrderDerivatives_ = yn; } 00294 void enableFirstOrderDerivatives(bool yn) { computeFirstOrderDerivatives_ = yn; } 00295 void enableSecondOrderDerivatives(bool yn) { computeFirstOrderDerivatives_ = computeSecondOrderDerivatives_ = yn; } 00296 bool enableFirstOrderDerivatives() const { return computeFirstOrderDerivatives_; } 00297 bool enableSecondOrderDerivatives() const { return computeSecondOrderDerivatives_; } 00298 bool isInitialized() const { return initialized_; } 00299 void initialize() throw (Exception) { initialized_ = true; } 00302 // protected: 00303 // 00304 // /** 00305 // * @brief Recompute pxy_, dpxy_ and d2pxy_ arrays, and derivatives if needed. 00306 // * 00307 // * This method is called when some parameter has changed. 00308 // * 00309 // * @param params The parameters that changed. 00310 // */ 00311 // virtual void fireParameterChanged(const ParameterList & params) = 0; 00312 00313 }; 00314 00315 } //end of namespace bpp. 00316 00317 #endif //_ABSTRACTTREELIKELIHOOD_H_ 00318