Clover coverage report - baseCode - 0.2.5 Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT FRAMES NO FRAMES |
file stats: | LOC: | 134 |  | Methods: | 3
| NCLOC: | 91 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
StringMatrixReader.java | 76.9% | 85.2% | 33.3% | 80.7% |

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 | 0 |
        public NamedMatrix read( String filename ) throws IOException {
30 | 0 |
           File infile = new File( filename );
31 | 0 |
           if ( !infile.exists() || !infile.canRead() ) {
32 | 0 |
              throw new IllegalArgumentException( "Could not read from " + filename );
33 |  |
           }
34 | 0 |
           FileInputStream stream = new FileInputStream( infile );
35 | 0 |
           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 | 113 |
        public NamedMatrix read( InputStream stream ) throws IOException {
46 | 113 |
           StringMatrix2DNamed matrix = null;
47 | 113 |
           Vector MTemp = new Vector();
48 | 113 |
           Vector rowNames = new Vector();
49 | 113 |
           Vector columnNames;
50 | 113 |
           BufferedReader dis = new BufferedReader( new InputStreamReader( stream ) );
51 |  |
           //    BufferedReader dis = new BufferedReader( new FileReader( filename ) );
52 | 113 |
           int columnNumber = 0;
53 | 113 |
           int rowNumber = 0;
54 | 113 |
           String row;
55 |  |
56 | 113 |
           columnNames = readHeader( dis );
57 | 113 |
           int numHeadings = columnNames.size();
58 |  |
59 | ? |
           while ( ( row = dis.readLine() ) != null ) {
60 | 3390 |
              StringTokenizer st = new StringTokenizer( row, "\t", true );
61 | 3390 |
              Vector rowTemp = new Vector();
62 | 3390 |
              columnNumber = 0;
63 | 3390 |
              String previousToken = "";
64 |  |
65 | 3390 |
              while ( st.hasMoreTokens() ) {
66 | 83991 |
                 String s = st.nextToken();
67 |  |
68 | 83991 |
                 boolean missing = false;
69 |  |
70 | 83991 |
                 if ( s.compareTo( "\t" ) == 0 ) {
71 |  |
                    /* two tabs in a row */
72 | 40680 |
                    if ( previousToken.compareTo( "\t" ) == 0 ) {
73 | 659 |
                       missing = true;
74 | 40021 |
                    } else if ( !st.hasMoreTokens() ) { // at end of line.
75 | 50 |
                       missing = true;
76 |  |
                    } else {
77 | 39971 |
                       previousToken = s;
78 | 39971 |
                       continue;
79 |  |
                    }
80 |  |
                 }
81 |  |
82 | 44020 |
                 if ( columnNumber > 0 ) {
83 | 40630 |
                    if ( missing ) {
84 |  |
                       //rowTemp.add(Double.toString(Double.NaN));
85 | 709 |
                       rowTemp.add( "" );
86 |  |
                    } else {
87 | 39921 |
                       rowTemp.add( s );
88 |  |
                    }
89 |  |
                 } else {
90 | 3390 |
                    if ( missing ) {
91 | 0 |
                       throw new IOException(
92 |  |
                             "Missing values not allowed for row labels" );
93 |  |
                    }
94 | 3390 |
                    rowNames.add( s );
95 |  |
                 }
96 |  |
97 | 44020 |
                 columnNumber++;
98 | 44020 |
                 previousToken = s;
99 |  |
              }
100 | 3390 |
              MTemp.add( rowTemp );
101 | 3390 |
              if ( rowTemp.size() > numHeadings ) {
102 | 0 |
                 throw new IOException( "Warning: too many values ("
103 |  |
                       + rowTemp.size() + ") in row " + rowNumber
104 |  |
                       + " (based on headings count of " + numHeadings + ")" );
105 |  |
              }
106 | 3390 |
              rowNumber++;
107 |  |
           }
108 |  |
109 | 113 |
           matrix = new StringMatrix2DNamed( rowNumber, numHeadings );
110 | 113 |
           matrix.setColumnNames( columnNames );
111 | 113 |
           matrix.setRowNames( rowNames );
112 |  |
113 | 113 |
           for ( int i = 0; i < matrix.rows(); i++ ) {
114 | 3390 |
              for ( int j = 0; j < matrix.columns(); j++ ) {
115 | 40680 |
                 if ( ( ( Vector ) MTemp.get( i ) ).size() < j + 1 ) {
116 | 50 |
                    matrix.set( i, j, "" );
117 |  |
                    // this allows the input file to have ragged ends.
118 |  |
                 } else {
119 | 40630 |
                    matrix.set( i, j, ( ( Vector ) MTemp.get( i ) ).get( j ) );
120 |  |
                 }
121 |  |
              }
122 |  |
           }
123 | 113 |
           stream.close();
124 | 113 |
           return matrix;
125 |  |
126 |  |
        }
127 |  |
128 |  |
        /* (non-Javadoc)
129 |  |
         * @see baseCode.io.reader.AbstractNamedMatrixReader#readOneRow(java.io.BufferedReader)
130 |  |
         */
131 | 0 |
        public NamedMatrix readOneRow( BufferedReader dis ) throws IOException {
132 | 0 |
          throw new UnsupportedOperationException();
133 |  |
        }
134 |  |
     }