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.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByGenomicVariantList(List<GenomicVariant> variantList, QueryOptions options) {
    List<DBObject> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(variantList.size());
    List<QueryResult> queryResultList;
    for (GenomicVariant genomicVariant : variantList) {
        QueryBuilder builder = QueryBuilder.start("chromosome").is(genomicVariant.getChromosome()).and("start")
                .is(genomicVariant.getPosition()).and("alternate").is(genomicVariant.getAlternative());
        if (genomicVariant.getReference() != null) {
            builder = builder.and("reference").is(genomicVariant.getReference());
        }//from   w w  w  . jav  a 2  s.c om
        queries.add(builder.get());
        ids.add(genomicVariant.toString());
    }

    queryResultList = executeQueryList2(ids, queries, options);

    for (QueryResult queryResult : queryResultList) {
        List<BasicDBObject> clinicalList = (List<BasicDBObject>) queryResult.getResult();

        List<Cosmic> cosmicList = null;
        List<Gwas> gwasList = null;
        List<Clinvar> clinvarList = null;

        for (Object clinicalObject : clinicalList) {
            BasicDBObject clinical = (BasicDBObject) clinicalObject;

            if (isCosmic(clinical)) {
                Cosmic cosmic = getCosmic(clinical);
                if (cosmicList == null) {
                    cosmicList = new ArrayList<>();
                }
                cosmicList.add(cosmic);
            } else if (isGwas(clinical)) {
                Gwas gwas = getGwas(clinical);
                if (gwasList == null) {
                    gwasList = new ArrayList<>();
                }
                gwasList.add(gwas);

            } else if (isClinvar(clinical)) {
                Clinvar clinvar = getClinvar(clinical);
                if (clinvarList == null) {
                    clinvarList = new ArrayList<>();
                }
                clinvarList.add(clinvar);
            }
        }
        Map<String, Object> clinicalData = new HashMap<>();
        clinicalData.put("Cosmic", cosmicList);
        clinicalData.put("Gwas", gwasList);
        clinicalData.put("Clinvar", clinvarList);
        // FIXME quick solution to compile
        //            queryResult.setResult(clinicalData);
        queryResult.setResult(Arrays.asList(clinicalData));
        queryResult.setNumResults(1);
    }

    return queryResultList;
}

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

License:Apache License

public QueryResult getListClinvarAccessions(QueryOptions queryOptions) {
    QueryBuilder builder = QueryBuilder.start("clinvarSet.referenceClinVarAssertion.clinVarAccession.acc")
            .exists(true);/*from w  w w .  j a v a2  s.c  o m*/
    queryOptions.put("include", Arrays.asList("clinvarSet.referenceClinVarAssertion.clinVarAccession.acc"));
    QueryResult queryResult = executeQuery("", builder.get(), queryOptions);
    List accInfoList = (List) queryResult.getResult();
    List<String> accList = new ArrayList<>(accInfoList.size());
    BasicDBObject accInfo;
    QueryResult listAccessionsToReturn = new QueryResult();

    for (Object accInfoObject : accInfoList) {
        accInfo = (BasicDBObject) accInfoObject;
        accInfo = (BasicDBObject) accInfo.get("clinvarSet");
        accList.add((String) ((BasicDBObject) ((BasicDBObject) ((BasicDBObject) accInfo
                .get("referenceClinVarAssertion"))).get("clinVarAccession")).get("acc"));
    }

    // setting listAccessionsToReturn fields
    listAccessionsToReturn.setId(queryResult.getId());
    listAccessionsToReturn.setDbTime(queryResult.getDbTime());
    listAccessionsToReturn.setNumResults(queryResult.getNumResults());
    listAccessionsToReturn.setNumTotalResults(queryResult.getNumTotalResults());
    listAccessionsToReturn.setResult(accList);

    return listAccessionsToReturn;
}

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

License:Apache License

public QueryResult updateAnnotations(List<VariantAnnotation> variantAnnotations, QueryOptions queryOptions) {

    /**/*w w  w .  ja v a  2s  .  c  o  m*/
     * Multiple documents may contain the same annotation
     */
    queryOptions.put("multi", true);

    /**
     * Prepare jackson to generate json strings
     */
    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    jsonObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    ObjectWriter writer = jsonObjectMapper.writer();

    long start = System.nanoTime();
    for (VariantAnnotation variantAnnotation : variantAnnotations) {
        QueryBuilder builder = QueryBuilder.start("chromosome").is(variantAnnotation.getChromosome())
                .and("start").is(variantAnnotation.getStart()).and("reference")
                .is(variantAnnotation.getReferenceAllele()).and("alternate")
                .is(variantAnnotation.getAlternativeAllele());
        DBObject update = null;
        try {
            update = new BasicDBObject("$set",
                    new BasicDBObject("annot", JSON.parse(writer.writeValueAsString(variantAnnotation))));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //            DBObject update = new BasicDBObject("$set", new BasicDBObject("annotation",
        //                    convertVariantAnnotation(variantAnnotation)));
        mongoDBCollection2.update(builder.get(), update, queryOptions);
    }

    return new QueryResult<>("", ((int) (System.nanoTime() - start)), 1, 1, "", "", new ArrayList());
}

From source file:org.opencb.cellbase.mongodb.db.ClinVarMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<DBObject> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("referenceClinVarAssertion.clinVarAccession.acc").is(id);
        queries.add(builder.get());/*from   w  ww  .j a v a2s  . c o  m*/
    }

    return executeQueryList(idList, queries, options);
}

