def merge_by_glycopeptide_sequence(matches):
    groups = collectiontools.groupby(matches.values(), lambda x: x["Glycopeptide_identifier"], kind="sqlist")
    merged_matches = sqlitedict.SqliteDict(matches.filename, tablename="matched_ions")
    cntr = 0
    for glycopeptide, matches in groups.items():
        merged_matches[cntr] = merge_matches(matches)
        cntr += 1
    merged_matches.commit()
    return merged_matches
def merge_ion_matches(matches):
    groups = collectiontools.groupby(itertools.chain.from_iterable(matches),
                                     lambda x: x["key"])
    best_matches = []
    fabs = math.fabs
    for key, matched_key in groups.items():
        best_match = matched_key[0]
        best_ppm = fabs(best_match["ppm_error"])
        ppm_to_scan_id = {best_match["scan_id"]: best_match["ppm_error"]}
        for match in matched_key[1:]:
            ppm_to_scan_id[match["scan_id"]] = match["ppm_error"]
            if fabs(match["ppm_error"]) < best_ppm:
                best_match = match
                best_ppm = fabs(match["ppm_error"])
        best_match["scan_map"] = ppm_to_scan_id
        best_matches.append(best_match)
    return best_matches