Search in sources :

Example 11 with BioAssayValueObject

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

the class ProcessedExpressionDataVectorDaoImpl method sliceSubSet.

/**
 * @param ee  ee
 * @param obs obs
 * @return Given an ExpressionExperimentSubset and vectors from the source experiment, give vectors that include just the
 * data for the subset.
 */
private Collection<DoubleVectorValueObject> sliceSubSet(ExpressionExperimentSubSet ee, Collection<DoubleVectorValueObject> obs) {
    Collection<DoubleVectorValueObject> sliced = new HashSet<>();
    if (obs == null || obs.isEmpty())
        return sliced;
    this.getSessionFactory().getCurrentSession().buildLockRequest(LockOptions.NONE).lock(ee);
    Hibernate.initialize(ee.getBioAssays());
    List<BioAssayValueObject> sliceBioAssays = new ArrayList<>();
    DoubleVectorValueObject exemplar = obs.iterator().next();
    BioAssayDimensionValueObject bad = new BioAssayDimensionValueObject(-1L);
    bad.setName("Subset of :" + exemplar.getBioAssayDimension().getName());
    bad.setDescription("Subset slice");
    bad.setSourceBioAssayDimension(exemplar.getBioAssayDimension());
    bad.setIsSubset(true);
    Collection<Long> subsetBioAssayIds = EntityUtils.getIds(ee.getBioAssays());
    for (BioAssayValueObject ba : exemplar.getBioAssays()) {
        if (!subsetBioAssayIds.contains(ba.getId())) {
            continue;
        }
        sliceBioAssays.add(ba);
    }
    bad.addBioAssays(sliceBioAssays);
    for (DoubleVectorValueObject vec : obs) {
        DoubleVectorValueObject s = new DoubleVectorValueObject(ee, vec, bad);
        sliced.add(s);
    }
    return sliced;
}
Also used : BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject)

Example 12 with BioAssayValueObject

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

the class ExperimentalDesignVisualizationServiceImpl method sortVectorDataByDesign.

@Override
public Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> sortVectorDataByDesign(Collection<DoubleVectorValueObject> dedVs) {
    if (dedVs == null) {
        return new HashMap<>(0);
    }
    Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> returnedLayouts = new HashMap<>(dedVs.size());
    StopWatch timer = new StopWatch();
    timer.start();
    /*
         * This is shared across experiments that might show up in the dedVs; this should be okay...saves computation.
         * This is the only slow part.
         */
    this.prepare(dedVs);
    /*
         * This loop is not a performance issue.
         */
    Map<DoubleVectorValueObject, List<BioAssayValueObject>> newOrderingsForBioAssayDimensions = new HashMap<>();
    for (DoubleVectorValueObject vec : dedVs) {
        if (vec.isReorganized()) {
            continue;
        }
        assert !vec.getBioAssays().isEmpty();
        LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> layout = null;
        if (cachedLayouts.containsKey(vec.getExpressionExperiment().getId())) {
            layout = cachedLayouts.get(vec.getExpressionExperiment().getId());
        } else if (vec.getExpressionExperiment().getClass().isInstance(ExpressionExperimentSubsetValueObject.class)) {
            // subset.
            layout = cachedLayouts.get(((ExpressionExperimentSubsetValueObject) vec.getExpressionExperiment()).getSourceExperiment());
        }
        if (layout == null || layout.isEmpty()) {
            log.error("Did not find cached layout for " + vec.getId());
            continue;
        }
        List<BioAssayValueObject> newOrdering = new ArrayList<>(layout.keySet());
        newOrdering.retainAll(vec.getBioAssays());
        /*
             * This can happen if the vectors are out of whack with the bioassays - e.g. two platforms were used but
             * merging is not done. See bug 3775. Skipping the ordering is not the right thing to do.
             */
        if (newOrdering.isEmpty()) {
            boolean allNaN = this.allNaN(vec);
            if (allNaN) {
                // reordering will have no effect.
                continue;
            }
            /*
                 * Add to the layout.
                 */
            layout = this.extendLayout(vec, vec.getExpressionExperiment().getId());
            newOrdering = new ArrayList<>(layout.keySet());
            newOrdering.retainAll(vec.getBioAssays());
            assert !newOrdering.isEmpty();
        }
        newOrderingsForBioAssayDimensions.put(vec, newOrdering);
        Map<BioAssayValueObject, Integer> ordering = this.getOrdering(newOrdering);
        Long eeId;
        // might be subset id.
        eeId = vec.getExpressionExperiment().getId();
        if (!returnedLayouts.containsKey(eeId)) {
            if (vec.isSliced()) {
                LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> trimmedLayout = new LinkedHashMap<>();
                for (BioAssayValueObject baVo : newOrdering) {
                    trimmedLayout.put(baVo, layout.get(baVo));
                }
                returnedLayouts.put(eeId, trimmedLayout);
            } else {
                returnedLayouts.put(eeId, layout);
            }
        }
        /*
             * Might be a faster way.
             */
        double[] data = vec.getData();
        double[] dol = ArrayUtils.clone(data);
        // assert ordering.size() == data.length : "got " + ordering.size() + " expected " + data.length;
        List<BioAssayValueObject> oldOrdering = vec.getBioAssayDimension().getBioAssays();
        int j = 0;
        if (log.isTraceEnabled())
            log.trace("Old order: " + StringUtils.join(ArrayUtils.toObject(data), ","));
        for (BioAssayValueObject ba : oldOrdering) {
            if (ordering.get(ba) == null) {
                assert Double.isNaN(dol[j]);
                j++;
                continue;
            }
            assert ordering.containsKey(ba);
            assert ordering.get(ba) != null;
            Integer targetIndex = ordering.get(ba);
            data[targetIndex] = dol[j++];
        }
        if (log.isTraceEnabled())
            log.trace("New order: " + StringUtils.join(ArrayUtils.toObject(data), ","));
        vec.setReorganized(true);
    }
    for (DoubleVectorValueObject vec : dedVs) {
        if (vec.getBioAssayDimension().isReordered())
            continue;
        List<BioAssayValueObject> newOrdering = newOrderingsForBioAssayDimensions.get(vec);
        if (newOrdering == null)
            // data was empty, etc.
            continue;
        vec.getBioAssayDimension().reorder(newOrdering);
    }
    if (timer.getTime() > 1500) {
        log.info("Sort vectors by design: " + timer.getTime() + "ms");
    }
    return returnedLayouts;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StopWatch(org.apache.commons.lang3.time.StopWatch) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) List(java.util.List) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 13 with BioAssayValueObject

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

