1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.dataStructure.matrix;
20
21 import java.util.List;
22
23 import org.apache.commons.lang3.StringUtils;
24
25 import cern.colt.matrix.ObjectMatrix1D;
26 import cern.colt.matrix.impl.DenseObjectMatrix2D;
27
28
29
30
31
32
33
34 public class StringMatrix<R, C> extends AbstractMatrix<R, C, String> implements ObjectMatrix<R, C, String> {
35
36
37
38
39
40 @Override
41 public ObjectMatrix<R, C, String> subsetColumns( List<C> columns ) {
42 StringMatrix<R, C> returnval = new StringMatrix<R, C>( this.rows(), columns.size() );
43 returnval.setRowNames( this.getRowNames() );
44 for ( int i = 0; i < this.rows(); i++ ) {
45 int currentColumn = 0;
46 for ( C c : columns ) {
47 int j = this.getColIndexByName( c );
48
49 returnval.set( i, currentColumn, this.get( i, j ) );
50
51 if ( i == 0 ) {
52 returnval.setColumnName( c, currentColumn );
53 }
54 currentColumn++;
55
56 }
57 }
58 return returnval;
59 }
60
61
62
63
64 private static final long serialVersionUID = -7369003979104984162L;
65 private DenseObjectMatrix2D matrix;
66
67 public StringMatrix( int x, int y ) {
68 super();
69 matrix = new DenseObjectMatrix2D( x, y );
70 }
71
72
73
74
75 @Override
76 public int columns() {
77 return matrix.columns();
78 }
79
80
81
82
83
84
85 @Override
86 public String get( int row, int column ) {
87 return ( String ) matrix.get( row, column );
88 }
89
90
91
92
93
94
95 public String get( R row, C column ) {
96 return this.get( this.getRowIndexByName( row ), this.getColIndexByName( column ) );
97 }
98
99 @Override
100 public String getByKeys( R r, C c ) {
101 return this.get( getRowIndexByName( r ), getColIndexByName( c ) );
102 }
103
104 public String[] getColObj( int col ) {
105 String[] result = new String[rows()];
106 for ( int i = 0; i < rows(); i++ ) {
107 result[i] = get( i, col );
108 }
109 return result;
110 }
111
112 @Override
113 public String[] getColumn( int col ) {
114 String[] result = new String[rows()];
115 for ( int i = 0; i < rows(); i++ ) {
116 result[i] = get( i, col );
117 }
118 return result;
119 }
120
121 @Override
122 public String getEntry( int row, int column ) {
123 return get( row, column );
124 }
125
126 public String getObject( int row, int col ) {
127 return get( row, col );
128 }
129
130 @Override
131 public String[] getRow( int row ) {
132 String[] result = new String[columns()];
133 for ( int i = 0; i < columns(); i++ ) {
134 result[i] = get( row, i );
135 }
136 return result;
137 }
138
139
140
141
142 @Override
143 public boolean isMissing( int i, int j ) {
144 return StringUtils.isBlank( get( i, j ) );
145 }
146
147
148
149
150 @Override
151 public int rows() {
152 return matrix.rows();
153 }
154
155
156
157
158
159
160 @Override
161 public void set( int row, int column, String value ) {
162 matrix.set( row, column, value );
163 }
164
165
166
167
168
169
170 @Override
171 public void setByKeys( R r, C c, String v ) {
172 this.set( getRowIndexByName( r ), getColIndexByName( c ), v );
173 }
174
175
176
177
178 @Override
179 public int size() {
180 return matrix.size();
181 }
182
183 @Override
184 public ObjectMatrix<R, C, String> subset( int startRow, int startCol, int numRow, int numCol ) {
185 int endRow = startRow + numRow - 1;
186 super.checkRowRange( startRow, endRow );
187 int endCol = startCol + numCol - 1;
188 super.checkColRange( startCol, endCol );
189 ObjectMatrix<R, C, String> result = new StringMatrix<R, C>( numRow, numCol );
190 int r = 0;
191 for ( int i = startRow; i <= endRow; i++ ) {
192 int c = 0;
193 result.setRowName( this.getRowName( i ), r );
194 for ( int j = startCol; j <= endCol; j++ ) {
195 if ( r == 0 ) {
196 result.setColumnName( this.getColName( j ), c );
197 }
198 result.set( r, c++, this.get( i, j ) );
199 }
200 r++;
201 }
202
203 return result;
204
205 }
206
207
208
209
210 @Override
211 public String toString() {
212 StringBuffer buf = new StringBuffer();
213 buf.append( "label" );
214 for ( int i = 0; i < columns(); i++ ) {
215 buf.append( "\t" + getColName( i ) );
216 }
217 buf.append( "\n" );
218
219 for ( int i = 0; i < rows(); i++ ) {
220 buf.append( getRowName( i ) );
221 for ( int j = 0; j < columns(); j++ ) {
222 buf.append( "\t" + get( i, j ) );
223 }
224 buf.append( "\n" );
225 }
226 return buf.toString();
227 }
228
229
230
231
232
233 public ObjectMatrix1D viewColumn( int column ) {
234 return matrix.viewColumn( column );
235 }
236
237
238
239
240
241 public ObjectMatrix1D viewRow( int row ) {
242 return matrix.viewRow( row );
243 }
244
245 }