Skip to content

trie get_approximate() strange behavior #636

Closed

Description

@pritykin

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_method

Questions/comments:

  1. Why is there redundant output in the list, i.e., multiple copies of exactly the same tuple such as ('ABB', None, 2) ?
  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?
  3. 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?
  4. 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

Activity

  1. peterjc commented on Oct 13, 2015

    @peterjc
    Member

    On the bright side, I can reproduce this:

    $ python2.7
    Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from Bio import trie
    >>> t = trie.trie()
    >>> t['ABB'] = None
    >>> t['BAB'] = None
    >>> t.get_approximate('ABA', 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)]
    >>> import Bio
    >>> Bio.__version__
    '1.65+'
    >>> quit()
    

    Unfortunately I don't know what this function is meant to do. Bio.trie does 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.

  2. pritykin commented on Oct 20, 2015

    @pritykin
    Author

    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.

  3. peterjc commented on Oct 20, 2015

    @peterjc
    Member

    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.

  4. pritykin commented on Jan 5, 2016

    @pritykin
    Author

    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:

    "D.get_approximate(key, ed) -> List of all tuples (k, value, d) in D such that k is at edit (Levenshtein) distance at most ed from key. Note: in output multiple copies with the same k and value and with d <= ed are possible (see https://github.com/biopython/biopython/issues/636 for more details).";
    
  5. peterjc commented on Jan 19, 2016

    @peterjc
    Member

    @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) to D.get_approximate(key, ed) or something more explicit like max_edit_distance?

  6. mdehoon commented on Jan 22, 2016

    @mdehoon
    Contributor

    @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.

  7. mdehoon commented on Apr 11, 2018

    @mdehoon
    Contributor

    @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.

  8. pritykin commented on Apr 11, 2018

    @pritykin
    Author

    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.

  9. peterjc commented on Apr 11, 2018

    @peterjc
    Member

    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?

  10. pritykin commented on Apr 11, 2018

    @pritykin
    Author

    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.

  11. pritykin commented on Apr 11, 2018

    @pritykin
    Author

    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).

  12. mdehoon commented on Apr 12, 2018

    @mdehoon
    Contributor

    @pritykin There is also a package marisa-trie on PyPi.

  13. gmjonker commented on May 16, 2019

    @gmjonker

    I compared several trie implementations, and found that Bio.trie is by far the best in terms of speed and memory usage.

  14. peterjc commented on Jul 24, 2020

    @peterjc
    Member

    Closing - We removed Bio.trie in #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions