Debian Codegraph @

src/dr/evomodelxml/continuous/OrderedLatentLiabilityTransformParser.java

raw symbols SCIP
1 /*
2 * OrderedLatentLiabilityTransformParser.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodelxml.continuous;
27
28 import dr.evolution.datatype.DataType;
29 import dr.evolution.datatype.TwoStates;
30 import dr.evomodel.continuous.OrderedLatentLiabilityLikelihood;
31 import dr.inference.model.*;
32 import dr.inferencexml.model.MaskedParameterParser;
33 import dr.util.Transform;
34 import dr.xml.*;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40 * @author Marc A. Suchard
41 */
42
43 public class OrderedLatentLiabilityTransformParser extends AbstractXMLObjectParser {
44
45 public static final String NAME = "orderedLatentLiabilityTransform";
46
47 @Override
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 OrderedLatentLiabilityLikelihood likelihood = (OrderedLatentLiabilityLikelihood)
51 xo.getChild(OrderedLatentLiabilityLikelihood.class);
52
53 CompoundParameter parameter = likelihood.getTipTraitParameter();
54
55 DataType dataType = likelihood.getPatternList().getDataType();
56
57 if (!(dataType instanceof TwoStates)) {
58 throw new XMLParseException("Liability transformation is currently only implemented for binary traits");
59 }
60
61 Parameter mask = null;
62 if (xo.hasChildNamed(MaskedParameterParser.MASKING)) {
63 mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);
64 }
65
66 List<Transform> transforms = new ArrayList<Transform>();
67 int index = 0;
68 for (int tip = 0; tip < parameter.getParameterCount(); ++tip) {
69
70 final int[] tipData = likelihood.getData(tip);
71
72 for (int trait = 0; trait < tipData.length; ++trait) {
73 int discreteState = tipData[trait];
74 boolean valid = true;
75
76 Transform transform;
77 if (discreteState == 0) {
78 transform = Transform.LOG_NEGATE;
79 // transforms.add(Transform.LOG_NEGATE);
80
81 if (parameter.getParameterValue(index) >= 0.0) {
82 valid = false;
83 }
84 } else if (discreteState == 1) {
85 transform = Transform.LOG;
86 // transforms.add(Transform.LOG);
87
88 if (parameter.getParameterValue(index) <= 0.0) {
89 valid = false;
90 }
91 } else {
92 transform = Transform.NONE;
93 // transforms.add(Transform.NONE);
94 }
95
96 if (!valid) {
97 throw new XMLParseException("Incompatible binary trait and latent value in tip '" +
98 parameter.getParameter(tip).getId() + "'");
99 }
100
101 if (mask == null || mask.getParameterValue(index) == 1.0) {
102 transforms.add(transform);
103 }
104
105 ++index;
106 }
107 }
108
109 return new Transform.Array(transforms, parameter);
110 }
111
112 @Override
113 public XMLSyntaxRule[] getSyntaxRules() {
114 return new XMLSyntaxRule[] {
115 new ElementRule(OrderedLatentLiabilityLikelihood.class),
116 new ElementRule(MaskedParameterParser.MASKING,
117 new XMLSyntaxRule[]{
118 new ElementRule(Parameter.class)
119 }, true),
120 };
121 }
122
123 @Override
124 public String getParserDescription() {
125 return null;
126 }
127
128 @Override
129 public Class getReturnType() {
130 return Transform.Collection.class;
131 }
132
133 @Override
134 public String getParserName() {
135 return NAME;
136 }
137
138 }