计算用于对齐的共同信息时出现错误[关闭]

问题描述 投票:0回答:2
我有一个列表,表示序列(B)和所有可能的字母的对齐方式,如下所示:

B = [ ['A','U','-','C','-','G','-','-','C','U','G','C'], ['A','A','A','U','C','A','-','A','G','A','-','A'], ['C','U','G','-','-','G','A','A','-','A','A','U'], ] set1 = ['A','G','C','U','-']

我正在尝试使用以下函数为集合中的每个元素计算序列比对中所有位置之间的互信息(https://en.wikipedia.org/wiki/Mutual_information

def MI(sequences,i,j): Pi = Counter(sequence[i] for sequence in sequences) Pj = Counter(sequence[j] for sequence in sequences) Pij = Counter((sequence[i],sequence[j]) for sequence in sequences) return sum(Pij[(x,y)]*log(Pij[(x,y)]/(Pi[x]*Pj[y])) for x,y in Pij) for r in range(len(set)): for s in range(0, len(B[0])): for t in range(0,len(B[0])): print(MI(str(set[r]),s,t))

我得到以下输出:

0.0 Traceback (most recent call last): File "test.py", line 51, in <module> print(MI(str(set[r]),s,t)) File "test.py", line 27, in MI Pj = Counter(sequence[j] for sequence in sequences) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/collections/__init__.py", line 568, in __init__ self.update(*args, **kwds) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/collections/__init__.py", line 655, in update _count_elements(self, iterable) File "test.py", line 27, in <genexpr> Pj = Counter(sequence[j] for sequence in sequences) IndexError: string index out of range

python arrays list biopython
2个回答
0
投票
请执行以下代码更改

for s in range(0, len(B[0])):

您尝试使用s,t实现什么?

0
投票
这解决了问题:

for r in range(len(set)): for s in range(0, len(B[0])): for t in range(0,len(B[0])): print(MI(B,s,t))

© www.soinside.com 2019 - 2024. All rights reserved.