Build a dictionary of interval trees indexed by chrom from a BED stream or
  file

  :param reffh: This can be either a string, or a stream-like object. In the
                former case, it is treated as a filename. The format of the
                file/stream must be BED.
  :param scoreType: The data type for scores (the fifth column) in the BED
                    file.
  :param verbose: output progress messages to sys.stderr if True

def intervalTrees(reffh, scoreType=int, verbose=False):
  """
  Build a dictionary of interval trees indexed by chrom from a BED stream or
  file

  :param reffh: This can be either a string, or a stream-like object. In the
                former case, it is treated as a filename. The format of the
                file/stream must be BED.
  :param scoreType: The data type for scores (the fifth column) in the BED
                    file.
  :param verbose: output progress messages to sys.stderr if True
  """
  if type(reffh).__name__ == "str":
    fh = open(reffh)
  else:
    fh = reffh

  # load all the regions and split them into lists for each chrom
  elements = {}
  if verbose and fh != sys.stdin:
    totalLines = linesInFile(fh.name)
    pind = ProgressIndicator(totalToDo=totalLines,
                             messagePrefix="completed",
                             messageSuffix="of loading " + fh.name)
  for element in BEDIterator(fh, scoreType=scoreType, verbose=verbose):
    if element.chrom not in elements:
      elements[element.chrom] = []
    elements[element.chrom].append(element)
    if verbose and fh != sys.stdin:
      pind.done += 1
      pind.showProgress()

  # create an interval tree for each list
  trees = {}
  if verbose:
    totalLines = len(elements)
    pind = ProgressIndicator(totalToDo=totalLines,
                             messagePrefix="completed",
                             messageSuffix="of making interval trees")
  for chrom in elements:
    trees[chrom] = IntervalTree(elements[chrom], openEnded=True)
    if verbose:
      pind.done += 1
      pind.showProgress()

  return trees