Search in sources :

Example 21 with DoubleVectorValueObject

use of ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject 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 22 with DoubleVectorValueObject

use of ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject in project Gemma by PavlidisLab.

the class DEDVController method getDEDVForPcaVisualization.

/**
 * AJAX
 */
public VisualizationValueObject[] getDEDVForPcaVisualization(Long eeId, int component, int count) {
    StopWatch watch = new StopWatch();
    watch.start();
    Map<ProbeLoading, DoubleVectorValueObject> topLoadedVectors = this.svdService.getTopLoadedVectors(eeId, component, count);
    if (topLoadedVectors == null)
        return null;
    Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts;
    Collection<DoubleVectorValueObject> values = topLoadedVectors.values();
    if (values.isEmpty()) {
        return null;
    }
    layouts = experimentalDesignVisualizationService.sortVectorDataByDesign(values);
    return makeVisCollection(values, null, null, layouts);
}
Also used : ProbeLoading(ubic.gemma.model.analysis.expression.pca.ProbeLoading) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 23 with DoubleVectorValueObject

use of ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject in project Gemma by PavlidisLab.

the class DEDVController method format4File.

/**
 * Converts the given map into a tab delimited String
 */
private String format4File(Map<ExpressionExperimentValueObject, Map<Long, Collection<DoubleVectorValueObject>>> result) {
    StringBuilder converted = new StringBuilder();
    // Saves us from loading genes
    Map<Long, GeneValueObject> genes = new HashMap<>();
    // unnecessarily
    converted.append("# Generated by Gemma\n# ").append(new Date()).append("\n");
    converted.append(ExpressionDataFileService.DISCLAIMER + "#\n");
    for (ExpressionExperimentValueObject ee : result.keySet()) {
        boolean didHeaderForEe = false;
        Collection<Long> geneIds = result.get(ee).keySet();
        for (Long geneId : geneIds) {
            GeneValueObject gene;
            if (genes.containsKey(geneId)) {
                gene = genes.get(geneId);
            } else {
                gene = geneService.loadValueObjectById(geneId);
                genes.put(geneId, gene);
            }
            String geneName = gene.getOfficialSymbol();
            Collection<DoubleVectorValueObject> vecs = result.get(ee).get(geneId);
            for (DoubleVectorValueObject dedv : vecs) {
                if (!didHeaderForEe) {
                    converted.append(makeHeader(dedv));
                    didHeaderForEe = true;
                }
                converted.append(geneName).append("\t").append(gene.getOfficialName()).append("\t");
                converted.append(dedv.getDesignElement().getName()).append("\t");
                if (dedv.getData() != null || dedv.getData().length != 0) {
                    for (double data : dedv.getData()) {
                        converted.append(String.format("%.3f", data)).append("\t");
                    }
                    // remove the trailing tab
                    converted.deleteCharAt(converted.length() - 1);
                }
                converted.append("\n");
            }
        }
        converted.append("\n");
    }
    converted.append("\r\n");
    return converted.toString();
}
Also used : GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 24 with DoubleVectorValueObject

use of ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject in project Gemma by PavlidisLab.

the class DEDVController method makeVectorMap.

private Map<ExpressionExperimentValueObject, Map<Long, Collection<DoubleVectorValueObject>>> makeVectorMap(Collection<DoubleVectorValueObject> newResults, Map<Long, LinkedHashMap<BioAssay, LinkedHashMap<ExperimentalFactor, Double>>> layouts) {
    // FIXME use the layouts.
    Map<ExpressionExperimentValueObject, Map<Long, Collection<DoubleVectorValueObject>>> result = new HashMap<>();
    for (DoubleVectorValueObject v : newResults) {
        ExpressionExperimentValueObject e = v.getExpressionExperiment();
        if (!result.containsKey(e)) {
            result.put(e, new HashMap<Long, Collection<DoubleVectorValueObject>>());
        }
        Map<Long, Collection<DoubleVectorValueObject>> innerMap = result.get(e);
        if (v.getGenes() == null || v.getGenes().isEmpty()) {
            continue;
        }
        for (Long g : v.getGenes()) {
            if (!innerMap.containsKey(g)) {
                innerMap.put(g, new HashSet<DoubleVectorValueObject>());
            }
            innerMap.get(g).add(v);
        }
    }
    return result;
}
Also used : DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 25 with DoubleVectorValueObject

