| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public NCBISparseTree(String inputFile) throws FileNotFoundException, IOException { |
| 30 |
|
| 31 |
int nodeId = 0; |
| 32 |
in = inputFile; |
| 33 |
|
| 34 |
|
| 35 |
try (BufferedReader br = new BufferedReader(new FileReader(in))) { |
| 36 |
String line; |
| 37 |
|
| 38 |
while ((line = br.readLine()) != null) { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
if(line.startsWith("#")) {header=line.split("\t"); |
| 43 |
} else { |
| 44 |
|
| 45 |
String[] data = Tools.tabPattern.split(line); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 64 |
nodeMap.put(taxID, orgNode); |
| 65 |
|
| 66 |
nodeList.add(orgNode); |
| 67 |
|
| 68 |
|
| 69 |
lines.add(line); |
| 70 |
|
| 71 |
|
| 72 |
linesProcessed++; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
addChild(nodeMap, lines); |
| 82 |
|
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void addChild(HashMap<Integer, NCBITreeNode> treeNodeMap, ArrayList<String> lineList) { |
| 94 |
int par; |
| 95 |
int org; |
| 96 |
String rank; |
| 97 |
|
| 98 |
|
| 99 |
for(String line : lineList) { |
| 100 |
|
| 101 |
String[] split = Tools.tabPattern.split(line); |
| 102 |
|
| 103 |
|
| 104 |
org = Integer.valueOf(split[0]); |
| 105 |
par = Integer.valueOf(split[2]); |
| 106 |
rank = split[4]; |
| 107 |
|
| 108 |
|
| 109 |
NCBITreeNode orgNode = treeNodeMap.get(org); |
| 110 |
NCBITreeNode parNode = treeNodeMap.get(par); |
| 111 |
|
| 112 |
|
| 113 |
assert(parNode != null || par == 1); |
| 114 |
|
| 115 |
|
| 116 |
assert(orgNode != null): org; |
| 117 |
|
| 118 |
|
| 119 |
parNode.addChildren(org); |
| 120 |
|
| 121 |
|
| 122 |
parNode.childNodes.add(orgNode); |
| 123 |
|
| 124 |
orgNode.parentNode = parNode; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 152 |
public Set<Integer> keySet() { |
| 153 |
return nodeMap.keySet(); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public void beginTraverse(int nodeID_) { |
| 161 |
NCBITreeNode firstNode = nodeMap.get(nodeID_); |
| 162 |
firstNode.assignLevels(1, "life"); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public NCBITreeNode getNodeByTaxID(int TaxonID) { |
| 171 |
return nodeMap.get(TaxonID); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public NCBITreeNode getNodeByNodeID(int nodeID) { |
| 180 |
return nodeList.get(nodeID); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public void beginAddDescendants(int taxID_) { |
| 189 |
|
| 190 |
|
| 191 |
NCBITreeNode earliestNode = nodeMap.get(taxID_); |
| 192 |
|
| 193 |
|
| 194 |
earliestNode.nodeAddDescendantNames(nodeMap.get(taxID_).descendentIDs); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
public void assignMatrixIdentity(NCBISparseSimilarityMatrix matrix, TreeNode node) { |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public boolean containsTaxID(int orgTaxID) { |
| 208 |
return nodeMap.containsKey(orgTaxID); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public int getOrgCount() { |
| 228 |
|
| 229 |
|
| 230 |
return nodeList.size(); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public void setIdentity(NCBITreeNode keyNode, NCBISparseSimilarityMatrix matrix) { |
| 239 |
|
| 240 |
|
| 241 |
ArrayList<NCBIComparison> row = matrix.getOrgRowByTaxonID(keyNode.taxID); |
| 242 |
|
| 243 |
int votes = NCBISparseTreeValidate.MAX_VOTES; |
| 244 |
|
| 245 |
|
| 246 |
for(int i=0; i<row.size(); i++) { |
| 247 |
|
| 248 |
|
| 249 |
NCBIComparison c = row.get(i); |
| 250 |
|
| 251 |
|
| 252 |
int otherNodeId = c.refNodeID; |
| 253 |
|
| 254 |
|
| 255 |
NCBITreeNode otherNode = nodeList.get(otherNodeId); |
| 256 |
|
| 257 |
|
| 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 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
NCBITreeNode root; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
HashMap<Integer, NCBITreeNode> nodeMap = new HashMap<Integer, NCBITreeNode>(); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
ArrayList<NCBITreeNode> nodeList = new ArrayList<NCBITreeNode>(); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
ArrayList<String> lines = new ArrayList<String>(); |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private String[] header; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private String in=null; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
private long linesProcessed=0; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
int orgLvl = 0; |
| 312 |
|
| 313 |
|
| 314 |
} |