Example usage for org.hibernate SQLQuery setParameterList

List of usage examples for org.hibernate SQLQuery setParameterList

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setParameterList.

Prototype

@Override
    NativeQuery<T> setParameterList(String name, Object[] values);

Source Link

Usage

From source file:service.ProblemService.java

public static List<ProblemInfo> getProblems(List<Integer> platforms, List<String> tags) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    String sql = "SELECT id, title, platform, problem_url from problem ";

    boolean emptyPlatform = true;
    boolean emptyTags = true;

    if (platforms != null && platforms.size() > 0) {
        emptyPlatform = false;// w w  w.j  a  v  a  2 s . c  o  m
        sql += "where platform in(:platforms)";
    }

    if (tags != null && tags.size() > 0) {
        emptyTags = false;

        if (emptyPlatform) {
            sql += "where ";
        } else {
            sql += "and ";
        }

        sql += "id in(select id from tags where tag_name in (:tags))";
    }

    SQLQuery query = session.createSQLQuery(sql);

    if (!emptyPlatform) {
        query.setParameterList("platforms", platforms);
    }
    if (!emptyTags) {
        query.setParameterList("tags", tags);
    }

    List result = query.list();
    List<ProblemInfo> problems = new ArrayList<>();

    for (Iterator iterator = result.iterator(); iterator.hasNext();) {
        Object[] objects = (Object[]) iterator.next();
        int id = (Integer) objects[0];
        String title = (String) objects[1];
        Platform platform = Platform.fromOrdinal((Integer) objects[2]);
        String problemUrl = (String) objects[3];
        ProblemInfo problemInfo = new ProblemInfo(id, title, platform, problemUrl);
        problems.add(problemInfo);
    }

    session.close();
    return problems;
}

From source file:ubic.gemma.persistence.service.analysis.expression.diff.DifferentialExpressionAnalysisDaoImpl.java

License:Apache License

private void fetchExperimentsTestingGeneNativeQuery(Collection<CompositeSequence> probes,
        Collection<BioAssaySet> result, final String nativeQuery, Taxon taxon) {

    if (probes.isEmpty())
        return;//from  ww w.  j a  v  a2  s.co  m

    SQLQuery nativeQ = this.getSessionFactory().getCurrentSession().createSQLQuery(nativeQuery);
    nativeQ.setParameterList("probes", EntityUtils.getIds(probes));
    nativeQ.setParameter("taxon", taxon);
    List<?> list = nativeQ.list();
    Set<Long> ids = new HashSet<>();
    for (Object o : list) {
        ids.add(((BigInteger) o).longValue());
    }
    if (!ids.isEmpty()) {
        //noinspection unchecked
        result.addAll(this.getHibernateTemplate()
                .findByNamedParam("from ExpressionExperiment e where e.id in (:ids)", "ids", ids));
    }
}

From source file:ubic.gemma.persistence.service.analysis.expression.diff.DifferentialExpressionResultDaoImpl.java

License:Apache License

