code: def _resolve_overlapping_variants(overlapping_variants):
      """Yields variants with compatible haplotypes, if possible.
  
      Args:
        overlapping_variants: list(Variant). A non-empty list of Variant protos in
          coordinate-sorted order that overlap on the reference genome and are
          predicted to contain alternate allele genotypes.
  
      Yields:
        Variant protos in coordinate-sorted order that try to resolve incompatible
        haplotypes.
      """
      # Short circuit the simplest case: A single variant in a region is compatible
      # with itself by definition.
      if len(overlapping_variants) == 1:
          yield overlapping_variants[0]
          return
  
      # If the actual genotype calls are compatible, we can safely return those
      # since they would be the most likely configuration also when restricting to
      # only valid configurations of genotype calls.
      calculator = _VariantCompatibilityCalculator(overlapping_variants)
      nonref_counts = [
          _nonref_genotype_count(v) for v in overlapping_variants
      ]
      if calculator.all_variants_compatible(nonref_counts):
          logging.vlog(
              2,
              "Overlapping variants are naturally compatible: %s",
              overlapping_variants,
          )
          for variant in overlapping_variants:
              yield variant
          return
  
      # The actual genotype calls produce an inconsistent haplotype. If the number
      # of affected variants is "too large", avoid processing since this is an
      # exponential process.
      if (
          len(overlapping_variants)
          > _MAX_OVERLAPPING_VARIANTS_TO_RESOLVE
      ):
          logging.vlog(
              2,
              (
                  "Overlapping variants are not naturally compatible, and there are"
                  " too many to exhaustively search (%s). Returning variants without"
                  " modification, beginning with %s."
              ),
              len(overlapping_variants),
              overlapping_variants[0],
          )
          for variant in overlapping_variants:
              yield variant
          return
  
      # Otherwise, the actual genotype calls are incompatible. Since the genotype
      # likelihoods are generally well-calibrated, we examine all configurations of
      # genotypes that create compatible haplotypes and retain the single
      # configuration with the highest joint likelihood across all variants as the
      # proposed genotype assignment. Separately, we rescale the likelihood of each
      # individual variant using only the valid genotype configurations. If the
      # results are concordant (i.e., the genotype predicted by the marginal
      # likelihood for each variant is the same as the genotype predicted when
      # maximizing the joint likelihood across all variants), we return variants
      # with those calls and the rescaled likelihoods. Otherwise, we log a warning
      # and emit the original (incompatible) variants.
      #
      # For example, a biallelic deletion with probabilities of homref, het, homalt
      # = 0.01, 0.9, 0.09 and inside it a biallelic SNP with probs 0.02, 0.48, 0.5.
      # Naively this would be called as a heterozygous indel and a homozygous SNP,
      # which is impossible as there are three total alternate genotypes. The
      # algorithm does the following:
      #
      #   Indel    SNP    Joint prob
      #   0/0      0/0    0.01 * 0.02 = 0.0002
      #   0/0      0/1    0.01 * 0.48 = 0.0048
      #   0/0      1/1    0.01 * 0.50 = 0.0050
      #   0/1      0/0    0.90 * 0.02 = 0.0180
      #   0/1      0/1    0.90 * 0.48 = 0.4320*
      #   0/1      1/1    <invalid>   = 0
      #   1/1      0/0    0.09 * 0.02 = 0.0018
      #   1/1      0/1    <invalid>   = 0
      #   1/1      1/1    <invalid>   = 0
      #
      #   So using the highest joint likelihood, we predict het indel and het SNP.
      #
      #   The marginal probability of each genotype for the indel is:
      #   0/0:  0.0002 + 0.0048 + 0.0050 = 0.01
      #   0/1:  0.0180 + 0.4320          = 0.45
      #   1/1:  0.0018                   = 0.0018
      #
      #   which after normalizing to sum to 1 is roughly 0.022, 0.974, 0.004.
      #   The marginal probability for the SNP, after performing similar
      #   calculations, is 0.043, 0.946, 0.011. So the marginals also predict a het
      #   indel and a het SNP. Since the two calculations agree, we use this
      #   genotype call and modified likelihoods.
      #
      # First, we find all non-reference count configurations that are compatible.
      # This represents each variant solely based on its number of non-reference
      # genotypes, and assumes that variants are compatible if the total number of
      # non-reference genotypes at a single position is at most two. By using
      # non-reference counts, we avoid testing multiple allele configurations that
      # will return the same result (e.g. a variant with two possible alternate
      # alleles has three allele configurations that are homozygous alternate
      # [1/1, 1/2, 2/2] and either all or none of them will be valid depending on
      # the variants it interacts with).
      valid_nonref_count_configurations = [
          conf
          for conf in itertools.product(
              [0, 1, 2], repeat=len(overlapping_variants)
          )
          if calculator.all_variants_compatible(conf)
      ]
  
      # Next, we find the single compatible variant assignment with the individually
      # highest likelihood and track the total likelihood distributed to all variant
      # genotypes.
      likelihood_aggregators = [
          _LikelihoodAggregator(len(v.alternate_bases))
          for v in overlapping_variants
      ]
      most_likely_allele_indices_config = None
      most_likely_likelihood = None
      for nonref_count_config in valid_nonref_count_configurations:
          for (
              allele_indices_config
          ) in _get_all_allele_indices_configurations(
              overlapping_variants, nonref_count_config
          ):
              config_likelihood = (
                  _allele_indices_configuration_likelihood(
                      overlapping_variants, allele_indices_config
                  )
              )
              if (
                  most_likely_likelihood is None
                  or config_likelihood > most_likely_likelihood
              ):
                  most_likely_likelihood = config_likelihood
                  most_likely_allele_indices_config = (
                      allele_indices_config
                  )
              for aggregator, allele_indices in zip(
                  likelihood_aggregators, allele_indices_config
              ):
                  aggregator.add(allele_indices, config_likelihood)
  
      marginal_allele_indices_config = tuple(
          agg.most_likely_allele_indices()
          for agg in likelihood_aggregators
      )
      if (
          marginal_allele_indices_config
          == most_likely_allele_indices_config
      ):
          logging.vlog(
              2,
              (
                  "Overlapping variants are not naturally compatible, but the"
                  " genotype configuration with the most likely joint likelihood is"
                  " the same as that from the scaled marginal likelihoods: %s"
              ),
              overlapping_variants[0],
          )
          # Collapse the probabilities of all configurations to a single GL for each
          # allele, independently for each variant.
          scaled_gls = [
              agg.scaled_likelihoods() for agg in likelihood_aggregators
          ]
  
          for variant, allele_indices, gls in zip(
              overlapping_variants,
              most_likely_allele_indices_config,
              scaled_gls,
          ):
              newvariant = copy.deepcopy(variant)
              call = variant_utils.only_call(newvariant)
              call.genotype[:] = allele_indices
              call.genotype_likelihood[:] = gls
              yield newvariant
      else:
          logging.vlog(
              2,
              (
                  "Overlapping variants are not naturally compatible, and the"
                  " genotype configuration with the most likely joint likelihood is"
                  " different from that using the scaled marginal likelihoods: %s"
              ),
              overlapping_variants[0],
          )
          # TODO: Do something better than just punting.
          for variant in overlapping_variants:
              yield variant
  
imports: import copy
  import itertools
  from absl import flags
  from absl import logging
  import numpy
  from third_party.nucleus.util import genomics_math
  from third_party.nucleus.util import variant_utils
label_desc: Use image node_id 4 for calling a global function with example usage: _resolve_overlapping_variants(overlapping_variants) without return types