If you want to count the number of unique k-mers that occur in your data set, you should use the unique-kmers.py script, which implements a HyperLogLog-based cardinality estimator.
If the number of unique k-mers is not what you're after, please clarify.
EDIT:
khmer uses probabilistic data structures internally, which store k-mers as hashed values that cannot be un-hashed back into k-mer sequences uniquely. Querying a k-mer's abundance requires you to know which k-mer(s) you're looking for, which in this case would require a second pass over the reads.
The closest thing to what you're asking provided in khmer's command-line scripts is abundance-dist.py (or the alternative abundance-dist-single.py), which will produce a k-mer abundance histogram but not per-kmer abundance.
The load-into-counting.py scripts computes the k-mer abundances and stores them in a probabilistic data structure, which is written to disk and can subsequently be re-loaded into memory quickly. Many of the scripts in khmer expect the reads to have been pre-processed by load-into-counting.py, while others will invoke the counting routines directly themselve. It's also fairly easy to do this with the Python API. In every case, the accuracy of the k-mer counts is dependant on memory considerations.
If you must have the counts of each k-mer, it's not much more work...although reporting each k-mer's abundance only once would consume a lot of memory with a naive approach.
>>> outfile = open('outfile.txt', 'w')
>>> seenkmers = set() # Consumes a lot of memory for large input!!!
>>> for read in khmer.ReadParser('reads.fq.gz):
... for kmer in counts.get_kmers(read.sequence):
... if kmer not in seenkmers:
... print(kmer, counttable.get(kmer), file=outfile)
... seenkmers.add(kmer)
If you want to count the number of unique k-mers that occur in your data set, you should use the unique-kmers.py script, which implements a HyperLogLog-based cardinality estimator.
If the number of unique k-mers is not what you're after, please clarify.
EDIT:
khmer uses probabilistic data structures internally, which store k-mers as hashed values that cannot be un-hashed back into k-mer sequences uniquely. Querying a k-mer's abundance requires you to know which k-mer(s) you're looking for, which in this case would require a second pass over the reads.
The closest thing to what you're asking provided in khmer's command-line scripts is abundance-dist.py (or the alternative abundance-dist-single.py), which will produce a k-mer abundance histogram but not per-kmer abundance.
The load-into-counting.py scripts computes the k-mer abundances and stores them in a probabilistic data structure, which is written to disk and can subsequently be re-loaded into memory quickly. Many of the scripts in khmer expect the reads to have been pre-processed by load-into-counting.py, while others will invoke the counting routines directly themselve. It's also fairly easy to do this with the Python API. In every case, the accuracy of the k-mer counts is dependant on memory considerations.
If you must have the counts of each k-mer, it's not much more work...although reporting each k-mer's abundance only once would consume a lot of memory with a naive approach.
>>> outfile = open('outfile.txt', 'w')>>> seenkmers = set() # Consumes a lot of memory for large input!!!>>> for read in khmer.ReadParser('reads.fq.gz):... for kmer in counts.get_kmers(read.sequence):... if kmer not in seenkmers:... print(kmer, counttable.get(kmer), file=outfile)... seenkmers.add(kmer)
You can use standard Python calls to manipulate this dictionary object and get sums of counts per record, for sequence, etc. which seems to answer your question. Please feel free to clarify what you're looking for if this object representation is not clear.
$ git clone https://github.com/alexpreynolds/kmer-counter.git$ cd kmer-counter$ make$ cp kmer-counter /usr/local/bin
Once the binary is in your path, you might use it in Python like so:
k = 6fastaFile = '/path/to/some/seqs.fa'kmerCmd = 'kmer-counter --fasta --k=%d %s' % (k, fastaFile)try: output = subprocess.check_output(kmerCmd, shell=True) result = {} for line in output.splitlines(): (header, counts) = line.strip().split('\t') header = header[1:] kmers = dict((k,int(v)) for (k,v) in [d.split(':') for d in counts.split(' ')]) result[header] = kmers sys.stdout.write("%s" % (str(result)))except subprocess.CalledProcessError as error: sys.stderr.write("%s" % (str(error)))
You can use standard Python calls to manipulate this dictionary object and get sums of counts per record, for sequence, etc. which seems to answer your question. Please feel free to clarify what you're looking for if this object representation is not clear.
If you want to count the number of unique k-mers that occur in your data set, you should use the unique-kmers.py script, which implements a HyperLogLog-based cardinality estimator.
If the number of unique k-mers is not what you're after, please clarify.
EDIT:
khmer uses probabilistic data structures internally, which store k-mers as hashed values that cannot be un-hashed back into k-mer sequences uniquely. Querying a k-mer's abundance requires you to know which k-mer(s) you're looking for, which in this case would require a second pass over the reads.
The closest thing to what you're asking provided in khmer's command-line scripts is abundance-dist.py (or the alternative abundance-dist-single.py), which will produce a k-mer abundance histogram but not per-kmer abundance.
The load-into-counting.py scripts computes the k-mer abundances and stores them in a probabilistic data structure, which is written to disk and can subsequently be re-loaded into memory quickly. Many of the scripts in khmer expect the reads to have been pre-processed by
load-into-counting.py, while others will invoke the counting routines directly themselve. It's also fairly easy to do this with the Python API. In every case, the accuracy of the k-mer counts is dependant on memory considerations.To check the false positive rate:
If you must have the counts of each k-mer, it's not much more work...although reporting each k-mer's abundance only once would consume a lot of memory with a naive approach.
If you want to count the number of unique k-mers that occur in your data set, you should use the unique-kmers.py script, which implements a HyperLogLog-based cardinality estimator.
If the number of unique k-mers is not what you're after, please clarify.
EDIT:
khmer uses probabilistic data structures internally, which store k-mers as hashed values that cannot be un-hashed back into k-mer sequences uniquely. Querying a k-mer's abundance requires you to know which k-mer(s) you're looking for, which in this case would require a second pass over the reads.
The closest thing to what you're asking provided in khmer's command-line scripts is abundance-dist.py (or the alternative abundance-dist-single.py), which will produce a k-mer abundance histogram but not per-kmer abundance.
The load-into-counting.py scripts computes the k-mer abundances and stores them in a probabilistic data structure, which is written to disk and can subsequently be re-loaded into memory quickly. Many of the scripts in khmer expect the reads to have been pre-processed by
load-into-counting.py, while others will invoke the counting routines directly themselve. It's also fairly easy to do this with the Python API. In every case, the accuracy of the k-mer counts is dependant on memory considerations.To check the false positive rate:
If you must have the counts of each k-mer, it's not much more work...although reporting each k-mer's abundance only once would consume a lot of memory with a naive approach.
More
VOTE
VOTE
VOTE
VOTE
VOTE
I wrote a command-line k-mer counter called
kmer-counterthat will output results in a form that your Python script can consume: https://github.com/alexpreynolds/kmer-counterYou can grab, build and install it like so:
Once the binary is in your path, you might use it in Python like so:
Given example FASTA like this:
For k=6, you would get an iterable Python dictionary like this:
You can use standard Python calls to manipulate this dictionary object and get sums of counts per record, for sequence, etc. which seems to answer your question. Please feel free to clarify what you're looking for if this object representation is not clear.
For a fully-fleshed out demonstration, see: https://github.com/alexpreynolds/kmer-counter/blob/master/test/kmer-test.py
I wrote a command-line k-mer counter called
kmer-counterthat will output results in a form that your Python script can consume: https://github.com/alexpreynolds/kmer-counterYou can grab, build and install it like so:
Once the binary is in your path, you might use it in Python like so:
Given example FASTA like this:
For k=6, you would get an iterable Python dictionary like this:
You can use standard Python calls to manipulate this dictionary object and get sums of counts per record, for sequence, etc. which seems to answer your question. Please feel free to clarify what you're looking for if this object representation is not clear.
For a fully-fleshed out demonstration, see: https://github.com/alexpreynolds/kmer-counter/blob/master/test/kmer-test.py
More
VOTE