Debian Codegraph @

src/dr/evomodelxml/coalescent/CoalescentLikelihoodParser.java

raw symbols SCIP
1 /*
2 * CoalescentLikelihoodParser.java
3 *
4 * Copyright (c) 2002-2015 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.coalescent;
27
28 import dr.evolution.tree.TreeUtils;
29 import dr.evolution.util.Taxa;
30 import dr.evolution.util.TaxonList;
31 import dr.evomodel.coalescent.CoalescentLikelihood;
32 import dr.evomodel.coalescent.DemographicModel;
33 import dr.evomodel.coalescent.MultiLociTreeSet;
34 import dr.evomodel.coalescent.OldAbstractCoalescentLikelihood;
35 import dr.evomodel.tree.TreeModel;
36 import dr.xml.*;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 /**
42 */
43 public class CoalescentLikelihoodParser extends AbstractXMLObjectParser {
44
45 public static final String COALESCENT_LIKELIHOOD = "coalescentLikelihood";
46 public static final String MODEL = "model";
47 public static final String POPULATION_TREE = "populationTree";
48 public static final String POPULATION_FACTOR = "factor";
49
50 public static final String INCLUDE = "include";
51 public static final String EXCLUDE = "exclude";
52
53 public String getParserName() {
54 return COALESCENT_LIKELIHOOD;
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 XMLObject cxo = xo.getChild(MODEL);
60 DemographicModel demoModel = (DemographicModel) cxo.getChild(DemographicModel.class);
61
62 List<TreeModel> trees = new ArrayList<TreeModel>();
63 List<Double> popFactors = new ArrayList<Double>();
64 MultiLociTreeSet treesSet = demoModel instanceof MultiLociTreeSet ? (MultiLociTreeSet) demoModel : null;
65
66 for (int k = 0; k < xo.getChildCount(); ++k) {
67 final Object child = xo.getChild(k);
68 if (child instanceof XMLObject) {
69 cxo = (XMLObject) child;
70 if (cxo.getName().equals(POPULATION_TREE)) {
71 final TreeModel t = (TreeModel) cxo.getChild(TreeModel.class);
72 assert t != null;
73 trees.add(t);
74
75 popFactors.add(cxo.getAttribute(POPULATION_FACTOR, 1.0));
76 }
77 }
78 // in the future we may have arbitrary multi-loci element
79 // else if( child instanceof MultiLociTreeSet ) {
80 // treesSet = (MultiLociTreeSet)child;
81 // }
82 }
83
84 TreeModel treeModel = null;
85 if (trees.size() == 1 && popFactors.get(0) == 1.0) {
86 treeModel = trees.get(0);
87 } else if (trees.size() > 1) {
88 treesSet = new MultiLociTreeSet.Default(trees, popFactors);
89 } else if (!(trees.size() == 0 && treesSet != null)) {
90 throw new XMLParseException("Incorrectly constructed likelihood element");
91 }
92
93 TaxonList includeSubtree = null;
94
95 if (xo.hasChildNamed(INCLUDE)) {
96 includeSubtree = (TaxonList) xo.getElementFirstChild(INCLUDE);
97 }
98
99 List<TaxonList> excludeSubtrees = new ArrayList<TaxonList>();
100
101 if (xo.hasChildNamed(EXCLUDE)) {
102 cxo = xo.getChild(EXCLUDE);
103 for (int i = 0; i < cxo.getChildCount(); i++) {
104 excludeSubtrees.add((TaxonList) cxo.getChild(i));
105 }
106 }
107
108 if (treeModel != null) {
109 try {
110 return new CoalescentLikelihood(treeModel, includeSubtree, excludeSubtrees, demoModel);
111 } catch (TreeUtils.MissingTaxonException mte) {
112 throw new XMLParseException("treeModel missing a taxon from taxon list in " + getParserName() + " element");
113 }
114 } else {
115 if (includeSubtree != null || excludeSubtrees.size() > 0) {
116 throw new XMLParseException("Include/Exclude taxa not supported for multi locus sets");
117 }
118 // Use old code for multi locus sets.
119 // This is a little unfortunate but the current code is using AbstractCoalescentLikelihood as
120 // a base - and modifing it will probsbly result in a bigger mess.
121 return new OldAbstractCoalescentLikelihood(treesSet, demoModel);
122 }
123 }
124
125 //************************************************************************
126 // AbstractXMLObjectParser implementation
127 //************************************************************************
128
129 public String getParserDescription() {
130 return "This element represents the likelihood of the tree given the demographic function.";
131 }
132
133 public Class getReturnType() {
134 return CoalescentLikelihood.class;
135 }
136
137 public XMLSyntaxRule[] getSyntaxRules() {
138 return rules;
139 }
140
141 private final XMLSyntaxRule[] rules = {
142 new ElementRule(MODEL, new XMLSyntaxRule[]{
143 new ElementRule(DemographicModel.class)
144 }, "The demographic model which describes the effective population size over time"),
145
146 new ElementRule(POPULATION_TREE, new XMLSyntaxRule[]{
147 AttributeRule.newDoubleRule(POPULATION_FACTOR, true),
148 new ElementRule(TreeModel.class)
149 }, "Tree(s) to compute likelihood for", 0, Integer.MAX_VALUE),
150
151 new ElementRule(INCLUDE, new XMLSyntaxRule[]{
152 new ElementRule(Taxa.class)
153 }, "An optional subset of taxa on which to calculate the likelihood (should be monophyletic)", true),
154
155 new ElementRule(EXCLUDE, new XMLSyntaxRule[]{
156 new ElementRule(Taxa.class, 1, Integer.MAX_VALUE)
157 }, "One or more subsets of taxa which should be excluded from calculate the likelihood (should be monophyletic)", true)
158 };
159 }