From source file:org.opencb.cellbase.mongodb.db.ClinVarMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    List<DBObject> queries = new ArrayList<>();

    List<String> ids = new ArrayList<>(regions.size());
    for (Region region : regions) {

        // If regions is 1 position then query can be optimize using chunks
        QueryBuilder builder = QueryBuilder
                .start("referenceClinVarAssertion.measureSet.measure.measureRelationship.sequenceLocation.chr")
                .is(region.getChromosome())
                .and("referenceClinVarAssertion.measureSet.measure.measureRelationship.sequenceLocation.stop")
                .greaterThanEquals(region.getStart())
                .and("referenceClinVarAssertion.measureSet.measure.measureRelationship.sequenceLocation.start")
                .lessThanEquals(region.getEnd());
        System.out.println(builder.get().toString());
        queries.add(builder.get());/*from  w w  w. j av a 2  s.co m*/
        ids.add(region.toString());
    }
    return executeQueryList(ids, queries, options);
}

From source file:org.opencb.cellbase.mongodb.db.ConservationMongoDBAdaptor.java

License:Apache License

@Deprecated
@Override/*from   w w  w .  jav a 2s  . c om*/
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<DBObject> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<String> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), this.chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            ////                    integerChunkIds.add(chunkId);
            //                    integerChunkIds.add(region.getChromosome() + "_" + chunkId + "_" + this.chunkSize/1000 + "k");
            //                }
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(hunkIds);
        /****/

        queries.add(builder.get());
        ids.add(region.toString());

        logger.info(builder.get().toString());
    }

    List<QueryResult> queryResults = executeQueryList(ids, queries, options);

    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        BasicDBList list = (BasicDBList) queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            BasicDBObject chunk = (BasicDBObject) list.get(j);
            String type = chunk.getString("type");
            List<Float> valuesList;
            if (!typeMap.containsKey(type)) {
                valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                    valuesList.add(null);
                }
                typeMap.put(type, valuesList);
            } else {
                valuesList = typeMap.get(type);
            }

            BasicDBList valuesChunk = (BasicDBList) chunk.get("values");

            int pos = 0;
            if (region.getStart() > chunk.getInt("start")) {
                pos = region.getStart() - chunk.getInt("start");
            }

            for (; pos < valuesChunk.size() && (pos + chunk.getInt("start") <= region.getEnd()); pos++) {
                //                    System.out.println("valuesList SIZE = " + valuesList.size());
                //                    System.out.println("pos = " + pos);
                //                    System.out.println("DIV " + (chunk.getInt("start") - region.getStart()));
                //                    System.out.println("valuesChunk = " + valuesChunk.get(pos));
                //                    System.out.println("indexFinal = " + (pos + chunk.getInt("start") - region.getStart()));
                valuesList.set(pos + chunk.getInt("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }
        //
        BasicDBList resultList = new BasicDBList();
        ConservedRegionFeature conservedRegionChunk;
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            conservedRegionChunk = new ConservedRegionFeature(region.getChromosome(), region.getStart(),
                    region.getEnd(), elem.getKey(), elem.getValue());
            resultList.add(conservedRegionChunk);
        }
        queryResult.setResult(resultList);
    }

    return queryResults;
}

From source file:org.opencb.cellbase.mongodb.db.ConservationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllScoresByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<DBObject> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<Integer> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);//  w  w  w  .j a va2 s  .  c  o m
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            //                    integerChunkIds.add(chunkId);
            //                }
            //    //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(hunkIds);
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        /****/

        queries.add(builder.get());
        ids.add(region.toString());

        logger.info(builder.get().toString());

    }
    List<QueryResult> queryResults = executeQueryList2(ids, queries, options);
    //        List<QueryResult> queryResults = executeQueryList(ids, queries, options);

    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        List<BasicDBObject> list = (List<BasicDBObject>) queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            BasicDBObject chunk = (BasicDBObject) list.get(j);
            String type = chunk.getString("type");
            List<Float> valuesList;
            if (!typeMap.containsKey(type)) {
                valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                    valuesList.add(null);
                }
                typeMap.put(type, valuesList);
            } else {
                valuesList = typeMap.get(type);
            }

            BasicDBList valuesChunk = (BasicDBList) chunk.get("values");

            int pos = 0;
            if (region.getStart() > chunk.getInt("start")) {
                pos = region.getStart() - chunk.getInt("start");
            }

            for (; pos < valuesChunk.size() && (pos + chunk.getInt("start") <= region.getEnd()); pos++) {
                valuesList.set(pos + chunk.getInt("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }

        BasicDBList resultList = new BasicDBList();
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            for (Float value : elem.getValue()) {
                resultList.add(value != null ? (new Score(new Double(value), elem.getKey())) : null);
            }
        }
        queryResult.setResult(resultList);
    }

    return queryResults;
}

