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.lib.db.regulatory.RegulatoryRegionMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult next(String chromosome, int position, QueryOptions options) {

    String featureType = options.getString("featureType", null);
    String featureClass = options.getString("featureClass", null);

    BasicDBList chunksId = new BasicDBList();
    String chunkId = chromosome + "_" + getChunkId(position, regulatoryRegionChunkSize) + "_"
            + regulatoryRegionChunkSize / 1000 + "k";
    chunksId.add(chunkId);/* ww w  .j a v a2 s  . c  o  m*/

    // TODO: Add query to find next item considering next chunk
    // db.regulatory_region.find({ "chromosome" : "19" , "start" : { "$gt" : 62005} , "featureType"
    // : "TF_binding_site_motif"}).sort({start:1}).limit(1)

    QueryBuilder builder;
    if (options.getString("strand") == null
            || (options.getString("strand").equals("1") || options.getString("strand").equals("+"))) {
        // db.core.find({chromosome: "1", start: {$gt: 1000000}}).sort({start: 1}).limit(1)
        builder = QueryBuilder.start("_chunkIds").in(chunksId).and("chromosome").is(chromosome).and("start")
                .greaterThan(position);
        options.put("sort", new Document("start", 1));
        options.put("limit", 1);
    } else {
        builder = QueryBuilder.start("_chunkIds").in(chunksId).and("chromosome").is(chromosome).and("end")
                .lessThan(position);
        options.put("sort", new Document("end", -1));
        options.put("limit", 1);
    }

    if (featureType != null) {
        builder.and("featureType").is(featureType);
    }
    if (featureClass != null) {
        builder.and("featureClass").is(featureClass);
    }
    System.out.println(new Document(builder.get().toMap()));
    return executeQuery("result", new Document(builder.get().toMap()), options);
}

From source file:org.opencb.cellbase.lib.db.regulatory.TfbsMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>();
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("name").is(id).and("featureType").is("TF_binding_site_motif");
        //            System.out.println("Query: " + builder.get());
        queries.add(new Document(builder.get().toMap()));
    }/*  w  ww.  jav  a2  s. c o m*/
    options = addExcludeReturnFields("chunkIds", options);
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.systems.ProteinProteinInteractionMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("xrefs.id").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//from  w  w  w.  ja  v a 2s  .  c  om
    //        options = addExcludeReturnFields("transcripts", options);
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.ClinicalMongoDBAdaptor.java

License:Apache License

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

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

        QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());

        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());//w w w .  j a  va  2s.c  o m
    }
    return executeQueryList2(ids, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.ClinicalMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByGenomicVariantList(List<Variant> variantList, QueryOptions options) {
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(variantList.size());
    List<QueryResult> queryResultList;
    for (Variant genomicVariant : variantList) {
        QueryBuilder builder = QueryBuilder.start("chromosome").is(genomicVariant.getChromosome()).and("start")
                .is(genomicVariant.getStart()).and("alternate").is(genomicVariant.getAlternate());
        if (genomicVariant.getReference() != null) {
            builder = builder.and("reference").is(genomicVariant.getReference());
        }//ww w .j  a va 2  s  .c o  m
        queries.add(new Document(builder.get().toMap()));
        ids.add(genomicVariant.toString());
    }

    queryResultList = executeQueryList2(ids, queries, options);

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

        List<Cosmic> cosmicList = new ArrayList<>();
        List<Gwas> gwasList = new ArrayList<>();
        List<ClinVar> clinvarList = new ArrayList<>();

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

            if (isCosmic(clinical)) {
                Cosmic cosmic = getCosmic(clinical);
                cosmicList.add(cosmic);
            } else if (isGwas(clinical)) {
                Gwas gwas = getGwas(clinical);
                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<>();
        //            if(cosmicList!=null && cosmicList.size()>0) {
        //                clinicalData.put("cosmic", cosmicList);
        //            }
        //            if(gwasList!=null && gwasList.size()>0) {
        //                clinicalData.put("gwas", gwasList);
        //            }
        //            if(clinvarList!=null && clinvarList.size()>0) {
        //                clinicalData.put("clinvar", clinvarList);
        //            }
        VariantTraitAssociation variantTraitAssociation = new VariantTraitAssociation(clinvarList, gwasList,
                cosmicList);
        if (!(variantTraitAssociation.getCosmic().isEmpty() && variantTraitAssociation.getGwas().isEmpty()
                && variantTraitAssociation.getClinvar().isEmpty())) {

            // FIXME quick solution to compile
            // queryResult.setResult(clinicalData);
            queryResult.setResult(Collections.singletonList(variantTraitAssociation));
            queryResult.setNumResults(variantTraitAssociation.getCosmic().size()
                    + variantTraitAssociation.getGwas().size() + variantTraitAssociation.getClinvar().size());
        } else {
            queryResult.setResult(null);
            queryResult.setNumResults(0);
        }
    }

    return queryResultList;
}

From source file:org.opencb.cellbase.lib.db.variation.ClinicalMongoDBAdaptor.java

License:Apache License

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

    for (Object accInfoObject : accInfoList) {
        accInfo = (Document) accInfoObject;
        accInfo = (Document) accInfo.get("clinvarSet");
        accList.add((String) ((Document) ((Document) ((Document) 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.lib.db.variation.ClinicalMongoDBAdaptor.java

License:Apache License

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

    /**/*w w w .  j  a v  a 2s  .c  om*/
     * 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);
    jsonObjectMapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true);
    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.getReference()).and("alternate").is(variantAnnotation.getAlternate());
        Document update = null;
        try {
            update = new Document("$set",
                    new Document("annot", JSON.parse(writer.writeValueAsString(variantAnnotation))));
            update.put("$addToSet",
                    new Document("_geneIds", new Document("$each", getGeneIds(variantAnnotation))));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //            Document update = new Document("$set", new Document("annotation",
        //                    convertVariantAnnotation(variantAnnotation)));
        mongoDBCollection.update(new Document(builder.get().toMap()), update, queryOptions);
    }

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

From source file:org.opencb.cellbase.lib.db.variation.ClinVarMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("referenceClinVarAssertion.clinVarAccession.acc").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//from w  ww  . ja  v a  2s .co m

    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.ClinVarMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    List<Document> 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(new Document(builder.get().toMap()).toString());
        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());/*from  w w w .  j  a v a2s . co m*/
    }
    return executeQueryList2(ids, queries, options);
}

From source file:org.opencb.cellbase.lib.db.variation.MutationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("id").is(id);
        queries.add(new Document(builder.get().toMap()));
    }/*from   www  . j a  va  2s  .com*/

    return executeQueryList2(idList, queries, options);
}