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.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

public static DBObject marshall(Variable variable) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start() //
            .add("name", variable.getName()) //
            .add("valueType", variable.getValueType().getName()) //
            .add("entityType", variable.getEntityType()) //
            .add("mimeType", variable.getMimeType()) //
            .add("repeatable", variable.isRepeatable()) //
            .add("occurrenceGroup", variable.getOccurrenceGroup()) //
            .add("referencedEntityType", variable.getReferencedEntityType()) //
            .add("unit", variable.getUnit()).add("index", variable.getIndex());

    if (variable.hasCategories()) {
        Collection<Object> list = new BasicDBList();
        for (Category category : variable.getCategories()) {
            list.add(marshall(category));
        }//from w w w  .ja  v  a 2  s.  com
        builder.add("categories", list);
    }

    if (variable.hasAttributes()) {
        Collection<Object> list = new BasicDBList();
        for (Attribute attribute : variable.getAttributes()) {
            list.add(marshall(attribute));
        }
        builder.add("attributes", list);
    }

    return builder.get();
}

From source file:org.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

private static DBObject marshall(Category category) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start() //
            .add("name", category.getName()).add("missing", category.isMissing());
    if (category.hasAttributes()) {
        Collection<Object> list = new BasicDBList();
        for (Attribute attribute : category.getAttributes()) {
            list.add(marshall(attribute));
        }/*from   www . j  a  v  a  2  s  . c o m*/
        builder.add("attributes", list);
    }
    return builder.get();
}

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

License:Apache License

@Deprecated
@Override/*from www .  j a v  a  2 s  . c om*/
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<Document> 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.getChromosomeInfo() + "_" + chunkId + "_" + this.chunkSize/1000 + "k");
            //                }
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).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.getChromosomeInfo()).and("chunkId").in(hunkIds);
        /****/

        queries.add(new Document(builder.get().toMap()));
        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++) {
            Document chunk = (Document) list.get(j);
            String source = chunk.getString("source");
            List<Float> valuesList;
            if (!typeMap.containsKey(source)) {
                valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                    valuesList.add(null);
                }
                typeMap.put(source, valuesList);
            } else {
                valuesList = typeMap.get(source);
            }

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

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

            for (; pos < valuesChunk.size() && (pos + chunk.getInteger("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.getInteger("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }
        //
        BasicDBList resultList = new BasicDBList();
        //            ConservationScoreRegion conservedRegionChunk;
        GenomicScoreRegion<Float> conservedRegionChunk;
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            //                conservedRegionChunk = new ConservationScoreRegion(region.getChromosome(), region.getStart(),
            //                        region.getEnd(), elem.getKey(), elem.getValue());
            conservedRegionChunk = new GenomicScoreRegion<>(region.getChromosome(), region.getStart(),
                    region.getEnd(), elem.getKey(), elem.getValue());
            resultList.add(conservedRegionChunk);
        }
        queryResult.setResult(resultList);
    }

    return queryResults;
}

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

License:Apache License

@Override
public List<QueryResult> getAllScoresByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<Document> 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);// ww  w.j a v  a 2 s  . c  om
        }
        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.getChromosomeInfo()).and("chunkId").in(hunkIds);
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(integerChunkIds);
            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());

        //            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<Document> list = (List<Document>) queryResult.getResult();

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

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            Document chunk = list.get(j);

            if (!chunk.isEmpty()) {
                BasicDBList valuesChunk = (BasicDBList) chunk.get("values");
                if (valuesChunk != null) { // TODO: temporary patch to skip empty chunks - remove as soon as conservation is reloaded
                    String source = chunk.getString("source");
                    List<Float> valuesList;
                    if (!typeMap.containsKey(source)) {
                        valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                        for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                            valuesList.add(null);
                        }
                        typeMap.put(source, valuesList);
                    } else {
                        valuesList = typeMap.get(source);
                    }

                    valuesChunk = (BasicDBList) chunk.get("values");
                    int pos = 0;
                    if (region.getStart() > chunk.getInteger("start")) {
                        pos = region.getStart() - chunk.getInteger("start");
                    }

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

            }

            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.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

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

    List<String> biotypes = options.getAsStringList("biotype");
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);/*www.j ava 2  s . c o  m*/
        builder = builder.and("biotype").in(biotypeIds);
    }

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

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

License:Apache License

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

    List<Object> biotypes = options.getList("biotype", null);
    BasicDBList biotypeIds = new BasicDBList();
    if (biotypes != null && biotypes.size() > 0) {
        biotypeIds.addAll(biotypes);// w  ww  .  java2  s.  c o  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(new Document(builder.get().toMap()));
        ids.add(region.toString());
    }
    return executeQueryList2(ids, queries, options);
    //        return executeQueryList(ids, queries, options);
}

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

License:Apache License

