daler/pybedtools — Issue #43: What determines equivalence between Intervals?
Author: daler
Created: 2011-10-12 20:35:32+00:00
State: open

Design question (for anyone, but hopefully @brentp and @arq5x can weigh in):

Currently, an Interval object is equal to another if chrom, start, stop, and [possibly] name match -- see https://github.com/daler/pybedtools/blob/f13ff482aab4e73197dc8652c370b15ecc017326/pybedtools/cbedtools.pyx#L156.

Since an Interval object really just encapsulates a line in a file, I think it would be preferable to do matching on the reconstituted string. That way, if attributes are added, or strand is changed, Intervals will no longer be identical.

However, this means "<" and ">" no longer have meaning. I think this is OK, cause "<" and ">" operators have fuzzy semantics when strand comes into play -- for example, does `>` mean "higher coords than" or "downstream from" ?

I propose implementing this with a `__hash__` method, so that Intervals can become keys in dicts and we can make sets out of them. Then the `__richcmp__` just tests hashes, which should be really fast (Cython accesses Python's C API for `hash()`)

Like so:

```python
# in Interval class, cbedtools.pyx
def __hash__(self):
return hash(str(self))

def __richcmp__(self, other, int op):
if op in (2, 3):
return hash(self) == hash(other)
else:
return False
```

thoughts?