import re


def parse_reference_genome_assembly_stats(path_to_reference_genome_assembly_stats):
    """
    Args:
        path_to_reference_genome_stats (str): Path to

    Returns:
        dict: genome stats
        For example:
        {
          'organism_name': 'Escherichia coli (E. coli)',
          'infraspecific_name': 'strain=GN02215',
          'refseq_assembly_accession': 'GCF_001519515.1',
          'taxid': '562',
          'total_length': 5185840,
          'contig_count': 219,
          'contig_N50': 52782
        }
    """
    reference_genome_assembly_stats = {
        'organism_name': '',
        'infraspecific_name': '',
        'refseq_assembly_accession': '',
        'taxid': '',
        'total_length': None,
        'contig_count': None,
        'contig_N50': None,
    }
    header_fields = [
        "Organism name",
        "Infraspecific name",
        "RefSeq assembly accession",
        "Taxid",
    ]
    body_fields = [
        'total-length',
        'contig-count',
        'contig-N50',
    ]
    with open(path_to_reference_genome_assembly_stats, 'r') as f:
        for line in f:
            for field in header_fields:
                if re.match("^# " + field + ":", line):
                    reference_genome_assembly_stats[field.lower().replace(" ", "_")] = re.split(
                        "# " + field + ":\s+", line.strip()
                    )[-1]
            for field in body_fields:
                if re.match("^all\tall\tall\tall\t" + field, line):
                    reference_genome_assembly_stats[field.replace('-', '_')] = int(
                        line.strip().split('\t')[-1].strip()
                    )
    return reference_genome_assembly_stats