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.core.GenomeMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllSequencesByRegionList(List<Region> regions, QueryOptions options) {
    /****//*from ww  w .j  av  a2 s . c  om*/
    String chunkIdSuffix = this.chunkSize / 1000 + "k";
    /****/

    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<String> chunkIds;
    List<Integer> integerChunkIds;
    for (Region region : regions) {
        chunkIds = new ArrayList<>();
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            String chunkIdStr = region.getChromosome() + "_" + chunkId + "_" + chunkIdSuffix;
            chunkIds.add(chunkIdStr);
            integerChunkIds.add(chunkId);
        }
        //            QueryBuilder builder = QueryBuilder.start("sequenceName").is(region.getChromosomeInfo()).and("_chunkIds").in(chunkIds);
        QueryBuilder builder = QueryBuilder.start("_chunkIds").in(chunkIds);
        /****/
        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());

        logger.info(new Document(builder.get().toMap()).toString());
    }

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

        List list = queryResult.getResult();
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < list.size(); j++) {
            Document chunk = (Document) list.get(j);
            sb.append(chunk.get("sequence"));
        }

        int startStr = getOffset(region.getStart());
        int endStr = getOffset(region.getStart()) + (region.getEnd() - region.getStart()) + 1;

        String subStr = "";

        if (getChunkId(region.getStart(), this.chunkSize) > 0) {
            if (sb.toString().length() > 0 && sb.toString().length() >= endStr) {
                subStr = sb.toString().substring(startStr, endStr);
            }
        } else {
            if (sb.toString().length() > 0 && sb.toString().length() + 1 >= endStr) {
                subStr = sb.toString().substring(startStr - 1, endStr - 1);
            }
        }
        logger.info("((Document)list.get(0)).getString(\"sequenceType\") = {}",
                ((Document) list.get(0)).getString("sequenceType"));
        logger.info("((Document)list.get(0)).getString(\"assembly\") = {}",
                ((Document) list.get(0)).getString("assembly"));
        GenomeSequenceFeature genomeSequenceFeature = new GenomeSequenceFeature(region.getChromosome(),
                region.getStart(), region.getEnd(), 1, ((Document) list.get(0)).getString("sequenceType"),
                ((Document) list.get(0)).getString("assembly"), subStr);

        queryResult.setResult(Arrays.asList(genomeSequenceFeature));
    }

    return queryResults;
}

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.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("name").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//from  w  w w  . j  a v  a  2s.c o  m

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

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByXrefList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("dbReference.id").is(id);
        queries.add(new Document(builder.get().toMap()));
    }/*from  w w w .jav a 2s.c  o m*/

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

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllFunctionPredictionByEnsemblTranscriptIdList(List<String> transcriptIdList,
        QueryOptions options) {//from  w  w  w .  j a v  a  2  s . c  o m
    List<Document> queries = new ArrayList<>(transcriptIdList.size());

    if (options.containsKey("aaPosition")) {
        if (options.containsKey("aaChange")) {
            addIncludeReturnFields(
                    "aaPositions." + options.getString("aaPosition") + "." + options.getString("aaChange"),
                    options);
        } else {
            addIncludeReturnFields("aaPositions." + options.getString("aaPosition"), options);
        }
    }

    for (String id : transcriptIdList) {
        QueryBuilder builder = QueryBuilder.start("transcriptId").is(id);
        queries.add(new Document(builder.get().toMap()));
    }

    //        options = addExcludeReturnFields("transcripts", options);
    return executeQueryList2(transcriptIdList, queries, options, proteinFunctionalPredictionCollection);
}

From source file:org.opencb.cellbase.lib.db.core.ProteinMongoDBAdaptor.java

License:Apache License

