Debian Codegraph @

src/dr/evomodelxml/treelikelihood/BeagleTreeLikelihoodParser.java

raw symbols SCIP
1 /*
2 * BeagleTreeLikelihoodParser.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.app.beagle.evomodel.treelikelihood.RestrictedPartialsSequenceLikelihood;
29
30 import dr.evolution.tree.TreeUtils;
31 import dr.evomodel.branchmodel.BranchModel;
32 import dr.evomodel.branchmodel.HomogeneousBranchModel;
33 import dr.evomodel.siteratemodel.GammaSiteRateModel;
34 import dr.evomodel.substmodel.FrequencyModel;
35 import dr.evomodel.substmodel.SubstitutionModel;
36 import dr.evomodel.treelikelihood.AbstractTreeLikelihood;
37 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
38 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
39 import dr.evolution.alignment.PatternList;
40 import dr.evolution.alignment.Patterns;
41 import dr.evolution.util.TaxonList;
42 import dr.evomodel.branchratemodel.BranchRateModel;
43 import dr.evomodel.tree.TreeModel;
44 import dr.evomodel.tipstatesmodel.TipStatesModel;
45 import dr.inference.model.CompoundLikelihood;
46 import dr.inference.model.Likelihood;
47 import dr.inference.model.Parameter;
48 import dr.xml.*;
49
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Set;
54
55 /**
56 * @author Andrew Rambaut
57 * @author Alexei Drummond
58 * @author Marc Suchard
59 * @version $Id$
60 */
61 public class BeagleTreeLikelihoodParser extends AbstractXMLObjectParser {
62
63 public static final String BEAGLE_INSTANCE_COUNT = "beagle.instance.count";
64
65 public static final String TREE_LIKELIHOOD = "treeLikelihood";
66 public static final String USE_AMBIGUITIES = "useAmbiguities";
67 public static final String INSTANCE_COUNT = "instanceCount";
68 // public static final String DEVICE_NUMBER = "deviceNumber";
69 // public static final String PREFER_SINGLE_PRECISION = "preferSinglePrecision";
70 public static final String SCALING_SCHEME = "scalingScheme";
71 public static final String DELAY_SCALING = "delayScaling";
72 public static final String PARTIALS_RESTRICTION = "partialsRestriction";
73
74 public String getParserName() {
75 return TREE_LIKELIHOOD;
76 }
77
78 protected BeagleTreeLikelihood createTreeLikelihood(PatternList patternList, TreeModel treeModel,
79 BranchModel branchModel,
80 GammaSiteRateModel siteRateModel,
81 BranchRateModel branchRateModel,
82 TipStatesModel tipStatesModel,
83 boolean useAmbiguities, PartialsRescalingScheme scalingScheme,
84 boolean delayRescalingUntilUnderflow,
85 Map<Set<String>, Parameter> partialsRestrictions,
86 XMLObject xo) throws XMLParseException {
87 return new BeagleTreeLikelihood(
88 patternList,
89 treeModel,
90 branchModel,
91 siteRateModel,
92 branchRateModel,
93 tipStatesModel,
94 useAmbiguities,
95 scalingScheme,
96 delayRescalingUntilUnderflow,
97 partialsRestrictions
98 );
99 }
100
101 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
102
103 boolean useAmbiguities = xo.getAttribute(USE_AMBIGUITIES, false);
104 int instanceCount = xo.getAttribute(INSTANCE_COUNT, 1);
105 if (instanceCount < 1) {
106 instanceCount = 1;
107 }
108
109 String ic = System.getProperty(BEAGLE_INSTANCE_COUNT);
110 if (ic != null && ic.length() > 0) {
111 instanceCount = Integer.parseInt(ic);
112 }
113
114 PatternList patternList = (PatternList) xo.getChild(PatternList.class);
115 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
116 GammaSiteRateModel siteRateModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
117
118 FrequencyModel rootFreqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
119
120 BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class);
121 if (branchModel == null) {
122 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
123 if (substitutionModel == null) {
124 substitutionModel = siteRateModel.getSubstitutionModel();
125 }
126 if (substitutionModel == null) {
127 throw new XMLParseException("No substitution model available for TreeLikelihood: "+xo.getId());
128 }
129 branchModel = new HomogeneousBranchModel(substitutionModel, rootFreqModel);
130 }
131
132 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
133
134 TipStatesModel tipStatesModel = (TipStatesModel) xo.getChild(TipStatesModel.class);
135 // if (xo.getChild(TipStatesModel.class) != null) {
136 // throw new XMLParseException("Sequence Error Models are not supported under BEAGLE yet. Please use Native BEAST Likelihood.");
137 // }
138
139 PartialsRescalingScheme scalingScheme = PartialsRescalingScheme.DEFAULT;
140 boolean delayScaling = true;
141 if (xo.hasAttribute(SCALING_SCHEME)) {
142 scalingScheme = PartialsRescalingScheme.parseFromString(xo.getStringAttribute(SCALING_SCHEME));
143 if (scalingScheme == null)
144 throw new XMLParseException("Unknown scaling scheme '"+xo.getStringAttribute(SCALING_SCHEME)+"' in "+
145 "OldBeagleTreeLikelihood object '"+xo.getId());
146
147 }
148 if (xo.hasAttribute(DELAY_SCALING)) {
149 delayScaling = xo.getBooleanAttribute(DELAY_SCALING);
150 }
151
152 Map<Set<String>, Parameter> partialsRestrictions = null;
153
154 if (xo.hasChildNamed(PARTIALS_RESTRICTION)) {
155 XMLObject cxo = xo.getChild(PARTIALS_RESTRICTION);
156 TaxonList taxonList = (TaxonList) cxo.getChild(TaxonList.class);
157 // Parameter parameter = (Parameter) cxo.getChild(Parameter.class);
158 try {
159 TreeUtils.getLeavesForTaxa(treeModel, taxonList);
160 } catch (TreeUtils.MissingTaxonException e) {
161 throw new XMLParseException("Unable to parse taxon list: " + e.getMessage());
162 }
163 throw new XMLParseException("Restricting internal nodes is not yet implemented. Contact Marc");
164
165 }
166
167
168 if (instanceCount == 1 || patternList.getPatternCount() < instanceCount) {
169 return createTreeLikelihood(
170 patternList,
171 treeModel,
172 branchModel,
173 siteRateModel,
174 branchRateModel,
175 tipStatesModel,
176 useAmbiguities,
177 scalingScheme,
178 delayScaling,
179 partialsRestrictions,
180 xo
181 );
182 }
183
184 // using multiple instances of BEAGLE...
185
186 // if (!(patternList instanceof SitePatterns)) {
187 // throw new XMLParseException("BEAGLE_INSTANCES option cannot be used with BEAUti-selected codon partitioning.");
188 // }
189
190 if (tipStatesModel != null) {
191 throw new XMLParseException("BEAGLE_INSTANCES option cannot be used with a TipStateModel (i.e., a sequence error model).");
192 }
193
194 List<Likelihood> likelihoods = new ArrayList<Likelihood>();
195 for (int i = 0; i < instanceCount; i++) {
196
197 Patterns subPatterns = new Patterns(patternList, i, instanceCount);
198
199 AbstractTreeLikelihood treeLikelihood = createTreeLikelihood(
200 subPatterns,
201 treeModel,
202 branchModel,
203 siteRateModel,
204 branchRateModel,
205 null,
206 useAmbiguities,
207 scalingScheme,
208 delayScaling,
209 partialsRestrictions,
210 xo);
211 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
212 likelihoods.add(treeLikelihood);
213 }
214
215 return new CompoundLikelihood(likelihoods);
216 }
217
218 //************************************************************************
219 // AbstractXMLObjectParser implementation
220 //************************************************************************
221
222 public String getParserDescription() {
223 return "This element represents the likelihood of a patternlist on a tree given the site model.";
224 }
225
226 public Class getReturnType() {
227 return Likelihood.class;
228 }
229
230 public static final XMLSyntaxRule[] rules = {
231 AttributeRule.newBooleanRule(USE_AMBIGUITIES, 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 new ElementRule(TipStatesModel.class, true),
239 AttributeRule.newStringRule(SCALING_SCHEME,true),
240 new ElementRule(PARTIALS_RESTRICTION, new XMLSyntaxRule[] {
241 new ElementRule(TaxonList.class),
242 new ElementRule(Parameter.class),
243 }, true),
244 new ElementRule(TipStatesModel.class, true),
245 new ElementRule(FrequencyModel.class, true),
246 };
247
248 public XMLSyntaxRule[] getSyntaxRules() {
249 return rules;
250 }
251 }