From 5a0ef385fb64198fff655a465c3de9bebfb6b3f4 Mon Sep 17 00:00:00 2001 From: Jonas Kellerer Date: Wed, 5 Apr 2023 12:45:07 +0200 Subject: [PATCH] feat: add Or expression to variantQuery --- .../model/variantqueryparser/VariantQuery.g4 | 1 + .../lapis/model/VariantQueryCustomListener.kt | 7 +++++++ .../org/genspectrum/lapis/silo/SiloQuery.kt | 2 ++ .../lapis/model/VariantQueryFacadeTest.kt | 16 +++++++++++++++ .../genspectrum/lapis/silo/SiloQueryTest.kt | 20 +++++++++++++++++++ 5 files changed, 46 insertions(+) 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 a550c9a9..7363978e 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 @@ -7,6 +7,7 @@ expr: single # Uni | '!' expr # Not | expr '&' expr # And + | expr '|' expr # Or ; 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 2c7d205b..07c67033 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt @@ -4,10 +4,12 @@ import VariantQueryBaseListener import VariantQueryParser.AndContext 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.Not import org.genspectrum.lapis.silo.NucleotideSymbolEquals +import org.genspectrum.lapis.silo.Or import org.genspectrum.lapis.silo.SiloFilterExpression class VariantQueryCustomListener : VariantQueryBaseListener(), ParseTreeListener { @@ -37,4 +39,9 @@ class VariantQueryCustomListener : VariantQueryBaseListener(), ParseTreeListener val child = expressionStack.removeLast() expressionStack.addLast(Not(child)) } + + override fun exitOr(ctx: OrContext?) { + val children = listOf(expressionStack.removeLast(), expressionStack.removeLast()).reversed() + expressionStack.addLast(Or(children)) + } } 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 18423bf6..6a56430c 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt @@ -41,4 +41,6 @@ object True : SiloFilterExpression("True") data class And(val children: List) : SiloFilterExpression("And") +data class Or(val children: List) : SiloFilterExpression("Or") + 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 21939199..d8283e2e 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt @@ -3,6 +3,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.genspectrum.lapis.silo.Or import org.hamcrest.MatcherAssert import org.hamcrest.Matchers import org.junit.jupiter.api.BeforeEach @@ -70,4 +71,19 @@ class VariantQueryFacadeTest { val expectedResult = Not(NucleotideSymbolEquals(300, "G")) MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) } + + @Test + 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) + + val expectedResult = Or( + listOf( + NucleotideSymbolEquals(300, "G"), + NucleotideSymbolEquals(400, "-"), + ), + ) + 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 f122c77d..e0453def 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt @@ -201,6 +201,26 @@ class SiloQueryTest { } """, ), + Arguments.of( + Or(listOf(StringEquals("theColumn", "theValue"), StringEquals("theOtherColumn", "theOtherValue"))), + """ + { + "type": "Or", + "children": [ + { + "type": "StringEquals", + "column": "theColumn", + "value": "theValue" + }, + { + "type": "StringEquals", + "column": "theOtherColumn", + "value": "theOtherValue" + } + ] + } + """, + ), ) } }