Skip to content

Instantly share code, notes, and snippets.

@adamewing
Created April 12, 2018 00:02
Show Gist options
  • Save adamewing/8d1fd11924292422c1be9ff397bc6c12 to your computer and use it in GitHub Desktop.
Save adamewing/8d1fd11924292422c1be9ff397bc6c12 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from collections import defaultdict as dd
from bx.intervals.intersection import Intersecter, Interval
def interval_forest(bed_file):
''' build dictionary of interval trees '''
forest = dd(Intersecter)
with open(bed_file, 'r') as bed:
for line in bed:
chrom, start, end = line.strip().split()[:3]
label = '|'.join(line.strip().split())
forest[chrom].add_interval(Interval(int(start), int(end), value=label))
return forest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment