Search in sources :

Example 81 with StopWatch

use of org.apache.commons.lang3.time.StopWatch in project Gemma by PavlidisLab.

the class DEDVController method getDEDVForVisualization.

/**
 * AJAX exposed method
 */
public VisualizationValueObject[] getDEDVForVisualization(Collection<Long> eeIds, Collection<Long> geneIds) {
    StopWatch watch = new StopWatch();
    watch.start();
    Collection<ExpressionExperiment> ees = expressionExperimentService.load(eeIds);
    if (ees == null || ees.isEmpty())
        return null;
    Collection<DoubleVectorValueObject> dedvs;
    if (geneIds == null || geneIds.isEmpty()) {
        dedvs = processedExpressionDataVectorService.getProcessedDataArrays(ees.iterator().next(), SAMPLE_SIZE);
    } else {
        if (geneIds.size() > MAX_RESULTS_TO_RETURN) {
            log.warn(geneIds.size() + " genes for visualization. Too many.  Only using first " + MAX_RESULTS_TO_RETURN + " genes. ");
            List<Long> reducedGeneIds = new ArrayList<>(geneIds);
            geneIds = reducedGeneIds.subList(0, MAX_RESULTS_TO_RETURN);
        }
        dedvs = processedExpressionDataVectorService.getProcessedDataArrays(ees, geneIds);
    }
    if (dedvs.isEmpty()) {
        return null;
    }
    Long time = watch.getTime();
    watch.reset();
    watch.start();
    if (time > 100) {
        log.info("Retrieved " + dedvs.size() + " DEDVs for " + eeIds.size() + " EEs" + (geneIds == null ? " sample" : " for " + geneIds.size() + " genes ") + " in " + time + " ms (times <100ms not reported).");
    }
    Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts = experimentalDesignVisualizationService.sortVectorDataByDesign(dedvs);
    time = watch.getTime();
    watch.reset();
    watch.start();
    if (time > 100) {
        log.info("Ran sortVectorDataByDesign on " + dedvs.size() + " DEDVs for " + eeIds.size() + " EEs" + " in " + time + " ms (times <100ms not reported).");
    }
    watch.stop();
    time = watch.getTime();
    if (time > 100) {
        log.info("Ran sortLayoutSamplesByFactor on " + layouts.size() + " layouts" + " in " + time + " ms (times <100ms not reported).");
    }
    return makeVisCollection(dedvs, geneIds, null, layouts);
}
Also used : DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 82 with StopWatch

use of org.apache.commons.lang3.time.StopWatch in project Gemma by PavlidisLab.

the class DEDVController method makeDiffVisCollection.

/**
 * Takes the DEDVs and put them in point objects and normalize the values. returns a map of eeid to visValueObject.
 * Currently removes multiple hits for same gene. Tries to pick best DEDV. Organizes the experiments from lowest to
 * higest p-value
 *
 * @param validatedProbes (bad name)
 */
