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.mongodb.db.VariationMongoDBAdaptor.java

License:Apache License

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

    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(builder.get());//from  w  w w . ja  v  a2 s  .c  om
            ids.add(region.toString());
        }
        return executeQueryList(ids, queries, options, db.getCollection("variation_phenotype_annotation"));
    } 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("transcriptVariations.consequenceTypes").in(consequenceTypeDBList);
            }
            queries.add(builder.get());
            ids.add(region.toString());
        }

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

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

License:Apache License

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

    for (GenomicVariant variation : variations) {
        String chunkId = getChunkIdPrefix(variation.getChromosome(), variation.getPosition(),
                variationChunkSize);// www.j a v a  2  s .  com
        QueryBuilder builder = QueryBuilder.start("_chunkIds").is(chunkId).and("chromosome")
                .is(variation.getChromosome()).and("start").is(variation.getPosition()).and("alternate")
                .is(variation.getAlternative());
        if (variation.getReference() != null) {
            builder = builder.and("reference").is(variation.getReference());
        }

        queries.add(builder.get());
    }

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

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

        BasicDBList idListObject = (BasicDBList) result.getResult();
        for (Object idObject : idListObject) {
            DBObject variantObject = (DBObject) idObject;
            idList.add(variantObject.get("id").toString());
        }

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

    return results;
}

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

License:Apache License

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

    for (GenomicVariant variation : variations) {
        String chunkId = getChunkIdPrefix(variation.getChromosome(), variation.getPosition(),
                variationChunkSize);//from   ww  w . j  a va  2  s.  c o  m

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

        if (variation.getReference() != null) {
            builder = builder.and("reference").is(variation.getReference());
        }

        queries.add(builder.get());
    }

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

    return results;
}

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.ja  v  a2  s  .  c  o m*/

        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.adaptors.VariantMongoDBAdaptor.java

License:Apache License