@Override
public Map<Long, Map<Long, DiffExprGeneSearchResult>> findDiffExAnalysisResultIdsInResultSets(
        Collection<DiffExResultSetSummaryValueObject> resultSets, Collection<Long> geneIds) {

    Map<Long, Map<Long, DiffExprGeneSearchResult>> results = new HashMap<>();

    Session session = this.getSessionFactory().getCurrentSession();

    Map<Long, DiffExResultSetSummaryValueObject> resultSetIdsMap = EntityUtils.getIdMap(resultSets,
            "getResultSetId");

    Map<Long, Collection<Long>> foundInCache = this.fillFromCache(results, resultSetIdsMap.keySet(), geneIds);

    if (!foundInCache.isEmpty()) {
        AbstractDao.log.info("Results for " + foundInCache.size() + " resultsets found in cache");
    } else {//from   ww  w .  j a v a  2s .c  om
        AbstractDao.log.info("No results were in the cache");
    }

    Collection<Long> resultSetsNeeded = this.stripUnneededResultSets(foundInCache, resultSetIdsMap.keySet(),
            geneIds);

    // Are we finished?
    if (resultSetsNeeded.isEmpty()) {
        AbstractDao.log.info("All results were in the cache.");
        return results;
    }

    AbstractDao.log.info(foundInCache.size() + "/" + resultSetIdsMap.size()
            + " resultsSets had at least some cached results; still need to query " + resultSetsNeeded.size());

    assert !resultSetsNeeded.isEmpty();

    org.hibernate.SQLQuery queryObject = session.createSQLQuery(
            DifferentialExpressionResultDaoImpl.fetchBatchDifferentialExpressionAnalysisResultsByResultSetsAndGeneQuery);

    /*
     * These values have been tweaked to probe for performance issues.
     */
    int resultSetBatchSize = 50;
    int geneBatchSize = 100;

    if (resultSetsNeeded.size() > geneIds.size()) {
        resultSetBatchSize = Math.min(500, resultSetsNeeded.size());
        AbstractDao.log.info("Batching by result sets (" + resultSetsNeeded.size() + " resultSets); "
                + geneIds.size() + " genes; batch size=" + resultSetBatchSize);

    } else {
        geneBatchSize = Math.min(200, geneIds.size());
        AbstractDao.log.info("Batching by genes (" + geneIds.size() + " genes); " + resultSetsNeeded.size()
                + " resultSets; batch size=" + geneBatchSize);
    }

    final int numResultSetBatches = (int) Math.ceil(resultSetsNeeded.size() / resultSetBatchSize);

    queryObject.setFlushMode(FlushMode.MANUAL);

    StopWatch timer = new StopWatch();
    timer.start();
    int numResults = 0;
    long timeForFillingNonSig = 0;

    Map<Long, Map<Long, DiffExprGeneSearchResult>> resultsFromDb = new HashMap<>();

    int numResultSetBatchesDone = 0;

    // Iterate over batches of resultSets
    for (Collection<Long> resultSetIdBatch : new BatchIterator<>(resultSetsNeeded, resultSetBatchSize)) {

        if (AbstractDao.log.isDebugEnabled())
            AbstractDao.log.debug("Starting batch of resultsets: "
                    + StringUtils.abbreviate(StringUtils.join(resultSetIdBatch, ","), 100));

        /*
         * Get the probes using the CommonQueries gene2cs. Otherwise we (in effect) end up doing this over and over
         * again.
         */
        Map<Long, Collection<Long>> cs2GeneIdMap = this.getProbesForGenesInResultSetBatch(session, geneIds,
                resultSetIdsMap, resultSetIdBatch);

        queryObject.setParameterList("rs_ids", resultSetIdBatch);

        int numGeneBatchesDone = 0;
        final int numGeneBatches = (int) Math.ceil(cs2GeneIdMap.size() / geneBatchSize);

        StopWatch innerQt = new StopWatch();

        // iterate over batches of probes (genes)
        for (Collection<Long> probeBatch : new BatchIterator<>(cs2GeneIdMap.keySet(), geneBatchSize)) {

            if (AbstractDao.log.isDebugEnabled())
                AbstractDao.log.debug("Starting batch of probes: "
                        + StringUtils.abbreviate(StringUtils.join(probeBatch, ","), 100));

            // would it help to sort the probeBatch/
            List<Long> pbL = new Vector<>(probeBatch);
            Collections.sort(pbL);

            queryObject.setParameterList("probe_ids", pbL);

            innerQt.start();
            List<?> queryResult = queryObject.list();
            innerQt.stop();

            if (innerQt.getTime() > 2000) {
                // show the actual query with params.
                AbstractDao.log.info("Query time: " + innerQt.getTime() + "ms:\n "
                        + queryObject.getQueryString().replace(":probe_ids", StringUtils.join(probeBatch, ","))
                                .replace(":rs_ids", StringUtils.join(resultSetIdBatch, ",")));
            }
            innerQt.reset();

            /*
             * Each query tuple are the probe, result, resultsSet, qvalue, pvalue.
             */
            for (Object o : queryResult) {
                // Long resultSetId = ( ( BigInteger )((Object[])o)[2] ).longValue();
                // if (!resultSetId.equals)
                numResults += this.processResultTuple(o, resultsFromDb, cs2GeneIdMap);
            }

            if (timer.getTime() > 5000 && AbstractDao.log.isInfoEnabled()) {
                AbstractDao.log.info("Batch time: " + timer.getTime() + "ms; Fetched DiffEx " + numResults
                        + " results so far. " + numResultSetBatchesDone + "/" + numResultSetBatches
                        + " resultset batches completed. " + numGeneBatchesDone + "/" + numGeneBatches
                        + " gene batches done.");
                timer.reset();
                timer.start();
            }

            // Check if task was cancelled.
            if (Thread.currentThread().isInterrupted()) {
                throw new TaskCancelledException("Search was cancelled");
            }

            numGeneBatchesDone++;

            if (DifferentialExpressionResultDaoImpl.CORRECTED_PVALUE_THRESHOLD_TO_BE_CONSIDERED_DIFF_EX < 1.0) {
                timeForFillingNonSig += this.fillNonSignificant(pbL, resultSetIdsMap, resultsFromDb,
                        resultSetIdBatch, cs2GeneIdMap, session);
            }
        } // over probes.

        // Check if task was cancelled.
        if (Thread.currentThread().isInterrupted()) {
            throw new TaskCancelledException("Search was cancelled");
        }

        numResultSetBatchesDone++;

    }

    if (timer.getTime() > 1000 && AbstractDao.log.isInfoEnabled()) {
        AbstractDao.log.info("Fetching DiffEx from DB took total of " + timer.getTime() + " ms : geneIds="
                + StringUtils.abbreviate(StringUtils.join(geneIds, ","), 50) + " result set="
                + StringUtils.abbreviate(StringUtils.join(resultSetsNeeded, ","), 50));
        if (timeForFillingNonSig > 100) {
            AbstractDao.log.info("Filling in non-significant values: " + timeForFillingNonSig + "ms in total");
        }
    }

    // Add the DB results to the cached results.
    this.addToCache(resultsFromDb, resultSetsNeeded, geneIds);

    for (Long resultSetId : resultsFromDb.keySet()) {
        Map<Long, DiffExprGeneSearchResult> geneResults = resultsFromDb.get(resultSetId);
        if (results.containsKey(resultSetId)) {
            results.get(resultSetId).putAll(geneResults);
        } else {
            results.put(resultSetId, geneResults);
        }
    }

    return results;
}

