Newer
Older
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
"""
Manage local downloaded data.
"""
from __future__ import (
print_function,
division,
absolute_import,
)
import logging
import yaml
from os.path import join, exists
from pipes import quote
from os import environ
from collections import OrderedDict
from appdirs import user_data_dir
from pkg_resources import resource_string
ENVIRONMENT_VARIABLES = [
"MHCFLURRY_DATA_DIR",
"MHCFLURRY_DOWNLOADS_CURRENT_RELEASE",
"MHCFLURRY_DOWNLOADS_DIR",
]
_DOWNLOADS_DIR = None
_CURRENT_RELEASE = None
_METADATA = None
def get_downloads_dir():
"""
Return the path to local downloaded data
"""
return _DOWNLOADS_DIR
def get_current_release():
"""
Return the current downloaded data release
"""
return _CURRENT_RELEASE
def get_downloads_metadata():
"""
Return the contents of downloads.yml as a dict
"""
global _METADATA
if _METADATA is None:
_METADATA = yaml.load(resource_string(__name__, "downloads.yml"))
return _METADATA
def get_current_release_downloads():
"""
Return a dict of all available downloads in the current release.
The dict keys are the names of the downloads. The values are a dict
downloaded : bool
Whether the download is currently available locally
metadata : dict
Info about the download from downloads.yml such as URL
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
"""
downloads = (
get_downloads_metadata()
['releases']
[get_current_release()]
['downloads'])
return OrderedDict(
(download["name"], {
'downloaded': exists(join(get_downloads_dir(), download["name"])),
'metadata': download,
}) for download in downloads
)
def get_path(download_name, filename='', test_exists=True):
"""
Get the local path to a file in a MHCflurry download
Parameters
-----------
download_name : string
filename : string
Relative path within the download to the file of interest
test_exists : boolean
If True (default) throw an error telling the user how to download the
data if the file does not exist
Returns
-----------
string giving local absolute path
"""
assert '/' not in download_name, "Invalid download: %s" % download_name
path = join(get_downloads_dir(), download_name, filename)
if test_exists and not exists(path):
raise RuntimeError(
"Missing MHCflurry downloadable file: %s. "
"To download this data, run:\n\tmhcflurry-downloads fetch %s\n"
"in a shell."
% (quote(path), download_name))
return path
def configure():
"""
Setup various global variables based on environment variables.
"""
global _DOWNLOADS_DIR
global _CURRENT_RELEASE
_CURRENT_RELEASE = None
_DOWNLOADS_DIR = environ.get("MHCFLURRY_DOWNLOADS_DIR")
if not _DOWNLOADS_DIR:
metadata = get_downloads_metadata()
_CURRENT_RELEASE = environ.get("MHCFLURRY_DOWNLOADS_CURRENT_RELEASE")
if not _CURRENT_RELEASE:
_CURRENT_RELEASE = metadata['current-release']
current_release_compatability = (
metadata["releases"][_CURRENT_RELEASE]["compatibility-version"])
current_compatability = metadata["current-compatibility-version"]
if current_release_compatability != current_compatability:
logging.warn(
"The specified downloads are not compatible with this version "
"of the MHCflurry codebase. Downloads: release %s, "
"compatability version: %d. Code compatability version: %d" % (
_CURRENT_RELEASE,
current_release_compatability,
current_compatability))
data_dir = environ.get("MHCFLURRY_DATA_DIR")
if not data_dir:
# increase the version every time we make a breaking change in
# how the data is organized. For changes to e.g. just model
# serialization, the downloads release numbers should be used.
data_dir = user_data_dir("mhcflurry", version="4")
_DOWNLOADS_DIR = join(data_dir, _CURRENT_RELEASE)
logging.debug("Configured MHCFLURRY_DOWNLOADS_DIR: %s" % _DOWNLOADS_DIR)
configure()