Find the number of nucleotides covered at all positions in a bed or vcf
    file.

    Parameters
    ----------
    bam : str or pysam.calignmentfile.AlignmentFile 
        Bam file opened with pysam or path to bam file (must
        be sorted and indexed).
    
    positions : str or pybedtools.BedTool
        Path to bed or vcf file or pybedtools.BedTool object. The extension is
        used to determine whether the file is a bed or vcf (.bed vs .vcf).

    stranded : boolean
        Boolean indicating whether read data is stranded and stranded nucleotide
        counts should be returned. Assumes R1 read on reverse strand implies +
        strand coverage etc.

    vcf : boolean
        Set to True if you are providing a vcf file that doesn't have a .vcf
        suffix.

    bed : boolean
        Set to True if you are providing a bed file that doesn't have a .bed
        suffix.

    Returns
    -------
    counts : pandas.DataFrame
        Data frame with the counts for each base in the region. The index of
        this data frame is one-based for compatibility with VCF files.

def nt_counts(bam, positions, stranded=False, vcf=False, bed=False):
    """
    Find the number of nucleotides covered at all positions in a bed or vcf
    file.

    Parameters
    ----------
    bam : str or pysam.calignmentfile.AlignmentFile 
        Bam file opened with pysam or path to bam file (must
        be sorted and indexed).
    
    positions : str or pybedtools.BedTool
        Path to bed or vcf file or pybedtools.BedTool object. The extension is
        used to determine whether the file is a bed or vcf (.bed vs .vcf).

    stranded : boolean
        Boolean indicating whether read data is stranded and stranded nucleotide
        counts should be returned. Assumes R1 read on reverse strand implies +
        strand coverage etc.

    vcf : boolean
        Set to True if you are providing a vcf file that doesn't have a .vcf
        suffix.

    bed : boolean
        Set to True if you are providing a bed file that doesn't have a .bed
        suffix.

    Returns
    -------
    counts : pandas.DataFrame
        Data frame with the counts for each base in the region. The index of
        this data frame is one-based for compatibility with VCF files.

    """
    if not bed and not vcf:
        if type(positions) == pbt.bedtool.BedTool:
            df = positions.to_dataframe()
        elif positions[-4:] == '.bed':
            bed = True
        elif positions[-4:] == '.vcf':
            vcf = True
        else:
            sys.stderr.write('Positions must be BedTool, bed file, or vcf '
                             'file.\n')

    if bed:
        df = pbt.BedTool(positions).to_dataframe()
    elif vcf:
        from variants import vcf_as_df
        tdf = vcf_as_df(positions)
        df = pd.DataFrame(index=tdf.index)
        df['chrom'] = tdf.CHROM
        df['start'] = tdf.POS - 1
        df['end'] = tdf.POS

    res = []
    for i in df.index:
        region = [df.ix[i, 'chrom'], df.ix[i, 'start'], df.ix[i, 'end']]
        res.append(get_region_nt_counts(region, bam, stranded))
    res = pd.concat(res)
    return res