code: def segment_reference_blocks(
      ref: "MatrixTable", intervals: "Table"
  ) -> "MatrixTable":
      """Returns a matrix table of reference blocks segmented according to intervals.
  
      Loci outside the given intervals are discarded. Reference blocks that start before
      but span an interval will appear at the interval start locus.
  
      Note
      ----
          Assumes disjoint intervals which do not span contigs.
  
          Requires start-inclusive intervals.
  
      Parameters
      ----------
      ref : :class:`.MatrixTable`
          MatrixTable of reference blocks.
      intervals : :class:`.Table`
          Table of intervals at which to segment reference blocks.
  
      Returns
      -------
      :class:`.MatrixTable`
      """
      interval_field = list(intervals.key)[0]
      if not intervals[interval_field].dtype == hl.tinterval(
          ref.locus.dtype
      ):
          raise ValueError(
              f"expect intervals to be keyed by intervals of loci matching the VariantDataset:"
              f" found {intervals[interval_field].dtype} / {ref.locus.dtype}"
          )
      intervals = intervals.select(
          _interval_dup=intervals[interval_field]
      )
  
      if not intervals.aggregate(
          hl.agg.all(
              intervals[interval_field].includes_start
              & (
                  intervals[interval_field].start.contig
                  == intervals[interval_field].end.contig
              )
          )
      ):
          raise ValueError("expect intervals to be start-inclusive")
  
      starts = intervals.key_by(
          _start_locus=intervals[interval_field].start
      )
      starts = starts.annotate(_include_locus=True)
      refl = ref.localize_entries("_ref_entries", "_ref_cols")
      joined = refl.join(starts, how="outer")
      rg = ref.locus.dtype.reference_genome
      contigs = rg.contigs
      contig_idx_map = hl.literal(
          {contigs[i]: i for i in range(len(contigs))},
          "dict<str, int32>",
      )
      joined = joined.annotate(
          __contig_idx=contig_idx_map[joined.locus.contig]
      )
      joined = joined.annotate(
          _ref_entries=joined._ref_entries.map(
              lambda e: e.annotate(__contig_idx=joined.__contig_idx)
          )
      )
      dense = joined.annotate(
          dense_ref=hl.or_missing(
              joined._include_locus,
              hl.rbind(
                  joined.locus.position,
                  lambda pos: hl.enumerate(
                      hl.scan._densify(
                          hl.len(joined._ref_cols), joined._ref_entries
                      )
                  ).map(
                      lambda idx_and_e: hl.rbind(
                          idx_and_e[0],
                          idx_and_e[1],
                          lambda idx, e: hl.coalesce(
                              joined._ref_entries[idx],
                              hl.or_missing(
                                  (
                                      e.__contig_idx
                                      == joined.__contig_idx
                                  )
                                  & (e.END >= pos),
                                  e,
                              ),
                          ),
                      ).drop("__contig_idx")
                  ),
              ),
          )
      )
      dense = dense.filter(dense._include_locus).drop(
          "_interval_dup", "_include_locus", "__contig_idx"
      )
  
      # at this point, 'dense' is a table with dense rows of reference blocks, keyed by locus
  
      refl_filtered = refl.annotate(
          **{interval_field: intervals[refl.locus]._interval_dup}
      )
  
      # remove rows that are not contained in an interval, and rows that are the start of an
      # interval (interval starts come from the 'dense' table)
      refl_filtered = refl_filtered.filter(
          hl.is_defined(refl_filtered[interval_field])
          & (refl_filtered.locus != refl_filtered[interval_field].start)
      )
  
      # union dense interval starts with filtered table
      refl_filtered = refl_filtered.union(
          dense.transmute(_ref_entries=dense.dense_ref)
      )
  
      # rewrite reference blocks to end at the first of (interval end, reference block end)
      refl_filtered = refl_filtered.annotate(
          interval_end=refl_filtered[interval_field].end.position
          - ~refl_filtered[interval_field].includes_end
      )
      refl_filtered = refl_filtered.annotate(
          _ref_entries=refl_filtered._ref_entries.map(
              lambda entry: entry.annotate(
                  END=hl.min(entry.END, refl_filtered.interval_end)
              )
          )
      )
  
      return refl_filtered._unlocalize_entries(
          "_ref_entries", "_ref_cols", list(ref.col_key)
      )
  
imports: from typing import Sequence
  import hail
  from hail import ir
  from hail.expr import expr_any, expr_array, expr_interval, expr_locus, expr_str, expr_bool
  from hail.matrixtable import MatrixTable
  from hail.methods.misc import require_first_key_field_locus
  from hail.table import Table
  from hail.typecheck import sequenceof, typecheck, nullable, oneof, enumeration, func_spec, dictof
  from hail.utils.java import Env, info, warning
  from hail.utils.misc import divide_null, new_temp_file, wrap_to_list
  from hail.vds.variant_dataset import VariantDataset
label_desc: Use image node_id 13 for calling a global function with example usage: segment_reference_blocks(ref, intervals) and returns: refl_filtered