#! /usr/bin/env python

import pysam
import numpy as np
import h5py
import click
import re
import os.path

def gen_outfile_name(out, bam):
    if out:
        return out
    bam = os.path.basename(bam)
    if bam.endswith('.bam'):
        return re.sub(r'bam$', 'h5', bam)
    return bam + '.h5'

def gen_sample_name(smpl, bam):
    if smpl:
        return smpl
    bam = os.path.basename(bam)
    return re.sub(r'\.bam$', '', bam)

def bam_to_h5(bamfile,
              sample=None,
              chrom=None,
              compression=None,
              outfile=None):
    out_fn = gen_outfile_name(outfile, bamfile)
    sample_name = gen_sample_name(sample, bamfile)
    f_h5 = h5py.File(out_fn)
    f_h5.attrs['sample'] = sample_name

@click.command()
@click.option('-s', '--sample', help='sample name')
@click.option('-c', '--chrom', multiple=True, help='chromosome to process')
@click.option('-d', '--compression', type=click.Choice(['gzip', 'lzf']),
              help='HDF5 file compression')
@click.option('-o', '--outfile', help='output HDF5 file name')
@click.argument('bamfile')
def cli(bamfile, sample, chrom, compression, outfile):
    """Convert BAM to coverage HDF5."""
    bam_to_h5(bamfile, sample, chrom, compression, outfile)

if __name__ == '__main__':
    cli()

@@ -100,16 +100,87 @@
 s.path%0A%0A
+NUM_BINS_LIST = %5B10000, 100000%5D%0Amedian_vec = np.vectorize(np.median)%0A%0A%0A
 def gen_
@@ -361,16 +361,17 @@
  '.h5'%0A%0A
+%0A
 def gen_
@@ -498,16 +498,202 @@
 , bam)%0A%0A
+%0Adef set_dset_attrs(dset, arr):%0A    dset.attrs%5B'min'%5D = np.min(arr)%0A    dset.attrs%5B'max'%5D = np.max(arr)%0A    dset.attrs%5B'mean'%5D = np.mean(arr)%0A    dset.attrs%5B'median'%5D = np.median(arr)%0A%0A%0A
 def bam_
@@ -753,16 +753,22 @@
    chrom
+s_keep
 =None,%0A 
@@ -922,24 +922,25 @@
 e, bamfile)%0A
+%0A
     f_h5 = h
@@ -996,16 +996,941 @@
 e_name%0A%0A
+    f_bam = pysam.Samfile(bamfile)%0A%0A    for sq in f_bam.header%5B'SQ'%5D:%0A        chrom = sq%5B'SN'%5D%0A        if chroms_keep is not None and chrom not in chroms_keep:%0A            continue%0A        click.echo('processing ' + chrom)%0A%0A        chrom_len = sq%5B'LN'%5D%0A        f_h5.create_group(chrom)%0A        f_h5%5Bchrom%5D.attrs%5B'length'%5D = np.uint32(chrom_len)%0A        cnts = np.zeros(chrom_len, dtype='uint16')%0A%0A        for pile in f_bam.pileup(chrom):%0A            cnts%5Bpile.pos%5D = pile.n%0A%0A        kwargs = %7B%7D%0A        if compression:%0A            kwargs%5B'compression'%5D = compression%0A%0A        for nbins in NUM_BINS_LIST:%0A            medians = median_vec(np.array_split(cnts, nbins))%0A            medians = np.array(medians, dtype=np.uint16)%0A            kwargs%5B'data'%5D = medians%0A            f_h5.create_dataset('%7B%7D/%7B%7D'.format(chrom, nbins), **kwargs)%0A            set_dset_attrs(f_h5%5B'%7B%7D/%7B%7D'.format(chrom, nbins)%5D, medians)%0A%0A    f_bam.close()%0A%0A%0A
 @click.c
@@ -2368,16 +2368,64 @@
 DF5.%22%22%22%0A
+    chroms_keep = set(chrom) if chrom else None%0A
     bam_
@@ -2444,32 +2444,38 @@
 e, sample, chrom
+s_keep
 , compression, o

Fix variable name typo