| 1 | package jasper; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.HashSet; |
| 6 | import java.util.List; |
| 7 | |
| 8 | public class TreeNode { |
| 9 | |
| 10 | /** |
| 11 | * Creates an object of class TreeNode |
| 12 | * |
| 13 | * @param name The name of the organism in this node |
| 14 | * @param olds The name of the parent node |
| 15 | */ |
| 16 | public TreeNode(String name, String olds, int nodeId) { |
| 17 | //this.taxId = id; |
| 18 | this.orgName = name; |
| 19 | //this.children = kids; |
| 20 | this.parentName = olds; |
| 21 | this.nodeId = nodeId; |
| 22 | |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Add a child node to the HashSet of children nodes for this node. |
| 27 | * |
| 28 | * @param kid Name of child node/organism. |
| 29 | */ |
| 30 | public void addChildren(String kid) { |
| 31 | childNames.add(kid); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Add a descendant node to the HashSet of descendant nodes for this node. |
| 36 | * |
| 37 | * @param kid Name of child node/organism. |
| 38 | */ |
| 39 | public void nodeAddDescendantNames(HashSet<String> desNames) { |
| 40 | //Iterate over child nodes |
| 41 | for(TreeNode childNode : childNodes) { |
| 42 | |
| 43 | //If the name of the descendant node is not equal to this node |
| 44 | //This is only applicable for the "life"/"0" node |
| 45 | if(childNode.orgName != orgName) { |
| 46 | |
| 47 | //Add the descendant node name to the input HashSet of names. |
| 48 | desNames.add(childNode.orgName); |
| 49 | |
| 50 | //Run this function on the child nodes of each child node of the starting node. |
| 51 | childNode.nodeAddDescendantNames(desNames); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return HashSet containing the names of descendant nodes. |
| 58 | * @return descendantNames HashSet<String> of descendant nodes. |
| 59 | */ |
| 60 | public HashSet getDescendantNames() { |
| 61 | return descendentNames; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns HashSet of child nodes for this node. |
| 66 | * |
| 67 | * @return children HashSet of child nodes. |
| 68 | */ |
| 69 | public HashSet<String> getChildren() { |
| 70 | return childNames; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the parent node of this node. |
| 75 | * |
| 76 | * @return parent The name of the parent node. |
| 77 | */ |
| 78 | public String getParentName() { |
| 79 | return parentName; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns a string of the structure <Organism name>, <Parent Organism/node name>, <Child node names if any>. |
| 84 | */ |
| 85 | public String toString() { |
| 86 | return "Name = " + orgName + ", Parent = " + parentName + ", Child names = " + childNames + ", Level = " + level |
| 87 | + ", Nodes with identity = " + nodesWithIdentity + ", Identity = " + identity + |
| 88 | ", Average identity" + averageIdentity(); |
| 89 | } |
| 90 | |
| 91 | // /** |
| 92 | // * Add similarity values and child node names to a HashMap. |
| 93 | // * |
| 94 | // * @param childName Name of direct child node. |
| 95 | // * @param similarity Similarity percentage between child node and this node. |
| 96 | // */ |
| 97 | // public void addChildSim(String childName, double similarity) { |
| 98 | // childSims.put(childName, similarity); |
| 99 | // } |
| 100 | |
| 101 | // /** |
| 102 | // * Returns the similarity percentage between this node and a child node. |
| 103 | // * |
| 104 | // * @param childName The name of the child node. |
| 105 | // * @return double similarity percentage |
| 106 | // */ |
| 107 | // public double getChildSim(String childName) { |
| 108 | // double sim = childSims.get(childName); |
| 109 | // return sim; |
| 110 | // } |
| 111 | |
| 112 | // /** |
| 113 | // * Takes a double similarity percentage and associates that with the parent node. |
| 114 | // * |
| 115 | // * @param similarity Percentage similarity between node and parent of type double |
| 116 | // */ |
| 117 | // public void addParSim(double similarity) { |
| 118 | // parSim = similarity; |
| 119 | // } |
| 120 | |
| 121 | // /** |
| 122 | // * Returns the minimum similarity percentage of all descendants. |
| 123 | // * |
| 124 | // * @return Similarity between node and child node with lowest similarity. |
| 125 | // */ |
| 126 | // public double minimumDescendantSim() { |
| 127 | // |
| 128 | // for(String childName : childNames) { |
| 129 | // if(childSims.get(childName) < minChildSim) { |
| 130 | // minChildSim = childSims.get(childName); |
| 131 | // //minChildName = childName; |
| 132 | // } |
| 133 | // } |
| 134 | // return minChildSim; |
| 135 | // } |
| 136 | |
| 137 | // /** |
| 138 | // * Returns name of child node with lowest similarity to this node. |
| 139 | // * |
| 140 | // * @return Node name of child with lowest similarity (type String). |
| 141 | // */ |
| 142 | // public String minimumDescendantName() { |
| 143 | // for(String childName : childNames) { |
| 144 | // |
| 145 | // if(childSims.get(childName) < minChildSim) { |
| 146 | // minChildName = childName; |
| 147 | // } |
| 148 | // } |
| 149 | // return minChildName; |
| 150 | // } |
| 151 | |
| 152 | /** |
| 153 | * Adds name and similarity to HashMap if flagged as higher than a parent or child similarity. |
| 154 | * |
| 155 | * @param orgName |
| 156 | * @param sim |
| 157 | */ |
| 158 | public void flagRelation(String orgName, double sim) { |
| 159 | flaggedRelationships.put(orgName, sim); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns HashMap holding all flagged relationships (nodes with higher similarity than |
| 164 | * this nodes parent or the lowest similarity child). |
| 165 | * @return HashMap |
| 166 | */ |
| 167 | public HashMap<String, Double> getFlaggedRelations(){ |
| 168 | return flaggedRelationships; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Add level value to node in the tree |
| 173 | * @param lvl Level of node in tree (type int). |
| 174 | */ |
| 175 | public void addLevel(int lvl) { |
| 176 | level = lvl; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Adds hierarchical levels to nodes recursively through the tree. |
| 181 | * @param level_ |
| 182 | */ |
| 183 | public void traverse(int level_) { |
| 184 | |
| 185 | level = level_; |
| 186 | level_++; |
| 187 | |
| 188 | |
| 189 | for(TreeNode childNode : childNodes) { |
| 190 | |
| 191 | if(childNode.orgName != orgName) { |
| 192 | childNode.traverse(level_); |
| 193 | |
| 194 | } |
| 195 | //childNode.traverse(level_); |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns hierarchical level of the node. |
| 202 | * @return int level |
| 203 | */ |
| 204 | public int getLevel() { |
| 205 | return level; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Tests to see if argument is descendant of this. |
| 211 | * A node is considered a descendant of itself. |
| 212 | * @param nodeB |
| 213 | * @return true if nodeB is a descendant of this |
| 214 | */ |
| 215 | public boolean isDescendantOf(final TreeNode nodeB) { |
| 216 | if(this == nodeB) {return true;} |
| 217 | else if(this.parentNode == this){return false;} |
| 218 | else {return parentNode.isDescendantOf(nodeB);} |
| 219 | } |
| 220 | |
| 221 | |
| 222 | public boolean isAncestorOf(final TreeNode nodeB) { |
| 223 | return nodeB.isDescendantOf(this); |
| 224 | } |
| 225 | |
| 226 | public void resetIdentity() { |
| 227 | identity = 0; |
| 228 | identitySum = 0; |
| 229 | nodesWithIdentity = 0; |
| 230 | sizeSum = 0; |
| 231 | |
| 232 | |
| 233 | for(TreeNode childNode : childNodes) { |
| 234 | |
| 235 | if(childNode.orgName != orgName) { |
| 236 | childNode.resetIdentity(); |
| 237 | |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Set nodes identity to the average identity of its descendants. |
| 244 | * |
| 245 | * @param queryNode int ID of node relative to this node. |
| 246 | */ |
| 247 | public void percolateIdentityUp(int queryNode) { |
| 248 | |
| 249 | //If THIS nodes identity is greater than 0 (has an identity at all), |
| 250 | //and if THIS nodes ID isn't equal to the input query node |
| 251 | //nodes with identity becomes 1, |
| 252 | //sizeSum (number of nodes accounted for) is set to size (usually 0), |
| 253 | //identitySum (total of all identities) is set to THIS nodes identity. |
| 254 | if(identity > 0 && nodeId != queryNode) {nodesWithIdentity = 1; sizeSum = size; identitySum = identity;} |
| 255 | |
| 256 | //Iterate over the child nodes of THIS node. |
| 257 | for(TreeNode childNode : childNodes) { |
| 258 | |
| 259 | //If this node isn't a child of itself (handles the life or "0" node). |
| 260 | if(childNode.orgName != orgName) { |
| 261 | |
| 262 | //Calls percolateIdentityUp method recursively, pulling identities up to this node. |
| 263 | childNode.percolateIdentityUp(queryNode); |
| 264 | |
| 265 | nodesWithIdentity+=childNode.nodesWithIdentity; |
| 266 | identitySum+=childNode.identitySum; |
| 267 | sizeSum+=childNode.sizeSum; |
| 268 | |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Returns the average identity of all descendant nodes to this node. |
| 275 | * @return int Average identity. |
| 276 | */ |
| 277 | public double averageIdentity() { |
| 278 | if(nodesWithIdentity < 1) {return 0;} |
| 279 | else {return identitySum / nodesWithIdentity;} |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Returns the nodeID. |
| 284 | * @return int. |
| 285 | */ |
| 286 | public int getNodeId() { |
| 287 | return nodeId; |
| 288 | } |
| 289 | |
| 290 | // /** |
| 291 | // * Returns the similarity of this node to its parent or |
| 292 | // * the average identity of the parent node. Whichever value is higher. |
| 293 | // * @return double Similarity value. |
| 294 | // */ |
| 295 | // public double parentSimilarity() { |
| 296 | // if(parentNode.identity > parentNode.averageIdentity()) { |
| 297 | // return parentNode.identity; |
| 298 | // } else { |
| 299 | // return parentNode.averageIdentity(); |
| 300 | // } |
| 301 | // } |
| 302 | |
| 303 | public double parentSimilarity() { |
| 304 | return parentNode.averageIdentity(); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Method to call toDot method without requiring input. |
| 309 | * @return StringBuilder from toDot. |
| 310 | */ |
| 311 | public StringBuilder toDot() { |
| 312 | return toDot(null); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Method to implement tree structure in GraphViz .dot format. |
| 317 | * @param sb StringBuilder. |
| 318 | * @return StringBuilder with structure of tree in .dot format. |
| 319 | */ |
| 320 | public StringBuilder toDot(StringBuilder sb) { |
| 321 | |
| 322 | //If the input StringBuilder is null, start a new String Builder |
| 323 | if(sb==null) {sb = new StringBuilder();} |
| 324 | |
| 325 | //Initialize first as true when sb is first initialized. |
| 326 | boolean first = sb.length() == 0; |
| 327 | |
| 328 | //If first == true, begin adding node and edge information for the current node |
| 329 | //to the StringBuilder. |
| 330 | if(first) { |
| 331 | |
| 332 | //First line of a .dot file. |
| 333 | sb.append("digraph g{\n"); |
| 334 | } |
| 335 | |
| 336 | //Node information for the .dot file. |
| 337 | sb.append("\t" + nodeId + " [label=\" Node ID= " + nodeId +"\\nID= " + String.format("%.2f", identity) + |
| 338 | "\\nAvg= " + String.format("%.2f", averageIdentity()) + "\"]\n"); |
| 339 | |
| 340 | //Iterate over child nodes and recursively call this method |
| 341 | //adding node connection to the StringBuilder. |
| 342 | for(TreeNode childNode : childNodes) { |
| 343 | if(childNode != this) { |
| 344 | childNode.toDot(sb); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | //iterate over child nodes and add edge information to the StringBuilder. |
| 349 | for(TreeNode childNode : childNodes) { |
| 350 | sb.append("\t" + nodeId + " -> " + childNode.nodeId + "\n"); |
| 351 | } |
| 352 | |
| 353 | //If first == true, append a final closing curly brace. |
| 354 | if(first) { |
| 355 | sb.append("}\n"); |
| 356 | } |
| 357 | return sb; |
| 358 | } |
| 359 | |
| 360 | // digraph g{ |
| 361 | // a [label="a\nidentity=98\navgident=5"] |
| 362 | // |
| 363 | // a -> b |
| 364 | // } |
| 365 | |
| 366 | /*--------------------------------------------------------------*/ |
| 367 | /*---------------- Fields ----------------*/ |
| 368 | /*--------------------------------------------------------------*/ |
| 369 | |
| 370 | /** |
| 371 | * The children/descendant nodes of this node. |
| 372 | */ |
| 373 | List<TreeNode> childNodes = new ArrayList<TreeNode>(); |
| 374 | |
| 375 | /** |
| 376 | * The parent node of this node. |
| 377 | */ |
| 378 | TreeNode parentNode = null; |
| 379 | |
| 380 | /** |
| 381 | * HashMap holding node names and similarities flagged as higher than similarities with |
| 382 | * direct children. |
| 383 | */ |
| 384 | HashMap<String, Double> flaggedRelationships = new HashMap<>(); |
| 385 | |
| 386 | //Minimum similarity of all child nodes and this node. |
| 387 | //double minChildSim = 100; |
| 388 | |
| 389 | //Name of child node with minimum similarity. |
| 390 | //String minChildName = null; |
| 391 | |
| 392 | //HashMap holding the names and similarity values between any direct children nodes |
| 393 | //and this node. |
| 394 | //HashMap<String, Double> childSims = new HashMap<>(); |
| 395 | |
| 396 | //Similarity percentage to the parent node. |
| 397 | //double parSim = -1; |
| 398 | |
| 399 | /** |
| 400 | * Organisms name associated with this node. |
| 401 | */ |
| 402 | String orgName; |
| 403 | |
| 404 | /** |
| 405 | * Taxanomic ID of this organism. |
| 406 | */ |
| 407 | int taxId; |
| 408 | /** |
| 409 | * int Node ID number. |
| 410 | */ |
| 411 | final int nodeId; |
| 412 | |
| 413 | /** |
| 414 | * HashSet of direct children of this node. |
| 415 | */ |
| 416 | HashSet<String> childNames=new HashSet<String>(); |
| 417 | |
| 418 | /** |
| 419 | * The names of descendant nodes of this node. |
| 420 | */ |
| 421 | HashSet<String> descendentNames=new HashSet<String>(); |
| 422 | |
| 423 | /** |
| 424 | * Name of the parent of this node. |
| 425 | */ |
| 426 | String parentName; |
| 427 | |
| 428 | /** |
| 429 | * Node level within the tree. Important for taxonomic classification. |
| 430 | */ |
| 431 | int level; |
| 432 | |
| 433 | /** |
| 434 | * Sequence similarity between this node and another, query node. |
| 435 | */ |
| 436 | double identity = 0.0; |
| 437 | |
| 438 | long size = 0; |
| 439 | |
| 440 | long descendantSize = 0; |
| 441 | |
| 442 | int numDescendants = 0; |
| 443 | |
| 444 | /** |
| 445 | * The identities of all children nodes to this node. |
| 446 | */ |
| 447 | double identitySum = 0; |
| 448 | |
| 449 | /** |
| 450 | * The number of children nodes to this node that have identities. |
| 451 | */ |
| 452 | long nodesWithIdentity = 0; |
| 453 | |
| 454 | long sizeSum = 0; |
| 455 | } |