code: def estimate_read_length(bam_file, nreads=1000):
      
      with open_samfile(bam_file) as bam_handle:
          reads = tz.itertoolz.take(nreads, bam_handle)
          lengths = [len(x.seq) for x in reads]
      return int(numpy.median(lengths))
docs: estimate median read length of a SAM/BAM file