Class TextReader

All Implemented Interfaces:
Closeable, AutoCloseable, ICompressedTileReader, IFormatHandler, IFormatReader, IMetadataConfigurable, IPyramidHandler

public class TextReader extends FormatReader
Reader for text files containing tables of data. All image planes are stored in memory as 32-bit floats until the file is closed, so very large text documents will require commensurate available RAM. Text format is flexible, but assumed to be in tabular form with a consistent number of columns, and a labeled header line immediately preceding the data.
Author:
Curtis Rueden ctrueden at wisc.edu
  • Field Details

    • LITTLE_ENDIAN

      private static final boolean LITTLE_ENDIAN
      See Also:
    • LABEL_X

      private static final String LABEL_X
      See Also:
    • LABEL_Y

      private static final String LABEL_Y
      See Also:
    • TIME_OFFSET

      private static final long TIME_OFFSET
      How often to report progress during initialization, in milliseconds.
      See Also:
    • data

      private float[][] data
      Because we have no way of indexing into the text file efficiently in general, we cheat and store the entire file's data in a giant array.
    • row

      private int row
      Current row number.
    • rowLength

      private int rowLength
      Number of tokens per row.
    • xIndex

      private int xIndex
      Column index for X coordinate.
    • yIndex

      private int yIndex
      Column index for Y coordinate.
    • channels

      private String[] channels
      List of channel labels.
    • sizeX

      private int sizeX
      Image width.
    • sizeY

      private int sizeY
      Image height.
  • Constructor Details

    • TextReader

      public TextReader()
      Constructs a new text reader.
  • Method Details

    • getChannelLabel

      public String getChannelLabel(int c)
      Gets the label for the given channel.
    • isThisType

      public boolean isThisType(loci.common.RandomAccessInputStream stream) throws IOException
      Description copied from interface: IFormatReader
      Checks if the given stream is a valid stream for this file format. The number of bytes read is format-dependent.
      Specified by:
      isThisType in interface IFormatReader
      Overrides:
      isThisType in class FormatReader
      Parameters:
      stream - A RandomAccessInputStream representing the file to check. The first byte in the stream is assumed to be the first byte in the file.
      Returns:
      true if the file represented by the stream can be read by this reader; false otherwise.
      Throws:
      IOException
    • openBytes

      public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException
      Description copied from interface: IFormatReader
      Obtains a sub-image of the specified image plane into a pre-allocated byte array.
      Specified by:
      openBytes in interface IFormatReader
      Specified by:
      openBytes in class FormatReader
      Parameters:
      no - the plane index within the current series.
      buf - a pre-allocated buffer.
      x - X coordinate of the upper-left corner of the sub-image
      y - Y coordinate of the upper-left corner of the sub-image
      w - width of the sub-image
      h - height of the sub-image
      Returns:
      the pre-allocated buffer buf for convenience.
      Throws:
      FormatException - if there was a problem parsing the metadata of the file.
      IOException - if there was a problem reading the file.
    • openPlane

      public Object openPlane(int no, int x, int y, int w, int h) throws FormatException, IOException
      Description copied from interface: IFormatReader
      Obtains the specified image plane (or sub-image thereof) in the reader's native data structure. For most readers this is a byte array; however, some readers call external APIs that work with other types such as BufferedImage. The openPlane method exists to maintain generality and efficiency while avoiding pollution of the API with AWT-specific logic.
      Specified by:
      openPlane in interface IFormatReader
      Overrides:
      openPlane in class FormatReader
      Throws:
      FormatException
      IOException
      See Also:
    • close

      public void close(boolean fileOnly) throws IOException
      Description copied from interface: IFormatReader
      Closes the currently open file. If the flag is set, this is all that happens; if unset, it is equivalent to calling Closeable.close().
      Specified by:
      close in interface IFormatReader
      Overrides:
      close in class FormatReader
      Throws:
      IOException
    • getNativeDataType

      public Class<?> getNativeDataType()
      Description copied from interface: IFormatHandler
      Returns the native data type of image planes for this reader, as returned by IFormatReader.openPlane(int, int, int, int, int) or IFormatWriter.savePlane(int, java.lang.Object). For most readers this type will be a byte array; however, some readers call external APIs that work with other types such as BufferedImage.
      Specified by:
      getNativeDataType in interface IFormatHandler
      Overrides:
      getNativeDataType in class FormatHandler
    • initFile

      protected void initFile(String id) throws FormatException, IOException
      Description copied from class: FormatReader
      Initializes the given file (parsing header information, etc.). Most subclasses should override this method to perform initialization operations such as parsing metadata.
      Overrides:
      initFile in class FormatReader
      Throws:
      FormatException - if a parsing error occurs processing the file.
      IOException - if an I/O error occurs processing the file
    • readFile

      private List<String> readFile(String id) throws IOException
      Throws:
      IOException
    • parseFileHeader

      private int parseFileHeader(List<String> lines) throws FormatException
      Parses the file looking for the file header. Determines image extents (sets sizeX and sizeY). Determines channel names (populates channels array).
      Returns:
      number of rows in the header
      Throws:
      FormatException
    • parseTableData

      private void parseTableData(List<String> lines, int linesToSkip)
      Reads the tabular data into the data array.
    • populateCoreMetadata

      private void populateCoreMetadata(int sizeX, int sizeY, int sizeZ, int sizeC, int sizeT)
      Populates the CoreMetadata values.
    • getRowData

      private boolean getRowData(String[] tokens, double[] rowData)
      Parses numerical row data from the given tokens.
      Parameters:
      tokens - list of token strings to parse
      rowData - array to fill in with the data; length must match tokens
      Returns:
      true if the data could be parsed
    • parseHeaderRow

      private void parseHeaderRow(String[] tokens)
      Populates rowLength, xIndex, yIndex, and channels.
    • assignValues

      private void assignValues(double[] rowData)
      Assigns values from the given row into the data array.
    • checkTime

      private long checkTime(long time, int no, long pos, long len)
    • getX

      private int getX(double[] rowData)
    • getY

      private int getY(double[] rowData)
    • getNextLine

      private String[] getNextLine(List<String> lines)