use of ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject in project Gemma by PavlidisLab.

the class DEDVController method getDEDVForCoexpressionVisualization.

/**
 * AJAX exposed method
 */
public VisualizationValueObject[] getDEDVForCoexpressionVisualization(Collection<Long> eeIds, Long queryGeneId, Long coexpressedGeneId) {
    StopWatch watch = new StopWatch();
    watch.start();
    Collection<ExpressionExperiment> ees = expressionExperimentService.load(eeIds);
    if (ees == null || ees.isEmpty())
        return new VisualizationValueObject[0];
    Gene queryGene = geneService.load(queryGeneId);
    Gene coexpressedGene = geneService.load(coexpressedGeneId);
    List<Long> genes = new ArrayList<>();
    genes.add(queryGeneId);
    genes.add(coexpressedGeneId);
    if (genes.isEmpty())
        return new VisualizationValueObject[0];
    Collection<DoubleVectorValueObject> dedvs = processedExpressionDataVectorService.getProcessedDataArrays(ees, genes);
    Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts;
    layouts = experimentalDesignVisualizationService.sortVectorDataByDesign(dedvs);
    // layouts = experimentalDesignVisualizationService.sortLayoutSamplesByFactor( layouts );
    watch.stop();
    Long time = watch.getTime();
    if (dedvs.size() == 0) {
        log.warn("No expression profiles (DEDVs) were available for the experiments:  " + eeIds + " and genes(s) " + queryGene.getOfficialSymbol() + ", " + coexpressedGene.getOfficialSymbol());
        return new VisualizationValueObject[0];
    }
    if (time > 1000) {
        log.info("Retrieved " + dedvs.size() + " DEDVs for " + eeIds.size() + " EEs and " + genes.size() + " genes in " + time + " ms.");
    }
    Map<Long, Collection<Long>> validatedProbes = getProbeLinkValidation(ees, queryGene, coexpressedGene, dedvs);
    return makeVisCollection(dedvs, genes, validatedProbes, layouts);
}
Also used : VisualizationValueObject(ubic.gemma.web.controller.visualization.VisualizationValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch) Gene(ubic.gemma.model.genome.Gene) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Aggregations

DoubleVectorValueObject (ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)31 StopWatch (org.apache.commons.lang3.time.StopWatch)13 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)9 Test (org.junit.Test)6 AbstractGeoServiceTest (ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest)6 BioAssayValueObject (ubic.gemma.model.expression.bioAssay.BioAssayValueObject)6 AlreadyExistsInSystemException (ubic.gemma.core.loader.util.AlreadyExistsInSystemException)5 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)5 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)5 Gene (ubic.gemma.model.genome.Gene)5 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)4 ExperimentExpressionLevelsValueObject (ubic.gemma.model.expression.bioAssayData.ExperimentExpressionLevelsValueObject)4 GeneValueObject (ubic.gemma.model.genome.gene.GeneValueObject)4 InputStream (java.io.InputStream)3 Transactional (org.springframework.transaction.annotation.Transactional)3 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)3 GeoDomainObjectGenerator (ubic.gemma.core.loader.expression.geo.GeoDomainObjectGenerator)3 GeoDomainObjectGeneratorLocal (ubic.gemma.core.loader.expression.geo.GeoDomainObjectGeneratorLocal)3 ProbeLoading (ubic.gemma.model.analysis.expression.pca.ProbeLoading)3 VisualizationValueObject (ubic.gemma.web.controller.visualization.VisualizationValueObject)3