Example usage for com.mongodb QueryBuilder and

List of usage examples for com.mongodb QueryBuilder and

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public QueryBuilder and(final DBObject... ands) 

Source Link

Document

Equivalent to an $and operand

Usage

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 v  a2s.  c  o 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> 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);//from   w  w w  .  j av  a2  s  . c o m
    }

    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.regulatory.RegulatoryRegionMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByPositionList(List<Position> positionList, QueryOptions options) {
    //  db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156})

    String featureType = options.getString("featureType", null);
    String featureClass = options.getString("featureClass", null);

    List<Document> queries = new ArrayList<>();
    for (Position position : positionList) {
        String chunkId = position.getChromosome() + "_"
                + getChunkId(position.getPosition(), regulatoryRegionChunkSize) + "_"
                + regulatoryRegionChunkSize / 1000 + "k";
        BasicDBList chunksId = new BasicDBList();
        chunksId.add(chunkId);// w w  w.  j  a  v a2 s. c  o m
        QueryBuilder builder = QueryBuilder.start("_chunkIds").in(chunksId).and("start")
                .is(position.getPosition());
        if (featureType != null) {
            builder.and("featureType").is(featureType);
        }
        if (featureClass != null) {
            builder.and("featureClass").is(featureClass);
        }

        //        System.out.println("Query: " + new Document(builder.get().toMap()));
        queries.add(new Document(builder.get().toMap()));
    }

    System.out.println("Query: " + queries);

    //        options = addExcludeReturnFields("chunkIds", options);
    return executeQueryList2(positionList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.regulatory.RegulatoryRegionMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regionList, QueryOptions options) {
    //  db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156})
    QueryBuilder builder = new QueryBuilder();

    List<Object> featureType = options.getAsList("featureType");
    List<Object> featureClass = options.getAsList("featureClass");

    //        options = addExcludeReturnFields("chunkIds", options);

    List<Document> queries = new ArrayList<>();
    for (Region region : regionList) {
        int firstChunkId = getChunkId(region.getStart(), regulatoryRegionChunkSize);
        int lastChunkId = getChunkId(region.getEnd(), regulatoryRegionChunkSize);
        BasicDBList chunksId = new BasicDBList();
        for (int j = firstChunkId; j <= lastChunkId; j++) {
            String chunkId = region.getChromosome() + "_" + j + "_" + regulatoryRegionChunkSize / 1000 + "k";
            chunksId.add(chunkId);//from w ww .jav  a2 s  .  c  om
        }

        //            logger.info(chunksId.toString());

        builder = builder.start("_chunkIds").in(chunksId).and("start").lessThanEquals(region.getEnd())
                .and("end").greaterThanEquals(region.getStart());

        if (featureType != null && featureType.size() > 0) {
            BasicDBList featureTypeDBList = new BasicDBList();
            featureTypeDBList.addAll(featureType);
            builder = builder.and("featureType").in(featureTypeDBList);
        }

        if (featureClass != null && featureClass.size() > 0) {
            BasicDBList featureClassDBList = new BasicDBList();
            featureClassDBList.addAll(featureClass);
            builder = builder.and("featureClass").in(featureClassDBList);
        }

        queries.add(new Document(builder.get().toMap()));
    }
    //        System.out.println(">>"+regionList);
    //        System.out.println(">>"+new Document(builder.get().toMap()).toString());
    return executeQueryList2(regionList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.regulatory.RegulatoryRegionMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult next(String chromosome, int position, QueryOptions options) {

    String featureType = options.getString("featureType", null);
    String featureClass = options.getString("featureClass", null);

    BasicDBList chunksId = new BasicDBList();
    String chunkId = chromosome + "_" + getChunkId(position, regulatoryRegionChunkSize) + "_"
            + regulatoryRegionChunkSize / 1000 + "k";
    chunksId.add(chunkId);/*  ww  w .j a  v a2s.c o  m*/

    // TODO: Add query to find next item considering next chunk
    // db.regulatory_region.find({ "chromosome" : "19" , "start" : { "$gt" : 62005} , "featureType"
    // : "TF_binding_site_motif"}).sort({start:1}).limit(1)

    QueryBuilder builder;
    if (options.getString("strand") == null
            || (options.getString("strand").equals("1") || options.getString("strand").equals("+"))) {
        // db.core.find({chromosome: "1", start: {$gt: 1000000}}).sort({start: 1}).limit(1)
        builder = QueryBuilder.start("_chunkIds").in(chunksId).and("chromosome").is(chromosome).and("start")
                .greaterThan(position);
        options.put("sort", new Document("start", 1));
        options.put("limit", 1);
    } else {
        builder = QueryBuilder.start("_chunkIds").in(chunksId).and("chromosome").is(chromosome).and("end")
                .lessThan(position);
        options.put("sort", new Document("end", -1));
        options.put("limit", 1);
    }

    if (featureType != null) {
        builder.and("featureType").is(featureType);
    }
    if (featureClass != null) {
        builder.and("featureClass").is(featureClass);
    }
    System.out.println(new Document(builder.get().toMap()));
    return executeQuery("result", new Document(builder.get().toMap()), options);
}

From source file:org.opencb.cellbase.lib.db.variation.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());
        }/*w  w w. j a va 2s.  c om*/
        queries.add(new Document(builder.get().toMap()));
        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.db.variation.MutationMongoDBAdaptor.java

License:Apache License

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

    List<Object> biotypes = options.getList("disease", null);
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);//www  .  j  a  v  a  2s.c  o m
        builder = builder.and("primaryHistology").in(biotypeIds);
    }

    return executeQuery("result", new Document(builder.get().toMap()), 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  w w w  .  j av  a 2  s .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());
        }//from   w w  w  .j av  a  2  s. 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());
        }/* w w  w .  j av  a 2 s . co  m*/

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

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

    return results;
}