from pathlib import Path
from typing import Tuple
import re


def parse_samtools_flagstat(p: Path) -> Tuple[int, int]:
    """Parse total and mapped number of reads from Samtools flagstat file"""
    total = 0
    mapped = 0
    with open(p) as fh:
        for line in fh:
            m = re.match(r'(\d+)', line)
            if m:
                if 'in total' in line:
                    total = int(m.group(1))
                if ' mapped (' in line:
                    mapped = int(m.group(1))
    return total, mapped