| 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 SparseTree { |
| 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 SparseTree(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 TreeNode from tree based on String node name. |
| 155 | * @param nodeName String name of node (organism/file) |
| 156 | * @return TreeNode. |
| 157 | */ |
| 158 | public TreeNode getNode(String nodeName) { |
| 159 | return nodeMap.get(nodeName); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns TreeNode from the tree based on the node ID |
| 164 | * @param nodeID int nodeId |
| 165 | * @return TreeNode. |
| 166 | */ |
| 167 | public TreeNode getNode(int nodeID) { |
| 168 | return nodeList.get(nodeID); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Currently takes node and adds all descendant node names to a HashSet<String>. |
| 173 | * Returned with the getDescendentNames method. |
| 174 | * @param nodeName |
| 175 | */ |
| 176 | public void beginAddDescendants(String nodeName) { |
| 177 | |
| 178 | //Place target node in variable |
| 179 | TreeNode earliestNode = nodeMap.get(nodeName); |
| 180 | |
| 181 | //Run the method to add descendant names to HashSet in each node. |
| 182 | earliestNode.nodeAddDescendantNames(nodeMap.get(nodeName).descendentNames); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | public void assignMatrixIdentity(SparseSimilarityMatrix matrix, TreeNode node) { |
| 187 | |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns boolean of whether the String organism name/node name is found in the tree. |
| 192 | * @param orgName String organism/node name. |
| 193 | * @return boolean |
| 194 | */ |
| 195 | public boolean containsName(String orgName) { |
| 196 | return nodeMap.containsKey(orgName); |
| 197 | } |
| 198 | |
| 199 | // //TODO: make a more efficient method of getting the total node count. |
| 200 | // public int getOrgCount() { |
| 201 | // int max = 0; |
| 202 | // for(String node : nodeMap.keySet()) { |
| 203 | // int id = this.getNode(node).getNodeId(); |
| 204 | // if(id > max) { |
| 205 | // max = id; |
| 206 | // } |
| 207 | // } |
| 208 | // return max; |
| 209 | // } |
| 210 | |
| 211 | /** |
| 212 | * Returns the size of the HashMap containing all nodes in the tree. |
| 213 | * @return int TreeNode count. |
| 214 | */ |
| 215 | public int getOrgCount() { |
| 216 | |
| 217 | //Return the size of the Set of keys in the nodeMap HashMap. |
| 218 | return nodeMap.keySet().size(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the identity of all other TreeNodes in relation to the input TreeNode name. |
| 223 | * @param node TreeNode query node. |
| 224 | * @param matrix SparseSimilarityMatrix name containing similarity Comparison objects. |
| 225 | */ |
| 226 | public void setIdentity(TreeNode node, SparseSimilarityMatrix matrix) { |
| 227 | |
| 228 | //Get the row containing all Comparisons for the query node. |
| 229 | ArrayList<Comparison> row = matrix.getOrgRow(node.orgName); |
| 230 | |
| 231 | //Iterate over the row. |
| 232 | for(int i=0; i<row.size(); i++) { |
| 233 | |
| 234 | //Get a Comparison object from the row. |
| 235 | Comparison c = row.get(i); |
| 236 | |
| 237 | //Get the other nodes ID from the Comparison object. |
| 238 | int otherNodeId = c.refID; |
| 239 | |
| 240 | //Get the TreeNode of the node being compared to the query node. |
| 241 | TreeNode otherNode = nodeList.get(otherNodeId); |
| 242 | |
| 243 | //Set the other nodes identity to the value in the comparison. |
| 244 | otherNode.identity = c.identity; |
| 245 | |
| 246 | |
| 247 | |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |
| 253 | /*--------------------------------------------------------------*/ |
| 254 | /*---------------- Fields ----------------*/ |
| 255 | /*--------------------------------------------------------------*/ |
| 256 | |
| 257 | TreeNode root; |
| 258 | |
| 259 | //HashMap holding the names of the organisms as keys and the organism node as values |
| 260 | HashMap<String, TreeNode> nodeMap = new HashMap<String, TreeNode>(); |
| 261 | |
| 262 | ArrayList<TreeNode> nodeList = new ArrayList<TreeNode>(); |
| 263 | |
| 264 | //ArrayList of all lines in input file. need these later to fill in values for children nodes |
| 265 | ArrayList<String> lines = new ArrayList<String>(); |
| 266 | |
| 267 | //Header line of input file |
| 268 | private String[] header; |
| 269 | |
| 270 | //Input file name |
| 271 | private String in=null; |
| 272 | |
| 273 | //Number of lines processed for data from input file |
| 274 | private long linesProcessed=0; |
| 275 | |
| 276 | //Node level counter |
| 277 | int orgLvl = 0; |
| 278 | |
| 279 | int orgCount = 0; |
| 280 | } |