Debian Codegraph @

current/jasper/NCBISparseTree.java

raw symbols SCIP
1 package jasper;
2
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.Map.Entry;
10 import java.util.Set;
11
12 import shared.Tools;
13
14
15 public class NCBISparseTree {
16
17 /*--------------------------------------------------------------*/
18 /*---------------- Initialization ----------------*/
19 /*--------------------------------------------------------------*/
20
21 /**
22 * Takes in an input file with 2 columns (organism, parent organism) and adds these to TreeNodes
23 * TreeNodes are then added to a HashMap and children node values are added to each node if applicable
24 *
25 * @param inputFile The input file you wish to have values added to the Tree object
26 * @throws FileNotFoundException
27 * @throws IOException
28 */
29 public NCBISparseTree(String inputFile) throws FileNotFoundException, IOException {
30
31 int nodeId = 0;
32 in = inputFile;
33
34 //parse file. create each node and place in
35 try (BufferedReader br = new BufferedReader(new FileReader(in))) {
36 String line;
37
38 while ((line = br.readLine()) != null) {
39
40
41 //if line is the header line, split and assign to variable.
42 if(line.startsWith("#")) {header=line.split("\t");
43 } else {
44 //String[] data = line.split("\t");
45 String[] data = Tools.tabPattern.split(line);
46
47 //Make sure you're not adding the header line to any data structure
48 //if(!Arrays.asList(header).contains(data[0])) {
49
50 //Create a TreeNode containing the name of the organism and the parent node/organism
51 //System.out.println(data[0]);
52
53 int taxID = Integer.valueOf(data[0]);
54 int parentTaxID = Integer.valueOf(data[2]);
55 String taxonomicRank = data[4];
56
57 NCBITreeNode orgNode = new NCBITreeNode(taxID, data[0], parentTaxID, nodeId, taxonomicRank);
58
59 if(nodeId == 0) {root = orgNode;}
60
61 nodeId++;
62
63 //Add node to HashMap nodes with the name of the organism as the key
64 nodeMap.put(taxID, orgNode);
65
66 nodeList.add(orgNode);
67
68 //Add line to lines list for further processing
69 lines.add(line);
70
71 //Increment linesProcessed
72 linesProcessed++;
73
74
75 //}
76 }
77 }
78 }
79
80 //Run method to add children nodes to each node if applicable
81 addChild(nodeMap, lines);
82
83 //return nodes;
84 }
85
86
87 /**
88 * Adds children node names to each node if applicable
89 *
90 * @param treeNodeMap HashMap of TreeNode objects
91 * @param lineList ArrayList<String> of lines from the input file
92 */
93 void addChild(HashMap<Integer, NCBITreeNode> treeNodeMap, ArrayList<String> lineList) {
94 int par;
95 int org;
96 String rank;
97
98 //iterate over lines from the file and split into the organism and the parent node
99 for(String line : lineList) {
100 //String[] split=line.split("\t");
101 String[] split = Tools.tabPattern.split(line);
102
103 //isolate the organism and the parent from the split
104 org = Integer.valueOf(split[0]);
105 par = Integer.valueOf(split[2]);
106 rank = split[4];
107
108 //get the organism node and parent node
109 NCBITreeNode orgNode = treeNodeMap.get(org);
110 NCBITreeNode parNode = treeNodeMap.get(par);
111
112 //Assert parent node isn't empty or parent node is the 0/life node.
113 assert(parNode != null || par == 1);
114
115 //Assert the query organism node isn't empty, if it is, return node name.
116 assert(orgNode != null): org;
117
118 //Add the child node name to the query node.
119 parNode.addChildren(org);
120
121 //add query node to its parent node's list of children nodes.
122 parNode.childNodes.add(orgNode);
123
124 orgNode.parentNode = parNode;
125
126 }
127
128 }
129
130 /**
131 * Returns a StringBuilder of names of organisms/nodes along with
132 * the parent node and the names of children nodes.
133 *
134 * @return StringBuilder
135 */
136 public String toString() {
137 StringBuilder sb=new StringBuilder();
138 for(Entry<Integer, NCBITreeNode> e : nodeMap.entrySet()) {
139
140 NCBITreeNode tn = e.getValue();
141
142 sb.append(tn);
143 sb.append('\n');
144 }
145 return sb.toString();
146 }
147
148 /**
149 * Returns Set<String> of node keys for the tree.
150 * @return Set<String>
151 */
152 public Set<Integer> keySet() {
153 return nodeMap.keySet();
154 }
155
156 /**
157 * Starting point for adding levels to nodes in the tree.
158 * @param nodeID_ Lowest node name, corresponding to "Life"
159 */
160 public void beginTraverse(int nodeID_) {
161 NCBITreeNode firstNode = nodeMap.get(nodeID_);
162 firstNode.assignLevels(1, "life");
163 }
164
165 /**
166 * Returns TreeNode from tree based on String node name.
167 * @param TaxonID String name of node (organism/file)
168 * @return TreeNode.
169 */
170 public NCBITreeNode getNodeByTaxID(int TaxonID) {
171 return nodeMap.get(TaxonID);
172 }
173
174 /**
175 * Returns TreeNode from the tree based on the node ID
176 * @param nodeID int nodeId
177 * @return TreeNode.
178 */
179 public NCBITreeNode getNodeByNodeID(int nodeID) {
180 return nodeList.get(nodeID);
181 }
182
183 /**
184 * Currently takes node and adds all descendant node names to a HashSet<String>.
185 * Returned with the getDescendentNames method.
186 * @param nodeName
187 */
188 public void beginAddDescendants(int taxID_) {
189
190 //Place target node in variable
191 NCBITreeNode earliestNode = nodeMap.get(taxID_);
192
193 //Run the method to add descendant names to HashSet in each node.
194 earliestNode.nodeAddDescendantNames(nodeMap.get(taxID_).descendentIDs);
195 }
196
197
198 public void assignMatrixIdentity(NCBISparseSimilarityMatrix matrix, TreeNode node) {
199
200 }
201
202 /**
203 * Returns boolean of whether the String organism name/node name is found in the tree.
204 * @param orgName String organism/node name.
205 * @return boolean
206 */
207 public boolean containsTaxID(int orgTaxID) {
208 return nodeMap.containsKey(orgTaxID);
209 }
210
211 // //TODO: make a more efficient method of getting the total node count.
212 // public int getOrgCount() {
213 // int max = 0;
214 // for(String node : nodeMap.keySet()) {
215 // int id = this.getNode(node).getNodeId();
216 // if(id > max) {
217 // max = id;
218 // }
219 // }
220 // return max;
221 // }
222
223 /**
224 * Returns the size of the HashMap containing all nodes in the tree.
225 * @return int TreeNode count.
226 */
227 public int getOrgCount() {
228
229 //Return the size of the Set of keys in the nodeMap HashMap.
230 return nodeList.size();
231 }
232
233 /**
234 * Sets the identity of all other TreeNodes in relation to the input TreeNode name.
235 * @param keyNode TreeNode query node.
236 * @param matrix SparseSimilarityMatrix name containing similarity Comparison objects.
237 */
238 public void setIdentity(NCBITreeNode keyNode, NCBISparseSimilarityMatrix matrix) {
239
240 //Get the row containing all Comparisons for the query node.
241 ArrayList<NCBIComparison> row = matrix.getOrgRowByTaxonID(keyNode.taxID);
242
243 int votes = NCBISparseTreeValidate.MAX_VOTES;
244
245 //Iterate over the row.
246 for(int i=0; i<row.size(); i++) {
247
248 //Get a Comparison object from the row.
249 NCBIComparison c = row.get(i);
250
251 //Get the other nodes ID from the Comparison object.
252 int otherNodeId = c.refNodeID;
253
254 //Get the TreeNode of the node being compared to the query node.
255 NCBITreeNode otherNode = nodeList.get(otherNodeId);
256
257 //Set the other nodes identity to the value in the comparison.
258 otherNode.identity = c.identity;
259
260 otherNode.votes = votes;
261 votes = Tools.max(votes - 1, 0);
262
263 }
264 }
265
266
267
268 /*--------------------------------------------------------------*/
269 /*---------------- Fields ----------------*/
270 /*--------------------------------------------------------------*/
271
272 /**
273 * Root node of the tree. Usually the "life" node or node 1 in NCBI.
274 */
275 NCBITreeNode root;
276
277 /**
278 * HashMap holding the taxon ID of the organisms as keys and the organism node as values.
279 *
280 */
281 HashMap<Integer, NCBITreeNode> nodeMap = new HashMap<Integer, NCBITreeNode>();
282
283 /**
284 * ArrayList of NCBITreeNode objects.
285 */
286 ArrayList<NCBITreeNode> nodeList = new ArrayList<NCBITreeNode>();
287
288 /**
289 * ArrayList of all lines in input file. Need these to fill in values for children nodes
290 */
291 ArrayList<String> lines = new ArrayList<String>();
292
293 /**
294 * Header line of input file.
295 */
296 private String[] header;
297
298 /**
299 * Input file name.
300 */
301 private String in=null;
302
303 /**
304 * Number of lines processed for data from input file.
305 */
306 private long linesProcessed=0;
307
308 /**
309 * Node level counter.
310 */
311 int orgLvl = 0;
312
313 //int orgCount = 0;
314 }