code: def export_gen(
      dataset,
      output,
      precision=4,
      gp=None,
      id1=None,
      id2=None,
      missing=None,
      varid=None,
      rsid=None,
  ):
      """Export a :class:`.MatrixTable` as GEN and SAMPLE files.
  
      .. include:: ../_templates/req_tvariant.rst
  
      .. include:: ../_templates/req_biallelic.rst
  
      Examples
      --------
      Import genotype probability data, filter variants based on INFO score, and
      export data to a GEN and SAMPLE file:
  
      >>> example_ds = hl.import_gen('data/example.gen', sample_file='data/example.sample')
      >>> example_ds = example_ds.filter_rows(agg.info_score(example_ds.GP).score >= 0.9) # doctest: +SKIP
      >>> hl.export_gen(example_ds, 'output/infoscore_filtered')
  
      Notes
      -----
      Writes out the dataset to a GEN and SAMPLE fileset in the
      `Oxford spec <http://www.stats.ox.ac.uk/%7Emarchini/software/gwas/file_format.html>`__.
  
      Parameters
      ----------
      dataset : :class:`.MatrixTable`
          Dataset.
      output : :class:`str`
          Filename root for output GEN and SAMPLE files.
      precision : :obj:`int`
          Number of digits to write after the decimal point.
      gp : :class:`.ArrayExpression` of type :py:data:`.tfloat64`, optional
          Expression for the genotype probabilities to output. If ``None``, the
          entry field `GP` is used if defined and is of type :class:`.tarray`
          with element type :py:data:`.tfloat64`. The array length must be 3.
          The values at indices 0, 1, and 2 are exported as the probabilities of
          homozygous reference, heterozygous, and homozygous variant,
          respectively. The default and missing value is ``[0, 0, 0]``.
      id1 : :class:`.StringExpression`, optional
          Expression for the first column of the SAMPLE file. If ``None``, the
          column key of the dataset is used and must be one field of type
          :py:data:`.tstr`.
      id2 : :class:`.StringExpression`, optional
          Expression for the second column of the SAMPLE file. If ``None``, the
          column key of the dataset is used and must be one field of type
          :py:data:`.tstr`.
      missing : :class:`.NumericExpression`, optional
          Expression for the third column of the SAMPLE file, which is the sample
          missing rate. Values must be between 0 and 1.
      varid : :class:`.StringExpression`, optional
          Expression for the variant ID (2nd column of the GEN file). 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([dataset.locus.contig, hl.str(dataset.locus.position), dataset.alleles[0], dataset.alleles[1]], ':')``
      rsid : :class:`.StringExpression`, optional
          Expression for the rsID (3rd column of the GEN file). If ``None``,
          the row field `rsid` is used if defined and is of type :py:data:`.tstr`.
          The default and missing value is ``"."``.
      """
  
      require_biallelic(dataset, "export_gen")
  
      hl.current_backend().validate_file(output)
  
      if gp is None:
          if "GP" in dataset.entry and dataset.GP.dtype == tarray(
              tfloat64
          ):
              entry_exprs = {"GP": dataset.GP}
          else:
              raise ValueError(
                  "exporting to GEN 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 id1 is None:
          require_col_key_str(dataset, "export_gen")
          id1 = dataset.col_key[0]
  
      if id2 is None:
          require_col_key_str(dataset, "export_gen")
          id2 = dataset.col_key[0]
  
      if missing is None:
          missing = hl.float64(0.0)
  
      if varid is None:
          if "varid" in dataset.row and dataset.varid.dtype == tstr:
              varid = dataset.varid
  
      if rsid is None:
          if "rsid" in dataset.row and dataset.rsid.dtype == tstr:
              rsid = dataset.rsid
  
      sample_exprs = {"id1": id1, "id2": id2, "missing": missing}
  
      locus = dataset.locus
      a = dataset.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 [
          (sample_exprs, dataset._col_indices),
          (gen_exprs, dataset._row_indices),
          (entry_exprs, dataset._entry_indices),
      ]:
          for name, expr in exprs.items():
              analyze("export_gen/{}".format(name), expr, axis)
  
      dataset = dataset._select_all(
          col_exprs=sample_exprs,
          col_key=[],
          row_exprs=gen_exprs,
          entry_exprs=entry_exprs,
      )
  
      writer = ir.MatrixGENWriter(output, precision)
      Env.backend().execute(ir.MatrixWrite(dataset._mir, writer))
  
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 3 for calling a global function with example usage: export_gen(dataset, output, precision, gp, id1, id2, missing, varid, rsid) without return types