1 回答

TA貢獻1868條經驗 獲得超4個贊
這個問題可能更適合基于圖形的解決方案。沒有任何方法可以防止多個范圍以不同的時間間隔重疊。
#!/usr/bin/env python3
from pprint import pprint
from itertools import groupby
def 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 r
def 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 retval
def 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 retval
def update_dict(d1, d2):
retval = d1
for key, value in d2.items():
if key in d1.keys():
retval[key].extend(value[1:])
return retval
def 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 retval
First_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]}
----------------------------------------
添加回答
舉報