Class FileTable

java.lang.Object
  |
  +--FileTable

public class FileTable
extends java.lang.Object

This class reads data from files and writes data to files. It also reads tab-delimited data from a file into a Table (tRows,tCols,tFields,tData). It can be used to create an empty table (not associated with a file).

This work was produced by Peter Lemkin of the National Cancer Institute, an agency of the United States Government. As a work of the United States Government there is no associated copyright. It is offered as open source software under the Mozilla Public License (version 1.1) subject to the limitations noted in the accompanying LEGAL file.

Version:
$Date: 2002/10/14 19:22:30 $ $Revision: 1.6 $
Author:
P. Lemkin (NCI), G. Thornwall (SAIC), B. Stephens(SAIC), NCI-Frederick, Frederick, MD
See Also:
MAExplorer Home

Field Summary
 int bufSize
          size of input buffer
private static boolean DBUG_FILE_TABLE
          Local debugging switch
 java.lang.String errMsgLog
          error message log it not null
static int estFieldsRowNbr
          estimated row number where Fields are listed
static int estLinesInFile
          number of lines found in file
static int estMaxColsInFile
          estimated max number Cols in file
static int estMaxGridsInFile
          estimated max number Grids found in file
static int estMaxNbrField
          estimated number of Fields
static int estMaxRowsInFile
          estimated max number Rows in file
static int estRowWithData
          estimated row where data starts
static int estSamplesRowNbr
          estimated row number where Samples are listed
private  java.io.File file
          file descriptor for I/O
 java.lang.String fileName
          file to read or write I/O
 int tCols
          number of columns/row
 java.lang.String[][] tData
          row vectors [0:tRows-1][0:tCols-1]
 java.lang.String[] tFields
          names of table fields
 java.lang.String tName
          Table name
 int tRows
          number of rows.
 
Constructor Summary
FileTable(java.lang.String tableName)
          FileTable() - generic Constructor, set some defaults to 0.
FileTable(java.lang.String tableName, int rows, int cols)
          FileTable() - Constructor to make empty table of known size
 
Method Summary
 boolean checkForBadTable()
          checkForBadTable() - verify that it is a well formed table with all field names present and with no duplicate entries.
private  boolean computeTableSize(java.lang.String rawData)
          computeTableSize() - setup the (tRows,tCols) of table data.
static int countLinesAndTabCountsInFile(Cvt2Mae cvt, java.lang.String fileName, boolean lookForSampleTabsFlag, java.lang.String msg, java.awt.Color color)
          countLinesAndTabCountsInFile() - open file and count number of lines & tabs
Set estLinesInFile to # lines found.
Set estSamplesRowNbr to estimated row # where Samples are listed.
Set estFieldsRowNbr to estimated row # where Fields are listed.
static int countLinesInFile(java.lang.String fileName)
          countLinesInFile() - open file and count # of lines in the file.
static int countNonNullTabData(java.lang.String sLine)
          countNonNullTabData() - count Non-Null data tabs in line Do not count tabs after reach field with no data.
 boolean cvtStrToTable(java.lang.String sData)
          cvtStrToTable() - convert tab-delim String data to Table data structure.
static boolean findMaxGridRowColInFile(Cvt2Mae cvt, java.lang.String fileName, java.lang.String msg, java.awt.Color color, int rowWithFields, int rowNbrDataStarts)
          findMaxGridRowColInFile() - open file and find Max Grid Row Col in file This assumes that ("grid, grid_row, and grid_col") are in the FieldMap.
static int findRowWithKeywordsInFile(Cvt2Mae cvt, java.lang.String fileName, java.lang.String[] keyWords, boolean needAllKeywordsFlag, int maxLinesToCheck, java.lang.String msg, java.awt.Color color)
          findRowWithKeywordsInFile() - open file and look for first line with keywords (case-independent) in it.
 void freeTable()
          freeTable() - free Table tData[][] and tFields so can garbage collect it Also zero tRows, tCols
 int lookupFieldIdx(java.lang.String fieldName)
          lookupFieldIdx() - lookup index of field if exists.
static int lookupSubstringFieldIdx(java.lang.String[] tblFields, java.lang.String fieldName)
          lookupSubstringFieldIdx() - lookup index of case independent field substring if exists and startsWith tblField[].
 java.lang.String promptFileName(java.lang.String promptDir, java.lang.String msg)
          promptFileName() - prompt for file name
 java.lang.String readFileAsString(java.io.File f)
          readFileAsString() - Will read file from disk & returns as String
 java.lang.String readFileAsString(java.lang.String fileName)
          readFileAsString() - read file from disk and return data as String
 boolean readFileAsTable(java.lang.String fileName)
          readFileAsTable() - read file and tab-delim data as Table.
 java.lang.String[] readTableFieldsFromFile(java.lang.String fileName, int r)
          readTableFieldsFromFile() - open file and read row r tab-delim data.