private StudyConfiguration parseStudyQueryParams(Query query, QueryBuilder builder) {

    if (query != null) {
        Map<String, Integer> studies = getStudyConfigurationManager().getStudies(null);

        boolean singleStudy = studies.size() == 1;
        boolean validStudiesFilter = isValidParam(query, VariantQueryParams.STUDIES);
        // SAMPLES filter will add a FILES filter if absent
        boolean validFilesFilter = isValidParam(query, VariantQueryParams.FILES)
                || isValidParam(query, VariantQueryParams.SAMPLES);
        boolean otherFilters = isValidParam(query, VariantQueryParams.FILES)
                || isValidParam(query, VariantQueryParams.GENOTYPE)
                || isValidParam(query, VariantQueryParams.SAMPLES)
                || isValidParam(query, VariantQueryParams.FILTER);

        // Use an elemMatch with all the study filters if there is more than one study registered,
        // or FILES and STUDIES filters are being used.
        // If filters STUDIES+FILES is used, elemMatch is required to use the index correctly. See #493
        boolean studyElemMatch = (!singleStudy || (validFilesFilter && validStudiesFilter));

        // If only studyId filter is being used, elemMatch is not needed
        if (validStudiesFilter && !otherFilters) {
            studyElemMatch = false;/*ww  w  . j  av  a 2  s. c o  m*/
        }

        // If using an elemMatch for the study, keys don't need to start with "studies"
        String studyQueryPrefix = studyElemMatch ? "" : DocumentToVariantConverter.STUDIES_FIELD + '.';
        QueryBuilder studyBuilder = QueryBuilder.start();
        final StudyConfiguration defaultStudyConfiguration = utils.getDefaultStudyConfiguration(query, null);

        if (isValidParam(query, VariantQueryParams.STUDIES)) {
            String sidKey = DocumentToVariantConverter.STUDIES_FIELD + '.'
                    + DocumentToStudyVariantEntryConverter.STUDYID_FIELD;
            String value = query.getString(VariantQueryParams.STUDIES.key());

            // Check that the study exists
            QueryOperation studiesOperation = checkOperator(value);
            List<String> studiesNames = splitValue(value, studiesOperation);
            List<Integer> studyIds = utils.getStudyIds(studiesNames, studies); // Non negated studyIds

            // If the Studies query has an AND operator or includes negated fields, it can not be represented only
            // in the "elemMatch". It needs to be in the root
            boolean anyNegated = studiesNames.stream().anyMatch(VariantDBAdaptorUtils::isNegated);
            boolean studyFilterAtRoot = studiesOperation == QueryOperation.AND || anyNegated;
            if (studyFilterAtRoot) {
                addQueryFilter(sidKey, value, builder, QueryOperation.AND,
                        study -> utils.getStudyId(study, false, studies));
            }

            // Add all non negated studies to the elemMatch builder if it is being used,
            // or it is not and it has not been added to the root
            if (studyElemMatch || !studyFilterAtRoot) {
                if (!studyIds.isEmpty()) {
                    if (!singleStudy || anyNegated || validFilesFilter) {
                        String studyIdsCsv = studyIds.stream().map(Object::toString)
                                .collect(Collectors.joining(","));
                        addQueryIntegerFilter(
                                studyQueryPrefix + DocumentToStudyVariantEntryConverter.STUDYID_FIELD,
                                studyIdsCsv, studyBuilder, QueryOperation.AND);
                    } // There is only one study! We can skip this filter
                }
            }
        }

        if (isValidParam(query, VariantQueryParams.FILES)) {
            addQueryFilter(
                    studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD + '.'
                            + DocumentToStudyVariantEntryConverter.FILEID_FIELD,
                    query.getString(VariantQueryParams.FILES.key()), studyBuilder, QueryOperation.AND,
                    f -> utils.getFileId(f, false, defaultStudyConfiguration));
        }

        if (isValidParam(query, VariantQueryParams.FILTER)) {
            String filesValue = query.getString(VariantQueryParams.FILES.key());
            QueryOperation filesOperation = checkOperator(filesValue);
            List<String> fileNames = splitValue(filesValue, filesOperation);
            List<Integer> fileIds = utils.getFileIds(fileNames, true, defaultStudyConfiguration);

            String fileQueryPrefix;
            if (fileIds.isEmpty()) {
                fileQueryPrefix = studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD + '.';
                addQueryStringFilter(
                        fileQueryPrefix + DocumentToStudyVariantEntryConverter.ATTRIBUTES_FIELD + '.'
                                + StudyEntry.FILTER,
                        query.getString(VariantQueryParams.FILTER.key()), studyBuilder, QueryOperation.AND);
            } else {
                QueryBuilder fileBuilder = QueryBuilder.start();
                addQueryStringFilter(
                        DocumentToStudyVariantEntryConverter.ATTRIBUTES_FIELD + '.' + StudyEntry.FILTER,
                        query.getString(VariantQueryParams.FILTER.key()), fileBuilder, QueryOperation.AND);
                fileBuilder.and(DocumentToStudyVariantEntryConverter.FILEID_FIELD).in(fileIds);
                studyBuilder.and(studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD)
                        .elemMatch(fileBuilder.get());
            }
        }

        Map<Object, List<String>> genotypesFilter = new HashMap<>();
        if (isValidParam(query, VariantQueryParams.GENOTYPE)) {
            String sampleGenotypes = query.getString(VariantQueryParams.GENOTYPE.key());
            parseGenotypeFilter(sampleGenotypes, genotypesFilter);
        }

        if (isValidParam(query, VariantQueryParams.SAMPLES)) {
            Set<Integer> files = new HashSet<>();
            String samples = query.getString(VariantQueryParams.SAMPLES.key());

            for (String sample : samples.split(",")) {
                int sampleId = utils.getSampleId(sample, defaultStudyConfiguration);
                genotypesFilter.put(sampleId,
                        Arrays.asList("1", "0/1", "0|1", "1|0", "1/1", "1|1", "1/2", "1|2", "2|1"));
                if (!isValidParam(query, VariantQueryParams.FILES) && defaultStudyConfiguration != null) {
                    for (Integer file : defaultStudyConfiguration.getIndexedFiles()) {
                        if (defaultStudyConfiguration.getSamplesInFiles().get(file).contains(sampleId)) {
                            files.add(file);
                        }
                    }
                }
            }

            // If there is no valid files filter, add files filter to speed up this query
            if (!isValidParam(query, VariantQueryParams.FILES) && !files.isEmpty()) {
                addQueryFilter(
                        studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD + '.'
                                + DocumentToStudyVariantEntryConverter.FILEID_FIELD,
                        files, studyBuilder, QueryOperation.AND,
                        f -> utils.getFileId(f, false, defaultStudyConfiguration));
            }
        }

        if (!genotypesFilter.isEmpty()) {
            for (Map.Entry<Object, List<String>> entry : genotypesFilter.entrySet()) {
                Object sample = entry.getKey();
                List<String> genotypes = entry.getValue();

                int sampleId = utils.getSampleId(sample, defaultStudyConfiguration);

                QueryBuilder genotypesBuilder = QueryBuilder.start();

                List<String> defaultGenotypes;
                if (defaultStudyConfiguration != null) {
                    defaultGenotypes = defaultStudyConfiguration.getAttributes()
                            .getAsStringList(DEFAULT_GENOTYPE.key());
                } else {
                    defaultGenotypes = Arrays.asList("0/0", "0|0");
                }
                for (String genotype : genotypes) {
                    boolean negated = isNegated(genotype);
                    if (negated) {
                        genotype = genotype.substring(1);
                    }
                    if (defaultGenotypes.contains(genotype)) {
                        List<String> otherGenotypes = Arrays.asList("0/0", "0|0", "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", DocumentToSamplesConverter.UNKNOWN_GENOTYPE);
                        if (negated) {
                            for (String otherGenotype : otherGenotypes) {
                                if (defaultGenotypes.contains(otherGenotype)) {
                                    continue;
                                }
                                String key = studyQueryPrefix
                                        + DocumentToStudyVariantEntryConverter.GENOTYPES_FIELD + '.'
                                        + otherGenotype;
                                genotypesBuilder.or(new BasicDBObject(key, sampleId));
                            }
                        } else {
                            QueryBuilder andBuilder = QueryBuilder.start();
                            for (String otherGenotype : otherGenotypes) {
                                if (defaultGenotypes.contains(otherGenotype)) {
                                    continue;
                                }
                                String key = studyQueryPrefix
                                        + DocumentToStudyVariantEntryConverter.GENOTYPES_FIELD + '.'
                                        + otherGenotype;
                                andBuilder.and(new BasicDBObject(key, new Document("$ne", sampleId)));
                            }
                            genotypesBuilder.or(andBuilder.get());
                        }
                    } else {
                        String s = studyQueryPrefix + DocumentToStudyVariantEntryConverter.GENOTYPES_FIELD + '.'
                                + DocumentToSamplesConverter.genotypeToStorageType(genotype);
                        if (negated) {
                            //and [ {"gt.0|1" : { $ne : <sampleId> } } ]
                            genotypesBuilder.and(new BasicDBObject(s, new BasicDBObject("$ne", sampleId)));

                        } else {
                            //or [ {"gt.0|1" : <sampleId> } ]
                            genotypesBuilder.or(new BasicDBObject(s, sampleId));
                        }
                    }
                }
                studyBuilder.and(genotypesBuilder.get());
            }
        }

        // If Study Query is used then we add a elemMatch query
        DBObject studyQuery = studyBuilder.get();
        if (!studyQuery.keySet().isEmpty()) {
            if (studyElemMatch) {
                builder.and(DocumentToVariantConverter.STUDIES_FIELD).elemMatch(studyQuery);
            } else {
                builder.and(studyQuery);
            }
        }
        return defaultStudyConfiguration;
    } else {
        return null;
    }
}

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

