class_docstr: Realign reads in regions to assembled haplotypes.
  
  This class helps us to realign reads in regions by:
  
  (1) Create smaller windows in which to operate over the region. These windows
  are created by finding evidence of genetic variation surrounded by stretches
  of reference-matching seqence.
  
  (2) Build a de-Bruijn assembly graph of the window. Edges are pruned if they
  don't meet the required weight threshold. Every remaining haplotype is listed
  by traversing the graph.
  
  (3) Realign reads using a Smith-Waterman algorithm to the best candidate
  haplotype and then realign that haplotype to the reference sequence to modify
  the read's alignment.
class_docstr_tok: ["Realign","reads","in","regions","to","assembled","haplotypes",".","This","class","helps","us","to","realign","reads","in","regions","by",":","(","1",")","Create","smaller","windows","in","which","to","operate","over","the","region",".","These","windows","are","created","by","finding","evidence","of","genetic","variation","surrounded","by","stretches","of","reference-matching","seqence",".","(","2",")","Build","a","de-Bruijn","assembly","graph","of","the","window",".","Edges","are","pruned","if","they","do","n't","meet","the","required","weight","threshold",".","Every","remaining","haplotype","is","listed","by","traversing","the","graph",".","(","3",")","Realign","reads","using","a","Smith-Waterman","algorithm","to","the","best","candidate","haplotype","and","then","realign","that","haplotype","to","the","reference","sequence","to","modify","the","read","'s","alignment","."]
docstr: Align reads to a given haplotype, not necessarily the reference.
  
  Align reads to a graph of haplotypes, reporting the alignments relative
  to a specific haplotype. This allows treating any alternate allele as
  the reference.
  
  Args:
    this_haplotype: string. Sequence of the haplotype to treat as reference,
      reporting alignments according to its coordinates.
    haplotypes: list of strings. All haplotypes to use in the graph, including
      this_haplotype.
    prefix: string. Sequence to the left of where the haplotypes differ.
    suffix: string. Sequence to the right of where the haplotypes differ.
    reads: reads to align.
    contig: string. Name of the 'reference' to report in read alignments.
    ref_start: integer. Start position of the region to report in read
      alignments. This should mark the beginning of the prefix sequence.
  
  Returns:
    Reads. Realigned and reported relative to the chosen haplotype.
docstr_tok: ["Align","reads","to","a","given","haplotype",",","not","necessarily","the","reference",".","Align","reads","to","a","graph","of","haplotypes",",","reporting","the","alignments","relative","to","a","specific","haplotype",".","This","allows","treating","any","alternate","allele","as","the","reference",".","Args",":","this_haplotype",":","string",".","Sequence","of","the","haplotype","to","treat","as","reference",",","reporting","alignments","according","to","its","coordinates",".","haplotypes",":","list","of","strings",".","All","haplotypes","to","use","in","the","graph",",","including","this_haplotype",".","prefix",":","string",".","Sequence","to","the","left","of","where","the","haplotypes","differ",".","suffix",":","string",".","Sequence","to","the","right","of","where","the","haplotypes","differ",".","reads",":","reads","to","align",".","contig",":","string",".","Name","of","the","'reference","'","to","report","in","read","alignments",".","ref_start",":","integer",".","Start","position","of","the","region","to","report","in","read","alignments",".","This","should","mark","the","beginning","of","the","prefix","sequence",".","Returns",":","Reads",".","Realigned","and","reported","relative","to","the","chosen","haplotype","."]
code: def align_to_haplotype(
      self,
      this_haplotype,
      haplotypes,
      prefix,
      suffix,
      reads,
      contig,
      ref_start,
  ):
      """Align reads to a given haplotype, not necessarily the reference.
  
      Align reads to a graph of haplotypes, reporting the alignments relative
      to a specific haplotype. This allows treating any alternate allele as
      the reference.
  
      Args:
        this_haplotype: string. Sequence of the haplotype to treat as reference,
          reporting alignments according to its coordinates.
        haplotypes: list of strings. All haplotypes to use in the graph, including
          this_haplotype.
        prefix: string. Sequence to the left of where the haplotypes differ.
        suffix: string. Sequence to the right of where the haplotypes differ.
        reads: reads to align.
        contig: string. Name of the 'reference' to report in read alignments.
        ref_start: integer. Start position of the region to report in read
          alignments. This should mark the beginning of the prefix sequence.
  
      Returns:
        Reads. Realigned and reported relative to the chosen haplotype.
      """
      if not reads:
          return []
      fast_pass_realigner = fast_pass_aligner.FastPassAligner()
      aln_config = self.config.aln_config
      aln_config.read_size = len(reads[0].aligned_sequence)
      aln_config.force_alignment = True
      fast_pass_realigner.set_options(aln_config)
      fast_pass_realigner.set_reference(
          prefix + this_haplotype + suffix
      )
      fast_pass_realigner.set_ref_start(contig, ref_start)
  
      # Testing found that when the prefix and suffix both go right up to the
      # ref/alt variants, the alignment does not work well, so a margin of 100
      # bases on each side of the variant are used here to pad each
      # haplotype with enough sequence to align against. While some further
      # testing showed this could be reduced, 100 is the only value that has been
      # tested with a full training experiment.
      central_allele_margin = min(len(prefix), len(suffix), 100)
      fast_pass_realigner.set_ref_prefix_len(
          len(prefix) - central_allele_margin
      )
      fast_pass_realigner.set_ref_suffix_len(
          len(suffix) - central_allele_margin
      )
      extended_haplotypes = [
          prefix + target + suffix for target in haplotypes
      ]
      fast_pass_realigner.set_haplotypes(extended_haplotypes)
      return fast_pass_realigner.realign_reads(reads)
  
imports: import csv
  import os
  import os.path
  from absl import flags
  from etils import epath
  from deepvariant.protos import realigner_pb2
  from deepvariant.realigner import window_selector
  from deepvariant.realigner.python import debruijn_graph
  from deepvariant.realigner.python import fast_pass_aligner
  from deepvariant.vendor import timer
  from google.protobuf import text_format
  from third_party.nucleus.io import sam
  from third_party.nucleus.protos import cigar_pb2
  from third_party.nucleus.protos import reads_pb2
  from third_party.nucleus.util import cigar
  from third_party.nucleus.util import ranges
  from third_party.nucleus.util import utils
  from third_party.nucleus.util.cigar import READ_ADVANCING_OPS
  from third_party.nucleus.util.cigar import REF_ADVANCING_OPS
label_desc: Use image node_id 5 for calling the Realigner obj's underlying member method code with example usage: obj.align_to_haplotype(this_haplotype, haplotypes, prefix, suffix, reads, contig, ref_start) and returns: fast_pass_realigner, list