-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_loci_inserter.py
606 lines (485 loc) · 20.4 KB
/
schema_loci_inserter.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Purpose
-------
This module creates new loci in the Chewie-NS and associates those
loci with their species and schema. The module attributes loci
identifiers sequentially and ensures that identifiers are not repeated
to avoid overlaps/conflicts.
Expected input
--------------
The process expects the following variable wheter through command line
execution or invocation of the :py:func:`main` function:
- ``-i``, ``input_dir`` : Temporary directory that receives the
data necessary for schema insertion (the basename has the
species and schema identifiers).
- e.g.: ``./schema_insertion_temp/1_1``
Code documentation
------------------
"""
import os
import sys
import time
import pickle
import logging
import argparse
import requests
import threading
import concurrent.futures
from itertools import repeat
from SPARQLWrapper import SPARQLWrapper
from app.utils import sparql_queries as sq
from app.utils import auxiliary_functions as aux
logfile = './log_files/schema_loci_inserter.log'
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S',
filename=logfile)
thread_local = threading.local()
def get_session():
""" Creates a Session object for a thread.
Creating a Session object allows the
persistance of certain parameters across
requests. It is best to create one Session
object per thread to ensure that the process
is threadsafe. Using a Session object will
make the process faster when compared to
simply using the get() or post() methods
from the requests package.
Returns
-------
A Session object.
"""
if not hasattr(thread_local, 'session'):
thread_local.session = requests.Session()
return thread_local.session
def post_loci(query_data, local_sparql, virtuoso_user, virtuoso_pass):
""" Performs a POST request to insert a new locus or link
a locus to its species or schema.
Parameters
----------
query_data : tup
A tuple with three elements: the locus hash, the locus
URI and the SPARQL query that will be executed by
Virtuoso's SPARQL endpoint.
Returns
-------
A tuple with the locus hash and the status code of the
POST request.
"""
locus_hash = query_data[0]
locus_uri = query_data[1]
query = query_data[2]
session = get_session()
headers = {'content-type': 'application/sparql-query'}
tries = 0
max_tries = 5
valid = False
while valid is False and tries < max_tries:
with session.post(local_sparql, data=query, headers=headers,
auth=requests.auth.HTTPBasicAuth(virtuoso_user, virtuoso_pass)) as response:
status_code = response.status_code
if status_code > 201:
tries += 1
logging.warning('Could not insert data for locus {0}\n'
'Response content:\n{1}\nRetrying ({2})'
'...\n'.format(locus_uri, response.content, tries))
else:
valid = True
return (locus_hash, status_code)
def send_queries(loci_queries, local_sparql, virtuoso_user, virtuoso_pass):
""" Sends a set of requests to Virtuoso's SPARQL endpoint.
Parameters
----------
loci_queries : list of tup
A list with a tuple per loci to insert or associate
with the species or schema. Each tuple has three
elements: the locus hash, the locus URI and the
SPARQL query that will be used to insert the data
into Virtuoso's database.
Returns
-------
responses : dict
A dictionary with loci hashes as keys and response
status codes as values.
"""
responses = {}
total = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
for res in executor.map(post_loci, loci_queries, repeat(local_sparql),
repeat(virtuoso_user), repeat(virtuoso_pass)):
responses[res[0]] = res[1]
total += 1
# slow down insertion during tutorial
time.sleep(1)
return responses
def assign_identifiers(loci_data, schema_hashes, loci_prefix, start_id, base_url):
""" Assigns identifiers to loci.
Parameters
----------
loci_data : list of list
A list with one sublist per locus. Each
sublist has seven elements: the locus
original name, the locus hash, the UniProt
annotation name, the UniProt annotation URI,
the UniProt annotation label, the User
annotation and a Custom annotation.
schema_hashes : dict
A dictionary with loci hashes as keys and lists
as values. Each list has two elements: a bool
that indicates if the alleles for that locus
have been uploaded and a sublist with three
bool elements that indicate if the locus has
been inserted, linked to the species and linked
to the schema.
loci_prefix : str
The prefix to include in the name of
all loci.
start_id : int
The starting identifier for new loci.
Returns
-------
A list with the following elements:
- response (dict): a dictionary with loci
hashes as keys and lists with loci URIs as values.
- hash_to_uri (dict): a dictionary with loci hashes
as keys and loci URIs as values.
- loci_data (list): the input ``loci_data`` with
the locus URI and locus prefix appended to each
sublist.
"""
# response with locus URI
response = {}
# locus file content hash mapping to locus URI
hash_to_uri = {}
# create loci URIs and map to original files
for i, locus in enumerate(loci_data):
inserted = schema_hashes[locus[1]][1][0]
# locus is new
if inserted is False:
locus_uri = '{0}loci/{1}'.format(base_url, start_id)
locus_prefix = '{0}-{1}'.format(loci_prefix, '{:08d}'.format(start_id))
# increment for next locus
start_id += 1
# locus was previously inserted, do not assign new identifier
else:
locus_uri = inserted
locus_prefix = '{0}-{1}'.format(loci_prefix, '{:08d}'.format(locus_uri.split('/')[-1]))
locus.append(locus_uri)
locus.append(locus_prefix)
response[locus[1]] = [locus_uri]
hash_to_uri[locus[1]] = locus_uri
return [response, hash_to_uri, loci_data]
def create_insert_queries(loci_data, schema_hashes, virtuoso_graph):
""" Creates SPARQL queries to insert loci into the Chewie-NS.
Parameters
----------
loci_data : list of list
A list with one sublist per locus. It needs
to have the same structure as the variable with
same name returned by the :py:func:`assign_identifiers`
function.
schema_hashes : dict
A dictionary with loci hashes as keys and lists
as values. Each list has two elements: a bool
that indicates if the alleles for that locus
have been uploaded and a sublist with three
bool elements that indicate if the locus has
been inserted, linked to the species and linked
to the schema.
Returns
-------
A list with two elements:
- insert_queries (list): list with the SPARQL
queries to insert the loci.
- insert (int): the number of loci that need
to be inserted (function determines if a locus
has already been inserted).
"""
# create queries
insert = 0
insert_queries = []
for l in loci_data:
# determined if locus was already inserted
inserted = schema_hashes[l[1]][1][0]
if inserted is False:
insert += 1
original_name = '; typon:originalName "{0}"^^xsd:string.'.format(l[0]) \
if l[0] not in ['', 'string', False] else ' .'
insert_query = (sq.INSERT_LOCUS.format(virtuoso_graph,
l[7], l[8], l[2],
l[3], l[4], l[5],
l[6], original_name))
# remove escape bars
insert_query = insert_query.replace('\\', ' ')
insert_queries.append((l[1], l[7], insert_query))
return [insert_queries, insert]
def species_link_queries(loci_data, schema_hashes, species_uri, virtuoso_graph):
""" Creates SPARQL queries to loci loci to a species.
Parameters
----------
loci_data : list
A list with one sublist per locus. It needs
to have the same structure as the variable with
same name returned by the :py:func:`assign_identifiers`
function.
schema_hashes : dict
A dictionary with loci hashes as keys and lists
as values. Each list has two elements: a bool
that indicates if the alleles for that locus
have been uploaded and a sublist with three
bool elements that indicate if the locus has
been inserted, linked to the species and linked
to the schema.
species_uri : str
The species URI in the Chewie-NS.
Returns
-------
A list with two elements:
- link_queries (list): list with the SPARQL
queries to link loci to species.
- link (int): number of loci that need to be
linked to the species.
"""
link = 0
link_queries = []
for l in loci_data:
sp_link = schema_hashes[l[1]][1][1]
if sp_link is False:
link += 1
link_query = (sq.INSERT_SPECIES_LOCUS.format(virtuoso_graph,
l[7], species_uri))
link_queries.append((l[1], l[7], link_query))
return [link_queries, link]
def schema_link_queries(loci_data, schema_hashes, schema_uri, virtuoso_graph):
""" Creates SPARQL queries to loci loci to a schema.
Parameters
----------
loci_data : list
A list with one sublist per locus. It needs
to have the same structure as the variable with
same name returned by the :py:func:`assign_identifiers`
function.
schema_hashes : dict
A dictionary with loci hashes as keys and lists
as values. Each list has two elements: a bool
that indicates if the alleles for that locus
have been uploaded and a sublist with three
bool elements that indicate if the locus has
been inserted, linked to the species and linked
to the schema.
schema_uri : str
The schema URI in the Chewie-NS.
Returns
-------
A list with two elements:
- link_queries (list): list with the SPARQL
queries to link loci to schema.
- link (int): number of loci that need to be
linked to the schema.
"""
link = 0
link_queries = []
schema_part_id = 1
for l in loci_data:
sc_link = schema_hashes[l[1]][1][2]
if sc_link is False:
link += 1
# create URI for new schema part
schema_part_url = '{0}/loci/{1}'.format(schema_uri, schema_part_id)
# link locus to schema
link_query = (sq.INSERT_SCHEMA_LOCUS.format(virtuoso_graph,
schema_part_url,
schema_part_id,
l[7],
schema_uri,
schema_part_url))
link_queries.append((l[1], l[7], link_query))
schema_part_id += 1
return [link_queries, link]
def results_status(results):
""" Determines if queries were executed successfully
based on status codes.
Parameters
----------
results : dict
A dictionary with loci hashes as keys and
status codes as values.
Returns
-------
A list with three elements:
- status (dict): a dictionary with loci hashes
as keys and True/False as values.
- success (int): number of queries that were
successfully executed.
- failed (int): number of queries that were not
successfully executed.
"""
status = {}
success = 0
failed = 0
for k in results:
# change upload status
status_code = results[k]
if status_code in [200, 201]:
status[k] = True
success += 1
else:
status[k] = False
failed += 1
return [status, success, failed]
def parse_arguments():
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-i', type=str,
dest='input_dir', required=True,
help='Temporary directory that '
'receives the data necessary '
'for schema insertion.')
parser.add_argument('--g', type=str,
dest='virtuoso_graph', required=True,
default=os.environ.get('DEFAULTHGRAPH'),
help='')
parser.add_argument('--s', type=str,
dest='local_sparql', required=True,
default=os.environ.get('LOCAL_SPARQL'),
help='')
parser.add_argument('--b', type=str,
dest='base_url', required=True,
default=os.environ.get('BASE_URL'),
help='')
parser.add_argument('--u', type=str,
dest='virtuoso_user', required=True,
default=os.environ.get('VIRTUOSO_USER'),
help='')
parser.add_argument('--p', type=str,
dest='virtuoso_pass', required=True,
default=os.environ.get('VIRTUOSO_PASS'),
help='')
args = parser.parse_args()
return [args.input_dir, args.virtuoso_graph,
args.local_sparql, args.base_url,
args.virtuoso_user, args.virtuoso_pass]
def main(temp_dir, graph, sparql, base_url, user, password):
# get species and schema identifiers
species_id = os.path.basename(temp_dir).split('_')[0]
schema_id = os.path.basename(temp_dir).split('_')[1]
# create species and schema URIs
species_uri = '{0}species/{1}'.format(base_url, species_id)
schema_uri = '{0}/schemas/{1}'.format(species_uri, schema_id)
logging.info('Started loci insertion for '
'schema {0}'.format(schema_uri))
# define path to file with loci data
loci_file = os.path.join(temp_dir, '{0}_{1}_loci'.format(species_id,
schema_id))
# read loci data
if os.path.isfile(loci_file) is True:
with open(loci_file, 'rb') as lf:
loci_prefix, loci_data = pickle.load(lf)
logging.info('Loci prefix is {0}.'.format(loci_prefix))
else:
logging.warning('Could not find file {0}. '
'Aborting\n\n'.format(loci_file))
sys.exit(1)
# determine locus with highest identifier
result = aux.get_data(SPARQLWrapper(sparql),
(sq.SELECT_HIGHEST_LOCUS.format(graph)))
highest_locus = result['results']['bindings']
# if there are no loci
if highest_locus != []:
highest_locus = highest_locus[0]['locus']['value']
highest_id = int(highest_locus.split('/')[-1])
start_id = highest_id + 1
elif highest_locus == []:
start_id = 1
# define path to file with schema upload status data
hashes_file = os.path.join(temp_dir,
'{0}_{1}_hashes'.format(species_id, schema_id))
# read schema upload status data
if os.path.isfile(hashes_file) is True:
with open(hashes_file, 'rb') as hf:
schema_hashes = pickle.load(hf)
else:
logging.warning('Could not find schema upload status file. Aborting.\n\n')
sys.exit(1)
# assign identifiers to new loci based on the total number of loci in the Chewie-NS
response, hash_to_uri, loci_data, = assign_identifiers(loci_data, schema_hashes,
loci_prefix, start_id,
base_url)
# insert loci
insert_queries, insert = create_insert_queries(loci_data, schema_hashes, graph)
logging.info('{0} loci to insert out of {1} total loci '
'in schema.'.format(insert, len(loci_data)))
logging.info('Loci integer identifiers interval: [{0} .. {1}]'
''.format(start_id, start_id+insert))
# insert data to create loci
if len(insert_queries) > 0:
loci_insertion = send_queries(insert_queries, sparql, user, password)
insert_status, success, failed = results_status(loci_insertion)
for h, v in insert_status.items():
if v is True:
schema_hashes[h][1][0] = hash_to_uri[h]
response[h].append(v)
elif v is False:
response[h].append(v)
logging.info('Successfully inserted {0} loci. '
'Failed {1}'.format(success, failed))
# halt process if it could not insert all loci
if failed > 0:
logging.warning('Could not insert all loci. Aborting.\n\n')
sys.exit(1)
# link loci to species
sp_queries, link = species_link_queries(loci_data,
schema_hashes,
species_uri,
graph)
logging.info('{0} loci to link to species out of {1} total loci '
'in schema.'.format(link, len(loci_data)))
if len(sp_queries) > 0:
species_links = send_queries(sp_queries, sparql, user, password)
link_status, success, failed = results_status(species_links)
for h, v in link_status.items():
if v is True:
schema_hashes[h][1][1] = True
response[h].append(v)
elif v is False:
response[h].append(v)
logging.info('Successfully linked {0} loci to species. '
'Failed {1}'.format(success, failed))
# link loci to schema
sc_queries, link = schema_link_queries(loci_data,
schema_hashes,
schema_uri,
graph)
logging.info('{0} loci to link to schema out of {1} total loci '
'in schema.'.format(link, len(loci_data)))
if len(sc_queries) > 0:
schema_links = send_queries(sc_queries, sparql, user, password)
link_status, success, failed = results_status(schema_links)
for h, v in link_status.items():
if v is True:
schema_hashes[h][1][2] = True
response[h].append(v)
elif v is False:
response[h].append(v)
logging.info('Successfully linked {0} loci to schema. '
'Failed {1}'.format(success, failed))
# save updated schema hashes
with open(hashes_file, 'wb') as hf:
pickle.dump(schema_hashes, hf)
# write response to file
response_file = os.path.join(temp_dir,
'{0}_{1}_loci_response'.format(species_id,
schema_id))
with open(response_file, 'wb') as rf:
pickle.dump(response, rf)
# remove temp file
os.remove(loci_file)
logging.info('Finished loci insertion for '
'schema {0}\n\n'.format(schema_uri))
return response_file
if __name__ == '__main__':
args = parse_arguments()
main(args[0], args[1], args[2],
args[3], args[4], args[5])