| 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 java.util.LinkedHashMap; |
| 29 |
import java.util.logging.Logger; |
| 30 |
|
| 31 |
import dr.evomodel.branchmodel.BranchAssignmentModel; |
| 32 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 33 |
import dr.evomodel.tree.TreeModel; |
| 34 |
import dr.xml.AbstractXMLObjectParser; |
| 35 |
import dr.xml.AttributeRule; |
| 36 |
import dr.xml.ElementRule; |
| 37 |
import dr.xml.XMLObject; |
| 38 |
import dr.xml.XMLParseException; |
| 39 |
import dr.xml.XMLSyntaxRule; |
| 40 |
|
| 41 |
public class BranchAssignmentModelParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public static final String ANNOTATION = "annotation"; |
| 44 |
public static final String ANNOTATION_VALUE = "annotationValue"; |
| 45 |
public static final String BASE_MODEL = "baseModel"; |
| 46 |
public static final String ASSIGNMENT = "assignment"; |
| 47 |
|
| 48 |
@Override |
| 49 |
public String getParserName() { |
| 50 |
return BranchAssignmentModel.BRANCH_ASSIGNMENT_MODEL; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
Logger.getLogger("dr.evomodel").info( |
| 57 |
"\nUsing branch assignment branch model."); |
| 58 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 59 |
String annotation = xo.getStringAttribute(ANNOTATION); |
| 60 |
|
| 61 |
LinkedHashMap<Integer, SubstitutionModel> modelIndexMap = new LinkedHashMap<Integer, SubstitutionModel>(); |
| 62 |
|
| 63 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 64 |
if (xo.getChild(i) instanceof XMLObject) { |
| 65 |
XMLObject xoc = (XMLObject) xo.getChild(i); |
| 66 |
if (xoc.getName().equals(ASSIGNMENT)) { |
| 67 |
|
| 68 |
Integer index = null; |
| 69 |
if (xoc.hasAttribute(ANNOTATION_VALUE)) { |
| 70 |
index = xoc.getIntegerAttribute(ANNOTATION_VALUE); |
| 71 |
} |
| 72 |
|
| 73 |
SubstitutionModel model = (SubstitutionModel) xoc |
| 74 |
.getChild(SubstitutionModel.class); |
| 75 |
|
| 76 |
modelIndexMap.put(index, model); |
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
SubstitutionModel baseModel = (SubstitutionModel) xo |
| 83 |
.getElementFirstChild(BASE_MODEL); |
| 84 |
|
| 85 |
return new BranchAssignmentModel(treeModel, annotation, modelIndexMap, |
| 86 |
baseModel); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 91 |
|
| 92 |
return new XMLSyntaxRule[] { |
| 93 |
|
| 94 |
new ElementRule(TreeModel.class, false), |
| 95 |
new ElementRule(ASSIGNMENT, |
| 96 |
new XMLSyntaxRule[] { |
| 97 |
AttributeRule.newIntegerRule(ANNOTATION_VALUE, |
| 98 |
false), |
| 99 |
new ElementRule(SubstitutionModel.class, false), |
| 100 |
}, |
| 101 |
1, Integer.MAX_VALUE), |
| 102 |
new ElementRule(BASE_MODEL, |
| 103 |
new XMLSyntaxRule[] { |
| 104 |
new ElementRule(SubstitutionModel.class, false) |
| 105 |
}, false) |
| 106 |
|
| 107 |
}; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public String getParserDescription() { |
| 112 |
return "This element provides a branch model which assigns " |
| 113 |
+ "substitution models to branches on the tree "; |
| 114 |
} |
| 115 |
|
| 116 |
@SuppressWarnings("rawtypes") |
| 117 |
@Override |
| 118 |
public Class getReturnType() { |
| 119 |
return BranchAssignmentModelParser.class; |
| 120 |
} |
| 121 |
|
| 122 |
} |