affyfusion-109/src/affymetrix/calvin/data/DataSet.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.data;
11 13 Sep 07 nicklas 22
11 13 Sep 07 nicklas 23 import affymetrix.calvin.parsers.*;
11 13 Sep 07 nicklas 24 import java.util.*;
11 13 Sep 07 nicklas 25 import affymetrix.vector.*;
11 13 Sep 07 nicklas 26 import java.nio.channels.*;
11 13 Sep 07 nicklas 27 import java.nio.channels.FileChannel.*;
11 13 Sep 07 nicklas 28 import java.nio.*;
11 13 Sep 07 nicklas 29 import java.io.*;
11 13 Sep 07 nicklas 30
11 13 Sep 07 nicklas 31 /** This class provides methods to access the data of a DataSet. */
11 13 Sep 07 nicklas 32 public class DataSet {
11 13 Sep 07 nicklas 33
11 13 Sep 07 nicklas 34     /** Constructor. Use thisructor do access the data using memory-mapping.
11 13 Sep 07 nicklas 35      *  On Windows, memory-mapping will be restricted to 200MB view of the DataSet data.
11 13 Sep 07 nicklas 36      *  @param fileName_ The name of the generic file to access.
11 13 Sep 07 nicklas 37      *  @param header_ The DataSetHeader of the DataSet to access.
11 13 Sep 07 nicklas 38      *  @param handle_ A handle to the file mapping object
11 13 Sep 07 nicklas 39      */
11 13 Sep 07 nicklas 40     public DataSet(String fileName_, DataSetHeader header_, FileInputStream handle_) {
11 13 Sep 07 nicklas 41         fileName = fileName_;
11 13 Sep 07 nicklas 42   header = header_;
11 13 Sep 07 nicklas 43   mappedData = null;
11 13 Sep 07 nicklas 44         fileInputStream = handle_;
11 13 Sep 07 nicklas 45   mapStart = 0;
11 13 Sep 07 nicklas 46   mapLen = 0;
11 13 Sep 07 nicklas 47     }
11 13 Sep 07 nicklas 48
11 13 Sep 07 nicklas 49     /** Method to release memory held by this object.  Closes object before deleting. */
11 13 Sep 07 nicklas 50     public void delete() {
11 13 Sep 07 nicklas 51         close();
11 13 Sep 07 nicklas 52     }
11 13 Sep 07 nicklas 53
11 13 Sep 07 nicklas 54     /** Method to open the DataSet to access the data.
11 13 Sep 07 nicklas 55      *  @return true if successful
11 13 Sep 07 nicklas 56      */
11 13 Sep 07 nicklas 57     public boolean open() {
11 13 Sep 07 nicklas 58   updateColumnByteOffsets();
11 13 Sep 07 nicklas 59         return openMM();
11 13 Sep 07 nicklas 60     }
11 13 Sep 07 nicklas 61
11 13 Sep 07 nicklas 62     
11 13 Sep 07 nicklas 63     /** Open the DataSet using memory-mapping
11 13 Sep 07 nicklas 64      *  @return True if the DataSet was successully mapped.
11 13 Sep 07 nicklas 65      */
11 13 Sep 07 nicklas 66     private boolean openMM() {
11 13 Sep 07 nicklas 67         try
11 13 Sep 07 nicklas 68         {
11 13 Sep 07 nicklas 69             mapLen = header.getDataSize();
11 13 Sep 07 nicklas 70             mapStart = header.getDataStartFilePos();
11 13 Sep 07 nicklas 71             mappedData = fileInputStream.getChannel().map(MapMode.READ_ONLY, mapStart, mapLen);
11 13 Sep 07 nicklas 72             mappedData.order(ByteOrder.BIG_ENDIAN);
11 13 Sep 07 nicklas 73             return true;
11 13 Sep 07 nicklas 74         }
11 13 Sep 07 nicklas 75         catch (Throwable t)
11 13 Sep 07 nicklas 76         {
11 13 Sep 07 nicklas 77             return false;
11 13 Sep 07 nicklas 78         }
11 13 Sep 07 nicklas 79     }
11 13 Sep 07 nicklas 80
11 13 Sep 07 nicklas 81     /** Updates the columnByteOffsets member. */
11 13 Sep 07 nicklas 82     private void updateColumnByteOffsets() {
11 13 Sep 07 nicklas 83   columnByteOffsets = new AffxIntVector();
11 13 Sep 07 nicklas 84   int accum = 0;
11 13 Sep 07 nicklas 85   int cols = header.getColumnCnt();
11 13 Sep 07 nicklas 86         columnByteOffsets.setSize(cols+1);
11 13 Sep 07 nicklas 87   for (int col = 0; col < cols; ++col)
11 13 Sep 07 nicklas 88   {
11 13 Sep 07 nicklas 89             columnByteOffsets.setInt(col, accum);
11 13 Sep 07 nicklas 90             accum += header.getColumnInfo(col).getSize();
11 13 Sep 07 nicklas 91   }
11 13 Sep 07 nicklas 92   columnByteOffsets.setInt(cols, accum);
11 13 Sep 07 nicklas 93     }
11 13 Sep 07 nicklas 94     
11 13 Sep 07 nicklas 95     /** Method to close the DataSet. */
11 13 Sep 07 nicklas 96     public void close() {
11 13 Sep 07 nicklas 97         mappedData = null;
11 13 Sep 07 nicklas 98         fileInputStream = null;
11 13 Sep 07 nicklas 99     }
11 13 Sep 07 nicklas 100
11 13 Sep 07 nicklas 101     /** Method to get a reference to the DataSetHeader
11 13 Sep 07 nicklas 102      *  @return A reference to the DataSetHeader.
11 13 Sep 07 nicklas 103      */
11 13 Sep 07 nicklas 104     public DataSetHeader getHeader() { return header; }
11 13 Sep 07 nicklas 105
11 13 Sep 07 nicklas 106     /** Return the number of rows in the DataSet. */
11 13 Sep 07 nicklas 107     public int getRows() { return header.getRowCnt(); }
11 13 Sep 07 nicklas 108
11 13 Sep 07 nicklas 109     /** Return the number of columns in the DataSet. */
11 13 Sep 07 nicklas 110     public int getCols() { return header.getColumnCnt(); }
11 13 Sep 07 nicklas 111
11 13 Sep 07 nicklas 112     /** Determines if the DataSet is open
11 13 Sep 07 nicklas 113      *  @return true if the DataSet is open
11 13 Sep 07 nicklas 114      */
11 13 Sep 07 nicklas 115     boolean isOpen() { return (mappedData != null); }
11 13 Sep 07 nicklas 116
11 13 Sep 07 nicklas 117     /** Provides access to single data elements
11 13 Sep 07 nicklas 118      *  @param row Row index.
11 13 Sep 07 nicklas 119      *  @param col Column index.
11 13 Sep 07 nicklas 120      *  @return Reference to the data type to fill with the data.
11 13 Sep 07 nicklas 121      */
11 13 Sep 07 nicklas 122     public byte getDataByte(int row, int col) {
11 13 Sep 07 nicklas 123         if (header.getRowCnt() == 0)
11 13 Sep 07 nicklas 124             return 0;
11 13 Sep 07 nicklas 125         mappedData.position(filePosition(row, col, 1));
11 13 Sep 07 nicklas 126   return FileInput.ReadInt8(mappedData);
11 13 Sep 07 nicklas 127     }
11 13 Sep 07 nicklas 128
11 13 Sep 07 nicklas 129     public short getDataShort(int row, int col) {
11 13 Sep 07 nicklas 130         if (header.getRowCnt() == 0)
11 13 Sep 07 nicklas 131             return 0;
11 13 Sep 07 nicklas 132         mappedData.position(filePosition(row, col, 1));
11 13 Sep 07 nicklas 133   return FileInput.ReadInt16(mappedData);
11 13 Sep 07 nicklas 134     }
11 13 Sep 07 nicklas 135
11 13 Sep 07 nicklas 136     public int getDataInt(int row, int col) {
11 13 Sep 07 nicklas 137         if (header.getRowCnt() == 0)
11 13 Sep 07 nicklas 138             return 0;
11 13 Sep 07 nicklas 139         mappedData.position(filePosition(row, col, 1));
11 13 Sep 07 nicklas 140   return FileInput.ReadUInt32(mappedData);
11 13 Sep 07 nicklas 141     }
11 13 Sep 07 nicklas 142
11 13 Sep 07 nicklas 143     public float getDataFloat(int row, int col) {
11 13 Sep 07 nicklas 144         if (header.getRowCnt() == 0)
11 13 Sep 07 nicklas 145             return 0.0f;
11 13 Sep 07 nicklas 146         mappedData.position(filePosition(row, col, 1));
11 13 Sep 07 nicklas 147   return FileInput.ReadFloat(mappedData);
11 13 Sep 07 nicklas 148     }
11 13 Sep 07 nicklas 149
11 13 Sep 07 nicklas 150     public String getDataString16(int row, int col) {
11 13 Sep 07 nicklas 151         if (header.getRowCnt() == 0)
11 13 Sep 07 nicklas 152             return null;
11 13 Sep 07 nicklas 153         mappedData.position(filePosition(row, col, 1));
11 13 Sep 07 nicklas 154   return FileInput.ReadString16(mappedData);
11 13 Sep 07 nicklas 155     }
11 13 Sep 07 nicklas 156     
11 13 Sep 07 nicklas 157     public String getDataString8(int row, int col) {
11 13 Sep 07 nicklas 158         if (header.getRowCnt() == 0)
11 13 Sep 07 nicklas 159             return null;
11 13 Sep 07 nicklas 160         mappedData.position(filePosition(row, col, 1));
11 13 Sep 07 nicklas 161   return FileInput.ReadString8(mappedData);
11 13 Sep 07 nicklas 162     }
11 13 Sep 07 nicklas 163
11 13 Sep 07 nicklas 164     /** Return the bytes per row.
11 13 Sep 07 nicklas 165      *  @return Bytes in a row.
11 13 Sep 07 nicklas 166      */
11 13 Sep 07 nicklas 167     private int bytesPerRow() { return columnByteOffsets.getInt(header.getColumnCnt()); }
11 13 Sep 07 nicklas 168
11 13 Sep 07 nicklas 169     /** Returns the address of a data element given a row and column.  Ensures that data from rowStart
11 13 Sep 07 nicklas 170      *  to rowCount+rowStart are mapped unless that is larger than the mapped window.
11 13 Sep 07 nicklas 171      *  @param rowStart Row index
11 13 Sep 07 nicklas 172      *  @param col Column index
11 13 Sep 07 nicklas 173      *  @param rowCount The number of rows to ensure are mapped starting at rowStart
11 13 Sep 07 nicklas 174      *  @return Pointer to the data element at rowStart
11 13 Sep 07 nicklas 175      */
11 13 Sep 07 nicklas 176     public int filePosition(int rowStart, int col, int rowCount/*=1*/) {
11 13 Sep 07 nicklas 177         int startByte = bytesPerRow()*rowStart + columnByteOffsets.getInt(col) + header.getDataStartFilePos();
11 13 Sep 07 nicklas 178   return startByte-mapStart;
11 13 Sep 07 nicklas 179     }
11 13 Sep 07 nicklas 180
11 13 Sep 07 nicklas 181     /** name of the file containing the data dataGroup.  */
11 13 Sep 07 nicklas 182     private String fileName;
11 13 Sep 07 nicklas 183
11 13 Sep 07 nicklas 184     /** copy of the DataSetHeader */
11 13 Sep 07 nicklas 185     private DataSetHeader header;
11 13 Sep 07 nicklas 186
11 13 Sep 07 nicklas 187     /** pointer to the mapped data, doesn't account for allocation granularity. */
11 13 Sep 07 nicklas 188     private MappedByteBuffer mappedData;
11 13 Sep 07 nicklas 189     
11 13 Sep 07 nicklas 190     /** The file input stream. */
11 13 Sep 07 nicklas 191     private FileInputStream fileInputStream;
11 13 Sep 07 nicklas 192
11 13 Sep 07 nicklas 193     /** Array of column byte offsets.  Updated when the file is opened.
11 13 Sep 07 nicklas 194      *  There are columns + 1 elements
11 13 Sep 07 nicklas 195      */
11 13 Sep 07 nicklas 196     private AffxIntVector columnByteOffsets;
11 13 Sep 07 nicklas 197     
11 13 Sep 07 nicklas 198     /** Byte offset to the start of the view */
11 13 Sep 07 nicklas 199     private int mapStart;
11 13 Sep 07 nicklas 200     
11 13 Sep 07 nicklas 201     /** Number of bytes mapped to the view */
11 13 Sep 07 nicklas 202     private int mapLen;
11 13 Sep 07 nicklas 203     
11 13 Sep 07 nicklas 204
11 13 Sep 07 nicklas 205 }