Search in sources :

Example 41 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class DifferentialExpressionAnalysisCli method processExperiment.

private void processExperiment(ExpressionExperiment ee) {
    Collection<DifferentialExpressionAnalysis> results;
    DifferentialExpressionAnalysisConfig config = new DifferentialExpressionAnalysisConfig();
    try {
        ee = this.eeService.thawLite(ee);
        if (delete) {
            AbstractCLI.log.info("Deleting any analyses for experiment=" + ee);
            differentialExpressionAnalyzerService.deleteAnalyses(ee);
            successObjects.add("Deleted analysis for: " + ee.toString());
            return;
        }
        Collection<ExperimentalFactor> experimentalFactors = ee.getExperimentalDesign().getExperimentalFactors();
        if (experimentalFactors.size() == 0) {
            if (this.expressionExperiments.size() == 1) {
                /*
                     * Only need to be noisy if this is the only ee. Batch processing should be less so.
                     */
                throw new RuntimeException("Experiment does not have an experimental design populated: " + ee.getShortName());
            }
            AbstractCLI.log.warn("Experiment does not have an experimental design populated: " + ee.getShortName());
            return;
        }
        Collection<ExperimentalFactor> factors = this.guessFactors(ee);
        if (factors.size() > 0) {
            /*
                 * Manual selection of factors
                 */
            ExperimentalFactor subsetFactor = this.getSubsetFactor(ee);
            AbstractCLI.log.info("Using " + factors.size() + " factors provided as arguments");
            if (subsetFactor != null) {
                if (factors.contains(subsetFactor)) {
                    throw new IllegalArgumentException("Subset factor cannot also be included as factor to analyze");
                }
                AbstractCLI.log.info("Subsetting by " + subsetFactor);
            }
            config.setAnalysisType(this.type);
            config.setFactorsToInclude(factors);
            config.setSubsetFactor(subsetFactor);
            config.setModerateStatistics(this.ebayes);
            config.setPersist(this.persist);
            boolean rnaSeq = super.eeService.isRNASeq(ee);
            config.setUseWeights(rnaSeq);
            /*
                 * Interactions included by default. It's actually only complicated if there is a subset factor.
                 */
            if (type == null && factors.size() == 2) {
                config.getInteractionsToInclude().add(factors);
            }
            results = this.differentialExpressionAnalyzerService.runDifferentialExpressionAnalyses(ee, config);
        } else {
            if (tryToCopyOld) {
                this.tryToRedoBasedOnOldAnalysis(ee);
            }
            Collection<ExperimentalFactor> factorsToUse = new HashSet<>();
            if (this.ignoreBatch) {
                for (ExperimentalFactor ef : experimentalFactors) {
                    if (!ExperimentalDesignUtils.isBatch(ef)) {
                        factorsToUse.add(ef);
                    }
                }
            } else {
                factorsToUse.addAll(experimentalFactors);
            }
            if (factorsToUse.isEmpty()) {
                throw new RuntimeException("No factors available for " + ee.getShortName());
            }
            if (factorsToUse.size() > 3) {
                if (!tryToCopyOld) {
                    throw new RuntimeException("Experiment has too many factors to run automatically: " + ee.getShortName() + "; try using the -redo flag to base it on an old analysis, or select factors manually");
                }
                results = this.tryToRedoBasedOnOldAnalysis(ee);
            } else {
                config.setFactorsToInclude(factorsToUse);
                config.setPersist(this.persist);
                config.setModerateStatistics(this.ebayes);
                if (factorsToUse.size() == 2) {
                    // include interactions by default
                    config.addInteractionToInclude(factorsToUse);
                }
                boolean rnaSeq = super.eeService.isRNASeq(ee);
                config.setUseWeights(rnaSeq);
                results = this.differentialExpressionAnalyzerService.runDifferentialExpressionAnalyses(ee, config);
            }
        }
        if (results == null) {
            throw new Exception("Failed to process differential expression for experiment " + ee.getShortName());
        }
        if (!this.persist) {
            AbstractCLI.log.info("Writing results to disk");
            for (DifferentialExpressionAnalysis r : results) {
                expressionDataFileService.writeDiffExArchiveFile(ee, r, config);
            }
        }
        successObjects.add(ee.toString());
    } catch (Exception e) {
        AbstractCLI.log.error("Error while processing " + ee + ": " + e.getMessage());
        ExceptionUtils.printRootCauseStackTrace(e);
        errorObjects.add(ee + ": " + e.getMessage());
    }
}
Also used : DifferentialExpressionAnalysisConfig(ubic.gemma.core.analysis.expression.diff.DifferentialExpressionAnalysisConfig) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) DifferentialExpressionAnalysis(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis) HashSet(java.util.HashSet)

Example 42 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class DifferentialExpressionAnalysisCli method guessFactors.

/**
 * Determine which factors to use if given from the command line. Only applicable if analysis is on a single data
 * set.
 */