private VisualizationValueObject[] makeDiffVisCollection(Collection<DoubleVectorValueObject> dedvs, List<Long> genes, Map<Long, Collection<DifferentialExpressionValueObject>> validatedProbes, Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts) {
    StopWatch watch = new StopWatch();
    watch.start();
    Map<Long, Collection<DoubleVectorValueObject>> vvoMap = new HashMap<>();
    Map<Long, ExpressionExperimentValueObject> eeMap = new HashMap<>();
    // Organize by expression experiment
    for (DoubleVectorValueObject dvvo : dedvs) {
        ExpressionExperimentValueObject ee = dvvo.getExpressionExperiment();
        eeMap.put(ee.getId(), ee);
        if (!vvoMap.containsKey(ee.getId())) {
            vvoMap.put(ee.getId(), new HashSet<DoubleVectorValueObject>());
        }
        vvoMap.get(ee.getId()).add(dvvo);
    }
    class EE2PValue implements Comparable<EE2PValue> {

        Long EEId;

        double pValue;

        public EE2PValue() {
            super();
        }

        public EE2PValue(Long eeid, double pValue) {
            this();
            this.EEId = eeid;
            this.pValue = pValue;
        }

        @Override
        public int compareTo(EE2PValue o) {
            if (this.pValue > o.getPValue())
                return 1;
            else if (this.pValue > o.getPValue())
                return -1;
            else
                return 0;
        }

        public Long getEEId() {
            return EEId;
        }

        public double getPValue() {
            return pValue;
        }
    }
    List<EE2PValue> sortedEE = new ArrayList<>();
    // Need to sort the expression experiments by lowest p-value
    for (Long eeId : vvoMap.keySet()) {
        Collection<DifferentialExpressionValueObject> devos = validatedProbes.get(eeId);
        double minP = 1;
        if (devos != null && !devos.isEmpty()) {
            for (DifferentialExpressionValueObject devo : devos) {
                if (minP > devo.getP()) {
                    minP = devo.getP();
                }
            }
        }
        sortedEE.add(new EE2PValue(eeId, minP));
    }
    Collections.sort(sortedEE);
    VisualizationValueObject[] result = new VisualizationValueObject[vvoMap.keySet().size()];
    List<GeneValueObject> geneValueObjects = getGeneValueObjectList(genes);
    // Create collection of visualizationValueObject for flotr on js side
    int i = 0;
    for (EE2PValue ee2P : sortedEE) {
        VisualizationValueObject vvo = new VisualizationValueObject(vvoMap.get(ee2P.getEEId()), geneValueObjects, ee2P.getPValue(), validatedProbes.get(ee2P.getEEId()));
        getSampleNames(vvoMap.get(ee2P.getEEId()), vvo, layouts);
        if (layouts != null && !layouts.isEmpty() && layouts.containsKey(ee2P.getEEId())) {
            LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> layout = layouts.get(ee2P.getEEId());
            this.prepareFactorsForFrontEndDisplay(vvo, layout);
        }
        if (layouts != null) {
            ExpressionExperimentValueObject ee = eeMap.get(ee2P.getEEId());
            log.debug("setup experimental design layout profiles for " + ee);
            vvo.setUpFactorProfiles(layouts.get(ee.getId()));
        }
        result[i] = vvo;
        i++;
    }
    Long time = watch.getTime();
    if (time > 1000) {
        log.info("Created vis value objects in: " + time);
    }
    return result;
}
Also used : VisualizationValueObject(ubic.gemma.web.controller.visualization.VisualizationValueObject) DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 83 with StopWatch

use of org.apache.commons.lang3.time.StopWatch in project Gemma by PavlidisLab.

the class DEDVController method handleRequestInternal.

/**
 * Handle case of text export of the results.
 */
