Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

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

Prototype

BasicDBList

Source Link

Usage

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

License:Apache License

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

    List<Object> biotypes = options.getList("biotype", null);
    BasicDBList biotypeIds = new BasicDBList();
    if (biotypes != null && biotypes.size() > 0) {
        biotypeIds.addAll(biotypes);//  w w w. j  a  v  a2s.  co  m
    }

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

        QueryBuilder builder = null;
        // If regions is 1 position then query can be optimize using chunks
        if (region.getStart() == region.getEnd()) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), geneChunkSize)).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        } else {
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }

        if (biotypeIds.size() > 0) {
            builder = builder.and("biotype").in(biotypeIds);
        }
        queries.add(builder.get());
        ids.add(region.toString());
    }
    return executeQueryList2(ids, queries, options);
    //        return executeQueryList(ids, queries, options);
}

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

License:Apache License

@Override
public List<QueryResult> getAllByDBNameList(List<String> ids, QueryOptions options) {

    // Mel de romer
    //db.core.aggregate(
    //{$match: {"transcripts.xrefs.id": "ENST00000544455"}},
    //{$unwind: "$transcripts"},
    //{$unwind: "$transcripts.xrefs"},
    //{$match: {"transcripts.xrefs.dbNameShort":{$in:["go"]}}},
    //{$group:{_id:{id:"$transcripts.xrefs.id", dbNameShort:"$transcripts.xrefs.dbNameShort", description:"$transcripts.xrefs.description"}}},
    //{$project:{"_id":0,"id":"$_id.id","dbNameShort":"$_id.dbNameShort","description":"$_id.description"}})

    // Biotype if gene given: db.core.find({"transcripts.xrefs.id": "BRCA2"}, {"biotype":1})
    // Biotype if protein/transcript given: db.core.aggregate({$match: {"transcripts.xrefs.id": "ENST00000470094"}}, {$unwind: "$transcripts"}, {$match: {"transcripts.xrefs.id": "ENST00000470094"}}, {$group:{_id:{biotype:"$transcripts.biotype"}}}, {$project:{"transcripts.biotype":1}})
    List<List<DBObject>> commandsList = new ArrayList<>(ids.size());
    for (String id : ids) {
        List<DBObject> commands = new ArrayList<>(ids.size());

        DBObject match = new BasicDBObject("$match", new BasicDBObject("transcripts.xrefs.id", id));
        DBObject unwind = new BasicDBObject("$unwind", "$transcripts");
        DBObject unwind2 = new BasicDBObject("$unwind", "$transcripts.xrefs");

        commands.add(match);/*from  w  ww  . j  ava  2s . c o  m*/
        commands.add(unwind);
        commands.add(match);
        commands.add(unwind2);

        //Check dbname option exists
        List<Object> list = options.getList("dbname", null);
        if (list != null && list.size() > 0) {
            BasicDBList dbnameDBList = new BasicDBList();
            dbnameDBList.addAll(list);
            DBObject dbnameMatch = new BasicDBObject("$match",
                    new BasicDBObject("transcripts.xrefs.dbName", new BasicDBObject("$in", dbnameDBList)));
            commands.add(dbnameMatch);
        }

        DBObject group = new BasicDBObject("$group", new BasicDBObject("_id",
                new BasicDBObject("id", "$transcripts.xrefs.id").append("dbName", "$transcripts.xrefs.dbName")
                        .append("dbDisplayName", "$transcripts.xrefs.dbDisplayName")
                        .append("description", "$transcripts.xrefs.description")));
        commands.add(group);

        DBObject project = new BasicDBObject("$project",
                new BasicDBObject("_id", 0).append("id", "$_id.id").append("dbName", "$_id.dbName")
                        .append("dbDisplayName", "$_id.dbDisplayName")
                        .append("description", "$_id.description"));
        commands.add(project);

        //ArrayList to array
        //            DBObject[] commandsArray = commands.toArray(new DBObject[0]);

        commandsList.add(commands);
    }
    return executeAggregationList2(ids, commandsList, options);
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<Object> biotypes = options.getList("biotypes", null);
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);// www.j av a 2  s .  c om
        builder = builder.and("biotype").in(biotypeIds);
    }

    //      options = addExcludeReturnFields("transcripts", options);
    return executeQuery("result", builder.get(), options);
}

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

