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(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

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 va 2 s .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 addQueryListFilter(String key, List<?> values, QueryBuilder builder, QueryOperation op) {
    if (values != null)
        if (values.size() == 1) {
            if (op == QueryOperation.AND) {
                builder.and(key).is(values.get(0));
            } else {
                builder.or(QueryBuilder.start(key).is(values.get(0)).get());
            }//from   w  w  w.  j ava 2 s  .  c o  m
        } else if (!values.isEmpty()) {
            if (op == QueryOperation.AND) {
                builder.and(key).in(values);
            } else {
                builder.or(QueryBuilder.start(key).in(values).get());
            }
        }
    return builder;
}

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

License:Apache License

@Deprecated
public QueryResult getAllVariantsByRegionAndStudies(Region region, List<String> studyId, QueryOptions options) {

    // Aggregation for filtering when more than one study is present
    QueryBuilder qb = QueryBuilder.start(DBObjectToVariantConverter.STUDIES_FIELD + "."
            + DBObjectToVariantSourceEntryConverter.STUDYID_FIELD).in(studyId);
    getRegionFilter(region, qb);//w  w  w.ja  v a2 s . c  om
    parseQueryOptions(options, qb);

    DBObject match = new BasicDBObject("$match", qb.get());
    DBObject unwind = new BasicDBObject("$unwind", "$" + DBObjectToVariantConverter.STUDIES_FIELD);
    DBObject match2 = new BasicDBObject("$match",
            new BasicDBObject(
                    DBObjectToVariantConverter.STUDIES_FIELD + "."
                            + DBObjectToVariantSourceEntryConverter.STUDYID_FIELD,
                    new BasicDBObject("$in", studyId)));

    logger.debug("Query to be executed {}", qb.get().toString());

    return variantsCollection.aggregate(/*"$variantsRegionStudies", */Arrays.asList(match, unwind, match2),
            options);
}

From source file:org.pentaho.di.trans.dataservice.optimization.mongod.MongodbPredicate.java

License:Apache License

public String asMatch() throws PushDownOptimizationException {
    return QueryBuilder.start(MATCH).is(conditionAsDBObject()).get().toString();
}

From source file:org.sglover.checksum.dao.mongo.MongoChecksumDAO.java

License:Open Source License

@Override
public NodeChecksums getChecksums(String nodeId, long nodeVersion) {
    DBObject query = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion).get();

    DBObject dbObject = checksums.findOne(query);
    NodeChecksums checksums = toChecksums(dbObject);
    return checksums;
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
// TODO use skip and maxItems
public Stream<Entity<String>> getNames(Node node, int skip, int maxItems) {
    String nodeId = node.getNodeId();
    String nodeVersion = node.getVersionLabel();

    Collection<Entity<String>> ret = new LinkedList<>();

    QueryBuilder queryBuilder = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion);
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("nm", 1);
    DBObject orderBy = orderByBuilder.get();

    DBCursor cursor = entitiesData.find(query).sort(orderBy);
    try {/*from  www  . jav  a 2s  . com*/
        for (DBObject dbObject : cursor) {
            String name = (String) dbObject.get("nm");
            int count = (Integer) dbObject.get("c");
            String type = map.get("nm");
            Entity<String> entity = new Entity<>(EntityType.valueOf(type), name, count);
            ret.add(entity);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return ret.stream();
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
// TODO use skip and maxItems
public Stream<Entity<String>> getOrgs(Node node, int skip, int maxItems) {
    String nodeId = node.getNodeId();
    String nodeVersion = node.getVersionLabel();

    Collection<Entity<String>> ret = new LinkedList<>();

    QueryBuilder queryBuilder = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion);
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("o", 1);
    DBObject orderBy = orderByBuilder.get();

    DBCursor cursor = entitiesData.find(query).sort(orderBy);
    try {//  w ww .  j a  va 2  s  .c  o m
        for (DBObject dbObject : cursor) {
            String org = (String) dbObject.get("o");
            int count = (Integer) dbObject.get("c");
            String type = map.get("nm");
            Entity<String> entity = new Entity<>(EntityType.valueOf(type), org, count);
            ret.add(entity);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return ret.stream();
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
public Stream<Node> matchingNodes(EntityType type, String name) {
    List<Node> nodes = new LinkedList<>();

    QueryBuilder queryBuilder = QueryBuilder.start("t").is(type).and("nm").is(name);
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("c", 1);
    DBObject orderBy = orderByBuilder.get();

    DBCursor cursor = entitiesData.find(query).sort(orderBy);
    try {/*from   w w  w .  java  2s.  c o  m*/
        for (DBObject dbObject : cursor) {
            String nodeId = (String) dbObject.get("n");
            long nodeInternalId = (Long) dbObject.get("ni");
            Long nodeVersion = (Long) dbObject.get("v");
            Node node = Node.build().nodeInternalId(nodeInternalId).nodeId(nodeId).nodeVersion(nodeVersion);
            nodes.add(node);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return nodes.stream();
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
public Entities getEntities(Node node) {
    String nodeId = node.getNodeId();
    String nodeVersion = node.getVersionLabel();

    Entities entities = Entities.empty(nodeId, nodeVersion);

    QueryBuilder queryBuilder = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion);

    Set<String> types = new HashSet<>();
    types.add("names");
    types.add("orgs");
    queryBuilder.and("t").in(types);

    DBObject query = queryBuilder.get();

    DBCursor cursor = entitiesData.find(query);
    try {/*from  w w  w .j  a va 2s .c  o m*/
        for (DBObject dbObject : cursor) {
            Entity<String> entity = getEntity(dbObject);
            entities.addEntity(entity);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return entities;
}

From source file:org.sglover.entities.dao.mongo.MongoSimilarityDAO.java

License:Open Source License

@Override
public double getSimilarity(Node node1, Node node2) {
    List<DBObject> ors = new LinkedList<>();
    ors.add(QueryBuilder.start("n1").is(node1.getNodeId()).and("v1").is(node1.getNodeVersion()).and("n2")
            .is(node2.getNodeId()).and("v2").is(node2.getNodeVersion()).get());
    ors.add(QueryBuilder.start("n1").is(node2.getNodeId()).and("v1").is(node2.getNodeVersion()).and("n2")
            .is(node1.getNodeId()).and("v2").is(node1.getNodeVersion()).get());

    DBObject query = QueryBuilder.start().or(ors.toArray(new DBObject[0])).get();

    DBObject dbObject = similarityData.findOne(query);
    Double similarity = (dbObject != null ? (Double) dbObject.get("s") : null);
    return (similarity != null ? similarity : -1.0);
}