| 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 | |
| 9 | public class NCBISparseSimilarityMatrix { |
| 10 | |
| 11 | /*--------------------------------------------------------------*/ |
| 12 | /*---------------- Initialization ----------------*/ |
| 13 | /*--------------------------------------------------------------*/ |
| 14 | |
| 15 | /** |
| 16 | * Takes in a file of sketch similarity percentages from SketchCompare. |
| 17 | * Returns a sparse matrix object containing each percentage |
| 18 | * |
| 19 | * @param inputFile The file containing pairwise comparisons of each sketch |
| 20 | * @throws FileNotFoundException |
| 21 | * @throws IOException |
| 22 | */ |
| 23 | public NCBISparseSimilarityMatrix(String inputFile, NCBISparseTree tree_, boolean ncbi) throws FileNotFoundException, IOException { |
| 24 | |
| 25 | //Assigns the input tree object to the tree variable. |
| 26 | tree = tree_; |
| 27 | |
| 28 | //Take file name as input for building tree of related nodes |
| 29 | in = inputFile; |
| 30 | |
| 31 | // //Read in file, add header line and add to header variable |
| 32 | // try (BufferedReader br = new BufferedReader(new FileReader(in))) { |
| 33 | // String line; |
| 34 | // |
| 35 | // //while line isn't empty, process |
| 36 | // while ((line = br.readLine()) != null) { |
| 37 | // |
| 38 | // //if line is the header line, split and assign to variable. |
| 39 | // //may be used when header becomes more complex |
| 40 | // if(line.startsWith("#")) {header=line.split("\t"); |
| 41 | // } else { |
| 42 | // |
| 43 | // //If not a header line, split on tab. |
| 44 | // String[] data = line.split("\t"); |
| 45 | // |
| 46 | // //Query organism is column 0. |
| 47 | // String queryName = data[0]; |
| 48 | // //String refName = data[1]; |
| 49 | // |
| 50 | // } |
| 51 | // } |
| 52 | // } |
| 53 | |
| 54 | //Get the total number of organisms in the tree. |
| 55 | orgCount = tree.getOrgCount(); |
| 56 | //System.out.println(orgCount); |
| 57 | |
| 58 | //Initialize the matrix with the appropriate size of all nodes. |
| 59 | sparseMatrix = new ArrayList[orgCount + 1]; |
| 60 | |
| 61 | //Iterate over the matrix and add an ArrayList<Comparison> to each ArrayList. |
| 62 | for(int i=0; i<sparseMatrix.length; i++) { |
| 63 | |
| 64 | sparseMatrix[i] = new ArrayList<NCBIComparison>(); |
| 65 | //System.out.println("Building matrix row"); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | //Begin reading the file a second time. |
| 70 | try (BufferedReader br = new BufferedReader(new FileReader(in))) { |
| 71 | String line; |
| 72 | |
| 73 | //while line isn't empty, process |
| 74 | while ((line = br.readLine()) != null) { |
| 75 | |
| 76 | //If line is the header line, split and assign to variable. |
| 77 | //may be used when header becomes more complex |
| 78 | if(line.startsWith("#")) {assert true; |
| 79 | } else { |
| 80 | |
| 81 | //If not a header line, split on tab. |
| 82 | String[] data = line.split("\t"); |
| 83 | |
| 84 | //System.out.println(line); |
| 85 | int queryTaxID = 0; |
| 86 | int refTaxID = 0; |
| 87 | |
| 88 | //Section handles the position of the taxon ID numbers the output |
| 89 | //of sketchsimilarities. |
| 90 | //in the NCBI nodes.dmp structure and a test dataset structure. |
| 91 | if(ncbi==true) { |
| 92 | |
| 93 | //Column 8 is query ID. |
| 94 | queryTaxID = Integer.valueOf(data[7]); |
| 95 | |
| 96 | //Column 9 is reference ID. |
| 97 | refTaxID = Integer.valueOf(data[8]); |
| 98 | |
| 99 | }else if(ncbi==false) { |
| 100 | |
| 101 | //Column 0 is query ID. |
| 102 | queryTaxID = Integer.valueOf(data[0]); |
| 103 | |
| 104 | //Column 1 is reference ID. |
| 105 | refTaxID = Integer.valueOf(data[1]); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | //Column 2 is the similarity percentage. |
| 110 | double similarity = Double.parseDouble(data[2]); |
| 111 | |
| 112 | //Check that both names are in the HashMap (too slow?) |
| 113 | if(tree.containsTaxID(queryTaxID)==true && tree.containsTaxID(refTaxID)==true) { |
| 114 | |
| 115 | //Get the positions assigned to both organisms. |
| 116 | int queryPos = taxIDToNodeId(queryTaxID); |
| 117 | int refPos = taxIDToNodeId(refTaxID); |
| 118 | |
| 119 | NCBIComparison currentComparison = new NCBIComparison(queryPos, refPos, similarity); |
| 120 | |
| 121 | //Add the similarity percentage to the appropriate matrix position. |
| 122 | sparseMatrix[queryPos].add(currentComparison); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Method for taking the node taxon ID and returning the node value |
| 131 | * @param taxID the organism taxon ID (int). |
| 132 | * @return NCBITreeNode The node of the organism taxon ID taken as input. |
| 133 | */ |
| 134 | public int taxIDToNodeId(int taxID) { |
| 135 | |
| 136 | //Get node from the tree corresponding to the taxon ID. |
| 137 | NCBITreeNode orgNode = tree.nodeMap.get(taxID); |
| 138 | |
| 139 | //Asserts the org nod is in the tree. |
| 140 | assert(orgNode != null) : taxID; |
| 141 | |
| 142 | //Return the int node ID. |
| 143 | return orgNode.nodeId; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Prints out the entire matrix. |
| 149 | * Impractical in cases of large input datasets. |
| 150 | * |
| 151 | */ |
| 152 | public String toString() { |
| 153 | StringBuilder sb=new StringBuilder(); |
| 154 | for (int i = 0; i < sparseMatrix.length; i++) { |
| 155 | for (int j = 0; j < sparseMatrix[i].size(); j++) { |
| 156 | sb.append(sparseMatrix[i].get(j) + " "); |
| 157 | } |
| 158 | sb.append('\n'); |
| 159 | } |
| 160 | return sb.toString(); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | //TODO: This method is slow and doesnt work, need something better. |
| 165 | // /** |
| 166 | // * Returns the similarity of two specified organisms. |
| 167 | // * Both organisms must have been compared using SketchCompare. |
| 168 | // * |
| 169 | // * @param org1 The Name of an organism. |
| 170 | // * @param org2 The name of a second organism. |
| 171 | // * @return similarity The Double percentage similarity between the two sketches. |
| 172 | // */ |
| 173 | // public Comparison getComparison(String org1, String org2) { |
| 174 | // int orgName1 = nameToNodeId(org1); |
| 175 | // int orgName2 = nameToNodeId(org2); |
| 176 | // |
| 177 | // return sparseMatrix[orgName1].get(orgName2); |
| 178 | // } |
| 179 | |
| 180 | |
| 181 | public int getSize() { |
| 182 | return orgCount; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | public ArrayList<NCBIComparison> getOrgRowByTaxonID(Integer taxonID) { |
| 187 | int rowNum = tree.nodeMap.get(taxonID).nodeId; |
| 188 | return sparseMatrix[rowNum]; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | public int numComparisonsByTaxonID(int taxonID_) { |
| 193 | return getOrgRowByTaxonID(taxonID_).size(); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /*--------------------------------------------------------------*/ |
| 198 | /*---------------- Fields ----------------*/ |
| 199 | /*--------------------------------------------------------------*/ |
| 200 | |
| 201 | /** |
| 202 | * A NCBISparseTree object that contains taxonomic information relevant to this matrix. |
| 203 | */ |
| 204 | final NCBISparseTree tree; |
| 205 | |
| 206 | /** |
| 207 | * An arraylist containing comparisons between nodes in the tree. |
| 208 | */ |
| 209 | private final ArrayList<NCBIComparison>[] sparseMatrix; |
| 210 | |
| 211 | /** |
| 212 | * The number of sketches being analyzed. |
| 213 | */ |
| 214 | private int orgCount; |
| 215 | |
| 216 | /** |
| 217 | * ArrayList that will hold the lines of the input file. |
| 218 | */ |
| 219 | ArrayList<String> lines = new ArrayList<String>(); |
| 220 | |
| 221 | /** |
| 222 | * Header line of the comparison input file. |
| 223 | */ |
| 224 | private String[] header; |
| 225 | |
| 226 | /** |
| 227 | * Input file name. |
| 228 | */ |
| 229 | private String in=null; |
| 230 | |
| 231 | /** |
| 232 | * Number of lines processed from the sketch comparison file. |
| 233 | */ |
| 234 | private long linesProcessed=0; |
| 235 | |
| 236 | } |