use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class BioMaterialServiceImpl method associateBatchFactor.
@Override
public void associateBatchFactor(final Map<BioMaterial, Date> dates, final Map<Date, FactorValue> d2fv) {
for (final BioMaterial bm : dates.keySet()) {
final BioMaterial toUpdate = this.bioMaterialDao.load(bm.getId());
if (!d2fv.isEmpty()) {
toUpdate.getFactorValues().add(d2fv.get(dates.get(toUpdate)));
}
for (final BioAssay ba : toUpdate.getBioAssaysUsedIn()) {
if (ba.getProcessingDate() != null) {
if (!ba.getProcessingDate().equals(dates.get(toUpdate))) {
ba.setProcessingDate(dates.get(toUpdate));
bioAssayDao.update(ba);
}
} else {
ba.setProcessingDate(dates.get(toUpdate));
bioAssayDao.update(ba);
}
}
bioMaterialDao.update(toUpdate);
}
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class BioMaterialServiceImpl method getBioMaterialIdList.
@Override
public String getBioMaterialIdList(Collection<BioMaterial> bioMaterials) {
StringBuilder buf = new StringBuilder();
for (BioMaterial bm : bioMaterials) {
buf.append(bm.getId());
buf.append(",");
}
return buf.toString().replaceAll(",$", "");
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial 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.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionExperimentDaoImpl method thaw.
private ExpressionExperiment thaw(ExpressionExperiment ee, boolean vectorsAlso) {
if (ee == null) {
return null;
}
if (ee.getId() == null)
throw new IllegalArgumentException("id cannot be null, cannot be thawed: " + ee);
/*
* Trying to do everything fails miserably, so we still need a hybrid approach. But returning the thawed object,
* as opposed to thawing the one passed in, solves problems.
*/
String thawQuery = "select distinct e from ExpressionExperiment e " + " left join fetch e.accession acc left join fetch acc.externalDatabase where e.id=:eeId";
List res = this.getSessionFactory().getCurrentSession().createQuery(thawQuery).setParameter("eeId", ee.getId()).list();
if (res.size() == 0) {
throw new IllegalArgumentException("No experiment with id=" + ee.getId() + " could be loaded.");
}
ExpressionExperiment result = (ExpressionExperiment) res.iterator().next();
Hibernate.initialize(result.getMeanVarianceRelation());
Hibernate.initialize(result.getQuantitationTypes());
Hibernate.initialize(result.getCharacteristics());
Hibernate.initialize(result.getRawDataFile());
Hibernate.initialize(result.getPrimaryPublication());
Hibernate.initialize(result.getOtherRelevantPublications());
Hibernate.initialize(result.getBioAssays());
Hibernate.initialize(result.getAuditTrail());
Hibernate.initialize(result.getGeeq());
if (result.getAuditTrail() != null)
Hibernate.initialize(result.getAuditTrail().getEvents());
Hibernate.initialize(result.getCurationDetails());
for (BioAssay ba : result.getBioAssays()) {
Hibernate.initialize(ba.getArrayDesignUsed());
Hibernate.initialize(ba.getArrayDesignUsed().getDesignProvider());
Hibernate.initialize(ba.getDerivedDataFiles());
Hibernate.initialize(ba.getSampleUsed());
BioMaterial bm = ba.getSampleUsed();
if (bm != null) {
Hibernate.initialize(bm.getFactorValues());
Hibernate.initialize(bm.getTreatments());
}
}
ExperimentalDesign experimentalDesign = result.getExperimentalDesign();
if (experimentalDesign != null) {
Hibernate.initialize(experimentalDesign);
Hibernate.initialize(experimentalDesign.getExperimentalFactors());
experimentalDesign.getTypes().size();
for (ExperimentalFactor factor : experimentalDesign.getExperimentalFactors()) {
Hibernate.initialize(factor.getAnnotations());
for (FactorValue f : factor.getFactorValues()) {
Hibernate.initialize(f.getCharacteristics());
if (f.getMeasurement() != null) {
Hibernate.initialize(f.getMeasurement());
if (f.getMeasurement().getUnit() != null) {
Hibernate.initialize(f.getMeasurement().getUnit());
}
}
}
}
}
this.thawReferences(result);
this.thawMeanVariance(result);
if (vectorsAlso) {
/*
* Optional because this could be slow.
*/
Hibernate.initialize(result.getRawExpressionDataVectors());
Hibernate.initialize(result.getProcessedExpressionDataVectors());
}
return result;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionExperimentDaoImpl method remove.
@Override
public void remove(final ExpressionExperiment ee) {
if (ee == null)
throw new IllegalArgumentException();
Session session = this.getSessionFactory().getCurrentSession();
try {
// Note that links and analyses are deleted separately - see the ExpressionExperimentService.
// At this point, the ee is probably still in the session, as the service already has gotten it
// in this transaction.
session.flush();
session.clear();
session.buildLockRequest(LockOptions.NONE).lock(ee);
Hibernate.initialize(ee.getAuditTrail());
Set<BioAssayDimension> dims = new HashSet<>();
Set<QuantitationType> qts = new HashSet<>();
Collection<RawExpressionDataVector> designElementDataVectors = ee.getRawExpressionDataVectors();
Hibernate.initialize(designElementDataVectors);
ee.setRawExpressionDataVectors(null);
/*
* We don't remove the investigators, just breaking the association.
*/
ee.getInvestigators().clear();
int count = 0;
if (designElementDataVectors != null) {
count = this.removeDataVectors(session, dims, qts, designElementDataVectors, count);
}
Collection<ProcessedExpressionDataVector> processedVectors = ee.getProcessedExpressionDataVectors();
Hibernate.initialize(processedVectors);
if (processedVectors != null && processedVectors.size() > 0) {
ee.setProcessedExpressionDataVectors(null);
this.removeProcessedVectors(session, dims, qts, count, processedVectors);
}
session.flush();
session.clear();
session.update(ee);
AbstractDao.log.info("Removing BioAssay Dimensions ...");
for (BioAssayDimension dim : dims) {
dim.getBioAssays().clear();
session.update(dim);
session.delete(dim);
}
dims.clear();
session.flush();
AbstractDao.log.info("Removing Bioassays and biomaterials ...");
// keep to put back in the object.
Map<BioAssay, BioMaterial> copyOfRelations = new HashMap<>();
Collection<BioMaterial> bioMaterialsToDelete = new HashSet<>();
Collection<BioAssay> bioAssays = ee.getBioAssays();
this.removeBioAssays(session, copyOfRelations, bioMaterialsToDelete, bioAssays);
AbstractDao.log.info("Last bits ...");
// We remove them here in case they are associated to more than one bioassay-- no cascade is possible.
for (BioMaterial bm : bioMaterialsToDelete) {
session.delete(bm);
}
for (QuantitationType qt : qts) {
session.delete(qt);
}
session.flush();
session.delete(ee);
/*
* Put transient instances back. This is possibly useful for clearing ACLS.
*/
ee.setProcessedExpressionDataVectors(processedVectors);
ee.setRawExpressionDataVectors(designElementDataVectors);
for (BioAssay ba : ee.getBioAssays()) {
ba.setSampleUsed(copyOfRelations.get(ba));
}
AbstractDao.log.info("Deleted " + ee);
} catch (Exception e) {
AbstractDao.log.error(e);
} finally {
AbstractDao.log.info("Finalising remove method.");
}
}
Aggregations