19 map<size_t, std::shared_ptr<PhyloTree>> BppPhylogeneticsApplication::getPhyloTreesMap(
20 const map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mSites,
21 map<string, string>& unparsedParams,
22 const std::string& prefix,
23 const std::string& suffix,
24 bool suffixIsOptional)
const
26 map<size_t, std::shared_ptr<PhyloTree>> mpTree = PhylogeneticsApplicationTools::getPhyloTrees(params_, mSites, unparsedParams, prefix, suffix, suffixIsOptional, verbose_, warn_);
29 double scale = ApplicationTools::getDoubleParameter(
"input.tree.scale", params_, 1, suffix, suffixIsOptional, warn_);
35 ApplicationTools::displayResult(
"Trees are scaled by", scale);
38 for (
auto it : mpTree)
40 it.second->scaleTree(scale);
47 unique_ptr<SubstitutionProcessCollection> BppPhylogeneticsApplication::getCollection(
48 std::shared_ptr<const Alphabet> alphabet,
49 std::shared_ptr<const GeneticCode> gCode,
50 const map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mSites,
51 map<string, string>& unparsedParams,
52 const std::string& prefix,
53 const std::string& suffix,
54 bool suffixIsOptional)
const
56 auto mpTree = getPhyloTreesMap(mSites, unparsedParams, prefix, suffix, suffixIsOptional);
57 auto SPC = getCollection(alphabet, gCode, mSites, mpTree, unparsedParams, prefix, suffix, suffixIsOptional);
61 std::unique_ptr<SubstitutionProcessCollection> BppPhylogeneticsApplication::getCollection(
62 std::shared_ptr<const Alphabet> alphabet,
63 std::shared_ptr<const GeneticCode> gCode,
64 const map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mSites,
65 const map<
size_t, std::shared_ptr<PhyloTree>>& mpTree,
66 map<string, string>& unparsedParams,
67 const std::string& prefix,
68 const std::string& suffix,
69 bool suffixIsOptional)
const
71 auto mDist = PhylogeneticsApplicationTools::getRateDistributions(params_, suffix, suffixIsOptional, verbose_);
72 auto mModU = PhylogeneticsApplicationTools::getBranchModels(alphabet, gCode, mSites, params_, unparsedParams, suffix, suffixIsOptional, verbose_, warn_);
73 auto mMod = PhylogeneticsApplicationTools::uniqueToSharedMap<BranchModelInterface>(mModU);
74 auto mRootFreqU = PhylogeneticsApplicationTools::getRootFrequencySets(alphabet, gCode, mSites, params_, unparsedParams, suffix, suffixIsOptional, verbose_, warn_);
75 auto mRootFreq = PhylogeneticsApplicationTools::uniqueToSharedMap<FrequencySetInterface>(mRootFreqU);
76 auto mModelPathU = PhylogeneticsApplicationTools::getModelPaths(params_, mMod, suffix, suffixIsOptional, verbose_, warn_);
77 auto mModelPath = PhylogeneticsApplicationTools::uniqueToSharedMap<ModelPath>(mModelPathU);
78 auto mScenarioU = PhylogeneticsApplicationTools::getModelScenarios(params_, mModelPath, mMod, suffix, suffixIsOptional, verbose_, warn_);
79 auto mScenario = PhylogeneticsApplicationTools::uniqueToSharedMap<ModelScenario>(mScenarioU);
81 auto SPC = PhylogeneticsApplicationTools::getSubstitutionProcessCollection(alphabet, gCode, mpTree, mMod, mRootFreq, mDist, mScenario, params_, unparsedParams, suffix, suffixIsOptional, verbose_, warn_);
87 map<size_t, std::unique_ptr<SequenceEvolution>> BppPhylogeneticsApplication::getProcesses(
88 shared_ptr<SubstitutionProcessCollection> collection,
89 map<string, string>& unparsedParams,
90 const std::string& suffix,
91 bool suffixIsOptional)
const
93 return PhylogeneticsApplicationTools::getSequenceEvolutions(
94 collection, params_, unparsedParams, suffix, suffixIsOptional, verbose_, warn_);
98 std::shared_ptr<PhyloLikelihoodContainer> BppPhylogeneticsApplication::getPhyloLikelihoods(
100 map<
size_t, shared_ptr<SequenceEvolution>> mSeqEvol,
101 shared_ptr<SubstitutionProcessCollection> collection,
102 const map<
size_t, shared_ptr<const AlignmentDataInterface>>& mSites,
103 const std::string& suffix,
104 bool suffixIsOptional)
const
106 return PhylogeneticsApplicationTools::getPhyloLikelihoodContainer(
107 context, collection, mSeqEvol, mSites, params_, suffix, suffixIsOptional, verbose_, warn_);
111 void BppPhylogeneticsApplication::fixLikelihood(
112 shared_ptr<const Alphabet> alphabet,
113 shared_ptr<const GeneticCode> gCode,
114 shared_ptr<PhyloLikelihoodInterface> phylolik,
115 const std::string& suffix,
116 bool suffixIsOptional)
const
118 double logL = phylolik->getValue();
120 if (!std::isnormal(logL))
123 ApplicationTools::displayWarning(
"!!! Warning!!! Initial likelihood is zero.");
124 ApplicationTools::displayWarning(
"!!! This may be due to branch length == 0.");
125 ApplicationTools::displayWarning(
"!!! All null branch lengths will be set to 0.001.");
128 for (
size_t i = 0; i < pl.
size(); i++)
130 if (pl[i].getValue() < 0.000001)
131 pl[i].setValue(0.001);
134 logL = phylolik->getValue();
137 ApplicationTools::displayMessage(
"");
138 ApplicationTools::displayResult(
"Initial log likelihood", TextTools::toString(-logL, 15));
139 if (!std::isnormal(logL))
141 ApplicationTools::displayError(
"!!! Unexpected initial likelihood == 0.");
143 map<size_t, shared_ptr<SingleDataPhyloLikelihoodInterface>> mSD;
145 if (dynamic_pointer_cast<SingleDataPhyloLikelihoodInterface>(phylolik))
146 mSD[1] = dynamic_pointer_cast<SingleDataPhyloLikelihoodInterface>(phylolik);
149 auto sOAP = dynamic_pointer_cast<PhyloLikelihoodSetInterface>(phylolik);
152 const vector<size_t>& nSD = sOAP->getNumbersOfPhyloLikelihoods();
154 for (
size_t iSD = 0; iSD < nSD.size(); ++iSD)
156 auto pASDP = dynamic_pointer_cast<SingleDataPhyloLikelihoodInterface>(sOAP->getPhyloLikelihood(nSD[iSD]));
159 mSD[nSD[iSD]] = pASDP;
164 for (
auto& itm : mSD)
166 ApplicationTools::displayWarning(
"Checking for phyloLikelihood " + TextTools::toString(itm.first));
168 if (!std::isnormal(itm.second->getValue()))
170 auto sDP = itm.second;
172 auto vData = std::shared_ptr<AlignmentDataInterface>(sDP->getData()->clone());
174 auto vSC = std::dynamic_pointer_cast<SiteContainerInterface>(vData);
175 auto pSC = std::dynamic_pointer_cast<ProbabilisticSiteContainerInterface>(vData);
177 if (AlphabetTools::isCodonAlphabet(*alphabet))
181 for (
size_t i = 0; i < vData->getNumberOfSites(); ++i)
183 if (!std::isnormal(sDP->getLogLikelihoodForASite(i)))
187 const Site& site = vSC->site(i);
189 for (
size_t j = 0; j < s; ++j)
191 if (gCode->isStop(site.
getValue(j)))
193 (*ApplicationTools::error <<
"Stop Codon at site " << site.
getCoordinate() <<
" in sequence " << vData->sequence(j).getName()).endLine();
202 for (
size_t j = 0; j < s; ++j)
205 for (
int st = 0; !g && st < static_cast<int>(alphabet->getSize()); ++st)
212 (*ApplicationTools::error <<
"Only stop Codons at site " << site.
getCoordinate() <<
" in sequence " << vData->sequence(j).getName()).endLine();
225 bool removeSaturated = ApplicationTools::getBooleanParameter(
"input.sequence.remove_saturated_sites", params_,
false, suffix, suffixIsOptional, warn_);
230 ApplicationTools::displayBooleanResult(
"Saturated site removal enabled",
true);
231 for (
size_t i = vData->getNumberOfSites(); i > 0; --i)
233 if (!std::isnormal(sDP->getLogLikelihoodForASite(i - 1)))
235 ApplicationTools::displayResult(
"Ignore saturated site", vData->site(i - 1).getCoordinate());
236 vData->deleteSites(i - 1, 1);
239 ApplicationTools::displayResult(
"Number of sites retained", vData->getNumberOfSites());
244 logL = sDP->getValue();
246 vector<size_t> vsiteok, vsitemin;
248 if (!std::isnormal(logL))
250 ApplicationTools::displayError(
"!!! Removing problem sites:");
251 for (
unsigned int i = 0; i < vData->getNumberOfSites(); i++)
253 auto x = sDP->getLogLikelihoodForASite(i);
254 if (!std::isnormal(x))
256 (*ApplicationTools::error <<
"Site " << vData->site(i).getCoordinate() <<
"\tlog likelihood = " << x).endLine();
257 vsitemin.push_back(i);
260 vsiteok.push_back(i);
263 shared_ptr<AlignmentDataInterface> vDataok = SiteContainerTools::getSelectedSites(*vData, vsiteok);
266 sDP->setData(vDataok);
267 logL = sDP->getValue();
268 ApplicationTools::displayResult(
"Filtered log likelihood", TextTools::toString(-logL, 15));
277 ApplicationTools::displayResult(
"Initial log likelihood", TextTools::toString(-logL, 15));
280 ApplicationTools::displayResult(
"Initial log likelihood", TextTools::toString(-itm.second->getValue(), 15));
290 ApplicationTools::displayResult(
"Log likelihood", TextTools::toString(-tl.
getValue(), 15));
293 ApplicationTools::displayMessage(
"Too many parameters for screen output!");
298 for (
size_t i = 0; i < parameters.
size(); i++)
300 ApplicationTools::displayResult(parameters[i].getName(), TextTools::toString(parameters[i].getValue()));
int getCoordinate() const override
const int & getValue(size_t pos) const override
Context for dataflow node construction.
virtual size_t size() const=0
virtual double getValue() const=0
virtual std::vector< std::string > getParameterNames() const
virtual bool matchParametersValues(const ParameterList ¶ms, std::vector< size_t > *updatedParameters=0)
virtual void deleteParameters(const std::vector< std::string > &names, bool mustExist=true)
virtual size_t getNumberOfParameters() const=0
virtual const ParameterList & getParameters() const=0
The PhyloLikelihood interface, for phylogenetic likelihood.
virtual ParameterList getBranchLengthParameters() const =0
Get the independent branch lengths parameters.
double getStateValueAt(size_t sequencePosition, int state) const
Defines the basic types of data flow nodes.