from typing import OrderedDict
import itertools


def group_samples(samples, grouping="sample"):
    """Group samples by sample or sample run.

    :param samples: list of :class:`ISample <ratatosk.experiment.ISample>` objects
    :param grouping: what to group by

    :returns: dictionary of grouped items
    """
    groups = OrderedDict()
    if grouping == "sample":
        for k,g in itertools.groupby(samples, key=lambda x:x.sample_id()):
            groups[k] = list(g)
    elif grouping == "samplerun":
        for k,g in itertools.groupby(samples, key=lambda x:x.prefix("samplerun")):
            groups[k] = list(g)
    return groups