ExpressionDataMatrixColumnSort { public static <R> DoubleMatrix<R, BioAssay> orderByExperimentalDesign( DoubleMatrix<R, BioAssay> mat ) { List<BioAssay> bioAssays = mat.getColNames(); List<BioMaterial> start = new ArrayList<>(); Map<BioMaterial, BioAssay> bm2ba = new HashMap<>(); for ( BioAssay bioAssay : bioAssays ) { start.add( bioAssay.getSampleUsed() ); bm2ba.put( bioAssay.getSampleUsed(), bioAssay ); } List<BioMaterial> bm = ExpressionDataMatrixColumnSort.orderByExperimentalDesign( start, null ); List<BioAssay> newBioAssayOrder = new ArrayList<>(); for ( BioMaterial bioMaterial : bm ) { assert bm2ba.containsKey( bioMaterial ); newBioAssayOrder.add( bm2ba.get( bioMaterial ) ); } return mat.subsetColumns( newBioAssayOrder ); }  static Map<ExperimentalFactor, FactorValue> getBaselineLevels( Collection<ExperimentalFactor> factors ); static Map<ExperimentalFactor, FactorValue> getBaselineLevels( List<BioMaterial> samplesUsed,
            Collection<ExperimentalFactor> factors ); static DoubleMatrix<R, BioAssay> orderByExperimentalDesign( DoubleMatrix<R, BioAssay> mat ); static List<BioMaterial> orderByExperimentalDesign( ExpressionDataMatrix<?> mat ); static List<BioMaterial> orderByExperimentalDesign( List<BioMaterial> start,
            Collection<ExperimentalFactor> factors );  }
@Test public void testOrderByExperimentalDesignB() { BioAssayDimension bad = BioAssayDimension.Factory.newInstance(); Collection<ExperimentalFactor> factors = new HashSet<>(); for ( int i = 0; i < 5; i++ ) { ExperimentalFactor ef = ExperimentalFactor.Factory.newInstance(); ef.setType( FactorType.CATEGORICAL ); ef.setName( "factor" + i ); if ( i == 4 ) { ef.setName( "mfact" + i ); } ef.setId( ( long ) i ); for ( int j = 0; j < 3; j++ ) { FactorValue fv = FactorValue.Factory.newInstance(); fv.setValue( "fv" + ( j + 1 ) * ( i + 1 ) ); fv.setId( ( long ) ( j + 1 ) * ( i + 1 ) ); fv.setExperimentalFactor( ef ); ef.getFactorValues().add( fv ); if ( j == 2 && i != 4 ) { fv.setValue( "control_group" ); } if ( i == 4 ) { ef.setType( FactorType.CONTINUOUS ); Measurement m = Measurement.Factory.newInstance(); m.setId( ( long ) j * ( i + 1 ) ); m.setValue( j + ".00" ); m.setRepresentation( PrimitiveType.DOUBLE ); fv.setMeasurement( m ); } } factors.add( ef ); } Random random = new Random(); for ( int i = 0; i < 100; i++ ) { BioAssay ba = BioAssay.Factory.newInstance(); ba.setName( "ba" + i ); ba.setId( ( long ) i ); bad.getBioAssays().add( ba ); BioMaterial bm = BioMaterial.Factory.newInstance(); bm.setId( ( long ) i ); bm.setName( "bm" + i ); ba.setSampleUsed( bm ); for ( ExperimentalFactor ef : factors ) { int k = random.nextInt( 4 ); int m = 0; FactorValue toUse = null; for ( FactorValue fv : ef.getFactorValues() ) { if ( m == k ) { toUse = fv; break; } m++; } if ( toUse != null ) bm.getFactorValues().add( toUse ); } } EmptyExpressionMatrix mat = new EmptyExpressionMatrix( bad ); assertEquals( 100, mat.columns() ); List<BioMaterial> ordered = ExpressionDataMatrixColumnSort.orderByExperimentalDesign( mat ); assertEquals( 100, ordered.size() ); }