| 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.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 |
|
| 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 |
|
| 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 |
} |