Debian Codegraph @

src/dr/evomodelxml/coalescent/OldCoalescentSimulatorParser.java

raw symbols SCIP
1 /*
2 * OldCoalescentSimulatorParser.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.SimpleTree;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.util.Taxa;
31 import dr.evolution.util.Taxon;
32 import dr.evolution.util.TaxonList;
33 import dr.evomodel.coalescent.CoalescentSimulator;
34 import dr.evomodel.coalescent.DemographicModel;
35 import dr.evomodelxml.tree.TreeModelParser;
36 import dr.inference.distribution.ParametricDistributionModel;
37 import dr.xml.*;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42 /**
43 * The original Coalescent Simulator parser. Responds to the parser name 'coalescentTree'.
44 * This has been replaced, since v1.7.4 by a more flexible class, CoalescentSimulator, with
45 * parser name 'coalescentSimulator'. Left for backwards XML compatibility.
46 *
47 * @deprecated
48 */
49 public class OldCoalescentSimulatorParser extends AbstractXMLObjectParser {
50
51 public static final String COALESCENT_TREE = "coalescentTree";
52 public static final String RESCALE_HEIGHT = "rescaleHeight";
53 public static final String ROOT_HEIGHT = TreeModelParser.ROOT_HEIGHT;
54 public static final String CONSTRAINED_TAXA = "constrainedTaxa";
55 public static final String TMRCA_CONSTRAINT = "tmrca";
56 public static final String IS_MONOPHYLETIC = "monophyletic";
57
58 public String getParserName() {
59 return COALESCENT_TREE;
60 }
61
62 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
63
64 CoalescentSimulator simulator = new CoalescentSimulator();
65
66 DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
67 List<TaxonList> taxonLists = new ArrayList<TaxonList>();
68 List<Tree> subtrees = new ArrayList<Tree>();
69
70 double rootHeight = xo.getAttribute(ROOT_HEIGHT, -1.0);
71
72 if (xo.hasAttribute(RESCALE_HEIGHT)) {
73 rootHeight = xo.getDoubleAttribute(RESCALE_HEIGHT);
74 }
75
76 // should have one child that is node
77 for (int i = 0; i < xo.getChildCount(); i++) {
78 final Object child = xo.getChild(i);
79
80 // AER - swapped the order of these round because Trees are TaxonLists...
81 if (child instanceof Tree) {
82 subtrees.add((Tree) child);
83 } else if (child instanceof TaxonList) {
84 taxonLists.add((TaxonList) child);
85 } else if (xo.getChildName(i).equals(CONSTRAINED_TAXA)) {
86 XMLObject constrainedTaxa = (XMLObject) child;
87
88 // all taxa
89 final TaxonList taxa = (TaxonList) constrainedTaxa.getChild(TaxonList.class);
90
91 List<CoalescentSimulator.TaxaConstraint> constraints = new ArrayList<CoalescentSimulator.TaxaConstraint>();
92 final String setsNotCompatibleMessage = "taxa sets not compatible";
93
94 // pick up all constraints. order in partial order, where taxa_1 @in taxa_2 implies
95 // taxa_1 is before taxa_2.
96
97
98 for (int nc = 0; nc < constrainedTaxa.getChildCount(); ++nc) {
99
100 final Object object = constrainedTaxa.getChild(nc);
101 if (object instanceof XMLObject) {
102 final XMLObject constraint = (XMLObject) object;
103
104 if (constraint.getName().equals(TMRCA_CONSTRAINT)) {
105 TaxonList taxaSubSet = (TaxonList) constraint.getChild(TaxonList.class);
106 ParametricDistributionModel dist =
107 (ParametricDistributionModel) constraint.getChild(ParametricDistributionModel.class);
108 boolean isMono = constraint.getAttribute(IS_MONOPHYLETIC, true);
109
110 final CoalescentSimulator.TaxaConstraint taxaConstraint = new CoalescentSimulator.TaxaConstraint(taxaSubSet, dist, isMono);
111 int insertPoint;
112 for (insertPoint = 0; insertPoint < constraints.size(); ++insertPoint) {
113 // if new <= constraints[insertPoint] insert before insertPoint
114
115 final CoalescentSimulator.TaxaConstraint iConstraint = constraints.get(insertPoint);
116 if (iConstraint.isMonophyletic) {
117 if (!taxaConstraint.isMonophyletic) {
118 continue;
119 }
120
121 final TaxonList taxonsip = iConstraint.taxons;
122 final int nIn = simulator.sizeOfIntersection(taxonsip, taxaSubSet);
123 if (nIn == taxaSubSet.getTaxonCount()) {
124 break;
125 }
126 if (nIn > 0 && nIn != taxonsip.getTaxonCount()) {
127 throw new XMLParseException(setsNotCompatibleMessage);
128 }
129 } else {
130 // reached non mono area
131 if (!taxaConstraint.isMonophyletic) {
132 if (iConstraint.upper >= taxaConstraint.upper) {
133 break;
134 }
135 } else {
136 break;
137 }
138 }
139 }
140 constraints.add(insertPoint, taxaConstraint);
141 }
142 }
143 }
144 final int nConstraints = constraints.size();
145
146 if (nConstraints == 0) {
147 if (taxa != null) {
148 taxonLists.add(taxa);
149 }
150 } else {
151 for (int nc = 0; nc < nConstraints; ++nc) {
152 CoalescentSimulator.TaxaConstraint cnc = constraints.get(nc);
153 if (!cnc.isMonophyletic) {
154 for (int nc1 = nc - 1; nc1 >= 0; --nc1) {
155 CoalescentSimulator.TaxaConstraint cnc1 = constraints.get(nc1);
156 int x = simulator.sizeOfIntersection(cnc.taxons, cnc1.taxons);
157 if (x > 0) {
158 Taxa combinedTaxa = new Taxa(cnc.taxons);
159 combinedTaxa.addTaxa(cnc1.taxons);
160 cnc = new CoalescentSimulator.TaxaConstraint(combinedTaxa, cnc.lower, cnc.upper, cnc.isMonophyletic);
161 constraints.set(nc, cnc);
162 }
163 }
164 }
165 }
166 // determine upper bound for each set.
167 double[] upper = new double[nConstraints];
168 for (int nc = nConstraints - 1; nc >= 0; --nc) {
169 final CoalescentSimulator.TaxaConstraint cnc = constraints.get(nc);
170 if (cnc.realLimits()) {
171 upper[nc] = cnc.upper;
172 } else {
173 upper[nc] = Double.POSITIVE_INFINITY;
174 }
175 }
176
177 for (int nc = nConstraints - 1; nc >= 0; --nc) {
178 final CoalescentSimulator.TaxaConstraint cnc = constraints.get(nc);
179 if (upper[nc] < Double.POSITIVE_INFINITY) {
180 for (int nc1 = nc - 1; nc1 >= 0; --nc1) {
181 final CoalescentSimulator.TaxaConstraint cnc1 = constraints.get(nc1);
182 if (simulator.contained(cnc1.taxons, cnc.taxons)) {
183 upper[nc1] = Math.min(upper[nc1], upper[nc]);
184 if (cnc1.realLimits() && cnc1.lower > upper[nc1]) {
185 throw new XMLParseException(setsNotCompatibleMessage);
186 }
187 break;
188 }
189 }
190 }
191 }
192 // collect subtrees here
193 List<Tree> st = new ArrayList<Tree>();
194 for (int nc = 0; nc < constraints.size(); ++nc) {
195 final CoalescentSimulator.TaxaConstraint nxt = constraints.get(nc);
196 // collect all previously built subtrees which are a subset of taxa set to be added
197 List<Tree> subs = new ArrayList<Tree>();
198 Taxa newTaxons = new Taxa(nxt.taxons);
199 for (int k = 0; k < st.size(); ++k) {
200 final Tree stk = st.get(k);
201 int x = simulator.sizeOfIntersection(stk, nxt.taxons);
202 if (x == st.get(k).getTaxonCount()) {
203 final Tree tree = st.remove(k);
204 --k;
205 subs.add(tree);
206 newTaxons.removeTaxa(tree);
207 }
208 }
209
210 SimpleTree tree = simulator.simulateTree(newTaxons, demoModel);
211 final double lower = nxt.realLimits() ? nxt.lower : 0;
212 if (upper[nc] < Double.MAX_VALUE) {
213 simulator.attemptToScaleTree(tree, (lower + upper[nc]) / 2);
214 }
215 if (subs.size() > 0) {
216 if (tree.getTaxonCount() > 0) subs.add(tree);
217 double h = -1;
218 if (upper[nc] < Double.MAX_VALUE) {
219 for (Tree t : subs) {
220 // protect against 1 taxa tree with height 0
221 final double rh = t.getNodeHeight(t.getRoot());
222 h = Math.max(h, rh > 0 ? rh : (lower + upper[nc]) / 2);
223 }
224 h = (h + upper[nc]) / 2;
225 }
226 tree = simulator.simulateTree(subs.toArray(new Tree[subs.size()]), demoModel, h, true);
227 }
228 st.add(tree);
229
230 }
231
232 // add a taxon list for remaining taxa
233 if (taxa != null) {
234 final Taxa list = new Taxa();
235 for (int j = 0; j < taxa.getTaxonCount(); ++j) {
236 Taxon taxonj = taxa.getTaxon(j);
237 for (Tree aSt : st) {
238 if (aSt.getTaxonIndex(taxonj) >= 0) {
239 taxonj = null;
240 break;
241 }
242 }
243 if (taxonj != null) {
244 list.addTaxon(taxonj);
245 }
246 }
247 if (list.getTaxonCount() > 0) {
248 taxonLists.add(list);
249 }
250 }
251 if (st.size() > 1) {
252 final Tree t = simulator.simulateTree(st.toArray(new Tree[st.size()]), demoModel, -1, false);
253 subtrees.add(t);
254 } else {
255 subtrees.add(st.get(0));
256 }
257 }
258 }
259 }
260
261 if (taxonLists.size() == 0) {
262 if (subtrees.size() == 1) {
263 return subtrees.get(0);
264 }
265 throw new XMLParseException("Expected at least one taxonList or two subtrees in "
266 + getParserName() + " element.");
267 }
268
269 try {
270 Tree[] trees = new Tree[taxonLists.size() + subtrees.size()];
271 // simulate each taxonList separately
272 for (int i = 0; i < taxonLists.size(); i++) {
273 trees[i] = simulator.simulateTree(taxonLists.get(i), demoModel);
274 }
275 // add the preset trees
276 for (int i = 0; i < subtrees.size(); i++) {
277 trees[i + taxonLists.size()] = subtrees.get(i);
278 }
279
280 return simulator.simulateTree(trees, demoModel, rootHeight, trees.length != 1);
281
282 } catch (IllegalArgumentException iae) {
283 throw new XMLParseException(iae.getMessage());
284 }
285 }
286
287 //************************************************************************
288 // AbstractXMLObjectParser implementation
289 //************************************************************************
290
291 public String getParserDescription() {
292 return "This element returns a simulated tree under the given demographic model.";
293 }
294
295 public Class getReturnType() {
296 return CoalescentSimulator.class; //Object.class;
297 }
298
299 public XMLSyntaxRule[] getSyntaxRules() {
300 return rules;
301 }
302
303 private final XMLSyntaxRule[] rules = {
304 AttributeRule.newDoubleRule(RESCALE_HEIGHT, true, "Attempt to rescale the tree to the given root height"),
305 AttributeRule.newDoubleRule(ROOT_HEIGHT, true, ""),
306 new ElementRule(Tree.class, 0, Integer.MAX_VALUE),
307 new ElementRule(TaxonList.class, 0, Integer.MAX_VALUE),
308 new ElementRule(CONSTRAINED_TAXA, new XMLSyntaxRule[]{
309 new ElementRule(TaxonList.class, 0, Integer.MAX_VALUE),
310 new ElementRule(TMRCA_CONSTRAINT, new XMLSyntaxRule[]{
311 new ElementRule(TaxonList.class, 0, Integer.MAX_VALUE),
312 }),
313 }, true),
314 new ElementRule(DemographicModel.class),
315 };
316 }