License:Apache License

public QueryResult getIntervalFrequencies(Region region, QueryOptions options) {
    //  MONGO QUERY TO IMPLEMENT
    //    db.variation.aggregate({$match: {$and: [{chromosome: "1"}, {start: {$gt: 251391, $lt: 2701391}}]}}, {$group: {_id: {$subtract: [{$divide: ["$start", 40000]}, {$divide: [{$mod: ["$start", 40000]}, 40000]}]}, totalCount: {$sum: 1}}})
    //        {/* www  . j  a  va2 s  . co  m*/
    //            $match: {
    //                $and: [{
    //                    chromosome: "1"
    //                }, {
    //                    start: {
    //                        $gt: 251391,
    //                                $lt: 2701391
    //                    }
    //                }
    //                ]
    //            }
    //        }, {
    //            $group: {
    //                _id: {
    //                    $subtract: [{
    //                        $divide: ["$start", 40000]
    //                    }, {
    //                        $divide: [{
    //                            $mod: ["$start", 40000]
    //                        },
    //                        40000
    //                        ]
    //                    }
    //                    ]
    //                },
    //                totalCount: {
    //                    $sum: 1
    //                }
    //            }
    //        }

    int interval = options.getInt("interval");

    BasicDBObject start = new BasicDBObject("$gt", region.getStart());
    start.append("$lt", region.getEnd());

    BasicDBList andArr = new BasicDBList();
    andArr.add(new BasicDBObject("chromosome", region.getChromosome()));
    andArr.add(new BasicDBObject("start", start));

    BasicDBObject match = new BasicDBObject("$match", new BasicDBObject("$and", andArr));

    BasicDBList divide1 = new BasicDBList();
    divide1.add("$start");
    divide1.add(interval);

    BasicDBList divide2 = new BasicDBList();
    divide2.add(new BasicDBObject("$mod", divide1));
    divide2.add(interval);

    BasicDBList subtractList = new BasicDBList();
    subtractList.add(new BasicDBObject("$divide", divide1));
    subtractList.add(new BasicDBObject("$divide", divide2));

    BasicDBObject substract = new BasicDBObject("$subtract", subtractList);

    DBObject totalCount = new BasicDBObject("$sum", 1);

    BasicDBObject g = new BasicDBObject("_id", substract);
    g.append("features_count", totalCount);
    BasicDBObject group = new BasicDBObject("$group", g);

    BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));

    logger.info("getIntervalFrequencies - (>_)>");
    System.out.println(options.toString());

    System.out.println(match.toString());
    System.out.println(group.toString());
    System.out.println(sort.toString());

    QueryResult<DBObject> aggregationOutput = mongoDBCollection.aggregate(Arrays.asList(match, group, sort),
            options);
    Map<Long, DBObject> ids = new HashMap<>();
    for (DBObject intervalObj : aggregationOutput.getResult()) {
        Long _id = Math.round((Double) intervalObj.get("_id"));//is double

        DBObject intervalVisited = ids.get(_id);
        if (intervalVisited == null) {
            intervalObj.put("_id", _id);
            intervalObj.put("start", getChunkStart(_id.intValue(), interval));
            intervalObj.put("end", getChunkEnd(_id.intValue(), interval));
            intervalObj.put("chromosome", region.getChromosome());
            intervalObj.put("features_count", Math.log((int) intervalObj.get("features_count")));
            ids.put(_id, intervalObj);
        } else {
            Double sum = (Double) intervalVisited.get("features_count")
                    + Math.log((int) intervalObj.get("features_count"));
            intervalVisited.put("features_count", sum.intValue());
        }
    }

    /****/
    BasicDBList resultList = new BasicDBList();
    int firstChunkId = getChunkId(region.getStart(), interval);
    int lastChunkId = getChunkId(region.getEnd(), interval);
    DBObject intervalObj;
    for (int chunkId = firstChunkId; chunkId <= lastChunkId; chunkId++) {
        intervalObj = ids.get((long) chunkId);
        if (intervalObj == null) {
            intervalObj = new BasicDBObject();
            intervalObj.put("_id", chunkId);
            intervalObj.put("start", getChunkStart(chunkId, interval));
            intervalObj.put("end", getChunkEnd(chunkId, interval));
            intervalObj.put("chromosome", region.getChromosome());
            intervalObj.put("features_count", 0);
        }
        resultList.add(intervalObj);
    }
    /****/

    QueryResult queryResult = new QueryResult();
    queryResult.setResult(resultList);
    queryResult.setId(region.toString());
    queryResult.setResultType("frequencies");

    return queryResult;

    /***************************/
    //        QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getSequenceName()).and("end")
    //                .greaterThan(region.getStart()).and("start").lessThan(region.getEnd());
    //
    //        int numIntervals = (region.getEnd() - region.getStart()) / interval + 1;
    //        int[] intervalCount = new int[numIntervals];
    //
    //        List<Variation> variationList = executeQuery(builder.get(), Arrays.asList("id,chromosome,end,strand,type,reference,alternate,alleleString,species,assembly,source,version,transcriptVariations,xrefs,featureId,featureAlias,variantFreq,validationStatus"));
    //
    //        System.out.println("Variation index");
    //        System.out.println("numIntervals: " + numIntervals);
    //        for (Variation variation : variationList) {
    //            System.out.print("gsnp start:" + variation.getStart() + " ");
    //            if (variation.getStart() >= region.getStart() && variation.getStart() <= region.getEnd()) {
    //                int intervalIndex = (variation.getStart() - region.getStart()) / interval; // truncate
    //                System.out.print(intervalIndex + " ");
    //                intervalCount[intervalIndex]++;
    //            }
    //        }
    //        System.out.println("Variation index");
    //
    //        int intervalStart = region.getStart();
    //        int intervalEnd = intervalStart + interval - 1;
    //        BasicDBList intervalList = new BasicDBList();
    //        for (int i = 0; i < numIntervals; i++) {
    //            BasicDBObject intervalObj = new BasicDBObject();
    //            intervalObj.put("start", intervalStart);
    //            intervalObj.put("end", intervalEnd);
    //            intervalObj.put("interval", i);
    //            intervalObj.put("value", intervalCount[i]);
    //            intervalList.add(intervalObj);
    //            intervalStart = intervalEnd + 1;
    //            intervalEnd = intervalStart + interval - 1;
    //        }
    //
    //        System.out.println(region.getSequenceName());
    //        System.out.println(region.getStart());
    //        System.out.println(region.getEnd());
    //        return intervalList.toString();
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<Object> biotypes = options.getList("disease", null);
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);/*from  w ww  .j  av a 2s .c  o  m*/
        builder = builder.and("primaryHistology").in(biotypeIds);
    }

    return executeQuery("result", builder.get(), options);
}

