Commit f5d19d0a authored by Mikael Boden's avatar Mikael Boden

prac4

parent f5bf266d
...@@ -136,10 +136,14 @@ class BedEntry(): ...@@ -136,10 +136,14 @@ class BedEntry():
def getInterval(self): def getInterval(self):
return ival.Interval(self.chromStart, self.chromEnd) return ival.Interval(self.chromStart, self.chromEnd)
def dist(entry1, entry2): def dist(entry1, entry2, signed = False, centre2centre = False):
""" Calculate and return the BedEntry with the closest distance (from one end of the interval of this to the end of the interval of that).
If centre2centre is True, use the centre-to-centre distance instead.
If signed is True, the distance is negative if this interval is after the that.
"""
if isinstance(entry1, BedEntry) and isinstance(entry2, BedEntry): if isinstance(entry1, BedEntry) and isinstance(entry2, BedEntry):
if (entry1.chrom == entry2.chrom): if (entry1.chrom == entry2.chrom):
return ival.dist(entry1.getInterval(), entry2.getInterval()) return ival.dist(entry1.getInterval(), entry2.getInterval(), signed, centre2centre)
return None return None
class BedFile: class BedFile:
...@@ -282,6 +286,18 @@ class BedFile: ...@@ -282,6 +286,18 @@ class BedFile:
else: return None else: return None
else: return None else: return None
def getOneOfClosest(self, item):
all = self.getClosest(item)
if all == None: return None
else: return next(iter(all))
def getOneOfOverlap(self, item):
all = self.getOverlap(item)
if all == None: return None
elif len(all) == 0: return None
else: return next(iter(all))
def readBedFile(filename, format = 'Limited'): def readBedFile(filename, format = 'Limited'):
""" Read a BED file. """ Read a BED file.
format: specifies the format of the file, format: specifies the format of the file,
......
...@@ -294,14 +294,31 @@ class Interval: ...@@ -294,14 +294,31 @@ class Interval:
def __sizeof__(self): def __sizeof__(self):
return self.max - self.min return self.max - self.min
def dist(self, that): def dist(self, that, signed = False, centre2centre = False):
if (self.min > that.max): return self.min - that.max # that interval is BEFORE this """ Calculate and return the closest distance (from one end of the interval of this to the end of the interval of that).
if (self.max < that.min): return that.min - self.max # that interval is AFTER this If centre2centre is True, use the centre-to-centre distance instead.
return 0 If signed is True, the distance is negative if this interval is after the that.
"""
def dist(first, second): if not centre2centre:
if not signed:
if (self.min > that.max): return self.min - that.max # that interval is BEFORE this
if (self.max < that.min): return that.min - self.max # that interval is AFTER this
else: # distance is signed
if (self.min > that.max): return that.max - self.min # that interval is BEFORE this
if (self.max < that.min): return that.min - self.max # that interval is AFTER this
return 0
else:
thiscentre = (self.max - self.min) / 2 + self.min
thatcentre = (that.max - that.min) / 2 + that.min
return thatcentre - thiscentre if signed else abs(thatcentre - thiscentre)
def dist(first, second, signed = False, centre2centre = False):
""" Calculate and return the closest distance (from one end of the interval to the other).
If centre2centre is True, use the centre-to-centre distance instead.
If signed is True, the distance is negative if the first is after the second.
"""
if isinstance(first, Interval) and isinstance(second, Interval): if isinstance(first, Interval) and isinstance(second, Interval):
return first.dist(second) return first.dist(second, signed, centre2centre)
return RuntimeError return RuntimeError
def union(first, second): def union(first, second):
...@@ -347,6 +364,10 @@ if __name__ == '__main__': ...@@ -347,6 +364,10 @@ if __name__ == '__main__':
k = Interval(24, 24) k = Interval(24, 24)
l = Interval(52, 55) l = Interval(52, 55)
print('dist(b,a,signed=False,centre2centre=False)=', dist(b, a, signed = False, centre2centre=False))
print('dist(b,a,signed=True,centre2centre=False)=', dist(b, a, signed = True, centre2centre=False))
print('dist(b,a,signed=False,centre2centre=True)=', dist(b, a, signed = False, centre2centre=True))
print('dist(b,a,signed=True,centre2centre=True)=', dist(b, a, signed = True, centre2centre=True))
t = IntervalTree() t = IntervalTree()
t.put(a, 'A') t.put(a, 'A')
t.put(b, 'B') t.put(b, 'B')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment