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"
58 std::shared_ptr<const Alphabet> alphabet,
59 const std::string& modelDescription,
60 const std::map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mData,
64 unparsedArguments_.clear();
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)
130 unparsedArguments_[
"OneChange." + it.first] = it.second;
135 else if (((modelName.substr(0, 4) ==
"YNGP") || (modelName ==
"DFP07") || (modelName ==
"RELAX")) && (alphabetCode_ & CODON))
137 if (!(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())
168 unparsedArguments_[modelName +
"." + it.first] = it.second;
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 +
".");
223 geneticCode_->getTargetAlphabet(),
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));
237 if (!(alphabetCode_ & PROTEIN))
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"]);
259 model = make_unique<LGL08_CAT>(alpha, nbCat);
264 model = readSubstitutionModel(alphabet, modelDescription, mData, nData, parseArguments);
270 updateParameters_(*model, args);
275 initialize_(*model, mData.at(nData));
277 initialize_(*model, 0);
285 std::shared_ptr<const Alphabet> alphabet,
286 const std::string& modelDescription,
287 const std::map<
size_t, std::shared_ptr<const AlignmentDataInterface>>& mData,
290 unique_ptr<MixedTransitionModelInterface> model;
292 string modelName =
"";
293 map<string, string> args;
295 unique_ptr<TransitionModelInterface> pSM;
297 if (modelName ==
"MixedModel")
299 if (args.find(
"model") == args.end())
300 throw Exception(
"The argument 'model' is missing from MixedModel description");
301 string nestedModelDescription = args[
"model"];
308 pSM = nestedReader.
readTransitionModel(alphabet, nestedModelDescription, mData, nData,
false);
312 map<string, unique_ptr<DiscreteDistributionInterface>> mdist;
313 map<string, string> unparsedParameterValuesNested2;
315 for (
auto& it : unparsedParameterValuesNested)
317 if (it.second.find(
"(") != string::npos)
322 for (
auto& it2 : unparsedParameterValuesNested3)
324 unparsedParameterValuesNested2[it.first +
"_" + it2.first] = it2.second;
328 unparsedParameterValuesNested2[it.first] = it.second;
331 for (
auto& it : unparsedParameterValuesNested2)
333 unparsedArguments_[it.first] = it.second;
339 if (args.find(
"from") != args.end())
340 fi = alphabet->charToInt(args[
"from"]);
341 if (args.find(
"to") != args.end())
342 ti = alphabet->charToInt(args[
"to"]);
344 string sModN = pSM->getName();
345 model = make_unique<MixtureOfATransitionModel>(alphabet, std::move(pSM), mdist, fi, ti);
347 vector<string> v = model->getParameters().getParameterNames();
355 else if (modelName ==
"Mixture")
357 vector<string> v_nestedModelDescription;
358 vector< std::unique_ptr<TransitionModelInterface>> v_pSM;
360 if (args.find(
"model1") == args.end())
362 throw Exception(
"Missing argument 'model1' for model " + modelName +
".");
364 unsigned int nbmodels = 0;
372 throw Exception(
"Missing nested models for model " + modelName +
".");
374 for (
unsigned i = 0; i < v_nestedModelDescription.size(); ++i)
380 pSM = nestedReader.
readTransitionModel(alphabet, v_nestedModelDescription[i], mData, nData,
false);
383 for (
auto& it : unparsedParameterValuesNested)
385 unparsedArguments_[modelName +
"." +
TextTools::toString(i + 1) +
"_" + it.first] = it.second;
388 v_pSM.push_back(std::move(pSM));
391 model = make_unique<MixtureOfTransitionModels>(alphabet, v_pSM);
396 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.