Debian Codegraph @

src/dr/evomodelxml/treedatalikelihood/MultiPartitionDataLikelihoodParser.java

raw symbols SCIP
1 /*
2 * MultiPartitionDataLikelihoodParser.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.alignment.Patterns;
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.DataLikelihoodDelegate;
41 import dr.evomodel.treedatalikelihood.MultiPartitionDataLikelihoodDelegate;
42 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
43 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
44 import dr.xml.*;
45
46 import java.util.ArrayList;
47 import java.util.List;
48
49 /**
50 * @author Guy Baele
51 * @version $Id$
52 */
53 public class MultiPartitionDataLikelihoodParser extends AbstractXMLObjectParser {
54
55 public static final boolean DEBUG = false;
56
57 public static final String BEAGLE_INSTANCE_COUNT = "beagle.instance.count";
58
59 //TODO: eventually change String to parse back to treeDataLikelihood
60 public static final String TREE_DATA_LIKELIHOOD = "newTreeDataLikelihood";
61 public static final String USE_AMBIGUITIES = "useAmbiguities";
62 public static final String INSTANCE_COUNT = "instanceCount";
63 public static final String SCALING_SCHEME = "scalingScheme";
64 public static final String DELAY_SCALING = "delayScaling";
65
66 public String getParserName() {
67 return TREE_DATA_LIKELIHOOD;
68 }
69
70 protected TreeDataLikelihood createTreeDataLikelihood(List<PatternList> patternLists,
71 TreeModel treeModel,
72 List<BranchModel> branchModels,
73 List<SiteRateModel> siteRateModels,
74 BranchRateModel branchRateModel,
75 TipStatesModel tipStatesModel,
76 boolean useAmbiguities,
77 PartialsRescalingScheme scalingScheme,
78 boolean delayRescalingUntilUnderflow,
79 XMLObject xo) throws XMLParseException {
80
81 if (tipStatesModel != null) {
82 throw new XMLParseException("Tip State Error models are not supported yet with TreeDataLikelihood");
83 }
84
85 // DataLikelihoodDelegate dataLikelihoodDelegate = new BeagleDataLikelihoodDelegate(
86 // treeModel,
87 // patternLists.get(0),
88 // branchModel,
89 // siteRateModel,
90 // useAmbiguities,
91 // scalingScheme,
92 // delayRescalingUntilUnderflow);
93
94 try {
95 DataLikelihoodDelegate dataLikelihoodDelegate = new MultiPartitionDataLikelihoodDelegate(
96 treeModel,
97 patternLists,
98 branchModels,
99 siteRateModels,
100 useAmbiguities,
101 scalingScheme,
102 delayRescalingUntilUnderflow);
103
104 return new TreeDataLikelihood(
105 dataLikelihoodDelegate,
106 treeModel,
107 branchRateModel);
108 } catch (DataLikelihoodDelegate.DelegateTypeException dte) {
109 throw new XMLParseException("Failed to create multiPartitionDataLikelihoodDelegate instance (wrong resource type or no partitions, needs to be CUDA or OpenCL device with multiple partitions)");
110 }
111
112 }
113
114 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
115
116 boolean useAmbiguities = xo.getAttribute(USE_AMBIGUITIES, false);
117 int instanceCount = xo.getAttribute(INSTANCE_COUNT, 1);
118 if (instanceCount < 1) {
119 instanceCount = 1;
120 }
121
122 String ic = System.getProperty(BEAGLE_INSTANCE_COUNT);
123 if (ic != null && ic.length() > 0) {
124 instanceCount = Integer.parseInt(ic);
125 }
126
127 if (DEBUG) {
128 System.out.println("instanceCount: " + instanceCount);
129 }
130
131 List<PatternList> patternLists = xo.getAllChildren(PatternList.class);
132
133 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
134
135 List<SiteRateModel> siteRateModels = xo.getAllChildren(SiteRateModel.class);
136
137 FrequencyModel rootFreqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
138
139 List<BranchModel> branchModels = xo.getAllChildren(BranchModel.class);
140 if (branchModels.size() == 0) {
141 if (DEBUG) {
142 System.out.println("branchModels == null");
143 }
144 branchModels = new ArrayList<BranchModel>();
145 List<SubstitutionModel> substitutionModels = xo.getAllChildren(SubstitutionModel.class);
146 if (substitutionModels.size() == 0) {
147 if (DEBUG) {
148 System.out.println("substitutionModels == null");
149 }
150 for (SiteRateModel siteRateModel : siteRateModels) {
151 SubstitutionModel substitutionModel = ((GammaSiteRateModel)siteRateModel).getSubstitutionModel();
152 if (substitutionModel == null) {
153 throw new XMLParseException("No substitution model available for TreeDataLikelihood: "+xo.getId());
154 }
155 branchModels.add(new HomogeneousBranchModel(substitutionModel, rootFreqModel));
156 }
157 }
158 if (DEBUG) {
159 System.out.println("branchModels size: " + branchModels.size());
160 }
161 for (BranchModel branchModel : branchModels) {
162 System.out.println(" " + branchModel.getId() + " " + branchModel.getModelName());
163 }
164 }
165
166 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
167 if (branchRateModel == null) {
168 branchRateModel = new DefaultBranchRateModel();
169 }
170
171 if (DEBUG) {
172 System.out.println("BranchRateModel: " + branchRateModel.getId());
173 }
174
175 TipStatesModel tipStatesModel = (TipStatesModel) xo.getChild(TipStatesModel.class);
176
177 PartialsRescalingScheme scalingScheme = PartialsRescalingScheme.DEFAULT;
178 boolean delayScaling = true;
179 if (xo.hasAttribute(SCALING_SCHEME)) {
180 scalingScheme = PartialsRescalingScheme.parseFromString(xo.getStringAttribute(SCALING_SCHEME));
181 if (scalingScheme == null)
182 throw new XMLParseException("Unknown scaling scheme '"+xo.getStringAttribute(SCALING_SCHEME)+"' in "+
183 "OldBeagleTreeLikelihood object '"+xo.getId());
184
185 }
186 if (xo.hasAttribute(DELAY_SCALING)) {
187 delayScaling = xo.getBooleanAttribute(DELAY_SCALING);
188 }
189
190 if (instanceCount == 1) {
191 if (DEBUG) {
192 System.out.println("instanceCount == 1");
193 }
194 return createTreeDataLikelihood(
195 patternLists,
196 treeModel,
197 branchModels,
198 siteRateModels,
199 branchRateModel,
200 tipStatesModel,
201 useAmbiguities,
202 scalingScheme,
203 delayScaling,
204 xo
205 );
206 }
207
208 // using multiple instances of BEAGLE...
209
210 if (tipStatesModel != null) {
211 throw new XMLParseException("BEAGLE_INSTANCES option cannot be used with a TipStateModel (i.e., a sequence error model).");
212 }
213
214 List<PatternList> patternInstanceLists = new ArrayList<PatternList>();
215 for (int j = 0; j < patternLists.size(); j++) {
216 for (int i = 0; i < instanceCount; i++) {
217 patternInstanceLists.add(new Patterns(patternLists.get(j), i, instanceCount));
218 }
219 }
220
221 return createTreeDataLikelihood(
222 patternLists,
223 treeModel,
224 branchModels,
225 siteRateModels,
226 branchRateModel,
227 null,
228 useAmbiguities,
229 scalingScheme,
230 delayScaling,
231 xo);
232 }
233
234 //************************************************************************
235 // AbstractXMLObjectParser implementation
236 //************************************************************************
237
238 public String getParserDescription() {
239 return "This element represents the likelihood of data on a tree given the site model.";
240 }
241
242 public Class getReturnType() {
243 return TreeDataLikelihood.class;
244 }
245
246 public static final XMLSyntaxRule[] rules = {
247 AttributeRule.newBooleanRule(USE_AMBIGUITIES, true),
248 //TODO: remove hard-coded maximum of 3 partitions
249 new ElementRule(PatternList.class,0,2),
250 new ElementRule(TreeModel.class),
251 //TODO: remove hard-coded maximum of 3 partitions
252 new ElementRule(SiteRateModel.class,0,2),
253 new ElementRule(BranchModel.class, true),
254 new ElementRule(SubstitutionModel.class, true),
255 new ElementRule(BranchRateModel.class, true),
256 new ElementRule(TipStatesModel.class, true),
257 AttributeRule.newStringRule(SCALING_SCHEME,true),
258 new ElementRule(TipStatesModel.class, true),
259 new ElementRule(FrequencyModel.class, true),
260 };
261
262 public XMLSyntaxRule[] getSyntaxRules() {
263 return rules;
264 }
265 }