||
生物信息分析中,需要对很多的range数据进行merge,但是目前还没有对于大片段的range进行merge的方法,所以自己写了一个,发现其中也隐含很多的python使用经验。比如下面一个程序:
def region_redup(regionList1):
regionList1.sort()
indList = range(1, len(regionList1))
indList.sort(reverse=True)
for i in indList:
if regionList1[i][0] < regionList1[i-1][1]:
regionList1[i-1][1] = regionList1[i][1]
regionList1.remove(regionList1[i])
return regionList1
def region_merge(regionList1, regionList2):
print "redup 1"
region_redup(regionList1)
print "redup 2"
region_redup(regionList2)
for region2 in regionList2:
flag = 0
for region1 in regionList1:
if region2[0] < region1[1]:
flag = 1
if region2[1] > region1[0]:
regionList1.remove(region1)
regionList1.append([min(region1[0], region2[0]), max(region1[1], region2[1])])
else:
regionList1.append(region2)
if flag == 0:
regionList1.append(region2)
region_redup(regionList1)
return regionList1
import random
regionList1=[]
regionList2=[]
for i in xrange(0, 10000):
rand=random.randint(1, 90000)
regionList1.append([rand,rand+1000])
rand=random.randint(1, 90000)
regionList2.append([rand,rand+1000])
print "merge now"
print region_merge(regionList1, regionList2)
为了实现merge,我需要对range的element进行逐个处理,我做了一个测试,对100万个range进行merge估计需要最少三个多小时。但是灵机一动,可以先把各自内部相连的range先merge起来,这样就快速很多了,果然,现在只需要三秒钟。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-4-25 09:01
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社