-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77e97bc
commit 3a7a7a2
Showing
9 changed files
with
254 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
lapis2/src/main/antlr/org/genspectrum/lapis/model/variantqueryparser/VariantQuery.g4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
grammar VariantQuery; | ||
|
||
// parser rules | ||
|
||
start: expr EOF; | ||
expr: | ||
single # Uni | ||
| expr '&' expr # And | ||
; | ||
|
||
single: | ||
nucleotide_mutation | ||
; | ||
|
||
nucleotide_mutation : nucleotide_symbol? position ambigous_nucleotide_symbol?; | ||
|
||
position: NUMBER+; | ||
nucleotide_symbol: A | C | G | T; | ||
ambigous_nucleotide_symbol: nucleotide_symbol | M | R | W | S | Y | K | V | H | D | B | N | MINUS | DOT; | ||
|
||
// lexer rules | ||
|
||
A: 'A'; | ||
B: 'B'; | ||
C: 'C'; | ||
D: 'D'; | ||
E: 'E'; | ||
F: 'F'; | ||
G: 'G'; | ||
H: 'H'; | ||
I: 'I'; | ||
J: 'J'; | ||
K: 'K'; | ||
L: 'L'; | ||
M: 'M'; | ||
N: 'N'; | ||
O: 'O'; | ||
P: 'P'; | ||
Q: 'Q'; | ||
R: 'R'; | ||
S: 'S'; | ||
T: 'T'; | ||
U: 'U'; | ||
V: 'V'; | ||
W: 'W'; | ||
X: 'X'; | ||
Y: 'Y'; | ||
Z: 'Z'; | ||
MINUS: '-'; | ||
DOT: '.'; | ||
|
||
NUMBER: [0-9]; | ||
WHITESPACE: [ \r\n\t] -> skip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryCustomListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.genspectrum.lapis.model | ||
|
||
import VariantQueryBaseListener | ||
import VariantQueryParser.AndContext | ||
import VariantQueryParser.Nucleotide_mutationContext | ||
import org.antlr.v4.runtime.tree.ParseTreeListener | ||
import org.genspectrum.lapis.silo.And | ||
import org.genspectrum.lapis.silo.NucleotideSymbolEquals | ||
import org.genspectrum.lapis.silo.SiloFilterExpression | ||
|
||
class VariantQueryCustomListener : VariantQueryBaseListener(), ParseTreeListener { | ||
private var expressionStack = ArrayDeque<SiloFilterExpression>() | ||
|
||
fun getExpr(): SiloFilterExpression { | ||
return expressionStack.first() | ||
} | ||
|
||
override fun enterNucleotide_mutation(ctx: Nucleotide_mutationContext?) { | ||
if (ctx == null) { | ||
return | ||
} | ||
val position = ctx.position().text.toInt() | ||
val secondSymbol = if (ctx.ambigous_nucleotide_symbol() != null) ctx.ambigous_nucleotide_symbol().text else "-" | ||
|
||
val expr = NucleotideSymbolEquals(position, secondSymbol) | ||
expressionStack.addLast(expr) | ||
} | ||
|
||
override fun exitAnd(ctx: AndContext?) { | ||
val children = listOf(expressionStack.removeLast(), expressionStack.removeLast()).reversed() | ||
expressionStack.addLast(And(children)) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
lapis2/src/main/kotlin/org/genspectrum/lapis/model/VariantQueryFacade.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.genspectrum.lapis.model | ||
|
||
import VariantQueryLexer | ||
import VariantQueryParser | ||
import org.antlr.v4.runtime.CharStreams | ||
import org.antlr.v4.runtime.CommonTokenStream | ||
import org.antlr.v4.runtime.tree.ParseTreeWalker | ||
import org.genspectrum.lapis.silo.SiloFilterExpression | ||
|
||
class VariantQueryFacade { | ||
|
||
fun map(variantQuery: String): SiloFilterExpression { | ||
val lexer = VariantQueryLexer(CharStreams.fromString(variantQuery)) | ||
val tokens = CommonTokenStream(lexer) | ||
val parser = VariantQueryParser(tokens) | ||
val listener = VariantQueryCustomListener() | ||
|
||
val walker = ParseTreeWalker() | ||
walker.walk(listener, parser.start()) | ||
|
||
return listener.getExpr() | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
lapis2/src/test/kotlin/org/genspectrum/lapis/model/VariantQueryFacadeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.genspectrum.lapis.model | ||
|
||
import org.genspectrum.lapis.silo.And | ||
import org.genspectrum.lapis.silo.NucleotideSymbolEquals | ||
import org.hamcrest.MatcherAssert | ||
import org.hamcrest.Matchers | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class VariantQueryFacadeTest { | ||
private lateinit var underTest: VariantQueryFacade | ||
|
||
@BeforeEach | ||
fun setup() { | ||
underTest = VariantQueryFacade() | ||
} | ||
|
||
@Test | ||
fun `given a variant query with a single entry then map should return the corresponding SiloQuery`() { | ||
val variantQuery = "300G" | ||
|
||
val result = underTest.map(variantQuery) | ||
|
||
val expectedResult = NucleotideSymbolEquals(300, "G") | ||
MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) | ||
} | ||
|
||
@Test | ||
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) | ||
|
||
val expectedResult = And( | ||
listOf( | ||
NucleotideSymbolEquals(300, "G"), | ||
NucleotideSymbolEquals(400, "-"), | ||
), | ||
) | ||
MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) | ||
} | ||
|
||
@Test | ||
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) | ||
|
||
val expectedResult = And( | ||
listOf( | ||
And( | ||
listOf( | ||
NucleotideSymbolEquals(300, "G"), | ||
NucleotideSymbolEquals(400, "-"), | ||
), | ||
), | ||
NucleotideSymbolEquals(500, "B"), | ||
), | ||
) | ||
MatcherAssert.assertThat(result, Matchers.equalTo(expectedResult)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters