Search in sources :

Example 1 with DifferentialExpressionValueObject

use of ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject in project Gemma by PavlidisLab.

the class DifferentialExpressionProbeResultEndpoint method invokeInternal.

/**
 * Reads the given <code>requestElement</code>, and sends a the response back.
 *
 * @param requestElement the contents of the SOAP message as DOM elements
 * @param document       a DOM document to be used for constructing <code>Node</code>s
 * @return the response element
 */
@Override
protected Element invokeInternal(Element requestElement, Document document) {
    StopWatch watch = new StopWatch();
    watch.start();
    setLocalName(LOCAL_NAME);
    // taxon input
    Collection<String> taxonInput = getSingleNodeValue(requestElement, "taxon_id");
    String taxonId = "";
    for (String id : taxonInput) {
        taxonId = id;
    }
    Taxon taxon = taxonService.load(Long.parseLong(taxonId));
    if (taxon == null) {
        String msg = "No taxon with id, " + taxonId + ", can be found.";
        return buildBadResponse(document, msg);
    }
    // gene ids input
    Collection<String> geneInput = getArrayValues(requestElement, "gene_ids");
    Collection<Long> geneIDLong = new HashSet<Long>();
    for (String id : geneInput) geneIDLong.add(Long.parseLong(id));
    Collection<Gene> rawGeneCol = geneService.loadThawed(geneIDLong);
    if (rawGeneCol.isEmpty()) {
        String msg = "None of the gene id's can be found.";
        return buildBadResponse(document, msg);
    }
    Collection<Gene> geneCol = retainGenesInCorrectTaxon(rawGeneCol, taxon);
    if (geneCol == null || geneCol.isEmpty()) {
        String msg = "Input genes do not match input taxon.";
        return buildBadResponse(document, msg);
    }
    // expression experiment set id input
    Collection<String> analysisInput = getSingleNodeValue(requestElement, "expression_experiment_set_id");
    String analysisId = "";
    for (String id : analysisInput) {
        analysisId = id;
    }
    ExpressionExperimentSet ees = expressionExperimentSetService.load(Long.parseLong(analysisId));
    if (ees == null) {
        String msg = "No matching expression experiment set can be found for id, " + analysisId;
        return buildBadResponse(document, msg);
    }
    if (!(ees.getTaxon().getId()).equals(taxon.getId())) {
        String msg = "Expression experiment set " + analysisId + " does not match input taxon " + taxon.getCommonName();
        return buildBadResponse(document, msg);
    }
    Collection<ExpressionExperiment> eeCol = getExperiments(ees);
    Collection<BioAssaySet> bioAssaySets = new HashSet<BioAssaySet>();
    bioAssaySets.addAll(eeCol);
    // threshold input
    Collection<String> thresholdInput = getSingleNodeValue(requestElement, "threshold");
    String threshold = "";
    for (String id : thresholdInput) {
        threshold = id;
    }
    log.info("XML input read: " + geneInput.size() + " gene ids,  & taxon id " + taxonId + ", & expression experiment set id " + analysisId + ", and threshold " + threshold);
    Element responseWrapper = document.createElementNS(NAMESPACE_URI, LOCAL_NAME);
    Element responseElement = document.createElementNS(NAMESPACE_URI, LOCAL_NAME + RESPONSE);
    responseWrapper.appendChild(responseElement);
    for (Gene gene : geneCol) {
        Map<ExpressionExperimentValueObject, List<DifferentialExpressionValueObject>> results = differentialExpressionResultService.find(gene, EntityUtils.getIds(bioAssaySets), Double.parseDouble(threshold), null);
        for (ExpressionExperimentValueObject ee : results.keySet()) {
            // main call to the DifferentialExpressionAnalysisService to retrieve
            // DifferentialExpressionAnalysisResultSet collection
            Collection<DifferentialExpressionValueObject> parCol = results.get(ee);
            // check that a DifferentialExpressionAnalysisResult is not null
            if (parCol == null || parCol.isEmpty()) {
                log.error("No probe analysis results can be found for gene: " + gene.getOfficialSymbol() + " & experiment: " + ee);
                buildXMLResponse(document, responseElement, gene.getId().toString(), ee.getId().toString(), null);
            } else
                buildXMLResponse(document, responseElement, gene.getId().toString(), ee.getId().toString(), parCol);
        }
    }
    watch.stop();
    Long time = watch.getTime();
    log.info("XML response for differential expression probe results built in " + time + "ms.");
    return responseWrapper;
}
Also used : Taxon(ubic.gemma.model.genome.Taxon) Element(org.w3c.dom.Element) DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) StopWatch(org.apache.commons.lang3.time.StopWatch) Gene(ubic.gemma.model.genome.Gene) BioAssaySet(ubic.gemma.model.expression.experiment.BioAssaySet) ExpressionExperimentValueObject(ubic.gemma.model.expression.experiment.ExpressionExperimentValueObject) ExpressionExperimentSet(ubic.gemma.model.analysis.expression.ExpressionExperimentSet)

Example 2 with DifferentialExpressionValueObject