License:Apache License

private <T> QueryBuilder addQueryFilter(String key, String value, final QueryBuilder builder, QueryOperation op,
        Function<String, T> map) {
    QueryOperation operation = checkOperator(value);
    QueryBuilder auxBuilder;
    if (op == QueryOperation.OR) {
        auxBuilder = QueryBuilder.start();
    } else {// w  ww. j  av  a2  s. co  m
        auxBuilder = builder;
    }

    if (operation == null) {
        if (value.startsWith("!")) {
            T mapped = map.apply(value.substring(1));
            if (mapped instanceof Collection) {
                auxBuilder.and(key).notIn(mapped);
            } else {
                auxBuilder.and(key).notEquals(mapped);
            }
        } else {
            T mapped = map.apply(value);
            if (mapped instanceof Collection) {
                auxBuilder.and(key).in(mapped);
            } else {
                auxBuilder.and(key).is(mapped);
            }
        }
    } else if (operation == QueryOperation.OR) {
        String[] array = value.split(OR);
        List list = new ArrayList(array.length);
        for (String elem : array) {
            if (elem.startsWith("!")) {
                throw new VariantQueryException(
                        "Unable to use negate (!) operator in OR sequences (<it_1>(,<it_n>)*)");
            } else {
                T mapped = map.apply(elem);
                if (mapped instanceof Collection) {
                    list.addAll(((Collection) mapped));
                } else {
                    list.add(mapped);
                }
            }
        }
        auxBuilder.and(key).in(list);
    } else {
        //Split in two lists: positive and negative
        String[] array = value.split(AND);
        List listIs = new ArrayList(array.length);
        List listNotIs = new ArrayList(array.length);

        for (String elem : array) {
            if (elem.startsWith("!")) {
                T mapped = map.apply(elem.substring(1));
                if (mapped instanceof Collection) {
                    listNotIs.addAll(((Collection) mapped));
                } else {
                    listNotIs.add(mapped);
                }
            } else {
                T mapped = map.apply(elem);
                if (mapped instanceof Collection) {
                    listIs.addAll(((Collection) mapped));
                } else {
                    listIs.add(mapped);
                }
            }
        }

        if (!listIs.isEmpty()) { //Can not use method "is" because it will be overwritten with the "notEquals" or "notIn" method
            auxBuilder.and(key).all(listIs);
        }
        if (listNotIs.size() == 1) {
            auxBuilder.and(key).notEquals(listNotIs.get(0));
        } else if (listNotIs.size() > 1) {
            auxBuilder.and(key).notIn(listNotIs);
        }

    }

    if (op == QueryOperation.OR) {
        builder.or(auxBuilder.get());
    }
    return builder;
}

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

