각 파일을 하나의 파일 (한 줄에 1 개씩)로 이동하고 두 번째 파일에서 해당 행의 한 열을 기준으로 모든 일치하는 기능을 찾으려고합니다. 나는 작은 파일에서 원하는 것을 수행하는이 솔루션을 가지고 있지만 대용량 파일에서는 매우 느립니다 (내 파일은 20,000,000 개가 넘습니다). Here's a sample of the two input files.두 배열 (파이썬)에서 일치하는 기능을 빠르게 찾을 수 있습니까?
내 (느린) 코드 : 당신이 conservationFile
의 모든 걸쳐 루핑과
FEATUREFILE = 'S2_STARRseq_rep1_vsControl_peaks.bed'
CONSERVATIONFILEDIR = './conservation/'
with open(str(FEATUREFILE),'r') as peakFile, open('featureConservation.td',"w+") as outfile:
for line in peakFile.readlines():
chrom = line.split('\t')[0]
startPos = int(line.split('\t')[1])
endPos = int(line.split('\t')[2])
peakName = line.split('\t')[3]
enrichVal = float(line.split('\t')[4])
#Reject negative peak starts, if they exist (sometimes this can happen w/ MACS)
if startPos > 0:
with open(str(CONSERVATIONFILEDIR) + str(chrom)+'.bed','r') as conservationFile:
cumulConserv = 0.
n = 0
for conservLine in conservationFile.readlines():
position = int(conservLine.split('\t')[1])
conservScore = float(conservLine.split('\t')[3])
if position >= startPos and position <= endPos:
cumulConserv += conservScore
n+=1
featureConservation = cumulConserv/(n)
outfile.write(str(chrom) + '\t' + str(startPos) + '\t' + str(endPos) + '\t' + str(peakName) + '\t' + str(enrichVal) + '\t' + str(featureConservation) + '\n')
, 당신은 위치에 의해'conservationFile' 정렬 고려'peakFile'은 가정 오히려 N에 N^2이의 복잡성을 줄일 수있는이 방법이 정렬되었습니다. (당신은'sort -r -k2'를 사용할 수 있습니다) – Oren
판다를 사용하십시오. 그것은 당신의 삶을 바꿀 수도 있습니다. – Chris
@Chris OP가 도서관 추천을 찾지 못했습니다. –