我有一个包含各种字母序列的列表。
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
我想查看该列表中每个序列的最后3个字母是否与所有其他序列的前3个字母匹配。如果发生这种情况,我想知道这两个序列的索引。
我基本上是想产生一个邻接表。以下是输入示例:
>Sample_0
AAGTAAA
>Sample_1
AAATGAT
>Sample_2
AAAGTTT
>Sample_3
TTTTCCC
>Sample_4
AATTCGC
>Sample_5
CGCTCCC
输出:
>Sample_0 >Sample_1
>Sample_0 >Sample_2
>Sample_2 >Sample_3
>Sample_4 >Sample_5
现在,我尝试制作两个包含所有前缀和所有后缀的不同列表,但是我不知道这是否可以帮助您以及如何使用它来解决我的问题。
file = open("rosalind_grph2.txt", "r")
gene_names, sequences, = [], []
seq = ""
for line in file:
if line[0] == ">":
gene_names.append(line.strip())
if seq == "":
continue
sequences.append(seq)
seq = ""
if line[0] in "ATCG":
seq = seq + line.strip()
sequences.append(seq)
#So far I put all I needed into a list
prefix = [i[0:3] for i in sequences]
suffix = [i[len(i)-3:] for i in sequences]
#Now, all suffixes and prefixes are in lists as well
#but what now?
print(suffix)
print(prefix)
print(sequences)
file.close
答案 0 :(得分:1)
如果我正确理解了您的问题,此代码将在列表中枚举两次。它会将第一个元素的最后3个字母与第二个元素的前3个字母进行比较,如果存在匹配项,则打印这些元素的索引。请提供反馈/说明是否不是您要的内容。这是O(n ^ 2),如果您初次通过并将索引存储在字典等结构中,则可能会加快速度。
for index1, sequence1 in enumerate(sequences):
for index2, sequence2 in enumerate(sequences):
if index1 != index2:
if sequence1[-3:] == sequence2[0:3]:
print(sequence1[-3:], index1, sequence2[0:3], index2)
答案 1 :(得分:0)
如果我正确理解,您想做的是连接 sequences
的另一个元素,其中连接是字符串的开头与另一个字符串的结尾匹配。
使用dict
的一种方法是使用以下函数match_head_tail()
:
def match_head_tail(items, length=3):
result = {}
for x in items:
v = [y for y in items if y[:length] == x[-length:]]
if v:
result[x] = v
return result
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
print(match_head_tail(sequences))
# {'AAGTAAA': ['AAATGAT', 'AAAGTTT'], 'AAAGTTT': ['TTTTCCC'], 'AATTCGC': ['CGCTCCC']}
如果您还希望包括不匹配的序列,则可以使用以下函数match_head_tail_all()
:
def match_head_tail_all( items, length=3):
return {x: [y for y in items if y[:length] == x[-length:]] for x in items}
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
print(match_head_tail_all(sequences))
# {'AAGTAAA': ['AAATGAT', 'AAAGTTT'], 'AAATGAT': [], 'AAAGTTT': ['TTTTCCC'], 'TTTTCCC': [], 'AATTCGC': ['CGCTCCC'], 'CGCTCCC': []}
如果您确实需要索引,请将以上内容与enumerate()
结合使用以获取索引,例如:
def match_head_tail_all_indexes( items, length=3):
return {
i: [j for j, y in enumerate(items) if y[:length] == x[-length:]]
for i, x in enumerate(items)}
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
print(match_head_tail_all_indexes(sequences))
# {0: [1, 2], 1: [], 2: [3], 3: [], 4: [5], 5: []}
如果您的输入包含许多具有相同结尾的序列,则您可能要考虑实现某种缓存机制以提高计算效率(以内存效率为代价),例如:
def match_head_tail_cached(items, length=3, caching=True):
result = {}
if caching:
cached = {}
for x in items:
if caching and x[-length:] in cached:
v = cached[x[-length:]]
else:
v = [y for y in items if y[:length] == x[-length:]]
if v:
result[x] = v
return result
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
print(match_head_tail_cached(sequences))
# {'AAGTAAA': ['AAATGAT', 'AAAGTTT'], 'AAAGTTT': ['TTTTCCC'], 'AATTCGC': ['CGCTCCC']}
所有这些也只能用list
来实现,例如:
def match_head_tail_list(items, length=3):
result = []
for x in items:
v = [y for y in items if y[:length] == x[-length:]]
if v:
result.append([x, v])
return result
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
print(match_head_tail_list(sequences))
# [['AAGTAAA', ['AAATGAT', 'AAAGTTT']], ['AAAGTTT', ['TTTTCCC']], ['AATTCGC', ['CGCTCCC']]]
甚至更少的嵌套:
def match_head_tail_flat(items, length=3):
result = []
for x in items:
for y in items:
if y[:length] == x[-length:]:
result.append([x, y])
return result
sequences = ['AAGTAAA', 'AAATGAT', 'AAAGTTT', 'TTTTCCC', 'AATTCGC', 'CGCTCCC']
print(match_head_tail_flat(sequences))
# [['AAGTAAA', 'AAATGAT'], ['AAGTAAA', 'AAAGTTT'], ['AAAGTTT', 'TTTTCCC'], ['AATTCGC', 'CGCTCCC']]