Search in sources :

Example 71 with ExperimentalFactor

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

the class DiffExTest method testCountData.

/**
 * Test differential expression analysis on RNA-seq data. See bug 3383. R code in voomtest.R
 */
@Test
public void testCountData() throws Exception {
    geoService.setGeoDomainObjectGenerator(new GeoDomainObjectGenerator());
    ExpressionExperiment ee = eeService.findByShortName("GSE29006");
    if (ee != null) {
        eeService.remove(ee);
    }
    assertTrue(eeService.findByShortName("GSE29006") == null);
    try {
        Collection<?> results = geoService.fetchAndLoad("GSE29006", false, false, false);
        ee = (ExpressionExperiment) results.iterator().next();
    } catch (AlreadyExistsInSystemException e) {
        throw new IllegalStateException("Need to remove this data set before test is run");
    }
    ee = eeService.thaw(ee);
    try (InputStream is = this.getClass().getResourceAsStream("/data/loader/expression/flatfileload/GSE29006_design.txt")) {
        assertNotNull(is);
        experimentalDesignImporter.importDesign(ee, is);
    }
    // Load the data from a text file.
    DoubleMatrixReader reader = new DoubleMatrixReader();
    ArrayDesign targetArrayDesign;
    try (InputStream countData = this.getClass().getResourceAsStream("/data/loader/expression/flatfileload/GSE29006_expression_count.test.txt")) {
        DoubleMatrix<String, String> countMatrix = reader.read(countData);
        Collection<ExperimentalFactor> experimentalFactors = ee.getExperimentalDesign().getExperimentalFactors();
        assertEquals(1, experimentalFactors.size());
        List<String> probeNames = countMatrix.getRowNames();
        assertEquals(199, probeNames.size());
        // we have to find the right generic platform to use.
        targetArrayDesign = this.getTestPersistentArrayDesign(probeNames, taxonService.findByCommonName("human"));
        targetArrayDesign = arrayDesignService.thaw(targetArrayDesign);
        // the experiment has 8 samples but the data has 4 columns so allow missing samples
        // GSM718707 GSM718708 GSM718709 GSM718710
        dataUpdater.addCountData(ee, targetArrayDesign, countMatrix, null, 36, true, true);
    }
    // make sure to do a thawRawAndProcessed() to get the addCountData() updates
    ee = eeService.thaw(ee);
    // verify rows and columns
    Collection<DoubleVectorValueObject> processedDataArrays = processedExpressionDataVectorService.getProcessedDataArrays(ee);
    assertEquals(199, processedDataArrays.size());
    for (DoubleVectorValueObject v : processedDataArrays) {
        assertEquals(4, v.getBioAssays().size());
    }
    // I confirmed that log2cpm is working same as voom here; not bothering to test directly.
    TestUtils.assertBAs(ee, targetArrayDesign, "GSM718709", 320383);
    // DE analysis without weights to assist comparison to R
    DifferentialExpressionAnalysisConfig config = new DifferentialExpressionAnalysisConfig();
    config.setUseWeights(false);
    config.setFactorsToInclude(ee.getExperimentalDesign().getExperimentalFactors());
    Collection<DifferentialExpressionAnalysis> analyses = analyzer.run(ee, config);
    assertNotNull(analyses);
    assertEquals(1, analyses.size());
    DifferentialExpressionAnalysis results = analyses.iterator().next();
    boolean found = false;
    ExpressionAnalysisResultSet resultSet = results.getResultSets().iterator().next();
    for (DifferentialExpressionAnalysisResult r : resultSet.getResults()) {
        if (r.getProbe().getName().equals("ENSG00000000938")) {
            found = true;
            ContrastResult contrast = r.getContrasts().iterator().next();
            assertEquals(0.007055717, r.getPvalue(), // R: 0.006190738; coeff = 2.2695215; t=12.650422; R with our weights: 0.009858270, 2.2317534; t=9.997007
            0.00001);
            // up to sign
            assertEquals(2.2300049, Math.abs(contrast.getCoefficient()), 0.001);
            break;
        }
    }
    assertTrue(found);
    // With weights
    config = new DifferentialExpressionAnalysisConfig();
    // <----
    config.setUseWeights(true);
    config.setFactorsToInclude(ee.getExperimentalDesign().getExperimentalFactors());
    analyses = analyzer.run(ee, config);
    results = analyses.iterator().next();
    resultSet = results.getResultSets().iterator().next();
    for (DifferentialExpressionAnalysisResult r : resultSet.getResults()) {
        if (r.getProbe().getName().equals("ENSG00000000938")) {
            assertEquals(1, r.getContrasts().size());
            ContrastResult contrast = r.getContrasts().iterator().next();
            // yes!
            assertEquals(2.232816, Math.abs(contrast.getCoefficient()), 0.001);
            assertEquals(0.000311, contrast.getPvalue(), 0.00001);
            assertEquals(56.66342, Math.abs(contrast.getTstat()), 0.001);
            assertEquals(0.007068, r.getPvalue(), 0.00001);
            break;
        }
    }
}
Also used : InputStream(java.io.InputStream) ArrayDesign(ubic.gemma.model.expression.arrayDesign.ArrayDesign) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) DifferentialExpressionAnalysis(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis) DifferentialExpressionAnalysisResult(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysisResult) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) DoubleMatrixReader(ubic.basecode.io.reader.DoubleMatrixReader) ExpressionAnalysisResultSet(ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet) GeoDomainObjectGenerator(ubic.gemma.core.loader.expression.geo.GeoDomainObjectGenerator) AlreadyExistsInSystemException(ubic.gemma.core.loader.util.AlreadyExistsInSystemException) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) ContrastResult(ubic.gemma.model.analysis.expression.diff.ContrastResult) AbstractGeoServiceTest(ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest) Test(org.junit.Test)

