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.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);/*from   w ww  .j  a v a2s . 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.variation.ClinicalMongoDBAdaptor.java

License:Apache License

private Document getFilters(QueryOptions options) {
    Document filtersDBObject = new Document();
    if (filteringOptionsEnabled(options)) {
        Document commonFiltersDBObject = getCommonFilters(options);
        BasicDBList sourceSpecificFilterList = new BasicDBList();
        List<String> sourceContent = options.getAsStringList("source");
        if (sourceContent == null || sourceContent.isEmpty() || includeContains(sourceContent, "clinvar")) {
            sourceSpecificFilterList.add(getClinvarFilters(options));
        }/* w  w w.j av a 2s. c  o  m*/
        if (sourceContent == null || sourceContent.isEmpty() || includeContains(sourceContent, "cosmic")) {
            sourceSpecificFilterList.add(getCosmicFilters(options));
        }
        if (sourceContent == null || sourceContent.isEmpty() || includeContains(sourceContent, "gwas")) {
            sourceSpecificFilterList.add(getGwasFilters(options));
        }
        if (sourceSpecificFilterList.size() > 0 && commonFiltersDBObject != null) {
            BasicDBList filtersDBList = new BasicDBList();
            filtersDBList.add(commonFiltersDBObject);
            filtersDBList.add(new Document("$or", sourceSpecificFilterList));
            filtersDBObject.put("$and", filtersDBList);
        } else if (commonFiltersDBObject != null) {
            filtersDBObject = commonFiltersDBObject;
        } else if (sourceSpecificFilterList.size() > 0) {
            filtersDBObject = new Document("$or", sourceSpecificFilterList);
        }
    }
    return filtersDBObject;
}

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

License:Apache License

private Document getCommonFilters(QueryOptions options) {
    BasicDBList filterList = new BasicDBList();
    addIfNotNull(filterList, getSoTermFilter(options.getAsStringList("so")));
    addIfNotNull(filterList, getRegionFilter(Region.parseRegions((String) options.get("region"))));
    addIfNotNull(filterList, getGeneFilter(options.getAsStringList("gene")));
    addIfNotNull(filterList, getPhenotypeFilter(options.getString("phenotype")));

    if (filterList.size() > 0) {
        return new Document("$and", filterList);
    } else {// w  w w  .ja v  a2 s. c o m
        return null;
    }
}

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

License:Apache License

private Document getClinvarFilters(QueryOptions options) {
    BasicDBList filterList = new BasicDBList();
    filterList.add(new Document("source", "clinvar"));
    addIfNotNull(filterList, getClinvarRcvFilter(options.getAsStringList("rcv")));
    addIfNotNull(filterList, getClinvarRsFilter(options.getAsStringList("rs")));
    addIfNotNull(filterList, getClinvarTypeFilter(options.getAsStringList("type")));
    addIfNotNull(filterList, getClinvarReviewFilter(options.getAsStringList("review")));
    addIfNotNull(filterList, getClinvarClinicalSignificanceFilter(options.getAsStringList("significance")));

    return new Document("$and", filterList);
}

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

License:Apache License

private Document getClinvarRegionFilterDBObject(List<Region> regionList) {
    BasicDBList orDBList = new BasicDBList();
    for (Region region : regionList) {
        BasicDBList andDBList = new BasicDBList();
        andDBList.add(new Document("chromosome", region.getChromosome()));
        andDBList.add(new Document("end", new Document("$gte", region.getStart())));
        andDBList.add(new Document("start", new Document("$lte", region.getEnd())));
        orDBList.add(new Document("$and", andDBList));
    }//from  w  w w. ja v  a2 s  .co  m

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

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

License:Apache License

private Document getClinvarRsFilter(List<String> rsStringList) {
    //        List<String> rsStringList = options.getAsStringList("rs");
    //        List<String> rsStringList = (List<String>) options.get("rs");
    if (rsStringList != null && rsStringList.size() > 0) {
        logger.info("rs filter activated, res list: " + rsStringList.toString());
        List<String> rsList = new ArrayList<>(rsStringList.size());
        for (String rsString : rsStringList) {
            rsList.add(rsString.substring(2));
        }/*w  w  w .ja va2 s.  co  m*/
        BasicDBList filterList = new BasicDBList();
        filterList.add(new Document("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.id",
                new Document("$in", rsList)));
        filterList.add(new Document("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.type", "rs"));
        return new Document("$and", filterList);
    }
    return null;
}

From source file:org.opencb.cellbase.lib.db.variation.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 w  w .java  2 s  .co  m*/
        builder = builder.and("primaryHistology").in(biotypeIds);
    }

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

