123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Iterator;
- /**
- *
- * @author DTian
- */
- public class Matrix {
- private HashMap matrix; // store data
- private int rowSize; // row size of matrix
- private int colSize; // column size of value array
- private final int lookupTableSize = 9000; //size of look up table
- public int getColSize() {
- return colSize;
- }
- public void setColSize(int colSize) {
- this.colSize = colSize;
- }
- public HashMap getMatrix() {
- return matrix;
- }
- public void setMatrix(HashMap matrix) {
- this.matrix = matrix;
- }
- public int getRowSize() {
- return rowSize;
- }
- public void setRowSize(int rowSize) {
- this.rowSize = rowSize;
- }
- public Matrix() {
- rowSize = 0;
- colSize = 0;
- matrix = new HashMap();
- }
- /**
- * constructor with 1 String parameter
- *
- * @param filename : the name of the input file
- *
- * @result: create a matrix from a input file
- *
- */
- public Matrix(String filename) {
- // Initialize variables
- this.setRowSize(0);
- this.setColSize(0);
- matrix = new HashMap(lookupTableSize);
- try {
- FileReader fr = new FileReader(filename);
- BufferedReader br = new BufferedReader(fr);
- // strRow is used to read line from file(skip first row)
- String strRow = br.readLine();
- // The while loop read the data from data file to vvf
- while ((strRow = br.readLine()) != null) {
- // strArray was used to store the float value from data file in
- // string format
- String delimiter = "";
- if (strRow.indexOf(",") >= 0) { //for CSV file
- delimiter = "\\,";
- } else { // for whitespace delimited file
- delimiter = "\\s";
- }
- String[] strArray = strRow.trim().split(delimiter);
- String[] strArrValue = Arrays.copyOfRange(strArray, 1, strArray.length);
- // strArray[0] is the orf name, others are value
- matrix.put(strArray[0].trim().toLowerCase(), strArrValue);
- rowSize++;
- colSize = strArrValue.length;
- }
- br.close();
- fr.close();
- } catch (IOException e) {
- // catch possible io errors from readLine()
- System.out.println("IOException error in 'class Matrix, constructor'");
- }
- }
- /**
- *
- * @param index, the specifed key
- * @return: the string array of the value
- */
- public String[] getSpecifiedValue(Object key) {
- return (String[]) matrix.get(key);
- }
- /**
- * @return the list of orf names
- */
- public ArrayList getOrfNames() {
- ArrayList result = new ArrayList(this.getRowSize());
- Iterator it = matrix.keySet().iterator();
- while (it.hasNext()) {
- result.add(it.next());
- }
- return result;
- }
- public void addValue(Object key, Object value) {
- matrix.put(key, value);
- }
- }
|