use of ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject in project Gemma by PavlidisLab.

the class GeneDifferentialExpressionServiceImpl method postProcessDiffExResults.

private Collection<DifferentialExpressionValueObject> postProcessDiffExResults(Map<ExpressionExperimentValueObject, List<DifferentialExpressionValueObject>> results) {
    StopWatch timer = new StopWatch();
    timer.start();
    Collection<DifferentialExpressionValueObject> devos = new ArrayList<>();
    /*
         * convert to DEVOs
         */
    for (ExpressionExperimentValueObject eevo : results.keySet()) {
        Collection<DifferentialExpressionValueObject> probeResults = results.get(eevo);
        assert probeResults != null && !probeResults.isEmpty();
        for (DifferentialExpressionValueObject r : probeResults) {
            // this doesn't do anything any more since we're already working with valueobjects.
            Collection<ExperimentalFactorValueObject> efs = r.getExperimentalFactors();
            if (efs == null) {
                // This should not happen any more, but just in case.
                GeneDifferentialExpressionServiceImpl.log.warn("No experimentalfactor(s) for DifferentialExpressionAnalysisResult: " + r.getId());
                continue;
            }
            if (r.getCorrP() == null) {
                GeneDifferentialExpressionServiceImpl.log.warn("No p-value for DifferentialExpressionAnalysisResult: " + r.getId());
                continue;
            }
            devos.add(r);
        }
    }
    timer.stop();
    if (timer.getTime() > 1000) {
        GeneDifferentialExpressionServiceImpl.log.info("Postprocess Diff ex results: " + timer.getTime() + " ms");
    }
    return devos;
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 3 with DifferentialExpressionValueObject

use of ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject in project Gemma by PavlidisLab.

the class GeneDifferentialExpressionServiceImpl method findMinPenalizedProbeResult.

/**
 * When n probes map to the sa
 *
 * @return the result with the min p value.
 */
private DifferentialExpressionValueObject findMinPenalizedProbeResult(Collection<DifferentialExpressionValueObject> results) {
    DifferentialExpressionValueObject minResult = null;
    int numProbesForGene = results.size();
    if (numProbesForGene == 1) {
        return results.iterator().next();
    }
    double min = 0;
    int i = 0;
    for (DifferentialExpressionValueObject r : results) {
        if (r.getP() == null)
            continue;
        /* penalize pvals */
        double pval = r.getP() * numProbesForGene;
        if (pval > GeneDifferentialExpressionServiceImpl.MAX_PVAL)
            pval = GeneDifferentialExpressionServiceImpl.MAX_PVAL;
        /* find the best pval */
        if (i == 0 || pval <= min) {
            min = pval;
            minResult = r;
            minResult.setP(min);
        }
        i++;
    }
    return minResult;
}
Also used : DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject)

Example 4 with DifferentialExpressionValueObject

use of ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject in project Gemma by PavlidisLab.

the class GeneDifferentialExpressionServiceImpl method getDifferentialExpressionMetaAnalysis.