From source file:org.opencb.cellbase.lib.db.variation.VariationMongoDBAdaptor.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());

    // TODO: do not allow phenotype queries on the variation collection.
    // TODO: phenotype queries shall be answered by the clinicalMongoDBAdaptor
    String phenotype = options.getString("phenotype");
    if (phenotype != null && !phenotype.equals("")) {
        for (Region region : regions) {
            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("start")
                    .greaterThanEquals(region.getStart()).lessThanEquals(region.getEnd());
            builder = builder.and("phenotype").is(phenotype);
            queries.add(new Document(builder.get().toMap()));
            ids.add(region.toString());/* w  w w  .j  a  va  2 s  . co  m*/
        }
        return executeQueryList2(ids, queries, options, mongoVariationPhenotypeDBCollection2);
    } else {
        String consequenceTypes = options.getString("consequence_type", null);
        BasicDBList consequenceTypeDBList = new BasicDBList();
        if (consequenceTypes != null && !consequenceTypes.equals("")) {
            for (String ct : consequenceTypes.split(",")) {
                consequenceTypeDBList.add(ct);
            }
        }

        for (Region region : regions) {
            // QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getSequenceName())
            // .and("end").greaterThan(region.getStart()).and("start").lessThan(region.getEnd());
            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("start")
                    .greaterThanEquals(region.getStart()).lessThanEquals(region.getEnd());
            if (consequenceTypeDBList.size() > 0) {
                builder = builder.and("annotation.consequenceTypes.sequenceOntologyTerms.name")
                        .in(consequenceTypeDBList);
            }
            queries.add(new Document(builder.get().toMap()));
            ids.add(region.toString());
        }

        return executeQueryList2(ids, queries, options);
    }
}

From source file:org.opencb.cellbase.lib.impl.ConservationMongoDBAdaptor.java

License:Apache License

public List<QueryResult> getAllByRegionList(List regionList, QueryOptions options) {
    //TODO not finished yet
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regionList.size());
    List<String> integerChunkIds;

    List<Region> regions = regionList;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);/*from ww  w  .j  a v  a2 s  .com*/
        }
        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(),
                MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE);
        int regionChunkEnd = getChunkId(region.getEnd(),
                MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds").is(getChunkIdPrefix(region.getChromosome(),
                    region.getStart(), MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE));
        } 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");
            ArrayList valuesChunk = chunk.get("values", ArrayList.class);

            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.impl.ConservationMongoDBAdaptor.java

License:Apache License

@Override
@Deprecated//  w  w w . ja v a2s. co  m
public List<QueryResult> getAllScoresByRegionList(List regionList, QueryOptions options) {
    //TODO not finished yet
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regionList.size());
    List<Integer> integerChunkIds;

    List<Region> regions = regionList;
    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(),
                MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE);
        int regionChunkEnd = getChunkId(region.getEnd(),
                MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds").is(getChunkIdPrefix(region.getChromosome(),
                    region.getStart(), MongoDBCollectionConfiguration.CONSERVATION_CHUNK_SIZE));
        } 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");
                ArrayList valuesChunk = chunk.get("values", ArrayList.class);

                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");
                    valuesChunk = chunk.get("values", ArrayList.class);
                    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(), null));
                    }
                }
            }
            if (!resultList.isEmpty()) {
                queryResult.setResult(resultList);
            } else {
                queryResult.setResult(null);
            }
        }

    }
    return queryResults;
}