TLDR:对于您描述的数据规模,在我的计算机上进行估计大约需要 2 小时。
import numpy as np
import pandas as pd
def substring_search(fullstrings, sublen=4):
'''
fullstrings: array like of strings
sublen: length of substring to search
'''
# PART 1: FIND SUBSTRINGS
# length of full strings, assumes all are same
strsize = len(fullstrings[0])
# get unique strings, # occurences
strs, counts = np.unique(fullstrings, return_counts=True)
fullstrings = pd.DataFrame({'string':strs,
'count':counts})
unique_n = len(fullstrings)
# create array to hold substrings
substrings = np.empty(unique_n * (strsize - sublen + 1), dtype=str)
substrings = pd.Series(substrings)
# slice to find each substring
c = 0
while c + sublen <= strsize:
sliced = fullstrings['string'].str.slice(c, c+sublen)
s = c * unique_n
e = s + unique_n
substrings[s: e] = sliced
c += 1
# take the set of substrings, save in output df
substrings = np.unique(substrings)
output = pd.DataFrame({'substrings':substrings,
'repeats': 0,
'unique_sources': 0})
# PART 2: CHECKING FULL STRINGS FOR SUBSTRINGS
for i, s in enumerate(output['substrings']):
# check which fullstrings contain each substring
idx = fullstrings['string'].str.contains(s)
count = fullstrings['count'][idx].sum()
output.loc[i, 'repeats'] = count
output.loc[i, 'unique_sources'] = idx.sum()
print('Finished!')
return output
应用于您的示例:
>>> example = ['AAAAB', 'BBBBA', 'BBBBA', 'ABBBB']
>>> substring_search(example)
substrings repeats unique_sources
0 AAAA 1 1
1 AAAB 1 1
2 ABBB 1 1
3 BBBA 2 1
4 BBBB 3 2
说明
上面代码中的基本思想是遍历所有唯一的子字符串,并(对于每个子字符串)使用pandasstr方法检查完整字符串列表。这节省了一个 for 循环(即,您不会为每个子字符串遍历每个完整字符串)。另一个想法是只检查唯一的完整字符串(除了唯一的子字符串);您事先保存每个完整字符串的出现次数并在最后更正计数。
基本结构是:
- 获取输入中的唯一字符串,并记录每个字符串出现的次数。
- 在输入中查找所有唯一的子字符串(我使用
pandas.Series.str.slice 进行此操作)
- 循环遍历每个子字符串,并使用
pandas.Series.str.contains(按元素)检查完整字符串。由于这些是唯一的,而且我们知道每个发生的次数,我们可以同时填写 repeats 和 unique_sources。
测试
这是我用来创建更大输入数据的代码:
n = 100
size = 12
letters = list(string.ascii_uppercase[:20])
bigger = [''.join(np.random.choice(letters, size)) for i in range(n)]
所以bigger 是n size-长度字符串:
['FQHMHSOIEKGO',
'FLLNCKAHFISM',
'LDKKRKJROIRL',
...
'KDTTLOKCDMCD',
'SKLNSAQQBQHJ',
'TAIAGSIEQSGI']
使用打印进度的修改代码(发布在下面),我尝试使用n=150000 和size=12,并得到了这个初始输出:
Starting main loop...
5%, 344.59 seconds
10.0%, 685.28 seconds
所以 10 * 685 秒 / 60(秒/分钟)= ~114 分钟。所以 2 小时并不理想,但实际上比 1 周更有用。我不怀疑有一些更聪明的方法可以做到这一点,但如果没有发布其他内容,这可能会有所帮助。
如果您确实使用此代码,您可能需要通过一些较小的示例来验证结果是否正确。我不确定的一件事是你是否想计算一个子字符串是否只出现在每个完整字符串中(即contains),或者你是否想要它出现在一个完整字符串中的次数(即count)。至少希望这是一个小小的改变。
这是在进行搜索时打印进度的附加代码; #PART 2 中还有其他语句:
def substring_search_progress(fullstrings, sublen=4):
'''
fullstrings: array like of strings
sublen: length of substring to search
'''
# PART 1: FIND SUBSTRINGS
# length of full strings, assumes all are same
strsize = len(fullstrings[0])
# get unique strings, # occurences
strs, counts = np.unique(fullstrings, return_counts=True)
fullstrings = pd.DataFrame({'string':strs,
'count':counts})
unique_n = len(fullstrings)
# create array to hold substrings
substrings = np.empty(unique_n * (strsize - sublen + 1), dtype=str)
substrings = pd.Series(substrings)
# slice to find each substring
c = 0
while c + sublen <= strsize:
sliced = fullstrings['string'].str.slice(c, c+sublen)
s = c * unique_n
e = s + unique_n
substrings[s: e] = sliced
c += 1
# take the set of substrings, save in output df
substrings = np.unique(substrings)
output = pd.DataFrame({'substrings':substrings,
'repeats': 0,
'unique_sources': 0})
# PART 2: CHECKING FULL STRINGS FOR SUBSTRINGS
# for marking progress
total = len(output)
every = 5
progress = every
# main loop
print('Starting main loop...')
start = time.time()
for i, s in enumerate(output['substrings']):
# progress
if (i / total * 100) > progress:
now = round(time.time() - start, 2)
print(f'{progress}%, {now} seconds')
progress = (((i / total * 100) // every) + 1) * every
# check which fullstrings contain each substring
idx = fullstrings['string'].str.contains(s)
count = fullstrings['count'][idx].sum()
output.loc[i, 'repeats'] = count
output.loc[i, 'unique_sources'] = idx.sum()
print('Finished!')
return output