@Override
public DifferentialExpressionMetaAnalysisValueObject getDifferentialExpressionMetaAnalysis(double threshold, Gene g, Map<Long, Long> eeFactorsMap, Collection<BioAssaySet> activeExperiments) {
    StopWatch timer = new StopWatch();
    timer.start();
    /*
         * Get results for each active experiment on given gene. Handling the threshold check below since we ignore this
         * for the meta analysis. The results returned are for all factors, not just the factors we are seeking.
         */
    Map<ExpressionExperimentValueObject, List<DifferentialExpressionValueObject>> resultsMap = differentialExpressionResultService.find(g, EntityUtils.getIds(activeExperiments));
    GeneDifferentialExpressionServiceImpl.log.debug(resultsMap.size() + " results for " + g + " in " + activeExperiments);
    DifferentialExpressionMetaAnalysisValueObject mavo = new DifferentialExpressionMetaAnalysisValueObject();
    DoubleArrayList pvaluesToCombine = new DoubleArrayList();
    /* a gene can have multiple probes that map to it, so store one diff value object for each probe */
    Collection<DifferentialExpressionValueObject> devos = new ArrayList<>();
    Collection<Long> eesThatMetThreshold = new HashSet<>();
    for (ExpressionExperimentValueObject ee : resultsMap.keySet()) {
        ExpressionExperimentValueObject eevo = this.configExpressionExperimentValueObject(ee);
        Collection<DifferentialExpressionValueObject> proberesults = resultsMap.get(ee);
        Collection<DifferentialExpressionValueObject> filteredResults = new HashSet<>();
        for (DifferentialExpressionValueObject r : proberesults) {
            Collection<ExperimentalFactorValueObject> efs = r.getExperimentalFactors();
            assert efs.size() > 0;
            if (efs.size() > 1) {
                // We always ignore interaction effects.
                continue;
            }
            ExperimentalFactorValueObject ef = efs.iterator().next();
            /*
                 * note that we don't care about the reverse: the eefactorsmap can have stuff we don't need. We focus on
                 * the experiments because they are easy to select & secure. The eefactorsmap provides additional
                 * details.
                 */
            assert eeFactorsMap.containsKey(ee.getId()) : "eeFactorsMap does not contain ee=" + ee.getId();
            Long sfId = eeFactorsMap.get(ee.getId());
            if (!ef.getId().equals(sfId)) {
                /*
                     * Screen out factors we're not using.
                     */
                continue;
            }
            /* filtered result with chosen factor */
            filteredResults.add(r);
        }
        if (filteredResults.size() == 0) {
            GeneDifferentialExpressionServiceImpl.log.warn("No result for ee=" + ee);
            continue;
        }
        /*
             * For the diff expression meta analysis, ignore threshold. Select the 'best' penalized probe if multiple
             * probes map to the same gene.
             */
        DifferentialExpressionValueObject res = this.findMinPenalizedProbeResult(filteredResults);
        if (res == null)
            continue;
        Double p = res.getP();
        if (p == null)
            continue;
        /*
             * Moderate the pvalues by setting all values to be no smaller than PVALUE_CLIP_THRESHOLD
             */
        pvaluesToCombine.add(Math.max(p, GeneDifferentialExpressionService.PVALUE_CLIP_THRESHOLD));
        for (DifferentialExpressionValueObject r : filteredResults) {
            Collection<ExperimentalFactorValueObject> efs = r.getExperimentalFactors();
            if (efs == null) {
                // This should not happen any more, but just in case.
                GeneDifferentialExpressionServiceImpl.log.warn("No experimentalFactor(s) for DifferentialExpressionAnalysisResult: " + r.getId());
                continue;
            }
            Boolean metThreshold = r.getCorrP() != null && (r.getCorrP() <= threshold);
            r.setMetThreshold(metThreshold);
            if (metThreshold) {
                eesThatMetThreshold.add(eevo.getId());
            }
            Boolean fisherContribution = r.equals(res);
            r.setFisherContribution(fisherContribution);
        }
    }
    /*
         * Meta-analysis part.
         */
    double fisherPval = MetaAnalysis.fisherCombinePvalues(pvaluesToCombine);
    mavo.setFisherPValue(fisherPval);
    mavo.setGene(new GeneValueObject(g));
    mavo.setActiveExperiments(activeExperiments);
    mavo.setProbeResults(devos);
    mavo.setNumMetThreshold(eesThatMetThreshold.size());
    mavo.setSortKey();
    timer.stop();
    if (timer.getTime() > 1000) {
        GeneDifferentialExpressionServiceImpl.log.info("Meta-analysis results: " + timer.getTime() + " ms");
    }
    return mavo;
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject) DoubleArrayList(cern.colt.list.DoubleArrayList) StopWatch(org.apache.commons.lang3.time.StopWatch) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 5 with DifferentialExpressionValueObject

use of ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject in project Gemma by PavlidisLab.

the class GeneDifferentialExpressionServiceImpl method getDifferentialExpression.

@Override
public Collection<DifferentialExpressionValueObject> getDifferentialExpression(Gene gene, double threshold, Integer limit) {
    Collection<DifferentialExpressionValueObject> devos = new ArrayList<>();
    if (gene == null)
        return devos;
    StopWatch timer = new StopWatch();
    timer.start();
    Map<ExpressionExperimentValueObject, List<DifferentialExpressionValueObject>> results = differentialExpressionResultService.find(gene, threshold, limit);
    if (timer.getTime() > 1000) {
        GeneDifferentialExpressionServiceImpl.log.info("Diff raw ex results: " + timer.getTime() + " ms");
    }
    return this.postProcessDiffExResults(results);
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject) DoubleArrayList(cern.colt.list.DoubleArrayList) StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

DifferentialExpressionValueObject (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject)14 StopWatch (org.apache.commons.lang3.time.StopWatch)8 DoubleArrayList (cern.colt.list.DoubleArrayList)6 Element (org.w3c.dom.Element)2 DoubleVectorValueObject (ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)2 BioAssaySet (ubic.gemma.model.expression.experiment.BioAssaySet)2 Gene (ubic.gemma.model.genome.Gene)2 GeneValueObject (ubic.gemma.model.genome.gene.GeneValueObject)2 DiffExpressionSelectedFactorCommand (ubic.gemma.core.analysis.expression.diff.DiffExpressionSelectedFactorCommand)1 ExpressionExperimentSet (ubic.gemma.model.analysis.expression.ExpressionExperimentSet)1 ExpressionAnalysisResultSet (ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet)1 BioAssayValueObject (ubic.gemma.model.expression.bioAssay.BioAssayValueObject)1 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)1 ExpressionExperimentValueObject (ubic.gemma.model.expression.experiment.ExpressionExperimentValueObject)1 Taxon (ubic.gemma.model.genome.Taxon)1 ExpressionExperimentExperimentalFactorValueObject (ubic.gemma.web.controller.expression.experiment.ExpressionExperimentExperimentalFactorValueObject)1 VisualizationValueObject (ubic.gemma.web.controller.visualization.VisualizationValueObject)1