Search in sources :

Example 56 with BioMaterial

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);
    }
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) RawExpressionDataVector(ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector) ArrayDesign(ubic.gemma.model.expression.arrayDesign.ArrayDesign) ExpressionDataDoubleMatrix(ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) ConfigurationException(org.apache.commons.configuration.ConfigurationException) PreprocessingException(ubic.gemma.core.analysis.preprocess.PreprocessingException) IOException(java.io.IOException)

Example 57 with BioMaterial

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);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) LocalFile(ubic.gemma.model.common.description.LocalFile) ArrayDesign(ubic.gemma.model.expression.arrayDesign.ArrayDesign)

Example 58 with BioMaterial

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;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) Treatment(ubic.gemma.model.expression.biomaterial.Treatment) Characteristic(ubic.gemma.model.common.description.Characteristic)

Example 59 with BioMaterial

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;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) JSONInteger(com.sdicons.json.model.JSONInteger) JSONString(com.sdicons.json.model.JSONString) IOException(java.io.IOException) JSONValue(com.sdicons.json.model.JSONValue) ANTLRException(antlr.ANTLRException) StringInputStream(org.apache.tools.ant.filters.StringInputStream) Entry(java.util.Map.Entry) JSONParser(com.sdicons.json.parser.JSONParser) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) JSONString(com.sdicons.json.model.JSONString)

Example 60 with BioMaterial

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;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ArrayList(java.util.ArrayList) EntityNotFoundException(ubic.gemma.web.util.EntityNotFoundException) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Aggregations

BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)132 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)67 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)27 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)22 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)19 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)15 HashSet (java.util.HashSet)13 Test (org.junit.Test)13 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)12 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)12 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)10 InputStream (java.io.InputStream)7 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)7 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)7 QuantitationType (ubic.gemma.model.common.quantitationtype.QuantitationType)7 Characteristic (ubic.gemma.model.common.description.Characteristic)6 RawExpressionDataVector (ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)5 ArrayList (java.util.ArrayList)5