/
build.py
executable file
·526 lines (405 loc) · 18.5 KB
/
build.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
from __future__ import division, print_function, absolute_import
import numpy as np
import ailist
import pysam
import genome_info
import glob
import platform
import re
import subprocess
import sys
import sysconfig
import os
import collections
from contextlib import contextmanager
from distutils import log
from setuptools import setup, Command
from setuptools.dist import Distribution
from Cython.Build import cythonize
import shutil
# Local imports
from cy_build import CyExtension as Extension, cy_build_ext as build_ext
try:
import cython
HAVE_CYTHON = True
except ImportError:
HAVE_CYTHON = False
IS_PYTHON3 = sys.version_info.major >= 3
@contextmanager
def changedir(path):
save_dir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(save_dir)
def run_configure(option):
sys.stdout.flush()
try:
retcode = subprocess.call(
" ".join(("./configure", option)),
shell=True)
if retcode != 0:
return False
else:
return True
except OSError as e:
return False
def run_make_print_config():
stdout = subprocess.check_output(["make", "-s", "print-config"])
if IS_PYTHON3:
stdout = stdout.decode("ascii")
make_print_config = {}
for line in stdout.splitlines():
if "=" in line:
row = line.split("=")
if len(row) == 2:
make_print_config.update(
{row[0].strip(): row[1].strip()})
return make_print_config
@contextmanager
def set_compiler_envvars():
tmp_vars = []
for var in ['CC', 'CFLAGS', 'LDFLAGS']:
if var in os.environ:
print ("# ngsfragments: (env) {}={}".format(var, os.environ[var]))
elif var in sysconfig.get_config_vars():
value = sysconfig.get_config_var(var)
print ("# ngsfragments: (sysconfig) {}={}".format(var, value))
os.environ[var] = value
tmp_vars += [var]
try:
yield
finally:
for var in tmp_vars:
del os.environ[var]
def configure_library(library_dir, env_options=None, options=[]):
configure_script = os.path.join(library_dir, "configure")
on_rtd = os.environ.get("READTHEDOCS") == "True"
# RTD has no bzip2 development libraries installed:
if on_rtd:
env_options = "--disable-bz2"
if not os.path.exists(configure_script):
raise ValueError(
"configure script {} does not exist".format(configure_script))
with changedir(library_dir), set_compiler_envvars():
if env_options is not None:
if run_configure(env_options):
return env_options
for option in options:
if run_configure(option):
return option
return None
def distutils_dir_name(dname):
"""Returns the name of a distutils build directory
see: http://stackoverflow.com/questions/14320220/
testing-python-c-libraries-get-build-path
"""
f = "{dirname}.{platform}-{version[0]}.{version[1]}"
return f.format(dirname=dname,
platform=sysconfig.get_platform(),
version=sys.version_info)
class clean_ext(Command):
description = "clean up Cython temporary files"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
objs = glob.glob(os.path.join("ngsfragments", "libc*.c"))
if objs:
self.announce("removing 'ngsfragments/libc*.c' ({} Cython objects)".format(len(objs)), level=log.INFO)
for obj in objs:
os.remove(obj)
def build():
# Python 3
MyFileNotFoundError = FileNotFoundError
# Library name
libname = "ngsfragments"
# Build type
build_type = "optimized"
#build_type="debug"
# Descriptions of package
SHORTDESC = "Python package for Next Generation Sequencing fragment manipulation"
#DESC = """A python package for Next Generation Sequencing fragment manipulation."""
with open("README.md", "r") as fh:
long_description = fh.read()
# Directories (relative to the top-level directory where setup.py resides) in which to look for data files.
datadirs = ("tests", "data")
# File extensions to be considered as data files. (Literal, no wildcards.)
dataexts = (".py", ".pyx", ".pxd", ".c", ".h", ".h5", ".txt", ".bed", ".gz", ".bed.gz")
# Standard documentation to detect (and package if it exists).
standard_docs = ["README", "LICENSE", "TODO", "CHANGELOG", "AUTHORS"]
standard_doc_exts = [".md", ".rst", ".txt", ""]
#########################################################
# Init
#########################################################
# check for Python 2.7 or later
if sys.version_info < (2,7):
sys.exit('Sorry, Python < 2.7 is not supported')
# Gather user-defined data files
datafiles = []
getext = lambda filename: os.path.splitext(filename)[1]
for datadir in datadirs:
datafiles.extend( [(root, [os.path.join(root, f) for f in files if getext(f) in dataexts])
for root, dirs, files in os.walk(datadir)] )
# Add standard documentation (README et al.), if any, to data files
detected_docs = []
for docname in standard_docs:
for ext in standard_doc_exts:
filename = "".join( (docname, ext) ) # relative to the directory in which setup.py resides
if os.path.isfile(filename):
detected_docs.append(filename)
datafiles.append( ('.', detected_docs) )
# Extract __version__ from the package __init__.py
import ast
init_py_path = os.path.join(libname, '__init__.py')
version = '0.0.0'
try:
with open(init_py_path) as f:
for line in f:
if line.startswith('__version__'):
version = ast.parse(line).body[0].value.s
break
else:
print( "WARNING: Version information not found in '%s', using placeholder '%s'" % (init_py_path, version), file=sys.stderr )
except MyFileNotFoundError:
print( "WARNING: Could not find file '%s', using placeholder version information '%s'" % (init_py_path, version), file=sys.stderr )
#########################################################
# Set up modules
#########################################################
# How to link against HTSLIB
# shared: build shared chtslib from builtin htslib code.
# external: use shared libhts.so compiled outside of
# ngsfragments
# 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. Fallback if shared module compilation
# fails.
HTSLIB_MODE = os.environ.get("HTSLIB_MODE", "shared")
HTSLIB_LIBRARY_DIR = os.environ.get("HTSLIB_LIBRARY_DIR", None)
HTSLIB_INCLUDE_DIR = os.environ.get("HTSLIB_INCLUDE_DIR", None)
HTSLIB_CONFIGURE_OPTIONS = os.environ.get("HTSLIB_CONFIGURE_OPTIONS", None)
HTSLIB_SOURCE = None
package_list = ['ngsfragments',
'ngsfragments.io',
'ngsfragments.io.parse_sam',
'ngsfragments.peak_calling',
'ngsfragments.segment',
'ngsfragments.plot',
'ngsfragments.coverage',
'ngsfragments.sequence',
'ngsfragments.metrics',
'ngsfragments.correct']
package_dirs = {'ngsfragments': 'ngsfragments',
'ngsfragments.io': 'ngsfragments/io',
'ngsfragments.io.parse_sam': 'ngsfragments/io/parse_sam',
'ngsfragments.peak_calling': 'ngsfragments/peak_calling',
'ngsfragments.segment': 'ngsfragments/segment',
'ngsfragments.plot': 'ngsfragments/plot',
'ngsfragments.coverage': 'ngsfragments/coverage',
'ngsfragments.sequence': 'ngsfragments/sequence',
'ngsfragments.metrics': 'ngsfragments/metrics',
'ngsfragments.correct': 'ngsfragments/correct'}
# list of config files that will be automatically generated should
# they not already exist or be created by configure scripts in the
# subpackages.
config_headers = []
# If cython is available, the ngsfragments will be built using cython from
# the .pyx files. If no cython is available, the C-files included in the
# distribution will be used.
if HAVE_CYTHON:
print ("# ngsfragments: cython is available - using cythonize if necessary")
source_pattern = "ngsfragments/libc%s.pyx"
else:
print ("# ngsfragments: no cython available - using pre-compiled C")
source_pattern = "ngsfragments/libc%s.c"
# Exit if there are no pre-compiled files and no cython available
fn = source_pattern % "htslib"
if not os.path.exists(fn):
raise ValueError(
"no cython installed, but can not find {}."
"Make sure that cython is installed when building "
"from the repository"
.format(fn))
print ("# ngsfragments: htslib mode is {}".format(HTSLIB_MODE))
print ("# ngsfragments: HTSLIB_CONFIGURE_OPTIONS={}".format(
HTSLIB_CONFIGURE_OPTIONS))
htslib_configure_options = None
if HTSLIB_MODE in ['shared', 'separate']:
package_list += ['ngsfragments.htslib',
'ngsfragments.htslib.htslib']
package_dirs.update({'ngsfragments.htslib':'htslib'})
htslib_configure_options = configure_library(
"htslib",
HTSLIB_CONFIGURE_OPTIONS,
["--enable-libcurl",
"--disable-libcurl"])
HTSLIB_SOURCE = "builtin"
print ("# ngsfragments: htslib configure options: {}".format(
str(htslib_configure_options)))
config_headers += ["htslib/config.h"]
if htslib_configure_options is None:
# create empty config.h file
with open("htslib/config.h", "w") as outf:
outf.write(
"/* empty config.h created by ngsfragments */\n")
outf.write(
"/* conservative compilation options */\n")
with changedir("htslib"):
htslib_make_options = run_make_print_config()
for key, value in htslib_make_options.items():
print ("# ngsfragments: htslib_config {}={}".format(key, value))
external_htslib_libraries = ['z']
if "LIBS" in htslib_make_options:
external_htslib_libraries.extend(
[re.sub("^-l", "", x) for x in htslib_make_options["LIBS"].split(" ") if x.strip()])
shared_htslib_sources = [re.sub("\.o", ".c", os.path.join("htslib", x))
for x in
htslib_make_options["LIBHTS_OBJS"].split(" ")]
htslib_sources = []
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]
external_htslib_libraries = ['z', 'hts']
elif HTSLIB_MODE == 'separate':
# add to each ngsfragments component a separately compiled
# htslib
htslib_sources = shared_htslib_sources
shared_htslib_sources = htslib_sources
htslib_library_dirs = []
htslib_include_dirs = ['htslib']
elif HTSLIB_MODE == 'shared':
# link each ngsfragments component against the same
# htslib built from sources included in the ngsfragments
# package.
htslib_library_dirs = [
"ngsfragments", # when using setup.py develop?
".", # when using setup.py develop?
os.path.join("build", distutils_dir_name("lib"), "ngsfragments")]
htslib_include_dirs = ['htslib']
else:
raise ValueError("unknown HTSLIB value '%s'" % HTSLIB_MODE)
# build config.py
with open(os.path.join("ngsfragments", "config.py"), "w") as outf:
outf.write('HTSLIB = "{}"\n'.format(HTSLIB_SOURCE))
config_values = collections.defaultdict(int)
if HTSLIB_SOURCE == "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] = value
for key in ["ENABLE_GCS",
"ENABLE_PLUGINS",
"ENABLE_S3",
"HAVE_COMMONCRYPTO",
"HAVE_HMAC",
"HAVE_LIBBZ2",
"HAVE_LIBCURL",
"HAVE_LIBDEFLATE",
"HAVE_LIBLZMA",
"HAVE_MMAP"]:
outf.write("{} = {}\n".format(key, config_values[key]))
print ("# ngsfragments: config_option: {}={}".format(key, config_values[key]))
# create empty config.h files if they have not been created automatically
# or created by the user:
for fn in config_headers:
if not os.path.exists(fn):
with open(fn, "w") as outf:
outf.write(
"/* empty config.h created by ngsfragments */\n")
outf.write(
"/* conservative compilation options */\n")
#######################################################
# Windows compatibility - untested
if platform.system() == 'Windows':
include_os = ['win32']
os_c_files = ['win32/getopt.c']
extra_compile_args = []
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-unused",
"-Wno-strict-prototypes",
"-Wno-sign-compare",
"-Wno-error=declaration-after-statement"]
define_macros = []
suffix = sysconfig.get_config_var('EXT_SUFFIX')
if not suffix:
suffix = sysconfig.get_config_var('SO')
libraries_for_ngs_module = external_htslib_libraries
# The list below uses the union of include_dirs and library_dirs for
# reasons of simplicity.
modules = [
dict(name="ngsfragments.io.parse_sam.ReadSam",
sources=["ngsfragments.io.parse_sam.ReadSam".replace(".", os.path.sep)+".pyx"] + shared_htslib_sources + os_c_files,
libraries=external_htslib_libraries),
dict(name="ngsfragments.peak_calling.RunningMedian",
sources=["ngsfragments.peak_calling.RunningMedian".replace(".", os.path.sep)+".pyx"] + htslib_sources + os_c_files,
libraries=external_htslib_libraries),
dict(name="ngsfragments.peak_calling.RunningMean",
sources=["ngsfragments.peak_calling.RunningMean".replace(".", os.path.sep)+".pyx"] + htslib_sources + os_c_files,
libraries=external_htslib_libraries),
dict(name="ngsfragments.peak_calling.CallPeaks",
sources=["ngsfragments.peak_calling.CallPeaks".replace(".", os.path.sep)+".pyx"] + htslib_sources + os_c_files,
libraries=external_htslib_libraries),
dict(name="ngsfragments.segment.smooth_cnv.smooth_cnv",
sources=["ngsfragments.segment.smooth_cnv.smooth_cnv".replace(".", os.path.sep)+".pyx"] + htslib_sources + os_c_files,
libraries=external_htslib_libraries),
dict(name="ngsfragments.correct.cylowess.cylowess",
sources=["ngsfragments.correct.cylowess.cylowess".replace(".", os.path.sep)+".pyx"] + htslib_sources + os_c_files,
libraries=external_htslib_libraries),
]
common_options = dict(
language="c",
extra_compile_args=extra_compile_args,
define_macros=define_macros + pysam.get_defines(),
language_level=3,
# for out-of-tree compilation, use absolute paths
library_dirs=[os.path.abspath(x) for x in ["ngsfragments"] + htslib_library_dirs],
include_dirs=[os.path.abspath(x) for x in htslib_include_dirs + \
["ngsfragments", ".", np.get_include(), ailist.get_include(), genome_info.get_include()] + include_os + pysam.get_include()])
# add common options (in python >3.5, could use n = {**a, **b}
for module in modules:
module.update(**common_options)
# declare Cython extension modules here
#ext_module_interval = declare_cython_extension( "ailist.Interval_core", use_openmp=False , include_dirs=include_dirs )
#ext_module_ailist = declare_cython_extension( "ailist.AIList_core", use_openmp=False , include_dirs=include_dirs )
#ext_module_aiarray_labeled = declare_cython_extension( "ailist.LabeledIntervalArray_core", use_openmp=False , include_dirs=include_dirs )
#ext_module_array_query = declare_cython_extension( "ailist.array_query_core", use_openmp=False , include_dirs=include_dirs )
# this is mainly to allow a manual logical ordering of the declared modules
ext_modules = [Extension(**opts) for opts in modules]
# Call cythonize() explicitly, as recommended in the Cython documentation. See
# This will favor Cython's own handling of '.pyx' sources over that provided by setuptools.
# cythonize() just performs the Cython-level processing, and returns a list of Extension objects.
#ext_modules = cythonize(ext_modules, include_path=include_dirs, gdb_debug=False, language_level=3)
distribution = Distribution({"name": "extended", "ext_modules": ext_modules, 'clean_ext': clean_ext})
distribution.package_dir = "extended"
cmd = build_ext(distribution)
cmd.ensure_finalized()
cmd.run()
# Copy built extensions back to the project
#files = os.listdir(cmd.build_lib)
#print(files)
for output in cmd.get_outputs():
relative_extension = os.path.relpath(output, cmd.build_lib)
shutil.copyfile(output, relative_extension)
# mode = os.stat(relative_extension).st_mode
# mode |= (mode & 0o444) >> 2
# os.chmod(relative_extension, mode)
if __name__ == "__main__":
build()