本文整理汇总了C++中TreeTemplate::getNodesId方法的典型用法代码示例。如果您正苦于以下问题:C++ TreeTemplate::getNodesId方法的具体用法?C++ TreeTemplate::getNodesId怎么用?C++ TreeTemplate::getNodesId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TreeTemplate
的用法示例。
在下文中一共展示了TreeTemplate::getNodesId方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
TreeTemplate<Node>* tree = TreeTemplateTools::parenthesisToTree("((A:0.001, B:0.002):0.003,C:0.01,D:0.1);");
cout << tree->getNumberOfLeaves() << endl;
vector<int> ids = tree->getNodesId();
//-------------
NucleicAlphabet* alphabet = new DNA();
SubstitutionModel* model = new GTR(alphabet, 1, 0.2, 0.3, 0.4, 0.4, 0.1, 0.35, 0.35, 0.2);
//DiscreteDistribution* rdist = new GammaDiscreteDistribution(4, 0.4, 0.4);
DiscreteDistribution* rdist = new ConstantDistribution(1.0);
HomogeneousSequenceSimulator simulator(model, rdist, tree);
unsigned int n = 100000;
map<int, RowMatrix<unsigned int> > counts;
for (size_t j = 0; j < ids.size() - 1; ++j) //ignore root, the last id
counts[ids[j]].resize(4, 4);
for (unsigned int i = 0; i < n; ++i) {
RASiteSimulationResult* result = simulator.dSimulate();
for (size_t j = 0; j < ids.size() - 1; ++j) { //ignore root, the last id
result->getMutationPath(ids[j]).getEventCounts(counts[ids[j]]);
}
delete result;
}
map<int, RowMatrix<double> >freqs;
map<int, double> sums;
for (size_t k = 0; k < ids.size() - 1; ++k) { //ignore root, the last id
RowMatrix<double>* freqsP = &freqs[ids[k]];
RowMatrix<unsigned int>* countsP = &counts[ids[k]];
freqsP->resize(4, 4);
for (unsigned int i = 0; i < 4; ++i)
for (unsigned int j = 0; j < 4; ++j)
(*freqsP)(i, j) = static_cast<double>((*countsP)(i, j)) / (static_cast<double>(n));
//For now we simply compare the total number of substitutions:
sums[ids[k]] = MatrixTools::sumElements(*freqsP);
cout << "Br" << ids[k] << " BrLen = " << tree->getDistanceToFather(ids[k]) << " counts = " << sums[ids[k]] << endl;
MatrixTools::print(*freqsP);
}
//We should compare this matrix with the expected one!
for (size_t k = 0; k < ids.size() - 1; ++k) { //ignore root, the last id
if (abs(sums[ids[k]] - tree->getDistanceToFather(ids[k])) > 0.01) {
delete tree;
delete alphabet;
delete model;
delete rdist;
return 1;
}
}
//-------------
delete tree;
delete alphabet;
delete model;
delete rdist;
//return (abs(obs - 0.001) < 0.001 ? 0 : 1);
return 0;
}
示例2: main
int main() {
TreeTemplate<Node>* tree = TreeTemplateTools::parenthesisToTree("(((A:0.01, B:0.01):0.02,C:0.03):0.01,D:0.04);");
vector<string> seqNames = tree->getLeavesNames();
vector<int> ids = tree->getNodesId();
//-------------
const NucleicAlphabet* alphabet = &AlphabetTools::DNA_ALPHABET;
SubstitutionModel* model = new T92(alphabet, 3.);
DiscreteDistribution* rdist = new GammaDiscreteDistribution(4, 1.0);
rdist->aliasParameters("alpha", "beta");
VectorSiteContainer sites(alphabet);
sites.addSequence(BasicSequence("A", "AAATGGCTGTGCACGTC", alphabet));
sites.addSequence(BasicSequence("B", "AACTGGATCTGCATGTC", alphabet));
sites.addSequence(BasicSequence("C", "ATCTGGACGTGCACGTG", alphabet));
sites.addSequence(BasicSequence("D", "CAACGGGAGTGCGCCTA", alphabet));
try {
fitModelH(model, rdist, *tree, sites, 93.017264552603336369, 71.265543199977557265);
} catch (Exception& ex) {
cerr << ex.what() << endl;
return 1;
}
try {
fitModelHClock(model, rdist, *tree, sites, 92.27912072473920090943, 71.26554020984087856050);
} catch (Exception& ex) {
cerr << ex.what() << endl;
return 1;
}
//-------------
delete tree;
delete model;
delete rdist;
return 0;
}
示例3: main
int main() {
TreeTemplate<Node>* tree = TreeTemplateTools::parenthesisToTree("((A:0.01, B:0.02):0.03,C:0.01,D:0.1);");
vector<string> seqNames= tree->getLeavesNames();
vector<int> ids = tree->getNodesId();
//-------------
NucleicAlphabet* alphabet = new DNA();
SubstitutionModel* model = new T92(alphabet, 3.);
FrequenciesSet* rootFreqs = new GCFrequenciesSet(alphabet);
std::vector<std::string> globalParameterNames;
globalParameterNames.push_back("T92.kappa");
map<string, string> alias;
SubstitutionModelSet* modelSet = SubstitutionModelSetTools::createNonHomogeneousModelSet(model, rootFreqs, tree, alias, globalParameterNames);
DiscreteDistribution* rdist = new ConstantRateDistribution();
vector<double> thetas;
for (unsigned int i = 0; i < modelSet->getNumberOfModels(); ++i) {
double theta = RandomTools::giveRandomNumberBetweenZeroAndEntry(0.99) + 0.005;
cout << "Theta" << i << " set to " << theta << endl;
modelSet->setParameterValue("T92.theta_" + TextTools::toString(i + 1), theta);
thetas.push_back(theta);
}
NonHomogeneousSequenceSimulator simulator(modelSet, rdist, tree);
unsigned int n = 100000;
OutputStream* profiler = new StlOutputStream(new ofstream("profile.txt", ios::out));
OutputStream* messenger = new StlOutputStream(new ofstream("messages.txt", ios::out));
//Check fast simulation first:
cout << "Fast check:" << endl;
//Generate data set:
VectorSiteContainer sites(seqNames, alphabet);
for (unsigned int i = 0; i < n; ++i) {
auto_ptr<Site> site(simulator.simulateSite());
site->setPosition(static_cast<int>(i));
sites.addSite(*site, false);
}
//Now fit model:
SubstitutionModelSet* modelSet2 = modelSet->clone();
RNonHomogeneousTreeLikelihood tl(*tree, sites, modelSet2, rdist);
tl.initialize();
OptimizationTools::optimizeNumericalParameters2(
&tl, tl.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
//Now compare estimated values to real ones:
for (size_t i = 0; i < thetas.size(); ++i) {
cout << thetas[i] << "\t" << modelSet2->getModel(i)->getParameter("theta").getValue() << endl;
double diff = abs(thetas[i] - modelSet2->getModel(i)->getParameter("theta").getValue());
if (diff > 0.1)
return 1;
}
delete modelSet2;
//Now try detailed simulations:
cout << "Detailed check:" << endl;
//Generate data set:
VectorSiteContainer sites2(seqNames, alphabet);
for (unsigned int i = 0; i < n; ++i) {
RASiteSimulationResult* result = simulator.dSimulateSite();
auto_ptr<Site> site(result->getSite(*simulator.getSubstitutionModelSet()->getModel(0)));
site->setPosition(static_cast<int>(i));
sites2.addSite(*site, false);
delete result;
}
//Now fit model:
SubstitutionModelSet* modelSet3 = modelSet->clone();
RNonHomogeneousTreeLikelihood tl2(*tree, sites2, modelSet3, rdist);
tl2.initialize();
OptimizationTools::optimizeNumericalParameters2(
&tl2, tl2.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
//Now compare estimated values to real ones:
for (size_t i = 0; i < thetas.size(); ++i) {
cout << thetas[i] << "\t" << modelSet3->getModel(i)->getParameter("theta").getValue() << endl;
double diff = abs(thetas[i] - modelSet3->getModel(i)->getParameter("theta").getValue());
if (diff > 0.1)
return 1;
}
delete modelSet3;
//-------------
delete tree;
delete alphabet;
delete modelSet;
delete rdist;
return 0;
}
示例4: main
int main() {
TreeTemplate<Node>* tree = TreeTemplateTools::parenthesisToTree("(((A:0.1, B:0.2):0.3,C:0.1):0.2,(D:0.3,(E:0.2,F:0.05):0.1):0.1);");
vector<string> seqNames= tree->getLeavesNames();
vector<int> ids = tree->getNodesId();
//-------------
const NucleicAlphabet* alphabet = &AlphabetTools::DNA_ALPHABET;
FrequenciesSet* rootFreqs = new GCFrequenciesSet(alphabet);
SubstitutionModel* model = new T92(alphabet, 3.);
std::vector<std::string> globalParameterNames;
globalParameterNames.push_back("T92.kappa");
map<string, string> alias;
SubstitutionModelSet* modelSet = SubstitutionModelSetTools::createNonHomogeneousModelSet(model, rootFreqs, tree, alias, globalParameterNames);
//DiscreteDistribution* rdist = new ConstantDistribution(1.0, true);
//Very difficult to optimize on small datasets:
DiscreteDistribution* rdist = new GammaDiscreteRateDistribution(4, 1.0);
size_t nsites = 1000;
unsigned int nrep = 20;
size_t nmodels = modelSet->getNumberOfModels();
vector<double> thetas(nmodels);
vector<double> thetasEst1(nmodels);
vector<double> thetasEst2(nmodels);
for (size_t i = 0; i < nmodels; ++i) {
double theta = RandomTools::giveRandomNumberBetweenZeroAndEntry(0.99) + 0.005;
cout << "Theta" << i << " set to " << theta << endl;
modelSet->setParameterValue("T92.theta_" + TextTools::toString(i + 1), theta);
thetas[i] = theta;
}
NonHomogeneousSequenceSimulator simulator(modelSet, rdist, tree);
for (unsigned int j = 0; j < nrep; j++) {
OutputStream* profiler = new StlOutputStream(new ofstream("profile.txt", ios::out));
OutputStream* messenger = new StlOutputStream(new ofstream("messages.txt", ios::out));
//Simulate data:
auto_ptr<SiteContainer> sites(simulator.simulate(nsites));
//Now fit model:
auto_ptr<SubstitutionModelSet> modelSet2(modelSet->clone());
auto_ptr<SubstitutionModelSet> modelSet3(modelSet->clone());
RNonHomogeneousTreeLikelihood tl(*tree, *sites.get(), modelSet2.get(), rdist, true, true, false);
tl.initialize();
RNonHomogeneousTreeLikelihood tl2(*tree, *sites.get(), modelSet3.get(), rdist, true, true, true);
tl2.initialize();
unsigned int c1 = OptimizationTools::optimizeNumericalParameters2(
&tl, tl.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
unsigned int c2 = OptimizationTools::optimizeNumericalParameters2(
&tl2, tl2.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
cout << c1 << ": " << tl.getValue() << "\t" << c2 << ": " << tl2.getValue() << endl;
for (size_t i = 0; i < nmodels; ++i) {
cout << modelSet2->getModel(i)->getParameter("theta").getValue() << "\t" << modelSet3->getModel(i)->getParameter("theta").getValue() << endl;
//if (abs(modelSet2->getModel(i)->getParameter("theta").getValue() - modelSet3->getModel(i)->getParameter("theta").getValue()) > 0.1)
// return 1;
thetasEst1[i] += modelSet2->getModel(i)->getParameter("theta").getValue();
thetasEst2[i] += modelSet3->getModel(i)->getParameter("theta").getValue();
}
}
thetasEst1 /= static_cast<double>(nrep);
thetasEst2 /= static_cast<double>(nrep);
//Now compare estimated values to real ones:
for (size_t i = 0; i < thetas.size(); ++i) {
cout << thetas[i] << "\t" << thetasEst1[i] << "\t" << thetasEst2[i] << endl;
double diff1 = abs(thetas[i] - thetasEst1[i]);
double diff2 = abs(thetas[i] - thetasEst2[i]);
if (diff1 > 0.2 || diff2 > 0.2)
return 1;
}
//-------------
delete tree;
delete modelSet;
delete rdist;
return 0;
}
示例5: main
int main() {
TreeTemplate<Node>* tree = TreeTemplateTools::parenthesisToTree("((A:0.001, B:0.002):0.008,C:0.01,D:0.1);");
vector<int> ids = tree->getNodesId();
ids.pop_back(); //Ignore root
//-------------
CodonAlphabet* alphabet = new CodonAlphabet(&AlphabetTools::DNA_ALPHABET);
GeneticCode* gc = new StandardGeneticCode(&AlphabetTools::DNA_ALPHABET);
CodonSubstitutionModel* model = new YN98(gc, CodonFrequenciesSet::getFrequenciesSetForCodons(CodonFrequenciesSet::F0, gc));
//SubstitutionModel* model = new CodonRateSubstitutionModel(
// gc,
// new JCnuc(dynamic_cast<CodonAlphabet*>(alphabet)->getNucleicAlphabet()));
cout << model->getNumberOfStates() << endl;
MatrixTools::printForR(model->getGenerator(), "g");
DiscreteDistribution* rdist = new ConstantDistribution(1.0);
HomogeneousSequenceSimulator simulator(model, rdist, tree);
TotalSubstitutionRegister* totReg = new TotalSubstitutionRegister(model);
DnDsSubstitutionRegister* dndsReg = new DnDsSubstitutionRegister(model);
unsigned int n = 20000;
vector< vector<double> > realMap(n);
vector< vector< vector<double> > > realMapTotal(n);
vector< vector< vector<double> > > realMapDnDs(n);
VectorSiteContainer sites(tree->getLeavesNames(), alphabet);
for (unsigned int i = 0; i < n; ++i) {
ApplicationTools::displayGauge(i, n-1, '=');
RASiteSimulationResult* result = simulator.dSimulateSite();
realMap[i].resize(ids.size());
realMapTotal[i].resize(ids.size());
realMapDnDs[i].resize(ids.size());
for (size_t j = 0; j < ids.size(); ++j) {
realMap[i][j] = static_cast<double>(result->getSubstitutionCount(ids[j]));
realMapTotal[i][j].resize(totReg->getNumberOfSubstitutionTypes());
realMapDnDs[i][j].resize(dndsReg->getNumberOfSubstitutionTypes());
result->getSubstitutionCount(ids[j], *totReg, realMapTotal[i][j]);
result->getSubstitutionCount(ids[j], *dndsReg, realMapDnDs[i][j]);
if (realMapTotal[i][j][0] != realMap[i][j]) {
cerr << "Error, total substitution register provides wrong result." << endl;
return 1;
}
//if (abs(VectorTools::sum(realMapDetailed[i][j]) - realMap[i][j]) > 0.000001) {
// cerr << "Error, detailed substitution register provides wrong result." << endl;
// return 1;
//}
}
auto_ptr<Site> site(result->getSite(*model));
site->setPosition(static_cast<int>(i));
sites.addSite(*site, false);
delete result;
}
ApplicationTools::displayTaskDone();
//-------------
//Now build the substitution vectors with the true model:
//Fasta fasta;
//fasta.write("Simulations.fasta", sites);
DRHomogeneousTreeLikelihood drhtl(*tree, sites, model, rdist);
drhtl.initialize();
cout << drhtl.getValue() << endl;
SubstitutionCount* sCountAna = new LaplaceSubstitutionCount(model, 10);
Matrix<double>* m = sCountAna->getAllNumbersOfSubstitutions(0.001,1);
cout << "Analytical total count:" << endl;
MatrixTools::print(*m);
delete m;
ProbabilisticSubstitutionMapping* probMapAna =
SubstitutionMappingTools::computeSubstitutionVectors(drhtl, ids, *sCountAna);
SubstitutionCount* sCountTot = new NaiveSubstitutionCount(model, totReg);
m = sCountTot->getAllNumbersOfSubstitutions(0.001,1);
cout << "Simple total count:" << endl;
MatrixTools::print(*m);
delete m;
ProbabilisticSubstitutionMapping* probMapTot =
SubstitutionMappingTools::computeSubstitutionVectors(drhtl, ids, *sCountTot);
SubstitutionCount* sCountDnDs = new NaiveSubstitutionCount(model, dndsReg);
m = sCountDnDs->getAllNumbersOfSubstitutions(0.001,1);
cout << "Detailed count, type 1:" << endl;
MatrixTools::print(*m);
delete m;
ProbabilisticSubstitutionMapping* probMapDnDs =
SubstitutionMappingTools::computeSubstitutionVectors(drhtl, ids, *sCountDnDs);
SubstitutionCount* sCountUniTot = new UniformizationSubstitutionCount(model, totReg);
m = sCountUniTot->getAllNumbersOfSubstitutions(0.001,1);
cout << "Total count, uniformization method:" << endl;
MatrixTools::print(*m);
delete m;
ProbabilisticSubstitutionMapping* probMapUniTot =
SubstitutionMappingTools::computeSubstitutionVectors(drhtl, ids, *sCountUniTot);
SubstitutionCount* sCountUniDnDs = new UniformizationSubstitutionCount(model, dndsReg);
m = sCountUniDnDs->getAllNumbersOfSubstitutions(0.001,2);
cout << "Detailed count, uniformization method, type 2:" << endl;
MatrixTools::print(*m);
delete m;
ProbabilisticSubstitutionMapping* probMapUniDnDs =
SubstitutionMappingTools::computeSubstitutionVectors(drhtl, ids, *sCountUniDnDs);
//.........这里部分代码省略.........
示例6: main
int main() {
TreeTemplate<Node>* tree = TreeTemplateTools::parenthesisToTree("(((A:0.1, B:0.2):0.3,C:0.1):0.2,(D:0.3,(E:0.2,F:0.05):0.1):0.1);");
vector<string> seqNames= tree->getLeavesNames();
vector<int> ids = tree->getNodesId();
//-------------
const NucleicAlphabet* alphabet = &AlphabetTools::DNA_ALPHABET;
FrequenciesSet* rootFreqs = new GCFrequenciesSet(alphabet);
SubstitutionModel* model = new T92(alphabet, 3.);
std::vector<std::string> globalParameterNames;
globalParameterNames.push_back("T92.kappa");
//Very difficult to optimize on small datasets:
DiscreteDistribution* rdist = new GammaDiscreteRateDistribution(4, 1.0);
ParametrizableTree* parTree = new ParametrizableTree(*tree);
FrequenciesSet* rootFreqs2 = rootFreqs->clone();
DiscreteDistribution* rdist2 = rdist->clone();
SubstitutionModel* model2=model->clone();
map<string, string> alias;
SubstitutionModelSet* modelSet = SubstitutionModelSetTools::createNonHomogeneousModelSet(model, rootFreqs, tree, alias, globalParameterNames);
unique_ptr<SubstitutionModelSet> modelSetSim(modelSet->clone());
NonHomogeneousSubstitutionProcess* subPro= NonHomogeneousSubstitutionProcess::createNonHomogeneousSubstitutionProcess(model2, rdist2, rootFreqs2, parTree, globalParameterNames);
// Simulation
size_t nsites = 1000;
unsigned int nrep = 20;
size_t nmodels = modelSet->getNumberOfModels();
vector<double> thetas(nmodels);
vector<double> thetasEst1(nmodels);
vector<double> thetasEst2(nmodels);
vector<double> thetasEst1n(nmodels);
vector<double> thetasEst2n(nmodels);
for (size_t i = 0; i < nmodels; ++i) {
double theta = RandomTools::giveRandomNumberBetweenZeroAndEntry(0.99) + 0.005;
cout << "Theta" << i << " set to " << theta << endl;
modelSetSim->setParameterValue("T92.theta_" + TextTools::toString(i + 1), theta);
//subPro->setParameterValue("T92.theta_" + TextTools::toString(i + 1), theta);
thetas[i] = theta;
}
NonHomogeneousSequenceSimulator simulator(modelSetSim.get(), rdist, tree);
NonHomogeneousSubstitutionProcess* subPro2 = subPro->clone();
for (unsigned int j = 0; j < nrep; j++) {
OutputStream* profiler = new StlOutputStream(new ofstream("profile.txt", ios::out));
OutputStream* messenger = new StlOutputStream(new ofstream("messages.txt", ios::out));
//Simulate data:
unique_ptr<SiteContainer> sites(simulator.simulate(nsites));
//Now fit model:
unique_ptr<SubstitutionModelSet> modelSet2(modelSet->clone());
RNonHomogeneousTreeLikelihood tl(*tree, *sites.get(), modelSet, rdist, true, true, false);
tl.initialize();
RNonHomogeneousTreeLikelihood tl2(*tree, *sites.get(), modelSet2.get(), rdist, true, true, true);
tl2.initialize();
SubstitutionProcess* nsubPro=subPro->clone();
SubstitutionProcess* nsubPro2=subPro2->clone();
RecursiveLikelihoodTreeCalculation* tlComp = new RecursiveLikelihoodTreeCalculation(*sites->clone(), nsubPro, true, false);
SingleProcessPhyloLikelihood ntl(nsubPro, tlComp, true);
RecursiveLikelihoodTreeCalculation* tlComp2 = new RecursiveLikelihoodTreeCalculation(*sites->clone(), nsubPro2, true);
SingleProcessPhyloLikelihood ntl2(nsubPro2, tlComp2, true);
for (size_t i = 0; i < nmodels; ++i) {
ntl.setParameterValue("T92.theta_" + TextTools::toString(i + 1), thetas[i]);
ntl2.setParameterValue("T92.theta_" + TextTools::toString(i + 1), thetas[i]);
}
cout << setprecision(10) << "OldTL init: " << tl.getValue() << "\t" << tl2.getValue() << endl;
cout << setprecision(10) << "NewTL init: " << ntl.getValue() << "\t" << ntl2.getValue() << endl;
unsigned int c1 = OptimizationTools::optimizeNumericalParameters2(
&tl, tl.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
unsigned int c2 = OptimizationTools::optimizeNumericalParameters2(
&tl2, tl2.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
unsigned int nc1 = OptimizationTools::optimizeNumericalParameters2(
&ntl, ntl.getParameters(), 0,
0.0001, 10000, messenger, profiler, false, false, 1, OptimizationTools::OPTIMIZATION_NEWTON);
//.........这里部分代码省略.........