bpp-phyl3  3.0.0
BppOBranchModelFormat.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
7 #include <Bpp/Io/FileTools.h>
8 #include <Bpp/Io/OutputStream.h>
11 #include <Bpp/Text/KeyvalTools.h>
13 #include <Bpp/Text/TextTools.h>
14 
15 #include "../App/PhylogeneticsApplicationTools.h"
16 #include "../Model/MultinomialFromTransitionModel.h"
17 #include "../Model/TransitionFromTransitionModel.h"
18 #include "BppOBranchModelFormat.h"
19 #include "BppOFrequencySetFormat.h"
20 
21 // From Numeric
22 
25 
26 
28 
29 using namespace bpp;
30 
31 // From the STL:
32 #include <iomanip>
33 #include <set>
34 
35 using namespace std;
36 
37 std::unique_ptr<BranchModelInterface> BppOBranchModelFormat::readBranchModel(
38  shared_ptr<const Alphabet> alphabet,
39  const string& modelDescription,
40  const std::map<size_t, std::shared_ptr<const AlignmentDataInterface>>& mData,
41  size_t nData,
42  bool parseArguments)
43 {
44  unparsedArguments_.clear();
45  unique_ptr<BranchModelInterface> model;
46  string modelName = "";
47  map<string, string> args;
48  KeyvalTools::parseProcedure(modelDescription, modelName, args);
49 
50  // get data number
51 
52  if (args.find("data") != args.end())
53  nData = TextTools::to<size_t>(args["data"]);
54 
55 
56  // //////////////////////////////////
57  // / MIXED MODELS
58  // ////////////////////////////////
59 
60  if (modelName == "Multinomial")
61  {
62  // We have to parse the nested model first:
63  if (args.find("model") == args.end())
64  throw Exception("BppOBranchModelFormat::read. Missing argument 'model' for model " + modelName);
65  string nestedModelDescription = args["model"];
66  BppOTransitionModelFormat nestedReader(ALL, false, allowMixed_, allowGaps_, verbose_, warningLevel_);
67  if (geneticCode_)
68  nestedReader.setGeneticCode(geneticCode_);
69 
70  std::shared_ptr<TransitionModelInterface> nestedModel = nestedReader.readTransitionModel(alphabet, nestedModelDescription, mData, nData, false);
71  map<string, string> unparsedParameterValuesNested(nestedReader.getUnparsedArguments());
72 
73  model.reset(new MultinomialFromTransitionModel(nestedModel));
74  }
75 
76  if (!model)
77  model = readTransitionModel(alphabet, modelDescription, mData, nData, parseArguments);
78  else
79  {
80  if (verbose_)
81  ApplicationTools::displayResult("Branch model", modelName);
82 
83  updateParameters_(*model, args);
84 
85  if (parseArguments)
86  {
87  if (nData)
88  initialize_(*model, mData.at(nData));
89  else
90  initialize_(*model, 0);
91  }
92  }
93 
94  if (verbose_)
95  {
96  if (nData != 0)
98  }
99 
100 
101  return model;
102 }
static void displayResult(const std::string &text, const T &result)
std::unique_ptr< BranchModelInterface > readBranchModel(std::shared_ptr< const Alphabet > alphabet, const std::string &modelDescription, const std::map< size_t, std::shared_ptr< const AlignmentDataInterface >> &mData, size_t nData, bool parseArguments=true)
const std::map< std::string, std::string > & getUnparsedArguments() const override
void setGeneticCode(std::shared_ptr< const GeneticCode > gCode)
Set the genetic code to use in case a codon frequencies set should be built.
Transition model I/O in BppO format.
std::unique_ptr< TransitionModelInterface > readTransitionModel(std::shared_ptr< const Alphabet > alphabet, const std::string &modelDescription, const std::map< size_t, std::shared_ptr< const AlignmentDataInterface >> &mData, size_t nData, bool parseArguments=true)
static void parseProcedure(const std::string &desc, std::string &name, std::map< std::string, std::string > &args)
From a model, compute the likelihood of counts given an ancestral state.
std::string toString(T t)
Defines the basic types of data flow nodes.