Search in sources :

Example 21 with BioAssay

use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.

the class VectorMergingServiceImpl method combineBioAssayDimensions.

/**
 * Create a new one or use an existing one. (an existing one might be found if this process was started once before
 * and aborted partway through).
 *
 * @param oldDims in the sort order to be used.
 * @return BA dim
 */
private BioAssayDimension combineBioAssayDimensions(List<BioAssayDimension> oldDims) {
    List<BioAssay> bioAssays = new ArrayList<>();
    for (BioAssayDimension bioAd : oldDims) {
        for (BioAssay bioAssay : bioAd.getBioAssays()) {
            if (bioAssays.contains(bioAssay)) {
                throw new IllegalStateException("Duplicate bioassay for biodimension: " + bioAssay + "; inspecting " + oldDims.size() + " BioAssayDimensions");
            }
            bioAssays.add(bioAssay);
        }
    }
    // first see if we already have an equivalent one.
    boolean found = true;
    for (BioAssayDimension newDim : oldDims) {
        // size should be the same.
        List<BioAssay> assaysInExisting = newDim.getBioAssays();
        if (assaysInExisting.size() != bioAssays.size()) {
            continue;
        }
        for (int i = 0; i < bioAssays.size(); i++) {
            if (!assaysInExisting.get(i).equals(bioAssays.get(i))) {
                found = false;
                break;
            }
        }
        if (!found)
            continue;
        VectorMergingServiceImpl.log.info("Already have a dimension created that fits the bill - removing it from the 'old' list.");
        oldDims.remove(newDim);
        return newDim;
    }
    BioAssayDimension newBioAd = BioAssayDimension.Factory.newInstance();
    newBioAd.setName("");
    newBioAd.setDescription(VectorMergingServiceImpl.MERGED_DIM_DESC_PREFIX + " " + oldDims.size() + " dimensions: ");
    for (BioAssayDimension bioAd : oldDims) {
        newBioAd.setName(newBioAd.getName() + bioAd.getName() + " ");
        newBioAd.setDescription(newBioAd.getDescription() + bioAd.getName() + " ");
    }
    newBioAd.setName(StringUtils.abbreviate(newBioAd.getName(), 255));
    newBioAd.setBioAssays(bioAssays);
    newBioAd = bioAssayDimensionService.create(newBioAd);
    VectorMergingServiceImpl.log.info("Created new bioAssayDimension with " + newBioAd.getBioAssays().size() + " bioassays.");
    return newBioAd;
}
Also used : BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 22 with BioAssay

use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.

the class OutlierFlaggingServiceImpl method markAsMissing.

@Override
public void markAsMissing(Collection<BioAssay> bioAssays) {
    if (bioAssays == null || bioAssays.size() == 0)
        return;
    boolean hasNewOutliers = false;
    /*
         * FIXME: if there are two (or more) platforms, make sure we flag all bioassays that use the same biomaterial.
         * However, we are intending to turn all multiplatform datasets into single platform ones
         */
    for (BioAssay ba : bioAssays) {
        if (ba.getIsOutlier()) {
            continue;
        }
        hasNewOutliers = true;
        ba.setIsOutlier(true);
        bioAssayService.update(ba);
        this.audit(ba, "Sample " + ba.getName() + " marked as missing data.");
    }
    if (!hasNewOutliers) {
        System.out.println("No new outliers.");
        return;
    }
    ExpressionExperiment expExp = expressionExperimentService.findByBioAssay(bioAssays.iterator().next());
    auditTrailService.addUpdateEvent(expExp, SampleRemovalEvent.Factory.newInstance(), bioAssays.size() + " flagged as outliers", StringUtils.join(bioAssays, ","));
    try {
        preprocessorService.process(expExp);
    } catch (PreprocessingException e) {
        OutlierFlaggingServiceImpl.log.error("Error during postprocessing, make sure additional steps are completed", e);
    }
}
Also used : PreprocessingException(ubic.gemma.core.analysis.preprocess.PreprocessingException) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 23 with BioAssay

use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.

the class ProcessedExpressionDataVectorCreateHelperServiceImpl method reorderByDesign.

