import json


def json_to_fns_and_samples(fn):
    """
    Input json must contain a list of tuples [(a0, b0), (a1, b1), ...], where (a, b) represents a filename `a`
    and its sample tag `b`.
    return ([a0, a1, ...], [b0, b1, ...])

    fn -- json file containing a list of tuples, e.g., [(a0, b0), ...,]
    ...doctest:
        >>> fn = op.join(op.dirname(op.dirname(op.dirname(__file__))), 'tests', 'out', 'fn2sample.json')
        >>> d = [['align_m.bam', 'm'], ['align_f.bam', 'f'], ['align_p.bam', 'p']]
        >>> json.dump(d, open(fn, 'w'))
        >>> json_to_fns_and_samples(fn)
        (['align_m.bam', 'align_f.bam', 'align_p.bam'], ['m', 'f', 'p'])
    """
    tuples = json.load(open(fn, 'r')) # load [(a0, b0), ..., ]
    try:
        return [str(t[0]) for t in tuples], [str(t[1]) for t in tuples]
    except IndexError as e:
        raise ValueError('Json file must only contain a list of (filename, sample) tuples: %s, %s' % (fn, str(e)))