Hi all I am supposed to use a Python script to identify possible SNPs at specified positions from a csv file in vcf files.
I just started using python and sadly I always get following Error :
Traceback (most recent call last):
File "getSNPs.py", line 20, in <module> oo = line[2] + "_" +
line[3]
IndexError: list index out of range from the following script
!/bin/python usage: python getSNPs.py your.vcf PhenoSNPs.csv
Code:
import sys
import gzip
SNPs = {}
for i in gzip.open(sys.argv[1], "r"):
if '#' not in i:
line = i.split("\t")
oo = line[0] + "_" + line[1]
SNPs[oo] = i
pp = sys.argv[1] + ".captureSNPs"
out = open(pp, "w")
for i in open(sys.argv[2], "r"):
line = i.split(",")
oo = line[2] + "_" + line[3]
try:
out.write(SNPs[oo])
except KeyError:
ow = line[2] + "\t" + line[3] + "\t" + "not covered" + "\n"
out.write(ow)
If for instance i = 'aa' and you do line = i.split(",") it implies that line = ['aa'], then you will get an IndexError when you do line[2] + "_" + line[3] because line doesn't have 2nd and 3rd elements.
Use try/except or re-think the logic of your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With