Group a set of sample items by batch (or singleton) name.

    Items in multiple batches cause two batches to be merged together.

def group_by_batch(items, require_bam=True):
    """Group a set of sample items by batch (or singleton) name.

    Items in multiple batches cause two batches to be merged together.
    """
    out = collections.defaultdict(list)
    batch_groups = _get_representative_batch(_merge_batches(_find_all_groups(items, require_bam)))
    for data in items:
        batches = _get_batches(data, require_bam)
        # take first batch as representative
        batch = batch_groups[batches[0]]
        out[batch].append(utils.deepish_copy(data))
    return dict(out)