#include "AnError.hh" #include "BirthDeathMCMC.hh" //#include "IntegralBirthDeathMCMC.hh" #include "DummyMCMC.hh" #include "Hacks.hh" #include "TopTimeMCMC.hh" #include "ReconciledTreeMCMC.hh" #include "TreeIO.hh" #include "SimpleML.hh" #include "SimpleMCMC.hh" #include "StrStrMap.hh" #include "Node.hh" //#include "MCMCOutputReader.hh" #include // Author: Bengt Sennblad, copyright: the MCMC group, SBC // copyright The MCMC club, SBC // Global options //------------------------------------------------------------- int nParams = 3; char* outfile=NULL; unsigned MaxIter = 10000; unsigned Thinning = 10; unsigned printFactor = 1; unsigned RSeed = 0; bool quiet = false; bool show_debug_info = false; bool do_likelihood = false; bool do_ML = false; // Birth-death process related bool fixed_bdrates = false; double birthRate = 1.0; double deathRate = 1.0; double topTime = -1.0; bool estimateTopTime = true; double Beta = -1.0; bool mustChooseRates = true; // reconciliation related bool estimate_orthology = false; bool specprob = false; bool fixRecon = true; // helper functions //------------------------------------------------------------- void usage(char *cmd); int readOptions(int argc, char **argv); int main(int argc, char** argv) { using namespace beep; using namespace std; if (argc < nParams) { usage(argv[0]); exit(1); } try { // tell the user we've started //--------------------------------------------------------- cerr << "Running: "; for(int i = 0; i < argc; i++) { cerr << argv[i] << " "; } cerr << " in directory "; cerr << getenv("PWD"); cerr << "\n\n"; //--------------------------------------------------------- // Read input and options //--------------------------------------------------------- // Check for options //----------------------------------- int opt = readOptions(argc, argv); if(opt + 2 > argc) { cerr << "Arguments missing\n"; exit(2); } //Get the trees //--------------------------------------------- string guest(argv[opt++]); TreeIO io = TreeIO::fromFile(guest); vector AC; StrStrMap gs; Tree G = io.readBeepTree(&AC, &gs); if(G.getName() == "Tree") { G.setName("G"); } if(AC.size() == 0) { throw AnError("No reconciliation given in gene tree", 1); } string host(argv[opt++]); TreeIO io2 = TreeIO::fromFile(host); Tree S = io2.readHostTree(); if(S.getName() == "Tree") { S.setName("S"); } // if(gs.size() == 0) // { // if(opt + 1 > argc) // { // cerr << "gs was not present in guest tree, " // << "therefore I expected a argument\n"; // usage(argv[0]); // } // gs = TreeIO::readGeneSpeciesInfo(argv[opt]); // } //-------------------------------------------------------- // Set up all classes //-------------------------------------------------------- //Set up random number generator and "end MCMCModel" //--------------------------------------------------------- PRNG rand; if (RSeed != 0) { rand.setSeed(RSeed); } DummyMCMC dm; // Set up priors //------------------------------------------------------- if(topTime <0) { topTime = S.getTopTime(); } else { S.setTopTime(topTime); } if(Beta < 0) { Beta = S.getRootNode()->getNodeTime(); } TopTimeMCMC stm = TopTimeMCMC(dm, S, Beta); BirthDeathMCMC bdm= BirthDeathMCMC(stm, S, birthRate, deathRate, &stm.getTopTime()); if(fixed_bdrates) { bdm.fixRates(); } ReconciledTreeMCMC gtm(bdm, G, gs, bdm); if (mustChooseRates) { gtm.chooseStartingRates(); } gtm.fixGamma(); // Set up MCMC handler //------------------------------------------------------- SimpleMCMC* iterator; if(do_ML) { iterator = new SimpleML(gtm, Thinning); } else { iterator = new SimpleMCMC(gtm, Thinning); } if (outfile != NULL) { try { iterator->setOutputFile(outfile); } catch(AnError e) { e.action(); } catch (int e) { cerr << "Problems opening output file! ('" << outfile << "') Using stdout instead.\n"; } } if (quiet) { iterator->setShowDiagnostics(false); } if (do_likelihood) { cout << "# L\tN\t" << gtm.strHeader() << endl << gtm.currentStateProb() << "\t0\t" << gtm.strRepresentation() << endl; exit(0); } if (!quiet) { cerr << "Start MCMC (Seed = " << rand.getSeed() << ")\n"; } // Copy startup info to outfile (can be used to restart analysis) cout << "# Running: "; for(int i = 0; i < argc; i++) { cout << argv[i] << " "; } cout << " in directory" << getenv("PWD") << "\n"; //-------------------------------------------------------- // Now we're set to do stuff //--------------------------------------------------------- time_t t0 = time(0); clock_t ct0 = clock(); iterator->iterate(MaxIter, printFactor); time_t t1 = time(0); clock_t ct1 = clock(); cerr << "Wall time: " << readableTime(t1 - t0) << endl << "CPU time: " << readableTime((ct1 - ct0)/CLOCKS_PER_SEC) << endl; if (!quiet) { cerr << gtm.getAcceptanceRatio() << " = acceptance ratio Wall time = " << readableTime(t1-t0) << "\n"; } if (gtm.getAcceptanceRatio() == 0) { cerr << "Warning! MCMC acceptance ratio was 0.\n"; } } catch(AnError& e) { cerr << "Error:\n"; e.action(); } catch(exception& e) { cerr << "Exception:\n" << e.what() << endl; } return(0); }; void usage(char* cmd) { using std::cerr; cerr << "Usage: " << cmd << " [] " << "\n" << "Parameters:\n" << " Name of file containing reconciled tree in\n" << " PRIME format, see documentation.\n" << " Species tree in Newick format. with divergence\n" << " times\n" << "Options:\n" << " -h This text.\n" << " -o Output file\n" << " -i number of iterations\n" << " -t thinning\n" << " -m Do maximum likelihood. No MCMC.\n" << " -w Write to cerr times less often than\n" << " to cout\n" << " -s Seed for pseudo-random number generator.\n" << " If set to 0 (default), the process id is\n" << " used as seed.\n" << " -q Do not output diagnostics to stderr.\n" << " -g Debug info.\n" << " -l Output likelihood. No MCMC.\n" << " -B