<p>There's a lot detail missing here, but at a guess could you try,</p>
<pre><code>from Bio import SeqIO
from Bio.Seq import Seq
for record in SeqIO.parse(&quot;ls_orchid.gbk&quot;, &quot;genbank&quot;):
    reversec = record.seq.reverse_complement())
    revcount = reversec.count_overlap('AT')
    print (revcount + count_overlap('AT'))
</code></pre>
<p>Essentially <code>count_overlap</code> only counts the forward strand if this is the Biopython method. Not debugged the above.</p>

CC BY-SA 4.0
2020-11-21T10:08:44.393Z
2020-11-21T10:19:32.927Z
2020-11-21T10:19:32.927Z
M__
<p><a href="https://github.com/alexpreynolds/kmer-counter" rel="nofollow noreferrer">Here's a C++ kmer counter</a> I wrote for my own purposes. It can count canonical and reverse complement kmers from FASTA files up to length 31. Run <code>kmer-counter --help</code> or read the README for count options.</p>
<p>For longer kmers, you'd want to look at an approximation tool like <a href="https://github.com/gmarcais/Jellyfish" rel="nofollow noreferrer">Jellyfish</a>.</p>

CC BY-SA 4.0
2020-12-06T21:32:35.623Z
2020-12-06T21:32:35.623Z
Alex Reynolds
<p>I would NOT recommend using biopython for a task like this. First, it's slow, but also it's really not necessary. There are several other existing tools optimised for exactly this purpose.</p>
<p>My favourite is <a href="https://github.com/refresh-bio/KMC" rel="nofollow noreferrer">KMC</a>. It's very fast, and by default, it reports canonical kmers (Parameter <code>-b</code> would turn this off). So, assuming you have KMC installed, this is how you get all the kmers from a range of occurances (1 - 10000).</p>
<pre><code># just making a directory for temprary files
mkdir tmp

# building a kmer database with k = 21, 
# 16 cores and 
# up to 64GB of memory. 
# Counting occurances between 1 to 10000, 
# `-fa` specifies that the input is fasta, you can also input other formats
# the database name will be kmer_counts
# &amp; saving temporary data to tmp directory; 
kmc -k21 -t16 -m64 -ci1 -cs10000 -fa my_seq.fasta kmer_counts tmp # run kmc

# extract kmers from the database in a text file
kmc_dump -ci1 -cx10000 kmer_counts kmer_k21.dump
</code></pre>

CC BY-SA 4.0
2020-12-02T07:25:09.457Z
I am lost here. Please help! I am using python 3. One vaex dataframe has 1million kmers and I want to check the count of these kmers in the FASTA file. How will I use KMC for this?
Lipsa
CC BY-SA 4.0
2020-12-02T10:08:34.430Z
Yes, KMC can count kmer coverages directly from a fasta file. Just follow the code I posted there on how to use KMC to get kmers and their corresponding coverages.
Kamil S Jaron
CC BY-SA 4.0
2020-11-23T01:00:22.850Z
2020-11-23T01:00:22.850Z
Kamil S Jaron
<p>I am using count_overlap() for counting the kmers from Biopython .</p>
<p>Does it take the reverse complement of kmer into account?</p>
<p>I need to count reverse complement too</p>

CC BY-SA 4.0
2020-11-21T09:52:09.467Z
Hi @Lipsa, please provide a test example and your output together with any possible error messages. This appears certainly solvable. Is this method in Biopython?
M__
CC BY-SA 4.0
2020-11-21T10:12:11.053Z
I've had a guess at what I think the question should read
M__
CC BY-SA 4.0
2020-12-02T07:27:04.440Z
Thank you for your response Michael . However , this approach is getting very very slow when FASTA is of very large size and kmers are in millions. Any help is appreciated.
Lipsa
<python><covid-19><sars-cov-2><k-mer><seqio>
How to count the kmer occurrence in FASTA file considering overlapping and reverse complement?
# How to count the kmer occurrence in FASTA file considering overlapping and reverse complement?

(Asked by: username_0 on 2020-11-21)

I am using count\_overlap() for counting the kmers from Biopython .

Does it take the reverse complement of kmer into account?

I need to count reverse complement too

**Tags:** python, covid-19, sars-cov-2, k-mer, seqio
> **Comments:**
> **username_1**: Hi @Lipsa, please provide a test example and your output together with any possible error messages. This appears certainly solvable. Is this method in Biopython?  
> **username_1**: I've had a guess at what I think the question should read  
> **username_0**: Thank you for your response Michael . However , this approach is getting very very slow when FASTA is of very large size and kmers are in millions. Any help is appreciated.  

---

## 3 answers

### Answer by username_1 on 2020-11-21. Score: 1

There's a lot detail missing here, but at a guess could you try,
from Bio import SeqIO
from Bio.Seq import Seq
for record in SeqIO.parse("ls_orchid.gbk", "genbank"):
    reversec = record.seq.reverse_complement())
    revcount = reversec.count_overlap('AT')
    print (revcount + count_overlap('AT'))

Essentially count_overlap only counts the forward strand if this is the Biopython method. Not debugged the above.


---

### Answer by username_2 on 2020-12-06. Score: 1

[Here's a C++ kmer counter](https://github.com/alexpreynolds/kmer-counter) I wrote for my own purposes. It can count canonical and reverse complement kmers from FASTA files up to length 31. Run `kmer-counter --help` or read the README for count options.

For longer kmers, you'd want to look at an approximation tool like [Jellyfish](https://github.com/gmarcais/Jellyfish).


---

### Answer by username_3 on 2020-11-23. Score: 0

I would NOT recommend using biopython for a task like this. First, it's slow, but also it's really not necessary. There are several other existing tools optimised for exactly this purpose.
My favourite is KMC. It's very fast, and by default, it reports canonical kmers (Parameter -b would turn this off). So, assuming you have KMC installed, this is how you get all the kmers from a range of occurances (1 - 10000).
# just making a directory for temprary files
mkdir tmp

# building a kmer database with k = 21, 
# 16 cores and 
# up to 64GB of memory. 
# Counting occurances between 1 to 10000, 
# `-fa` specifies that the input is fasta, you can also input other formats
# the database name will be kmer_counts
# & saving temporary data to tmp directory; 
kmc -k21 -t16 -m64 -ci1 -cs10000 -fa my_seq.fasta kmer_counts tmp # run kmc

# extract kmers from the database in a text file
kmc_dump -ci1 -cx10000 kmer_counts kmer_k21.dump

> **Comments:**
> **username_0**: I am lost here. Please help! I am using python 3. One vaex dataframe has 1million kmers and I want to check the count of these kmers in the FASTA file. How will I use KMC for this?  
> **username_3**: Yes, KMC can count kmer coverages directly from a fasta file. Just follow the code I posted there on how to use KMC to get kmers and their corresponding coverages.  

---

