code: def range_depth(ranges, size, verbose=True):
      
      from jcvi.utils.iter import pairwise
      from jcvi.utils.cbook import percentage
  
      # Make endpoints
      endpoints = []
      for a, b in ranges:
          endpoints.append((a, LEFT))
          endpoints.append((b, RIGHT))
      endpoints.sort()
      vstart, vend = min(endpoints)[0], max(endpoints)[0]
  
      assert 0 <= vstart < size
      assert 0 <= vend < size
  
      depth = 0
      depthstore = defaultdict(int)
      depthstore[depth] += vstart
      depthdetails = [(0, vstart, depth)]
  
      for (a, atag), (b, btag) in pairwise(endpoints):
          if atag == LEFT:
              depth += 1
          elif atag == RIGHT:
              depth -= 1
          depthstore[depth] += b - a
          depthdetails.append((a, b, depth))
  
      assert btag == RIGHT
      depth -= 1
  
      assert depth == 0
      depthstore[depth] += size - vend
      depthdetails.append((vend, size, depth))
  
      assert sum(depthstore.values()) == size
      if verbose:
          for depth, count in sorted(depthstore.items()):
              print("Depth {0}: {1}".\
                      format(depth, percentage(count, size)), file=sys.stderr)
  
      return depthstore, depthdetails
docs: Overlay ranges on [start, end], and summarize the ploidy of the intervals.