@RequestMapping("/downloadDEDV.html")
protected ModelAndView handleRequestInternal(HttpServletRequest request) {
    StopWatch watch = new StopWatch();
    watch.start();
    // might not be any
    Collection<Long> geneIds = ControllerUtils.extractIds(request.getParameter("g"));
    // might not be there
    Collection<Long> eeIds = ControllerUtils.extractIds(request.getParameter("ee"));
    ModelAndView mav = new ModelAndView(new TextView());
    if (eeIds == null || eeIds.isEmpty()) {
        mav.addObject("text", "Input empty for finding DEDVs: " + geneIds + " and " + eeIds);
        return mav;
    }
    String threshSt = request.getParameter("thresh");
    String resultSetIdSt = request.getParameter("rs");
    Double thresh = 100.0;
    if (StringUtils.isNotBlank(threshSt)) {
        try {
            thresh = Double.parseDouble(threshSt);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Threshold was not a valid value: " + threshSt);
        }
    }
    Map<ExpressionExperimentValueObject, Map<Long, Collection<DoubleVectorValueObject>>> result;
    if (request.getParameter("pca") != null) {
        int component = Integer.parseInt(request.getParameter("component"));
        Long eeId = eeIds.iterator().next();
        Map<ProbeLoading, DoubleVectorValueObject> topLoadedVectors = this.svdService.getTopLoadedVectors(eeId, component, thresh.intValue());
        if (topLoadedVectors == null)
            return null;
        mav.addObject("text", format4File(topLoadedVectors.values()));
        return mav;
    }
    /*
         * The following should be set if we're viewing diff. ex results.
         */
    Long resultSetId = null;
    if (StringUtils.isNumeric(resultSetIdSt)) {
        resultSetId = Long.parseLong(resultSetIdSt);
    }
    if (resultSetId != null) {
        /*
             * Diff ex case.
             */
        Long eeId = eeIds.iterator().next();
        Collection<DoubleVectorValueObject> diffExVectors = processedExpressionDataVectorService.getDiffExVectors(resultSetId, thresh, MAX_RESULTS_TO_RETURN);
        if (diffExVectors == null || diffExVectors.isEmpty()) {
            mav.addObject("text", "No results");
            return mav;
        }
        /*
             * Organize the vectors in the same way expected by the ee+gene type of request.
             */
        ExpressionExperimentValueObject ee = expressionExperimentService.loadValueObject(expressionExperimentService.load(eeId));
        result = new HashMap<>();
        Map<Long, Collection<DoubleVectorValueObject>> gmap = new HashMap<>();
        for (DoubleVectorValueObject dv : diffExVectors) {
            for (Long g : dv.getGenes()) {
                if (!gmap.containsKey(g)) {
                    gmap.put(g, new HashSet<DoubleVectorValueObject>());
                }
                gmap.get(g).add(dv);
            }
        }
        result.put(ee, gmap);
    } else {
        // Generic listing.
        result = getDEDV(eeIds, geneIds);
    }
    if (result == null || result.isEmpty()) {
        mav.addObject("text", "No results");
        return mav;
    }
    mav.addObject("text", format4File(result));
    watch.stop();
    Long time = watch.getTime();
    if (time > 100) {
        log.info("Retrieved and Formated" + result.keySet().size() + " DEDVs for eeIDs: " + eeIds + " and GeneIds: " + geneIds + " in : " + time + " ms.");
    }
    return mav;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ProbeLoading(ubic.gemma.model.analysis.expression.pca.ProbeLoading) StopWatch(org.apache.commons.lang3.time.StopWatch) TextView(ubic.gemma.web.view.TextView) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 84 with StopWatch

use of org.apache.commons.lang3.time.StopWatch in project Gemma by PavlidisLab.

the class DEDVController method getDEDVForDiffExVisualization.

/**
 * AJAX exposed method - for ProbeLevelDiffExGrid, VisualizationDifferentialWindow,
 * DifferentialExpressionAnalysesSummaryTree
 *
 * @param eeIds     FIXME accommodate ExpressionExperimentSubSets. Currently we pass in the "source experiment" so we
 *                  don't get the slice.
 * @param geneIds   (could be just one)
 * @param threshold for 'significance'
 * @param factorMap Collection of DiffExpressionSelectedFactorCommand showing which factors to use.
 */
public VisualizationValueObject[] getDEDVForDiffExVisualization(Collection<Long> eeIds, Collection<Long> geneIds, Double threshold, Collection<DiffExpressionSelectedFactorCommand> factorMap) {
    if (eeIds.isEmpty() || geneIds.isEmpty())
        return null;
    StopWatch watch = new StopWatch();
    watch.start();
    Collection<? extends BioAssaySet> ees = expressionExperimentService.load(eeIds);
    if (ees == null || ees.isEmpty())
        return null;
    Collection<Gene> genes = geneService.load(geneIds);
    if (genes == null || genes.isEmpty())
        return null;
    Collection<DoubleVectorValueObject> dedvs = processedExpressionDataVectorService.getProcessedDataArrays(ees, geneIds);
    watch.stop();
    Long time = watch.getTime();
    log.info("Retrieved " + dedvs.size() + " DEDVs for " + eeIds.size() + " EEs and " + geneIds.size() + " genes in " + time + " ms.");
    watch = new StopWatch();
    watch.start();
    Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts;
    layouts = experimentalDesignVisualizationService.sortVectorDataByDesign(dedvs);
    time = watch.getTime();
    if (time > 100) {
        log.info("Ran sortVectorDataByDesign on " + dedvs.size() + " DEDVs for 1 EE" + " in " + time + " ms (times <100ms not reported).");
    }
    // layouts = experimentalDesignVisualizationService.sortLayoutSamplesByFactor( layouts ); // required? yes, see
    // GSE11859
    time = watch.getTime();
    if (time > 100) {
        log.info("Ran sortLayoutSamplesByFactor on " + layouts.size() + " layouts" + " in " + time + " ms (times <100ms not reported).");
    }
    watch = new StopWatch();
    watch.start();
    Map<Long, Collection<DifferentialExpressionValueObject>> validatedProbes = getProbeDiffExValidation(genes, threshold, factorMap);
    watch.stop();
    time = watch.getTime();
    log.info("Retrieved " + validatedProbes.size() + " valid probes in " + time + " ms.");
    return makeDiffVisCollection(dedvs, new ArrayList<>(geneIds), validatedProbes, layouts);
}
Also used : StopWatch(org.apache.commons.lang3.time.StopWatch) Gene(ubic.gemma.model.genome.Gene) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 85 with StopWatch

use of org.apache.commons.lang3.time.StopWatch in project Gemma by PavlidisLab.

the class DEDVController method getProbeLinkValidation.

/**
 * Identify which probes were 'responsible' for the coexpression links.
 * FIXME change this to actually compute the correlations.
 *
 * @return map of EEID -> collection ProbeIDs which underlie the stored coexpression links.
 */
private Map<Long, Collection<Long>> getProbeLinkValidation(Collection<ExpressionExperiment> ees, Gene queryGene, Gene coexpressedGene, Collection<DoubleVectorValueObject> dedvs) {
    StopWatch watch = new StopWatch();
    watch.start();
    Map<Long, Collection<Long>> coexpressedEE2ProbeIds = new HashMap<>();
    Map<Long, Collection<Long>> queryEE2ProbeIds = new HashMap<>();
    /*
         * Get the probes for the vectors, organize by ee.
         */
    for (DoubleVectorValueObject dedv : dedvs) {
        ExpressionExperimentValueObject ee = dedv.getExpressionExperiment();
        if (dedv.getGenes().contains(queryGene.getId())) {
            if (!queryEE2ProbeIds.containsKey(ee.getId())) {
                queryEE2ProbeIds.put(ee.getId(), new HashSet<Long>());
            }
            queryEE2ProbeIds.get(ee.getId()).add(dedv.getDesignElement().getId());
        } else if (dedv.getGenes().contains(coexpressedGene.getId())) {
            if (!coexpressedEE2ProbeIds.containsKey(ee.getId())) {
                coexpressedEE2ProbeIds.put(ee.getId(), new HashSet<Long>());
            }
            coexpressedEE2ProbeIds.get(ee.getId()).add(dedv.getDesignElement().getId());
        } else {
            log.error("Dedv doesn't belong to coexpressed or query gene. QueryGene= " + queryGene + "CoexpressedGene= " + coexpressedGene + "DEDV " + dedv.getId() + " has genes: " + dedv.getGenes());
        }
    }
    Map<Long, Collection<Long>> validatedProbes = new HashMap<>();
    for (ExpressionExperiment ee : ees) {
        Collection<Long> queryProbeIds = queryEE2ProbeIds.get(ee.getId());
        Collection<Long> coexpressedProbeIds = coexpressedEE2ProbeIds.get(ee.getId());
        if (queryProbeIds == null || queryProbeIds.isEmpty()) {
            log.warn("Unexpectedly no probes for " + queryGene + " in " + ee);
            continue;
        }
        if (coexpressedProbeIds == null || coexpressedProbeIds.isEmpty()) {
            log.warn("Unexpectedly no probes for " + coexpressedGene + " in " + ee);
        }
    /*
             * Note: this does a probe-level query FIXME if we don't store data at probe-level we can't do this.
             */
    // Collection<Long> probesInLinks = this.geneCoexpressionService.getCoexpressedProbes( queryProbeIds,
    // coexpressedProbeIds, ee, queryGene.getTaxon().getCommonName() );
    // if ( probesInLinks.isEmpty() ) {
    // log.warn( "Unexpectedly no probes for link between " + queryGene + " -and- " + coexpressedGene + " in "
    // + ee );
    // }
    // 
    // validatedProbes.put( ee.getId(), probesInLinks );
    // FIXME FIXME
    }
    watch.stop();
    Long time = watch.getTime();
    if (time > 1000) {
        log.info("Validation of probes for " + ees.size() + " experiments in " + time + "ms.");
    }
    return validatedProbes;
}
Also used : DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

StopWatch (org.apache.commons.lang3.time.StopWatch)528 Test (org.junit.Test)150 EventResult (org.alfresco.bm.event.EventResult)97 DBObject (com.mongodb.DBObject)90 Event (org.alfresco.bm.event.Event)87 FolderData (org.alfresco.bm.cm.FolderData)75 File (java.io.File)71 ArrayList (java.util.ArrayList)49 HashSet (java.util.HashSet)31 Gene (ubic.gemma.model.genome.Gene)31 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)26 BaseTest (org.umlg.sqlg.test.BaseTest)26 Element (org.w3c.dom.Element)25 IOException (java.io.IOException)23 LoadSingleComponentUnitTest (org.alfresco.bm.dataload.LoadSingleComponentUnitTest)23 UserModel (org.alfresco.utility.model.UserModel)23 Collectors (java.util.stream.Collectors)19 HashMap (java.util.HashMap)18 List (java.util.List)18 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)18