Example 72 with ExperimentalFactor

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

the class AncovaTest method testAncovaContinuousCovariate.

/*
     * With a continuous covariate only
     */
@Test
public void testAncovaContinuousCovariate() {
    this.configureMocks();
    /*
         * Add a continuous factor
         */
    ExperimentalFactor experimentalFactorC = ExperimentalFactor.Factory.newInstance();
    experimentalFactorC.setName("confabulatiliationity");
    experimentalFactorC.setId(5399424551L);
    experimentalFactorC.setType(FactorType.CONTINUOUS);
    this.setupFactorValues(experimentalFactorC);
    // leave off the others.
    expressionExperiment.getExperimentalDesign().getExperimentalFactors().clear();
    expressionExperiment.getExperimentalDesign().getExperimentalFactors().add(experimentalFactorC);
    DifferentialExpressionAnalysisConfig config = new DifferentialExpressionAnalysisConfig();
    config.setFactorsToInclude(expressionExperiment.getExperimentalDesign().getExperimentalFactors());
    Collection<DifferentialExpressionAnalysis> expressionAnalyses = analyzer.run(expressionExperiment, config);
    assertTrue(!expressionAnalyses.isEmpty());
    DifferentialExpressionAnalysis expressionAnalysis = expressionAnalyses.iterator().next();
    assertNotNull(expressionAnalysis);
    Collection<ExpressionAnalysisResultSet> resultSets = expressionAnalysis.getResultSets();
    assertEquals(1, resultSets.size());
    for (ExpressionAnalysisResultSet resultSet : resultSets) {
        Collection<ExperimentalFactor> factors = resultSet.getExperimentalFactors();
        assertEquals(1, factors.size());
        assertEquals(100, resultSet.getResults().size());
        for (DifferentialExpressionAnalysisResult r : resultSet.getResults()) {
            assertNotNull(r.getCorrectedPvalue());
        }
    }
}
Also used : ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) DifferentialExpressionAnalysis(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis) DifferentialExpressionAnalysisResult(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysisResult) ExpressionAnalysisResultSet(ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet) Test(org.junit.Test)

Example 73 with ExperimentalFactor

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

the class BaselineDetectionTest method testFetchAndLoadGSE18162.

@Test
public void testFetchAndLoadGSE18162() {
    Map<ExperimentalFactor, FactorValue> baselineLevels = ExpressionDataMatrixColumnSort.getBaselineLevels(ee.getExperimentalDesign().getExperimentalFactors());
    // the batch DOES get a baseline. IF we change that then we change
    assertEquals(2, baselineLevels.size());
    // this test.
    for (ExperimentalFactor ef : baselineLevels.keySet()) {
        if (ef.getName().equals(ExperimentalFactorService.BATCH_FACTOR_NAME))
            continue;
        FactorValue fv = baselineLevels.get(ef);
        assertEquals("Control_group", fv.getValue());
    }
}
Also used : FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) AbstractGeoServiceTest(ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest) Test(org.junit.Test)