From source file:org.opencb.cellbase.mongodb.db.network.ProteinProteinInteractionMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    // Fiilter all by interactor ID
    List<Object> interactors = options.getList("interactor", null);
    if (interactors != null && interactors.size() > 0) {
        BasicDBList interactorDBList = new BasicDBList();
        interactorDBList.addAll(interactors);

        BasicDBList or = new BasicDBList();
        DBObject orA = new BasicDBObject("interactorA.xrefs.id", new BasicDBObject("$in", interactorDBList));
        DBObject orB = new BasicDBObject("interactorB.xrefs.id", new BasicDBObject("$in", interactorDBList));
        or.add(orA);//from ww w.  ja  va 2 s  .c  om
        or.add(orB);
        //            builder = builder.or(orA, orB);
        builder = builder.and(new BasicDBObject("$or", or));
    }

    // Filter all by Interaction Type (name and PSIMI)
    List<Object> type = options.getList("type", null);
    if (type != null && type.size() > 0) {
        BasicDBList typeDBList = new BasicDBList();
        typeDBList.addAll(type);

        BasicDBList or = new BasicDBList();
        DBObject orName = new BasicDBObject("type.name", new BasicDBObject("$in", typeDBList));
        DBObject orPsimi = new BasicDBObject("type.psimi", new BasicDBObject("$in", typeDBList));
        or.add(orName);
        or.add(orPsimi);
        //            builder = builder.or(orName, orPsimi);
        builder = builder.and(new BasicDBObject("$or", or));
    }

    // Filter all by source database
    List<Object> database = options.getList("database", null);
    if (database != null && database.size() > 0) {
        BasicDBList databaseDBList = new BasicDBList();
        databaseDBList.addAll(database);
        builder = builder.and("source.name").in(databaseDBList);
    }

    // Filter all by detection method (name and PSIMI)
    List<Object> detectionMethod = options.getList("detectionMethod", null);
    if (detectionMethod != null && detectionMethod.size() > 0) {
        BasicDBList detectionMethodDBList = new BasicDBList();
        detectionMethodDBList.addAll(detectionMethod);

        BasicDBList or = new BasicDBList();
        DBObject orName = new BasicDBObject("detectionMethod.name",
                new BasicDBObject("$in", detectionMethodDBList));
        DBObject orPsimi = new BasicDBObject("detectionMethod.psimi",
                new BasicDBObject("$in", detectionMethodDBList));
        or.add(orName);
        or.add(orPsimi);
        //            builder = builder.or(orName, orPsimi);
        builder = builder.and(new BasicDBObject("$or", or));
    }

    // Filter all by status
    List<Object> status = options.getList("status", null);
    if (status != null && status.size() > 0) {
        BasicDBList statusDBList = new BasicDBList();
        statusDBList.addAll(status);
        builder = builder.and("status").in(statusDBList);
    }

    //        List<Object> type = options.getList("type", null);
    //        if (type != null && type.size() > 0) {
    //            BasicDBList typeDBList = new BasicDBList();
    //            typeDBList.addAll(type);
    //            builder = builder.and("type.name").in(typeDBList);
    //        }

    //        String type = options.getString("type", null);
    //        if (type != null && !type.equals("")) {
    //            builder = builder.and("type.name").is(type);
    //        }

    System.out.println(builder.get().toString());
    //      options = addExcludeReturnFields("transcripts", options);
    return executeQuery("result", builder.get(), options);
}