private  boolean storeDataBufParser(java.lang.String rawData)
          storeDataBufParser() - Put data into Table tData[r][c] and tFields[c], given a raw data String from the whole file.
 boolean writeData(java.lang.String fileName, java.lang.String data)
          writeData() - write string data to file
 boolean writeStringToFile(java.lang.String fileName, java.lang.String data)
          writeStringToFile() - write data to disk file
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

DBUG_FILE_TABLE

private static boolean DBUG_FILE_TABLE
Local debugging switch

estLinesInFile

public static int estLinesInFile
number of lines found in file

estSamplesRowNbr

public static int estSamplesRowNbr
estimated row number where Samples are listed

estFieldsRowNbr

public static int estFieldsRowNbr
estimated row number where Fields are listed

estMaxNbrField

public static int estMaxNbrField
estimated number of Fields

estMaxGridsInFile

public static int estMaxGridsInFile
estimated max number Grids found in file

estMaxRowsInFile

public static int estMaxRowsInFile
estimated max number Rows in file

estMaxColsInFile

public static int estMaxColsInFile
estimated max number Cols in file

estRowWithData

public static int estRowWithData
estimated row where data starts

bufSize

public int bufSize
size of input buffer

tCols

public int tCols
number of columns/row

tRows

public int tRows
number of rows. i.e. number row Clone Id's

errMsgLog

public java.lang.String errMsgLog
error message log it not null

fileName

public java.lang.String fileName
file to read or write I/O

tName

public java.lang.String tName
Table name

tFields

public java.lang.String[] tFields
names of table fields

tData

public java.lang.String[][] tData
row vectors [0:tRows-1][0:tCols-1]

file

private java.io.File file
file descriptor for I/O
Constructor Detail

FileTable

public FileTable(java.lang.String tableName)
FileTable() - generic Constructor, set some defaults to 0.
Parameters:
tableName - String name of table

FileTable

public FileTable(java.lang.String tableName,
                 int rows,
                 int cols)
FileTable() - Constructor to make empty table of known size
Parameters:
tableName - String name of table
rows - max rows in table
cols - max columns in table
Method Detail

promptFileName

public java.lang.String promptFileName(java.lang.String promptDir,
                                       java.lang.String msg)
promptFileName() - prompt for file name
Parameters:
promptDir - default dir
msg - message to display in file dialog
Returns:
full file path if successfull else null

checkForBadTable

public boolean checkForBadTable()
checkForBadTable() - verify that it is a well formed table with all field names present and with no duplicate entries.
Returns:
true if successful, also stuff error mesasge into "errMsgLog" to be displayed later.

readTableFieldsFromFile

public java.lang.String[] readTableFieldsFromFile(java.lang.String fileName,
                                                  int r)
readTableFieldsFromFile() - open file and read row r tab-delim data.
Parameters:
fileName - file name
r - row to read
Returns:
rFields is array of fields to return if successful else null.

countLinesInFile

public static int countLinesInFile(java.lang.String fileName)
countLinesInFile() - open file and count # of lines in the file. Set estLinesInFile to number lines found.
Parameters:
fileName - of file to read
Returns:
number of lines else 0.

countLinesAndTabCountsInFile

public static int countLinesAndTabCountsInFile(Cvt2Mae cvt,
                                               java.lang.String fileName,
                                               boolean lookForSampleTabsFlag,
                                               java.lang.String msg,
                                               java.awt.Color color)
countLinesAndTabCountsInFile() - open file and count number of lines & tabs
Set estLinesInFile to # lines found.
Set estSamplesRowNbr to estimated row # where Samples are listed.
Set estFieldsRowNbr to estimated row # where Fields are listed.

Try to estimate row numbers for Samples row and for Field row If lookForSampleTabsFlag is FALSE, it assumes that the first row with initial max number tabs with non-null data is the Fields row, otherwise] it is the Samples row.

Note that the Samples row may contain fewer tabs than the Fields row - but it is assumed that both contain more tabs than any other row. When we have satisfied the initial counts, just stop analyzing the tab counts to make it run MUCH MUCH faster.

Parameters:
cvt - instance of Cvt2Mae
fileName - file to count
lookForSampleTabsFlag - if true, look for sample tabs
msg - message for logMsg
color - is the logMsg color
Returns:
number of lines in file else 0.
See Also:
UtilCM.logMsg(java.lang.String, java.awt.Color), countNonNullTabData(java.lang.String)

findMaxGridRowColInFile

public static boolean findMaxGridRowColInFile(Cvt2Mae cvt,
                                              java.lang.String fileName,
                                              java.lang.String msg,
                                              java.awt.Color color,
                                              int rowWithFields,
                                              int rowNbrDataStarts)
findMaxGridRowColInFile() - open file and find Max Grid Row Col in file This assumes that ("grid, grid_row, and grid_col") are in the FieldMap. then set
    estMaxGridsInFile   as estimated max # Grids found in file
    estMaxRowsInFile    as estimated max # Rows in file
    estMaxColsInFile    as estimated max # Cols in file
