bpp-phyl3 3.0.0
NucleotideFrequencySet.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
6
8
9using namespace bpp;
10
11#include <cmath>
12using namespace std;
13
14// ///////////////////////////////////////
15// FullNucleotideFrequencySet
16
17
18FullNucleotideFrequencySet::FullNucleotideFrequencySet(
19 std::shared_ptr<const NucleicAlphabet> alphabet,
20 bool allowNullFreqs,
21 const string& name) :
23 make_shared<CanonicalStateMap>(alphabet, false),
24 "Full.",
25 name)
26{
28 "Full.theta", 0.5,
29 allowNullFreqs ?
33 "Full.theta1", 0.5,
34 allowNullFreqs ?
37 addParameter_(new Parameter("Full.theta2", 0.5,
38 allowNullFreqs ?
41 getFreq_(0) = getFreq_(1) = getFreq_(2) = getFreq_(3) = 0.25;
42}
43
45 std::shared_ptr<const NucleicAlphabet> alphabet,
46 double theta,
47 double theta1,
48 double theta2,
49 bool allowNullFreqs,
50 const string& name) :
52 make_shared<CanonicalStateMap>(alphabet, false),
53 "Full.",
54 name)
55{
57 "Full.theta",
58 theta,
59 allowNullFreqs ?
63 "Full.theta1",
64 theta1,
65 allowNullFreqs ?
69 "Full.theta2",
70 theta2,
71 allowNullFreqs ?
74 getFreq_(0) = theta1 * (1. - theta);
75 getFreq_(1) = (1 - theta2) * theta;
76 getFreq_(2) = theta2 * theta;
77 getFreq_(3) = (1 - theta1) * (1. - theta);
78}
79
80void FullNucleotideFrequencySet::setFrequencies(const vector<double>& frequencies)
81{
82 if (frequencies.size() != 4)
83 throw DimensionException(" FullNucleotideFrequencySet::setFrequencies", frequencies.size(), 4);
84 double sum = 0.0;
85 for (auto& f : frequencies)
86 {
87 sum += f;
88 }
89 if (fabs(1. - sum) > NumConstants::SMALL())
90 throw Exception("FullNucleotideFrequencySet::setFrequencies. Frequencies must equal 1 (sum = " + TextTools::toString(sum) + ").");
91 double theta = frequencies[1] + frequencies[2];
92 getParameter_(0).setValue(theta);
93 getParameter_(1).setValue(frequencies[0] / (1 - theta));
94 getParameter_(2).setValue(frequencies[2] / theta);
95
96 setFrequencies_(frequencies);
97}
98
100{
101 double theta = getParameter_(0).getValue();
102 double theta1 = getParameter_(1).getValue();
103 double theta2 = getParameter_(2).getValue();
104 getFreq_(0) = theta1 * (1. - theta);
105 getFreq_(1) = (1 - theta2) * theta;
106 getFreq_(2) = theta2 * theta;
107 getFreq_(3) = (1 - theta1) * (1. - theta);
108}
109
110// /////////////////////////////////////////
111// GCFrequencySet
112
113void GCFrequencySet::setFrequencies(const vector<double>& frequencies)
114{
115 if (frequencies.size() != 4)
116 throw DimensionException("GCFrequencySet::setFrequencies", frequencies.size(), 4);
117 double sum = 0.0;
118 for (unsigned int i = 0; i < 4; i++)
119 {
120 sum += frequencies[i];
121 }
122 if (fabs(1. - sum) > NumConstants::SMALL())
123 throw Exception("GCFrequencySet::setFrequencies. Frequencies must equal 1 (sum = " + TextTools::toString(sum) + ").");
124 double theta = frequencies[1] + frequencies[2];
125 // We set everything in one shot here:
126 getParameter_(0).setValue(theta);
127 getFreq_(0) = getFreq_(3) = (1. - theta) / 2.;
128 getFreq_(1) = getFreq_(2) = theta / 2.;
129}
130
132{
133 double theta = getParameter_(0).getValue();
134 getFreq_(0) = getFreq_(3) = (1. - theta) / 2.;
135 getFreq_(1) = getFreq_(2) = theta / 2.;
136}
Basic implementation of the FrequencySet interface.
Definition: FrequencySet.h:102
void setFrequencies_(const std::vector< double > &frequencies)
Definition: FrequencySet.h:181
double & getFreq_(size_t i)
Definition: FrequencySet.h:179
void addParameter_(Parameter *parameter)
Parameter & getParameter_(const std::string &name)
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
static std::shared_ptr< IntervalConstraint > FREQUENCE_CONSTRAINT_CENTI
Definition: FrequencySet.h:92
void fireParameterChanged(const ParameterList &parameters) override
FullNucleotideFrequencySet(std::shared_ptr< const NucleicAlphabet > alphabet, bool allowNullFreqs=false, const std::string &name="Full")
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
void fireParameterChanged(const ParameterList &parameters) override
static double SMALL()
virtual void setValue(double value)
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
virtual double getValue() const
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
std::string toString(T t)
Defines the basic types of data flow nodes.