code: def index_bgen(
      path,
      index_file_map=None,
      reference_genome="default",
      contig_recoding=None,
      skip_invalid_loci=False,
      _buffer_size=16_000_000,
  ):
      """Index BGEN files as required by :func:`.import_bgen`.
  
      If `index_file_map` is unspecified, then, for each BGEN file, the index file is written in the
      same directory and as the associated BGEN file with the same filename appended by
      `.idx2`. Otherwise, the `index_file_map` must specify a distinct `idx2` path for each BGEN file.
  
      Example
      -------
      Index a BGEN file, renaming contig name "01" to "1":
  
      >>> hl.index_bgen("data/example.8bits.bgen",
      ...               contig_recoding={"01": "1"},
      ...               reference_genome='GRCh37')
  
      Warning
      -------
      While this method parallelizes over a list of BGEN files, each file is
      indexed serially by one core. Indexing several BGEN files on a large cluster
      is a waste of resources, so indexing should generally be done once,
      separately from large analyses.
  
      See Also
      --------
      :func:`.import_bgen`
  
      Parameters
      ----------
      path : :class:`str` or :obj:`list` of :obj:`str`
          The .bgen files to index. May be one of: a BGEN file path, a list of BGEN file paths, or the
          path of a directory that contains BGEN files.
      index_file_map : :obj:`dict` of :class:`str` to :obj:`str`, optional
          Dict of BGEN file to index file location. Index file location must have
          a `.idx2` file extension. Cannot use Hadoop glob patterns in file names.
      reference_genome : :class:`str` or :class:`.ReferenceGenome`, optional
          Reference genome to use.
      contig_recoding : :obj:`dict` of :class:`str` to :obj:`str`, optional
          Dict of old contig name to new contig name. The new contig name must be
          in the reference genome given by `reference_genome`.
      skip_invalid_loci : :obj:`bool`
          If ``True``, skip loci that are not consistent with `reference_genome`.
  
      """
      rg_t = (
          hl.tlocus(reference_genome)
          if reference_genome
          else hl.tstruct(contig=hl.tstr, position=hl.tint32)
      )
      if index_file_map is None:
          index_file_map = {}
      if contig_recoding is None:
          contig_recoding = {}
      raw_paths = wrap_to_list(path)
  
      fs = hl.current_backend().fs
      paths = []
      for p in raw_paths:
          if fs.is_file(p):
              paths.append(p)
          else:
              if not fs.is_dir(p):
                  raise ValueError(
                      f"index_bgen: no file or directory at {p}"
                  )
              for stat_result in fs.ls(p):
                  if re.match(
                      r"^.*part-[0-9]+(-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})?$",
                      os.path.basename(stat_result.path),
                  ):
                      paths.append(stat_result.path)
  
      paths_lit = hl.literal(paths, hl.tarray(hl.tstr))
      index_file_map_lit = hl.literal(
          index_file_map, hl.tdict(hl.tstr, hl.tstr)
      )
      for k, v in index_file_map.items():
          if not v.endswith(".idx2"):
              raise FatalError(
                  f"index file for {k} is missing a .idx2 file extension"
              )
      contig_recoding_lit = hl.literal(
          contig_recoding, hl.tdict(hl.tstr, hl.tstr)
      )
      ht = hl.utils.range_table(len(paths), len(paths))
      path_fd = paths_lit[ht.idx]
      ht = ht.annotate(
          n_indexed=hl.expr.functions._func(
              "index_bgen",
              hl.tint64,
              path_fd,
              index_file_map_lit.get(path_fd, path_fd + ".idx2"),
              contig_recoding_lit,
              hl.bool(skip_invalid_loci),
              hl.int32(_buffer_size),
              type_args=(rg_t,),
          )
      )
  
      for r in ht.collect():
          idx = r.idx
          n = r.n_indexed
          path = paths[idx]
          idx_path = index_file_map.get(path, path)
          info(f"indexed {n} sites in {path} at {idx_path}")
  
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 21 for calling a global function with example usage: index_bgen(path, index_file_map, reference_genome, contig_recoding, skip_invalid_loci, _buffer_size) without return types