List of usage examples for com.mongodb QueryBuilder get
public DBObject get()
From source file:org.opencb.cellbase.lib.impl.ConservationMongoDBAdaptor.java
License:Apache License
@Override @Deprecated//from w w w .j av a 2s . c o 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; }
From source file:org.opencb.cellbase.lib.impl.GenomeMongoDBAdaptor.java
License:Apache License
@Override // public List<QueryResult<ConservationScoreRegion>> getConservation(List<Region> regionList, QueryOptions options) { public List<QueryResult<GenomicScoreRegion<Float>>> getConservation(List<Region> regionList, QueryOptions options) {/*from w ww.j a v a 2s . c om*/ //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); } 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()); } List<QueryResult> queryResults = executeQueryList2(ids, queries, options, conservationMongoDBCollection); // List<QueryResult<ConservationScoreRegion>> conservationQueryResults = new ArrayList<>(); List<QueryResult<GenomicScoreRegion<Float>>> conservationQueryResults = new ArrayList<>(); for (int i = 0; i < regions.size(); i++) { Region region = regions.get(i); QueryResult queryResult = queryResults.get(i); // QueryResult<ConservationScoreRegion> conservationQueryResult = new QueryResult<>(); QueryResult<GenomicScoreRegion<Float>> conservationQueryResult = new QueryResult<>(); // BasicDBList list = (BasicDBList) queryResult.getResult(); List list = queryResult.getResult(); Map<String, List<Float>> typeMap = new HashMap<>(); 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++) { valuesList.set(pos + chunk.getInteger("start") - region.getStart(), new Float((Double) valuesChunk.get(pos))); } } // BasicDBList resultList = new BasicDBList(); // List<ConservationScoreRegion> resultList = new ArrayList<>(); List<GenomicScoreRegion<Float>> resultList = new ArrayList<>(); // 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); conservationQueryResult.setResult(resultList); conservationQueryResults.add(conservationQueryResult); } return conservationQueryResults; }
From source file:org.opencb.cellbase.lib.impl.VariantMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Score> getFunctionalScoreVariant(Variant variant, QueryOptions queryOptions) { String chromosome = variant.getChromosome(); int position = variant.getStart(); String reference = variant.getReference(); String alternate = variant.getAlternate(); String chunkId = getChunkIdPrefix(chromosome, position, MongoDBCollectionConfiguration.VARIATION_FUNCTIONAL_SCORE_CHUNK_SIZE); QueryBuilder builder = QueryBuilder.start("_chunkIds").is(chunkId); // .and("chromosome").is(chromosome) // .and("start").is(position); // System.out.println(chunkId); QueryResult result = executeQuery(chromosome + "_" + position + "_" + reference + "_" + alternate, new Document(builder.get().toMap()), queryOptions, caddDBCollection); // System.out.println("result = " + result); List<Score> scores = new ArrayList<>(); for (Object object : result.getResult()) { // System.out.println("object = " + object); Document dbObject = (Document) object; int chunkStart = dbObject.getInteger("start"); int chunkEnd = dbObject.getInteger("end"); // CADD positions are not continuous through the whole chromosome. Several documents may be associated with // the same chunk id: we have to be sure that current document contains queried position. Only two documents // will contain queried position - one for raw and one for scaled values if (position >= chunkStart && position <= chunkEnd) { int offset = (position - chunkStart); ArrayList basicDBList = dbObject.get("values", ArrayList.class); // long l1 = 0L; // TODO: delete // try { // TODO: delete long l1 = Long.parseLong(basicDBList.get(offset).toString()); // l1 = (Long) basicDBList.get(offset); // } catch (Exception e) { // TODO: delete // logger.error("problematic variant: {}", variant.toString()); // throw e; // } if (dbObject.getString("source").equalsIgnoreCase("cadd_raw")) { float value = 0f; switch (alternate.toLowerCase()) { case "a": // value = ((short) (l1 >> 48) - 10000) / DECIMAL_RESOLUTION; value = (((short) (l1 >> 48)) / DECIMAL_RESOLUTION) - 10; break; case "c": value = (((short) (l1 >> 32)) / DECIMAL_RESOLUTION) - 10; break; case "g": value = (((short) (l1 >> 16)) / DECIMAL_RESOLUTION) - 10; break; case "t": value = (((short) (l1 >> 0)) / DECIMAL_RESOLUTION) - 10; break; default: break; }//from w ww .j a v a 2 s. co m scores.add(Score.newBuilder().setScore(value).setSource(dbObject.getString("source")) .setDescription(null) // .setDescription("") .build()); } if (dbObject.getString("source").equalsIgnoreCase("cadd_scaled")) { float value = 0f; switch (alternate.toLowerCase()) { case "a": value = ((short) (l1 >> 48)) / DECIMAL_RESOLUTION; break; case "c": value = ((short) (l1 >> 32)) / DECIMAL_RESOLUTION; break; case "g": value = ((short) (l1 >> 16)) / DECIMAL_RESOLUTION; break; case "t": value = ((short) (l1 >> 0)) / DECIMAL_RESOLUTION; break; default: break; } scores.add(Score.newBuilder().setScore(value).setSource(dbObject.getString("source")) .setDescription(null) // .setDescription("") .build()); } } } result.setResult(scores); return result; }
From source file:org.opencb.cellbase.mongodb.db.ChromosomeMongoDBAdaptor.java
License:Apache License
public QueryResult speciesInfoTmp(String id, QueryOptions options) { // reading application.properties file // String[] speciesArray = applicationProperties.getProperty("SPECIES").split(","); // List<DBObject> queries = new ArrayList<>(1); // for (String id : idList) { QueryBuilder builder = QueryBuilder.start("species").is(id); // queries.add(builder.get()); // }/*from w w w .j a va2 s. c o m*/ // options = addExcludeReturnFields("transcripts", options); return executeQuery(id, builder.get(), options); }
From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java
License:Apache License
@Override public List<QueryResult> getAllClinvarByIdList(List<String> idList, QueryOptions options) { List<DBObject> queries = new ArrayList<>(idList.size()); options.addToListOption("include", "clinvarList"); options.addToListOption("include", "chromosome"); options.addToListOption("include", "start"); options.addToListOption("include", "end"); options.addToListOption("include", "reference"); options.addToListOption("include", "alternate"); for (String id : idList) { if (id.toLowerCase().startsWith("rcv")) { QueryBuilder builder = addClinvarQueryFilters(QueryBuilder .start("clinvarList.clinvarSet.referenceClinVarAssertion.clinVarAccession.acc").is(id), options);//from www .j a v a 2 s . c o m queries.add(builder.get()); } else if (id.toLowerCase().startsWith("rs")) { QueryBuilder builder = addClinvarQueryFilters(QueryBuilder.start( "clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.type") .is("rs") .and("clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.id") .is(id), options); queries.add(builder.get()); } } return prepareClinvarQueryResultList(executeQueryList2(idList, queries, options)); }
From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java
License:Apache License
@Override public List<QueryResult> getAllClinvarByGeneList(List<String> geneList, QueryOptions options) { List<DBObject> queries = new ArrayList<>(geneList.size()); options.addToListOption("include", "clinvarList"); options.addToListOption("include", "chromosome"); options.addToListOption("include", "start"); options.addToListOption("include", "end"); options.addToListOption("include", "reference"); options.addToListOption("include", "alternate"); for (String gene : geneList) { QueryBuilder builder = addClinvarQueryFilters(QueryBuilder.start( "clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.measureRelationship.symbol.elementValue.value") .is(gene), options);/*from ww w. j a va2 s . co m*/ queries.add(builder.get()); } return prepareClinvarQueryResultList(executeQueryList2(geneList, queries, options)); }
From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java
License:Apache License
private QueryBuilder addClinvarPhenotypeFilter(QueryBuilder builder, QueryOptions options) { List<Object> phenotypeList = options.getList("phenotype", null); if (phenotypeList != null && phenotypeList.size() > 0) { QueryBuilder phenotypeQueryBuilder = QueryBuilder.start(); for (Object phenotype : phenotypeList) { String phenotypeString = (String) phenotype; phenotypeQueryBuilder = phenotypeQueryBuilder .or(QueryBuilder.start("referenceClinVarAssertion.traitSet.trait.name.elementValue.value") .text(phenotypeString).get()); }//w w w .j a v a2 s . c o m builder = builder.and(phenotypeQueryBuilder.get()); } return builder; }
From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java
License:Apache License
private QueryBuilder addClinvarRegionFilter(QueryBuilder builder, QueryOptions options) { List<Object> regions = options.getList("region", null); BasicDBList regionList = new BasicDBList(); if (regions != null) { Region region = (Region) regions.get(0); QueryBuilder regionQueryBuilder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end") .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd()); for (int i = 1; i < regions.size(); i++) { region = (Region) regions.get(i); regionQueryBuilder = regionQueryBuilder.or(QueryBuilder.start("chromosome") .is(region.getChromosome()).and("end").greaterThanEquals(region.getStart()).and("start") .lessThanEquals(region.getEnd()).get()); }/*w w w . ja v a 2s .c om*/ builder = builder.and(regionQueryBuilder.get()); } return builder; }
From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java
License:Apache License
private QueryBuilder addClinvarIdFilter(QueryBuilder builder, QueryOptions options) { List<Object> idList = options.getList("id", null); if (idList != null && idList.size() > 0) { QueryBuilder idQueryBuilder = QueryBuilder.start(); for (Object id : idList) { String idString = (String) id; if (idString.toLowerCase().startsWith("rs")) { idQueryBuilder = idQueryBuilder.or(QueryBuilder.start( "clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.type") .is("rs") .and("clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.id") .is(Integer.valueOf(idString.substring(2))).get()); } else if (idString.toLowerCase().startsWith("rcv")) { idQueryBuilder = idQueryBuilder.or(QueryBuilder .start("clinvarList.clinvarSet.referenceClinVarAssertion.clinVarAccession.acc") .is(idString).get()); }/*from w ww . j av a 2 s . c o m*/ } builder = builder.and(idQueryBuilder.get()); } return builder; }
From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java
License:Apache License
@Override public List<QueryResult> getAllClinvarByRegionList(List<Region> regions, QueryOptions options) { List<DBObject> queries = new ArrayList<>(); options.addToListOption("include", "clinvarList"); options.addToListOption("include", "chromosome"); options.addToListOption("include", "start"); options.addToListOption("include", "end"); options.addToListOption("include", "reference"); options.addToListOption("include", "alternate"); 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("chromosome").is(region.getChromosome()).and("end") .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd()); builder = addClinvarQueryFilters(builder, options); System.out.println(builder.get().toString()); queries.add(builder.get());//from w w w . j a v a 2s. c om ids.add(region.toString()); } return prepareClinvarQueryResultList(executeQueryList2(ids, queries, options)); }