-
Notifications
You must be signed in to change notification settings - Fork 0
/
edamci.py
635 lines (431 loc) · 24.1 KB
/
edamci.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import inspect
import unittest
from rdflib import OWL, ConjunctiveGraph, Namespace
import os
import pandas as pd
import argparse
import sys
from collections import Counter
from rdflib.namespace import RDF, RDFS, _OWL
from tabulate import tabulate
# from rich_dataframe import prettify
from queries.edamxpath_id_unique import check_unique_id
def parsing () :
parser = argparse.ArgumentParser(description="Level of tests, by default all levels are ran")
parser.add_argument('-e','--error', action='store_true', help='runs all error tests')
parser.add_argument('-E','--essential' , action='store_true', help='runs all essential tests')
parser.add_argument('-c','--curation', action='store_true', help='runs all curation tests')
parser.add_argument('unittest_args', nargs='*')
args = parser.parse_args()
#print("args argparse",args.error,args.essential,args.curation)
if (args.error!=False or args.essential!=False or args.curation!=False) :
run_error = args.error
run_essential = args.essential
run_curation = args.curation
else:
run_error = True
run_essential = True
run_curation = True
#print("def parsing",run_error,run_essential,run_curation)
sys.argv[1:] = args.unittest_args
return(run_error,run_essential,run_curation)
def suite ():
suite = unittest.TestSuite()
if run_curation == True :
suite.addTest(EdamQueryTest('test_deprecated_replacement_obsolete'))
if run_essential == True :
suite.addTest(EdamQueryTest('test_super_class_refers_to_self'))
if run_essential == True :
suite.addTest(EdamQueryTest('test_bad_uri'))
if run_error == True :
suite.addTest(EdamQueryTest('test_mandatory_property_missing'))
if run_curation == True :
suite.addTest(EdamQueryTest('test_formating'))
if run_error == True :
suite.addTest(EdamQueryTest('test_deprecated_replacement'))
if run_essential == True :
suite.addTest(EdamQueryTest('test_bad_uri_reference'))
if run_error == True :
suite.addTest(EdamQueryTest('test_missing_deprecated_property'))
if run_curation == True :
suite.addTest(EdamQueryTest('test_check_wikipedia_link'))
if run_curation == True :
suite.addTest(EdamQueryTest('test_identifier_property_missing'))
if run_error == True :
suite.addTest(EdamQueryTest('test_id_unique'))
if run_curation == True :
suite.addTest(EdamQueryTest('test_relation_too_broad'))
if run_curation == True :
suite.addTest(EdamQueryTest('test_duplicate_in_concept'))
# if run_curation == True :
# suite.addTest(EdamQueryTest('test_duplicate_all'))
if run_curation == True :
suite.addTest(EdamQueryTest('test_literal_links'))
if run_error == True :
suite.addTest(EdamQueryTest('test_next_id_modif'))
if run_error == True :
suite.addTest(EdamQueryTest('test_subset_id'))
return suite
# each test is added in function of the boolean input (error essential curation), to change level category, change tested variable (+ change level error when adding new_error in data frame)
class EdamQueryTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.edam_graph = ConjunctiveGraph()
cls.edam_graph.parse(os.environ.get('EDAM_PATH'), format='xml')
cls.report = pd.DataFrame(columns = ['Level','Test Name','Entity','Label','Debug Message'])
#print(cls.report)
################# DEPRECATED REPLACEMENT OBSOLETE ###########################
def test_deprecated_replacement_obsolete(self):
"""
Checks that the suggested replacement (replacedBy/consider) for a deprecated term is not obsolete.
"""
query = "queries/deprecated_replacement_obsolete.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','deprecated_replacement_obsolete',r['entity'],(f"'{r['label']}'"),(f"concept is replaced by ({r['property']}) an obsolete concept: {r['replacement']}")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# SUPERCLASS REFERS TO SELF ###########################
def test_super_class_refers_to_self(self):
"""
Checks if a given consept doesn't refers to itself as superclass.
"""
query = "queries/super_class_refers_to_self.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['ESSENTIAL','super_class_refers_to_self',r['entity'],(f"'{r['label']}'"),'concept declared as superclass of itself']], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# BAD URI ###########################
def test_bad_uri(self):
"""
Checks that the concepts URI matches the regex ^http://edamontology.org/(data|topic|operation|format)_[0-9]{4}$.
"""
query = "queries/bad_uri.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['ESSENTIAL','bad_rui',r['entity'],(f"'{r['label']}'"),'has a bad URI (entity) (regex :^http://edamontology.org/(data|topic|operation|format)_[0-9]\{4\}$)']], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# MANDATORY PROPERTY MISSING ###########################
def test_mandatory_property_missing(self):
"""
Checks that no mandatory property for all concepts are missing (oboInOwl:hasDefinition, rdfs:subClassOf, created_in, oboInOwl:inSubset, rdfs:label).
)
"""
query = "queries/mandatory_property_missing.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['ERROR','mandatory_property_missing',r['entity'],(f"'{r['label']}'"),(f"is missing mandatory property: {r['property']} ")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# FORMATING ###########################
def test_formating(self):
"""
Checks that no property has a space neither at the start nor the end, no tab and no end of line. Checks that label have not dot at the end and that definition do have a dot at the end.
"""
query = "queries/end_dot_def_missing.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','end_dot_def_missing',r['entity'],(f"'{r['label']}'"),'A dot is missing at the end of the definition.']], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
query = "queries/end_dot_label.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err += len(results) # add to same counter for the test
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','end_dot_label',r['entity'],(f"'{r['label']}'"),'There is an unwanted dot at the end of the label.']], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
query = "queries/end_space_annotation.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err += len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','end_space_annotation',r['entity'],(f"'{r['label']}'"),(f"There is an unwanted space at the end of {r['property']} : {r['value']}.")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
query = "queries/eol_in_annotation.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err += len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','eol_in_annotation',r['entity'],(f"'{r['label']}'"),(f"There is an unwanted end-of-line in {r['property']} : {r['value']}.")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
query = "queries/start_space_annotation.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err += len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','start_space_annotation',r['entity'],(f"'{r['label']}'"),(f"There is an unwanted space at the start of {r['property']} : {r['value']}.")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
query = "queries/tab_in_annotation.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err += len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','tab_in_annotation',r['entity'],(f"'{r['label']}'"),(f"There is an unwanted tabulation in {r['property']} : {r['value']}.")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# DEPRECATED REPLACEMENT ###########################
def test_deprecated_replacement(self):
""""
checks that every deprecated concept has a replacement suggested (replaced_by or consider).
"""
query = "queries/deprecated_replacement.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['ERROR','deprecated_replacement',r['entity'],(f"'{r['label']}'"),'is deprecated and is missing either a replacedBy property or a consider property']], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# BAD URI REFERENCE ###########################
def test_bad_uri_reference(self):
"""
Check that a reference (e.g. superclass) to another concept is actually declared in EDAM.
"get_uri.rq" retrieves all URI. "uri_reference.rq" retrieves all referenced URI. Then check if the references URIs are in the declared concept URIs.
"""
query = "queries/get_uri.rq"
uri=[]
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
f.close()
for r in results :
uri.append(r['entity'])
query = "queries/uri_reference.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
f.close()
nb_err = 0
for r in results:
if r['reference'] not in uri :
nb_err += 1
new_error = pd.DataFrame([['ESSENTIAL','bad_uri_reference',r['entity'],(f"'{r['label']}'"),(f"The property {r['property']} refers not an undeclared URI: '{r['reference']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# MISSING DEPRECATED PROPERTY ###########################
def test_missing_deprecated_property(self):
"""
Checks that no mandatory property for deprecated concept are missing (edam:obsolete_since, edam:oldParent, oboInOwl:inSubset <http://purl.obolibrary.org/obo/edam#obsolete>, rdfs:subClassOf <http://www.w3.org/2002/07/owl#DeprecatedClass>).
"""
query = "queries/missing_deprecated_property.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['ERROR','missing_deprecated_property',r['entity'],(f"'{r['label']}'"),(f"is missing mandatory deprecated property: {r['property']}")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# MISSING WIKIPEDIA LINK ###########################
def test_check_wikipedia_link(self):
"""
Checks that every topic has a wikipedia link filled in the seeAlso property.
"""
query = "queries/check_wikipedia_link.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','check_wikipedia_link',r['entity'],(f"'{r['label']}'"),"Topic concept missing a wikipedia link"]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# IDENTIFIER PROPERTY MISSING ###########################
def test_identifier_property_missing(self):
"""
Checks the no mandatory property for identifier (subclass of accession) are missing (regex).
"""
query = "queries/identifier_property_missing.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','identifier_property_missing',r['entity'],(f"'{r['label']}'"),"Missing regex property"]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# ID UNIQUE ###########################
def test_id_unique(self):
"""
Checks that no numeriacal part of the URI is duplicated.
"""
duplicate_id = check_unique_id(os.environ.get('EDAM_PATH'))
nb_err = len(duplicate_id)
query = "queries/get_uri.rq"
uri=[]
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
f.close()
for id in duplicate_id:
for r in results:
if id in str(r['entity']):
new_error = pd.DataFrame([['ERROR','id_unique',r['entity'],(f"'{r['label']}'"),(f"numerical id is used several times")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# RELATION TOO BROAD ###########################
def test_relation_too_broad(self):
"""
Checks that concept is not in relation (restriction) with a concept not recommanded for annotation.
"""
query = "queries/relation_too_broad.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','relation_too_broad',r['entity'],(f"'{r['label']}'"),(f"linked ({r['property']}) with a concept not recomanded for annotation : '{r['value']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# DUPLICATE IN CONCEPT ###########################
def test_duplicate_in_concept(self):
"""
Checks that there is no duplicate content (case insensitive) within a concept on given properties (see SPARQL query).
"""
query = "queries/duplicate_in_concept.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','duplicate_in_concept',r['entity'],(f"'{r['label']}'"),(f"{r['property']} and {r['property2']} have the same content: '{r['value']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
#here for each duplicate there will be 2 line in the table but this is mandaotry if there is 3 time the same content.
self.assertEqual(nb_err, 0)
################# DUPLICATE ALL ###########################
def test_duplicate_all(self):
"""
Checks that there is no duplicate content (case sensitive) across all the ontology on given properties (see SPARQl query).
"""
# this is case sensitive for computational time reasons
query = "queries/duplicate_all.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','duplicate_all',r['entity'],(f"'{r['label']}'"),(f"have the same content on {r['property']} as {r['entity2']} '{r['label2']}' on {r['property2']}: '{r['value']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
#here for each duplicate there will be 2 line in the table but this is mandaotry if there is 3 time the same content.
self.assertEqual(nb_err, 0)
################# LITERAL LINKS ###########################
def test_literal_links(self):
"""
Checks that all webpage and doi are literal links.
"""
query = "queries/literal_links.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['CURATION','literal_links',r['entity'],(f"'{r['label']}'"),(f"{r['property']} value is not declared as a literal: '{r['value']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# NEXT ID MODIF ###########################
def test_next_id_modif(self):
"""
Checks that the next id property is equal to the maximum id +1.
"""
query = "queries/get_id_and_next_id.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
f.close()
nb_err = 0
ids = []
for r in results:
ids.append(int(r['id']))
next_id = int(r['value'])
max_ids = max(ids)
if next_id != (max_ids+1) :
nb_err = 1
new_error = pd.DataFrame([['ERROR','next_id_modif','None','None',(f"The 'next_id' property has not been updated, it is not equal to the max id +1")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# SUBSET ID ###########################
def test_subset_id(self):
"""
Checks that the "subset" part of the id is the same as the superclass (e.g. data only subclass of data).
"""
query = "queries/subset_id.rq"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['ERROR','subset_id',r['entity'],(f"'{r['label']}'"),(f"Concept subset id ({r['subset']}) is different from its subclass {r['superclass']} '{r['label_sc']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
################# TEST NAME ###########################
def test_XXXTEST_NAMEXXX(self):
"""
Docstring documentation.
"""
query = "queries/XXXQUERY_FILEXXX"
with open(query,'r') as f:
query_term = f.read()
results = self.edam_graph.query(query_term)
nb_err = len(results)
f.close()
for r in results:
new_error = pd.DataFrame([['XXXLEVELXXX','XXXTEST_NAMEXXX',r['entity'],(f"'{r['label']}'"),(f" XXXDEBUG_MESSAGEXXX {r['property']}: '{r['value']}'")]], columns=['Level','Test Name','Entity','Label','Debug Message'])
self.__class__.report = pd.concat([self.report, new_error], ignore_index=True)
self.assertEqual(nb_err, 0)
@classmethod
def tearDownClass(cls):
#output = cls.report.sort('Level',)
if cls.report.empty == False:
print("\n_____________________________________________________________________________________________\n\nFollowing debug table can be found as a tsv file at the bottom of the summary of this job\n_____________________________________________________________________________________________")
pd.set_option("display.max_rows",None,"display.max_colwidth", 5000,"display.width",5000)
print(tabulate(cls.report[['Test Name','Entity','Label','Debug Message']], headers=['Test Name','Entity','Label','Debug Message']))
# prettify(cls.report[['Entity','Label','Debug Message']])
cls.report.to_csv("./output_edamci.tsv", sep='\t')
return super().tearDownClass()
if __name__ == '__main__':
run_error,run_essential,run_curation = parsing()
print(f"error = {run_error}, essential = {run_essential}, curation = {run_curation}")
runner = unittest.TextTestRunner()
sys.exit(runner.run(suite()))