import re


def generate_genome_paths(df, genome_paths, taxdb, taxonomic_rank):
    """Function to generate a genome paths file compatible with aMGSIM.

    Args:
        df (pandas.DataFrame): A dataframe with the genome information.
        genome_paths (pandas.DataFrame): A dataframe with the path information.
        taxdb (dict): A dictionary with the taxonomic information
        taxonomic_rank (str): Selected taxonomic scope

    Returns:
        pandas.DataFrame: A dataframe with the genome paths files
    """
    # Taxon	Accession	Fasta
    df_selected = (
        df[["reference", taxonomic_rank]].copy().merge(genome_paths, on="reference")
    )
    df_selected["Taxon"] = df_selected[taxonomic_rank].apply(
        lambda x: re.sub("[^0-9a-zA-Z]+", "_", taxdb.taxid2name[x])
    )
    df_selected = df_selected[["Taxon", taxonomic_rank, "reference", "path"]]
    df_selected.columns = ["Taxon", "TaxId", "Accession", "Fasta"]
    return df_selected