mev-4.0.01/source/org/tigr/microarray/file/AgilentMevFileParser.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2  * Created on Apr 29, 2004
2 26 Feb 07 jari 3  */
2 26 Feb 07 jari 4 package org.tigr.microarray.file;
2 26 Feb 07 jari 5
2 26 Feb 07 jari 6 /**
2 26 Feb 07 jari 7  * @author vu
2 26 Feb 07 jari 8  */
2 26 Feb 07 jari 9
2 26 Feb 07 jari 10
2 26 Feb 07 jari 11 import java.awt.Component;
2 26 Feb 07 jari 12 import java.io.BufferedReader;
2 26 Feb 07 jari 13 import java.io.IOException;
2 26 Feb 07 jari 14 //import java.io.FileReader;
2 26 Feb 07 jari 15 import java.io.File;
2 26 Feb 07 jari 16 import java.io.StringReader;
2 26 Feb 07 jari 17 import java.util.StringTokenizer;
2 26 Feb 07 jari 18 import java.util.Vector;
2 26 Feb 07 jari 19 import javax.swing.JFileChooser;
2 26 Feb 07 jari 20 import javax.swing.filechooser.FileFilter;
2 26 Feb 07 jari 21
2 26 Feb 07 jari 22 import org.tigr.microarray.file.FieldNotFoundException;
2 26 Feb 07 jari 23 import org.tigr.microarray.mev.SpotInformationData;
2 26 Feb 07 jari 24 import org.tigr.microarray.mev.file.agilent.MeVerizer;
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.file.agilent.ColumnMismatchException;
2 26 Feb 07 jari 26
2 26 Feb 07 jari 27 public class AgilentMevFileParser {
2 26 Feb 07 jari 28   
2 26 Feb 07 jari 29   public static final int INVALID_FILE = 0;
2 26 Feb 07 jari 30   public static final int MEV_FILE = 1;
2 26 Feb 07 jari 31   
2 26 Feb 07 jari 32   public static String mHeaderLine = "UID\tIA\tIB\tR\tC\tMR\tMC\tFlagA\tFlagB\tBGA\tBGB\tSDIA\tSDIB\tSDBGA\tSDBGB\tMedianA\tMedianB";
2 26 Feb 07 jari 33   public static String columnDefLine = "FeatureNum\tgDyeNormSignal\trDyeNormSignal\tRow\tCol\tMR\tMC\tgIsFeatNonUnifOL\trIsFeatNonUnifOL\tgBGMedianSignal\trBGMedianSignal\tgPixSDev\trPixSDev\tgBGPixSDev\trBGPixSDev\tgMedianSignal\trMedianSignal";
2 26 Feb 07 jari 34   public static String replacement = "1";
2 26 Feb 07 jari 35   
2 26 Feb 07 jari 36   public static final String UNIQUE_ID_STRING = "UID";
2 26 Feb 07 jari 37   
2 26 Feb 07 jari 38   private Vector columnHeaders;
2 26 Feb 07 jari 39   private Vector rawLines;
2 26 Feb 07 jari 40   private IntVector dataLinesMap;
2 26 Feb 07 jari 41   
2 26 Feb 07 jari 42   private boolean mevFileLoaded;
2 26 Feb 07 jari 43   
2 26 Feb 07 jari 44   
2 26 Feb 07 jari 45   /**
2 26 Feb 07 jari 46     Default and sole constructor
2 26 Feb 07 jari 47   */
2 26 Feb 07 jari 48   public AgilentMevFileParser() {
2 26 Feb 07 jari 49   }
2 26 Feb 07 jari 50     
2 26 Feb 07 jari 51     
2 26 Feb 07 jari 52   public SpotInformationData getSpotInformation(){
2 26 Feb 07 jari 53     if(this.isMevFileLoaded()){
2 26 Feb 07 jari 54       String [][] data = this.getDataMatrix();
2 26 Feb 07 jari 55       Vector headers = this.getColumnHeaders();
2 26 Feb 07 jari 56       if(headers.size() < 9)
2 26 Feb 07 jari 57         return null;
2 26 Feb 07 jari 58       int numberOfFields = headers.size()-9;
2 26 Feb 07 jari 59       String [][] spotData = new String[data.length][numberOfFields];
2 26 Feb 07 jari 60       for(int i = 0; i < spotData.length; i++){
2 26 Feb 07 jari 61         for(int j = 0; j < spotData[i].length; j++){
2 26 Feb 07 jari 62           spotData[i][j] = data[i][j+9];
2 26 Feb 07 jari 63         }                    
2 26 Feb 07 jari 64       }
2 26 Feb 07 jari 65       String [] headerStrings = new String[headers.size()-9];
2 26 Feb 07 jari 66       for(int i = 0; i < headerStrings.length; i++){
2 26 Feb 07 jari 67         headerStrings[i] = (String)(headers.elementAt(i+9));
2 26 Feb 07 jari 68       }
2 26 Feb 07 jari 69       return new SpotInformationData(headerStrings, spotData);            
2 26 Feb 07 jari 70     } else
2 26 Feb 07 jari 71       return null;
2 26 Feb 07 jari 72   }
2 26 Feb 07 jari 73   
2 26 Feb 07 jari 74   
2 26 Feb 07 jari 75   /**
2 26 Feb 07 jari 76     Displays a JFileChooser with an mev file filter. The default directory 
2 26 Feb 07 jari 77     is <i>user.dir</i>.
2 26 Feb 07 jari 78     
2 26 Feb 07 jari 79     @param dialogParent Parent component of the JFileChooser
2 26 Feb 07 jari 80     
2 26 Feb 07 jari 81     @return The selected mev file
2 26 Feb 07 jari 82   */
2 26 Feb 07 jari 83   public static File selectFile(Component dialogParent) {
2 26 Feb 07 jari 84     return selectFile(new File(System.getProperty("user.dir")), dialogParent);
2 26 Feb 07 jari 85   }
2 26 Feb 07 jari 86   
2 26 Feb 07 jari 87   /**
2 26 Feb 07 jari 88     Displays a JFileChooser with an mev file filter that opens to a specified 
2 26 Feb 07 jari 89     directory.
2 26 Feb 07 jari 90     
2 26 Feb 07 jari 91     @param defaultDirectory The default directory for the JFileChooser to 
2 26 Feb 07 jari 92     open to
2 26 Feb 07 jari 93     
2 26 Feb 07 jari 94     @param dialogParent Parent component of the JFileChooser
2 26 Feb 07 jari 95     
2 26 Feb 07 jari 96     @return The selected mev file
2 26 Feb 07 jari 97   */
2 26 Feb 07 jari 98   public static File selectFile(File defaultDirectory, Component dialogParent) {
2 26 Feb 07 jari 99     
2 26 Feb 07 jari 100     JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
2 26 Feb 07 jari 101     chooser.setDialogTitle("Select an Agilent Oligo Feature Extraction file");
2 26 Feb 07 jari 102     chooser.setCurrentDirectory(defaultDirectory);
2 26 Feb 07 jari 103     chooser.setMultiSelectionEnabled(false);
2 26 Feb 07 jari 104     chooser.addChoosableFileFilter(new FileFilter() {
2 26 Feb 07 jari 105       public boolean accept(File f) {
2 26 Feb 07 jari 106         String extension = "";
2 26 Feb 07 jari 107         if (f.isDirectory()) return true;
2 26 Feb 07 jari 108         
2 26 Feb 07 jari 109         if (f.getName().endsWith(".txt")) return true;
2 26 Feb 07 jari 110         else return false;
2 26 Feb 07 jari 111       }
2 26 Feb 07 jari 112       
2 26 Feb 07 jari 113       public String getDescription() {
2 26 Feb 07 jari 114         return "Agilent Oligo Feature Extraction Files (*.txt)";
2 26 Feb 07 jari 115       }
2 26 Feb 07 jari 116       
2 26 Feb 07 jari 117     });
2 26 Feb 07 jari 118     
2 26 Feb 07 jari 119     if (chooser.showOpenDialog(dialogParent) == JFileChooser.APPROVE_OPTION) {
2 26 Feb 07 jari 120       return chooser.getSelectedFile();
2 26 Feb 07 jari 121     } else {
2 26 Feb 07 jari 122       return null;
2 26 Feb 07 jari 123     }
2 26 Feb 07 jari 124   }
2 26 Feb 07 jari 125   
2 26 Feb 07 jari 126   /**
2 26 Feb 07 jari 127     Scans the specified file and returns filetype/validity code.
2 26 Feb 07 jari 128     
2 26 Feb 07 jari 129     <p> Duplicate UID check not yet implemented.
2 26 Feb 07 jari 130     
2 26 Feb 07 jari 131     @param targetFile The mev file to validate
2 26 Feb 07 jari 132     
2 26 Feb 07 jari 133     @throws FileFormatException
2 26 Feb 07 jari 134     
2 26 Feb 07 jari 135     @return The filetype/validity code
2 26 Feb 07 jari 136   */
2 26 Feb 07 jari 137   public static int validate(File targetFile) {
2 26 Feb 07 jari 138     
2 26 Feb 07 jari 139     IntVector dataLinesMap = new IntVector();
2 26 Feb 07 jari 140     Vector rawLines = new Vector();
2 26 Feb 07 jari 141     Vector columnHeaders = new Vector();
2 26 Feb 07 jari 142     
2 26 Feb 07 jari 143     String currentLine = new String();
2 26 Feb 07 jari 144     BufferedReader reader = null;
2 26 Feb 07 jari 145     boolean readHeaders = false;
2 26 Feb 07 jari 146     
2 26 Feb 07 jari 147     boolean valid1 = false; // Has a header containing UNIQUE_ID_STRING
2 26 Feb 07 jari 148     boolean valid2 = true; // No duplicate header fields
2 26 Feb 07 jari 149     boolean valid3 = false; // Dataset contains at least one row
2 26 Feb 07 jari 150     //boolean valid4 = false; // No duplicate UID values in dataset
2 26 Feb 07 jari 151     boolean valid4 = true; // Just until it's implemented...
2 26 Feb 07 jari 152     
2 26 Feb 07 jari 153     String s = null;
2 26 Feb 07 jari 154     try {
2 26 Feb 07 jari 155       MeVerizer mv = new MeVerizer(AgilentMevFileParser.mHeaderLine, AgilentMevFileParser.columnDefLine);
2 26 Feb 07 jari 156       mv.parseExtract(targetFile, true, AgilentMevFileParser.replacement);
2 26 Feb 07 jari 157       s = mv.getFileString();
2 26 Feb 07 jari 158     } catch (ColumnMismatchException e) {
2 26 Feb 07 jari 159       // TODO Auto-generated catch block
2 26 Feb 07 jari 160       e.printStackTrace();
2 26 Feb 07 jari 161     } catch (IOException e) {
2 26 Feb 07 jari 162       // TODO Auto-generated catch block
2 26 Feb 07 jari 163       e.printStackTrace();
2 26 Feb 07 jari 164     }
2 26 Feb 07 jari 165     
2 26 Feb 07 jari 166     try {
2 26 Feb 07 jari 167       reader = new BufferedReader(new StringReader(s));
2 26 Feb 07 jari 168       for (int lineCount = 0; ((currentLine = reader.readLine()) != null); lineCount++) {
2 26 Feb 07 jari 169         rawLines.add(currentLine);
2 26 Feb 07 jari 170         if (! currentLine.startsWith("#")) { // Non-comment line
2 26 Feb 07 jari 171           if (! readHeaders) { // Read/load the column headers
2 26 Feb 07 jari 172             readHeaders = true;
2 26 Feb 07 jari 173             StringTokenizer st = new StringTokenizer(currentLine, "\t");
2 26 Feb 07 jari 174             while (st.hasMoreTokens()) {
2 26 Feb 07 jari 175               String token = st.nextToken();
2 26 Feb 07 jari 176               
2 26 Feb 07 jari 177               if (token.equals(AgilentMevFileParser.UNIQUE_ID_STRING)) { // Validity test 1
2 26 Feb 07 jari 178                 valid1 = true;
2 26 Feb 07 jari 179               }
2 26 Feb 07 jari 180               
2 26 Feb 07 jari 181               for (int i = 0; i < columnHeaders.size(); i++) { // Validity test 2
2 26 Feb 07 jari 182                 String headerValue = (String) columnHeaders.elementAt(i);
2 26 Feb 07 jari 183                 if (token.equals(headerValue)) {
2 26 Feb 07 jari 184                   valid2 = false;
2 26 Feb 07 jari 185                   return AgilentMevFileParser.INVALID_FILE;
2 26 Feb 07 jari 186                 }
2 26 Feb 07 jari 187               }
2 26 Feb 07 jari 188               
2 26 Feb 07 jari 189               columnHeaders.add(token);
2 26 Feb 07 jari 190             }
2 26 Feb 07 jari 191             
2 26 Feb 07 jari 192           } else {
2 26 Feb 07 jari 193             dataLinesMap.add(lineCount);
2 26 Feb 07 jari 194           }
2 26 Feb 07 jari 195         }
2 26 Feb 07 jari 196       }
2 26 Feb 07 jari 197       
2 26 Feb 07 jari 198       if (dataLinesMap.size() > 0) { // Validity test 3
2 26 Feb 07 jari 199         valid3 = true;
2 26 Feb 07 jari 200       }
2 26 Feb 07 jari 201
2 26 Feb 07 jari 202     } catch (IOException ioe) {
2 26 Feb 07 jari 203       return AgilentMevFileParser.INVALID_FILE;
2 26 Feb 07 jari 204     }
2 26 Feb 07 jari 205     
2 26 Feb 07 jari 206     if (valid1 && valid2 && valid3 && valid4) {
2 26 Feb 07 jari 207       return AgilentMevFileParser.MEV_FILE;
2 26 Feb 07 jari 208     } else {
2 26 Feb 07 jari 209       return AgilentMevFileParser.INVALID_FILE;
2 26 Feb 07 jari 210     }
2 26 Feb 07 jari 211   }
2 26 Feb 07 jari 212                                         
2 26 Feb 07 jari 213   /**
2 26 Feb 07 jari 214     Reads the specified mev file, then instantiates and populates the 
2 26 Feb 07 jari 215     appropriate data objects. <code>isMeVFileLoaded</code> will return true 
2 26 Feb 07 jari 216     if this method was successful in loading the mev file.
2 26 Feb 07 jari 217     
2 26 Feb 07 jari 218     @param targetFile The mev file to load
2 26 Feb 07 jari 219   */
2 26 Feb 07 jari 220   public void loadFile(File targetFile) {
2 26 Feb 07 jari 221     
2 26 Feb 07 jari 222     dataLinesMap = new IntVector();
2 26 Feb 07 jari 223     rawLines = new Vector();
2 26 Feb 07 jari 224     columnHeaders = new Vector();
2 26 Feb 07 jari 225     
2 26 Feb 07 jari 226     String currentLine = new String();
2 26 Feb 07 jari 227     BufferedReader reader = null;
2 26 Feb 07 jari 228     boolean readHeaders = false;
2 26 Feb 07 jari 229     
2 26 Feb 07 jari 230     String s = null;
2 26 Feb 07 jari 231     try {
2 26 Feb 07 jari 232       MeVerizer mv = new MeVerizer(AgilentMevFileParser.mHeaderLine, AgilentMevFileParser.columnDefLine);
2 26 Feb 07 jari 233       mv.parseExtract(targetFile, true, AgilentMevFileParser.replacement);
2 26 Feb 07 jari 234       s = mv.getFileString();
2 26 Feb 07 jari 235     } catch (ColumnMismatchException e) {
2 26 Feb 07 jari 236       // TODO Auto-generated catch block
2 26 Feb 07 jari 237       e.printStackTrace();
2 26 Feb 07 jari 238     } catch (IOException e) {
2 26 Feb 07 jari 239       // TODO Auto-generated catch block
2 26 Feb 07 jari 240       e.printStackTrace();
2 26 Feb 07 jari 241     }
2 26 Feb 07 jari 242     
2 26 Feb 07 jari 243     try {
2 26 Feb 07 jari 244       //reader = new BufferedReader(new FileReader(targetFile));
2 26 Feb 07 jari 245       reader = new BufferedReader(new StringReader(s));
2 26 Feb 07 jari 246       for (int lineCount = 0; ((currentLine = reader.readLine()) != null); lineCount++) {
2 26 Feb 07 jari 247         rawLines.add(currentLine);
2 26 Feb 07 jari 248         if (! currentLine.startsWith("#")) { // Non-comment line
2 26 Feb 07 jari 249           if (! readHeaders) { // Read/load the column headers
2 26 Feb 07 jari 250             readHeaders = true;
2 26 Feb 07 jari 251             StringTokenizer st = new StringTokenizer(currentLine, "\t");
2 26 Feb 07 jari 252             while (st.hasMoreTokens()) {
2 26 Feb 07 jari 253               columnHeaders.add(st.nextToken());
2 26 Feb 07 jari 254             }
2 26 Feb 07 jari 255           } else {
2 26 Feb 07 jari 256             dataLinesMap.add(lineCount);
2 26 Feb 07 jari 257           }
2 26 Feb 07 jari 258         }
2 26 Feb 07 jari 259       }
2 26 Feb 07 jari 260
2 26 Feb 07 jari 261     } catch (IOException ioe) {
2 26 Feb 07 jari 262       ioe.printStackTrace();
2 26 Feb 07 jari 263       mevFileLoaded = false;
2 26 Feb 07 jari 264       return;
2 26 Feb 07 jari 265     }
2 26 Feb 07 jari 266     
2 26 Feb 07 jari 267     mevFileLoaded = true;
2 26 Feb 07 jari 268   }
2 26 Feb 07 jari 269   
2 26 Feb 07 jari 270   /**
2 26 Feb 07 jari 271     Returns true if the <code>loadFile</code> method was successful.
2 26 Feb 07 jari 272     
2 26 Feb 07 jari 273     @return The file load status
2 26 Feb 07 jari 274   */
2 26 Feb 07 jari 275   public boolean isMevFileLoaded() {
2 26 Feb 07 jari 276     return mevFileLoaded;
2 26 Feb 07 jari 277   }
2 26 Feb 07 jari 278   
2 26 Feb 07 jari 279   /**
2 26 Feb 07 jari 280     Returns a Vector containing the required row of column headers. Each 
2 26 Feb 07 jari 281     element in the Vector is one of the tab-delimited tokens from the first 
2 26 Feb 07 jari 282     non-comment line in the mev file.
2 26 Feb 07 jari 283     
2 26 Feb 07 jari 284     @return The Vector of column headers
2 26 Feb 07 jari 285   */
2 26 Feb 07 jari 286   public Vector getColumnHeaders() {
2 26 Feb 07 jari 287     return columnHeaders;
2 26 Feb 07 jari 288   }
2 26 Feb 07 jari 289   
2 26 Feb 07 jari 290   /**
2 26 Feb 07 jari 291     Returns a Vector containing the fields in the target column. All 
2 26 Feb 07 jari 292     comment lines will be ignored.
2 26 Feb 07 jari 293     
2 26 Feb 07 jari 294     @param targetColumn The index of the target column; valid values range 
2 26 Feb 07 jari 295     from 0 to n-1, where n is the number of columns in the mev file.
2 26 Feb 07 jari 296     
2 26 Feb 07 jari 297     @return The Vector of column data
2 26 Feb 07 jari 298   */
2 26 Feb 07 jari 299   public Vector getColumnAt(int targetColumn) {
2 26 Feb 07 jari 300     return getColumnAt(targetColumn, false);
2 26 Feb 07 jari 301   }
2 26 Feb 07 jari 302   
2 26 Feb 07 jari 303   /**
2 26 Feb 07 jari 304     Returns a Vector containing the fields and an optional header in the 
2 26 Feb 07 jari 305     target column. If requested, the first element of the Vector will be the 
2 26 Feb 07 jari 306     column header value. All comment lines will be ignored.
2 26 Feb 07 jari 307     
2 26 Feb 07 jari 308     @param targetColumn The index of the target column; valid values range 
2 26 Feb 07 jari 309     from 0 to n-1, where n is the number of columns in the mev file.
2 26 Feb 07 jari 310     
2 26 Feb 07 jari 311     @param withHeaders If true, the first element in the return Vector will 
2 26 Feb 07 jari 312     be the column header for the target column.
2 26 Feb 07 jari 313     
2 26 Feb 07 jari 314     @return The Vector of column data
2 26 Feb 07 jari 315   */
2 26 Feb 07 jari 316   public Vector getColumnAt(int targetColumn, boolean withHeaders) {
2 26 Feb 07 jari 317     
2 26 Feb 07 jari 318     Vector columnVector = new Vector(dataLinesMap.size() + (withHeaders ? 1 : 0));
2 26 Feb 07 jari 319     
2 26 Feb 07 jari 320     if ((targetColumn >= columnHeaders.size()) || (targetColumn < 0)) {
2 26 Feb 07 jari 321       throw new IndexOutOfBoundsException("Column Index out of bounds.");
2 26 Feb 07 jari 322     }
2 26 Feb 07 jari 323     
2 26 Feb 07 jari 324     if (withHeaders) columnVector.add(columnHeaders.elementAt(targetColumn));
2 26 Feb 07 jari 325     
2 26 Feb 07 jari 326     for (int i = 0; i < dataLinesMap.size(); i++) {
2 26 Feb 07 jari 327       StringTokenizer st = new StringTokenizer(getElementAtIndex(i));
2 26 Feb 07 jari 328       for (int j = 0; j < targetColumn; j++) {
2 26 Feb 07 jari 329         st.nextToken();
2 26 Feb 07 jari 330       }
2 26 Feb 07 jari 331       
2 26 Feb 07 jari 332       columnVector.add(st.nextToken());
2 26 Feb 07 jari 333     }
2 26 Feb 07 jari 334     
2 26 Feb 07 jari 335     return columnVector;
2 26 Feb 07 jari 336   }
2 26 Feb 07 jari 337   
2 26 Feb 07 jari 338   /**
2 26 Feb 07 jari 339     Returns a Vector containing the fields in the column which is 
2 26 Feb 07 jari 340     identified by the specified column header. All comment lines will be 
2 26 Feb 07 jari 341     ignored.
2 26 Feb 07 jari 342     
2 26 Feb 07 jari 343     @param columnName The column header of the target column
2 26 Feb 07 jari 344     
2 26 Feb 07 jari 345     @throws FieldNotFoundException
2 26 Feb 07 jari 346     
2 26 Feb 07 jari 347     @return The Vector of column data. If the specified column header is not 
2 26 Feb 07 jari 348     found, the return Vector will be null.
2 26 Feb 07 jari 349   */
2 26 Feb 07 jari 350   public Vector getColumnNamed(String columnName) throws FieldNotFoundException {
2 26 Feb 07 jari 351     return getColumnNamed(columnName, false);
2 26 Feb 07 jari 352   }
2 26 Feb 07 jari 353   
2 26 Feb 07 jari 354   /**
2 26 Feb 07 jari 355     Returns a Vector containing the fields and an optional header in the 
2 26 Feb 07 jari 356     column which is identified by the specified column header. If requested, 
2 26 Feb 07 jari 357     the first element of the Vector will be the column header value. All 
2 26 Feb 07 jari 358     comment lines will be ignored.
2 26 Feb 07 jari 359     
2 26 Feb 07 jari 360     @param columnName The column header of the target column
2 26 Feb 07 jari 361     
2 26 Feb 07 jari 362     @param withHeaders If true, the first element in the return Vector will 
2 26 Feb 07 jari 363     be the column header for the target column.
2 26 Feb 07 jari 364     
2 26 Feb 07 jari 365     @throws FieldNotFoundException
2 26 Feb 07 jari 366     
2 26 Feb 07 jari 367     @return The Vector of column data.
2 26 Feb 07 jari 368   */
2 26 Feb 07 jari 369   public Vector getColumnNamed(String columnName, boolean withHeaders) throws FieldNotFoundException {
2 26 Feb 07 jari 370     
2 26 Feb 07 jari 371     Vector columnHeaders = getColumnHeaders();
2 26 Feb 07 jari 372     
2 26 Feb 07 jari 373     if (columnHeaders.contains(columnName)) {
2 26 Feb 07 jari 374       return getColumnAt(columnHeaders.indexOf(columnName), withHeaders);
2 26 Feb 07 jari 375     } else {
2 26 Feb 07 jari 376       throw new FieldNotFoundException("Field " + columnName + " not found.");
2 26 Feb 07 jari 377     }
2 26 Feb 07 jari 378   }
2 26 Feb 07 jari 379   
2 26 Feb 07 jari 380   /**
2 26 Feb 07 jari 381     Returns the line from the mev file at the specified index.
2 26 Feb 07 jari 382     
2 26 Feb 07 jari 383     @param rawTargetline The index of the target line to be retrieved.
2 26 Feb 07 jari 384     
2 26 Feb 07 jari 385     @return The String containing the target line of text, as it appears in 
2 26 Feb 07 jari 386     the mev file. The trailing newline character, <code>\n</code>, if 
2 26 Feb 07 jari 387     present, is omitted.
2 26 Feb 07 jari 388   */
2 26 Feb 07 jari 389   public String getLineAt(int rawTargetLine) {
2 26 Feb 07 jari 390     return (String) rawLines.elementAt(rawTargetLine);
2 26 Feb 07 jari 391   }
2 26 Feb 07 jari 392   
2 26 Feb 07 jari 393   /**
2 26 Feb 07 jari 394     Returns the spot element line from the mev file at the specified index. 
2 26 Feb 07 jari 395     The index should refer to the position of the element in the mev file, 
2 26 Feb 07 jari 396     such that an index of 0 refers to the first spot in the file, an index 
2 26 Feb 07 jari 397     of 1 refers to the second spot in the file, and so forth. The header 
2 26 Feb 07 jari 398     row and all comment lines do not count towards this index.
2 26 Feb 07 jari 399     
2 26 Feb 07 jari 400     @param rawTargetline The index of the target element to be retrieved.
2 26 Feb 07 jari 401     
2 26 Feb 07 jari 402     @return The String encapsulating the target element, as it appears in 
2 26 Feb 07 jari 403     the mev file. The trailing newline character, <code>\n</code>, if 
2 26 Feb 07 jari 404     present, is omitted.
2 26 Feb 07 jari 405   */
2 26 Feb 07 jari 406   public String getElementAtIndex(int index) {
2 26 Feb 07 jari 407     return getLineAt(dataLinesMap.intElementAt(index));
2 26 Feb 07 jari 408   }
2 26 Feb 07 jari 409   
2 26 Feb 07 jari 410   /**
2 26 Feb 07 jari 411     Returns the spot element line from the mev file at with the specified 
2 26 Feb 07 jari 412     <i>row</i> and <i>column</i> values.
2 26 Feb 07 jari 413     
2 26 Feb 07 jari 414     <p> Not yet implemented.
2 26 Feb 07 jari 415   
2 26 Feb 07 jari 416     @param row The <i>row</i> value of the target element to be retrieved.
2 26 Feb 07 jari 417     
2 26 Feb 07 jari 418     @param column The <i>column</i> value of the target element to be 
2 26 Feb 07 jari 419     retrieved.
2 26 Feb 07 jari 420     
2 26 Feb 07 jari 421     @return The String encapsulating the target element, as it appears in 
2 26 Feb 07 jari 422     the mev file. The trailing newline character, <code>\n</code>, if 
2 26 Feb 07 jari 423     present, is omitted.
2 26 Feb 07 jari 424   */
2 26 Feb 07 jari 425   public String getElementAtRC(int row, int column) {
2 26 Feb 07 jari 426     return new String(); // Dummy return
2 26 Feb 07 jari 427   }
2 26 Feb 07 jari 428   
2 26 Feb 07 jari 429   /**
2 26 Feb 07 jari 430     Returns the spot element line from the mev file that has a <i>UID</i> 
2 26 Feb 07 jari 431     that matches the specified id value. If there are multiple matches, 
2 26 Feb 07 jari 432     only the first element will be returned.
2 26 Feb 07 jari 433     
2 26 Feb 07 jari 434     <p> Note: There should not be multiple elements with the same 
2 26 Feb 07 jari 435     <i>UID</i>, as defined in the mev file format description.
2 26 Feb 07 jari 436     
2 26 Feb 07 jari 437     @param id The <i>id</i> of the target element to be retrieved.
2 26 Feb 07 jari 438     
2 26 Feb 07 jari 439     @throws FieldNotFoundException
2 26 Feb 07 jari 440     
2 26 Feb 07 jari 441     @return The String encapsulating the target element, as it appears in 
2 26 Feb 07 jari 442     the mev file. The trailing newline character, <code>\n</code>, if 
2 26 Feb 07 jari 443     present, is omitted.
2 26 Feb 07 jari 444   */
2 26 Feb 07 jari 445   public String getElementById(String id) throws FieldNotFoundException {
2 26 Feb 07 jari 446     
2 26 Feb 07 jari 447     String element = null;
2 26 Feb 07 jari 448     
2 26 Feb 07 jari 449     try {
2 26 Feb 07 jari 450       element = getElementByField(AgilentMevFileParser.UNIQUE_ID_STRING, id);
2 26 Feb 07 jari 451       return element;
2 26 Feb 07 jari 452     } catch (FieldNotFoundException fnfe) {
2 26 Feb 07 jari 453       throw new FieldNotFoundException("Unique Identifier field (" + AgilentMevFileParser.UNIQUE_ID_STRING + ") not found.");
2 26 Feb 07 jari 454     }
2 26 Feb 07 jari 455   }
2 26 Feb 07 jari 456   
2 26 Feb 07 jari 457   /**
2 26 Feb 07 jari 458     Returns the spot element line from the mev file that contains the 
2 26 Feb 07 jari 459     specified value for the specified field. If there are multiple matches, 
2 26 Feb 07 jari 460     only the first element will be returned.
2 26 Feb 07 jari 461     
2 26 Feb 07 jari 462     @param fieldName The column header that identifies the column in which 
2 26 Feb 07 jari 463     to find the specified value of the target element to be retrieved.
2 26 Feb 07 jari 464     
2 26 Feb 07 jari 465     @param value The value in the specified column that identifies the 
2 26 Feb 07 jari 466     target element to be retrieved.
2 26 Feb 07 jari 467     
2 26 Feb 07 jari 468     @throws FieldNotFoundException
2 26 Feb 07 jari 469     
2 26 Feb 07 jari 470     @return The String encapsulating the target element, as it appears in 
2 26 Feb 07 jari 471     the mev file. The trailing newline character, <code>\n</code>, if 
2 26 Feb 07 jari 472     present, is omitted.
2 26 Feb 07 jari 473   */
2 26 Feb 07 jari 474   public String getElementByField(String fieldName, String value) throws FieldNotFoundException {
2 26 Feb 07 jari 475     
2 26 Feb 07 jari 476     Vector targetColumn = getColumnNamed(fieldName);
2 26 Feb 07 jari 477     
2 26 Feb 07 jari 478     if (targetColumn == null) throw new FieldNotFoundException("Field " + fieldName + " not found.");
2 26 Feb 07 jari 479     
2 26 Feb 07 jari 480     for (int i = 0; i < targetColumn.size(); i++) {
2 26 Feb 07 jari 481       if (((String) targetColumn.elementAt(i)).equals(value)) {
2 26 Feb 07 jari 482         return getElementAtIndex(i);
2 26 Feb 07 jari 483       }
2 26 Feb 07 jari 484     }
2 26 Feb 07 jari 485     
2 26 Feb 07 jari 486     return null;
2 26 Feb 07 jari 487   }
2 26 Feb 07 jari 488   
2 26 Feb 07 jari 489   /**
2 26 Feb 07 jari 490     Returns a Vector of spot element lines from the mev file that contains 
2 26 Feb 07 jari 491     the specified value for the specified field.
2 26 Feb 07 jari 492     
2 26 Feb 07 jari 493     @param fieldName The column header that identifies the column in which 
2 26 Feb 07 jari 494     to find the specified value of the target element to be retrieved.
2 26 Feb 07 jari 495     
2 26 Feb 07 jari 496     @param value The value in the specified column that identifies the 
2 26 Feb 07 jari 497     target element to be retrieved.
2 26 Feb 07 jari 498     
2 26 Feb 07 jari 499     @throws FieldNotFoundException
2 26 Feb 07 jari 500     
2 26 Feb 07 jari 501     @return The String encapsulating the target element, as it appears in 
2 26 Feb 07 jari 502     the mev file. The trailing newline character, <code>\n</code>, if 
2 26 Feb 07 jari 503     present, is omitted. If there are no matches, the return Vector will 
2 26 Feb 07 jari 504     be null.
2 26 Feb 07 jari 505   */
2 26 Feb 07 jari 506   public Vector getElementsByField(String fieldName, String value) throws FieldNotFoundException {
2 26 Feb 07 jari 507     
2 26 Feb 07 jari 508     Vector targetColumn = getColumnNamed(fieldName);
2 26 Feb 07 jari 509     Vector matchesVector = null;
2 26 Feb 07 jari 510     
2 26 Feb 07 jari 511     if (targetColumn == null) throw new FieldNotFoundException("Field " + fieldName + " not found.");
2 26 Feb 07 jari 512     
2 26 Feb 07 jari 513     for (int i = 0; i < targetColumn.size(); i++) {
2 26 Feb 07 jari 514       if (((String) targetColumn.elementAt(i)).equals(value)) {
2 26 Feb 07 jari 515         if (matchesVector == null) matchesVector = new Vector();
2 26 Feb 07 jari 516         matchesVector.add(getElementAtIndex(i));
2 26 Feb 07 jari 517       }
2 26 Feb 07 jari 518     }
2 26 Feb 07 jari 519     
2 26 Feb 07 jari 520     return matchesVector;
2 26 Feb 07 jari 521   }
2 26 Feb 07 jari 522   
2 26 Feb 07 jari 523   /**
2 26 Feb 07 jari 524     Returns a two-dimensional String array containing every value for each 
2 26 Feb 07 jari 525     column header for every spot in the mev file. The first dimension of the 
2 26 Feb 07 jari 526     array iterates over the columns, while the second dimension iterates 
2 26 Feb 07 jari 527     over the spots. All comments lines will be ignored.
2 26 Feb 07 jari 528     
2 26 Feb 07 jari 529     @return The String[][] containing all spot data
2 26 Feb 07 jari 530   */
2 26 Feb 07 jari 531   public String[][] getDataMatrix() {
2 26 Feb 07 jari 532     return getDataMatrix(false);
2 26 Feb 07 jari 533   }
2 26 Feb 07 jari 534   
2 26 Feb 07 jari 535   /**
2 26 Feb 07 jari 536     Returns a two-dimensional String array containing every value for each 
2 26 Feb 07 jari 537     column header for every spot in the mev file. The first dimension of the 
2 26 Feb 07 jari 538     array iterates over the columns, while the second dimension iterates 
2 26 Feb 07 jari 539     over the spots. Optionally, the first element in the first dimension of 
2 26 Feb 07 jari 540     the array can be an array of all column headers. All comment lines will 
2 26 Feb 07 jari 541     be ignored.
2 26 Feb 07 jari 542     
2 26 Feb 07 jari 543     @param withHeaders If true, headers are included in the returned array
2 26 Feb 07 jari 544     
2 26 Feb 07 jari 545     @return The String[][] containing all spot data
2 26 Feb 07 jari 546   */
2 26 Feb 07 jari 547   
2 26 Feb 07 jari 548
2 26 Feb 07 jari 549     public String[][] getDataMatrix(boolean withHeaders) {
2 26 Feb 07 jari 550     
2 26 Feb 07 jari 551     Vector columnHeaders = getColumnHeaders();
2 26 Feb 07 jari 552     int hc = withHeaders ? 1 : 0;
2 26 Feb 07 jari 553     
2 26 Feb 07 jari 554     String[][] matrix = new String[dataLinesMap.size() + hc][columnHeaders.size()];
2 26 Feb 07 jari 555     
2 26 Feb 07 jari 556     if (withHeaders) {
2 26 Feb 07 jari 557       for (int i = 0; i < columnHeaders.size(); i++) {
2 26 Feb 07 jari 558         matrix[0][i] = (String) columnHeaders.elementAt(i);
2 26 Feb 07 jari 559       }
2 26 Feb 07 jari 560     }
2 26 Feb 07 jari 561     
2 26 Feb 07 jari 562     for (int i = hc; i < matrix.length; i++) {
2 26 Feb 07 jari 563       
2 26 Feb 07 jari 564       String currentLine = getElementAtIndex(i - hc);
2 26 Feb 07 jari 565       StringTokenizer st = new StringTokenizer(currentLine, "\t");
2 26 Feb 07 jari 566       
2 26 Feb 07 jari 567       for (int j = 0; j < matrix[i].length; j++) {
2 26 Feb 07 jari 568         matrix[i][j] = st.nextToken();
2 26 Feb 07 jari 569       }
2 26 Feb 07 jari 570     }
2 26 Feb 07 jari 571     
2 26 Feb 07 jari 572     return matrix;
2 26 Feb 07 jari 573   }
2 26 Feb 07 jari 574   
2 26 Feb 07 jari 575   
2 26 Feb 07 jari 576   
2 26 Feb 07 jari 577   private static class IntVector extends Vector {
2 26 Feb 07 jari 578
2 26 Feb 07 jari 579     public void add(int element) {
2 26 Feb 07 jari 580       super.add(new Integer(element));
2 26 Feb 07 jari 581     }
2 26 Feb 07 jari 582     
2 26 Feb 07 jari 583     public int intElementAt(int index) {
2 26 Feb 07 jari 584       return ((Integer) super.elementAt(index)).intValue();
2 26 Feb 07 jari 585     }
2 26 Feb 07 jari 586   }
2 26 Feb 07 jari 587 }