From source file:ubic.gemma.persistence.service.analysis.expression.diff.DifferentialExpressionResultDaoImpl.java

License:Apache License

/**
 * Key method for getting contrasts associated with results.
 *///from   www. j  a va 2 s.  c o m
@Override
public Map<Long, ContrastsValueObject> loadContrastDetailsForResults(Collection<Long> ids) {
    //language=SQL
    final String queryString = "SELECT DISTINCT c.ID, c.LOG_FOLD_CHANGE, c.FACTOR_VALUE_FK,"
            + " c.DIFFERENTIAL_EXPRESSION_ANALYSIS_RESULT_FK, c.PVALUE FROM CONTRAST_RESULT c"
            + " WHERE c.DIFFERENTIAL_EXPRESSION_ANALYSIS_RESULT_FK IN (:ids)  ";

    Map<Long, ContrastsValueObject> probeResults = new HashMap<>();

    if (ids.isEmpty()) {
        return probeResults;
    }

    SQLQuery query = this.getSessionFactory().getCurrentSession().createSQLQuery(queryString);

    int BATCH_SIZE = 2000; // previously: 500, then 1000. New optimized query is plenty fast.
    StopWatch timer = new StopWatch();
    for (Collection<Long> batch : new BatchIterator<>(ids, BATCH_SIZE)) {
        timer.reset();
        timer.start();

        query.setParameterList("ids", batch);

        List<?> batchR = query.list();

        for (Object o : batchR) {
            Object[] ol = (Object[]) o;

            Long resultId = ((BigInteger) ol[3]).longValue();

            if (!probeResults.containsKey(resultId)) {
                probeResults.put(resultId, new ContrastsValueObject(resultId));
            }

            ContrastsValueObject cvo = probeResults.get(resultId);

            Long contrastId = ((BigInteger) ol[0]).longValue();

            Double logFoldChange = ol[1] == null ? null : (Double) ol[1];

            Long factorValueId = ol[2] == null ? null : ((BigInteger) ol[2]).longValue();

            Double pvalue = ol[4] == null ? null : (Double) ol[4];

            cvo.addContrast(contrastId, factorValueId, logFoldChange, pvalue, null);

        }

        if (timer.getTime() > 2000) {
            AbstractDao.log.info("Fetch " + batch.size() + " results with contrasts: " + timer.getTime()
                    + "ms; query was\n " + queryString.replace(":ids", StringUtils.join(batch, ",")));
        }
    }

    return probeResults;
}

