code: def match_candidate_and_cohort_haplotypes(
      candidate_haps, cohort_haps_and_freqs
  ):
      """Match candidate haplotypes with cohort haplotypes and update frequency.
  
      First, we look for exact haplotype matches between candidate and cohorts.
      If there're any matches, the REF allele frequency associated with the matching
      ALT allele is updated as well.
  
      Second, if no matches are found, we try to find inexact matches, where only
      REF alleles are matched. The inexact matching step is only used to update REF
      allele frequency. If no exact and inexact matches are found, set REF allele
      frequency to 1.
  
      Args:
        candidate_haps: A list of haplotype objects from a candidate.
        cohort_haps_and_freqs: A list of haplotype objects from cohorts. Haplotype
          objects are stored as dicts: {'haplotype': a haplotype (string), 'alt': an
          alt allele (string), 'variant': a Variant proto}
  
      Returns:
        A dict with candidate alt alleles as keys, and associated frequencies
        as values.
      """
      dict_allele_frequency = {}
      for candidate_obj in candidate_haps:
          candidate_haplotype = candidate_obj["haplotype"]
          candidate_alt = candidate_obj["alt"]
          candidate_variant = candidate_obj["variant"]
  
          for cohort_obj in cohort_haps_and_freqs:
              cohort_haplotype = cohort_obj["haplotype"]
              # Exact haplotype match.
              if candidate_haplotype == cohort_haplotype:
                  cohort_variant = cohort_obj["variant"]
                  cohort_frequency = get_allele_frequency(
                      cohort_variant,
                      list(cohort_variant.alternate_bases).index(
                          cohort_obj["alt"]
                      ),
                  )
                  dict_allele_frequency[
                      candidate_alt
                  ] = cohort_frequency
  
                  # Update REF frequency if it is not in the dictionary.
                  if not dict_allele_frequency.get(
                      candidate_variant.reference_bases
                  ):
                      dict_allele_frequency[
                          candidate_variant.reference_bases
                      ] = get_ref_allele_frequency(cohort_variant)
  
          # For an unmatched alt allele, set the frequency to 0.
          if not dict_allele_frequency.get(candidate_alt):
              dict_allele_frequency[candidate_alt] = 0
  
      # Calculate REF allele frequency if no exact match was found.
      # It is possible a novel mutation happens at a site where there are other
      # cohort variants. In this case, we cannot simply set REF frequency to 1.
      if sum(dict_allele_frequency.values()) == 0:
          candidate = candidate_haps[0]["variant"]
          # Left align variants.
          s_candidate = variant_utils.simplify_variant_alleles(
              candidate
          )
          for cohort_obj in cohort_haps_and_freqs:
              s_cohort_variant = variant_utils.simplify_variant_alleles(
                  cohort_obj["variant"]
              )
              # Try to find inexact matches to set REF allele frequency.
              # Inexact matches here mean only REF alleles match.
              if (
                  s_candidate.start == s_cohort_variant.start
                  and s_candidate.reference_bases
                  == s_cohort_variant.reference_bases
              ):
                  dict_allele_frequency[
                      s_candidate.reference_bases
                  ] = get_ref_allele_frequency(s_cohort_variant)
  
          # If still no match, set REF allele frequency to 1.
          if not dict_allele_frequency.get(candidate.reference_bases):
              dict_allele_frequency[candidate.reference_bases] = 1
  
      return dict_allele_frequency
  
file_path: deepvariant/deepvariant/allele_frequency.py
imports: import collections
  from typing import DefaultDict, Sequence, Optional
  from absl import logging
  from third_party.nucleus.io import vcf
  from third_party.nucleus.util import ranges
  from third_party.nucleus.util import variant_utils
  from deepvariant.protos import deepvariant_pb2
label_desc: Use image node_id 5 for calling a global function with example usage: match_candidate_and_cohort_haplotypes(candidate_haps, cohort_haps_and_freqs) and returns: dict_allele_frequency