def add_operators(self, beastxml): if beastxml.config.languages.sample_branch_lengths: updown = xml.operator( beastxml.run, attrib={ "id": "UpDown", "spec": "UpDownOperator", "scaleFactor": "0.5", "weight": "3.0"}) xml.tree(updown, idref="Tree.t:beastlingTree", name="up") xml.parameter(updown, idref="birthRate.t:beastlingTree", name="down") ### Include clock rates in up/down only if calibrations are given if beastxml.config.calibrations: for clock in beastxml.config.clocks: if clock.estimate_rate: xml.parameter(updown, idref=clock.mean_rate_id, name="down") # Birth rate scaler # Birth rate is *always* scaled. xml.operator( beastxml.run, attrib={"id": "YuleBirthRateScaler.t:beastlingTree", "spec": "ScaleOperator", "parameter": "@birthRate.t:beastlingTree", "scaleFactor": "0.5", "weight": "3.0"})
def add_likelihood(self, likelihood): """ Add likelihood distribution corresponding to all features in the dataset. """ for n, f in enumerate(self.features): fname = "%s:%s" % (self.name, xml.valid_id(f)) attribs = { "id": "featureLikelihood:%s" % fname, "spec": "TreeLikelihood", "useAmbiguities": "true" } if self.pruned: distribution = xml.distribution(likelihood, attrib=attribs) # Create pruned tree tree_id = "Tree.t:prunedBeastlingTree.%s" % fname tree = xml.tree(distribution, id=tree_id, spec="beast.evolution.tree.PrunedTree", quickshortcut="true", attrib={'assert': "false"}) xml.tree(tree, idref="Tree.t:beastlingTree") xml.alignment(tree, idref="pruned_data_%s" % fname) # Create pruned branchrate self.clock.add_pruned_branchrate_model(distribution, fname, tree_id) else: attribs[ "branchRateModel"] = "@%s" % self.clock.branchrate_model_id attribs["tree"] = "@Tree.t:beastlingTree" distribution = xml.distribution(likelihood, attrib=attribs) if f in self.reconstruct: # Use a different likelihood spec (also depending on whether # the whole tree is reconstructed, or only some nodes) if self.treewide_reconstruction: distribution.attrib[ "spec"] = "AncestralStateTreeLikelihood" self.treedata.append(attribs["id"]) distribution.attrib["tag"] = f else: distribution.attrib[ "spec"] = "lucl.beast.statereconstruction.AncestralStatesLogger" distribution.attrib["value"] = " ".join( self.pattern_names(f)) for label in self.reconstruct_at: langs = self.config.language_group(label) self.beastxml.add_taxon_set(distribution, label, langs) self.metadata.append(attribs["id"]) distribution.attrib["useAmbiguities"] = "false" # Sitemodel self.add_sitemodel(distribution, f, fname) # Data self.add_feature_data(distribution, n, f, fname)
def add_state_nodes(self, beastxml): """ Add tree-related <state> sub-elements. """ state = beastxml.state self.tree = xml.tree(state, id=self.tree_id, name="stateNode") xml.taxonset(self.tree, idref="taxa") if self.type in ["yule", "birthdeath"]: param = xml.parameter(state, id="birthRate.t:beastlingTree", name="stateNode") if self.birthrate_estimate is not None: param.text = str(self.birthrate_estimate) else: param.text = "1.0" if self.type in ["birthdeath"]: xml.parameter(beastxml.state, text="0.5", id="deathRate.t:beastlingTree", name="stateNode") xml.parameter(beastxml.state, text="0.2", id="sampling.t:beastlingTree", name="stateNode") elif self.type == "coalescent": xml.parameter(beastxml.state, text="1.0", id="popSize.t:beastlingTree", name="stateNode") if beastxml.config.tip_calibrations: self.add_tip_heights(beastxml.config.tip_calibrations)
def add_state_nodes(self, beastxml): """ Add tree-related <state> sub-elements. """ state = beastxml.state self.tree = xml.tree(state, id=self.tree_id, name="stateNode") xml.taxonset(self.tree, idref="taxa") if beastxml.config.tip_calibrations: self.add_tip_heights(beastxml.config.tip_calibrations)
def add_operators(self, beastxml): """ Add all <operator>s which act on the tree topology and branch lengths. """ """ Add all <operator>s which act on the tree topology and branch lengths. """ # Tree operators # Operators which affect the tree must respect the sample_topology and # sample_branch_length options. if beastxml.config.languages.sample_topology: ## Tree topology operators xml.operator(beastxml.run, attrib={ "id": "SubtreeSlide.t:beastlingTree", "spec": "SubtreeSlide", "tree": "@Tree.t:beastlingTree", "markclades": "true", "weight": "15.0" }) xml.operator(beastxml.run, attrib={ "id": "narrow.t:beastlingTree", "spec": "Exchange", "tree": "@Tree.t:beastlingTree", "markclades": "true", "weight": "15.0" }) xml.operator(beastxml.run, attrib={ "id": "wide.t:beastlingTree", "isNarrow": "false", "spec": "Exchange", "tree": "@Tree.t:beastlingTree", "markclades": "true", "weight": "3.0" }) xml.operator(beastxml.run, attrib={ "id": "WilsonBalding.t:beastlingTree", "spec": "WilsonBalding", "tree": "@Tree.t:beastlingTree", "markclades": "true", "weight": "3.0" }) if beastxml.config.languages.sample_branch_lengths: ## Branch length operators xml.operator(beastxml.run, attrib={ "id": "UniformOperator.t:beastlingTree", "spec": "Uniform", "tree": "@Tree.t:beastlingTree", "weight": "30.0" }) xml.operator(beastxml.run, attrib={ "id": "treeScaler.t:beastlingTree", "scaleFactor": "0.5", "spec": "ScaleOperator", "tree": "@Tree.t:beastlingTree", "weight": "3.0" }) xml.operator(beastxml.run, attrib={ "id": "treeRootScaler.t:beastlingTree", "scaleFactor": "0.5", "spec": "ScaleOperator", "tree": "@Tree.t:beastlingTree", "rootOnly": "true", "weight": "3.0" }) ## Up/down operator which scales tree height if self.type in ["yule", "birthdeath"]: updown = xml.operator(beastxml.run, attrib={ "id": "UpDown", "spec": "UpDownOperator", "scaleFactor": "0.5", "weight": "3.0" }) xml.tree(updown, idref="Tree.t:beastlingTree", name="up") xml.parameter(updown, idref="birthRate.t:beastlingTree", name="down") ### Include clock rates in up/down only if calibrations are given if beastxml.config.calibrations: for clock in beastxml.config.clocks: if clock.estimate_rate: xml.parameter(updown, idref=clock.mean_rate_id, name="down") if self.type in ["yule", "birthdeath"]: # Birth rate scaler # Birth rate is *always* scaled. xml.operator(beastxml.run, attrib={ "id": "YuleBirthRateScaler.t:beastlingTree", "spec": "ScaleOperator", "parameter": "@birthRate.t:beastlingTree", "scaleFactor": "0.5", "weight": "3.0" }) elif self.type == "coalescent": xml.operator(beastxml.run, attrib={ "id": "PopulationSizeScaler.t:beastlingTree", "spec": "ScaleOperator", "parameter": "@popSize.t:beastlingTree", "scaleFactor": "0.5", "weight": "3.0" }) if self.type in ["birthdeath"]: xml.operator(beastxml.run, attrib={ "id": "SamplingScaler.t:beastlingTree", "spec": "ScaleOperator", "parameter": "@sampling.t:beastlingTree", "scaleFactor": "0.8", "weight": "1.0" }) xml.operator(beastxml.run, attrib={ "id": "DeathRateScaler.t:beastlingTree", "spec": "ScaleOperator", "parameter": "@deathRate.t:beastlingTree", "scaleFactor": "0.5", "weight": "3.0" }) # Add a Tip Date scaling operator if required if beastxml.config.tip_calibrations and beastxml.config.languages.sample_branch_lengths: # Get a list of taxa with non-point tip cals tip_taxa = [ next(cal.langs.__iter__()) for cal in beastxml.config.tip_calibrations.values() if cal.dist != "point" ] for taxon in tip_taxa: tiprandomwalker = xml.operator(beastxml.run, attrib={ "id": "TipDatesandomWalker:%s" % taxon, "spec": "TipDatesRandomWalker", "windowSize": "1", "tree": "@Tree.t:beastlingTree", "weight": "3.0", }) beastxml.add_taxon_set(tiprandomwalker, taxon, (taxon, ))