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.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

@Override
public VariantDBIterator iterator(Query query, QueryOptions options) {
    if (options == null) {
        options = new QueryOptions();
    }//www.  ja  v a 2s .co  m
    if (query == null) {
        query = new Query();
    }
    QueryBuilder qb = QueryBuilder.start();
    //        parseQueryOptions(options, qb);
    qb = parseQuery(query, qb);
    //        DBObject projection = parseProjectionQueryOptions(options);
    DBObject projection = createProjection(query, options);
    DBCursor dbCursor = variantsCollection.nativeQuery().find(qb.get(), projection, options);
    dbCursor.batchSize(options.getInt("batchSize", 100));
    return new VariantMongoDBIterator(dbCursor, getDbObjectToVariantConverter(query, options));
}

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

License:Apache License

@Override
public QueryResult groupBy(Query query, String field, QueryOptions options) {
    String documentPath;//from  w ww.  j a va 2  s . c o m
    String unwindPath;
    int numUnwinds = 2;
    switch (field) {
    case "gene":
    default:
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.GENE_NAME_FIELD;
        unwindPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD;
        break;
    case "ensemblGene":
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.ENSEMBL_GENE_ID_FIELD;
        unwindPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD;

        break;
    case "ct":
    case "consequence_type":
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD;
        unwindPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD;
        numUnwinds = 3;
        break;
    }

    QueryBuilder qb = QueryBuilder.start();
    parseQuery(query, qb);

    boolean count = options != null && options.getBoolean("count", false);
    int order = options != null ? options.getInt("order", -1) : -1;

    DBObject project;
    DBObject projectAndCount;
    if (count) {
        project = new BasicDBObject("$project", new BasicDBObject("field", "$" + documentPath));
        projectAndCount = new BasicDBObject("$project", new BasicDBObject().append("id", "$_id")
                .append("_id", 0).append("count", new BasicDBObject("$size", "$values")));
    } else {
        project = new BasicDBObject("$project", new BasicDBObject().append("field", "$" + documentPath)
                //.append("_id._id", "$_id")
                .append("_id.start", "$" + DBObjectToVariantConverter.START_FIELD)
                .append("_id.end", "$" + DBObjectToVariantConverter.END_FIELD)
                .append("_id.chromosome", "$" + DBObjectToVariantConverter.CHROMOSOME_FIELD)
                .append("_id.alternate", "$" + DBObjectToVariantConverter.ALTERNATE_FIELD)
                .append("_id.reference", "$" + DBObjectToVariantConverter.REFERENCE_FIELD)
                .append("_id.ids", "$" + DBObjectToVariantConverter.IDS_FIELD));
        projectAndCount = new BasicDBObject("$project",
                new BasicDBObject().append("id", "$_id").append("_id", 0).append("values", "$values")
                        .append("count", new BasicDBObject("$size", "$values")));
    }

    DBObject match = new BasicDBObject("$match", qb.get());
    DBObject unwindField = new BasicDBObject("$unwind", "$field");
    DBObject notNull = new BasicDBObject("$match", new BasicDBObject("field", new BasicDBObject("$ne", null)));
    DBObject groupAndAddToSet = new BasicDBObject("$group",
            new BasicDBObject("_id", "$field").append("values", new BasicDBObject("$addToSet", "$_id"))); // sum, count, avg, ...?
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", order)); // 1 = ascending, -1 = descending
    DBObject skip = null;
    if (options != null && options.getInt("skip", -1) > 0) {
        skip = new BasicDBObject("$skip", options.getInt("skip", -1));
    }
    DBObject limit = new BasicDBObject("$limit",
            options != null && options.getInt("limit", -1) > 0 ? options.getInt("limit") : 10);

    List<DBObject> operations = new LinkedList<>();
    operations.add(match);
    operations.add(project);
    for (int i = 0; i < numUnwinds; i++) {
        operations.add(unwindField);
    }
    operations.add(notNull);
    operations.add(groupAndAddToSet);
    operations.add(projectAndCount);
    operations.add(sort);
    if (skip != null) {
        operations.add(skip);
    }
    operations.add(limit);
    logger.debug("db." + collectionName + ".aggregate( " + operations + " )");
    QueryResult<DBObject> queryResult = variantsCollection.aggregate(operations, options);

    //            List<Map<String, Object>> results = new ArrayList<>(queryResult.getResult().size());
    //            results.addAll(queryResult.getResult().stream().map(dbObject -> new ObjectMap("id", dbObject.get("_id")).append("count", dbObject.get("count"))).collect(Collectors.toList()));
    List<Map> results = queryResult.getResult().stream().map(DBObject::toMap).collect(Collectors.toList());

    return new QueryResult<>(queryResult.getId(), queryResult.getDbTime(), queryResult.getNumResults(),
            queryResult.getNumTotalResults(), queryResult.getWarningMsg(), queryResult.getErrorMsg(), results);
}

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 ww  .  ja  v  a 2 s .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.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

