Example usage for com.mongodb QueryBuilder get

List of usage examples for com.mongodb QueryBuilder get

Introduction

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

Prototype

public DBObject get() 

Source Link

Document

Creates a DBObject query to be used for the driver's find operations

Usage

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);//  ww  w . j a  v a 2 s . c om

    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);/*  www.j  a v a 2  s. c o m*/
    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();
    }//from   w w  w  .j a v  a2s .c  o  m
    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);/*from  w  ww .j  a va  2 s.  co  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  w w w  .j a  v a2s.  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());
}

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();
    }/*from w ww .ja v a2s. c  o  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  www . ja va2s  .  c  om*/
    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);/*  www  . j a v a  2  s . com*/
            }
            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

private QueryBuilder addScoreFilter(String key, List<String> list, QueryBuilder builder) {
    List<DBObject> ands = new ArrayList<>();
    for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
        String elem = iterator.next();
        String[] populationFrequency = splitKeyValue(elem);
        QueryBuilder scoreBuilder = new QueryBuilder();
        scoreBuilder.and(DBObjectToVariantAnnotationConverter.SCORE_SOURCE_FIELD).is(populationFrequency[0]);
        addCompQueryFilter(DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD, populationFrequency[1],
                scoreBuilder);//w  w  w  .j ava2s.  c  o m
        ands.add(new BasicDBObject(key, new BasicDBObject("$elemMatch", scoreBuilder.get())));
    }
    if (!ands.isEmpty()) {
        builder.and(ands.toArray(new DBObject[ands.size()]));
    }
    return builder;
}