New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added mechanisms for retrieving tags by position within a sequence #749
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1326,6 +1326,91 @@ static PyObject * hash_consume_and_tag(PyObject * self, PyObject * args) | |
return Py_BuildValue("K", n_consumed); | ||
} | ||
|
||
static PyObject * hash_get_tags_and_positions(PyObject * self, PyObject * args) | ||
{ | ||
khmer_KCountingHashObject * me = (khmer_KCountingHashObject *) self; | ||
CountingHash * counting = me->counting; | ||
|
||
const char * seq; | ||
|
||
if (!PyArg_ParseTuple(args, "s", &seq)) { | ||
return NULL; | ||
} | ||
|
||
// call the C++ function, and trap signals => Python | ||
|
||
std::vector<unsigned int> posns; | ||
std::vector<HashIntoType> tags; | ||
try { | ||
unsigned int pos = 1; | ||
KMerIterator kmers(seq, counting->ksize()); | ||
|
||
while (!kmers.done()) { | ||
HashIntoType kmer = kmers.next(); | ||
if (set_contains(counting->all_tags, kmer)) { | ||
posns.push_back(pos); | ||
tags.push_back(kmer); | ||
} | ||
pos++; | ||
} | ||
} catch (_khmer_signal &e) { | ||
PyErr_SetString(PyExc_ValueError, e.get_message().c_str()); | ||
return NULL; | ||
} | ||
|
||
PyObject * posns_list = PyList_New(posns.size()); | ||
for (size_t i = 0; i < posns.size(); i++) { | ||
PyObject * tup = Py_BuildValue("IK", posns[i], tags[i]); | ||
PyList_SET_ITEM(posns_list, i, tup); | ||
} | ||
|
||
return posns_list; | ||
} | ||
|
||
static PyObject * hash_find_all_tags_list(PyObject * self, PyObject *args) | ||
{ | ||
khmer_KCountingHashObject * me = (khmer_KCountingHashObject *) self; | ||
CountingHash * counting = me->counting; | ||
|
||
const char * kmer_s = NULL; | ||
|
||
if (!PyArg_ParseTuple(args, "s", &kmer_s)) { | ||
return NULL; | ||
} | ||
|
||
if (strlen(kmer_s) < counting->ksize()) { | ||
PyErr_SetString( PyExc_ValueError, | ||
"starting kmer is smaller than the K size of the counting table"); | ||
return NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code branch is untested |
||
} | ||
|
||
SeenSet tags; | ||
|
||
Py_BEGIN_ALLOW_THREADS | ||
|
||
HashIntoType kmer, kmer_f, kmer_r; | ||
kmer = _hash(kmer_s, counting->ksize(), kmer_f, kmer_r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GCC complained about kmer being defined but not used in this function, do you see it being used in the future? If not maybe just ignore the return value and remove it from the previous line? |
||
|
||
counting->partition->find_all_tags(kmer_f, kmer_r, tags, | ||
counting->all_tags); | ||
|
||
Py_END_ALLOW_THREADS | ||
|
||
PyObject * x = PyList_New(tags.size()); | ||
if (x == NULL) { | ||
return NULL; | ||
} | ||
SeenSet::iterator si; | ||
unsigned long long i = 0; | ||
for (si = tags.begin(); si != tags.end(); ++si) { | ||
// type K for python unsigned long long | ||
PyList_SET_ITEM(x, i, Py_BuildValue("K", *si)); | ||
i++; | ||
} | ||
|
||
return x; | ||
} | ||
|
||
static PyObject * hash_consume_fasta_and_tag(PyObject * self, PyObject * args) | ||
{ | ||
khmer_KCountingHashObject * me = (khmer_KCountingHashObject *) self; | ||
|
@@ -1485,6 +1570,8 @@ static PyMethodDef khmer_counting_methods[] = { | |
METH_VARARGS, "" | ||
}, | ||
{ "consume_and_tag", hash_consume_and_tag, METH_VARARGS, "Consume a sequence and tag it" }, | ||
{ "get_tags_and_positions", hash_get_tags_and_positions, METH_VARARGS, "Retrieve tags and their positions in a sequence." }, | ||
{ "find_all_tags_list", hash_find_all_tags_list, METH_VARARGS, "Find all tags within range of the given k-mer, return as list" }, | ||
{ "consume_fasta_and_tag", hash_consume_fasta_and_tag, METH_VARARGS, "Count all k-mers in a given file" }, | ||
{ "do_subset_partition_with_abundance", hash_do_subset_partition_with_abundance, METH_VARARGS, "" }, | ||
{ "find_all_tags_truncate_on_abundance", hash_find_all_tags_truncate_on_abundance, METH_VARARGS, "" }, | ||
|
@@ -2479,7 +2566,7 @@ static PyObject * hashbits_find_all_tags(PyObject * self, PyObject *args) | |
return NULL; | ||
} | ||
|
||
if (strlen(kmer_s) < hashbits->ksize()) { // @@ | ||
if (strlen(kmer_s) < hashbits->ksize()) { | ||
PyErr_SetString( PyExc_ValueError, | ||
"starting kmer is smaller than the K size of the hashbits"); | ||
return NULL; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1356-1357 lack testing coverage and won't be called ever anyhow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
On Tue, Feb 10, 2015 at 11:16:55AM -0800, Michael R. Crusoe wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks; making new pull request