Example usage for com.mongodb QueryBuilder get

List of usage examples for com.mongodb QueryBuilder get

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder get.

Prototype

public DBObject get() 

Source Link

Document

Creates a DBObject query to be used for the driver's find operations

Usage

From source file:org.opencb.cellbase.lib.db.core.ConservationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllScoresByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<Integer> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);//from   w w w. j a v  a 2s.c  om
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            //                    integerChunkIds.add(chunkId);
            //                }
            //    //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(hunkIds);
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        /****/

        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());

        //            logger.debug(builder.get().toString());

    }
    List<QueryResult> queryResults = executeQueryList2(ids, queries, options);
    //        List<QueryResult> queryResults = executeQueryList(ids, queries, options);

    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        List<Document> list = (List<Document>) queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            Document chunk = list.get(j);

            if (!chunk.isEmpty()) {
                BasicDBList valuesChunk = (BasicDBList) chunk.get("values");
                if (valuesChunk != null) { // TODO: temporary patch to skip empty chunks - remove as soon as conservation is reloaded
                    String source = chunk.getString("source");
                    List<Float> valuesList;
                    if (!typeMap.containsKey(source)) {
                        valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                        for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                            valuesList.add(null);
                        }
                        typeMap.put(source, valuesList);
                    } else {
                        valuesList = typeMap.get(source);
                    }

                    valuesChunk = (BasicDBList) chunk.get("values");
                    int pos = 0;
                    if (region.getStart() > chunk.getInteger("start")) {
                        pos = region.getStart() - chunk.getInteger("start");
                    }

                    for (; pos < valuesChunk.size()
                            && (pos + chunk.getInteger("start") <= region.getEnd()); pos++) {
                        valuesList.set(pos + chunk.getInteger("start") - region.getStart(),
                                new Float((Double) valuesChunk.get(pos)));
                    }
                } else {
                    continue;
                }

            }

            BasicDBList resultList = new BasicDBList();
            for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
                for (Float value : elem.getValue()) {
                    if (value != null) {
                        resultList.add(new Score(new Double(value), elem.getKey()));
                    }
                }
            }
            if (!resultList.isEmpty()) {
                queryResult.setResult(resultList);
            } else {
                queryResult.setResult(null);
            }
        }
    }
    return queryResults;
}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<String> biotypes = options.getAsStringList("biotype");
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);//from  w w  w . ja va 2 s.  co m
        builder = builder.and("biotype").in(biotypeIds);
    }

    return executeQuery("result", new Document(builder.get().toMap()), options);
}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("transcripts.xrefs.id").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//  w  w  w.ja  va 2  s. c  o  m

    //        options = addExcludeReturnFields("transcripts", options);
    //        return executeQueryList(idList, queries, options);
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getStatsById(String id, QueryOptions options) {

    Map<String, Object> stats = new HashMap<>();
    QueryResult queryResult = new QueryResult();
    queryResult.setId(id);//  www. ja  v  a2  s. c o m

    QueryBuilder geneBuilder = QueryBuilder.start("transcripts.xrefs.id").is(id);
    long dbTimeStart = System.currentTimeMillis();
    QueryResult geneQueryResult = executeQuery(id, new Document(geneBuilder.get().toMap()), new QueryOptions());
    // TODO: clinical variant summary is only provided for ClinVar (hardcoded below)
    QueryOptions clinicalQueryOptions = new QueryOptions("source", "clinvar");
    clinicalQueryOptions.put("include",
            "annot.consequenceTypes.soTerms,clinvarSet.referenceClinVarAssertion.clinicalSignificance.description");
    QueryResult clinicalQueryResult = clinicalDBAdaptor.getByGeneId(id, clinicalQueryOptions);
    long dbTimeEnd = System.currentTimeMillis();
    queryResult.setDbTime(Long.valueOf(dbTimeEnd - dbTimeStart).intValue());

    if (geneQueryResult.getNumResults() > 0) {
        queryResult.setNumResults(1);
        stats = setCoreGeneStats(geneQueryResult, stats);
        stats = setVariantStats(clinicalQueryResult, stats);
        queryResult.setResult(Collections.singletonList(stats));
    }

    return queryResult;
    //        gene name
    //        ensembl gene id
    //        chr
    //        start
    //        end
    //        sequence length
    //        num transcripts
    //        breakdown num transcripts by biotype
    //        num exons
    //        num drug interactions
    //        Clinical Variants {
    //            #
    //            Breakdown by clinical significance
    //            Breakdown by SO
    //        }

    //        options = addExcludeReturnFields("transcripts", options);
    //        return executeQueryList(idList, queries, options);

}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    List<Document> queries = new ArrayList<>();

    List<Object> biotypes = options.getList("biotype", null);
    BasicDBList biotypeIds = new BasicDBList();
    if (biotypes != null && biotypes.size() > 0) {
        biotypeIds.addAll(biotypes);// w  ww.j a  v a2s  .com
    }

    List<String> ids = new ArrayList<>(regions.size());
    for (Region region : regions) {

        QueryBuilder builder = null;
        // If regions is 1 position then query can be optimize using chunks
        if (region.getStart() == region.getEnd()) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), geneChunkSize)).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        } else {
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }

        if (biotypeIds.size() > 0) {
            builder = builder.and("biotype").in(biotypeIds);
        }
        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());
    }
    return executeQueryList2(ids, queries, options);
    //        return executeQueryList(ids, queries, options);
}

