class_docstr: Haplotype-based variant labeler.
class_docstr_tok: ["Haplotype-based","variant","labeler","."]
docstr: Update self._metrics with the HaplotypeMatch labeling results.
  
  This function updates the LabelingMetrics information in self._metrics using
  the labeling results in labeling.
  
  Args:
    labeling: HaplotypeMatch. The labeling information to use to update our
      LabelingMetrics.
code: def _update_metrics(self, labeling):
      """Update self._metrics with the HaplotypeMatch labeling results.
  
      This function updates the LabelingMetrics information in self._metrics using
      the labeling results in labeling.
  
      Args:
        labeling: HaplotypeMatch. The labeling information to use to update our
          LabelingMetrics.
      """
  
      def _n_alts_by_genotype(gt):
          """Returns the number of distinct alt alleles with non-zero genotype."""
          return len({g for g in gt if g > 0})
  
      def _is_hom_ref(gt):
          """Are all genotypes in gt the reference alleles (i.e., == 0)?"""
          return all(g == 0 for g in gt)
  
      def _has_alt_genotypes(gt):
          """Is any genotype in gt a non-ref (> 0) genotype?"""
          return any(g > 0 for g in gt)
  
      # Iterate over the truth variant and its associated original genotypes
      # (those provided by the input VCF) and the assigned genotypes (i.e., the
      # genotypes assigned to truth to make candidates and truth match haplotypes)
      # and compute a few metric values.
      for truth, original_gt, assigned_gt in zip(
          labeling.truths,
          labeling.original_truth_genotypes,
          labeling.truth_genotypes,
      ):
          n_alts_original = _n_alts_by_genotype(original_gt)
  
          self._metrics.n_truth_variant_sites += 1
          self._metrics.n_truth_variant_alleles += n_alts_original
          self._metrics.n_true_positive_sites += _has_alt_genotypes(
              assigned_gt
          )
          self._metrics.n_false_negative_sites += _is_hom_ref(
              assigned_gt
          )
  
          # If we have more than one alt allele in the original genotypes and the
          # assigned genotypes imply more or more are missing then we've got a
          # multi-allelic truth variant with some missing alleles.
          if (
              n_alts_original > 1
              and _n_alts_by_genotype(assigned_gt) < n_alts_original
          ):
              self._metrics.n_truth_multiallelics_sites_with_missed_alleles += (
                  1
              )
  
          # Iterate over the original and assigned genotypes for the truth variants
          # and count up the number of true positive alleles (i.e. original and
          # assigned genotypes are non-ref) and false negative alleles (i.e.,
          # original is non-ref but assigned is ref).
          for og, ag in zip(original_gt, assigned_gt):
              if og > 0:
                  if ag > 0:
                      self._metrics.n_true_positive_alleles += 1
                  elif ag == 0:
                      self._metrics.n_false_negative_alleles += 1
  
      # Create a dict from the start of truth to the truth variant itself and its
      # assigned genotypes. This is needed to compute site match counts below.
      truth_by_pos = {
          truth.start: (truth, gt)
          for truth, gt in zip(
              labeling.truths, labeling.truth_genotypes
          )
      }
  
      # Iterate over the candidates and their assigned genotypes to compute
      # the remaining metrics.
      #
      # Note that this counts all candidates, not just the ones in the confident
      # regions of the genome. This seems like a reasonable first approach, but it
      # may be necessary to restrict ourselves to only those overlapping the
      # confident regions.
      for candidate, genotype in zip(
          labeling.candidates, labeling.candidate_genotypes
      ):
          # If candidate isn't confident, add it to the non_confident count and
          # continue as the other metrics are only computed over confident
          # candidates.
          if not self._confident_regions.variant_overlaps(candidate):
              self._metrics.n_non_confident_candidate_variant_sites += 1
              continue
  
          n_alt_alleles = len(candidate.alternate_bases)
          self._metrics.n_candidate_variant_sites += 1
          self._metrics.n_candidate_variant_alleles += n_alt_alleles
          self._metrics.n_false_positive_sites += _is_hom_ref(genotype)
          self._metrics.n_false_positive_alleles += (
              n_alt_alleles - _n_alts_by_genotype(genotype)
          )
  
          # Use the truth_by_pos dict to determine which candidates occur at the
          # same position as a truth variant. If there is one, grab it and its
          # genotypes so we can compute metrics on exact position, allele, genotype
          # matches. If not, update the number of inexact matches if our candidate
          # is non-reference itself.
          truth, assigned_gt = truth_by_pos.get(
              candidate.start, (None, None)
          )
          if truth:
              self._metrics.n_exact_position_matches += 1
              if sorted(candidate.alternate_bases) == sorted(
                  truth.alternate_bases
              ):
                  self._metrics.n_exact_position_and_allele_matches += 1
                  if sorted(genotype) == sorted(assigned_gt):
                      self._metrics.n_exact_position_and_allele_and_genotype_matches += (
                          1
                      )
          elif _has_alt_genotypes(genotype):
              self._metrics.n_inexact_position_matches += 1
  
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 4 for calling the HaplotypeLabeler obj's underlying member method code with example usage: obj._update_metrics(labeling) and returns: len, all, any