| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 56 |
|
| 57 |
public class BalancedBeagleTreeLikelihoodParser extends AbstractXMLObjectParser { |
| 58 |
|
| 59 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
PartialsRescalingScheme scalingScheme = PartialsRescalingScheme.DEFAULT; |
| 134 |
if (xo.hasAttribute(BeagleTreeLikelihoodParser.SCALING_SCHEME)) { |
| 135 |
|
| 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 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 239 |
compound = new CompoundLikelihood(instanceCount, likelihoods); |
| 240 |
|
| 241 |
|
| 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 |
|
| 286 |
compound = new CompoundLikelihood(instanceCount, likelihoods); |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
return compound; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 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 |
} |