License:Apache License

private QueryBuilder addCompQueryFilter(String key, String obj, QueryBuilder builder, String op) {

    switch (op) {
    case "<":
        builder.and(key).lessThan(Double.parseDouble(obj));
        break;//from w w w.j a  v a 2  s .co  m
    case "<=":
        builder.and(key).lessThanEquals(Double.parseDouble(obj));
        break;
    case ">":
        builder.and(key).greaterThan(Double.parseDouble(obj));
        break;
    case ">=":
        builder.and(key).greaterThanEquals(Double.parseDouble(obj));
        break;
    case "=":
    case "==":
        try {
            builder.and(key).is(Double.parseDouble(obj));
        } catch (NumberFormatException e) {
            builder.and(key).is(obj);
        }
        break;
    case "!=":
        builder.and(key).notEquals(Double.parseDouble(obj));
        break;
    case "~=":
    case "~":
        builder.and(key).regex(Pattern.compile(obj));
        break;
    default:
        break;
    }
    return builder;
}

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

License:Apache License

private QueryBuilder addStringCompQueryFilter(String key, String value, QueryBuilder builder) {
    String op = getOperator(value);
    String obj = value.replaceFirst(op, "");

    switch (op) {
    case "!=":
    case "!":
        builder.and(key).notEquals(obj);
        break;//from   www .  ja v a  2  s  .com
    case "~=":
    case "~":
        builder.and(key).regex(Pattern.compile(obj));
        break;
    case "":
    case "=":
    case "==":
    default:
        builder.and(key).is(obj);
        break;
    }
    return builder;
}

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

License:Apache License

/**
 * Accepts a list of filters separated with "," or ";" with the expression: {SOURCE}{OPERATION}{VALUE}.
 *
 * @param value         Value to parse//  ww w.j a va2  s  .  c o  m
 * @param builder       QueryBuilder
 * @param scoreParam    Score VariantQueryParam
 * @param defaultSource Default source value. If null, must be present in the filter. If not, must not be present.
 * @param allowDescriptionFilter Use string values as filters for the score description
 * @return QueryBuilder
 */
