| 1 |
|
| 2 |
|
| 3 |
'''pysam --- a Python package for reading, manipulating, and writing |
| 4 |
genomic data sets. |
| 5 |
|
| 6 |
pysam is a lightweight wrapper of the HTSlib API and provides facilities |
| 7 |
to read and write SAM/BAM/CRAM/VCF/BCF/BED/GFF/GTF/FASTA/FASTQ files |
| 8 |
as well as access to the command-line functionality of samtools and bcftools. |
| 9 |
The module supports compression and random access through indexing. |
| 10 |
|
| 11 |
This module provides a low-level wrapper around HTSlib's C API using Cython |
| 12 |
and a high-level API for convenient access to the data within standard genomic |
| 13 |
file formats. |
| 14 |
''' |
| 15 |
|
| 16 |
import collections |
| 17 |
import glob |
| 18 |
import logging |
| 19 |
import os |
| 20 |
import platform |
| 21 |
import re |
| 22 |
import subprocess |
| 23 |
import sys |
| 24 |
import sysconfig |
| 25 |
from contextlib import contextmanager |
| 26 |
from setuptools import setup, Command |
| 27 |
from setuptools.command.sdist import sdist |
| 28 |
from setuptools.extension import Extension |
| 29 |
|
| 30 |
try: |
| 31 |
from setuptools.errors import CompileError, LinkError |
| 32 |
except ImportError: |
| 33 |
from distutils.errors import CompileError, LinkError |
| 34 |
|
| 35 |
try: |
| 36 |
from Cython.Distutils import build_ext |
| 37 |
except ImportError: |
| 38 |
from setuptools.command.build_ext import build_ext |
| 39 |
|
| 40 |
try: |
| 41 |
import cython |
| 42 |
HAVE_CYTHON = True |
| 43 |
except ImportError: |
| 44 |
HAVE_CYTHON = False |
| 45 |
|
| 46 |
IS_DARWIN = platform.system() == 'Darwin' |
| 47 |
|
| 48 |
log = logging.getLogger('pysam') |
| 49 |
|
| 50 |
|
| 51 |
@contextmanager |
| 52 |
def changedir(path): |
| 53 |
save_dir = os.getcwd() |
| 54 |
os.chdir(path) |
| 55 |
try: |
| 56 |
yield |
| 57 |
finally: |
| 58 |
os.chdir(save_dir) |
| 59 |
|
| 60 |
|
| 61 |
def run_configure(option): |
| 62 |
sys.stdout.flush() |
| 63 |
try: |
| 64 |
retcode = subprocess.call( |
| 65 |
" ".join(("./configure", option)), |
| 66 |
shell=True) |
| 67 |
if retcode != 0: |
| 68 |
return False |
| 69 |
else: |
| 70 |
return True |
| 71 |
except OSError as e: |
| 72 |
return False |
| 73 |
|
| 74 |
|
| 75 |
def run_make(targets): |
| 76 |
sys.stdout.flush() |
| 77 |
subprocess.check_call([os.environ.get("MAKE", "make")] + targets) |
| 78 |
|
| 79 |
|
| 80 |
def run_make_print_config(): |
| 81 |
stdout = subprocess.check_output(["make", "-s", "print-config"], encoding="ascii") |
| 82 |
|
| 83 |
make_print_config = {} |
| 84 |
for line in stdout.splitlines(): |
| 85 |
if "=" in line: |
| 86 |
row = line.split("=") |
| 87 |
if len(row) == 2: |
| 88 |
make_print_config.update( |
| 89 |
{row[0].strip(): row[1].strip()}) |
| 90 |
return make_print_config |
| 91 |
|
| 92 |
|
| 93 |
def run_nm_defined_symbols(objfile): |
| 94 |
stdout = subprocess.check_output(["nm", "-g", "-P", objfile], encoding="ascii") |
| 95 |
|
| 96 |
symbols = set() |
| 97 |
for line in stdout.splitlines(): |
| 98 |
(sym, symtype) = line.split()[:2] |
| 99 |
if symtype not in "UFNWw": |
| 100 |
if IS_DARWIN: |
| 101 |
|
| 102 |
symbols.add(sym[1:] if sym.startswith("_") else sym) |
| 103 |
else: |
| 104 |
|
| 105 |
if sym[0] not in "_$.@": symbols.add(sym) |
| 106 |
|
| 107 |
|
| 108 |
symbols.discard("__pyx_CommonTypesMetaclass_get_module") |
| 109 |
|
| 110 |
return symbols |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
def build_config_dict(ext): |
| 118 |
def env(var): |
| 119 |
return [os.environ[var]] if var in os.environ else [] |
| 120 |
|
| 121 |
def sc(var): |
| 122 |
value = sysconfig.get_config_var(var) |
| 123 |
return [value] if value is not None else [] |
| 124 |
|
| 125 |
def optionise(option, valuelist): |
| 126 |
def quote(s): return "'"+s+"'" if " " in s else s |
| 127 |
return list(quote(option+v) for v in valuelist) |
| 128 |
|
| 129 |
def kvtuples(pairlist): |
| 130 |
def appendoptvalue(t): return t[0] if t[1] is None else t[0]+"="+t[1] |
| 131 |
return map(appendoptvalue, pairlist) |
| 132 |
|
| 133 |
|
| 134 |
cc = (env('CC') + sc('CC') + ['gcc'])[0] |
| 135 |
|
| 136 |
|
| 137 |
cppflags = " ".join(env('CPPFLAGS') + optionise('-I', ext.include_dirs) + |
| 138 |
optionise('-D', kvtuples(ext.define_macros)) + |
| 139 |
optionise('-U', ext.undef_macros)) |
| 140 |
|
| 141 |
cflags = " ".join(sc('CFLAGS') + env('CFLAGS') + sc('CCSHARED') + |
| 142 |
ext.extra_compile_args) |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
ldflags = " ".join(sc('LDFLAGS') + env('LDFLAGS') + env('CFLAGS') + |
| 147 |
optionise('-L', ext.library_dirs) + |
| 148 |
ext.extra_link_args) |
| 149 |
|
| 150 |
|
| 151 |
libs = " ".join(optionise('-l', ext.libraries)) |
| 152 |
|
| 153 |
return { 'CC': cc, 'CPPFLAGS': cppflags, 'CFLAGS': cflags, |
| 154 |
'LDFLAGS': ldflags, 'LIBS': libs } |
| 155 |
|
| 156 |
|
| 157 |
def write_configvars_header(filename, ext, prefix): |
| 158 |
config = build_config_dict(ext) |
| 159 |
if prefix != 'HTS': |
| 160 |
config['HTSDIR'] = '(unused)' |
| 161 |
config['CURSES_LIB'] = '(unused)' |
| 162 |
|
| 163 |
log.info("creating %s for '%s' extension", filename, ext.name) |
| 164 |
with open(filename, "w") as outf: |
| 165 |
for var, value in config.items(): |
| 166 |
outf.write(f'#define {prefix}_{var} "{value}"\n') |
| 167 |
|
| 168 |
|
| 169 |
@contextmanager |
| 170 |
def set_compiler_envvars(): |
| 171 |
tmp_vars = [] |
| 172 |
for var in ['CC', 'CFLAGS', 'LDFLAGS']: |
| 173 |
if var in os.environ: |
| 174 |
if var == 'CFLAGS' and 'CCSHARED' in sysconfig.get_config_vars(): |
| 175 |
os.environ[var] += ' ' + sysconfig.get_config_var('CCSHARED') |
| 176 |
print(f"# pysam: (env) {var}={os.environ[var]}") |
| 177 |
elif var in sysconfig.get_config_vars(): |
| 178 |
value = sysconfig.get_config_var(var) |
| 179 |
if var == 'CFLAGS' and 'CCSHARED' in sysconfig.get_config_vars(): |
| 180 |
value += ' ' + sysconfig.get_config_var('CCSHARED') |
| 181 |
print(f"# pysam: (sysconfig) {var}={value}") |
| 182 |
os.environ[var] = value |
| 183 |
tmp_vars += [var] |
| 184 |
|
| 185 |
try: |
| 186 |
yield |
| 187 |
finally: |
| 188 |
for var in tmp_vars: |
| 189 |
del os.environ[var] |
| 190 |
|
| 191 |
|
| 192 |
def format_macro_option(name, value): |
| 193 |
return f"-D{name}={value}" if value is not None else f"-D{name}" |
| 194 |
|
| 195 |
|
| 196 |
def configure_library(library_dir, env_options=None, options=[]): |
| 197 |
|
| 198 |
configure_script = os.path.join(library_dir, "configure") |
| 199 |
|
| 200 |
on_rtd = os.environ.get("READTHEDOCS") == "True" |
| 201 |
|
| 202 |
if on_rtd: |
| 203 |
env_options = "--disable-bz2" |
| 204 |
|
| 205 |
if not os.path.exists(configure_script): |
| 206 |
raise ValueError(f"configure script {configure_script!r} does not exist") |
| 207 |
|
| 208 |
with changedir(library_dir), set_compiler_envvars(): |
| 209 |
if env_options is not None: |
| 210 |
if run_configure(env_options): |
| 211 |
return env_options |
| 212 |
|
| 213 |
for option in options: |
| 214 |
if run_configure(option): |
| 215 |
return option |
| 216 |
|
| 217 |
return None |
| 218 |
|
| 219 |
|
| 220 |
def get_pysam_version(): |
| 221 |
sys.path.insert(0, "pysam") |
| 222 |
import version |
| 223 |
return version.__version__ |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
class cythonize_sdist(sdist): |
| 228 |
|
| 229 |
if not any(opt[0] == 'owner=' for opt in sdist.user_options): |
| 230 |
sdist.user_options.append(('owner=', 'u', 'Specify owner inside tar')) |
| 231 |
if not any(opt[0] == 'group=' for opt in sdist.user_options): |
| 232 |
sdist.user_options.append(('group=', 'g', 'Specify group inside tar')) |
| 233 |
|
| 234 |
def run(self): |
| 235 |
from Cython.Build import cythonize |
| 236 |
cythonize(self.distribution.ext_modules) |
| 237 |
super().run() |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
class CyExtension(Extension): |
| 243 |
def __init__(self, *args, **kwargs): |
| 244 |
self._init_func = kwargs.pop("init_func", None) |
| 245 |
self._prebuild_func = kwargs.pop("prebuild_func", None) |
| 246 |
super().__init__(*args, **kwargs) |
| 247 |
|
| 248 |
def extend_includes(self, includes): |
| 249 |
self.include_dirs.extend(includes) |
| 250 |
|
| 251 |
def extend_macros(self, macros): |
| 252 |
self.define_macros.extend(macros) |
| 253 |
|
| 254 |
def extend_extra_objects(self, objs): |
| 255 |
self.extra_objects.extend(objs) |
| 256 |
|
| 257 |
|
| 258 |
class cy_build_ext(build_ext): |
| 259 |
def check_ext_symbol_conflicts(self): |
| 260 |
"""Checks for symbols defined in multiple extension modules, |
| 261 |
which can lead to crashes due to incorrect functions being invoked. |
| 262 |
Avoid by adding an appropriate #define to import/pysam.h or in |
| 263 |
unusual cases adding another rewrite rule to devtools/import.py. |
| 264 |
""" |
| 265 |
symbols = dict() |
| 266 |
for ext in self.distribution.ext_modules: |
| 267 |
for sym in run_nm_defined_symbols(self.get_ext_fullpath(ext.name)): |
| 268 |
symbols.setdefault(sym, []).append(ext.name.lstrip('pysam.')) |
| 269 |
|
| 270 |
errors = 0 |
| 271 |
for (sym, objs) in symbols.items(): |
| 272 |
if (len(objs) > 1): |
| 273 |
log.error("conflicting symbol (%s): %s", " ".join(objs), sym) |
| 274 |
errors += 1 |
| 275 |
|
| 276 |
if errors > 0: raise LinkError("symbols defined in multiple extensions") |
| 277 |
|
| 278 |
def c99_compile_args(self): |
| 279 |
"""Determines whether any compiler flags are needed to ensure C99 compilation.""" |
| 280 |
compiler = getattr(self.compiler, "compiler", "C compiler") |
| 281 |
if isinstance(compiler, list): compiler = compiler[0] |
| 282 |
log.info("checking for %s option to enable C99 features...", compiler) |
| 283 |
for flags in [None, ["-std=c99"], ["-std=gnu99"]]: |
| 284 |
try: |
| 285 |
self.compiler.compile(["pysam/conftest_cstd.c"], output_dir=self.build_temp, extra_preargs=flags) |
| 286 |
log.info("%s option to enable C99 features: %s", compiler, " ".join(flags) if flags else "none needed") |
| 287 |
return flags |
| 288 |
except CompileError: |
| 289 |
log.info("(ignoring errors from test probes)") |
| 290 |
|
| 291 |
log.error("%s cannot compile C99 source code", compiler) |
| 292 |
return None |
| 293 |
|
| 294 |
def run(self): |
| 295 |
if sys.platform == 'darwin': |
| 296 |
ldshared = os.environ.get('LDSHARED', sysconfig.get_config_var('LDSHARED')) |
| 297 |
os.environ['LDSHARED'] = ldshared.replace('-bundle', '') |
| 298 |
|
| 299 |
super().run() |
| 300 |
try: |
| 301 |
if HTSLIB_MODE != 'separate': |
| 302 |
self.check_ext_symbol_conflicts() |
| 303 |
except OSError as e: |
| 304 |
log.warning("skipping symbol collision check (invoking nm failed: %s)", e) |
| 305 |
except subprocess.CalledProcessError: |
| 306 |
log.warning("skipping symbol collision check (invoking nm failed)") |
| 307 |
|
| 308 |
def build_extensions(self): |
| 309 |
c99_flags = self.c99_compile_args() |
| 310 |
if c99_flags: |
| 311 |
executables = {} |
| 312 |
for executable in ["compiler", "compiler_so"]: |
| 313 |
command = getattr(self.compiler, executable, None) |
| 314 |
if command: |
| 315 |
if isinstance(command, list): executables[executable] = command + c99_flags |
| 316 |
elif isinstance(command, str): executables[executable] = f"{command} {' '.join(c99_flags)}" |
| 317 |
self.compiler.set_executables(**executables) |
| 318 |
|
| 319 |
super().build_extensions() |
| 320 |
|
| 321 |
def build_extension(self, ext): |
| 322 |
|
| 323 |
if isinstance(ext, CyExtension) and ext._init_func: |
| 324 |
ext._init_func(ext) |
| 325 |
|
| 326 |
if not self.inplace: |
| 327 |
ext.library_dirs.append(os.path.join(self.build_lib, "pysam")) |
| 328 |
|
| 329 |
if sys.platform == 'darwin': |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
relative_module_path = ext.name.replace(".", os.sep) + sysconfig.get_config_var('EXT_SUFFIX') |
| 336 |
library_path = os.path.join( |
| 337 |
"@rpath", os.path.basename(relative_module_path) |
| 338 |
) |
| 339 |
|
| 340 |
if not ext.extra_link_args: |
| 341 |
ext.extra_link_args = [] |
| 342 |
ext.extra_link_args += ['-dynamiclib', |
| 343 |
'-rpath', '@loader_path', |
| 344 |
'-Wl,-headerpad_max_install_names', |
| 345 |
f'-Wl,-install_name,{library_path}', |
| 346 |
'-Wl,-x'] |
| 347 |
else: |
| 348 |
if not ext.extra_link_args: |
| 349 |
ext.extra_link_args = [] |
| 350 |
|
| 351 |
ext.extra_link_args += ['-Wl,-rpath,$ORIGIN'] |
| 352 |
|
| 353 |
if isinstance(ext, CyExtension) and ext._prebuild_func: |
| 354 |
ext._prebuild_func(ext, self.force) |
| 355 |
|
| 356 |
super().build_extension(ext) |
| 357 |
|
| 358 |
|
| 359 |
class clean_ext(Command): |
| 360 |
description = "clean up Cython temporary files" |
| 361 |
user_options = [] |
| 362 |
|
| 363 |
def initialize_options(self): |
| 364 |
pass |
| 365 |
|
| 366 |
def finalize_options(self): |
| 367 |
pass |
| 368 |
|
| 369 |
def run(self): |
| 370 |
objs = glob.glob(os.path.join("pysam", "libc*.c")) |
| 371 |
if objs: |
| 372 |
log.info("removing 'pysam/libc*.c' (%s Cython objects)", len(objs)) |
| 373 |
for obj in objs: |
| 374 |
os.remove(obj) |
| 375 |
|
| 376 |
headers = (glob.glob(os.path.join("htslib", "*config*.h")) + |
| 377 |
glob.glob(os.path.join("samtools", "*config*.h")) + |
| 378 |
glob.glob(os.path.join("bcftools", "*config*.h"))) |
| 379 |
if headers: |
| 380 |
log.info("removing '*/*config*.h' (%s generated headers)", len(headers)) |
| 381 |
for header in headers: |
| 382 |
os.remove(header) |
| 383 |
|
| 384 |
objects = (glob.glob(os.path.join("htslib", "*.[oa]")) + |
| 385 |
glob.glob(os.path.join("htslib", "cram", "*.o")) + |
| 386 |
glob.glob(os.path.join("htslib", "htscodecs", "htscodecs", "*.o"))) |
| 387 |
if objects: |
| 388 |
log.info("removing 'htslib/**/*.o' and libhts.a (%s objects)", len(objects)) |
| 389 |
for obj in objects: |
| 390 |
os.remove(obj) |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
HTSLIB_MODE = os.environ.get("HTSLIB_MODE", "shared") |
| 404 |
HTSLIB_LIBRARY_DIR = os.environ.get("HTSLIB_LIBRARY_DIR", None) |
| 405 |
HTSLIB_INCLUDE_DIR = os.environ.get("HTSLIB_INCLUDE_DIR", None) |
| 406 |
HTSLIB_CONFIGURE_OPTIONS = os.environ.get("HTSLIB_CONFIGURE_OPTIONS", None) |
| 407 |
HTSLIB_SOURCE = None |
| 408 |
|
| 409 |
package_list = ['pysam', |
| 410 |
'pysam.include', |
| 411 |
'pysam.include.samtools', |
| 412 |
'pysam.include.bcftools'] |
| 413 |
package_dirs = {'pysam': 'pysam', |
| 414 |
'pysam.include.samtools': 'samtools', |
| 415 |
'pysam.include.bcftools': 'bcftools'} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
config_headers = ["samtools/config.h", |
| 421 |
"bcftools/config.h"] |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
if HAVE_CYTHON: |
| 427 |
print(f"# pysam: Cython {cython.__version__} is available - using cythonize if necessary") |
| 428 |
source_pattern = "pysam/libc%s.pyx" |
| 429 |
else: |
| 430 |
print("# pysam: no Cython available - using pre-compiled C") |
| 431 |
source_pattern = "pysam/libc%s.c" |
| 432 |
|
| 433 |
|
| 434 |
fn = source_pattern % "htslib" |
| 435 |
if not os.path.exists(fn): |
| 436 |
raise ValueError( |
| 437 |
f"no Cython installed, but cannot find {fn}. " |
| 438 |
"Make sure that Cython is installed when building from the repository") |
| 439 |
|
| 440 |
print(f"# pysam: htslib mode is {HTSLIB_MODE}") |
| 441 |
print(f"# pysam: HTSLIB_CONFIGURE_OPTIONS={HTSLIB_CONFIGURE_OPTIONS}") |
| 442 |
htslib_configure_options = None |
| 443 |
|
| 444 |
if HTSLIB_MODE in ['shared', 'separate']: |
| 445 |
package_list += ['pysam.include.htslib', |
| 446 |
'pysam.include.htslib.htslib'] |
| 447 |
package_dirs.update({'pysam.include.htslib':'htslib'}) |
| 448 |
|
| 449 |
htslib_configure_options = configure_library( |
| 450 |
"htslib", |
| 451 |
HTSLIB_CONFIGURE_OPTIONS, |
| 452 |
["--enable-libcurl", |
| 453 |
"--disable-libcurl"]) |
| 454 |
|
| 455 |
HTSLIB_SOURCE = "builtin" |
| 456 |
print(f"# pysam: htslib configure options: {htslib_configure_options}") |
| 457 |
|
| 458 |
config_headers += ["htslib/config.h"] |
| 459 |
if htslib_configure_options is None: |
| 460 |
|
| 461 |
with open("htslib/config.h", "w") as outf: |
| 462 |
outf.write( |
| 463 |
"/* empty config.h created by pysam */\n") |
| 464 |
outf.write( |
| 465 |
"/* conservative compilation options */\n") |
| 466 |
|
| 467 |
with changedir("htslib"): |
| 468 |
htslib_make_options = run_make_print_config() |
| 469 |
|
| 470 |
for key, value in htslib_make_options.items(): |
| 471 |
print(f"# pysam: htslib_config {key}={value}") |
| 472 |
|
| 473 |
external_htslib_libraries = ['z'] |
| 474 |
if "LIBS" in htslib_make_options: |
| 475 |
external_htslib_libraries.extend( |
| 476 |
[re.sub("^-l", "", x) for x in htslib_make_options["LIBS"].split(" ") if x.strip()]) |
| 477 |
|
| 478 |
if HTSLIB_LIBRARY_DIR: |
| 479 |
|
| 480 |
|
| 481 |
htslib_objects = [] |
| 482 |
separate_htslib_objects = [] |
| 483 |
chtslib_sources = [] |
| 484 |
htslib_library_dirs = [HTSLIB_LIBRARY_DIR] |
| 485 |
htslib_include_dirs = [HTSLIB_INCLUDE_DIR] |
| 486 |
external_htslib_libraries = ['z', 'hts'] |
| 487 |
elif HTSLIB_MODE == 'separate': |
| 488 |
|
| 489 |
|
| 490 |
htslib_objects = ['htslib/libhts.a'] |
| 491 |
separate_htslib_objects = ['htslib/libhts.a'] |
| 492 |
htslib_library_dirs = [] |
| 493 |
htslib_include_dirs = ['htslib'] |
| 494 |
elif HTSLIB_MODE == 'shared': |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
htslib_objects = [os.path.join("htslib", x) |
| 502 |
for x in htslib_make_options["LIBHTS_OBJS"].split(" ")] |
| 503 |
separate_htslib_objects = [] |
| 504 |
|
| 505 |
htslib_library_dirs = ["."] |
| 506 |
htslib_include_dirs = ['htslib'] |
| 507 |
else: |
| 508 |
raise ValueError(f"unknown HTSLIB value {HTSLIB_MODE!r}") |
| 509 |
|
| 510 |
|
| 511 |
with open(os.path.join("pysam", "config.py"), "w") as outf: |
| 512 |
outf.write(f'HTSLIB = "{HTSLIB_SOURCE}"\n') |
| 513 |
config_values = collections.defaultdict(int) |
| 514 |
|
| 515 |
if HTSLIB_SOURCE == "builtin": |
| 516 |
with open(os.path.join("htslib", "config.h")) as inf: |
| 517 |
for line in inf: |
| 518 |
if line.startswith("#define"): |
| 519 |
key, value = re.match( |
| 520 |
r"#define (\S+)\s+(\S+)", line).groups() |
| 521 |
config_values[key] = value |
| 522 |
for key in ["ENABLE_GCS", |
| 523 |
"ENABLE_PLUGINS", |
| 524 |
"ENABLE_S3", |
| 525 |
"HAVE_COMMONCRYPTO", |
| 526 |
"HAVE_HMAC", |
| 527 |
"HAVE_LIBBZ2", |
| 528 |
"HAVE_LIBCURL", |
| 529 |
"HAVE_LIBDEFLATE", |
| 530 |
"HAVE_LIBLZMA", |
| 531 |
"HAVE_MMAP"]: |
| 532 |
outf.write(f"{key} = {config_values[key]}\n") |
| 533 |
print(f"# pysam: config_option: {key}={config_values[key]}") |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
for fn in config_headers: |
| 538 |
if not os.path.exists(fn): |
| 539 |
with open(fn, "w") as outf: |
| 540 |
outf.write( |
| 541 |
"/* empty config.h created by pysam */\n") |
| 542 |
outf.write( |
| 543 |
"/* conservative compilation options */\n") |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
if platform.system() == 'Windows': |
| 548 |
include_os = ['win32'] |
| 549 |
os_c_files = ['win32/getopt.c'] |
| 550 |
extra_compile_args = [] |
| 551 |
else: |
| 552 |
include_os = [] |
| 553 |
os_c_files = [] |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
extra_compile_args = [ |
| 559 |
"-Wno-unused", |
| 560 |
"-Wno-strict-prototypes", |
| 561 |
"-Wno-sign-compare", |
| 562 |
"-Wno-error=declaration-after-statement"] |
| 563 |
|
| 564 |
define_macros = [] |
| 565 |
|
| 566 |
if os.environ.get("CIBUILDWHEEL", "0") == "1": |
| 567 |
define_macros.append(("BUILDING_WHEEL", None)) |
| 568 |
|
| 569 |
suffix = sysconfig.get_config_var('EXT_SUFFIX') |
| 570 |
|
| 571 |
internal_htslib_libraries = [ |
| 572 |
os.path.splitext(f"chtslib{suffix}")[0], |
| 573 |
] |
| 574 |
internal_samtools_libraries = [ |
| 575 |
os.path.splitext(f"csamtools{suffix}")[0], |
| 576 |
os.path.splitext(f"cbcftools{suffix}")[0], |
| 577 |
] |
| 578 |
internal_pysamutil_libraries = [ |
| 579 |
os.path.splitext(f"cutils{suffix}")[0], |
| 580 |
] |
| 581 |
|
| 582 |
libraries_for_pysam_module = external_htslib_libraries + internal_htslib_libraries + internal_pysamutil_libraries |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
def prebuild_libchtslib(ext, force): |
| 596 |
if HTSLIB_MODE not in ['shared', 'separate']: return |
| 597 |
|
| 598 |
write_configvars_header("htslib/config_vars.h", ext, "HTS") |
| 599 |
|
| 600 |
if force or not os.path.exists("htslib/libhts.a"): |
| 601 |
log.info("building 'libhts.a'") |
| 602 |
with changedir("htslib"): |
| 603 |
|
| 604 |
|
| 605 |
args = " ".join(ext.extra_compile_args) |
| 606 |
defines = " ".join([format_macro_option(*pair) for pair in ext.define_macros]) |
| 607 |
run_make(["ALL_CPPFLAGS=-I. " + args + " " + defines + " $(CPPFLAGS)", "lib-static"]) |
| 608 |
else: |
| 609 |
log.warning("skipping 'libhts.a' (already built)") |
| 610 |
|
| 611 |
|
| 612 |
def prebuild_libcsamtools(ext, force): |
| 613 |
write_configvars_header("samtools/samtools_config_vars.h", ext, "SAMTOOLS") |
| 614 |
|
| 615 |
|
| 616 |
modules = [ |
| 617 |
dict(name="pysam.libchtslib", |
| 618 |
prebuild_func=prebuild_libchtslib, |
| 619 |
sources=[source_pattern % "htslib", "pysam/htslib_util.c"] + os_c_files, |
| 620 |
extra_objects=htslib_objects, |
| 621 |
libraries=external_htslib_libraries), |
| 622 |
dict(name="pysam.libcsamtools", |
| 623 |
prebuild_func=prebuild_libcsamtools, |
| 624 |
sources=[source_pattern % "samtools"] + glob.glob(os.path.join("samtools", "*.pysam.c")) + |
| 625 |
[os.path.join("samtools", "lz4", "lz4.c")] + os_c_files, |
| 626 |
extra_objects=separate_htslib_objects, |
| 627 |
libraries=external_htslib_libraries + internal_htslib_libraries), |
| 628 |
dict(name="pysam.libcbcftools", |
| 629 |
sources=[source_pattern % "bcftools"] + glob.glob(os.path.join("bcftools", "*.pysam.c")) + os_c_files, |
| 630 |
extra_objects=separate_htslib_objects, |
| 631 |
libraries=external_htslib_libraries + internal_htslib_libraries), |
| 632 |
dict(name="pysam.libcutils", |
| 633 |
sources=[source_pattern % "utils", "pysam/pysam_util.c"] + os_c_files, |
| 634 |
extra_objects=separate_htslib_objects, |
| 635 |
libraries=external_htslib_libraries + internal_htslib_libraries + internal_samtools_libraries), |
| 636 |
dict(name="pysam.libcalignmentfile", |
| 637 |
sources=[source_pattern % "alignmentfile"] + os_c_files, |
| 638 |
extra_objects=separate_htslib_objects, |
| 639 |
libraries=libraries_for_pysam_module), |
| 640 |
dict(name="pysam.libcsamfile", |
| 641 |
sources=[source_pattern % "samfile"] + os_c_files, |
| 642 |
extra_objects=separate_htslib_objects, |
| 643 |
libraries=libraries_for_pysam_module), |
| 644 |
dict(name="pysam.libcalignedsegment", |
| 645 |
sources=[source_pattern % "alignedsegment"] + os_c_files, |
| 646 |
extra_objects=separate_htslib_objects, |
| 647 |
libraries=libraries_for_pysam_module), |
| 648 |
dict(name="pysam.libctabix", |
| 649 |
sources=[source_pattern % "tabix"] + os_c_files, |
| 650 |
extra_objects=separate_htslib_objects, |
| 651 |
libraries=libraries_for_pysam_module), |
| 652 |
dict(name="pysam.libcfaidx", |
| 653 |
sources=[source_pattern % "faidx"] + os_c_files, |
| 654 |
extra_objects=separate_htslib_objects, |
| 655 |
libraries=libraries_for_pysam_module), |
| 656 |
dict(name="pysam.libcbcf", |
| 657 |
sources=[source_pattern % "bcf"] + os_c_files, |
| 658 |
extra_objects=separate_htslib_objects, |
| 659 |
libraries=libraries_for_pysam_module), |
| 660 |
dict(name="pysam.libcbgzf", |
| 661 |
sources=[source_pattern % "bgzf"] + os_c_files, |
| 662 |
extra_objects=separate_htslib_objects, |
| 663 |
libraries=libraries_for_pysam_module), |
| 664 |
dict(name="pysam.libctabixproxies", |
| 665 |
sources=[source_pattern % "tabixproxies"] + os_c_files, |
| 666 |
extra_objects=separate_htslib_objects, |
| 667 |
libraries=libraries_for_pysam_module), |
| 668 |
dict(name="pysam.libcvcf", |
| 669 |
sources=[source_pattern % "vcf"] + os_c_files, |
| 670 |
extra_objects=separate_htslib_objects, |
| 671 |
libraries=libraries_for_pysam_module), |
| 672 |
] |
| 673 |
|
| 674 |
common_options = dict( |
| 675 |
language="c", |
| 676 |
extra_compile_args=extra_compile_args, |
| 677 |
define_macros=define_macros, |
| 678 |
|
| 679 |
library_dirs=[os.path.abspath(x) for x in ["pysam"] + htslib_library_dirs], |
| 680 |
include_dirs=[os.path.abspath(x) for x in ["pysam"] + htslib_include_dirs + \ |
| 681 |
["samtools", "samtools/lz4", "bcftools", "."] + include_os]) |
| 682 |
|
| 683 |
|
| 684 |
for module in modules: |
| 685 |
module.update(**common_options) |
| 686 |
|
| 687 |
classifiers = """ |
| 688 |
Development Status :: 4 - Beta |
| 689 |
Intended Audience :: Science/Research |
| 690 |
Intended Audience :: Developers |
| 691 |
License :: OSI Approved |
| 692 |
Programming Language :: Python |
| 693 |
Topic :: Software Development |
| 694 |
Topic :: Scientific/Engineering |
| 695 |
Operating System :: POSIX |
| 696 |
Operating System :: Unix |
| 697 |
Operating System :: MacOS |
| 698 |
""" |
| 699 |
|
| 700 |
metadata = { |
| 701 |
'name': "pysam", |
| 702 |
'version': get_pysam_version(), |
| 703 |
'description': "Package for reading, manipulating, and writing genomic data", |
| 704 |
'long_description': __doc__, |
| 705 |
'long_description_content_type': "text/x-rst", |
| 706 |
'author': "Andreas Heger", |
| 707 |
'author_email': "[email protected]", |
| 708 |
'license': "MIT", |
| 709 |
'platforms': ["POSIX", "UNIX", "MacOS"], |
| 710 |
'classifiers': [_f for _f in classifiers.split("\n") if _f], |
| 711 |
'url': "https://github.com/pysam-developers/pysam", |
| 712 |
'packages': package_list, |
| 713 |
'ext_modules': [CyExtension(**opts) for opts in modules], |
| 714 |
'cmdclass': {'build_ext': cy_build_ext, 'clean_ext': clean_ext, 'sdist': cythonize_sdist}, |
| 715 |
'package_dir': package_dirs, |
| 716 |
'package_data': {'': ['*.pxd', '*.h', 'py.typed', '*.pyi'], }, |
| 717 |
|
| 718 |
'zip_safe': False, |
| 719 |
} |
| 720 |
|
| 721 |
if __name__ == '__main__': |
| 722 |
dist = setup(**metadata) |