use of dr.evolution.datatype.TwoStates in project beast-mcmc by beast-dev.
the class OrderedLatentLiabilityTransformParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
OrderedLatentLiabilityLikelihood likelihood = (OrderedLatentLiabilityLikelihood) xo.getChild(OrderedLatentLiabilityLikelihood.class);
CompoundParameter parameter = likelihood.getTipTraitParameter();
DataType dataType = likelihood.getPatternList().getDataType();
if (!(dataType instanceof TwoStates)) {
throw new XMLParseException("Liability transformation is currently only implemented for binary traits");
}
Parameter mask = null;
if (xo.hasChildNamed(MaskedParameterParser.MASKING)) {
mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);
}
List<Transform> transforms = new ArrayList<Transform>();
int index = 0;
for (int tip = 0; tip < parameter.getParameterCount(); ++tip) {
final int[] tipData = likelihood.getData(tip);
for (int trait = 0; trait < tipData.length; ++trait) {
int discreteState = tipData[trait];
boolean valid = true;
Transform transform;
if (discreteState == 0) {
transform = Transform.LOG_NEGATE;
if (parameter.getParameterValue(index) >= 0.0) {
valid = false;
}
} else if (discreteState == 1) {
transform = Transform.LOG;
if (parameter.getParameterValue(index) <= 0.0) {
valid = false;
}
} else {
transform = Transform.NONE;
// transforms.add(Transform.NONE);
}
if (!valid) {
throw new XMLParseException("Incompatible binary trait and latent value in tip '" + parameter.getParameter(tip).getId() + "'");
}
if (mask == null || mask.getParameterValue(index) == 1.0) {
transforms.add(transform);
}
++index;
}
}
return new Transform.Array(transforms, parameter);
}
Aggregations