From source file:ubic.gemma.persistence.service.association.coexpression.CoexpressionDaoImpl.java

License:Apache License

@SuppressWarnings({ "unused", "WeakerAccess" }) // Possible external use
public List<Object[]> getRawCoexpressionFromDbViaGenes(Collection<Long> geneIds, Taxon t, int stringency) {
    String sqlQuery1 = "select ID, POSITIVE, SUPPORT, FIRST_GENE_FK, SECOND_GENE_FK, SUPPORT_DETAILS_FK from "
            + CoexpressionQueryUtils.getGeneLinkTableName(t)
            + " where FIRST_GENE_FK in (:genes) and SUPPORT>=:s";

    Session sess = this.getSessionFactory().getCurrentSession();
    SQLQuery query1 = sess.createSQLQuery(sqlQuery1);

    query1.setParameterList("genes", geneIds.toArray());
    query1.setParameter("s", Math.max(1, stringency));

    // This is actually pretty fast.
    return (List<Object[]>) query1.list();
}

From source file:ubic.gemma.persistence.service.association.coexpression.CoexpressionDaoImpl.java

License:Apache License

/**
 * Alternative method: query twice, once to get the coexpression basics and then again to get the support details,
 * instead of using a join./*  w w  w. j  a va 2s  .com*/
 *
 * @param populateTestedInDetails populate tested in details
 * @param stringency              stringency
 * @param geneIds                 gene IDs
 * @param t                       taxon
 */
private Map<Long, List<CoexpressionValueObject>> getCoexpressionFromDbViaGenes2(Collection<Long> geneIds,
        Taxon t, int stringency, boolean populateTestedInDetails) {

    StopWatch timer = new StopWatch();
    timer.start();
    List<Object[]> q1results = this.getRawCoexpressionFromDbViaGenes(geneIds, t, stringency);

    CoexpressionDaoImpl.log.debug(q1results.size() + " raw coexpression results for " + geneIds.size()
            + " genes at support>=" + stringency + " " + timer.getTime() + "ms");

    if (q1results.isEmpty()) {
        return new HashMap<>();
    }

    List<Object[]> supportDetails = new ArrayList<>();

    /*
     * Because we are not trimming the results at all here, this can be a lot of data to iterate over, even at
     * high stringencies. For example, for 20 genes at a stringency of 5, because the query above does not
     * constrain to data sets, there can be >500 per gene, or >100k links in total. Fetching the support details
     * here is rather wasteful if we are not retaining the results, but we don't know that until we know which
     * data sets are supporting.
     */

    BatchIterator<Object[]> batches = BatchIterator.batches(q1results, CoexpressionDaoImpl.BATCH_SIZE);

    int n = 1;
    for (Collection<Object[]> batch : batches) {
        StopWatch timer2 = new StopWatch();
        timer2.start();

        List<Long> supportDetailsIds = new ArrayList<>();
        for (Object[] oa : batch) {
            Long supportDetailsId = ((BigInteger) oa[5]).longValue();
            supportDetailsIds.add(supportDetailsId);
        }

        // Note: should never be empty
        String sqlQuery2 = "select ID,BYTES from " + CoexpressionQueryUtils.getSupportDetailsTableName(t)
                + " where ID in (:ids)";
        SQLQuery query2 = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery2);

        query2.setParameterList("ids", supportDetailsIds.toArray());

        supportDetails.addAll(query2.list());

        if (timer2.getTime() > 1000) {
            CoexpressionDaoImpl.log
                    .debug("Fetch batch " + n + " of support details: " + timer2.getTime() + "ms");
        }
        n++;
    }

    CoexpressionDaoImpl.log
            .debug("Fetched details for " + supportDetails.size() + " coexpressions, " + n + " batches");

    if (timer.getTime() > 5000) {
        CoexpressionDaoImpl.log.info("Coexpression query: " + geneIds.size() + " genes took " + timer.getTime()
                + "ms: " + q1results.size() + " results");
    }

    timer.reset();
    timer.start();
    // it might be better to do this in the loop above, incrementally per batch.
    Map<Long, List<CoexpressionValueObject>> results = this.convertToValueObjects(q1results, supportDetails,
            geneIds);
    if (timer.getTime() > 100) {
        CoexpressionDaoImpl.log
                .info("Convert to value objects " + q1results.size() + " results: " + timer.getTime() + "ms");
    }

    timer.reset();
    timer.start();

    for (Long g : results.keySet()) {
        List<CoexpressionValueObject> gc = results.get(g);
        Collections.sort(gc);
        if (populateTestedInDetails) {
            this.populateTestedInDetails(gc);
        }
    }

    if (timer.getTime() > 100) {
        CoexpressionDaoImpl.log
                .info("Filter, sort and finish " + q1results.size() + " results: " + timer.getTime() + "ms");
    }

    return results;
}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

