Search in sources :

Example 56 with ExperimentalFactor

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

the class FactorValueDaoImpl method remove.

@Override
public void remove(final FactorValue factorValue) {
    if (factorValue == null)
        return;
    // noinspection unchecked
    Collection<BioMaterial> bms = this.getSessionFactory().getCurrentSession().createQuery("select distinct bm from BioMaterial as bm join bm.factorValues fv where fv = :fv").setParameter("fv", factorValue).list();
    AbstractDao.log.info("Disassociating " + factorValue + " from " + bms.size() + " biomaterials");
    for (BioMaterial bioMaterial : bms) {
        // temporary, debugging.
        AbstractDao.log.info("Processing " + bioMaterial);
        if (bioMaterial.getFactorValues().remove(factorValue)) {
            this.getSessionFactory().getCurrentSession().update(bioMaterial);
        } else {
            AbstractDao.log.warn("Unexpectedly the factor value was not actually associated with " + bioMaterial);
        }
    }
    List<?> efs = this.getHibernateTemplate().findByNamedParam("select ef from ExperimentalFactor ef join ef.factorValues fv where fv = :fv", "fv", factorValue);
    ExperimentalFactor ef = (ExperimentalFactor) efs.iterator().next();
    ef.getFactorValues().remove(factorValue);
    this.getSessionFactory().getCurrentSession().update(ef);
    // will get the dreaded 'already in session' error if we don't do this.
    this.getSessionFactory().getCurrentSession().flush();
    this.getSessionFactory().getCurrentSession().clear();
    this.getSessionFactory().getCurrentSession().delete(factorValue);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor)

Example 57 with ExperimentalFactor

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

the class BioMaterialServiceImpl method update.

private BioMaterial update(BioMaterialValueObject bmvo) {
    BioMaterial bm = this.load(bmvo.getId());
    Collection<FactorValue> updatedFactorValues = new HashSet<>();
    // all of them.
    Map<String, String> factorIdToFactorValueId = bmvo.getFactorIdToFactorValueId();
    for (String factorIdString : factorIdToFactorValueId.keySet()) {
        String factorValueString = factorIdToFactorValueId.get(factorIdString);
        assert factorIdString.matches("factor\\d+");
        Long factorId = Long.parseLong(factorIdString.substring(6));
        // noinspection StatementWithEmptyBody // no value provided, that's okay, the curator can fill it in later.
        if (StringUtils.isBlank(factorValueString)) {
        } else if (factorValueString.matches("fv\\d+")) {
            // categorical
            long fvId = Long.parseLong(factorValueString.substring(2));
            FactorValue fv = factorValueDao.load(fvId);
            if (fv == null) {
                throw new RuntimeException("No such factorValue with id=" + fvId);
            }
            updatedFactorValues.add(fv);
        } else {
            // continuous, the value send is the actual value, not an id. This will only make sense if the value is
            // a measurement.
            boolean found = false;
            // find the right factor value to update.
            for (FactorValue fv : bm.getFactorValues()) {
                if (fv.getExperimentalFactor().getId().equals(factorId)) {
                    if (fv.getMeasurement() == null) {
                        throw new IllegalStateException("Should have been a measurement associated with fv=" + fv + ", cannot update.");
                    } else if (!fv.getMeasurement().getValue().equals(factorValueString)) {
                        AbstractService.log.debug("Updating continuous value on biomaterial:" + bmvo + ", factor=" + fv.getExperimentalFactor() + " value= '" + factorValueString + "'");
                        fv.getMeasurement().setValue(factorValueString);
                    } else {
                        AbstractService.log.debug("Value unchanged from " + fv.getMeasurement().getValue());
                    }
                    // always add...
                    updatedFactorValues.add(fv);
                    found = true;
                    break;
                }
            }
            if (!found) {
                /*
                     * Have to load the factor, create a factor value.
                     */
                ExperimentalFactor ef = experimentalFactorDao.load(factorId);
                // note that this type of factorvalues are not reused for continuous ones.
                AbstractService.log.info("Adding factor value for " + ef + ": " + factorValueString + " to " + bm);
                FactorValue fv = FactorValue.Factory.newInstance();
                fv.setExperimentalFactor(ef);
                fv.setValue(factorValueString);
                Measurement m = Measurement.Factory.newInstance();
                m.setType(MeasurementType.ABSOLUTE);
                m.setValue(fv.getValue());
                try {
                    // noinspection ResultOfMethodCallIgnored // check if it is a number, don't need the value.
                    Double.parseDouble(fv.getValue());
                    m.setRepresentation(PrimitiveType.DOUBLE);
                } catch (NumberFormatException e) {
                    m.setRepresentation(PrimitiveType.STRING);
                }
                fv.setMeasurement(m);
                fv = factorValueDao.create(fv);
                updatedFactorValues.add(fv);
                ef.getFactorValues().add(fv);
                experimentalFactorDao.update(ef);
            }
        }
    }
    // this is not valid, because it's possible that we are removing a factor value.
    // assert bm.getFactorValues().size() <= updatedFactorValues.size();
    bm.getFactorValues().clear();
    bm.getFactorValues().addAll(updatedFactorValues);
    assert !bm.getFactorValues().isEmpty();
    this.update(bm);
    assert !bm.getFactorValues().isEmpty();
    return bm;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) Measurement(ubic.gemma.model.common.measurement.Measurement) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) HashSet(java.util.HashSet)

