├── .gitignore ├── AUTHORS ├── CMakeLists.txt ├── LICENSE ├── README.md ├── doc ├── Makefile ├── source │ ├── _themes │ │ └── bamm │ │ │ ├── layout.html │ │ │ ├── static │ │ │ └── bamm.css │ │ │ └── theme.conf │ ├── advanced.rst │ ├── announcements.rst │ ├── bamm-header.png │ ├── bamm-logo.png │ ├── bammgraph.rst │ ├── bgg-f1.R │ ├── changes.rst │ ├── colorbreaks.rst │ ├── conf.py │ ├── configuration.rst │ ├── contact_us.rst │ ├── documentation.rst │ ├── download.rst │ ├── faq.rst │ ├── figs │ │ ├── .RData │ │ ├── .Rapp.history │ │ ├── .Rhistory │ │ ├── BAMM_E_t.png │ │ ├── BAMM_E_t_3event.png │ │ ├── BF_pairwise.R │ │ ├── BF_pairwise.pdf │ │ ├── IncompSampling.pdf │ │ ├── breaksForGallery.png │ │ ├── breaksTest.png │ │ ├── breaksTestTrees.png │ │ ├── cohortExample.png │ │ ├── cohort_whales.png │ │ ├── cumulativeST.pdf │ │ ├── dtrates.pdf │ │ ├── dtrates1.pdf │ │ ├── dtrates2.pdf │ │ ├── dtrates3.pdf │ │ ├── dtrates_polar.pdf │ │ ├── jetz_time_rates.R │ │ ├── jetztree_shiftnodes.R │ │ ├── lhmodel │ │ │ ├── branch_history.png │ │ │ ├── likelihood_nodecombine.png │ │ │ ├── shifttype_fig.001.png │ │ │ ├── shifttype_fig_true.png │ │ │ ├── shifttype_fig_true2.png │ │ │ └── x_tree_extinction.png │ │ ├── maxshiftcredibility.pdf │ │ ├── multi_shift.pdf │ │ ├── pairwiseShiftNodes.R │ │ ├── plot_multishift.R │ │ ├── primates_bodysize.pdf │ │ ├── rate_through_time_whales.R │ │ ├── rate_through_time_whales_color.R │ │ ├── shifts │ │ │ ├── Fig2.key │ │ │ ├── Fig2a.pdf │ │ │ ├── Fig2a.png │ │ │ ├── Fig2b.key │ │ │ ├── Fig2b.pdf │ │ │ ├── Fig2b.png │ │ │ ├── Fig3a.key │ │ │ ├── Fig3a.pdf │ │ │ ├── fig1.key │ │ │ ├── fig1.pdf │ │ │ ├── fig3a.png │ │ │ ├── figu4.key │ │ │ ├── figu4.pdf │ │ │ ├── primatesX.pdf │ │ │ ├── primatesX2.pdf │ │ │ ├── primates_msc.pdf │ │ │ ├── primates_msc.png │ │ │ ├── shiftProbabilities.R │ │ │ ├── shifts1.pdf │ │ │ ├── test.R │ │ │ ├── whales.pdf │ │ │ ├── whales_cst.png │ │ │ ├── whales_cst1.pdf │ │ │ └── whales_pairwiseProbs.pdf │ │ ├── tmptree.pdf │ │ ├── whale_color_rtt.pdf │ │ ├── whalerates1.pdf │ │ ├── whalerates2.pdf │ │ ├── whales_through_time.pdf │ │ ├── xBayesFactorsJetzPW.png │ │ ├── xCumShiftTree.png │ │ ├── xFig2a.png │ │ ├── xFig2b.png │ │ ├── xGrayscaleWhalerates.png │ │ ├── xIncompSampling.png │ │ ├── xIntroFig_whalerates.png │ │ ├── xMaxCredShiftTree.png │ │ ├── xMultipleShifts.png │ │ ├── xPolarRates_whales.png │ │ ├── xPrimatesBodySize.png │ │ ├── xWhales_color_rtt.png │ │ ├── x_distinctShiftConfigs.png │ │ ├── x_interpret1.png │ │ ├── x_whales_cst.png │ │ ├── xdistinct_illustrate_A.png │ │ ├── xdistinct_illustrate_B.png │ │ ├── xdistinct_illustrate_C.png │ │ ├── xdistinct_illustrate_D.png │ │ ├── xfig3a.png │ │ ├── xprimates_msc.png │ │ └── xprimates_shiftconfigs.png │ ├── figures │ │ ├── breaksForGallery.png │ │ ├── breaksmethodPhylorates.png │ │ ├── cohort_whales_illustrated.png │ │ ├── primates_credShiftSet.png │ │ ├── rateHistograms.png │ │ ├── scripts │ │ │ ├── colorbreaks_page.R │ │ │ ├── graph_gallery.R │ │ │ ├── intro_page.R │ │ │ └── rateshifts_page.R │ │ ├── whales_RatesThroughTime.png │ │ ├── whales_RatesThroughTimeBW.png │ │ ├── whales_cohort.png │ │ ├── whales_distinctShiftConfigs.png │ │ ├── whales_polar.png │ │ ├── whales_sepRateShiftConfigs.png │ │ ├── xIntroFig_whalerates.png │ │ ├── x_whale_credibleshiftset.png │ │ ├── x_whale_marginalodds.png │ │ ├── x_whale_marginals.png │ │ ├── x_whale_priors.png │ │ ├── xprimates_shiftconfigs.png │ │ └── xx_whale_postshiftsamples.png │ ├── filesForDownload │ │ ├── Rabosky_etal_SystematicBiology_2017.pdf │ │ ├── SupplementaryMaterial_BAMM_Text&Figures.pdf │ │ ├── primates_logmass.txt │ │ ├── primatetree.txt │ │ ├── template_diversification.txt │ │ ├── template_trait.txt │ │ └── whaletree.txt │ ├── fossilbamm.rst │ ├── glossary.rst │ ├── index.rst │ ├── introduction.rst │ ├── likelihoodmodel.rst │ ├── mc3.rst │ ├── normal.png │ ├── nsf-logo.gif │ ├── postprocess.rst │ ├── quickstart.rst │ ├── rateshifts.rst │ ├── rcode │ │ ├── .Rapp.history │ │ ├── 3.rateshifts.R │ │ ├── BAMM_likelihood_analysis.R │ │ ├── BF_pairwise.R │ │ ├── cetaceans_polarRateTree.R │ │ ├── combine_extinction_nodes.R │ │ ├── distinctShifts.R │ │ ├── incompleteSampling.R │ │ ├── interpretShifts.R │ │ ├── jetztree_shifts.R │ │ ├── plot_multishift.R │ │ ├── postprocess_script.R │ │ ├── primate_bodysize.R │ │ ├── rate_through_time_whales.R │ │ ├── rate_through_time_whales_color.R │ │ ├── x_extinctionprobs_bamm.png │ │ ├── x_extinctionprobs_recomputed.png │ │ ├── x_whale_credibleshiftset.png │ │ ├── x_whale_marginalodds.png │ │ ├── x_whale_marginals.png │ │ ├── x_whale_priors.png │ │ └── xx_whale_postshiftsamples.png │ ├── settingup.rst │ ├── strapp.rst │ ├── template.rst │ ├── testingbamm.rst │ ├── testingbamm │ │ ├── R_BatchSimulate_MEA_trees.R │ │ ├── R_analyze_BAMM_results.R │ │ ├── R_simulate_tree_from_seed.R │ │ ├── R_test_BAMM_functions.R │ │ ├── phylorate15.png │ │ ├── phylorate8.png │ │ ├── phylorate8b.png │ │ ├── phylorate9.png │ │ ├── s15_points.png │ │ ├── s8_points.png │ │ ├── s9_points.png │ │ └── s9_points2.png │ ├── time-flip.rst │ ├── troubleshooting.rst │ └── v2rcode │ │ ├── bammgraphgallery.R │ │ ├── bammgraphgallery1.png │ │ ├── bammgraphgallery2.png │ │ ├── bammgraphgallery3.png │ │ ├── bammgraphgallery4.png │ │ ├── bammgraphgallery5.png │ │ ├── bammgraphgallery6.png │ │ ├── bammgraphgallery7.png │ │ ├── bayesfactorbranches.R │ │ ├── bayesfactorbranches1.png │ │ ├── bayesfactorbranches2.png │ │ ├── bayesfactorbranches3.png │ │ ├── bayesfactorbranches4.png │ │ ├── distinctshiftconfigs.R │ │ ├── distinctshiftconfigs1.png │ │ ├── distinctshiftconfigs2.png │ │ ├── distinctshiftconfigs3.png │ │ ├── distinctshiftconfigs4.png │ │ ├── overallbestshiftconfig.R │ │ ├── overallbestshiftconfig1.png │ │ ├── prior_probs_0.1.txt │ │ ├── rateshifts_prior.R │ │ ├── rateshifts_prior.png │ │ └── rateshifts_prior_0.1.png └── sweave │ ├── bammdoc1.Rnw │ ├── bammdoc_makefigs.R │ ├── data │ ├── event_data.txt │ ├── mcmc_out.txt │ ├── mcmc_prior_only.txt │ ├── run_info_post.txt │ └── whaletree.tre │ └── makesweave.sh ├── examples ├── diversification │ ├── anoles │ │ ├── GA_Anolis_MCC.tre │ │ └── divcontrol.txt │ └── whales │ │ ├── divcontrol.txt │ │ └── whaletree.tre └── traits │ ├── fishsize │ ├── fishmorph.txt │ ├── fishtreeFinalPL_Feb8.tre │ └── traitcontrol.txt │ ├── primatemass │ ├── primates.tre │ ├── primates_logmass.txt │ └── traitcontrol.txt │ └── whalesize │ ├── traitcontrol.txt │ ├── whale_size.txt │ └── whaletree.tre ├── problems ├── acanthurid.tre ├── balistoidae.tre └── tetraodontidae.tre ├── src ├── AcceptanceDataWriter.cpp ├── AcceptanceDataWriter.h ├── BetaInitProposal.cpp ├── BetaInitProposal.h ├── BetaShiftProposal.cpp ├── BetaShiftProposal.h ├── BetaTimeModeProposal.cpp ├── BetaTimeModeProposal.h ├── BranchEvent.cpp ├── BranchEvent.h ├── BranchHistory.cpp ├── BranchHistory.h ├── ChainSwapDataWriter.cpp ├── ChainSwapDataWriter.h ├── CommandLineProcessor.cpp ├── CommandLineProcessor.h ├── EventCountLog.cpp ├── EventCountLog.h ├── EventDataWriter.cpp ├── EventDataWriter.h ├── EventNumberForBranchProposal.cpp ├── EventNumberForBranchProposal.h ├── EventNumberProposal.cpp ├── EventNumberProposal.h ├── EventParameterProposal.cpp ├── EventParameterProposal.h ├── EventRateProposal.cpp ├── EventRateProposal.h ├── FastSimulatePrior.cpp ├── FastSimulatePrior.h ├── LambdaInitProposal.cpp ├── LambdaInitProposal.h ├── LambdaShiftProposal.cpp ├── LambdaShiftProposal.h ├── LambdaTimeModeProposal.cpp ├── LambdaTimeModeProposal.h ├── Log.cpp ├── Log.h ├── MCMC.cpp ├── MCMC.h ├── MCMCDataWriter.cpp ├── MCMCDataWriter.h ├── MatchPathSeparator.h ├── MbRandom.cpp ├── MbRandom.h ├── MetropolisCoupledMCMC.cpp ├── MetropolisCoupledMCMC.h ├── Model.cpp ├── Model.h ├── ModelDataWriter.cpp ├── ModelDataWriter.h ├── ModelFactory.h ├── MoveEventProposal.cpp ├── MoveEventProposal.h ├── MuInitProposal.cpp ├── MuInitProposal.h ├── MuShiftProposal.cpp ├── MuShiftProposal.h ├── NewickTreeReader.cpp ├── NewickTreeReader.h ├── Node.cpp ├── Node.h ├── NodeStateDataWriter.cpp ├── NodeStateDataWriter.h ├── NodeStateProposal.cpp ├── NodeStateProposal.h ├── PreservationRateProposal.cpp ├── PreservationRateProposal.h ├── Prior.cpp ├── Prior.h ├── Proposal.cpp ├── Proposal.h ├── Random.cpp ├── Random.h ├── Settings.cpp ├── Settings.h ├── SettingsParameter.cpp ├── SettingsParameter.h ├── SpExBranchEvent.cpp ├── SpExBranchEvent.h ├── SpExDataWriter.cpp ├── SpExDataWriter.h ├── SpExEventDataWriter.cpp ├── SpExEventDataWriter.h ├── SpExModel.cpp ├── SpExModel.h ├── SpExModelFactory.h ├── Stat.cpp ├── Stat.h ├── StdOutDataWriter.cpp ├── StdOutDataWriter.h ├── TimeModeProposal.cpp ├── TimeModeProposal.h ├── Tools.cpp ├── Tools.h ├── TraitBranchEvent.cpp ├── TraitBranchEvent.h ├── TraitDataWriter.cpp ├── TraitDataWriter.h ├── TraitEventDataWriter.cpp ├── TraitEventDataWriter.h ├── TraitModel.cpp ├── TraitModel.h ├── TraitModelFactory.h ├── Tree.cpp ├── Tree.h ├── TreeReader.h └── main.cpp ├── tests ├── CommandLineProcessorTest.cpp ├── Makefile └── NodeTest.cpp └── tools ├── chain-swap-percent.sh ├── chainSwapPercent.R └── generate-authors.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | 15 | # Build directories 16 | build 17 | 18 | # Default output files 19 | mcmc_out* 20 | event_data* 21 | run_info* 22 | shiftPrior* 23 | chain_swap* 24 | prior_probs* 25 | 26 | # Other junk 27 | .DS_Store 28 | ._* 29 | *~ 30 | *.swp 31 | .Rapp.history 32 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | This is a list of people who have contributed to the development of BAMM 2 | and/or part of BAMMtools. It is sorted by the number of commits made to 3 | to the master branch of BAMM's Git repository. This file was generated 4 | using the script tools/generate-authors.sh. 5 | 6 | Carlos Anderson 7 | Daniel Rabosky 8 | Mike Grundler 9 | Pascal Title 10 | Jeff J. Shi 11 | Joseph W. Brown 12 | Simon Greenhill 13 | Jonathan Chang 14 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.8) 2 | PROJECT(BAMM C CXX) 3 | 4 | SET(BAMM_VERSION 2.5.0) 5 | SET(BAMM_VERSION_DATE 2015-11-01) 6 | 7 | # Specify executable and source files 8 | AUX_SOURCE_DIRECTORY(src BAMM_SRC) 9 | ADD_EXECUTABLE(bamm ${BAMM_SRC}) 10 | 11 | # Specify flags according to compiler 12 | IF(${CMAKE_CXX_COMPILER_ID} MATCHES Clang) 13 | SET(CMAKE_CXX_FLAGS "-g -Wall -Wextra -O3 -std=c++11 -stdlib=libc++") 14 | SET(CMAKE_EXE_LINKER_FLAGS "-stdlib=libc++") 15 | ELSEIF(${CMAKE_CXX_COMPILER_ID} MATCHES GNU) 16 | IF(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 4.6.3) 17 | MESSAGE(FATAL_ERROR "GCC version must be >= 4.6.3") 18 | ELSEIF(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 4.7) 19 | SET(CMAKE_CXX_FLAGS "-g -Wall -Wextra -O3 -std=c++0x") 20 | ELSE() 21 | SET(CMAKE_CXX_FLAGS "-g -Wall -Wextra -O3 -std=c++11") 22 | ENDIF() 23 | FIND_PACKAGE(Threads REQUIRED) 24 | IF(Threads_FOUND) 25 | TARGET_LINK_LIBRARIES (bamm ${CMAKE_THREAD_LIBS_INIT}) 26 | ENDIF() 27 | ELSEIF(${CMAKE_CXX_COMPILER_ID} MATCHES MSVC) 28 | SET(CMAKE_CXX_FLAGS "/W4") 29 | ENDIF() 30 | 31 | # Provide BAMM version to the compiler 32 | ADD_DEFINITIONS(-DBAMM_VERSION=\"${BAMM_VERSION}\") 33 | ADD_DEFINITIONS(-DBAMM_VERSION_DATE=\"${BAMM_VERSION_DATE}\") 34 | 35 | # Set Git commit id 36 | EXECUTE_PROCESS(COMMAND "git" "rev-parse" "HEAD" 37 | OUTPUT_VARIABLE GIT_COMMIT_ID 38 | OUTPUT_STRIP_TRAILING_WHITESPACE) 39 | ADD_DEFINITIONS(-DGIT_COMMIT_ID=\"${GIT_COMMIT_ID}\") 40 | 41 | INSTALL(TARGETS bamm RUNTIME DESTINATION bin) 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BAMM 2 | ==== 3 | 4 | A program for multimodel inference on speciation and trait evolution. 5 | Please see the project's website 6 | ([http://bamm-project.org](http://bamm-project.org)) 7 | for the full documentation. 8 | 9 | Requirements 10 | ------------ 11 | 12 | In order to compile BAMM, 13 | you need [CMake](http://www.cmake.org) and a C++11 compiler. 14 | You also need a Unix shell (e.g., `bash`) to run the following commands. 15 | 16 | Installation 17 | ------------ 18 | 19 | In the project's root directory, 20 | create a new directory called `build` and go into it: 21 | 22 | mkdir build 23 | cd build 24 | 25 | To compile BAMM, run the following commands: 26 | 27 | cmake .. 28 | make -j 29 | 30 | The final executable will be named `bamm`. You may run `bamm` from this 31 | directory, or you may install it in a more permanent location. 32 | To do this, run the following command within the `build` directory: 33 | 34 | sudo make install 35 | 36 | You may now run `bamm` from any directory in your system. 37 | -------------------------------------------------------------------------------- /doc/source/_themes/bamm/layout.html: -------------------------------------------------------------------------------- 1 | {% extends "sphinxdoc/layout.html" %} 2 | 3 | {% block rootrellink %} 4 |
  • Home |
  • 5 |
  • Download |
  • 6 |
  • Documentation |
  • 7 |
  • Contact Us
  • 8 | {% endblock %} 9 | 10 | {% block header %} 11 | 14 | {% endblock %} 15 | 16 | {% block footer %} 17 | {{ super() }} 18 | 26 | {% endblock %} 27 | -------------------------------------------------------------------------------- /doc/source/_themes/bamm/static/bamm.css: -------------------------------------------------------------------------------- 1 | @import url("sphinxdoc.css"); 2 | 3 | .pageheader { 4 | text-align: left; 5 | padding: 20px 20px; 6 | background-color: #4d4d4d; 7 | } 8 | 9 | .pageheader ul { 10 | float: right; 11 | list-style-type: none; 12 | padding-left: 0; 13 | margin-top: 30px; 14 | margin-right: 10px; 15 | } 16 | 17 | .pageheader li { 18 | float: left; 19 | margin: 0 0 0 10px; 20 | } 21 | 22 | .pageheader li a { 23 | padding: 8px 12px; 24 | } 25 | 26 | .pageheader li a:hover { 27 | color: white; 28 | } 29 | -------------------------------------------------------------------------------- /doc/source/_themes/bamm/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = sphinxdoc 3 | stylesheet = bamm.css 4 | -------------------------------------------------------------------------------- /doc/source/announcements.rst: -------------------------------------------------------------------------------- 1 | 2 | Announcements 3 | ========================== 4 | 5 | June 22-23: Macroevolution / diversification short course 6 | .......................................................... 7 | 8 | **Short course on the analysis of diversification rates from phylogenies: June 22-23** on the campus of Oregon State University, to coincide with the North American Evolution meetings (SSE/ASN/SSB) in Portland (June 23 - June 27). The workshop is funded in part by the National Science Foundation with additional support from Oregon State University and is co-organized by Dan Rabosky (University of Michigan) and Brian Sidlauskas (Oregon State University). Travel awards of up to $500 per person are available to cover participation costs. 9 | 10 | **Overview**: Rates of speciation, extinction, and phenotypic evolution vary widely across the Tree of Life and through time. This workshop will provide theoretical background and a hands-on practicum in the analysis of lineage diversification rates using time-calibrated phylogenetic trees. Topics will include: 11 | 12 | * Developing your intuition for diversification models 13 | * Using BAMM to study complex patterns of diversification rate variation on phylogenies 14 | * Testing hypotheses about trait-dependent diversification 15 | * Assessing the reliability of inferences with BAMM and other methods 16 | * Visualizing macroevolutionary dynamics on phylogenies 17 | * Inferring diversification rates on phylogenies that include fossils 18 | * Working with phylogenies in R 19 | 20 | Course will primarily be taught by Dan Rabosky (University of Michigan) with contributions from several co-instructors. The course will assume basic proficiency with the R programming/statistical environment and some familiarity with command line interfaces. Example datasets will be provided, but participants are encouraged to bring any phylogenetic dataset they wish to analyze (time calibrated phylogenetic trees). A personal laptop is essential. 21 | 22 | Workshop participants will arrive in Corvallis (Oregon) any time on Wednesday June 21 and we will depart for Portland on the evening of Friday June 23, such that individuals can attend the Evolution meeting. Shuttles offer easy transport between the Portland Airport and OSU’s campus every two hours, and housing is available at several hotels near campus. Details regarding accommodation and transport will be provided to successful applicants. 23 | 24 | **To apply.** please send a CV and a short statement (1-2 paragraphs) detailing your research interests, why you are interested in the course, and your prior experience with R and phylogenetics / comparative methods. Please email your application (or questions) to Dan Rabosky (macroevolution.workshop@gmail.com). 25 | 26 | **Applications will be accepted until April 2, 2017**, but please apply early as spaces are limited. Target audience is graduate students and postdocs but applications from researchers at other career stages are welcome. Preference will be given to students with a clear interest and research focus in phylogenetics & macroevolution. 27 | 28 | 29 | -------------------------------------------------------------------------------- /doc/source/bamm-header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/bamm-header.png -------------------------------------------------------------------------------- /doc/source/bamm-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/bamm-logo.png -------------------------------------------------------------------------------- /doc/source/bgg-f1.R: -------------------------------------------------------------------------------- 1 | .. _bammgraphcode1: 2 | 3 | Code for Figure 1 4 | ============ -------------------------------------------------------------------------------- /doc/source/contact_us.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | Contact Us 4 | ========== 5 | 6 | Please post any questions, comments, suggestions, possible bugs, etc. to our 7 | `BAMM Google Groups `_ 8 | page or (for bug reports) to our `GitHub `_ forum. You may also contact us directly: 9 | Dan Rabosky . 10 | 11 | If you are reporting a problem, please include (if possible) the data files 12 | that led to the problem, the control file, and as much information as possible 13 | about your operating system and computer architecture. 14 | To get the version of OS X you are using, click on the apple icon 15 | on the upper-left corner of the screen, then click on "About this Mac." 16 | On Linux, you should be able to obtain most of this information using:: 17 | 18 | uname -a 19 | 20 | If you compiled BAMM on your system, please send us details about the compiler 21 | you used. The compiler used is shown when you typed ``make`` and CMake reported 22 | the compiler found. Most compilers have the option ``--version`` to print out 23 | the version. 24 | 25 | If BAMM actually begins to perform an analysis, then it will have generated 26 | a detailed *run_info.txt* file that contains information about your analysis. 27 | Please include this file in your post or e-mail. 28 | -------------------------------------------------------------------------------- /doc/source/documentation.rst: -------------------------------------------------------------------------------- 1 | .. bamm documentation master file, created by 2 | sphinx-quickstart on Thu Oct 10 17:05:46 2013. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | BAMM Documentation 7 | ================== 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | :numbered: 12 | 13 | introduction 14 | announcements 15 | bammgraph 16 | rateshifts 17 | 18 | quickstart 19 | settingup 20 | configuration 21 | postprocess 22 | advanced 23 | 24 | colorbreaks 25 | faq 26 | likelihoodmodel 27 | testingbamm 28 | mc3 29 | strapp 30 | time-flip 31 | fossilbamm 32 | troubleshooting 33 | glossary 34 | 35 | Search 36 | ====== 37 | 38 | * :ref:`search` 39 | -------------------------------------------------------------------------------- /doc/source/download.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | Download BAMM 4 | ============= 5 | 6 | Once downloaded, see the installation instructions in 7 | `Setting Up BAMM `_. 8 | 9 | OS X 10 | ---- 11 | 12 | **Note**: See `Setting Up BAMM `_ to download and install 13 | BAMM using Homebrew. 14 | 15 | `bamm-2.5.0-MacOSX.tar.gz 16 | `_ 17 | 18 | Requires OS X 10.7.5 or greater. 19 | 20 | Windows 21 | ------- 22 | 23 | `bamm-2.5.0-windows.zip 24 | `_ 25 | 26 | Requires Windows 7 or greater (does not run on Windows XP). 27 | 28 | Examples 29 | -------- 30 | 31 | The following tar.gz and zip files (for compatibility) contain 32 | example data files for diversification and phenotypic evolution analysis. 33 | 34 | `bamm-examples.tar.gz 35 | `_ 36 | 37 | `bamm-examples.zip 38 | `_ 39 | 40 | Download BAMMtools 41 | ================== 42 | 43 | BAMMtools is now available on CRAN. 44 | Please update your BAMMtools package within R:: 45 | 46 | install.packages('BAMMtools') 47 | 48 | You can check which version of BAMMtools you have installed as follows:: 49 | 50 | packageDescription('BAMMtools') 51 | 52 | and look for the ``Version`` label. 53 | 54 | Download BAMM Source Files 55 | ========================== 56 | 57 | Access the development source files from our 58 | `GitHub page `_. 59 | 60 | BAMM Changes 61 | ============ 62 | 63 | Read important changes to BAMM since its release: 64 | `BAMM Changes `_ 65 | 66 | Previous Versions 67 | ================= 68 | 69 | You may download previous versions of the binaries and examples 70 | for BAMM using the following links: 71 | 72 | `bamm-2.3.0-MacOSX.tar.gz 73 | `_ 74 | 75 | `bamm-2.3.0-Windows.zip 76 | `_ 77 | 78 | `bamm-2.2.0-MacOSX.tar.gz 79 | `_ 80 | 81 | `bamm-2.2.0-Windows.zip 82 | `_ 83 | 84 | `bamm-2.1.0-MacOSX.tar.gz 85 | `_ 86 | 87 | `bamm-2.1.0-Windows.zip 88 | `_ 89 | 90 | `bamm-2.0.0-MacOSX.tar.gz 91 | `_ 92 | 93 | `bamm-2.0.0-Windows.zip 94 | `_ 95 | 96 | `bamm-1.0.0-MacOSX.tar.gz 97 | `_ 98 | 99 | `bamm-1.0.0-Windows.zip 100 | `_ 101 | 102 | `bamm-examples-1.0.0.tar.gz 103 | `_ 104 | 105 | `bamm-examples-1.0.0.zip 106 | `_ 107 | -------------------------------------------------------------------------------- /doc/source/figs/.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/.RData -------------------------------------------------------------------------------- /doc/source/figs/.Rhistory: -------------------------------------------------------------------------------- 1 | ls() 2 | getwd() 3 | history() 4 | quit() 5 | -------------------------------------------------------------------------------- /doc/source/figs/BAMM_E_t.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/BAMM_E_t.png -------------------------------------------------------------------------------- /doc/source/figs/BAMM_E_t_3event.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/BAMM_E_t_3event.png -------------------------------------------------------------------------------- /doc/source/figs/BF_pairwise.R: -------------------------------------------------------------------------------- 1 | library('colorspace'); 2 | library(gplots); 3 | 4 | source('/Users/danrabosky/DanWork/bamm/devel/bamm/doc/sweave/BAMMtools.R'); 5 | 6 | 7 | getColorKey <- function(x, ncols = 16, units=2){ 8 | 9 | if (ncols %% 2 != 0){ 10 | stop("expecting even number of colors"); 11 | } 12 | 13 | index <- (1:(ncols-1)) - (ncols/2); 14 | xv <- index * log(units); 15 | 16 | colset <- rich.colors(n=ncols); 17 | colmat <- matrix('', nrow=nrow(x), ncol=ncol(x)); 18 | 19 | for (i in 1:nrow(x)){ 20 | for (j in 1:ncol(x)){ 21 | if (!is.na(x[i,j])){ 22 | if (x[i,j] <= xv[1]){ 23 | colmat[i,j] <- colset[1]; 24 | }else if (x[i,j] > xv[length(xv)]){ 25 | colmat[i,j] <- colset[length(colset)]; 26 | }else{ 27 | for (k in 1:(length(xv)-1)){ 28 | if (x[i, j] > xv[k] & x[i,j] <= xv[k+1]){ 29 | colmat[i,j] <- colset[k]; 30 | } 31 | } 32 | } 33 | 34 | } 35 | } 36 | } 37 | 38 | return(colmat); 39 | } 40 | 41 | getColorBar <- function(ncols = 16, units=2){ 42 | 43 | if (ncols %% 2 != 0){ 44 | stop("expecting even number of colors"); 45 | } 46 | 47 | index <- (1:(ncols-1)) - (ncols/2); 48 | xv <- index * log(units); 49 | 50 | colset <- rich.colors(n=ncols); 51 | cx <- c(xv, max(xv)+log(units)); 52 | 53 | return(data.frame(cuts=cx, cols=colset, stringsAsFactors=F)); 54 | } 55 | 56 | 57 | 58 | cmat <- getColorKey(log(bfmat)) 59 | 60 | bfmat <- computeBayesFactors('post_mcmc_p50run1.txt', 'prior_mcmc_out.txt', modelset=0:100, constrain=F, burnin=0.25, threshold=0); 61 | 62 | 63 | 64 | 65 | 66 | ########################## 67 | ###### The figure! 68 | 69 | quartz.options(height=7, width=10, dpi=72); 70 | ll <- c(rep(1, 9), rep(2,3)); 71 | lmat <- matrix(ll, nrow=3, byrow=F); 72 | 73 | plot.new(); 74 | 75 | layout(lmat); 76 | plot.new(); 77 | par(mar=c(6,6,1,1)); 78 | 79 | plot.window(xlim=c(0,101), ylim=c(0,101), asp=1); 80 | 81 | 82 | for (i in 1:nrow(bfmat)){ 83 | #for (i in 1:5){ 84 | for (j in 1:nrow(bfmat)){ 85 | if (!is.na(bfmat[i,j])){ 86 | xval <- as.numeric(rownames(bfmat)[i]); 87 | yval <- as.numeric(colnames(bfmat)[j]) 88 | xco <- c(xval, xval, xval+1, xval+1); 89 | yco <- c(yval, yval+1, yval+1, yval); 90 | polygon(x=xco, y=yco, lwd=0.8, col=cmat[i, j], border=cmat[i,j]) 91 | } 92 | } 93 | } 94 | 95 | axis(1, at=seq(-10, 100, by=10), cex.axis=1.2); 96 | axis(2, at=seq(-10, 100, by=10), las=1, cex.axis=1.2); 97 | mtext(side=1, text="Macroevolutionary regimes, numerator", cex=1.4, line=3.5); 98 | mtext(side=2, text="Macroevolutionary regimes, denominator", cex=1.4, line=3.5); 99 | 100 | # Add the color bar 101 | cb <- getColorBar(); 102 | 103 | plot.new(); 104 | par(mar=c(7,1,1,7)); 105 | 106 | plot.window(xlim=c(0, 2.5), ylim=c(-8,8)); 107 | 108 | for (i in 2:nrow(cb)){ 109 | xv <- c(0,0,1,1); 110 | yv <- c(cb$cuts[c(i-1,i,i,i-1)]); 111 | polygon(xv, yv, col=cb$cols[i]); 112 | 113 | } 114 | 115 | axis(4, at=seq(-5, 5, by=2), las=1, cex.axis=2,pos=1.5); 116 | mtext(side=4, at=0, text="(Log) Bayes factor", cex=1.5) 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /doc/source/figs/BF_pairwise.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/BF_pairwise.pdf -------------------------------------------------------------------------------- /doc/source/figs/IncompSampling.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/IncompSampling.pdf -------------------------------------------------------------------------------- /doc/source/figs/breaksForGallery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/breaksForGallery.png -------------------------------------------------------------------------------- /doc/source/figs/breaksTest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/breaksTest.png -------------------------------------------------------------------------------- /doc/source/figs/breaksTestTrees.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/breaksTestTrees.png -------------------------------------------------------------------------------- /doc/source/figs/cohortExample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/cohortExample.png -------------------------------------------------------------------------------- /doc/source/figs/cohort_whales.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/cohort_whales.png -------------------------------------------------------------------------------- /doc/source/figs/cumulativeST.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/cumulativeST.pdf -------------------------------------------------------------------------------- /doc/source/figs/dtrates.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/dtrates.pdf -------------------------------------------------------------------------------- /doc/source/figs/dtrates1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/dtrates1.pdf -------------------------------------------------------------------------------- /doc/source/figs/dtrates2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/dtrates2.pdf -------------------------------------------------------------------------------- /doc/source/figs/dtrates3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/dtrates3.pdf -------------------------------------------------------------------------------- /doc/source/figs/dtrates_polar.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/dtrates_polar.pdf -------------------------------------------------------------------------------- /doc/source/figs/jetz_time_rates.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | quartz.options(height=6, width=6); 5 | plotRateThroughTime(ed, plot=FALSE); 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /doc/source/figs/lhmodel/branch_history.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/lhmodel/branch_history.png -------------------------------------------------------------------------------- /doc/source/figs/lhmodel/likelihood_nodecombine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/lhmodel/likelihood_nodecombine.png -------------------------------------------------------------------------------- /doc/source/figs/lhmodel/shifttype_fig.001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/lhmodel/shifttype_fig.001.png -------------------------------------------------------------------------------- /doc/source/figs/lhmodel/shifttype_fig_true.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/lhmodel/shifttype_fig_true.png -------------------------------------------------------------------------------- /doc/source/figs/lhmodel/shifttype_fig_true2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/lhmodel/shifttype_fig_true2.png -------------------------------------------------------------------------------- /doc/source/figs/lhmodel/x_tree_extinction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/lhmodel/x_tree_extinction.png -------------------------------------------------------------------------------- /doc/source/figs/maxshiftcredibility.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/maxshiftcredibility.pdf -------------------------------------------------------------------------------- /doc/source/figs/multi_shift.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/multi_shift.pdf -------------------------------------------------------------------------------- /doc/source/figs/pairwiseShiftNodes.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | zz <- computeJointShiftCorrelations(ed, threshold = 0.4); 4 | 5 | -------------------------------------------------------------------------------- /doc/source/figs/plot_multishift.R: -------------------------------------------------------------------------------- 1 | 2 | # load(BAMMtools); 3 | library(gplots); 4 | 5 | 6 | # 7 | # We will use the pre-loaded event data for the whales: 8 | # 9 | #data(events.cetaceans); # here is the event data 10 | #data(cetaceans); # here is the whale tree 11 | 12 | # Process the event data in a 'bammdata' object: 13 | bammdata <- getEventData(cetaceans, events.cetaceans); 14 | 15 | class(bammdata); # check: should be phylo and 'bammdata' 16 | 17 | # We will plot the 1st, 30th, and 40th samples from the posterior. 18 | 19 | ixx <- rep(c(10, 30, 40), 3); 20 | 21 | # Set up the plot windows 22 | par(mar=numeric(4)); 23 | quartz.options(height=10, width=10, dpi=72); 24 | plot.new(); 25 | par(mfrow=c(3,3)); 26 | 27 | # Here we set up the color schemes 28 | colschemes <- list(); 29 | colschemes[1:3] <- 'temperature'; 30 | colschemes[4:6] <- 'Spectral'; 31 | colschemes[7:9] <- list(c('blue', 'gray', 'red')); 32 | 33 | for (i in 1:length(ixx)) { 34 | #for (i in 1:3){ 35 | 36 | index <- ixx[i]; 37 | 38 | eventsub <- subsetEventData(bammdata, index=index); 39 | plot.dtrates(eventsub, method='polar', pal= colschemes[[i]], par.reset=FALSE); 40 | addBAMMshifts(eventsub, method='polar', index=1, col='white', bg='black', cex=4, par.reset=FALSE); 41 | } 42 | -------------------------------------------------------------------------------- /doc/source/figs/primates_bodysize.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/primates_bodysize.pdf -------------------------------------------------------------------------------- /doc/source/figs/rate_through_time_whales_color.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | eventfname <- '/Users/danrabosky/DanWork/bamm/devel/bamm/doc/sweave/data/event_data.txt'; 4 | whaletree <- read.tree('/Users/danrabosky/DanWork/bamm/devel/bamm/doc/sweave/data/whaletree.tre') 5 | source('/Users/danrabosky/DanWork/bamm/devel/bamm/Rcode/Load_BAMMtools.R') 6 | 7 | 8 | ## Process the event data 9 | ed <- getEventData(whaletree, eventfname, burnin=0.25, nsamples=200, verbose=T); 10 | 11 | ## Generate the rate through time matrices 12 | ## Do for ALL lineages, and for dolphins only, 13 | ## and for BACKGROUND lineages 14 | 15 | bmat <- getRateThroughTimeMatrix(ed); 16 | bmat.dolphin <- getRateThroughTimeMatrix(ed, node=140, nodetype='include'); 17 | bmat.background <- getRateThroughTimeMatrix(ed, node=140, nodetype='exclude'); 18 | 19 | 20 | 21 | quartz.options(height=10, width=10); 22 | par(mfrow=c(2,2)); 23 | 24 | plotRateThroughTime(bmat, ratetype='speciation', intervalCol='red', avgCol='red', start.time = 0, end.time=35); 25 | 26 | 27 | plotRateThroughTime(bmat, ratetype='speciation', intervalCol='red', avgCol='red', start.time = NULL, end.time=NULL); 28 | 29 | 30 | -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig2.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig2.key -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig2a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig2a.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig2a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig2a.png -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig2b.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig2b.key -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig2b.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig2b.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig2b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig2b.png -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig3a.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig3a.key -------------------------------------------------------------------------------- /doc/source/figs/shifts/Fig3a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/Fig3a.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/fig1.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/fig1.key -------------------------------------------------------------------------------- /doc/source/figs/shifts/fig1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/fig1.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/fig3a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/fig3a.png -------------------------------------------------------------------------------- /doc/source/figs/shifts/figu4.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/figu4.key -------------------------------------------------------------------------------- /doc/source/figs/shifts/figu4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/figu4.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/primatesX.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/primatesX.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/primatesX2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/primatesX2.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/primates_msc.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/primates_msc.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/primates_msc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/primates_msc.png -------------------------------------------------------------------------------- /doc/source/figs/shifts/shiftProbabilities.R: -------------------------------------------------------------------------------- 1 | 2 | library(BAMMtools); 3 | data(whales, events.whales); 4 | ewhales <- getEventData(whales, events.whales, burnin=0.1); 5 | cstw <- cumulativeShiftProbsTree(ewhales); 6 | 7 | ec <- rep('gray40', length(whales$edge.length)); 8 | ec[cstw$edge.length >= 0.95] <- 'red'; 9 | 10 | plot(whales, edge.color=ec, show.tip.label=F); 11 | 12 | 13 | 14 | 15 | data(primates, events.primates); 16 | eprimates <- getEventData(primates, events.primates, burnin=0.1, type = 'trait'); 17 | cstp <- cumulativeShiftProbsTree(eprimates); 18 | 19 | ec <- rep('gray40', length(primates$edge.length)); 20 | ec[cstp$edge.length >= 0.95] <- 'red'; 21 | 22 | plot(primates, edge.color=ec, show.tip.label=F); 23 | 24 | ################## 25 | data(prior.primates, mcmc.primates); 26 | bf <- computeBayesFactors(mcmc.primates, prior.primates) 27 | 28 | ec <- rep('gray40', length(primates$edge.length)); 29 | ec[cstp$edge.length >= 0.95] <- 'red'; 30 | 31 | plot(primates, edge.color=ec, show.tip.label=F); 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /doc/source/figs/shifts/shifts1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/shifts1.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/test.R: -------------------------------------------------------------------------------- 1 | 2 | library(BAMMtools) 3 | library(RColorBrewer) 4 | 5 | data(whales, events.whales) 6 | 7 | ed <- getEventData(whales, events.whales); 8 | 9 | plot.bammdata(ed, method='phylogram', pal= c('blue', 'gray', 'red')) 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /doc/source/figs/shifts/whales.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/whales.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/whales_cst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/whales_cst.png -------------------------------------------------------------------------------- /doc/source/figs/shifts/whales_cst1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/whales_cst1.pdf -------------------------------------------------------------------------------- /doc/source/figs/shifts/whales_pairwiseProbs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/shifts/whales_pairwiseProbs.pdf -------------------------------------------------------------------------------- /doc/source/figs/tmptree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/tmptree.pdf -------------------------------------------------------------------------------- /doc/source/figs/whale_color_rtt.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/whale_color_rtt.pdf -------------------------------------------------------------------------------- /doc/source/figs/whalerates1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/whalerates1.pdf -------------------------------------------------------------------------------- /doc/source/figs/whalerates2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/whalerates2.pdf -------------------------------------------------------------------------------- /doc/source/figs/whales_through_time.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/whales_through_time.pdf -------------------------------------------------------------------------------- /doc/source/figs/xBayesFactorsJetzPW.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xBayesFactorsJetzPW.png -------------------------------------------------------------------------------- /doc/source/figs/xCumShiftTree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xCumShiftTree.png -------------------------------------------------------------------------------- /doc/source/figs/xFig2a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xFig2a.png -------------------------------------------------------------------------------- /doc/source/figs/xFig2b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xFig2b.png -------------------------------------------------------------------------------- /doc/source/figs/xGrayscaleWhalerates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xGrayscaleWhalerates.png -------------------------------------------------------------------------------- /doc/source/figs/xIncompSampling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xIncompSampling.png -------------------------------------------------------------------------------- /doc/source/figs/xIntroFig_whalerates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xIntroFig_whalerates.png -------------------------------------------------------------------------------- /doc/source/figs/xMaxCredShiftTree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xMaxCredShiftTree.png -------------------------------------------------------------------------------- /doc/source/figs/xMultipleShifts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xMultipleShifts.png -------------------------------------------------------------------------------- /doc/source/figs/xPolarRates_whales.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xPolarRates_whales.png -------------------------------------------------------------------------------- /doc/source/figs/xPrimatesBodySize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xPrimatesBodySize.png -------------------------------------------------------------------------------- /doc/source/figs/xWhales_color_rtt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xWhales_color_rtt.png -------------------------------------------------------------------------------- /doc/source/figs/x_distinctShiftConfigs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/x_distinctShiftConfigs.png -------------------------------------------------------------------------------- /doc/source/figs/x_interpret1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/x_interpret1.png -------------------------------------------------------------------------------- /doc/source/figs/x_whales_cst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/x_whales_cst.png -------------------------------------------------------------------------------- /doc/source/figs/xdistinct_illustrate_A.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xdistinct_illustrate_A.png -------------------------------------------------------------------------------- /doc/source/figs/xdistinct_illustrate_B.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xdistinct_illustrate_B.png -------------------------------------------------------------------------------- /doc/source/figs/xdistinct_illustrate_C.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xdistinct_illustrate_C.png -------------------------------------------------------------------------------- /doc/source/figs/xdistinct_illustrate_D.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xdistinct_illustrate_D.png -------------------------------------------------------------------------------- /doc/source/figs/xfig3a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xfig3a.png -------------------------------------------------------------------------------- /doc/source/figs/xprimates_msc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xprimates_msc.png -------------------------------------------------------------------------------- /doc/source/figs/xprimates_shiftconfigs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figs/xprimates_shiftconfigs.png -------------------------------------------------------------------------------- /doc/source/figures/breaksForGallery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/breaksForGallery.png -------------------------------------------------------------------------------- /doc/source/figures/breaksmethodPhylorates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/breaksmethodPhylorates.png -------------------------------------------------------------------------------- /doc/source/figures/cohort_whales_illustrated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/cohort_whales_illustrated.png -------------------------------------------------------------------------------- /doc/source/figures/primates_credShiftSet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/primates_credShiftSet.png -------------------------------------------------------------------------------- /doc/source/figures/rateHistograms.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/rateHistograms.png -------------------------------------------------------------------------------- /doc/source/figures/scripts/colorbreaks_page.R: -------------------------------------------------------------------------------- 1 | setwd('~/bamm/doc/source/figures') 2 | 3 | library(BAMMtools) 4 | library(TeachingDemos) 5 | 6 | 7 | ##################################### 8 | ## COLOR BREAKS PAGE 9 | ##################################### 10 | 11 | # ------------------------------------------------------------------ 12 | # rateHistograms.png: rates histograms figure for colorbreaks page 13 | 14 | require(BAMMtools) 15 | data(primates, events.primates) 16 | ed <- getEventData(primates, events.primates, burnin=0.25, type = 'trait') 17 | 18 | png('rateHistograms.png', height=12, width=8, units='in', res=300) 19 | par(mfrow=c(5,1)) 20 | 21 | q <- plot.bammdata(ed, breaksmethod='linear', show=FALSE) 22 | ratesHistogram(q, xlab='', ylab='') 23 | title(main='linear', cex=2) 24 | 25 | q <- plot.bammdata(ed, breaksmethod='linear', show=FALSE, logcolor=TRUE) 26 | ratesHistogram(q, xlab='', ylab='') 27 | title(main='linear - log', cex=2) 28 | 29 | q <- plot.bammdata(ed, breaksmethod='linear', show=FALSE, color.interval=c(NA, 0.12)) 30 | ratesHistogram(q, xlab='', ylab='') 31 | title(main='linear - color.interval', cex=2) 32 | 33 | q <- plot.bammdata(ed, breaksmethod='quantile', show=FALSE) 34 | ratesHistogram(q, xlab='', ylab='') 35 | title(main='quantile', cex=2) 36 | 37 | q <- plot.bammdata(ed, breaksmethod='jenks', show=FALSE) 38 | ratesHistogram(q, xlab='', ylab='') 39 | title(main='jenks', cex=2) 40 | 41 | dev.off() 42 | 43 | 44 | #---------------------------------------- 45 | # breaksmethods example for colorbreaks page 46 | 47 | png('breaksmethodPhylorates.png', width=12, height=20, units='in', res=300) 48 | 49 | par(mfrow=c(3,2), xpd=T) 50 | q <- plot.bammdata(ed, tau=0.001, breaksmethod='linear', lwd=2) 51 | addBAMMshifts(ed, par.reset=FALSE, cex=2) 52 | title(main='linear',cex.main=2) 53 | addBAMMlegend(q, location=c(0,1,140, 250)) 54 | q <- plot.bammdata(ed, tau=0.001, breaksmethod='linear', logcolor=T, lwd=2) 55 | addBAMMshifts(ed, par.reset=FALSE, cex=2) 56 | title(main='linear - log',cex.main=2) 57 | addBAMMlegend(q, location=c(0,1,140, 250)) 58 | q <- plot.bammdata(ed, tau=0.001, breaksmethod='linear', color.interval=c(NA,0.12), lwd=2) 59 | addBAMMshifts(ed, par.reset=FALSE, cex=2) 60 | title(main='linear - color.interval',cex.main=2) 61 | addBAMMlegend(q, location=c(0,1,140, 250)) 62 | q <- plot.bammdata(ed,tau=0.001, breaksmethod='quantile', lwd=2) 63 | addBAMMshifts(ed, par.reset=FALSE, cex=2) 64 | title(main='quantile',cex.main=2) 65 | addBAMMlegend(q, location=c(0,1,140, 250)) 66 | q <- plot.bammdata(ed,tau=0.001, breaksmethod='jenks', lwd=2) 67 | addBAMMshifts(ed, par.reset=FALSE, cex=2) 68 | title(main='jenks',cex.main=2) 69 | addBAMMlegend(q, location=c(0,1,140, 250)) 70 | 71 | dev.off() 72 | -------------------------------------------------------------------------------- /doc/source/figures/scripts/intro_page.R: -------------------------------------------------------------------------------- 1 | setwd('~/bamm/doc/source/figures') 2 | library(BAMMtools) 3 | library(TeachingDemos) 4 | 5 | ################################# 6 | ## INTRODUCTION PAGE 7 | ################################# 8 | 9 | ## xIntroFig_whalerates.png 10 | library(BAMMtools) 11 | data(whales, events.whales) 12 | edata_whales <- getEventData(whales, events.whales, burnin=0.1) 13 | 14 | png(height=500, width=500, file = "xIntroFig_whalerates.png"); 15 | par(mar=c(1,1,1,1)) 16 | q <- plot.bammdata(edata_whales, lwd=3, pal="temperature") 17 | 18 | subplot(ratesHistogram(q, plotBrks=FALSE), x=7, y=75, size=c(2, 2)) 19 | 20 | dev.off(); 21 | -------------------------------------------------------------------------------- /doc/source/figures/whales_RatesThroughTime.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/whales_RatesThroughTime.png -------------------------------------------------------------------------------- /doc/source/figures/whales_RatesThroughTimeBW.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/whales_RatesThroughTimeBW.png -------------------------------------------------------------------------------- /doc/source/figures/whales_cohort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/whales_cohort.png -------------------------------------------------------------------------------- /doc/source/figures/whales_distinctShiftConfigs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/whales_distinctShiftConfigs.png -------------------------------------------------------------------------------- /doc/source/figures/whales_polar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/whales_polar.png -------------------------------------------------------------------------------- /doc/source/figures/whales_sepRateShiftConfigs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/whales_sepRateShiftConfigs.png -------------------------------------------------------------------------------- /doc/source/figures/xIntroFig_whalerates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/xIntroFig_whalerates.png -------------------------------------------------------------------------------- /doc/source/figures/x_whale_credibleshiftset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/x_whale_credibleshiftset.png -------------------------------------------------------------------------------- /doc/source/figures/x_whale_marginalodds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/x_whale_marginalodds.png -------------------------------------------------------------------------------- /doc/source/figures/x_whale_marginals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/x_whale_marginals.png -------------------------------------------------------------------------------- /doc/source/figures/x_whale_priors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/x_whale_priors.png -------------------------------------------------------------------------------- /doc/source/figures/xprimates_shiftconfigs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/xprimates_shiftconfigs.png -------------------------------------------------------------------------------- /doc/source/figures/xx_whale_postshiftsamples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/figures/xx_whale_postshiftsamples.png -------------------------------------------------------------------------------- /doc/source/filesForDownload/Rabosky_etal_SystematicBiology_2017.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/filesForDownload/Rabosky_etal_SystematicBiology_2017.pdf -------------------------------------------------------------------------------- /doc/source/filesForDownload/SupplementaryMaterial_BAMM_Text&Figures.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/filesForDownload/SupplementaryMaterial_BAMM_Text&Figures.pdf -------------------------------------------------------------------------------- /doc/source/fossilbamm.rst: -------------------------------------------------------------------------------- 1 | .. _fossilbamm: 2 | 3 | fossil BAMM 4 | =========== 5 | 6 | **Under development. Check back later!** 7 | -------------------------------------------------------------------------------- /doc/source/normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/normal.png -------------------------------------------------------------------------------- /doc/source/nsf-logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/nsf-logo.gif -------------------------------------------------------------------------------- /doc/source/rcode/BF_pairwise.R: -------------------------------------------------------------------------------- 1 | library('colorspace'); 2 | 3 | 4 | library(gplots); 5 | library(BAMMtools); 6 | 7 | getColorKey <- function(x, ncols = 16, units=2){ 8 | 9 | if (ncols %% 2 != 0){ 10 | stop("expecting even number of colors"); 11 | } 12 | 13 | index <- (1:(ncols-1)) - (ncols/2); 14 | xv <- index * log(units); 15 | 16 | colset <- rich.colors(n=ncols); 17 | colmat <- matrix('', nrow=nrow(x), ncol=ncol(x)); 18 | 19 | for (i in 1:nrow(x)){ 20 | for (j in 1:ncol(x)){ 21 | if (!is.na(x[i,j])){ 22 | if (x[i,j] <= xv[1]){ 23 | colmat[i,j] <- colset[1]; 24 | }else if (x[i,j] > xv[length(xv)]){ 25 | colmat[i,j] <- colset[length(colset)]; 26 | }else{ 27 | for (k in 1:(length(xv)-1)){ 28 | if (x[i, j] > xv[k] & x[i,j] <= xv[k+1]){ 29 | colmat[i,j] <- colset[k]; 30 | } 31 | } 32 | } 33 | 34 | } 35 | } 36 | } 37 | 38 | return(colmat); 39 | } 40 | 41 | getColorBar <- function(ncols = 16, units=2){ 42 | 43 | if (ncols %% 2 != 0){ 44 | stop("expecting even number of colors"); 45 | } 46 | 47 | index <- (1:(ncols-1)) - (ncols/2); 48 | xv <- index * log(units); 49 | 50 | colset <- rich.colors(n=ncols); 51 | cx <- c(xv, max(xv)+log(units)); 52 | 53 | return(data.frame(cuts=cx, cols=colset, stringsAsFactors=F)); 54 | } 55 | 56 | 57 | # These data files are not available for download, 58 | # but you can follow the syntax here to see 59 | # how to perform a similar analysis on your dataset: 60 | 61 | postfile <- '/Users/danrabosky/DanWork/bamm/analyses/jetzp50/post_mcmc_p50run1.txt'; 62 | prefile <- '/Users/danrabosky/DanWork/bamm/analyses/jetzp50/prior_mcmc_out.txt'; 63 | 64 | bfmat <- computeBayesFactors(postfile, prefile, modelset=0:100, burnin=0.25, threshpost=1, threshprior=0); 65 | 66 | cmat <- getColorKey(log(bfmat)) 67 | 68 | 69 | 70 | 71 | ########################## 72 | ###### The figure! 73 | 74 | quartz.options(height=7, width=10, dpi=72); 75 | ll <- c(rep(1, 9), rep(2,3)); 76 | lmat <- matrix(ll, nrow=3, byrow=F); 77 | 78 | plot.new(); 79 | 80 | layout(lmat); 81 | plot.new(); 82 | par(mar=c(6,6,1,1)); 83 | 84 | plot.window(xlim=c(0,101), ylim=c(0,101), asp=1); 85 | 86 | 87 | for (i in 1:nrow(bfmat)){ 88 | #for (i in 1:5){ 89 | for (j in 1:nrow(bfmat)){ 90 | if (!is.na(bfmat[i,j])){ 91 | xval <- as.numeric(rownames(bfmat)[i]); 92 | yval <- as.numeric(colnames(bfmat)[j]) 93 | xco <- c(xval, xval, xval+1, xval+1); 94 | yco <- c(yval, yval+1, yval+1, yval); 95 | polygon(x=xco, y=yco, lwd=0.8, col=cmat[i, j], border=cmat[i,j]) 96 | } 97 | } 98 | } 99 | 100 | axis(1, at=seq(-10, 100, by=10), cex.axis=1.2); 101 | axis(2, at=seq(-10, 100, by=10), las=1, cex.axis=1.2); 102 | mtext(side=1, text="Macroevolutionary regimes, numerator", cex=1.4, line=3.5); 103 | mtext(side=2, text="Macroevolutionary regimes, denominator", cex=1.4, line=3.5); 104 | 105 | # Add the color bar 106 | cb <- getColorBar(); 107 | 108 | plot.new(); 109 | par(mar=c(7,1,1,7)); 110 | 111 | plot.window(xlim=c(0, 2.5), ylim=c(-8,8)); 112 | 113 | for (i in 2:nrow(cb)){ 114 | xv <- c(0,0,1,1); 115 | yv <- c(cb$cuts[c(i-1,i,i,i-1)]); 116 | polygon(xv, yv, col=cb$cols[i]); 117 | 118 | } 119 | 120 | axis(4, at=seq(-5, 5, by=2), las=1, cex.axis=2,pos=1.5); 121 | mtext(side=4, at=0, text="(Log) Bayes factor", cex=1.5) 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /doc/source/rcode/cetaceans_polarRateTree.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Make sure BAMMtools package is installed. 4 | 5 | library(BAMMtools); 6 | 7 | data(whales); 8 | data(events.whales); 9 | 10 | ed <- getEventData(phy=whales, eventdata = events.whales, burnin=0.1); 11 | 12 | # This plots the 13 | plot.bammdata(ed, method='polar', pal='temperature', lwd=2); 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /doc/source/rcode/distinctShifts.R: -------------------------------------------------------------------------------- 1 | 2 | library(BAMMtools); 3 | data(whales); 4 | data(events.whales); 5 | data(prior.whales) 6 | ed <- getEventData(whales, events.whales, burnin=0.1); 7 | priordist <- getBranchShiftPriors(whales, prior.whales) 8 | cset <- credibleShiftSet(ed, threshold = priordist) 9 | 10 | quartz.options(height=5, width=8); 11 | plot.credibleshiftset(cset, plotmax=6, lwd=1.1); 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /doc/source/rcode/incompleteSampling.R: -------------------------------------------------------------------------------- 1 | library(BAMMtools); 2 | 3 | tmptree <- '((A:2,B:2):4,((C:1.5,D:1.5):1.5,(E:2,F:2):1):3);' 4 | 5 | v <- read.tree(text=tmptree); 6 | 7 | d1 <- getDesc(v, node=8)$desc_set; 8 | d2 <- getDesc(v, node=9)$desc_set; 9 | 10 | edgecolor <- rep('black', nrow(v$edge)); 11 | 12 | edgecolor[v$edge[,1] %in% d1] <- 'red'; 13 | edgecolor[v$edge[,1] %in% d2] <- 'blue'; 14 | 15 | quartz.options(height=6, width=6); 16 | par(oma=c(0,0,0,0)); 17 | plot.phylo(v, edge.width=3, edge.color=edgecolor, cex=2, label.offset=0.15); 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /doc/source/rcode/plot_multishift.R: -------------------------------------------------------------------------------- 1 | 2 | library(BAMMtools); 3 | 4 | 5 | # 6 | # We will use the pre-loaded event data for the whales: 7 | # 8 | 9 | data(events.whales); # here is the event data 10 | data(whales); # here is the whale tree 11 | 12 | # Process the event data in a 'bammdata' object: 13 | bammdata <- getEventData(whales, events.whales, burnin=0.1); 14 | 15 | class(bammdata); # check: should be 'bammdata' 16 | 17 | # We will plot the 1st, 30th, and 40th samples from the posterior. 18 | 19 | ixx <- rep(c(10, 30, 40), 3); 20 | 21 | # Set up the plot windows 22 | par(mar=numeric(4)); 23 | quartz.options(height=10, width=10, dpi=72); #This line is OSX specific 24 | plot.new(); 25 | par(mfrow=c(3,3)); 26 | 27 | # Here we set up the color schemes 28 | colschemes <- list(); 29 | colschemes[1:3] <- 'temperature'; 30 | colschemes[4:6] <- 'Spectral'; 31 | colschemes[7:9] <- list(c('blue', 'gray', 'red')); 32 | 33 | for (i in 1:length(ixx)) { 34 | #for (i in 1:3){ 35 | 36 | index <- ixx[i]; 37 | 38 | eventsub <- subsetEventData(bammdata, index=index); 39 | plot.bammdata(eventsub, method='polar', pal= colschemes[[i]], par.reset=FALSE); 40 | addBAMMshifts(eventsub, method='polar', index=1, col='white', bg='black', cex=4, par.reset=FALSE); 41 | } 42 | -------------------------------------------------------------------------------- /doc/source/rcode/primate_bodysize.R: -------------------------------------------------------------------------------- 1 | 2 | library(BAMMtools); 3 | data(primates); 4 | data(events.primates); 5 | 6 | ed <- getEventData(primates, events.primates, burnin=0.2, type='trait'); 7 | 8 | makeTransparent<-function(someColor, alpha=100) 9 | { 10 | newColor<-col2rgb(someColor) 11 | apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2], 12 | blue=curcoldata[3],alpha=alpha, maxColorValue=255)}) 13 | } 14 | 15 | graynew <- makeTransparent('gray40', alpha=75); 16 | 17 | quartz.options(height=6, width=10, dpi=72); # on mac osx 18 | par(oma=c(0,0,0,0)); 19 | par(mar=c(1,1,1,1)); 20 | par(mfrow=c(1,2)); 21 | 22 | plot.bammdata(ed, lwd=2.5, method='polar', pal = c("darkgreen", "yellow2", "red")); 23 | 24 | plot.bammdata(ed, lwd=2.5, method='polar', pal = c("darkgreen", "yellow2", "red")); 25 | 26 | for (i in 1:400){ 27 | addBAMMshifts(ed, method='polar', index=i, cex=1.5, pch=19, col =graynew); 28 | } 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /doc/source/rcode/rate_through_time_whales_color.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | library(BAMMtools); 4 | data(events.whales); 5 | data(whales); 6 | 7 | ## Process the event data 8 | ed <- getEventData(whales, events.whales, burnin=0.25, nsamples=200, verbose=F); 9 | 10 | ## Generate the rate through time matrices 11 | ## Do for ALL lineages, and for dolphins only, 12 | ## and for BACKGROUND lineages 13 | 14 | bmat <- getRateThroughTimeMatrix(ed); 15 | bmat.dolphin <- getRateThroughTimeMatrix(ed, node=140, nodetype='include'); 16 | bmat.background <- getRateThroughTimeMatrix(ed, node=140, nodetype='exclude'); 17 | 18 | 19 | 20 | quartz.options(height=4, width=10); 21 | par(oma=c(1,1,2,1)); 22 | 23 | par(mfrow=c(1,3)); 24 | 25 | plotRateThroughTime(bmat, intervalCol='red', avgCol='red', ylim=c(0,1)); 26 | mtext(side=3, line=1, "All Cetaceans", cex=1.2, font=4); 27 | 28 | plotRateThroughTime(bmat.dolphin, intervalCol='blue', avgCol='blue', ylim=c(0, 1)); 29 | mtext(side=3, line=1, "Dolphins", cex=1.2, font=4); 30 | 31 | plotRateThroughTime(bmat.background, intervalCol='darkgreen', avgCol='darkgreen', ylim=c(0, 1)); 32 | mtext(side=3, line=1, "Background (non-dolphin)", cex=1.2, font=4); 33 | 34 | -------------------------------------------------------------------------------- /doc/source/rcode/x_extinctionprobs_bamm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/x_extinctionprobs_bamm.png -------------------------------------------------------------------------------- /doc/source/rcode/x_extinctionprobs_recomputed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/x_extinctionprobs_recomputed.png -------------------------------------------------------------------------------- /doc/source/rcode/x_whale_credibleshiftset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/x_whale_credibleshiftset.png -------------------------------------------------------------------------------- /doc/source/rcode/x_whale_marginalodds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/x_whale_marginalodds.png -------------------------------------------------------------------------------- /doc/source/rcode/x_whale_marginals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/x_whale_marginals.png -------------------------------------------------------------------------------- /doc/source/rcode/x_whale_priors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/x_whale_priors.png -------------------------------------------------------------------------------- /doc/source/rcode/xx_whale_postshiftsamples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/rcode/xx_whale_postshiftsamples.png -------------------------------------------------------------------------------- /doc/source/strapp.rst: -------------------------------------------------------------------------------- 1 | .. _strapp: 2 | 3 | STRAPP: STructured Rate Permutations on Phylogenies 4 | =================================================== 5 | 6 | **Under development. Check back later!** 7 | -------------------------------------------------------------------------------- /doc/source/template.rst: -------------------------------------------------------------------------------- 1 | Template for Sphinx Commands 2 | ============================ 3 | 4 | This template shows how to use several commands available in Sphinx 5 | for creating pretty documentation, including code, math, and figures. 6 | 7 | The equation for a normal distribution is 8 | 9 | .. math:: 10 | 11 | f(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x - \mu)^2}{2\sigma^2}} 12 | 13 | In R, it can be approximated using the following commands:: 14 | 15 | > data <- rnorm(100000) 16 | > hist(data, breaks = 50) 17 | 18 | The figure produced is shown below. 19 | 20 | .. figure:: normal.png 21 | :width: 640 22 | :align: center 23 | 24 | Figure 1: Approximation of the normal distribution 25 | -------------------------------------------------------------------------------- /doc/source/testingbamm/R_BatchSimulate_MEA_trees.R: -------------------------------------------------------------------------------- 1 | # This script illustrates ascertainment bias in MEA's simulations 2 | # Script loops over a set of random starting seeds, 3 | # generates trees under MEAs simulator 4 | # 5 | # Note that MEA rejected all trees with fewer than 50 or more than 150 tips 6 | # As you can see from executing this script, trees larger than 150 tips 7 | # are very common given their parameterization 8 | # 9 | # We have made a simple modification to MEA's tree simulator such that it can easily be 10 | # used to generate trees outside of their target size range, 11 | # such that we can see what types of trees they rejected 12 | 13 | 14 | library(geiger) 15 | library(ape) 16 | 17 | # load tree simulation functions 18 | source("R_test_bamm_functions.R") 19 | 20 | # ------------------------------------- 21 | # Parameter initialization block 22 | # these are MEA's parameters from their "variable rates" dataset 23 | 24 | lamfx <- function() return(rexp(1, 1/0.15)) 25 | mufx <- function() return(rexp(1, 1/0.05)) 26 | 27 | # rate at which events occur along the phylogeny 28 | trate <- 0.006 29 | 30 | 31 | # ------------------------------------- 32 | # 33 | 34 | # Make vector of integer-valued seeds so this can be repeated: 35 | # Here we will just look at the first 50 integers from 1 to 50 36 | # but you can modify this as desired 37 | 38 | seedvec <- 1:20 39 | 40 | treeset <- list() # this stores the raw trees 41 | treeset_pruned <- list() # This stores the trees without extinct tips 42 | ntips <- rep(NA, length(seedvec)) # vector of tip counts for trees 43 | 44 | 45 | for (i in 1:length(seedvec)){ 46 | cat(i, "\n") 47 | 48 | set.seed(seedvec[i]) 49 | 50 | tree <- SimulateCBDPTree(35.5, trate, lamfx, mufx, verbose=F, NMAX = 2000, MAX_FAILS = 1) 51 | 52 | treeset[[i]] <- tree 53 | 54 | if (length(tree) > 1){ 55 | 56 | # Map shifts onto tree 57 | simTree <- CPBDPStochasticMap(tree) 58 | 59 | # Prune out extinct lineages 60 | treeset_pruned[[i]] <- pruneCPBDPTree(simTree) 61 | ntips[ i ] <- length(treeset_pruned[[i]]$tip.label) 62 | }else{ 63 | treeset_pruned[[i]] <- NA 64 | } 65 | 66 | 67 | } 68 | 69 | # You now have a list of trees that can be explored. 70 | # 71 | # For now, we will simply look at the most basic property of these trees: 72 | # their size. 73 | # 74 | # Create a dataframe that holds taxon counts 75 | # 76 | 77 | dff <- data.frame(seed = seedvec, ntips = ntips) 78 | write.table(dff, file = "MEA_treedata_unbiased.csv", sep="", quote=F, row.names=F) 79 | 80 | # IMPORTANT: this output file stores as "NA" every tree simulation that failed. 81 | # tree simulations fail whenever the total number of tips exceeds the 82 | # maximum that we specified. We set this max to 2000 for these simulations. 83 | # MEA set this to 150. You can see that many, many trees are generated that 84 | # exceed 150 tips. 85 | # 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /doc/source/testingbamm/phylorate15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/phylorate15.png -------------------------------------------------------------------------------- /doc/source/testingbamm/phylorate8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/phylorate8.png -------------------------------------------------------------------------------- /doc/source/testingbamm/phylorate8b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/phylorate8b.png -------------------------------------------------------------------------------- /doc/source/testingbamm/phylorate9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/phylorate9.png -------------------------------------------------------------------------------- /doc/source/testingbamm/s15_points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/s15_points.png -------------------------------------------------------------------------------- /doc/source/testingbamm/s8_points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/s8_points.png -------------------------------------------------------------------------------- /doc/source/testingbamm/s9_points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/s9_points.png -------------------------------------------------------------------------------- /doc/source/testingbamm/s9_points2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/testingbamm/s9_points2.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery1.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery2.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery3.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery4.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery5.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery6.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bammgraphgallery7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bammgraphgallery7.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bayesfactorbranches1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bayesfactorbranches1.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bayesfactorbranches2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bayesfactorbranches2.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bayesfactorbranches3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bayesfactorbranches3.png -------------------------------------------------------------------------------- /doc/source/v2rcode/bayesfactorbranches4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/bayesfactorbranches4.png -------------------------------------------------------------------------------- /doc/source/v2rcode/distinctshiftconfigs.R: -------------------------------------------------------------------------------- 1 | 2 | data(whales, events.whales, prior.whales) 3 | edata <- getEventData(whales, events.whales, burnin=0.1) 4 | 5 | set.seed(5) 6 | tipset <- sample(whales$tip.label, size=50) 7 | edata <- subtreeBAMM(edata, tips = tipset) 8 | 9 | set.seed(1); 10 | sset <- sample(1:length(edata$eventData), size=20) 11 | 12 | dset <- subsetEventData(edata, index=sset) 13 | 14 | 15 | png(height=700, width=600, file = 'distinctshiftconfigs1.png') 16 | par(mfrow=c(5,4)) 17 | for (i in 1:20){ 18 | par(mar=c(1,1,1,1)) 19 | tmp <- subsetEventData(edata, index=sset[i]) 20 | plot(as.phylo.bammdata(tmp), no.margin=F, show.tip.label=F) 21 | addBAMMshifts(tmp, bg='red', cex=2, par.reset=F) 22 | } 23 | dev.off(); 24 | 25 | 26 | 27 | ########### plot whale phylo with 28 | data(whales, events.whales, prior.whales) 29 | edata <- getEventData(whales, events.whales, burnin=0.1) 30 | mprobs <- marginalShiftProbsTree(edata) 31 | nodes <- mprobs$edge[,2]; 32 | probs <- mprobs$edge.length 33 | 34 | nodes <- nodes[mprobs$edge.length > 0] 35 | probs <- probs[mprobs$edge.length > 0] 36 | 37 | minx <- 1; 38 | maxx <- 3; 39 | minp <- min(probs) 40 | maxp <- max(probs) 41 | 42 | mm <- (maxx - minx) / (maxp - minp) 43 | 44 | cexvec <- (probs - minp) * mm + minx; 45 | 46 | png(height = 700, width=600, file = 'distinctshiftconfigs2.png') 47 | plot.phylo(whales, show.tip.label=F, edge.width=1.3) 48 | 49 | nodelabels(node=nodes, cex=1.3, pch=21, bg='blue') 50 | 51 | dev.off() 52 | 53 | 54 | ### 55 | data(prior.whales) 56 | prior <- getBranchShiftPriors(whales, prior.whales) 57 | bf <- bayesFactorBranches(edata, prior) 58 | 59 | nodeset <- bf$edge[,2][bf$edge.length >= 3] 60 | 61 | png(height = 700, width=600, file = 'distinctshiftconfigs3.png') 62 | plot.phylo(whales, show.tip.label=F, edge.width=1.3) 63 | 64 | nodelabels(node=nodeset, cex=1.6, pch=21, bg='red') 65 | 66 | dev.off() 67 | 68 | ############ 69 | 70 | 71 | data(prior.whales) 72 | prior <- getBranchShiftPriors(whales, prior.whales) 73 | css <- credibleShiftSet(edata, prior, BFcriterion=3) 74 | plot(css) 75 | 76 | png(height = 600, width=600, file = 'distinctshiftconfigs4.png') 77 | plot(css) 78 | dev.off() 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /doc/source/v2rcode/distinctshiftconfigs1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/distinctshiftconfigs1.png -------------------------------------------------------------------------------- /doc/source/v2rcode/distinctshiftconfigs2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/distinctshiftconfigs2.png -------------------------------------------------------------------------------- /doc/source/v2rcode/distinctshiftconfigs3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/distinctshiftconfigs3.png -------------------------------------------------------------------------------- /doc/source/v2rcode/distinctshiftconfigs4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/distinctshiftconfigs4.png -------------------------------------------------------------------------------- /doc/source/v2rcode/overallbestshiftconfig.R: -------------------------------------------------------------------------------- 1 | 2 | data(primates, events.primates, prior.primates) 3 | prior <- getBranchShiftPriors(primates, prior.primates) 4 | ed <- getEventData(primates, events.primates, burnin=0.1, type ='trait') 5 | best <- getBestShiftConfiguration(ed, prior=prior, BFcriterion=25) 6 | plot.bammdata(best, lwd=1.25) 7 | addBAMMshifts(best, cex=2) 8 | 9 | data(whales, events.whales, prior.whales) 10 | prior <- getBranchShiftPriors(whales, prior.whales) 11 | ed <- getEventData(whales, events.whales, burnin=0.1) 12 | best <- getBestShiftConfiguration(ed, prior=prior, BFcriterion=3) 13 | plot.bammdata(best, lwd=1.25) 14 | addBAMMshifts(best, cex=2) 15 | 16 | 17 | 18 | png(height=1000, width=1000, file = 'overallbestshiftconfig1.png') 19 | plot.bammdata(best, lwd=1.75) 20 | addBAMMshifts(best, cex=2) 21 | dev.off() 22 | 23 | -------------------------------------------------------------------------------- /doc/source/v2rcode/overallbestshiftconfig1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/overallbestshiftconfig1.png -------------------------------------------------------------------------------- /doc/source/v2rcode/rateshifts_prior.R: -------------------------------------------------------------------------------- 1 | library(BAMMtools) 2 | 3 | prob.k <- function(k, poissonRatePrior=1) { 4 | Denom <- (poissonRatePrior + 1)^(k+1) 5 | Prob <- poissonRatePrior / Denom 6 | return(Prob) 7 | } 8 | 9 | png(file = 'rateshifts_prior.png', width=450, height=450); 10 | 11 | obsK <- seq(from=0, to=19, by=1) 12 | expectedNumberofShifts <- 1 13 | 14 | priorD <- sapply(obsK, prob.k, poissonRatePrior=1/expectedNumberofShifts) 15 | 16 | par(mar=c(5,5,1,1)) 17 | plot(1, 1, xlim=c(0,max(obsK)), ylim=c(0,0.5), type='n', xlab="Number of shifts", ylab="Probability") 18 | points(obsK, priorD, pch=21, bg='red', type='b') 19 | 20 | dev.off(); 21 | 22 | ##### 23 | 24 | png(file = 'rateshifts_prior_0.1.png', width=450, height=450); 25 | 26 | 27 | obsK <- seq(from=0, to=19, by=1) 28 | expectedNumberofShifts <- 10 29 | 30 | priorD <- sapply(obsK, prob.k, poissonRatePrior=1/expectedNumberofShifts) 31 | 32 | par(mar=c(5,5,1,1)) 33 | plot(1, 1, xlim=c(0,max(obsK)), ylim=c(0,0.5), type='n', xlab="Number of shifts", ylab="Probability") 34 | points(obsK, priorD, pch=21, bg='red', type='b') 35 | 36 | dev.off(); 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /doc/source/v2rcode/rateshifts_prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/rateshifts_prior.png -------------------------------------------------------------------------------- /doc/source/v2rcode/rateshifts_prior_0.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macroevolution/bamm/ab1b69be13e9841d9e103170d0f61e4324f78676/doc/source/v2rcode/rateshifts_prior_0.1.png -------------------------------------------------------------------------------- /doc/sweave/data/run_info_post.txt: -------------------------------------------------------------------------------- 1 | Command line: ./bamm -control divcontrol.txt 2 | Git commit id: a032dcb67696d74bafde049bef68f48343f3843c 3 | Random seed (clock): 1385505927 4 | Start time: Tue Nov 26 17:45:27 2013 5 | 6 | Current parameter settings: 7 | acceptWriteFreq 5000 8 | autotune 0 9 | branchRatesWriteFreq 0 10 | eventDataInfile event_data_in.txt 11 | eventDataOutfile event_data.txt 12 | eventDataWriteFreq 5000 13 | globalSamplingFraction 1.0 14 | initialNumberEvents 0 15 | initializeModel 1 16 | lambdaInit0 0.032 17 | lambdaInitPrior 1.0 18 | lambdaOutfile lambda_rates.txt 19 | lambdaShift0 0 20 | lambdaShiftPrior 0.05 21 | loadEventData 0 22 | localGlobalMoveRatio 10.0 23 | mcmcOutfile mcmc_out.txt 24 | mcmcWriteFreq 5000 25 | minCladeSizeForShift 1 26 | modeltype speciationextinction 27 | muInit0 .005 28 | muInitPrior 1.0 29 | muOutfile mu_rates.txt 30 | muShift0 0.0 31 | muShiftPrior 1.0 32 | numberGenerations 5000000 33 | outName 34 | overwrite 1 35 | poissonRatePrior 1.0 36 | printFreq 5000 37 | runInfoFilename run_info.txt 38 | runMCMC 1 39 | sampleFromPriorOnly 0 40 | sampleProbsFilename sample_probs.txt 41 | seed -1 42 | segLength 1.0 43 | treefile ../examples/diversification/whales/whaletree.tre 44 | updateEventLocationScale 2.0 45 | updateEventRateScale 4.0 46 | updateLambdaInitScale 2.0 47 | updateLambdaShiftScale 0.1 48 | updateMuInitScale 2.0 49 | updateMuShiftScale 0.0 50 | updateRateEventNumber 0.1 51 | updateRateEventPosition 1 52 | updateRateEventRate 1 53 | updateRateLambda0 1 54 | updateRateLambdaShift 1 55 | updateRateMu0 1 56 | updateRateMuShift 0.0 57 | useGlobalSamplingProbability 1 58 | writeMeanBranchLengthTrees 0 59 | End time: Tue Nov 26 17:54:02 2013 60 | 61 | -------------------------------------------------------------------------------- /doc/sweave/makesweave.sh: -------------------------------------------------------------------------------- 1 | 2 | #This will overwrite, but assume everything in Rcode/BAMMtools is current 3 | rm BAMMtools.R 4 | cat ../../Rcode/BAMMtools/*.R >> BAMMtools.R 5 | 6 | 7 | R CMD batch bammdoc_makefigs.R 8 | 9 | R CMD sweave bammdoc1.Rnw 10 | 11 | # Run this three times for good measure! 12 | 13 | pdflatex bammdoc1.tex 14 | pdflatex bammdoc1.tex 15 | pdflatex bammdoc1.tex 16 | 17 | -------------------------------------------------------------------------------- /examples/diversification/anoles/GA_Anolis_MCC.tre: -------------------------------------------------------------------------------- 1 | ((((((((ahli:6.544436481,allogus:6.544436481):5.45394495,rubribarbus:11.99838143):17.38620364,imias:29.38458508):6.398895957,((((sagrei:12.88102021,(bremeri:5.487182621,quadriocellifer:5.487182621):7.39383759):3.075299922,ophiolepis:15.95632013):4.36096088,mestrei:20.31728101):6.490702506,(((jubar:5.943297621,homolechis:5.943297621):4.526135954,confusus:10.46943358):2.107788591,guafe:12.57722217):14.23076135):8.975497514):6.886185624,((((garmani:10.00167905,opalinus:10.00167905):0.9843599411,grahami:10.98603899):10.8904957,valencienni:21.87653468):6.130643032,(lineatopus:23.56855311,reconditus:23.56855311):4.438624604):14.66248894):3.351759762,(((evermanni:10.67601358,stratulus:10.67601358):17.60760293,(((krugi:16.33780327,pulchellus:16.33780327):6.564651854,(gundlachi:19.32330063,poncensis:19.32330063):3.57915449):1.517539033,(cooki:19.7644096,cristatellus:19.7644096):4.655584552):3.863622354):7.477878776,(((brevirostris:13.78711733,(caudalis:8.524873097,marron:8.524873097):5.262244234):1.336374726,websteri:15.12349206):4.917874343,distichus:20.0413664):15.72012888):10.25993114):1.744366152,(((barbouri:40.10542509,(((alumina:13.40538439,semilineatus:13.40538439):10.9683589,olssoni:24.37374329):13.11118303,(etheridgei:29.41536076,(fowleri:18.854692,insolitus:18.854692):10.56066875):8.069565566):2.620498769):3.360194845,((((whitemani:17.10135633,((haetianus:13.34917036,breslini:13.34917036):3.481091738,((armouri:7.419547631,cybotes:7.419547631):2.208359111,shrevei:9.627906742):7.202355359):0.2710942246):5.332800474,(longitibialis:12.60626673,strahmi:12.60626673):9.827890069):4.571501266,marcanoi:27.00565807):12.52637604,((((((baleatus:2.086522712,barahonae:2.086522712):2.631837766,ricordii:4.718360478):10.18010755,eugenegrahami:14.89846803):4.255550995,christophei:19.15401903):4.547667011,cuvieri:23.70168604):5.381927504,(barbatus:7.339713347,(porcus:4.655292118,(chamaeleonides:3.815118093,guamuhaya:3.815118093):0.8401740246):2.68442123):21.74390019):10.44842056):3.933585836):3.798999624,((((((((altitudinalis:8.744497094,oporinus:8.744497094):4.610159031,isolepis:13.35465613):12.69460446,(allisoni:14.8011465,porcatus:14.8011465):11.24811409):1.851745599,(((argillaceus:5.710826139,centralis:5.710826139):1.24881222,pumilis:6.959638359):11.78128137,loysiana:18.74091973):9.160086453):4.261431265,guazuma:32.16243745):2.322058746,((placidus:9.347897895,sheplani:9.347897895):18.86829905,(alayoni:18.96909033,(angusticeps:10.86063481,paternus:10.86063481):8.10845552):9.247106617):6.26829925):3.943522271,((alutaceus:6.04309845,inexpectatus:6.04309845):20.21257905,(((clivicola:16.79799015,(cupeyalensis:4.303151532,cyanopleurus:4.303151532):12.49483861):5.948682113,(alfaroi:14.0116969,macilentus:14.0116969):8.734975363):0.461393415,vanidicus:23.20806568):3.047611821):12.17234097):4.717657381,(argenteolus:32.82165973,lucius:32.82165973):10.32401612):4.118943716):0.5011730105):2.234207429,(((bartschi:26.23626837,vermiculatus:26.23626837):12.4729884,((((baracoae:2.926988768,(noblei:1.070308761,smallwoodi:1.070308761):1.856680007):1.424582119,luteogularis:4.351570887):0.89496035,equestris:5.246531237):31.48597248,(((monticola:30.27768839,(bahorucoensis:19.20550341,(dolichocephalus:7.546354666,hendersoni:7.546354666):11.65914875):11.07218498):1.574600858,darlingtoni:31.85228925):1.644368006,(((aliniger:8.917713736,singularis:8.917713736):6.885287536,chlorocyanus:15.80300127):10.67813301,coelestinus:26.48113428):7.015522978):3.235846465):1.976753045):6.037411928,occultus:44.74666869):5.253331306); 2 | -------------------------------------------------------------------------------- /examples/traits/whalesize/whale_size.txt: -------------------------------------------------------------------------------- 1 | Balaena_mysticetus 18 2 | Eubalaena_australis 13.85 3 | Eubalaena_glacialis 13.7 4 | Caperea_marginata 6.21 5 | Eschrichtius_robustus 14.63 6 | Balaenoptera_acutorostrata 10.7 7 | Balaenoptera_physalus 21.2 8 | Megaptera_novaeangliae 17.98 9 | Balaenoptera_musculus 33.58 10 | Balaenoptera_borealis 16.09 11 | Balaenoptera_edeni 15.4 12 | Physeter_catodon 11.03 13 | Kogia_breviceps 3.4 14 | Kogia_simus 2.43 15 | Platanista_gangetica 2.52 16 | Platanista_minor 2.52 17 | Tasmacetus_shepherdi 6.5 18 | Berardius_arnuxii 8.85 19 | Berardius_bairdii 12 20 | Ziphius_cavirostris 6.37 21 | Indopacetus_pacificus 7.25 22 | Hyperoodon_ampullatus 7.92 23 | Hyperoodon_planifrons 7.45 24 | Mesoplodon_bidens 5.05 25 | Mesoplodon_ginkgodens 4.9 26 | Mesoplodon_europaeus 5.2 27 | Mesoplodon_mirus 5.14 28 | Mesoplodon_bowdoini 4.47 29 | Mesoplodon_carlhubbsi 5.32 30 | Mesoplodon_layardii 6.15 31 | Mesoplodon_hectori 4.43 32 | Mesoplodon_densirostris 4.71 33 | Mesoplodon_stejnegeri 5.67 34 | Mesoplodon_grayi 5.33 35 | Mesoplodon_peruvianus 3.52 36 | Inia_geoffrensis 1.98 37 | Pontoporia_blainvillei 1.49 38 | Delphinapterus_leucas 3.81 39 | Monodon_monoceros 4.27 40 | Neophocaena_phocaenoides 1.41 41 | Phocoena_phocoena 1.86 42 | Phocoenoides_dalli 1.94 43 | Phocoena_sinus 1.14 44 | Phocoena_spinipinnis 1.67 45 | Orcinus_orca 7.92 46 | Orcaella_brevirostris 2.21 47 | Grampus_griseus 3.66 48 | Pseudorca_crassidens 5.06 49 | Feresa_attenuata 2.43 50 | Peponocephala_electra 2.57 51 | Globicephala_macrorhynchus 4.79 52 | Globicephala_melas 5.12 53 | Lagenorhynchus_albirostris 3.05 54 | Lagenorhynchus_acutus 2.43 55 | Lissodelphis_borealis 2.27 56 | Lissodelphis_peronii 2.97 57 | Cephalorhynchus_hectori 1.53 58 | Cephalorhynchus_commersonii 1.46 59 | Lagenorhynchus_obscurus 1.93 60 | Lagenorhynchus_obliquidens 2.36 61 | Cephalorhynchus_heavisidii 1.69 62 | Lagenorhynchus_australis 2.1 63 | Lagenorhynchus_cruciger 1.83 64 | Steno_bredanensis 2.55 65 | Sotalia_fluviatilis 1.49 66 | Lagenodelphis_hosei 2.64 67 | Stenella_longirostris 1.96 68 | Stenella_attenuata 2.11 69 | Tursiops_truncatus 2.37 70 | Sousa_chinensis 2.44 71 | Stenella_clymene 1.88 72 | Stenella_coeruleoalba 2.3 73 | Stenella_frontalis 2.13 74 | Delphinus_delphis 2.3 75 | -------------------------------------------------------------------------------- /examples/traits/whalesize/whaletree.tre: -------------------------------------------------------------------------------- 1 | (((Balaena_mysticetus:8.816019,(Eubalaena_australis:1.622021,Eubalaena_glacialis:1.622021):7.193998):19.183981,(Caperea_marginata:26.063016,(Eschrichtius_robustus:17.890655,((Balaenoptera_acutorostrata:15.099324,(Balaenoptera_physalus:10.472234,Megaptera_novaeangliae:10.472234):4.62709):0.967361,(Balaenoptera_musculus:12.847395,(Balaenoptera_borealis:5.265325,Balaenoptera_edeni:5.265325):7.58207):3.21929):1.82397):8.172361):1.936984):7.857844,((Physeter_catodon:22.044391,(Kogia_breviceps:8.803018,Kogia_simus:8.803018):13.241373):11.754612,((Platanista_gangetica:0.28307,Platanista_minor:0.28307):32.107591,((Tasmacetus_shepherdi:19.195664,((Berardius_arnuxii:6.28945,Berardius_bairdii:6.28945):11.733962,(Ziphius_cavirostris:15.669702,((Indopacetus_pacificus:11.028304,(Hyperoodon_ampullatus:8.100266,Hyperoodon_planifrons:8.100266):2.928038):3.511979,(Mesoplodon_bidens:13.042869,(Mesoplodon_ginkgodens:8.925943,(Mesoplodon_europaeus:8.252103,(Mesoplodon_mirus:7.677424,((Mesoplodon_bowdoini:4.732448,(Mesoplodon_carlhubbsi:4.170968,Mesoplodon_layardii:4.170968):0.56148):2.444232,(Mesoplodon_hectori:6.375631,((Mesoplodon_densirostris:4.927159,Mesoplodon_stejnegeri:4.927159):0.869428,(Mesoplodon_grayi:5.096384,Mesoplodon_peruvianus:5.096384):0.700202):0.579045):0.801049):0.500744):0.574679):0.67384):4.116927):1.497414):1.129419):2.35371):1.172252):12.425865,((Inia_geoffrensis:18.226419,Pontoporia_blainvillei:18.226419):7.77358,(((Delphinapterus_leucas:5.466432,Monodon_monoceros:5.466432):8.595122,((Neophocaena_phocaenoides:4.985876,(Phocoena_phocoena:3.706276,Phocoenoides_dalli:3.706276):1.2796):0.630505,(Phocoena_sinus:4.94717,Phocoena_spinipinnis:4.94717):0.66921):8.445174):3.877872,(Orcinus_orca:10.70277,((Orcaella_brevirostris:8.20905,(Grampus_griseus:6.04703,(Pseudorca_crassidens:5.493241,(Feresa_attenuata:4.45272,(Peponocephala_electra:3.048675,(Globicephala_macrorhynchus:1.470782,Globicephala_melas:1.470782):1.577893):1.404045):1.040521):0.55379):2.16202):1.228963,(Lagenorhynchus_albirostris:8.716039,((Lagenorhynchus_acutus:6.975185,((Lissodelphis_borealis:1.361358,Lissodelphis_peronii:1.361358):3.902137,((Cephalorhynchus_hectori:3.291163,Cephalorhynchus_commersonii:3.291164):1.279729,(Lagenorhynchus_obscurus:3.791853,(Lagenorhynchus_obliquidens:2.919779,(Cephalorhynchus_heavisidii:2.096219,(Lagenorhynchus_australis:1.570433,Lagenorhynchus_cruciger:1.570433):0.525786):0.82356):0.872074):0.77904):0.692602):1.71169):1.167873,((Steno_bredanensis:5.897506,Sotalia_fluviatilis:5.897506):1.616888,((Lagenodelphis_hosei:3.44536,Stenella_longirostris:3.44536):0.910357,((Stenella_attenuata:3.07917,Tursiops_truncatus:3.07917):0.546945,(Sousa_chinensis:2.83298,(Stenella_clymene:1.934812,((Stenella_coeruleoalba:1.011163,Stenella_frontalis:1.011163):0.495574,Delphinus_delphis:1.506737):0.428075):0.898168):0.793135):0.729602):3.158677):0.628664):0.572981):0.721974):1.264757):7.236657):8.060574):5.621528):0.769132):1.408343):2.058841); 2 | -------------------------------------------------------------------------------- /problems/acanthurid.tre: -------------------------------------------------------------------------------- 1 | ((((((Zebrasoma_flavecens:1.3164506477491957,Zebrasoma_scopas:1.3164506477491957):13.482349334016167,(Zebrasoma_velifer:5.2741496201023965,Zebrasoma_desjarinii:5.2741496201023965):9.524650361662967):5.030515076995094,Paracanthurus_hepatus:19.829315058760457):19.030409060000384,((((((Acanthurus_olivaceus:16.019616537614958,((Acanthurus_xanthopterus:8.893079275216206,Acanthurus_bariene:8.893079275216206):3.7840585775298585,(Acanthurus_nigricauda:9.449754246689249,(Acanthurus_leucocheilus:7.119791920113494,Acanthurus_auranticavus:7.119791920113494):2.329962326575756):3.227383606056815):3.342478684868894):2.1086691323924,((Acanthurus_chirurgus:6.29528596494765,Acanthurus_bahianus:6.29528596494765):7.3758241953976125,Acanthurus_monroviae:13.671110160345261):4.457175509662097):2.5569303217512918,((((Ctenochatus_truncatus:2.052597970642159,Ctenochaetus_strigosus:2.052597970642159):1.9414905937453955,Ctenochaetus_binotatus:3.9940885643875546):6.417563484953077,Ctenochaetus_tominensis:10.411652049340631):4.1579625697699605,(Ctenochaetus_striatus:11.77120310781751,Acanthurus_pyroferus:11.77120310781751):2.7984115112930823):6.115601372648058):1.8941324397743564,Acanthurus_lineatus:22.579348431533006):1.6109521434722254,(((Acanthurus_coeruleus:12.436476579532828,Acanthurus_guttatus:12.436476579532828):6.98869045713891,(Acanthurus_achilles:6.105102238722402,((Acanthurus_japonicus:0.9943695130731589,Acanthurus_nigricans:0.9943695130731589):2.0028184404856946,Acanthurus_leucosternon:2.9971879535588535):3.1079142851635484):13.320064797949335):3.5542840884288722,(Acanthurus_nigrfuscus:20.462558803721222,Acanthurus_thomsoni:20.462558803721222):2.5168923213793875):1.2108494499046216):1.7042576475921933,Acanthurus_triostegus:25.894558222597425):12.965165896163416):5.92688819917241,((Prionurus_biafranensis:5.016294854826533,(Prionurus_punctatus:0.5794927108616157,Prionurus_laticlavus:0.5794927108616157):4.4368021439649175):16.637027384127194,(Prionurus_microlepidotus:16.870455425306815,(Prionurus_maculatus:9.309220816451095,Prionurus_scalprum:9.309220816451095):7.56123460885572):4.782866813646912):23.133290078979524):11.124415189591929,(Naso_elegans:18.827794171210236,(Naso_lituratus:15.379894776677546,(Naso_unicornis:12.987694199019462,((Naso_minor:9.180038643411908,(Naso_annulatus:6.747731098168156,Naso_lopezi:6.747731098168157):2.4323075452437504):1.3522979303500815,((Naso_caesius:1.2154199495017144,Naso_hexacanthus:1.2154199495017144):5.7460479740048,Naso_vlamingii:6.961467923506515):3.5708686502554743):2.4553576252574736):2.392200577658084):3.4478993945326906):37.08323333631495):5.367639891259252; 2 | -------------------------------------------------------------------------------- /src/AcceptanceDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "AcceptanceDataWriter.h" 2 | #include "Settings.h" 3 | #include "Model.h" 4 | 5 | #include 6 | #include 7 | 8 | 9 | AcceptanceDataWriter::AcceptanceDataWriter(const Settings& settings) : 10 | _shouldOutputData(settings.get("outputAcceptanceInfo")), 11 | _outputFileName(settings.get("acceptanceInfoFileName")) 12 | { 13 | if (_shouldOutputData) { 14 | initializeStream(); 15 | writeHeader(); 16 | } 17 | } 18 | 19 | 20 | void AcceptanceDataWriter::initializeStream() 21 | { 22 | _outputStream.open(_outputFileName.c_str()); 23 | } 24 | 25 | 26 | void AcceptanceDataWriter::writeHeader() 27 | { 28 | _outputStream << header() << std::endl; 29 | } 30 | 31 | 32 | std::string AcceptanceDataWriter::header() 33 | { 34 | return "param,accepted"; 35 | } 36 | 37 | 38 | AcceptanceDataWriter::~AcceptanceDataWriter() 39 | { 40 | if (_shouldOutputData) { 41 | _outputStream.close(); 42 | } 43 | } 44 | 45 | 46 | void AcceptanceDataWriter::writeData(Model& model) 47 | { 48 | if (!_shouldOutputData) { 49 | return; 50 | } 51 | 52 | _outputStream << model.getLastParameterUpdated() << "," 53 | << model.getAcceptLastUpdate() << std::endl; 54 | } 55 | -------------------------------------------------------------------------------- /src/AcceptanceDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef ACCEPTANCE_DATA_WRITER_H 2 | #define ACCEPTANCE_DATA_WRITER_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | class Settings; 9 | class Model; 10 | 11 | 12 | class AcceptanceDataWriter 13 | { 14 | public: 15 | 16 | AcceptanceDataWriter(const Settings& settings); 17 | ~AcceptanceDataWriter(); 18 | 19 | void writeData(Model& model); 20 | 21 | private: 22 | 23 | void initializeStream(); 24 | void writeHeader(); 25 | std::string header(); 26 | 27 | bool _shouldOutputData; 28 | 29 | std::string _outputFileName; 30 | std::ofstream _outputStream; 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/BetaInitProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "BetaInitProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | #include "TraitBranchEvent.h" 8 | 9 | 10 | BetaInitProposal::BetaInitProposal 11 | (Random& random, Settings& settings, Model& model, Prior& prior) : 12 | EventParameterProposal(random, settings, model, prior) 13 | { 14 | _weight = _settings.get("updateRateBeta0"); 15 | _updateBetaInitScale = _settings.get("updateBetaInitScale"); 16 | } 17 | 18 | 19 | double BetaInitProposal::getCurrentParameterValue() 20 | { 21 | return static_cast(_event)->getBetaInit(); 22 | } 23 | 24 | 25 | double BetaInitProposal::computeNewParameterValue() 26 | { 27 | _cterm = std::exp(_updateBetaInitScale * (_random.uniform() - 0.5)); 28 | return _cterm * _currentParameterValue; 29 | } 30 | 31 | 32 | void BetaInitProposal::setProposedParameterValue() 33 | { 34 | static_cast(_event)-> 35 | setBetaInit(_proposedParameterValue); 36 | } 37 | 38 | 39 | void BetaInitProposal::revertToOldParameterValue() 40 | { 41 | static_cast(_event)->setBetaInit(_currentParameterValue); 42 | } 43 | 44 | 45 | void BetaInitProposal::updateParameterOnTree() 46 | { 47 | _tree->setMeanBranchTraitRates(); 48 | } 49 | 50 | 51 | double BetaInitProposal::computeRootLogPriorRatio() 52 | { 53 | return _prior.betaInitRootPrior(_proposedParameterValue) - 54 | _prior.betaInitRootPrior(_currentParameterValue); 55 | } 56 | 57 | 58 | double BetaInitProposal::computeNonRootLogPriorRatio() 59 | { 60 | return _prior.betaInitPrior(_proposedParameterValue) - 61 | _prior.betaInitPrior(_currentParameterValue); 62 | } 63 | 64 | 65 | double BetaInitProposal::computeLogQRatio() 66 | { 67 | return std::log(_cterm); 68 | } 69 | -------------------------------------------------------------------------------- /src/BetaInitProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef BETA_INIT_PROPOSAL_H 2 | #define BETA_INIT_PROPOSAL_H 3 | 4 | 5 | #include "EventParameterProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class BetaInitProposal : public EventParameterProposal 14 | { 15 | public: 16 | 17 | BetaInitProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | private: 21 | 22 | virtual double getCurrentParameterValue(); 23 | virtual double computeNewParameterValue(); 24 | 25 | virtual void setProposedParameterValue(); 26 | virtual void revertToOldParameterValue(); 27 | 28 | virtual void updateParameterOnTree(); 29 | 30 | virtual double computeRootLogPriorRatio(); 31 | virtual double computeNonRootLogPriorRatio(); 32 | virtual double computeLogQRatio(); 33 | 34 | double _updateBetaInitScale; 35 | double _cterm; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/BetaShiftProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "BetaShiftProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | #include "TraitBranchEvent.h" 8 | 9 | 10 | BetaShiftProposal::BetaShiftProposal 11 | (Random& random, Settings& settings, Model& model, Prior& prior) : 12 | EventParameterProposal(random, settings, model, prior) 13 | { 14 | _weight = _settings.get("updateRateBetaShift"); 15 | _updateBetaShiftScale = _settings.get("updateBetaShiftScale"); 16 | } 17 | 18 | 19 | double BetaShiftProposal::acceptanceRatio() 20 | { 21 | if (static_cast(_event)->isTimeVariable()) { 22 | return EventParameterProposal::acceptanceRatio(); 23 | } else { 24 | return 0.0; 25 | } 26 | } 27 | 28 | 29 | double BetaShiftProposal::getCurrentParameterValue() 30 | { 31 | return static_cast(_event)->getBetaShift(); 32 | } 33 | 34 | 35 | double BetaShiftProposal::computeNewParameterValue() 36 | { 37 | return _currentParameterValue + _random.normal(0.0, _updateBetaShiftScale); 38 | } 39 | 40 | 41 | void BetaShiftProposal::setProposedParameterValue() 42 | { 43 | static_cast(_event)-> 44 | setBetaShift(_proposedParameterValue); 45 | } 46 | 47 | 48 | void BetaShiftProposal::revertToOldParameterValue() 49 | { 50 | static_cast(_event)-> 51 | setBetaShift(_currentParameterValue); 52 | } 53 | 54 | 55 | void BetaShiftProposal::updateParameterOnTree() 56 | { 57 | _tree->setMeanBranchTraitRates(); 58 | } 59 | 60 | 61 | double BetaShiftProposal::computeRootLogPriorRatio() 62 | { 63 | return _prior.betaShiftRootPrior(_proposedParameterValue) - 64 | _prior.betaShiftRootPrior(_currentParameterValue); 65 | } 66 | 67 | 68 | double BetaShiftProposal::computeNonRootLogPriorRatio() 69 | { 70 | return _prior.betaShiftPrior(_proposedParameterValue) - 71 | _prior.betaShiftPrior(_currentParameterValue); 72 | } 73 | -------------------------------------------------------------------------------- /src/BetaShiftProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef BETA_SHIFT_PROPOSAL_H 2 | #define BETA_SHIFT_PROPOSAL_H 3 | 4 | 5 | #include "EventParameterProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class BetaShiftProposal : public EventParameterProposal 14 | { 15 | public: 16 | 17 | BetaShiftProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | virtual double acceptanceRatio(); 21 | 22 | private: 23 | 24 | virtual double getCurrentParameterValue(); 25 | virtual double computeNewParameterValue(); 26 | 27 | virtual void setProposedParameterValue(); 28 | virtual void revertToOldParameterValue(); 29 | 30 | virtual void updateParameterOnTree(); 31 | 32 | virtual double computeRootLogPriorRatio(); 33 | virtual double computeNonRootLogPriorRatio(); 34 | 35 | double _updateBetaShiftScale; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/BetaTimeModeProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "BetaTimeModeProposal.h" 2 | 3 | #include "Settings.h" 4 | #include "Prior.h" 5 | #include "TraitBranchEvent.h" 6 | 7 | class Random; 8 | class Model; 9 | 10 | 11 | BetaTimeModeProposal::BetaTimeModeProposal 12 | (Random& random, Settings& settings, Model& model) 13 | : TimeModeProposal(random, settings, model) 14 | { 15 | _weight = settings.get("updateRateBetaTimeMode"); 16 | } 17 | 18 | 19 | double BetaTimeModeProposal::initialParameter(BranchEvent* event) 20 | { 21 | return static_cast(event)->getBetaInit(); 22 | } 23 | 24 | 25 | double BetaTimeModeProposal::rateParameter(BranchEvent* event) 26 | { 27 | return static_cast(event)->getBetaShift(); 28 | } 29 | 30 | 31 | bool BetaTimeModeProposal::isTimeVariable(BranchEvent* event) 32 | { 33 | return static_cast(event)->isTimeVariable(); 34 | } 35 | 36 | 37 | void BetaTimeModeProposal::setEventParameters(BranchEvent* event, 38 | double initParam, double rateParam, bool isTimeVariable) 39 | { 40 | TraitBranchEvent* traitEvent = static_cast(event); 41 | 42 | traitEvent->setBetaInit(initParam); 43 | traitEvent->setBetaShift(rateParam); 44 | traitEvent->setTimeVariable(isTimeVariable); 45 | } 46 | 47 | 48 | void BetaTimeModeProposal::setModelParameters() 49 | { 50 | _tree->setMeanBranchTraitRates(); 51 | } 52 | 53 | 54 | double BetaTimeModeProposal::rateParameterFromPrior() 55 | { 56 | return _prior.generateBetaShiftFromPrior(); 57 | } 58 | -------------------------------------------------------------------------------- /src/BetaTimeModeProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef BETA_TIME_MODE_PROPOSAL_H 2 | #define BETA_TIME_MODE_PROPOSAL_H 3 | 4 | 5 | #include "TimeModeProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | 11 | 12 | class BetaTimeModeProposal : public TimeModeProposal 13 | { 14 | public: 15 | 16 | BetaTimeModeProposal(Random& random, Settings& settings, Model& model); 17 | 18 | protected: 19 | 20 | double initialParameter(BranchEvent* event); 21 | double rateParameter(BranchEvent* event); 22 | bool isTimeVariable(BranchEvent* event); 23 | 24 | void setEventParameters(BranchEvent* event, 25 | double initParam, double rateParam, bool isTimeVariable); 26 | 27 | void setModelParameters(); 28 | 29 | double rateParameterFromPrior(); 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/BranchHistory.h: -------------------------------------------------------------------------------- 1 | #ifndef BRANCH_HISTORY_H 2 | #define BRANCH_HISTORY_H 3 | 4 | #include 5 | #include "BranchEvent.h" 6 | 7 | 8 | class BranchHistory 9 | { 10 | typedef std::set EventSet; 11 | typedef EventSet::size_type EventSetSizeType; 12 | 13 | private: 14 | 15 | BranchEvent* _nodeEvent; // event describing focal node 16 | BranchEvent* _ancestralNodeEvent; // event describing ancestor 17 | 18 | // Set of all events on branch. Of length 0 if no events occurred on branch. 19 | // Also, if no events occur on branch, then entire branch is described by 20 | // the event referenced at nodeEvent 21 | EventSet _eventsOnBranch; 22 | 23 | public: 24 | 25 | BranchHistory(); 26 | 27 | BranchEvent* getLastEvent(); 28 | 29 | // Get last event from a reference event that occurred on branch: 30 | BranchEvent* getLastEvent(BranchEvent* x); 31 | BranchEvent* getLastEvent(double ttime); 32 | BranchEvent* getNextEvent(double ttime); 33 | int getNumberOfEventsOnInterval(double t1, double t2); 34 | BranchEvent* getEventByIndexPosition(int i); 35 | 36 | void setNodeEvent(BranchEvent* x); 37 | BranchEvent* getNodeEvent(); 38 | 39 | void setAncestralNodeEvent(BranchEvent* x); 40 | BranchEvent* getAncestralNodeEvent(); 41 | 42 | void printBranchHistory(); 43 | void reversePrintBranchHistory(); 44 | void printEvent(BranchEvent* event); 45 | 46 | void popEventOffBranchHistory(BranchEvent* x); 47 | void addEventToBranchHistory(BranchEvent* x); 48 | int getNumberOfBranchEvents(); 49 | }; 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/ChainSwapDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "ChainSwapDataWriter.h" 2 | #include "Settings.h" 3 | #include "Model.h" 4 | #include "MCMC.h" 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | ChainSwapDataWriter::ChainSwapDataWriter(Settings& settings) : 12 | _numberOfChains(settings.get("numberOfChains")), 13 | _outputFileName(settings.get("chainSwapFileName")) 14 | { 15 | if (_numberOfChains > 1) { 16 | initializeStream(); 17 | writeHeader(); 18 | } 19 | } 20 | 21 | 22 | void ChainSwapDataWriter::initializeStream() 23 | { 24 | _outputStream.open(_outputFileName.c_str()); 25 | } 26 | 27 | 28 | void ChainSwapDataWriter::writeHeader() 29 | { 30 | _outputStream << header() << std::endl; 31 | } 32 | 33 | 34 | std::string ChainSwapDataWriter::header() const 35 | { 36 | return "generation,rank_1,rank_2,swapAccepted"; 37 | } 38 | 39 | 40 | ChainSwapDataWriter::~ChainSwapDataWriter() 41 | { 42 | if (_numberOfChains > 1) { 43 | _outputStream.close(); 44 | } 45 | } 46 | 47 | 48 | void ChainSwapDataWriter::writeData(int generation, 49 | const std::vector& chains, int chain_1, int chain_2, bool accepted) 50 | { 51 | const std::vector& chainRanks = rankChainsByTemp(chains); 52 | 53 | int rank_1 = chainRanks[chain_1]; 54 | int rank_2 = chainRanks[chain_2]; 55 | 56 | if (rank_2 < rank_1) { 57 | std::swap(rank_1, rank_2); 58 | } 59 | 60 | _outputStream << generation << "," 61 | << rank_1 << "," 62 | << rank_2 << "," 63 | << accepted << std::endl; 64 | } 65 | 66 | 67 | std::vector ChainSwapDataWriter::rankChainsByTemp 68 | (const std::vector& chains) const 69 | { 70 | const std::vector& temps = chainTemperatures(chains); 71 | const std::vector& sortedTemps = sortValues(temps); 72 | 73 | std::vector ranks; 74 | for (int i = 0; i < (int)temps.size(); i++) { 75 | int tempRank = rankValue(temps[i], sortedTemps); 76 | if (tempRank > 0) { 77 | ranks.push_back(tempRank); 78 | } else { 79 | log(Error) << "Error while ranking chain temperatures.\n"; 80 | std::exit(1); 81 | } 82 | } 83 | 84 | return ranks; 85 | } 86 | 87 | 88 | std::vector ChainSwapDataWriter::chainTemperatures 89 | (const std::vector& chains) const 90 | { 91 | std::vector temps; 92 | for (int i = 0; i < (int)chains.size(); i++) { 93 | temps.push_back(chains[i]->model().getTemperatureMH()); 94 | } 95 | return temps; 96 | } 97 | 98 | 99 | std::vector ChainSwapDataWriter::sortValues 100 | (std::vector values) const 101 | { 102 | std::vector sortedValues(values); 103 | std::sort(sortedValues.begin(), sortedValues.end()); 104 | std::reverse(sortedValues.begin(), sortedValues.end()); 105 | return sortedValues; 106 | } 107 | 108 | 109 | int ChainSwapDataWriter::rankValue 110 | (double value, std::vector sortedValues) const 111 | { 112 | for (int i = 0; i < (int)sortedValues.size(); i++) { 113 | if (value == sortedValues[i]) 114 | return i + 1; 115 | } 116 | 117 | return -1; 118 | } 119 | -------------------------------------------------------------------------------- /src/ChainSwapDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef CHAIN_SWAP_DATA_WRITER_H 2 | #define CHAIN_SWAP_DATA_WRITER_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | class Settings; 10 | class Model; 11 | class MCMC; 12 | 13 | 14 | class ChainSwapDataWriter 15 | { 16 | public: 17 | 18 | ChainSwapDataWriter(Settings& settings); 19 | ~ChainSwapDataWriter(); 20 | 21 | void writeData(int generation, const std::vector& chains, 22 | int chain_1, int chain_2, bool accepted); 23 | 24 | private: 25 | 26 | void initializeStream(); 27 | void writeHeader(); 28 | std::string header() const; 29 | 30 | std::vector rankChainsByTemp(const std::vector& chains) const; 31 | std::vector chainTemperatures 32 | (const std::vector& chains) const; 33 | std::vector sortValues(std::vector values) const; 34 | int rankValue(double value, std::vector sortedValues) const; 35 | 36 | int _numberOfChains; 37 | 38 | std::string _outputFileName; 39 | std::ofstream _outputStream; 40 | }; 41 | 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/CommandLineProcessor.cpp: -------------------------------------------------------------------------------- 1 | #include "CommandLineProcessor.h" 2 | #include "Log.h" 3 | 4 | #include 5 | #include 6 | 7 | 8 | CommandLineProcessor::CommandLineProcessor(int argc, char* argv[]) 9 | { 10 | // Start at argv[1] because argv[0] is the program name 11 | for (int i = 1; i < argc; i += 2) { 12 | std::string argName(argv[i]); 13 | 14 | // Print help message 15 | if (argName == "-h" || argName == "--help") { 16 | exitWithMessage(usageText()); 17 | } 18 | 19 | if (argName == "--version") { 20 | exitWithMessage(versionText()); 21 | } 22 | 23 | // Every argument name must be followed by its value 24 | if (i + 1 == argc) { 25 | exitWithError(missingArgumentValueText()); 26 | } 27 | 28 | std::string argValue(argv[i + 1]); 29 | 30 | // Control file argument is special, don't add it to parameter list 31 | if (argName == "-c" || argName == "--control") { 32 | _controlFileName = argValue; 33 | } else { 34 | // Every parameter name must start with two hyphens 35 | if (!startsWithTwoHyphens(argName)) { 36 | exitWithError(invalidArgumentNameText()); 37 | } 38 | 39 | argName = argName.substr(2); // Cut out the "--" 40 | _parameters.push_back(UserParameter(argName, argValue)); 41 | } 42 | } 43 | 44 | // Print help message if control file was not specified 45 | if (_controlFileName == "") { 46 | exitWithMessage(usageText()); 47 | } 48 | } 49 | 50 | 51 | std::string CommandLineProcessor::usageText() const 52 | { 53 | return "Usage: bamm -c " 54 | "[-- ...]"; 55 | } 56 | 57 | 58 | std::string CommandLineProcessor::versionText() const 59 | { 60 | // "BAMM " is in a std::string so that its operator+() is used 61 | return std::string("BAMM ") + BAMM_VERSION + 62 | " (" + BAMM_VERSION_DATE + ")"; 63 | } 64 | 65 | 66 | std::string CommandLineProcessor::missingArgumentValueText() const 67 | { 68 | return "Missing value after parameter name"; 69 | } 70 | 71 | 72 | bool CommandLineProcessor::startsWithTwoHyphens(const std::string& str) const 73 | { 74 | return (str.length() > 2) && (str[0] == '-') && (str[1] == '-'); 75 | } 76 | 77 | 78 | std::string CommandLineProcessor::invalidArgumentNameText() const 79 | { 80 | return "Parameter name must start with \"--\""; 81 | } 82 | 83 | 84 | const std::string& CommandLineProcessor::controlFileName() const 85 | { 86 | return _controlFileName; 87 | } 88 | 89 | 90 | const std::vector& CommandLineProcessor::parameters() const 91 | { 92 | return _parameters; 93 | } 94 | -------------------------------------------------------------------------------- /src/CommandLineProcessor.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_LINE_PROCESSOR_H 2 | #define COMMAND_LINE_PROCESSOR_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | typedef std::pair UserParameter; 9 | 10 | 11 | class CommandLineProcessor 12 | { 13 | public: 14 | 15 | CommandLineProcessor(int argc, char* argv[]); 16 | 17 | const std::string& controlFileName() const; 18 | const std::vector& parameters() const; 19 | 20 | private: 21 | 22 | std::string usageText() const; 23 | std::string versionText() const; 24 | std::string missingArgumentValueText() const; 25 | bool startsWithTwoHyphens(const std::string& str) const; 26 | std::string invalidArgumentNameText() const; 27 | 28 | std::string _controlFileName; 29 | std::vector _parameters; 30 | }; 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/EventCountLog.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // EventCountLog.cpp 3 | // bammx 4 | // 5 | // Created by Dan Rabosky on 5/23/14. 6 | // Copyright (c) 2014 Dan Rabosky. All rights reserved. 7 | // 8 | 9 | #include "EventCountLog.h" 10 | 11 | 12 | EventCountLog::EventCountLog(int x) 13 | { 14 | _eventCount = x; 15 | 16 | _addProposeCount = 0; 17 | _addAcceptCount = 0; 18 | _subtractProposeCount = 0; 19 | _subtractAcceptCount = 0; 20 | 21 | _inStateCount = 0; 22 | 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/EventCountLog.h: -------------------------------------------------------------------------------- 1 | // 2 | // EventCountLog.h 3 | // bammx 4 | // 5 | // Created by Dan Rabosky on 5/23/14. 6 | // Copyright (c) 2014 Dan Rabosky. All rights reserved. 7 | // 8 | 9 | #ifndef __bammx__EventCountLog__ 10 | #define __bammx__EventCountLog__ 11 | 12 | #include 13 | 14 | class EventCountLog 15 | { 16 | 17 | public: 18 | 19 | EventCountLog(int x); 20 | 21 | 22 | int getAddProposeCount(); 23 | void incrementAddProposeCount(); 24 | int getAddAcceptCount(); 25 | void incrementAddAcceptCount(); 26 | int getSubtractProposeCount(); 27 | void incrementSubtractProposeCount(); 28 | int getSubtractAcceptCount(); 29 | void incrementSubtractAcceptCount(); 30 | 31 | int getEventCount(); 32 | 33 | int getInStateCount(); 34 | void incrementInStateCount(); 35 | 36 | private: 37 | 38 | int _addProposeCount; 39 | int _addAcceptCount; 40 | int _subtractProposeCount; 41 | int _subtractAcceptCount; 42 | 43 | int _eventCount; 44 | 45 | int _inStateCount; 46 | 47 | }; 48 | 49 | 50 | inline int EventCountLog::getAddAcceptCount() 51 | { 52 | return _addAcceptCount; 53 | } 54 | 55 | inline void EventCountLog::incrementAddAcceptCount() 56 | { 57 | _addAcceptCount++; 58 | } 59 | 60 | inline int EventCountLog::getAddProposeCount() 61 | { 62 | return _addProposeCount; 63 | } 64 | 65 | inline void EventCountLog::incrementAddProposeCount() 66 | { 67 | _addProposeCount++; 68 | } 69 | 70 | inline int EventCountLog::getSubtractProposeCount() 71 | { 72 | return _subtractProposeCount; 73 | } 74 | 75 | inline void EventCountLog::incrementSubtractProposeCount() 76 | { 77 | _subtractProposeCount++; 78 | } 79 | 80 | inline int EventCountLog::getSubtractAcceptCount() 81 | { 82 | return _subtractAcceptCount; 83 | } 84 | 85 | inline void EventCountLog::incrementSubtractAcceptCount() 86 | { 87 | _subtractAcceptCount++; 88 | } 89 | 90 | inline int EventCountLog::getEventCount() 91 | { 92 | return _eventCount; 93 | } 94 | 95 | 96 | inline int EventCountLog::getInStateCount() 97 | { 98 | return _inStateCount; 99 | } 100 | 101 | inline void EventCountLog::incrementInStateCount() 102 | { 103 | _inStateCount++; 104 | } 105 | 106 | #endif /* defined(__bammx__EventCountLog__) */ 107 | -------------------------------------------------------------------------------- /src/EventDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "EventDataWriter.h" 2 | #include "Settings.h" 3 | #include "Model.h" 4 | #include "BranchEvent.h" 5 | #include "Node.h" 6 | 7 | #include 8 | 9 | 10 | EventDataWriter::EventDataWriter(Settings& settings) : 11 | _outputFileName(settings.get("eventDataOutfile")), 12 | _outputFreq(settings.get("eventDataWriteFreq")), 13 | _headerWritten(false) 14 | { 15 | if (_outputFreq > 0) { 16 | _outputStream.open(_outputFileName.c_str()); 17 | } 18 | } 19 | 20 | 21 | 22 | EventDataWriter::~EventDataWriter() 23 | { 24 | if (_outputFreq > 0) { 25 | _outputStream.close(); 26 | } 27 | } 28 | 29 | 30 | void EventDataWriter::writeData(int generation, Model& model) 31 | { 32 | if (_outputFreq == 0 || generation % _outputFreq != 0) { 33 | return; 34 | } 35 | 36 | writeHeaderOnce(); 37 | writeEventData(generation, model); 38 | } 39 | 40 | 41 | void EventDataWriter::writeHeaderOnce() 42 | { 43 | if (_headerWritten) { 44 | return; 45 | } 46 | 47 | writeHeader(); 48 | _headerWritten = true; 49 | } 50 | 51 | 52 | void EventDataWriter::writeHeader() 53 | { 54 | _outputStream << header() << specificHeader() << std::endl; 55 | } 56 | 57 | 58 | std::string EventDataWriter::header() 59 | { 60 | return "generation,leftchild,rightchild,abstime"; 61 | } 62 | 63 | 64 | void EventDataWriter::writeEventData(int generation, Model& model) 65 | { 66 | writeRootEvent(generation, model); 67 | writeEvents(generation, model); 68 | } 69 | 70 | 71 | void EventDataWriter::writeRootEvent(int generation, Model& model) 72 | { 73 | writeEvent(generation, model.getRootEvent()); 74 | } 75 | 76 | 77 | void EventDataWriter::writeEvents(int generation, Model& model) 78 | { 79 | EventSet& events = model.events(); 80 | 81 | EventSet::iterator it; 82 | for (it = events.begin(); it != events.end(); ++it) { 83 | writeEvent(generation, *it); 84 | } 85 | } 86 | 87 | 88 | void EventDataWriter::writeEvent(int generation, BranchEvent* event) 89 | { 90 | _outputStream << generation << "," 91 | << leftNodeName(event) << "," 92 | << rightNodeName(event) << "," 93 | << event->getAbsoluteTime() << "," 94 | << eventParameters(event) << std::endl; 95 | } 96 | 97 | 98 | std::string EventDataWriter::leftNodeName(BranchEvent* event) 99 | { 100 | Node* eventNode = event->getEventNode(); 101 | if (eventNode->getIsTip()) { 102 | return eventNode->getName(); 103 | } else { 104 | return eventNode->getRandomLeftTipNode()->getName(); 105 | } 106 | } 107 | 108 | 109 | std::string EventDataWriter::rightNodeName(BranchEvent* event) 110 | { 111 | Node* eventNode = event->getEventNode(); 112 | if (eventNode->getIsTip()) { 113 | return "NA"; 114 | } else { 115 | return eventNode->getRandomRightTipNode()->getName(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/EventDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef EVENT_DATA_WRITER_H 2 | #define EVENT_DATA_WRITER_H 3 | 4 | #include 5 | #include 6 | 7 | class Settings; 8 | class Model; 9 | class BranchEvent; 10 | class Node; 11 | 12 | 13 | class EventDataWriter 14 | { 15 | public: 16 | 17 | EventDataWriter(Settings& settings); 18 | virtual ~EventDataWriter(); 19 | 20 | void writeData(int generation, Model& model); 21 | 22 | protected: 23 | 24 | void writeHeaderOnce(); 25 | void writeHeader(); 26 | std::string header(); 27 | virtual std::string specificHeader() = 0; 28 | 29 | void writeEventData(int generation, Model& model); 30 | void writeRootEvent(int generation, Model& model); 31 | void writeEvents(int generation, Model& model); 32 | 33 | void writeEvent(int generation, BranchEvent* event); 34 | std::string leftNodeName(BranchEvent* event); 35 | std::string rightNodeName(BranchEvent* event); 36 | virtual std::string eventParameters(BranchEvent* event) = 0; 37 | 38 | std::string _outputFileName; 39 | std::ofstream _outputStream; 40 | int _outputFreq; 41 | 42 | bool _headerWritten; 43 | }; 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/EventNumberForBranchProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef EVENT_NUMBER_FOR_BRANCH_PROPOSAL_H 2 | #define EVENT_NUMBER_FOR_BRANCH_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class BranchEvent; 11 | 12 | 13 | class EventNumberForBranchProposal : public Proposal 14 | { 15 | enum ProposalType { 16 | AddEvent, 17 | RemoveEvent 18 | }; 19 | 20 | public: 21 | 22 | EventNumberForBranchProposal 23 | (Random& random, Settings& settings, Model& model); 24 | 25 | virtual void propose(); 26 | virtual void accept(); 27 | virtual void reject(); 28 | 29 | virtual double acceptanceRatio(); 30 | 31 | private: 32 | 33 | double computeLogLikelihoodRatio(); 34 | double computeLogPriorRatio(); 35 | double computeLogQRatio(); 36 | 37 | Random& _random; 38 | Model& _model; 39 | 40 | bool _validateEventConfiguration; 41 | 42 | int _numberOfBranches; 43 | double _totalTreeLength; 44 | 45 | int _currentEventCount; 46 | double _currentLogLikelihood; 47 | double _currentLogPrior; 48 | 49 | int _proposedEventCount; 50 | double _proposedLogLikelihood; 51 | double _proposedLogPrior; 52 | 53 | ProposalType _lastProposal; 54 | BranchEvent* _lastEventChanged; 55 | }; 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/EventNumberProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef EVENT_NUMBER_PROPOSAL_H 2 | #define EVENT_NUMBER_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class BranchEvent; 11 | 12 | 13 | class EventNumberProposal : public Proposal 14 | { 15 | // TODO: Future: Add and Remove events should be separate Proposals 16 | enum ProposalType { 17 | AddEvent, 18 | RemoveEvent 19 | }; 20 | 21 | public: 22 | 23 | EventNumberProposal(Random& random, Settings& settings, Model& model); 24 | 25 | virtual void propose(); 26 | virtual void accept(); 27 | virtual void reject(); 28 | 29 | virtual double acceptanceRatio(); 30 | 31 | private: 32 | 33 | double computeLogLikelihoodRatio(); 34 | double computeLogPriorRatio(); 35 | double computeLogQRatio(); 36 | 37 | Random& _random; 38 | Model& _model; 39 | 40 | bool _validateEventConfiguration; 41 | 42 | int _currentEventCount; 43 | double _currentLogLikelihood; 44 | double _currentLogPrior; 45 | 46 | int _proposedEventCount; 47 | double _proposedLogLikelihood; 48 | double _proposedLogPrior; 49 | 50 | ProposalType _lastProposal; 51 | BranchEvent* _lastEventChanged; 52 | }; 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /src/EventParameterProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "EventParameterProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | 8 | #include 9 | #include 10 | 11 | 12 | EventParameterProposal::EventParameterProposal 13 | (Random& random, Settings& settings, Model& model, Prior& prior) : 14 | _random(random), _settings(settings), _model(model), _prior(prior), 15 | _tree(_model.getTreePtr()) 16 | { 17 | } 18 | 19 | 20 | void EventParameterProposal::propose() 21 | { 22 | _event = _model.chooseEventAtRandom(true); 23 | _currentParameterValue = getCurrentParameterValue(); 24 | 25 | _currentLogLikelihood = _model.getCurrentLogLikelihood(); 26 | 27 | _proposedParameterValue = computeNewParameterValue(); 28 | setProposedParameterValue(); 29 | 30 | updateParameterOnTree(); 31 | 32 | _proposedLogLikelihood = _model.computeLogLikelihood(); 33 | } 34 | 35 | 36 | void EventParameterProposal::accept() 37 | { 38 | _model.setCurrentLogLikelihood(_proposedLogLikelihood); 39 | } 40 | 41 | 42 | void EventParameterProposal::reject() 43 | { 44 | revertToOldParameterValue(); 45 | updateParameterOnTree(); 46 | } 47 | 48 | 49 | double EventParameterProposal::acceptanceRatio() 50 | { 51 | double logLikelihoodRatio = computeLogLikelihoodRatio(); 52 | double logPriorRatio = computeLogPriorRatio(); 53 | double logQRatio = computeLogQRatio(); 54 | 55 | double t = _model.getTemperatureMH(); 56 | double logRatio = t * (logLikelihoodRatio + logPriorRatio) + logQRatio; 57 | 58 | if (std::isfinite(logRatio)) { 59 | return std::min(1.0, std::exp(logRatio)); 60 | } else { 61 | return 0.0; 62 | } 63 | } 64 | 65 | 66 | double EventParameterProposal::computeLogLikelihoodRatio() 67 | { 68 | return _proposedLogLikelihood - _currentLogLikelihood; 69 | } 70 | 71 | 72 | double EventParameterProposal::computeLogPriorRatio() 73 | { 74 | if (_event == _model.getRootEvent()) { 75 | return computeRootLogPriorRatio(); 76 | } else { 77 | return computeNonRootLogPriorRatio(); 78 | } 79 | } 80 | 81 | 82 | double EventParameterProposal::computeRootLogPriorRatio() 83 | { 84 | return 0.0; 85 | } 86 | 87 | 88 | double EventParameterProposal::computeNonRootLogPriorRatio() 89 | { 90 | return 0.0; 91 | } 92 | 93 | 94 | double EventParameterProposal::computeLogQRatio() 95 | { 96 | return 0.0; 97 | } 98 | -------------------------------------------------------------------------------- /src/EventParameterProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef EVENT_PARAMETER_PROPOSAL_H 2 | #define EVENT_PARAMETER_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | class Tree; 12 | class BranchEvent; 13 | 14 | 15 | class EventParameterProposal : public Proposal 16 | { 17 | public: 18 | 19 | EventParameterProposal(Random& random, Settings& settings, Model& model, 20 | Prior& prior); 21 | 22 | virtual void propose(); 23 | virtual void accept(); 24 | virtual void reject(); 25 | 26 | virtual double acceptanceRatio(); 27 | 28 | protected: 29 | 30 | virtual double getCurrentParameterValue() = 0; 31 | virtual double computeNewParameterValue() = 0; 32 | 33 | virtual void setProposedParameterValue() = 0; 34 | virtual void revertToOldParameterValue() = 0; 35 | 36 | virtual void updateParameterOnTree() = 0; 37 | 38 | virtual double computeLogLikelihoodRatio(); 39 | virtual double computeLogPriorRatio(); 40 | virtual double computeRootLogPriorRatio(); 41 | virtual double computeNonRootLogPriorRatio(); 42 | virtual double computeLogQRatio(); 43 | 44 | Random& _random; 45 | Settings& _settings; 46 | Model& _model; 47 | Prior& _prior; 48 | 49 | Tree* _tree; 50 | BranchEvent* _event; 51 | 52 | double _currentParameterValue; 53 | double _proposedParameterValue; 54 | 55 | double _currentLogLikelihood; 56 | double _proposedLogLikelihood; 57 | }; 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/EventRateProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "EventRateProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | 7 | #include 8 | 9 | #define USE_ANALYTICAL_POSTERIOR 10 | 11 | 12 | EventRateProposal::EventRateProposal 13 | (Random& random, Settings& settings, Model& model, Prior& prior) : 14 | _random(random), _settings(settings), _model(model), _prior(prior) 15 | { 16 | _weight = _settings.get("updateRateEventRate"); 17 | _updateEventRateScale = _settings.get("updateEventRateScale"); 18 | } 19 | 20 | 21 | void EventRateProposal::propose() 22 | { 23 | _currentEventRate = _model.getEventRate(); 24 | 25 | _cterm = std::exp(_updateEventRateScale * (_random.uniform() - 0.5)); 26 | _proposedEventRate = _cterm * _currentEventRate; 27 | 28 | _model.setEventRate(_proposedEventRate); 29 | 30 | 31 | 32 | } 33 | 34 | 35 | void EventRateProposal::accept() 36 | { 37 | } 38 | 39 | 40 | void EventRateProposal::reject() 41 | { 42 | _model.setEventRate(_currentEventRate); 43 | } 44 | 45 | 46 | double EventRateProposal::acceptanceRatio() 47 | { 48 | 49 | double logQRatio = computeLogQRatio(); 50 | 51 | //#ifdef USE_ANALYTICAL_POSTERIOR 52 | 53 | double logPosteriorRatio = computeLogPosteriorRatio(); 54 | double t = _model.getTemperatureMH(); 55 | double logRatio = t * logPosteriorRatio + logQRatio; 56 | 57 | //#else 58 | // double logPriorRatio = computeLogPriorRatio(); 59 | // double t = _model.getTemperatureMH(); 60 | // double logRatio = t * logPriorRatio + logQRatio; 61 | // 62 | //#endif 63 | 64 | if (std::isfinite(logRatio)) { 65 | return std::min(1.0, std::exp(logRatio)); 66 | } else { 67 | return 0.0; 68 | } 69 | } 70 | 71 | 72 | double EventRateProposal::computeLogPriorRatio() 73 | { 74 | return _prior.poissonRatePrior(_proposedEventRate) - 75 | _prior.poissonRatePrior(_currentEventRate); 76 | } 77 | 78 | 79 | double EventRateProposal::computeLogQRatio() 80 | { 81 | return std::log(_cterm); 82 | } 83 | 84 | 85 | // May 26 , 2015: directly compute log-posterior ratio 86 | // after correspondence with Bret Larget / Cecile Ane 87 | double EventRateProposal::computeLogPosteriorRatio() 88 | { 89 | 90 | double NN = (double)_model.getNumberOfEvents(); 91 | 92 | double logPosteriorRatio = NN * (std::log(_proposedEventRate) - std::log(_currentEventRate) ); 93 | logPosteriorRatio += (_settings.get("poissonRatePrior") + 1 ) * (_currentEventRate - _proposedEventRate); 94 | 95 | return logPosteriorRatio; 96 | 97 | } 98 | 99 | #undef USE_ANALYTICAL_POSTERIOR 100 | 101 | 102 | -------------------------------------------------------------------------------- /src/EventRateProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef EVENT_RATE_PROPOSAL_H 2 | #define EVENT_RATE_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class EventRateProposal : public Proposal 14 | { 15 | public: 16 | 17 | EventRateProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | virtual void propose(); 21 | virtual void accept(); 22 | virtual void reject(); 23 | 24 | virtual double acceptanceRatio(); 25 | 26 | private: 27 | 28 | double computeLogLikelihoodRatio(); 29 | double computeLogPosteriorRatio(); 30 | double computeLogPriorRatio(); 31 | double computeLogQRatio(); 32 | 33 | Random& _random; 34 | Settings& _settings; 35 | Model& _model; 36 | Prior& _prior; 37 | 38 | double _updateEventRateScale; 39 | 40 | double _currentEventRate; 41 | double _proposedEventRate; 42 | 43 | double _cterm; 44 | }; 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/FastSimulatePrior.h: -------------------------------------------------------------------------------- 1 | // 2 | // FastSimulatePrior.h 3 | // bamm 4 | // 5 | // Created by Dan Rabosky on 11/29/13. 6 | // Copyright (c) 2013 Dan Rabosky. All rights reserved. 7 | // 8 | 9 | #ifndef __bamm__FastSimulatePrior__ 10 | #define __bamm__FastSimulatePrior__ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | //Forward declarations 19 | class Node; 20 | class Random; 21 | class Settings; 22 | class EventCountLog; 23 | 24 | class FastSimulatePrior 25 | { 26 | public: 27 | 28 | FastSimulatePrior(Random& random, Settings* sp); 29 | ~FastSimulatePrior(); 30 | 31 | void updateState(); 32 | void updateState(int min, int max); 33 | void changeNumberOfEventsMH(); 34 | void changeNumberOfEventsMH(int min, int max); 35 | 36 | void updateEventRateMH(); 37 | int getNumberOfEvents(); 38 | double getEventRate(); 39 | bool acceptMetropolisHastings(const double lnR); 40 | 41 | 42 | void fastSimulatePriorOldWay(); 43 | void fastSimulatePriorExperimental(); 44 | 45 | // Output settings: 46 | // void writeStateToFile(); 47 | void writeHeaderToOutputFile(); 48 | 49 | private: 50 | 51 | int round(double x); 52 | 53 | Random& _random; 54 | Settings* sttings; 55 | 56 | double _eventRate; 57 | int _numberEvents; 58 | int _generations; 59 | double _updateEventRateScale; 60 | double _poissonRatePrior; 61 | 62 | std::string _outfileName; 63 | 64 | void incrementGeneration(); 65 | void setEventRate(double x); 66 | 67 | std::ofstream _fspOutStream; 68 | std::ofstream _eventDataOutStream; 69 | 70 | void writeStateTofile(); 71 | void writeStateToStream(std::ostream& outStream); 72 | void exitWithErrorOutputFileExists(); 73 | 74 | 75 | 76 | /**** New params May 23 2014 ****/ 77 | 78 | // Track event proposals: additions and subtractions. 79 | std::vector _TrackingVector; 80 | 81 | void writeTransitionProbsToFile(); 82 | void writePriorProbsToFile_Experimental(); 83 | void writePriorProbsToFile_OldWay(); 84 | 85 | int _maxEvents; 86 | int _intervalGens; 87 | 88 | // End new params 89 | 90 | 91 | }; 92 | 93 | 94 | inline int FastSimulatePrior::round(double x) 95 | { 96 | return std::ceil(x - 0.5); 97 | } 98 | 99 | 100 | inline void FastSimulatePrior::setEventRate(double x) 101 | { 102 | _eventRate = x; 103 | } 104 | 105 | inline void FastSimulatePrior::incrementGeneration() 106 | { 107 | _generations++; 108 | } 109 | 110 | inline int FastSimulatePrior::getNumberOfEvents() 111 | { 112 | return _numberEvents; 113 | } 114 | 115 | 116 | inline double FastSimulatePrior::getEventRate() 117 | { 118 | return _eventRate; 119 | } 120 | 121 | 122 | 123 | #endif /* defined(__bamm__FastSimulatePrior__) */ 124 | -------------------------------------------------------------------------------- /src/LambdaInitProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "LambdaInitProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | #include "SpExBranchEvent.h" 8 | 9 | 10 | LambdaInitProposal::LambdaInitProposal 11 | (Random& random, Settings& settings, Model& model, Prior& prior) : 12 | EventParameterProposal(random, settings, model, prior) 13 | { 14 | _weight = _settings.get("updateRateLambda0"); 15 | _updateLambdaInitScale = _settings.get("updateLambdaInitScale"); 16 | } 17 | 18 | 19 | double LambdaInitProposal::getCurrentParameterValue() 20 | { 21 | return static_cast(_event)->getLamInit(); 22 | } 23 | 24 | 25 | double LambdaInitProposal::computeNewParameterValue() 26 | { 27 | _cterm = std::exp(_updateLambdaInitScale * (_random.uniform() - 0.5)); 28 | return _cterm * _currentParameterValue; 29 | } 30 | 31 | 32 | void LambdaInitProposal::setProposedParameterValue() 33 | { 34 | static_cast(_event)->setLamInit(_proposedParameterValue); 35 | } 36 | 37 | 38 | void LambdaInitProposal::revertToOldParameterValue() 39 | { 40 | static_cast(_event)->setLamInit(_currentParameterValue); 41 | } 42 | 43 | 44 | void LambdaInitProposal::updateParameterOnTree() 45 | { 46 | _tree->setNodeSpeciationParameters(); 47 | _tree->setNodeExtinctionParameters(); 48 | } 49 | 50 | 51 | double LambdaInitProposal::computeRootLogPriorRatio() 52 | { 53 | return _prior.lambdaInitRootPrior(_proposedParameterValue) - 54 | _prior.lambdaInitRootPrior(_currentParameterValue); 55 | } 56 | 57 | 58 | double LambdaInitProposal::computeNonRootLogPriorRatio() 59 | { 60 | return _prior.lambdaInitPrior(_proposedParameterValue) - 61 | _prior.lambdaInitPrior(_currentParameterValue); 62 | } 63 | 64 | 65 | double LambdaInitProposal::computeLogQRatio() 66 | { 67 | return std::log(_cterm); 68 | } 69 | -------------------------------------------------------------------------------- /src/LambdaInitProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef LAMBDA_INIT_PROPOSAL_H 2 | #define LAMBDA_INIT_PROPOSAL_H 3 | 4 | 5 | #include "EventParameterProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class LambdaInitProposal : public EventParameterProposal 14 | { 15 | public: 16 | 17 | LambdaInitProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | private: 21 | 22 | virtual double getCurrentParameterValue(); 23 | virtual double computeNewParameterValue(); 24 | 25 | virtual void setProposedParameterValue(); 26 | virtual void revertToOldParameterValue(); 27 | 28 | virtual void updateParameterOnTree(); 29 | 30 | virtual double computeRootLogPriorRatio(); 31 | virtual double computeNonRootLogPriorRatio(); 32 | virtual double computeLogQRatio(); 33 | 34 | double _updateLambdaInitScale; 35 | double _cterm; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/LambdaShiftProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "LambdaShiftProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | #include "SpExBranchEvent.h" 8 | 9 | 10 | LambdaShiftProposal::LambdaShiftProposal 11 | (Random& random, Settings& settings, Model& model, Prior& prior) : 12 | EventParameterProposal(random, settings, model, prior) 13 | { 14 | _weight = _settings.get("updateRateLambdaShift"); 15 | _updateLambdaShiftScale = _settings.get("updateLambdaShiftScale"); 16 | } 17 | 18 | 19 | double LambdaShiftProposal::acceptanceRatio() 20 | { 21 | if (static_cast(_event)->isTimeVariable()) { 22 | return EventParameterProposal::acceptanceRatio(); 23 | } else { 24 | return 0.0; 25 | } 26 | } 27 | 28 | 29 | double LambdaShiftProposal::getCurrentParameterValue() 30 | { 31 | return static_cast(_event)->getLamShift(); 32 | } 33 | 34 | 35 | double LambdaShiftProposal::computeNewParameterValue() 36 | { 37 | return _currentParameterValue + 38 | _random.normal(0.0, _updateLambdaShiftScale); 39 | } 40 | 41 | 42 | void LambdaShiftProposal::setProposedParameterValue() 43 | { 44 | static_cast(_event)->setLamShift(_proposedParameterValue); 45 | } 46 | 47 | 48 | void LambdaShiftProposal::revertToOldParameterValue() 49 | { 50 | static_cast(_event)->setLamShift(_currentParameterValue); 51 | } 52 | 53 | 54 | void LambdaShiftProposal::updateParameterOnTree() 55 | { 56 | _tree->setNodeSpeciationParameters(); 57 | _tree->setNodeExtinctionParameters(); 58 | } 59 | 60 | 61 | double LambdaShiftProposal::computeRootLogPriorRatio() 62 | { 63 | return _prior.lambdaShiftRootPrior(_proposedParameterValue) - 64 | _prior.lambdaShiftRootPrior(_currentParameterValue); 65 | } 66 | 67 | 68 | double LambdaShiftProposal::computeNonRootLogPriorRatio() 69 | { 70 | return _prior.lambdaShiftPrior(_proposedParameterValue) - 71 | _prior.lambdaShiftPrior(_currentParameterValue); 72 | } 73 | -------------------------------------------------------------------------------- /src/LambdaShiftProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef LAMBDA_SHIFT_PROPOSAL_H 2 | #define LAMBDA_SHIFT_PROPOSAL_H 3 | 4 | 5 | #include "EventParameterProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class LambdaShiftProposal : public EventParameterProposal 14 | { 15 | public: 16 | 17 | LambdaShiftProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | virtual double acceptanceRatio(); 21 | 22 | private: 23 | 24 | virtual double getCurrentParameterValue(); 25 | virtual double computeNewParameterValue(); 26 | 27 | virtual void setProposedParameterValue(); 28 | virtual void revertToOldParameterValue(); 29 | 30 | virtual void updateParameterOnTree(); 31 | 32 | virtual double computeRootLogPriorRatio(); 33 | virtual double computeNonRootLogPriorRatio(); 34 | 35 | double _updateLambdaShiftScale; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/LambdaTimeModeProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "LambdaTimeModeProposal.h" 2 | 3 | #include "Settings.h" 4 | #include "Prior.h" 5 | #include "SpExBranchEvent.h" 6 | 7 | class Random; 8 | class Model; 9 | 10 | 11 | LambdaTimeModeProposal::LambdaTimeModeProposal 12 | (Random& random, Settings& settings, Model& model) 13 | : TimeModeProposal(random, settings, model) 14 | { 15 | _weight = settings.get("updateRateLambdaTimeMode"); 16 | } 17 | 18 | 19 | double LambdaTimeModeProposal::initialParameter(BranchEvent* event) 20 | { 21 | return static_cast(event)->getLamInit(); 22 | } 23 | 24 | 25 | double LambdaTimeModeProposal::rateParameter(BranchEvent* event) 26 | { 27 | return static_cast(event)->getLamShift(); 28 | } 29 | 30 | 31 | bool LambdaTimeModeProposal::isTimeVariable(BranchEvent* event) 32 | { 33 | return static_cast(event)->isTimeVariable(); 34 | } 35 | 36 | 37 | void LambdaTimeModeProposal::setEventParameters(BranchEvent* event, 38 | double initParam, double rateParam, bool isTimeVariable) 39 | { 40 | SpExBranchEvent* spExEvent = static_cast(event); 41 | 42 | spExEvent->setLamInit(initParam); 43 | spExEvent->setLamShift(rateParam); 44 | spExEvent->setTimeVariable(isTimeVariable); 45 | } 46 | 47 | 48 | void LambdaTimeModeProposal::setModelParameters() 49 | { 50 | _tree->setNodeSpeciationParameters(); 51 | _tree->setNodeExtinctionParameters(); 52 | } 53 | 54 | 55 | double LambdaTimeModeProposal::rateParameterFromPrior() 56 | { 57 | return _prior.generateLambdaShiftFromPrior(); 58 | } 59 | -------------------------------------------------------------------------------- /src/LambdaTimeModeProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef LAMBDA_TIME_MODE_PROPOSAL_H 2 | #define LAMBDA_TIME_MODE_PROPOSAL_H 3 | 4 | 5 | #include "TimeModeProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | 11 | 12 | class LambdaTimeModeProposal : public TimeModeProposal 13 | { 14 | public: 15 | 16 | LambdaTimeModeProposal(Random& random, Settings& settings, Model& model); 17 | 18 | protected: 19 | 20 | double initialParameter(BranchEvent* event); 21 | double rateParameter(BranchEvent* event); 22 | bool isTimeVariable(BranchEvent* event); 23 | 24 | void setEventParameters(BranchEvent* event, 25 | double initParam, double rateParam, bool isTimeVariable); 26 | 27 | void setModelParameters(); 28 | 29 | double rateParameterFromPrior(); 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/Log.cpp: -------------------------------------------------------------------------------- 1 | #include "Log.h" 2 | 3 | #include 4 | #include 5 | 6 | 7 | Log Log::_logger; 8 | 9 | 10 | Log& Log::instance() 11 | { 12 | return _logger; 13 | } 14 | 15 | 16 | std::ostream& Log::outputStream(LogType logType, std::ostream& out) 17 | { 18 | if (logType == Message) { 19 | // Do nothing 20 | } else if (logType == Warning) { 21 | startWarning(out); 22 | } else if (logType == Error) { 23 | startError(out); 24 | } 25 | 26 | return out; 27 | } 28 | 29 | 30 | void Log::startWarning(std::ostream& out) 31 | { 32 | out << TEXT_COLOR_WARNING << "\nWARNING: " << TEXT_COLOR_DEFAULT; 33 | } 34 | 35 | 36 | void Log::startError(std::ostream& out) 37 | { 38 | out << TEXT_COLOR_ERROR << "\nERROR: " << TEXT_COLOR_DEFAULT; 39 | } 40 | 41 | 42 | std::ostream& log(LogType logType) 43 | { 44 | std::ostream* out; 45 | 46 | if (logType == Message) { 47 | out = &std::cout; 48 | } else if (logType == Warning) { 49 | out = &std::cerr; 50 | } else if (logType == Error) { 51 | out = &std::cerr; 52 | } else { // Should never happen 53 | out = &std::cout; 54 | } 55 | 56 | return log(logType, *out); 57 | } 58 | 59 | 60 | std::ostream& log(std::ostream& out) 61 | { 62 | return log(Message, out); 63 | } 64 | 65 | 66 | std::ostream& log(LogType logType, std::ostream& out) 67 | { 68 | return Log::instance().outputStream(logType, out); 69 | } 70 | 71 | 72 | void exitWithMessage(const std::string& message) 73 | { 74 | log() << message << std::endl; 75 | std::exit(0); 76 | } 77 | 78 | 79 | void exitWithError(const std::string& message) 80 | { 81 | log(Error) << message << std::endl; 82 | std::exit(1); 83 | } 84 | -------------------------------------------------------------------------------- /src/Log.h: -------------------------------------------------------------------------------- 1 | #ifndef LOG_H 2 | #define LOG_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | #define TEXT_COLOR_DEFAULT "\x1b[39m" // Default color (gray) 9 | #define TEXT_COLOR_MESSAGE "\x1b[39m" // Default color (gray) 10 | #define TEXT_COLOR_WARNING "\x1b[33m" // Yellow 11 | #define TEXT_COLOR_ERROR "\x1b[31m" // Red 12 | 13 | 14 | enum LogType 15 | { 16 | Message, 17 | Warning, 18 | Error 19 | }; 20 | 21 | 22 | class Log 23 | { 24 | 25 | public: 26 | 27 | static Log& instance(); 28 | 29 | std::ostream& outputStream(LogType logType, std::ostream& out); 30 | 31 | private: 32 | 33 | Log() {}; 34 | 35 | void startMessage(std::ostream& out); 36 | void startWarning(std::ostream& out); 37 | void startError(std::ostream& out); 38 | 39 | static Log _logger; 40 | 41 | }; 42 | 43 | 44 | std::ostream& log(LogType logType = Message); 45 | std::ostream& log(std::ostream& out); 46 | std::ostream& log(LogType logType, std::ostream& out); 47 | 48 | void exitWithMessage(const std::string& message); 49 | void exitWithError(const std::string& message); 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/MCMC.cpp: -------------------------------------------------------------------------------- 1 | #include "MCMC.h" 2 | #include "Random.h" 3 | #include "Model.h" 4 | #include "ModelFactory.h" 5 | 6 | #include 7 | 8 | 9 | // Choose a random number up to INT_MAX - 1, not INT_MAX, 10 | // because MbRandom adds 1 internally, causing an overflow 11 | MCMC::MCMC(Random& seeder, Settings& settings, ModelFactory& modelFactory) : 12 | _random(seeder.uniformInteger(0, INT_MAX - 1)) 13 | { 14 | _model = modelFactory.createModel(_random, settings); 15 | 16 | } 17 | 18 | 19 | MCMC::~MCMC() 20 | { 21 | delete _model; 22 | } 23 | 24 | 25 | void MCMC::run(int generations) 26 | { 27 | for (int g = 0; g < generations; g++) { 28 | step(); 29 | } 30 | } 31 | 32 | 33 | void MCMC::step() 34 | { 35 | //std::cout << _model->getCurrentLogLikelihood() << "\tActual: " << _model->computeLogLikelihood() << std::endl; 36 | 37 | //double logL = _model->computeLogLikelihood(); 38 | 39 | _model->proposeNewState(); 40 | 41 | double acceptanceRatio = _model->acceptanceRatio(); 42 | if (_random.trueWithProbability(acceptanceRatio)) { 43 | _model->acceptProposal(); 44 | } else { 45 | _model->rejectProposal(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/MCMC.h: -------------------------------------------------------------------------------- 1 | #ifndef MCMC_H 2 | #define MCMC_H 3 | 4 | 5 | #include "Random.h" 6 | 7 | class Settings; 8 | class Model; 9 | class ModelFactory; 10 | 11 | 12 | class MCMC 13 | { 14 | public: 15 | 16 | MCMC(Random& seeder, Settings& settings, ModelFactory& modelFactory); 17 | ~MCMC(); 18 | 19 | void run(int generations); 20 | void step(); 21 | 22 | Model& model(); 23 | 24 | protected: 25 | 26 | // MCMC has its own random generator, using the seeder 27 | // (another random generator) to seed it 28 | Random _random; 29 | Model* _model; 30 | }; 31 | 32 | 33 | inline Model& MCMC::model() 34 | { 35 | return *_model; 36 | } 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/MCMCDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "MCMCDataWriter.h" 2 | #include "Settings.h" 3 | #include "Model.h" 4 | 5 | // TODO: make abstract class for MCMC datawriter with derived classes 6 | // than can handle fossils 7 | // Then no more need to declare SpExModel.h here. 8 | 9 | #include "SpExModel.h" 10 | 11 | #include 12 | 13 | 14 | MCMCDataWriter::MCMCDataWriter(Settings& settings) : 15 | _outputFileName(settings.get("mcmcOutfile")), 16 | _outputFreq(settings.get("mcmcWriteFreq")) 17 | { 18 | 19 | if (settings.get("updateRatePreservationRate") >= 0.0){ 20 | _hasPreservationRate = true; 21 | }else{ 22 | _hasPreservationRate = false; 23 | } 24 | 25 | 26 | if (_outputFreq > 0) { 27 | initializeStream(); 28 | writeHeader(); 29 | } 30 | 31 | 32 | } 33 | 34 | 35 | void MCMCDataWriter::initializeStream() 36 | { 37 | _outputStream.open(_outputFileName.c_str()); 38 | } 39 | 40 | 41 | void MCMCDataWriter::writeHeader() 42 | { 43 | _outputStream << header() << std::endl; 44 | } 45 | 46 | 47 | std::string MCMCDataWriter::header() 48 | { 49 | if (_hasPreservationRate){ 50 | return "generation,N_shifts,logPrior,logLik,eventRate,preservationRate,acceptRate"; 51 | }else{ 52 | return "generation,N_shifts,logPrior,logLik,eventRate,acceptRate"; 53 | } 54 | } 55 | 56 | 57 | MCMCDataWriter::~MCMCDataWriter() 58 | { 59 | if (_outputFreq > 0) { 60 | _outputStream.close(); 61 | } 62 | } 63 | 64 | 65 | void MCMCDataWriter::writeData(int generation, Model& model) 66 | { 67 | if (_outputFreq == 0 || generation % _outputFreq != 0) { 68 | return; 69 | } 70 | 71 | if (_hasPreservationRate == false){ 72 | _outputStream << generation << "," 73 | << model.getNumberOfEvents() << "," 74 | << model.computeLogPrior() << "," 75 | << model.getCurrentLogLikelihood() << "," 76 | << model.getEventRate() << "," 77 | << model.getMHAcceptanceRate() << std::endl; 78 | }else{ 79 | 80 | double prate = static_cast(&model)->getPreservationRate(); 81 | 82 | _outputStream << generation << "," 83 | << model.getNumberOfEvents() << "," 84 | << model.computeLogPrior() << "," 85 | << model.getCurrentLogLikelihood() << "," 86 | << model.getEventRate() << "," 87 | << prate << "," 88 | << model.getMHAcceptanceRate() << std::endl; 89 | 90 | } 91 | 92 | 93 | } 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /src/MCMCDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef MCMC_DATA_WRITER_H 2 | #define MCMC_DATA_WRITER_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | class Settings; 9 | class Model; 10 | 11 | 12 | class MCMCDataWriter 13 | { 14 | public: 15 | 16 | MCMCDataWriter(Settings& settings); 17 | ~MCMCDataWriter(); 18 | 19 | void writeData(int generation, Model& model); 20 | 21 | private: 22 | 23 | void initializeStream(); 24 | void writeHeader(); 25 | std::string header(); 26 | 27 | std::string _outputFileName; 28 | int _outputFreq; 29 | 30 | std::ofstream _outputStream; 31 | 32 | bool _hasPreservationRate; 33 | 34 | }; 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /src/MatchPathSeparator.h: -------------------------------------------------------------------------------- 1 | #ifndef MATCH_PATH_SEPARATOR 2 | #define MATCH_PATH_SEPARATOR 3 | 4 | 5 | // Used with STL algorithms to find path separators 6 | struct MatchPathSeparator 7 | { 8 | bool operator()(char c) const 9 | { 10 | // Match both Unix-style and Windows-style path separators 11 | return c == '/' || c == '\\'; 12 | } 13 | }; 14 | 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/MetropolisCoupledMCMC.h: -------------------------------------------------------------------------------- 1 | #ifndef METROPOLIS_COUPLED_MCMC_H 2 | #define METROPOLIS_COUPLED_MCMC_H 3 | 4 | 5 | #include "ChainSwapDataWriter.h" 6 | #include 7 | 8 | class Random; 9 | class Settings; 10 | class ModelFactory; 11 | class MCMC; 12 | class Model; 13 | class ModelDataWriter; 14 | 15 | 16 | class MetropolisCoupledMCMC 17 | { 18 | public: 19 | 20 | MetropolisCoupledMCMC 21 | (Random& random, Settings& settings, ModelFactory* modelFactory); 22 | ~MetropolisCoupledMCMC(); 23 | 24 | void run(); 25 | 26 | private: 27 | 28 | void createChains(); 29 | MCMC* createMCMC(int chainIndex) const; 30 | double calculateTemperature(int i, double deltaT) const; 31 | 32 | void createDataWriter(); 33 | 34 | void runChains(int genStart, int genEnd); 35 | void runChain(int i, int genStart, int genEnd); 36 | void tryChainSwap(int generation); 37 | 38 | void chooseTwoNumbers(int* x, int* y, int from, int to); 39 | bool acceptChainSwap(int chain_1, int chain_2) const; 40 | bool trueWithProbability(double p) const; 41 | double chainSwapProbability(int chain_1, int chain_2) const; 42 | double calculateLogPosterior(Model& model) const; 43 | double logSwapPosteriorRatio(double beta_1, double beta_2, 44 | double log_post_1, double log_post_2) const; 45 | void swapTemperature(int chain_1, int chain_2); 46 | 47 | Random& _random; 48 | Settings& _settings; 49 | ModelFactory* _modelFactory; 50 | 51 | int _nGenerations; 52 | 53 | // Holds a variable number of Markov chains 54 | std::vector _chains; 55 | int _nChains; 56 | 57 | // From Altekar, et al. 2004: delta T (> 1) is a temparature 58 | // increment parameter chosen such that swaps are accepted 59 | // between 20 and 60% of the time. 60 | double _deltaT; 61 | 62 | // Number of steps/generations in which chair swapping occurs 63 | int _swapPeriod; 64 | 65 | // Current index of the cold chain (it changes when a swap occurs) 66 | int _coldChainIndex; 67 | 68 | ChainSwapDataWriter _chainSwapDataWriter; 69 | 70 | ModelDataWriter* _dataWriter; 71 | 72 | int _acceptanceResetFreq; 73 | }; 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/ModelDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "ModelDataWriter.h" 2 | 3 | #include "Settings.h" 4 | #include "StdOutDataWriter.h" 5 | #include "MCMCDataWriter.h" 6 | #include "AcceptanceDataWriter.h" 7 | 8 | class Model; 9 | 10 | 11 | ModelDataWriter::ModelDataWriter(Settings &settings) : 12 | _settings(settings), _stdOutDataWriter(_settings), 13 | _mcmcDataWriter(_settings), _acceptanceDataWriter(_settings) 14 | { 15 | } 16 | 17 | 18 | ModelDataWriter::~ModelDataWriter() 19 | { 20 | } 21 | 22 | 23 | void ModelDataWriter::writeData(int generation, Model& model) 24 | { 25 | _stdOutDataWriter.writeData(generation, model); 26 | _mcmcDataWriter.writeData(generation, model); 27 | _acceptanceDataWriter.writeData(model); 28 | } 29 | -------------------------------------------------------------------------------- /src/ModelDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef MODEL_DATA_WRITER_H 2 | #define MODEL_DATA_WRITER_H 3 | 4 | 5 | #include "StdOutDataWriter.h" 6 | #include "MCMCDataWriter.h" 7 | #include "AcceptanceDataWriter.h" 8 | 9 | class Settings; 10 | class Model; 11 | 12 | 13 | class ModelDataWriter 14 | { 15 | public: 16 | 17 | ModelDataWriter(Settings &settings); 18 | virtual ~ModelDataWriter(); 19 | 20 | virtual void writeData(int generation, Model& model); 21 | 22 | protected: 23 | 24 | Settings &_settings; 25 | 26 | StdOutDataWriter _stdOutDataWriter; 27 | MCMCDataWriter _mcmcDataWriter; 28 | AcceptanceDataWriter _acceptanceDataWriter; 29 | }; 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/ModelFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef MODEL_FACTORY 2 | #define MODEL_FACTORY 3 | 4 | 5 | class Model; 6 | class EventDataWriter; 7 | class ModelDataWriter; 8 | 9 | class Random; 10 | class Settings; 11 | class Prior; 12 | 13 | 14 | class ModelFactory 15 | { 16 | public: 17 | 18 | virtual ~ModelFactory() {} 19 | 20 | virtual Model* createModel(Random& random, Settings& settings) const = 0; 21 | virtual ModelDataWriter* createModelDataWriter 22 | (Settings& settings) const = 0; 23 | }; 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/MoveEventProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef MOVE_EVENT_PROPOSAL_H 2 | #define MOVE_EVENT_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class BranchEvent; 11 | 12 | 13 | class MoveEventProposal : public Proposal 14 | { 15 | public: 16 | 17 | MoveEventProposal(Random& random, Settings& settings, Model& model); 18 | 19 | virtual void propose(); 20 | virtual void accept(); 21 | virtual void reject(); 22 | 23 | virtual double acceptanceRatio(); 24 | 25 | private: 26 | 27 | virtual double computeLogLikelihoodRatio(); 28 | 29 | Random& _random; 30 | Settings& _settings; 31 | Model& _model; 32 | 33 | double _localToGlobalMoveRatio; 34 | double _scale; 35 | 36 | bool _validateEventConfiguration; 37 | 38 | BranchEvent* _event; 39 | 40 | int _currentEventCount; 41 | double _currentLogLikelihood; 42 | double _proposedLogLikelihood; 43 | }; 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/MuInitProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "MuInitProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | #include "SpExBranchEvent.h" 8 | 9 | 10 | MuInitProposal::MuInitProposal 11 | (Random& random, Settings& settings, Model& model, Prior& prior) : 12 | EventParameterProposal(random, settings, model, prior) 13 | { 14 | _weight = _settings.get("updateRateMu0"); 15 | _updateMuInitScale = _settings.get("updateMuInitScale"); 16 | } 17 | 18 | 19 | double MuInitProposal::getCurrentParameterValue() 20 | { 21 | return static_cast(_event)->getMuInit(); 22 | } 23 | 24 | 25 | double MuInitProposal::computeNewParameterValue() 26 | { 27 | _cterm = std::exp(_updateMuInitScale * (_random.uniform() - 0.5)); 28 | return _cterm * _currentParameterValue; 29 | } 30 | 31 | 32 | void MuInitProposal::setProposedParameterValue() 33 | { 34 | static_cast(_event)->setMuInit(_proposedParameterValue); 35 | } 36 | 37 | 38 | void MuInitProposal::revertToOldParameterValue() 39 | { 40 | static_cast(_event)->setMuInit(_currentParameterValue); 41 | } 42 | 43 | 44 | void MuInitProposal::updateParameterOnTree() 45 | { 46 | _tree->setNodeSpeciationParameters(); 47 | _tree->setNodeExtinctionParameters(); 48 | } 49 | 50 | 51 | double MuInitProposal::computeRootLogPriorRatio() 52 | { 53 | return _prior.muInitRootPrior(_proposedParameterValue) - 54 | _prior.muInitRootPrior(_currentParameterValue); 55 | } 56 | 57 | 58 | double MuInitProposal::computeNonRootLogPriorRatio() 59 | { 60 | return _prior.muInitPrior(_proposedParameterValue) - 61 | _prior.muInitPrior(_currentParameterValue); 62 | } 63 | 64 | 65 | double MuInitProposal::computeLogQRatio() 66 | { 67 | return std::log(_cterm); 68 | } 69 | -------------------------------------------------------------------------------- /src/MuInitProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_INIT_PROPOSAL_H 2 | #define MU_INIT_PROPOSAL_H 3 | 4 | 5 | #include "EventParameterProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class MuInitProposal : public EventParameterProposal 14 | { 15 | public: 16 | 17 | MuInitProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | private: 21 | 22 | virtual double getCurrentParameterValue(); 23 | virtual double computeNewParameterValue(); 24 | 25 | virtual void setProposedParameterValue(); 26 | virtual void revertToOldParameterValue(); 27 | 28 | virtual void updateParameterOnTree(); 29 | 30 | virtual double computeRootLogPriorRatio(); 31 | virtual double computeNonRootLogPriorRatio(); 32 | virtual double computeLogQRatio(); 33 | 34 | double _updateMuInitScale; 35 | double _cterm; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/MuShiftProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "MuShiftProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "Tree.h" 7 | #include "SpExBranchEvent.h" 8 | 9 | 10 | MuShiftProposal::MuShiftProposal 11 | (Random& random, Settings& settings, Model& model, Prior& prior) : 12 | EventParameterProposal(random, settings, model, prior) 13 | { 14 | _weight = _settings.get("updateRateMuShift"); 15 | _updateMuShiftScale = _settings.get("updateMuShiftScale"); 16 | } 17 | 18 | 19 | double MuShiftProposal::getCurrentParameterValue() 20 | { 21 | return static_cast(_event)->getMuShift(); 22 | } 23 | 24 | 25 | double MuShiftProposal::computeNewParameterValue() 26 | { 27 | return _currentParameterValue + _random.normal(0.0, _updateMuShiftScale); 28 | } 29 | 30 | 31 | void MuShiftProposal::setProposedParameterValue() 32 | { 33 | static_cast(_event)->setMuShift(_proposedParameterValue); 34 | } 35 | 36 | 37 | void MuShiftProposal::revertToOldParameterValue() 38 | { 39 | static_cast(_event)->setMuShift(_currentParameterValue); 40 | } 41 | 42 | 43 | void MuShiftProposal::updateParameterOnTree() 44 | { 45 | _tree->setNodeSpeciationParameters(); 46 | _tree->setNodeExtinctionParameters(); 47 | } 48 | 49 | 50 | double MuShiftProposal::computeRootLogPriorRatio() 51 | { 52 | return _prior.muShiftRootPrior(_proposedParameterValue) - 53 | _prior.muShiftRootPrior(_currentParameterValue); 54 | } 55 | 56 | 57 | double MuShiftProposal::computeNonRootLogPriorRatio() 58 | { 59 | return _prior.muShiftPrior(_proposedParameterValue) - 60 | _prior.muShiftPrior(_currentParameterValue); 61 | } 62 | -------------------------------------------------------------------------------- /src/MuShiftProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_SHIFT_PROPOSAL_H 2 | #define MU_SHIFT_PROPOSAL_H 3 | 4 | 5 | #include "EventParameterProposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class Prior; 11 | 12 | 13 | class MuShiftProposal : public EventParameterProposal 14 | { 15 | public: 16 | 17 | MuShiftProposal(Random& random, Settings& settings, Model& model, 18 | Prior& prior); 19 | 20 | private: 21 | 22 | virtual double getCurrentParameterValue(); 23 | virtual double computeNewParameterValue(); 24 | 25 | virtual void setProposedParameterValue(); 26 | virtual void revertToOldParameterValue(); 27 | 28 | virtual void updateParameterOnTree(); 29 | 30 | virtual double computeRootLogPriorRatio(); 31 | virtual double computeNonRootLogPriorRatio(); 32 | 33 | double _updateMuShiftScale; 34 | }; 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /src/NewickTreeReader.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWICK_TREE_READER_H 2 | #define NEWICK_TREE_READER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "TreeReader.h" 8 | 9 | class Tree; 10 | class Node; 11 | 12 | class NewickTreeReader : public TreeReader 13 | { 14 | public: 15 | 16 | virtual void read(std::istream &in, Tree &tree) const; 17 | 18 | private: 19 | 20 | Node *readNode(std::istream &in) const; 21 | 22 | bool characterStartsBranchNode(char c) const; 23 | bool characterEndsBranchNode(char c) const; 24 | bool characterStartsNextChildNode(char c) const; 25 | bool characterStartsBranchLength(char c) const; 26 | bool characterIsNormal(char c) const; 27 | bool characterIsWhitespace(char c) const; 28 | 29 | char peekNextCharacter(std::istream &in) const; 30 | char readNextCharacter(std::istream &in) const; 31 | void ignoreNextCharacter(std::istream &in) const; 32 | bool endOfInput(std::istream &in) const; 33 | 34 | std::string readName(std::istream &in) const; 35 | double readBranchLength(std::istream &in) const; 36 | 37 | std::string readString(std::istream &in) const; 38 | double readNumber(std::istream &in) const; 39 | 40 | static const std::string _specialCharacters; 41 | static const std::string _whitespaceCharacters; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/NodeStateDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "NodeStateDataWriter.h" 2 | #include "Settings.h" 3 | #include "TraitModel.h" 4 | #include "Tree.h" 5 | 6 | #include 7 | 8 | 9 | NodeStateDataWriter::NodeStateDataWriter(Settings& settings) : 10 | _outputFileName(settings.get("nodeStateOutfile")), 11 | _outputFreq(settings.get("nodeStateWriteFreq")) 12 | { 13 | if (_outputFreq > 0) { 14 | initializeStream(); 15 | } 16 | } 17 | 18 | 19 | void NodeStateDataWriter::initializeStream() 20 | { 21 | _outputStream.open(_outputFileName.c_str()); 22 | } 23 | 24 | 25 | NodeStateDataWriter::~NodeStateDataWriter() 26 | { 27 | if (_outputFreq > 0) { 28 | _outputStream.close(); 29 | } 30 | } 31 | 32 | 33 | void NodeStateDataWriter::writeData(int generation, TraitModel& model) 34 | { 35 | if (_outputFreq == 0 || generation % _outputFreq != 0) { 36 | return; 37 | } 38 | 39 | Tree& tree = *model.getTreePtr(); 40 | 41 | _outputStream << generation; 42 | tree.writeBranchPhenotypes(tree.getRoot(), _outputStream); 43 | _outputStream << ";\n"; 44 | } 45 | -------------------------------------------------------------------------------- /src/NodeStateDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef NODE_STATE_DATA_WRITER_H 2 | #define NODE_STATE_DATA_WRITER_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | class Settings; 9 | class TraitModel; 10 | 11 | 12 | class NodeStateDataWriter 13 | { 14 | public: 15 | 16 | NodeStateDataWriter(Settings& settings); 17 | ~NodeStateDataWriter(); 18 | 19 | void writeData(int generation, TraitModel& model); 20 | 21 | private: 22 | 23 | void initializeStream(); 24 | 25 | std::string _outputFileName; 26 | int _outputFreq; 27 | 28 | std::ofstream _outputStream; 29 | }; 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/NodeStateProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef NODE_STATE_PROPOSAL_H 2 | #define NODE_STATE_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | 7 | class Random; 8 | class Settings; 9 | class Model; 10 | class TraitModel; 11 | class Tree; 12 | class Node; 13 | 14 | 15 | class NodeStateProposal : public Proposal 16 | { 17 | public: 18 | 19 | NodeStateProposal(Random& random, Settings& settings, Model& model); 20 | 21 | virtual void propose(); 22 | virtual void accept(); 23 | virtual void reject(); 24 | 25 | virtual double acceptanceRatio(); 26 | 27 | private: 28 | 29 | void updateMinMaxTraitPriorSettings(); 30 | double computeLogLikelihoodRatio(); 31 | 32 | Random& _random; 33 | Settings& _settings; 34 | TraitModel& _model; 35 | 36 | Tree* _tree; 37 | Node* _node; 38 | 39 | double _updateNodeStateScale; 40 | double _priorMin; 41 | double _priorMax; 42 | 43 | bool _minMaxTraitPriorUpdated; 44 | 45 | double _currentNodeState; 46 | double _proposedNodeState; 47 | 48 | double _currentLogLikelihood; 49 | double _proposedLogLikelihood; 50 | }; 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/PreservationRateProposal.cpp: -------------------------------------------------------------------------------- 1 | #include "PreservationRateProposal.h" 2 | #include "Random.h" 3 | #include "Settings.h" 4 | #include "Model.h" 5 | #include "Prior.h" 6 | #include "SpExModel.h" 7 | #include 8 | 9 | PreservationRateProposal::PreservationRateProposal 10 | (Random& random, Settings& settings, Model& model, Prior& prior) : 11 | _random(random), _settings(settings), _model(model), _prior(prior) 12 | { 13 | _weight = settings.get("updateRatePreservationRate"); 14 | _updatePreservationRateScale = settings.get("updatePreservationRateScale"); 15 | 16 | } 17 | 18 | void PreservationRateProposal::propose() 19 | { 20 | _currentParameterValue = getCurrentParameterValue(); 21 | 22 | _currentLogLikelihood = _model.getCurrentLogLikelihood(); 23 | 24 | _cterm = std::exp(_updatePreservationRateScale * ( _random.uniform() - 0.5 )); 25 | _proposedParameterValue = _cterm * _currentParameterValue; 26 | 27 | setProposedParameterValue(); 28 | 29 | _proposedLogLikelihood = _model.computeLogLikelihood(); 30 | 31 | } 32 | 33 | double PreservationRateProposal::getCurrentParameterValue() 34 | { 35 | return static_cast(&_model)->getPreservationRate(); 36 | } 37 | 38 | void PreservationRateProposal::setProposedParameterValue() 39 | { 40 | static_cast(&_model)->setPreservationRate(_proposedParameterValue); 41 | } 42 | 43 | void PreservationRateProposal::revertToOldParameterValue() 44 | { 45 | static_cast(&_model)->setPreservationRate(_currentParameterValue); 46 | } 47 | 48 | 49 | void PreservationRateProposal::accept() 50 | { 51 | _model.setCurrentLogLikelihood(_proposedLogLikelihood); 52 | } 53 | 54 | 55 | void PreservationRateProposal::reject() 56 | { 57 | revertToOldParameterValue(); 58 | } 59 | 60 | 61 | double PreservationRateProposal::acceptanceRatio() 62 | { 63 | double logLikelihoodRatio = _proposedLogLikelihood - _currentLogLikelihood; 64 | double logPriorRatio = computeLogPriorRatio(); 65 | double logQratio = computeLogQRatio(); 66 | 67 | double t = _model.getTemperatureMH(); 68 | double logRatio = t * (logLikelihoodRatio + logPriorRatio) + logQratio; 69 | 70 | if (std::isfinite(logRatio)) { 71 | return std::min(1.0, std::exp(logRatio)); 72 | } else { 73 | return 0.0; 74 | } 75 | 76 | } 77 | 78 | double PreservationRateProposal::computeLogPriorRatio() 79 | { 80 | 81 | return _prior.preservationRatePrior(_proposedParameterValue) - 82 | _prior.preservationRatePrior(_currentParameterValue); 83 | 84 | } 85 | 86 | 87 | double PreservationRateProposal::computeLogQRatio() 88 | { 89 | return 0.0; 90 | } 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /src/PreservationRateProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef ____PreservationRateProposal__ 2 | 3 | 4 | 5 | #define ____PreservationRateProposal__ 6 | 7 | 8 | #include "Proposal.h" 9 | 10 | 11 | class Random; 12 | class Settings; 13 | class Model; 14 | class Prior; 15 | 16 | class PreservationRateProposal : public Proposal 17 | { 18 | 19 | public: 20 | 21 | PreservationRateProposal(Random& random, Settings& settings, 22 | Model& model, Prior& prior); 23 | 24 | virtual void propose(); 25 | virtual void accept(); 26 | virtual void reject(); 27 | 28 | virtual double acceptanceRatio(); 29 | 30 | 31 | private: 32 | 33 | double getCurrentParameterValue(); 34 | void setProposedParameterValue(); 35 | void revertToOldParameterValue(); 36 | 37 | double computeLogPriorRatio(); 38 | double computeLogQRatio(); 39 | 40 | Random& _random; 41 | Settings& _settings; 42 | Model& _model; 43 | Prior& _prior; 44 | 45 | 46 | double _currentParameterValue; 47 | double _proposedParameterValue; 48 | double _currentLogLikelihood; 49 | double _currentLogPrior; 50 | 51 | int _proposedEventCount; 52 | double _proposedLogLikelihood; 53 | double _proposedLogPrior; 54 | 55 | double _updatePreservationRateScale; 56 | double _cterm; 57 | 58 | }; 59 | 60 | 61 | #endif /* defined(____PreservationRateProposal__) */ 62 | -------------------------------------------------------------------------------- /src/Prior.h: -------------------------------------------------------------------------------- 1 | // Returns either: 2 | // Log-prior densities for parameter 3 | // A random deviate from the prior distribution for a parameter 4 | 5 | #ifndef PRIOR_H 6 | #define PRIOR_H 7 | 8 | #include 9 | 10 | class Settings; 11 | class Random; 12 | 13 | 14 | class Prior 15 | { 16 | public: 17 | 18 | Prior(Random& random, Settings* sp); 19 | ~Prior(); 20 | 21 | /* 22 | Every function to compute a prior density should have a 23 | counterpart function that generates a random variable from 24 | the same distribution. 25 | 26 | */ 27 | 28 | double lambdaShiftPrior(double); 29 | double generateLambdaShiftFromPrior(); 30 | 31 | double lambdaInitPrior(double); 32 | double generateLambdaInitFromPrior(); 33 | 34 | double muShiftPrior(double); 35 | double generateMuShiftFromPrior(); 36 | 37 | double muInitPrior(double); 38 | double generateMuInitFromPrior(); 39 | 40 | bool generateLambdaIsTimeVariableFromPrior(); 41 | double lambdaIsTimeVariablePrior(); 42 | 43 | double poissonRatePrior(double); 44 | double generatePoissonRateFromPrior(); 45 | 46 | double betaInitPrior(double); 47 | double generateBetaInitFromPrior(); 48 | 49 | double betaShiftPrior(double); 50 | double generateBetaShiftFromPrior(); 51 | 52 | bool generateBetaIsTimeVariableFromPrior(); 53 | double betaIsTimeVariablePrior(); 54 | 55 | double preservationRatePrior(double); 56 | 57 | 58 | // Root priors: 59 | 60 | double lambdaShiftRootPrior(double); 61 | double generateLambdaShiftRootPrior(); 62 | 63 | double lambdaInitRootPrior(double); 64 | double generateLambdaInitRootFromPrior(); 65 | 66 | double muShiftRootPrior(double); 67 | double generateMuShiftRootFromPrior(); 68 | 69 | double muInitRootPrior(double); 70 | double generateMuInitRootFromPrior(); 71 | 72 | double betaInitRootPrior(double); 73 | double generateBetaInitRootFromPrior(); 74 | 75 | double betaShiftRootPrior(double); 76 | double generateBetaShiftRootFromPrior(); 77 | 78 | 79 | private: 80 | 81 | Random& _random; 82 | 83 | // Initial parameters 84 | double _lambdaInit0; 85 | double _muInit0; 86 | double _betaInit; 87 | 88 | // Shift parameters 89 | double _lambdaShift0; 90 | double _muShift0; 91 | double _betaShiftInit; 92 | 93 | // Priors (non-root) for initial parameters 94 | double _lambdaInitPrior; 95 | double _muInitPrior; 96 | double _betaInitPrior; 97 | 98 | // Priors (non-root) for shift parameters 99 | double _lambdaShiftPrior; 100 | double _muShiftPrior; 101 | double _betaShiftPrior; 102 | 103 | // Root priors for initial parameters 104 | double _lambdaInitRootPrior; 105 | double _muInitRootPrior; 106 | double _betaInitRootPrior; 107 | 108 | // Root priors for shift parameters 109 | double _lambdaShiftRootPrior; 110 | double _muShiftRootPrior; 111 | double _betaShiftRootPrior; 112 | 113 | // Time variable/constant prior 114 | double _lambdaIsTimeVariablePrior; 115 | double _betaIsTimeVariablePrior; 116 | 117 | // Update rate for initial parameters 118 | double _updateRateLambda0; 119 | double _updateRateMu0; 120 | double _updateRateBeta0; 121 | 122 | // Update rate for shift parameters 123 | double _updateRateLambdaShift; 124 | double _updateRateMuShift; 125 | double _updateRateBetaShift; 126 | 127 | double _poissonRatePrior; 128 | 129 | double _preservationRatePrior; 130 | 131 | }; 132 | 133 | 134 | #endif 135 | -------------------------------------------------------------------------------- /src/Proposal.cpp: -------------------------------------------------------------------------------- 1 | #include "Proposal.h" 2 | 3 | 4 | Proposal::~Proposal() 5 | { 6 | } 7 | 8 | 9 | double Proposal::weight() const 10 | { 11 | return _weight; 12 | } 13 | -------------------------------------------------------------------------------- /src/Proposal.h: -------------------------------------------------------------------------------- 1 | #ifndef PROPOSAL_H 2 | #define PROPOSAL_H 3 | 4 | 5 | class Proposal 6 | { 7 | public: 8 | 9 | virtual ~Proposal(); 10 | 11 | virtual void propose() = 0; 12 | virtual void accept() = 0; 13 | virtual void reject() = 0; 14 | 15 | virtual double acceptanceRatio() = 0; 16 | 17 | double weight() const; 18 | 19 | protected: 20 | 21 | double _weight; 22 | }; 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/Random.cpp: -------------------------------------------------------------------------------- 1 | #include "Random.h" 2 | 3 | 4 | // For the default constructor, initialize the MbRandom object first 5 | // for it to generate a seed based on the clock, then assign it internally 6 | Random::Random() : _rng(), _seed(_rng.getSeed()) 7 | { 8 | warmUp(); 9 | } 10 | 11 | 12 | Random::Random(unsigned long int seed) : _rng((long int)seed), _seed(seed) 13 | { 14 | warmUp(); 15 | } 16 | 17 | 18 | void Random::warmUp() 19 | { 20 | // Random number generations often need to be "warmed up" 21 | // (i.e., first few numbers thrown away) 22 | for (int i = 0; i < 1000; i++) 23 | _rng.uniformRv(); 24 | } 25 | 26 | 27 | unsigned long int Random::getSeed() const 28 | { 29 | return _seed; 30 | } 31 | 32 | 33 | double Random::uniform() 34 | { 35 | return _rng.uniformRv(); 36 | } 37 | 38 | 39 | double Random::uniform(double a, double b) 40 | { 41 | return _rng.uniformRv(a, b); 42 | } 43 | 44 | 45 | // Returns a random number in [a, b] under a uniform distribution 46 | int Random::uniformInteger(int a, int b) 47 | { 48 | return _rng.discreteUniformRv(a, b); 49 | } 50 | 51 | 52 | double Random::normal(double mean, double sd) 53 | { 54 | return _rng.normalRv(mean, sd); 55 | } 56 | 57 | 58 | double Random::exponential(double rate) 59 | { 60 | return _rng.exponentialRv(rate); 61 | } 62 | 63 | 64 | bool Random::trueWithProbability(double p) 65 | { 66 | return uniform() < p; 67 | } 68 | -------------------------------------------------------------------------------- /src/Random.h: -------------------------------------------------------------------------------- 1 | #ifndef RANDOM_H 2 | #define RANDOM_H 3 | 4 | 5 | #include "MbRandom.h" 6 | 7 | 8 | class Random 9 | { 10 | public: 11 | 12 | Random(); 13 | Random(unsigned long int seed); 14 | 15 | void setSeed(unsigned long int seed); 16 | unsigned long int getSeed() const; 17 | 18 | double uniform(); 19 | double uniform(double a, double b); 20 | 21 | int uniformInteger(int a, int b); 22 | 23 | double normal(double mean, double sd); 24 | double exponential(double rate); 25 | 26 | bool trueWithProbability(double p); 27 | 28 | private: 29 | 30 | void warmUp(); 31 | 32 | // Mutable allows const methods to call methods in MbRandom 33 | mutable MbRandom _rng; 34 | 35 | unsigned long int _seed; 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/Settings.h: -------------------------------------------------------------------------------- 1 | #ifndef SETTINGS_H 2 | #define SETTINGS_H 3 | 4 | #include "SettingsParameter.h" 5 | #include "Log.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | typedef std::pair Parameter; 15 | typedef std::map ParameterMap; 16 | 17 | typedef std::pair UserParameter; 18 | 19 | 20 | class Settings 21 | { 22 | public: 23 | 24 | Settings(const std::string& controlFilename, 25 | const std::vector& commandLineParameters); 26 | 27 | std::string get(const std::string& name) const; 28 | template T get(const std::string& name) const; 29 | 30 | void set(const std::string& name, const std::string& value); 31 | 32 | void printCurrentSettings(std::ostream& out = std::cout) const; 33 | 34 | private: 35 | 36 | void readControlFile(const std::string& controlFilename); 37 | 38 | void initializeGlobalSettings(); 39 | void initializeSpeciationExtinctionSettings(); 40 | void initializeTraitSettings(); 41 | void initializeSettingsWithUserValues(); 42 | 43 | void checkAllSettingsAreUserDefined() const; 44 | void checkAllOutputFilesAreWriteable() const; 45 | 46 | void assertNotUserDefined(const SettingsParameter& parameter) const; 47 | void addParameter(const std::string& name, const std::string& value, 48 | UserDefinedStatus userDefined = Required, 49 | DeprecationStatus deprecated = NotDeprecated); 50 | 51 | void attachPrefixToOutputFiles(); 52 | std::string attachPrefix 53 | (const std::string& prefix, const std::string& str) const; 54 | std::string extractDir(const std::string& path) const; 55 | std::string extractFileName(const std::string& path) const; 56 | 57 | bool anyOutputFileExists() const; 58 | bool fileExists(const std::string& filename) const; 59 | 60 | void exitWithErrorNoControlFile() const; 61 | void exitWithErrorInvalidLine(const std::string& line) const; 62 | void exitWithErrorUndefinedParameter(const std::string& name) const; 63 | void exitWithErrorInvalidModelType() const; 64 | void exitWithErrorParametersNotFound 65 | (const std::vector& paramsNotFound) const; 66 | void exitWithErrorParameterIsDeprecated(const std::string& param) const; 67 | void exitWithErrorDuplicateParameter(const std::string& param) const; 68 | void exitWithErrorOutputFileExists() const; 69 | 70 | static const size_t NumberOfParamsToPrefix = 10; 71 | 72 | // Parameters that settings knows about 73 | ParameterMap _parameters; 74 | 75 | // Parameters read from the control file 76 | std::vector _userParameters; 77 | 78 | // Parameters read from the command line 79 | std::vector _commandLineParameters; 80 | 81 | // function to handle the validation of settings for 82 | // expanded oct 2015 options 83 | void validateSettings(void); 84 | 85 | 86 | }; 87 | 88 | 89 | inline std::string Settings::get(const std::string& name) const 90 | { 91 | return get(name); 92 | } 93 | 94 | 95 | template 96 | inline T Settings::get(const std::string& name) const 97 | { 98 | ParameterMap::const_iterator it = _parameters.find(name); 99 | if (it != _parameters.end()) { 100 | return (it->second).value(); 101 | } else { 102 | log(Error) << "Parameter <<" << name << ">> does not exist.\n"; 103 | std::exit(1); 104 | } 105 | } 106 | 107 | 108 | 109 | 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /src/SettingsParameter.cpp: -------------------------------------------------------------------------------- 1 | #include "SettingsParameter.h" 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | SettingsParameter::SettingsParameter(const std::string& name, 8 | const std::string& defaultValue, UserDefinedStatus userDefined, 9 | DeprecationStatus deprecated) : 10 | _name(name), _value(defaultValue), _userDefined(userDefined), 11 | _isUserDefined(false), _deprecated(deprecated) 12 | { 13 | } 14 | 15 | 16 | SettingsParameter::SettingsParameter(const SettingsParameter& other) : 17 | _name(other._name), _value(other._value), _userDefined(other._userDefined), 18 | _isUserDefined(other._isUserDefined), _deprecated(other._deprecated) 19 | { 20 | } 21 | 22 | 23 | SettingsParameter& SettingsParameter::operator=(const SettingsParameter &other) 24 | { 25 | _name = other._name; 26 | _value = other._value; 27 | _userDefined = other._userDefined; 28 | _isUserDefined = other._isUserDefined; 29 | _deprecated = other._deprecated; 30 | 31 | return *this; 32 | } 33 | 34 | 35 | void SettingsParameter::exitWithErrorWrongType() const 36 | { 37 | std::cout << "ERROR: Parameter " << _name << " has the wrong type.\n"; 38 | std::cout << "Fix by assigning the parameter a value of the right type.\n"; 39 | std::exit(1); 40 | } 41 | -------------------------------------------------------------------------------- /src/SettingsParameter.h: -------------------------------------------------------------------------------- 1 | #ifndef SETTINGS_PARAMETER_H 2 | #define SETTINGS_PARAMETER_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | 9 | enum UserDefinedStatus { 10 | Required, 11 | NotRequired 12 | }; 13 | 14 | 15 | enum DeprecationStatus { 16 | Deprecated, 17 | NotDeprecated 18 | }; 19 | 20 | 21 | class SettingsParameter 22 | { 23 | 24 | public: 25 | 26 | SettingsParameter(const std::string& name, const std::string& defaultValue, 27 | UserDefinedStatus userDefined = Required, 28 | DeprecationStatus deprecated = NotDeprecated); 29 | 30 | SettingsParameter(const SettingsParameter& other); 31 | SettingsParameter& operator=(const SettingsParameter& other); 32 | 33 | const std::string& name() const; 34 | // No setter: the parameter name should never be changed. 35 | 36 | template T value() const; 37 | template void setValue(const T& value); 38 | 39 | void setStringValue(const std::string& value); 40 | 41 | const std::string& version() const; 42 | // No setter: the parameter version should never be changed. 43 | 44 | // By default, parameters must be user-defined, but some parameters 45 | // are optional and should instead use their default value 46 | bool mustBeUserDefined() const; 47 | 48 | // The parameter becomes user-defined when the user sets its value 49 | // (even if it is the same as the default value). 50 | bool isUserDefined() const; 51 | 52 | bool isDeprecated() const; 53 | 54 | private: 55 | 56 | void exitWithErrorWrongType() const; 57 | 58 | std::string _name; 59 | std::string _value; 60 | 61 | UserDefinedStatus _userDefined; 62 | bool _isUserDefined; 63 | 64 | DeprecationStatus _deprecated; 65 | 66 | }; 67 | 68 | 69 | inline const std::string& SettingsParameter::name() const 70 | { 71 | return _name; 72 | } 73 | 74 | 75 | template 76 | T SettingsParameter::value() const 77 | { 78 | // Convert string value to the specified type 79 | std::istringstream valueStream(_value); 80 | T typedValue; 81 | valueStream >> typedValue; 82 | 83 | if (valueStream.fail()) 84 | exitWithErrorWrongType(); 85 | 86 | return typedValue; 87 | } 88 | 89 | 90 | template <> inline // Must be inline for linker to work 91 | std::string SettingsParameter::value<>() const 92 | { 93 | return _value; 94 | } 95 | 96 | 97 | template 98 | void SettingsParameter::setValue(const T& value) 99 | { 100 | // Convert value to a string 101 | std::ostringstream valueStream; 102 | valueStream << value; 103 | 104 | if (valueStream.fail()) 105 | exitWithErrorWrongType(); 106 | 107 | _value = valueStream.str(); 108 | _isUserDefined = true; 109 | } 110 | 111 | 112 | template <> inline // Must be inline for linker to work 113 | void SettingsParameter::setValue<>(const std::string& value) 114 | { 115 | _value = value; 116 | _isUserDefined = true; 117 | } 118 | 119 | 120 | inline void SettingsParameter::setStringValue(const std::string& value) 121 | { 122 | _value = value; 123 | _isUserDefined = true; 124 | } 125 | 126 | 127 | inline bool SettingsParameter::mustBeUserDefined() const 128 | { 129 | return _userDefined == Required; 130 | } 131 | 132 | 133 | inline bool SettingsParameter::isUserDefined() const 134 | { 135 | return _isUserDefined; 136 | } 137 | 138 | 139 | inline bool SettingsParameter::isDeprecated() const 140 | { 141 | return _deprecated == Deprecated; 142 | } 143 | 144 | 145 | #endif 146 | -------------------------------------------------------------------------------- /src/SpExBranchEvent.cpp: -------------------------------------------------------------------------------- 1 | #include "SpExBranchEvent.h" 2 | 3 | class Node; 4 | class Tree; 5 | class Random; 6 | 7 | 8 | SpExBranchEvent::SpExBranchEvent(double speciation, double lamshift, 9 | double extinction, double mushift, bool isTimeVariable, 10 | Node* x, Tree* tp, Random& random, double map) : 11 | BranchEvent(x, tp, random, map), _lamInit(speciation), _lamShift(lamshift), 12 | _muInit(extinction), _muShift(mushift), _isTimeVariable(isTimeVariable) 13 | { 14 | } 15 | -------------------------------------------------------------------------------- /src/SpExBranchEvent.h: -------------------------------------------------------------------------------- 1 | #ifndef SP_EX_BRANCH_EVENT_H 2 | #define SP_EX_BRANCH_EVENT_H 3 | 4 | #include "BranchEvent.h" 5 | 6 | class Tree; 7 | class Node; 8 | class Random; 9 | 10 | 11 | class SpExBranchEvent : public BranchEvent 12 | { 13 | 14 | private: 15 | 16 | double _lamInit; // Initial speciation rate at event 17 | double _lamShift; // magnitude & direction of speciation shift 18 | double _muInit; // Initial Mu rate at event 19 | double _muShift; // magnitude & direction of mu shift 20 | 21 | bool _isTimeVariable; 22 | 23 | public: 24 | 25 | // constructors, depending on whether you want trait rate or lambda/mu 26 | SpExBranchEvent(double speciation, double lamshift, double extinction, 27 | double mushift, bool isTimeVariable, Node* x, Tree* tp, Random& random, 28 | double map); 29 | virtual ~SpExBranchEvent() {}; 30 | 31 | void setLamInit(double x); 32 | double getLamInit(); 33 | 34 | void setMuInit(double x); 35 | double getMuInit(); 36 | 37 | void setLamShift(double x); 38 | double getLamShift(); 39 | 40 | void setMuShift(double x); 41 | double getMuShift(); 42 | 43 | void setTimeVariable(bool isTimeVariable); 44 | bool isTimeVariable(); 45 | }; 46 | 47 | 48 | inline void SpExBranchEvent::setLamInit(double x) 49 | { 50 | _lamInit = x; 51 | } 52 | 53 | 54 | inline double SpExBranchEvent::getLamInit() 55 | { 56 | return _lamInit; 57 | } 58 | 59 | 60 | inline void SpExBranchEvent::setMuInit(double x) 61 | { 62 | _muInit = x; 63 | } 64 | 65 | 66 | inline double SpExBranchEvent::getMuInit() 67 | { 68 | return _muInit; 69 | } 70 | 71 | 72 | inline void SpExBranchEvent::setLamShift(double x) 73 | { 74 | _lamShift = x; 75 | } 76 | 77 | 78 | inline double SpExBranchEvent::getLamShift() 79 | { 80 | return _lamShift; 81 | } 82 | 83 | 84 | inline void SpExBranchEvent::setMuShift(double x) 85 | { 86 | _muShift = x; 87 | } 88 | 89 | 90 | inline double SpExBranchEvent::getMuShift() 91 | { 92 | return _muShift; 93 | } 94 | 95 | 96 | inline void SpExBranchEvent::setTimeVariable(bool isTimeVariable) 97 | { 98 | _isTimeVariable = isTimeVariable; 99 | } 100 | 101 | 102 | inline bool SpExBranchEvent::isTimeVariable() 103 | { 104 | return _isTimeVariable; 105 | } 106 | 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /src/SpExDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "SpExDataWriter.h" 2 | #include "ModelDataWriter.h" 3 | 4 | class Settings; 5 | class Model; 6 | 7 | 8 | SpExDataWriter::SpExDataWriter(Settings &settings) : 9 | ModelDataWriter(settings), _eventDataWriter(settings) 10 | { 11 | } 12 | 13 | 14 | void SpExDataWriter::writeData(int generation, Model& model) 15 | { 16 | ModelDataWriter::writeData(generation, model); 17 | _eventDataWriter.writeData(generation, model); 18 | } 19 | -------------------------------------------------------------------------------- /src/SpExDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef SP_EX_DATA_WRITER_H 2 | #define SP_EX_DATA_WRITER_H 3 | 4 | 5 | #include "ModelDataWriter.h" 6 | #include "SpExEventDataWriter.h" 7 | 8 | class Settings; 9 | class Model; 10 | 11 | 12 | class SpExDataWriter : public ModelDataWriter 13 | { 14 | public: 15 | 16 | SpExDataWriter(Settings &settings); 17 | 18 | virtual void writeData(int generation, Model& model); 19 | 20 | protected: 21 | 22 | SpExEventDataWriter _eventDataWriter; 23 | }; 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/SpExEventDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "SpExEventDataWriter.h" 2 | #include "EventDataWriter.h" 3 | #include "SpExBranchEvent.h" 4 | 5 | #include 6 | 7 | class Settings; 8 | class BranchEvent; 9 | 10 | 11 | SpExEventDataWriter::SpExEventDataWriter(Settings& settings) : 12 | EventDataWriter(settings) 13 | { 14 | } 15 | 16 | 17 | SpExEventDataWriter::~SpExEventDataWriter() 18 | { 19 | } 20 | 21 | 22 | std::string SpExEventDataWriter::eventParameters(BranchEvent* event) 23 | { 24 | SpExBranchEvent* specificEvent = static_cast(event); 25 | 26 | std::ostringstream stringStream; 27 | stringStream << specificEvent->getLamInit() << "," 28 | << specificEvent->getLamShift() << "," 29 | << specificEvent->getMuInit() << "," 30 | << specificEvent->getMuShift(); 31 | return stringStream.str(); 32 | } 33 | -------------------------------------------------------------------------------- /src/SpExEventDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef SP_EX_EVENT_DATA_WRITER_H 2 | #define SP_EX_EVENT_DATA_WRITER_H 3 | 4 | 5 | #include "EventDataWriter.h" 6 | #include 7 | 8 | class Settings; 9 | class BranchEvent; 10 | 11 | 12 | class SpExEventDataWriter : public EventDataWriter 13 | { 14 | public: 15 | 16 | SpExEventDataWriter(Settings& settings); 17 | virtual ~SpExEventDataWriter(); 18 | 19 | private: 20 | 21 | virtual std::string specificHeader(); 22 | virtual std::string eventParameters(BranchEvent* event); 23 | }; 24 | 25 | 26 | inline std::string SpExEventDataWriter::specificHeader() 27 | { 28 | return ",lambdainit,lambdashift,muinit,mushift"; 29 | } 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/SpExModelFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef SP_EX_MODEL_FACTORY 2 | #define SP_EX_MODEL_FACTORY 3 | 4 | 5 | #include "ModelFactory.h" 6 | #include "SpExModel.h" 7 | #include "SpExDataWriter.h" 8 | 9 | class Model; 10 | class ModelDataWriter; 11 | 12 | class Random; 13 | class Settings; 14 | class Prior; 15 | 16 | 17 | class SpExModelFactory : public ModelFactory 18 | { 19 | public: 20 | 21 | virtual ~SpExModelFactory() {} 22 | 23 | virtual Model* createModel(Random& random, Settings& settings) const; 24 | virtual ModelDataWriter* createModelDataWriter(Settings& settings) const; 25 | }; 26 | 27 | 28 | inline Model* SpExModelFactory::createModel 29 | (Random& random, Settings& settings) const 30 | { 31 | return new SpExModel(random, settings); 32 | } 33 | 34 | 35 | inline ModelDataWriter* SpExModelFactory::createModelDataWriter 36 | (Settings& settings) const 37 | { 38 | return new SpExDataWriter(settings); 39 | } 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/Stat.cpp: -------------------------------------------------------------------------------- 1 | #include "Stat.h" 2 | #include "MbRandom.h" 3 | 4 | #include 5 | #include 6 | 7 | 8 | MbRandom Stat::_random; 9 | 10 | 11 | double Stat::standard_deviation(const std::vector& values) 12 | { 13 | return std::sqrt(variance(values)); 14 | } 15 | 16 | 17 | // Algorithm taken from the Naive algorithm in Wikipedia: 18 | // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 19 | double Stat::variance(const std::vector& values) 20 | { 21 | int n = 0; 22 | double sum = 0.0; 23 | double sum_of_squares = 0.0; 24 | 25 | std::vector::const_iterator it; 26 | for (it = values.begin(); it != values.end(); ++it) { 27 | n++; 28 | sum += *it; 29 | sum_of_squares += *it * *it; 30 | } 31 | 32 | return (sum_of_squares - sum * sum / (double)n) / (double)(n - 1); 33 | } 34 | 35 | 36 | double Stat::lnNormalPDF(double x, double mean, double sd) 37 | { 38 | return _random.lnNormalPdf(mean, sd * sd, x); 39 | } 40 | 41 | 42 | double Stat::lnExponentialPDF(double x, double rate) 43 | { 44 | return _random.lnExponentialPdf(rate, x); 45 | } 46 | -------------------------------------------------------------------------------- /src/Stat.h: -------------------------------------------------------------------------------- 1 | #ifndef STAT_H 2 | #define STAT_H 3 | 4 | 5 | #include "MbRandom.h" 6 | #include 7 | 8 | 9 | class Stat 10 | { 11 | public: 12 | 13 | static double standard_deviation(const std::vector& values); 14 | static double variance(const std::vector& values); 15 | 16 | static double lnNormalPDF(double x, double mean, double sd); 17 | static double lnExponentialPDF(double x, double rate); 18 | 19 | private: 20 | 21 | static MbRandom _random; 22 | }; 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/StdOutDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "StdOutDataWriter.h" 2 | #include "Settings.h" 3 | #include "Model.h" 4 | 5 | #include 6 | #include 7 | 8 | 9 | StdOutDataWriter::StdOutDataWriter(Settings& settings) : 10 | _outputFreq(settings.get("printFreq")), 11 | _headerWritten(false) 12 | { 13 | } 14 | 15 | 16 | StdOutDataWriter::~StdOutDataWriter() 17 | { 18 | } 19 | 20 | 21 | void StdOutDataWriter::writeData(int generation, Model& model) 22 | { 23 | if (!_headerWritten && _outputFreq > 0) { 24 | writeHeader(); 25 | _headerWritten = true; 26 | } 27 | 28 | if (_outputFreq == 0 || generation % _outputFreq != 0) { 29 | return; 30 | } 31 | 32 | std::cout << std::setw(12) << generation 33 | << std::setw(12) << model.getNumberOfEvents() 34 | << std::setw(12) << model.computeLogPrior() 35 | << std::setw(12) << model.getCurrentLogLikelihood() 36 | << std::setw(12) << model.getEventRate() 37 | << std::setw(12) << model.getMHAcceptanceRate() 38 | << std::endl; 39 | } 40 | 41 | 42 | void StdOutDataWriter::writeHeader() 43 | { 44 | std::cout << header() << std::endl; 45 | } 46 | 47 | 48 | std::string StdOutDataWriter::header() 49 | { 50 | return " generation" 51 | " N_shifts" 52 | " logPrior" 53 | " logLik" 54 | " eventRate" 55 | " acceptRate"; 56 | } 57 | -------------------------------------------------------------------------------- /src/StdOutDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef STD_OUT_DATA_WRITER_H 2 | #define STD_OUT_DATA_WRITER_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | class Settings; 9 | class Model; 10 | 11 | 12 | class StdOutDataWriter 13 | { 14 | public: 15 | 16 | StdOutDataWriter(Settings& settings); 17 | ~StdOutDataWriter(); 18 | 19 | void writeData(int generation, Model& model); 20 | 21 | private: 22 | 23 | void writeHeader(); 24 | std::string header(); 25 | 26 | int _outputFreq; 27 | bool _headerWritten; 28 | }; 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/TimeModeProposal.h: -------------------------------------------------------------------------------- 1 | #ifndef TIME_MODE_PROPOSAL_H 2 | #define TIME_MODE_PROPOSAL_H 3 | 4 | 5 | #include "Proposal.h" 6 | #include "Prior.h" 7 | 8 | class Random; 9 | class Settings; 10 | class Model; 11 | class Tree; 12 | class BranchEvent; 13 | 14 | 15 | class TimeModeProposal : public Proposal 16 | { 17 | enum ProposalType { 18 | TimeConstant, 19 | TimeVariable 20 | }; 21 | 22 | public: 23 | 24 | TimeModeProposal(Random& random, Settings& settings, Model& model); 25 | 26 | virtual void propose(); 27 | virtual void accept(); 28 | virtual void reject(); 29 | 30 | virtual double acceptanceRatio(); 31 | 32 | protected: 33 | 34 | virtual double initialParameter(BranchEvent* event) = 0; 35 | virtual double rateParameter(BranchEvent* event) = 0; 36 | virtual bool isTimeVariable(BranchEvent* event) = 0; 37 | 38 | virtual void setEventParameters(BranchEvent* event, 39 | double initParam, double rateParam, bool isTimeVariable) = 0; 40 | 41 | virtual void setModelParameters() = 0; 42 | 43 | virtual double rateParameterFromPrior() = 0; 44 | 45 | void makeTimeConstant(BranchEvent* event); 46 | void makeTimeVariable(BranchEvent* event); 47 | 48 | double computeMeanRate(double init, double k, double T); 49 | double computeRateInit(double mean, double k, double T); 50 | 51 | double computeLogLikelihoodRatio(); 52 | double computeLogPriorRatio(); 53 | double computeLogJacobian(); 54 | double computeJacobian(double k, double T); 55 | 56 | Model& _model; 57 | Tree* _tree; 58 | 59 | Prior _prior; 60 | 61 | BranchEvent* _event; 62 | 63 | double _currentInitParam; 64 | double _currentRateParam; 65 | bool _currentIsTimeVariable; 66 | 67 | double _currentLogLikelihood; 68 | double _currentLogPrior; 69 | 70 | double _proposedLogLikelihood; 71 | double _proposedLogPrior; 72 | 73 | ProposalType _lastTimeModeProposal; 74 | }; 75 | 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/Tools.cpp: -------------------------------------------------------------------------------- 1 | #include "Tools.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | std::vector split_string(const std::string& str, char delim) 9 | { 10 | std::vector elems; 11 | std::stringstream ss(str); 12 | std::string item; 13 | 14 | while (std::getline(ss, item, delim)) { 15 | elems.push_back(item); 16 | } 17 | 18 | return elems; 19 | } 20 | -------------------------------------------------------------------------------- /src/Tools.h: -------------------------------------------------------------------------------- 1 | #ifndef TOOLS_H 2 | #define TOOLS_H 3 | 4 | 5 | #include "Log.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | std::vector split_string(const std::string& str, char delim); 14 | 15 | template T convert_string(const std::string& str) 16 | { 17 | std::istringstream iss(str); 18 | 19 | T value; 20 | iss >> value; 21 | 22 | if (iss.fail()) { 23 | log(Error) << "Could not convert <<" << str 24 | << ">> to the proper type.\n"; 25 | std::exit(1); 26 | } 27 | 28 | return value; 29 | } 30 | 31 | 32 | template T sqr(const T& x) 33 | { 34 | return x * x; 35 | } 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /src/TraitBranchEvent.cpp: -------------------------------------------------------------------------------- 1 | #include "TraitBranchEvent.h" 2 | 3 | class Random; 4 | class Node; 5 | class Tree; 6 | 7 | 8 | TraitBranchEvent::TraitBranchEvent(double beta, double shift, 9 | bool isTimeVariable, Node* x, Tree* tp, Random& random, double map) : 10 | BranchEvent(x, tp, random, map), 11 | _betaInit(beta), _betaShift(shift), _isTimeVariable(isTimeVariable) 12 | { 13 | } 14 | -------------------------------------------------------------------------------- /src/TraitBranchEvent.h: -------------------------------------------------------------------------------- 1 | #ifndef TRAIT_BRANCH_EVENT_H 2 | #define TRAIT_BRANCH_EVENT_H 3 | 4 | #include "BranchEvent.h" 5 | 6 | class Tree; 7 | class Node; 8 | class Random; 9 | 10 | 11 | class TraitBranchEvent : public BranchEvent 12 | { 13 | 14 | private: 15 | 16 | double _betaInit; // initial beta value. 17 | double _betaShift; // temporal shift parameter of trait evolution rate. 18 | bool _isTimeVariable; 19 | 20 | public: 21 | 22 | // constructors, depending on whether you want trait rate or lambda/mu 23 | TraitBranchEvent(double beta, double shift, bool isTimeVariable, 24 | Node* x, Tree* tp, Random& random, double map); 25 | virtual ~TraitBranchEvent() {}; 26 | 27 | void setBetaInit(double x); 28 | double getBetaInit(); 29 | 30 | void setBetaShift(double x); 31 | double getBetaShift(); 32 | 33 | void setTimeVariable(bool isTimeVariable); 34 | bool isTimeVariable(); 35 | }; 36 | 37 | 38 | inline void TraitBranchEvent::setBetaInit(double x) 39 | { 40 | _betaInit = x; 41 | } 42 | 43 | 44 | inline double TraitBranchEvent::getBetaInit() 45 | { 46 | return _betaInit; 47 | } 48 | 49 | 50 | inline void TraitBranchEvent::setBetaShift(double x) 51 | { 52 | _betaShift = x; 53 | } 54 | 55 | 56 | inline double TraitBranchEvent::getBetaShift() 57 | { 58 | return _betaShift; 59 | } 60 | 61 | 62 | inline void TraitBranchEvent::setTimeVariable(bool isTimeVariable) 63 | { 64 | _isTimeVariable = isTimeVariable; 65 | } 66 | 67 | 68 | inline bool TraitBranchEvent::isTimeVariable() 69 | { 70 | return _isTimeVariable; 71 | } 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/TraitDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "TraitDataWriter.h" 2 | #include "ModelDataWriter.h" 3 | #include "NodeStateDataWriter.h" 4 | #include "TraitModel.h" 5 | 6 | class Settings; 7 | class Model; 8 | 9 | 10 | TraitDataWriter::TraitDataWriter(Settings &settings) : 11 | ModelDataWriter(settings), _eventDataWriter(settings), 12 | _nodeStateDataWriter(settings) 13 | { 14 | } 15 | 16 | 17 | void TraitDataWriter::writeData(int generation, Model& model) 18 | { 19 | ModelDataWriter::writeData(generation, model); 20 | _eventDataWriter.writeData(generation, model); 21 | _nodeStateDataWriter.writeData(generation, static_cast(model)); 22 | } 23 | -------------------------------------------------------------------------------- /src/TraitDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef TRAIT_DATA_WRITER_H 2 | #define TRAIT_DATA_WRITER_H 3 | 4 | 5 | #include "ModelDataWriter.h" 6 | #include "TraitEventDataWriter.h" 7 | #include "NodeStateDataWriter.h" 8 | 9 | class Settings; 10 | class Model; 11 | 12 | 13 | class TraitDataWriter : public ModelDataWriter 14 | { 15 | public: 16 | 17 | TraitDataWriter(Settings &settings); 18 | 19 | virtual void writeData(int generation, Model& model); 20 | 21 | protected: 22 | 23 | TraitEventDataWriter _eventDataWriter; 24 | NodeStateDataWriter _nodeStateDataWriter; 25 | }; 26 | 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/TraitEventDataWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "TraitEventDataWriter.h" 2 | #include "EventDataWriter.h" 3 | #include "TraitBranchEvent.h" 4 | 5 | #include 6 | 7 | class Settings; 8 | class BranchEvent; 9 | 10 | 11 | TraitEventDataWriter::TraitEventDataWriter(Settings& settings) : 12 | EventDataWriter(settings) 13 | { 14 | } 15 | 16 | 17 | TraitEventDataWriter::~TraitEventDataWriter() 18 | { 19 | } 20 | 21 | 22 | std::string TraitEventDataWriter::eventParameters(BranchEvent* event) 23 | { 24 | TraitBranchEvent* specificEvent = static_cast(event); 25 | 26 | std::ostringstream stringStream; 27 | stringStream << specificEvent->getBetaInit() << "," 28 | << specificEvent->getBetaShift(); 29 | return stringStream.str(); 30 | } 31 | -------------------------------------------------------------------------------- /src/TraitEventDataWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef TRAIT_EVENT_DATA_WRITER_H 2 | #define TRAIT_EVENT_DATA_WRITER_H 3 | 4 | 5 | #include "EventDataWriter.h" 6 | #include 7 | 8 | class Settings; 9 | class BranchEvent; 10 | 11 | 12 | class TraitEventDataWriter : public EventDataWriter 13 | { 14 | public: 15 | 16 | TraitEventDataWriter(Settings& settings); 17 | virtual ~TraitEventDataWriter(); 18 | 19 | private: 20 | 21 | virtual std::string specificHeader(); 22 | virtual std::string eventParameters(BranchEvent* event); 23 | }; 24 | 25 | 26 | inline std::string TraitEventDataWriter::specificHeader() 27 | { 28 | return ",betainit,betashift"; 29 | } 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/TraitModel.h: -------------------------------------------------------------------------------- 1 | #ifndef TRAIT_MODEL_H 2 | #define TRAIT_MODEL_H 3 | 4 | 5 | #include "Model.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | class Node; 12 | class Random; 13 | class Settings; 14 | class BranchEvent; 15 | class Proposal; 16 | 17 | 18 | class TraitModel : public Model 19 | { 20 | 21 | public: 22 | 23 | TraitModel(Random& rng, Settings& settings); 24 | 25 | virtual double computeLogLikelihood(); 26 | virtual double computeTriadLikelihoodTraits(Node* x); 27 | 28 | virtual double computeLogPrior(); 29 | 30 | private: 31 | 32 | virtual void setRootEventWithReadParameters 33 | (const std::vector& parameters); 34 | virtual BranchEvent* newBranchEventWithReadParameters 35 | (Node* x, double time, const std::vector& parameters); 36 | 37 | double betaInitParameter(const std::vector& parameters); 38 | double betaShiftParameter(const std::vector& parameters); 39 | 40 | virtual BranchEvent* newBranchEventWithRandomParameters(double x); 41 | virtual BranchEvent* newBranchEventWithParametersFromSettings(double x); 42 | virtual BranchEvent* newBranchEventFromLastDeletedEvent(); 43 | 44 | virtual void setMeanBranchParameters(); 45 | virtual void setDeletedEventParameters(BranchEvent* be); 46 | 47 | virtual double calculateLogQRatioJump(); 48 | 49 | virtual void getSpecificEventDataString 50 | (std::stringstream& ss, BranchEvent* event); 51 | 52 | bool _sampleFromPriorOnly; 53 | 54 | double _lastDeletedEventBetaInit;; 55 | double _lastDeletedEventBetaShift; 56 | bool _lastDeletedEventTimeVariable; 57 | 58 | // Here are several variables that track the previous 59 | // state. At some point, these should have their own class 60 | 61 | // Ultimately, initializations should be handled in TraitModel 62 | // and Model classes 63 | // NOT in class TREE!! 64 | void initializeTraitParamsForNodes(); 65 | 66 | double _readBetaInit; 67 | double _readBetaShift; 68 | }; 69 | 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /src/TraitModelFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef TRAIT_MODEL_FACTORY 2 | #define TRAIT_MODEL_FACTORY 3 | 4 | 5 | #include "ModelFactory.h" 6 | #include "TraitModel.h" 7 | #include "TraitDataWriter.h" 8 | 9 | class Model; 10 | class ModelDataWriter; 11 | 12 | class Random; 13 | class Settings; 14 | class Prior; 15 | 16 | 17 | class TraitModelFactory : public ModelFactory 18 | { 19 | public: 20 | 21 | virtual ~TraitModelFactory() {} 22 | 23 | virtual Model* createModel(Random& random, Settings& settings) const; 24 | virtual ModelDataWriter* createModelDataWriter(Settings& settings) const; 25 | }; 26 | 27 | 28 | inline Model* TraitModelFactory::createModel 29 | (Random& random, Settings& settings) const 30 | { 31 | return new TraitModel(random, settings); 32 | } 33 | 34 | 35 | inline ModelDataWriter* TraitModelFactory::createModelDataWriter 36 | (Settings& settings) const 37 | { 38 | return new TraitDataWriter(settings); 39 | } 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/TreeReader.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_READER_H 2 | #define TREE_READER_H 3 | 4 | #include 5 | 6 | class Tree; 7 | 8 | class TreeReader 9 | { 10 | public: 11 | 12 | virtual void read(std::istream &in, Tree &tree) const = 0; 13 | }; 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /tests/CommandLineProcessorTest.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "CommandLineProcessor.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | void tokenizeArgumentString(const std::string& args, int& argc, char** argv[]); 10 | std::vector tokenize(const std::string& str, const char* delim); 11 | char* copyToNewCharArray(const std::string& str); 12 | char** copyToNewArrayOfCharArrays(const std::vector& tokens); 13 | void deleteArguments(int argc, char* argv[]); 14 | 15 | 16 | TEST(CommandLineProcessor, Processing) 17 | { 18 | int argc = 0; 19 | char** argv = NULL; 20 | 21 | // Convert the "command line" into argc and argv 22 | std::string argumentString 23 | ("./bamm -c divcontrol.txt --seed 1979 --overwrite 1"); 24 | tokenizeArgumentString(argumentString, argc, &argv); 25 | 26 | // Simple test of the conversion 27 | EXPECT_EQ(argc, 7); 28 | 29 | CommandLineProcessor cmdLineProcessor(argc, argv); 30 | deleteArguments(argc, argv); 31 | 32 | // Test that the control file name was processed 33 | EXPECT_EQ("divcontrol.txt", cmdLineProcessor.controlFileName()); 34 | 35 | // Test that parameters were processed 36 | const std::vector& parameters = 37 | cmdLineProcessor.parameters(); 38 | EXPECT_EQ("seed", parameters[0].first); 39 | EXPECT_EQ("1979", parameters[0].second); 40 | EXPECT_EQ("overwrite", parameters[1].first); 41 | EXPECT_EQ("1", parameters[1].second); 42 | } 43 | 44 | 45 | void tokenizeArgumentString(const std::string& args, int& argc, char** argv[]) 46 | { 47 | const std::vector& tokens = tokenize(args, " \t"); 48 | argc = tokens.size(); 49 | *argv = copyToNewArrayOfCharArrays(tokens); 50 | } 51 | 52 | 53 | std::vector tokenize(const std::string& str, const char* delim) 54 | { 55 | std::vector tokens; 56 | 57 | char* cStr = copyToNewCharArray(str); 58 | 59 | char* token = std::strtok(cStr, delim); 60 | while (token != NULL) { 61 | tokens.push_back(token); 62 | token = std::strtok(NULL, delim); 63 | } 64 | 65 | delete[] cStr; 66 | 67 | return tokens; 68 | } 69 | 70 | 71 | char* copyToNewCharArray(const std::string& str) 72 | { 73 | char* cStr = new char[str.length() + 1]; 74 | std::strcpy(cStr, str.c_str()); 75 | return cStr; 76 | } 77 | 78 | 79 | char** copyToNewArrayOfCharArrays(const std::vector& tokens) 80 | { 81 | char** arrayOfCharArrays = new char*[tokens.size()]; 82 | for (int i = 0; i < tokens.size(); i++) { 83 | arrayOfCharArrays[i] = copyToNewCharArray(tokens[i]); 84 | } 85 | return arrayOfCharArrays; 86 | } 87 | 88 | 89 | void deleteArguments(int argc, char* argv[]) 90 | { 91 | for (int i = 0; i < argc; i++) { 92 | delete[] argv[i]; 93 | } 94 | 95 | delete[] argv; 96 | } 97 | -------------------------------------------------------------------------------- /tests/Makefile: -------------------------------------------------------------------------------- 1 | CXX_COMPILER = g++ 2 | CXX_FLAGS = 3 | 4 | src_dir = ~/projects/bamm/src/ 5 | gtest_include_dir = ~/gtest-1.7.0/include/ 6 | gtest_src_dir = ~/gtest-1.7.0/src/ 7 | 8 | test_files = *.cpp 9 | src_files = $(src_dir)/[A-Z]*.cpp # Excludes main.cpp 10 | 11 | test-all: $(test_files) $(src_files) 12 | $(CXX_COMPILER) $(CXX_FLAGS) -I $(src_dir) -I $(gtest_include_dir) \ 13 | -o test-all $(test_files) $(src_files) \ 14 | $(gtest_src_dir)/gtest-all.o $(gtest_src_dir)/gtest_main.o 15 | 16 | clean: 17 | rm test-all 18 | -------------------------------------------------------------------------------- /tests/NodeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "Node.h" 3 | 4 | 5 | TEST(NodeTest, Construction) 6 | { 7 | Node* node = new Node(); 8 | Node* leftNode = new Node(); 9 | Node* rightNode = new Node(); 10 | Node* ancNode = new Node(); 11 | 12 | EXPECT_EQ("", node->getName()); 13 | node->setName("TestNode"); 14 | EXPECT_EQ("TestNode", node->getName()); 15 | 16 | EXPECT_EQ(NULL, node->getLfDesc()); 17 | node->setLfDesc(leftNode); 18 | EXPECT_EQ(leftNode, node->getLfDesc()); 19 | 20 | EXPECT_EQ(NULL, node->getRtDesc()); 21 | node->setRtDesc(rightNode); 22 | EXPECT_EQ(rightNode, node->getRtDesc()); 23 | 24 | EXPECT_EQ(NULL, node->getAnc()); 25 | node->setAnc(ancNode); 26 | EXPECT_EQ(ancNode, node->getAnc()); 27 | 28 | EXPECT_EQ(0, node->getIndex()); 29 | node->setIndex(10); 30 | EXPECT_EQ(10, node->getIndex()); 31 | 32 | EXPECT_EQ(0.0, node->getTime()); 33 | node->setTime(1.0); 34 | EXPECT_EQ(1.0, node->getTime()); 35 | 36 | EXPECT_EQ(0.0, node->getBrlen()); 37 | node->setBrlen(1.0); 38 | EXPECT_EQ(1.0, node->getBrlen()); 39 | 40 | EXPECT_EQ(0, node->getTipDescCount()); 41 | node->setTipDescCount(10); 42 | EXPECT_EQ(10, node->getTipDescCount()); 43 | 44 | EXPECT_EQ(false, node->getExtantStatus()); 45 | node->setExtantStatus(true); 46 | EXPECT_EQ(true, node->getExtantStatus()); 47 | 48 | EXPECT_EQ(false, node->getIsTip()); 49 | node->setIsTip(true); 50 | EXPECT_EQ(true, node->getIsTip()); 51 | 52 | EXPECT_EQ(false, node->getIsConstant()); 53 | node->setIsConstant(true); 54 | EXPECT_EQ(true, node->getIsConstant()); 55 | 56 | EXPECT_EQ(false, node->getIsLivingTip()); 57 | node->setIsLivingTip(true); 58 | EXPECT_EQ(true, node->getIsLivingTip()); 59 | 60 | EXPECT_EQ(0.0, node->getMapStart()); 61 | node->setMapStart(1.0); 62 | EXPECT_EQ(1.0, node->getMapStart()); 63 | 64 | EXPECT_EQ(0.0, node->getMapEnd()); 65 | node->setMapEnd(1.0); 66 | EXPECT_EQ(1.0, node->getMapEnd()); 67 | 68 | delete ancNode; 69 | delete rightNode; 70 | delete leftNode; 71 | delete node; 72 | } 73 | -------------------------------------------------------------------------------- /tools/generate-authors.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo This is a list of people who have contributed to the development of BAMM 4 | echo and/or part of BAMMtools. It is sorted by the number of commits made to 5 | echo to the master branch of BAMM\'s Git repository. This file was generated 6 | echo using the script tools/generate-authors.sh. 7 | echo 8 | 9 | git log --format='%an' | 10 | awk '{ if ($0 == "blueraleigh") 11 | print "Mike Grundler" 12 | else if ($0 == "Dan Rabosky") 13 | print "Daniel Rabosky" 14 | else if ($0 == "pascaltitle") 15 | print "Pascal Title" 16 | else if ($0 == "josephwb") 17 | print "Joseph W. Brown" 18 | else if ($0 == "SimonGreenhill") 19 | print "Simon Greenhill" 20 | else 21 | print 22 | }' | sort | uniq -c | sort -nr | 23 | awk '{ $1 = ""; print }' | cut -d' ' -f2- 24 | --------------------------------------------------------------------------------