# Bad performance: using structs to find substrings

**URL:** https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177
**Category:** New to Julia
**Tags:** performance
**Created:** [January 7, 2022, 8:18am UTC](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177 "2022-01-07T08:18:48Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [January 7, 2022, 7:49pm UTC](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177/21 "2022-01-07T19:49:47Z")

</div>

There is not, and unfortunately all these pieces of software are executables and expose no libraries (as is typical of bioinformatics, everything is done through the shell). However, `bwa` and `samtools` standardized the [BAM file format](https://samtools.github.io/hts-specs/SAMv1.pdf), which is parsed by BioJulia’s XAM.jl. So your best bet is to shell out to these executables, then parse their output.

---

<div class="post-metadata">

### Author: ![candidaorelmex](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@candidaorelmex](https://discourse.julialang.org/u/candidaorelmex)
#### Post date: [January 10, 2022, 8:10am UTC](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177/22 "2022-01-10T08:10:11Z")

</div>

In case anyone is interested: my most efficient solution (old code would have taken ~50min, new solution does it in 90s - 10000 short strings, 17000 long strings) is the following:

1. throw all short strings into a regex seperate by “|”  
(I couldnt fit more than 3000 short strings into one regex, so you creat e.g. 3 regex for 8000 short strings. I assume this is a bug, but it is cirmuventable)
2. use `findall.(regex, long_string[:])` where regex is constant and long\_string[:] an array with all long strings you’d like to have checked.

I would have like to turn all strings into BioSequences with BioSequences.jl, but I didnt manage to find the equivalent of `Regex()` for biore which I’d need since all my sequences are saved as variables. I also don’t know if `findall` is compatible with `biore`s as it is with “normal” regular expressions

---

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [January 11, 2022, 3:24pm UTC](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177/23 "2022-01-11T15:24:56Z")

</div>

Findall is indeed not implemented for biosequences (though a PR implementing it was created yesterday).

---

<div class="post-metadata">

### Author: ![woolstar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woolstar/32/19992_2.png) [@woolstar](https://discourse.julialang.org/u/woolstar)
#### Post date: [January 14, 2022, 11:08pm UTC](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177/24 "2022-01-14T23:08:06Z")

</div>

Even standard command line tools for matching strings, like `grep` suffer from this. Standard `grep` operates on unicode under most conditions, but if you restrict it to ASCII only via:

```
LANG=C grep ...

```

You can see significant speedups.

---

<div class="post-metadata">

### Author: ![amarcotte59](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@amarcotte59](https://discourse.julialang.org/u/amarcotte59)
#### Post date: [January 17, 2022, 10:51pm UTC](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177/25 "2022-01-17T22:51:44Z")

</div>

If I had to write such a piece of code, I would program building a tree or a state machine from the short string list identifying the end of a short string then travel through it with the long strings; probably what the aforementioned projects do. This is to try to get the most efficiency; grep might be sufficient depending on amount of data and turnaround time.

[Previous page](https://discourse.julialang.org/t/bad-performance-using-structs-to-find-substrings/74177.md?page=1)
