use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class DataUpdater method log2cpmFromCounts.
/**
* For back filling log2cpm when only counts are available. This wouldn't be used routinely, because new experiments
* get log2cpm computed when loaded.
*
* @param ee ee
* @param qt qt
*/
public void log2cpmFromCounts(ExpressionExperiment ee, QuantitationType qt) {
ee = experimentService.thawLite(ee);
/*
* Get the count data; Make sure it is currently preferred (so we don't do this twice by accident)
* We need to do this from the Raw data, not the data that has been normalized etc.
*/
Collection<RawExpressionDataVector> counts = rawExpressionDataVectorService.find(qt);
ExpressionDataDoubleMatrix countMatrix = new ExpressionDataDoubleMatrix(counts);
try {
/*
* Get the count data quantitation type and make it non-preferred
*/
qt.setIsPreferred(false);
qtService.update(qt);
// so updated QT is attached.
ee = experimentService.thawLite(ee);
QuantitationType log2cpmQt = this.makelog2cpmQt();
DoubleMatrix1D librarySize = MatrixStats.colSums(countMatrix.getMatrix());
DoubleMatrix<CompositeSequence, BioMaterial> log2cpmMatrix = MatrixStats.convertToLog2Cpm(countMatrix.getMatrix(), librarySize);
ExpressionDataDoubleMatrix log2cpmEEMatrix = new ExpressionDataDoubleMatrix(ee, log2cpmQt, log2cpmMatrix);
assert log2cpmEEMatrix.getQuantitationTypes().iterator().next().getIsPreferred();
Collection<ArrayDesign> platforms = experimentService.getArrayDesignsUsed(ee);
if (platforms.size() > 1)
throw new IllegalArgumentException("Cannot apply to multiplatform data sets");
this.addData(ee, platforms.iterator().next(), log2cpmEEMatrix);
} catch (Exception e) {
DataUpdater.log.error(e, e);
// try to recover.
qt.setIsPreferred(true);
qtService.update(qt);
}
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionPersister method fillInBioAssayAssociations.
private void fillInBioAssayAssociations(BioAssay bioAssay, ArrayDesignsForExperimentCache c) {
ArrayDesign arrayDesign = bioAssay.getArrayDesignUsed();
ArrayDesign arrayDesignUsed;
if (!this.isTransient(arrayDesign)) {
arrayDesignUsed = arrayDesign;
} else if (c == null || !c.getArrayDesignCache().containsKey(arrayDesign.getShortName())) {
throw new UnsupportedOperationException("You must provide the persistent platforms in a cache object");
} else {
arrayDesignUsed = c.getArrayDesignCache().get(arrayDesign.getShortName());
if (arrayDesignUsed == null || arrayDesignUsed.getId() == null) {
throw new IllegalStateException("You must provide the platform in the cache object");
}
arrayDesignUsed = (ArrayDesign) this.getSessionFactory().getCurrentSession().load(ArrayDesign.class, arrayDesignUsed.getId());
if (arrayDesignUsed == null) {
throw new IllegalStateException("No platform matching " + arrayDesign.getShortName());
}
AbstractPersister.log.debug("Setting platform used for bioassay to " + arrayDesignUsed.getId());
}
assert !this.isTransient(arrayDesignUsed);
bioAssay.setArrayDesignUsed(arrayDesignUsed);
boolean hadFactors = false;
BioMaterial material = bioAssay.getSampleUsed();
for (FactorValue factorValue : material.getFactorValues()) {
// Factors are not compositioned in any more, but by association with the ExperimentalFactor.
this.fillInFactorValueAssociations(factorValue);
this.persistFactorValue(factorValue);
hadFactors = true;
}
if (hadFactors)
AbstractPersister.log.debug("factor values done");
// DatabaseEntries are persisted by composition, so we just need to fill in the ExternalDatabase.
if (bioAssay.getAccession() != null) {
bioAssay.getAccession().setExternalDatabase(this.persistExternalDatabase(bioAssay.getAccession().getExternalDatabase()));
// IN CASE we are retrying.
bioAssay.getAccession().setId(null);
AbstractPersister.log.debug("external database done");
}
// BioMaterials
bioAssay.setSampleUsed((BioMaterial) this.persist(bioAssay.getSampleUsed()));
AbstractPersister.log.debug("biomaterials done");
LocalFile rawDataFile = bioAssay.getRawDataFile();
if (rawDataFile != null) {
if (this.isTransient(rawDataFile)) {
// in case of retry.
rawDataFile.setId(null);
// raw file is unique for bioassay.
bioAssay.setRawDataFile(this.persistLocalFile(rawDataFile, true));
} else {
// re-sync.
this.localFileDao.update(rawDataFile);
}
AbstractPersister.log.debug("raw data file done");
}
for (LocalFile file : bioAssay.getDerivedDataFiles()) {
if (this.isTransient(file))
// in case of retry
file.setId(null);
this.persistLocalFile(file);
}
if (this.isTransient(bioAssay.getAuditTrail()) && bioAssay.getAuditTrail() != null)
// in case of retry;
bioAssay.getAuditTrail().setId(null);
AbstractPersister.log.debug("Done with " + bioAssay);
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionPersister method persistBioMaterial.
private BioMaterial persistBioMaterial(BioMaterial entity) {
if (entity == null)
return null;
AbstractPersister.log.debug("Persisting " + entity);
if (!this.isTransient(entity))
return entity;
assert entity.getSourceTaxon() != null;
AbstractPersister.log.debug("Persisting " + entity);
this.fillInDatabaseEntry(entity.getExternalAccession());
AbstractPersister.log.debug("db entry done");
entity.setSourceTaxon(this.persistTaxon(entity.getSourceTaxon()));
AbstractPersister.log.debug("taxon done");
for (Treatment treatment : entity.getTreatments()) {
Characteristic action = treatment.getAction();
AbstractPersister.log.debug(treatment + " action: " + action);
AbstractPersister.log.debug("treatment done");
}
AbstractPersister.log.debug("start save");
BioMaterial bm = bioMaterialDao.findOrCreate(entity);
AbstractPersister.log.debug("save biomaterial done");
return bm;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionExperimentFormController method updateBioMaterialMap.
/**
* Change the relationship between bioassays and biomaterials.
*
* @param request request
* @param expressionExperiment ee
* @return true if there were changes
*/
private boolean updateBioMaterialMap(HttpServletRequest request, ExpressionExperiment expressionExperiment) {
// parse JSON-serialized map
String jsonSerialization = request.getParameter("assayToMaterialMap");
// convert back to a map
Map<String, JSONValue> bioAssayMap;
try (StringInputStream aStream = new StringInputStream(jsonSerialization)) {
JSONParser parser = new JSONParser(aStream);
bioAssayMap = ((JSONObject) parser.nextValue()).getValue();
} catch (IOException | ANTLRException e) {
throw new RuntimeException(e);
}
Map<BioAssay, BioMaterial> deleteAssociations = new HashMap<>();
Set<Entry<String, JSONValue>> bioAssays = bioAssayMap.entrySet();
boolean anyChanges = false;
int newBioMaterialCount = 0;
for (Entry<String, JSONValue> entry : bioAssays) {
// if it is, skip over this entry
if (entry.getKey().equalsIgnoreCase("nullElement")) {
continue;
}
Long bioAssayId = Long.parseLong(entry.getKey());
JSONValue value = entry.getValue();
Long newMaterialId;
if (value.isString()) {
newMaterialId = Long.parseLong(((JSONString) value).getValue());
} else {
newMaterialId = ((JSONInteger) value).getValue().longValue();
}
// maybe we need to do
BioAssay bioAssay = bioAssayService.load(bioAssayId);
if (bioAssay == null) {
throw new IllegalArgumentException("Bioassay with id=" + bioAssayId + " was not associated with the experiment");
}
BioMaterial currentBioMaterial = bioAssay.getSampleUsed();
if (newMaterialId.equals(currentBioMaterial.getId())) {
// / no change
continue;
}
BioMaterial newMaterial;
if (newMaterialId < 0) {
// This kludge signifies that it is a 'brand new' biomaterial.
newMaterial = bioMaterialService.copy(currentBioMaterial);
newMaterial.setName("Modeled after " + currentBioMaterial.getName());
newMaterial.getFactorValues().clear();
newMaterial = (BioMaterial) persisterHelper.persist(newMaterial);
newBioMaterialCount++;
} else {
// FIXME can we just use this from the experiment, probably no need to fetch it again.
newMaterial = bioMaterialService.load(newMaterialId);
if (newMaterial == null) {
throw new IllegalArgumentException("BioMaterial with id=" + newMaterialId + " could not be loaded");
}
}
anyChanges = true;
BaseFormController.log.info("Associating " + bioAssay + " with " + newMaterial);
bioAssayService.addBioMaterialAssociation(bioAssay, newMaterial);
}
if (anyChanges) {
/*
* FIXME Decide if we need to remove the biomaterial -> factor value associations, it could be completely
* fouled up.
*/
BaseFormController.log.info("There were changes to the BioMaterial -> BioAssay map");
this.audit(expressionExperiment, BioMaterialMappingUpdate.Factory.newInstance(), // remove unnecessary biomaterial associations
newBioMaterialCount + " biomaterials");
Collection<BioAssay> deleteKeys = deleteAssociations.keySet();
for (BioAssay assay : deleteKeys) {
/*
* BUG: if this fails, we end up with a useless extra biomaterial associated with the bioassay.
*/
bioAssayService.removeBioMaterialAssociation(assay, deleteAssociations.get(assay));
}
} else {
BaseFormController.log.info("There were no changes to the BioMaterial -> BioAssay map");
}
return anyChanges;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class BioMaterialController method getBioMaterialsForEE.
/**
* @param id of experiment
*/
public Collection<BioMaterial> getBioMaterialsForEE(Long id) {
ExpressionExperiment expressionExperiment = expressionExperimentService.load(id);
if (expressionExperiment == null) {
throw new EntityNotFoundException("Expression experiment with id=" + id + " not found");
}
expressionExperiment = expressionExperimentService.thawLite(expressionExperiment);
Collection<BioAssay> bioAssays = expressionExperiment.getBioAssays();
Collection<BioMaterial> bioMaterials = new ArrayList<>();
for (BioAssay assay : bioAssays) {
BioMaterial material = assay.getSampleUsed();
if (material != null) {
bioMaterials.add(material);
}
}
return bioMaterials;
}
Aggregations