List of usage examples for com.mongodb QueryBuilder or
@SuppressWarnings("unchecked") public QueryBuilder or(final DBObject... ors)
From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java
License:Apache License
private <T> QueryBuilder addQueryFilter(String key, String value, final QueryBuilder builder, QueryOperation op, Function<String, T> map) { QueryOperation operation = checkOperator(value); QueryBuilder auxBuilder;/*ww w . java2s . co m*/ if (op == QueryOperation.OR) { auxBuilder = QueryBuilder.start(); } else { auxBuilder = builder; } if (operation == null) { if (value.startsWith("!")) { T mapped = map.apply(value.substring(1)); if (mapped instanceof Collection) { auxBuilder.and(key).notIn(mapped); } else { auxBuilder.and(key).notEquals(mapped); } } else { T mapped = map.apply(value); if (mapped instanceof Collection) { auxBuilder.and(key).in(mapped); } else { auxBuilder.and(key).is(mapped); } } } else if (operation == QueryOperation.OR) { String[] array = value.split(OR); List list = new ArrayList(array.length); for (String elem : array) { if (elem.startsWith("!")) { throw new VariantQueryException( "Unable to use negate (!) operator in OR sequences (<it_1>(,<it_n>)*)"); } else { T mapped = map.apply(elem); if (mapped instanceof Collection) { list.addAll(((Collection) mapped)); } else { list.add(mapped); } } } auxBuilder.and(key).in(list); } else { //Split in two lists: positive and negative String[] array = value.split(AND); List listIs = new ArrayList(array.length); List listNotIs = new ArrayList(array.length); for (String elem : array) { if (elem.startsWith("!")) { T mapped = map.apply(elem.substring(1)); if (mapped instanceof Collection) { listNotIs.addAll(((Collection) mapped)); } else { listNotIs.add(mapped); } } else { T mapped = map.apply(elem); if (mapped instanceof Collection) { listIs.addAll(((Collection) mapped)); } else { listIs.add(mapped); } } } if (!listIs.isEmpty()) { //Can not use method "is" because it will be overwritten with the "notEquals" or "notIn" method auxBuilder.and(key).all(listIs); } if (listNotIs.size() == 1) { auxBuilder.and(key).notEquals(listNotIs.get(0)); } else if (listNotIs.size() > 1) { auxBuilder.and(key).notIn(listNotIs); } } if (op == QueryOperation.OR) { builder.or(auxBuilder.get()); } return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java
License:Apache License
/** * Accept a list of comparative filters separated with "," or ";" with the expression: * {OPERATION}{VALUE}, where the accepted operations are: <, <=, >, >=, =, ==, !=, ~=. * * @param key/*from w w w . j a v a 2 s . c o m*/ * @param value * @param builder * @param extendKey * @return */ private QueryBuilder addCompListQueryFilter(String key, String value, QueryBuilder builder, boolean extendKey) { QueryOperation op = checkOperator(value); List<String> values = splitValue(value, op); QueryBuilder compBuilder; if (op == QueryOperation.OR) { compBuilder = QueryBuilder.start(); } else { compBuilder = builder; } for (String elem : values) { addCompQueryFilter(key, elem, compBuilder, extendKey); } if (op == QueryOperation.OR) { builder.or(compBuilder.get()); } return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java
License:Apache License
/** * Accept filters separated with "," or ";" with the expression: * [{STUDY}:]{COHORT}{OPERATION}{VALUE}. * Where STUDY is optional if defaultStudyConfiguration is provided * * @param key Stats field to filter * @param values Values to parse * @param builder QueryBuilder * @param defaultStudyConfiguration/* www. j ava 2s . c o m*/ */ private void addStatsFilterList(String key, String values, QueryBuilder builder, StudyConfiguration defaultStudyConfiguration) { QueryOperation op = checkOperator(values); List<String> valuesList = splitValue(values, op); List<DBObject> statsQueries = new LinkedList<>(); for (String value : valuesList) { statsQueries.add(addStatsFilter(key, value, new QueryBuilder(), defaultStudyConfiguration).get()); } if (!statsQueries.isEmpty()) { if (op == QueryOperation.OR) { builder.or(statsQueries.toArray(new DBObject[statsQueries.size()])); } else { builder.and(statsQueries.toArray(new DBObject[statsQueries.size()])); } } }
From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java
License:Apache License
private QueryBuilder getRegionFilter(List<Region> regions, QueryBuilder builder) { if (regions != null && !regions.isEmpty()) { DBObject[] objects = new DBObject[regions.size()]; int i = 0; for (Region region : regions) { DBObject regionObject = new BasicDBObject(); // if (region.getEnd() - region.getStart() < 1000000) { // List<String> chunkIds = getChunkIds(region); // regionObject.put(DocumentToVariantConverter.AT_FIELD + '.' + DocumentToVariantConverter // .CHUNK_IDS_FIELD, // new Document("$in", chunkIds)); // } else { // regionObject.put(DocumentToVariantConverter.CHROMOSOME_FIELD, region.getChromosome()); // } int end = region.getEnd(); if (end < Integer.MAX_VALUE) { // Avoid overflow end++;/*from w ww .ja v a 2s .c o m*/ } regionObject .put("_id", new Document() .append("$gte", VariantStringIdConverter.buildId(region.getChromosome(), region.getStart())) .append("$lt", VariantStringIdConverter.buildId(region.getChromosome(), end))); objects[i] = regionObject; i++; } builder.or(objects); } return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
private QueryBuilder parseQuery(Query query, QueryBuilder builder) { if (query != null) { /** VARIANT PARAMS **/ if (query.containsKey(VariantQueryParams.REGION.key()) && !query.getString(VariantQueryParams.REGION.key()).isEmpty()) { List<String> stringList = query.getAsStringList(VariantQueryParams.REGION.key()); List<Region> regions = new ArrayList<>(stringList.size()); for (String reg : stringList) { Region region = Region.parseRegion(reg); regions.add(region);/* www.j a v a 2 s.co m*/ } getRegionFilter(regions, builder); } if (query.getString(VariantQueryParams.ID.key()) != null && !query.getString(VariantQueryParams.ID.key()).isEmpty()) { List<String> ids = query.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); } if (query.containsKey(VariantQueryParams.GENE.key())) { List<String> xrefs = query.getAsStringList(VariantQueryParams.GENE.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "." + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD, xrefs, builder, QueryOperation.OR); } if (query.containsKey(VariantQueryParams.REFERENCE.key()) && query.getString(VariantQueryParams.REFERENCE.key()) != null) { addQueryStringFilter(DBObjectToVariantConverter.REFERENCE_FIELD, query.getString(VariantQueryParams.REFERENCE.key()), builder); } if (query.containsKey(VariantQueryParams.ALTERNATE.key()) && query.getString(VariantQueryParams.ALTERNATE.key()) != null) { addQueryStringFilter(DBObjectToVariantConverter.ALTERNATE_FIELD, query.getString(VariantQueryParams.ALTERNATE.key()), builder); } if (query.containsKey(VariantQueryParams.TYPE.key()) && !query.getString(VariantQueryParams.TYPE.key()).isEmpty()) { addQueryStringFilter(DBObjectToVariantConverter.TYPE_FIELD, query.getString(VariantQueryParams.TYPE.key()), builder); } /** ANNOTATION PARAMS **/ if (query.containsKey(VariantQueryParams.ANNOTATION_EXISTS.key())) { builder.and(DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.ANNOT_ID_FIELD); builder.exists(query.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key())); } if (query.containsKey(VariantQueryParams.ANNOT_XREF.key())) { List<String> xrefs = query.getAsStringList(VariantQueryParams.ANNOT_XREF.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "." + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD, xrefs, builder, QueryOperation.AND); } if (query.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())) { List<String> cts = new ArrayList<>( query.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. } } query.put(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key(), cts); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD, ctsInteger, builder, QueryOperation.AND); } if (query.containsKey(VariantQueryParams.ANNOT_BIOTYPE.key())) { List<String> biotypes = query.getAsStringList(VariantQueryParams.ANNOT_BIOTYPE.key()); addQueryListFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.BIOTYPE_FIELD, biotypes, builder, QueryOperation.AND); } if (query.containsKey(VariantQueryParams.POLYPHEN.key())) { addCompQueryFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.POLYPHEN_FIELD + "." + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD, query.getString(VariantQueryParams.POLYPHEN.key()), builder); } if (query.containsKey(VariantQueryParams.SIFT.key())) { System.out.println(query.getString(VariantQueryParams.SIFT.key())); addCompQueryFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.SIFT_FIELD + "." + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD, query.getString(VariantQueryParams.SIFT.key()), builder); } if (query.containsKey(VariantQueryParams.PROTEIN_SUBSTITUTION.key())) { List<String> list = new ArrayList<>( query.getAsStringList(VariantQueryParams.PROTEIN_SUBSTITUTION.key())); addScoreFilter( DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "." + DBObjectToVariantAnnotationConverter.PROTEIN_SUBSTITUTION_SCORE_FIELD, list, builder); query.put(VariantQueryParams.PROTEIN_SUBSTITUTION.key(), list); //Replace the QueryOption without the malformed query params } if (query.containsKey(VariantQueryParams.CONSERVATION.key())) { List<String> list = new ArrayList<>(query.getAsStringList(VariantQueryParams.CONSERVATION.key())); addScoreFilter(DBObjectToVariantConverter.ANNOTATION_FIELD + "." + DBObjectToVariantAnnotationConverter.CONSERVED_REGION_SCORE_FIELD, list, builder); query.put(VariantQueryParams.CONSERVATION.key(), list); } if (query.containsKey(VariantQueryParams.ALTERNATE_FREQUENCY.key())) { List<String> list = new ArrayList<>( query.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 (query.containsKey(VariantQueryParams.REFERENCE_FREQUENCY.key())) { List<String> list = new ArrayList<>( query.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 PARAMS **/ if (query.get(VariantQueryParams.STATS_MAF.key()) != null && !query.getString(VariantQueryParams.STATS_MAF.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MAF_FIELD, query.getString(VariantQueryParams.STATS_MAF.key()), builder); } if (query.get(VariantQueryParams.STATS_MGF.key()) != null && !query.getString(VariantQueryParams.STATS_MGF.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MGF_FIELD, query.getString(VariantQueryParams.STATS_MGF.key()), builder); } if (query.get(VariantQueryParams.MISSING_ALLELES.key()) != null && !query.getString(VariantQueryParams.MISSING_ALLELES.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MISSALLELE_FIELD, query.getString(VariantQueryParams.MISSING_ALLELES.key()), builder); } if (query.get(VariantQueryParams.MISSING_GENOTYPES.key()) != null && !query.getString(VariantQueryParams.MISSING_GENOTYPES.key()).isEmpty()) { addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MISSGENOTYPE_FIELD, query.getString(VariantQueryParams.MISSING_GENOTYPES.key()), builder); } if (query.get("numgt") != null && !query.getString("numgt").isEmpty()) { for (String numgt : query.getAsStringList("numgt")) { String[] split = numgt.split(":"); addCompQueryFilter( DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.NUMGT_FIELD + "." + split[0], split[1], builder); } } /** STUDIES **/ QueryBuilder studyBuilder = QueryBuilder.start(); if (query.containsKey(VariantQueryParams.STUDIES.key())) { // && !options.getList("studies").isEmpty() && !options.getListAs("studies", String.class).get(0).isEmpty()) { List<Integer> studyIds = getStudyIds(query.getAsList(VariantQueryParams.STUDIES.key()), null); addQueryListFilter(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, studyIds, studyBuilder, QueryOperation.AND); } if (query.containsKey(VariantQueryParams.FILES.key())) { // && !options.getList("files").isEmpty() && !options.getListAs("files", String.class).get(0).isEmpty()) { addQueryListFilter( DBObjectToVariantSourceEntryConverter.FILES_FIELD + "." + DBObjectToVariantSourceEntryConverter.FILEID_FIELD, query.getAsIntegerList(VariantQueryParams.FILES.key()), studyBuilder, QueryOperation.AND); } if (query.containsKey(VariantQueryParams.GENOTYPE.key())) { String sampleGenotypesCSV = query.getString(VariantQueryParams.GENOTYPE.key()); // String AND = ","; // String OR = ";"; // String IS = ":"; // String AND = "AND"; // String OR = "OR"; // String IS = ":"; String AND = ";"; String OR = ","; String IS = ":"; // we may need to know the study type // studyConfigurationManager.getStudyConfiguration(1, null).getResult().get(0). String[] sampleGenotypesArray = sampleGenotypesCSV.split(AND); System.out.println("sampleGenotypesArray = " + Arrays.toString(sampleGenotypesArray)); 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) { if ("0/0".equals(genotype) || "0|0".equals(genotype)) { QueryBuilder andBuilder = QueryBuilder.start(); List<String> otherGenotypes = Arrays.asList("0/1", "1/0", "1/1", "-1/-1", "0|1", "1|0", "1|1", "-1|-1", "0|2", "2|0", "2|1", "1|2", "2|2", "0/2", "2/0", "2/1", "1/2", "2/2", DBObjectToSamplesConverter.UNKNOWN_GENOTYPE); for (String otherGenotype : otherGenotypes) { andBuilder.and(new BasicDBObject( DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "." + otherGenotype, new BasicDBObject("$not", new BasicDBObject("$elemMatch", new BasicDBObject("$eq", sample))))); } genotypesBuilder.or(andBuilder.get()); } else { 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)))); } } studyBuilder.and(genotypesBuilder.get()); } } // If Study Query is used then we add a elemMatch query DBObject studyQuery = studyBuilder.get(); if (studyQuery.keySet().size() != 0) { builder.and(DBObjectToVariantConverter.STUDIES_FIELD).elemMatch(studyQuery); } } logger.debug("Find = " + builder.get()); return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
private QueryBuilder addQueryListFilter(String key, List<?> values, QueryBuilder builder, QueryOperation op) { if (values != null) if (values.size() == 1) { if (op == QueryOperation.AND) { builder.and(key).is(values.get(0)); } else { builder.or(QueryBuilder.start(key).is(values.get(0)).get()); }/*from w ww. j av a 2 s . c o m*/ } else if (!values.isEmpty()) { if (op == QueryOperation.AND) { builder.and(key).in(values); } else { builder.or(QueryBuilder.start(key).in(values).get()); } } return builder; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
private QueryBuilder getRegionFilter(List<Region> regions, QueryBuilder builder) { if (regions != null && !regions.isEmpty()) { DBObject[] objects = new DBObject[regions.size()]; int i = 0; for (Region region : regions) { if (region.getEnd() - region.getStart() < 1000000) { List<String> chunkIds = getChunkIds(region); DBObject regionObject = new BasicDBObject("_at.chunkIds", new BasicDBObject("$in", chunkIds)) .append(DBObjectToVariantConverter.START_FIELD, new BasicDBObject("$lte", region.getEnd())) .append(DBObjectToVariantConverter.END_FIELD, new BasicDBObject("$gte", region.getStart())); objects[i] = regionObject; } else { DBObject regionObject = new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, region.getChromosome()) .append(DBObjectToVariantConverter.START_FIELD, new BasicDBObject("$lte", region.getEnd())) .append(DBObjectToVariantConverter.END_FIELD, new BasicDBObject("$gte", region.getStart())); objects[i] = regionObject; }//from w ww.j av a 2 s .c o m i++; } builder.or(objects); } 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 w w .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.wrml.contrib.runtime.service.mongo.MongoService.java
License:Apache License
private DBObject createMongoQuery(final SearchCriteria searchCriteria) { QueryBuilder queryBuilder = null;/*from w w w . j a va 2 s .com*/ final List<SearchCriterion> and = searchCriteria.getAnd(); if (and != null && !and.isEmpty()) { queryBuilder = new QueryBuilder(); for (final SearchCriterion searchCriterion : and) { final String referenceSlot = searchCriterion.getReferenceSlot(); queryBuilder.and(referenceSlot); addQueryCriterion(searchCriterion, queryBuilder); } } final List<SearchCriterion> or = searchCriteria.getOr(); if (or != null && !or.isEmpty()) { final DBObject[] orQueryCriterionArray = new DBObject[or.size()]; for (int i = 0; i < or.size(); i++) { final SearchCriterion searchCriterion = or.get(i); final String referenceSlot = searchCriterion.getReferenceSlot(); final QueryBuilder orQueryCriterionBuilder = QueryBuilder.start(referenceSlot); addQueryCriterion(searchCriterion, orQueryCriterionBuilder); orQueryCriterionArray[i] = orQueryCriterionBuilder.get(); } final QueryBuilder orQueryBuilder = new QueryBuilder(); orQueryBuilder.or(orQueryCriterionArray); if (queryBuilder != null) { // AND the OR clause together with the AND query queryBuilder.and(orQueryBuilder.get()); } else { queryBuilder = orQueryBuilder; } } if (queryBuilder == null) { return null; } final DBObject mongoQuery = queryBuilder.get(); return mongoQuery; }