Repository navigation
Closed
Description
I observe a behavior of function get_approximate() in Bio.trie that seems strange and maybe unwanted. However, the function is not really documented, so I'm not sure.
Here is my session:
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from Bio import trie
In [2]: t = trie.trie()
In [3]: t['ABB'] = None
In [4]: t['BAB'] = None
In [5]: t.get_approximate('ABA', 5)
Out[5]:
[('ABB', None, 1),
('ABB', None, 2),
('ABB', None, 2),
('BAB', None, 3),
('BAB', None, 4),
('BAB', None, 4),
('BAB', None, 3),
('BAB', None, 3),
('BAB', None, 2),
('BAB', None, 2)]
In [6]: import Bio
In [7]: Bio.__version__
Out[7]: '1.65'And here is a docstring for this function, the only documentation I found:
In [11]: t.get_approximate?
Docstring: D.get_approximate(key, k) -> List of (key, value, mismatches) in D, allowing up to k mismatches in key.
Type: builtin_function_or_methodQuestions/comments:
- Why is there redundant output in the list, i.e., multiple copies of exactly the same tuple such as ('ABB', None, 2) ?
- Why are there different values output for the same sequence? We observe both ('ABB', None, 1) and ('ABB', None, 2), while one would expect that only the tuple with minimum possible value would be output?
- Why is ('BAB', None, 2) is a valid output? One could interpret the docstring so that the output will contain Hamming distance. However, 'BAB' is at Hamming distance 3 from 'ABA'. Does the function actually aim to compute edit distance? Or it's Hamming distance not correctly implemented?
- Maybe irrelevant question in light of the first one, but why is the maximum value along with 'BAB' is 4, while the maximum value along with 'ABB' is 2? Both seem strange considering the input had 5.
Thanks,
Yuri
Metadata
Metadata
Assignees
Labels
No labels
Activity
peterjc commentedon Oct 13, 2015
On the bright side, I can reproduce this:
Unfortunately I don't know what this function is meant to do.
Bio.triedoes not have an active maintainer. The original author of this code (and Biopython co-founder) was Jeff Chang, who has not been active within Biopython for many years.pritykin commentedon Oct 20, 2015
I read the code implementing the function, namely, mutually recursive functions _get_approximate_transition and _get_approximate_trie in biopython/Bio/trie.c. I also experimented with the function on 1000 random sequences: added them all to a trie, computed distances between them using get_approximate() and then verified using stringDist in R Bioconductor package Biostrings. My conclusion is that get_approximate() actually implements edit distance exactly and accurately, if one considers the output with the smallest distance value (last element of a triple) for a string (first element of a triple). In my example above, having ('BAB', None, 2) in the output means edit distance between 'ABA' and 'BAB' is 2. The output is indeed redundant (many copies of the same triple and copies varying just by the distance value at the last position), but this comes from the way the function is implemented in C. With this caveat in mind, this implementation of a trie is very efficient, including get_approximate() function. We tested it on millions of sequences. Maybe some details can be added to a docstring for get_approximate(), or maybe simply this thread will do.
peterjc commentedon Oct 20, 2015
Thanks for your research @pritykin - a comment to help document the function seems ideal. You can type something here or submit a pull request on GitHub if you prefer.
pritykin commentedon Jan 5, 2016
By now I've experimented with this code a lot, and further confirm my conclusion above. For example, I suggest that line 443 in triemodule.c , which is doc line for get_approximate(), is changed to something like this:
peterjc commentedon Jan 19, 2016
@mdehoon - do we need to worry about the argument names in this C code?
i.e. Changing the docstring signature from
D.get_approximate(key, k)toD.get_approximate(key, ed)or something more explicit likemax_edit_distance?mdehoon commentedon Jan 22, 2016
@peterjc I don't have any particular preference. I have written some bug fixes for this code in the past, but otherwise I don't know much about it.
mdehoon commentedon Apr 11, 2018
@pritykin Would you be able to use pygtrie as a replacement for Bio.trie? On another thread (issue #912 ) we have been discussing to deprecate and then remove Bio.trie from Biopython.
pritykin commentedon Apr 11, 2018
We published a paper about a tool where we used Bio.trie as a critical part: tool , paper
However, we adopted the code and slightly modified it (namely, the method
get_approximate()discussed earlier in this thread), so that our code is self-contained, see here at bitbucket .Therefore we don't really need Bio.trie. However when looking for a trie implementation suitable for our needs, in 2015 this was the most efficient and simple one to use. This could be the reason to keep it, but maybe indeed separate from the rest of Biopython. I haven't tried pygtrie and don't know if it's superior.
peterjc commentedon Apr 11, 2018
Would you be interested in taking over the maintenance of Bio.trie if we moved it into a separate package with its own entry on PyPI?
pritykin commentedon Apr 11, 2018
I will be happy to try if it's helpful, but I won't be an active maintainer, for the lack of experience. I'll try pygtrie within a week, to assess if it has necessary functionality, maybe it already covers what Bio.trie can do.
pritykin commentedon Apr 11, 2018
Ok, after examination, I don't think pygtrie has the same functionality implemented, namely, the function
get_approximate()this thread started with. Also, implementation in C is very efficient, while pygtrie is in pure python. We don't need Bio.trie as of now in our project (see above), but its functionality may be useful in the future. I'm happy to try to maintain it as a standalone package (though I haven't tried before).mdehoon commentedon Apr 12, 2018
@pritykin There is also a package marisa-trie on PyPi.
gmjonker commentedon May 16, 2019
I compared several trie implementations, and found that Bio.trie is by far the best in terms of speed and memory usage.
peterjc commentedon Jul 24, 2020
Closing - We removed
Bio.triein #2501 - but the licence would allow anyone to fork it and make a stand alone project for release on PyPI.Thank you @chebizarro for issue triage.