import struct


def get_ref_index(alignment: bytes) -> int:
    """
    Extract the reference index from a BAM alignment

    Parameters
    ----------
    alignment : bytes
        A byte string of a bam alignment entry in raw binary format

    Returns
    -------
    int
        The zero based rank of the reference in the BAM header

    Notes
    -----
    The index can be converted to the reference name with
    pylazybam.bam.FileReader().index_to_ref[index]
    """
    return struct.unpack("<i", alignment[4:8])[0]