code: def trim_read(read, region):
      """Trim a read down to the part that aligns within a given region.
  
      The following properties of the read are updated, trimming on both sides as
      necessary to save only the parts of the read that fit fully within the
      region, potentially starting and ending at the region's boundaries:
      - The alignment position (read.alignment.position.position).
      - The read sequence (read.aligned_sequence).
      - Base qualities (read.aligned_quality).
      - The cigar string of the alignment (read.alignment.cigar)
  
      Args:
        read: A `nucleus.protos.Read` that is aligned to the region.
        region: A `nucleus.protos.Range` region.
  
      Returns:
        a new `nucleus.protos.Read` trimmed to the region.
      """
      if not read.alignment:
          raise ValueError("Read must already be aligned.")
  
      read_start = read.alignment.position.position
  
      trim_left = max(region.start - read_start, 0)
  
      ref_length = region.end - max(region.start, read_start)
      new_cigar, read_trim, new_read_length = trim_cigar(
          read.alignment.cigar, trim_left, ref_length
      )
  
      # Copy everything but aligned_sequence and aligned_quality fields of the read
      # to get all recursive properties and prevent mutating the original.
      new_read = reads_pb2.Read()
      new_read.fragment_name = read.fragment_name
      new_read.id = read.id
      new_read.read_group_id = read.read_group_id
      new_read.read_group_set_id = read.read_group_set_id
      new_read.read_number = read.read_number
      new_read.fragment_length = read.fragment_length
      new_read.number_reads = read.number_reads
      for each_info_key in read.info:
          new_read.info[each_info_key].CopyFrom(
              read.info[each_info_key]
          )
      new_read.alignment.position.position = (
          read.alignment.position.position
      )
      new_read.alignment.position.reference_name = (
          read.alignment.position.reference_name
      )
      new_read.alignment.position.reverse_strand = (
          read.alignment.position.reverse_strand
      )
      new_read.alignment.mapping_quality = (
          read.alignment.mapping_quality
      )
      # Following fields are not needed but we copy them for consistency:
      new_read.next_mate_position.CopyFrom(read.next_mate_position)
      new_read.proper_placement = read.proper_placement
      new_read.duplicate_fragment = read.duplicate_fragment
      new_read.failed_vendor_quality_checks = (
          read.failed_vendor_quality_checks
      )
      new_read.secondary_alignment = read.secondary_alignment
      new_read.supplementary_alignment = read.supplementary_alignment
  
      if trim_left != 0:
          new_read.alignment.position.position = region.start
      # Set aligned_sequence, a string:
      new_read.aligned_sequence = read.aligned_sequence[
          read_trim : read_trim + new_read_length
      ]
      # Set aligned_quality, a repeated integer:
      new_read.aligned_quality[:] = read.aligned_quality[
          read_trim : read_trim + new_read_length
      ]
  
      # Direct assignment on a repeated message field is not allowed, so setting
      # the cigar by using 'extend'.
      new_read.alignment.cigar.extend(new_cigar)
  
      return new_read
  
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 7 for calling a global function with example usage: trim_read(read, region) and returns: new_read