Skip to content

Commit 835b9c1

Browse files
author
Jonas Kellerer
committed
feat: add Maybe and bracket expression
1 parent 5a0ef38 commit 835b9c1

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

lapis2/src/main/antlr/org/genspectrum/lapis/model/variantqueryparser/VariantQuery.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ expr:
88
| '!' expr # Not
99
| expr '&' expr # And
1010
| expr '|' expr # Or
11+
| '(' expr ')' # Parentesis
12+
| 'MAYBE(' expr ')' # Maybe
1113
;
1214

1315
single:

lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package org.genspectrum.lapis.model
22

33
import VariantQueryBaseListener
44
import VariantQueryParser.AndContext
5+
import VariantQueryParser.MaybeContext
56
import VariantQueryParser.NotContext
67
import VariantQueryParser.Nucleotide_mutationContext
78
import VariantQueryParser.OrContext
89
import org.antlr.v4.runtime.tree.ParseTreeListener
910
import org.genspectrum.lapis.silo.And
11+
import org.genspectrum.lapis.silo.Maybe
1012
import org.genspectrum.lapis.silo.Not
1113
import org.genspectrum.lapis.silo.NucleotideSymbolEquals
1214
import org.genspectrum.lapis.silo.Or
@@ -44,4 +46,9 @@ class VariantQueryCustomListener : VariantQueryBaseListener(), ParseTreeListener
4446
val children = listOf(expressionStack.removeLast(), expressionStack.removeLast()).reversed()
4547
expressionStack.addLast(Or(children))
4648
}
49+
50+
override fun exitMaybe(ctx: MaybeContext?) {
51+
val child = expressionStack.removeLast()
52+
expressionStack.addLast(Maybe(child))
53+
}
4754
}

lapis2/src/main/kotlin/org/genspectrum/lapis/silo/SiloQuery.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ data class And(val children: List<SiloFilterExpression>) : SiloFilterExpression(
4444
data class Or(val children: List<SiloFilterExpression>) : SiloFilterExpression("Or")
4545

4646
data class Not(val child: SiloFilterExpression) : SiloFilterExpression("Not")
47+
48+
data class Maybe(val child: SiloFilterExpression) : SiloFilterExpression("Maybe")

lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.genspectrum.lapis.model
22

33
import org.genspectrum.lapis.silo.And
4+
import org.genspectrum.lapis.silo.Maybe
45
import org.genspectrum.lapis.silo.Not
56
import org.genspectrum.lapis.silo.NucleotideSymbolEquals
67
import org.genspectrum.lapis.silo.Or
@@ -28,7 +29,7 @@ class VariantQueryFacadeTest {
2829
}
2930

3031
@Test
31-
fun `given a variant variantQuery with an and expression the map should return the corresponding SiloQuery`() {
32+
fun `given a variant variantQuery with an 'And' expression the map should return the corresponding SiloQuery`() {
3233
val variantQuery = "300G & 400"
3334

3435
val result = underTest.map(variantQuery)
@@ -43,7 +44,7 @@ class VariantQueryFacadeTest {
4344
}
4445

4546
@Test
46-
fun `given a variant variantQuery with two and expression the map should return the corresponding SiloQuery`() {
47+
fun `given a variant variantQuery with two 'And' expression the map should return the corresponding SiloQuery`() {
4748
val variantQuery = "300G & 400- & 500B"
4849

4950
val result = underTest.map(variantQuery)
@@ -63,7 +64,7 @@ class VariantQueryFacadeTest {
6364
}
6465

6566
@Test
66-
fun `given a variant variantQuery with a not expression the map should return the corresponding SiloQuery`() {
67+
fun `given a variant variantQuery with a 'Not' expression the map should return the corresponding SiloQuery`() {
6768
val variantQuery = "!300G"
6869

6970
val result = underTest.map(variantQuery)
@@ -73,7 +74,7 @@ class VariantQueryFacadeTest {
7374
}
7475

7576
@Test
76-
fun `given a variant variantQuery with an Or expression the map should return the corresponding SiloQuery`() {
77+
fun `given a variant variantQuery with an 'Or' expression the map should return the corresponding SiloQuery`() {
7778
val variantQuery = "300G | 400"
7879

7980
val result = underTest.map(variantQuery)
@@ -86,4 +87,34 @@ class VariantQueryFacadeTest {
8687
)
8788
MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult))
8889
}
90+
91+
@Test
92+
fun `given a variant variantQuery with an bracket expression the map should return the corresponding SiloQuery`() {
93+
val variantQuery = "300C & (400A | 500G)"
94+
95+
val result = underTest.map(variantQuery)
96+
97+
val expectedResult = And(
98+
listOf(
99+
NucleotideSymbolEquals(300, "C"),
100+
Or(
101+
listOf(
102+
NucleotideSymbolEquals(400, "A"),
103+
NucleotideSymbolEquals(500, "G"),
104+
),
105+
),
106+
),
107+
)
108+
MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult))
109+
}
110+
111+
@Test
112+
fun `given a variant variantQuery with a 'Maybe' expression the map should return the corresponding SiloQuery`() {
113+
val variantQuery = "MAYBE(300G)"
114+
115+
val result = underTest.map(variantQuery)
116+
117+
val expectedResult = Maybe(NucleotideSymbolEquals(300, "G"))
118+
MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult))
119+
}
89120
}

lapis2/src/test/kotlin/org/genspectrum/lapis/silo/SiloQueryTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ class SiloQueryTest {
221221
}
222222
""",
223223
),
224+
Arguments.of(
225+
Maybe(StringEquals("theColumn", "theValue")),
226+
"""
227+
{
228+
"type": "Maybe",
229+
"child": {
230+
"type": "StringEquals",
231+
"column": "theColumn",
232+
"value": "theValue"
233+
}
234+
}
235+
""",
236+
),
224237
)
225238
}
226239
}

0 commit comments

Comments
 (0)