Search in sources :

Example 1 with CountingMap

use of ubic.basecode.dataStructure.CountingMap in project Gemma by PavlidisLab.

the class CoexpressionDaoImpl method getCoexpressionFromCacheOrDbViaExperiments.

/*
     * Get links from the cache or the database, querying in experiment-first mode, but constrained to involve the given
     * genes. Does not do the trimming step, nor are the results guaranteed to meet the stringency set.
     */
private Map<Long, List<CoexpressionValueObject>> getCoexpressionFromCacheOrDbViaExperiments(Taxon t, Collection<Long> genes, Collection<Long> bas, int stringency, boolean quick) {
    assert stringency <= bas.size();
    assert !genes.isEmpty();
    Map<Long, List<CoexpressionValueObject>> results = new HashMap<>();
    /*
         * First, check the cache -- if the stringency is >= limit
         */
    Collection<Long> genesNeeded = new HashSet<>(genes);
    if (stringency >= CoexpressionCache.CACHE_QUERY_STRINGENCY) {
        genesNeeded = this.checkCache(genes, results);
        if (genesNeeded.isEmpty()) {
            return results;
        }
    }
    /*
         * Get all the data for all the experiments queried, constrained to involve the genes in question.
         *
         * This uses the ECL1EFK index, which is of (experiment, gene1, gene2). Note that if there are a lot of genes
         * this can get slow ...
         */
    Query q = this.getSessionFactory().getCurrentSession().createQuery(" from " + CoexpressionQueryUtils.getExperimentLinkClassName(t) + " where experiment.id in (:ees) and firstGene in (:genes)");
    // May need to batch over genes...
    BatchIterator<Long> it = BatchIterator.batches(bas, CoexpressionDaoImpl.BATCH_SIZE_SMALL);
    StopWatch timer = new StopWatch();
    timer.start();
    List<ExperimentCoexpressionLink> links = new ArrayList<>();
    for (; it.hasNext(); ) {
        q.setParameterList("ees", it.next()).setParameterList("genes", genesNeeded);
        links.addAll(q.list());
    }
    if (timer.getTime() > 2000) {
        CoexpressionDaoImpl.log.info("Query for coexp for : " + genes.size() + " genes " + " in " + bas.size() + " experiments: " + timer.getTime() + "ms");
    }
    /*
         * Track the support for the links among the queried data sets as we go over this in experiment-major mode.
         */
    // noinspection MismatchedQueryAndUpdateOfCollection // We still need to compare it to stringency
    CountingMap<Long> supportCounts = new CountingMap<>();
    List<Long> keepers = new ArrayList<>();
    for (ExperimentCoexpressionLink link : links) {
        assert genes.contains(link.getFirstGene());
        if (supportCounts.increment(link.getLinkId()) >= stringency) {
            keepers.add(link.getLinkId());
        }
    }
    if (keepers.isEmpty()) {
        return new HashMap<>();
    }
    return this.loadAndConvertLinks(t, keepers, genes, quick);
}
Also used : ExperimentCoexpressionLink(ubic.gemma.model.association.coexpression.ExperimentCoexpressionLink) StopWatch(org.apache.commons.lang3.time.StopWatch) CountingMap(ubic.basecode.dataStructure.CountingMap)

Aggregations

StopWatch (org.apache.commons.lang3.time.StopWatch)1 CountingMap (ubic.basecode.dataStructure.CountingMap)1 ExperimentCoexpressionLink (ubic.gemma.model.association.coexpression.ExperimentCoexpressionLink)1