| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evomodel.substmodel.MarkovModulatedSubstitutionModel; |
| 29 |
import dr.evolution.datatype.HiddenDataType; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class MarkovModulatedHiddenClassRewardParser extends AbstractXMLObjectParser { |
| 38 |
|
| 39 |
public static final String PARSER_NAME = "hiddenClassRewardParameter"; |
| 40 |
public static final String NAME = "name"; |
| 41 |
public static final String CLASS_NUMBER = "class"; |
| 42 |
|
| 43 |
@Override |
| 44 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 45 |
|
| 46 |
MarkovModulatedSubstitutionModel substitutionModel = (MarkovModulatedSubstitutionModel) xo.getChild(MarkovModulatedSubstitutionModel.class); |
| 47 |
HiddenDataType hiddenDataType = (HiddenDataType) substitutionModel.getDataType(); |
| 48 |
int classNumber = xo.getIntegerAttribute(CLASS_NUMBER); |
| 49 |
int hiddenClassCount = hiddenDataType.getHiddenClassCount(); |
| 50 |
if (classNumber < 1 || classNumber > hiddenClassCount) { |
| 51 |
throw new XMLParseException("Invalid class number in " + xo.getId()); |
| 52 |
} |
| 53 |
classNumber--; |
| 54 |
int stateCount = hiddenDataType.getStateCount() / hiddenClassCount; |
| 55 |
|
| 56 |
|
| 57 |
Parameter parameter = new Parameter.Default(stateCount * hiddenClassCount, 0.0); |
| 58 |
for (int i = 0; i < stateCount; ++i) { |
| 59 |
parameter.setParameterValue(i + classNumber * stateCount, 1.0); |
| 60 |
} |
| 61 |
|
| 62 |
if (xo.hasAttribute(NAME)) { |
| 63 |
parameter.setId((String) xo.getAttribute(NAME)); |
| 64 |
} else { |
| 65 |
parameter.setId(substitutionModel.getId() + "_" + Integer.toString(classNumber + 1)); |
| 66 |
} |
| 67 |
|
| 68 |
return parameter; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Override |
| 76 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 77 |
return rules; |
| 78 |
} |
| 79 |
|
| 80 |
private XMLSyntaxRule[] rules = { |
| 81 |
AttributeRule.newIntegerRule(CLASS_NUMBER), |
| 82 |
AttributeRule.newStringRule(NAME, true), |
| 83 |
new ElementRule(MarkovModulatedSubstitutionModel.class), |
| 84 |
}; |
| 85 |
|
| 86 |
@Override |
| 87 |
public String getParserDescription() { |
| 88 |
return "Generates a reward parameter to log hidden classes in Markov-modulated substitutionProcess"; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public Class getReturnType() { |
| 93 |
return Parameter.class; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public String getParserName() { |
| 100 |
return PARSER_NAME; |
| 101 |
} |
| 102 |
} |