use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class CharacteristicUpdateTaskImpl method removeFromParent.
private void removeFromParent(Characteristic c, Object parent) {
if (parent instanceof ExpressionExperiment) {
ExpressionExperiment ee = (ExpressionExperiment) parent;
ee = expressionExperimentService.thawLite(ee);
ee.getCharacteristics().remove(c);
expressionExperimentService.update(ee);
} else if (parent instanceof BioMaterial) {
BioMaterial bm = (BioMaterial) parent;
bm.getCharacteristics().remove(c);
bioMaterialService.update(bm);
} else if (parent instanceof FactorValue) {
FactorValue fv = (FactorValue) parent;
fv.getCharacteristics().remove(c);
factorValueService.update(fv);
}
}
use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class BioAssayDimensionValueObject method makeDummyBioAssayDimension.
private BioAssayDimension makeDummyBioAssayDimension() {
assert this.id == null;
BioAssayDimension fakeBd = BioAssayDimension.Factory.newInstance("Placeholder representing: " + name, description, new ArrayList<BioAssay>());
Map<Long, ExperimentalFactor> fakeEfs = new HashMap<>();
for (BioAssayValueObject bav : this.bioAssays) {
BioAssay ba = BioAssay.Factory.newInstance();
ba.setId(bav.getId());
ba.setName(bav.getName());
ba.setDescription("Fake placeholder");
BioMaterial sampleUsed = BioMaterial.Factory.newInstance();
BioMaterialValueObject bmVo = bav.getSample();
assert bmVo != null;
sampleUsed.setId(bmVo.getId());
sampleUsed.setName(bmVo.getName());
sampleUsed.setDescription("Fake placeholder");
for (IdentifiableValueObject iVo : bmVo.getFactorValueObjects()) {
FactorValueValueObject fvVo = (FactorValueValueObject) iVo;
FactorValue fv = FactorValue.Factory.newInstance();
assert fvVo.getId() != null;
fv.setId(fvVo.getId());
assert fvVo.getValue() != null;
fv.setValue(fvVo.getValue());
Long efId = fvVo.getFactorId();
ExperimentalFactor ef;
if (fakeEfs.containsKey(efId)) {
ef = fakeEfs.get(efId);
} else {
ef = ExperimentalFactor.Factory.newInstance();
ef.setId(efId);
ef.setName(fvVo.getCategory());
ef.setType(fvVo.isMeasurement() ? FactorType.CONTINUOUS : FactorType.CATEGORICAL);
fakeEfs.put(efId, ef);
}
ef.getFactorValues().add(fv);
fv.setExperimentalFactor(ef);
sampleUsed.getFactorValues().add(fv);
}
ba.setSampleUsed(sampleUsed);
ArrayDesign ad = ArrayDesign.Factory.newInstance();
ArrayDesignValueObject adVo = bav.getArrayDesign();
assert adVo != null;
ad.setId(adVo.getId());
ad.setShortName(adVo.getShortName());
ad.setDescription("Fake placeholder");
ba.setArrayDesignUsed(ad);
fakeBd.getBioAssays().add(ba);
}
return fakeBd;
}
use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class ExpressionExperimentSubSetDaoImpl method getFactorValuesUsed.
@Override
public Collection<FactorValueValueObject> getFactorValuesUsed(Long subSetId, Long experimentalFactor) {
// noinspection unchecked
List<FactorValue> list = this.getSessionFactory().getCurrentSession().createQuery("select distinct fv from ExpressionExperimentSubSet es join es.bioAssays ba join ba.sampleUsed bm " + "join bm.factorValues fv where es.id=:es and fv.experimentalFactor.id = :ef ").setParameter("es", subSetId).setParameter("ef", experimentalFactor).list();
Collection<FactorValueValueObject> result = new HashSet<>();
for (FactorValue fv : list) {
result.add(new FactorValueValueObject(fv));
}
return result;
}
use of ubic.gemma.model.expression.experiment.FactorValue 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;
}
use of ubic.gemma.model.expression.experiment.FactorValue 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;
}
Aggregations