Skip to content

Add an overlapping count method to seq.py? #1294

Closed

Description

@chris-rands

I wonder if an overlapping count is sufficiently common that it would be worth adding a method alongside count. This implementation (modified from SO) is fairly simple I think (although I've not tested it extensively):

def overlapping_count(self, sub, start = 0, end = sys.maxsize):
    count = 0
    while True:
        start = self.find(sub, start, end) + 1
        if not start == 0:
            count += 1
        else:
            return count

print(overlapping_count('AAAA', 'AA'))
# 3

Thoughts? I can make a git branch if people think this is useful

Activity

  1. peterjc commented on Jun 23, 2017

    @peterjc
    Member

    It may be worth asking on the mailing list? Personally I don't recall ever needing the overlapping count - anything complex with motif finding might already be better covered in Bio.motifs. What are your thoughts @barwil and @mdehoon given you've worked on the motif code?

  2. mdehoon commented on Jun 26, 2017

    @mdehoon
    Contributor

    I think it's useful, but I would implement it as a method of Seq objects rather than a stand-alone function:

    def count(self, sub, start = 0, end = sys.maxsize, overlap=False):
        if not overlap:
            return str.count(self)
        else:
            ...

    Btw why use if not start == 0: instead of if start != 0:?

  3. peterjc commented on Jun 26, 2017

    @peterjc
    Member

    @chris-rands that sounds like a green light to work on a pull request :)

    @mdehoon we would both like the Seq object to behave more like a string, and potentially subclass it http://mailman.open-bio.org/pipermail/biopython-dev/2017-May/021698.html

    To that end, is there much to choose between adding a new method (.overlapping_count(...) or perhaps .count_overlapping(...) instead) versus a new argument .count(..., overlap=True)?

  4. chris-rands commented on Jun 26, 2017

    @chris-rands
    ContributorAuthor

    Thank you both for the comments! I like the idea of the new argument instead of the new method, but of course it is not my decision to make.

    not start == 0 vs start != 0 is a question of taste, the performance should be identical (since the CPython bycode output from dis.dis() is identical). However, since I note you use != elsewhere, I guess sticking to that would be best for consistency.

    Anyway, I'll start working on a pull request :)

  5. peterjc commented on Jun 26, 2017

    @peterjc
    Member

    The chances of Python adding an overlapping method to the core string seems practically zero, while I would say there is a small chance of them adding more arguments to the existing count method. So defining our own method seems less slightly likely to lead to a conflict with the base string?

    In terms of what would happen on an older version of Biopython, the two errors are both very clear I think:

    >>> from Bio.Seq import Seq
    >>> Seq("AAAA").count("AA", overlap=True)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: count() got an unexpected keyword argument 'overlap'
    >>> Seq("AAAA").overlap_count("AA")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Seq' object has no attribute 'overlap_count'

    Basically I think it comes down to a style preference.

  6. chris-rands commented on Jun 26, 2017

    @chris-rands
    ContributorAuthor

    Thank you Peter, I do see you point about the potential conflict, maybe a separate method is better. Anyway, I will wait until a consensus is reached on this.

    Are there new tests that should be added with this?

  7. peterjc commented on Jun 26, 2017

    @peterjc
    Member

    Yes, please do include tests (probably in Tests/test_Seq_objs.py) and also a doctest in the docstring would seem sensible (which will be tested via run_tests.py).

  8. chris-rands commented on Jun 27, 2017

    @chris-rands
    ContributorAuthor

    Thanks! On reflection, a new method seem perhaps simpler and would fit more easily with the current code structure and tests, so I would lean towards that if people agree? The name is still an open question, like .count_overlap or .overlap_count?

    Should the new method be introduced for the Seq, UnknownSeq and MutableSeq classes? Currently, each has a count method.

  9. peterjc commented on Jun 27, 2017

    @peterjc
    Member

    I would suggest .count_overlap because it starts .count so both would be suggested if the user's editor has auto-completion, but up to you.

    Ideally yes, the method would be added to all three classes Seq, UnknownSeq and ``MutableSeq`.

    Also you can update the existing .count(...) method docstrings to reference the new method (where they currently warn that they do not do an overlapping count).

  10. chris-rands commented on Jun 28, 2017

    @chris-rands
    ContributorAuthor

    Thanks again, I am making a pull request and I suggest we move the discussions to there

  11. chris-rands commented on Jul 5, 2017

    @chris-rands
    ContributorAuthor

    Closing, see pull request 1301

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