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.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());
        }//  www . ja  v  a  2s.c om
        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.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

private QueryBuilder addClinvarPhenotypeFilter(QueryBuilder builder, QueryOptions options) {
    List<Object> phenotypeList = options.getList("phenotype", null);
    if (phenotypeList != null && phenotypeList.size() > 0) {
        QueryBuilder phenotypeQueryBuilder = QueryBuilder.start();
        for (Object phenotype : phenotypeList) {
            String phenotypeString = (String) phenotype;
            phenotypeQueryBuilder = phenotypeQueryBuilder
                    .or(QueryBuilder.start("referenceClinVarAssertion.traitSet.trait.name.elementValue.value")
                            .text(phenotypeString).get());
        }//from w w  w  .j a va  2  s .  com
        builder = builder.and(phenotypeQueryBuilder.get());
    }
    return builder;
}

From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

private QueryBuilder addClinvarRegionFilter(QueryBuilder builder, QueryOptions options) {
    List<Object> regions = options.getList("region", null);
    BasicDBList regionList = new BasicDBList();
    if (regions != null) {
        Region region = (Region) regions.get(0);
        QueryBuilder regionQueryBuilder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        for (int i = 1; i < regions.size(); i++) {
            region = (Region) regions.get(i);
            regionQueryBuilder = regionQueryBuilder.or(QueryBuilder.start("chromosome")
                    .is(region.getChromosome()).and("end").greaterThanEquals(region.getStart()).and("start")
                    .lessThanEquals(region.getEnd()).get());
        }//from  w ww  . j  av a2 s.c o m
        builder = builder.and(regionQueryBuilder.get());
    }
    return builder;
}

From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

private QueryBuilder addClinvarIdFilter(QueryBuilder builder, QueryOptions options) {
    List<Object> idList = options.getList("id", null);
    if (idList != null && idList.size() > 0) {
        QueryBuilder idQueryBuilder = QueryBuilder.start();
        for (Object id : idList) {
            String idString = (String) id;
            if (idString.toLowerCase().startsWith("rs")) {
                idQueryBuilder = idQueryBuilder.or(QueryBuilder.start(
                        "clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.type")
                        .is("rs")
                        .and("clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.id")
                        .is(Integer.valueOf(idString.substring(2))).get());
            } else if (idString.toLowerCase().startsWith("rcv")) {
                idQueryBuilder = idQueryBuilder.or(QueryBuilder
                        .start("clinvarList.clinvarSet.referenceClinVarAssertion.clinVarAccession.acc")
                        .is(idString).get());
            }//from w  w w  .  j  a  va 2s  .  c o m
        }
        builder = builder.and(idQueryBuilder.get());
    }
    return builder;
}

From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

private QueryBuilder addClinvarGeneFilter(QueryBuilder builder, QueryOptions options) {
    List<Object> genes = options.getList("gene", null);
    BasicDBList geneSymbols = new BasicDBList();
    if (genes != null && genes.size() > 0) {
        geneSymbols.addAll(genes);//from  w  ww .  j  a  va  2s . com
        builder = builder.and(
                "clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.measureRelationship.symbol.elementValue.value")
                .in(geneSymbols);
    }
    return builder;
}

From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByGenomicVariantList(List<GenomicVariant> variantList, QueryOptions options) {
    List<DBObject> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(variantList.size());
    List<QueryResult> queryResultList;
    for (GenomicVariant genomicVariant : variantList) {
        QueryBuilder builder = QueryBuilder.start("chromosome").is(genomicVariant.getChromosome()).and("start")
                .is(genomicVariant.getPosition()).and("alternate").is(genomicVariant.getAlternative());
        if (genomicVariant.getReference() != null) {
            builder = builder.and("reference").is(genomicVariant.getReference());
        }/*ww  w. j  ava2s. co m*/
        queries.add(builder.get());
        ids.add(genomicVariant.toString());
    }

    queryResultList = executeQueryList2(ids, queries, options);

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

        List<Cosmic> cosmicList = null;
        List<Gwas> gwasList = null;
        List<Clinvar> clinvarList = null;

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

            if (isCosmic(clinical)) {
                Cosmic cosmic = getCosmic(clinical);
                if (cosmicList == null) {
                    cosmicList = new ArrayList<>();
                }
                cosmicList.add(cosmic);
            } else if (isGwas(clinical)) {
                Gwas gwas = getGwas(clinical);
                if (gwasList == null) {
                    gwasList = new ArrayList<>();
                }
                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<>();
        clinicalData.put("Cosmic", cosmicList);
        clinicalData.put("Gwas", gwasList);
        clinicalData.put("Clinvar", clinvarList);
        // FIXME quick solution to compile
        //            queryResult.setResult(clinicalData);
        queryResult.setResult(Arrays.asList(clinicalData));
        queryResult.setNumResults(1);
    }

    return queryResultList;
}

From source file:org.opencb.cellbase.mongodb.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);/* w  ww. j  a  v  a 2s .  c  o  m*/
        builder = builder.and("biotype").in(biotypeIds);
    }

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

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

License:Apache License

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

    List<Object> biotypes = options.getList("biotype", null);
    BasicDBList biotypeIds = new BasicDBList();
    if (biotypes != null && biotypes.size() > 0) {
        biotypeIds.addAll(biotypes);//  www.  ja  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(builder.get());
        ids.add(region.toString());
    }
    return executeQueryList2(ids, queries, options);
    //        return executeQueryList(ids, queries, options);
}

From source file:org.opencb.cellbase.mongodb.db.GeneMongoDBAdaptor.java

License:Apache License

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

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

    //      options = addExcludeReturnFields("transcripts", options);
    return executeQuery("result", builder.get(), options);
}

From source file:org.opencb.cellbase.mongodb.db.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);/* ww w  .j  a  va2 s .com*/
        builder = builder.and("primaryHistology").in(biotypeIds);
    }

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