import re


def parse_sample(line):
    """Parse a line in sample list file.

    Format: num name scan1,scan2,...
    """
    p = re.compile(r'(\d+)\s+(\w+)\s+([\w,]+)')
    m = p.match(line)
    if m is None:
        raise ValueError(f'Invalid line in samplelist file: {line}')
    case, name, scans = m.groups()
    case = int(case)
    name = name.lower()
    scans = tuple(sorted(scans.lower().split(',')))
    return dict(case=case, name=name, scans=scans)