| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.util; |
| 27 |
|
| 28 |
import java.io.*; |
| 29 |
import java.util.*; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public interface DataTable<T> { |
| 36 |
int getColumnCount(); |
| 37 |
|
| 38 |
int getRowCount(); |
| 39 |
|
| 40 |
String[] getColumnLabels(); |
| 41 |
String[] getRowLabels(); |
| 42 |
|
| 43 |
T getColumn(int columnIndex); |
| 44 |
T getRow(int rowIndex); |
| 45 |
|
| 46 |
T[] getData(); |
| 47 |
|
| 48 |
public class Double implements DataTable<double[]> { |
| 49 |
|
| 50 |
private Double(Reader source, boolean hasColumnLabels, boolean hasRowLabels, boolean isCSV) throws IOException { |
| 51 |
this(source, hasColumnLabels, hasRowLabels, false, isCSV); |
| 52 |
} |
| 53 |
|
| 54 |
private Double(Reader source, boolean hasColumnLabels, boolean hasRowLabels, boolean includeFirstColumnLabel, boolean isCSV) throws IOException { |
| 55 |
BufferedReader reader = new BufferedReader(source); |
| 56 |
|
| 57 |
String line = reader.readLine(); |
| 58 |
if (line == null) { |
| 59 |
throw new IllegalArgumentException("Empty file"); |
| 60 |
} |
| 61 |
|
| 62 |
int columnCount = -1; |
| 63 |
|
| 64 |
String delim = (isCSV ? "," : "\t"); |
| 65 |
|
| 66 |
if (hasColumnLabels) { |
| 67 |
List<String> columnLabels = new ArrayList<String>(); |
| 68 |
|
| 69 |
StringTokenizer tokenizer = new StringTokenizer(line, delim); |
| 70 |
|
| 71 |
if (hasRowLabels && !line.startsWith(delim)) { |
| 72 |
|
| 73 |
String name = tokenizer.nextToken().trim(); |
| 74 |
if (includeFirstColumnLabel) { |
| 75 |
columnLabels.add(name); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
while (tokenizer.hasMoreTokens()) { |
| 80 |
String label = tokenizer.nextToken().trim(); |
| 81 |
columnLabels.add(label); |
| 82 |
} |
| 83 |
|
| 84 |
this.columnLabels = new String[columnLabels.size()]; |
| 85 |
columnLabels.toArray(this.columnLabels); |
| 86 |
|
| 87 |
columnCount = columnLabels.size(); |
| 88 |
|
| 89 |
line = reader.readLine(); |
| 90 |
} |
| 91 |
|
| 92 |
List<String> rowLabels = new ArrayList<String>(); |
| 93 |
List<double[]> rows = new ArrayList<double[]>(); |
| 94 |
|
| 95 |
int rowIndex = 1; |
| 96 |
|
| 97 |
while (line != null) { |
| 98 |
StringTokenizer tokenizer = new StringTokenizer(line, delim); |
| 99 |
|
| 100 |
if (columnCount == -1) { |
| 101 |
columnCount = tokenizer.countTokens(); |
| 102 |
if (hasRowLabels) { |
| 103 |
columnCount --; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (hasRowLabels) { |
| 108 |
String label = tokenizer.nextToken().trim(); |
| 109 |
rowLabels.add(label); |
| 110 |
} |
| 111 |
|
| 112 |
double[] row = new double[columnCount]; |
| 113 |
|
| 114 |
int columnIndex = 0; |
| 115 |
while (tokenizer.hasMoreTokens()) { |
| 116 |
String value = tokenizer.nextToken(); |
| 117 |
try { |
| 118 |
row[columnIndex] = java.lang.Double.valueOf(value); |
| 119 |
} catch (NumberFormatException nfe) { |
| 120 |
row[columnIndex] = java.lang.Double.NaN; |
| 121 |
|
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
columnIndex ++; |
| 126 |
} |
| 127 |
if (columnIndex != columnCount - (includeFirstColumnLabel ? 1 : 0)) { |
| 128 |
throw new IllegalArgumentException("Wrong number of values on row " + (rowIndex + 1) + |
| 129 |
", expecting " + columnCount + " but actually " + columnIndex); |
| 130 |
} |
| 131 |
|
| 132 |
rows.add(row); |
| 133 |
|
| 134 |
line = reader.readLine(); |
| 135 |
rowIndex++; |
| 136 |
} |
| 137 |
|
| 138 |
if (hasRowLabels) { |
| 139 |
this.rowLabels = new String[rowLabels.size()]; |
| 140 |
rowLabels.toArray(this.rowLabels); |
| 141 |
} |
| 142 |
|
| 143 |
data = new double[rows.size()][]; |
| 144 |
rows.toArray(data); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
public int getColumnCount() { |
| 149 |
return columnLabels.length; |
| 150 |
} |
| 151 |
|
| 152 |
public int getRowCount() { |
| 153 |
return rowLabels.length; |
| 154 |
} |
| 155 |
|
| 156 |
public String[] getColumnLabels() { |
| 157 |
return columnLabels; |
| 158 |
} |
| 159 |
|
| 160 |
public String[] getRowLabels() { |
| 161 |
return rowLabels; |
| 162 |
} |
| 163 |
|
| 164 |
public double[] getColumn(final int columnIndex) { |
| 165 |
double[] column = new double[data.length]; |
| 166 |
for (int i = 0; i < data.length; i++) { |
| 167 |
column[i] = data[i][columnIndex]; |
| 168 |
} |
| 169 |
return column; |
| 170 |
} |
| 171 |
|
| 172 |
public double[] getRow(final int rowIndex) { |
| 173 |
return data[rowIndex]; |
| 174 |
} |
| 175 |
|
| 176 |
public double[][] getData() { |
| 177 |
return data; |
| 178 |
} |
| 179 |
|
| 180 |
private String[] columnLabels; |
| 181 |
private String[] rowLabels; |
| 182 |
|
| 183 |
private double[][] data; |
| 184 |
|
| 185 |
public static DataTable<double []> parse(Reader source) throws IOException { |
| 186 |
return new DataTable.Double(source, true, true, false); |
| 187 |
} |
| 188 |
|
| 189 |
public static DataTable<double []> parse(Reader source, boolean isCSV) throws IOException { |
| 190 |
return new DataTable.Double(source, true, true, isCSV); |
| 191 |
} |
| 192 |
|
| 193 |
public static DataTable<double []> parse(Reader source, boolean columnLabels, boolean rowLabels) throws IOException { |
| 194 |
return new DataTable.Double(source, columnLabels, rowLabels, false); |
| 195 |
} |
| 196 |
|
| 197 |
public static DataTable<double []> parse(Reader source, boolean columnLabels, boolean rowLabels, boolean isCSV) throws IOException { |
| 198 |
return new DataTable.Double(source, columnLabels, rowLabels, isCSV); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
class Text implements DataTable<String[]> { |
| 203 |
|
| 204 |
private Text(Reader source, boolean hasColumnLabels, boolean hasRowLabels, boolean isCSV) throws IOException { |
| 205 |
this(source, hasColumnLabels, hasRowLabels, false, isCSV); |
| 206 |
} |
| 207 |
|
| 208 |
private Text(Reader source, boolean hasColumnLabels, boolean hasRowLabels, boolean includeFirstColumnLabel, boolean isCSV) throws IOException { |
| 209 |
BufferedReader reader = new BufferedReader(source); |
| 210 |
|
| 211 |
String delim = (isCSV ? "," : "\t"); |
| 212 |
|
| 213 |
String line = reader.readLine(); |
| 214 |
if (line == null) { |
| 215 |
throw new IllegalArgumentException("Empty file"); |
| 216 |
} |
| 217 |
|
| 218 |
int columnCount = -1; |
| 219 |
|
| 220 |
if (hasColumnLabels) { |
| 221 |
List<String> columnLabels = new ArrayList<String>(); |
| 222 |
|
| 223 |
StringTokenizer tokenizer = new StringTokenizer(line, delim); |
| 224 |
|
| 225 |
if (hasRowLabels && !line.startsWith(delim)) { |
| 226 |
|
| 227 |
String name = tokenizer.nextToken().trim(); |
| 228 |
if (includeFirstColumnLabel) { |
| 229 |
columnLabels.add(name); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
while (tokenizer.hasMoreTokens()) { |
| 234 |
String label = tokenizer.nextToken().trim(); |
| 235 |
columnLabels.add(label); |
| 236 |
} |
| 237 |
|
| 238 |
this.columnLabels = new String[columnLabels.size()]; |
| 239 |
columnLabels.toArray(this.columnLabels); |
| 240 |
|
| 241 |
columnCount = columnLabels.size(); |
| 242 |
|
| 243 |
line = reader.readLine(); |
| 244 |
} |
| 245 |
|
| 246 |
List<String> rowLabels = new ArrayList<String>(); |
| 247 |
List<String[]> rows = new ArrayList<String[]>(); |
| 248 |
int rowIndex = 1; |
| 249 |
|
| 250 |
while (line != null) { |
| 251 |
StringTokenizer tokenizer = new StringTokenizer(line, delim); |
| 252 |
|
| 253 |
if (columnCount == -1) { |
| 254 |
columnCount = tokenizer.countTokens(); |
| 255 |
if (hasRowLabels) { |
| 256 |
columnCount --; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (hasRowLabels) { |
| 261 |
String label = tokenizer.nextToken().trim(); |
| 262 |
rowLabels.add(label); |
| 263 |
} |
| 264 |
|
| 265 |
String[] row = new String [columnCount]; |
| 266 |
|
| 267 |
int columnIndex = 0; |
| 268 |
while (tokenizer.hasMoreTokens()) { |
| 269 |
row[columnIndex] = tokenizer.nextToken().trim(); |
| 270 |
|
| 271 |
columnIndex ++; |
| 272 |
} |
| 273 |
if (columnIndex != columnCount - (includeFirstColumnLabel ? 1 : 0)) { |
| 274 |
throw new IllegalArgumentException("Wrong number of values on row " + (rowIndex + 1) + |
| 275 |
", expecting " + columnCount + " but actually " + columnIndex); |
| 276 |
} |
| 277 |
|
| 278 |
rows.add(row); |
| 279 |
|
| 280 |
line = reader.readLine(); |
| 281 |
rowIndex++; |
| 282 |
} |
| 283 |
|
| 284 |
if (hasRowLabels) { |
| 285 |
this.rowLabels = new String[rowLabels.size()]; |
| 286 |
rowLabels.toArray(this.rowLabels); |
| 287 |
} |
| 288 |
|
| 289 |
data = new String[rows.size()][]; |
| 290 |
rows.toArray(data); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
public int getColumnCount() { |
| 295 |
return data[0].length; |
| 296 |
} |
| 297 |
|
| 298 |
public int getRowCount() { |
| 299 |
return data.length; |
| 300 |
} |
| 301 |
|
| 302 |
public String[] getColumnLabels() { |
| 303 |
return columnLabels; |
| 304 |
} |
| 305 |
|
| 306 |
public String[] getRowLabels() { |
| 307 |
return rowLabels; |
| 308 |
} |
| 309 |
|
| 310 |
public String[] getColumn(final int columnIndex) { |
| 311 |
String[] column = new String[data.length]; |
| 312 |
for (int i = 0; i < data.length; i++) { |
| 313 |
column[i] = data[i][columnIndex]; |
| 314 |
} |
| 315 |
return column; |
| 316 |
} |
| 317 |
|
| 318 |
public String[] getRow(final int rowIndex) { |
| 319 |
return data[rowIndex]; |
| 320 |
} |
| 321 |
|
| 322 |
public String[][] getData() { |
| 323 |
return data; |
| 324 |
} |
| 325 |
|
| 326 |
private String[] columnLabels; |
| 327 |
private String[] rowLabels; |
| 328 |
|
| 329 |
private String[][] data; |
| 330 |
|
| 331 |
public static DataTable<String []> parse(Reader source) throws IOException { |
| 332 |
return new DataTable.Text(source, true, true, false); |
| 333 |
} |
| 334 |
|
| 335 |
public static DataTable<String []> parse(Reader source, boolean isCSV) throws IOException { |
| 336 |
return new DataTable.Text(source, true, true, isCSV); |
| 337 |
} |
| 338 |
|
| 339 |
public static DataTable<String []> parse(Reader source, boolean columnLabels, boolean rowLabels) throws IOException { |
| 340 |
return new DataTable.Text(source, columnLabels, rowLabels, false); |
| 341 |
} |
| 342 |
|
| 343 |
public static DataTable<String []> parse(Reader source, boolean columnLabels, boolean rowLabels, boolean isCSV) throws IOException { |
| 344 |
return new DataTable.Text(source, columnLabels, rowLabels, isCSV); |
| 345 |
} |
| 346 |
|
| 347 |
public static DataTable<String []> parse(Reader source, boolean columnLabels, boolean rowLabels, boolean firstColumnLabel, boolean isCSV) throws IOException { |
| 348 |
return new DataTable.Text(source, columnLabels, rowLabels, firstColumnLabel, isCSV); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
} |