Debian Codegraph @

src/dr/evomodelxml/treedatalikelihood/TreeDataLikelihoodParser.java

raw symbols SCIP
1 /*
2 * TreeDataLikelihoodParser.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.treedatalikelihood;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.util.Taxon;
30 import dr.evomodel.branchmodel.BranchModel;
31 import dr.evomodel.branchmodel.HomogeneousBranchModel;
32 import dr.evomodel.branchratemodel.BranchRateModel;
33 import dr.evomodel.branchratemodel.DefaultBranchRateModel;
34 import dr.evomodel.siteratemodel.GammaSiteRateModel;
35 import dr.evomodel.siteratemodel.SiteRateModel;
36 import dr.evomodel.substmodel.FrequencyModel;
37 import dr.evomodel.substmodel.SubstitutionModel;
38 import dr.evomodel.tipstatesmodel.TipStatesModel;
39 import dr.evomodel.tree.TreeModel;
40 import dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate;
41 import dr.evomodel.treedatalikelihood.DataLikelihoodDelegate;
42 import dr.evomodel.treedatalikelihood.MultiPartitionDataLikelihoodDelegate;
43 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
44 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
45 import dr.inference.model.CompoundLikelihood;
46 import dr.inference.model.Likelihood;
47 import dr.xml.*;
48
49 import java.util.ArrayList;
50 import java.util.List;
51
52 /**
53 * @author Andrew Rambaut
54 * @author Marc Suchard
55 * @version $Id$
56 */
57 public class TreeDataLikelihoodParser extends AbstractXMLObjectParser {
58
59 public static final String BEAGLE_INSTANCE_COUNT = "beagle.instance.count";
60
61 public static final String TREE_DATA_LIKELIHOOD = "treeDataLikelihood";
62 public static final String USE_AMBIGUITIES = "useAmbiguities";
63 public static final String INSTANCE_COUNT = "instanceCount";
64 public static final String SCALING_SCHEME = "scalingScheme";
65 public static final String DELAY_SCALING = "delayScaling";
66
67 public static final String PARTITION = "partition";
68
69 public String getParserName() {
70 return TREE_DATA_LIKELIHOOD;
71 }
72
73 protected Likelihood createTreeDataLikelihood(List<PatternList> patternLists,
74 List<BranchModel> branchModels,
75 List<SiteRateModel> siteRateModels,
76 TreeModel treeModel,
77 BranchRateModel branchRateModel,
78 TipStatesModel tipStatesModel,
79 boolean useAmbiguities,
80 PartialsRescalingScheme scalingScheme,
81 boolean delayRescalingUntilUnderflow) throws XMLParseException {
82
83 if (tipStatesModel != null) {
84 throw new XMLParseException("Tip State Error models are not supported yet with TreeDataLikelihood");
85 }
86
87 List<Taxon> treeTaxa = treeModel.asList();
88 List<Taxon> patternTaxa = patternLists.get(0).asList();
89
90 if (!patternTaxa.containsAll(treeTaxa)) {
91 throw new XMLParseException("TreeModel contains more taxa than the partition pattern list.");
92 }
93
94 if (!treeTaxa.containsAll(patternTaxa)) {
95 throw new XMLParseException("TreeModel contains fewer taxa than the partition pattern list.");
96 }
97
98 boolean useBeagle3MultiPartition = false;
99
100 if (patternLists.size() > 1) {
101 // will currently recommend true if using GPU, CUDA or OpenCL.
102 useBeagle3MultiPartition = MultiPartitionDataLikelihoodDelegate.IS_MULTI_PARTITION_RECOMMENDED();
103
104 if (System.getProperty("USE_BEAGLE3_EXTENSIONS") != null) {
105 useBeagle3MultiPartition = Boolean.parseBoolean(System.getProperty("USE_BEAGLE3_EXTENSIONS"));
106 }
107
108 if (System.getProperty("beagle.multipartition.extensions") != null &&
109 !System.getProperty("beagle.multipartition.extensions").equals("auto")) {
110 useBeagle3MultiPartition = Boolean.parseBoolean(System.getProperty("beagle.multipartition.extensions"));
111 }
112 }
113
114 boolean useJava = Boolean.parseBoolean(System.getProperty("java.only", "false"));
115
116 if ( useBeagle3MultiPartition && !useJava) {
117 try {
118 DataLikelihoodDelegate dataLikelihoodDelegate = new MultiPartitionDataLikelihoodDelegate(
119 treeModel,
120 patternLists,
121 branchModels,
122 siteRateModels,
123 useAmbiguities,
124 scalingScheme,
125 delayRescalingUntilUnderflow);
126
127 return new TreeDataLikelihood(
128 dataLikelihoodDelegate,
129 treeModel,
130 branchRateModel);
131 } catch (DataLikelihoodDelegate.DelegateTypeException dte) {
132 useBeagle3MultiPartition = false;
133 }
134
135 }
136
137 // The multipartition data likelihood isn't available so make a set of single partition data likelihoods
138 List<Likelihood> treeDataLikelihoods = new ArrayList<Likelihood>();
139
140 for (int i = 0; i < patternLists.size(); i++) {
141
142 DataLikelihoodDelegate dataLikelihoodDelegate = new BeagleDataLikelihoodDelegate(
143 treeModel,
144 patternLists.get(i),
145 branchModels.get(i),
146 siteRateModels.get(i),
147 useAmbiguities,
148 scalingScheme,
149 delayRescalingUntilUnderflow);
150
151 treeDataLikelihoods.add(
152 new TreeDataLikelihood(
153 dataLikelihoodDelegate,
154 treeModel,
155 branchRateModel));
156
157 }
158
159 if (treeDataLikelihoods.size() == 1) {
160 return treeDataLikelihoods.get(0);
161 }
162
163 return new CompoundLikelihood(treeDataLikelihoods);
164
165 }
166
167 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
168
169 boolean useAmbiguities = xo.getAttribute(USE_AMBIGUITIES, false);
170
171 // TreeDataLikelihood doesn't currently support Instances defined from the command line
172 // int instanceCount = xo.getAttribute(INSTANCE_COUNT, 1);
173 // if (instanceCount < 1) {
174 // instanceCount = 1;
175 // }
176 //
177 // String ic = System.getProperty(BEAGLE_INSTANCE_COUNT);
178 // if (ic != null && ic.length() > 0) {
179 // instanceCount = Integer.parseInt(ic);
180 // }
181
182 List<PatternList> patternLists = new ArrayList<PatternList>();
183 List<SiteRateModel> siteRateModels = new ArrayList<SiteRateModel>();
184 List<BranchModel> branchModels = new ArrayList<BranchModel>();
185
186 boolean hasSinglePartition = false;
187
188 PatternList patternList = (PatternList)xo.getChild(PatternList.class);
189 if (patternList != null) {
190 hasSinglePartition = true;
191 patternLists.add(patternList);
192
193 GammaSiteRateModel siteRateModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
194 siteRateModels.add(siteRateModel);
195
196 FrequencyModel rootFreqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
197
198 BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class);
199 if (branchModel == null) {
200 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
201 if (substitutionModel == null) {
202 substitutionModel = siteRateModel.getSubstitutionModel();
203 }
204 if (substitutionModel == null) {
205 throw new XMLParseException("No substitution model available for partition in DataTreeLikelihood: "+xo.getId());
206 }
207 branchModel = new HomogeneousBranchModel(substitutionModel, rootFreqModel);
208 }
209 branchModels.add(branchModel);
210 }
211
212 int k = 0;
213 for (int i = 0; i < xo.getChildCount(); i++) {
214 if (xo.getChildName(i).equals(PARTITION)) {
215 if (hasSinglePartition) {
216 throw new XMLParseException("Either a single set of patterns should be given or multiple 'partitions' elements within DataTreeLikelihood: "+xo.getId());
217 }
218 k += 1;
219
220 XMLObject cxo = (XMLObject)xo.getChild(i);
221 patternList = (PatternList) cxo.getChild(PatternList.class);
222 patternLists.add(patternList);
223
224 GammaSiteRateModel siteRateModel = (GammaSiteRateModel) cxo.getChild(GammaSiteRateModel.class);
225 siteRateModels.add(siteRateModel);
226
227 FrequencyModel rootFreqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
228
229 BranchModel branchModel = (BranchModel) cxo.getChild(BranchModel.class);
230 if (branchModel == null) {
231 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
232 if (substitutionModel == null) {
233 substitutionModel = siteRateModel.getSubstitutionModel();
234 }
235 if (substitutionModel == null) {
236 throw new XMLParseException("No substitution model available for partition " + k + " in DataTreeLikelihood: "+xo.getId());
237 }
238 branchModel = new HomogeneousBranchModel(substitutionModel, rootFreqModel);
239 }
240 branchModels.add(branchModel);
241
242 BranchRateModel branchRateModel = (BranchRateModel) cxo.getChild(BranchRateModel.class);
243 if (branchRateModel != null) {
244 throw new XMLParseException("Partitions are not currently allowed their own BranchRateModel in TreeDataLikelihood object '"+xo.getId());
245 }
246 }
247 }
248
249 if (patternLists.size() == 0) {
250 throw new XMLParseException("Either a single set of patterns should be given or multiple 'partitions' elements within DataTreeLikelihood: "+xo.getId());
251 }
252
253 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
254
255 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
256 if (branchRateModel == null) {
257 throw new XMLParseException("BranchRateModel missing from TreeDataLikelihood object '"+xo.getId());
258 // branchRateModel = new DefaultBranchRateModel();
259 }
260
261 TipStatesModel tipStatesModel = (TipStatesModel) xo.getChild(TipStatesModel.class);
262
263 PartialsRescalingScheme scalingScheme = PartialsRescalingScheme.DEFAULT;
264 boolean delayScaling = true;
265 if (xo.hasAttribute(SCALING_SCHEME)) {
266 scalingScheme = PartialsRescalingScheme.parseFromString(xo.getStringAttribute(SCALING_SCHEME));
267 if (scalingScheme == null)
268 throw new XMLParseException("Unknown scaling scheme '"+xo.getStringAttribute(SCALING_SCHEME)+"' in "+
269 "TreeDataLikelihood object '"+xo.getId());
270
271 }
272 if (xo.hasAttribute(DELAY_SCALING)) {
273 delayScaling = xo.getBooleanAttribute(DELAY_SCALING);
274 }
275
276 if (tipStatesModel != null) {
277 throw new XMLParseException("BEAGLE_INSTANCES option cannot be used with a TipStateModel (i.e., a sequence error model).");
278 }
279
280 return createTreeDataLikelihood(
281 patternLists,
282 branchModels,
283 siteRateModels,
284 treeModel,
285 branchRateModel,
286 null,
287 useAmbiguities,
288 scalingScheme,
289 delayScaling);
290 }
291
292 //************************************************************************
293 // AbstractXMLObjectParser implementation
294 //************************************************************************
295
296 public String getParserDescription() {
297 return "This element represents the likelihood of data on a tree given the site model.";
298 }
299
300 public Class getReturnType() {
301 return Likelihood.class;
302 }
303
304 public static final XMLSyntaxRule[] rules = {
305 AttributeRule.newBooleanRule(USE_AMBIGUITIES, true),
306 AttributeRule.newStringRule(SCALING_SCHEME,true),
307
308 // really it should be this set of elements or the PARTITION elements
309 new OrRule(new AndRule(new XMLSyntaxRule[]{
310 new ElementRule(PatternList.class, true),
311 new ElementRule(SiteRateModel.class, true),
312 new ElementRule(FrequencyModel.class, true),
313 new ElementRule(BranchModel.class, true)})
314 ,
315 new ElementRule(PARTITION, new XMLSyntaxRule[] {
316 new ElementRule(PatternList.class),
317 new ElementRule(SiteRateModel.class),
318 new ElementRule(FrequencyModel.class, true),
319 new ElementRule(BranchModel.class, true)
320 }, 1, Integer.MAX_VALUE)),
321
322 new ElementRule(BranchRateModel.class, true),
323 new ElementRule(TreeModel.class),
324 new ElementRule(TipStatesModel.class, true)
325 };
326
327 public XMLSyntaxRule[] getSyntaxRules() {
328 return rules;
329 }
330 }