Example usage for com.mongodb QueryBuilder exists

List of usage examples for com.mongodb QueryBuilder exists

Introduction

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

Prototype

public QueryBuilder exists(final Object object) 

Source Link

Document

Equivalent of the $exists operand

Usage

From source file:com.gigaspaces.persistency.parser.SQL2MongoBaseVisitor.java

License:Open Source License

private void buildIsExpression(String val, QueryBuilder subQuery) {

    int index = val.indexOf("NOT");

    subQuery.exists(!(index > -1));
}

From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java

License:Apache License

private void parseAnnotationQueryParams(Query query, QueryBuilder builder) {
    if (query != null) {
        if (isValidParam(query, VariantQueryParams.ANNOTATION_EXISTS)) {
            builder.and(DocumentToVariantConverter.ANNOTATION_FIELD + "."
                    + DocumentToVariantAnnotationConverter.ANNOT_ID_FIELD);
            builder.exists(query.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key()));
        }//w w w  . j a  v  a 2s .c  om

        if (isValidParam(query, VariantQueryParams.ANNOT_CONSEQUENCE_TYPE)) {
            String value = query.getString(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key());
            addQueryFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.CT_SO_ACCESSION_FIELD,
                    value, builder, QueryOperation.AND, VariantDBAdaptorUtils::parseConsequenceType);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_BIOTYPE)) {
            String biotypes = query.getString(VariantQueryParams.ANNOT_BIOTYPE.key());
            addQueryStringFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DocumentToVariantAnnotationConverter.CT_BIOTYPE_FIELD,
                    biotypes, builder, QueryOperation.AND);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_POLYPHEN)) {
            String value = query.getString(VariantQueryParams.ANNOT_POLYPHEN.key());
            //                addCompListQueryFilter(DocumentToVariantConverter.ANNOTATION_FIELD
            //                                + "." + DocumentToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD
            //                                + "." + DocumentToVariantAnnotationConverter.CT_PROTEIN_POLYPHEN_FIELD
            //                                + "." + DocumentToVariantAnnotationConverter.SCORE_SCORE_FIELD,
            //                        value, builder);
            addScoreFilter(value, builder, VariantQueryParams.ANNOT_POLYPHEN,
                    DocumentToVariantAnnotationConverter.POLYPHEN, true);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_SIFT)) {
            String value = query.getString(VariantQueryParams.ANNOT_SIFT.key());
            //                addCompListQueryFilter(DocumentToVariantConverter.ANNOTATION_FIELD
            //                        + "." + DocumentToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD
            //                        + "." + DocumentToVariantAnnotationConverter.CT_PROTEIN_SIFT_FIELD + "."
            //                        + DocumentToVariantAnnotationConverter.SCORE_SCORE_FIELD, value, builder);
            addScoreFilter(value, builder, VariantQueryParams.ANNOT_SIFT,
                    DocumentToVariantAnnotationConverter.SIFT, true);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_PROTEIN_SUBSTITUTION)) {
            String value = query.getString(VariantQueryParams.ANNOT_PROTEIN_SUBSTITUTION.key());
            addScoreFilter(value, builder, VariantQueryParams.ANNOT_PROTEIN_SUBSTITUTION, true);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_CONSERVATION)) {
            String value = query.getString(VariantQueryParams.ANNOT_CONSERVATION.key());
            addScoreFilter(value, builder, VariantQueryParams.ANNOT_CONSERVATION, false);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_TRANSCRIPTION_FLAGS)) {
            String value = query.getString(VariantQueryParams.ANNOT_TRANSCRIPTION_FLAGS.key());
            addQueryStringFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DocumentToVariantAnnotationConverter.CT_TRANSCRIPT_ANNOT_FLAGS,
                    value, builder, QueryOperation.AND);
        }

        //            QueryBuilder geneTraitBuilder = QueryBuilder.start();
        if (isValidParam(query, VariantQueryParams.ANNOT_GENE_TRAITS_ID)) {
            String value = query.getString(VariantQueryParams.ANNOT_GENE_TRAITS_ID.key());
            addQueryStringFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.GENE_TRAIT_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.GENE_TRAIT_ID_FIELD,
                    value, builder, QueryOperation.AND);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_GENE_TRAITS_NAME)) {
            String value = query.getString(VariantQueryParams.ANNOT_GENE_TRAITS_NAME.key());
            addCompQueryFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.GENE_TRAIT_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.GENE_TRAIT_NAME_FIELD,
                    value, builder, false);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_HPO)) {
            String value = query.getString(VariantQueryParams.ANNOT_HPO.key());
            //                addQueryStringFilter(DocumentToVariantAnnotationConverter.GENE_TRAIT_HPO_FIELD, value, geneTraitBuilder,
            //                        QueryOperation.AND);
            addQueryStringFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.XREFS_FIELD + '.'
                            + DocumentToVariantAnnotationConverter.XREF_ID_FIELD,
                    value, builder, QueryOperation.AND);
        }

        //            DBObject geneTraitQuery = geneTraitBuilder.get();
        //            if (geneTraitQuery.keySet().size() != 0) {
        //                builder.and(DocumentToVariantConverter.ANNOTATION_FIELD
        //                        + "." + DocumentToVariantAnnotationConverter.GENE_TRAIT_FIELD).elemMatch(geneTraitQuery);
        //            }

        if (isValidParam(query, VariantQueryParams.ANNOT_GO)) {
            String value = query.getString(VariantQueryParams.ANNOT_GO.key());

            // Check if comma separated of semi colon separated (AND or OR)
            QueryOperation queryOperation = checkOperator(value);
            // Split by comma or semi colon
            List<String> goValues = splitValue(value, queryOperation);

            if (queryOperation == QueryOperation.AND) {
                throw VariantQueryException.malformedParam(VariantQueryParams.ANNOT_GO, value,
                        "Unimplemented AND operator");
            }

            Set<String> genes = utils.getGenesByGo(goValues);

            builder.and(DocumentToVariantConverter.ANNOTATION_FIELD + "."
                    + DocumentToVariantAnnotationConverter.XREFS_FIELD + "."
                    + DocumentToVariantAnnotationConverter.XREF_ID_FIELD).in(genes);

        }

        if (isValidParam(query, VariantQueryParams.ANNOT_EXPRESSION)) {
            String value = query.getString(VariantQueryParams.ANNOT_EXPRESSION.key());

            // Check if comma separated of semi colon separated (AND or OR)
            QueryOperation queryOperation = checkOperator(value);
            // Split by comma or semi colon
            List<String> expressionValues = splitValue(value, queryOperation);

            if (queryOperation == QueryOperation.AND) {
                throw VariantQueryException.malformedParam(VariantQueryParams.ANNOT_EXPRESSION, value,
                        "Unimplemented AND operator");
            }

            Set<String> genes = utils.getGenesByExpression(expressionValues);

            builder.and(DocumentToVariantConverter.ANNOTATION_FIELD + "."
                    + DocumentToVariantAnnotationConverter.XREFS_FIELD + "."
                    + DocumentToVariantAnnotationConverter.XREF_ID_FIELD).in(genes);

        }

        if (isValidParam(query, VariantQueryParams.ANNOT_PROTEIN_KEYWORDS)) {
            String value = query.getString(VariantQueryParams.ANNOT_PROTEIN_KEYWORDS.key());
            addQueryStringFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DocumentToVariantAnnotationConverter.CT_PROTEIN_KEYWORDS,
                    value, builder, QueryOperation.AND);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_DRUG)) {
            String value = query.getString(VariantQueryParams.ANNOT_DRUG.key());
            addQueryStringFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.DRUG_FIELD + "."
                            + DocumentToVariantAnnotationConverter.DRUG_NAME_FIELD,
                    value, builder, QueryOperation.AND);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_FUNCTIONAL_SCORE)) {
            String value = query.getString(VariantQueryParams.ANNOT_FUNCTIONAL_SCORE.key());
            addScoreFilter(value, builder, VariantQueryParams.ANNOT_FUNCTIONAL_SCORE, false);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_CUSTOM)) {
            String value = query.getString(VariantQueryParams.ANNOT_CUSTOM.key());
            addCompListQueryFilter(DocumentToVariantConverter.CUSTOM_ANNOTATION_FIELD, value, builder, true);
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_POPULATION_ALTERNATE_FREQUENCY)) {
            String value = query.getString(VariantQueryParams.ANNOT_POPULATION_ALTERNATE_FREQUENCY.key());
            addFrequencyFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, value,
                    builder, VariantQueryParams.ANNOT_POPULATION_ALTERNATE_FREQUENCY); // Same
            // method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field
            // (reference/alternate) where to check the frequency
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_POPULATION_REFERENCE_FREQUENCY)) {
            String value = query.getString(VariantQueryParams.ANNOT_POPULATION_REFERENCE_FREQUENCY.key());
            addFrequencyFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, value,
                    builder, VariantQueryParams.ANNOT_POPULATION_REFERENCE_FREQUENCY); // Same
            // method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field
            // (reference/alternate) where to check the frequency
        }

        if (isValidParam(query, VariantQueryParams.ANNOT_POPULATION_MINOR_ALLELE_FREQUENCY)) {
            String value = query.getString(VariantQueryParams.ANNOT_POPULATION_MINOR_ALLELE_FREQUENCY.key());
            addFrequencyFilter(
                    DocumentToVariantConverter.ANNOTATION_FIELD + "."
                            + DocumentToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    value, builder, VariantQueryParams.ANNOT_POPULATION_MINOR_ALLELE_FREQUENCY,
                    (v, queryBuilder) -> {
                        String op = getOperator(v);
                        String obj = v.replaceFirst(op, "");

                        double aDouble = Double.parseDouble(obj);
                        switch (op) {
                        case "<":
                            queryBuilder.or(QueryBuilder.start(
                                    DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD)
                                    .lessThan(aDouble).get(),
                                    QueryBuilder.start(
                                            DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD)
                                            .lessThan(aDouble).get());
                            break;
                        case "<=":
                            queryBuilder.or(QueryBuilder.start(
                                    DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD)
                                    .lessThanEquals(aDouble).get(),
                                    QueryBuilder.start(
                                            DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD)
                                            .lessThanEquals(aDouble).get());
                            break;
                        case ">":
                            queryBuilder.and(
                                    DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD)
                                    .greaterThan(aDouble)
                                    .and(DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD)
                                    .greaterThan(aDouble);
                            break;
                        case ">=":
                            queryBuilder.and(
                                    DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD)
                                    .greaterThanEquals(aDouble)
                                    .and(DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD)
                                    .greaterThanEquals(aDouble);
                            break;
                        default:
                            throw new IllegalArgumentException("Unsupported operator '" + op + "'");
                        }
                    });
        }
    }
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

