0
import os 

import sys

import numpy as np

from proteinss import ProteinSS

map_dssp_3_alphabet = {"H":1,"I":1,"G":1,"E":2,"B":2,"T":3,"S":3, "_":3, "?":3}
map_dssp_8_alphabet = {"H":1,"I":2,"G":3,"E":4,"B":5,"T":6,"S":7, "_":8, "?":8}

def extract_file(ss_file):
    print ("Extracting :"), ss_file

fi = open(ss_file, 'rU')
seq_string = ""
ss_string  = ""
alignments = []
for line in fi:
    if line.startswith("RES:"):
       seq_string = line.split(":")[1].rstrip('\n')

    if line.startswith("DSSP:"):
       ss_string = line.split(":")[1].rstrip('\n')

    if line.startswith("align"):
       alignment = line.split(":")[1].rstrip('\n')
       alignments.append( alignment.split(",")[0:-1] )

seq_l = seq_string.split(",")[0:-1]
ss_l  = ss_string.split(",")[0:-1]

prot = ProteinSS(seq_l, ss_l)

for al_ in alignments:
    prot.add_alignment(al_)

return prot


def prepare_db(cb513_path, db_ofile, db_classes, wsize, alphabet):
    fo1 = open(db_ofile, 'w')
    fo2 = open(db_classes, 'w')


for root, dirs, files in os.walk(cb513_path):
    for name in files:
        if name.endswith(( ".all" )):
            ss_file = root + "/" + name
            prot = extract_file ( ss_file )
            if prot.is_valid():
                s1, s2 = prot.get_db_strings(wsize, map_dssp_3_alphabet, True)
                fo1.write(s1)
                fo2.write(s2)

fo1.close()
fo2.close()


if __name__ == "__main__":
    cb513_path = '../data/CB513'
    classes_ofile   = "../data/db/ss_a3.dat" 

window_width = int( sys.argv[5] ) 
db_ofile = "../data/db/aa_w" + str(window_width) + "_a3.dat"

if sys.argv[2] == "3":
    alphabet = map_dssp_3_alphabet
elif sys.argv[2] == "8":
    alphabet = map_dssp_8_alphabet
else:
    print ("Alphabet not recognized: choose between 3 or 8")
    sys.exit(1)

pubs_data = prepare_db(cb513_path, db_ofile, classes_ofile, window_width, alphabet)

I am getting error at :

window_width = int( sys.argv[5] )

IndexError: list index out of range

When I am trying to run the code using spyder the coding is showing an error list out of range error, should I change the dimensions which are specified or use any other function?

It's one of the codes that I am using for protein secondary structure prediction, this is in particular is used to create the database .

The IndexError is one of the more basic and common exceptions found in Python, as it is raised whenever attempting to access an index that is outside the bounds of a list, is it because I am trying to access something outside the bound of the list.

How do I fix the above?

cswebdvlpr
  • 13
  • 1
  • 1
  • 5
  • 3
    `sys.argv[5]` isn't large enough to have an element at position 5. Are you running your command with all of the arguments this script expects? – Shadow Sep 24 '18 at 06:28
  • Remove all your code except the line causing the error and run the script again. Tell us the command you use to run the script. See how to create a [mcve]. Oh, and Welcome to Stack Overflow (c: – Peter Wood Sep 24 '18 at 06:31
  • @ Shadow , yes I am running with all the arguments within the script. – cswebdvlpr Sep 24 '18 at 06:33
  • @ Peter wood , thanks !! – cswebdvlpr Sep 24 '18 at 06:35
  • See [this question](https://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script) about how to use [**`sys.argv`**](https://docs.python.org/3/library/sys.html#sys.argv) – Peter Wood Sep 24 '18 at 06:45

1 Answers1

0

sys.argv is defined as a list in Python, which contains the command-line arguments passed to the script when you are going to execute any program.

You can check the passed argument while executing the list simply by printing length of sys.argv at start of the program.

Include below line at start of program:

print(len(sys.argv))

as from command line if I am executing below program in command line as

python demo.py 

then length of the sys.argv will be 1 and value of sys.argv[0] will be demo.py.

After checking this can modify the sys.argv index value as per your need. Hope this will solve your problem.

P.S. in your program this line is written wrong:

print ("Extracting :"), ss_file

correct it as

 print ("Extracting :"+ ss_file)
Suresh
  • 489
  • 3
  • 7