我正在尝试编写一些代码,用Python中的一组kmers(k字母长字符串,DNA测序读取)构建DeBruijn图,输出为边集合,将同一节点连接到其他节点。
当我在示例输入上运行代码时:
['GAGG','CAGG','GGGG','GGGA','CAGG','AGGG','GGAG']
我明白了:
CAG -> AGG
GAG -> AGG
而不是:
AGG -> GGG
CAG -> AGG,AGG
GAG -> AGG
GGA -> GAG
GGG -> GGA,GGG
任何我做错的提示?
这是代码:
import itertools
inp=['GAGG','CAGG','GGGG','GGGA','CAGG','AGGG','GGAG']
y=[a[1:] for a in inp]
z=[b[:len(b)-1] for b in inp]
y.extend(z)
edjes=list(set(y))
w=[c[1:] for c in edjes]
v=[d[:len(d)-1] for d in edjes]
w.extend(v)
nodes=list(set(w))
graph={}
new=itertools.product(edjes,edjes)
for node in nodes:
for edj in new:
edje1,edje2=edj[0],edj[1]
if edje1[1:]==node and edje2[:len(edje2)-1]==node:
if edje1 in graph:
graph[edje1].append(edje2)
else:
graph[edje1]=[edje2]
for val in graph.values():
val.sort()
for k,v in sorted(graph.items()):
if len(v)<1:
continue
else:
line=k+' -> '+','.join(v)+'\n'
print (line)
答案 0 :(得分:3)
我认为你使算法太复杂了:你可以简单地首先对输入执行唯一性过滤器:
inp=['GAGG','CAGG','GGGG','GGGA','CAGG','AGGG','GGAG']
edges=list(set(inp))
然后迭代这个“边缘”列表。对于每个边,前三个字符是来自节点,后三个字符是到节点:
for edge in edges:
frm = edge[:len(edge)-1]
to = edge[1:]
#...
现在您只需将其添加到图表中:
for edge in edges:
frm = edge[:len(edge)-1]
to = edge[1:]
if frm in graph:
graph[frm].append(to)
else:
graph[frm]=[to]
最后像你自己一样进行分类和打印:
for val in graph.values():
val.sort()
for k,v in sorted(graph.items()):
print(k+' -> '+','.join(v))
这导致:
AGG -> GGG
CAG -> AGG
GAG -> AGG
GGA -> GAG
GGG -> GGA,GGG
正如您所看到的,2
行有一个小差异:您的预期输出包含AGG
两次,这没什么意义。
所以完整的算法类似于:
inp=['GAGG','CAGG','GGGG','GGGA','CAGG','AGGG','GGAG']
edges=list(set(inp))
graph={}
for edge in edges:
frm = edge[:len(edge)-1]
to = edge[1:]
if frm in graph:
graph[frm].append(to)
else:
graph[frm]=[to]
for val in graph.values():
val.sort()
for k,v in sorted(graph.items()):
print(k+' -> '+','.join(v))
我认为一个问题是你认为三个字母序列是“edjes”(可能是 edge )。边是四个序列字符。通过执行此转换,信息将丢失。接下来,您构造一组双字符项(nodes
,它们根本没有节点)。它们似乎用于将节点“粘合”在一起。但是在那个阶段,你不知道这些碎片是如何粘在一起的。