private QueryBuilder parseQuery(Query query, QueryBuilder builder) {
    if (query != null) {

        /** VARIANT PARAMS **/

        if (query.containsKey(VariantQueryParams.REGION.key())
                && !query.getString(VariantQueryParams.REGION.key()).isEmpty()) {
            List<String> stringList = query.getAsStringList(VariantQueryParams.REGION.key());
            List<Region> regions = new ArrayList<>(stringList.size());
            for (String reg : stringList) {
                Region region = Region.parseRegion(reg);
                regions.add(region);//from  w w w. j av a2s .c om
            }
            getRegionFilter(regions, builder);
        }

        if (query.getString(VariantQueryParams.ID.key()) != null
                && !query.getString(VariantQueryParams.ID.key()).isEmpty()) {
            List<String> ids = query.getAsStringList(VariantQueryParams.ID.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    ids, builder, QueryOperation.OR);
            addQueryListFilter(DBObjectToVariantConverter.IDS_FIELD, ids, builder, QueryOperation.OR);
        }

        if (query.containsKey(VariantQueryParams.GENE.key())) {
            List<String> xrefs = query.getAsStringList(VariantQueryParams.GENE.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    xrefs, builder, QueryOperation.OR);
        }

        if (query.containsKey(VariantQueryParams.REFERENCE.key())
                && query.getString(VariantQueryParams.REFERENCE.key()) != null) {
            addQueryStringFilter(DBObjectToVariantConverter.REFERENCE_FIELD,
                    query.getString(VariantQueryParams.REFERENCE.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.ALTERNATE.key())
                && query.getString(VariantQueryParams.ALTERNATE.key()) != null) {
            addQueryStringFilter(DBObjectToVariantConverter.ALTERNATE_FIELD,
                    query.getString(VariantQueryParams.ALTERNATE.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.TYPE.key())
                && !query.getString(VariantQueryParams.TYPE.key()).isEmpty()) {
            addQueryStringFilter(DBObjectToVariantConverter.TYPE_FIELD,
                    query.getString(VariantQueryParams.TYPE.key()), builder);
        }

        /** ANNOTATION PARAMS **/

        if (query.containsKey(VariantQueryParams.ANNOTATION_EXISTS.key())) {
            builder.and(DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                    + DBObjectToVariantAnnotationConverter.ANNOT_ID_FIELD);
            builder.exists(query.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key()));
        }

        if (query.containsKey(VariantQueryParams.ANNOT_XREF.key())) {
            List<String> xrefs = query.getAsStringList(VariantQueryParams.ANNOT_XREF.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    xrefs, builder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())) {
            List<String> cts = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()));
            List<Integer> ctsInteger = new ArrayList<>(cts.size());
            for (Iterator<String> iterator = cts.iterator(); iterator.hasNext();) {
                String ct = iterator.next();
                if (ct.startsWith("SO:")) {
                    ct = ct.substring(3);
                }
                try {
                    ctsInteger.add(Integer.parseInt(ct));
                } catch (NumberFormatException e) {
                    logger.error("Error parsing integer ", e);
                    iterator.remove(); //Remove the malformed query params.
                }
            }
            query.put(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key(), cts);
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD,
                    ctsInteger, builder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.ANNOT_BIOTYPE.key())) {
            List<String> biotypes = query.getAsStringList(VariantQueryParams.ANNOT_BIOTYPE.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.BIOTYPE_FIELD,
                    biotypes, builder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.POLYPHEN.key())) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POLYPHEN_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD,
                    query.getString(VariantQueryParams.POLYPHEN.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.SIFT.key())) {
            System.out.println(query.getString(VariantQueryParams.SIFT.key()));
            addCompQueryFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SIFT_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD,
                    query.getString(VariantQueryParams.SIFT.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.PROTEIN_SUBSTITUTION.key())) {
            List<String> list = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.PROTEIN_SUBSTITUTION.key()));
            addScoreFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.PROTEIN_SUBSTITUTION_SCORE_FIELD,
                    list, builder);
            query.put(VariantQueryParams.PROTEIN_SUBSTITUTION.key(), list); //Replace the QueryOption without the malformed query params
        }

        if (query.containsKey(VariantQueryParams.CONSERVATION.key())) {
            List<String> list = new ArrayList<>(query.getAsStringList(VariantQueryParams.CONSERVATION.key()));
            addScoreFilter(DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                    + DBObjectToVariantAnnotationConverter.CONSERVED_REGION_SCORE_FIELD, list, builder);
            query.put(VariantQueryParams.CONSERVATION.key(), list);
        }

        if (query.containsKey(VariantQueryParams.ALTERNATE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.ALTERNATE_FREQUENCY.key()));
            addFrequencyFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, list,
                    builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency
        }

        if (query.containsKey(VariantQueryParams.REFERENCE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.REFERENCE_FREQUENCY.key()));
            addFrequencyFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, list,
                    builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency
        }

        /** STATS PARAMS **/

        if (query.get(VariantQueryParams.STATS_MAF.key()) != null
                && !query.getString(VariantQueryParams.STATS_MAF.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MAF_FIELD,
                    query.getString(VariantQueryParams.STATS_MAF.key()), builder);
        }

        if (query.get(VariantQueryParams.STATS_MGF.key()) != null
                && !query.getString(VariantQueryParams.STATS_MGF.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MGF_FIELD,
                    query.getString(VariantQueryParams.STATS_MGF.key()), builder);
        }

        if (query.get(VariantQueryParams.MISSING_ALLELES.key()) != null
                && !query.getString(VariantQueryParams.MISSING_ALLELES.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "."
                            + DBObjectToVariantStatsConverter.MISSALLELE_FIELD,
                    query.getString(VariantQueryParams.MISSING_ALLELES.key()), builder);
        }

        if (query.get(VariantQueryParams.MISSING_GENOTYPES.key()) != null
                && !query.getString(VariantQueryParams.MISSING_GENOTYPES.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "."
                            + DBObjectToVariantStatsConverter.MISSGENOTYPE_FIELD,
                    query.getString(VariantQueryParams.MISSING_GENOTYPES.key()), builder);
        }

        if (query.get("numgt") != null && !query.getString("numgt").isEmpty()) {
            for (String numgt : query.getAsStringList("numgt")) {
                String[] split = numgt.split(":");
                addCompQueryFilter(
                        DBObjectToVariantConverter.STATS_FIELD + "."
                                + DBObjectToVariantStatsConverter.NUMGT_FIELD + "." + split[0],
                        split[1], builder);
            }
        }

        /** STUDIES **/
        QueryBuilder studyBuilder = QueryBuilder.start();

        if (query.containsKey(VariantQueryParams.STUDIES.key())) { // && !options.getList("studies").isEmpty() && !options.getListAs("studies", String.class).get(0).isEmpty()) {
            List<Integer> studyIds = getStudyIds(query.getAsList(VariantQueryParams.STUDIES.key()), null);
            addQueryListFilter(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, studyIds, studyBuilder,
                    QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.FILES.key())) { // && !options.getList("files").isEmpty() && !options.getListAs("files", String.class).get(0).isEmpty()) {
            addQueryListFilter(
                    DBObjectToVariantSourceEntryConverter.FILES_FIELD + "."
                            + DBObjectToVariantSourceEntryConverter.FILEID_FIELD,
                    query.getAsIntegerList(VariantQueryParams.FILES.key()), studyBuilder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.GENOTYPE.key())) {
            String sampleGenotypesCSV = query.getString(VariantQueryParams.GENOTYPE.key());

            //                String AND = ",";
            //                String OR = ";";
            //                String IS = ":";

            //                String AND = "AND";
            //                String OR = "OR";
            //                String IS = ":";

            String AND = ";";
            String OR = ",";
            String IS = ":";

            // we may need to know the study type
            //                studyConfigurationManager.getStudyConfiguration(1, null).getResult().get(0).

            String[] sampleGenotypesArray = sampleGenotypesCSV.split(AND);
            System.out.println("sampleGenotypesArray = " + Arrays.toString(sampleGenotypesArray));

            for (String sampleGenotypes : sampleGenotypesArray) {
                String[] sampleGenotype = sampleGenotypes.split(IS);
                if (sampleGenotype.length != 2) {
                    continue;
                }
                int sample = Integer.parseInt(sampleGenotype[0]);
                String[] genotypes = sampleGenotype[1].split(OR);
                QueryBuilder genotypesBuilder = QueryBuilder.start();
                for (String genotype : genotypes) {
                    if ("0/0".equals(genotype) || "0|0".equals(genotype)) {
                        QueryBuilder andBuilder = QueryBuilder.start();
                        List<String> otherGenotypes = Arrays.asList("0/1", "1/0", "1/1", "-1/-1", "0|1", "1|0",
                                "1|1", "-1|-1", "0|2", "2|0", "2|1", "1|2", "2|2", "0/2", "2/0", "2/1", "1/2",
                                "2/2", DBObjectToSamplesConverter.UNKNOWN_GENOTYPE);
                        for (String otherGenotype : otherGenotypes) {
                            andBuilder.and(new BasicDBObject(
                                    DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "." + otherGenotype,
                                    new BasicDBObject("$not", new BasicDBObject("$elemMatch",
                                            new BasicDBObject("$eq", sample)))));
                        }
                        genotypesBuilder.or(andBuilder.get());
                    } else {
                        String s = DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "."
                                + DBObjectToSamplesConverter.genotypeToStorageType(genotype);
                        //or [ {"samp.0|0" : { $elemMatch : { $eq : <sampleId> } } } ]
                        genotypesBuilder.or(new BasicDBObject(s,
                                new BasicDBObject("$elemMatch", new BasicDBObject("$eq", sample))));
                    }
                }
                studyBuilder.and(genotypesBuilder.get());
            }
        }

        // If Study Query is used then we add a elemMatch query
        DBObject studyQuery = studyBuilder.get();
        if (studyQuery.keySet().size() != 0) {
            builder.and(DBObjectToVariantConverter.STUDIES_FIELD).elemMatch(studyQuery);
        }
    }

    logger.debug("Find = " + builder.get());
    return builder;
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

