Search in sources :

Example 1 with ObjectMatrixImpl

use of ubic.basecode.dataStructure.matrix.ObjectMatrixImpl in project Gemma by PavlidisLab.

the class ExperimentalDesignUtils method sampleInfoMatrix.

/**
 * @return Experimental design matrix
 */
public static ObjectMatrix<BioMaterial, ExperimentalFactor, Object> sampleInfoMatrix(List<ExperimentalFactor> factors, List<BioMaterial> samplesUsed, Map<ExperimentalFactor, FactorValue> baselines) {
    ObjectMatrix<BioMaterial, ExperimentalFactor, Object> designMatrix = new ObjectMatrixImpl<>(samplesUsed.size(), factors.size());
    designMatrix.setColumnNames(factors);
    int row = 0;
    for (BioMaterial samp : samplesUsed) {
        int col = 0;
        for (ExperimentalFactor factor : factors) {
            Object value = ExperimentalDesignUtils.extractFactorValueForSample(baselines, samp, factor);
            designMatrix.set(row, col, value);
            col++;
        }
        row++;
    }
    designMatrix.setRowNames(samplesUsed);
    return designMatrix;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExperimentalFactorValueObject(ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject) ObjectMatrixImpl(ubic.basecode.dataStructure.matrix.ObjectMatrixImpl)

Example 2 with ObjectMatrixImpl

use of ubic.basecode.dataStructure.matrix.ObjectMatrixImpl in project Gemma by PavlidisLab.

the class ExperimentalDesignUtils method buildDesignMatrix.

/**
 * Convert factors to a matrix usable in R. The rows are in the same order as the columns of our data matrix
 * (defined by samplesUsed).
 *
 * @param factors in the order they will be used
 * @return a design matrix
 */
public static ObjectMatrix<String, String, Object> buildDesignMatrix(List<ExperimentalFactor> factors, List<BioMaterial> samplesUsed, Map<ExperimentalFactor, FactorValue> baselines) {
    ObjectMatrix<String, String, Object> designMatrix = new ObjectMatrixImpl<>(samplesUsed.size(), factors.size());
    Map<ExperimentalFactor, String> factorNamesInR = new LinkedHashMap<>();
    for (ExperimentalFactor factor : factors) {
        factorNamesInR.put(factor, ExperimentalDesignUtils.nameForR(factor));
    }
    designMatrix.setColumnNames(new ArrayList<>(factorNamesInR.values()));
    List<String> rowNames = new ArrayList<>();
    int row = 0;
    for (BioMaterial samp : samplesUsed) {
        rowNames.add("biomat_" + samp.getId());
        int col = 0;
        for (ExperimentalFactor factor : factors) {
            Object value = ExperimentalDesignUtils.extractFactorValueForSample(baselines, samp, factor);
            designMatrix.set(row, col, value);
            // if the value is null, we have to skip this factor, actually, but we do it later.
            if (value == null) {
                throw new IllegalStateException("Missing values not tolerated in design matrix");
            }
            col++;
        }
        row++;
    }
    // 
    // /*
    // * Drop columns that have missing values.
    // */
    // List<String> toKeep = new ArrayList<String>();
    // for ( int i = 0; i < designMatrix.columns(); i++ ) {
    // boolean skip = false;
    // Object[] column = designMatrix.getColumn( i );
    // for ( Object o : column ) {
    // if ( o == null ) {
    // skip = true;
    // }
    // }
    // 
    // if ( !skip ) {
    // toKeep.add( designMatrix.getColName( i ) );
    // }
    // }
    // 
    // if ( toKeep.isEmpty() ) {
    // throw new IllegalStateException( "Design matrix had no columns without missing values" );
    // }
    // 
    // if ( toKeep.size() < designMatrix.columns() ) {
    // designMatrix = designMatrix.subsetColumns( toKeep );
    // }
    designMatrix.setRowNames(rowNames);
    return designMatrix;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExperimentalFactorValueObject(ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject) ObjectMatrixImpl(ubic.basecode.dataStructure.matrix.ObjectMatrixImpl)

Example 3 with ObjectMatrixImpl

use of ubic.basecode.dataStructure.matrix.ObjectMatrixImpl in project Gemma by PavlidisLab.

the class ExpressionDataBooleanMatrix method createMatrix.

/**
 * Fill in the data
 */
private ObjectMatrixImpl<CompositeSequence, Integer, Boolean> createMatrix(Collection<? extends DesignElementDataVector> vectors, int maxSize) {
    ObjectMatrixImpl<CompositeSequence, Integer, Boolean> mat = new ObjectMatrixImpl<>(vectors.size(), maxSize);
    // initialize the matrix to false
    for (int i = 0; i < mat.rows(); i++) {
        for (int j = 0; j < mat.columns(); j++) {
            mat.set(i, j, Boolean.FALSE);
        }
    }
    for (int j = 0; j < mat.columns(); j++) {
        mat.addColumnName(j);
    }
    ByteArrayConverter bac = new ByteArrayConverter();
    Map<Integer, CompositeSequence> rowNames = new TreeMap<>();
    for (DesignElementDataVector vector : vectors) {
        BioAssayDimension dimension = vector.getBioAssayDimension();
        byte[] bytes = vector.getData();
        CompositeSequence designElement = vector.getDesignElement();
        Integer rowIndex = this.rowElementMap.get(designElement);
        assert rowIndex != null;
        rowNames.put(rowIndex, designElement);
        boolean[] vals = this.getVals(bac, vector, bytes);
        Collection<BioAssay> bioAssays = dimension.getBioAssays();
        if (bioAssays.size() != vals.length) {
            throw new IllegalStateException("Expected " + vals.length + " bioassays at design element " + designElement + ", got " + bioAssays.size());
        }
        Iterator<BioAssay> it = bioAssays.iterator();
        this.setMatBioAssayValues(mat, rowIndex, ArrayUtils.toObject(vals), bioAssays, it);
    }
    for (int i = 0; i < mat.rows(); i++) {
        mat.addRowName(rowNames.get(i));
    }
    assert mat.getRowNames().size() == mat.rows();
    return mat;
}
Also used : ByteArrayConverter(ubic.basecode.io.ByteArrayConverter) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) ObjectMatrixImpl(ubic.basecode.dataStructure.matrix.ObjectMatrixImpl) DesignElementDataVector(ubic.gemma.model.expression.bioAssayData.DesignElementDataVector) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Aggregations

ObjectMatrixImpl (ubic.basecode.dataStructure.matrix.ObjectMatrixImpl)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)2 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)2 ExperimentalFactorValueObject (ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject)2 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)1 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)1 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)1 DesignElementDataVector (ubic.gemma.model.expression.bioAssayData.DesignElementDataVector)1 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)1