public QueryResult getFunctionPredictionByAaChange(String transcriptId, Integer aaPosition, String newAa,
        QueryOptions queryOptions) {//  www  . j  a  v a 2s.  c o  m

    QueryBuilder builder = QueryBuilder.start("transcriptId").is(transcriptId);
    QueryResult allChangesQueryResult = executeQuery(transcriptId, new Document(builder.get().toMap()),
            queryOptions, proteinFunctionalPredictionCollection);

    QueryResult proteinSubstitionScoresQueryResult = new QueryResult();
    proteinSubstitionScoresQueryResult.setDbTime(allChangesQueryResult.getDbTime());
    proteinSubstitionScoresQueryResult.setId(transcriptId + "-" + aaPosition + "-" + newAa);

    //<<<<<<< HEAD
    //        String currentAaShortName = aaShortName.get(newAa);
    //        Map aaPositions = ((HashMap) ((Document) allChangesQueryResult.getResult().get(0)).get("aaPositions"));
    //        if (allChangesQueryResult.getNumResults() > 0 && currentAaShortName != null && aaPositions != null) {
    //            Document positionDBObject = (Document) aaPositions.get(Integer.toString(aaPosition));
    //            if (positionDBObject != null) {
    //                Object aaObject = positionDBObject.get(currentAaShortName);
    //                if (aaObject != null) {
    //                    proteinSubstitionScoresQueryResult.setNumResults(1);
    //                    proteinSubstitionScoresQueryResult.setResult(Arrays.asList(aaObject));
    //=======
    if (allChangesQueryResult.getNumResults() > 0) {
        String currentAaShortName = aaShortName.get(newAa);
        Map aaPositions = ((HashMap) ((Document) allChangesQueryResult.getResult().get(0)).get("aaPositions"));
        if (currentAaShortName != null && aaPositions != null) {
            Document positionDBObject = (Document) aaPositions.get(Integer.toString(aaPosition));
            if (positionDBObject != null) {
                Object aaObject = positionDBObject.get(currentAaShortName);
                if (aaObject != null) {
                    proteinSubstitionScoresQueryResult.setNumResults(1);
                    proteinSubstitionScoresQueryResult.setResult(Arrays.asList(aaObject));
                } else {
                    proteinSubstitionScoresQueryResult.setErrorMsg("Unaccepted AA " + currentAaShortName
                            + ". Available AA changes for transcript " + transcriptId + ", position "
                            + aaPosition + ": " + positionDBObject.keySet().toString());
                    return proteinSubstitionScoresQueryResult;
                }
                //>>>>>>> develop
            } else {
                proteinSubstitionScoresQueryResult.setErrorMsg("Unaccepted position "
                        + Integer.toString(aaPosition) + ". Available positions for transcript " + transcriptId
                        + ": " + aaPositions.keySet().toString());
                return proteinSubstitionScoresQueryResult;
            }
        } else {
            proteinSubstitionScoresQueryResult.setNumResults(0);
        }
    }

    return proteinSubstitionScoresQueryResult;
}

From source file:org.opencb.cellbase.lib.db.core.XRefsMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getByStartsWithQueryList(List<String> ids, QueryOptions options) {
    List<Document> queries = new ArrayList<>();

    for (String id : ids) {
        QueryBuilder builder = QueryBuilder.start("transcripts.xrefs.id").regex(Pattern.compile("^" + id));
        queries.add(new Document(builder.get().toMap()));
    }/*from   w  ww .  ja va 2s  . c  o m*/
    int limit = options.getInt("limit", 50);
    if (limit > 50) {
        options.put("limit", 50);
    }
    System.out.println(options.getInt("limit"));
    options.put("include", Arrays.asList("chromosome", "start", "end", "id", "name"));

    return executeQueryList2(ids, queries, options);
}

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

License:Apache License

