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

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