Search in sources :

Example 61 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBat method run.

/**
 * @param parametric if false, use the non-parametric (slower) method for estimating the priors.
 * @return corrected data
 * @throws ComBatException combat problems
 */
public DoubleMatrix2D run(boolean parametric) throws ComBatException {
    StopWatch timer = new StopWatch();
    timer.start();
    final DoubleMatrix2D sdata = this.standardize(y, x);
    this.checkForProblems(sdata);
    if (timer.getTime() > 1000) {
        ComBat.log.info("Standardized");
    }
    timer.reset();
    timer.start();
    this.gammaHat(sdata);
    this.deltaHat(sdata);
    // assertEquals( 1.618, deltaHat.get( 0, 0 ), 0.001 );
    // gamma.bar <- apply(gamma.hat, 1, mean)
    gammaBar = new DoubleArrayList();
    t2 = new DoubleArrayList();
    for (int batchIndex = 0; batchIndex < gammaHat.rows(); batchIndex++) {
        double mean = DescriptiveWithMissing.mean(new DoubleArrayList(gammaHat.viewRow(batchIndex).toArray()));
        gammaBar.add(mean);
        t2.add(DescriptiveWithMissing.sampleVariance(new DoubleArrayList(gammaHat.viewRow(batchIndex).toArray()), mean));
    }
    // assertEquals( -0.092144, gammaBar.get( 0 ), 0.001 );
    // assertEquals( 0.2977, t2.get( 1 ), 0.001 );
    aPrior = this.aPrior(deltaHat);
    bPrior = this.bPrior(deltaHat);
    if (timer.getTime() > 1000) {
        ComBat.log.info("Computed priors");
    }
    // assertEquals( 17.4971, aPrior.get( 0 ), 0.0001 );
    // assertEquals( 4.514, bPrior.get( 1 ), 0.0001 );
    DoubleMatrix2D gammastar = new DenseDoubleMatrix2D(numBatches, numProbes);
    DoubleMatrix2D deltastar = new DenseDoubleMatrix2D(numBatches, numProbes);
    if (!parametric) {
        this.runNonParametric(sdata, gammastar, deltastar);
    } else {
        this.runParametric(sdata, gammastar, deltastar);
    }
    DoubleMatrix2D adjustedData = this.rawAdjust(sdata, gammastar, deltastar);
    // assertEquals( -0.95099, adjustedData.get( 18, 0 ), 0.0001 );
    // assertEquals( -0.30273984, adjustedData.get( 14, 6 ), 0.0001 );
    // assertEquals( 0.2097977, adjustedData.get( 7, 3 ), 0.0001 );
    // log.info( adjustedData );
    DoubleMatrix2D result = this.restoreScale(adjustedData);
    if (timer.getTime() > 1000) {
        ComBat.log.info("Done");
    }
    return result;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleArrayList(cern.colt.list.DoubleArrayList) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 62 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBat method restoreScale.

private DoubleMatrix2D restoreScale(DoubleMatrix2D adjustedData) {
    DoubleMatrix2D ones = new DenseDoubleMatrix2D(1, numSamples);
    ones.assign(1.0);
    DoubleMatrix2D adj = solver.mult(varpooled.copy().assign(Functions.sqrt), ones);
    DoubleMatrix2D varRestore = adjustedData.assign(adj, Functions.mult);
    // log.info( varRestore );
    return varRestore.assign(standMean, Functions.plus);
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 63 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBat method deltaHat.

private void deltaHat(DoubleMatrix2D sdata) {
    int batchIndex;
    deltaHat = new DenseDoubleMatrix2D(numBatches, numProbes);
    batchIndex = 0;
    for (String batchId : batches.keySet()) {
        DoubleMatrix2D batchData = this.getBatchData(sdata, batchId);
        for (int j = 0; j < batchData.rows(); j++) {
            DoubleArrayList row = new DoubleArrayList(batchData.viewRow(j).toArray());
            double variance = DescriptiveWithMissing.sampleVariance(row, DescriptiveWithMissing.mean(row));
            deltaHat.set(batchIndex, j, variance);
        }
        batchIndex++;
    }
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 64 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBat method rawAdjust.

private DoubleMatrix2D rawAdjust(DoubleMatrix2D sdata, DoubleMatrix2D gammastar, DoubleMatrix2D deltastar) {
    int batchIndex;
    int batchNum = 0;
    DoubleMatrix2D adjustedData = new DenseDoubleMatrix2D(sdata.rows(), sdata.columns());
    for (String batchId : batches.keySet()) {
        DoubleMatrix2D batchData = this.getBatchData(sdata, batchId);
        DoubleMatrix2D Xbb = this.getBatchDesign(batchId);
        DoubleMatrix2D adjustedBatch = batchData.copy().assign(solver.transpose(solver.mult(Xbb, gammastar)), Functions.minus);
        DoubleMatrix1D deltaStarRow = deltastar.viewRow(batchNum);
        deltaStarRow.assign(Functions.sqrt);
        DoubleMatrix1D ones = new DenseDoubleMatrix1D(batchData.columns());
        ones.assign(1.0);
        DoubleMatrix2D divisor = solver.multOuter(deltaStarRow, ones, null);
        adjustedBatch.assign(divisor, Functions.div);
        /*
             * Now we have to put the data back in the right order -- the batches are all together.
             */
        Map<C, Integer> locations = originalLocationsInMatrix.get(batchId);
        for (batchIndex = 0; batchIndex < adjustedBatch.rows(); batchIndex++) {
            int j = 0;
            for (Integer index : locations.values()) {
                adjustedData.set(batchIndex, index, adjustedBatch.get(batchIndex, j));
                j++;
            }
        }
        batchNum++;
    }
    return adjustedData;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D)

Example 65 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBat method getBatchDesign.

private DoubleMatrix2D getBatchDesign(String batchId) {
    Collection<C> sampleNames = batches.get(batchId);
    DoubleMatrix2D result = new DenseDoubleMatrix2D(sampleNames.size(), batches.keySet().size());
    for (int j = 0; j < batches.keySet().size(); j++) {
        int i = 0;
        for (C sname : sampleNames) {
            DoubleMatrix1D rowInBatch = x.viewRow(data.getColIndexByName(sname));
            result.set(i, j, rowInBatch.get(j));
            i++;
        }
    }
    // log.info( result );
    return result;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)136 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)38 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)36 Algebra (cern.colt.matrix.linalg.Algebra)16 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)13 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)13 Node (edu.cmu.tetrad.graph.Node)11 Graph (edu.cmu.tetrad.graph.Graph)8 Test (org.junit.Test)6 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)6 StringMatrixReader (ubic.basecode.io.reader.StringMatrixReader)6 DataSet (edu.cmu.tetrad.data.DataSet)5 DoubleArrayList (cern.colt.list.DoubleArrayList)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)4 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)3 Endpoint (edu.cmu.tetrad.graph.Endpoint)3 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)3 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)3