def to_bigbed(df, chromsizes, outpath, schema='bed6'):
    """
    Save a bedGraph-like dataframe as a binary BigWig track.

    Parameters
    ----------
    df : pandas.DataFrame
        Data frame with columns 'chrom', 'start', 'end' and one or more value
        columns
    chromsizes : pandas.Series
        Series indexed by chromosome name mapping to their lengths in bp
    outpath : str
        The output BigWig file path
    value_field : str, optional
        Select the column label of the data frame to generate the track. Default
        is to use the fourth column.

    """
    import tempfile
    import subprocess
    is_bed6 = True
    for col in ['chrom', 'start', 'end', 'name', 'score', 'strand']:
        if col not in df.columns:
            is_bed6 = False
    if len(df.columns) < 6:
        is_bed6 = False
    if not is_bed6:
        raise ValueError('A bed6-like DataFrame is required, got {}'.format
            (df.columns))
    columns = ['chrom', 'start', 'end', 'name', 'score', 'strand']
    bed = df[columns].copy()
    bed['chrom'] = bed['chrom'].astype(str)
    bed = bed.sort_values(['chrom', 'start', 'end'])
    with tempfile.NamedTemporaryFile(suffix='.bed'
        ) as f, tempfile.NamedTemporaryFile('wt', suffix='.chrom.sizes') as cs:
        chromsizes.to_csv(cs, sep='\t', header=False)
        cs.flush()
        bed.to_csv(f.name, sep='\t', columns=columns, index=False, header=
            False, na_rep='nan')
        p = subprocess.run(['bedToBigBed', '-type={}'.format(schema), f.
            name, cs.name, outpath], stdout=subprocess.PIPE, stderr=
            subprocess.PIPE)
    return p
