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.datafilter;
20  
21  import java.util.List;
22  import java.util.Set;
23  import java.util.Vector;
24  
25  import ubic.basecode.dataStructure.matrix.Matrix2D;
26  import ubic.basecode.dataStructure.matrix.MatrixUtil;
27  
28  /**
29   * Remove or retain rows that are on a list.
30   * 
31   * @author Paul Pavlidis
32   * 
33   */
34  public class RowNameFilter<M extends Matrix2D<R, C, V>, R, C, V> extends AbstractFilter<M, R, C, V> {
35  
36      private boolean exclude = false;
37      private Set<R> filterNames;
38  
39      public RowNameFilter() {
40          filterNames = null;
41      }
42  
43      /**
44       * @param namesToFilter
45       */
46      public RowNameFilter( Set<R> namesToFilter ) {
47          filterNames = namesToFilter;
48      }
49  
50      /**
51       * @param namesToFilter
52       * @param exclude Set to true if you want the list to indicate items to be skipped, rather than selected.
53       */
54      public RowNameFilter( Set<R> namesToFilter, boolean exclude ) {
55          this( namesToFilter );
56          this.exclude = exclude;
57      }
58  
59      /**
60       * Filter according to row names.
61       * 
62       * @param data
63       * @return
64       */
65      @Override
66      public M filter( M data ) {
67          List<V[]> MTemp = new Vector<V[]>();
68          List<R> rowNames = new Vector<R>();
69          int numRows = data.rows();
70          int numCols = data.columns();
71          int numNeeded = filterNames.size();
72          int kept = 0;
73          for ( int i = 0; i < numRows; i++ ) {
74              R name = data.getRowName( i );
75  
76              // apply the rules.
77              if ( filterNames.contains( name ) ) {
78                  if ( exclude ) {
79                      continue;
80                  }
81                  MTemp.add( MatrixUtil.getRow( data, i ) );
82                  rowNames.add( name );
83                  kept++;
84                  if ( kept >= numNeeded ) {
85                      break; // no use in continuing.
86                  }
87              }
88  
89              if ( exclude ) {
90                  MTemp.add( MatrixUtil.getRow( data, i ) );
91                  rowNames.add( name );
92                  kept++;
93              }
94          }
95  
96          M returnval = getOutputMatrix( data, MTemp.size(), numCols );
97  
98          for ( int i = 0; i < MTemp.size(); i++ ) {
99              for ( int j = 0; j < numCols; j++ ) {
100                 returnval.set( i, j, MTemp.get( i )[j] );
101             }
102         }
103         returnval.setColumnNames( data.getColNames() );
104         returnval.setRowNames( rowNames );
105 
106         log.info( "There are " + kept + " rows left after filtering." );
107 
108         return returnval;
109     }
110 
111     public void setFilterNames( Set<R> namesToFilter, boolean exclude ) {
112         this.filterNames = namesToFilter;
113         this.exclude = exclude;
114     }
115 
116 }