Debian Codegraph @

current/jasper/NCBITreeNode.java

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