/
File.py
538 lines (446 loc) · 17.7 KB
/
File.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
import datetime
import os
import time
from dateutil import parser
from urllib.parse import quote
from askomics.libaskomics.Params import Params
from askomics.libaskomics.Database import Database
from askomics.libaskomics.PrefixManager import PrefixManager
from askomics.libaskomics.SparqlQueryLauncher import SparqlQueryLauncher
from askomics.libaskomics.Utils import Utils
from askomics.libaskomics.RdfGraph import RdfGraph
from askomics.libaskomics.TriplestoreExplorer import TriplestoreExplorer
from pkg_resources import get_distribution
import rdflib
from rdflib.namespace import Namespace
class File(Params):
"""Summary
Attributes
----------
namespace_internal : Namespace
AskOmics namespace askomics:
namespace_data : Namespace
AskOmics prefix :
dc : Namespace
dc namespace
default_graph : string
Default rdf graph
faldo : Namespace
faldo namespace
faldo_entity : bool
True if entity is a faldo entity
file_graph : string
File graph containing the file
host_url : string
AskOmics url
id : int
database file id
max_chunk_size : int
Max number of triple to insert in one Load or insert
method : int
Load or insert
name : string
Name of the file
now : datetime
timestamp of the current time
ntriples : int
Number of triples
path : string
Path of the file
prov : Namespace
prov namespace
public : bool
True if the file is public
size : int
file size
timestamp : TYPE
Description
ttl_dir : string
path to the ttl directory
type : string
file type
user_graph : string
User graph
"""
def __init__(self, app, session, file_info, host_url=None, external_endpoint=None, custom_uri=None, external_graph=None):
"""init
Parameters
----------
app : Flask
Flask app
session :
AskOmics session
file_info : dict
File info
host_url : None, optional
AskOmics url
"""
Params.__init__(self, app, session)
self.host_url = host_url
self.human_name = file_info["name"]
self.name = self.format_uri(file_info['name'], remove_space=True)
self.path = file_info['path']
self.type = file_info['type']
self.size = file_info['size']
self.id = file_info['id']
self.public = False
self.ntriples = 0
self.timestamp = int(time.time())
self.external_endpoint = external_endpoint
self.external_graph = external_graph
self.default_graph = "{}".format(self.settings.get('triplestore', 'default_graph'))
self.user_graph = "{}:{}_{}".format(
self.settings.get('triplestore', 'default_graph'),
self.session['user']['id'],
self.session['user']['username']
)
if "graph_name" not in file_info:
self.file_graph = "{}:{}_{}:{}_{}".format(
self.settings.get('triplestore', 'default_graph'),
self.session['user']['id'],
self.session['user']['username'],
self.name,
self.timestamp
)
else:
self.file_graph = file_info["graph_name"]
self.ttl_dir = '{}/{}_{}/ttl'.format(
self.settings.get('askomics', 'data_directory'),
self.session['user']['id'],
self.session['user']['username']
)
self.now = datetime.datetime.now().isoformat()
self.namespace_internal = Namespace(self.settings.get('triplestore', 'namespace_internal'))
self.namespace_data = Namespace(self.settings.get('triplestore', 'namespace_data'))
self.namespace_entity = Namespace(custom_uri) if custom_uri else self.namespace_data
self.faldo = Namespace('http://biohackathon.org/resource/faldo/')
self.prov = Namespace('http://www.w3.org/ns/prov#')
self.dc = Namespace('http://purl.org/dc/elements/1.1/')
self.dcat = Namespace('http://www.w3.org/ns/dcat#')
self.faldo_entity = False
self.faldo_abstraction = {
"start": None,
"end": None,
"strand": None,
"reference": None
}
self.faldo_abstraction_eq = {
"start": self.namespace_internal["faldoStart"],
"end": self.namespace_internal["faldoEnd"],
"strand": self.namespace_internal["faldoStrand"],
"reference": self.namespace_internal["faldoReference"]
}
self.method = self.settings.get('triplestore', 'upload_method')
self.max_chunk_size = self.settings.getint('triplestore', 'chunk_size')
self.serialization_format = self.settings.get('triplestore', 'serialization_format')
self.rdf_extention = 'ttl' if self.serialization_format == 'turtle' else self.serialization_format
self.graph_chunk = RdfGraph(self.app, self.session)
self.graph_abstraction_dk = RdfGraph(self.app, self.session)
self.graph_metadata = RdfGraph(self.app, self.session)
self.error = False
self.error_message = ""
def edit_name_in_db(self, new_name):
"""Edit file name
Parameters
----------
new_name : str
New name
"""
query = '''
UPDATE files SET
name=?
WHERE id = ? and user_id = ?
'''
database = Database(self.app, self.session)
database.execute_sql_query(query, (new_name, self.id, self.session["user"]["id"]))
def update_percent_in_db(self, percent, dataset_id):
"""Update dataset percent
Parameters
----------
percent : float
The new percent
dataset_id : int
the corresponding dataset id
"""
query = '''
UPDATE datasets SET
percent = ?
WHERE id = ? and user_id = ?
'''
database = Database(self.app, self.session)
database.execute_sql_query(query, (percent, dataset_id, self.session["user"]["id"]))
def format_uri(self, string, remove_space=False):
"""remove space and quote"""
if remove_space:
return quote(string.replace(' ', ''))
return quote(string)
def rdfize(self, string, custom_namespace=None):
"""Rdfize a string
Return the literal if string is an url, else,
prefix it with askomics prefix
Parameters
----------
string : string
Term to rdfize
Returns
-------
rdflib.???
Rdfized term
"""
if Utils.is_valid_url(string):
return rdflib.URIRef(string)
elif ":" in string and len(string.split(":")) == 2:
prefix, val = string.split(":")
if prefix:
prefix_manager = PrefixManager(self.app, self.session)
namespace = prefix_manager.get_namespace(prefix)
if namespace:
return rdflib.URIRef("{}{}".format(namespace, val))
else:
# If not prefix, default to entity prefix
string = val
if custom_namespace:
return custom_namespace[self.format_uri(string)]
else:
return self.namespace_data[self.format_uri(string)]
def get_uri_label(self, string):
"""Labelize a string
Try to extract a label from an URI
Parameters
----------
uri : string
Term to extract label from
Returns
-------
String
Label
"""
if Utils.is_valid_url(string):
string = string.rstrip("/")
if "/" in string:
end_term = string.split("/")[-1].rstrip("#")
if "#" in end_term:
end_term = end_term.split("#")[-1]
else:
end_term = string
elif ":" in string and len(string.split(":")) == 2:
end_term = string.split(":")[-1]
else:
end_term = string
return end_term
def set_metadata(self):
"""Get a rdflib graph of the metadata
Returns
-------
Graph
graph containing metadata of the file
"""
location_endpoint = rdflib.Literal(self.external_endpoint) if self.external_endpoint else rdflib.Literal(self.settings.get('triplestore', 'endpoint'))
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.prov.atLocation, location_endpoint))
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.prov.generatedAtTime, rdflib.Literal(self.now)))
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.dc.creator, rdflib.Literal(self.session['user']['username'])))
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.prov.wasDerivedFrom, rdflib.Literal(self.name)))
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.dc.hasVersion, rdflib.Literal(get_distribution('askomics').version)))
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.prov.describesService, rdflib.Literal(os.uname()[1])))
if self.external_graph:
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.dcat.Dataset, rdflib.Literal(self.external_graph)))
if self.public:
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.namespace_internal['public'], rdflib.Literal(True)))
else:
self.graph_metadata.add((rdflib.Literal(self.file_graph), self.namespace_internal['public'], rdflib.Literal(False)))
def load_graph(self, rdf_graph, tmp_file_name):
"""Load a rdflib graph into the triplestore
Write rdf to a tmp file, and send the url to this file
to the triplestore with a LOAD request
Parameters
----------
rdf_graph : Graph
rdf graph to load
tmp_file_name : string
Path to a tmp file
"""
sparql = SparqlQueryLauncher(self.app, self.session)
temp_file_path = '{}/{}'.format(self.ttl_dir, tmp_file_name)
encoding = 'utf-8' if self.serialization_format != 'nt' else None
rdf_graph.serialize(format=self.serialization_format, encoding=encoding, destination=temp_file_path)
# Load the chunk
sparql.load_data(tmp_file_name, self.file_graph, self.host_url)
# Remove tmp file
if not self.settings.getboolean('askomics', 'debug_ttl'):
os.remove(temp_file_path)
def rollback(self):
"""Drop the dataset from the triplestore in case of error"""
sparql = SparqlQueryLauncher(self.app, self.session)
sparql.drop_dataset(self.file_graph)
def set_triples_number(self):
"""Set graph triples number by requesting the triplestore"""
query = """
SELECT count(*) AS ?count
FROM <{}>
WHERE {{
?s ?p ?o .
}}
""".format(self.file_graph)
sparql = SparqlQueryLauncher(self.app, self.session)
result = sparql.process_query(query)
self.ntriples = result[1][0]["count"]
def integrate(self, dataset_id=None):
"""Integrate the file into the triplestore"""
sparql = SparqlQueryLauncher(self.app, self.session)
tse = TriplestoreExplorer(self.app, self.session)
# insert metadata
self.set_metadata()
sparql.insert_data(self.graph_metadata, self.file_graph, metadata=True)
content_generator = self.generate_rdf_content()
# Insert content
chunk_number = 0
for _ in content_generator:
if self.graph_chunk.ntriple >= self.max_chunk_size:
if self.graph_chunk.percent and dataset_id:
self.update_percent_in_db(self.graph_chunk.percent, dataset_id)
if self.method == 'load':
# write rdf into a tmpfile and load it
temp_file_name = 'tmp_{}_{}_chunk_{}.{}'.format(
Utils.get_random_string(5),
self.name,
chunk_number,
self.rdf_extention
)
# Try to load data. if failure, wait 5 sec and retry 5 time
Utils.redo_if_failure(self.log, 5, 5, self.load_graph, self.graph_chunk, temp_file_name)
else:
# Insert
# Try to insert data. if failure, wait 5 sec and retry 5 time
Utils.redo_if_failure(self.log, 5, 5, sparql.insert_data, self.graph_chunk, self.file_graph)
chunk_number += 1
self.graph_chunk = RdfGraph(self.app, self.session)
# Load the last chunk
if self.graph_chunk.percent and dataset_id:
self.update_percent_in_db(99, dataset_id)
if self.method == 'load':
temp_file_name = 'tmp_{}_{}_chunk_{}.{}'.format(
Utils.get_random_string(5),
self.name,
chunk_number,
self.rdf_extention
)
# Try to load data. if failure, wait 5 sec and retry 5 time
Utils.redo_if_failure(self.log, 5, 5, self.load_graph, self.graph_chunk, temp_file_name)
else:
# Insert
# Try to insert data. if failure, wait 5 sec and retry 5 time
Utils.redo_if_failure(self.log, 5, 5, sparql.insert_data, self.graph_chunk, self.file_graph)
# Content is inserted, now insert abstraction and domain_knowledge
self.set_rdf_abstraction_domain_knowledge()
# Remove chached abstraction
tse.uncache_abstraction(public=self.public)
if self.method == 'load':
temp_file_name = 'tmp_{}_{}_abstraction_domain_knowledge.{}'.format(
Utils.get_random_string(5),
self.name,
self.rdf_extention
)
self.load_graph(self.graph_abstraction_dk, temp_file_name)
else:
# Insert
sparql.insert_data(self.graph_abstraction_dk, self.file_graph)
self.update_percent_in_db(100, dataset_id)
self.set_triples_number()
def get_faldo_strand(self, raw_strand):
"""Get faldo strand
Parameters
----------
raw_strand : string
raw value of strand
Returns
-------
rdf term
Faldo "Foward", "Reverse" or "Both" uri
"""
if raw_strand in ("+", "plus", "1"):
return self.faldo.ForwardStrandPosition
if raw_strand in ("-", "minus", "moins", "-1"):
return self.faldo.ReverseStrandPosition
return self.faldo.BothStrandPosition
def get_faldo_strand_label(self, raw_strand):
"""Get faldo strand label
Parameters
----------
raw_strand : string
raw value of strand
Returns
-------
label
"+", "-", or "."
"""
if raw_strand in ("+", "plus", "1", "positive", "forward"):
return "+"
if raw_strand in ("-", "minus", "moins", "-1", "reverse", "negative"):
return "-"
return "."
def get_reference_strand_uri(self, reference, strand, block=None):
faldo_dict = {
self.faldo.ForwardStrandPosition: "ForwardStrand",
self.faldo.ReverseStrandPosition: "ReverseStrand",
self.faldo.BothStrandPosition: "BothStrand"
}
if reference is None:
if strand == self.faldo.BothStrandPosition:
return [self.rdfize(self.format_uri("s{}_{}".format(dstrand, block))) for dstrand in faldo_dict.values()]
return [self.rdfize(self.format_uri("s{}_{}".format(faldo_dict[strand], block)))]
if block is None:
if strand == self.faldo.BothStrandPosition:
return [self.rdfize(self.format_uri("{}_s{}s".format(reference, dstrand))) for dstrand in faldo_dict.values()]
return [self.rdfize(self.format_uri("{}_s{}".format(reference, faldo_dict[strand])))]
if strand == self.faldo.BothStrandPosition:
return [self.rdfize(self.format_uri("{}_s{}_{}".format(reference, dstrand, block))) for dstrand in faldo_dict.values()]
return [self.rdfize(self.format_uri("{}_s{}_{}".format(reference, faldo_dict[strand], block)))]
def get_rdf_type(self, value):
"""get xsd type of a value
Parameters
----------
value :
The value to get type
Returns
-------
TYPE
rdflib.XSD.string or rdflib.XSD.decimal
"""
try:
int(value)
return rdflib.XSD.decimal
except ValueError:
try:
float(value)
return rdflib.XSD.decimal
except ValueError:
try:
parser.parse(value, dayfirst=True).date()
return rdflib.XSD.date
except parser.ParserError:
return rdflib.XSD.string
return rdflib.XSD.string
def convert_type(self, value, try_date=False):
"""Convert a value to a date, an int or float or text
Parameters
----------
value : string
The value to convert
Returns
-------
string/float/int
the converted value
"""
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
if not try_date:
return value
try:
return parser.parse(value, dayfirst=True).date()
except Exception:
return value
return value