Debian Codegraph @

src/dr/evomodelxml/branchmodel/BranchSpecificBranchModelParser.java

raw symbols SCIP
1 /*
2 * BranchSpecificBranchModelParser.java
3 *
4 * Copyright (c) 2002-2016 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodelxml.branchmodel;
27
28 import dr.evolution.tree.TreeUtils;
29 import dr.evomodel.branchmodel.BranchSpecificBranchModel;
30 import dr.evomodel.substmodel.SubstitutionModel;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.TaxonList;
33 import dr.evomodel.tree.TreeModel;
34 import dr.xml.*;
35
36 import java.util.logging.Logger;
37
38 /**
39 * @author Andrew Rambaut
40 */
41 public class BranchSpecificBranchModelParser extends AbstractXMLObjectParser {
42
43 public static final String BRANCH_SPECIFIC_SUBSTITUTION_MODEL = "branchSpecificSubstitutionModel";
44 public static final String CLADE = "clade";
45 public static final String EXTERNAL_BRANCHES = "externalBranches";
46 public static final String BACKBONE = "backbone";
47 public static final String STEM_WEIGHT = "stemWeight";
48
49 public String getParserName() {
50 return BRANCH_SPECIFIC_SUBSTITUTION_MODEL;
51 }
52
53 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
54
55 Logger.getLogger("dr.evomodel").info("\nUsing clade-specific branch model.");
56
57 TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
58 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
59 BranchSpecificBranchModel branchModel = new BranchSpecificBranchModel(tree, substitutionModel);
60
61 for (int i = 0; i < xo.getChildCount(); i++) {
62 if (xo.getChild(i) instanceof XMLObject) {
63
64 XMLObject xoc = (XMLObject) xo.getChild(i);
65 if (xoc.getName().equals(CLADE)) {
66
67 double stemWeight = xoc.getAttribute(STEM_WEIGHT, 0.0);
68
69 substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
70 TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
71
72 if (taxonList.getTaxonCount() == 1) {
73 throw new XMLParseException("A clade must be defined by at least two taxa");
74 }
75
76 try {
77 branchModel.addClade(taxonList, substitutionModel, stemWeight);
78
79 } catch (TreeUtils.MissingTaxonException mte) {
80 throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
81 }
82 } else if (xoc.getName().equals(EXTERNAL_BRANCHES)) {
83
84 substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
85 TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
86
87
88 try {
89 branchModel.addExternalBranches(taxonList, substitutionModel);
90
91 } catch (TreeUtils.MissingTaxonException mte) {
92 throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
93 }
94 } else if (xoc.getName().equals(BACKBONE)) {
95
96 substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
97 TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
98
99 try {
100 branchModel.addBackbone(taxonList, substitutionModel);
101
102 } catch (TreeUtils.MissingTaxonException mte) {
103 throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
104 }
105 }
106
107 }
108 }
109
110 return branchModel;
111 }
112
113 //************************************************************************
114 // AbstractXMLObjectParser implementation
115 //************************************************************************
116
117 public String getParserDescription() {
118 return
119 "This element provides a branch model which allows different substitution models" +
120 "on different parts of the tree.";
121 }
122
123 public Class getReturnType() {
124 return BranchSpecificBranchModel.class;
125 }
126
127 public XMLSyntaxRule[] getSyntaxRules() {
128 return rules;
129 }
130
131 private final XMLSyntaxRule[] rules = {
132 new ElementRule(TreeModel.class, "The tree"),
133 new ElementRule(SubstitutionModel.class, "The substitution model for branches not explicitly included"),
134 new ElementRule(EXTERNAL_BRANCHES,
135 new XMLSyntaxRule[]{
136 new ElementRule(Taxa.class, "A substitution model will be applied to the external branches for these taxa"),
137 new ElementRule(SubstitutionModel.class, "The substitution model"),
138 }, 0, Integer.MAX_VALUE),
139 new ElementRule(CLADE,
140 new XMLSyntaxRule[]{
141 AttributeRule.newDoubleRule(STEM_WEIGHT, true, "What proportion of the stem branch to include [0 <= w <= 1] (default 0)."),
142 new ElementRule(Taxa.class, "A set of taxa which defines a clade to apply a different site model to"),
143 new ElementRule(SubstitutionModel.class, "The substitution model"),
144 }, 0, Integer.MAX_VALUE),
145 new ElementRule(BACKBONE,
146 new XMLSyntaxRule[]{
147 new ElementRule(Taxa.class, "A substitution model will be applied only to " +
148 "the 'backbone' branches defined by these taxa."),
149 new ElementRule(SubstitutionModel.class, "The substitution model"),
150 }, 0, Integer.MAX_VALUE),
151 };
152
153 }