@Override
@Transactional
public void reorderByDesign(Long eeId) {
    ExpressionExperiment ee = expressionExperimentDao.load(eeId);
    if (ee.getExperimentalDesign().getExperimentalFactors().size() == 0) {
        ProcessedExpressionDataVectorCreateHelperServiceImpl.log.info(ee.getShortName() + " does not have a populated experimental design, skipping");
        return;
    }
    Collection<ProcessedExpressionDataVector> processedDataVectors = ee.getProcessedExpressionDataVectors();
    if (processedDataVectors.size() == 0) {
        ProcessedExpressionDataVectorCreateHelperServiceImpl.log.info(ee.getShortName() + " does not have processed data");
        return;
    }
    Collection<BioAssayDimension> dims = this.eeService.getBioAssayDimensions(ee);
    if (dims.size() > 1) {
        this.checkAllBioAssayDimensionsMatch(dims);
    }
    BioAssayDimension bioassaydim = dims.iterator().next();
    List<BioMaterial> start = new ArrayList<>();
    for (BioAssay ba : bioassaydim.getBioAssays()) {
        start.add(ba.getSampleUsed());
    }
    /*
         * Get the ordering we want.
         */
    List<BioMaterial> orderByExperimentalDesign = ExpressionDataMatrixColumnSort.orderByExperimentalDesign(start, ee.getExperimentalDesign().getExperimentalFactors());
    /*
         * Map of biomaterials to the new order index.
         */
    final Map<BioMaterial, Integer> ordering = new HashMap<>();
    int i = 0;
    for (BioMaterial bioMaterial : orderByExperimentalDesign) {
        ordering.put(bioMaterial, i);
        i++;
    }
    /*
         * Map of the original order to new order of bioassays.
         */
    Map<Integer, Integer> indexes = new HashMap<>();
    Map<BioAssayDimension, BioAssayDimension> old2new = new HashMap<>();
    for (BioAssayDimension bioAssayDimension : dims) {
        Collection<BioAssay> bioAssays = bioAssayDimension.getBioAssays();
        assert bioAssays != null;
        /*
             * Initialize the new bioassay list.
             */
        List<BioAssay> resorted = new ArrayList<>(bioAssays.size());
        for (int m = 0; m < bioAssays.size(); m++) {
            resorted.add(null);
        }
        for (int oldIndex = 0; oldIndex < bioAssays.size(); oldIndex++) {
            BioAssay bioAssay = ((List<BioAssay>) bioAssays).get(oldIndex);
            BioMaterial sam1 = bioAssay.getSampleUsed();
            if (ordering.containsKey(sam1)) {
                Integer newIndex = ordering.get(sam1);
                resorted.set(newIndex, bioAssay);
                /*
                     * Should be the same for all dimensions....
                     */
                assert !indexes.containsKey(oldIndex) || indexes.get(oldIndex).equals(newIndex);
                indexes.put(oldIndex, newIndex);
            } else {
                throw new IllegalStateException();
            }
        }
        BioAssayDimension newBioAssayDimension = BioAssayDimension.Factory.newInstance();
        newBioAssayDimension.setBioAssays(resorted);
        newBioAssayDimension.setName("Processed data of ee " + ee.getShortName() + " ordered by design");
        newBioAssayDimension.setDescription("Data was reordered based on the experimental design.");
        newBioAssayDimension = bioAssayDimensionService.create(newBioAssayDimension);
        old2new.put(bioAssayDimension, newBioAssayDimension);
    }
    ByteArrayConverter converter = new ByteArrayConverter();
    for (ProcessedExpressionDataVector v : processedDataVectors) {
        BioAssayDimension revisedBioAssayDimension = old2new.get(v.getBioAssayDimension());
        assert revisedBioAssayDimension != null;
        double[] data = converter.byteArrayToDoubles(v.getData());
        /*
             * Put the data in the order of the bioAssayDimension.
             */
        Double[] resortedData = new Double[data.length];
        for (int k = 0; k < data.length; k++) {
            resortedData[k] = data[indexes.get(k)];
        }
        v.setData(converter.toBytes(resortedData));
        v.setBioAssayDimension(revisedBioAssayDimension);
    }
    ProcessedExpressionDataVectorCreateHelperServiceImpl.log.info("Updating bioassay ordering of " + processedDataVectors.size() + " vectors");
    this.auditTrailService.addUpdateEvent(ee, "Reordered the data vectors by experimental design");
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ByteArrayConverter(ubic.basecode.io.ByteArrayConverter) DoubleArrayList(cern.colt.list.DoubleArrayList) ProcessedExpressionDataVector(ubic.gemma.model.expression.bioAssayData.ProcessedExpressionDataVector) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) DoubleArrayList(cern.colt.list.DoubleArrayList) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with BioAssay

use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.

the class ProcessedExpressionDataVectorCreateHelperServiceImpl method checkAllBioAssayDimensionsMatch.

/**
 * Make sure we have only one ordering!!! If the sample matching is botched, there will be problems.
 */
private void checkAllBioAssayDimensionsMatch(Collection<BioAssayDimension> dims) {
    ProcessedExpressionDataVectorCreateHelperServiceImpl.log.info("Data set has more than one bioassaydimension for its processed data vectors");
    List<BioMaterial> ordering = new ArrayList<>();
    int i = 0;
    for (BioAssayDimension dim : dims) {
        int j = 0;
        for (BioAssay ba : dim.getBioAssays()) {
            BioMaterial sample = ba.getSampleUsed();
            if (i == 0) {
                ordering.add(sample);
            } else {
                if (!ordering.get(j).equals(sample)) {
                    throw new IllegalStateException("Two dimensions didn't have the same BioMaterial ordering for the same data set.");
                }
                j++;
            }
        }
        i++;
    }
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) DoubleArrayList(cern.colt.list.DoubleArrayList) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 25 with BioAssay

use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.

the class BatchInfoParser method getAccessionToBioAssayMap.

private Map<String, BioAssay> getAccessionToBioAssayMap(ExpressionExperiment ee) {
    Map<String, BioAssay> assayAccessions = new HashMap<>();
    for (BioAssay ba : ee.getBioAssays()) {
        DatabaseEntry accession = ba.getAccession();
        if (StringUtils.isBlank(accession.getAccession())) {
            throw new IllegalStateException("Must have accession for each bioassay to get batch information from source for " + ee.getShortName());
        }
        assayAccessions.put(accession.getAccession(), ba);
    }
    return assayAccessions;
}
Also used : DatabaseEntry(ubic.gemma.model.common.description.DatabaseEntry) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Aggregations

BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)144 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)67 Test (org.junit.Test)29 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)29 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)24 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)20 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)18 RawExpressionDataVector (ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector)16 AbstractGeoServiceTest (ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest)15 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)14 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)14 InputStream (java.io.InputStream)11 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)10 HashSet (java.util.HashSet)9 AlreadyExistsInSystemException (ubic.gemma.core.loader.util.AlreadyExistsInSystemException)8 DesignElementDataVector (ubic.gemma.model.expression.bioAssayData.DesignElementDataVector)8 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)7