| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodelxml.branchratemodel; |
| 27 |
|
| 28 |
import dr.evolution.tree.TreeUtils; |
| 29 |
import dr.evolution.util.Taxa; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 32 |
import dr.evomodel.branchratemodel.LocalClockModel; |
| 33 |
import dr.evomodel.tree.TreeModel; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.util.logging.Logger; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class LocalClockModelParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public static final String LOCAL_CLOCK_MODEL = "localClockModel"; |
| 44 |
public static final String RATE = BranchRateModel.RATE; |
| 45 |
public static final String RELATIVE = "relative"; |
| 46 |
public static final String CLADE = "clade"; |
| 47 |
public static final String INCLUDE_STEM = "includeStem"; |
| 48 |
public static final String STEM_PROPORTION = "stemProportion"; |
| 49 |
public static final String EXCLUDE_CLADE = "excludeClade"; |
| 50 |
public static final String EXTERNAL_BRANCHES = "externalBranches"; |
| 51 |
public static final String TRUNK = "trunk"; |
| 52 |
public static final String INDEX = "index"; |
| 53 |
|
| 54 |
|
| 55 |
public String getParserName() { |
| 56 |
return LOCAL_CLOCK_MODEL; |
| 57 |
} |
| 58 |
|
| 59 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 60 |
|
| 61 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 62 |
|
| 63 |
Parameter globalRateParameter = (Parameter) xo.getElementFirstChild(RATE); |
| 64 |
LocalClockModel localClockModel = new LocalClockModel(tree, globalRateParameter); |
| 65 |
|
| 66 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 67 |
if (xo.getChild(i) instanceof XMLObject) { |
| 68 |
|
| 69 |
XMLObject xoc = (XMLObject) xo.getChild(i); |
| 70 |
if (xoc.getName().equals(CLADE)) { |
| 71 |
|
| 72 |
boolean relative = xoc.getAttribute(RELATIVE, false); |
| 73 |
|
| 74 |
Parameter rateParameter = (Parameter) xoc.getChild(Parameter.class); |
| 75 |
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class); |
| 76 |
|
| 77 |
if (taxonList.getTaxonCount() == 1) { |
| 78 |
throw new XMLParseException("A local clock for a clade must be defined by at least two taxa"); |
| 79 |
} |
| 80 |
|
| 81 |
boolean excludeClade = false; |
| 82 |
double stemProportion = 0.0; |
| 83 |
|
| 84 |
if (xoc.hasAttribute(INCLUDE_STEM)) { |
| 85 |
|
| 86 |
stemProportion = xoc.getBooleanAttribute(INCLUDE_STEM) ? 1.0 : 0.0; |
| 87 |
} |
| 88 |
|
| 89 |
if (xoc.hasAttribute(STEM_PROPORTION)) { |
| 90 |
stemProportion = xoc.getDoubleAttribute(STEM_PROPORTION); |
| 91 |
if (stemProportion < 0.0 || stemProportion > 1.0) { |
| 92 |
throw new XMLParseException("A stem proportion should be between 0, 1"); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (xoc.hasAttribute(EXCLUDE_CLADE)) { |
| 97 |
excludeClade = xoc.getBooleanAttribute(EXCLUDE_CLADE); |
| 98 |
} |
| 99 |
|
| 100 |
try { |
| 101 |
localClockModel.addCladeClock(taxonList, rateParameter, relative, stemProportion, excludeClade); |
| 102 |
|
| 103 |
} catch (TreeUtils.MissingTaxonException mte) { |
| 104 |
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree."); |
| 105 |
} |
| 106 |
} else if (xoc.getName().equals(EXTERNAL_BRANCHES)) { |
| 107 |
|
| 108 |
boolean relative = xoc.getAttribute(RELATIVE, false); |
| 109 |
|
| 110 |
Parameter rateParameter = (Parameter) xoc.getChild(Parameter.class); |
| 111 |
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class); |
| 112 |
|
| 113 |
|
| 114 |
try { |
| 115 |
localClockModel.addExternalBranchClock(taxonList, rateParameter, relative); |
| 116 |
|
| 117 |
} catch (TreeUtils.MissingTaxonException mte) { |
| 118 |
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree."); |
| 119 |
} |
| 120 |
} else if (xoc.getName().equals(TRUNK)) { |
| 121 |
|
| 122 |
boolean relative = xoc.getAttribute(RELATIVE, false); |
| 123 |
|
| 124 |
Parameter indexParameter = null; |
| 125 |
if (xoc.hasChildNamed(INDEX)) { |
| 126 |
indexParameter = (Parameter) xoc.getElementFirstChild(INDEX); |
| 127 |
} |
| 128 |
Parameter rateParameter = (Parameter) xoc.getChild(Parameter.class); |
| 129 |
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class); |
| 130 |
|
| 131 |
|
| 132 |
try { |
| 133 |
localClockModel.addTrunkClock(taxonList, rateParameter, indexParameter, relative); |
| 134 |
|
| 135 |
} catch (TreeUtils.MissingTaxonException mte) { |
| 136 |
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree."); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
Logger.getLogger("dr.evomodel").info("\nUsing local clock branch rate model."); |
| 144 |
|
| 145 |
return localClockModel; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public String getParserDescription() { |
| 153 |
return "This element returns a branch rate model that adds a delta to each terminal branch length."; |
| 154 |
} |
| 155 |
|
| 156 |
public Class getReturnType() { |
| 157 |
return LocalClockModel.class; |
| 158 |
} |
| 159 |
|
| 160 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 161 |
return rules; |
| 162 |
} |
| 163 |
|
| 164 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 165 |
new ElementRule(TreeModel.class), |
| 166 |
new ElementRule(RATE, Parameter.class, "The molecular evolutionary rate parameter", false), |
| 167 |
new ElementRule(EXTERNAL_BRANCHES, |
| 168 |
new XMLSyntaxRule[]{ |
| 169 |
AttributeRule.newBooleanRule(RELATIVE, true), |
| 170 |
new ElementRule(Taxa.class, "A local clock that will be applied only to " + |
| 171 |
"the external branches for these taxa"), |
| 172 |
new ElementRule(Parameter.class, "The rate parameter"), |
| 173 |
}, 0, Integer.MAX_VALUE), |
| 174 |
new ElementRule(CLADE, |
| 175 |
new XMLSyntaxRule[]{ |
| 176 |
AttributeRule.newBooleanRule(RELATIVE, true), |
| 177 |
AttributeRule.newBooleanRule(INCLUDE_STEM, true, "determines whether or not the stem branch above this clade is included in the siteModel (default false)."), |
| 178 |
AttributeRule.newDoubleRule(STEM_PROPORTION, true, "proportion of stem to include in clade rate (default 0)."), |
| 179 |
AttributeRule.newBooleanRule(EXCLUDE_CLADE, true, "determines whether to exclude actual branches of the clade from the siteModel (default false)."), |
| 180 |
new ElementRule(Taxa.class, "A set of taxa which defines a clade to apply a different site model to"), |
| 181 |
new ElementRule(Parameter.class, "The rate parameter") |
| 182 |
}, 0, Integer.MAX_VALUE), |
| 183 |
new ElementRule(TRUNK, |
| 184 |
new XMLSyntaxRule[]{ |
| 185 |
AttributeRule.newBooleanRule(RELATIVE, true), |
| 186 |
new ElementRule(Taxa.class, "A local clock that will be applied only to " + |
| 187 |
"the 'trunk' branches defined by these taxa"), |
| 188 |
new ElementRule(Parameter.class, "The rate parameter"), |
| 189 |
new ElementRule(INDEX, Parameter.class, "The trunk taxon index", true), |
| 190 |
}, 0, Integer.MAX_VALUE), |
| 191 |
}; |
| 192 |
} |