-
Notifications
You must be signed in to change notification settings - Fork 2
/
shared.py
208 lines (160 loc) · 6.85 KB
/
shared.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
import json
import os
import urlparse
import uuid
import tempfile
from collections import defaultdict
import vcf as pyvcf
import celery
from sqlalchemy import select, func, create_engine, MetaData, bindparam, not_, or_
import pywebhdfs.webhdfs
import pywebhdfs.errors
import varcode
import config
from common.helpers import tables
CELERY_BACKEND = os.environ['CELERY_BACKEND']
CELERY_BROKER = os.environ['CELERY_BROKER']
WEBHDFS_HOST, WEBHDFS_PORT = (
urlparse.urlparse(config.WEBHDFS_URL).netloc.split(':'))
CYCLEDASH_PORT = os.environ['PORT']
RUNS_URL = 'http://localhost:{}/runs/{}'
CONCORDANCE_URL = 'http://localhost:{}/runs/{}/concordance'
DATABASE_URI = os.environ['DATABASE_URI']
TEMPORARY_DIR = config.TEMPORARY_DIR
worker = celery.Celery(broker=CELERY_BROKER, backend=CELERY_BACKEND)
worker.config_from_object({
'CELERY_TRACK_STARTED': True # track transition from PENDING-->STARTED.
})
def _get_hdfs_client():
return pywebhdfs.webhdfs.PyWebHdfsClient(host=WEBHDFS_HOST,
port=WEBHDFS_PORT,
user_name=config.WEBHDFS_USER)
def get_contents_from_hdfs(hdfs_path):
if not hdfs_path:
raise ValueError('HDFS path must be provided.')
if hdfs_path.startswith('/'):
hdfs_path = hdfs_path[1:]
return _get_hdfs_client().read_file(hdfs_path)
def does_hdfs_file_exist(hdfs_path):
"""Determine whether a file exists on HDFS. Shouldn't have leading '/'."""
hdfs = _get_hdfs_client()
try:
stat = hdfs.get_file_dir_status(hdfs_path)
except pywebhdfs.errors.FileNotFound:
return False
else:
return True
class HdfsFileAlreadyExistsError(Exception):
pass
def put_new_file_to_hdfs(hdfs_path, contents):
"""Place contents in a new file on HDFS.
hdfs_path should not have a leading '/'.
Raises HdfsFileAlreadyExistsError is the file already exists.
"""
if does_hdfs_file_exist(hdfs_path):
raise HdfsFileAlreadyExistsError(hdfs_path)
_get_hdfs_client().create_file(hdfs_path, contents)
def hdfs_to_local_path(hdfs_path):
contents = get_contents_from_hdfs(hdfs_path)
filename = '/tmp/' + uuid.uuid4().get_hex() + '.vcf'
with open(filename, 'w') as fsock:
fsock.write(contents)
return filename
def guess_ensembl_release(filepath):
try:
release = varcode.load_vcf(filepath)[0].ensembl.release
except ValueError: # no guesses from varcode, return default
release = config.ENSEMBL_RELEASE
except Exception: # varcode cannot handle this one, so go w/ default
release = config.ENSEMBL_RELEASE
finally:
return release
def load_vcf(vcf_path):
"""Return a vcf.Reader, header text for the given VCF."""
if config.ALLOW_LOCAL_VCFS and vcf_path.startswith('/tests/'):
filepath = vcf_path[1:];
text = open(filepath).read()
elif vcf_path.startswith('file://'):
filepath = vcf_path[6:];
text = open(filepath).read()
elif vcf_path.startswith('hdfs://'):
return load_vcf(vcf_path[6:])
else:
text = get_contents_from_hdfs(vcf_path)
filepath = hdfs_to_local_path(vcf_path)
header = '\n'.join(l for l in text.split('\n') if l.startswith('#'))
release = guess_ensembl_release(filepath)
return pyvcf.Reader(l for l in text.split('\n')), header, release
def initialize_database(database_uri):
"""Return engine, connection, metadata (reflected) for the given DB URI."""
engine = create_engine(database_uri)
connection = engine.connect()
metadata = MetaData(bind=connection)
metadata.reflect()
return engine, connection, metadata
def temp_csv(mode, tmp_dir=None):
"""Create a temporary csv file and return it. Don't delete on close.
Finally, do a chmod 644 on the file in any a different process owner needs
to read it.
"""
# TODO(tavi) Address the fact that these files are not going to be deleted.
csvfile = tempfile.NamedTemporaryFile(mode=mode, delete=False, dir=tmp_dir)
# In case a different process owner, e.g. postgres, needs to read it.
os.chmod(csvfile.name, 0o644)
return csvfile
def update_extant_columns(metadata, connection, vcf_id):
"""Determine which columns actually exist in this VCF, and cache them
(as this is a time-consuming operation) in the vcfs table for later use.
"""
extant_cols = json.dumps(extant_columns(metadata, connection, vcf_id))
vcfs = metadata.tables.get('vcfs')
vcfs.update().where(vcfs.c.id == vcf_id).values(
extant_columns=extant_cols).execute()
def extant_columns(metadata, connection, vcf_id):
"""Return list of column names which have values in this VCF."""
genotypes = metadata.tables.get('genotypes')
columns = (col.name for col in genotypes.columns
if col.name.startswith('info:') or
col.name.startswith('sample:') or
col.name.startswith('annotations:'))
query = 'SELECT '
query += ', '.join('max("{c}") as "{c}"'.format(c=col) for col in columns)
query += ' FROM genotypes WHERE vcf_id = ' + str(vcf_id)
maxed_columns = dict(connection.execute(query).fetchall()[0])
return [k for k, v in maxed_columns.iteritems() if v is not None]
def update_vcf_count(metadata, connection, vcf_id):
"""Adds variant count to the metadata for the VCF."""
genotypes = metadata.tables.get('genotypes')
count_q = select([func.count()]).where(genotypes.c.vcf_id == vcf_id)
(count,) = connection.execute(count_q).fetchone()
vcfs = metadata.tables.get('vcfs')
vcfs.update().where(vcfs.c.id == vcf_id).values(
genotype_count=count).execute()
def register_running_task(task, vcf_id):
"""Record the existence of a Celery task in the database."""
record = {
'task_id': task.request.id,
'type': task.name,
'state': 'STARTED',
'vcf_id': vcf_id
}
with tables(create_engine(DATABASE_URI), 'task_states') as (con, tasks):
tasks.insert(record).execute()
def update_tasks_table():
"""Update the tasks table using data from Celery.
This checks in on all tasks which were last seen in a non-terminal state,
i.e. something other than SUCCESS or FAILURE.
"""
engine = create_engine(DATABASE_URI)
with tables(engine, 'task_states') as (con, tasks):
q = (select([tasks.c.id, tasks.c.task_id, tasks.c.state])
.where(not_(tasks.c.state.in_(['SUCCESS', 'FAILURE']))))
updates = []
for table_id, task_id, old_state in con.execute(q).fetchall():
# pylint: disable=too-many-function-args
new_state = worker.AsyncResult(task_id).state
if new_state != old_state:
updates.append({'id_': table_id, 'state': new_state})
if updates:
update_q = tasks.update().where(tasks.c.id == bindparam('id_'))
con.execute(update_q, updates)