<p>I've encountered this problem before, and used python <code>re</code> module to solve this problem.</p>

<pre><code>import re
all = re.findall(r'(?=(AA))','AAAA')
counts = len(all)
</code></pre>

<p>You can get more details in this <a href="https://stackoverflow.com/questions/11430863/how-to-find-overlapping-matches-with-a-regexp">thread</a></p>

CC BY-SA 3.0
2017-08-10T14:34:10.333Z
This is a valid approach, and you could add start and end arguments easily enough, but it's not very memory efficient for large sequences. `re.finditer` would be better for that. Also, `UnknownSeq` type objects require a rather different approach to maximize memory efficiency
Chris_Rands
CC BY-SA 3.0
2017-07-12T03:18:23.897Z
2017-07-12T05:17:53.587Z
2017-07-12T05:17:53.587Z
l0o0
<p>You can use <code>finditer</code> from python's <a href="https://docs.python.org/2/library/re.html#module-re" rel="nofollow noreferrer"><code>re</code></a> module. The advantage of this approach is it allows for getting the indices of those matches, which could be handy down the track.</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; matches = re.finditer(r'(?=(AA))', 'AAAA')
&gt;&gt;&gt; indices = [match.span(1) for match in matches]
&gt;&gt;&gt; indices
[(0, 2), (1, 3), (2, 4)]
&gt;&gt;&gt; num_matches = len(indices)
&gt;&gt;&gt; num_matches
3
</code></pre>

CC BY-SA 3.0
2017-08-14T08:53:07.083Z
2017-08-14T08:58:49.933Z
2017-08-14T08:58:49.933Z
Michael Hall
<p>For Biopython 1.70, there is a new <code>Seq.count_overlap()</code> method, which includes optional <code>start</code> and <code>end</code> arguments:</p>

<pre><code>&gt;&gt;&gt; from Bio.Seq import Seq
&gt;&gt;&gt; Seq('AAAA').count_overlap('AA')
3
&gt;&gt;&gt; Seq('AAAA').count_overlap('AA', 1, 4)
2
</code></pre>

<p>This method is also implemented for the <code>MutableSeq</code> and <code>UnknownSeq</code> classes:</p>

<pre><code>&gt;&gt;&gt; from Bio.Seq import MutableSeq, UnknownSeq
&gt;&gt;&gt; MutableSeq('AAAA').count_overlap('AA')
3
&gt;&gt;&gt; UnknownSeq(4, character='A').count_overlap('AA')
3
</code></pre>

<hr>

<p>Disclaimer: I co-contributed the <code>.count_overlap()</code> methods with Peter Cock, see <a href="https://github.com/biopython/biopython/commit/97709cc7a4b8591e794b442796d41e4f7b855a0f" rel="noreferrer"><code>97709cc</code></a></p>

CC BY-SA 3.0
2017-07-11T13:10:56.437Z
2017-07-11T13:10:56.437Z
Chris_Rands
<p>Biopython's <code>.count()</code> methods, like Python's <code>str.count()</code>, perform a non-overlapping count, how can I do an overlapping one?</p>

<p>For example, these code snippets <code>return 2</code>, but I want the answer <code>3</code>:</p>

<pre><code>&gt;&gt;&gt; from Bio.Seq import Seq
&gt;&gt;&gt; Seq('AAAA').count('AA')
2
&gt;&gt;&gt; 'AAAA'.count('AA')
2
</code></pre>

<python>
How can I do an overlapping sequence count in Biopython?
# How can I do an overlapping sequence count in Biopython?

(Asked by: username_0 on 2017-07-11)

Biopython's .count() methods, like Python's str.count(), perform a non-overlapping count, how can I do an overlapping one?
For example, these code snippets return 2, but I want the answer 3:
>>> from Bio.Seq import Seq
>>> Seq('AAAA').count('AA')
2
>>> 'AAAA'.count('AA')
2

**Tags:** python

---

## 3 answers

### Answer by username_0 on 2017-07-11. Score: 18 | Accepted answer

For Biopython 1.70, there is a new Seq.count_overlap() method, which includes optional start and end arguments:
>>> from Bio.Seq import Seq
>>> Seq('AAAA').count_overlap('AA')
3
>>> Seq('AAAA').count_overlap('AA', 1, 4)
2

This method is also implemented for the MutableSeq and UnknownSeq classes:
>>> from Bio.Seq import MutableSeq, UnknownSeq
>>> MutableSeq('AAAA').count_overlap('AA')
3
>>> UnknownSeq(4, character='A').count_overlap('AA')
3


Disclaimer: I co-contributed the .count_overlap() methods with Peter Cock, see 97709cc


---

### Answer by username_1 on 2017-07-12. Score: 9

I've encountered this problem before, and used python re module to solve this problem.
import re
all = re.findall(r'(?=(AA))','AAAA')
counts = len(all)

You can get more details in this thread

> **Comments:**
> **username_0**: This is a valid approach, and you could add start and end arguments easily enough, but it's not very memory efficient for large sequences. `re.finditer` would be better for that. Also, `UnknownSeq` type objects require a rather different approach to maximize memory efficiency  

---

### Answer by username_2 on 2017-08-14. Score: 4

You can use finditer from python's re module. The advantage of this approach is it allows for getting the indices of those matches, which could be handy down the track.
>>> import re
>>> matches = re.finditer(r'(?=(AA))', 'AAAA')
>>> indices = [match.span(1) for match in matches]
>>> indices
[(0, 2), (1, 3), (2, 4)]
>>> num_matches = len(indices)
>>> num_matches
3


---