/**
 * Key method: find Genes link to a phenotype taking into account private and public evidence Here on the cases :
 * <ul>//  w w w  .  ja  v a2  s .c o m
 * <li>1- Admin - can see anything
 * <li>2- user not logged in - only public data
 * <li>3- user logged in only showing what he has read access - public, shared + owned
 * <li>4- user logged in only showing what he has write access - owned.
 * </ul>
 */
@Override
public Collection<GeneEvidenceValueObject> findGenesWithPhenotypes(Set<String> phenotypeUris, Taxon taxon,
        boolean showOnlyEditable, Collection<Long> externalDatabaseIds) {

    if (phenotypeUris.isEmpty()) {
        return new HashSet<>();
    }

    // build query.
    // base query; 0: gene id 1: ncbi id 2: name 3: symbol 4: taxon id 5: taxon name 6: characteristic value URI
    String sqlSelectQuery = "select distinct gene.ID as gid, gene.NCBI_GENE_ID, gene.OFFICIAL_NAME, "
            + "gene.OFFICIAL_SYMBOL, tax.ID as taxonid, tax.COMMON_NAME, charac.VALUE_URI ";

    String sqlQuery = sqlSelectQuery + this.getPhenotypesGenesAssociationsBeginQuery(false);

    sqlQuery += this.addValuesUriToQuery(SecurityUtil.isUserAdmin() ? " where " : " and ", phenotypeUris);

    if (!SecurityUtil.isUserAdmin()) {
        if (!sqlQuery.trim().endsWith("where")) {
            sqlQuery += " and ";
        }
        sqlQuery += EntityUtils.addGroupAndUserNameRestriction(showOnlyEditable, true);
    }
    sqlQuery += this.addTaxonToQuery(taxon, !showOnlyEditable);
    sqlQuery += this.addExternalDatabaseQuery(externalDatabaseIds);

    // create query and set parameters.
    SQLQuery queryObject = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery);
    queryObject.setParameterList("valueUris", phenotypeUris);

    if (sqlQuery.contains(":taxonId")) {
        queryObject.setParameter("taxonId", taxon.getId());
    }
    EntityUtils.addUserAndGroupParameters(queryObject, this.getSessionFactory());

    return this.populateGenesWithPhenotypes(queryObject);

}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

/**
 * find all private phenotypes associated with genes on a specific taxon and containing the valuesUri
 *///from  w ww .ja va  2  s.c  om
