diff --git a/lapis2/src/main/antlr/org/genspectrum/lapis/model/variantqueryparser/VariantQuery.g4 b/lapis2/src/main/antlr/org/genspectrum/lapis/model/variantqueryparser/VariantQuery.g4 index c2b5965f..a550c9a9 100644 --- a/lapis2/src/main/antlr/org/genspectrum/lapis/model/variantqueryparser/VariantQuery.g4 +++ b/lapis2/src/main/antlr/org/genspectrum/lapis/model/variantqueryparser/VariantQuery.g4 @@ -5,6 +5,7 @@ grammar VariantQuery; start: expr EOF; expr: single # Uni + | '!' expr # Not | expr '&' expr # And ; diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt index 50704589..2c7d205b 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt @@ -2,9 +2,11 @@ package org.genspectrum.lapis.model import VariantQueryBaseListener import VariantQueryParser.AndContext +import VariantQueryParser.NotContext import VariantQueryParser.Nucleotide_mutationContext import org.antlr.v4.runtime.tree.ParseTreeListener import org.genspectrum.lapis.silo.And +import org.genspectrum.lapis.silo.Not import org.genspectrum.lapis.silo.NucleotideSymbolEquals import org.genspectrum.lapis.silo.SiloFilterExpression @@ -30,4 +32,9 @@ class VariantQueryCustomListener : VariantQueryBaseListener(), ParseTreeListener val children = listOf(expressionStack.removeLast(), expressionStack.removeLast()).reversed() expressionStack.addLast(And(children)) } + + override fun exitNot(ctx: NotContext?) { + val child = expressionStack.removeLast() + expressionStack.addLast(Not(child)) + } } diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt index d0b295a6..18423bf6 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt @@ -40,3 +40,5 @@ data class DateBetween(val column: String, val from: LocalDate?, val to: LocalDa object True : SiloFilterExpression("True") data class And(val children: List) : SiloFilterExpression("And") + +data class Not(val child: SiloFilterExpression) : SiloFilterExpression("Not") diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt index 100ef76f..21939199 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt @@ -1,6 +1,7 @@ package org.genspectrum.lapis.model import org.genspectrum.lapis.silo.And +import org.genspectrum.lapis.silo.Not import org.genspectrum.lapis.silo.NucleotideSymbolEquals import org.hamcrest.MatcherAssert import org.hamcrest.Matchers @@ -59,4 +60,14 @@ class VariantQueryFacadeTest { ) MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) } + + @Test + fun `given a variant variantQuery with a not expression the map should return the corresponding SiloQuery`() { + val variantQuery = "!300G" + + val result = underTest.map(variantQuery) + + val expectedResult = Not(NucleotideSymbolEquals(300, "G")) + MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) + } } diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt index 861896d1..f122c77d 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt @@ -188,6 +188,19 @@ class SiloQueryTest { } """, ), + Arguments.of( + Not(StringEquals("theColumn", "theValue")), + """ + { + "type": "Not", + "child": { + "type": "StringEquals", + "column": "theColumn", + "value": "theValue" + } + } + """, + ), ) } }