Debian Codegraph @

current/jasper/DenseTree.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.Arrays;
9 import java.util.HashMap;
10 import java.util.Map.Entry;
11 import java.util.Set;
12
13
14 public class DenseTree {
15
16 /*--------------------------------------------------------------*/
17 /*---------------- Initialization ----------------*/
18 /*--------------------------------------------------------------*/
19
20 /**
21 * Takes in an input file with 2 columns (organism, parent organism) and adds these to TreeNodes
22 * TreeNodes are then added to a HashMap and children node values are added to each node if applicable
23 *
24 * @param inputFile The input file you wish to have values added to the Tree object
25 * @throws FileNotFoundException
26 * @throws IOException
27 */
28 public DenseTree(String inputFile) throws FileNotFoundException, IOException {
29
30 int nodeId = 0;
31 in = inputFile;
32
33 //parse file. create each node and place in
34 try (BufferedReader br = new BufferedReader(new FileReader(in))) {
35 String line;
36
37 while ((line = br.readLine()) != null) {
38
39
40 //if line is the header line, split and assign to variable.
41 if(line.startsWith("#")) {header=line.split("\t");
42 } else {
43 String[] data = line.split("\t");
44
45 //Make sure you're not adding the header line to any data structure
46 if(!Arrays.asList(header).contains(data[0])) {
47
48 //Create a TreeNode containing the name of the organism and the parent node/organism
49 //System.out.println(data[0]);
50 TreeNode orgNode = new TreeNode(data[0], data[1], nodeId);
51
52 if(nodeId == 0) {root = orgNode;}
53
54 nodeId++;
55
56 //Add node to HashMap nodes with the name of the organism as the key
57 nodeMap.put(data[0], orgNode);
58
59 nodeList.add(orgNode);
60
61 //Add line to lines list for further processing
62 lines.add(line);
63
64 //Increment linesProcessed
65 linesProcessed++;
66 }
67 }
68 }
69 }
70
71 //Run method to add children nodes to each node if applicable
72 addChild(nodeMap, lines);
73
74 //return nodes;
75 }
76
77
78 /**
79 * Adds children node names to each node if applicable
80 *
81 * @param treeNodeMap HashMap of TreeNode objects
82 * @param lineList ArrayList<String> of lines from the input file
83 */
84 void addChild(HashMap<String, TreeNode> treeNodeMap, ArrayList<String> lineList) {
85 String par;
86 String org;
87
88 //iterate over lines from the file and split into the organism and the parent node
89 for(String line : lineList) {
90 String[] split=line.split("\t");
91
92 //isolate the organism and the parent from the split
93 org = split[0];
94 par = split[1];
95
96 //get the organism node and parent node
97 TreeNode orgNode = treeNodeMap.get(org);
98 TreeNode parNode = treeNodeMap.get(par);
99
100 //Assert parent node isn't empty or parent node is the 0/life node.
101 assert(parNode != null || par.equals("0"));
102
103 //Assert the query organism node isn't empty, if it is, return node name.
104 assert(orgNode != null): org;
105
106 //Add the child node name to the query node.
107 parNode.addChildren(org);
108
109 //add query node to its parent node's list of children nodes.
110 parNode.childNodes.add(orgNode);
111
112 orgNode.parentNode = parNode;
113
114 }
115
116 }
117
118 /**
119 * Returns a StringBuilder of names of organisms/nodes along with
120 * the parent node and the names of children nodes.
121 *
122 * @return StringBuilder
123 */
124 public String toString() {
125 StringBuilder sb=new StringBuilder();
126 for(Entry<String, TreeNode> e : nodeMap.entrySet()) {
127
128 TreeNode tn = e.getValue();
129
130 sb.append(tn);
131 sb.append('\n');
132 }
133 return sb.toString();
134 }
135
136 /**
137 * Returns Set<String> of node keys for the tree.
138 * @return Set<String>
139 */
140 public Set<String> keySet() {
141 return nodeMap.keySet();
142 }
143
144 /**
145 * Starting point for adding levels to nodes in the tree.
146 * @param nodeName Lowest node name, corresponding to "Life"
147 */
148 public void beginTraverse(String nodeName) {
149 TreeNode firstNode = nodeMap.get(nodeName);
150 firstNode.traverse(0);
151 }
152
153 /**
154 * Returns node from tree.
155 * @param nodeName
156 * @return
157 */
158 public TreeNode getNode(String nodeName) {
159 return nodeMap.get(nodeName);
160 }
161
162 public TreeNode getNode(int nodeId) {
163 return nodeList.get(nodeId);
164 }
165
166 /**
167 * Currently takes node and adds all descendant node names to a HashSet<String>.
168 * Returned with the getDescendentNames method.
169 * @param nodeName
170 */
171 public void beginAddDescendants(String nodeName) {
172 //Place target node in variable
173 TreeNode earliestNode = nodeMap.get(nodeName);
174
175 //Run the method to add descendant names to HashSet in each node.
176 earliestNode.nodeAddDescendantNames(nodeMap.get(nodeName).descendentNames);
177 }
178
179
180 public void assignMatrixIdentity(DenseSimilarityMatrix matrix, TreeNode node) {
181
182 }
183
184 public boolean containsName(String orgName) {
185 return nodeMap.containsKey(orgName);
186 }
187
188 public int getOrgCount() {
189 int max = 0;
190 for(String node : nodeMap.keySet()) {
191 int id = this.getNode(node).getNodeId();
192 if(id > max) {
193 max = id;
194 }
195 }
196 return max;
197 }
198
199 public void setIdentity(TreeNode node, DenseSimilarityMatrix matrix) {
200
201 double[] row = matrix.getOrgRow(node.orgName);
202 for(int i=0; i<row.length; i++) {
203 double id = row[i];
204 nodeList.get(i).identity = id;
205 }
206 }
207
208
209
210 /*--------------------------------------------------------------*/
211 /*---------------- Fields ----------------*/
212 /*--------------------------------------------------------------*/
213
214 TreeNode root;
215
216 //HashMap holding the names of the organisms as keys and the organism node as values
217 HashMap<String, TreeNode> nodeMap = new HashMap<String, TreeNode>();
218
219 ArrayList<TreeNode> nodeList = new ArrayList<TreeNode>();
220
221 //ArrayList of all lines in input file. need these later to fill in values for children nodes
222 ArrayList<String> lines = new ArrayList<String>();
223
224 //Header line of input file
225 private String[] header;
226
227 //Input file name
228 private String in=null;
229
230 //Number of lines processed for data from input file
231 private long linesProcessed=0;
232
233 //Node level counter
234 int orgLvl = 0;
235
236 int orgCount = 0;
237 }