def infer_format(filename):
    """
    Infer the genomic file format from the filename.  First known formats are checked.  Next presenece of
    the magic string "refgene" in the filename is checked for UCSC refgene files.  This is a legacy
    IGV convention.  The order is important, a recognized extension wins.
    NOTE: Formats are for output data uris.  CRAM format is converted to BAM before output.
    :param filename:
    :return:
    """
    filename = filename.lower()
    if filename.endswith('.gz'):
        filename = filename[:-3]
    if filename.endswith('.bam'):
        return 'bam'
    if filename.endswith('.cram'):
        return 'bam'
    elif filename.endswith('.vcf'):
        return 'vcf'
    elif filename.endswith('.bed'):
        return 'bed'
    elif filename.endswith('.gff') or filename.endswith('.gff3'):
        return 'gff'
    elif filename.endswith('.gtf'):
        return 'gtf'
    elif filename.find('refgene'):
        return 'refgene'
    else:
        idx = filename.rfind('.')
        if idx > 0:
            return filename[idx + 1:]
        else:
            return None
        idx = filename.rfind('.')
        if idx > 0:
            return filename[idx + 1:]
        else:
            return None