public QueryResult next(String chromosome, int position, QueryOptions options,
        MongoDBCollection mongoDBCollection) {
    QueryBuilder builder;/*from ww w  .  j  a  v a  2s.  co m*/
    if (options.getString("strand") == null || options.getString("strand").equals("")
            || (options.getString("strand").equals("1") || options.getString("strand").equals("+"))) {
        builder = QueryBuilder.start("chromosome").is(chromosome).and("start").greaterThanEquals(position);
        options.put("sort", new HashMap<String, String>().put("start", "asc"));
        options.put("limit", 1);
    } else {
        builder = QueryBuilder.start("chromosome").is(chromosome).and("end").lessThanEquals(position);
        options.put("sort", new HashMap<String, String>().put("end", "desc"));
        options.put("limit", 1);
    }
    return executeQuery("result", new Document(builder.get().toMap()), options, mongoDBCollection);
}

From source file:org.opencb.cellbase.lib.db.regulatory.RegulatoryRegionMongoDBAdaptor.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);
        //          System.out.println("Query: " + new Document(builder.get().toMap()));
        queries.add(new Document(builder.get().toMap()));
    }/*  w  w w .j a v a2  s .  c o  m*/
    //        options = addExcludeReturnFields("chunkIds", options);
    return executeQueryList2(idList, queries, options);
}

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

License:Apache License

@Override
public List<QueryResult> getAllByPositionList(List<Position> positionList, QueryOptions options) {
    //  db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156})

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

    List<Document> queries = new ArrayList<>();
    for (Position position : positionList) {
        String chunkId = position.getChromosome() + "_"
                + getChunkId(position.getPosition(), regulatoryRegionChunkSize) + "_"
                + regulatoryRegionChunkSize / 1000 + "k";
        BasicDBList chunksId = new BasicDBList();
        chunksId.add(chunkId);//from  www . ja  v a2  s  .c  o m
        QueryBuilder builder = QueryBuilder.start("_chunkIds").in(chunksId).and("start")
                .is(position.getPosition());
        if (featureType != null) {
            builder.and("featureType").is(featureType);
        }
        if (featureClass != null) {
            builder.and("featureClass").is(featureClass);
        }

        //        System.out.println("Query: " + new Document(builder.get().toMap()));
        queries.add(new Document(builder.get().toMap()));
    }

    System.out.println("Query: " + queries);

    //        options = addExcludeReturnFields("chunkIds", options);
    return executeQueryList2(positionList, queries, options);
}

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

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regionList, QueryOptions options) {
    //  db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156})
    QueryBuilder builder = new QueryBuilder();

    List<Object> featureType = options.getAsList("featureType");
    List<Object> featureClass = options.getAsList("featureClass");

    //        options = addExcludeReturnFields("chunkIds", options);

    List<Document> queries = new ArrayList<>();
    for (Region region : regionList) {
        int firstChunkId = getChunkId(region.getStart(), regulatoryRegionChunkSize);
        int lastChunkId = getChunkId(region.getEnd(), regulatoryRegionChunkSize);
        BasicDBList chunksId = new BasicDBList();
        for (int j = firstChunkId; j <= lastChunkId; j++) {
            String chunkId = region.getChromosome() + "_" + j + "_" + regulatoryRegionChunkSize / 1000 + "k";
            chunksId.add(chunkId);/* w  w w  .java  2 s. c om*/
        }

        //            logger.info(chunksId.toString());

        builder = builder.start("_chunkIds").in(chunksId).and("start").lessThanEquals(region.getEnd())
                .and("end").greaterThanEquals(region.getStart());

        if (featureType != null && featureType.size() > 0) {
            BasicDBList featureTypeDBList = new BasicDBList();
            featureTypeDBList.addAll(featureType);
            builder = builder.and("featureType").in(featureTypeDBList);
        }

        if (featureClass != null && featureClass.size() > 0) {
            BasicDBList featureClassDBList = new BasicDBList();
            featureClassDBList.addAll(featureClass);
            builder = builder.and("featureClass").in(featureClassDBList);
        }

        queries.add(new Document(builder.get().toMap()));
    }
    //        System.out.println(">>"+regionList);
    //        System.out.println(">>"+new Document(builder.get().toMap()).toString());
    return executeQueryList2(regionList, queries, options);
}