Debian Codegraph @

src/cxx/applications/primeGEM/troyo.cc

raw symbols SCIP
1 #include "AnError.hh"
2 #include "BirthDeathMCMC.hh"
3 //#include "IntegralBirthDeathMCMC.hh"
4 #include "DummyMCMC.hh"
5 #include "Hacks.hh"
6 #include "TopTimeMCMC.hh"
7 #include "TreeMCMC.hh"
8 #include "TreeIO.hh"
9 #include "SimpleML.hh"
10 #include "SimpleMCMC.hh"
11 #include "StrStrMap.hh"
12 #include "Node.hh"
13
14 #include "MCMCOutputReader.hh"
15 #include <stdlib.h>
16 //#include "conf_value_from_cmake_primeGEM.hh"
17
18 // Author: Bengt Sennblad, copyright: the MCMC group, SBC
19 // copyright The MCMC club, SBC
20
21 // Global options
22 //-------------------------------------------------------------
23 int nParams = 3;
24
25 char* outfile=NULL;
26 unsigned MaxIter = 10000;
27 unsigned Thinning = 10;
28 unsigned printFactor = 1;
29 unsigned RSeed = 0;
30 bool quiet = false;
31 bool show_debug_info = false;
32 bool do_likelihood = false;
33 bool do_ML = false;
34
35 // Birth-death process related
36 bool fixed_bdrates = false;
37 double birthRate = 1.0;
38 double deathRate = 1.0;
39 double topTime = -1.0;
40 bool estimateTopTime = true;
41 double Beta = -1.0;
42 bool mustChooseRates = true;
43
44 // reconciliation related
45 bool estimate_orthology = false;
46 bool specprob = false;
47
48 bool fixTree = true;
49 bool fixRoot = true;
50
51
52 // helper functions
53 //-------------------------------------------------------------
54 void usage(char *cmd);
55 int readOptions(int argc, char **argv);
56
57 int
58 main(int argc, char** argv)
59 {
60 using namespace beep;
61 using namespace std;
62
63 if (argc < nParams)
64 {
65 usage(argv[0]);
66 exit(1);
67 }
68 try
69 {
70 // tell the user we've started
71 //---------------------------------------------------------
72 cerr << "Running: ";
73 for(int i = 0; i < argc; i++)
74 {
75 cerr << argv[i] << " ";
76 }
77 cerr << " in directory ";
78 cerr << getenv("PWD");
79 cerr << "\n\n";
80
81 //---------------------------------------------------------
82 // Read input and options
83 //---------------------------------------------------------
84
85 // Check for options
86 //-----------------------------------
87 int opt = readOptions(argc, argv);
88
89 if(opt + 2 > argc)
90 {
91 cerr << "Arguments missing\n";
92 exit(2);
93 }
94
95 //Get the trees
96 //---------------------------------------------
97 string guest(argv[opt++]);
98 TreeIO io = TreeIO::fromFile(guest);
99 StrStrMap gs;
100 Tree G = io.readBeepTree(0, &gs);
101 if(G.getName() == "Tree")
102 {
103 G.setName("G");
104 }
105
106
107 string host(argv[opt++]);
108 TreeIO io2 = TreeIO::fromFile(host);
109 Tree S = io2.readHostTree();
110 if(S.getName() == "Tree")
111 {
112 S.setName("S");
113 }
114 S.rescale_specie_tree();
115 cout << S << endl;
116 if(gs.size() == 0)
117 {
118 if(opt + 1 > argc)
119 {
120 cerr << "gs was not present in guest tree, "
121 << "therefore I expected a <gs> argument\n";
122 usage(argv[0]);
123 }
124 gs = TreeIO::readGeneSpeciesInfo(argv[opt]);
125 }
126
127 //--------------------------------------------------------
128 // Set up all classes
129 //--------------------------------------------------------
130
131 //Set up random number generator and "end MCMCModel"
132 //---------------------------------------------------------
133 PRNG rand;
134 if (RSeed != 0)
135 {
136 rand.setSeed(RSeed);
137 }
138 DummyMCMC dm;
139
140 // Set up priors
141 //-------------------------------------------------------
142 if(Beta < 0)
143 {
144 Beta = S.getRootNode()->getNodeTime();
145 }
146 if(estimateTopTime)
147 {
148 topTime = S.getTopTime();
149 }
150 TopTimeMCMC stm = TopTimeMCMC(dm, S, Beta);
151 if(estimateTopTime == false)
152 {
153 stm.fixTopTime();
154 }
155 BirthDeathMCMC bdm= BirthDeathMCMC(stm, S, birthRate,
156 deathRate, &stm.getTopTime());
157 if(fixed_bdrates)
158 {
159 bdm.fixRates();
160 }
161
162 OrthologyMCMC gtm(bdm, G, gs, bdm);
163 if (mustChooseRates)
164 {
165 gtm.chooseStartingRates();
166 }
167
168 if(fixRoot)
169 {
170 gtm.fixRoot();
171 }
172 gtm.fixTree();
173
174 if(estimate_orthology)
175 {
176 gtm.estimateOrthology(specprob);
177 }
178
179 // Set up MCMC handler
180 //-------------------------------------------------------
181 SimpleMCMC* iterator;
182 if(do_ML)
183 {
184 iterator = new SimpleML(gtm, Thinning);
185 }
186 else
187 {
188 iterator = new SimpleMCMC(gtm, Thinning);
189 }
190
191 if (outfile != NULL)
192 {
193 try
194 {
195 iterator->setOutputFile(outfile);
196 }
197 catch(AnError e)
198 {
199 e.action();
200 }
201 catch (int e)
202 {
203 cerr << "Problems opening output file! ('"
204 << outfile
205 << "') Using stdout instead.\n";
206 }
207 }
208
209 if (quiet)
210 {
211 iterator->setShowDiagnostics(false);
212 }
213
214 if (do_likelihood)
215 {
216 cout << "# L\tN\t"
217 << gtm.strHeader()
218 << endl
219 << gtm.currentStateProb()
220 << "\t0\t"
221 << gtm.strRepresentation()
222 << endl;
223 exit(0);
224 }
225
226 if (!quiet)
227 {
228 cerr << "Start MCMC (Seed = " << rand.getSeed() << ")\n";
229 }
230
231
232 // Copy startup info to outfile (can be used to restart analysis)
233 cout << "# Running: ";
234 for(int i = 0; i < argc; i++)
235 {
236 cout << argv[i] << " ";
237 }
238 cout << " in directory"
239 << getenv("PWD")
240 << "\n";
241
242 //--------------------------------------------------------
243 // Now we're set to do stuff
244 //---------------------------------------------------------
245
246 time_t t0 = time(0);
247 clock_t ct0 = clock();
248
249
250 iterator->iterate(MaxIter, printFactor);
251
252 time_t t1 = time(0);
253
254 clock_t ct1 = clock();
255 cerr << "Wall time: " << readableTime(t1 - t0)
256 << endl
257 << "CPU time: " << readableTime((ct1 - ct0)/CLOCKS_PER_SEC)
258 << endl;
259
260 if (!quiet)
261 {
262 cerr << gtm.getAcceptanceRatio()
263 << " = acceptance ratio Wall time = "
264 << readableTime(t1-t0)
265 << "\n";
266 }
267
268 if (gtm.getAcceptanceRatio() == 0)
269 {
270 cerr << "Warning! MCMC acceptance ratio was 0.\n";
271 }
272 }
273 catch(AnError& e)
274 {
275 cerr << "Error:\n";
276 e.action();
277 }
278 catch(exception& e)
279 {
280 cerr << "Exception:\n"
281 << e.what()
282 << endl;
283 }
284
285 return(0);
286 };
287
288
289 void
290 usage(char* cmd)
291 {
292 using std::cerr;
293 cerr
294 << "Usage: "
295 << cmd
296 << " [<options>] <guest tree> <host tree> [<gene-species map>]\n"
297 << "\n"
298 << "Parameters:\n"
299 << " <guest tree> Name of file containing guest tree\n"
300 << " <species tree> Species tree in Newick format. with divergence\n"
301 << " times\n"
302 << " <gene-species map> Optional. This file contains lines with a\n"
303 << " gene name in the first column and species\n"
304 << " name as found in the species tree in the\n"
305 << " second. You can also choose to associate the \n"
306 << " genes with species in the gene tree. Please\n"
307 << " see documentation.\n"
308
309
310 << "Options:\n"
311 << " -u, -h This text.\n"
312 << " -o <filename> Output file\n"
313 << " -i <int> number of iterations\n"
314 << " -t <int> thinning\n"
315 << " -m Do maximum likelihood. No MCMC.\n"
316 << " -w <int> Write to cerr <int> times less often than\n"
317 << " t to cout\n"
318 << " -s <int> Seed for pseudo-random number generator.\n"
319 << " If set to 0 (default), the process id is\n"
320 << " used as seed.\n"
321 << " -q Do not output diagnostics to stderr.\n"
322 << " -g Debug info.\n"
323 << " -l Output likelihood. No MCMC.\n"
324 << " -G<option> Options related to the gene tree\n"
325 << " -Gr reroot gene tree. Default is a fixed root\n"
326 << " -Go Estimate orthology. Default is discard \n"
327 << " orthology data.\n"
328 << " -Gs record speciation probabilities (orthology\n"
329 << " probabilities will not be recorded)\n"
330 << " -B<option> Options related to the birth death process\n"
331 << " -Bf <float> <float> fix the birth/death rates to these values \n"
332 << " -Bp <float> <float> start values of birth/death rate parameters\n"
333 << " -Bt <float> fix 'top time', the time between the first\n"
334 << " duplication and root of S, to this value.\n"
335 << " -Bb <float> The beta parameter for a prior distribution\n"
336 << " on species root distance\n"
337
338 ;
339 exit(1);
340 }
341
342 int
343 readOptions(int argc, char **argv)
344 {
345 using namespace beep;
346 using namespace std;
347
348 int opt=1;
349 while (opt < argc && argv[opt][0] == '-')
350 {
351 switch (argv[opt][1])
352 {
353 case 'h':
354 case 'u':
355 {
356 usage(argv[0]);
357 break;
358 }
359 case 'o':
360 {
361 if (opt + 1 < argc)
362 {
363 outfile = argv[++opt];
364 }
365 else
366 {
367 cerr << "Expected filename after option '-o'\n";
368 usage(argv[0]);
369 }
370 break;
371 }
372 case 'i':
373 {
374 if (sscanf(argv[++opt], "%d", &MaxIter) == 0)
375 {
376 cerr << "Expected integer after option '-i'\n";
377 usage(argv[0]);
378 }
379 break;
380 }
381 case 't':
382 {
383 if (sscanf(argv[++opt], "%d", &Thinning) == 0)
384 {
385 cerr << "Expected integer after option '-t'\n";
386 usage(argv[0]);
387 }
388 break;
389 }
390 case 'w':
391 {
392 if (sscanf(argv[++opt], "%d", &printFactor) == 0)
393 {
394 cerr << "Expected integer after option '-w'\n";
395 usage(argv[0]);
396 }
397 break;
398 }
399
400 case 's':
401 {
402 if (opt + 1 < argc && sscanf(argv[++opt], "%d", &RSeed) == 0)
403 {
404 cerr << "Expected integer after option '-s'\n";
405 usage(argv[0]);
406 }
407 break;
408 }
409 case 'q':
410 {
411 quiet = true;
412 break;
413 }
414 case 'd':
415 {
416 show_debug_info = true;
417 break;
418 }
419 case 'l':
420 {
421 do_likelihood = true;
422 break;
423 }
424 case 'm':
425 {
426 do_ML = true;
427 break;
428 }
429 case 'G':
430 {
431 switch(argv[opt][2])
432 {
433 case 'r':
434 {
435 fixRoot = false;
436 break;
437 }
438 case 's':
439 {
440 specprob = true;
441 // no break here
442 }
443 case 'o':
444 {
445 estimate_orthology = true;
446 break;
447 }
448 default:
449 {
450 cerr << "Warning: Unknown option '" << argv[opt]
451 << "'!\n\n";
452 usage(argv[0]);
453 exit(1); // Check for correct error code
454 }
455 }
456 break;
457 }
458 case 'B':
459 {
460 switch(argv[opt][2])
461 {
462 case 'f':
463 {
464 fixed_bdrates = true;
465 // Don't break here, because we want to fall through to 'r'
466 // set the rates that are arguments both to '-r' and '-f'!
467 }
468 case 'p':
469 {
470 mustChooseRates = false;
471 if (++opt < argc)
472 {
473 birthRate = atof(argv[opt]);
474 if (++opt < argc)
475 {
476 deathRate = atof(argv[opt]);
477 }
478 else
479 {
480 cerr << "Error: Expected a gene loss (death) rate\n";
481 usage(argv[0]);
482 }
483 }
484 else
485 {
486 cerr << "Expected birth rate for option '-p' or '-f'!\n";
487 usage(argv[0]);
488 }
489 break;
490 }
491 case 't':
492 {
493 if (++opt < argc)
494 {
495 topTime = atof(argv[opt]);
496 estimateTopTime = false;
497 }
498 else
499 {
500 cerr << "Error: Expected a 'top time'!\n";
501 usage(argv[0]);
502 }
503 break;
504 }
505 case 'b':
506 {
507 if (++opt < argc)
508 {
509 if (sscanf(argv[opt], "%lf", &Beta) == 0)
510 {
511 cerr << "Expected number after option '-b'\n";
512 usage(argv[0]);
513 }
514 }
515 break;
516 }
517 default:
518 {
519 cerr << "Warning: Unknown option '" << argv[opt] << "'!\n\n";
520 usage(argv[0]);
521 exit(1); // Check for correct error code
522 }
523 }
524 break;
525 }
526 default:
527 {
528 cerr << "Warning: Unknown option '" << argv[opt] << "'!\n\n";
529 usage(argv[0]);
530 exit(1); // Check for correct error code
531 }
532 break;
533 }
534 opt++;
535 }
536 return opt;
537 };
538
539
540
541
542
543