Debian Codegraph @

src/dr/evomodelxml/coalescent/CoalescentSimulatorParser.java

raw symbols SCIP
1 /*
2 * CoalescentSimulatorParser.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.SimpleNode;
29 import dr.evolution.tree.SimpleTree;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.TaxonList;
34 import dr.evomodel.coalescent.CoalescentSimulator;
35 import dr.evomodel.coalescent.DemographicModel;
36 import dr.evomodelxml.tree.TreeModelParser;
37 import dr.inference.distribution.ParametricDistributionModel;
38 import dr.xml.*;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 /**
44 * CoalescentSimulator parser. Replaces the one now called 'OldCoalescentSimulator' which
45 * used the parser name 'coalescentTree'. Simulates a tree using arbitrarily nested subtrees
46 * which can be scaled to have particular node heights to be compatible with calibrations.
47 */
48 public class CoalescentSimulatorParser extends AbstractXMLObjectParser {
49
50 public static final String COALESCENT_SIMULATOR = "coalescentSimulator";
51 public static final String HEIGHT = "height";
52
53 public String getParserName() {
54 return COALESCENT_SIMULATOR;
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 CoalescentSimulator simulator = new CoalescentSimulator();
60
61 DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
62 List<TaxonList> taxonLists = new ArrayList<TaxonList>();
63 List<Tree> subtrees = new ArrayList<Tree>();
64
65 double height = xo.getAttribute(HEIGHT, Double.NaN);
66
67 // should have one child that is node
68 for (int i = 0; i < xo.getChildCount(); i++) {
69 final Object child = xo.getChild(i);
70
71 // AER - swapped the order of these round because Trees are TaxonLists...
72 if (child instanceof Tree) {
73 subtrees.add((Tree) child);
74 } else if (child instanceof TaxonList) {
75 taxonLists.add((TaxonList) child);
76 }
77 }
78
79 if (taxonLists.size() == 0) {
80 if (subtrees.size() == 1) {
81 return subtrees.get(0);
82 }
83 throw new XMLParseException("Expected at least one taxonList or two subtrees in "
84 + getParserName() + " element.");
85 }
86
87 Taxa remainingTaxa = new Taxa();
88 for (int i = 0; i < taxonLists.size(); i++) {
89 remainingTaxa.addTaxa(taxonLists.get(i));
90 }
91
92 for (int i = 0; i < subtrees.size(); i++) {
93 remainingTaxa.removeTaxa(subtrees.get(i));
94 }
95
96 try {
97 Tree[] trees = new Tree[subtrees.size() + remainingTaxa.getTaxonCount()];
98 // add the preset trees
99 for (int i = 0; i < subtrees.size(); i++) {
100 trees[i] = subtrees.get(i);
101 }
102
103 // add all the remaining taxa in as single tip trees...
104 for (int i = 0; i < remainingTaxa.getTaxonCount(); i++) {
105 Taxa tip = new Taxa();
106 tip.addTaxon(remainingTaxa.getTaxon(i));
107 trees[i + subtrees.size()] = simulator.simulateTree(tip, demoModel);
108 }
109
110 return simulator.simulateTree(trees, demoModel, height, trees.length != 1);
111
112 } catch (IllegalArgumentException iae) {
113 throw new XMLParseException(iae.getMessage());
114 }
115 }
116
117 //************************************************************************
118 // AbstractXMLObjectParser implementation
119 //************************************************************************
120
121 public String getParserDescription() {
122 return "This element returns a simulated tree under the given demographic model. The element can " +
123 "be nested to simulate with monophyletic clades. The tree will be rescaled to the given height.";
124 }
125
126 public Class getReturnType() {
127 return CoalescentSimulator.class; //Object.class;
128 }
129
130 public XMLSyntaxRule[] getSyntaxRules() {
131 return rules;
132 }
133
134 private final XMLSyntaxRule[] rules = {
135 AttributeRule.newDoubleRule(HEIGHT, true, ""),
136 new ElementRule(Tree.class, 0, Integer.MAX_VALUE),
137 new ElementRule(TaxonList.class, 0, Integer.MAX_VALUE),
138 new ElementRule(DemographicModel.class, 0, Integer.MAX_VALUE),
139 };
140 }