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.variation.MutationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByProteinIdList(List<String> proteinIdList, QueryOptions options) {
    List<Document> queries = new ArrayList<>();
    for (String id : proteinIdList) {
        QueryBuilder builder = QueryBuilder.start("protein").is(id);
        queries.add(new Document(builder.get().toMap()));
    }/*from   ww w.  j a v  a2  s  . c om*/

    return executeQueryList2(proteinIdList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.MutationMongoDBAdaptor.java

License:Apache License

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

    List<String> ids = new ArrayList<>(regions.size());
    for (Region region : regions) {
        QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("start")
                .greaterThanEquals(region.getStart()).lessThanEquals(region.getEnd());
        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());/* ww  w  . j  ava 2 s .  c o m*/
    }

    return executeQueryList2(ids, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.VariantFunctionalScoreMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getByVariant(String chromosome, int position, String reference, String alternate,
        QueryOptions queryOptions) {//from w w w.j a va  2 s  .  com
    String chunkId = getChunkIdPrefix(chromosome, position,
            MongoDBCollectionConfiguration.VARIATION_FUNCTIONAL_SCORE_CHUNK_SIZE);
    QueryBuilder builder = QueryBuilder.start("_chunkIds").is(chunkId);
    //                .and("chromosome").is(chromosome)
    //                .and("start").is(position);
    //        System.out.println(chunkId);
    QueryResult result = executeQuery(chromosome + "_" + position + "_" + reference + "_" + alternate,
            new Document(builder.get().toMap()), queryOptions, mongoDBCollection);

    //        System.out.println("result = " + result);

    int offset = (position % MongoDBCollectionConfiguration.VARIATION_FUNCTIONAL_SCORE_CHUNK_SIZE) - 1;
    List<Score> scores = new ArrayList<>();
    for (Object object : result.getResult()) {
        //            System.out.println("object = " + object);
        Document dbObject = (Document) object;
        BasicDBList basicDBList = (BasicDBList) dbObject.get("values");
        Long l1 = (Long) basicDBList.get(offset);
        //            System.out.println("l1 = " + l1);
        if (dbObject.getString("source").equalsIgnoreCase("cadd_raw")) {
            float value = 0f;
            switch (alternate.toLowerCase()) {
            case "a":
                value = ((short) (l1 >> 48) - 10000) / DECIMAL_RESOLUTION;
                break;
            case "c":
                value = ((short) (l1 >> 32) - 10000) / DECIMAL_RESOLUTION;
                break;
            case "g":
                value = ((short) (l1 >> 16) - 10000) / DECIMAL_RESOLUTION;
                break;
            case "t":
                value = ((short) (l1 >> 0) - 10000) / DECIMAL_RESOLUTION;
                break;
            default:
                break;
            }
            scores.add(Score.newBuilder().setScore(value).setSource(dbObject.getString("source"))
                    .setDescription(null)
                    //                        .setDescription("")
                    .build());
        }

        if (dbObject.getString("source").equalsIgnoreCase("cadd_scaled")) {
            float value = 0f;
            switch (alternate.toLowerCase()) {
            case "a":
                value = ((short) (l1 >> 48)) / DECIMAL_RESOLUTION;
                break;
            case "c":
                value = ((short) (l1 >> 32)) / DECIMAL_RESOLUTION;
                break;
            case "g":
                value = ((short) (l1 >> 16)) / DECIMAL_RESOLUTION;
                break;
            case "t":
                value = ((short) (l1 >> 0)) / DECIMAL_RESOLUTION;
                break;
            default:
                break;
            }
            scores.add(Score.newBuilder().setScore(value).setSource(dbObject.getString("source"))
                    .setDescription(null)
                    //                        .setDescription("")
                    .build());
        }
    }

    result.setResult(scores);
    return result;
}

From source file:org.opencb.cellbase.lib.db.variation.VariationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>();
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("ids").is(id);
        queries.add(new Document(builder.get().toMap()));
    }/*from  w  w  w.  j  a  va2 s. com*/
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.VariationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByTranscriptIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>();
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("annotation.consequenceTypes.ensemblTranscriptId").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//from  ww  w. j  a v a 2s. c o m
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.VariationMongoDBAdaptor.java

License:Apache License

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

    // TODO: do not allow phenotype queries on the variation collection.
    // TODO: phenotype queries shall be answered by the clinicalMongoDBAdaptor
    String phenotype = options.getString("phenotype");
    if (phenotype != null && !phenotype.equals("")) {
        for (Region region : regions) {
            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("start")
                    .greaterThanEquals(region.getStart()).lessThanEquals(region.getEnd());
            builder = builder.and("phenotype").is(phenotype);
            queries.add(new Document(builder.get().toMap()));
            ids.add(region.toString());//from   ww  w  . ja v  a2s. c  o  m
        }
        return executeQueryList2(ids, queries, options, mongoVariationPhenotypeDBCollection2);
    } else {
        String consequenceTypes = options.getString("consequence_type", null);
        BasicDBList consequenceTypeDBList = new BasicDBList();
        if (consequenceTypes != null && !consequenceTypes.equals("")) {
            for (String ct : consequenceTypes.split(",")) {
                consequenceTypeDBList.add(ct);
            }
        }

        for (Region region : regions) {
            // QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getSequenceName())
            // .and("end").greaterThan(region.getStart()).and("start").lessThan(region.getEnd());
            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("start")
                    .greaterThanEquals(region.getStart()).lessThanEquals(region.getEnd());
            if (consequenceTypeDBList.size() > 0) {
                builder = builder.and("annotation.consequenceTypes.sequenceOntologyTerms.name")
                        .in(consequenceTypeDBList);
            }
            queries.add(new Document(builder.get().toMap()));
            ids.add(region.toString());
        }

        return executeQueryList2(ids, queries, options);
    }
}

