public static VariantQueryException mixedAndOrOperators(VariantQueryParam param, String value) { return VariantQueryException.malformedParam(param, value, "Unable to mix AND (" + AND + ") and OR (" + OR + ") in the same query."); }
public static VariantQueryException geneNotFound(String gene) { return VariantQueryException.malformedParam(GENE, gene, "Gene not found!"); }
public static Column getConservationScoreColumn(String source, String rawValue, boolean throwException) throws VariantQueryException { source = source.toUpperCase(); switch (source) { case "PHASTCONS": return PHASTCONS; case "PHYLOP": return PHYLOP; case "GERP": return GERP; default: if (throwException) { throw VariantQueryException.malformedParam(ANNOT_CONSERVATION, rawValue); } else { logger.warn("Unknown Conservation source {}", rawValue); } return null; } }
public static VariantQueryException malformedParam(QueryParam queryParam, String value) { return malformedParam(queryParam, value, "Expected: " + queryParam.description()); }
private int parseInteger(Object value, VariantQueryParam param, String rawValue) { if (value instanceof Number) { return ((Number) value).intValue(); } else { try { return Integer.parseInt(value.toString()); } catch (NumberFormatException e) { if (param != null) { throw VariantQueryException.malformedParam(param, rawValue); } else { throw new VariantQueryException("Error parsing integer value '" + value + '\'', e); } } } }
public static int parseConsequenceType(String so) { int soAccession; boolean startsWithSO = so.toUpperCase().startsWith("SO:"); if (startsWithSO || StringUtils.isNumeric(so)) { try { if (startsWithSO) { soAccession = Integer.parseInt(so.substring("SO:".length())); } else { soAccession = Integer.parseInt(so); } } catch (NumberFormatException e) { throw VariantQueryException.malformedParam(VariantQueryParam.ANNOT_CONSEQUENCE_TYPE, so, "Not a valid SO number"); } if (!ConsequenceTypeMappings.accessionToTerm.containsKey(soAccession)) { throw VariantQueryException.malformedParam(VariantQueryParam.ANNOT_CONSEQUENCE_TYPE, so, "Not a valid SO number"); } } else { if (!ConsequenceTypeMappings.termToAccession.containsKey(so)) { throw VariantQueryException.malformedParam(VariantQueryParam.ANNOT_CONSEQUENCE_TYPE, so, "Not a valid Accession term"); } else { soAccession = ConsequenceTypeMappings.termToAccession.get(so); } } return soAccession; }
private double parseDouble(Object value, VariantQueryParam param, String rawValue) { if (value instanceof Number) { return ((Number) value).doubleValue(); } else { try { return Double.parseDouble(value.toString()); } catch (NumberFormatException e) { if (param != null) { throw VariantQueryException.malformedParam(param, rawValue); } else { throw new VariantQueryException("Error parsing decimal value '" + value + '\'', e); } } } }
public static Column getFunctionalScoreColumn(String source, boolean throwException, String rawValue) { switch (source.toUpperCase()) { case "CADD_RAW": return CADD_RAW; case "CADD_SCALED": return CADD_SCALED; default: if (throwException) { // throw VariantQueryException.malformedParam(ANNOT_FUNCTIONAL_SCORE, rawValue, "Unknown functional score."); throw VariantQueryException.malformedParam(ANNOT_FUNCTIONAL_SCORE, rawValue); } else { logger.warn("Unknown Conservation source {}", source); } } return Column.build(FUNCTIONAL_SCORE_PREFIX + source.toUpperCase(), PFloat.INSTANCE); }
/** * Parse the genotype filter. * * @param sampleGenotypes Genotypes filter value * @param map Initialized map to be filled with the sample to list of genotypes * @return QueryOperation between samples */ public static QueryOperation parseGenotypeFilter(String sampleGenotypes, Map<Object, List<String>> map) { Pair<QueryOperation, Map<String, String>> pair = parseMultiKeyValueFilter(GENOTYPE, sampleGenotypes); for (Map.Entry<String, String> entry : pair.getValue().entrySet()) { List<String> gts = splitValue(entry.getValue(), QueryOperation.OR); boolean anyNegated = false; boolean allNegated = true; for (String gt : gts) { if (isNegated(gt)) { anyNegated = true; } else { allNegated = false; } } if (!allNegated && anyNegated) { throw VariantQueryException.malformedParam(GENOTYPE, sampleGenotypes); } map.put(entry.getKey(), gts); } return pair.getKey(); }
public static void validateAnnotationQuery(Query query) { if (query == null) { return; } List<VariantQueryParam> acceptedParams = Arrays.asList(ID, REGION); List<VariantQueryParam> ignoredParams = Arrays.asList(INCLUDE_STUDY, INCLUDE_SAMPLE, INCLUDE_FILE); Set<VariantQueryParam> queryParams = VariantQueryUtils.validParams(query); queryParams.removeAll(acceptedParams); queryParams.removeAll(ignoredParams); if (!queryParams.isEmpty()) { // System.out.println("query.toJson() = " + query.toJson()); throw VariantQueryException.unsupportedVariantQueryFilters(queryParams, "Accepted params when querying annotation are : " + acceptedParams.stream() .map(QueryParam::key) .collect(Collectors.toList())); } List<String> invalidValues = new LinkedList<>(); for (String s : query.getAsStringList(ID.key())) { if (!VariantQueryUtils.isVariantId(s)) { invalidValues.add(s); break; } } if (!invalidValues.isEmpty()) { throw VariantQueryException.malformedParam(ID, invalidValues.toString(), "Only variants supported: chrom:start:ref:alt"); } }
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); } }
private QueryBuilder addStringCompQueryFilter(VariantQueryParam param, String key, String value, QueryBuilder builder) { String[] split = splitOperator(value); String op = split[1]; String obj = split[2]; switch (op) { case "!=": case "!": builder.and(key).notEquals(obj); break; case "~=": case "~": builder.and(key).regex(Pattern.compile(obj)); break; case "": case "=": case "==": builder.and(key).is(obj); break; default: throw VariantQueryException.malformedParam(param, value, "Unsupported operator " + op); } return builder; }
public Integer getReleaseFilter(Query query, String sessionId) throws CatalogException { Integer release; if (isValidParam(query, VariantQueryParam.RELEASE)) { release = query.getInt(VariantQueryParam.RELEASE.key(), -1); if (release <= 0) { throw VariantQueryException.malformedParam(VariantQueryParam.RELEASE, query.getString(VariantQueryParam.RELEASE.key())); } Project project = getProjectFromQuery(query, sessionId, new QueryOptions(INCLUDE, ProjectDBAdaptor.QueryParams.CURRENT_RELEASE.key())); int currentRelease = project.getCurrentRelease(); if (release > currentRelease) { throw VariantQueryException.malformedParam(VariantQueryParam.RELEASE, query.getString(VariantQueryParam.RELEASE.key())); } else if (release == currentRelease) { // Using latest release. We don't need to filter by release! release = null; } // else, filter by release } else { release = null; } return release; }
@Test public void testGetAllVariants_functionalScore_wrongValue() { String value = "cadd_scaled<=A"; VariantQueryException expected = VariantQueryException.malformedParam(ANNOT_FUNCTIONAL_SCORE, value); thrown.expect(expected.getClass()); thrown.expectMessage(expected.getMessage()); query(new Query(ANNOT_FUNCTIONAL_SCORE.key(), value), null); }
@Test public void testGetAllVariants_conservationScoreWrongValue() { VariantQueryException e = VariantQueryException.malformedParam(ANNOT_CONSERVATION, "phastCons<a"); thrown.expect(e.getClass()); thrown.expectMessage(e.getMessage()); query(new Query(ANNOT_CONSERVATION.key(), "phastCons<a"), null); }
@Test public void testGetAllVariants_functionalScore_wrongSource() { String value = "cad<=0.5"; VariantQueryException expected = VariantQueryException.malformedParam(ANNOT_FUNCTIONAL_SCORE, value); thrown.expect(expected.getClass()); thrown.expectMessage(expected.getMessage()); query(new Query(ANNOT_FUNCTIONAL_SCORE.key(), value), null); }
@Test public void testGetAllVariants_conservationScoreWrongSource() { VariantQueryException e = VariantQueryException.malformedParam(ANNOT_CONSERVATION, "phast<0.5"); thrown.expect(e.getClass()); thrown.expectMessage(e.getMessage()); query(new Query(ANNOT_CONSERVATION.key(), "phast<0.5"), null); }
@Test public void queryByFamilyMissingModeOfInheritance() throws CatalogException { VariantQueryException e = VariantQueryException.malformedParam(FAMILY_PHENOTYPE, "asdf", "Require parameter \"family\" and \"modeOfInheritance\" to use \"familyDisease\"."); thrown.expectMessage(e.getMessage()); thrown.expect(e.getClass()); queryUtils.parseQuery(new Query(STUDY.key(), "s1").append(FAMILY.key(), "f1").append(FAMILY_PHENOTYPE.key(), "asdf"), sessionId); }
@Test public void queryByFamilyUnknownDisease() throws CatalogException { VariantQueryException e = VariantQueryException.malformedParam(FAMILY_PHENOTYPE, "asdf", "Available phenotypes: [phenotype]"); thrown.expectMessage(e.getMessage()); thrown.expect(e.getClass()); queryUtils.parseQuery(new Query(STUDY.key(), "s1") .append(FAMILY.key(), "f1") .append(MODE_OF_INHERITANCE.key(), "monoallelic") .append(FAMILY_PHENOTYPE.key(), "asdf"), sessionId); }