use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class SVDServiceHelperImpl method getFactorsForAnalysis.
private void getFactorsForAnalysis(Collection<BioAssay> bioAssays, Map<Long, Date> bioMaterialDates, Map<ExperimentalFactor, Map<Long, Double>> bioMaterialFactorMap) {
for (BioAssay bioAssay : bioAssays) {
Date processingDate = bioAssay.getProcessingDate();
BioMaterial bm = bioAssay.getSampleUsed();
// can be null
bioMaterialDates.put(bm.getId(), processingDate);
SVDServiceHelperImpl.populateBMFMap(bioMaterialFactorMap, bm);
}
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class BatchConfound method getBioMaterialFactorMap.
private static Map<ExperimentalFactor, Map<Long, Double>> getBioMaterialFactorMap(ExpressionExperiment ee) {
Map<ExperimentalFactor, Map<Long, Double>> bioMaterialFactorMap = new HashMap<>();
for (BioAssay bioAssay : ee.getBioAssays()) {
BioMaterial bm = bioAssay.getSampleUsed();
SVDServiceHelperImpl.populateBMFMap(bioMaterialFactorMap, bm);
}
return bioMaterialFactorMap;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionExperimentBatchCorrectionServiceImpl method checkCorrectability.
@Override
public boolean checkCorrectability(ExpressionExperiment ee, boolean force) {
for (QuantitationType qt : expressionExperimentService.getQuantitationTypes(ee)) {
if (qt.getIsBatchCorrected()) {
ExpressionExperimentBatchCorrectionServiceImpl.log.warn("Experiment already has a batch-corrected quantitation type: " + ee + ": " + qt);
return false;
}
}
ExperimentalFactor batch = this.getBatchFactor(ee);
if (batch == null) {
ExpressionExperimentBatchCorrectionServiceImpl.log.warn("No batch factor found: " + ee);
return false;
}
String bConf = expressionExperimentService.getBatchConfound(ee);
if (bConf != null && !force) {
ExpressionExperimentBatchCorrectionServiceImpl.log.warn("Experiment can not be batch corrected: " + bConf);
ExpressionExperimentBatchCorrectionServiceImpl.log.info("To force batch-correction of a confounded experiment, use the force option (note, that this option also allows outliers while batch correcting).");
return false;
}
/*
* Make sure we have at least two samples per batch. This generally won't happen if batches were defined by
* Gemma.
*/
Map<Long, Integer> batches = new HashMap<>();
Set<BioMaterial> seen = new HashSet<>();
for (BioAssay ba : ee.getBioAssays()) {
BioMaterial bm = ba.getSampleUsed();
if (seen.contains(bm))
continue;
seen.add(bm);
for (FactorValue fv : bm.getFactorValues()) {
if (fv.getExperimentalFactor().equals(batch)) {
Long batchId = fv.getId();
if (!batches.containsKey(batchId))
batches.put(batchId, 0);
batches.put(batchId, batches.get(batchId) + 1);
}
}
}
/*
* consider merging batches. - we already do this when we create the batch factor, so in general batches should
* always have at least 2 samples
*/
for (Long batchId : batches.keySet()) {
if (batches.get(batchId) < 2) {
ExpressionExperimentBatchCorrectionServiceImpl.log.info("Batch with only one sample detected, correction not possible: " + ee + ", batchId=" + batchId);
return false;
}
}
return true;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionExperimentBatchCorrectionServiceImpl method doComBat.
private ExpressionDataDoubleMatrix doComBat(ExpressionExperiment ee, ExpressionDataDoubleMatrix originalDataMatrix, ObjectMatrix<BioMaterial, ExperimentalFactor, Object> design) {
ObjectMatrix<BioMaterial, String, Object> designU = this.convertFactorValuesToStrings(design);
DoubleMatrix<CompositeSequence, BioMaterial> matrix = originalDataMatrix.getMatrix();
designU = this.orderMatrix(matrix, designU);
ScaleType scale = originalDataMatrix.getQuantitationTypes().iterator().next().getScale();
boolean transformed = false;
if (!(scale.equals(ScaleType.LOG2) || scale.equals(ScaleType.LOG10) || scale.equals(ScaleType.LOGBASEUNKNOWN) || scale.equals(ScaleType.LN))) {
ExpressionExperimentBatchCorrectionServiceImpl.log.info(" *** COMBAT: LOG TRANSFORMING ***");
transformed = true;
MatrixStats.logTransform(matrix);
}
/*
* Process
*/
ComBat<CompositeSequence, BioMaterial> comBat = new ComBat<>(matrix, designU);
// false: NONPARAMETRIC
DoubleMatrix2D results = comBat.run(true);
// note these plots always reflect the parametric setup.
// TEMPORARY?
comBat.plot(ee.getId() + "." + FileTools.cleanForFileName(ee.getShortName()));
/*
* Postprocess. Results is a raw matrix/
*/
DoubleMatrix<CompositeSequence, BioMaterial> correctedDataMatrix = new DenseDoubleMatrix<>(results.toArray());
correctedDataMatrix.setRowNames(matrix.getRowNames());
correctedDataMatrix.setColumnNames(matrix.getColNames());
if (transformed) {
MatrixStats.unLogTransform(correctedDataMatrix);
}
ExpressionDataDoubleMatrix correctedExpressionDataMatrix = new ExpressionDataDoubleMatrix(originalDataMatrix, correctedDataMatrix);
assert correctedExpressionDataMatrix.getQuantitationTypes().size() == 1;
/*
* It is easier if we make a new quantitationtype.
*/
QuantitationType oldQt = correctedExpressionDataMatrix.getQuantitationTypes().iterator().next();
QuantitationType newQt = this.makeNewQuantitationType(oldQt);
correctedExpressionDataMatrix.getQuantitationTypes().clear();
correctedExpressionDataMatrix.getQuantitationTypes().add(newQt);
// Sanity check...
for (int i = 0; i < correctedExpressionDataMatrix.columns(); i++) {
assert correctedExpressionDataMatrix.getBioMaterialForColumn(i).equals(originalDataMatrix.getBioMaterialForColumn(i));
}
return correctedExpressionDataMatrix;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionExperimentBatchCorrectionServiceImpl method comBat.
@Override
public ExpressionDataDoubleMatrix comBat(ExpressionDataDoubleMatrix originalDataMatrix) {
ExpressionExperiment ee = originalDataMatrix.getExpressionExperiment();
ee = expressionExperimentService.thawLite(ee);
/*
* is there a batch to use?
*/
ExperimentalFactor batch = this.getBatchFactor(ee);
if (batch == null) {
ExpressionExperimentBatchCorrectionServiceImpl.log.warn("No batch factor found");
return null;
}
ObjectMatrix<BioMaterial, ExperimentalFactor, Object> design = this.getDesign(ee, originalDataMatrix);
return this.doComBat(ee, originalDataMatrix, design);
}
Aggregations