code: def find_best_matching_haplotypes(candidates, truths, ref):
      """Assigns genotypes to each variant to best match truths.
  
      See the module-level documentation for general information on how this
      algorithm works.
  
      Args:
        candidates: list[nucleus.protos.Variant]. A list of candidate variants, in
          coordinate-sorted order, all on the same chromosome.
        truths: list[nucleus.protos.Variant]. A list of truth variants, in
          coordinate-sorted order, for the same interval on the genome as variants.
        ref: ReferenceRegion. Used to get reference bases for variants. Must cover
          at least the span of the variants.
  
      Returns:
        A HaplotypeMatch object describing the best assignment of genotypes between
        the candidates and truth_variants, or None, if no consistent assignment can
        be found.
  
      Raises:
        ValueError: If any inputs are malformed.
      """
      candidates = list(candidates)
      truths = list(truths)
  
      if _DEBUG_PRINTING_IS_ENABLED:
          _log_variants("candidates", candidates)
          _log_variants("truth", truths)
  
      if not variant_utils.variants_are_sorted(candidates):
          raise ValueError("candidates are not sorted", candidates)
      if not variant_utils.variants_are_sorted(truths):
          raise ValueError("truths are not sorted", truths)
  
      def _hom_ref_enum_if_empty(list_of_variants, non_empty_enum):
          """If list_of_variants is empty, use a ONLY_HOM_REF enum for speed."""
          return (
              non_empty_enum
              if list_of_variants
              else EnumerationType.ONLY_HOM_REF
          )
  
      truth_haplotypes = deduplicate_haplotypes(
          enumerate_all_possible_haplotypes(
              truths,
              ref,
              _hom_ref_enum_if_empty(candidates, EnumerationType.TRUTH),
          )
      )
  
      # Note, it may be worth deduplicating these haplotypes as well.
      variant_haplotypes = enumerate_all_possible_haplotypes(
          candidates,
          ref,
          _hom_ref_enum_if_empty(truths, EnumerationType.CANDIDATES),
      )
  
      found = []
      for vh, vgt_list in variant_haplotypes.items():
          tgt = truth_haplotypes.get(vh)
          # Explicitly check tgt is  None, because () is a valid value for tgt
          # here to continue.
          if tgt is None:
              continue
          for vgt in vgt_list:
              found.append(
                  HaplotypeMatch(
                      haplotypes=vh,
                      candidates=candidates,
                      candidate_genotypes=vgt,
                      truths=truths,
                      truth_genotypes=tgt,
                  )
              )
  
      if not found:
          return None
      else:
          return select_best_haplotype_match(found)
  
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 12 for calling a global function with example usage: find_best_matching_haplotypes(candidates, truths, ref) and returns: unknown, None, select_best_haplotype_match