You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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?
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)?
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.
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:
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?
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).
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.
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).
Activity
peterjc commentedon Jun 23, 2017
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?mdehoon commentedon Jun 26, 2017
I think it's useful, but I would implement it as a method of Seq objects rather than a stand-alone function:
Btw why use
if not start == 0:instead ofif start != 0:?peterjc commentedon Jun 26, 2017
@chris-rands that sounds like a green light to work on a pull request :)
@mdehoon we would both like the
Seqobject to behave more like astring, and potentially subclass it http://mailman.open-bio.org/pipermail/biopython-dev/2017-May/021698.htmlTo 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)?chris-rands commentedon Jun 26, 2017
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 == 0vsstart != 0is a question of taste, the performance should be identical (since the CPython bycode output fromdis.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 :)
peterjc commentedon Jun 26, 2017
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
countmethod. 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:
Basically I think it comes down to a style preference.
chris-rands commentedon Jun 26, 2017
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?
peterjc commentedon Jun 26, 2017
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 viarun_tests.py).chris-rands commentedon Jun 27, 2017
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_overlapor.overlap_count?Should the new method be introduced for the
Seq,UnknownSeqandMutableSeqclasses? Currently, each has acountmethod.peterjc commentedon Jun 27, 2017
I would suggest
.count_overlapbecause it starts.countso 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,UnknownSeqand ``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).chris-rands commentedon Jun 28, 2017
Thanks again, I am making a pull request and I suggest we move the discussions to there
chris-rands commentedon Jul 5, 2017
Closing, see pull request 1301