@Deprecated
private QueryBuilder parseQueryOptions(QueryOptions options, QueryBuilder builder) {
    if (options != null) {

        if (options.containsKey("sort")) {
            if (options.getBoolean("sort")) {
                options.put("sort", new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, 1)
                        .append(DBObjectToVariantConverter.START_FIELD, 1));
            } else {
                options.remove("sort");
            }/*from   www .  ja va 2 s. co  m*/
        }

        /** GENOMIC REGION **/

        if (options.containsKey(VariantQueryParams.REGION.key())
                && !options.getString(VariantQueryParams.REGION.key()).isEmpty()) {
            List<String> stringList = options.getAsStringList(VariantQueryParams.REGION.key());
            List<Region> regions = new ArrayList<>(stringList.size());
            for (String reg : stringList) {
                Region region = Region.parseRegion(reg);
                regions.add(region);
            }
            getRegionFilter(regions, builder);
        }

        //            if (options.containsKey(VariantQueryParams.CHROMOSOME.key())) {
        //                List<String> chromosome = options.getAsStringList(VariantQueryParams.CHROMOSOME.key());
        //                addQueryListFilter(DBObjectToVariantConverter.CHROMOSOME_FIELD, chromosome, builder, QueryOperation.OR);
        //            }

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

        if (options.getString(VariantQueryParams.ID.key()) != null
                && !options.getString(VariantQueryParams.ID.key()).isEmpty()) { //) && !options.getString("id").isEmpty()) {
            List<String> ids = options.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);
        }

        /** VARIANT **/

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

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

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

        /** ANNOTATION **/

        if (options.containsKey(VariantQueryParams.ANNOTATION_EXISTS.key())) {
            builder.and(DBObjectToVariantConverter.ANNOTATION_FIELD)
                    .exists(options.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key()));
        }

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

        if (options.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())) {
            List<String> cts = new ArrayList<>(
                    options.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.
                }
            }
            options.put(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key(), cts); //Replace the QueryOption without the malformed query params
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD,
                    ctsInteger, builder, QueryOperation.AND);
        }

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

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

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

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

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

        if (options.containsKey(VariantQueryParams.ALTERNATE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    options.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 (options.containsKey(VariantQueryParams.REFERENCE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    options.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 **/

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

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

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

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

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

        //            if (options.get("freqgt") != null && !options.getString("freqgt").isEmpty()) {
        //                for (String freqgt : getStringList(options.get("freqgt"))) {
        //                    String[] split = freqgt.split(":");
        //                    addCompQueryFilter(
        //                            DBObjectToVariantSourceEntryConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.FREQGT_FIELD + "." + split[0],
        //                            split[1], builder);
        //                }
        //            }

        /** FILES **/
        QueryBuilder fileBuilder = QueryBuilder.start();

        if (options.containsKey(VariantQueryParams.STUDIES.key())) { // && !options.getList("studies").isEmpty() && !options.getListAs("studies", String.class).get(0).isEmpty()) {
            addQueryListFilter(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD,
                    options.getAsIntegerList(VariantQueryParams.STUDIES.key()), fileBuilder,
                    QueryOperation.AND);
        }

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

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

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

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

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

            String[] sampleGenotypesArray = sampleGenotypesCSV.split(AND);
            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) {
                    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))));
                }
                fileBuilder.and(genotypesBuilder.get());
            }
        }

        DBObject fileQuery = fileBuilder.get();
        if (fileQuery.keySet().size() != 0) {
            builder.and(DBObjectToVariantConverter.STUDIES_FIELD).elemMatch(fileQuery);
        }
    }

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

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

