-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
280 lines (242 loc) · 9.14 KB
/
setup.py
File metadata and controls
280 lines (242 loc) · 9.14 KB
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
#!/usr/bin/env python
import configparser
import contextlib
import csv
import glob
import hashlib
import io
import json
import math
import os
import re
import shutil
import ssl
import struct
import sys
import tarfile
import time
import urllib.request
from functools import partial
from xml.etree import ElementTree as etree
import setuptools
from distutils import log
from distutils.command.build import build as _build
from distutils.command.clean import clean as _clean
try:
import rich.progress
except ImportError as err:
rich = None
try:
from pyhmmer.plan7 import HMMFile
except ImportError as err:
HMMFile = err
try:
from isal import igzip as gzip
except ImportError:
import gzip
try:
from compression import zstd
except ImportError as err:
zstd = err
try:
import zstandard
except ImportError as err:
zstandard = err
class list_requirements(setuptools.Command):
"""A custom command to write the project requirements.
"""
description = "list the project requirements"
user_options = [
("setup", "s", "show the setup requirements as well."),
(
"requirements=",
"r",
"the name of the requirements file (defaults to requirements.txt)"
)
]
def initialize_options(self):
self.setup = False
self.output = None
def finalize_options(self):
if self.output is None:
self.output = "requirements.txt"
def run(self):
cfg = configparser.ConfigParser()
cfg.read(__file__.replace(".py", ".cfg"))
with open(self.output, "w") as f:
if self.setup:
f.write(cfg.get("options", "setup_requires"))
f.write(cfg.get("options", "install_requires"))
for _, v in cfg.items("options.extras_require"):
f.write(v)
class download_pfam(setuptools.Command):
"""A custom `setuptools` command to download data before wheel creation.
"""
description = "download the Pfam HMMs required by CHAMOIS to annotate domains"
user_options = [
("force", "f", "force downloading the files even if they exist"),
("inplace", "i", "ignore build-lib and put data alongside your Python code"),
("rebuild", "r", "rebuild the CHAMOIS HMM from Pfam source"),
("version=", "v", "the Pfam version to dowload"),
]
def initialize_options(self):
self.force = False
self.inplace = False
self.rebuild = False
self.version = None
def finalize_options(self):
_build_py = self.get_finalized_command("build_py")
self.build_lib = _build_py.build_lib
if self.version is None:
self.version = "38.0"
def info(self, msg):
self.announce(msg, level=2)
def get_url(self):
return f"http://ftp.ebi.ac.uk/pub/databases/Pfam/releases/Pfam{self.version}/Pfam-A.hmm.gz"
def run(self):
# make sure the build/lib/ folder exists
self.mkpath(self.build_lib)
# Load domain whitelist from the predictor
predictor_file = os.path.join("chamois", "predictor", "predictor.json")
self.info(f"loading domain accesssions from {predictor_file}")
with open(predictor_file, "rb") as f:
data = json.load(f)
features = data["features_"]
kind_index = features['columns'].index('kind')
domains = [
accession
for accession, row in zip(features["index"], features["data"])
if row[kind_index] == "Pfam"
]
# Download and binarize required HMMs
local = os.path.join(self.build_lib, "chamois", "domains", f"Pfam{self.version}.hmm.zst")
self.mkpath(os.path.dirname(local))
# Fall back to filtering the HMMs from the Pfam FTP server
self.make_file(predictor_file, local, self.download_pfam, (local, domains))
if self.inplace:
copy = os.path.relpath(local, self.build_lib)
self.make_file([local], copy, shutil.copy, (local, copy))
def download_pfam(self, local, domains):
# Try getting the GitHub artifacts first, unless asked to rebuild
if not self.rebuild:
try:
self._download_release_hmm(local)
except urllib.error.HTTPError:
pass
else:
return
# check `rich` and `pyhmmer` are installed if subsetting Pfam
if isinstance(HMMFile, ImportError):
raise RuntimeError("pyhmmer is required to run the `download_pfam` command") from HMMFile
if isinstance(zstd, ImportError) and isinstance(zstandard, ImportError):
raise RuntimeError("Zstd support is required to run the `download_pfam` command") from zstd
# download the HMM to `local`, and delete the file if any error
# or interruption happens during the download
# if not os.path.exists(local):
error = None
# streaming the HMMs may not work on all platforms (e.g. MacOS)
# so we fall back to a buffered implementation if needed.
for stream in (True, False):
try:
self._download_pfam_hmms(local, domains, stream=stream)
except Exception as exc:
error = exc
if os.path.exists(local):
os.remove(local)
else:
return
raise RuntimeError("Failed to download Pfam HMMs") from error
def _download_release_hmm(self, output):
# build the GitHub releases URL
base = "https://github.com/zellerlab/CHAMOIS/releases/download/v{version}/Pfam{pfam_version}.hmm.zst"
url = base.format(pfam_version=self.version, version=self.distribution.get_version())
# fetch the resource
self.info(f"fetching {url}")
response = urllib.request.urlopen(url)
# download to output
with contextlib.ExitStack() as ctx:
# use `rich.progress` to make a progress bar
if rich is not None:
pbar = rich.progress.wrap_file(
response,
total=int(response.headers["Content-Length"]),
description=os.path.basename(output),
)
src = ctx.enter_context(pbar)
else:
src = ctx.enter_context(response)
# download to `output`
dst = ctx.enter_context(open(output, "wb"))
shutil.copyfileobj(src, dst)
def _download_pfam_hmms(self, output, domains, stream=True):
# get URL for the requested Pfam version
url = self.get_url()
self.info(f"fetching {url}")
response = urllib.request.urlopen(url)
# download to `output`
nsource = nwritten = 0
with contextlib.ExitStack() as ctx:
# use `rich` to make a progress bar if available
if rich is not None:
pbar = rich.progress.wrap_file(
response,
total=int(response.headers["Content-Length"]),
description=os.path.basename(output),
)
dl = ctx.enter_context(pbar)
else:
dl = ctx.enter_context(response)
# download to buffer or stream
src = ctx.enter_context(gzip.open(dl))
if not isinstance(zstd, ImportError):
dst = ctx.enter_context(zstd.open(
output,
mode="wb",
options={zstd.CompressionParameter.compression_level: 20},
))
else:
dst = ctx.enter_context(zstandard.open(
output,
mode="wb",
cctx=zstandard.ZstdCompressor(level=20)
))
if stream:
hmm_file = ctx.enter_context(HMMFile(src))
else:
buffer = io.BytesIO()
shutil.copyfileobj(src, buffer)
buffer.seek(0)
hmm_file = ctx.enter_context(HMMFile(buffer))
for hmm in hmm_file:
nsource += 1
if hmm.accession in domains:
nwritten += 1
hmm.write(dst, binary=False)
# log number of HMMs kept in final files
self.info(f"downloaded {nwritten} HMMs out of {nsource} in the source file")
class build(_build):
"""A hacked `build` command that will also download required data.
"""
user_options = _build.user_options + [
("inplace", "i", "ignore build-lib and put data alongside your Python code"),
]
def initialize_options(self):
_build.initialize_options(self)
self.inplace = False
def run(self):
# build data if needed
if not self.distribution.have_run.get("download_pfam", False):
command = self.get_finalized_command("download_pfam")
command.force = self.force
command.inplace = self.inplace
command.run()
# build rest as normal
_build.run(self)
if __name__ == "__main__":
setuptools.setup(
cmdclass={
"build": build,
"download_pfam": download_pfam,
"list_requirements": list_requirements,
},
)