1

Now I'm working on bioinformatics project for my diploma work. I have written Python-script, which should translate the list of strings of 1000 DNA-Sequences to proteins by 1152 different codontables (genetics codes). This codontables are contained in a list of dictionaries, which were received by shuffling of keys and values (codons and amino acids). I want, what this script translates 1000 Sequences in 1152 ways in one go mileage in IPython console or just in Python-3.6 IDLE. This script was written in Spyder 3.2.7. I tried a lot of ways to fix my code, but my script doesn't run. There is my code:

file = open('F:\\біоінформатика\\DNA_Sequence72 - Копія.py', 'r')
DNA = file.read()
DNA_Sequences = DNA.split(',')
Genetic_Codes = open('F:\\біоінформатика\\Genetic_Codes.py', 'r')
Genetic_Codes = Genetic_Codes.read()
Genetic_Codes_list = Genetic_Codes.split('\n')
for row in Genetic_Codes_list: #for str in list[Genetic_Codes_list] in range(1152):   
    Alternative_Genetic_Codes = Genetic_Codes_list.pop(0)
    for dna in range(1000):
        dna = DNA_Sequences.pop(0)
        codontable = Alternative_Genetic_Codes
        codontable_sequence = ""
        for i in range(0, len(dna)-(3+len(dna)%3), 3):
            if codontable[dna[i:i+3]] == "_":
                break
            codontable_sequence += codontable[dna[i:i+3]]
        print(list([codontable_sequence]))

After running I get such error:

File "", line 8, in

if codontable[dna[i:i+3]] == "_":

TypeError: string indices must be integers There is a screenshot of variables after script running: enter image description here I will be very thankful for your help.

Community
  • 1
  • 1
  • As the error say, you are trying to access elements of a string with a variable that is not an integer. I suspect it to be string: `dna = DNA_Sequences.pop(0)` and `DNA_Sequences` is a list of strings. So `dna[i:i+3]` is also a string. And bam, error. – pawamoy Apr 04 '18 at 14:17
  • Yes, *DNA_Sequences* is a list of strings, but *dna = DNA_Sequences.pop(0)* is just a string, which is got from *DNA_Sequences*. Problem is in variable *i*, which is assigned value *0*. Other lines of script, including *dna = DNA_Sequences.pop(0)*, run perfectly. – Niels Isaacson Apr 04 '18 at 19:33
  • 3
    Yes, all lines work fine except the one printed in the error: `if codontable[dna[i:i+3]] == "_":`. Knowing that `i` **is** an integer, the error comes from `codontable[...]`, not `dna[...]`. It's because `dna[i:i+3]` is a string and `codontable` is also a string. You cannot do `string[other_string]`. – pawamoy Apr 04 '18 at 19:41
  • @Pawamoy, seems to have correct answer. Basically, `codontable` need integer as index, but `dna[i:i+3]` is not an integer, it is a string. – Vince Apr 05 '18 at 00:27
  • 1
    Without your data we can't do much. Only you know what's inside `codontable`. Add some `print` statements in the loop to see what's happening in details. – pawamoy Apr 07 '18 at 10:05
  • In my post is screenshot with variables. *Codontable* is, essentially, a dictionary, which was popped from a list of dictionary. – Niels Isaacson Apr 07 '18 at 10:34
  • @Pawamoy, how I can fix **if codontable[dna[i:i+3]] == "_":** and which construct should be instead that? – Niels Isaacson Apr 11 '18 at 19:20
  • @NielsIsaacson, you can debug your program in a community version of PyCharm. Or try ipdb in the Python interpreter. If you understand that you need an integer to access `codontable` elements but do not understand why or how to fix this, I suggest you start again from the beginning. It's only a 10-line loop, it should not be difficult. – pawamoy Apr 11 '18 at 19:23
  • @Pawamoy, thank you for good idea and tip for my further job) – Niels Isaacson Apr 12 '18 at 15:04
  • @Pawamoy, I improved my code with PyCharm to that variant: if codontable.union[[dna[i:i+3]] == '_': but programm gave that: break ^ SyntaxError: invalid syntax. The rest of the script stays same. How I can fix this error? And is my if-statement correct now? – Niels Isaacson Apr 18 '18 at 16:13
  • @NielsIsaacson, it seems you are trying things randomly, without any comprehension of what you're doing. `obj.union[]` is not valid syntax, nor `obj.append[]`. I think you should read again a tutorial on the basis of Python. – pawamoy Apr 18 '18 at 16:21
  • @Pawamoy, after analize of my I edited *if-statement* to that look `for item in range(0, len(dna)-(3+len(dna)%3), 3): if codontable.index(dna[item:item + 3]) == "_": break codontable_sequence += codontable.index(dna[item:item + 3]) print(list([codontable_sequence]))`. But PyCharm and IPython gave that error: `codontable_sequence += codontable.index(dna[item:item + 3]) TypeError: must be str, not int`. – Niels Isaacson Apr 20 '18 at 17:43
  • I don't have enough time for searching of necessary part of Python tutorial and I need to know the concrete place of Python tutorial, where decribed сombination of different types of data in one statement. I will be very thankful for your hint in my search. – Niels Isaacson Apr 20 '18 at 17:43
  • Why in one statement? If you cannot do it in one, split it in several statements. Easier to understand and easier to debug. Come on man it is just a matter of list, strings and integers... I will stop answering here this is going nowhere. – pawamoy Apr 20 '18 at 18:34

0 Answers0