Keyerror: '>R' in Google Colab Python

Asked

Viewed 629 times

3

I copied the following code from a data visualization tutorial in Python, but it does not perform properly, warning Keyerror as seen in the title. Can someone help me? There’s something wrong there?

entrada = open("/bacteria.fasta").read()
saida = open("/bacteria.html", "w")

cont = {}

for i in ['A', 'T', 'C', 'G']:
    for j in ['A', 'T', 'C', 'G']:
        cont[i+j] = 0

entrada = entrada.replace("\n", "")

for k in range(len(entrada)-1):
    cont[entrada[k]+entrada[k+1]] += 1

# html

i = 1
for k in cont:
    transparencia = cont[k]/max(cont.values())
    saida.write("<div style='width:100px; border:1px solid #111; height:100px; float:left; background-color:rgba(0,0,255,"+str(transparencia)+"')></div>")

saida.close()

The log says the following:

KeyError                                  Traceback (most recent call last)
<ipython-input-9-e27a0dec51b4> in <module>()
     13 
     14 for k in range(len(entrada)-1):
---> 15         cont[entrada[k]+entrada[k+1]] += 1
     16 
     17 # html

KeyError: '>R'
  • Can you please put the full bug log in the question?

  • Okay, I’ll log

  • Here in entrada[k+1] the index k+1 in the last element k violates the list size entrada.

  • Open in block of text and delete the information not referring to the genetic code. For me, it worked right after

1 answer

3


I recently conducted the same study, and I was able to solve the same problem by opening the file "bacteria.fasta" with Sublime Text software and deleting the first two lines that do not have information regarding the genetic code. After saving, the error did not appear again.

Att.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.