code: def export_vcf(
      dataset,
      output,
      append_to_header=None,
      parallel=None,
      metadata=None,
      *,
      tabix=False,
  ):
      """Export a :class:`.MatrixTable` or :class:`.Table` as a VCF file.
  
      .. include:: ../_templates/req_tvariant.rst
  
      Examples
      --------
      Export to VCF as a block-compressed file:
  
      >>> hl.export_vcf(dataset, 'output/example.vcf.bgz')
  
      Notes
      -----
      :func:`.export_vcf` writes the dataset to disk in VCF format as described in the
      `VCF 4.2 spec <https://samtools.github.io/hts-specs/VCFv4.2.pdf>`__.
  
      Use the ``.vcf.bgz`` extension rather than ``.vcf`` in the output file name
      for `blocked GZIP <http://www.htslib.org/doc/tabix.html>`__ compression.
  
      Note
      ----
          We strongly recommended compressed (``.bgz`` extension) and parallel
          output (`parallel` set to ``'separate_header'`` or
          ``'header_per_shard'``) when exporting large VCFs.
  
      Hail exports the fields of struct `info` as INFO fields, the elements of
      ``set<str>`` `filters` as FILTERS, the value of str `rsid` as ID, and the
      value of float64 `qual` as QUAL. No other row fields are exported.
  
      The FORMAT field is generated from the entry schema, which
      must be a :class:`.tstruct`.  There is a FORMAT
      field for each field of the struct. If `dataset` is a :class:`.Table`,
      then there will be no FORMAT field and the output will be a sites-only VCF.
  
      INFO and FORMAT fields may be generated from Struct fields of type
      :py:data:`.tcall`, :py:data:`.tint32`, :py:data:`.tfloat32`,
      :py:data:`.tfloat64`, or :py:data:`.tstr`. If a field has type
      :py:data:`.tint64`, every value must be a valid ``int32``. Arrays and sets
      containing these types are also allowed but cannot be nested; for example,
      ``array<array<int32>>`` is invalid. Arrays and sets are written with the
      same comma-separated format. Fields of type :py:data:`.tbool` are also
      permitted in `info` and will generate INFO fields of VCF type Flag.
  
      Hail also exports the name, length, and assembly of each contig as a VCF
      header line, where the assembly is set to the :class:`.ReferenceGenome`
      name.
  
      Consider the workflow of importing a VCF and immediately exporting the
      dataset back to VCF. The output VCF header will contain FORMAT lines for
      each entry field and INFO lines for all fields in `info`, but these lines
      will have empty Description fields and the Number and Type fields will be
      determined from their corresponding Hail types. To output a desired
      Description, Number, and/or Type value in a FORMAT or INFO field or to
      specify FILTER lines, use the `metadata` parameter to supply a dictionary
      with the relevant information. See
      :func:`get_vcf_metadata` for how to obtain the
      dictionary corresponding to the original VCF, and for info on how this
      dictionary should be structured.
  
      The output VCF header will also contain CONTIG lines
      with ID, length, and assembly fields derived from the reference genome of
      the dataset.
  
      The output VCF header will `not` contain lines added by external tools
      (such as bcftools and GATK) unless they are explicitly inserted using the
      `append_to_header` parameter.
  
      Warning
      -------
  
      INFO fields stored at VCF import are `not` automatically modified to
      reflect filtering of samples or genotypes, which can affect the value of
      AC (allele count), AF (allele frequency), AN (allele number), etc. If a
      filtered dataset is exported to VCF without updating `info`, downstream
      tools which may produce erroneous results. The solution is to create new
      fields in `info` or overwrite existing fields. For example, in order to
      produce an accurate `AC` field, one can run :func:`.variant_qc` and copy
      the `variant_qc.AC` field to `info.AC` as shown below.
  
      >>> ds = dataset.filter_entries(dataset.GQ >= 20)
      >>> ds = hl.variant_qc(ds)
      >>> ds = ds.annotate_rows(info = ds.info.annotate(AC=ds.variant_qc.AC)) # doctest: +SKIP
      >>> hl.export_vcf(ds, 'output/example.vcf.bgz')
  
      Warning
      -------
      Do not export to a path that is being read from in the same pipeline.
  
      Parameters
      ----------
      dataset : :class:`.MatrixTable`
          Dataset.
      output : :class:`str`
          Path of .vcf or .vcf.bgz file to write.
      append_to_header : :class:`str`, optional
          Path of file to append to VCF header.
      parallel : :class:`str`, optional
          If ``'header_per_shard'``, return a set of VCF files (one per
          partition) rather than serially concatenating these files. If
          ``'separate_header'``, return a separate VCF header file and a set of
          VCF files (one per partition) without the header. If ``None``,
          concatenate the header and all partitions into one VCF file.
      metadata : :obj:`dict` [:obj:`str`, :obj:`dict` [:obj:`str`, :obj:`dict` [:obj:`str`, :obj:`str`]]], optional
          Dictionary with information to fill in the VCF header. See
          :func:`get_vcf_metadata` for how this
          dictionary should be structured.
      tabix : :obj:`bool`, optional
          If true, writes a tabix index for the output VCF.
          **Note**: This feature is experimental, and the interface and defaults
          may change in future versions.
      """
      hl.current_backend().validate_file(output)
  
      _, ext = os.path.splitext(output)
      if ext == ".gz":
          warning(
              "VCF export with standard gzip compression requested. This is almost *never* desired and will "
              "cause issues with other tools that consume VCF files. The compression format used for VCF "
              "files is traditionally *block* gzip compression. To use block gzip compression with hail VCF "
              "export, use a path ending in `.bgz`."
          )
  
      if isinstance(dataset, Table):
          mt = MatrixTable.from_rows_table(dataset)
          dataset = mt.key_cols_by(sample="")
  
      require_col_key_str(dataset, "export_vcf")
      require_row_key_variant(dataset, "export_vcf")
  
      if "filters" in dataset.row and dataset.filters.dtype != hl.tset(
          hl.tstr
      ):
          raise ValueError(
              f"'export_vcf': expect the 'filters' field to be set<str>, found {dataset.filters.dtype}"
              f"\n  Either transform this field to set<str> to export as VCF FILTERS field, or drop it from the dataset."
          )
  
      info_fields = list(dataset.info) if "info" in dataset.row else []
      invalid_info_fields = [
          f
          for f in info_fields
          if not re.fullmatch(r"^([A-Za-z_][0-9A-Za-z_.]*|1000G)", f)
      ]
      if invalid_info_fields:
          invalid_info_str = "".join(
              f"\n    {f!r}" for f in invalid_info_fields
          )
          warning(
              "export_vcf: the following info field names are invalid in VCF 4.3 and may not work with some tools: "
              + invalid_info_str
          )
  
      row_fields_used = {"rsid", "info", "filters", "qual"}
  
      fields_dropped = []
      for f in dataset.globals:
          fields_dropped.append((f, "global"))
      for f in dataset.col_value:
          fields_dropped.append((f, "column"))
      for f in dataset.row_value:
          if f not in row_fields_used:
              fields_dropped.append((f, "row"))
  
      if fields_dropped:
          ignored_str = "".join(
              f"\n    {f!r} ({axis})" for f, axis in fields_dropped
          )
          warning(
              "export_vcf: ignored the following fields:" + ignored_str
          )
          dataset = dataset.drop(*(f for f, _ in fields_dropped))
  
      parallel = ir.ExportType.default(parallel)
  
      writer = ir.MatrixVCFWriter(
          output, append_to_header, parallel, metadata, tabix
      )
      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 6 for calling a global function with example usage: export_vcf(dataset, output, append_to_header, parallel, metadata) without return types