protected QueryOperation getQueryOperation(String valuesStr) { QueryOperation queryOperation = VariantQueryUtils.checkOperator(valuesStr); if (queryOperation == null) { queryOperation = QueryOperation.OR; } return queryOperation; }
/** * Checks that the filter value list contains only one type of operations. * * @param value List of values to check * @return The used operator. Null if no operator is used. * @throws VariantQueryException if the list contains different operators. */ public static QueryOperation checkOperator(String value) throws VariantQueryException { return checkOperator(value, null); }
private QueryOperation parseOrAndFilter(String param, String value) { QueryOperation queryOperation = VariantQueryUtils.checkOperator(value, VariantQueryParam.valueOf(param)); if (queryOperation == null) { // return AND by default return QueryOperation.AND; } else { return queryOperation; } }
/** * Splits the string with the specified operation. * * @param value Value to split * @return List of values, without the delimiter */ public static Pair<QueryOperation, List<String>> splitValue(String value) { QueryOperation operation = checkOperator(value); return Pair.of(operation, splitValue(value, operation)); }
private <T> QueryBuilder addQueryFilter(String key, String value, final QueryBuilder builder, QueryOperation op, Function<String, T> map) { VariantQueryUtils.QueryOperation intraOp = checkOperator(value); return addQueryFilter(key, splitValue(value, intraOp), builder, op, intraOp, map); }
public static boolean isValidParam(Query query, QueryParam param, boolean discardNegated) { boolean validParam = isValidParam(query, param); if (!discardNegated || !validParam) { return validParam; } else { String strValue = query.getString(param.key()); return splitValue(strValue, checkOperator(strValue)) .stream() .anyMatch((v) -> !isNegated(v)); // Discard negated } }
/** * 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 */ 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()])); } } }
public static boolean isOutputMultiStudy(Query query, QueryOptions options, Collection<?> studies) { Set<VariantField> fields = VariantField.getIncludeFields(options); if (!fields.contains(VariantField.STUDIES)) { return false; } else if (isValidParam(query, INCLUDE_STUDY)) { String includeStudy = query.getString(VariantQueryParam.INCLUDE_STUDY.key()); if (NONE.equals(includeStudy)) { return false; } else if (ALL.equals(includeStudy)) { return studies.size() > 1; } else { return query.getAsList(VariantQueryParam.INCLUDE_STUDY.key()).size() > 1; } } else if (isValidParam(query, STUDY)) { String value = query.getString(VariantQueryParam.STUDY.key()); long numStudies = splitValue(value, checkOperator(value)).stream().filter(s -> !isNegated(s)).count(); return numStudies > 1; } else { return studies.size() > 1; } }
/** * Parse INFO param. * * @param query Query to parse * @return a pair with the internal QueryOperation (AND/OR) and a map between Files and INFO filters. */ public static Pair<QueryOperation, Map<String, String>> parseInfo(Query query) { if (!isValidParam(query, INFO)) { return Pair.of(null, Collections.emptyMap()); } String value = query.getString(INFO.key()); if (value.contains(IS)) { return parseMultiKeyValueFilter(INFO, value); } else { List<String> files = query.getAsStringList(FILE.key()); files.removeIf(VariantQueryUtils::isNegated); if (files.isEmpty()) { files = query.getAsStringList(INCLUDE_FILE.key()); } if (files.isEmpty()) { throw VariantQueryException.malformedParam(INFO, value, "Missing \"" + FILE.key() + "\" param."); } QueryOperation operator = checkOperator(value); Map<String, String> map = new LinkedHashMap<>(files.size()); for (String file : files) { map.put(file, value); } return Pair.of(operator, map); } }
public static List<String> getIncludeStudiesList(Query query, Set<VariantField> fields) { List<String> studies; if (!fields.contains(VariantField.STUDIES)) { studies = Collections.emptyList(); } else if (isValidParam(query, INCLUDE_STUDY)) { String includeStudy = query.getString(VariantQueryParam.INCLUDE_STUDY.key()); if (NONE.equals(includeStudy)) { studies = Collections.emptyList(); } else if (ALL.equals(includeStudy)) { studies = null; } else { studies = query.getAsStringList(VariantQueryParam.INCLUDE_STUDY.key()); } } else if (isValidParam(query, STUDY)) { String value = query.getString(VariantQueryParam.STUDY.key()); studies = new ArrayList<>(splitValue(value, checkOperator(value))); studies.removeIf(VariantQueryUtils::isNegated); // if empty, all the studies if (studies.isEmpty()) { studies = null; } } else { studies = null; } return studies; }
@Test public void testCheckOperatorMixQuotesAnd() throws Exception { assertEquals(VariantQueryUtils.QueryOperation.AND, VariantQueryUtils.checkOperator("a;\"b,c\"")); }
@Test public void testCheckOperatorMix() throws Exception { thrown.expect(VariantQueryException.class); VariantQueryUtils.checkOperator("a,b;c"); }
@Test public void testCheckOperatorMixQuotesOr() throws Exception { assertEquals(VariantQueryUtils.QueryOperation.OR, VariantQueryUtils.checkOperator("a,\"b;c\"")); }
@Test public void testCheckOperatorOR() throws Exception { assertEquals(VariantQueryUtils.QueryOperation.OR, VariantQueryUtils.checkOperator("a,b,c")); }
@Test public void testCheckOperatorANY() throws Exception { assertNull(VariantQueryUtils.checkOperator("a")); }
@Test public void testCheckOperatorAND() throws Exception { assertEquals(VariantQueryUtils.QueryOperation.AND, VariantQueryUtils.checkOperator("a;b;c")); }
public static void convertExpressionToGeneQuery(Query query, CellBaseUtils cellBaseUtils) { if (isValidParam(query, VariantQueryParam.ANNOT_EXPRESSION)) { String value = query.getString(VariantQueryParam.ANNOT_EXPRESSION.key()); // Check if comma separated of semi colon separated (AND or OR) VariantQueryUtils.QueryOperation queryOperation = checkOperator(value); // Split by comma or semi colon List<String> expressionValues = splitValue(value, queryOperation); if (queryOperation == VariantQueryUtils.QueryOperation.AND) { throw VariantQueryException.malformedParam(VariantQueryParam.ANNOT_EXPRESSION, value, "Unimplemented AND operator"); } // query.remove(VariantQueryParam.ANNOT_EXPRESSION.key()); Set<String> genesByExpression = cellBaseUtils.getGenesByExpression(expressionValues); if (genesByExpression.isEmpty()) { genesByExpression = Collections.singleton(NONE); } query.put(ANNOT_EXPRESSION_GENES.key(), genesByExpression); } }
public static void convertGoToGeneQuery(Query query, CellBaseUtils cellBaseUtils) { if (isValidParam(query, VariantQueryParam.ANNOT_GO)) { String value = query.getString(VariantQueryParam.ANNOT_GO.key()); // Check if comma separated of semi colon separated (AND or OR) VariantQueryUtils.QueryOperation queryOperation = checkOperator(value); // Split by comma or semi colon List<String> goValues = splitValue(value, queryOperation); if (queryOperation == VariantQueryUtils.QueryOperation.AND) { throw VariantQueryException.malformedParam(VariantQueryParam.ANNOT_GO, value, "Unimplemented AND operator"); } // query.remove(VariantQueryParam.ANNOT_GO.key()); Set<String> genesByGo = cellBaseUtils.getGenesByGo(goValues); if (genesByGo.isEmpty()) { genesByGo = Collections.singleton(NONE); } query.put(ANNOT_GO_GENES.key(), genesByGo); } }
public static StudyConfiguration getDefaultStudyConfiguration(Query query, QueryOptions options, StudyConfigurationManager studyConfigurationManager) { final StudyConfiguration defaultStudyConfiguration; if (isValidParam(query, VariantQueryParam.STUDY)) { String value = query.getString(VariantQueryParam.STUDY.key()); // Check that the study exists VariantQueryUtils.QueryOperation studiesOperation = checkOperator(value); List<String> studiesNames = splitValue(value, studiesOperation); List<Integer> studyIds = studyConfigurationManager.getStudyIds(studiesNames, options); // Non negated studyIds if (studyIds.size() == 1) { defaultStudyConfiguration = studyConfigurationManager.getStudyConfiguration(studyIds.get(0), null).first(); } else { defaultStudyConfiguration = null; } } else { List<String> studyNames = studyConfigurationManager.getStudyNames(null); if (studyNames != null && studyNames.size() == 1) { defaultStudyConfiguration = studyConfigurationManager.getStudyConfiguration(studyNames.get(0), new QueryOptions()).first(); } else { defaultStudyConfiguration = null; } } return defaultStudyConfiguration; }
/** * Accept a list of comparative filters separated with "," or ";" with the expression: * {OPERATION}{VALUE}, where the accepted operations are: <, <=, >, >=, =, ==, !=, ~=. * * @param key * @param value * @param builder * @param extendKey * @return */ private QueryBuilder addCompListQueryFilter(String key, String value, QueryBuilder builder, boolean extendKey) { VariantQueryUtils.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; }