private QueryBuilder addScoreFilter(String value, QueryBuilder builder, VariantQueryParams scoreParam,
        final String defaultSource, boolean allowDescriptionFilter) {
    final List<String> list;
    QueryOperation operation = checkOperator(value);
    list = splitValue(value, operation);
    List<DBObject> dbObjects = new ArrayList<>();
    for (String elem : list) {
        String[] score = VariantDBAdaptorUtils.splitOperator(elem);
        String source;
        String op;
        String scoreValue;
        // No given score
        if (StringUtils.isEmpty(score[0])) {
            if (defaultSource == null) {
                logger.error("Bad score filter: " + elem);
                throw VariantQueryException.malformedParam(scoreParam, value);
            }
            source = defaultSource;
            op = score[1];
            scoreValue = score[2];
        } else {
            if (defaultSource != null) {
                logger.error("Bad score filter: " + elem);
                throw VariantQueryException.malformedParam(scoreParam, value);
            }
            source = score[0];
            op = score[1];
            scoreValue = score[2];
        }

        String key = DocumentToVariantAnnotationConverter.SCORE_FIELD_MAP.get(source);
        if (key == null) {
            // Unknown score
            throw VariantQueryException.malformedParam(scoreParam, value);
        }

        QueryBuilder scoreBuilder = new QueryBuilder();
        if (NumberUtils.isParsable(scoreValue)) {
            // Query by score
            key += '.' + DocumentToVariantAnnotationConverter.SCORE_SCORE_FIELD;
            addCompQueryFilter(key, scoreValue, scoreBuilder, op);
        } else if (allowDescriptionFilter) {
            // Query by description
            key += '.' + DocumentToVariantAnnotationConverter.SCORE_DESCRIPTION_FIELD;
            addStringCompQueryFilter(key, scoreValue, scoreBuilder);
        } else {
            throw VariantQueryException.malformedParam(scoreParam, value);
        }
        dbObjects.add(scoreBuilder.get());
    }

    if (!dbObjects.isEmpty()) {
        if (operation == null || operation == QueryOperation.AND) {
            builder.and(dbObjects.toArray(new DBObject[dbObjects.size()]));
        } else {
            builder.and(new BasicDBObject("$or", dbObjects));
        }
    }
    return builder;
}

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

License:Apache License

/**
 * Accepts a list of filters separated with "," or ";" with the expression:
 * {STUDY}:{POPULATION}{OPERATION}{VALUE}.
 *
 * @param key       PopulationFrequency schema field
 * @param value     Value to parse//from w ww.ja v a 2s.c  o  m
 * @param builder   QueryBuilder
 * @param addFilter For complex filter
 * @return QueryBuilder
 */
private QueryBuilder addFrequencyFilter(String key, String value, QueryBuilder builder,
        VariantQueryParams queryParam, BiConsumer<String, QueryBuilder> addFilter) {
    final List<String> list;
    QueryOperation operation = checkOperator(value);
    list = splitValue(value, operation);

    List<BasicDBObject> dbObjects = new ArrayList<>();
    for (String elem : list) {
        String[] split = elem.split(IS);
        if (split.length != 2) {
            logger.error("Bad population frequency filter: " + elem);
            throw VariantQueryException.malformedParam(queryParam, value);
            //new IllegalArgumentException("Bad population frequency filter: " + elem);
        }
        String study = split[0];
        String population = split[1];
        String[] populationFrequency = splitKeyValue(population);
        logger.debug("populationFrequency = " + Arrays.toString(populationFrequency));

        QueryBuilder frequencyBuilder = new QueryBuilder();
        frequencyBuilder.and(DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_STUDY_FIELD).is(study);
        frequencyBuilder.and(DocumentToVariantAnnotationConverter.POPULATION_FREQUENCY_POP_FIELD)
                .is(populationFrequency[0]);
        Document studyPopFilter = new Document(frequencyBuilder.get().toMap());
        addFilter.accept(populationFrequency[1], frequencyBuilder);
        BasicDBObject elemMatch = new BasicDBObject(key,
                new BasicDBObject("$elemMatch", frequencyBuilder.get()));
        if (populationFrequency[1].startsWith("<")) {
            BasicDBObject orNotExistsAnyPopulation = new BasicDBObject(key,
                    new BasicDBObject("$exists", false));
            BasicDBObject orNotExistsPopulation = new BasicDBObject(key,
                    new BasicDBObject("$not", new BasicDBObject("$elemMatch", studyPopFilter)));
            dbObjects.add(new BasicDBObject("$or",
                    Arrays.asList(orNotExistsAnyPopulation, orNotExistsPopulation, elemMatch)));
        } else {
            dbObjects.add(elemMatch);
        }
    }
    if (!dbObjects.isEmpty()) {
        if (operation == null || operation == QueryOperation.AND) {
            builder.and(dbObjects.toArray(new BasicDBObject[dbObjects.size()]));
        } else {
            builder.and(new BasicDBObject("$or", dbObjects));
        }
    }
    return builder;
}