Search in sources :

Example 1 with Measurement

use of ubic.gemma.model.common.measurement.Measurement in project Gemma by PavlidisLab.

the class ExpressionDataMatrixColumnSortTest method testOrderByExperimentalDesignB.

@Test
public void testOrderByExperimentalDesignB() {
    BioAssayDimension bad = BioAssayDimension.Factory.newInstance();
    /*
         * Five factors. Factor4 is a measurmeent.
         */
    Collection<ExperimentalFactor> factors = new HashSet<>();
    for (int i = 0; i < 5; i++) {
        ExperimentalFactor ef = ExperimentalFactor.Factory.newInstance();
        ef.setType(FactorType.CATEGORICAL);
        ef.setName("factor" + i);
        if (i == 4) {
            ef.setName("mfact" + i);
        }
        ef.setId((long) i);
        for (int j = 0; j < 3; j++) {
            FactorValue fv = FactorValue.Factory.newInstance();
            fv.setValue("fv" + (j + 1) * (i + 1));
            fv.setId((long) (j + 1) * (i + 1));
            fv.setExperimentalFactor(ef);
            ef.getFactorValues().add(fv);
            if (j == 2 && i != 4) {
                fv.setValue("control_group");
            }
            if (i == 4) {
                ef.setType(FactorType.CONTINUOUS);
                Measurement m = Measurement.Factory.newInstance();
                m.setId((long) j * (i + 1));
                m.setValue(j + ".00");
                m.setRepresentation(PrimitiveType.DOUBLE);
                fv.setMeasurement(m);
            }
        }
        factors.add(ef);
    }
    Random random = new Random();
    for (int i = 0; i < 100; i++) {
        BioAssay ba = BioAssay.Factory.newInstance();
        ba.setName("ba" + i);
        ba.setId((long) i);
        bad.getBioAssays().add(ba);
        BioMaterial bm = BioMaterial.Factory.newInstance();
        bm.setId((long) i);
        bm.setName("bm" + i);
        ba.setSampleUsed(bm);
        for (ExperimentalFactor ef : factors) {
            /*
                 * Note: if we use 4, then some of the biomaterials will not have a factorvalue for each factor. This is
                 * realistic. Use 3 to fill it in completely.
                 */
            int k = random.nextInt(4);
            int m = 0;
            FactorValue toUse = null;
            for (FactorValue fv : ef.getFactorValues()) {
                if (m == k) {
                    toUse = fv;
                    break;
                }
                m++;
            }
            if (toUse != null)
                bm.getFactorValues().add(toUse);
        // log.info( ba + " -> " + bm + " -> " + ef + " -> " + toUse );
        }
    }
    EmptyExpressionMatrix mat = new EmptyExpressionMatrix(bad);
    assertEquals(100, mat.columns());
    List<BioMaterial> ordered = ExpressionDataMatrixColumnSort.orderByExperimentalDesign(mat);
    assertEquals(100, ordered.size());
// for ( BioMaterial bioMaterial : ordered ) {
// log.info( bioMaterial + " .... " + StringUtils.join( bioMaterial.getFactorValues(), "  --- " ) );
// }
}
Also used : Measurement(ubic.gemma.model.common.measurement.Measurement) BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) Random(java.util.Random) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) HashSet(java.util.HashSet) Test(org.junit.Test) BaseSpringContextTest(ubic.gemma.core.testing.BaseSpringContextTest)

Example 2 with Measurement

use of ubic.gemma.model.common.measurement.Measurement in project Gemma by PavlidisLab.

the class ExperimentalDesignImporterImpl method addMeasurementToFactorValueOfTypeContinous.

/**
 * Add a measurement to a factor value which is of type continuous
 *
 * @param factorValue representing a continuous factor with an associated measurement
 */
private void addMeasurementToFactorValueOfTypeContinous(FactorValue factorValue) {
    Measurement m = Measurement.Factory.newInstance();
    m.setType(MeasurementType.ABSOLUTE);
    m.setValue(factorValue.getValue());
    try {
        // noinspection ResultOfMethodCallIgnored // check if it is a number, don't need the value.
        Double.parseDouble(factorValue.getValue());
        m.setRepresentation(PrimitiveType.DOUBLE);
    } catch (NumberFormatException e) {
        m.setRepresentation(PrimitiveType.STRING);
    }
    factorValue.setMeasurement(m);
    ExperimentalDesignImporterImpl.log.debug("Created " + factorValue + " for experimental factor ");
}
Also used : Measurement(ubic.gemma.model.common.measurement.Measurement)

Example 3 with Measurement

use of ubic.gemma.model.common.measurement.Measurement 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 4 with Measurement

use of ubic.gemma.model.common.measurement.Measurement in project Gemma by PavlidisLab.

the class ExperimentalDesignUtils method extractFactorValueForSample.

private static Object extractFactorValueForSample(Map<ExperimentalFactor, FactorValue> baselines, BioMaterial samp, ExperimentalFactor factor) {
    FactorValue baseLineFV = baselines.get(factor);
    /*
         * Find this biomaterial's value for the current factor.
         */
    Object value = null;
    boolean found = false;
    for (FactorValue fv : samp.getFactorValues()) {
        if (fv.getExperimentalFactor().equals(factor)) {
            if (found) {
                // not unique
                throw new IllegalStateException("Biomaterial had more than one value for factor: " + factor);
            }
            boolean isBaseline = baseLineFV != null && fv.equals(baseLineFV);
            if (ExperimentalDesignUtils.isContinuous(factor)) {
                Measurement measurement = fv.getMeasurement();
                assert measurement != null;
                try {
                    value = Double.parseDouble(measurement.getValue());
                } catch (NumberFormatException e) {
                    value = Double.NaN;
                }
            } else {
                /*
                     * We always use a dummy value. It's not as human-readable but at least we're sure it is unique and
                     * R-compliant. (assuming the fv is persistent!)
                     */
                value = ExperimentalDesignUtils.nameForR(fv, isBaseline);
            }
            found = true;
        // could break here but nice to check for uniqueness.
        }
    }
    if (!found) {
        return null;
    }
    return value;
}
Also used : Measurement(ubic.gemma.model.common.measurement.Measurement) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ExperimentalFactorValueObject(ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject)

Aggregations

Measurement (ubic.gemma.model.common.measurement.Measurement)4 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)3 HashSet (java.util.HashSet)2 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)2 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)2 Random (java.util.Random)1 Test (org.junit.Test)1 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)1 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)1 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)1 ExperimentalFactorValueObject (ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject)1