the class ExperimentalDesignVisualizationServiceImpl method plotExperimentalDesign.

/**
 * Test method for now, shows how this can be used.
 *
 * @param e ee
 */
// Test method for now, shows how this can be used.
@SuppressWarnings("unused")
protected void plotExperimentalDesign(ExpressionExperiment e) {
    LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> layout = this.getExperimentalDesignLayout(e);
    List<String> efStrings = new ArrayList<>();
    List<String> baStrings = new ArrayList<>();
    List<double[]> rows = new ArrayList<>();
    boolean first = true;
    int i = 0;
    for (BioAssayValueObject ba : layout.keySet()) {
        baStrings.add(ba.getName());
        int j = 0;
        for (ExperimentalFactor ef : layout.get(ba).keySet()) {
            if (first) {
                double[] nextRow = new double[layout.size()];
                rows.add(nextRow);
                // make sure they are unique.
                efStrings.add(ef.getName() + " ( id=" + ef.getId() + ")");
            }
            double d = layout.get(ba).get(ef);
            rows.get(j)[i] = d;
            j++;
        }
        i++;
        first = false;
    }
    double[][] mat = rows.toArray(new double[][] {});
    DoubleMatrix<String, String> data = DoubleMatrixFactory.dense(mat);
    data.setRowNames(efStrings);
    data.setColumnNames(baStrings);
    ColorMatrix<String, String> cm = new ColorMatrix<>(data, ColorMap.GREENRED_COLORMAP, Color.GRAY);
    try {
        this.writeImage(cm, File.createTempFile(e.getShortName() + "_", ".png"));
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }
}
Also used : ColorMatrix(ubic.basecode.graphics.ColorMatrix) IOException(java.io.IOException) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject)

Example 14 with BioAssayValueObject

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

the class ExperimentalDesignVisualizationServiceImpl method getOrdering.

/**
 * Get the order that bioassays need to be in for the 'real' data.
 */
private Map<BioAssayValueObject, Integer> getOrdering(List<BioAssayValueObject> bioAssaysInOrder) {
    assert !bioAssaysInOrder.isEmpty();
    Map<BioAssayValueObject, Integer> ordering = new HashMap<>();
    int i = 0;
    for (BioAssayValueObject bbb : bioAssaysInOrder) {
        ordering.put(bbb, i++);
    }
    return ordering;
}
Also used : BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 15 with BioAssayValueObject

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

the class BioAssayOutlierProcessingTaskImpl method execute.

@Override
public TaskResult execute() {
    Collection<BioAssay> bioAssays = bioAssayService.load(taskCommand.getBioAssayIds());
    if (bioAssays.isEmpty()) {
        throw new RuntimeException("Could not locate the bioassays");
    }
    if (taskCommand.isRevert()) {
        sampleRemoveService.unmarkAsMissing(bioAssays);
    } else {
        sampleRemoveService.markAsMissing(bioAssays);
    }
    bioAssays = bioAssayService.thaw(bioAssays);
    Collection<BioAssayValueObject> flagged = new HashSet<>();
    for (BioAssay ba : bioAssays) {
        flagged.add(new BioAssayValueObject(ba, false));
    }
    return new TaskResult(taskCommand, flagged);
}
Also used : BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) TaskResult(ubic.gemma.core.job.TaskResult) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) HashSet(java.util.HashSet)

Aggregations

BioAssayValueObject (ubic.gemma.model.expression.bioAssay.BioAssayValueObject)20 DoubleVectorValueObject (ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)7 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)6 BioMaterialValueObject (ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 StopWatch (org.apache.commons.lang3.time.StopWatch)3 ArrayDesignValueObject (ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject)3 BioAssayDimensionValueObject (ubic.gemma.model.expression.bioAssayData.BioAssayDimensionValueObject)3 HashSet (java.util.HashSet)2 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)2 GeneValueObject (ubic.gemma.model.genome.gene.GeneValueObject)2 VisualizationValueObject (ubic.gemma.web.controller.visualization.VisualizationValueObject)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 MapperException (com.sdicons.json.mapper.MapperException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JspException (javax.servlet.jsp.JspException)1 Test (org.junit.Test)1