code: def import_bed(
      path,
      reference_genome="default",
      skip_invalid_intervals=False,
      contig_recoding=None,
      **kwargs,
  ) -> Table:
      """Import a UCSC BED file as a :class:`.Table`.
  
      Examples
      --------
  
      The file formats are
  
      .. code-block:: text
  
          $ cat data/file1.bed
          track name="BedTest"
          20    1          14000000
          20    17000000   18000000
          ...
  
          $ cat file2.bed
          track name="BedTest"
          20    1          14000000  cnv1
          20    17000000   18000000  cnv2
          ...
  
      Add the row field `cnv_region` indicating inclusion in
      at least one interval of the three-column BED file:
  
      >>> bed = hl.import_bed('data/file1.bed', reference_genome='GRCh37')
      >>> result = dataset.annotate_rows(cnv_region = hl.is_defined(bed[dataset.locus]))
  
      Add a row field `cnv_id` with the value given by the
      fourth column of a BED file:
  
      >>> bed = hl.import_bed('data/file2.bed')
      >>> result = dataset.annotate_rows(cnv_id = bed[dataset.locus].target)
  
      Notes
      -----
  
      The table produced by this method has one of two possible structures. If
      the .bed file has only three fields (`chrom`, `chromStart`, and
      `chromEnd`), then the produced table has only one column:
  
          - **interval** (:class:`.tinterval`) - Row key. Genomic interval. If
            `reference_genome` is defined, the point type of the interval will be
            :class:`.tlocus` parameterized by the `reference_genome`. Otherwise,
            the point type is a :class:`.tstruct` with two fields: `contig` with
            type :py:data:`.tstr` and `position` with type :py:data:`.tint32`.
  
      If the .bed file has four or more columns, then Hail will store the fourth
      column as a row field in the table:
  
          - *interval* (:class:`.tinterval`) - Row key. Genomic interval. Same schema as above.
          - *target* (:py:data:`.tstr`) - Fourth column of .bed file.
  
      `UCSC bed files <https://genome.ucsc.edu/FAQ/FAQformat.html#format1>`__ can
      have up to 12 fields, but Hail will only ever look at the first four. Hail
      ignores header lines in BED files.
  
      Warning
      -------
      Intervals in UCSC BED files are 0-indexed and half open.
      The line "5  100  105" correpsonds to the interval ``[5:101-5:106)`` in Hail's
      1-indexed notation. Details
      `here <http://genome.ucsc.edu/blog/the-ucsc-genome-browser-coordinate-counting-systems/>`__.
  
      Parameters
      ----------
      path : :class:`str`
          Path to .bed file.
      reference_genome : :class:`str` or :class:`.ReferenceGenome`, optional
          Reference genome to use.
      skip_invalid_intervals : :obj:`bool`
          If ``True`` and `reference_genome` is not ``None``, skip lines with
          intervals that are not consistent with the reference genome.
      contig_recoding: :obj:`dict` of (:class:`str`, :obj:`str`)
          Mapping from contig name in BED to contig name in loaded dataset.
          All contigs must be present in the `reference_genome`, so this is
          useful for mapping differently-formatted data onto known references.
      **kwargs
          Additional optional arguments to :func:`import_table` are valid arguments here except:
          `no_header`, `delimiter`, `impute`, `skip_blank_lines`, `types`, and `comment` as these
          are used by import_bed.
  
      Returns
      -------
      :class:`.Table`
          Interval-keyed table.
      """
  
      # UCSC BED spec defined here: https://genome.ucsc.edu/FAQ/FAQformat.html#format1
  
      t = import_table(
          path,
          no_header=True,
          delimiter=r"\s+",
          impute=False,
          skip_blank_lines=True,
          types={
              "f0": tstr,
              "f1": tint32,
              "f2": tint32,
              "f3": tstr,
              "f4": tstr,
          },
          comment=[
              """^browser.*""",
              """^track.*""",
              r"""^\w+=("[\w\d ]+"|\d+).*""",
          ],
          **kwargs,
      )
  
      if contig_recoding is not None:
          contig_recoding = hl.literal(contig_recoding)
  
      def recode_contig(x):
          if contig_recoding is None:
              return x
          return contig_recoding.get(x, x)
  
      if t.row.dtype == tstruct(f0=tstr, f1=tint32, f2=tint32):
          t = t.select(
              interval=locus_interval_expr(
                  recode_contig(t["f0"]),
                  t["f1"] + 1,
                  t["f2"] + 1,
                  True,
                  False,
                  reference_genome,
                  skip_invalid_intervals,
              )
          )
  
      elif len(t.row) >= 4 and tstruct(
          **dict(
              [(n, typ) for n, typ in t.row.dtype._field_types.items()][
                  :4
              ]
          )
      ) == tstruct(f0=tstr, f1=tint32, f2=tint32, f3=tstr):
          t = t.select(
              interval=locus_interval_expr(
                  recode_contig(t["f0"]),
                  t["f1"] + 1,
                  t["f2"] + 1,
                  True,
                  False,
                  reference_genome,
                  skip_invalid_intervals,
              ),
              target=t["f3"],
          )
  
      else:
          raise FatalError(
              "too few fields for BED file: expected 3 or more, but found {}".format(
                  len(t.row)
              )
          )
  
      if skip_invalid_intervals and reference_genome:
          t = t.filter(hl.is_defined(t.interval))
  
      return t.key_by("interval")
  
imports: import os
  import re
  from collections import defaultdict
  import avro.schema
  from avro.datafile import DataFileReader
  from avro.io import DatumReader
  import hail
  from hail import ir
  from hail.expr import StructExpression, LocusExpression, expr_array, expr_float64, expr_str, expr_numeric, expr_call, expr_bool, expr_int32, to_expr, analyze
  from hail.expr.types import hail_type, tarray, tfloat64, tstr, tint32, tstruct, tcall, tbool, tint64, tfloat32
  from hail.genetics.reference_genome import reference_genome_type
  from hail.ir.utils import parse_type
  from hail.matrixtable import MatrixTable
  from hail.methods.misc import require_biallelic, require_row_key_variant, require_col_key_str
  from hail.table import Table
  from hail.typecheck import typecheck, nullable, oneof, dictof, anytype, sequenceof, enumeration, sized_tupleof, numeric, table_key_type, char
  from hail.utils import new_temp_file
  from hail.utils.deduplicate import deduplicate
  from hail.utils.java import Env, FatalError, jindexed_seq_args, warning
  from hail.utils.java import info
  from hail.utils.misc import wrap_to_list, plural
  from .import_lines_helpers import split_lines, should_remove_line
label_desc: Use image node_id 8 for calling a global function with example usage: import_bed(path, reference_genome, skip_invalid_intervals, contig_recoding) and returns: t, contig_recoding, x