Debian Codegraph @

current/jasper/SimilarityMatrix.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.HashSet;
10 import java.util.Set;
11
12 public class SimilarityMatrix {
13
14 //ArrayList that will hold the lines of the input file
15 ArrayList<String> lines = new ArrayList<String>();
16
17 //Set that will hold the names of the organisms being compared in the input file
18 Set<String> nameSet = new HashSet<String>();
19
20 /**
21 * Builds a similarity matrix from an input file of similarity percentages
22 *
23 * @param inputFile The file holding the output of Sketch comparisons.
24 * @throws FileNotFoundException
25 * @throws IOException
26 */
27 public SimilarityMatrix(String inputFile) throws FileNotFoundException, IOException {
28
29 //Take file name as input for building tree of related nodes
30 String[] split=inputFile.split("=");
31 String a=split[0].toLowerCase();
32 String b=split.length>1 ? split[1] : null;
33 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
34 in = b;
35
36 //Read in file, add header line and add to header variable
37 try (BufferedReader br = new BufferedReader(new FileReader(in))) {
38 String line;
39
40 //while line isn't empty, process
41 while ((line = br.readLine()) != null) {
42
43 //if line is the header line, split and assign to variable.
44 //may be used when header becomes more complex
45 if(line.startsWith("#")) {header=line.split("\t");
46 } else {
47 String[] data = line.split("\t");
48
49 //make sure the data in column 1 isn't in the header line
50 //column 1 should be query names
51 //Add the name of the query to the Set nameSet
52 if(!Arrays.asList(header).contains(data[0])) {nameSet.add(data[0]);}
53
54 //add line to list of lines
55 lines.add(line);
56
57 }
58
59 }
60 }
61
62 //current location of the matrix. Not the ideal place for it
63 double[][] matrix = new double[nameSet.size() + 1 ][nameSet.size() + 1];
64
65 //loop over lines and fill in matrix
66 for(int i=0; i<lines.size(); i++) {
67
68
69 fillMatrix(matrix, nameSet, lines.toArray()[i]);
70 }
71
72 //return matrix;
73 }
74
75 /**
76 * Fill matrix with relationship information of organisms output from sketch comparison.
77 *
78 * @param matrix Matrix of comparison percentage values.
79 * @param setNames Set of names of included organisms.
80 * @param object Line of sketch comparison output file.
81 */
82 void fillMatrix(double[][] matrix, Set<String> setNames, Object object) {
83 //System.out.println(object);
84 //cast line as string
85 String stringLine = (String) object;
86
87 //split line
88 String[] lineData = stringLine.split("\t");
89
90 //place both organism names in variables
91 //qName is the query, column 1
92 String queryName = lineData[0];
93 String altName = lineData[1];
94
95 //collect similarity percentage
96 double similarity = Double.parseDouble(lineData[2]);
97
98 //set positions variables
99 int qPos = -1;
100 int mPos = -1;
101
102 //convert setNames to array that can be iterated over
103 String[] nameArray = setNames.toArray(new String[setNames.size()]);
104
105 //loop over setNames and get each organisms positions within the matrix
106 //add the similarity percentage to the appropriate position within the matrix
107 for(int i = 0; i<nameArray.length; i++) {
108 if(nameArray[i].contentEquals(queryName)) {qPos = i;}
109 else if(nameArray[i].contentEquals(altName)) {mPos = i;}
110
111
112 //after finding both name positions, add similarity value to matrix
113 if(qPos!=-1 && mPos!=-1) {matrix[qPos][mPos] = similarity;}
114 }
115
116 }
117
118 /*
119 public void showMatrix() {
120 for (int i = 0; i < matrix.length; i++) {
121 for (int j = 0; j < matrix[i].length; j++) {
122 System.out.print(matrix[i][j] + " ");
123 }
124 System.out.println();
125 }
126 }
127 */
128
129 private String[] header;
130 private String in=null;
131 private long linesProcessed=0;
132 }