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