From source file:org.opencb.cellbase.lib.db.core.GenomeMongoDBAdaptor.java

License:Apache License

@Deprecated
public QueryResult speciesInfoTmp(String id, QueryOptions options) {
    // reading application.properties file

    //        String[] speciesArray = applicationProperties.getProperty("SPECIES").split(",");

    //        List<Document> queries = new ArrayList<>(1);
    //        for (String id : idList) {
    QueryBuilder builder = QueryBuilder.start("species").is(id);

    //        queries.add(builder.get());
    //        }//from w  ww . j  a  v a2  s .  co  m

    //        options = addExcludeReturnFields("transcripts", options);
    return executeQuery(id, new Document(builder.get().toMap()), options);

}

From source file:org.opencb.cellbase.lib.db.core.GenomeMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllSequencesByRegionList(List<Region> regions, QueryOptions options) {
    /****//* www.j  av  a  2  s.  c  o m*/
    String chunkIdSuffix = this.chunkSize / 1000 + "k";
    /****/

    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<String> chunkIds;
    List<Integer> integerChunkIds;
    for (Region region : regions) {
        chunkIds = new ArrayList<>();
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            String chunkIdStr = region.getChromosome() + "_" + chunkId + "_" + chunkIdSuffix;
            chunkIds.add(chunkIdStr);
            integerChunkIds.add(chunkId);
        }
        //            QueryBuilder builder = QueryBuilder.start("sequenceName").is(region.getChromosomeInfo()).and("_chunkIds").in(chunkIds);
        QueryBuilder builder = QueryBuilder.start("_chunkIds").in(chunkIds);
        /****/
        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());

        logger.info(new Document(builder.get().toMap()).toString());
    }

    List<QueryResult> queryResults = executeQueryList2(ids, queries, options, genomeSequenceCollection);
    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);

        List list = queryResult.getResult();
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < list.size(); j++) {
            Document chunk = (Document) list.get(j);
            sb.append(chunk.get("sequence"));
        }

        int startStr = getOffset(region.getStart());
        int endStr = getOffset(region.getStart()) + (region.getEnd() - region.getStart()) + 1;

        String subStr = "";

        if (getChunkId(region.getStart(), this.chunkSize) > 0) {
            if (sb.toString().length() > 0 && sb.toString().length() >= endStr) {
                subStr = sb.toString().substring(startStr, endStr);
            }
        } else {
            if (sb.toString().length() > 0 && sb.toString().length() + 1 >= endStr) {
                subStr = sb.toString().substring(startStr - 1, endStr - 1);
            }
        }
        logger.info("((Document)list.get(0)).getString(\"sequenceType\") = {}",
                ((Document) list.get(0)).getString("sequenceType"));
        logger.info("((Document)list.get(0)).getString(\"assembly\") = {}",
                ((Document) list.get(0)).getString("assembly"));
        GenomeSequenceFeature genomeSequenceFeature = new GenomeSequenceFeature(region.getChromosome(),
                region.getStart(), region.getEnd(), 1, ((Document) list.get(0)).getString("sequenceType"),
                ((Document) list.get(0)).getString("assembly"), subStr);

        queryResult.setResult(Arrays.asList(genomeSequenceFeature));
    }

    return queryResults;
}

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("name").is(id);
        queries.add(new Document(builder.get().toMap()));
    }/*ww  w  . ja v a2 s  .c o  m*/

    //        options = addExcludeReturnFields("transcripts", options);
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByXrefList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("dbReference.id").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//from  w ww .j a  v a  2  s. co  m

    //        options = addExcludeReturnFields("transcripts", options);
    //        return executeQueryList(idList, queries, options);
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllFunctionPredictionByEnsemblTranscriptIdList(List<String> transcriptIdList,
        QueryOptions options) {//from  w  w  w . jav  a  2 s  .  c  o m
    List<Document> queries = new ArrayList<>(transcriptIdList.size());

    if (options.containsKey("aaPosition")) {
        if (options.containsKey("aaChange")) {
            addIncludeReturnFields(
                    "aaPositions." + options.getString("aaPosition") + "." + options.getString("aaChange"),
                    options);
        } else {
            addIncludeReturnFields("aaPositions." + options.getString("aaPosition"), options);
        }
    }

    for (String id : transcriptIdList) {
        QueryBuilder builder = QueryBuilder.start("transcriptId").is(id);
        queries.add(new Document(builder.get().toMap()));
    }

    //        options = addExcludeReturnFields("transcripts", options);
    return executeQueryList2(transcriptIdList, queries, options, proteinFunctionalPredictionCollection);
}