code: def filter_samples(
      vds: "VariantDataset",
      samples,
      *,
      keep: bool = True,
      remove_dead_alleles: bool = False,
  ) -> "VariantDataset":
      """Filter samples in a :class:`.VariantDataset`.
  
      Parameters
      ----------
      vds : :class:`.VariantDataset`
          Dataset in VariantDataset representation.
      samples : :class:`.Table` or list of str
          Samples to keep or remove.
      keep : :obj:`bool`
          Whether to keep (default), or filter out the samples from `samples_table`.
      remove_dead_alleles : :obj:`bool`
          If true, remove alleles observed in no samples. Alleles with AC == 0 will be
          removed, and LA values recalculated.
  
      Returns
      -------
      :class:`.VariantDataset`
      """
      if not isinstance(samples, hl.Table):
          samples = hl.Table.parallelize(
              samples.map(lambda s: hl.struct(s=s)), key="s"
          )
      if not list(samples[x].dtype for x in samples.key) == [hl.tstr]:
          raise TypeError(f"invalid key: {samples.key.dtype}")
      samples_to_keep = samples.aggregate(
          hl.agg.collect_as_set(samples.key[0]), _localize=False
      )._persist()
      reference_data = vds.reference_data.filter_cols(
          samples_to_keep.contains(vds.reference_data.col_key[0]),
          keep=keep,
      )
      reference_data = reference_data.filter_rows(hl.agg.count() > 0)
      variant_data = vds.variant_data.filter_cols(
          samples_to_keep.contains(vds.variant_data.col_key[0]),
          keep=keep,
      )
  
      if remove_dead_alleles:
          vd = variant_data
          vd = vd.annotate_rows(
              __allele_counts=hl.agg.explode(
                  lambda x: hl.agg.counter(x), vd.LA
              ),
              __n=hl.agg.count(),
          )
          vd = vd.filter_rows(vd.__n > 0)
          vd = vd.drop("__n")
  
          vd = vd.annotate_rows(
              __kept_indices=hl.dict(
                  hl.enumerate(
                      hl.range(hl.len(vd.alleles)).filter(
                          lambda idx: (idx == 0)
                          | (vd.__allele_counts.get(idx, 0) > 0)
                      ),
                      index_first=False,
                  )
              )
          )
  
          vd = vd.annotate_rows(
              __old_to_new_LA=hl.range(hl.len(vd.alleles)).map(
                  lambda idx: vd.__kept_indices.get(idx, -1)
              )
          )
  
          def new_la_index(old_idx):
              raw_idx = vd.__old_to_new_LA[old_idx]
              return (
                  hl.case()
                  .when(raw_idx >= 0, raw_idx)
                  .or_error(
                      "'filter_samples': unexpected local allele: old index="
                      + hl.str(old_idx)
                  )
              )
  
          vd = vd.annotate_entries(
              LA=vd.LA.map(lambda la: new_la_index(la))
          )
          vd = vd.key_rows_by("locus")
          vd = vd.annotate_rows(
              alleles=vd.__kept_indices.keys().map(
                  lambda i: vd.alleles[i]
              )
          )
          vd = vd._key_rows_by_assert_sorted("locus", "alleles")
          vd = vd.drop(
              "__allele_counts", "__kept_indices", "__old_to_new_LA"
          )
          return VariantDataset(reference_data, vd)
  
      variant_data = variant_data.filter_rows(hl.agg.count() > 0)
      return VariantDataset(reference_data, variant_data)
  
imports: from typing import Sequence
  import hail
  from hail import ir
  from hail.expr import expr_any, expr_array, expr_interval, expr_locus, expr_str, expr_bool
  from hail.matrixtable import MatrixTable
  from hail.methods.misc import require_first_key_field_locus
  from hail.table import Table
  from hail.typecheck import sequenceof, typecheck, nullable, oneof, enumeration, func_spec, dictof
  from hail.utils.java import Env, info, warning
  from hail.utils.misc import divide_null, new_temp_file, wrap_to_list
  from hail.vds.variant_dataset import VariantDataset
label_desc: Use image node_id 5 for calling a global function with example usage: filter_samples(vds, samples) and returns: VariantDataset, VariantDataset, hl