From source file:org.opencb.cellbase.lib.db.variation.VariationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getIdByVariantList(List<Variant> variations, QueryOptions options) {
    List<Document> queries = new ArrayList<>(variations.size());
    List<QueryResult> results;

    for (Variant variation : variations) {
        String chunkId = getChunkIdPrefix(variation.getChromosome(), variation.getStart(), variationChunkSize);
        QueryBuilder builder = QueryBuilder.start("_chunkIds").is(chunkId).and("chromosome")
                .is(variation.getChromosome()).and("start").is(variation.getStart()).and("alternate")
                .is(variation.getAlternate());
        if (variation.getReference() != null) {
            builder = builder.and("reference").is(variation.getReference());
        }//w  w w.  j  a  v  a 2s. c  o  m

        queries.add(new Document(builder.get().toMap()));
    }

    results = executeQueryList2(variations, queries, options, mongoDBCollection);

    for (QueryResult result : results) {
        List<String> idList = new LinkedList();

        BasicDBList idListObject = (BasicDBList) result.getResult();
        for (Object idObject : idListObject) {
            Document variantObject = (Document) idObject;
            // Arbitrarily selects the first one. Assuming variants in variation collection will just have one id
            idList.add(((BasicDBList) variantObject.get("ids")).get(0).toString());
        }

        //            result.setResult(Joiner.on(",").skipNulls().join(idList));
        result.setResult(idList);
    }

    return results;
}

From source file:org.opencb.cellbase.lib.db.variation.VariationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByVariantList(List<Variant> variations, QueryOptions options) {
    List<Document> queries = new ArrayList<>(variations.size());
    List<QueryResult> results;

    for (Variant variation : variations) {
        String chunkId = getChunkIdPrefix(variation.getChromosome(), variation.getStart(), variationChunkSize);

        QueryBuilder builder = QueryBuilder.start("_chunkIds").is(chunkId).and("chromosome")
                .is(variation.getChromosome()).and("start").is(variation.getStart()).and("alternate")
                .is(variation.getAlternate());

        if (variation.getReference() != null) {
            builder = builder.and("reference").is(variation.getReference());
        }//from   w ww.j  a  v a 2  s  . c  o m

        queries.add(new Document(builder.get().toMap()));
    }

    results = executeQueryList2(variations, queries, options, mongoDBCollection);
    //        results = executeQueryList(variations, queries, options, mongoDBCollection);

    return results;
}