@Override
public Map<String, Set<Integer>> findPrivatePhenotypesGenesAssociations(Taxon taxon, Set<String> valuesUri,
        boolean showOnlyEditable, Collection<Long> externalDatabaseIds, boolean noElectronicAnnotation) {

    /*
     * At this level of the application, we can't access acls. The reason for this so we don't get uneven page
     * numbers. ACESID 4 is anonymous; MASK=1 is read.
     */
    String sqlQuery = "select gene.NCBI_GENE_ID, charac.VALUE_URI ";
    sqlQuery += this.getPhenotypesGenesAssociationsBeginQuery(true);
    if (!sqlQuery.trim().endsWith("where")) {
        sqlQuery += " and ";
    }
    sqlQuery += EntityUtils.addGroupAndUserNameRestriction(showOnlyEditable, false);
    sqlQuery += "and phen.ID not in " + "(select phen.ID from CHARACTERISTIC as charac "
            + " inner join PHENOTYPE_ASSOCIATION as phen on charac.PHENOTYPE_ASSOCIATION_FK = phen.ID "
            + "inner join CHROMOSOME_FEATURE as gene on gene.ID = phen.GENE_FK "
            + "inner join TAXON tax on tax.ID = gene.TAXON_FK "
            // ACL
            + "inner join ACLOBJECTIDENTITY aoi on phen.ID = aoi.OBJECT_ID "
            + "inner join ACLENTRY ace on ace.OBJECTIDENTITY_FK = aoi.ID "
            + "inner join ACLSID sid on sid.ID = aoi.OWNER_SID_FK where ace.MASK = 1 and ace.SID_FK = 4 "
            + "and aoi.OBJECT_CLASS IN " + PhenotypeAssociationDaoImpl.DISCRIMINATOR_CLAUSE + ") ";
    sqlQuery += this.addTaxonToQuery(taxon);
    sqlQuery += this.addValuesUriToQuery("and", valuesUri);
    sqlQuery += this.addExternalDatabaseQuery(externalDatabaseIds);

    if (noElectronicAnnotation) {
        sqlQuery += PhenotypeAssociationDaoImpl.QUERY_EV_CODE;
    }

    SQLQuery queryObject = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery);

    if (sqlQuery.contains(":valueUris")) {
        queryObject.setParameterList("valueUris", valuesUri);
    }

    if (sqlQuery.contains(":taxonId")) {
        queryObject.setParameter("taxonId", taxon.getId());
    }

    EntityUtils.addUserAndGroupParameters(queryObject, this.getSessionFactory());
    return this.populateGenesAssociations(queryObject);

}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

/**
 * find all public phenotypes associated with genes on a specific taxon and containing the valuesUri
 *//*from  ww w .j  ava 2 s . c  o m*/
@Override
public Map<String, Set<Integer>> findPublicPhenotypesGenesAssociations(Taxon taxon, Set<String> valuesUri,
        boolean showOnlyEditable, Collection<Long> externalDatabaseIds, boolean noElectronicAnnotation) {

    String sqlQuery = "select gene.NCBI_GENE_ID, charac.VALUE_URI ";
    sqlQuery += this.getPhenotypesGenesAssociationsBeginQuery(true);

    // rule to find public: anonymous, READ.
    if (!sqlQuery.trim().endsWith("where")) {
        sqlQuery += " and ";
    }
    sqlQuery += " ace.MASK = 1 and ace.SID_FK = 4 ";
    sqlQuery += this.addTaxonToQuery(taxon);
    sqlQuery += this.addValuesUriToQuery("and", valuesUri);
    sqlQuery += this.addExternalDatabaseQuery(externalDatabaseIds);

    if (noElectronicAnnotation) {
        sqlQuery += PhenotypeAssociationDaoImpl.QUERY_EV_CODE;
    }

    if (showOnlyEditable) {
        sqlQuery += "and phen.ID in ( select phen.ID ";
        sqlQuery += this.getPhenotypesGenesAssociationsBeginQuery(false);
        if (!sqlQuery.trim().endsWith("where")) {
            sqlQuery += " and ";
        }
        sqlQuery += EntityUtils.addGroupAndUserNameRestriction(true, false);
        sqlQuery += ") ";
    }

    SQLQuery queryObject = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery);

    if (sqlQuery.contains(":valueUris")) {
        queryObject.setParameterList("valueUris", valuesUri);
    }

    if (sqlQuery.contains(":taxonId")) {
        queryObject.setParameter("taxonId", taxon.getId());
    }

    EntityUtils.addUserAndGroupParameters(queryObject, this.getSessionFactory());
    return this.populateGenesAssociations(queryObject);

}

From source file:ubic.gemma.persistence.service.expression.designElement.CompositeSequenceDaoImpl.java

License:Apache License