Example 58 with ExperimentalFactor

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

the class ExperimentalDesignUtils method buildDesignMatrix.

/**
 * Convert factors to a matrix usable in R. The rows are in the same order as the columns of our data matrix
 * (defined by samplesUsed).
 *
 * @param factors in the order they will be used
 * @return a design matrix
 */
public static ObjectMatrix<String, String, Object> buildDesignMatrix(List<ExperimentalFactor> factors, List<BioMaterial> samplesUsed, Map<ExperimentalFactor, FactorValue> baselines) {
    ObjectMatrix<String, String, Object> designMatrix = new ObjectMatrixImpl<>(samplesUsed.size(), factors.size());
    Map<ExperimentalFactor, String> factorNamesInR = new LinkedHashMap<>();
    for (ExperimentalFactor factor : factors) {
        factorNamesInR.put(factor, ExperimentalDesignUtils.nameForR(factor));
    }
    designMatrix.setColumnNames(new ArrayList<>(factorNamesInR.values()));
    List<String> rowNames = new ArrayList<>();
    int row = 0;
    for (BioMaterial samp : samplesUsed) {
        rowNames.add("biomat_" + samp.getId());
        int col = 0;
        for (ExperimentalFactor factor : factors) {
            Object value = ExperimentalDesignUtils.extractFactorValueForSample(baselines, samp, factor);
            designMatrix.set(row, col, value);
            // if the value is null, we have to skip this factor, actually, but we do it later.
            if (value == null) {
                throw new IllegalStateException("Missing values not tolerated in design matrix");
            }
            col++;
        }
        row++;
    }
    // 
    // /*
    // * Drop columns that have missing values.
    // */
    // List<String> toKeep = new ArrayList<String>();
    // for ( int i = 0; i < designMatrix.columns(); i++ ) {
    // boolean skip = false;
    // Object[] column = designMatrix.getColumn( i );
    // for ( Object o : column ) {
    // if ( o == null ) {
    // skip = true;
    // }
    // }
    // 
    // if ( !skip ) {
    // toKeep.add( designMatrix.getColName( i ) );
    // }
    // }
    // 
    // if ( toKeep.isEmpty() ) {
    // throw new IllegalStateException( "Design matrix had no columns without missing values" );
    // }
    // 
    // if ( toKeep.size() < designMatrix.columns() ) {
    // designMatrix = designMatrix.subsetColumns( toKeep );
    // }
    designMatrix.setRowNames(rowNames);
    return designMatrix;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExperimentalFactorValueObject(ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject) ObjectMatrixImpl(ubic.basecode.dataStructure.matrix.ObjectMatrixImpl)

Example 59 with ExperimentalFactor

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

the class ExperimentalDesignImporterTest method checkResults.

private void checkResults(Collection<BioMaterial> bms) {
    // check.
    assertEquals(4, ee.getExperimentalDesign().getExperimentalFactors().size());
    boolean foundpmi = false;
    Collection<Long> seenFactorValueIds = new HashSet<>();
    for (ExperimentalFactor ef : ee.getExperimentalDesign().getExperimentalFactors()) {
        if (ef.getName().equals("Profile")) {
            assertEquals(FactorType.CATEGORICAL, ef.getType());
            assertEquals(2, ef.getFactorValues().size());
        } else if (ef.getName().equals("PMI (h)")) {
            assertEquals(FactorType.CONTINUOUS, ef.getType());
            assertEquals(8, ef.getFactorValues().size());
        }
        for (FactorValue fv : ef.getFactorValues()) {
            ExperimentalDesignImporterTest.assertFv(fv);
            if (fv.getExperimentalFactor().getName().equals("PMI (h)")) {
                foundpmi = true;
                // continuous
                assertNotNull(fv.getMeasurement());
            }
            seenFactorValueIds.add(fv.getId());
        }
    }
    assertTrue(foundpmi);
    for (BioMaterial bm : bms) {
        assertEquals(4, bm.getFactorValues().size());
        for (FactorValue fv : bm.getFactorValues()) {
            assertTrue(seenFactorValueIds.contains(fv.getId()));
        }
    }
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) HashSet(java.util.HashSet)

Example 60 with ExperimentalFactor

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

the class ExperimentalDesignImporterTestC method testUploadBadDesign.

@Test
public final void testUploadBadDesign() throws Exception {
    /*
         * The following file has a bug in it. It should fail.
         */
    try (InputStream is = this.getClass().getResourceAsStream("/data/loader/expression/geo/designLoadTests/annotationLoadFileBrain2003FirstBadFile.txt")) {
        try {
            // dry run, should fail.
            experimentalDesignImporter.importDesign(ee, is);
            fail("Should have gotten an error when loading a bad file");
        } catch (IOException ok) {
        // ok
        }
        is.close();
    }
    /*
         * make sure we didn't load anything
         */
    ee = this.expressionExperimentService.load(ee.getId());
    ee = expressionExperimentService.thawLite(ee);
    assertEquals(0, ee.getExperimentalDesign().getExperimentalFactors().size());
    /*
         * Now try the good one.
         */
    try (InputStream is = this.getClass().getResourceAsStream("/data/loader/expression/geo/designLoadTests/annotationLoadFileBrain2003SecondGoodFile.txt")) {
        // dry run, should pass
        experimentalDesignImporter.importDesign(ee, is);
    }
    /*
         * Reopen the file.
         */
    try (InputStream is = this.getClass().getResourceAsStream("/data/loader/expression/geo/designLoadTests/annotationLoadFileBrain2003SecondGoodFile.txt")) {
        // not a dry run, should pass.
        experimentalDesignImporter.importDesign(ee, is);
    }
    ee = this.expressionExperimentService.load(ee.getId());
    this.aclTestUtils.checkEEAcls(ee);
    ee = expressionExperimentService.thawLite(ee);
    assertEquals(3, ee.getExperimentalDesign().getExperimentalFactors().size());
    for (BioAssay ba : ee.getBioAssays()) {
        // noinspection ResultOfMethodCallIgnored // Checking if accessible
        ba.getSampleUsed();
    }
    this.checkResults();
    int s = ee.getExperimentalDesign().getExperimentalFactors().size();
    ExperimentalFactor toDelete = ee.getExperimentalDesign().getExperimentalFactors().iterator().next();
    for (BioAssay ba : ee.getBioAssays()) {
        BioMaterial bm = ba.getSampleUsed();
        boolean removed = false;
        for (Iterator<FactorValue> fIt = bm.getFactorValues().iterator(); fIt.hasNext(); ) {
            if (fIt.next().getExperimentalFactor().equals(toDelete)) {
                fIt.remove();
                removed = true;
            }
        }
        if (removed) {
            bioMaterialService.update(bm);
        }
    }
    ee.getExperimentalDesign().getExperimentalFactors().remove(toDelete);
    experimentalFactorService.delete(toDelete);
    experimentalDesignService.update(ee.getExperimentalDesign());
    assertEquals(s - 1, ee.getExperimentalDesign().getExperimentalFactors().size());
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) InputStream(java.io.InputStream) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) IOException(java.io.IOException) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) AbstractGeoServiceTest(ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest) Test(org.junit.Test)

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