From source file:org.opencb.cellbase.lib.impl.ClinicalMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByGenomicVariantList(List<Variant> variantList, QueryOptions options) {
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(variantList.size());
    List<QueryResult> queryResultList;
    for (Variant genomicVariant : variantList) {
        QueryBuilder builder = QueryBuilder.start("chromosome").is(genomicVariant.getChromosome()).and("start")
                .is(genomicVariant.getStart()).and("alternate").is(genomicVariant.getAlternate());
        if (genomicVariant.getReference() != null) {
            builder = builder.and("reference").is(genomicVariant.getReference());
        }//from   w ww  .j av a  2 s.  c o  m
        queries.add(new Document(builder.get().toMap()));
        logger.debug(new Document(builder.get().toMap()).toJson());
        ids.add(genomicVariant.toString());
    }

    queryResultList = executeQueryList2(ids, queries, options);

    for (QueryResult queryResult : queryResultList) {
        List<Document> clinicalList = (List<Document>) queryResult.getResult();

        List<Cosmic> cosmicList = new ArrayList<>();
        List<Gwas> gwasList = new ArrayList<>();
        List<ClinVar> clinvarList = new ArrayList<>();

        for (Object clinicalObject : clinicalList) {
            Document clinical = (Document) clinicalObject;

            if (isCosmic(clinical)) {
                Cosmic cosmic = getCosmic(clinical);
                cosmicList.add(cosmic);
            } else if (isGwas(clinical)) {
                Gwas gwas = getGwas(clinical);
                gwasList.add(gwas);

            } else if (isClinvar(clinical)) {
                ClinVar clinvar = getClinvar(clinical);
                //                    if (clinvarList == null) {
                //                        clinvarList = new ArrayList<>();
                //                    }
                clinvarList.add(clinvar);
            }
        }
        //            Map<String, Object> clinicalData = new HashMap<>();
        //            if(cosmicList!=null && cosmicList.size()>0) {
        //                clinicalData.put("cosmic", cosmicList);
        //            }
        //            if(gwasList!=null && gwasList.size()>0) {
        //                clinicalData.put("gwas", gwasList);
        //            }
        //            if(clinvarList!=null && clinvarList.size()>0) {
        //                clinicalData.put("clinvar", clinvarList);
        //            }
        VariantTraitAssociation variantTraitAssociation = new VariantTraitAssociation(clinvarList, gwasList,
                cosmicList);
        if (!(variantTraitAssociation.getCosmic().isEmpty() && variantTraitAssociation.getGwas().isEmpty()
                && variantTraitAssociation.getClinvar().isEmpty())) {

            // FIXME quick solution to compile
            // queryResult.setResult(clinicalData);
            queryResult.setResult(Collections.singletonList(variantTraitAssociation));
            queryResult.setNumResults(variantTraitAssociation.getCosmic().size()
                    + variantTraitAssociation.getGwas().size() + variantTraitAssociation.getClinvar().size());
        } else {
            queryResult.setResult(null);
            queryResult.setNumResults(0);
        }
    }

    return queryResultList;
}

From source file:org.opencb.cellbase.lib.impl.ConservationMongoDBAdaptor.java

License:Apache License

public List<QueryResult> getAllByRegionList(List regionList, QueryOptions options) {
    //TODO not finished yet
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regionList.size());
    List<String> integerChunkIds;

    List<Region> regions = regionList;
    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 a2 s. c  o  m
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        // Max region size is 10000bp
        if (region.getEnd() - region.getStart() > 10000) {
            region.setEnd(region.getStart() + 10000);
        }

        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(),
                MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE);
        int regionChunkEnd = getChunkId(region.getEnd(),
                MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds").is(getChunkIdPrefix(region.getChromosome(),
                    region.getStart(), MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            ////                    integerChunkIds.add(chunkId);
            //                    integerChunkIds.add(region.getChromosomeInfo() + "_" + chunkId + "_" + this.chunkSize/1000 + "k");
            //                }
            //                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());
        }
        //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(hunkIds);
        /****/

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

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

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

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

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            Document chunk = (Document) list.get(j);
            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);
            }

            //                BasicDBList valuesChunk = (BasicDBList) chunk.get("values");
            ArrayList valuesChunk = chunk.get("values", ArrayList.class);

            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++) {
                //                    System.out.println("valuesList SIZE = " + valuesList.size());
                //                    System.out.println("pos = " + pos);
                //                    System.out.println("DIV " + (chunk.getInt("start") - region.getStart()));
                //                    System.out.println("valuesChunk = " + valuesChunk.get(pos));
                //                    System.out.println("indexFinal = " + (pos + chunk.getInt("start") - region.getStart()));
                valuesList.set(pos + chunk.getInteger("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }
        //
        BasicDBList resultList = new BasicDBList();
        //            ConservationScoreRegion conservedRegionChunk;
        GenomicScoreRegion<Float> conservedRegionChunk;
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            //                conservedRegionChunk = new ConservationScoreRegion(region.getChromosome(), region.getStart(),
            //                        region.getEnd(), elem.getKey(), elem.getValue());
            conservedRegionChunk = new GenomicScoreRegion<>(region.getChromosome(), region.getStart(),
                    region.getEnd(), elem.getKey(), elem.getValue());
            resultList.add(conservedRegionChunk);
        }
        queryResult.setResult(resultList);
    }

    return queryResults;
}