def overlap_len(range1, range2):
  """Computes the number of overlapping bases of range1 and range2.

  Args:
    range1: nucleus.genomics.v1.Range.
    range2: nucleus.genomics.v1.Range.

  Returns:
    int. The number of basepairs in common. 0 if the ranges are not on the same
    contig.
  """
  if range1.reference_name != range2.reference_name:
    return 0
  return max(0, (min(range1.end, range2.end) - max(range1.start, range2.start)))