code: def sample_qc(mt, name="sample_qc") -> MatrixTable:
      """Compute per-sample metrics useful for quality control.
  
      .. include:: ../_templates/req_tvariant.rst
  
      Examples
      --------
  
      Compute sample QC metrics and remove low-quality samples:
  
      >>> dataset = hl.sample_qc(dataset, name='sample_qc')
      >>> filtered_dataset = dataset.filter_cols((dataset.sample_qc.dp_stats.mean > 20) & (dataset.sample_qc.r_ti_tv > 1.5))
  
      Notes
      -----
  
      This method computes summary statistics per sample from a genetic matrix and stores
      the results as a new column-indexed struct field in the matrix, named based on the
      `name` parameter.
  
      If `mt` contains an entry field `DP` of type :py:data:`.tint32`, then the
      field `dp_stats` is computed. If `mt` contains an entry field `GQ` of type
      :py:data:`.tint32`, then the field `gq_stats` is computed. Both `dp_stats`
      and `gq_stats` are structs with with four fields:
  
      - `mean` (``float64``) -- Mean value.
      - `stdev` (``float64``) -- Standard deviation (zero degrees of freedom).
      - `min` (``int32``) -- Minimum value.
      - `max` (``int32``) -- Maximum value.
  
      If the dataset does not contain an entry field `GT` of type
      :py:data:`.tcall`, then an error is raised. The following fields are always
      computed from `GT`:
  
      - `call_rate` (``float64``) -- Fraction of calls not missing or filtered.
        Equivalent to `n_called` divided by :meth:`.count_rows`.
      - `n_called` (``int64``) -- Number of non-missing calls.
      - `n_not_called` (``int64``) -- Number of missing calls.
      - `n_filtered` (``int64``) -- Number of filtered entries.
      - `n_hom_ref` (``int64``) -- Number of homozygous reference calls.
      - `n_het` (``int64``) -- Number of heterozygous calls.
      - `n_hom_var` (``int64``) -- Number of homozygous alternate calls.
      - `n_non_ref` (``int64``) -- Sum of `n_het` and `n_hom_var`.
      - `n_snp` (``int64``) -- Number of SNP alternate alleles.
      - `n_insertion` (``int64``) -- Number of insertion alternate alleles.
      - `n_deletion` (``int64``) -- Number of deletion alternate alleles.
      - `n_singleton` (``int64``) -- Number of private alleles. Reference alleles are never counted as singletons, even if
        every other allele at a site is non-reference.
      - `n_transition` (``int64``) -- Number of transition (A-G, C-T) alternate alleles.
      - `n_transversion` (``int64``) -- Number of transversion alternate alleles.
      - `n_star` (``int64``) -- Number of star (upstream deletion) alleles.
      - `r_ti_tv` (``float64``) -- Transition/Transversion ratio.
      - `r_het_hom_var` (``float64``) -- Het/HomVar call ratio.
      - `r_insertion_deletion` (``float64``) -- Insertion/Deletion allele ratio.
  
      Missing values ``NA`` may result from division by zero.
  
      Parameters
      ----------
      mt : :class:`.MatrixTable`
          Dataset.
      name : :class:`str`
          Name for resulting field.
  
      Returns
      -------
      :class:`.MatrixTable`
          Dataset with a new column-indexed field `name`.
      """
  
      require_row_key_variant(mt, "sample_qc")
  
      from hail.expr.functions import _num_allele_type, _allele_types
  
      allele_types = _allele_types[:]
      allele_types.extend(["Transition", "Transversion"])
      allele_enum = {i: v for i, v in enumerate(allele_types)}
      allele_ints = {v: k for k, v in allele_enum.items()}
  
      def allele_type(ref, alt):
          return hl.bind(
              lambda at: hl.if_else(
                  at == allele_ints["SNP"],
                  hl.if_else(
                      hl.is_transition(ref, alt),
                      allele_ints["Transition"],
                      allele_ints["Transversion"],
                  ),
                  at,
              ),
              _num_allele_type(ref, alt),
          )
  
      variant_ac = Env.get_uid()
      variant_atypes = Env.get_uid()
      mt = mt.annotate_rows(
          **{
              variant_ac: hl.agg.call_stats(mt.GT, mt.alleles).AC,
              variant_atypes: mt.alleles[1:].map(
                  lambda alt: allele_type(mt.alleles[0], alt)
              ),
          }
      )
  
      bound_exprs = {}
      gq_dp_exprs = {}
  
      def has_field_of_type(name, dtype):
          return name in mt.entry and mt[name].dtype == dtype
  
      if has_field_of_type("DP", hl.tint32):
          gq_dp_exprs["dp_stats"] = hl.agg.stats(mt.DP).select(
              "mean", "stdev", "min", "max"
          )
  
      if has_field_of_type("GQ", hl.tint32):
          gq_dp_exprs["gq_stats"] = hl.agg.stats(mt.GQ).select(
              "mean", "stdev", "min", "max"
          )
  
      if not has_field_of_type("GT", hl.tcall):
          raise ValueError(
              "'sample_qc': expect an entry field 'GT' of type 'call'"
          )
  
      bound_exprs["n_called"] = hl.agg.count_where(
          hl.is_defined(mt["GT"])
      )
      bound_exprs["n_not_called"] = hl.agg.count_where(
          hl.is_missing(mt["GT"])
      )
  
      n_rows_ref = hl.expr.construct_expr(
          hl.ir.Ref("n_rows", hl.tint64),
          hl.tint64,
          mt._col_indices,
          hl.utils.LinkedList(hl.expr.expressions.Aggregation),
      )
      bound_exprs["n_filtered"] = n_rows_ref - hl.agg.count()
      bound_exprs["n_hom_ref"] = hl.agg.count_where(
          mt["GT"].is_hom_ref()
      )
      bound_exprs["n_het"] = hl.agg.count_where(mt["GT"].is_het())
      bound_exprs["n_singleton"] = hl.agg.sum(
          hl.rbind(
              mt["GT"],
              lambda gt: hl.sum(
                  hl.range(0, gt.ploidy).map(
                      lambda i: hl.rbind(
                          gt[i],
                          lambda gti: (gti != 0)
                          & (mt[variant_ac][gti] == 1),
                      )
                  )
              ),
          )
      )
  
      bound_exprs["allele_type_counts"] = hl.agg.explode(
          lambda allele_type: hl.tuple(
              hl.agg.count_where(allele_type == i)
              for i in range(len(allele_ints))
          ),
          (
              hl.range(0, mt["GT"].ploidy)
              .map(lambda i: mt["GT"][i])
              .filter(lambda allele_idx: allele_idx > 0)
              .map(
                  lambda allele_idx: mt[variant_atypes][allele_idx - 1]
              )
          ),
      )
  
      result_struct = hl.rbind(
          hl.struct(**bound_exprs),
          lambda x: hl.rbind(
              hl.struct(
                  **{
                      **gq_dp_exprs,
                      "call_rate": hl.float64(x.n_called)
                      / (x.n_called + x.n_not_called + x.n_filtered),
                      "n_called": x.n_called,
                      "n_not_called": x.n_not_called,
                      "n_filtered": x.n_filtered,
                      "n_hom_ref": x.n_hom_ref,
                      "n_het": x.n_het,
                      "n_hom_var": x.n_called - x.n_hom_ref - x.n_het,
                      "n_non_ref": x.n_called - x.n_hom_ref,
                      "n_singleton": x.n_singleton,
                      "n_snp": (
                          x.allele_type_counts[
                              allele_ints["Transition"]
                          ]
                          + x.allele_type_counts[
                              allele_ints["Transversion"]
                          ]
                      ),
                      "n_insertion": x.allele_type_counts[
                          allele_ints["Insertion"]
                      ],
                      "n_deletion": x.allele_type_counts[
                          allele_ints["Deletion"]
                      ],
                      "n_transition": x.allele_type_counts[
                          allele_ints["Transition"]
                      ],
                      "n_transversion": x.allele_type_counts[
                          allele_ints["Transversion"]
                      ],
                      "n_star": x.allele_type_counts[
                          allele_ints["Star"]
                      ],
                  }
              ),
              lambda s: s.annotate(
                  r_ti_tv=divide_null(
                      hl.float64(s.n_transition), s.n_transversion
                  ),
                  r_het_hom_var=divide_null(
                      hl.float64(s.n_het), s.n_hom_var
                  ),
                  r_insertion_deletion=divide_null(
                      hl.float64(s.n_insertion), s.n_deletion
                  ),
              ),
          ),
      )
  
      mt = mt.annotate_cols(**{name: result_struct})
      mt = mt.drop(variant_ac, variant_atypes)
  
      return mt
  
imports: import abc
  import logging
  import hail
  from collections import Counter
  import os
  from shlex import quote
  from typing import Dict, Tuple, List, Optional, Union
  from hailtop import pip_version
  from hailtop.utils import async_to_blocking
  import hailtop.batch_client
  from hailtop.config import get_deploy_config
  from hailtop import yamlx
  from hail.backend.service_backend import ServiceBackend
  from hail.typecheck import typecheck, oneof, anytype, nullable, numeric
  from hail.expr.expressions.expression_typecheck import expr_float64
  from hail.utils import FatalError
  from hail.utils.java import Env, info, warning
  from hail.utils.misc import divide_null, guess_cloud_spark_provider, new_temp_file
  from hail.matrixtable import MatrixTable
  from hail.table import Table
  from hail.ir import TableToTableApply
  from .misc import require_biallelic, require_row_key_variant, require_col_key_str, require_table_key_variant, require_alleles_field
label_desc: Use image node_id 1 for calling a global function with example usage: sample_qc(mt, name) and returns: mt, hl, unknown