I am getting "Type Error: 0" on dict when acquiring the length of the Dict (t = len(Motifs[0])
I reviewed the previous post on "Type Error: 0) and I tried casting t = int(len(Motifs[0]))
def Consensus(Motifs):
k = len(Motifs[0])
profile = ProfileWithPseudocounts(Motifs)
consensus = ""
for j in range(k):
maximum = 0
frequentSymbol = ""
for symbol in "ACGT":
if profile[symbol][j] > maximum:
maximum = profile[symbol][j]
frequentSymbol = symbol
consensus += frequentSymbol
return consensus
def ProfileWithPseudocounts(Motifs):
t = len(Motifs)
k = len(Motifs[0])
profile = {}
count = CountWithPseudocounts(Motifs)
for key, motif_lists in sorted(count.items()):
profile[key] = motif_lists
for motif_list, number in enumerate(motif_lists):
motif_lists[motif_list] = number/(float(t+4))
return profile
def CountWithPseudocounts(Motifs):
t = len(Motifs)
k = len(Motifs[0])
count = {}
for symbol in "ACGT":
count[symbol] = []
for j in range(k):
count[symbol].append(1)
for i in range(t):
for j in range(k):
symbol = Motifs[i][j]
count[symbol][j] += 1
return count
Motifs = {'A': [0.4, 0.3, 0.0, 0.1, 0.0, 0.9],
'C': [0.2, 0.3, 0.0, 0.4, 0.0, 0.1],
'G': [0.1, 0.3, 1.0, 0.1, 0.5, 0.0],
'T': [0.3, 0.1, 0.0, 0.4, 0.5, 0.0]}
#print(type(Motifs))
print(Consensus(Motifs))
"Type Error: 0"
"t = len(Motifs)"
"k = len(Motifs[0])"
"symbol = Motifs[i][j]"
on lines(9, 24, 35, 44) when code executes!!! Traceback:
Traceback (most recent call last):
File "myfile.py", line 47, in <module>
print(Consensus(Motifs))
File "myfile.py", line 2, in Consensus
k = len(Motifs[0])
KeyError: 0
I should get the "Consensus matrix" without errors