Debian Codegraph @

src/dr/evomodelxml/treelikelihood/BalancedBeagleTreeLikelihoodParser.java

raw symbols SCIP
1 /*
2 * BalancedBeagleTreeLikelihoodParser.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.evolution.tree.TreeUtils;
29 import dr.evomodel.branchmodel.BranchModel;
30 import dr.evomodel.branchmodel.HomogeneousBranchModel;
31 import dr.evomodel.siteratemodel.GammaSiteRateModel;
32 import dr.evomodel.substmodel.FrequencyModel;
33 import dr.evomodel.substmodel.SubstitutionModel;
34 import dr.evomodel.treelikelihood.AbstractTreeLikelihood;
35 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
36 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
37 import dr.evolution.alignment.PatternList;
38 import dr.evolution.alignment.Patterns;
39 import dr.evolution.alignment.SitePatterns;
40 import dr.evolution.util.TaxonList;
41 import dr.evomodel.branchratemodel.BranchRateModel;
42 import dr.evomodel.tree.TreeModel;
43 import dr.evomodel.tipstatesmodel.TipStatesModel;
44 import dr.inference.model.CompoundLikelihood;
45 import dr.inference.model.Likelihood;
46 import dr.inference.model.Parameter;
47 import dr.xml.*;
48
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Set;
53
54 /**
55 * @author Guy Baele
56 */
57 public class BalancedBeagleTreeLikelihoodParser extends AbstractXMLObjectParser {
58
59 //public static final String BEAGLE_INSTANCE_COUNT = "beagle.instance.count";
60
61 public static final String TREE_LIKELIHOOD = "balancedTreeLikelihood";
62 public static final String INSTANCE_COUNT = "instanceCount";
63 public static final String PARTIALS_RESTRICTION = "partialsRestriction";
64
65 public final int TEST_RUNS = 100;
66 public final double TEST_CUTOFF = 1.30;
67
68 public String getParserName() {
69 return TREE_LIKELIHOOD;
70 }
71
72 protected BeagleTreeLikelihood createTreeLikelihood(PatternList patternList, TreeModel treeModel,
73 BranchModel branchModel,
74 GammaSiteRateModel siteRateModel,
75 BranchRateModel branchRateModel,
76 TipStatesModel tipStatesModel,
77 boolean useAmbiguities, PartialsRescalingScheme scalingScheme,
78 boolean delayScaling,
79 Map<Set<String>, Parameter> partialsRestrictions,
80 XMLObject xo) throws XMLParseException {
81 return new BeagleTreeLikelihood(
82 patternList,
83 treeModel,
84 branchModel,
85 siteRateModel,
86 branchRateModel,
87 tipStatesModel,
88 useAmbiguities,
89 scalingScheme,
90 delayScaling,
91 partialsRestrictions
92 );
93 }
94
95 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
96
97 boolean useAmbiguities = xo.getAttribute(BeagleTreeLikelihoodParser.USE_AMBIGUITIES, false);
98 /*int instanceCount = xo.getAttribute(INSTANCE_COUNT, 1);
99 if (instanceCount < 1) {
100 instanceCount = 1;
101 }
102
103 String ic = System.getProperty(BEAGLE_INSTANCE_COUNT);
104 if (ic != null && ic.length() > 0) {
105 instanceCount = Integer.parseInt(ic);
106 }*/
107
108 PatternList patternList = (PatternList) xo.getChild(PatternList.class);
109 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
110 GammaSiteRateModel siteRateModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
111
112 FrequencyModel rootFreqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
113
114 BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class);
115 if (branchModel == null) {
116 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
117 if (substitutionModel == null) {
118 substitutionModel = siteRateModel.getSubstitutionModel();
119 }
120 if (substitutionModel == null) {
121 throw new XMLParseException("No substitution model available for TreeLikelihood: "+xo.getId());
122 }
123 branchModel = new HomogeneousBranchModel(substitutionModel, rootFreqModel);
124 }
125
126 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
127
128 TipStatesModel tipStatesModel = (TipStatesModel) xo.getChild(TipStatesModel.class);
129 // if (xo.getChild(TipStatesModel.class) != null) {
130 // throw new XMLParseException("Sequence Error Models are not supported under BEAGLE yet. Please use Native BEAST Likelihood.");
131 // }
132
133 PartialsRescalingScheme scalingScheme = PartialsRescalingScheme.DEFAULT;
134 if (xo.hasAttribute(BeagleTreeLikelihoodParser.SCALING_SCHEME)) {
135 // scalingScheme = PartialsRescalingScheme.parseFromString(xo.getStringAttribute(BeagleTreeLikelihoodParser.SCALING_SCHEME));
136 if (scalingScheme == null)
137 throw new XMLParseException("Unknown scaling scheme '"+xo.getStringAttribute(BeagleTreeLikelihoodParser.SCALING_SCHEME)+"' in "+
138 "OldBeagleTreeLikelihood object '"+xo.getId());
139
140 }
141
142 boolean delayScaling = true;
143 Map<Set<String>, Parameter> partialsRestrictions = null;
144
145 if (xo.hasChildNamed(PARTIALS_RESTRICTION)) {
146 XMLObject cxo = xo.getChild(PARTIALS_RESTRICTION);
147 TaxonList taxonList = (TaxonList) cxo.getChild(TaxonList.class);
148 // Parameter parameter = (Parameter) cxo.getChild(Parameter.class);
149 try {
150 TreeUtils.getLeavesForTaxa(treeModel, taxonList);
151 } catch (TreeUtils.MissingTaxonException e) {
152 throw new XMLParseException("Unable to parse taxon list: " + e.getMessage());
153 }
154 throw new XMLParseException("Restricting internal nodes is not yet implemented. Contact Marc");
155
156 }
157
158 /*if (instanceCount == 1 || patternList.getPatternCount() < instanceCount) {
159 return createTreeLikelihood(
160 patternList,
161 treeModel,
162 branchModel,
163 siteRateModel,
164 branchRateModel,
165 tipStatesModel,
166 useAmbiguities,
167 scalingScheme,
168 partialsRestrictions,
169 xo
170 );
171 }*/
172
173 //first run a test for instanceCount == 1
174 System.err.println("\nTesting instanceCount == 1");
175 Likelihood baseLikelihood = createTreeLikelihood(
176 patternList,
177 treeModel,
178 branchModel,
179 siteRateModel,
180 branchRateModel,
181 tipStatesModel,
182 useAmbiguities,
183 scalingScheme,
184 delayScaling,
185 partialsRestrictions,
186 xo
187 );
188 double start = System.nanoTime();
189 for (int i = 0; i < TEST_RUNS; i++) {
190 baseLikelihood.makeDirty();
191 baseLikelihood.getLogLikelihood();
192 }
193 double end = System.nanoTime();
194 double baseResult = end - start;
195 System.err.println("Evaluation took: " + baseResult);
196
197 // using multiple instances of BEAGLE...
198
199 if (!(patternList instanceof SitePatterns)) {
200 throw new XMLParseException("BEAGLE_INSTANCES option cannot be used with BEAUti-selected codon partitioning.");
201 }
202
203 if (tipStatesModel != null) {
204 throw new XMLParseException("BEAGLE_INSTANCES option cannot be used with a TipStateModel (i.e., a sequence error model).");
205 }
206
207 //List<Likelihood> likelihoods = new ArrayList<Likelihood>();
208 List<Likelihood> likelihoods = null;
209 CompoundLikelihood compound = null;
210
211 int instanceCount = 2;
212 boolean optimal = false;
213
214 while (optimal == false) {
215
216 System.err.println("\nCreating instanceCount == " + instanceCount);
217 likelihoods = new ArrayList<Likelihood>();
218
219 for (int i = 0; i < instanceCount; i++) {
220 Patterns subPatterns = new Patterns((SitePatterns)patternList, 0, 0, 1, i, instanceCount);
221
222 AbstractTreeLikelihood treeLikelihood = createTreeLikelihood(
223 subPatterns,
224 treeModel,
225 branchModel,
226 siteRateModel,
227 branchRateModel,
228 null,
229 useAmbiguities,
230 scalingScheme,
231 delayScaling,
232 partialsRestrictions,
233 xo);
234 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
235 likelihoods.add(treeLikelihood);
236 }
237
238 //construct compoundLikelihood
239 compound = new CompoundLikelihood(instanceCount, likelihoods);
240
241 //test timings
242 System.err.println("\nTesting instanceCount == " + instanceCount);
243 start = System.nanoTime();
244 for (int i = 0; i < TEST_RUNS; i++) {
245 compound.makeDirty();
246 compound.getLogLikelihood();
247 }
248 end = System.nanoTime();
249 double newResult = end - start;
250 System.err.println("Evaluation took: " + newResult);
251
252 if (baseResult/newResult > TEST_CUTOFF) {
253
254 instanceCount++;
255 baseResult = newResult;
256
257 } else {
258
259 optimal = true;
260 instanceCount--;
261
262 System.err.println("\nCreating final BeagleTreeLikelihood with instanceCount: " + instanceCount);
263
264 likelihoods = new ArrayList<Likelihood>();
265
266 for (int i = 0; i < instanceCount; i++) {
267 Patterns subPatterns = new Patterns((SitePatterns)patternList, 0, 0, 1, i, instanceCount);
268
269 AbstractTreeLikelihood treeLikelihood = createTreeLikelihood(
270 subPatterns,
271 treeModel,
272 branchModel,
273 siteRateModel,
274 branchRateModel,
275 null,
276 useAmbiguities,
277 scalingScheme,
278 delayScaling,
279 partialsRestrictions,
280 xo);
281 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
282 likelihoods.add(treeLikelihood);
283 }
284
285 //construct compoundLikelihood
286 compound = new CompoundLikelihood(instanceCount, likelihoods);
287
288 }
289
290 }
291
292 return compound;
293
294 /*for (int i = 0; i < instanceCount; i++) {
295
296 Patterns subPatterns = new Patterns((SitePatterns)patternList, 0, 0, 1, i, instanceCount);
297
298 AbstractTreeLikelihood treeLikelihood = createTreeLikelihood(
299 subPatterns,
300 treeModel,
301 branchModel,
302 siteRateModel,
303 branchRateModel,
304 null,
305 useAmbiguities,
306 scalingScheme,
307 partialsRestrictions,
308 xo);
309 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
310 likelihoods.add(treeLikelihood);
311 }
312
313 return new CompoundLikelihood(likelihoods);*/
314 }
315
316 //************************************************************************
317 // AbstractXMLObjectParser implementation
318 //************************************************************************
319
320 public String getParserDescription() {
321 return "This element represents the likelihood of a patternlist on a tree given the site model, with an automated detection of instanceCount.";
322 }
323
324 public Class getReturnType() {
325 return Likelihood.class;
326 }
327
328 public static final XMLSyntaxRule[] rules = {
329 AttributeRule.newBooleanRule(BeagleTreeLikelihoodParser.USE_AMBIGUITIES, true),
330 new ElementRule(PatternList.class),
331 new ElementRule(TreeModel.class),
332 new ElementRule(GammaSiteRateModel.class),
333 new ElementRule(BranchModel.class, true),
334 new ElementRule(SubstitutionModel.class, true),
335 new ElementRule(BranchRateModel.class, true),
336 new ElementRule(TipStatesModel.class, true),
337 AttributeRule.newStringRule(BeagleTreeLikelihoodParser.SCALING_SCHEME,true),
338 AttributeRule.newBooleanRule(BeagleTreeLikelihoodParser.DELAY_SCALING,true),
339 new ElementRule(PARTIALS_RESTRICTION, new XMLSyntaxRule[] {
340 new ElementRule(TaxonList.class),
341 new ElementRule(Parameter.class),
342 }, true),
343 new ElementRule(TipStatesModel.class, true),
344 new ElementRule(FrequencyModel.class, true),
345 };
346
347 public XMLSyntaxRule[] getSyntaxRules() {
348 return rules;
349 }
350 }