Question: (Closed) Python IndexError: list index out of range
0
gravatar for larafrommer
4.9 years ago by
Germany
larafrommer0 wrote:

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)   


 

gzip error snp python • 12k views
ADD COMMENTlink modified 4.9 years ago by Dan Lenhard0 • written 4.9 years ago by larafrommer0

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?

ADD REPLYlink modified 10 months ago by RamRS30k • written 4.9 years ago by Nicola Casiraghi450

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:

16,89985753
16,89986091
16,89986154
16,89986144
16,89985844
16,89985918
16,89986117
16,89986546
16,89986122
16,89985940
16,89986130
5,33958959
5,33951693
12,87852466
6,457748
6,396321
11,88911696
15,28230318
14,92801203
15,28356859
20,33218090
14,92773663
11,89011046
9,12709305

Thanks alot for you help =)

ADD REPLYlink modified 10 months ago by RamRS30k • written 4.9 years ago by larafrommer0

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.

ADD REPLYlink modified 10 months ago • written 4.9 years ago by RamRS30k

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.

ADD REPLYlink modified 10 months ago by RamRS30k • written 4.9 years ago by duxan50

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!

ADD REPLYlink written 4.9 years ago by RamRS30k

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 =)

ADD REPLYlink written 4.9 years ago by larafrommer0

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.

ADD REPLYlink written 4.9 years ago by RamRS30k
0
gravatar for Dan Lenhard
4.9 years ago by
United States
Dan Lenhard0 wrote:

Kind of new to Python myself, but what happens if you change that semicolon in the i.split(";") to a comma?

ADD COMMENTlink modified 10 months ago by RamRS30k • written 4.9 years ago by Dan Lenhard0
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 985 users visited in the last hour