import re


def extract_filestem(data):
    """Extract filestem from Entrez eSummary data.

    :param data:  Entrez eSummary

    Function expects esummary['DocumentSummarySet']['DocumentSummary'][0]

    Some illegal characters may occur in AssemblyName - for these, a more
    robust regex replace/escape may be required. Sadly, NCBI don't just
    use standard percent escapes, but instead replace certain
    characters with underscores: white space, slash, comma, hash, brackets.
    """
    escapes = re.compile(r"[\s/,#\(\)]")
    escname = re.sub(escapes, "_", data["AssemblyName"])
    return "_".join([data["AssemblyAccession"], escname])