15#include "../App/PhylogeneticsApplicationTools.h"
16#include "../Model/Codon/DFP07.h"
17#include "../Model/Codon/RELAX.h"
18#include "../Model/Codon/YNGP_M1.h"
19#include "../Model/Codon/YNGP_M10.h"
20#include "../Model/Codon/YNGP_M2.h"
21#include "../Model/Codon/YNGP_M3.h"
22#include "../Model/Codon/YNGP_M7.h"
23#include "../Model/Codon/YNGP_M8.h"
24#include "../Model/Codon/YNGP_M9.h"
25#include "../Model/MixedTransitionModel.h"
26#include "../Model/MixtureOfATransitionModel.h"
27#include "../Model/MixtureOfTransitionModels.h"
28#include "../Model/OneChangeRegisterTransitionModel.h"
29#include "../Model/OneChangeTransitionModel.h"
30#include "../Model/Protein/LG10_EX_EHO.h"
31#include "../Model/Protein/LGL08_CAT.h"
32#include "../Model/Protein/LLG08_EHO.h"
33#include "../Model/Protein/LLG08_EX2.h"
34#include "../Model/Protein/LLG08_EX3.h"
35#include "../Model/Protein/LLG08_UL2.h"
36#include "../Model/Protein/LLG08_UL3.h"
57unique_ptr<TransitionModelInterface> BppOTransitionModelFormat::readTransitionModel(
58 std::shared_ptr<const Alphabet> alphabet,
59 const std::string& modelDescription,
60 const std::map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mData,
65 unique_ptr<TransitionModelInterface> model;
66 string modelName =
"";
67 map<string, string> args;
72 if (args.find(
"data") != args.end())
73 nData = TextTools::to<size_t>(args[
"data"]);
79 if ((modelName ==
"MixedModel" || (modelName ==
"Mixture")) &&
allowMixed_)
80 model =
readMixed_(alphabet, modelDescription, mData, nData);
81 else if (modelName ==
"OneChange")
84 if (args.find(
"model") == args.end())
85 throw Exception(
"BppOTransitionModelFormat::read. Missing argument 'model' for model 'OneChange'.");
86 string nestedModelDescription = args[
"model"];
91 auto nestedModel = nestedReader.
readSubstitutionModel(alphabet, nestedModelDescription, mData, nData,
false);
95 if (args.find(
"register") == args.end())
96 model = make_unique<OneChangeTransitionModel>(std::move(nestedModel));
99 shared_ptr<AlphabetIndex2> weights;
100 shared_ptr<AlphabetIndex2> distances;
101 string registerDescription = args[
"register"];
104 if (args.find(
"numReg") == args.end())
105 throw Exception(
"Missing argument 'numReg' (number of event for register in model " + modelName);
107 vector<size_t> vNumRegs;
115 size_t n = TextTools::to<size_t>(nst.
nextToken());
116 vNumRegs.push_back(n);
124 model = make_unique<OneChangeRegisterTransitionModel>(std::move(nestedModel), *reg, vNumRegs);
128 for (
auto& it : unparsedParameterValuesNested)
135 else if (((modelName.substr(0, 4) ==
"YNGP") || (modelName ==
"DFP07") || (modelName ==
"RELAX")) && (
alphabetCode_ &
CODON))
138 throw Exception(
"BppOTransitionModelFormat::read. Codon alphabet not supported.");
140 throw Exception(
"BppOTransitionModelFormat::readTransitionModel(). No genetic code specified! Consider using 'setGeneticCode'.");
143 throw Exception(
"Alphabet should be Codon Alphabet.");
144 auto pCA = dynamic_pointer_cast<const CodonAlphabet>(alphabet);
146 if (args.find(
"genetic_code") != args.end())
148 ApplicationTools::displayWarning(
"'genetic_code' argument is no longer supported inside model description, and has been supersided by a global 'genetic_code' option.");
149 throw Exception(
"BppOSubstitutionModelFormat::read. Deprecated 'genetic_code' argument.");
152 if (
geneticCode_->getSourceAlphabet()->getAlphabetType() != pCA->getAlphabetType())
153 throw Exception(
"Mismatch between genetic code and codon alphabet");
156 unique_ptr<CodonFrequencySetInterface> codonFreqs;
158 if (args.find(
"frequencies") != args.end())
172 throw Exception(
"Missing 'frequencies' for model " + modelName);
174 if (modelName ==
"YNGP_M1")
175 model = make_unique<YNGP_M1>(
geneticCode_, std::move(codonFreqs));
176 else if (modelName ==
"YNGP_M2")
177 model = make_unique<YNGP_M2>(
geneticCode_, std::move(codonFreqs));
178 else if (modelName ==
"RELAX")
179 model = make_unique<RELAX>(
geneticCode_, std::move(codonFreqs));
180 else if (modelName ==
"YNGP_M3")
181 if (args.find(
"n") == args.end())
182 model = make_unique<YNGP_M3>(
geneticCode_, std::move(codonFreqs));
184 model = make_unique<YNGP_M3>(
geneticCode_, std::move(codonFreqs), TextTools::to<unsigned int>(args[
"n"]));
185 else if ((modelName ==
"YNGP_M7") || modelName ==
"YNGP_M8" || modelName ==
"YNGP_M8a")
187 if (args.find(
"n") == args.end())
188 throw Exception(
"Missing argument 'n' (number of classes) in " + modelName +
" distribution");
189 unsigned int nbClasses = TextTools::to<unsigned int>(args[
"n"]);
193 if (modelName ==
"YNGP_M7")
194 model = make_unique<YNGP_M7>(
geneticCode_, std::move(codonFreqs), nbClasses);
195 else if (modelName ==
"YNGP_M8")
196 model = make_unique<YNGP_M8>(
geneticCode_, std::move(codonFreqs), nbClasses);
197 else if (modelName ==
"YNGP_M8a")
198 model = make_unique<YNGP_M8>(
geneticCode_, std::move(codonFreqs), nbClasses,
true);
200 else if (modelName ==
"YNGP_M9" || modelName ==
"YNGP_M10")
202 if (args.find(
"nbeta") == args.end())
203 throw Exception(
"Missing argument 'nbeta' (number of classes of beta distribution) in " + modelName +
" distribution");
204 unsigned int nbBeta = TextTools::to<unsigned int>(args[
"nbeta"]);
205 if (args.find(
"ngamma") == args.end())
206 throw Exception(
"Missing argument 'ngamma' (number of classes of gamma distribution) in " + modelName +
" distribution");
207 unsigned int nbGamma = TextTools::to<unsigned int>(args[
"ngamma"]);
211 if (modelName ==
"YNGP_M9")
212 model = make_unique<YNGP_M9>(
geneticCode_, std::move(codonFreqs), nbBeta, nbGamma);
214 model = make_unique<YNGP_M10>(
geneticCode_, std::move(codonFreqs), nbBeta, nbGamma);
216 else if (modelName ==
"DFP07")
218 if (args.find(
"protmodel") == args.end())
219 throw Exception(
"Missing 'protmodel in model " + modelName +
".");
224 args[
"protmodel"], mData, nData,
false);
225 auto nestedProtModel = unique_ptr<ProteinSubstitutionModelInterface>(
230 unparsedArguments_.insert(unparsedParameterValuesNested.begin(), unparsedParameterValuesNested.end());
232 model = make_unique<DFP07>(
geneticCode_, std::move(nestedProtModel), std::move(codonFreqs));
238 throw Exception(
"BppOTransitionModelFormat::read. Protein alphabet not supported.");
239 auto alpha = dynamic_pointer_cast<const ProteicAlphabet>(alphabet);
241 if (modelName ==
"LLG08_EHO")
242 model = make_unique<LLG08_EHO>(alpha);
243 else if (modelName ==
"LLG08_EX2")
244 model = make_unique<LLG08_EX2>(alpha);
245 else if (modelName ==
"LLG08_EX3")
246 model = make_unique<LLG08_EX3>(alpha);
247 else if (modelName ==
"LLG08_UL2")
248 model = make_unique<LLG08_UL2>(alpha);
249 else if (modelName ==
"LLG08_UL3")
250 model = make_unique<LLG08_UL3>(alpha);
251 else if (modelName ==
"LG10_EX_EHO")
252 model = make_unique<LG10_EX_EHO>(alpha);
253 else if (modelName ==
"LGL08_CAT")
255 if (args.find(
"nbCat") == args.end())
256 throw Exception(
"'nbCat' argument is compulsory for model 'LGL08_CAT'");
258 unsigned int nbCat = TextTools::to<unsigned int>(args[
"nbCat"]);
260 throw Exception(
"nbCat argument has to be 10, 20, 30, 40, 50 or 60.");
261 model = make_unique<LGL08_CAT>(alpha, nbCat);
287 std::shared_ptr<const Alphabet> alphabet,
288 const std::string& modelDescription,
289 const std::map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mData,
292 unique_ptr<MixedTransitionModelInterface> model;
294 string modelName =
"";
295 map<string, string> args;
297 unique_ptr<TransitionModelInterface> pSM;
299 if (modelName ==
"MixedModel")
301 if (args.find(
"model") == args.end())
302 throw Exception(
"The argument 'model' is missing from MixedModel description");
303 string nestedModelDescription = args[
"model"];
310 pSM = nestedReader.
readTransitionModel(alphabet, nestedModelDescription, mData, nData,
false);
314 map<string, unique_ptr<DiscreteDistributionInterface>> mdist;
315 map<string, string> unparsedParameterValuesNested2;
317 for (
auto& it : unparsedParameterValuesNested)
319 if (it.second.find(
"(") != string::npos)
324 for (
auto& it2 : unparsedParameterValuesNested3)
326 unparsedParameterValuesNested2[it.first +
"_" + it2.first] = it2.second;
330 unparsedParameterValuesNested2[it.first] = it.second;
333 for (
auto& it : unparsedParameterValuesNested2)
341 if (args.find(
"from") != args.end())
342 fi = alphabet->charToInt(args[
"from"]);
343 if (args.find(
"to") != args.end())
344 ti = alphabet->charToInt(args[
"to"]);
346 string sModN = pSM->getName();
347 model = make_unique<MixtureOfATransitionModel>(alphabet, std::move(pSM), mdist, fi, ti);
349 vector<string> v = model->getParameters().getParameterNames();
357 else if (modelName ==
"Mixture")
359 vector<string> v_nestedModelDescription;
360 vector< std::unique_ptr<TransitionModelInterface>> v_pSM;
362 if (args.find(
"model1") == args.end())
364 throw Exception(
"Missing argument 'model1' for model " + modelName +
".");
366 unsigned int nbmodels = 0;
374 throw Exception(
"Missing nested models for model " + modelName +
".");
376 for (
unsigned i = 0; i < v_nestedModelDescription.size(); ++i)
382 pSM = nestedReader.
readTransitionModel(alphabet, v_nestedModelDescription[i], mData, nData,
false);
385 for (
auto& it : unparsedParameterValuesNested)
390 v_pSM.push_back(std::move(pSM));
393 model = make_unique<MixtureOfTransitionModels>(alphabet, v_pSM);
398 throw Exception(
"Unknown model name for mixture " + modelName);
Parametrize a set of state frequencies for codons.
Specialized interface for protein substitution model.
const std::string & nextToken()
bool hasMoreToken() const
std::string toString(T t)
Defines the basic types of data flow nodes.