code: def export_bgen(
      mt,
      output,
      gp=None,
      varid=None,
      rsid=None,
      parallel=None,
      compression_codec="zlib",
  ):
      """Export MatrixTable as :class:`.MatrixTable` as BGEN 1.2 file with 8
      bits of per probability.  Also writes SAMPLE file.
  
      If `parallel` is ``None``, the BGEN file is written to ``output + '.bgen'``. Otherwise, ``output
      + '.bgen'`` will be a directory containing many BGEN files. In either case, the SAMPLE file is
      written to ``output + '.sample'``. For example,
  
      >>> hl.export_bgen(mt, '/path/to/dataset')  # doctest: +SKIP
  
      Will write two files: `/path/to/dataset.bgen` and `/path/to/dataset.sample`. In contrast,
  
      >>> hl.export_bgen(mt, '/path/to/dataset', parallel='header_per_shard')  # doctest: +SKIP
  
      Will create `/path/to/dataset.sample` and will create ``mt.n_partitions()`` files into the
      directory `/path/to/dataset.bgen/`.
  
  
      Notes
      -----
      The :func:`export_bgen` function requires genotype probabilities, either as an entry
      field of `mt` (of type ``array<float64>``), or an entry expression passed in the `gp`
      argument.
  
      Parameters
      ----------
      mt : :class:`.MatrixTable`
          Input matrix table.
      output : :class:`str`
          Root for output BGEN and SAMPLE files.
      gp : :class:`.ArrayExpression` of type :py:data:`.tfloat64`, optional
          Expression for genotype probabilities.  If ``None``, entry
          field `GP` is used if it exists and is of type
          :class:`.tarray` with element type :py:data:`.tfloat64`.
      varid : :class:`.StringExpression`, optional
          Expression for the variant ID. If ``None``, the row field
          `varid` is used if defined and is of type :py:data:`.tstr`.
          The default and missing value is
          ``hl.delimit([mt.locus.contig, hl.str(mt.locus.position), mt.alleles[0], mt.alleles[1]], ':')``
      rsid : :class:`.StringExpression`, optional
          Expression for the rsID. If ``None``, the row field `rsid` is
          used if defined and is of type :py:data:`.tstr`.  The default
          and missing value is ``"."``.
      parallel : :class:`str`, optional
          If ``None``, write a single BGEN file.  If ``'header_per_shard'``, write a collection of
          BGEN files (one per partition), each with its own header.  If ``'separate_header'``, write a
          file for each partition, without header, and a header file for the combined dataset. Note
          that the files produced by ``'separate_header'`` are each individually invalid BGEN files,
          they can only be read if they are concatenated together with the header file.
      compresssion_codec : str, optional
          Compression codec. One of 'zlib', 'zstd'.
  
      """
      require_row_key_variant(mt, "export_bgen")
      require_col_key_str(mt, "export_bgen")
  
      hl.current_backend().validate_file(output)
  
      if gp is None:
          if "GP" in mt.entry and mt.GP.dtype == tarray(tfloat64):
              entry_exprs = {"GP": mt.GP}
          else:
              raise ValueError(
                  "exporting to BGEN requires a GP (genotype probability) array<float64> field in the entry"
                  "\n  of the matrix table. If you only have hard calls (GT), BGEN is probably not the"
                  "\n  right format."
              )
      else:
          entry_exprs = {"GP": gp}
  
      if varid is None:
          if "varid" in mt.row and mt.varid.dtype == tstr:
              varid = mt.varid
  
      if rsid is None:
          if "rsid" in mt.row and mt.rsid.dtype == tstr:
              rsid = mt.rsid
  
      parallel = ir.ExportType.default(parallel)
  
      locus = mt.locus
      a = mt.alleles
      gen_exprs = {
          "varid": expr_or_else(
              varid,
              hl.delimit(
                  [locus.contig, hl.str(locus.position), a[0], a[1]],
                  ":",
              ),
          ),
          "rsid": expr_or_else(rsid, "."),
      }
  
      for exprs, axis in [
          (gen_exprs, mt._row_indices),
          (entry_exprs, mt._entry_indices),
      ]:
          for name, expr in exprs.items():
              analyze("export_bgen/{}".format(name), expr, axis)
  
      mt = mt._select_all(
          col_exprs={}, row_exprs=gen_exprs, entry_exprs=entry_exprs
      )
  
      Env.backend().execute(
          ir.MatrixWrite(
              mt._mir,
              ir.MatrixBGENWriter(output, parallel, compression_codec),
          )
      )
  
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 4 for calling a global function with example usage: export_bgen(mt, output, gp, varid, rsid, parallel, compression_codec) without return types