Skip to content

Commit

Permalink
Merge pull request #55 from standage/range_funcs
Browse files Browse the repository at this point in the history
Simpler range and point overlap queries
  • Loading branch information
standage authored Feb 3, 2017
2 parents 6804d3d + ffaab2e commit a695b68
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.3.0] - 2017-02-02
### Added
- New convenience functions in the `Range` and `Feature` classes for range and
point overlap queries.

## [0.2.0] - 2017-01-18
### Added
- An index class for efficient in-memory access of sequence features.
Expand Down
10 changes: 10 additions & 0 deletions tag/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Feature(object):
'gene'
>>> feature.start, feature.end
(999, 7500)
>>> feature.contains_point(2044)
True
>>> feature.score is None
True
>>> feature.strand
Expand Down Expand Up @@ -465,3 +467,11 @@ def cdslen(self):
return None

return sum([len(c) for c in self.children if c.type == 'CDS'])

def contains(self, rng):
"""Report whether this feature contains the specified range."""
return self._range.contains(rng)

def contains_point(self, point):
"""Report whether this feature contains the specified point."""
return self._range.contains_point(point)
12 changes: 9 additions & 3 deletions tag/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Range(object):
[0, 1000)
>>> len(rng)
1000
>>> rng.contains(Range(10, 20))
True
>>> rng.contains_point(2345)
False
>>> rng.start = 500
>>> rng.start
500
Expand Down Expand Up @@ -136,9 +140,11 @@ def overlap(self, other):

def contains(self, other):
"""Determine whether this range contains another."""
if self._start <= other.start and self._end >= other.end:
return True
return False
return self._start <= other.start and self._end >= other.end

def contains_point(self, point):
"""Determine whether this range contains the specified point."""
return self._start <= point and self._end >= point

def within(self, other):
"""Determine whether this range is contained within another."""
Expand Down
2 changes: 2 additions & 0 deletions tests/test_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def test_region():
f1 = Feature('\t'.join(gff3))
assert f1._range == Range(999, 2000)
assert f1.start == 999 and f1.end == 2000
assert f1.contains(Range(1500, 1600))
assert f1.contains_point(5) is False

gff3 = ['contig5', 'vim', 'mRNA', '500', '2500', '.', '+', '.',
'ID=t1;Parent=g1']
Expand Down
2 changes: 2 additions & 0 deletions tests/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def test_contains_within():
assert Range(279886, 283581).contains(Range(279986, 283481)) is True
assert Range(279986, 283481).contains(Range(279886, 283581)) is False
assert Range(279986, 283481).within(Range(279886, 283581)) is True
assert Range(9876, 9999).contains_point(10) is False
assert Range(9876, 9999).contains_point(9900) is True


def test_transform():
Expand Down

0 comments on commit a695b68

Please sign in to comment.