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: Run realigner.
  
  This is the main function that
    - parses the input reads and reference sequence.
    - select candidate windows for local assembly (WindowSelector (ws)
      module).
      - Windows larger than max_window_size are skipped.
    - build pruned De-Bruijn graph for each candidate window (DeBruijnGraph
      (dbg) module).
      - Graphs with more than max_num_paths candidate haplotypes or
        with reference sequence as the only candidate are skipped.
    - Align reads based on candidate haplotypes (Aligner (aln) module).
    - Output all input reads (whether they required realignment or not).
  
  Args:
    reads: [`third_party.nucleus.protos.Read` protos]. The list of input reads
      to realign.
    region: A `third_party.nucleus.protos.Range` proto. Specifies the region
      on the genome we should process.
  
  Returns:
    [realigner_pb2.CandidateHaplotypes]. Information on the list of candidate
      haplotypes.
    [`third_party.nucleus.protos.Read` protos]. The realigned
      reads for the region. NOTE THESE READS MAY NO LONGER BE IN THE SAME
      ORDER AS BEFORE.
docstr_tok: ["Run","realigner",".","This","is","the","main","function","that","-","parses","the","input","reads","and","reference","sequence",".","-","select","candidate","windows","for","local","assembly","(","WindowSelector","(","ws",")","module",")",".","-","Windows","larger","than","max_window_size","are","skipped",".","-","build","pruned","De-Bruijn","graph","for","each","candidate","window","(","DeBruijnGraph","(","dbg",")","module",")",".","-","Graphs","with","more","than","max_num_paths","candidate","haplotypes","or","with","reference","sequence","as","the","only","candidate","are","skipped",".","-","Align","reads","based","on","candidate","haplotypes","(","Aligner","(","aln",")","module",")",".","-","Output","all","input","reads","(","whether","they","required","realignment","or","not",")",".","Args",":","reads",":","[","`","third_party.nucleus.protos.Read","`","protos","]",".","The","list","of","input","reads","to","realign",".","region",":","A","`","third_party.nucleus.protos.Range","`","proto",".","Specifies","the","region","on","the","genome","we","should","process",".","Returns",":","[","realigner_pb2.CandidateHaplotypes","]",".","Information","on","the","list","of","candidate","haplotypes",".","[","`","third_party.nucleus.protos.Read","`","protos","]",".","The","realigned","reads","for","the","region",".","NOTE","THESE","READS","MAY","NO","LONGER","BE","IN","THE","SAME","ORDER","AS","BEFORE","."]
code: def realign_reads(self, reads, region):
      """Run realigner.
  
      This is the main function that
        - parses the input reads and reference sequence.
        - select candidate windows for local assembly (WindowSelector (ws)
          module).
          - Windows larger than max_window_size are skipped.
        - build pruned De-Bruijn graph for each candidate window (DeBruijnGraph
          (dbg) module).
          - Graphs with more than max_num_paths candidate haplotypes or
            with reference sequence as the only candidate are skipped.
        - Align reads based on candidate haplotypes (Aligner (aln) module).
        - Output all input reads (whether they required realignment or not).
  
      Args:
        reads: [`third_party.nucleus.protos.Read` protos]. The list of input reads
          to realign.
        region: A `third_party.nucleus.protos.Range` proto. Specifies the region
          on the genome we should process.
  
      Returns:
        [realigner_pb2.CandidateHaplotypes]. Information on the list of candidate
          haplotypes.
        [`third_party.nucleus.protos.Read` protos]. The realigned
          reads for the region. NOTE THESE READS MAY NO LONGER BE IN THE SAME
          ORDER AS BEFORE.
      """
  
      # Split reads containing SKIP operations.
      if self.config.split_skip_reads:
          reads = split_reads(reads)
  
      # Compute the windows where we need to assemble in the region.
      candidate_windows = window_selector.select_windows(
          self.config.ws_config, self.ref_reader, reads, region
      )
  
      # Assemble each of those regions.
      candidate_haplotypes = self.call_debruijn_graph(
          candidate_windows, reads
      )
      # Create our simple container to store candidate / read mappings.
      assembled_regions = [
          AssemblyRegion(ch) for ch in candidate_haplotypes
      ]
  
      # Our realigned_reads start off with all of the unassigned reads.
      realigned_reads = assign_reads_to_assembled_regions(
          assembled_regions, reads
      )
  
      if not _USE_FAST_PASS_ALIGNER.value:
          raise ValueError(
              "--use_fast_pass_aligner is always true. "
              "The older implementation is deprecated and removed."
          )
      # Walk over each region and align the reads in that region, adding them to
      # our realigned_reads.
      for assembled_region in assembled_regions:
          realigned_reads_copy = self.call_fast_pass_aligner(
              assembled_region
          )
          realigned_reads.extend(realigned_reads_copy)
  
      self.diagnostic_logger.log_realigned_reads(
          region, realigned_reads, self.shared_header
      )
  
      return candidate_haplotypes, realigned_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 4 for calling the Realigner obj's underlying member method code with example usage: obj.realign_reads(reads, region) and returns: candidate_haplotypes,  realigned_reads