Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

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

Prototype

public static QueryBuilder start() 

Source Link

Document

Returns a new QueryBuilder.

Usage

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

License:Apache License

@Override
public QueryResult getAllClinvar(QueryOptions options) {
    //        options.addToListOption("include", "clinvarSet");
    //        options.addToListOption("include", "chromosome");
    //        options.addToListOption("include", "start");
    //        options.addToListOption("include", "end");
    //        options.addToListOption("include", "reference");
    //        options.addToListOption("include", "alternate");
    //        options.addToListOption("include", "annot");
    QueryBuilder builder = QueryBuilder.start();

    builder = addClinvarFilters(builder, options);

    //        List<DBObject> pipeline = new ArrayList<>();
    //        pipeline = addClinvarAggregationFilters(pipeline, options);
    //        DBObject fields = new BasicDBObject();
    //        fields.put("clinvarSet", 1);
    //        fields.put("chromosome", 1);
    //        fields.put("start", 1);
    //        fields.put("end", 1);
    //        fields.put("reference", 1);
    //        fields.put("alternate", 1);
    //        fields.put("annot", 1);
    //        pipeline.add(new BasicDBObject("$project", fields));

    //        return executeAggregation2("", pipeline, options);
    return executeQuery("result", builder.get(), options);
    //        return prepareClinvarQueryResultList(Collections.singletonList(executeQuery("result", builder.get(), options))).get(0);
}

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;/*w ww.j a v 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;/*from  w ww  .  ja  v a2  s  . c o  m*/
    if (op == QueryOperation.OR) {
        auxBuilder = QueryBuilder.start();
    } else {
        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

/**
 * Accept a list of comparative filters separated with "," or ";" with the expression:
 * {OPERATION}{VALUE}, where the accepted operations are: <, <=, >, >=, =, ==, !=, ~=.
 *
 * @param key/*w w w .ja v a2s  .  com*/
 * @param value
 * @param builder
 * @param extendKey
 * @return
 */
private QueryBuilder addCompListQueryFilter(String key, String value, QueryBuilder builder, boolean extendKey) {
    QueryOperation op = checkOperator(value);
    List<String> values = splitValue(value, op);

    QueryBuilder compBuilder;
    if (op == QueryOperation.OR) {
        compBuilder = QueryBuilder.start();
    } else {
        compBuilder = builder;
    }

    for (String elem : values) {
        addCompQueryFilter(key, elem, compBuilder, extendKey);
    }

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

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

License:Apache License

@Override
public QueryResult findStudyNameOrStudyId(String study, QueryOptions options) {
    MongoDBCollection coll = db.getCollection(collectionName);
    QueryBuilder qb = QueryBuilder.start();
    qb.or(new BasicDBObject(DBObjectToVariantSourceConverter.STUDYNAME_FIELD, study),
            new BasicDBObject(DBObjectToVariantSourceConverter.STUDYID_FIELD, study));
    //        parseQueryOptions(options, qb);

    DBObject projection = new BasicDBObject(DBObjectToVariantSourceConverter.STUDYID_FIELD, 1).append("_id", 0);

    options.add("limit", 1);

    return coll.find(qb.get(), projection, options);
}

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

License:Apache License

@Override
public QueryResult getStudyById(String studyId, QueryOptions options) {
    // db.files.aggregate( { $match : { "studyId" : "abc" } }, 
    //                     { $project : { _id : 0, studyId : 1, studyName : 1 } }, 
    //                     { $group : {
    //                           _id : { studyId : "$studyId", studyName : "$studyName"}, 
    //                           numSources : { $sum : 1} 
    //                     }} )
    MongoDBCollection coll = db.getCollection(collectionName);

    QueryBuilder qb = QueryBuilder.start();
    getStudyIdFilter(studyId, qb);/*from   w  w  w.  ja  v  a 2s .  c  o m*/

    DBObject match = new BasicDBObject("$match", qb.get());
    DBObject project = new BasicDBObject("$project",
            new BasicDBObject("_id", 0).append(DBObjectToVariantSourceConverter.STUDYID_FIELD, 1)
                    .append(DBObjectToVariantSourceConverter.STUDYNAME_FIELD, 1));
    DBObject group = new BasicDBObject("$group",
            new BasicDBObject("_id",
                    new BasicDBObject("studyId", "$" + DBObjectToVariantSourceConverter.STUDYID_FIELD)
                            .append("studyName", "$" + DBObjectToVariantSourceConverter.STUDYNAME_FIELD))
                                    .append("numFiles", new BasicDBObject("$sum", 1)));

    QueryResult aggregationResult = coll.aggregate(/*"$studyInfo", */Arrays.asList(match, project, group),
            options);
    Iterable<DBObject> results = aggregationResult.getResult();
    DBObject dbo = results.iterator().next();
    DBObject dboId = (DBObject) dbo.get("_id");

    DBObject outputDbo = new BasicDBObject("studyId", dboId.get("studyId"))
            .append("studyName", dboId.get("studyName")).append("numFiles", dbo.get("numFiles"));
    QueryResult transformedResult = new QueryResult(aggregationResult.getId(), aggregationResult.getDbTime(),
            aggregationResult.getNumResults(), aggregationResult.getNumTotalResults(),
            aggregationResult.getWarningMsg(), aggregationResult.getErrorMsg(), Arrays.asList(outputDbo));
    return transformedResult;
}

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

License:Apache License

@Override
public QueryResult delete(Query query, QueryOptions options) {
    QueryBuilder qb = QueryBuilder.start();
    qb = parseQuery(query, qb);/*from ww w.  ja  va2  s. com*/
    logger.debug("Delete to be executed: '{}'", qb.get().toString());
    QueryResult queryResult = variantsCollection.remove(qb.get(), options);

    return queryResult;
}

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

License:Apache License

@Override
public QueryResult<Variant> get(Query query, QueryOptions options) {
    if (options == null) {
        options = new QueryOptions();
    }// ww w  . ja v  a 2 s.c om
    QueryBuilder qb = QueryBuilder.start();
    //        parseQueryOptions(options, qb);
    parseQuery(query, qb);
    //        DBObject projection = parseProjectionQueryOptions(options);
    DBObject projection = createProjection(query, options);
    logger.debug("Query to be executed: '{}'", qb.get().toString());

    QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection,
            getDbObjectToVariantConverter(query, options), options);
    // set query Id?

    return queryResult;
}

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

License:Apache License

@Override
public QueryResult<Long> count(Query query) {
    QueryBuilder qb = QueryBuilder.start();
    parseQuery(query, qb);// ww w. j  a  v a2 s.  c  o m
    logger.debug("Query to be executed: '{}'", qb.get().toString());
    QueryResult<Long> queryResult = queryResult = variantsCollection.count(qb.get());
    return queryResult;
}

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

License:Apache License

@Override
public QueryResult distinct(Query query, String field) {
    String documentPath;/*from  ww w  . ja va 2 s.  c  o m*/
    switch (field) {
    case "gene":
    default:
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.GENE_NAME_FIELD;
        break;
    case "ensemblGene":
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.ENSEMBL_GENE_ID_FIELD;
        break;
    case "ensemblTranscript":
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.ENSEMBL_TRANSCRIPT_ID_FIELD;
        break;
    case "ct":
    case "consequence_type":
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD;
        break;
    }

    QueryBuilder qb = QueryBuilder.start();
    parseQuery(query, qb);
    return variantsCollection.distinct(documentPath, qb.get());
}