Rename genome and slugify headers

    Rename genomes according to a simple naming scheme; this
    is mainly done to avoid special character weirdness.

    Parameters
    ----------
    genome_in : file, str or pathlib.Path
        The input genome to be renamed and slugify.
    genome_out : file, str or pathlib.Path
        The output genome to be written into. Defaults is <base>_renamed.fa,
        where <base> is genome_in without its extension.

def rename_genome(genome_in, genome_out=None):
    """Rename genome and slugify headers

    Rename genomes according to a simple naming scheme; this
    is mainly done to avoid special character weirdness.

    Parameters
    ----------
    genome_in : file, str or pathlib.Path
        The input genome to be renamed and slugify.
    genome_out : file, str or pathlib.Path
        The output genome to be written into. Defaults is <base>_renamed.fa,
        where <base> is genome_in without its extension.
    """

    if genome_out is None:
        genome_out = "{}_renamed.fa".format(genome_in.split(".")[0])

    with open(genome_out, "w") as output_handle:
        for record in SeqIO.parse(genome_in, "fasta"):

            # Replace hyphens, tabs and whitespace with underscores
            new_record_id = record.id.replace(" ", "_")
            new_record_id = new_record_id.replace("-", "_")
            new_record_id = new_record_id.replace("\t", "_")

            # Remove anything that's weird, i.e. not alphanumeric
            # or an underscore
            new_record_id = re.sub("[^_A-Za-z0-9]+", "", new_record_id)
            header = ">{}\n".format(new_record_id)

            output_handle.write(header)
            output_handle.write("{}\n".format(str(record.seq)))