import re


def _parse_virsorter_metadata(name):
    """Extract the fasta header metadata. Each fasta sequence has information encoded in the header.
    Virsorter format header has 3 flavors:
    - non-prophage: VIRSorter_seq124-circular-cat_1
                    VIRSorter_seq247-cat_2
    - prophage:     VIRSorter_NODE_306_length_14315_cov_22_151052_gene_1_gene_12-0-9235-cat_5
    Returns seq name (with no virsoter data), circular bool, prophage range as start:end
    """
    name = name.replace("VIRSorter_", "")
    # re to remove all the metadata from the header
    clean_re = r"(?:_gene_\d+_gene_\d+-\d+-\d+)?(?:-circular)?-cat_\d+$"
    clean_name = re.sub(clean_re, "", name)
    # prophage range
    match = re.search(r"\d+-(?P<start>\d+)-(?P<end>\d+)?(?:-circular)?-cat_\d+$", name)
    prange = None
    if match:
        prange = [match.group("start"), match.group("end")]
    return clean_name, "circular" in name, prange