Matrix.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. /**
  13. *
  14. * @author DTian
  15. */
  16. public class Matrix {
  17. private HashMap matrix; // store data
  18. private int rowSize; // row size of matrix
  19. private int colSize; // column size of value array
  20. private final int lookupTableSize = 9000; //size of look up table
  21. public int getColSize() {
  22. return colSize;
  23. }
  24. public void setColSize(int colSize) {
  25. this.colSize = colSize;
  26. }
  27. public HashMap getMatrix() {
  28. return matrix;
  29. }
  30. public void setMatrix(HashMap matrix) {
  31. this.matrix = matrix;
  32. }
  33. public int getRowSize() {
  34. return rowSize;
  35. }
  36. public void setRowSize(int rowSize) {
  37. this.rowSize = rowSize;
  38. }
  39. public Matrix() {
  40. rowSize = 0;
  41. colSize = 0;
  42. matrix = new HashMap();
  43. }
  44. /**
  45. * constructor with 1 String parameter
  46. *
  47. * @param filename : the name of the input file
  48. *
  49. * @result: create a matrix from a input file
  50. *
  51. */
  52. public Matrix(String filename) {
  53. // Initialize variables
  54. this.setRowSize(0);
  55. this.setColSize(0);
  56. matrix = new HashMap(lookupTableSize);
  57. try {
  58. FileReader fr = new FileReader(filename);
  59. BufferedReader br = new BufferedReader(fr);
  60. // strRow is used to read line from file(skip first row)
  61. String strRow = br.readLine();
  62. // The while loop read the data from data file to vvf
  63. while ((strRow = br.readLine()) != null) {
  64. // strArray was used to store the float value from data file in
  65. // string format
  66. String delimiter = "";
  67. if (strRow.indexOf(",") >= 0) { //for CSV file
  68. delimiter = "\\,";
  69. } else { // for whitespace delimited file
  70. delimiter = "\\s";
  71. }
  72. String[] strArray = strRow.trim().split(delimiter);
  73. String[] strArrValue = Arrays.copyOfRange(strArray, 1, strArray.length);
  74. // strArray[0] is the orf name, others are value
  75. matrix.put(strArray[0].trim().toLowerCase(), strArrValue);
  76. rowSize++;
  77. colSize = strArrValue.length;
  78. }
  79. br.close();
  80. fr.close();
  81. } catch (IOException e) {
  82. // catch possible io errors from readLine()
  83. System.out.println("IOException error in 'class Matrix, constructor'");
  84. }
  85. }
  86. /**
  87. *
  88. * @param index, the specifed key
  89. * @return: the string array of the value
  90. */
  91. public String[] getSpecifiedValue(Object key) {
  92. return (String[]) matrix.get(key);
  93. }
  94. /**
  95. * @return the list of orf names
  96. */
  97. public ArrayList getOrfNames() {
  98. ArrayList result = new ArrayList(this.getRowSize());
  99. Iterator it = matrix.keySet().iterator();
  100. while (it.hasNext()) {
  101. result.add(it.next());
  102. }
  103. return result;
  104. }
  105. public void addValue(Object key, Object value) {
  106. matrix.put(key, value);
  107. }
  108. }