Example 74 with ExperimentalFactor

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

the class DiffExMetaAnalyzerServiceTest method testAnalyze.

@Test
public void testAnalyze() throws Exception {
    ExpressionExperiment ds1 = experimentService.findByShortName("GSE2018");
    ExpressionExperiment ds2 = experimentService.findByShortName("GSE6344");
    ExpressionExperiment ds3 = experimentService.findByShortName("GSE2111");
    assertNotNull(ds1);
    assertNotNull(ds2);
    assertNotNull(ds3);
    ds1 = experimentService.thawLite(ds1);
    ds2 = experimentService.thawLite(ds2);
    ds3 = experimentService.thawLite(ds3);
    processedExpressionDataVectorService.computeProcessedExpressionData(ds1);
    processedExpressionDataVectorService.computeProcessedExpressionData(ds2);
    processedExpressionDataVectorService.computeProcessedExpressionData(ds3);
    /*
         * Delete the experimental design (which came with the GEO import) and reload. the new designs have been
         * modified to have just one factor with two levels. (The data sets have nothing to do with each other, it's
         * just a test)
         */
    for (ExperimentalFactor ef : ds1.getExperimentalDesign().getExperimentalFactors()) {
        experimentalFactorService.delete(ef);
    }
    for (ExperimentalFactor ef : ds2.getExperimentalDesign().getExperimentalFactors()) {
        experimentalFactorService.delete(ef);
    }
    for (ExperimentalFactor ef : ds3.getExperimentalDesign().getExperimentalFactors()) {
        experimentalFactorService.delete(ef);
    }
    ds1.getExperimentalDesign().getExperimentalFactors().clear();
    ds2.getExperimentalDesign().getExperimentalFactors().clear();
    ds3.getExperimentalDesign().getExperimentalFactors().clear();
    experimentService.update(ds1);
    experimentService.update(ds2);
    experimentService.update(ds3);
    ds1 = experimentService.load(ds1.getId());
    ds1 = experimentService.thawLite(ds1);
    ds2 = experimentService.load(ds2.getId());
    ds2 = experimentService.thawLite(ds2);
    ds3 = experimentService.load(ds3.getId());
    ds3 = experimentService.thawLite(ds3);
    designImporter.importDesign(ds1, this.getClass().getResourceAsStream("/data/loader/expression/geo/meta-analysis/gse2018.design.txt"));
    designImporter.importDesign(ds2, this.getClass().getResourceAsStream("/data/loader/expression/geo/meta-analysis/gse6344.design.txt"));
    designImporter.importDesign(ds3, this.getClass().getResourceAsStream("/data/loader/expression/geo/meta-analysis/gse2111.design.txt"));
    ds1 = experimentService.thawLite(ds1);
    ds2 = experimentService.thawLite(ds2);
    ds3 = experimentService.thawLite(ds3);
    /*
         * Run differential analyses.
         */
    differentialExpressionAnalyzerService.runDifferentialExpressionAnalyses(ds1, this.getConfig(ds1));
    differentialExpressionAnalyzerService.runDifferentialExpressionAnalyses(ds2, this.getConfig(ds2));
    differentialExpressionAnalyzerService.runDifferentialExpressionAnalyses(ds3, this.getConfig(ds3));
    /*
         * Prepare for meta-analysis.
         */
    Collection<DifferentialExpressionAnalysis> ds1Analyses = differentialExpressionAnalysisService.findByInvestigation(ds1);
    Collection<DifferentialExpressionAnalysis> ds2Analyses = differentialExpressionAnalysisService.findByInvestigation(ds2);
    Collection<DifferentialExpressionAnalysis> ds3Analyses = differentialExpressionAnalysisService.findByInvestigation(ds3);
    assertTrue(!ds1Analyses.isEmpty());
    assertTrue(!ds2Analyses.isEmpty());
    assertTrue(!ds3Analyses.isEmpty());
    differentialExpressionAnalysisService.thaw(ds1Analyses);
    differentialExpressionAnalysisService.thaw(ds2Analyses);
    differentialExpressionAnalysisService.thaw(ds3Analyses);
    ExpressionAnalysisResultSet rs1 = ds1Analyses.iterator().next().getResultSets().iterator().next();
    ExpressionAnalysisResultSet rs2 = ds2Analyses.iterator().next().getResultSets().iterator().next();
    ExpressionAnalysisResultSet rs3 = ds3Analyses.iterator().next().getResultSets().iterator().next();
    Collection<Long> analysisResultSetIds = new HashSet<>();
    analysisResultSetIds.add(rs1.getId());
    analysisResultSetIds.add(rs2.getId());
    analysisResultSetIds.add(rs3.getId());
    /*
         * Perform the meta-analysis without saving it.
         */
    GeneDifferentialExpressionMetaAnalysis metaAnalysis = analyzerService.analyze(analysisResultSetIds);
    assertNotNull(metaAnalysis);
    assertEquals(3, metaAnalysis.getResultSetsIncluded().size());
    this.testGenes(metaAnalysis);
    /*
         * Test ancillary methods
         */
    metaAnalysis.setName(RandomStringUtils.random(10));
    metaAnalysis = analyzerService.persist(metaAnalysis);
    assertNotNull(metaAnalysis.getId());
    /*
         * Test validity of stored analysis.
         */
    this.testAnalysis(metaAnalysis);
    // bug 3722
    analysisService.remove(metaAnalysis);
    /*
         * Kitchen sink extra tests.
         */
    this.extraTests1(ds1);
    /*
         * More tests since we have a bunch of stuff loaded.
         */
    this.extraTests2(ds1, ds2, ds3);
}
Also used : ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) AbstractGeoServiceTest(ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest) Test(org.junit.Test)

