Created
January 29, 2019 05:52
-
-
Save fwgreen/cea743b579f37dd077082398860c2658 to your computer and use it in GitHub Desktop.
RegexRedux in Kotlin with coroutines
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
import kotlinx.coroutines.* | |
import java.io.File | |
fun main() = runBlocking { | |
val start = System.nanoTime() | |
val input = File("file_path.txt").readText(Charsets.UTF_8) | |
val sequence = ">.*\n|\n".toRegex().replace(input, "") | |
val replacements = listOf( | |
"tHa[Nt]" to "<4>", | |
"aND|caN|Ha[DS]|WaS" to "<3>", | |
"a[NSt]|BY" to "<2>", | |
"<[^>]*>" to "|", | |
"\\|[^|][^|]*\\|" to "-" | |
) | |
val reps = GlobalScope.launch { | |
replacements.fold(sequence) { | |
buffer, | |
(pattern, replacement) -> pattern.toRegex().replace(buffer, replacement) | |
}.let { | |
println(it.length) | |
} | |
} | |
val variants = listOf( | |
"agggtaaa|tttaccct", | |
"[cgt]gggtaaa|tttaccc[acg]", | |
"a[act]ggtaaa|tttacc[agt]t", | |
"ag[act]gtaaa|tttac[agt]ct", | |
"agg[act]taaa|ttta[agt]cct", | |
"aggg[acg]aaa|ttt[cgt]ccct", | |
"agggt[cgt]aa|tt[acg]accct", | |
"agggta[cgt]a|t[acg]taccct", | |
"agggtaa[cgt]|[acg]ttaccct" | |
) | |
val vars = GlobalScope.launch { | |
variants.map { | |
async { | |
it to it.toRegex().findAll(sequence).count() | |
} | |
} | |
.map { it.await() } | |
.forEach { (k, v) -> println("$k $v") } | |
println( | |
""" | |
${input.length} | |
${sequence.length} | |
""".trimIndent()) | |
} | |
vars.join() | |
reps.join() | |
println("elapsed: ${(System.nanoTime().toDouble() - start.toDouble()) / 1E9} secs") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment