# Path: ratatosk/report/picard.py
# class PicardMetricsCollection(object):
#     def __init__(self, mlist):
#         self._metrics = []
#         self._mlist = mlist
#         self._collect_metrics()
# 
#     def _collect_metrics(self):
#         for (sid, fn) in self._mlist:
#             pm = PicardMetrics(sid, fn)
#             self._metrics.append(pm)
#             
#     def metrics(self, as_csv=False):
#         return [pm.metrics(as_csv=as_csv) for pm in self._metrics]
# 
#     def hist(self, as_csv=False):
#         return [pm.hist(as_csv=as_csv) for pm in self._metrics]
# 
#     def idlist(self):
#         return [pm.id() for pm in self._metrics]


import os
import glob
import itertools
from collections import OrderedDict
from ratatosk.report.picard import PicardMetricsCollection
    :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

def collect_metrics(grouped_samples, projroot, tgtdir, ext, grouping="sample"):
    """Collect metrics for a collection of samples.

    :param grouped_samples: samples grouped in some way
    :param projroot: project root directory
    :param tgtdir: documentation target directory 
    :param ext: metrics extension to search for
    :param grouping: what grouping to use

    :returns: list of (item_id, metrics file name)
    """
    metrics = []
    for item_id, itemlist in grouped_samples.items():
        item = itemlist[0]
        # FIXME: tgtdir should be docroot!
        pfx = os.path.relpath(itemlist[0].prefix(grouping), os.path.dirname(tgtdir))
        mfile = glob.glob(pfx + ".*" + ext)
        if mfile:
            metrics.append((item_id, mfile[0]))

# Path: ratatosk/report/picard.py
# class PicardMetricsCollection(object):
#     def __init__(self, mlist):
#         self._metrics = []
#         self._mlist = mlist
#         self._collect_metrics()
# 
#     def _collect_metrics(self):
#         for (sid, fn) in self._mlist:
#             pm = PicardMetrics(sid, fn)
#             self._metrics.append(pm)
#             
#     def metrics(self, as_csv=False):
#         return [pm.metrics(as_csv=as_csv) for pm in self._metrics]
# 
#     def hist(self, as_csv=False):
#         return [pm.hist(as_csv=as_csv) for pm in self._metrics]
# 
#     def idlist(self):
#         return [pm.id() for pm in self._metrics]


# Path: ratatosk/report/utils.py
import os
import glob
import itertools
from collections import OrderedDict
from ratatosk.report.picard import PicardMetricsCollection

    :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

def collect_metrics(grouped_samples, projroot, tgtdir, ext, grouping="sample"):
    """Collect metrics for a collection of samples.

    :param grouped_samples: samples grouped in some way
    :param projroot: project root directory
    :param tgtdir: documentation target directory 
    :param ext: metrics extension to search for
    :param grouping: what grouping to use

    :returns: list of (item_id, metrics file name)
    """
    metrics = []
    for item_id, itemlist in grouped_samples.items():
        item = itemlist[0]
        # FIXME: tgtdir should be docroot!
        pfx = os.path.relpath(itemlist[0].prefix(grouping), os.path.dirname(tgtdir))
        mfile = glob.glob(pfx + ".*" + ext)
        if mfile:
            metrics.append((item_id, mfile[0]))

    return PicardMetricsCollection(metrics)