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
for s in range(0, len(B[0])):
您尝试使用s,t实现什么?
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))