0

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

saulotoledo
  • 1,737
  • 3
  • 19
  • 36
Barrington
  • 25
  • 6

2 Answers2

0

You have a dictionary called Motifs with 4 keys:

>>> Motifs.keys()
dict_keys(['A', 'C', 'G', 'T'])

But you are trying to get the value for the key 0, that does not exist (see, for example, Motifs[0] on line 2).

You should use a valid key as, for example, Motifs['A'].

saulotoledo
  • 1,737
  • 3
  • 19
  • 36
0

You defined Motifs as a dictionary.

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]}

Motifs[0] raises KeyError: 0 because the keys are ['T', 'G', 'A', 'C']. It seems like you wanted to access the length of the first List associated with key A. You can achieve this by taking len(Motifs['A']).

Note: Ordering of elements in a python dictionary is only a language feature starting from Python3.7. Mail thread here.

amiabl
  • 1,047
  • 19
  • 27
  • 1
    Python dictionaries are actually ordered now! (..as of python 3.7+) https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6 – SyntaxVoid Jul 24 '19 at 18:23
  • @SyntaxVoid Thanks, I edited my answer to reflect this precision. – amiabl Jul 24 '19 at 20:03