@Override
public QueryResult getVariantAnnotation(String ensemblTranscriptId, Integer position, String aaReference,
        String aaAlternate, QueryOptions queryOptions) {

    QueryResult queryResult = new QueryResult();
    queryResult.setId(ensemblTranscriptId + "/" + position + "/" + aaAlternate);
    long dbTimeStart = System.currentTimeMillis();

    ProteinVariantAnnotation proteinVariantAnnotation = new ProteinVariantAnnotation();
    proteinVariantAnnotation.setPosition(position);
    proteinVariantAnnotation.setReference(aaReference);
    proteinVariantAnnotation.setAlternate(aaAlternate);
    proteinVariantAnnotation/*from w  ww  .  ja  va2  s .  com*/
            .setSubstitutionScores(getProteinSubstitutionScores(ensemblTranscriptId, position, aaAlternate));

    QueryResult proteinVariantData = null;
    String shortAlternativeAa = aaShortName.get(aaAlternate);
    if (shortAlternativeAa != null) {
        List<Bson> pipeline = new ArrayList<>();

        //            BasicDBList andDBList1 = new BasicDBList();
        //            andDBList1.add(new Document("dbReference.id", ensemblTranscriptId));
        //            andDBList1.add(new Document("feature.location.position.position", position));
        //            andDBList1.add(new Document("feature.variation", shortAlternativeAa));
        //            pipeline.add(new Document("$match", new Document("$and", andDBList1)));

        pipeline.add(new Document("$match", new Document("dbReference.id", ensemblTranscriptId)));

        Document projection = new Document();
        projection.put("accession", 1);
        projection.put("keyword", 1);
        projection.put("feature", 1);
        pipeline.add(new Document("$project", projection));

        pipeline.add(new Document("$unwind", "$feature"));

        BasicDBList andDBList2 = new BasicDBList();
        andDBList2.add(new Document("feature.location.position.position", position));
        andDBList2.add(new Document("feature.variation", shortAlternativeAa));
        Document firstOr = new Document("$and", andDBList2);
        BasicDBList andDBList3 = new BasicDBList();
        andDBList3.add(new Document("feature.location.end.position", new Document("$gte", position)));
        andDBList3.add(new Document("feature.location.begin.position", new Document("$lte", position)));
        Document secondOr = new Document();
        secondOr.put("$and", andDBList3);
        BasicDBList orList = new BasicDBList();
        orList.add(firstOr);
        orList.add(secondOr);
        pipeline.add(new Document("$match", new Document("$or", orList)));
        //            pipeline.add(new Document("$match", firstOr));
        //
        Document groupFields = new Document();
        groupFields.put("_id", "$accession");
        groupFields.put("keyword", new Document("$addToSet", "$keyword"));
        groupFields.put("feature", new Document("$addToSet", "$feature"));
        pipeline.add(new Document("$group", groupFields));

        //TODO:terminar el pipeline de agregacion
        //            QueryBuilder builder = QueryBuilder.start("dbReference.id").is(ensemblTranscriptId)
        //                    .and("feature.location.position.position").is(position)
        //                    .and("feature.variation").is(shortAlternativeAa);
        //
        //            Document firstOr = new Document();
        //            firstOr.put("location.position.position", position);
        //            firstOr.put("variation", shortAlternativeAa);
        //
        //            BasicDBList andList = new BasicDBList();
        //            andList.add(new Document("location.end.position", new Document("$gte", position)));
        //            andList.add(new Document("location.begin.position", new Document("$lte", position)));
        //            Document secondOr = new Document();
        //            secondOr.put("$and", andList);
        //
        //            BasicDBList orList = new BasicDBList();
        //            orList.add(firstOr);
        //            orList.add(secondOr);
        //
        //            Document elemMatch = new Document();
        //            elemMatch.put("$elemMatch", new Document("$or", orList));
        //
        //            Document projection = new Document();
        //            projection.put("feature", elemMatch);
        //
        //            QueryOptions localQueryOptions = new QueryOptions();
        //            localQueryOptions.put("elemMatch",projection);
        //            localQueryOptions.put("include","accession,keyword,feature");
        //            proteinVariantData = executeQuery(ensemblTranscriptId + "_" + String.valueOf(position) + "_"
        //                            + aaAlternate, new Document(builder.get().toMap()), localQueryOptions);
        proteinVariantData = executeAggregation2(
                ensemblTranscriptId + "_" + String.valueOf(position) + "_" + aaAlternate, pipeline,
                new QueryOptions());
        if (proteinVariantData.getNumResults() > 0) {
            proteinVariantAnnotation = processProteinVariantData(proteinVariantAnnotation, shortAlternativeAa,
                    (Document) proteinVariantData.getResult().get(0));
        }
    }

    long dbTimeEnd = System.currentTimeMillis();
    queryResult.setDbTime(Long.valueOf(dbTimeEnd - dbTimeStart).intValue());

    if (proteinVariantAnnotation.getSubstitutionScores() != null
            || proteinVariantAnnotation.getUniprotAccession() != null) {
        queryResult.setNumResults(1);
        queryResult.setResult(Collections.singletonList(proteinVariantAnnotation));
    }
    return queryResult;

}

From source file:org.opencb.cellbase.lib.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<Bson>> commandsList = new ArrayList<>(ids.size());
    for (String id : ids) {
        List<Bson> commands = new ArrayList<>(ids.size());

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

        commands.add(match);//from   w w w  .  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);
            Document dbnameMatch = new Document("$match",
                    new Document("transcripts.xrefs.dbName", new Document("$in", dbnameDBList)));
            commands.add(dbnameMatch);
        }

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

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

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

        commandsList.add(commands);
    }
    return executeAggregationList2(ids, commandsList, 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);/*w w  w  . j a  v a 2 s.co 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);/*from  ww w.  j  a v  a  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);
}