code: def regionsIntersection(s1, s2, collapse=True):
    debug = False
    s1_c = collapseRegions(s1)
    s2_c = collapseRegions(s2)
    if len(s1_c) == 0 or len(s2_c) == 0:
      return []
    res = []
    j = 0
    for i in range(0, len(s1_c)):
      if debug:
        sys.stderr.write("processing from s1_c : " + str(s1_c[i]) + "\n")
      if debug:
        sys.stderr.write("i = " + str(i) + " and j = " + str(j) + "\n")
      while (j < len(s2_c) and
             (s2_c[j].chrom < s1_c[i].chrom or
             (s2_c[j].chrom == s1_c[i].chrom and s2_c[j].end <= s1_c[i].start))):
        j += 1
      if j >= len(s2_c) or s2_c[j].chrom > s1_c[i].chrom or \
         (s2_c[j].chrom == s1_c[i].chrom and s2_c[j].start >= s1_c[i].end):
        continue
      while s2_c[j].start < s1_c[i].end:
        s = max(s1_c[i].start, s2_c[j].start)
        e = min(s1_c[i].end, s2_c[j].end)
        overlap = GenomicInterval(s1_c[i].chrom, s, e, "X", 0, "+")
        if debug:
          sys.stderr.write("\tadding to overlaps: " + str(overlap) + "\n")
        res.append(overlap)
        j += 1
        if j >= len(s2_c) or s2_c[j].chrom != s1_c[i].chrom:
          break
      j -= 1
      if debug:
        sys.stderr.write("\tmoving s2_c index back to " + str(s2_c[j]) + "\n")
    return res