@Override
public Map<CompositeSequence, Collection<Gene>> getGenes(Collection<CompositeSequence> compositeSequences) {
    Map<CompositeSequence, Collection<Gene>> returnVal = new HashMap<>();

    int BATCH_SIZE = 2000;

    if (compositeSequences.size() == 0)
        return returnVal;

    /*/*  ww w  .j av a  2s  . c om*/
     * Get the cs->gene mapping
     */
    final String nativeQuery = "SELECT CS, GENE FROM GENE2CS WHERE CS IN (:csids) ";

    for (CompositeSequence cs : compositeSequences) {
        returnVal.put(cs, new HashSet<Gene>());
    }

    List<Object> csGene = new ArrayList<>();
    Session session = this.getSessionFactory().getCurrentSession();
    org.hibernate.SQLQuery queryObject = session.createSQLQuery(nativeQuery);
    queryObject.addScalar("cs", new LongType());
    queryObject.addScalar("gene", new LongType());

    Collection<Long> csIdBatch = new HashSet<>();
    for (CompositeSequence cs : compositeSequences) {
        csIdBatch.add(cs.getId());

        if (csIdBatch.size() == BATCH_SIZE) {
            queryObject.setParameterList("csids", csIdBatch);
            csGene.addAll(queryObject.list());
            session.clear();
            csIdBatch.clear();
        }
    }

    if (csIdBatch.size() > 0) {
        queryObject.setParameterList("csids", csIdBatch);
        csGene.addAll(queryObject.list());
        session.clear();
    }

    StopWatch watch = new StopWatch();
    watch.start();

    int count = 0;
    Collection<Long> genesToFetch = new HashSet<>();
    Map<Long, Collection<Long>> cs2geneIds = new HashMap<>();

    for (Object object : csGene) {
        Object[] ar = (Object[]) object;
        Long cs = (Long) ar[0];
        Long gene = (Long) ar[1];
        if (!cs2geneIds.containsKey(cs)) {
            cs2geneIds.put(cs, new HashSet<Long>());
        }
        cs2geneIds.get(cs).add(gene);
        genesToFetch.add(gene);
    }

    // nothing found?
    if (genesToFetch.size() == 0) {
        returnVal.clear();
        return returnVal;
    }

    if (AbstractDao.log.isDebugEnabled())
        AbstractDao.log.debug("Built cs -> gene map in " + watch.getTime() + " ms; fetching "
                + genesToFetch.size() + " genes.");

    // fetch the genes
    Collection<Long> batch = new HashSet<>();
    Collection<Gene> genes = new HashSet<>();
    String geneQuery = "from Gene g where g.id in ( :gs )";

    org.hibernate.Query geneQueryObject = this.getSessionFactory().getCurrentSession().createQuery(geneQuery)
            .setFetchSize(1000);

    for (Long gene : genesToFetch) {
        batch.add(gene);
        if (batch.size() == BATCH_SIZE) {
            AbstractDao.log.debug("Processing batch ... ");
            geneQueryObject.setParameterList("gs", batch);
            //noinspection unchecked
            genes.addAll(geneQueryObject.list());
            batch.clear();
        }
    }

    if (batch.size() > 0) {
        geneQueryObject.setParameterList("gs", batch);
        //noinspection unchecked
        genes.addAll(geneQueryObject.list());
    }

    if (AbstractDao.log.isDebugEnabled())
        AbstractDao.log.debug("Got information on " + genes.size() + " genes in " + watch.getTime() + " ms");

    Map<Long, Gene> geneIdMap = new HashMap<>();
    for (Gene g : genes) {
        Hibernate.initialize(g);
        Long id = g.getId();
        geneIdMap.put(id, g);
    }

    // fill in the return value.
    for (CompositeSequence cs : compositeSequences) {
        Long csId = cs.getId();
        assert csId != null;
        Collection<Long> genesToAttach = cs2geneIds.get(csId);
        if (genesToAttach == null) {
            // this means there was no gene for that cs; we should remove it from the result
            returnVal.remove(cs);
            continue;
        }
        for (Long geneId : genesToAttach) {
            returnVal.get(cs).add(geneIdMap.get(geneId));
        }
        ++count;
    }

    if (AbstractDao.log.isDebugEnabled())
        AbstractDao.log.debug("Done, " + count + " result rows processed, " + returnVal.size() + "/"
                + compositeSequences.size() + " probes are associated with genes");
    return returnVal;
}