bpp-phyl  2.2.0
NucleotideFrequenciesSet.cpp
Go to the documentation of this file.
1 //
2 // File: NucleotideFrequenciesSet.cpp
3 // Created by: Bastien Boussau
4 // Julien Dutheil
5 // Created on: Tue Aug 21 2007
6 //
7 
8 /*
9  Copyright or (c) or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
42 
43 #include <Bpp/Numeric/NumConstants.h>
44 
45 using namespace bpp;
46 
47 #include <cmath>
48 using namespace std;
49 
50 // ///////////////////////////////////////
51 // FullNucleotideFrequenciesSet
52 
53 
55  const NucleicAlphabet* alphabet, bool allowNullFreqs,
56  const string& name) :
57  AbstractFrequenciesSet(new CanonicalStateMap(alphabet, false), "Full.", name)
58 {
59  addParameter_(new Parameter(
60  "Full.theta", 0.5,
61  allowNullFreqs ?
62  &Parameter::PROP_CONSTRAINT_IN :
64  addParameter_(new Parameter(
65  "Full.theta1", 0.5,
66  allowNullFreqs ?
67  &Parameter::PROP_CONSTRAINT_IN :
69  addParameter_(new Parameter("Full.theta2", 0.5,
70  allowNullFreqs ?
71  &Parameter::PROP_CONSTRAINT_IN :
73  getFreq_(0) = getFreq_(1) = getFreq_(2) = getFreq_(3) = 0.25;
74 }
75 
77  const NucleicAlphabet* alphabet, double theta, double theta1, double theta2,
78  bool allowNullFreqs, const string& name) :
79  AbstractFrequenciesSet(new CanonicalStateMap(alphabet, false), "Full.", name)
80 {
81  addParameter_(new Parameter(
82  "Full.theta",
83  theta,
84  allowNullFreqs ?
85  &Parameter::PROP_CONSTRAINT_IN :
87  addParameter_(new Parameter(
88  "Full.theta1",
89  theta1,
90  allowNullFreqs ?
91  &Parameter::PROP_CONSTRAINT_IN :
93  addParameter_(new Parameter(
94  "Full.theta2",
95  theta2,
96  allowNullFreqs ?
97  &Parameter::PROP_CONSTRAINT_IN :
98  &Parameter::PROP_CONSTRAINT_EX));
99  getFreq_(0) = theta1 * (1. - theta);
100  getFreq_(1) = (1 - theta2) * theta;
101  getFreq_(2) = theta2 * theta;
102  getFreq_(3) = (1 - theta1) * (1. - theta);
103 }
104 
105 void FullNucleotideFrequenciesSet::setFrequencies(const vector<double>& frequencies)
106 {
107  if (frequencies.size() != 4) throw DimensionException(" FullNucleotideFrequenciesSet::setFrequencies", frequencies.size(), 4);
108  double sum = 0.0;
109  for (unsigned int i = 0; i < 4; i++)
110  {
111  sum += frequencies[i];
112  }
113  if (fabs(1. - sum) > NumConstants::SMALL())
114  throw Exception("FullNucleotideFrequenciesSet::setFrequencies. Frequencies must equal 1 (sum = " + TextTools::toString(sum) + ").");
115  double theta = frequencies[1] + frequencies[2];
116  getParameter_(0).setValue(theta);
117  getParameter_(1).setValue(frequencies[0] / (1 - theta));
118  getParameter_(2).setValue(frequencies[2] / theta);
119 
120  setFrequencies_(frequencies);
121 }
122 
123 void FullNucleotideFrequenciesSet::fireParameterChanged(const ParameterList& parameters)
124 {
125  double theta = getParameter_(0).getValue();
126  double theta1 = getParameter_(1).getValue();
127  double theta2 = getParameter_(2).getValue();
128  getFreq_(0) = theta1 * (1. - theta);
129  getFreq_(1) = (1 - theta2) * theta;
130  getFreq_(2) = theta2 * theta;
131  getFreq_(3) = (1 - theta1) * (1. - theta);
132 }
133 
134 // /////////////////////////////////////////
135 // GCFrequenciesSet
136 
137 void GCFrequenciesSet::setFrequencies(const vector<double>& frequencies)
138 {
139  if (frequencies.size() != 4) throw DimensionException("GCFrequenciesSet::setFrequencies", frequencies.size(), 4);
140  double sum = 0.0;
141  for (unsigned int i = 0; i < 4; i++)
142  {
143  sum += frequencies[i];
144  }
145  if (fabs(1. - sum) > NumConstants::SMALL())
146  throw Exception("GCFrequenciesSet::setFrequencies. Frequencies must equal 1 (sum = " + TextTools::toString(sum) + ").");
147  double theta = frequencies[1] + frequencies[2];
148  // We set everything in one shot here:
149  getParameter_(0).setValue(theta);
150  getFreq_(0) = getFreq_(3) = (1. - theta) / 2.;
151  getFreq_(1) = getFreq_(2) = theta / 2.;
152 }
153 
154 void GCFrequenciesSet::fireParameterChanged(const ParameterList& parameters)
155 {
156  double theta = getParameter_(0).getValue();
157  getFreq_(0) = getFreq_(3) = (1. - theta) / 2.;
158  getFreq_(1) = getFreq_(2) = theta / 2.;
159 }
160 
161 
void fireParameterChanged(const ParameterList &parameters)
void fireParameterChanged(const ParameterList &parameters)
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
void setFrequencies_(const std::vector< double > &frequencies)
STL namespace.
FullNucleotideFrequenciesSet(const NucleicAlphabet *alphabet, bool allowNullFreqs=false, const std::string &name="Full")
static IntervalConstraint FREQUENCE_CONSTRAINT_SMALL
double & getFreq_(size_t i)
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
Basic implementation of the FrequenciesSet interface.