pysam for Python 3.2, on Ubuntu – mainly a Cython problem

Update: With the recently released pysam version 0.7.4 this works now also for Python 3.3.

I wanted to install pysam for Python 3.2 (actually, I wanted to install it for Python 3.3 but this is not supported so far) on a machine with Ubuntu 12.10 and ran into some trouble. The solution might be helpful for other pysam users and similar issues. What happened? I downloaded pysam 0.7 and tried to build it:

$ wget
$ tar xfz pysam-0.7.tar.gz
$ cd pysam-0.7/
$ python3.2 build
Traceback (most recent call last):
  File "", line 123, in 
    from Cython.Distutils import build_ext
ImportError: No module named Cython.Distutils

As you can see I got a error message telling me that the Cython.Distutils module is not available. I had the Ubuntu package cython installed and when performing the installation with python2.7 this problem did not occure. The solution is to install Cython manually for Python 3.2. For this it is necessary to have the packages python3.2-dev installed. Otherwise you will get the following error:

fatal error: Python.h: No such file or directory

So make sure you have this package (and of course python3.2) is installed

$ sudo apt-get update
$ sudo apt-get install python3.2 python3.2-dev

Now you download and install Cython:

$ wget
$ tar xfz Cython-0.17.4.tar.gz
$ cd Cython-0.17.4
$ python3.2 install --user

I use the –user flag to install Cython module (and later pysam) only locally to my user directory. If you prefer to install it system wide add sudo in front of the call and leave the flag away. Once Cython is available for Python 3.2 the installation of pysam works without any problems

$ cd pysam-0.7/
$ python3.2 build
$ python3.2 install --user

Now you should be able to import the pysam module in Python 3.2:

$ python3.2
Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pysam
>>> dir(pysam)
['AlignedRead', 'Fastafile', 'IndexedReads', 'IteratorColumn', 'IteratorRow', 'Pileup', 'PileupColumn', 'PileupProxy', 'PileupRead', 'SAMTOOLS_DISPATCH', 'Samfile', 'SamtoolsDispatcher', 'SamtoolsError', 'Tabixfile', 'VCF', 'VCFRecord', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__samtools_version__', '__version__', 'asBed', 'asGTF', 'asTuple', 'asVCF', 'bam2fq', 'calmd', 'cat', 'cmd', 'csamtools', 'ctabix', 'cvcf', 'depth', 'faidx', 'fixmate', 'flagstat', 'idxstats', 'index', 'key', 'merge', 'mpileup', 'options', 'os', 'parser', 'phase', 'reheader', 'rmdup', 'samimport', 'sort', 'sys', 'tabix_compress', 'tabix_index', 'tabix_inplace_iterator', 'tabix_iterator', 'targetcut', 'tview', 'version', 'view']