Skip to content

Instantly share code, notes, and snippets.

@fwgreen
Created January 29, 2019 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwgreen/cea743b579f37dd077082398860c2658 to your computer and use it in GitHub Desktop.
Save fwgreen/cea743b579f37dd077082398860c2658 to your computer and use it in GitHub Desktop.
RegexRedux in Kotlin with coroutines
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