private void addQueryCriterion(final SearchCriterion searchCriterion, final QueryBuilder queryBuilder) {

    final ComparisonOperator comparisonOperator = searchCriterion.getComparisonOperator();
    final Object comparisonValue = searchCriterion.getComparisonValue();
    switch (comparisonOperator) {

    case containsAll: {
        queryBuilder.all(comparisonValue);
        break;//from  w w  w  .ja v a2  s.  com
    }

    case equalTo: {
        queryBuilder.equals(comparisonValue);
        break;
    }

    case equalToAny: {
        queryBuilder.in(comparisonValue);
        break;
    }

    case exists: {
        queryBuilder.exists(true);
        break;
    }

    case greaterThan: {
        queryBuilder.greaterThan(comparisonValue);
        break;
    }

    case greaterThanOrEqualTo: {
        queryBuilder.greaterThanEquals(comparisonValue);
        break;
    }

    case lessThan: {
        queryBuilder.lessThan(comparisonValue);
        break;
    }

    case lessThanOrEqualTo: {
        queryBuilder.lessThanEquals(comparisonValue);
        break;
    }

    case notEqualTo: {
        queryBuilder.notEquals(comparisonValue);
        break;
    }

    case notEqualToAny: {
        queryBuilder.notIn(comparisonValue);
        break;
    }

    case notExists: {
        queryBuilder.exists(false);
        break;
    }

    case regex: {
        final Pattern regexPattern = searchCriterion.getRegexPattern();
        if (regexPattern != null) {
            queryBuilder.regex(regexPattern);
        }

        break;
    }

    }
}