Debian Codegraph @

src/dr/evomodelxml/treelikelihood/MarkovJumpsTreeLikelihoodParser.java

raw symbols SCIP
1 /*
2 * MarkovJumpsTreeLikelihoodParser.java
3 *
4 * Copyright (c) 2002-2016 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.treelikelihood;
27
28 import dr.evomodel.branchmodel.BranchModel;
29 import dr.evomodel.siteratemodel.GammaSiteRateModel;
30 import dr.evomodel.substmodel.FrequencyModel;
31 import dr.evomodel.substmodel.SubstitutionModel;
32 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
33 import dr.evomodel.treelikelihood.MarkovJumpsBeagleTreeLikelihood;
34 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
35 import dr.evolution.alignment.PatternList;
36 import dr.evolution.datatype.DataType;
37 import dr.evolution.util.TaxonList;
38 import dr.evomodel.branchratemodel.BranchRateModel;
39 import dr.evomodel.tree.TreeModel;
40 import dr.evomodel.tipstatesmodel.TipStatesModel;
41 import dr.inference.markovjumps.MarkovJumpsRegisterAcceptor;
42 import dr.inference.markovjumps.MarkovJumpsType;
43 import dr.inference.model.Parameter;
44 import dr.xml.*;
45
46 import java.util.Map;
47 import java.util.Set;
48
49 /**
50 * @author Marc Suchard
51 */
52
53 public class MarkovJumpsTreeLikelihoodParser extends AncestralStateTreeLikelihoodParser {
54
55 public static final String MARKOV_JUMP_TREE_LIKELIHOOD = "markovJumpsTreeLikelihood";
56 public static final String JUMP_TAG = "jumps";
57 public static final String JUMP_TAG_NAME = "jumpTagName";
58 public static final String COUNTS = MarkovJumpsType.COUNTS.getText();
59 public static final String REWARDS = MarkovJumpsType.REWARDS.getText();
60 public static final String SCALE_REWARDS = "scaleRewardsByTime";
61 public static final String USE_UNIFORMIZATION = "useUniformization";
62 public static final String SAVE_HISTORY = "saveCompleteHistory";
63 public static final String LOG_HISTORY = "logCompleteHistory";
64 public static final String COMPACT_HISTORY = "compactHistory";
65 public static final String NUMBER_OF_SIMULANTS = "numberOfSimulants";
66 public static final String REPORT_UNCONDITIONED_COLUMNS = "reportUnconditionedValues";
67
68
69 public String getParserName() {
70 return MARKOV_JUMP_TREE_LIKELIHOOD;
71 }
72
73 protected BeagleTreeLikelihood createTreeLikelihood(PatternList patternList, TreeModel treeModel,
74 BranchModel branchModel,
75 GammaSiteRateModel siteRateModel,
76 BranchRateModel branchRateModel,
77 TipStatesModel tipStatesModel,
78 boolean useAmbiguities, PartialsRescalingScheme scalingScheme,
79 boolean delayScaling,
80 Map<Set<String>, Parameter> partialsRestrictions,
81 XMLObject xo) throws XMLParseException {
82
83 DataType dataType = branchModel.getRootSubstitutionModel().getDataType();
84
85 String stateTag = xo.getAttribute(RECONSTRUCTION_TAG_NAME,RECONSTRUCTION_TAG);
86 String jumpTag = xo.getAttribute(JUMP_TAG_NAME, JUMP_TAG);
87
88 boolean scaleRewards = xo.getAttribute(SCALE_REWARDS,true);
89
90 boolean useMAP = xo.getAttribute(MAP_RECONSTRUCTION, false);
91 boolean useMarginalLogLikelihood = xo.getAttribute(MARGINAL_LIKELIHOOD, true);
92
93 boolean useUniformization = xo.getAttribute(USE_UNIFORMIZATION, false);
94 boolean reportUnconditionedColumns = xo.getAttribute(REPORT_UNCONDITIONED_COLUMNS, false);
95 int nSimulants = xo.getAttribute(NUMBER_OF_SIMULANTS, 1);
96
97 if (patternList.areUnique()) {
98 throw new XMLParseException("Markov Jumps reconstruction cannot be used with compressed (unique) patterns.");
99 }
100
101 MarkovJumpsBeagleTreeLikelihood treeLikelihood = new MarkovJumpsBeagleTreeLikelihood(
102 patternList,
103 treeModel,
104 branchModel,
105 siteRateModel,
106 branchRateModel,
107 tipStatesModel,
108 useAmbiguities,
109 scalingScheme,
110 delayScaling,
111 partialsRestrictions,
112 dataType,
113 stateTag,
114 useMAP,
115 useMarginalLogLikelihood,
116 useUniformization,
117 reportUnconditionedColumns,
118 nSimulants
119 );
120
121 int registersFound = parseAllChildren(xo, treeLikelihood, dataType.getStateCount(), jumpTag,
122 MarkovJumpsType.COUNTS, false); // For backwards compatibility
123
124 XMLObject cxo = xo.getChild(COUNTS);
125 if (cxo != null) {
126 registersFound += parseAllChildren(cxo, treeLikelihood, dataType.getStateCount(), jumpTag,
127 MarkovJumpsType.COUNTS, false);
128 }
129
130 cxo = xo.getChild(REWARDS);
131 if (cxo != null) {
132 registersFound += parseAllChildren(cxo, treeLikelihood, dataType.getStateCount(), jumpTag,
133 MarkovJumpsType.REWARDS, scaleRewards);
134 }
135
136 if (registersFound == 0) { // Some default values for testing
137 // double[] registration = new double[dataType.getStateCount()*dataType.getStateCount()];
138 // MarkovJumpsCore.fillRegistrationMatrix(registration,dataType.getStateCount()); // Count all transitions
139 // Parameter registerParameter = new Parameter.Default(registration);
140 // registerParameter.setId(jumpTag);
141 // treeLikelihood.addRegister(registerParameter,
142 // MarkovJumpsType.COUNTS,
143 // false);
144 // Do nothing, should run the same as AncestralStateBeagleTreeLikelihood
145 }
146
147 boolean saveCompleteHistory = xo.getAttribute(SAVE_HISTORY, false);
148 if (saveCompleteHistory) {
149 Parameter allCounts = new Parameter.Default(dataType.getStateCount() * dataType.getStateCount());
150 for (int i = 0; i < dataType.getStateCount(); ++i) {
151 for (int j = 0; j < dataType.getStateCount(); ++j) {
152 if (j == i) {
153 allCounts.setParameterValue(i * dataType.getStateCount() + j, 0.0);
154 } else {
155 allCounts.setParameterValue(i * dataType.getStateCount() + j, 1.0);
156 }
157 }
158 }
159 allCounts.setId(MarkovJumpsBeagleTreeLikelihood.TOTAL_COUNTS);
160 treeLikelihood.setLogHistories(xo.getAttribute(LOG_HISTORY, false));
161 treeLikelihood.setUseCompactHistory(xo.getAttribute(COMPACT_HISTORY, false));
162 treeLikelihood.addRegister(allCounts, MarkovJumpsType.HISTORY, false);
163 }
164
165 return treeLikelihood;
166 }
167
168 public static int parseAllChildren(XMLObject xo,
169 MarkovJumpsRegisterAcceptor acceptor,
170 int stateCount,
171 String jumpTag,
172 MarkovJumpsType type,
173 boolean scaleRewards) throws XMLParseException {
174 int registersFound = 0;
175 for(int i = 0; i < xo.getChildCount(); i++) {
176 Object obj = xo.getChild(i);
177 if (obj instanceof Parameter) {
178 Parameter registerParameter = (Parameter) obj;
179 if (type == MarkovJumpsType.COUNTS &&
180 registerParameter.getDimension() != stateCount * stateCount) {
181 if (registerParameter. getDimension() == 1) {
182 // if the dimension hasn't been set then default to counting all jumps
183 registerParameter.setDimension(stateCount * stateCount);
184 for (int j = 0; j < stateCount; j++) {
185 for (int k = 0; k < stateCount; k++) {
186 registerParameter.setParameterValueQuietly((j * stateCount) + k, (j == k ? 0.0 : 1.0));
187 }
188 }
189 } else {
190 throw new XMLParseException("Markov Jumps register parameter " + registerParameter.getId() + " is of the wrong dimension");
191 }
192 }
193 if (type == MarkovJumpsType.REWARDS &&
194 registerParameter.getDimension() != stateCount) {
195 if (registerParameter.getDimension() == 1) {
196 // if the dimension hasn't been set then default to getting rewards for all states
197 registerParameter.setDimension(stateCount);
198 for (int j = 0; j < stateCount; j++) {
199 registerParameter.setParameterValueQuietly(j, 1.0);
200 }
201 } else {
202 throw new XMLParseException("Markov Rewards register parameter " + registerParameter.getId() + " is of the wrong dimension");
203 }
204 }
205
206 if (registerParameter.getId() == null) {
207 registerParameter.setId(jumpTag+(registersFound+1));
208 }
209 acceptor.addRegister(registerParameter, type, scaleRewards);
210 registersFound++;
211 }
212 }
213 return registersFound;
214 }
215
216 public static XMLSyntaxRule[] rules =
217 new XMLSyntaxRule[] {
218 AttributeRule.newBooleanRule(BeagleTreeLikelihoodParser.USE_AMBIGUITIES, true),
219 AttributeRule.newStringRule(RECONSTRUCTION_TAG_NAME, true),
220 AttributeRule.newStringRule(JUMP_TAG_NAME, true),
221 AttributeRule.newBooleanRule(SCALE_REWARDS,true),
222 AttributeRule.newBooleanRule(USE_UNIFORMIZATION,true),
223 AttributeRule.newBooleanRule(REPORT_UNCONDITIONED_COLUMNS, true),
224 AttributeRule.newIntegerRule(NUMBER_OF_SIMULANTS,true),
225 AttributeRule.newBooleanRule(SAVE_HISTORY, true),
226 AttributeRule.newBooleanRule(LOG_HISTORY, true),
227 AttributeRule.newBooleanRule(COMPACT_HISTORY, true),
228 new ElementRule(PARTIALS_RESTRICTION, new XMLSyntaxRule[] {
229 new ElementRule(TaxonList.class),
230 new ElementRule(Parameter.class),
231 }, true),
232 new ElementRule(PatternList.class),
233 new ElementRule(TreeModel.class),
234 new ElementRule(GammaSiteRateModel.class),
235 new ElementRule(BranchModel.class, true),
236 new ElementRule(SubstitutionModel.class, true),
237 new ElementRule(BranchRateModel.class, true),
238 AttributeRule.newStringRule(BeagleTreeLikelihoodParser.SCALING_SCHEME, true),
239 new ElementRule(Parameter.class,0,Integer.MAX_VALUE), // For backwards compatibility
240 new ElementRule(COUNTS,
241 new XMLSyntaxRule[] {
242 new ElementRule(Parameter.class,0,Integer.MAX_VALUE)
243 },true),
244 new ElementRule(REWARDS,
245 new XMLSyntaxRule[] {
246 new ElementRule(Parameter.class,0,Integer.MAX_VALUE)
247 },true),
248 new ElementRule(FrequencyModel.class, true),
249 };
250
251 public XMLSyntaxRule[] getSyntaxRules() {
252 return rules;
253 }
254 }