VariantParseQuery extends ParseQuery implements Serializable { public String parse(Query query, QueryOptions queryOptions, String viewName) { Set<String> keySet = query.keySet(); for (String key : keySet) { String value = (String) query.get(key); if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { throw new IllegalArgumentException("key or value are null or empty (key = " + key + ", value = " + value + ")"); } String[] fields = key.split("\\."); if (fields.length == 0) { System.out.println("length is 0, nothing to do"); return null; } switch (fields[0]) { case "id": case "chromosome": case "type": filters.add(processFilter(fields[0], value, true, false)); break; case "start": case "end": case "length": filters.add(processFilter(fields[0], value, false, false)); break; case "region": processRegionQuery(value, "chromosome", "start", "end"); break; case "names": filters.add(processFilter("hgvs", value, true, true)); break; case "studies": processStudyQuery(fields, value); break; case "annotation": processAnnotationQuery(fields, value); break; default: break; } } buildQueryString(viewName, queryOptions); return sqlQueryString.toString(); } VariantParseQuery(); String parse(Query query, QueryOptions queryOptions, String viewName);  }
@Test public void parseConservationAND() throws Exception { String result = "SELECT * FROM test LATERAL VIEW explode(annotation.conservation) acons as cons " + "WHERE ((cons.source = 'phastCons' AND cons.score<03) " + "AND (cons.source = 'phylop' AND cons.score>0.5)) " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("annotation.conservation", "phastCons<03;phylop>0.5"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }
@Test public void parseConsequenceTypeName() throws Exception { String result = "SELECT * FROM test LATERAL VIEW explode(annotation.consequenceTypes) act as ct " + "LATERAL VIEW explode(ct.sequenceOntologyTerms) ctso as so WHERE so.name = 'missense_variant' " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("annotation.consequenceTypes.sequenceOntologyTerms.name", "missense_variant"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }
@Test public void parseConsequenceTypeAccession() throws Exception { String result = "SELECT * FROM test LATERAL VIEW explode(annotation.consequenceTypes) act as ct " + "LATERAL VIEW explode(ct.sequenceOntologyTerms) ctso as so WHERE so.accession = 'SO:0001566' " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("annotation.consequenceTypes.sequenceOntologyTerms.accession", "SO:0001566"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }
@Test public void parsePopulationFrequenciesOR() throws Exception { String result = "SELECT * FROM test LATERAL VIEW explode(annotation.populationFrequencies) apf as popfreq " + "WHERE ((popfreq.study = '1000G' AND popfreq.population = 'CEU' AND popfreq.altAlleleFreq<1.2) " + "OR (popfreq.study = '1000G' AND popfreq.population = 'ASW' AND popfreq.altAlleleFreq<1.25)) " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("annotation.populationFrequencies.altAlleleFreq", "1000G::CEU<1.2,1000G::ASW<1.25"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }
@Test public void parsePopulationFrequenciesAND() throws Exception { String result = "SELECT * FROM test LATERAL VIEW explode(annotation.populationFrequencies) apf as popfreq " + "WHERE ((popfreq.study = '1000G' AND popfreq.population = 'CEU' AND popfreq.altAlleleFreq<1.2) " + "AND (popfreq.study = '1000G' AND popfreq.population = 'ASW' AND popfreq.altAlleleFreq<1.25)) " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("annotation.populationFrequencies.altAlleleFreq", "1000G::CEU<1.2;1000G::ASW<1.25"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }
@Test public void parseMultipleIds() throws Exception { String result = "SELECT * FROM test WHERE (id = 'rs587604674' OR id = 'rs587603352') " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("id", "rs587604674,rs587603352"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }
@Test public void parseEmptyKey() throws Exception { Query query = new Query(); query.put("", "2"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("key or value are null or empty (key = , value = 2)"); variantParseQuery.parse(query, null, "test"); }
@Test public void parseEmptyValue() throws Exception { Query query = new Query(); query.put("chromosome", ""); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("key or value are null or empty (key = chromosome, value = )"); variantParseQuery.parse(query, null, "test"); }
@Test public void parseConservationOR() throws Exception { String result = "SELECT * FROM test LATERAL VIEW explode(annotation.conservation) acons as cons " + "WHERE ((cons.source = 'phastCons' AND cons.score<03) " + "OR (cons.source = 'phylop' AND cons.score>0.5)) " + "ORDER BY chromosome, start ASC"; Query query = new Query(); query.put("annotation.conservation", "phastCons<03,phylop>0.5"); String test = variantParseQuery.parse(query, null, "test"); assertEquals("SQL query does not match", result, test); }