affyfusion-109/src/affymetrix/calvin/parsers/GenericFileReader.java

Code
Comments
Other
Rev Date Author Line
11 13 Sep 07 nicklas 1 /////////////////////////////////////////////////////////////////
11 13 Sep 07 nicklas 2 //
11 13 Sep 07 nicklas 3 // Copyright (C) 2005 Affymetrix, Inc.
11 13 Sep 07 nicklas 4 //
11 13 Sep 07 nicklas 5 // This library is free software; you can redistribute it and/or modify
11 13 Sep 07 nicklas 6 // it under the terms of the GNU Lesser General Public License as published
11 13 Sep 07 nicklas 7 // by the Free Software Foundation; either version 2.1 of the License,
11 13 Sep 07 nicklas 8 // or (at your option) any later version.
11 13 Sep 07 nicklas 9 //
11 13 Sep 07 nicklas 10 // This library is distributed in the hope that it will be useful, but
11 13 Sep 07 nicklas 11 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 13 Sep 07 nicklas 12 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 13 Sep 07 nicklas 13 // for more details.
11 13 Sep 07 nicklas 14 //
11 13 Sep 07 nicklas 15 // You should have received a copy of the GNU Lesser General Public License
11 13 Sep 07 nicklas 16 // along with this library; if not, write to the Free Software Foundation, Inc.,
11 13 Sep 07 nicklas 17 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
11 13 Sep 07 nicklas 18 //
11 13 Sep 07 nicklas 19 /////////////////////////////////////////////////////////////////
11 13 Sep 07 nicklas 20
11 13 Sep 07 nicklas 21 package affymetrix.calvin.parsers;
11 13 Sep 07 nicklas 22
11 13 Sep 07 nicklas 23 import affymetrix.calvin.data.*;
11 13 Sep 07 nicklas 24 import java.io.*;
11 13 Sep 07 nicklas 25 import affymetrix.calvin.exception.*;
11 13 Sep 07 nicklas 26
11 13 Sep 07 nicklas 27 /** This class reads a generic data file. It is the top-level generic data file reader.*/
11 13 Sep 07 nicklas 28 public class GenericFileReader {
11 13 Sep 07 nicklas 29     
11 13 Sep 07 nicklas 30     /** Creates a new instance of GenericFileReader */
11 13 Sep 07 nicklas 31     public GenericFileReader() {
11 13 Sep 07 nicklas 32         fileName = null;
11 13 Sep 07 nicklas 33         fileStream = null;
11 13 Sep 07 nicklas 34         gendata = null;
11 13 Sep 07 nicklas 35     }
11 13 Sep 07 nicklas 36     
11 13 Sep 07 nicklas 37     /** Gets the name of the input file.
11 13 Sep 07 nicklas 38      *
11 13 Sep 07 nicklas 39      * @return The name of the input file.
11 13 Sep 07 nicklas 40      */
11 13 Sep 07 nicklas 41     public String getFilename() { return fileName; }
11 13 Sep 07 nicklas 42
11 13 Sep 07 nicklas 43     /** Sets the name of the input file.
11 13 Sep 07 nicklas 44      *
11 13 Sep 07 nicklas 45      * @param name The name of the input file.
11 13 Sep 07 nicklas 46      */
11 13 Sep 07 nicklas 47     public void setFilename(String name) { fileName = name; }
11 13 Sep 07 nicklas 48
11 13 Sep 07 nicklas 49     /** Read the file header of the generic file.
11 13 Sep 07 nicklas 50      *
11 13 Sep 07 nicklas 51      * @param data A reference to a GenericData object that will receive header information from the file.
11 13 Sep 07 nicklas 52      * @param option Indicates how much DataGroupHeader and DataSetHeader information to read.
11 13 Sep 07 nicklas 53      * @exception affymetrix_calvin_exceptions::FileNotFoundException The file does not exist.
11 13 Sep 07 nicklas 54      * @exception affymetrix_calvin_exceptions::InvalidVersionException The file version does not match.
11 13 Sep 07 nicklas 55      * @exception affymetrix_calvin_exceptions::InvalidFileTypeException The file is not of the right type.
11 13 Sep 07 nicklas 56      */
11 13 Sep 07 nicklas 57     public void readHeader(GenericData data, int option/*=ReadHeaderOption.ReadAllHeaders*/)
11 13 Sep 07 nicklas 58         throws FileNotFoundException, InvalidVersionException, InvalidFileTypeException
11 13 Sep 07 nicklas 59     {
11 13 Sep 07 nicklas 60         openFile();
11 13 Sep 07 nicklas 61   switch(option)
11 13 Sep 07 nicklas 62   {
11 13 Sep 07 nicklas 63   case ReadHeaderOption.ReadNoDataGroupHeader:
11 13 Sep 07 nicklas 64             readFileHeaderNoDataGroupHeader(data);
11 13 Sep 07 nicklas 65             break;
11 13 Sep 07 nicklas 66   case ReadHeaderOption.ReadMinDataGroupHeader:
11 13 Sep 07 nicklas 67             readFileHeaderMinDP(data);
11 13 Sep 07 nicklas 68             break;
11 13 Sep 07 nicklas 69   case ReadHeaderOption.ReadAllHeaders:  // fall through
11 13 Sep 07 nicklas 70   default:
11 13 Sep 07 nicklas 71             readFileHeader(data);
11 13 Sep 07 nicklas 72             break;
11 13 Sep 07 nicklas 73   }
11 13 Sep 07 nicklas 74   close();
11 13 Sep 07 nicklas 75     }
11 13 Sep 07 nicklas 76
11 13 Sep 07 nicklas 77     /** Open the file for reading
11 13 Sep 07 nicklas 78      *  @param data A reference to a GenericData object that will receive header information from the file.  Amount of info depends on the hint.
11 13 Sep 07 nicklas 79      *  @param hint A hint on how to open the file.
11 13 Sep 07 nicklas 80      *  @exception affymetrix_calvin_exceptions::FileNotFoundException The file does not exist.
11 13 Sep 07 nicklas 81      *  @exception affymetrix_calvin_exceptions::InvalidVersionException The file version does not match.
11 13 Sep 07 nicklas 82      *  @exception affymetrix_calvin_exceptions::InvalidFileTypeException The file is not of the right type.
11 13 Sep 07 nicklas 83      */
11 13 Sep 07 nicklas 84     public void open(GenericData data, int hint/*=All*/) throws FileNotFoundException, InvalidVersionException, InvalidFileTypeException, NotImplementedException {
11 13 Sep 07 nicklas 85         if (hint == OpenHint.All)
11 13 Sep 07 nicklas 86         {
11 13 Sep 07 nicklas 87             openFile();
11 13 Sep 07 nicklas 88             readFileHeader(data);
11 13 Sep 07 nicklas 89             gendata = data;
11 13 Sep 07 nicklas 90   }
11 13 Sep 07 nicklas 91   else
11 13 Sep 07 nicklas 92   {
11 13 Sep 07 nicklas 93             throw new NotImplementedException();
11 13 Sep 07 nicklas 94   }
11 13 Sep 07 nicklas 95         
11 13 Sep 07 nicklas 96     }
11 13 Sep 07 nicklas 97
11 13 Sep 07 nicklas 98     /** Gets the number of DataGroups in the file.
11 13 Sep 07 nicklas 99      *  @return The number of DataGroups in the file.
11 13 Sep 07 nicklas 100      */
11 13 Sep 07 nicklas 101     public int getDataGroupCnt() {
11 13 Sep 07 nicklas 102         if (gendata != null)
11 13 Sep 07 nicklas 103             return gendata.getDataGroupCnt();
11 13 Sep 07 nicklas 104   return 0;
11 13 Sep 07 nicklas 105     }
11 13 Sep 07 nicklas 106
11 13 Sep 07 nicklas 107     /** Closes the file.
11 13 Sep 07 nicklas 108      */
11 13 Sep 07 nicklas 109     public void close() {
11 13 Sep 07 nicklas 110         if (fileStream != null)
11 13 Sep 07 nicklas 111         {
11 13 Sep 07 nicklas 112             try { fileStream.close(); }
11 13 Sep 07 nicklas 113             catch (Throwable t) {}
11 13 Sep 07 nicklas 114         }
11 13 Sep 07 nicklas 115         fileStream = null;        
11 13 Sep 07 nicklas 116     }
11 13 Sep 07 nicklas 117
11 13 Sep 07 nicklas 118     /** Opens the file for reading */
11 13 Sep 07 nicklas 119     private void openFile() throws FileNotFoundException {
11 13 Sep 07 nicklas 120         try
11 13 Sep 07 nicklas 121         {
11 13 Sep 07 nicklas 122             fileStream = new FileInputStream(fileName);
11 13 Sep 07 nicklas 123         }
11 13 Sep 07 nicklas 124         catch (Throwable t)
11 13 Sep 07 nicklas 125         {
11 13 Sep 07 nicklas 126             throw new FileNotFoundException();
11 13 Sep 07 nicklas 127         }
11 13 Sep 07 nicklas 128     }
11 13 Sep 07 nicklas 129
11 13 Sep 07 nicklas 130     /** Read the file header and minimize amount of information read from the DataSetHeaders.
11 13 Sep 07 nicklas 131      *  It does not attempt to read the complete DataSetHeader.  That is deferred
11 13 Sep 07 nicklas 132      *  until accessed by the DataSet object.
11 13 Sep 07 nicklas 133      *  @param data Reference to the GenericData object to fill.
11 13 Sep 07 nicklas 134      */
11 13 Sep 07 nicklas 135     private void readFileHeaderMinDP(GenericData data) throws InvalidVersionException, InvalidFileTypeException {
11 13 Sep 07 nicklas 136         data.getHeader().setFilename(fileName);
11 13 Sep 07 nicklas 137
11 13 Sep 07 nicklas 138         FileHeaderReader fhReader = new FileHeaderReader(fileStream, data.getHeader());
11 13 Sep 07 nicklas 139         fhReader.read();
11 13 Sep 07 nicklas 140
11 13 Sep 07 nicklas 141         DataGroupHeaderReader dchReader = new DataGroupHeaderReader();
11 13 Sep 07 nicklas 142         dchReader.readAllMinimumInfo(fileStream, data.getHeader(), fhReader.getDataGroupCnt());
11 13 Sep 07 nicklas 143
11 13 Sep 07 nicklas 144     }
11 13 Sep 07 nicklas 145
11 13 Sep 07 nicklas 146     /** Reads the file header of the generic file and reads all the DataSetHeader information.
11 13 Sep 07 nicklas 147      *  @param data Reference to the GenericData object to fill.
11 13 Sep 07 nicklas 148      */
11 13 Sep 07 nicklas 149     private void readFileHeader(GenericData data) throws InvalidVersionException, InvalidFileTypeException {
11 13 Sep 07 nicklas 150         // Set the file name
11 13 Sep 07 nicklas 151         data.getHeader().setFilename(fileName);
11 13 Sep 07 nicklas 152
11 13 Sep 07 nicklas 153         FileHeaderReader fhReader = new FileHeaderReader(fileStream, data.getHeader());
11 13 Sep 07 nicklas 154         fhReader.read();
11 13 Sep 07 nicklas 155
11 13 Sep 07 nicklas 156         DataGroupHeaderReader dchReader = new DataGroupHeaderReader();
11 13 Sep 07 nicklas 157         dchReader.readAll(fileStream, data.getHeader(), fhReader.getDataGroupCnt());
11 13 Sep 07 nicklas 158         
11 13 Sep 07 nicklas 159     }
11 13 Sep 07 nicklas 160
11 13 Sep 07 nicklas 161     /** Reads the file header of the generic file but does not read any DataGroupHeaders or DataSetHeaders.
11 13 Sep 07 nicklas 162      *  @param data Reference to the GenericData object to fill.
11 13 Sep 07 nicklas 163      */
11 13 Sep 07 nicklas 164     private void readFileHeaderNoDataGroupHeader(GenericData data) throws InvalidVersionException, InvalidFileTypeException  {
11 13 Sep 07 nicklas 165         // Set the file name
11 13 Sep 07 nicklas 166         data.getHeader().setFilename(fileName);
11 13 Sep 07 nicklas 167
11 13 Sep 07 nicklas 168         FileHeaderReader fhReader = new FileHeaderReader(fileStream, data.getHeader());
11 13 Sep 07 nicklas 169         fhReader.read();
11 13 Sep 07 nicklas 170     }
11 13 Sep 07 nicklas 171
11 13 Sep 07 nicklas 172     /** The name of the input file. */
11 13 Sep 07 nicklas 173     private String fileName;
11 13 Sep 07 nicklas 174
11 13 Sep 07 nicklas 175     /** The file stream. */
11 13 Sep 07 nicklas 176     private FileInputStream fileStream;
11 13 Sep 07 nicklas 177
11 13 Sep 07 nicklas 178     /** A pointer to the GenericData object */
11 13 Sep 07 nicklas 179     private GenericData gendata;
11 13 Sep 07 nicklas 180 }