forked from pysam-developers/pysam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
600 lines (527 loc) · 18 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#! /usr/bin/python
'''The SAM/BAM/CRAM format is a way to store efficiently large numbers
of alignments, such as those routinely are created by next-generation
sequencing methods.
This module provides a low-level wrapper around the htslib C-API as
using cython and a high-level API for convenient access to the data in
SAM/BAM formatted files. Also included is an interface to the samtools
command line utilities and the tabix C-API for reading compressed and
indexed tabular data.
The current version wraps htslib-1.3 and samtools-1.3.
See:
http://www.htslib.org
https://github.com/pysam-developers/pysam
http://pysam.readthedocs.org/en/stable
'''
import collections
import fnmatch
import glob
import hashlib
import os
import platform
import re
import shutil
import subprocess
import sys
from contextlib import contextmanager
from setuptools import Extension, setup
@contextmanager
def changedir(path):
save_dir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(save_dir)
def configure_library(library_dir, env_options=None, options=[]):
configure_script = os.path.join(library_dir, "configure")
if not os.path.exists(configure_script):
raise ValueError(
"configure script {} does not exist".format(configure_script))
def run_configure(option):
try:
retcode = subprocess.call(
" ".join(("./configure", option)),
shell=True)
if retcode != 0:
return False
else:
print (
"# successful configure run with options {}".format(
option))
return True
except OSError as e:
return False
with changedir(library_dir):
if env_options is not None:
if run_configure(env_options):
return True
for option in options:
if run_configure(option):
break
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern in and below
supplied root directory.
'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
def _update_pysam_files(cf, destdir):
'''update pysam files applying redirection of ouput'''
for filename in cf:
if not filename:
continue
dest = filename + ".pysam.c"
with open(filename) as infile:
with open(dest, "w") as outfile:
outfile.write('#include "pysam.h"\n\n')
outfile.write(
re.sub("stderr", "pysamerr", "".join(infile.readlines())))
with open(os.path.join(destdir, "pysam.h"), "w")as outfile:
outfile.write("""#ifndef PYSAM_H
#define PYSAM_H
#include "stdio.h"
extern FILE * pysamerr;
#endif
""")
IS_PYTHON3 = sys.version_info[0] >= 3
# How to link against HTSLIB
# separate: use included htslib and include in each extension
# module. No dependencies between modules and works
# with setup.py install, but wasteful in terms of
# memory and compilation time.
# shared: share chtslib across extension modules. This would be
# the ideal method, but currently requires
# LD_LIBRARY_PATH to be set correctly when using
# pysam.
# external: use shared libhts.so compiled outside of
# pysam
HTSLIB_MODE = "separate"
HTSLIB_LIBRARY_DIR = os.environ.get('HTSLIB_LIBRARY_DIR', None)
HTSLIB_INCLUDE_DIR = os.environ.get('HTSLIB_INCLUDE_DIR', None)
# collect pysam version
sys.path.insert(0, "pysam")
import version
version = version.__version__
# exclude sources that contains a main function
EXCLUDE = {
"samtools": ("razip.c",
"bgzip.c",
"main.c",
"calDepth.c",
"bam2bed.c",
"wgsim.c",
"md5fa.c",
"md5sum-lite.c",
"maq2sam.c",
"bamcheck.c",
"chk_indel.c",
"vcf-miniview.c",
"htslib-1.3", # do not import twice
"hfile_irods.c", # requires irods library
),
"bcftools": ("test",
"plugins",
"peakfit.c",
"peakfit.h",
# needs to renamed, name conflict with samtools reheader
"reheader.c",
"polysomy.c"),
"htslib": ('htslib/tabix.c',
'htslib/bgzip.c',
'htslib/htsfile.c',
'htslib/hfile_irods.c'),
}
# destination directories for import of samtools and tabix
samtools_dest = os.path.abspath("samtools")
if HTSLIB_LIBRARY_DIR:
# linking against a shared, externally installed htslib version, no
# sources required for htslib
htslib_sources = []
shared_htslib_sources = []
chtslib_sources = []
htslib_library_dirs = [HTSLIB_LIBRARY_DIR]
htslib_include_dirs = [HTSLIB_INCLUDE_DIR]
htslib_libraries = ['hts']
elif HTSLIB_MODE == 'separate':
# add to each pysam component a separately compiled
# htslib
htslib_sources = [
x for x in
glob.glob(os.path.join("htslib", "*.c")) +
glob.glob(os.path.join("htslib", "cram", "*.c"))
if x not in EXCLUDE["htslib"]]
shared_htslib_sources = htslib_sources
htslib_library_dirs = []
htslib_include_dirs = ['htslib']
htslib_libraries = []
elif HTSLIB_MODE == 'shared':
# link each pysam component against the same
# htslib built from sources included in the pysam
# package.
htslib_sources = []
shared_htslib_sources = [
x for x in
glob.glob(os.path.join("htslib", "*.c")) +
glob.glob(os.path.join("htslib", "cram", "*.c"))
if x not in EXCLUDE["htslib"]]
htslib_library_dirs = ['pysam']
htslib_include_dirs = ['htslib']
htslib_libraries = ['chtslib']
else:
raise ValueError("unknown HTSLIB value '%s'" % HTSLIB_MODE)
print ("# htslib mode is {}".format(HTSLIB_MODE))
if HTSLIB_MODE in ['shared', 'separate']:
configure_library(
"htslib",
os.environ.get('HTSLIB_COMPILE_OPTIONS', None),
["--enable-libcurl --enable-plugins",
"--enable-plugins",
""])
HTSLIB_MODE = "builtin"
# build config.py
with open(os.path.join("pysam", "config.py"), "w") as outf:
outf.write('HTSLIB_MODE = "{}"\n'.format(HTSLIB_MODE))
config_values = collections.defaultdict(int)
if HTSLIB_MODE == "builtin":
with open(os.path.join("htslib", "config.h")) as inf:
for line in inf:
if line.startswith("#define"):
key, value = re.match(
"#define (\S+)\s+(\S+)", line).groups()
config_values[key] = int(value)
for key in ["ENABLE_PLUGINS",
"HAVE_COMMONCRYPTO",
"HAVE_GMTIME_R",
"HAVE_HMAC",
"HAVE_IRODS",
"HAVE_LIBCURL",
"HAVE_MMAP"]:
outf.write("{} = {}\n".format(key, config_values[key]))
#################################################################
# Importing samtools and htslib
#
# For htslib, simply copy the whole release tar-ball
# into the directory "htslib" and recreate the file version.h
#
# rm -rf htslib
# mv download/htslib htslib
# git checkout -- htslib/version.h
# Edit the file htslib/version.h to set the right version number.
#
# For samtools, type:
# rm -rf samtools
# python setup.py import samtools download/samtools
# Manually, then:
# modify config.h to set compatibility flags
# change bamtk.c.pysam.c/main to bamtk.c.pysam.c/samtools_main
#
# For bcftools, type:
# rm -rf bedtools
# python setup.py import bedtools download/bedtools
if len(sys.argv) >= 2 and sys.argv[1] == "import":
if len(sys.argv) != 4:
raise ValueError("import requires dest src")
dest, srcdir = sys.argv[2:4]
if dest not in EXCLUDE:
raise ValueError("import expected one of %s" %
",".join(EXCLUDE.keys()))
exclude = EXCLUDE[dest]
destdir = os.path.abspath(dest)
srcdir = os.path.abspath(srcdir)
if not os.path.exists(srcdir):
raise IOError(
"source directory `%s` does not exist." % srcdir)
cfiles = locate("*.c", srcdir)
hfiles = locate("*.h", srcdir)
# remove unwanted files and htslib subdirectory.
cfiles = [x for x in cfiles if os.path.basename(x) not in exclude
and not re.search("htslib-", x)]
hfiles = [x for x in hfiles if os.path.basename(x) not in exclude
and not re.search("htslib-", x)]
ncopied = 0
def _compareAndCopy(src, srcdir, destdir, exclude):
d, f = os.path.split(src)
common_prefix = os.path.commonprefix((d, srcdir))
subdir = re.sub(common_prefix, "", d)[1:]
targetdir = os.path.join(destdir, subdir)
if not os.path.exists(targetdir):
os.makedirs(targetdir)
old_file = os.path.join(targetdir, f)
if os.path.exists(old_file):
md5_old = hashlib.md5(
"".join(open(old_file, "r").readlines())).digest()
md5_new = hashlib.md5(
"".join(open(src, "r").readlines())).digest()
if md5_old != md5_new:
raise ValueError(
"incompatible files for %s and %s" %
(old_file, src))
shutil.copy(src, targetdir)
return old_file
for src_file in hfiles:
_compareAndCopy(src_file, srcdir, destdir, exclude)
ncopied += 1
cf = []
for src_file in cfiles:
cf.append(_compareAndCopy(src_file,
srcdir,
destdir,
exclude))
ncopied += 1
sys.stdout.write(
"installed latest source code from %s: "
"%i files copied\n" % (srcdir, ncopied))
# redirect stderr to pysamerr and replace bam.h with a stub.
sys.stdout.write("applying stderr redirection\n")
_update_pysam_files(cf, destdir)
sys.exit(0)
if len(sys.argv) >= 2 and sys.argv[1] == "refresh":
sys.stdout.write("refreshing latest source code from .c to .pysam.c")
# redirect stderr to pysamerr and replace bam.h with a stub.
sys.stdout.write("applying stderr redirection")
for destdir in ('samtools', ):
pysamcfiles = locate("*.pysam.c", destdir)
for f in pysamcfiles:
os.remove(f)
cfiles = locate("*.c", destdir)
_update_pysam_files(cfiles, destdir)
sys.exit(0)
parts = ["samtools",
"bcftools",
"htslib",
"tabix",
"faidx",
"samfile",
"utils",
"alignmentfile",
"tabixproxies",
"vcf",
"bcf"]
try:
from cy_build import CyExtension as Extension, cy_build_ext as build_ext
except ImportError:
# no Cython available - use existing C code
cmdclass = {}
source_pattern = "pysam/c%s.c"
else:
# remove existing files to recompute
# necessary to be both compatible for python 2.7 and 3.3
if IS_PYTHON3:
for part in parts:
try:
os.unlink("pysam/c%s.c" % part)
except:
pass
source_pattern = "pysam/c%s.pyx"
cmdclass = {'build_ext': build_ext}
#######################################################
classifiers = """
Development Status :: 3 - Beta
Operating System :: MacOS :: MacOS X
Operating System :: POSIX
Operating System :: POSIX :: Linux
Operating System :: Unix
Programming Language :: Python
Topic :: Scientific/Engineering
Topic :: Scientific/Engineering :: Bioinformatics
"""
#######################################################
# Windows compatibility - currently broken
if platform.system() == 'Windows':
include_os = ['win32']
os_c_files = ['win32/getopt.c']
else:
include_os = []
os_c_files = []
#######################################################
# for python 3.4, see for example
# http://stackoverflow.com/questions/25587039/error-compiling-rpy2-on-python3-4-due-to-werror-declaration-after-statement
extra_compile_args = ["-Wno-error=declaration-after-statement"]
define_macros = []
chtslib = Extension(
"pysam.libchtslib",
[source_pattern % "htslib",
"pysam/htslib_util.c"] +
shared_htslib_sources +
os_c_files,
library_dirs=htslib_library_dirs,
include_dirs=["pysam"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
# samfile requires functions defined in bam_md.c
# for __advance_samtools method.
# Selected ones have been copied into samfile_utils.c
# Needs to be devolved somehow.
csamfile = Extension(
"pysam.csamfile",
[source_pattern % "samfile",
"pysam/htslib_util.c",
"pysam/samfile_util.c",
"samtools/kprobaln.c"] +
htslib_sources +
os_c_files,
library_dirs=htslib_library_dirs,
include_dirs=["pysam", "samtools"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
# alignmentfile requires functions defined in bam_md.c
# for __advance_samtools method.
# Selected ones have been copied into samfile_utils.c
# Needs to be devolved somehow.
calignmentfile = Extension(
"pysam.calignmentfile",
[source_pattern % "alignmentfile",
"pysam/htslib_util.c",
"pysam/samfile_util.c",
"samtools/kprobaln.c"] +
htslib_sources +
os_c_files,
library_dirs=htslib_library_dirs,
include_dirs=["pysam", "samtools"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
# alignmentfile requires functions defined in bam_md.c
# for __advance_samtools method.
# Selected ones have been copied into samfile_utils.c
# Needs to be devolved somehow.
calignedsegment = Extension(
"pysam.calignedsegment",
[source_pattern % "alignedsegment",
"pysam/htslib_util.c",
"pysam/samfile_util.c",
"samtools/kprobaln.c"] +
htslib_sources +
os_c_files,
library_dirs=htslib_library_dirs,
include_dirs=["pysam", "samtools"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
ctabix = Extension(
"pysam.ctabix",
[source_pattern % "tabix",
"pysam/tabix_util.c"] +
htslib_sources +
os_c_files,
library_dirs=["pysam"] + htslib_library_dirs,
include_dirs=["pysam"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
cutils = Extension(
"pysam.cutils",
[source_pattern % "utils", "pysam/pysam_util.c"] +
glob.glob(os.path.join("samtools", "*.pysam.c")) +
# glob.glob(os.path.join("samtools", "*", "*.pysam.c")) +
glob.glob(os.path.join("bcftools", "*.pysam.c")) +
# glob.glob(os.path.join("bcftools", "*", "*.pysam.c")) +
htslib_sources +
os_c_files,
library_dirs=["pysam"] + htslib_library_dirs,
include_dirs=["samtools", "bcftools", "pysam"] +
include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
cfaidx = Extension(
"pysam.cfaidx",
[source_pattern % "faidx"] +
htslib_sources +
os_c_files,
library_dirs=["pysam"] + htslib_library_dirs,
include_dirs=["pysam"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
ctabixproxies = Extension(
"pysam.ctabixproxies",
[source_pattern % "tabixproxies"] +
os_c_files,
library_dirs=[],
include_dirs=include_os,
libraries=["z"],
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
cvcf = Extension(
"pysam.cvcf",
[source_pattern % "vcf"] +
os_c_files,
library_dirs=[],
include_dirs=["htslib"] + include_os + htslib_include_dirs,
libraries=["z"],
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
cbcf = Extension(
"pysam.cbcf",
[source_pattern % "bcf"] +
htslib_sources +
os_c_files,
library_dirs=htslib_library_dirs,
include_dirs=["htslib"] + include_os + htslib_include_dirs,
libraries=["z"] + htslib_libraries,
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros
)
metadata = {
'name': "pysam",
'version': version,
'description': "pysam",
'long_description': __doc__,
'author': "Andreas Heger",
'author_email': "[email protected]",
'license': "MIT",
'platforms': "ALL",
'url': "https://github.com/pysam-developers/pysam",
'packages': ['pysam',
'pysam.include',
'pysam.include.htslib',
'pysam.include.htslib.htslib',
'pysam.include.samtools',
'pysam.include.bcftools',
'pysam.include.samtools.win32'],
'requires': ['cython (>=0.21)'],
'ext_modules': [chtslib,
csamfile,
calignmentfile,
calignedsegment,
ctabix,
ctabixproxies,
cvcf,
cbcf,
cfaidx,
cutils],
'cmdclass': cmdclass,
'package_dir': {'pysam': 'pysam',
'pysam.include.htslib': 'htslib',
'pysam.include.samtools': 'samtools',
'pysam.include.bcftools': 'bcftools'},
'package_data': {'': ['*.pxd', '*.h'], },
# do not pack in order to permit linking to csamtools.so
'zip_safe': False,
'use_2to3': True,
}
if __name__ == '__main__':
dist = setup(**metadata)