From source file:org.opencb.cellbase.mongodb.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<DBObject> 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);//  ww  w .  ja  v  a  2 s  . c om
        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: " + builder.get());
        queries.add(builder.get());
    }

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

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

From source file:org.opencb.cellbase.mongodb.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<DBObject> 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);// www .j a  va 2  s.c o m
        }

        //            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(builder.get());
    }
    //        System.out.println(">>"+regionList);
    //        System.out.println(">>"+builder.get().toString());
    return executeQueryList2(regionList, queries, options);
}

From source file:org.opencb.cellbase.mongodb.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);//from w  w  w  .java2  s .c  om

    // 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 BasicDBObject("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 BasicDBObject("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(builder.get());
    return executeQuery("result", builder.get(), options);
}

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

License:Apache License

private DBObject getClinvarRegionFilterDBObject(List<Region> regionList) {
    BasicDBList orDBList = new BasicDBList();
    for (Region region : regionList) {
        BasicDBList andDBList = new BasicDBList();
        andDBList.add(new BasicDBObject("chromosome", region.getChromosome()));
        andDBList.add(new BasicDBObject("end", new BasicDBObject("$gte", region.getStart())));
        andDBList.add(new BasicDBObject("start", new BasicDBObject("$lte", region.getEnd())));
        orDBList.add(new BasicDBObject("$and", andDBList));
    }// w  w w.j a  v  a  2  s.  c om

    return new BasicDBObject("$or", orDBList);
}