Parameters:
cvt - instance of Cvt2Mae
fileName - file to count
msg - message for logMsg
color - logMsg color
rowNbrDataStarts - row that data starts
Returns:
true if successful
See Also:
FileTable, readTableFieldsFromFile(java.lang.String, int), ParseTable, ParseTable.getDelimTokens(java.lang.String, boolean[], java.lang.String[], boolean, java.lang.String), UtilCM.cvs2i(java.lang.String, int), UtilCM.logMsg(java.lang.String, java.awt.Color), countNonNullTabData(java.lang.String)

countNonNullTabData

public static int countNonNullTabData(java.lang.String sLine)
countNonNullTabData() - count Non-Null data tabs in line Do not count tabs after reach field with no data.
Parameters:
sLine - line to count lines on
Returns:
number of non-null data tabs

findRowWithKeywordsInFile

public static int findRowWithKeywordsInFile(Cvt2Mae cvt,
                                            java.lang.String fileName,
                                            java.lang.String[] keyWords,
                                            boolean needAllKeywordsFlag,
                                            int maxLinesToCheck,
                                            java.lang.String msg,
                                            java.awt.Color color)
findRowWithKeywordsInFile() - open file and look for first line with keywords (case-independent) in it. If needAllKeywordsFlag is set then all keywords need to be present.
Parameters:
cvt - instance of Cvt2Mae
fileName - file to open and search
keyWords - array of keywords to search
needAllKeywordsFlag - boolean look at all keywords flag
maxLinesToCheck - only look at max number of lines
msg - Not used, Future
color - Not used, Future
Returns:
line number (starting at line 1), else return 0

cvtStrToTable

public boolean cvtStrToTable(java.lang.String sData)
cvtStrToTable() - convert tab-delim String data to Table data structure. Set up the table data structures in this instance of FileTable: (tRows, tCols, tFields[], tData[][]).
Parameters:
sData - data to convert to table
Returns:
true if successful
See Also:
computeTableSize(java.lang.String), storeDataBufParser(java.lang.String)

readFileAsTable

public boolean readFileAsTable(java.lang.String fileName)
readFileAsTable() - read file and tab-delim data as Table. Set up the table data structures in this instance of FileTable: (tRows, tCols, tFields[], tData[][]).
Parameters:
fileName - to open and read to get data to convert
Returns:
true if successful
See Also:
computeTableSize(java.lang.String), readFileAsString(java.lang.String), storeDataBufParser(java.lang.String)

writeData

public boolean writeData(java.lang.String fileName,
                         java.lang.String data)
writeData() - write string data to file
Parameters:
fileName - file to write data to disk
data - write data to disk
Returns:
true if successful
See Also:
writeStringToFile(java.lang.String, java.lang.String)

readFileAsString

public java.lang.String readFileAsString(java.lang.String fileName)
readFileAsString() - read file from disk and return data as String
Parameters:
fileName - file name to read data
Returns:
data as String, null if failed

readFileAsString

public java.lang.String readFileAsString(java.io.File f)
readFileAsString() - Will read file from disk & returns as String
Parameters:
f - File to read
Returns:
data read from the file, else null if failed.

writeStringToFile

public boolean writeStringToFile(java.lang.String fileName,
                                 java.lang.String data)
writeStringToFile() - write data to disk file
Parameters:
fileName - is name of the file to write
data - to write to the file
Returns:
true if successful false if failed

storeDataBufParser

private boolean storeDataBufParser(java.lang.String rawData)
storeDataBufParser() - Put data into Table tData[r][c] and tFields[c], given a raw data String from the whole file. The tData[0:tRow-1][0:tCol-1] and tFields[0:tRow-1]
Parameters:
rawData - is raw data string read from file to convert into Table
Returns:
true if successful false if failed

computeTableSize

private boolean computeTableSize(java.lang.String rawData)
computeTableSize() - setup the (tRows,tCols) of table data. This is a tab-delimited file consisting of the field names in the first row followed by data fields. and set (tRows,tCols, tFields[], tData[][]) to (0,0,null,null).
Parameters:
rawData - raw data representing string table to analyze
Returns:
true if successful false if failed

lookupFieldIdx

public int lookupFieldIdx(java.lang.String fieldName)
lookupFieldIdx() - lookup index of field if exists. if it does not exist, return -1
Parameters:
fieldName -  
Returns:
index if successful, -1 if not found

lookupSubstringFieldIdx

public static int lookupSubstringFieldIdx(java.lang.String[] tblFields,
                                          java.lang.String fieldName)
lookupSubstringFieldIdx() - lookup index of case independent field substring if exists and startsWith tblField[]. if it does not exist, return -1
Parameters:
fieldName - field name to look up
tblFields - array of fields
Returns:
index if successful, -1 if not found

freeTable

public void freeTable()
freeTable() - free Table tData[][] and tFields so can garbage collect it Also zero tRows, tCols