Debian Codegraph @

current/jasper/DenseSimilarityMatrix.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
9 public class DenseSimilarityMatrix {
10
11 /*--------------------------------------------------------------*/
12 /*---------------- Initialization ----------------*/
13 /*--------------------------------------------------------------*/
14
15 /**
16 * Takes in a file of sketch similarity percentages from SketchCompare.
17 * Returns an 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 DenseSimilarityMatrix(String inputFile, DenseTree tree_) throws FileNotFoundException, IOException {
24
25 tree = tree_;
26
27 //Take file name as input for building tree of related nodes
28 in = inputFile;
29
30 //Read in file, add header line and add to header variable
31 try (BufferedReader br = new BufferedReader(new FileReader(in))) {
32 String line;
33
34 //while line isn't empty, process
35 while ((line = br.readLine()) != null) {
36
37 //if line is the header line, split and assign to variable.
38 //may be used when header becomes more complex
39 if(line.startsWith("#")) {header=line.split("\t");
40 } else {
41
42 //If not a header line, split on tab.
43 String[] data = line.split("\t");
44
45 //Query organism is column 0.
46 String queryName = data[0];
47 //String refName = data[1];
48
49 //If query name isn't in the HashMap already, add it.
50 //if(orgPosMap.containsKey(queryName)==false) {
51 // orgPosMap.put(queryName, orgCount);
52
53 //Update count of organisms in HashMap.
54 //This is used for the size of the matrix.
55 //orgCount++;
56 //}
57 }
58 }
59 }
60
61 orgCount = tree.getOrgCount();
62
63 //Initialize the matrix with the appropriate sizes.
64 matrix = new double[orgCount + 1][orgCount + 1];
65
66 //Begin reading the file a second time.
67 try (BufferedReader br = new BufferedReader(new FileReader(in))) {
68 String line;
69
70 //while line isn't empty, process
71 while ((line = br.readLine()) != null) {
72
73 //If line is the header line, split and assign to variable.
74 //may be used when header becomes more complex
75 if(line.startsWith("#")) {assert true;
76 } else {
77
78 //If not a header line, split on tab.
79 String[] data = line.split("\t");
80
81 //Column 0 is query name.
82 String queryName = data[0];
83
84 //Column 1 is reference name.
85 String refName = data[1];
86
87 //Column 2 is the similarity percentage.
88 double similarity = Double.parseDouble(data[2]);
89
90 //Check that both names are in the HashMap (too slow?)
91 if(tree.containsName(queryName)==true && tree.containsName(refName)) {
92
93 //Get the positions assigned to both organisms.
94 int queryPos = nameToNodeId(queryName);
95 int refPos = nameToNodeId(refName);
96
97 //Add the similarity percentage to the appropriate matrix position.
98 matrix[queryPos][refPos] = similarity;
99 }
100 }
101 }
102 }
103 }
104
105 public int nameToNodeId(String orgName) {
106 TreeNode org = tree.getNode(orgName);
107
108 assert(org != null) : orgName;
109
110 return org.nodeId;
111 }
112
113
114 /**
115 * Prints out the entire matrix.
116 * Impractical in cases of large input datasets.
117 *
118 */
119 public String toString() {
120 StringBuilder sb=new StringBuilder();
121 for (int i = 0; i < matrix.length; i++) {
122 for (int j = 0; j < matrix[i].length; j++) {
123 sb.append(matrix[i][j] + " ");
124 }
125 sb.append('\n');
126 }
127 return sb.toString();
128 }
129
130 /**
131 * Returns the similarity of two specified organisms.
132 * Both organisms must have been compared using SketchCompare.
133 *
134 * @param org1 The Name of an organism.
135 * @param org2 The name of a second organism.
136 * @return similarity The Double percentage similarity between the two sketches.
137 */
138 public Double getSimilarity(String org1, String org2) {
139 int orgName1 = nameToNodeId(org1);
140 int orgName2 = nameToNodeId(org2);
141
142 return matrix[orgName1][orgName2];
143 }
144
145
146 public int getSize() {
147 return orgCount;
148 }
149
150
151 public double[] getOrgRow(String orgName) {
152 int rowNum = tree.nodeMap.get(orgName).nodeId;
153 return matrix[rowNum];
154 }
155
156
157 /*--------------------------------------------------------------*/
158 /*---------------- Fields ----------------*/
159 /*--------------------------------------------------------------*/
160
161 final DenseTree tree;
162
163 //Matrix that will hold the percentages of sketch comparisons
164 private double[][] matrix;
165
166 //The number of sketches being analyzed
167 private int orgCount;
168
169 //ArrayList that will hold the lines of the input file
170 ArrayList<String> lines = new ArrayList<String>();
171
172 //Set that will hold the names of the organisms being compared in the input file
173 //Set<String> nameSet = new HashSet<String>();
174
175 //HashMap containing the name of the organism and its position within the matrix
176 //HashMap<String, Integer> orgPosMap = new HashMap<>();
177
178 //Header line of the comparison input file
179 private String[] header;
180
181 //Input file name
182 private String in=null;
183
184 //Number of lines processed from the sketch comparison file
185 private long linesProcessed=0;
186
187 }