Example usage for com.mongodb.client.model Filters eq

List of usage examples for com.mongodb.client.model Filters eq

Introduction

In this page you can find the example usage for com.mongodb.client.model Filters eq.

Prototype

public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates a filter that matches all documents where the value of the field name equals the specified value.

Usage

From source file:org.opencb.cellbase.lib.impl.ClinicalMongoDBAdaptor.java

License:Apache License

private void getClinvarFilters(Query query, Set<String> sourceContent, List<Bson> sourceBson) {
    List<Bson> andBsonList = new ArrayList<>();

    if (sourceContent != null && sourceContent.contains("clinvar")) {
        andBsonList.add(Filters.eq("source", "clinvar"));
    }/*  w  w w. j  a v a  2 s.c o  m*/

    createOrQuery(query, QueryParams.CLINVARRCV.key(),
            "clinvarSet.referenceClinVarAssertion.clinVarAccession.acc", andBsonList);
    createClinvarRsQuery(query, andBsonList);
    createClinvarTypeQuery(query, andBsonList);
    createClinvarReviewQuery(query, andBsonList);
    createClinvarClinicalSignificanceQuery(query, andBsonList);

    if (andBsonList.size() == 1) {
        sourceBson.add(andBsonList.get(0));
    } else if (andBsonList.size() > 0) {
        sourceBson.add(Filters.and(andBsonList));
    }
}

From source file:org.opencb.cellbase.lib.impl.ClinicalMongoDBAdaptor.java

License:Apache License

private void createClinvarRsQuery(Query query, List<Bson> andBsonList) {
    if (query != null && query.getString(QueryParams.CLINVARRS.key()) != null
            && !query.getString(QueryParams.CLINVARRS.key()).isEmpty()) {
        List<String> queryList = query.getAsStringList(QueryParams.CLINVARRS.key());
        if (queryList.size() == 1) {
            andBsonList.add(Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.id",
                    queryList.get(0).substring(2)));
            andBsonList/*from w  w  w.ja  v  a  2  s  . c  om*/
                    .add(Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.type", "rs"));
        } else {
            List<Bson> orBsonList = new ArrayList<>(queryList.size());
            for (String queryItem : queryList) {
                List<Bson> innerAndBsonList = new ArrayList<>();
                innerAndBsonList
                        .add(Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.id",
                                queryList.get(0).substring(2)));
                innerAndBsonList.add(
                        Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.type", "rs"));
                orBsonList.add(Filters.and(innerAndBsonList));
            }
            andBsonList.add(Filters.or(orBsonList));
        }
    }
}

From source file:org.opencb.cellbase.lib.impl.GeneMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getRegulatoryElements(Query query, QueryOptions queryOptions) {
    Bson bson = parseQuery(query);//  w w  w . j  av a2 s.  c o m
    QueryResult<Document> queryResult = null;
    QueryResult<Document> gene = mongoDBCollection.find(bson,
            new QueryOptions(MongoDBCollection.INCLUDE, "chromosome,start,end"));
    if (gene != null) {
        MongoDBCollection regulatoryRegionCollection = mongoDataStore.getCollection("regulatory_region");
        for (Document document : gene.getResult()) {
            //                String region = document.getString("chromosome") + ":"
            //                        + document.getInteger("start", 1) + "-" + document.getInteger("end", Integer.MAX_VALUE);
            //                query.put(RegulationDBAdaptor.QueryParams.REGION.key(), region);
            Bson eq = Filters.eq("chromosome", document.getString("chromosome"));
            Bson lte = Filters.lte("start", document.getInteger("end", Integer.MAX_VALUE));
            Bson gte = Filters.gte("end", document.getInteger("start", 1));
            queryResult = regulatoryRegionCollection.find(Filters.and(eq, lte, gte), queryOptions);
        }
    }
    return queryResult;
}

From source file:org.opencb.cellbase.lib.impl.MongoDBAdaptor.java

License:Apache License

