Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
- "src/**"
- "tests/**"


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #192

jobs:
build:
strategy:
Expand Down
19 changes: 11 additions & 8 deletions src/py_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Copyright (C) 2016-2023 Deep Genomics Inc. All Rights Reserved.
#define GK_CHECK_REFG(a, b) \
GK_CHECK((a).refg == (b).refg, value, "Coordinate system mismatch, {} and {}.", (a), (b));

#define GKPY_INTERVAL_BOOL_METHOD_ONEARG(pyname, method) \
GKPY_METHOD_BEGIN_ONEARG(pyname, method) \
const Py##pyname::value_t& c = Py##pyname::value(selfo); \
#define GKPY_INTERVAL_BOOL_METHOD_ONEARG(method) \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive by. This macro is only ever used for PyInterval

GKPY_METHOD_BEGIN_ONEARG(Interval, method) \
const PyInterval::value_t& c = PyInterval::value(selfo); \
if (PyInterval::check(arg)) { const interval_t& a = PyInterval::value(arg); GK_CHECK_REFG(c, a); GKPY_RETURN_BOOL(c.method(a)); } \
GK_THROW(type, "argument must be an Interval, not '{}'", Py_TYPE(arg)->tp_name); \
GKPY_METHOD_END
Expand Down Expand Up @@ -270,6 +270,9 @@ GKPY_METHOD_BEGIN_ONEARG(Interval, intersect)
auto x = PyInterval::value(selfo);
if (PyInterval::check(arg)) {
auto y = PyInterval::value(arg);
if (!y.same_strand(x)) {
GKPY_RETURN_NONE;
}
if (((PyInterval*)selfo)->get_anchor() != invalid_pos || ((PyInterval*)arg)->get_anchor() != invalid_pos) {
GK_THROW(value, "anchored intersection ({}, {}) is not supported.", x, y);
}
Expand All @@ -285,11 +288,11 @@ GKPY_METHOD_BEGIN_ONEARG(Interval, intersect)
}
GK_THROW(type, "argument must be Interval, not '{}'", Py_TYPE(arg)->tp_name);
GKPY_METHOD_END
GKPY_INTERVAL_BOOL_METHOD_ONEARG(Interval, upstream_of)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(Interval, dnstream_of)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(Interval, contains)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(Interval, within)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(Interval, overlaps)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(upstream_of)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(dnstream_of)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(contains)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(within)
GKPY_INTERVAL_BOOL_METHOD_ONEARG(overlaps)
GKPY_INTERVAL_BOOL_METHOD_NOARG(Interval, is_pos_strand)
GKPY_METHOD_BEGIN_ONEARG(Interval, as_positive_strand)
return PyInterval::create(PyInterval::value(selfo).as_pos_strand(), self->get_anchor(), self->get_anchor_offset());
Expand Down
30 changes: 30 additions & 0 deletions tests/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,18 @@ def test_comparisons_forward(self):

self.assertTrue(c.upstream_of(a))
self.assertFalse(b.upstream_of(a))
self.assertTrue(a.overlaps(b))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a new test interval just so we can have a overlap == false test

self.assertFalse(a.overlaps(c))

self.assertTrue(a.dnstream_of(c))
self.assertFalse(a.dnstream_of(b))

self.assertTrue(a.contains(d))
self.assertFalse(a.contains(b))

self.assertTrue(d.within(a))
self.assertFalse(b.within(a))

self.assertTrue(a.upstream_of(Interval.from_rna1('chr1', 21, 21, 'hg19')))
self.assertFalse(a.upstream_of(Interval.from_rna1('chr1', 20, 20, 'hg19')))
self.assertFalse(a.upstream_of(Interval.from_rna1('chr1', 19, 19, 'hg19')))
Expand All @@ -476,6 +481,18 @@ def test_comparisons_forward(self):
with self.assertRaises(ValueError):
d != e

# test incompatible strand/chromosome
self.assertFalse(c.upstream_of(a.as_opposite_strand()))
self.assertFalse(c.upstream_of(Interval('chr2', a.strand, a.start, a.end, a.refg)))
self.assertFalse(a.dnstream_of(c.as_opposite_strand()))
self.assertFalse(a.dnstream_of(Interval('chr2', c.strand, c.start, c.end, c.refg)))
self.assertFalse(a.contains(d.as_opposite_strand()))
self.assertFalse(a.contains(Interval('chr2', d.strand, d.start, d.end, d.refg)))
self.assertFalse(d.within(a.as_opposite_strand()))
self.assertFalse(d.within(Interval('chr2', a.strand, a.start, a.end, a.refg)))
self.assertFalse(a.overlaps(b.as_opposite_strand()))
self.assertFalse(a.overlaps(Interval('chr2', b.strand, b.start, b.end, b.refg)))

def test_comparisons_reverse(self):
a = Interval.from_rna1('chr1', -20, -10, 'hg19')
b = Interval.from_rna1('chr1', -15, -5, 'hg19')
Expand All @@ -485,6 +502,7 @@ def test_comparisons_reverse(self):
self.assertTrue(a.upstream_of(c))
self.assertFalse(a.upstream_of(b))
self.assertTrue(a.overlaps(b))
self.assertFalse(a.overlaps(c))

self.assertTrue(c.dnstream_of(a))
self.assertFalse(b.dnstream_of(a))
Expand All @@ -502,6 +520,17 @@ def test_comparisons_reverse(self):
self.assertTrue(a.dnstream_of(Interval.from_rna1('chr1', -21, -21, 'hg19')))
self.assertFalse(a.dnstream_of(Interval.from_rna1('chr1', -20, -20, 'hg19')))

# test incompatible strand/chromosome
self.assertFalse(a.upstream_of(c.as_opposite_strand()))
self.assertFalse(a.upstream_of(Interval('chr2', c.strand, c.start, c.end, c.refg)))
self.assertFalse(c.dnstream_of(a.as_opposite_strand()))
self.assertFalse(c.dnstream_of(Interval('chr2', a.strand, a.start, a.end, a.refg)))
self.assertFalse(a.contains(d.as_opposite_strand()))
self.assertFalse(a.contains(Interval('chr2', d.strand, d.start, d.end, d.refg)))
self.assertFalse(d.within(Interval('chr2', a.strand, a.start, a.end, a.refg)))
self.assertFalse(a.overlaps(b.as_opposite_strand()))
self.assertFalse(a.overlaps(Interval('chr2', b.strand, b.start, b.end, b.refg)))

def test_input_validation(self):

with self.assertRaises(ValueError):
Expand Down Expand Up @@ -693,6 +722,7 @@ def test_intersect(self):
self.assertEqual(neg_mid.intersect(neg_inside), neg_inside)

self.assertIsNone(left.intersect(Interval(left.chrom, left.strand, left.start, left.end, 'hg38.p12')))
self.assertIsNone(left.intersect(Interval('chr2', left.strand, left.start, left.end, left.reference_genome)))
self.assertIsNone(neg_left.intersect(left))

with self.assertRaises(ValueError):
Expand Down
Loading