| 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.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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 97 |
setNodeHeight(parent, time); |
| 98 |
enforcePopulations(time, parent); |
| 99 |
} |
| 100 |
|
| 101 |
protected void mergeSubtreePopulation(NodeRef parent) { |
| 102 |
|
| 103 |
|
| 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); |
| 116 |
while (true) { |
| 117 |
NodeRef currentNode = sequenceNodes.removeFirst(); |
| 118 |
if (this.isExternal(currentNode)) { |
| 119 |
sequenceNodes.addFirst(currentNode); |
| 120 |
|
| 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); |
| 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(); |
| 164 |
} |
| 165 |
return popTreeModel; |
| 166 |
} |
| 167 |
|
| 168 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 169 |
|
| 170 |
return rules; |
| 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 |
} |