View Javadoc

    1   package baseCode.io.reader;
    2
    3   import java.io.BufferedReader;
    4   import java.io.File;
    5   import java.io.FileInputStream;
    6   import java.io.IOException;
    7   import java.io.InputStream;
    8   import java.io.InputStreamReader;
    9   import java.util.StringTokenizer;
    10  import java.util.Vector;
    11
    12  import baseCode.dataStructure.matrix.NamedMatrix;
    13  import baseCode.dataStructure.matrix.StringMatrix2DNamed;
    14
    15  /***
    16   * Reader for {@link baseCode.dataStructure.matrix.StringMatrix2DNamed}
    17   * <p>
    18   * Copyright (c) 2004
    19   * </p>
    20   * <p>
    21   * Institution:: Columbia University
    22   * </p>
    23   *
    24   * @author Paul Pavlidis
    25   * @version $Id: StringMatrixReader.java,v 1.3 2004/08/11 22:53:43 pavlidis Exp $
    26   */
    27  public class StringMatrixReader extends AbstractNamedMatrixReader {
    28
    29     public NamedMatrix read( String filename ) throws IOException {
    30        File infile = new File( filename );
    31        if ( !infile.exists() || !infile.canRead() ) {
    32           throw new IllegalArgumentException( "Could not read from " + filename );
    33        }
    34        FileInputStream stream = new FileInputStream( infile );
    35        return read( stream );
    36     }
    37
    38     /***
    39      * Missing values are entered as an empty string.
    40      *
    41      * @param stream InputStream
    42      * @return NamedMatrix
    43      * @throws IOException
    44      */
    45     public NamedMatrix read( InputStream stream ) throws IOException {
    46        StringMatrix2DNamed matrix = null;
    47        Vector MTemp = new Vector();
    48        Vector rowNames = new Vector();
    49        Vector columnNames;
    50        BufferedReader dis = new BufferedReader( new InputStreamReader( stream ) );
    51        //    BufferedReader dis = new BufferedReader( new FileReader( filename ) );
    52        int columnNumber = 0;
    53        int rowNumber = 0;
    54        String row;
    55
    56        columnNames = readHeader( dis );
    57        int numHeadings = columnNames.size();
    58
    59        while ( ( row = dis.readLine() ) != null ) {
    60           StringTokenizer st = new StringTokenizer( row, "\t", true );
    61           Vector rowTemp = new Vector();
    62           columnNumber = 0;
    63           String previousToken = "";
    64
    65           while ( st.hasMoreTokens() ) {
    66              String s = st.nextToken();
    67
    68              boolean missing = false;
    69
    70              if ( s.compareTo( "\t" ) == 0 ) {
    71                 /* two tabs in a row */
    72                 if ( previousToken.compareTo( "\t" ) == 0 ) {
    73                    missing = true;
    74                 } else if ( !st.hasMoreTokens() ) { // at end of line.
    75                    missing = true;
    76                 } else {
    77                    previousToken = s;
    78                    continue;
    79                 }
    80              }
    81
    82              if ( columnNumber > 0 ) {
    83                 if ( missing ) {
    84                    //rowTemp.add(Double.toString(Double.NaN));
    85                    rowTemp.add( "" );
    86                 } else {
    87                    rowTemp.add( s );
    88                 }
    89              } else {
    90                 if ( missing ) {
    91                    throw new IOException(
    92                          "Missing values not allowed for row labels" );
    93                 }
    94                 rowNames.add( s );
    95              }
    96
    97              columnNumber++;
    98              previousToken = s;
    99           }
    100          MTemp.add( rowTemp );
    101          if ( rowTemp.size() > numHeadings ) {
    102             throw new IOException( "Warning: too many values ("
    103                   + rowTemp.size() + ") in row " + rowNumber
    104                   + " (based on headings count of " + numHeadings + ")" );
    105          }
    106          rowNumber++;
    107       }
    108
    109       matrix = new StringMatrix2DNamed( rowNumber, numHeadings );
    110       matrix.setColumnNames( columnNames );
    111       matrix.setRowNames( rowNames );
    112
    113       for ( int i = 0; i < matrix.rows(); i++ ) {
    114          for ( int j = 0; j < matrix.columns(); j++ ) {
    115             if ( ( ( Vector ) MTemp.get( i ) ).size() < j + 1 ) {
    116                matrix.set( i, j, "" );
    117                // this allows the input file to have ragged ends.
    118             } else {
    119                matrix.set( i, j, ( ( Vector ) MTemp.get( i ) ).get( j ) );
    120             }
    121          }
    122       }
    123       stream.close();
    124       return matrix;
    125
    126    }
    127
    128    /* (non-Javadoc)
    129     * @see baseCode.io.reader.AbstractNamedMatrixReader#readOneRow(java.io.BufferedReader)
    130     */
    131    public NamedMatrix readOneRow( BufferedReader dis ) throws IOException {
    132      throw new UnsupportedOperationException();
    133    }
    134 }