code: def calc_paired_insert_stats(in_bam, nsample=1000000):
      
      dists = []
      n = 0
      with pysam.Samfile(in_bam, "rb") as in_pysam:
          for read in in_pysam:
              if read.is_proper_pair and read.is_read1:
                  n += 1
                  dists.append(abs(read.isize))
                  if n >= nsample:
                      break
      return insert_size_stats(dists)
docs: Retrieve statistics for paired end read insert distances.