From source file:org.opencb.cellbase.mongodb.db.core.ConservationMongoDBAdaptor.java

License:Apache License

@Deprecated
@Override//  w w w  . j a  v  a 2  s  . co  m
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<DBObject> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<String> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        // Max region size is 10000bp
        if (region.getEnd() - region.getStart() > 10000) {
            region.setEnd(region.getStart() + 10000);
        }

        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), this.chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            ////                    integerChunkIds.add(chunkId);
            //                    integerChunkIds.add(region.getChromosome() + "_" + chunkId + "_" + this.chunkSize/1000 + "k");
            //                }
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(hunkIds);
        /****/

        queries.add(builder.get());
        ids.add(region.toString());

        logger.debug(builder.get().toString());
    }

    List<QueryResult> queryResults = executeQueryList2(ids, queries, options);
    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        //            BasicDBList list = (BasicDBList) queryResult.getResult();
        List list = queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            BasicDBObject chunk = (BasicDBObject) list.get(j);
            String type = chunk.getString("type");
            List<Float> valuesList;
            if (!typeMap.containsKey(type)) {
                valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                    valuesList.add(null);
                }
                typeMap.put(type, valuesList);
            } else {
                valuesList = typeMap.get(type);
            }

            BasicDBList valuesChunk = (BasicDBList) chunk.get("values");

            int pos = 0;
            if (region.getStart() > chunk.getInt("start")) {
                pos = region.getStart() - chunk.getInt("start");
            }

            for (; pos < valuesChunk.size() && (pos + chunk.getInt("start") <= region.getEnd()); pos++) {
                //                    System.out.println("valuesList SIZE = " + valuesList.size());
                //                    System.out.println("pos = " + pos);
                //                    System.out.println("DIV " + (chunk.getInt("start") - region.getStart()));
                //                    System.out.println("valuesChunk = " + valuesChunk.get(pos));
                //                    System.out.println("indexFinal = " + (pos + chunk.getInt("start") - region.getStart()));
                valuesList.set(pos + chunk.getInt("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }
        //
        BasicDBList resultList = new BasicDBList();
        ConservedRegionFeature conservedRegionChunk;
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            conservedRegionChunk = new ConservedRegionFeature(region.getChromosome(), region.getStart(),
                    region.getEnd(), elem.getKey(), elem.getValue());
            resultList.add(conservedRegionChunk);
        }
        queryResult.setResult(resultList);
    }

    return queryResults;
}

From source file:org.opencb.cellbase.mongodb.db.core.ConservationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllScoresByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<DBObject> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<Integer> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);/*from w ww  .  j a va2 s  .  c  o  m*/
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            //                    integerChunkIds.add(chunkId);
            //                }
            //    //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(hunkIds);
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        /****/

        queries.add(builder.get());
        ids.add(region.toString());

        logger.debug(builder.get().toString());

    }
    List<QueryResult> queryResults = executeQueryList2(ids, queries, options);
    //        List<QueryResult> queryResults = executeQueryList(ids, queries, options);

    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        List<BasicDBObject> list = (List<BasicDBObject>) queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            BasicDBObject chunk = (BasicDBObject) list.get(j);
            String type = chunk.getString("type");
            List<Float> valuesList;
            if (!typeMap.containsKey(type)) {
                valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                    valuesList.add(null);
                }
                typeMap.put(type, valuesList);
            } else {
                valuesList = typeMap.get(type);
            }

            BasicDBList valuesChunk = (BasicDBList) chunk.get("values");

            int pos = 0;
            if (region.getStart() > chunk.getInt("start")) {
                pos = region.getStart() - chunk.getInt("start");
            }

            for (; pos < valuesChunk.size() && (pos + chunk.getInt("start") <= region.getEnd()); pos++) {
                valuesList.set(pos + chunk.getInt("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }

        BasicDBList resultList = new BasicDBList();
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            for (Float value : elem.getValue()) {
                if (value != null) {
                    resultList.add(new Score(new Double(value), elem.getKey()));
                }
            }
        }
        if (!resultList.isEmpty()) {
            queryResult.setResult(resultList);
        } else {
            queryResult.setResult(null);
        }
    }

    return queryResults;
}

From source file:org.opencb.cellbase.mongodb.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    //      QueryBuilder builder = QueryBuilder.start("transcripts.xrefs.id").in(idList);

    List<DBObject> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("transcripts.xrefs.id").is(id);
        queries.add(builder.get());//from www . j  ava  2  s. co  m
    }

    //        options = addExcludeReturnFields("transcripts", options);
    //        return executeQueryList(idList, queries, options);
    return executeQueryList2(idList, queries, options);
}