| 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.evolution.tree; |
| 27 |
|
| 28 |
|
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.util.Attributable; |
| 31 |
|
| 32 |
import java.util.Iterator; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class SimpleNode implements NodeRef, Attributable { |
| 43 |
|
| 44 |
|
| 45 |
private SimpleNode parent; |
| 46 |
|
| 47 |
|
| 48 |
private int nodeNumber; |
| 49 |
|
| 50 |
|
| 51 |
private double height; |
| 52 |
|
| 53 |
|
| 54 |
private double rate; |
| 55 |
|
| 56 |
|
| 57 |
private Taxon taxon = null; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private SimpleNode[] child; |
| 64 |
|
| 65 |
|
| 66 |
public SimpleNode() |
| 67 |
{ |
| 68 |
parent = null; |
| 69 |
child =null; |
| 70 |
height = 0.0; |
| 71 |
rate = 1.0; |
| 72 |
|
| 73 |
nodeNumber = 0; |
| 74 |
} |
| 75 |
|
| 76 |
public SimpleNode(SimpleNode node) { |
| 77 |
parent = null; |
| 78 |
|
| 79 |
setHeight(node.getHeight()); |
| 80 |
setRate(node.getRate()); |
| 81 |
setId(node.getId()); |
| 82 |
setNumber(node.getNumber()); |
| 83 |
setTaxon(node.getTaxon()); |
| 84 |
|
| 85 |
child = null; |
| 86 |
|
| 87 |
for (int i = 0; i < node.getChildCount(); i++) { |
| 88 |
addChild(new SimpleNode(node.getChild(i))); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
public SimpleNode(Tree tree, NodeRef node) |
| 94 |
{ |
| 95 |
parent = null; |
| 96 |
setHeight(tree.getNodeHeight(node)); |
| 97 |
setRate(tree.getNodeRate(node)); |
| 98 |
final int nodeNumber = node.getNumber(); |
| 99 |
setId(tree.getTaxonId(nodeNumber)); |
| 100 |
setNumber(nodeNumber); |
| 101 |
setTaxon(tree.getNodeTaxon(node)); |
| 102 |
|
| 103 |
child = null; |
| 104 |
|
| 105 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 106 |
addChild(new SimpleNode(tree, tree.getChild(node, i))); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public SimpleNode getDeepCopy() { |
| 111 |
return new SimpleNode(this); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public final SimpleNode getParent() { |
| 118 |
return parent; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
public void setParent(SimpleNode node) { parent = node; } |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public final double getHeight() { |
| 128 |
return height; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public final void setHeight(double value) { |
| 135 |
height = value; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public final double getRate() { |
| 142 |
return rate; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public final void setRate(double value) { |
| 149 |
rate = value; |
| 150 |
} |
| 151 |
|
| 152 |
public void setNumber(int n) { |
| 153 |
nodeNumber = n; |
| 154 |
} |
| 155 |
|
| 156 |
public int getNumber() { |
| 157 |
return nodeNumber; |
| 158 |
} |
| 159 |
|
| 160 |
public void setTaxon(Taxon taxon) { |
| 161 |
this.taxon = taxon; |
| 162 |
} |
| 163 |
|
| 164 |
public Taxon getTaxon() { |
| 165 |
return taxon; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public SimpleNode getChild(int n) |
| 176 |
{ |
| 177 |
assert 0 <= n && n < child.length; |
| 178 |
|
| 179 |
return child[n]; |
| 180 |
} |
| 181 |
|
| 182 |
public boolean hasChild(SimpleNode node) { |
| 183 |
|
| 184 |
for (int i = 0, n = getChildCount(); i < n; i++) { |
| 185 |
if (node == child[i]) return true; |
| 186 |
} |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public void addChild(SimpleNode n) |
| 196 |
{ |
| 197 |
insertChild(n, getChildCount()); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public void insertChild(SimpleNode n, int pos) |
| 207 |
{ |
| 208 |
int numChildren = getChildCount(); |
| 209 |
|
| 210 |
SimpleNode[] newChild = new SimpleNode[numChildren + 1]; |
| 211 |
|
| 212 |
for (int i = 0; i < pos; i++) |
| 213 |
{ |
| 214 |
newChild[i] = child[i]; |
| 215 |
} |
| 216 |
newChild[pos] = n; |
| 217 |
for (int i = pos; i < numChildren; i++) |
| 218 |
{ |
| 219 |
newChild[i+1] = child[i]; |
| 220 |
} |
| 221 |
|
| 222 |
child = newChild; |
| 223 |
|
| 224 |
n.setParent(this); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public SimpleNode removeChild(SimpleNode n) |
| 233 |
{ |
| 234 |
int numChildren = getChildCount(); |
| 235 |
SimpleNode[] newChild = new SimpleNode[numChildren-1]; |
| 236 |
|
| 237 |
int j = 0; |
| 238 |
boolean found = false; |
| 239 |
for (int i = 0; i < numChildren; i++) { |
| 240 |
if (child[i] != n) { |
| 241 |
newChild[j] = child[i]; |
| 242 |
j++; |
| 243 |
} else |
| 244 |
found = true; |
| 245 |
} |
| 246 |
|
| 247 |
if (!found) |
| 248 |
throw new IllegalArgumentException("Nonexistent child"); |
| 249 |
|
| 250 |
|
| 251 |
n.setParent(null); |
| 252 |
|
| 253 |
child = newChild; |
| 254 |
|
| 255 |
return n; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public SimpleNode removeChild(int n) |
| 264 |
{ |
| 265 |
int numChildren = getChildCount(); |
| 266 |
|
| 267 |
if (n >= numChildren) |
| 268 |
{ |
| 269 |
throw new IllegalArgumentException("Nonexistent child"); |
| 270 |
} |
| 271 |
|
| 272 |
return removeChild(child[n]); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
public void replaceChild(SimpleNode childNode, SimpleNode replacment) { |
| 277 |
for(int nc = 0; nc < child.length; ++nc) { |
| 278 |
if( child[nc] == childNode ) { |
| 279 |
replacment.setParent(this); |
| 280 |
child[nc] = replacment; |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public boolean hasChildren() |
| 292 |
{ |
| 293 |
return (getChildCount() != 0); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public boolean isExternal() { |
| 302 |
return !hasChildren(); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public boolean isRoot() |
| 311 |
{ |
| 312 |
return (getParent() == null); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public final int getChildCount() { |
| 320 |
if (child == null) return 0; |
| 321 |
return child.length; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
private String id = null; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public String getId() { |
| 334 |
return id; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public void setId(String id) { |
| 341 |
this.id = id; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
private Attributable.AttributeHelper attributes = null; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public void setAttribute(String name, Object value) { |
| 356 |
if (attributes == null) |
| 357 |
attributes = new Attributable.AttributeHelper(); |
| 358 |
attributes.setAttribute(name, value); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public Object getAttribute(String name) { |
| 366 |
if (attributes == null) |
| 367 |
return null; |
| 368 |
else |
| 369 |
return attributes.getAttribute(name); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public Iterator<String> getAttributeNames() { |
| 376 |
if (attributes == null) |
| 377 |
return null; |
| 378 |
else |
| 379 |
return attributes.getAttributeNames(); |
| 380 |
} |
| 381 |
} |