From 835b9c17a3dff528536e99fe2e0f5c5b4b3b8a26 Mon Sep 17 00:00:00 2001 From: Jonas Kellerer Date: Wed, 5 Apr 2023 16:57:57 +0200 Subject: [PATCH] feat: add Maybe and bracket expression --- .../model/variantqueryparser/VariantQuery.g4 | 2 + .../lapis/model/VariantQueryCustomListener.kt | 7 ++++ .../org/genspectrum/lapis/silo/SiloQuery.kt | 2 + .../lapis/model/VariantQueryFacadeTest.kt | 39 +++++++++++++++++-- .../genspectrum/lapis/silo/SiloQueryTest.kt | 13 +++++++ 5 files changed, 59 insertions(+), 4 deletions(-) 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 7363978e..074b17dc 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 @@ -8,6 +8,8 @@ expr: | '!' expr # Not | expr '&' expr # And | expr '|' expr # Or + | '(' expr ')' # Parentesis + | 'MAYBE(' expr ')' # Maybe ; single: 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 07c67033..6955f282 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt @@ -2,11 +2,13 @@ package org.genspectrum.lapis.model import VariantQueryBaseListener import VariantQueryParser.AndContext +import VariantQueryParser.MaybeContext import VariantQueryParser.NotContext import VariantQueryParser.Nucleotide_mutationContext import VariantQueryParser.OrContext import org.antlr.v4.runtime.tree.ParseTreeListener import org.genspectrum.lapis.silo.And +import org.genspectrum.lapis.silo.Maybe import org.genspectrum.lapis.silo.Not import org.genspectrum.lapis.silo.NucleotideSymbolEquals import org.genspectrum.lapis.silo.Or @@ -44,4 +46,9 @@ class VariantQueryCustomListener : VariantQueryBaseListener(), ParseTreeListener val children = listOf(expressionStack.removeLast(), expressionStack.removeLast()).reversed() expressionStack.addLast(Or(children)) } + + override fun exitMaybe(ctx: MaybeContext?) { + val child = expressionStack.removeLast() + expressionStack.addLast(Maybe(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 6a56430c..3400f5a7 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt @@ -44,3 +44,5 @@ data class And(val children: List) : SiloFilterExpression( data class Or(val children: List) : SiloFilterExpression("Or") data class Not(val child: SiloFilterExpression) : SiloFilterExpression("Not") + +data class Maybe(val child: SiloFilterExpression) : SiloFilterExpression("Maybe") 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 d8283e2e..a59ca265 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.Maybe import org.genspectrum.lapis.silo.Not import org.genspectrum.lapis.silo.NucleotideSymbolEquals import org.genspectrum.lapis.silo.Or @@ -28,7 +29,7 @@ class VariantQueryFacadeTest { } @Test - fun `given a variant variantQuery with an and expression the map should return the corresponding SiloQuery`() { + fun `given a variant variantQuery with an 'And' expression the map should return the corresponding SiloQuery`() { val variantQuery = "300G & 400" val result = underTest.map(variantQuery) @@ -43,7 +44,7 @@ class VariantQueryFacadeTest { } @Test - fun `given a variant variantQuery with two and expression the map should return the corresponding SiloQuery`() { + fun `given a variant variantQuery with two 'And' expression the map should return the corresponding SiloQuery`() { val variantQuery = "300G & 400- & 500B" val result = underTest.map(variantQuery) @@ -63,7 +64,7 @@ class VariantQueryFacadeTest { } @Test - fun `given a variant variantQuery with a not expression the map should return the corresponding SiloQuery`() { + fun `given a variant variantQuery with a 'Not' expression the map should return the corresponding SiloQuery`() { val variantQuery = "!300G" val result = underTest.map(variantQuery) @@ -73,7 +74,7 @@ class VariantQueryFacadeTest { } @Test - fun `given a variant variantQuery with an Or expression the map should return the corresponding SiloQuery`() { + fun `given a variant variantQuery with an 'Or' expression the map should return the corresponding SiloQuery`() { val variantQuery = "300G | 400" val result = underTest.map(variantQuery) @@ -86,4 +87,34 @@ class VariantQueryFacadeTest { ) MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) } + + @Test + fun `given a variant variantQuery with an bracket expression the map should return the corresponding SiloQuery`() { + val variantQuery = "300C & (400A | 500G)" + + val result = underTest.map(variantQuery) + + val expectedResult = And( + listOf( + NucleotideSymbolEquals(300, "C"), + Or( + listOf( + NucleotideSymbolEquals(400, "A"), + NucleotideSymbolEquals(500, "G"), + ), + ), + ), + ) + MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) + } + + @Test + fun `given a variant variantQuery with a 'Maybe' expression the map should return the corresponding SiloQuery`() { + val variantQuery = "MAYBE(300G)" + + val result = underTest.map(variantQuery) + + val expectedResult = Maybe(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 e0453def..9ff42bc7 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt @@ -221,6 +221,19 @@ class SiloQueryTest { } """, ), + Arguments.of( + Maybe(StringEquals("theColumn", "theValue")), + """ + { + "type": "Maybe", + "child": { + "type": "StringEquals", + "column": "theColumn", + "value": "theValue" + } + } + """, + ), ) } }