protected void createRegionQuery(Query query, String queryParam, List<Bson> andBsonList) {
    if (query != null && query.getString(queryParam) != null && !query.getString(queryParam).isEmpty()) {
        List<Region> regions = Region.parseRegions(query.getString(queryParam));
        if (regions != null && regions.size() > 0) {
            // if there is only one region we add the AND filter directly to the andBsonList passed
            if (regions.size() == 1) {
                Bson chromosome = Filters.eq("chromosome", regions.get(0).getChromosome());
                Bson start = Filters.lte("start", regions.get(0).getEnd());
                Bson end = Filters.gte("end", regions.get(0).getStart());
                andBsonList.add(Filters.and(chromosome, start, end));
            } else {
                // when multiple regions then we create and OR list before add it to andBsonList
                List<Bson> orRegionBsonList = new ArrayList<>(regions.size());
                for (Region region : regions) {
                    Bson chromosome = Filters.eq("chromosome", region.getChromosome());
                    Bson start = Filters.lte("start", region.getEnd());
                    Bson end = Filters.gte("end", region.getStart());
                    orRegionBsonList.add(Filters.and(chromosome, start, end));
                }//from   w w w  . j  av a 2  s . c om
                andBsonList.add(Filters.or(orRegionBsonList));
            }
        } else {
            logger.warn("Region query no created, region object is null or empty.");
        }
    }
}

From source file:org.opencb.cellbase.lib.impl.MongoDBAdaptor.java

License:Apache License

protected <T> void createOrQuery(List<T> queryValues, String mongoDbField, List<Bson> andBsonList) {
    if (queryValues.size() == 1) {
        andBsonList.add(Filters.eq(mongoDbField, queryValues.get(0)));
    } else {//from ww  w  .j  av a2 s  . c  om
        List<Bson> orBsonList = new ArrayList<>(queryValues.size());
        for (T queryItem : queryValues) {
            orBsonList.add(Filters.eq(mongoDbField, queryItem));
        }
        andBsonList.add(Filters.or(orBsonList));
    }
}

From source file:org.opencb.cellbase.lib.impl.ProteinMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult<Score> getSubstitutionScores(Query query, QueryOptions options) {
    QueryResult result = null;//from   w  w  w  .  j a  v  a2 s .  c  o m

    // Ensembl transcript id is needed for this collection
    if (query.getString("transcript") != null) {
        Bson transcript = Filters.eq("transcriptId", query.getString("transcript"));

        int position = -1;
        String aaShortName = null;
        // If position and aa change are provided we create a 'projection' to return only the required data from the database
        if (query.get("position") != null && !query.getString("position").isEmpty()
                && query.getInt("position", 0) != 0) {
            position = query.getInt("position");
            String projectionString = "aaPositions." + position;

            // If aa change is provided we only return that information
            if (query.getString("aa") != null && !query.getString("aa").isEmpty()) {
                aaShortName = aaShortNameMap.get(query.getString("aa").toUpperCase());
                projectionString += "." + aaShortName;
            }

            // Projection is used to minimize the returned data
            Bson positionProjection = Projections.include(projectionString);
            result = proteinSubstitutionMongoDBCollection.find(transcript, positionProjection, options);
        } else {
            // Return the whole transcript data
            result = proteinSubstitutionMongoDBCollection.find(transcript, options);
        }

        if (result != null && !result.getResult().isEmpty()) {
            Document document = (Document) result.getResult().get(0);
            Document aaPositionsDocument = (Document) document.get("aaPositions");

            // Position or aa change were not provided, returning whole transcript data
            if (position == -1 || aaShortName == null) {
                // Return only the inner Document, not the whole document projected
                result.setResult(Collections.singletonList(aaPositionsDocument));
                // Position and aa were provided, return only corresponding Score objects
            } else {
                List<Score> scoreList = null;
                if (result.getNumResults() == 1 && aaPositionsDocument != null) {
                    scoreList = new ArrayList<>(NUM_PROTEIN_SUBSTITUTION_SCORE_METHODS);
                    Document positionDocument = (Document) aaPositionsDocument.get(Integer.toString(position));
                    Document aaDocument = (Document) positionDocument.get(aaShortName);
                    //                        Document proteinSubstitutionScores = (Document) result.getResult().get(0);
                    if (aaDocument.get("ss") != null) {
                        scoreList.add(new Score(Double.parseDouble("" + aaDocument.get("ss")), "sift",
                                VariantAnnotationUtils.SIFT_DESCRIPTIONS.get(aaDocument.get("se"))));
                    }
                    if (aaDocument.get("ps") != null) {
                        scoreList.add(new Score(Double.parseDouble("" + aaDocument.get("ps")), "polyphen",
                                VariantAnnotationUtils.POLYPHEN_DESCRIPTIONS.get(aaDocument.get("pe"))));
                    }
                }
                result.setResult(scoreList);
                //                    result.setResult(Collections.singletonList(scoreList));
            }
            //            // Return empty QueryResult if the query did not return any result
            //            } else {
            //                return result;
        }
    }
    // Return null if no transcript id is provided
    return result;

}

From source file:org.opencb.cellbase.lib.impl.VariantMongoDBAdaptor.java

