Skip to main content Accessibility help
×
Hostname: page-component-7479d7b7d-m9pkr Total loading time: 0 Render date: 2024-07-12T05:24:37.987Z Has data issue: false hasContentIssue false

14 - Coding and design patterns

from Part III - Programming

Published online by Cambridge University Press:  05 October 2015

Alexei J. Drummond
Affiliation:
University of Auckland
Remco R. Bouckaert
Affiliation:
University of Auckland
Get access

Summary

We will show a few patterns commonly used with BEAST 2, illustrating how the MCMC framework can be exploited to write efficient models. The way BEAST 2 classes are based on BEAST-objects has the advantage that it does automatically define a fairly readable form of XML and allows automatic checking of a number of validation rules. The validation helps in debugging models. These advantages are based on Java introspection and there are a few peculiarities due to the limitations of Java introspection which can lead to unexpected behaviour. These issues will be highlighted in this chapter.

We start with some basic patterns for BEAST-objects, inputs and the core StateNode and CalulationNode BEAST-objects, and how to write efficient versions of them. We also show a number of the most commonly used BEAST-objects from the evolution library and how to extend them. Finally, there are some tips and a list of common errors that one should be aware of.

Basic patterns

First, we have a look at some basic patterns.

14.1.1 Basic BEAST-object layout

Box 14.1 shows the layout of most BEAST-objects that you will find in the BEAST code. We all love to have documented code, so it starts with a Description annotation, typically a one-line comment on the function of the BEAST-object, but multiline descriptions are fine as well. The Description is used in documentation and in online help in GUIs like BEAUti. Omitting such a description leads to failure of the DocumentationTest. This does not mean that further Java comments are not allowed or necessary, but the description does help in documenting classes for code writers as well.

Every BEAST-object implements BEASTInterface. BEASTObject is a base implementation of BEASTInterface and many BEASTObjects extend the BEASTObject class. By implementing BEASTInterface, services that use introspection, like validation of models, are automatically provided. To specify an input for a BEAST-object, just declare an Input member. Input is a template class, so the type of input can be specified to make sure that when Inputs are connected to BEAST-objects the correct type of BEAST-object is used.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2015

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×