private Collection<ExperimentalFactor> guessFactors(ExpressionExperiment ee) {
    Collection<ExperimentalFactor> factors = new HashSet<>();
    ExperimentalFactorService efs = this.getBean(ExperimentalFactorService.class);
    if (this.factorNames.size() > 0) {
        if (this.factorIds.size() > 0) {
            throw new IllegalArgumentException("Please provide factor names or ids, not a mixture of each");
        }
        Collection<ExperimentalFactor> experimentalFactors = ee.getExperimentalDesign().getExperimentalFactors();
        for (ExperimentalFactor experimentalFactor : experimentalFactors) {
            // has already implemented way of figuring out human-friendly name of factor value.
            ExperimentalFactorValueObject fvo = new ExperimentalFactorValueObject(experimentalFactor);
            if (ignoreBatch && BatchInfoPopulationServiceImpl.isBatchFactor(experimentalFactor)) {
                AbstractCLI.log.info("Ignoring batch factor:" + experimentalFactor);
                continue;
            }
            if (factorNames.contains(experimentalFactor.getName().replaceAll(" ", "_"))) {
                factors.add(experimentalFactor);
            } else if (fvo.getCategory() != null && factorNames.contains(fvo.getCategory().replaceAll(" ", "_"))) {
                factors.add(experimentalFactor);
            }
        }
        if (factors.size() != factorNames.size()) {
            throw new IllegalArgumentException("Didn't find factors for all the provided factor names");
        }
    } else if (this.factorIds.size() > 0) {
        for (Long factorId : factorIds) {
            if (this.factorNames.size() > 0) {
                throw new IllegalArgumentException("Please provide factor names or ids, not a mixture of each");
            }
            ExperimentalFactor factor = efs.load(factorId);
            if (factor == null) {
                throw new IllegalArgumentException("No factor for id=" + factorId);
            }
            if (!factor.getExperimentalDesign().equals(ee.getExperimentalDesign())) {
                throw new IllegalArgumentException("Factor with id=" + factorId + " does not belong to " + ee);
            }
            if (ignoreBatch && BatchInfoPopulationServiceImpl.isBatchFactor(factor)) {
                AbstractCLI.log.warn("Selected factor looks like a batch, and 'ignoreBatch' is true, skipping:" + factor);
                continue;
            }
            factors.add(factor);
        }
    }
    return factors;
}
Also used : ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExperimentalFactorValueObject(ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject) ExperimentalFactorService(ubic.gemma.persistence.service.expression.experiment.ExperimentalFactorService) HashSet(java.util.HashSet)

Example 43 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class BatchInfoPopulationServiceImpl method removeExistingBatchFactor.

/**
 * Remove an existing batch factor, if it exists. This is really only relevant in a 'force' situation.
 *
 * @param ee ee
 */
private void removeExistingBatchFactor(ExpressionExperiment ee) {
    ExperimentalDesign ed = ee.getExperimentalDesign();
    ExperimentalFactor toRemove = null;
    for (ExperimentalFactor ef : ed.getExperimentalFactors()) {
        if (BatchInfoPopulationServiceImpl.isBatchFactor(ef)) {
            toRemove = ef;
            break;
        /*
                 * FIXME handle the case where we somehow have two or more.
                 */
        }
    }
    if (toRemove == null) {
        return;
    }
    BatchInfoPopulationServiceImpl.log.info("Removing existing batch factor: " + toRemove);
    experimentalFactorService.delete(toRemove);
    ee.getExperimentalDesign().getExperimentalFactors().remove(toRemove);
    this.expressionExperimentService.update(ee);
}
Also used : ExperimentalDesign(ubic.gemma.model.expression.experiment.ExperimentalDesign) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor)

Example 44 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor 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;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) QuantitationType(ubic.gemma.model.common.quantitationtype.QuantitationType) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 45 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor 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);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment)

Aggregations

ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)88 Test (org.junit.Test)31 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)30 DifferentialExpressionAnalysis (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis)26 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)22 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)18 HashSet (java.util.HashSet)17 AbstractGeoServiceTest (ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest)17 ExpressionAnalysisResultSet (ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet)16 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)14 DifferentialExpressionAnalysisResult (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysisResult)12 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)11 GeoDomainObjectGeneratorLocal (ubic.gemma.core.loader.expression.geo.GeoDomainObjectGeneratorLocal)10 AlreadyExistsInSystemException (ubic.gemma.core.loader.util.AlreadyExistsInSystemException)10 Before (org.junit.Before)8 Collection (java.util.Collection)7 ContrastResult (ubic.gemma.model.analysis.expression.diff.ContrastResult)6 AnalysisType (ubic.gemma.core.analysis.expression.diff.DifferentialExpressionAnalyzerServiceImpl.AnalysisType)5 ExperimentalFactorValueObject (ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject)5 InputStream (java.io.InputStream)4