MYYA
这个问题可能更适合基于图形的解决方案。没有任何方法可以防止多个范围以不同的时间间隔重叠。#!/usr/bin/env python3 from pprint import pprintfrom itertools import groupbydef mapper(d, overlap=1000): """Each chromsomal coordinate must be interrogated to determine if it is within +/-overlap of any other Range within any other Original Dictionary Transcript value will match key and chromosome element from the list ------------------------ ---------------------- ---------- (el-overlap, el+overlap), (dict-key, chromosome), el) """ for key, ch in d.items(): for el in ch[1:]: yield ((el-overlap, el+overlap), (key, ch[0]), el)def sorted_mapper(d, overlap=1000): """Simply sort the mapper data by its first element """ for r in sorted(mapper(d, overlap), key=lambda x: x[0]): yield rdef groups(iter_): previous = next(iter_) retval = [previous] for chrm in iter_: if previous[0][0] <= chrm[-1] <= previous[0][1]: retval.append(chrm) else: yield retval previous = chrm retval = [previous] yield retvaldef reduce_phase1(iter_): for l in iter_: retval = {} for (minc, maxc), (key, lbl), chrm in l: x = retval.get(key,[lbl]) x.append(chrm) retval[key] = x yield retvaldef update_dict(d1, d2): retval = d1 for key, value in d2.items(): if key in d1.keys(): retval[key].extend(value[1:]) return retvaldef reduce_phase2(iter_): retval = [next(iter_)] retval_keys = [set([k for k in retval[0].keys()])] for d in iter_: keyset = set([k for k in d.keys()]) isnew = True for i, e in enumerate(retval_keys): if keyset <= e: isnew = False retval[i] = update_dict(retval[i], d) if isnew: retval.append(d) retval_keys.append(keyset) return retvalFirst_dict = {Key1: ['chr10', 19010495, 19014590, 19014064],Key2: ['chr10', 19010495, 19014658],Key3: ['chr10', 19010502, 19014641],Key4: ['chr10', 37375766, 37377526],Key5: ['chr10', 76310389, 76315990, 76312224, 76312963],Key6: ['chr11', 14806147, 14814006]} New_list = [ { "Key1": ['chr10', 19010495, 19014590, 19014064], "Key2": ['chr10', 19010495, 19014658], "Key3": ['chr10', 19010502, 19014641] }, {"Key4": ['chr10', 37375766, 37377526]}, {"Key5": ['chr10', 76310389, 76315990, 76312224, 76312963]}, {"Key6": ['chr11', 14806147, 14814006]}]pprint(First_dict)print('-'*40)g = groups(sorted_ranges(First_dict))p1 = reduce_phase1(groups(sorted_ranges(First_dict)))p2 = reduce_phase2(p1)pprint(p2)输出{'Key1': ['chr10', 19010495, 19014590, 19014064], 'Key2': ['chr10', 19010495, 19014658], 'Key3': ['chr10', 19010502, 19014641], 'Key4': ['chr10', 37375766, 37377526], 'Key5': ['chr10', 76310389, 76315990, 76312224, 76312963], 'Key6': ['chr11', 14806147, 14814006]}----------------------------------------[{'Key6': ['chr11', 14806147, 14814006]}, {'Key1': ['chr10', 19010495, 19014064, 19014590], 'Key2': ['chr10', 19010495, 19014658], 'Key3': ['chr10', 19010502, 19014641]}, {'Key4': ['chr10', 37375766, 37377526]}, {'Key5': ['chr10', 76310389, 76312224, 76312963, 76315990]}]TLDR;映射器输出映射器为每个字典键和染色体元素发出一条记录。每条记录都有一个关联的范围,可以在其中匹配其元素。((el-1000, el+1000), (dict-key, chromosome), el)(el-1000,el+1000)是任何其他染色体元素可以匹配的范围。(字典键,染色体)这条染色体的原始字典。el是染色体坐标中的一个元素。((19009495, 19011495), ('Key1', 'chr10'), 19010495)((19013590, 19015590), ('Key1', 'chr10'), 19014590)((19013064, 19015064), ('Key1', 'chr10'), 19014064)((19009495, 19011495), ('Key2', 'chr10'), 19010495)((19013658, 19015658), ('Key2', 'chr10'), 19014658)((19009502, 19011502), ('Key3', 'chr10'), 19010502)((19013641, 19015641), ('Key3', 'chr10'), 19014641)((37374766, 37376766), ('Key4', 'chr10'), 37375766)((37376526, 37378526), ('Key4', 'chr10'), 37377526)((76309389, 76311389), ('Key5', 'chr10'), 76310389)((76314990, 76316990), ('Key5', 'chr10'), 76315990)((76311224, 76313224), ('Key5', 'chr10'), 76312224)((76311963, 76313963), ('Key5', 'chr10'), 76312963)((14805147, 14807147), ('Key6', 'chr11'), 14806147)((14813006, 14815006), ('Key6', 'chr11'), 14814006)注意:映射器的输出未排序。排序我们需要使用 (el-1000, el+1000) 作为键对转换后的数据进行排序。这将允许我们检查下一个值是否在上一个值的范围内。由于键按排序顺序排列,因此我们将能够将指定重叠范围内的值链接在一起。((14805147, 14807147), ('Key6', 'chr11'), 14806147)((14813006, 14815006), ('Key6', 'chr11'), 14814006)((19009495, 19011495), ('Key1', 'chr10'), 19010495)((19009495, 19011495), ('Key2', 'chr10'), 19010495)((19009502, 19011502), ('Key3', 'chr10'), 19010502)((19013064, 19015064), ('Key1', 'chr10'), 19014064)((19013590, 19015590), ('Key1', 'chr10'), 19014590)((19013641, 19015641), ('Key3', 'chr10'), 19014641)((19013658, 19015658), ('Key2', 'chr10'), 19014658)((37374766, 37376766), ('Key4', 'chr10'), 37375766)((37376526, 37378526), ('Key4', 'chr10'), 37377526)((76309389, 76311389), ('Key5', 'chr10'), 76310389)((76311224, 76313224), ('Key5', 'chr10'), 76312224)((76311963, 76313963), ('Key5', 'chr10'), 76312963)((76314990, 76316990), ('Key5', 'chr10'), 76315990)群对指定重叠范围内的值进行分组。出现的列表将包含来自染色体的值,这些染色体位于前一条染色体的重叠范围内。[((14805147, 14807147), ('Key6', 'chr11'), 14806147)]----------------------------------------[((14813006, 14815006), ('Key6', 'chr11'), 14814006)]----------------------------------------[((19009495, 19011495), ('Key1', 'chr10'), 19010495), ((19009495, 19011495), ('Key2', 'chr10'), 19010495), ((19009502, 19011502), ('Key3', 'chr10'), 19010502)]----------------------------------------[((19013064, 19015064), ('Key1', 'chr10'), 19014064), ((19013590, 19015590), ('Key1', 'chr10'), 19014590), ((19013641, 19015641), ('Key3', 'chr10'), 19014641), ((19013658, 19015658), ('Key2', 'chr10'), 19014658)]----------------------------------------[((37374766, 37376766), ('Key4', 'chr10'), 37375766)]----------------------------------------[((37376526, 37378526), ('Key4', 'chr10'), 37377526)]----------------------------------------[((76309389, 76311389), ('Key5', 'chr10'), 76310389)]----------------------------------------[((76311224, 76313224), ('Key5', 'chr10'), 76312224), ((76311963, 76313963), ('Key5', 'chr10'), 76312963)]----------------------------------------[((76314990, 76316990), ('Key5', 'chr10'), 76315990)]----------------------------------------减少 - 第 1 阶段通过删除工程功能来清理数据。{'Key6': ['chr11', 14806147]}----------------------------------------{'Key6': ['chr11', 14814006]}----------------------------------------{'Key1': ['chr10', 19010495], 'Key2': ['chr10', 19010495], 'Key3': ['chr10', 19010502]}----------------------------------------{'Key1': ['chr10', 19014064, 19014590], 'Key2': ['chr10', 19014658], 'Key3': ['chr10', 19014641]}----------------------------------------{'Key4': ['chr10', 37375766]}----------------------------------------{'Key4': ['chr10', 37377526]}----------------------------------------{'Key5': ['chr10', 76310389]}----------------------------------------{'Key5': ['chr10', 76312224, 76312963]}----------------------------------------{'Key5': ['chr10', 76315990]}----------------------------------------减少 - 第 2 阶段将替换的字典键与其原始字典聚合。当字典键匹配时,追加相应染色体的值。{'Key6': ['chr11', 14806147, 14814006]}----------------------------------------{'Key1': ['chr10', 19010495, 19014064, 19014590], 'Key2': ['chr10', 19010495, 19014658], 'Key3': ['chr10', 19010502, 19014641]}----------------------------------------{'Key4': ['chr10', 37375766, 37377526]}----------------------------------------{'Key5': ['chr10', 76310389, 76312224, 76312963, 76315990]}----------------------------------------