FunnyWeb

据说这里有很多神奇的东西...

python调用C++的pybam库

2018-01-15 11:30:274296 views


pybam是一个C++ 实现的读取bam文件的库,调用了boost库。

http://shahlab.ca/projects/mutationseq/

根据文档,安装之后一直出现以下问题。

image.png

ImportError: /home/sclixd/soft/python-2.7.12/lib/python2.7/site-packages/pybam.so: undefined symbol: _ZTIN5boost6python15instance_holderE

后来发现是boost源码路径指定错误

然后修改路径后又出现了新的错误

image.png

/home/sczhuhd/workspace_zhu/workspace_mutationseq/boost_1_66_0//libs/python/src/numpy/ndarray.cpp:25:11: error: 'NPY_ARRAY_C_CONTIGUOUS' was not declared in this scope

   if (f & NPY_ARRAY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);

修改源码后编译成功

正确的安装步骤:

1、下载编译安装Python

wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar zxf Python-2.7.14.tgz
cd Python-2.7.14
./configure --prefix=/home/sczhuhd/software/
make -j24
make install

2、安装setuptools、pip、wheel

wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python get-pip.py

备用脚本

get-pip.zip

3、安装相关依赖库

最主要的是要安装numpy,否则boost不能编译相关组件

pip install numpy


4、下载安装boost

wget https://dl.bintray.com/boostorg/release/1.66.0/source/boost_1_66_0.tar.gz
tar zxf boost_1_66_0.tar.gz
cd boost_1_66_0/
./bootstrap.sh --prefix=/home/sczhuhd/software/
./b2 install --prefix=/home/sczhuhd/software/

5.修改boost.numpy源码问题

(此时可以尝试直接运行下一步试一试,如果没有错误可以跳过这一步)

测试Ubuntu下没有出现此问题,CentOS6.4下出现。

image.png

根据github上numpy的源码

https://github.com/numpy/numpy/blob/1368cbb696ae27b849eed67b4fd31c550a55dad5/numpy/core/include/numpy/ndarraytypes.h

编辑boost源码里(注意是下载编译后的源码目录不是安装目录)./boost_1_66_0/libs/python/src/numpy/ndarray.cpp

在前面加上

#define NPY_ARRAY_C_CONTIGUOUS    0x0001
#define NPY_ARRAY_F_CONTIGUOUS    0x0002
#define NPY_ARRAY_ALIGNED         0x0100
#define NPY_ARRAY_WRITEABLE       0x0400
#define NPY_ARRAY_ENSUREARRAY     0x0040

image.png

改完后并不需要重新编译

6、编译pybam.so

接下来即可进入mutationseq文件夹下编译pybam.so

wget ftp://ftp.bcgsc.ca/public/shahlab/MutationSeq/museq_4.3.8.tar.gz
tar zxf museq_4.3.8.tar.gz
cd mutationseq
cd museq
python setup.py install --boost_source=/home/sczhuhd/workspace_zhu/workspace_mutationseq/boost_1_66_0

这里指定的也是下载编译后的源码目录,不是安装目录

接下来测试python中引用情况

python

image.png

即可正常工作~