Python IndexError: list index out of range
Hi all I am supposed to use a Python script to identify possible SNPs at specified positions from a csv file in vcf files. Sadly I always get followig 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
# uses a lot of RAM
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)
• 13,530 views
•
link
1 answer
Kind of new to Python myself, but what happens if you change that semicolon in the i.split(";") to a comma?
• 1 views
•
link
Log in to answer this question.
It seems that the list returned from command
line = i.split(";")contain less than 3 elements.Could you show head of your PhenoSNPs.csv file?
Sure. I already changed the
;to,but sadly it did not solve my problem, as the Error still occurs. The PhenoSNPs.csv looks like this:Thanks alot for you help =)
Your
argv[2]comma/semicolon delimited values file has 2 columns, you're using indices[2]and[3], neither of which is a valid index.Can you show a snippet of PhenoSNPs.csv file? It seems like your script doesn-t parse it well. In script, on line 19, you use
;as delimiter and my guess is that maybe CSV has commas.Hello larafrommer!
We believe that this post does not fit the main topic of this site.
This is a programming question. Please ask stackoverflow.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
Yes it obviously is a programming question, but as scripting becomes more and more important in the field of DNA analysis for example I believe it does fit the main topic quite well and people above do seem to actually be able to help me I don't see a problem in leaving this question open.
Greetings =)
Scripting is important, yes, but what you're seeing here is a pure programming error. If complex operations that need an understanding of the underlying biology were required, I would not have closed the question. However, we have to take a call on the bioinformatics/programming boundary at which we stop letting in questions, and unfortunately, your question falls on the wrong side of said boundary.