View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2006 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.dataStructure.matrix;
20  
21  import java.util.List;
22  
23  import cern.colt.matrix.ObjectMatrix1D;
24  import cern.colt.matrix.impl.DenseObjectMatrix2D;
25  
26  /**
27   * Matrix that can hold any type of object
28   * 
29   * @author pavlidis
30   * 
31   */
32  public class ObjectMatrixImpl<R, C, V> extends AbstractMatrix<R, C, V> implements ObjectMatrix<R, C, V> {
33  
34      private static final long serialVersionUID = -902358802107186038L;
35      private DenseObjectMatrix2D matrix;
36  
37      public ObjectMatrixImpl( int x, int y ) {
38          super();
39          matrix = new DenseObjectMatrix2D( x, y );
40      }
41  
42      /**
43       * @param columns
44       * @return
45       */
46      @Override
47      public ObjectMatrixImpl<R, C, V> subsetColumns( List<C> columns ) {
48          ObjectMatrixImpl<R, C, V> returnval = new ObjectMatrixImpl<R, C, V>( this.rows(), columns.size() );
49          returnval.setRowNames( this.getRowNames() );
50          for ( int i = 0; i < this.rows(); i++ ) {
51              int currentColumn = 0;
52              for ( C c : columns ) {
53                  int j = this.getColIndexByName( c );
54  
55                  returnval.set( i, currentColumn, this.get( i, j ) );
56  
57                  if ( i == 0 ) {
58                      returnval.setColumnName( c, currentColumn );
59                  }
60  
61                  currentColumn++;
62  
63              }
64  
65          }
66          return returnval;
67      }
68  
69      /**
70       * @return
71       */
72      @Override
73      public int columns() {
74          return matrix.columns();
75      }
76  
77      /**
78       * @param row
79       * @param column
80       * @return
81       */
82      @SuppressWarnings("unchecked")
83      @Override
84      public V get( int row, int column ) {
85          return ( V ) matrix.getQuick( row, column );
86      }
87  
88      /*
89       * (non-Javadoc)
90       * 
91       * @see ubic.basecode.dataStructure.matrix.NamedObjectMatrix#get(java.lang.Object, java.lang.Object)
92       */
93      public Object get( R row, C column ) {
94          return get( getRowIndexByName( row ), getColIndexByName( column ) );
95      }
96  
97      @Override
98      public V getByKeys( R r, C c ) {
99          return this.get( getRowIndexByName( r ), getColIndexByName( c ) );
100     }
101 
102     @Override
103     @SuppressWarnings("unchecked")
104     public V[] getColumn( int col ) {
105         V[] result = ( V[] ) new Object[rows()]; // this is how they do it in ArrayList
106         for ( int i = 0; i < rows(); i++ ) {
107             result[i] = get( i, col );
108         }
109 
110         return result;
111     }
112 
113     @Override
114     public V getEntry( int row, int column ) {
115         return get( row, column );
116     }
117 
118     @Override
119     @SuppressWarnings("unchecked")
120     public V[] getRow( int row ) {
121         Object[] ro = viewRow( row ).toArray();
122         V[] result = ( V[] ) new Object[columns()]; // this is how they do it in ArrayList
123         System.arraycopy( ro, 0, result, 0, ro.length );
124         return result;
125     }
126 
127     @Override
128     public boolean isMissing( int i, int j ) {
129         return get( i, j ) == "";
130     }
131 
132     /**
133      * @return
134      */
135 
136     @Override
137     public int rows() {
138         return matrix.rows();
139     }
140 
141     /*
142      * (non-Javadoc)
143      * 
144      * @see ubic.basecode.dataStructure.matrix.NamedMatrix#set(int, int, java.lang.Object)
145      */
146     @Override
147     public void set( int row, int column, V value ) {
148         matrix.setQuick( row, column, value );
149     }
150 
151     /*
152      * (non-Javadoc)
153      * 
154      * @see ubic.basecode.dataStructure.matrix.NamedMatrix#set(java.lang.Object, java.lang.Object, java.lang.Object)
155      */
156     @Override
157     public void setByKeys( R r, C c, V v ) {
158         this.set( getRowIndexByName( r ), getColIndexByName( c ), v );
159     }
160 
161     /**
162      * @return
163      */
164     @Override
165     public int size() {
166         return matrix.size();
167     }
168 
169     @Override
170     public ObjectMatrix<R, C, V> subset( int startRow, int startCol, int numRow, int numCol ) {
171         int endRow = startRow + numRow - 1;
172         super.checkRowRange( startRow, endRow );
173         int endCol = startCol + numCol - 1;
174         super.checkColRange( startCol, endCol );
175         ObjectMatrix<R, C, V> result = new ObjectMatrixImpl<R, C, V>( numRow, numCol );
176         int r = 0;
177         for ( int i = startRow; i < endRow; i++ ) {
178             int c = 0;
179             for ( int j = startCol; j < endCol; j++ ) {
180                 result.set( r, c++, this.get( i, j ) );
181             }
182             r++;
183         }
184         /*
185          * FIXME set up the row/column names.
186          */
187         return result;
188 
189     }
190 
191     @Override
192     public String toString() {
193         StringBuffer buf = new StringBuffer();
194         buf.append( "label" );
195         for ( int i = 0; i < columns(); i++ ) {
196             buf.append( "\t" + getColName( i ) );
197         }
198         buf.append( "\n" );
199 
200         for ( int i = 0; i < rows(); i++ ) {
201             buf.append( getRowName( i ) );
202             for ( int j = 0; j < columns(); j++ ) {
203                 V v = get( i, j );
204 
205                 if ( v instanceof Double ) {
206                     buf.append( String.format( "\t%.3g", v ) );
207 
208                 } else {
209                     buf.append( "\t" + v );
210                 }
211             }
212             buf.append( "\n" );
213         }
214         return buf.toString();
215     }
216 
217     /**
218      * @param column
219      * @return
220      */
221     public ObjectMatrix1D viewColumn( int column ) {
222         return matrix.viewColumn( column );
223     }
224 
225     /**
226      * @param row
227      * @return
228      */
229     public ObjectMatrix1D viewRow( int row ) {
230         return matrix.viewRow( row );
231     }
232 }