2

Hi I want to check the score value of the Amino acid sequence, for which I wrote the following code which works absolutely fine. But the problem is every time I have to edit the file. is there any way I can give the the amino acid sequence from the command interface.

AA_seq='AVTLSPQRS' # this is the input variable
sum=0

value={"V": 3.1,"Y":3.5,"W":4.7,"T" :5.3,"S":5.1,"P":3.7,
"F":4.7,"M":1.5,"K":8.9,"L":6,"I":4.3,"H":3.3,"G":7.1,
"E":7,"Q":5.4,"C":0.6,"D":7.6,"N":6,"R":8.7,"A":3.4}

print("Total length of sequence is:", len(AA_seq))
for i in AA_seq:
    
    sum+=value[i]


print("Total Score is :", sum)
Hossain
  • 53
  • 5

2 Answers2

1

If I understood your problem well, you can get it done by using 'input' function of python. This will enable you to give input from the CLI. Below is the modifies code.

Hope this helps you. Also if you want to avoid small/caps hedache for the input, you can use .upper().

Though its not required for this question.

AA_seq=input("write Amino Acid Sequence:" )
AA_seq=AA_seq.upper()

sum=0

value={"V": 3.1,"Y":3.5,"W":4.7,"T" :5.3,"S":5.1,"P":3.7,
"F":4.7,"M":1.5,"K":8.9,"L":6,"I":4.3,"H":3.3,"G":7.1,
"E":7,"Q":5.4,"C":0.6,"D":7.6,"N":6,"R":8.7,"A":3.4}

print("Total length of sequence is:", len(AA_seq))
for i in AA_seq:
    
    sum+=value[i]


print("Total Score is :", sum)
shivam
  • 596
  • 2
  • 9
1

You can easily access string arguments from the command line in sys.argv. (For historical reasons, sys.argv[0] contains the name of the script itself, so ignore that.)

import sys

value={"V": 3.1, "Y": 3.5, "W": 4.7, "T": 5.3, "S": 5.1,
       "P": 3.7, "F": 4.7, "M": 1.5, "K": 8.9, "L": 6, "I": 4.3, 
       "H": 3.3, "G": 7.1, "E": 7, "Q": 5.4, "C": 0.6, "D": 7.6,
       "N": 6, "R": 8.7, "A": 3.4}

for AA_seq in sys.argv[1:]:
    print("Total length of sequence", AA_seq, "is:", len(AA_seq))
    total = 0
    for i in AA_seq:
        total += value[i]
    print(AA_seq, "Total Score is :", total)

You'll notice that I added a loop so you can process multiple sequences in a single run if you like.

Usage:

python3 script.py 'AVTLSPQRS' 'KIIGAPEAR'

(or just python if that's the name of the Python 3 executable on your platform).

Accepting input from the command line enables you to use the various facilities of the shell (tab completion, history, variable expansion, etc) as well as use this script programmatically from other scripts.

A proper tool should probably include some error handling, too (basically check that there are arguments, and display an error if not) but this should already get you started. Also, perhaps trap KeyError so you don't get a traceback if the input string contains symbols which are not in the value hash.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    I tried to run your code as it is but shows the following error. I am newbie to coding so pardon me for my mistakes: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'float' – Hossain Mar 29 '22 at 07:37
  • 2
    Sorry about that, I just assumed your code was otherwise working. Updated now to rename the variable and initialize it properly. – tripleee Mar 29 '22 at 07:40