public static < T > T [ ] [ ] removeGaps ( final T [ ] [ ] gapped ) { if ( gapped == null ) return null ; if ( gapped . length < 1 ) return Arrays . copyOf ( gapped , gapped . length ) ; final int nProts = gapped . length ; final int protLen = gapped [ 0 ] . length ; // length of gapped proteins // Verify that input is rectangular for ( int i = 0 ; i < nProts ; i ++ ) { if ( gapped [ i ] . length != protLen ) { throw new IllegalArgumentException ( String . format ( "Expected a rectangular array, but row 0 has %d elements " + "while row %d has %d." , protLen , i , gapped [ i ] . length ) ) ; } } // determine where gaps exist in any structures boolean [ ] isGap = new boolean [ protLen ] ; int gaps = 0 ; for ( int j = 0 ; j < protLen ; j ++ ) { for ( int i = 0 ; i < nProts ; i ++ ) { if ( gapped [ i ] [ j ] == null ) { isGap [ j ] = true ; gaps ++ ; break ; //go to next position } } } // Create ungapped array T [ ] [ ] ungapped = Arrays . copyOf ( gapped , nProts ) ; final int ungappedLen = protLen - gaps ; for ( int i = 0 ; i < nProts ; i ++ ) { ungapped [ i ] = Arrays . copyOf ( gapped [ i ] , ungappedLen ) ; int k = 0 ; for ( int j = 0 ; j < protLen ; j ++ ) { if ( ! isGap [ j ] ) { //skip gaps assert ( gapped [ i ] [ j ] != null ) ; ungapped [ i ] [ k ] = gapped [ i ] [ j ] ; k ++ ; } } assert ( k == ungappedLen ) ; } return ungapped ; }
Creates a new list consisting of all columns of gapped where no row contained a null value .