Debian Codegraph @

src/dr/oldevomodel/approxPopTree/PopTreeModel.java

raw symbols SCIP
1 /*
2 * PopTreeModel.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.oldevomodel.approxPopTree;
27
28 import dr.evolution.tree.FlexibleTree;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.util.Taxon;
31 import dr.evomodel.tree.TreeModel;
32 import dr.xml.*;
33
34 import java.util.HashMap;
35 import java.util.LinkedList;
36 import java.util.Map;
37
38 /**
39 * Package: PopTree
40 * Description:
41 * <p/>
42 * <p/>
43 * Created by
44 *
45 * @author Alexander V. Alekseyenko ([email protected])
46 * Date: Feb 1, 2010
47 * Time: 11:39:59 AM
48 */
49 public class PopTreeModel extends FlexibleTree {
50 protected Map<NodeRef, LinkedList<NodeRef>> populations;
51 protected double time;
52
53 public PopTreeModel(TreeModel treeModel, double time) throws InvalidTreeException {
54 super(treeModel);
55 this.time = time;
56 this.populations = new HashMap<NodeRef, LinkedList<NodeRef>>();
57 enforcePopulations(time);
58 }
59
60 public void enforcePopulations(double time) {
61 enforcePopulations(time, this.getRoot());
62 }
63
64 public void enforcePopulations(double time, NodeRef node) {
65 beginTreeEdit();
66 reducePopulations(time, node);
67 endTreeEdit();
68 this.adoptTreeModelOrdering();
69 }
70
71 protected void reducePopulations(double time, NodeRef node) {
72 if (this.getNodeHeight(node) <= time) {
73 if (!this.isExternal(node))
74 this.setNodeHeight(node, time);
75 mergeSubtreePopulation(node);
76
77 } else {
78 for (int i = 0; i < this.getChildCount(node); ++i) {
79 NodeRef child = this.getChild(node, i);
80 reducePopulations(time, child);
81 }
82 }
83 }
84
85 public void splitPopulation(NodeRef node) {
86 splitPopulation(node, .5);
87 }
88
89 public void splitPopulation(NodeRef node, double frac) {
90 if (populations.containsKey(node) && populations.get(node).size() >= 1) {
91 //
92 }
93 }
94
95 public void mergePopulations(NodeRef parent) {
96 // assumes parent is an internal node which will become a new population node with all the crap below merged into this population
97 setNodeHeight(parent, time);
98 enforcePopulations(time, parent);
99 }
100
101 protected void mergeSubtreePopulation(NodeRef parent) {
102 /*
103 merge the subtree below node into a new population
104 */
105 LinkedList<NodeRef> sequenceNodes;
106 sequenceNodes = new LinkedList<NodeRef>();
107 if (this.getNodeTaxon(parent) == null) {
108 this.setNodeTaxon(parent, new Taxon("popNode" + populations.size()));
109 }
110
111 if (this.isExternal(parent)) {
112
113 }
114
115 sequenceNodes.addFirst(parent); //push(parent);
116 while (true) {
117 NodeRef currentNode = sequenceNodes.removeFirst();
118 if (this.isExternal(currentNode)) {
119 sequenceNodes.addFirst(currentNode); //push(currentNode);
120 //this.setNodeAttribute(parent,"sequenceNodes",sequenceNodes);
121 populations.put(parent, sequenceNodes);
122 return;
123 }
124 int numChildren = this.getChildCount(currentNode);
125 for (int i = 0; getChildCount(currentNode) > 0;) {
126 NodeRef child = this.getChild(currentNode, i);
127 sequenceNodes.addFirst(child); //push(child);
128 this.removeChild(currentNode, child);
129 }
130 }
131 }
132
133 public int getPopulationNodeCount() {
134 return populations.size();
135 }
136
137 public String toString() {
138 return super.toString() + formatPopulationNodes();
139 }
140
141 public String formatPopulationNodes() {
142 String retval = "\n";
143 retval += getExternalNodeCount() + "\n";
144 for (NodeRef node : populations.keySet()) {
145 retval += this.getNodeTaxon(node).getId() + "(" + this.isExternal(node) + ")::";
146 for (NodeRef seqNode : populations.get(node)) {
147 retval += this.getNodeTaxon(seqNode).getId() + ",";
148 }
149 retval += "\n";
150 }
151 return retval;
152 }
153
154 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
155
156 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
157 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
158 double timeCutoff = xo.getDoubleAttribute(POP_HEIGHT_CUTOFF);
159 PopTreeModel popTreeModel = null;
160 try {
161 popTreeModel = new PopTreeModel(treeModel, timeCutoff);
162 } catch (InvalidTreeException e) {
163 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
164 }
165 return popTreeModel;
166 }
167
168 public XMLSyntaxRule[] getSyntaxRules() {
169
170 return rules; //AUTOGENERATED METHOD IMPLEMENTATION
171 }
172
173 private final XMLSyntaxRule[] rules = {
174 new ElementRule(TreeModel.class),
175 AttributeRule.newDoubleRule(POP_HEIGHT_CUTOFF, false),
176 };
177
178 public String getParserDescription() {
179 return "Creates a Population Tree Model with specified divergence cut-off for population nodes";
180 }
181
182 public Class getReturnType() {
183 return PopTreeModel.class;
184 }
185
186 public String getParserName() {
187 return POP_TREE_MODEL;
188 }
189
190 };
191
192 public static final String POP_TREE_MODEL = "popTreeModel";
193 public static final String POP_HEIGHT_CUTOFF = "populationNodeHeight";
194 public static final String TREE_MODEL = "treeModel";
195 }