bpp-phyl-omics  2.2.0
MaximumLikelihoodModelFitMafStatistics.cpp
Go to the documentation of this file.
1 //
2 // File: MaximumLikelihoodModelFitMafStatistics.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mar 25 2014
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team
9 
10 This software is a computer program whose purpose is to test the
11 homogeneity of the substitution process of a given alignment.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
41 
42 //From bpp-seq:
43 #include <Bpp/Seq/Container/SiteContainerTools.h>
44 
45 //From bpp-phyl:
46 #include <Bpp/Phyl/Likelihood/RHomogeneousTreeLikelihood.h>
47 #include <Bpp/Phyl/Likelihood/RNonHomogeneousTreeLikelihood.h>
48 #include <Bpp/Phyl/Model/SubstitutionModelSetTools.h>
49 #include <Bpp/Phyl/OptimizationTools.h>
50 
51 using namespace bpp;
52 using namespace std;
53 
54 const string MaximumLikelihoodModelFitMafStatistics::NO_PROPERTY = "RESERVED_NOPROPERTY";
55 
57 {
58  //First we get the alignment:
59  auto_ptr<SiteContainer> sites(SiteContainerTools::removeGapSites(block.getAlignment(), propGapsToKeep_));
60  if (gapsAsUnresolved_)
61  SiteContainerTools::changeGapsToUnknownCharacters(*sites);
62 
63  //Second we get the tree:
64  const Tree* tree = 0;
65  if (!tree_.get()) {
66  //No default tree is given, we try to retrieve one from the block:
67  if (!block.hasProperty(treePropertyIn_))
68  throw Exception("MaximumLikelihoodModelFitMafIterator::fitModelBlock. No property available for " + treePropertyIn_);
69  try {
70  tree = &(dynamic_cast<const Tree&>(block.getProperty(treePropertyIn_)));
71  if (tree->isRooted())
72  throw Exception("MaximumLikelihoodModelFitMafIterator::fitModelBlock. Tree must be unrooted.");
73  } catch (bad_cast& e) {
74  throw Exception("MaximumLikelihoodModelFitMafIterator::fitModelBlock. A property was found for '" + treePropertyIn_ + "' but does not appear to contain a phylogenetic tree.");
75  }
76  } else {
77  tree = tree_.get();
78  }
79 
80  //We build a new TreeLikelihood object:
81  auto_ptr<DiscreteRatesAcrossSitesTreeLikelihood> tl;
82 
83  if (rootFreqs_.get()) {
84  modelSet_.reset(SubstitutionModelSetTools::createHomogeneousModelSet(model_->clone(), rootFreqs_->clone(), tree));
85  tl.reset(new RNonHomogeneousTreeLikelihood(*tree, *sites, modelSet_.get(), rDist_.get(), false, true, false));
86  //Initialize:
87  if (initParameters_.size() == 0)
88  init_(); //so far, even if tree changed, parameter names are supposingly the same. This might not be true in some complex cases...
89  } else {
90  tl.reset(new RHomogeneousTreeLikelihood(*tree, *sites, model_.get(), rDist_.get(), false, false, true));
91  }
92  tl->initialize();
93  tl->setParameters(fixedParameters_);
94 
95  //We optimize parameters:
96  unsigned int nbIt = OptimizationTools::optimizeNumericalParameters2(tl.get(), initParameters_, 0, 0.000001, 10000, 0, 0, false, false, 0);
97 
98  //And we save interesting parameter values:
99  result_.setValue("NbIterations", static_cast<double>(nbIt));
100  for (size_t i = 0;i < parametersOut_.size(); ++i) {
101  result_.setValue(parametersOut_[i], tl->getParameterValue(parametersOut_[i]));
102  }
103 }
104 
106  if (rootFreqs_.get()) {
107  initParameters_.addParameters(modelSet_->getIndependentParameters());
108  } else {
109  initParameters_.addParameters(model_->getIndependentParameters());
110  }
111  initParameters_.addParameters(rDist_->getIndependentParameters());
112  //Remove from initParameters the ones to consider fixed:
113  initParameters_.deleteParameters(fixedParameters_.getParameterNames());
114  }
115 
STL namespace.