License:Apache License

private Bson getGeneQuery(String geneId) {
    //        List<Bson> orBsonList = new ArrayList<>(3);
    //        orBsonList.add(Filters.eq("annotation.consequenceTypes.geneName", geneId));
    //        orBsonList.add(Filters.eq("annotation.consequenceTypes.ensemblGeneId", geneId));
    //        orBsonList.add(Filters.eq("annotation.consequenceTypes.ensemblTranscriptId", geneId));

    // For some reason Mongo does not deal properly with OR queries and indexes. It is extremely slow to perform
    // the commented query above. On the contrary this query below provides instant results
    if (geneId.startsWith(ENSEMBL_GENE_ID_PATTERN)) {
        return Filters.eq("annotation.consequenceTypes.ensemblGeneId", geneId);
    } else if (geneId.startsWith(ENSEMBL_TRANSCRIPT_ID_PATTERN)) {
        return Filters.eq("annotation.consequenceTypes.ensemblTranscriptId", geneId);
    } else {/*from  w w  w .  j  a va  2 s  . c o  m*/
        return Filters.eq("annotation.consequenceTypes.geneName", geneId);
    }
}

From source file:org.opencb.cellbase.mongodb.impl.ClinicalMongoDBAdaptor.java

License:Apache License

private void getCosmicFilters(Query query, Set<String> sourceContent, List<Bson> sourceBson) {
    // If only clinvar-specific filters are provided it must be avoided to include the source=cosmic condition since
    // sourceBson is going to be an OR list
    if (!(query.containsKey(QueryParams.CLINVARRCV.key()) || query.containsKey(QueryParams.CLINVARCLINSIG.key())
            || query.containsKey(QueryParams.CLINVARREVIEW.key())
            || query.containsKey(QueryParams.CLINVARTYPE.key())
            || query.containsKey(QueryParams.CLINVARRS.key()))) {
        if (sourceContent != null && sourceContent.contains("cosmic")) {
            sourceBson.add(Filters.eq("source", "cosmic"));
        }/*from w w  w .  jav a  2  s  . c o  m*/
    }
}

From source file:org.opencb.cellbase.mongodb.impl.MongoDBAdaptor.java

License:Apache License

protected void createOrQuery(List<String> queryValues, String mongoDbField, List<Bson> andBsonList) {
    if (queryValues.size() == 1) {
        andBsonList.add(Filters.eq(mongoDbField, queryValues.get(0)));
    } else {/*from   w ww  .  j a va  2 s.co m*/
        List<Bson> orBsonList = new ArrayList<>(queryValues.size());
        for (String queryItem : queryValues) {
            orBsonList.add(Filters.eq(mongoDbField, queryItem));
        }
        andBsonList.add(Filters.or(orBsonList));
    }
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java

License:Apache License

public static <T> Bson createFilter(String mongoDbField, T queryValue, ComparisonOperator comparator) {
    Bson filter = null;// w w w  .  jav a2 s.  com

    if (queryValue != null) {
        if (queryValue instanceof String) {
            switch (comparator) {
            case EQUALS:
                filter = Filters.eq(mongoDbField, queryValue);
                break;
            case NOT_EQUALS:
                filter = Filters.ne(mongoDbField, queryValue);
                break;
            case EQUAL_IGNORE_CASE:
                filter = Filters.regex(mongoDbField, queryValue.toString(), "i");
                break;
            case STARTS_WITH:
                filter = Filters.regex(mongoDbField, "^" + queryValue + "*");
                break;
            case ENDS_WITH:
                filter = Filters.regex(mongoDbField, "*" + queryValue + "$");
                break;
            case REGEX:
                filter = Filters.regex(mongoDbField, queryValue.toString());
                break;
            case TEXT:
                filter = Filters.text(String.valueOf(queryValue));
                break;
            default:
                break;
            }
        } else {
            switch (comparator) {
            case EQUALS:
                filter = Filters.eq(mongoDbField, queryValue);
                break;
            case NOT_EQUALS:
                filter = Filters.ne(mongoDbField, queryValue);
                break;
            case GREATER_THAN:
                filter = Filters.gt(mongoDbField, queryValue);
                break;
            case GREATER_THAN_EQUAL:
                filter = Filters.gte(mongoDbField, queryValue);
                break;
            case LESS_THAN:
                filter = Filters.lt(mongoDbField, queryValue);
                break;
            case LESS_THAN_EQUAL:
                filter = Filters.lte(mongoDbField, queryValue);
                break;
            default:
                break;
            }
        }
    }
    return filter;
}