1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.io.reader;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.List;
25 import java.util.Vector;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import ubic.basecode.dataStructure.matrix.Matrix2D;
32
33
34
35
36
37
38
39 public abstract class AbstractMatrixReader<M extends Matrix2D<String, String, V>, V> {
40
41 protected static Logger log = LoggerFactory.getLogger( AbstractMatrixReader.class );
42
43 public abstract M read( InputStream stream ) throws IOException;
44
45 public abstract M read( String filename ) throws IOException;
46
47 public abstract M read( String filename, int maxRows ) throws IOException;
48
49
50
51
52
53
54
55 protected List<String> readHeader( BufferedReader dis, int skipColumns ) throws IOException {
56 List<String> headerVec = new Vector<String>();
57 String header = null;
58
59
60
61
62 while ( ( header = dis.readLine() ) != null ) {
63 if ( header.startsWith( "#" ) || header.startsWith( "!" ) || StringUtils.isBlank( header ) ) {
64 continue;
65 }
66 break;
67 }
68
69 if ( header == null ) return headerVec;
70
71 if ( header.startsWith( "\t" ) ) header = "c" + header;
72
73 String[] tokens = StringUtils.splitPreserveAllTokens( header, "\t" );
74
75
76 String previousToken = "";
77 int columnNumber = 0;
78
79 for ( int i = 0; i < tokens.length; i++ ) {
80 String s = StringUtils.strip( tokens[i], " " );
81 boolean missing = false;
82
83 if ( s.compareTo( "\t" ) == 0 ) {
84
85 if ( previousToken.compareTo( "\t" ) == 0 ) {
86 missing = true;
87 } else if ( i == tokens.length - 1 ) {
88 missing = true;
89 } else {
90 previousToken = s;
91 continue;
92 }
93 } else if ( StringUtils.isBlank( s ) ) {
94 missing = true;
95 }
96
97 if ( missing ) {
98 throw new IOException( "Missing values are not allowed in the header (column " + columnNumber + " at '"
99 + header + "')" );
100 }
101 if ( columnNumber > 0 ) {
102
103 if ( skipColumns > 0 && columnNumber <= skipColumns ) {
104
105
106 } else {
107 headerVec.add( s );
108 }
109 } else {
110
111 }
112 columnNumber++;
113
114 previousToken = s;
115 }
116
117 return headerVec;
118
119 }
120 }