class_docstr: Haplotype-based variant labeler.
class_docstr_tok: ["Haplotype-based","variant","labeler","."]
code: def label_variants(self, variants, region):
      # Grab our truth variants and group up variants + truth into small enough
      # chunks that we can safely send them into our find_best_matching_haplotypes
      # function.
      truths = list(self._get_truth_variants(region))
      if truths:
          # Filter out homozygous reference labels.
          truths = [
              y
              for x, y in zip(
                  map(lambda x: sum(x) > 0, _variant_genotypes(truths)),
                  truths,
              )
              if x
          ]
      grouped = group_variants(
          candidates=list(variants),
          truths=truths,
          max_group_size=self.max_group_size,
          max_separation=self.max_separation,
          max_gt_options_product=self.max_gt_options_product,
      )
  
      # Now loop over our grouped variants, labeling them, and yielding
      # VariantLabel objects.
      for candidates_group, truth_group in grouped:
          ref = self.make_labeler_ref(candidates_group, truth_group)
          labeling = find_best_matching_haplotypes(
              candidates_group, truth_group, ref
          )
          if labeling is None:
              # Note this test must be 'is None' since label_variants can return an
              # empty list.
              raise ValueError(
                  "Failed to assign labels for variants",
                  candidates_group,
                  truth_group,
                  ref,
              )
  
          self._update_metrics(labeling)
          for labeled in labeling.candidates_with_assigned_genotypes():
              # This logic doesn't make a huge amount of sense when you are doing
              # haplotype-based labeling. Currently we only say a variant is confident
              # if it overlaps the confident regions, which is the baseline behavior.
              # However, it may be useful to rethink how we establish a variant is
              # confident, as the "event" may be within the confident regions but
              # shifted outside due to differences in representational choices. Seems
              # like another approach would be to assign confidence if it has a
              # non-ref genotype (as we only consider confident truth variants) or if
              # it overlaps the confident regions.
              yield variant_labeler.VariantLabel(
                  is_confident=self._confident_regions.variant_overlaps(
                      labeled
                  ),
                  genotype=tuple(labeled.calls[0].genotype),
                  variant=labeled,
              )
  
imports: import collections
  import copy
  import enum
  import heapq
  import itertools
  from absl import logging
  from deepvariant.labeler import variant_labeler
  from deepvariant.protos import deepvariant_pb2
  from third_party.nucleus.io import fasta
  from third_party.nucleus.util import ranges
  from third_party.nucleus.util import variant_utils
  from third_party.nucleus.util import variantcall_utils
label_desc: Use image node_id 2 for calling the HaplotypeLabeler obj's underlying member method code with example usage: obj.label_variants(variants, region) without return types