Example 75 with ExperimentalFactor

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

the class BusinessKey method createQueryObject.

public static void createQueryObject(Criteria queryObject, FactorValue factorValue) {
    ExperimentalFactor ef = factorValue.getExperimentalFactor();
    if (ef == null)
        throw new IllegalArgumentException("Must have experimentalfactor on factorvalue to search");
    Criteria innerQuery = queryObject.createCriteria("experimentalFactor");
    BusinessKey.addRestrictions(innerQuery, ef);
    if (factorValue.getValue() != null) {
        queryObject.add(Restrictions.eq("value", factorValue.getValue()));
    } else if (factorValue.getCharacteristics().size() > 0) {
        /*
             * All the characteristics have to match ones in the result, and the result cannot have any extras. In other
             * words there has to be a one-to-one match between the characteristics.
             */
        // this takes care of the size check
        queryObject.add(Restrictions.sizeEq("characteristics", factorValue.getCharacteristics().size()));
        // now the equivalence.
        Criteria characteristicsCriteria = queryObject.createCriteria("characteristics");
        /*
             * Note that this isn't exactly correct, but it should work okay: "If all the characteristics in the
             * candidate are also in the query", along with the size restriction. The only problem would be if the same
             * characteristic were added to an object more than once - so the sizes would be the same, but a
             * characteristic in the query might not show up in the candidate. Multiple entries of the same
             * characteristic shouldn't be allowed, and even if it did happen the chance of a problem is small.... but a
             * formal possibility.
             */
        Disjunction vdj = Restrictions.disjunction();
        for (Characteristic characteristic : factorValue.getCharacteristics()) {
            Conjunction c = Restrictions.conjunction();
            if (characteristic instanceof VocabCharacteristic) {
                VocabCharacteristic vc = (VocabCharacteristic) characteristic;
                if (vc.getCategoryUri() != null) {
                    c.add(Restrictions.eq("categoryUri", vc.getCategoryUri()));
                }
                if (vc.getValueUri() != null) {
                    c.add(Restrictions.eq("valueUri", vc.getValueUri()));
                }
            }
            if (characteristic.getValue() != null)
                c.add(Restrictions.eq("value", characteristic.getValue()));
            if (characteristic.getCategory() != null)
                c.add(Restrictions.eq("category", characteristic.getCategory()));
            vdj.add(c);
        }
        characteristicsCriteria.add(vdj);
    } else if (factorValue.getMeasurement() != null) {
        queryObject.add(Restrictions.eq("measurement", factorValue.getMeasurement()));
    }
    queryObject.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
}
Also used : ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) Criteria(org.hibernate.Criteria)

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