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