0

I am trying to set up a code that looks into a file with Amino Acids in it and pulls out the first amino acid (for example pulls out Ala). I want it to then search in another file with codons that match the Ala codons given. When I run the code I have set up it says that there are no matching amino acid codons in the text even though there are.

codonsinput=open("Codons.txt", "r")
aminoacidsinput=open("AminoAcids.txt", "r")

allcodons=codonsinput.readlines()
allaminoacids=aminoacidsinput.readlines()
codonsinput.seek(0)
aminoacidsinput.seek(0)
AlaName = ['Ala']
CodonsAla = ['GCU', 'GCC', 'GCA', 'GCG']
string1 = 'coding'
file1 = open("AminoAcids.txt", "r")
flag = 0
index = 0
for line in file1:  
index + 1 
if 'AlaName' in line:
    flag = 1
    break
if flag == 0: 
    print('AminoAcid', AlaName , 'Not Found in text') 
else:
    file2 = open("Codons.txt", "r")
    flag = 0
    index = 0
file2 = open("Codons.txt", "r")
for line in file2:  
    index + 1 
    if 'CodonsAla' in line:
        flag = 1
        break 
if flag == 0: 
    print('Ala', CodonsAla , 'Not Found in AminoAcid text') 
else: 
    print('Ala', CodonsAla , 'Found')
file1.close()
file2.close()
Ceezy
  • 1
  • Have you printed the values of `line` to see why the condition `if 'CodonsAla' in line` is apparently never true? – mkrieger1 Jul 03 '21 at 17:14
  • Or did you mean to test if any of the strings in the *list* `CodonsAla` is contained in the file? Because currently you are checking if the *string* `'CodonsAla'` is in the file which understandably it is not if the file contains strings like `'GCU'`, `'GCC'`, etc. – mkrieger1 Jul 03 '21 at 17:15
  • This should answer your question: [How to check if a string contains an element from a list in Python](https://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python) – mkrieger1 Jul 03 '21 at 17:17
  • `if 'CodonsAla' in line` is checking to see if the literal string `'CodonsAla'` is in the line. You probably wanted to use the variable rather than a fixed string, in which case you would be looking at a list of strings, where each string is a codon. – Tom Karzes Jul 03 '21 at 17:18
  • When I change it to CodonsAla and AlaName I get the error: if AlaName in line: TypeError: 'in ' requires string as left operand, not list – Ceezy Jul 03 '21 at 17:28
  • That's why you should do it like shown [here](https://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python) instead. Or rather, like shown [here](https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string) more clearly. – mkrieger1 Jul 03 '21 at 17:33
  • K so I redid the code to follow that format but now it won't print('Ala', CodonsAla , 'Found'). The program just ends. (Sorry for all the questions lol im new to Python) – Ceezy Jul 03 '21 at 17:42
  • So you are trying to say that the value of `flag` was neither equal to 0, nor different from 0? – mkrieger1 Jul 03 '21 at 17:43

0 Answers0