License:Apache License

@Override
@Deprecated/*  www  .  ja  v a  2  s. co m*/
public QueryResult<Variant> getAllVariants(QueryOptions options) {

    QueryBuilder qb = QueryBuilder.start();
    parseQueryOptions(options, qb);
    DBObject projection = parseProjectionQueryOptions(options);
    logger.debug("Query to be executed {}", qb.get().toString());

    return variantsCollection.find(qb.get(), projection,
            getDbObjectToVariantConverter(new Query(options), options), options);
}

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

License:Apache License

@Override
@Deprecated/*from   w  ww  . java 2  s  . c  o  m*/
public QueryResult<Variant> getVariantById(String id, QueryOptions options) {

    //        BasicDBObject query = new BasicDBObject(DBObjectToVariantConverter.ID_FIELD, id);

    if (options == null) {
        options = new QueryOptions(VariantQueryParams.ID.key(), id);
    } else {
        options.addToListOption(VariantQueryParams.ID.key(), id);
    }

    QueryBuilder qb = QueryBuilder.start();
    parseQueryOptions(options, qb);
    DBObject projection = parseProjectionQueryOptions(options);
    logger.debug("Query to be executed {}", qb.get().toString());

    //        return coll.find(query, options, variantConverter);
    QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection,
            getDbObjectToVariantConverter(new Query(options), options), options);
    queryResult.setId(id);
    return queryResult;
}

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

License:Apache License

@Override
@Deprecated//w  ww  .  j  a v a2  s .c o m
public QueryResult<Variant> getAllVariantsByRegion(Region region, QueryOptions options) {

    QueryBuilder qb = QueryBuilder.start();
    getRegionFilter(region, qb);
    parseQueryOptions(options, qb);
    DBObject projection = parseProjectionQueryOptions(options);

    if (options == null) {
        options = new QueryOptions();
    }

    QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection,
            getDbObjectToVariantConverter(new Query(options), options), options);
    queryResult.setId(region.toString());
    return queryResult;
}

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

License:Apache License

@Deprecated
public QueryResult getAllVariantsByGene(String geneName, QueryOptions options) {
    QueryBuilder qb = QueryBuilder.start();
    if (options == null) {
        options = new QueryOptions(VariantQueryParams.GENE.key(), geneName);
    } else {/*  w ww  . j  av a  2 s  .  c  o  m*/
        options.addToListOption(VariantQueryParams.GENE.key(), geneName);
    }
    options.put(VariantQueryParams.GENE.key(), geneName);
    parseQueryOptions(options, qb);
    DBObject projection = parseProjectionQueryOptions(options);
    QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection,
            getDbObjectToVariantConverter(new Query(options), options), options);
    queryResult.setId(geneName);
    return queryResult;
}

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

License:Apache License

@Override
@Deprecated/*from   ww w  . j  a  v a 2  s .c om*/
public QueryResult groupBy(String field, QueryOptions options) {

    String documentPath;
    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 "ct":
    case "consequence_type":
        documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD;
        break;
    }

    QueryBuilder qb = QueryBuilder.start();
    parseQueryOptions(options, qb);

    DBObject match = new BasicDBObject("$match", qb.get());
    DBObject project = new BasicDBObject("$project", new BasicDBObject("field", "$" + documentPath));
    DBObject unwind = new BasicDBObject("$unwind", "$field");
    DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$field")
            //                .append("field", "$field")
            .append("count", new BasicDBObject("$sum", 1))); // sum, count, avg, ...?
    DBObject sort = new BasicDBObject("$sort",
            new BasicDBObject("count", options != null ? options.getInt("order", -1) : -1)); // 1 = ascending, -1 = descending
    DBObject limit = new BasicDBObject("$limit",
            options != null && options.getInt("limit", -1) > 0 ? options.getInt("limit") : 10);

    return variantsCollection.aggregate(Arrays.asList(match, project, unwind, group, sort, limit), options);
}

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

License:Apache License

@Override
public QueryResult<VariantSource> getAllSources(QueryOptions options) {
    MongoDBCollection coll = db.getCollection(collectionName);
    QueryBuilder qb = QueryBuilder.start();
    parseQueryOptions(options, qb);/*from ww  w  .j  av  a  2s . co  m*/

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