Error reading VCF file using python script
1
0
Entering edit mode
9.1 years ago
niharraul • 0

Hello,

Trying to access a VCF file (PyVCF 0.6.7) using the python script and the library, both provided on the following website:

https://pypi.python.org/pypi/PyVCF

import vcf

vcf_reader = vcf.Reader(open('example_GRCh37.vcf', 'r'))
for x in vcf_reader:
    print (x)

The error is as follows:

Traceback (most recent call last):
  File "/Volumes/Documents HD/Python_test/Convert_TO_VCF_USING_LIBRARAY.py", line 3, in <module>
    vcf_r = vcf.Reader(open('example_GRCh37.vcf'))
AttributeError: 'module' object has no attribute 'Reader'

It would be great if you could suggest something to fix this error

SNP • 8.3k views
ADD COMMENT
1
Entering edit mode
9.1 years ago
ebrown1955 ▴ 320

Try reinstalling PyVCF. It seems as though it may be incomplete.

easy_install pyvcf
ADD COMMENT
0
Entering edit mode

Hi,

Thanks for the reply. I am still getting the same error. Is it possible that I am running the code on the default python 2.7 version on mac and I should be upgrading it to higher that 3.4 or something.

Regards

ADD REPLY
1
Entering edit mode

Do you have a file in the same directory called "vcf.py"? I think python may be trying to call vcf.py instead of PyVCF.

ADD REPLY
0
Entering edit mode

It should be fine on 2.7. I'm using 2.7 on OS X, and it works fine. What does "import vcf" return?

ADD REPLY
0
Entering edit mode

(1) Since Mac OSX has default installation of python 2.7. But as per the suggestions when I tried to install the library using the command easy_install pyvcf it throws the following error:

 error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-10786.pth'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /Library/Python/2.7/site-packages/

Perhaps your account does not have write access to this directory? If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account. If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.

For information on other options, you may wish to consult the
documentation at:
  https://pythonhosted.org/setuptools/easy_install.html

Please make the appropriate changes for your system and try again.

(2) So I manually (not through command line) downloaded it visiting the website and that is clicking on the green button and then unzipping the folder in the appropriate directory. It was throwing error in the code so I copy pasted the "vcf" folder from the downloaded, unzipped PyVCF-0.6.7 folder.Since I import the vcf library in the code.Now for the same code ,I am getting the following error:

Traceback (most recent call last):
  File "/Volumes/Documents HD/Python_test/Convert_TO_VCF_USING_LIBRARAY.py", line 1, in <module>
    import vcf
  File "/Volumes/Documents HD/Python_test/vcf/__init__.py", line 175, in <module>
    from vcf.parser import Reader, Writer
  File "/Volumes/Documents HD/Python_test/vcf/parser.py", line 603, in <module>
    class Writer(object):
  File "/Volumes/Documents HD/Python_test/vcf/parser.py", line 607, in Writer
    counts = dict((v,k) for k,v in field_counts.iteritems())
AttributeError: 'dict' object has no attribute 'iter items'

(3)No, I am not having any "vcf.py" in the directory.

(4) I am using PyCharm IDE

ADD REPLY
0
Entering edit mode

You have to be root to use easy_install. Try "sudo easy_install pyvcf". You will be prompted for your password.

ADD REPLY
0
Entering edit mode

Thanks for the reply.

I could install the pyvcf as suggested with a password and with the sudo command and then restarted everything but getting the same error

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 "/Volumes/Documents HD/Python_test/Convert_TO_VCF_USING_LIBRARAY.py"
Traceback (most recent call last):
  File "/Volumes/Documents HD/Python_test/Convert_TO_VCF_USING_LIBRARAY.py", line 1, in <module>
    import vcf
  File "/Volumes/Documents HD/Python_test/vcf/__init__.py", line 175, in <module>
    from vcf.parser import Reader, Writer
  File "/Volumes/Documents HD/Python_test/vcf/parser.py", line 603, in <module>
    class Writer(object):
  File "/Volumes/Documents HD/Python_test/vcf/parser.py", line 607, in Writer
    counts = dict((v,k) for k,v in field_counts.iteritems())
AttributeError: 'dict' object has no attribute 'iteritems'

Process finished with exit code 1

Thanks again, really appreciate for your help.

Regards

ADD REPLY
0
Entering edit mode

It's actually a different error specific to Python 3. You're using Python 3, and you should be using Python 2.7.

ADD REPLY
0
Entering edit mode

But the default installation is for python 2.7 since I am using mac.

ADD REPLY
0
Entering edit mode

In your error message, it says

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 "/Volumes/Documents HD/Python_test"

The error message:

AttributeError: 'dict' object has no attribute 'iteritems'

is specific to Python 3, where the "dict.iteritems" was changed to "dict.items". Python 2.7 should not throw this error.

How are you running this script? Are you using the terminal? I.e. Typing:

python [script].py
ADD REPLY
0
Entering edit mode

No , I am using Pycharm IDE ... I guess Pycharm might be using Python 3

ADD REPLY

Login before adding your answer.

Traffic: 2880 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6