code: def trim_cigar(cigar, ref_trim, ref_length):
      """Trim a cigar string to a certain reference length.
  
      Args:
        cigar: list of `nucleus.protos.CigarUnit`s of the original read alignment.
        ref_trim: integer. Number of reference bases to trim off the beginning of
          the read.
        ref_length: integer. Number of reference bases to cover with the read, the
          middle part that is not trimmed from the start or end of the read.
  
      Returns:
        new_cigar: list of `nucleus.protos.CigarUnit`s of the final read alignment,
            after the left and/or right have been trimmed off.
        read_trim: The number of bases of the read that are trimmed off.
        new_read_length: The number of bases of the read that remain after trimming.
            This is different from the final number of reference bases; for example,
            an insertion makes the read longer without affecting the reference.
      """
      # First consume the ref until the trim is covered.
      trim_remaining = ref_trim
      # Then consume the ref until the ref_length is covered.
      ref_to_cover_remaining = ref_length
      read_trim = 0
      new_cigar = []
      new_read_length = 0
      for cigar_unit in cigar:
          c_operation_length = cigar_unit.operation_length
          # Each operation moves forward in the ref, the read, or both.
          advances_ref = (
              cigar_unit.operation in cigar_utils.REF_ADVANCING_OPS
          )
          advances_read = (
              cigar_unit.operation in cigar_utils.READ_ADVANCING_OPS
          )
          ref_step = c_operation_length if advances_ref else 0
          # First, use up each operation until the trimmed area is covered.
          if trim_remaining > 0:
              if ref_step <= trim_remaining:
                  # Fully apply to the trim.
                  trim_remaining -= ref_step
                  read_trim += (
                      c_operation_length if advances_read else 0
                  )
                  continue
              else:
                  # Partially apply to finish the trim.
                  ref_step -= trim_remaining
                  read_trim += trim_remaining if advances_read else 0
                  # If trim finishes here, the rest of the ref_step can apply to the
                  # next stage and count towards covering the given ref window.
                  c_operation_length = ref_step
                  trim_remaining = 0
  
          # Once the trim is done, start applying cigar entries to covering the ref
          # window.
          if trim_remaining == 0:
              if ref_step <= ref_to_cover_remaining:
                  # Fully apply to the window.
                  new_cigar.append(
                      cigar_pb2.CigarUnit(
                          operation=cigar_unit.operation,
                          operation_length=c_operation_length,
                      )
                  )
                  ref_to_cover_remaining -= ref_step
                  new_read_length += (
                      c_operation_length if advances_read else 0
                  )
              else:
                  # Partially apply to finish the window.
                  c_operation_length = ref_to_cover_remaining
                  new_cigar.append(
                      cigar_pb2.CigarUnit(
                          operation=cigar_unit.operation,
                          operation_length=c_operation_length,
                      )
                  )
                  new_read_length += (
                      c_operation_length if advances_read else 0
                  )
                  ref_to_cover_remaining = 0
                  break
  
      return new_cigar, read_trim, new_read_length
  
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 6 for calling a global function with example usage: trim_cigar(cigar, ref_trim, ref_length) and returns: new_cigar,  read_trim,  new_read_length