List of usage examples for com.mongodb QueryBuilder get
public DBObject get()
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
private QueryBuilder addFrequencyFilter(String key, String alleleFrequencyField, List<String> list, QueryBuilder builder) {//www.j a va2 s .co m List<DBObject> ands = new ArrayList<>(); for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) { String elem = iterator.next(); String[] split = elem.split(":"); if (split.length == 2) { String study = split[0]; String population = split[1]; // String frequency = split[2]; String[] populationFrequency = splitKeyValue(population); QueryBuilder frequencyBuilder = new QueryBuilder(); frequencyBuilder.and(DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_STUDY_FIELD) .is(study); // frequencyBuilder.and(DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_POP_FIELD).is(population); frequencyBuilder.and(DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_POP_FIELD) .is(populationFrequency[0]); // addCompQueryFilter(alleleFrequencyField, frequency, frequencyBuilder); System.out.println("populationFrequency = " + Arrays.toString(populationFrequency)); addCompQueryFilter(alleleFrequencyField, populationFrequency[1], frequencyBuilder); ands.add(new BasicDBObject(key, new BasicDBObject("$elemMatch", frequencyBuilder.get()))); } else { logger.error("Bad score filter: " + elem); iterator.remove(); //Remove the malformed query params. } } if (!ands.isEmpty()) { builder.and(ands.toArray(new DBObject[ands.size()])); } return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Deprecated private QueryBuilder parseQueryOptions(QueryOptions options, QueryBuilder builder) { if (options != null) { if (options.containsKey("sort")) { if (options.getBoolean("sort")) { options.put("sort", new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, 1) .append(DBObjectToVariantConverter.START_FIELD, 1)); } else { options.remove("sort"); }//from w ww . ja v a 2 s . co m } /** GENOMIC REGION **/ if (options.containsKey(VariantQueryParams.REGION.key()) && !options.getString(VariantQueryParams.REGION.key()).isEmpty()) { List<String> stringList = options.getAsStringList(VariantQueryParams.REGION.key()); List<Region> regions = new ArrayList<>(stringList.size()); for (String reg : stringList) { Region region = Region.parseRegion(reg); regions.add(region); } getRegionFilter(regions, builder); } // if (options.containsKey(VariantQueryParams.CHROMOSOME.key())) { // List<String> chromosome = options.getAsStringList(VariantQueryParams.CHROMOSOME.key()); // addQueryListFilter(DBObjectToVariantConverter.CHROMOSOME_FIELD, chromosome, builder, QueryOperation.OR); // } if (options.containsKey(VariantQueryParams.GENE.key())) { List<String> xrefs = options.getAsStringList(VariantQueryParams.GENE.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "." + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD, xrefs, builder, QueryOperation.OR); } if (options.getString(VariantQueryParams.ID.key()) != null && !options.getString(VariantQueryParams.ID.key()).isEmpty()) { //) && !options.getString("id").isEmpty()) { List<String> ids = options.getAsStringList(VariantQueryParams.ID.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "." + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD, ids, builder, QueryOperation.OR); addQueryListFilter(DBObjectToVariantConverter.IDS_FIELD, ids, builder, QueryOperation.OR); } /** VARIANT **/ if (options.containsKey(VariantQueryParams.TYPE.key())) { // && !options.getString("type").isEmpty()) { addQueryStringFilter(DBObjectToVariantConverter.TYPE_FIELD, options.getString(VariantQueryParams.TYPE.key()), builder); } if (options.containsKey(VariantQueryParams.REFERENCE.key()) && options.getString(VariantQueryParams.REFERENCE.key()) != null) { addQueryStringFilter(DBObjectToVariantConverter.REFERENCE_FIELD, options.getString(VariantQueryParams.REFERENCE.key()), builder); } if (options.containsKey(VariantQueryParams.ALTERNATE.key()) && options.getString(VariantQueryParams.ALTERNATE.key()) != null) { addQueryStringFilter(DBObjectToVariantConverter.ALTERNATE_FIELD, options.getString(VariantQueryParams.ALTERNATE.key()), builder); } /** ANNOTATION **/ if (options.containsKey(VariantQueryParams.ANNOTATION_EXISTS.key())) { builder.and(DBObjectToVariantConverter.ANNOTATION_FIELD) .exists(options.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key())); } if (options.containsKey(VariantQueryParams.ANNOT_XREF.key())) { List<String> xrefs = options.getAsStringList(VariantQueryParams.ANNOT_XREF.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "." + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD, xrefs, builder, QueryOperation.AND); } if (options.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())) { List<String> cts = new ArrayList<>( options.getAsStringList(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())); List<Integer> ctsInteger = new ArrayList<>(cts.size()); for (Iterator<String> iterator = cts.iterator(); iterator.hasNext();) { String ct = iterator.next(); if (ct.startsWith("SO:")) { ct = ct.substring(3); } try { ctsInteger.add(Integer.parseInt(ct)); } catch (NumberFormatException e) { logger.error("Error parsing integer ", e); iterator.remove(); //Remove the malformed query params. } } options.put(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key(), cts); //Replace the QueryOption without the malformed query params addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD, ctsInteger, builder, QueryOperation.AND); } if (options.containsKey(VariantQueryParams.ANNOT_BIOTYPE.key())) { List<String> biotypes = options.getAsStringList(VariantQueryParams.ANNOT_BIOTYPE.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.BIOTYPE_FIELD, biotypes, builder, QueryOperation.AND); } if (options.containsKey(VariantQueryParams.POLYPHEN.key())) { addCompQueryFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.POLYPHEN_FIELD + "." + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD, options.getString(VariantQueryParams.POLYPHEN.key()), builder); } if (options.containsKey(VariantQueryParams.SIFT.key())) { addCompQueryFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.SIFT_FIELD + "." + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD, options.getString(VariantQueryParams.SIFT.key()), builder); } if (options.containsKey(VariantQueryParams.PROTEIN_SUBSTITUTION.key())) { List<String> list = new ArrayList<>( options.getAsStringList(VariantQueryParams.PROTEIN_SUBSTITUTION.key())); addScoreFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.PROTEIN_SUBSTITUTION_SCORE_FIELD, list, builder); options.put(VariantQueryParams.PROTEIN_SUBSTITUTION.key(), list); //Replace the QueryOption without the malformed query params } if (options.containsKey(VariantQueryParams.CONSERVATION.key())) { List<String> list = new ArrayList<>(options.getAsStringList(VariantQueryParams.CONSERVATION.key())); addScoreFilter(DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSERVED_REGION_SCORE_FIELD, list, builder); options.put(VariantQueryParams.CONSERVATION.key(), list); //Replace the QueryOption without the malformed query params } if (options.containsKey(VariantQueryParams.ALTERNATE_FREQUENCY.key())) { List<String> list = new ArrayList<>( options.getAsStringList(VariantQueryParams.ALTERNATE_FREQUENCY.key())); addFrequencyFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD, DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, list, builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency } if (options.containsKey(VariantQueryParams.REFERENCE_FREQUENCY.key())) { List<String> list = new ArrayList<>( options.getAsStringList(VariantQueryParams.REFERENCE_FREQUENCY.key())); addFrequencyFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD, DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, list, builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency } /** STATS **/ if (options.get(VariantQueryParams.STATS_MAF.key()) != null && !options.getString(VariantQueryParams.STATS_MAF.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MAF_FIELD, options.getString(VariantQueryParams.STATS_MAF.key()), builder); } if (options.get(VariantQueryParams.STATS_MGF.key()) != null && !options.getString(VariantQueryParams.STATS_MGF.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MGF_FIELD, options.getString(VariantQueryParams.STATS_MGF.key()), builder); } if (options.get(VariantQueryParams.MISSING_ALLELES.key()) != null && !options.getString(VariantQueryParams.MISSING_ALLELES.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MISSALLELE_FIELD, options.getString(VariantQueryParams.MISSING_ALLELES.key()), builder); } if (options.get(VariantQueryParams.MISSING_GENOTYPES.key()) != null && !options.getString(VariantQueryParams.MISSING_GENOTYPES.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MISSGENOTYPE_FIELD, options.getString(VariantQueryParams.MISSING_GENOTYPES.key()), builder); } if (options.get("numgt") != null && !options.getString("numgt").isEmpty()) { for (String numgt : options.getAsStringList("numgt")) { String[] split = numgt.split(":"); addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.NUMGT_FIELD + "." + split[0], split[1], builder); } } // if (options.get("freqgt") != null && !options.getString("freqgt").isEmpty()) { // for (String freqgt : getStringList(options.get("freqgt"))) { // String[] split = freqgt.split(":"); // addCompQueryFilter( // DBObjectToVariantSourceEntryConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.FREQGT_FIELD + "." + split[0], // split[1], builder); // } // } /** FILES **/ QueryBuilder fileBuilder = QueryBuilder.start(); if (options.containsKey(VariantQueryParams.STUDIES.key())) { // && !options.getList("studies").isEmpty() && !options.getListAs("studies", String.class).get(0).isEmpty()) { addQueryListFilter(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, options.getAsIntegerList(VariantQueryParams.STUDIES.key()), fileBuilder, QueryOperation.AND); } if (options.containsKey(VariantQueryParams.FILES.key())) { // && !options.getList("files").isEmpty() && !options.getListAs("files", String.class).get(0).isEmpty()) { addQueryListFilter( DBObjectToVariantSourceEntryConverter.FILES_FIELD + "." + DBObjectToVariantSourceEntryConverter.FILEID_FIELD, options.getAsIntegerList(VariantQueryParams.FILES.key()), fileBuilder, QueryOperation.AND); } if (options.containsKey(VariantQueryParams.GENOTYPE.key())) { String sampleGenotypesCSV = options.getString(VariantQueryParams.GENOTYPE.key()); // String AND = ","; // String OR = ";"; // String IS = ":"; // String AND = "AND"; // String OR = "OR"; // String IS = ":"; String AND = ";"; String OR = ","; String IS = ":"; String[] sampleGenotypesArray = sampleGenotypesCSV.split(AND); for (String sampleGenotypes : sampleGenotypesArray) { String[] sampleGenotype = sampleGenotypes.split(IS); if (sampleGenotype.length != 2) { continue; } int sample = Integer.parseInt(sampleGenotype[0]); String[] genotypes = sampleGenotype[1].split(OR); QueryBuilder genotypesBuilder = QueryBuilder.start(); for (String genotype : genotypes) { String s = DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "." + DBObjectToSamplesConverter.genotypeToStorageType(genotype); //or [ {"samp.0|0" : { $elemMatch : { $eq : <sampleId> } } } ] genotypesBuilder.or(new BasicDBObject(s, new BasicDBObject("$elemMatch", new BasicDBObject("$eq", sample)))); } fileBuilder.and(genotypesBuilder.get()); } } DBObject fileQuery = fileBuilder.get(); if (fileQuery.keySet().size() != 0) { builder.and(DBObjectToVariantConverter.STUDIES_FIELD).elemMatch(fileQuery); } } logger.debug("Find = " + builder.get()); return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override @Deprecated/* w ww .jav a 2 s . c om*/ public QueryResult<Variant> getAllVariants(QueryOptions options) { QueryBuilder qb = QueryBuilder.start(); parseQueryOptions(options, qb); DBObject projection = parseProjectionQueryOptions(options); logger.debug("Query to be executed {}", qb.get().toString()); return variantsCollection.find(qb.get(), projection, getDbObjectToVariantConverter(new Query(options), options), options); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override @Deprecated//from w ww .j av a2 s . co m public QueryResult<Variant> getVariantById(String id, QueryOptions options) { // BasicDBObject query = new BasicDBObject(DBObjectToVariantConverter.ID_FIELD, id); if (options == null) { options = new QueryOptions(VariantQueryParams.ID.key(), id); } else { options.addToListOption(VariantQueryParams.ID.key(), id); } QueryBuilder qb = QueryBuilder.start(); parseQueryOptions(options, qb); DBObject projection = parseProjectionQueryOptions(options); logger.debug("Query to be executed {}", qb.get().toString()); // return coll.find(query, options, variantConverter); QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection, getDbObjectToVariantConverter(new Query(options), options), options); queryResult.setId(id); return queryResult; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override @Deprecated/* ww w .j av a2 s . co m*/ public QueryResult<Variant> getAllVariantsByRegion(Region region, QueryOptions options) { QueryBuilder qb = QueryBuilder.start(); getRegionFilter(region, qb); parseQueryOptions(options, qb); DBObject projection = parseProjectionQueryOptions(options); if (options == null) { options = new QueryOptions(); } QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection, getDbObjectToVariantConverter(new Query(options), options), options); queryResult.setId(region.toString()); return queryResult; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Deprecated public QueryResult getAllVariantsByRegionAndStudies(Region region, List<String> studyId, QueryOptions options) { // Aggregation for filtering when more than one study is present QueryBuilder qb = QueryBuilder.start(DBObjectToVariantConverter.STUDIES_FIELD + "." + DBObjectToVariantSourceEntryConverter.STUDYID_FIELD).in(studyId); getRegionFilter(region, qb);/*from w w w .j a v a2 s . c o m*/ parseQueryOptions(options, qb); DBObject match = new BasicDBObject("$match", qb.get()); DBObject unwind = new BasicDBObject("$unwind", "$" + DBObjectToVariantConverter.STUDIES_FIELD); DBObject match2 = new BasicDBObject("$match", new BasicDBObject( DBObjectToVariantConverter.STUDIES_FIELD + "." + DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, new BasicDBObject("$in", studyId))); logger.debug("Query to be executed {}", qb.get().toString()); return variantsCollection.aggregate(/*"$variantsRegionStudies", */Arrays.asList(match, unwind, match2), options); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Deprecated public QueryResult getAllVariantsByGene(String geneName, QueryOptions options) { QueryBuilder qb = QueryBuilder.start(); if (options == null) { options = new QueryOptions(VariantQueryParams.GENE.key(), geneName); } else {/*www.j av a 2 s. c o m*/ options.addToListOption(VariantQueryParams.GENE.key(), geneName); } options.put(VariantQueryParams.GENE.key(), geneName); parseQueryOptions(options, qb); DBObject projection = parseProjectionQueryOptions(options); QueryResult<Variant> queryResult = variantsCollection.find(qb.get(), projection, getDbObjectToVariantConverter(new Query(options), options), options); queryResult.setId(geneName); return queryResult; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override @Deprecated// w ww. j av a2 s . c o m public QueryResult groupBy(String field, QueryOptions options) { String documentPath; switch (field) { case "gene": default: documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.GENE_NAME_FIELD; break; case "ensemblGene": documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.ENSEMBL_GENE_ID_FIELD; break; case "ct": case "consequence_type": documentPath = DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD; break; } QueryBuilder qb = QueryBuilder.start(); parseQueryOptions(options, qb); DBObject match = new BasicDBObject("$match", qb.get()); DBObject project = new BasicDBObject("$project", new BasicDBObject("field", "$" + documentPath)); DBObject unwind = new BasicDBObject("$unwind", "$field"); DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$field") // .append("field", "$field") .append("count", new BasicDBObject("$sum", 1))); // sum, count, avg, ...? DBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", options != null ? options.getInt("order", -1) : -1)); // 1 = ascending, -1 = descending DBObject limit = new BasicDBObject("$limit", options != null && options.getInt("limit", -1) > 0 ? options.getInt("limit") : 10); return variantsCollection.aggregate(Arrays.asList(match, project, unwind, group, sort, limit), options); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantSourceMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<VariantSource> getAllSources(QueryOptions options) { MongoDBCollection coll = db.getCollection(collectionName); QueryBuilder qb = QueryBuilder.start(); parseQueryOptions(options, qb);//from w ww .ja va2s . com return coll.find(qb.get(), null, variantSourceConverter, options); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantSourceMongoDBAdaptor.java
License:Apache License
@Override public QueryResult getAllSourcesByStudyId(String studyId, QueryOptions options) { MongoDBCollection coll = db.getCollection(collectionName); QueryBuilder qb = QueryBuilder.start(); options.put("studyId", studyId); parseQueryOptions(options, qb);//from www. ja v a2s . c o m return coll.find(qb.get(), null, variantSourceConverter, options); }