#include #include #include #include #include "AnError.hh" #include "Beep.hh" #include "DummyMCMC.hh" #include "EdgeRateMCMC_common.hh" #include "EdgeWeightHandler.hh" #include "EdgeWeightMCMC.hh" #include "LengthRateMCMC.hh" #include "GammaDensity.hh" #include "InvGaussDensity.hh" #include "LengthRateModel.hh" #include "LogNormDensity.hh" #include "MatrixTransitionHandler.hh" #include "PRNG.hh" #include "SequenceData.hh" #include "SequenceGenerator.hh" #include "SequenceType.hh" #include "SiteRateHandler.hh" #include "Tree.hh" #include "TreeIO.hh" #include "SeqIO.hh" #include "UniformDensity.hh" using std::vector; // Global options with default settings //------------------------------------ int nParams = 2; double alpha = 1.0; unsigned n_cats = 1; // double pinvar = 0.0; bool useWeights = false; bool onlyWeights = false; double mean = 0.5; double variance = 0.1; std::string density = "UNIFORM"; std::string rateModel = "CONST"; std::string seqModel = "JC69"; bool generateRates = true; //bool generateRates = false; char* outfile = 0; //NULL; char* t90file = 0; //NULL // allowed models std::vector subst_models; std::vector rate_models; std::vector rate_densities; std::string seq_file = ""; // output to true file bool outputTree = false; bool outputSubst = true; bool outputRates = true; bool outputNodeTimes = true; bool outputBD = true; // helper functions //------------------------------------------------------------- void usage(char *cmd); int readOptions(int argc, char **argv); // Main program //------------------------------------------------------------- int main(int argc, char **argv) { using namespace beep; using namespace std; // Set up allowed models //------------------------------------------------------------- //rate model subst_models.push_back("UNIFORMAA"); subst_models.push_back("JTT"); subst_models.push_back("UNIFORMCODON"); subst_models.push_back("ARVECODON"); subst_models.push_back("JC69"); subst_models.push_back("F81"); //rate model rate_models.push_back("IID"); rate_models.push_back("GBM"); rate_models.push_back("CONST"); //rate model rate_densities.push_back("UNIFORM"); rate_densities.push_back("GAMMA"); rate_densities.push_back("LOGN"); rate_densities.push_back("INVG"); if (argc < nParams + 1) { usage(argv[0]); } try { // Tell user we started cerr << "Running: "; for (int i = 0; i < argc; i++) { cerr << argv[i] << " "; } cerr << " in directory: "; // system("pwd"); cerr << "\n\n"; // Read options //--------------------------------------------- int opt = readOptions(argc, argv); // Read arguments //--------------------------------------------- if (opt >= argc) { cerr << "Expected treefile argument\n"; usage(argv[0]); } string treefile(argv[opt++]); if (opt >= argc) { cerr << "Expected number of character argument\n"; usage(argv[0]); } unsigned nchar = atoi(argv[opt]); // Get the model tree //--------------------------------------------- TreeIO io = TreeIO::fromFile(treefile); TreeIOTraits traits; io.checkTagsForTree(traits); if (traits.hasNT()) { traits.setNT(generateRates); } else if (traits.hasET() || traits.hasNW()) { traits.setET(generateRates); traits.setNWisET(generateRates); } else { assert(generateRates == false); } traits.setBL(false); // traits.setBL(!generateRates); Tree G = io.readBeepTree(traits, 0, 0); //cout << G << endl; // Tree G = io.readBeepTree(0,0); // generateRates, false, !generateRates, // false, 0, 0); // Tree G = io.readGuestTree(); G.setName("G"); Tree oldG = G; // create the random number generator to use PRNG rand; // Set up the SubstitutionMatrix //--------------------------------------------------------- cout << "Sequence Model used is " << seqModel << endl; MatrixTransitionHandler Q = MatrixTransitionHandler::create(seqModel); if (seqModel == "F81") { if(seq_file == ""){ cerr << "For F81 model to work, we need to compute base frequencies" << endl; cerr << "Please specify sequence file (with -Ss option) for computing base frequencies for F81 model" << endl; cerr << "Exiting now" << endl; exit(EXIT_FAILURE); } // Get sequence data. This is why Q must already exist.* SequenceData *Df = new SequenceData(SeqIO::readSequences(seq_file, Q.getType())); // since Q is needed for D, and for counting base frequencies in Q, we need // D (chicken and egg problem) so, as a temporary hack, I am redefining Q // for F81 model std::map bf = Df->getBaseFrequencies(); Q = MatrixTransitionHandler::create("F81", &bf); } // Set up Density function for rates //--------------------------------------------------------- Density2P* df; capitalize(density); if (density == "INVG") { df = new InvGaussDensity(mean, variance); } else if (density == "LOGN") { df = new LogNormDensity(mean, variance); } else if (density == "GAMMA") { df = new GammaDensity(mean, variance); } else// (density == "UNIFORM") { df = new UniformDensity(mean, variance, true); } // Test for sanenss of mean and variance! Real min, max; df->getRange(min, max); if (min < 0) { cerr << "The mean and variance given for density yields\n" << "yields a partially negative range. Please correct\n"; exit(1); } // Set up rates and EdgeWeightHandler //--------------------------------------------------------- DummyMCMC dm; EdgeRateMCMC* erm; EdgeWeightModel* ewm; if (generateRates == false) { ewm = new LengthRateModel(*df, G); // erm = new EdgeWeightMCMC(dm, *lrm); // throw AnError("Currently not possible to generate lengths directly",1); erm = new iidRateMCMC(dm, *df, G, "EdgeRates"); } else if (rateModel == "CONST") { ewm = erm = new ConstRateMCMC(dm, *df, G, "EdgeRates"); } else if (rateModel == "IID") { ewm = erm = new iidRateMCMC(dm, *df, G, "EdgeRates"); } else //(rateModel == "GBM") { ewm = erm = new gbmRateMCMC(dm, *df, G, "EdgeRates"); } if (generateRates) { G.setRates(erm->getWeightVector()); } //generate Rates if (generateRates || onlyWeights) { erm->generateRates(); } EdgeWeightHandler* ewh; if (useWeights || onlyWeights) { //ewh = new EdgeWeightHandler(*ewm); ewh = new EdgeTimeRateHandler(*erm); } else if (!generateRates) { ewh = new EdgeWeightHandler(*ewm); } else { ewh = new EdgeTimeRateHandler(*erm); } if (onlyWeights) { //cerr << G.print(true, true, true,true); TreeIOTraits traits; traits.setID(true); traits.setBL(true); traits.setNW(true); //cout << TreeIO::writeBeepTree(G, traits, 0) << endl;//true, false, false, true, true, 0) << endl; ofstream treeWithLengths(outfile); string gtree = TreeIO::writeBeepTree(G, traits, 0); //cout << gtree << endl; treeWithLengths << gtree << "\n"; treeWithLengths.close(); for (unsigned i = 0; i < G.getNumberOfNodes(); i++) { if (G.getNode(i)->getName() != oldG.getNode(i)->getName()) { cerr << "ERROR: There is some mismatch between input tree "; cerr << " and the gene tree with lengths... \nExiting now" << endl; exit(EXIT_FAILURE); } } cout << "Gene tree with lengths named '" << outfile << "' has been generated." << endl; exit(0); } // Set up SiteRates //--------------------------------------------------------- UniformDensity uf(0, 100, true); ConstRateMCMC alphaC(dm, uf, G, "Alpha"); SiteRateHandler srh(n_cats, alphaC); srh.setAlpha(alpha); ewh->update(); // Set up SequenceGenerator //--------------------------------------------------------- //cerr << "here A\n"; SequenceGenerator SG(G, Q, srh, *ewh, rand); //cerr << "here B\n"; //cout << SG; //cerr << "here C\n"; // Generate Sequence Data //--------------------------------------------------------- SequenceData D = SG.generateData(nchar); // Output Data //--------------------------------------------------------- cout << endl << nchar << " characters were generated"; if (outfile != NULL) { ofstream data(outfile); data << D.data4fasta() << "\n"; cout << " and written to file " << outfile << endl; } else { cout << ":\n" << D.data4fasta(); } if (t90file != 0) { cout << "'True' parameters for 90% test is written to file " << t90file << "\n" ; ofstream t90((string(t90file)).data()); ostringstream t90header; ostringstream t90data; TreeIO io_out; t90header << "# True parameters input for 90% test\n" << "# T N\t"; t90data << "0 0\t"; if (outputSubst && n_cats != 1) { t90header << alphaC.strHeader(); t90data << alphaC.strRepresentation(); } if (outputRates) { t90header << erm->strHeader(); t90data << erm->strRepresentation(); } if (outputTree) { t90header << G.getName() << "(tree);\t"; t90data << io_out.writeGuestTree(G) + ";\t"; } if (outputNodeTimes) { for (unsigned i = 0; i < G.getNumberOfNodes(); i++) { Node& n = *G.getNode(i); if (!(n.isLeaf() || n.isRoot())) { t90header << "EdgeTimes.nodeTime[" << i << "](float); "; t90data << n.getNodeTime() << "; "; } } } t90header << "\n"; t90data << "\n"; t90 << t90header.str() << t90data.str(); delete erm; delete df; delete ewh; } } catch (AnError e) { cerr << "error found\n"; e.action(); } } void usage(char *cmd) { using std::cerr; cerr << "Usage: " << cmd << " [] \n" << "\n" << "Generates either:\n" << "1) edge lengths and sequence data on the input tree, \n" << "2) sequence data from a tree with given edge lengths, or\n" << "2) only edge lengths.\n" << "Edge lengths can be achieved either by a) by generating edge rates \n" << "from a model of rate variation over the tree and using the divergence\n" << "times to achieve the edge lengths, or b) directly generating\n" << "the edge lengths from the 'rate model'.\n" << "In 1) and 2) a user-specified substitution model is used to generate\n" << "sequences.\n" << "The '-T' options allows saving generation parameters (recommended)\n" << "\n" << "Parameters:\n" << " name of file with tree to generated data on.\n" << " an int, the number of characters to generate\n" << "Options:\n" << " -u, -h This text.\n" << " -o output file\n" << " -T