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;
}
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);
}
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++;
}
}
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;
}
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;
}
Aggregations