Debian Codegraph @

current/jasper/TaxCompare.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 import shared.Timer;
13
14
15 public class TaxCompare {
16
17 /*--------------------------------------------------------------*/
18 /*---------------- Initialization ----------------*/
19 /*--------------------------------------------------------------*/
20
21 public static void main(String[] args) throws FileNotFoundException, IOException {
22 //Start a timer immediately upon code entrance.
23 Timer t=new Timer();
24
25 TaxCompare x=new TaxCompare(args);
26
27 x.process(t);
28
29 //Organism cat = new Organism(9, "snake");
30 //cat.printOrg();
31
32 }
33
34 /**
35 * Constructor. Handle inputs.
36 * @param args Command line arguments
37 */
38 public TaxCompare(String[] args){
39
40 String arg=args[0];
41 String[] split=arg.split("=");
42 String a=split[0].toLowerCase();
43 String b=split.length>1 ? split[1] : null;
44 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
45 in = b;
46 }
47
48
49
50 /*--------------------------------------------------------------*/
51 /*---------------- Initialization Helpers ----------------*/
52 /*--------------------------------------------------------------*/
53
54
55
56 /*--------------------------------------------------------------*/
57 /*---------------- Outer Methods ----------------*/
58 /*--------------------------------------------------------------*/
59
60
61
62 /*--------------------------------------------------------------*/
63 /*---------------- Inner Methods ----------------*/
64 /*--------------------------------------------------------------*/
65 /**
66 * Process input sketch comparison file, pass names of organisms (eventually taxon IDs) to matrix.
67 *
68 * @param t
69 * @throws FileNotFoundException
70 * @throws IOException
71 */
72 void process(Timer t) throws FileNotFoundException, IOException{
73
74 //Set for taxon names.
75 Set<String> nameSet = new HashSet<String>();
76
77 //ArrayList of all names in sketch comparison file, not unique values.
78 ArrayList<String> names = new ArrayList<String>();
79
80 //ArrayList of all lines. need these later to fill in values for the matrix
81 //This will get ugly with large comparisons.
82 ArrayList<String> lines = new ArrayList<String>();
83
84 //Read in file, add header line and add to header variable
85 try (BufferedReader br = new BufferedReader(new FileReader(in))) {
86 String line;
87
88 while ((line = br.readLine()) != null) {
89
90 //if line is the header line, split and assign to variable.
91 if(line.startsWith("#")) {header=line.split("\t");
92 } else {
93 String[] data = line.split("\t");
94
95 //make sure the data in column 1 isnt in the header line
96 //column 1 should be query names
97 if(!Arrays.asList(header).contains(data[0])) {nameSet.add(data[0]);}
98 lines.add(line);
99
100 }
101
102 }
103
104 //create matrix and add values
105 double[][] matrix = new double[nameSet.size() + 1 ][nameSet.size() + 1];
106
107 //loop over lines and fill in matrix
108 for(int i=0; i<lines.size(); i++) {
109
110 fillMatrix(matrix, nameSet, lines.toArray()[i]);
111 }
112
113 System.out.println(nameSet);
114 for (int i = 0; i < matrix.length; i++) {
115 for (int j = 0; j < matrix[i].length; j++) {
116 System.out.print(matrix[i][j] + " ");
117 }
118 System.out.println();
119 }
120
121
122 }
123 t.stop();
124 System.out.println(t);
125
126 }
127
128 /**
129 * Fill matrix with relationship information of organisms output from sketch comparison.
130 *
131 * @param matrix Matrix of comparison percentage values.
132 * @param setNames Set of names of included organisms.
133 * @param object Line of sketch comparison output file.
134 */
135 void fillMatrix(double[][] matrix, Set<String> setNames, Object object) {
136 //System.out.println(object);
137 //cast line as string
138 String stringLine = (String) object;
139
140 //split line
141 String[] lineData = stringLine.split("\t");
142
143 //place both organism names in variables
144 //qName is the query, column 1
145 String qName = lineData[0];
146 String mName = lineData[1];
147 double similarity = Double.parseDouble(lineData[2]);
148
149 //set positions variables
150 int qPos = -1;
151 int mPos = -1;
152
153 //convert setNames to array that can be iterated over
154 String[] nameArray = setNames.toArray(new String[setNames.size()]);
155
156 //loop over setNames and get each organisms positions within the matrix
157 for(int i = 0; i<nameArray.length; i++) {
158 if(nameArray[i].contentEquals(qName)) {qPos = i;}
159 else if(nameArray[i].contentEquals(mName)) {mPos = i;}
160
161
162 //after finding both name positions, add similarity value to matrix
163 if(qPos!=-1 && mPos!=-1) {matrix[qPos][mPos] = similarity;}
164 }
165
166 //displays matrix
167 /*
168 for (int i = 0; i < matrix.length; i++) {
169 for (int j = 0; j < matrix[i].length; j++) {
170 System.out.print(matrix[i][j] + " ");
171 }
172 System.out.println();
173 }*/
174
175 }
176
177 /*--------------------------------------------------------------*/
178 /*---------------- Fields ----------------*/
179 /*--------------------------------------------------------------*/
180
181 private String in=null;
182 private String out=null;
183
184 /*--------------------------------------------------------------*/
185
186 private long linesProcessed=0;
187 private long linesOut=0;
188 private long bytesProcessed=0;
189 private long bytesOut=0;
190
191 /*--------------------------------------------------------------*/
192 /*---------------- Final Fields ----------------*/
193 /*--------------------------------------------------------------*/
194
195 private String[] header;
196 //private final FileFormat ffin;
197 //private final FileFormat ffout;
198
199 /*--------------------------------------------------------------*/
200 /*---------------- Common Fields ----------------*/
201 /*--------------------------------------------------------------*/
202
203 //private PrintStream outstream=System.err;
204 public static boolean verbose=false;
205 public boolean errorState=false;
206 private boolean overwrite=true;
207 private boolean append=false;
208 }