简体   繁体   中英

What am i doing wrong while running this code?

First off, I am in no way a programming expert, and am not well versed with python, so forgive me if this is a stupid question. I am trying to run the code below to filter a fasta file down to only the sequences I want using an "ID" file, but every time I run it, I get an error. Any help is greatly appreciated!

"""
%prog file.fasta wanted_ids.txt
"""
from Bio import SeqIO
import sys

wanted = [line.strip() for line in open(sys.argv[2])]
seqiter = SeqIO.parse(open(sys.argv[1]), 'fasta')
SeqIO.write((seq for seq in seqiter if seq.id in wanted), sys.stdout, "fasta")

This is the error I get:

File "filter.py", line 7, in <module>
    wanted = [line.strip() for line in open(sys.argv[2])]
IndexError: list index out of range

The exception you describe can only occur if you have not passed the expected number of arguments to your program when you run it. The sys.argv variable contains the arguments to the program. It will be a list, and sys.argv[0] will have the filename of the script itself, while later values will be later arguments passed in.

Your program's docstring ( %prog file.fasta wanted_ids.txt ) suggests that you're expected to pass two arguments to the program, one of which is a .fasta file and the other which is a .txt file.You are omitting one or both of those arguments, so the program fails to do the lookup of sys.argv[2] , and raises an IndexError .

You might want to add code to your program to check the number of arguments and give a more helpful exception if you get the wrong number:

from Bio import SeqIO
import sys

if len(sys.argv) != 3:
    sys.exit("Bad arguments! Usage: {} <file.fasta> <wanted_ids.txt>".format(sys.argv[0]))

wanted = [line.strip() for line in open(sys.argv[2])]
#...

我希望它可以为您with open (sys.argv[2]) as file_: for line in file_:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM