Skip to content

Commit

Permalink
feat: add Maybe and bracket expression
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKellerer committed Apr 27, 2023
1 parent 5a0ef38 commit 835b9c1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ expr:
| '!' expr # Not
| expr '&' expr # And
| expr '|' expr # Or
| '(' expr ')' # Parentesis
| 'MAYBE(' expr ')' # Maybe
;

single:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ data class And(val children: List<SiloFilterExpression>) : SiloFilterExpression(
data class Or(val children: List<SiloFilterExpression>) : SiloFilterExpression("Or")

data class Not(val child: SiloFilterExpression) : SiloFilterExpression("Not")

data class Maybe(val child: SiloFilterExpression) : SiloFilterExpression("Maybe")
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
}
}
13 changes: 13 additions & 0 deletions lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ class SiloQueryTest {
}
""",
),
Arguments.of(
Maybe(StringEquals("theColumn", "theValue")),
"""
{
"type": "Maybe",
"child": {
"type": "StringEquals",
"column": "theColumn",
"value": "theValue"
}
}
""",
),
)
}
}

0 comments on commit 835b9c1

Please sign in to comment.