from typing import Tuple
from typing import List
import re


def parse_mate_info_from_id(
    seq_id: str
) -> Tuple[int, str]:
    """Extracts mate information from sequence identifier.

    Args:
        seq_id: Sequence identifier.

    Returns:
        An integer representing the mate information and the sequence
            identifier minus the mate information, if it could be extracted,
            or the original sequence identifier otherwise.
            The following mate information integers may be observed:
                0: Mate information could not be extracted
                1: First mate
                2: Second mate
    """
    # Store sequence ID conventions and index of correspoding capture group
    conventions: List[re.Pattern] = []
    # Illumina Casava >=1.8
    # example EAS139:136:FC706VJ:2:2104:15343:197393 1:Y:18:ATCACG
    # https://support.illumina.com/help/BaseSpace_OLH_009008/Content/Source/Informatics/BS/FileFormat_FASTQ-files_swBS.htm
    # https://en.wikipedia.org/wiki/FASTQ_format#Illumina_sequence_identifiers
    conventions.append(
        re.compile(
            r'(?P<prefix>\w+:\d+:\w+:\d+:\d+:\d+:\d+(:[ACGTN]\+[ACGTN])? )'
            r'(?P<mate>[12])'
            r'(?P<suffix>:[YN]:\d*[02468]:([ACGTN]|\d)+)'
        )
    )
    # Illumina Casava <1.8
    # example: HWUSI-EAS100R:6:73:941:1973#0/1
    conventions.append(
        re.compile(
            r'(?P<prefix>[\w-]+:\d+:\d+:\d+:\d+#([ACGTN|\d])+/)'
            r'(?P<mate>[12])'
            r'(?P<suffix>)'
        )
    )

    # Scan for regexes and return mate information
    for regex in conventions:
        match = re.search(regex, seq_id)
        if match:
            mate_info = int(match.group('mate'))
            return (
                mate_info,
                ''.join([
                    match.group('prefix'),
                    match.group('suffix'),
                ])
            )

    # Return value if no regex matched
    return (0, seq_id)