-
Notifications
You must be signed in to change notification settings - Fork 124
/
ontree.py
executable file
·1151 lines (1007 loc) · 42.8 KB
/
ontree.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Render a tree from a predicate root pair.
Normally run as a web service.
Usage:
ontree server [options]
ontree [options] <predicate-curie> <root-curie>
ontree --test
Options:
-a --api=API Full url to SciGraph api endpoint
--data-api=DAPI Full url to SciGraph data api endpoint
-k --key=APIKEY apikey for SciGraph instance
-p --port=PORT port on which to run the server [default: 8000]
-f --input-file=FILE don't use SciGraph, load an individual file instead
-o --outgoing if not specified defaults to incoming
-b --both if specified goes in both directions
-t --test run tests
-v --verbose print extra information
"""
import os
import asyncio
import subprocess
from pprint import pformat
from pathlib import Path
from datetime import datetime
from urllib.error import HTTPError
from urllib.parse import parse_qs
import htmlfn as hfn
from flask import (Flask,
url_for,
redirect,
request,
abort,
current_app,
send_from_directory)
from requests.exceptions import HTTPError as rHTTPError
from pyontutils import scigraph
from pyontutils.core import OntId, OntTerm, OntGraph
from pyontutils.scig import ImportChain, makeProv
from pyontutils.utils import getSourceLine, get_working_dir, makeSimpleLogger
from pyontutils.utils import Async, deferred, UTCNOWISO
from pyontutils.config import auth
from pyontutils.hierarchies import (Query,
creatTree,
dematerialize,
flatten as flatten_tree)
from pyontutils.namespaces import rdfs, OntCuries
from pyontutils.scigraph_codegen import moduleDirect
from nifstd_tools.sheets_sparc import (hyperlink_tree,
tag_row,
open_custom_sparc_view_yml,
YML_DELIMITER)
from nifstd_tools.simplify import simplify, apinat_deblob, cleanBad
from nifstd_tools import __version__
log = makeSimpleLogger('ontree')
# FIXME these will go to network which is :/
sgg = scigraph.Graph(cache=False, verbose=True)
sgv = scigraph.Vocabulary(cache=False, verbose=True)
sgc = scigraph.Cypher(cache=False, verbose=True)
sgd = scigraph.Dynamic(cache=False, verbose=True)
# This was for ttl creation extension for sparc view
# ixr.setup(instrumented=OntTerm)
a = 'rdfs:subClassOf'
_hpp = 'RO_OLD:has_proper_part' # and apparently this fails too
hpp = 'http://www.obofoundry.org/ro/ro.owl#has_proper_part'
hpp = 'NIFRID:has_proper_part'
po = 'BFO:0000050' # how?! WHY does this fail!? the curie is there!
_po = 'http://purl.obolibrary.org/obo/BFO_0000050'
hr = 'RO:0000087'
_hr = 'http://purl.obolibrary.org/obo/RO_0000087'
inc = 'INCOMING'
out = 'OUTGOING'
both = 'BOTH'
uot = (
"UBERON:0000056",
"UBERON:0000057",
"UBERON:0000074",
"UBERON:0001224",
"UBERON:0001226",
"UBERON:0001227",
"UBERON:0001255",
"UBERON:0001287",
"UBERON:0001290",
"UBERON:0001291",
"UBERON:0001292",
"UBERON:0004193",
"UBERON:0004203",
"UBERON:0004204",
"UBERON:0004205",
"UBERON:0004639",
"UBERON:0004640",
"UBERON:0005096",
"UBERON:0005097",
"UBERON:0005750",
"UBERON:0005751",
"UBERON:0009973",
"UBERON:0012240",
"UBERON:0012242",
)
def time():
''' Get current time in a year-month-day:meta format '''
return str(datetime.utcnow().isoformat()).replace('.', ',')
def node_list(nodes):
log.debug(pformat(nodes))
# FIXME why the heck are these coming in as lbl now?!?!
s = sorted([(n['lbl'], hfn.atag(n['id'], n['lbl'], new_tab=True))
for n in nodes])
return '<br>\n'.join([at for _, at in s])
def graphFromGithub(link, verbose=False):
# mmmm no validation
# also caching probably
if verbose:
log.info(link)
g = OntGraph().parse(f'{link}?raw=true', format='turtle')
OntCuries.populate(g)
return g
collapse_apinat = [ # FIXME config?
['apinatomy:conveys', 'apinatomy:source'],
['apinatomy:conveys', 'apinatomy:target'],
['apinatomy:conveys', 'apinatomy:source',
# FIXME if the final external is missing the rendering
# halts at layerIn and doesn't show the subtree
'apinatomy:internalIn', 'apinatomy:layerIn', 'apinatomy:external'],
['apinatomy:conveys', 'apinatomy:target',
'apinatomy:internalIn', 'apinatomy:layerIn', 'apinatomy:external'],
['apinatomy:fasciculatesIn', 'apinatomy:external'],
['apinatomy:fasciculatesIn', 'apinatomy:layerIn', 'apinatomy:external'],
['apinatomy:fasciculatesIn', 'apinatomy:supertype', 'apinatomy:external'],
['apinatomy:fasciculatesIn', 'apinatomy:cloneOf', 'apinatomy:supertype', 'apinatomy:external'],
]
def sparc_dynamic(data_sgd, data_sgc, path, wgb, process=lambda coll, blob: blob):
args = dict(request.args)
if 'direction' in args:
direction = args.pop('direction')
else:
direction = 'OUTGOING' # should always be outgoing here since we can't specify?
if 'format' in args:
format_ = args.pop('format')
else:
format_ = None
if 'apinat' in path: # FIXME bad hardcoded hack
_old_get = data_sgd._get
try:
data_sgd._get = data_sgd._normal_get
j = data_sgd.dispatch(path, **args)
except ValueError as e:
log.exception(e)
abort(404)
except rHTTPError as e:
log.exception(e)
abort(e.response.status_code) # DO NOT PASS ALONG THE MESSAGE
finally:
data_sgd._get = _old_get
else:
try:
j = data_sgd.dispatch(path, **args)
except rHTTPError as e:
log.exception(e)
abort(e.response.status_code) # DO NOT PASS ALONG THE MESSAGE
except ValueError as e:
log.exception(e)
abort(404)
j = process(collapse_apinat, j)
if j is None or 'edges' not in j:
log.error(pformat(j))
return abort(400)
elif not j['edges']:
return node_list(j['nodes']) # FIXME ... really should error?
if path.endswith('housing-lyphs'): # FIXME hack
root = 'NLX:154731'
#direction = 'INCOMING'
else:
root = None
prov = [
hfn.titletag(f'Dynamic query result for {path}'),
f'<meta name="date" content="{UTCNOWISO()}">',
f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">',
'<meta name="representation" content="SciGraph">',
('<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" '
f'href="{data_sgd._last_url}">')]
kwargs = {'json': cleanBad(j),
'html_head': prov,
'prefixes': data_sgc.getCuries(), # FIXME efficiency
}
tree, extras = creatTree(*Query(root, None, direction, None), **kwargs)
#print(extras.hierarhcy)
#print(tree)
if format_ is not None:
if format_ == 'table':
#breakpoint()
def nowrap(class_, tag=''):
return (f'{tag}.{class_}'
'{ white-space: nowrap; }')
ots = [OntTerm(n)
for n in flatten_tree(extras.hierarchy)
if 'CYCLE' not in n]
#rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots]
rows = [[ot.label, hfn.atag(ot.iri, ot.curie), ot.definition]
for ot in ots]
return hfn.htmldoc(hfn.render_table(rows, 'label', 'curie', 'definition'),
styles=(hfn.table_style, nowrap('col-label', 'td')))
return hfn.htmldoc(extras.html,
other=prov,
styles=hfn.tree_styles)
def render(pred, root, direction=None, depth=10, local_filepath=None,
branch='master', restriction=False, wgb='FIXME', local=False,
verbose=False, flatten=False,):
kwargs = {'local':local, 'verbose':verbose}
prov = makeProv(pred, root, wgb)
if local_filepath is not None:
github_link = ('https://github.com/SciCrunch/NIF-Ontology/raw/'
f'{branch}/{local_filepath}')
prov.append('<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" '
f'href="{github_link}">')
graph = graphFromGithub(github_link, verbose)
qname = graph.namespace_manager._qhrm # FIXME
labels_index = {qname(s):str(o) for s, o in graph[:rdfs.label:]}
if pred == 'subClassOf':
pred = 'rdfs:subClassOf' # FIXME qname properly?
elif pred == 'subPropertyOf':
pred = 'rdfs:subPropertyOf'
try:
kwargs['json'] = graph.asOboGraph(pred, restriction=restriction)
kwargs['prefixes'] = {k:str(v) for k, v in graph.namespace_manager}
except KeyError as e:
if verbose:
log.error(str(e))
return abort(422, 'Unknown predicate.')
else:
kwargs['graph'] = sgg
# FIXME this does not work for a generic scigraph load ...
# and it should not be calculated every time anyway!
# oh look, here we are needed a class again
if False:
versionIRI = [
e['obj']
for e in sgg.getNeighbors('http://ontology.neuinfo.org/'
'NIF/ttl/nif.ttl')['edges']
if e['pred'] == 'versionIRI'][0]
#print(versionIRI)
prov.append('<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" '
f'href="{versionIRI}">') # FIXME wrong and wont resolve
prov.append(
('<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" '
f'href="{sgg._last_url}">'))
prov.append('<meta name="representation" content="SciGraph">') # FIXME :/
kwargs['html_head'] = prov
try:
if root.startswith('http'): # FIXME this codepath is completely busted?
if 'prefixes' in kwargs:
rec = None
for k, v in kwargs.items():
if root.startswith(v):
rec = k + 'r:' + root.strip(v) # FIXME what?!
break
if rec is None:
raise KeyError('no prefix found for {root}')
else:
rec = sgv.findById(root)
if 'curie' in rec:
root_curie = rec['curie']
# FIXME https://github.com/SciGraph/SciGraph/issues/268
if not root_curie.endswith(':') and '/' not in root_curie:
root = root_curie
else:
kwargs['curie'] = root_curie
elif 'prefixes' not in kwargs and root.endswith(':'):
kwargs['curie'] = root
root = sgc._curies[root.rstrip(':')] # also 268
tree, extras = creatTree(*Query(root, pred, direction, depth), **kwargs)
dematerialize(list(tree.keys())[0], tree)
if flatten:
if local_filepath is not None:
def safe_find(n):
return {'labels':[labels_index[n]],
'deprecated': False # FIXME inacurate
}
else:
def safe_find(n): # FIXME scigraph bug
if n.endswith(':'):
n = sgc._curies[n.rstrip(':')]
elif '/' in n:
prefix, suffix = n.split(':')
iriprefix = sgc._curies[prefix]
n = iriprefix + suffix
return sgv.findById(n)
out = set(n for n in flatten_tree(extras.hierarchy))
try:
lrecs = Async()(deferred(safe_find)(n) for n in out)
except RuntimeError:
asyncio.set_event_loop(current_app.config['loop'])
lrecs = Async()(deferred(safe_find)(n) for n in out)
rows = sorted(((r['labels'][0] if r['labels'] else '')
+ ',' + n for r, n in zip(lrecs, out)
# FIXME still stuff wrong, but better for non cache case
if not r['deprecated']), key=lambda lid: lid.lower())
return '\n'.join(rows), 200, {'Content-Type':'text/plain;charset=utf-8'}
else:
return hfn.htmldoc(extras.html,
other=prov,
styles=hfn.tree_styles)
except (KeyError, TypeError) as e:
if verbose:
log.error(f'{type(e)} {e}')
if sgg.getNode(root):
# FIXME distinguish these cases...
message = 'Unknown predicate or no results.'
elif 'json' in kwargs:
message = 'Unknown root.'
r = graph.namespace_manager.expand(root)
for s in graph.subjects():
if r == s:
message = ('No results. '
'You are querying a ttl file directly, '
'did you remember to set ?restriction=true?')
break
else:
message = 'Unknown root.'
return abort(422, message)
def getArgs(request):
want = {'direction':inc, # INCOMING OUTGOING BOTH
'depth':10,
'branch':'master',
'restriction':False, # True False
'local':False, # True False # canonoical vs scigraph ? interlex?
'flatten':False,
}
def convert(k):
if k in request.args:
v = request.args[k]
else:
return want[k]
if isinstance(want[k], bool):
if v.lower() == 'true':
return True
elif v.lower() == 'false':
return False
else:
raise TypeError(f'Expected a bool, got "{v}" instead.')
elif isinstance(want[k], int):
try:
return int(v)
except (TypeError, ValueError) as e:
raise TypeError(f'Expected an int, got "{v}" instead.') from e
else:
return v
return {k:convert(k)
for k, v in want.items()}
def sanitize(pred, kwargs):
if pred == 'isDefinedBy' and kwargs['depth'] > 1:
return abort(400, 'isDefinedBy not allowed for queries with depth > 1.')
examples = (
('Brain parts', hpp, 'UBERON:0000955', '?direction=OUTGOING'), # FIXME direction=lol doesn't cause issues...
('Brain parts alt', po, 'UBERON:0000955'),
('Brain parts alt flat', po, 'UBERON:0000955', '?flatten=true'),
('Anatomical entities', a, 'UBERON:0001062'),
('Cell parts', a, 'GO:0044464'),
('Cells', a, 'SAO:1813327414'),
('Proteins', a, 'SAO:26622963'),
('GPCRs', a, 'NIFEXT:5012'),
('Mulitmeric ion channels', a, 'NIFEXT:2502'),
('Monomeric ion channels', a, 'NIFEXT:2500'),
('Diseases', a, 'DOID:4'),
('Vertebrata', a, 'NCBITaxon:7742', '?depth=40'),
('Metazoa', a, 'NCBITaxon:33208', '?depth=40'),
('Rodentia', a, 'NCBITaxon:9989'),
('Insecta', a, 'NCBITaxon:50557', '?depth=40'),
('Neurotransmitters', hr, 'CHEBI:25512'),
('Neurotransmitters', a, 'NLXMOL:100306'),
('IRIs ok for roots', a, 'http://uri.neuinfo.org/nif/nifstd/nlx_mol_100306'),
('Provenance', 'isDefinedBy',
'http://ontology.neuinfo.org/NIF/ttl/generated/chebislim.ttl', '?depth=1'),
)
extra_examples = (
('No tech?', a, 'ilxtr:technique'),
('Cell septum', a, 'GO:0044457'),
('Old NIFGA part of', hpp, 'BIRNLEX:796'),
('Cereberal cortex parts', po, 'UBERON:0002749'),
('Broken iri of borken curie', a, 'http://uri.interlex.org/paxinos/uris/rat/labels/'),
('Broken curie', a, 'PAXRAT:'),
)
file_examples = (
('Resources', a, 'NLXRES:20090101', 'ttl/resources.ttl'),
('Staging branch', a, 'PAXRAT:',
'ttl/generated/parcellation/paxinos-rat-labels.ttl', '?branch=staging'),
('Restriction example', hpp, 'UBERON:0000955',
'ttl/bridge/uberon-bridge.ttl', '?direction=OUTGOING&restriction=true'),
)
dynamic_examples = (
('Shortest path', 'shortestSimple',
'?start_id=UBERON:0000955&end_id=UBERON:0001062&relationship=subClassOf'),
('Shortest path table', 'shortestSimple',
'?start_id=UBERON:0000955&end_id=UBERON:0001062&relationship=subClassOf&format=table'),
('Stomach parts', 'prod/sparc/organParts/FMA:7148', None),
('Parc graph', 'prod/sparc/parcellationGraph', '?direction=INCOMING'),
('Parc arts', 'prod/sparc/parcellationArtifacts/NCBITaxon:10116', '?direction=INCOMING'),
('Parc roots', 'prod/sparc/parcellationRoots/NCBITaxon:10116', '?direction=INCOMING'),
('Parc region roots', 'prod/sparc/parcellationRoots/NCBITaxon:10116/UBERON:0000955', '?direction=INCOMING'),
)
demo_examples = (
#('All connectivity', '/trees/sparc/demos/isan2019/flatmap-queries'), # broken due to scigraph segfault for json return
('Neuron connectivity', '/trees/sparc/demos/isan2019/neuron-connectivity'),
)
def server(api_key=None, verbose=False, data_endpoint=None):
if data_endpoint:
_dataBP = data_endpoint
scigraphd = moduleDirect(_dataBP, 'scigraphd')
else:
from pyontutils import scigraph as scigraphd
data_sgd = scigraphd.Dynamic(cache=True, verbose=True, do_error=True)
data_sgc = scigraphd.Cypher(cache=True, verbose=True)
if data_endpoint:
data_sgd._basePath = _dataBP
data_sgc._basePath = _dataBP
f = Path(__file__).resolve()
working_dir = get_working_dir(__file__)
resources = auth.get_path('resources')
if working_dir:
git_dir = working_dir / '.git'
else:
git_dir = Path('/dev/null')
try:
if working_dir is not None:
ref = subprocess.check_output(['git',
'--git-dir', git_dir.as_posix(),
'--work-tree', working_dir.as_posix(),
'rev-parse', 'HEAD'],
stderr=subprocess.DEVNULL).decode().rstrip()
else:
ver = __version__
if '+' in ver:
ref = ver.split('+', 1)[-1]
else:
ref = ver
except subprocess.CalledProcessError:
ref = 'master' # 'NO-REPO-AT-MOST-TODO-GET-LATEST-HASH'
wasGeneratedBy = ('https://github.com/tgbugs/pyontutils/blob/'
f'{ref}/pyontutils/{f.name}'
'#L{line}')
line = getSourceLine(render)
wgb = wasGeneratedBy.format(line=line)
importchain = ImportChain(sgg=sgg, sgc=sgc, wasGeneratedBy=wasGeneratedBy)
importchain.make_html() # run this once, restart services on a new release
loop = asyncio.get_event_loop()
app = Flask('ontology tree service')
app.config['loop'] = loop
# gsheets = GoogleSheets()
sparc_view = open_custom_sparc_view_yml()
# sparc_view_raw = open_custom_sparc_view_yml(False)
log.info('starting index load')
uot_terms = [OntTerm(t) for t in uot]
uot_lookup = {t.label:t for t in uot_terms}
uot_ordered = sorted(t.label for t in uot_terms)
basename = 'trees'
@app.route(f'/{basename}', methods=['GET'])
@app.route(f'/{basename}/', methods=['GET'])
def route_():
d = url_for('route_docs')
e = url_for('route_examples')
i = url_for('route_import_chain')
return hfn.htmldoc(hfn.atag(d, 'Docs'),
'<br>',
hfn.atag(e, 'Examples'),
'<br>',
hfn.atag(i, 'Import chain'),
title='NIF ontology hierarchies')
@app.route(f'/{basename}/docs', methods=['GET'])
def route_docs():
return redirect('https://github.com/SciCrunch/NIF-Ontology/blob/master/docs') # TODO
@app.route(f'/{basename}/examples', methods=['GET'])
def route_examples():
links = hfn.render_table(
[[name,
hfn.atag(url_for("route_query", pred=pred, root=root) + (args[0] if args else ''),
f'../query/{pred}/{root}{args[0] if args else ""}')]
for name, pred, root, *args in examples],
'Root class',
'../query/{predicate-curie}/{root-curie}?direction=INCOMING&depth=10&branch=master&local=false',
halign='left')
flinks = hfn.render_table(
[[name,
hfn.atag(url_for("route_filequery", pred=pred, root=root, file=file) + (args[0] if args else ''),
f'../query/{pred}/{root}/{file}{args[0] if args else ""}')]
for name, pred, root, file, *args in file_examples],
'Root class',
'../query/{predicate-curie}/{root-curie}/{ontology-filepath}?direction=INCOMING&depth=10&branch=master&restriction=false',
halign='left')
dlinks = hfn.render_table(
[[name,
hfn.atag(url_for("route_dynamic", path=path) + (querystring if querystring else ''),
f'../query/dynamic/{path}{querystring if querystring else ""}')]
for name, path, querystring in dynamic_examples],
'Root class',
'../query/dynamic/{path}?direction=OUTGOING&dynamic=query&args=here',
halign='left')
return hfn.htmldoc(links, flinks, dlinks, title='Example hierarchy queries')
@app.route(f'/{basename}/sparc/demos/isan2019/neuron-connectivity', methods=['GET'])
def route_sparc_demos_isan2019_neuron_connectivity():
def connected(start):
log.debug(start)
blob = data_sgd.neurons_connectivity(start)#, limit=9999)
edges = blob['edges']
neurons = {}
types = {}
rows = []
start_type = None
sc = OntId(start).curie
for e in edges:
s, p, o = e['sub'], e['pred'], e['obj']
if p == 'operand':
continue
if s.startswith('_:'):
if s not in neurons:
neurons[s] = []
types[s] = {}
otp = OntTerm(p)
oto = OntTerm(o)
neurons[s].append((otp, oto))
if o == sc:
start_type = otp
if oto not in types[s]:
types[s][oto] = []
types[s][oto].append(otp)
for v in neurons.values():
v.sort()
return OntTerm(start), start_type, neurons, types
hrm = [connected(t) for t in set(test_terms)]
header =['Start', 'Start Type', 'Neuron', 'Relation', 'Target']
rows = []
for start, start_type, neurons, types in sorted(hrm):
start = start.atag()
start_type = start_type.atag() if start_type is not None else ''
for i, v in enumerate(neurons.values(), 1):
neuron = i
for p, o in v:
relation = p.atag()
target = o.atag()
row = start, start_type, neuron, relation, target
rows.append(row)
rows.append(['|'] + [' '] * 4)
h = hfn.htmldoc(hfn.render_table(rows, *header), title='neuron connectivity')
return h
@app.route(f'/{basename}/sparc/demos/isan2019/flatmap-queries', methods=['GET'])
def route_sparc_demos_isan2019_flatmap_queries():
# lift up to load from an external source at some point
# from pyontutils.core import OntResPath
# orp = OntResPath('annotations.ttl')
# [i for i in sorted(set(OntId(e) for t in orp.graph for e in t)) if i.prefix in ('UBERON', 'FMA', 'ILX')]
query = """
MATCH (blank)-
[entrytype:ilxtr:hasSomaLocatedIn|ilxtr:hasAxonLocatedIn|ilxtr:hasDendriteLocatedIn|ilxtr:hasPresynapticTerminalsIn]
->(location:Class{{iri: "{iri}"}})
WITH entrytype, location, blank
MATCH (phenotype)<-[predicate]-(blank)<-[:equivalentClass]-(neuron)
WHERE NOT (phenotype.iri =~ ".*_:.*")
RETURN location, entrytype.iri, neuron.iri, predicate.iri, phenotype
"""
def fetch(iri, limit=10):
q = query.format(iri=iri)
log.debug(q)
blob = data_sgc.execute(q, limit, 'application/json')
# oh boy
# if there are less results than the limit scigraph segfaults
# and returns nothing
return blob
hrm = [fetch(oid) for oid in test_terms]
return hfn.htmldoc(hfn.render_table([[1, 2]],'oh', 'no'),
title='Simulated flatmap query results',
)
@app.route(f'/{basename}/sparc/demos/apinat', methods=['GET'])
@app.route(f'/{basename}/sparc/demos/apinat/', methods=['GET'])
def route_sparc_demos_apinat():
return 'apinat'
@app.route(f'/{basename}/sparc/connectivity/query', methods=['GET'])
def route_sparc_connectivity_query():
kwargs = request.args
log.debug(kwargs)
script = """
var ele = document.getElementById('model-selector')
ele.onselect
"""
return hfn.htmldoc(hfn.render_form(
[[('Model',), {}],
[None, None],
[
#('Kidney', 'Defensive breathing',), # TODO autopopulate
('Urinary Omega Tree',),
{'id':'model-selector', 'name': 'model'}]], # FIXME auto via js?
# FIXME must switch start and stop per model (argh)
# or hide/show depending on which model is selected
[[('start',), {}],
[None, None],
[
#('one', 'two', 'three'), # TODO auto populate
uot_ordered,
{'name': 'start'}]], # FIXME auto via js?
[[('end',), {}],
[None, None],
[
#('one', 'two', 'three'), # TODO auto populate
uot_ordered,
{'name': 'end'}]], # FIXME auto via js?
[[tuple(), {}],
[tuple(), {'type': 'submit', 'value': 'Query'}],
[None, None]] # FIXME auto via js?
, action='view', method='POST'
),
scripts=(script,),
title='Connectivity query')
@app.route(f'/{basename}/sparc/connectivity/view', methods=['POST'])
def route_sparc_connectivity_view():
kwargs = request.args
log.debug(kwargs)
# FIXME error handling for bad data
model = request.form['model']
start = request.form['start']
end = request.form['end']
sid = uot_lookup[start].curie
eid = uot_lookup[end].curie
#start = 'UBERON:0005157'
#end = 'UBERON:0001255'
return redirect(f'/{basename}/sparc/dynamic/shortestSimple?start_id={sid}'
f'&end_id={eid}&direction=INCOMING&format=table') # TODO
#return hfn.htmldoc(title='Connectivity view')
@app.route(f'/{basename}/sparc/simple/dynamic/<path:path>', methods=['GET'])
def route_sparc_simple_dynamic(path):
if '/neru-' in path:
process = lambda x, blob: apinat_deblob(blob, remove_converge=True)[0]
else:
process = simplify
return sparc_dynamic(data_sgd, data_sgc, path, wgb, process)
@app.route(f'/{basename}/sparc/dynamic/<path:path>', methods=['GET'])
def route_sparc_dynamic(path):
return sparc_dynamic(data_sgd, data_sgc, path, wgb)
@app.route(f'/{basename}/dynamic/<path:path>', methods=['GET'])
def route_dynamic(path):
args = dict(request.args)
if 'direction' in args:
direction = args.pop('direction')
else:
direction = 'OUTGOING' # should always be outgoing here since we can't specify?
if 'format' in args:
format_ = args.pop('format')
else:
format_ = None
try:
j = sgd.dispatch(path, **args)
except rHTTPError as e:
log.exception(e)
abort(e.response.status_code) # DO NOT PASS ALONG THE MESSAGE
except ValueError as e:
log.exception(e)
abort(404)
if j is None or 'edges' not in j or not j['edges']:
log.error(pformat(j))
log.debug(sgd._last_url)
edges = [{'sub': 'ROOT', 'pred': 'hasMember', 'obj': n['id']}
for n in j['nodes']]
j['edges'] = edges
j['nodes'].append({'id': 'ROOT', 'lbl': 'ROOT'}) # in case of direction
#return abort(400)
prov = [hfn.titletag(f'Dynamic query result for {path}'),
f'<meta name="date" content="{UTCNOWISO()}">',
f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">',
'<meta name="representation" content="SciGraph">',
f'<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" href="{sgd._last_url}">']
kwargs = {'json': cleanBad(j),
'html_head': prov}
tree, extras = creatTree(*Query(None, None, direction, None), **kwargs)
#print(extras.hierarhcy)
#print(tree)
if format_ is not None:
if format_ == 'table':
#breakpoint()
def nowrap(class_, tag=''):
return (f'{tag}.{class_}'
'{ white-space: nowrap; }')
ots = [OntTerm(n) for n in flatten_tree(extras.hierarchy) if 'CYCLE' not in n]
#rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots]
rows = [[ot.label, hfn.atag(ot.iri, ot.curie), ot.definition] for ot in ots]
return hfn.htmldoc(hfn.render_table(rows, 'label', 'curie', 'definition'),
styles=(hfn.table_style, nowrap('col-label', 'td')))
return hfn.htmldoc(extras.html,
other=prov,
styles=hfn.tree_styles)
@app.route(f'/{basename}/imports/chain', methods=['GET'])
def route_import_chain():
return importchain.html
@app.route(f'/{basename}/query/<pred>/<root>', methods=['GET'])
def route_query(pred, root):
kwargs = getArgs(request)
kwargs['wgb'] = wgb
maybe_abort = sanitize(pred, kwargs)
if maybe_abort is not None:
return maybe_abort
if verbose:
kwargs['verbose'] = verbose
log.debug(str(kwargs))
return render(pred, root, **kwargs)
@app.route(f'/{basename}/query/<pred>/http:/<path:iri>', methods=['GET']) # one / due to nginx
@app.route(f'/{basename}/query/<pred>/https:/<path:iri>', methods=['GET']) # just in case
def route_iriquery(pred, iri): # TODO maybe in the future
root = 'http://' + iri # for now we have to normalize down can check request in future
if verbose:
log.debug(f'ROOOOT {root}')
kwargs = getArgs(request)
kwargs['wgb'] = wgb
maybe_abort = sanitize(pred, kwargs)
if maybe_abort is not None:
return maybe_abort
if verbose:
kwargs['verbose'] = verbose
log.debug(str(kwargs))
return render(pred, root, **kwargs)
@app.route(f'/{basename}/query/<pred>/<root>/<path:file>', methods=['GET'])
def route_filequery(pred, root, file):
kwargs = getArgs(request)
kwargs['local_filepath'] = file
kwargs['wgb'] = wgb
maybe_abort = sanitize(pred, kwargs)
if maybe_abort is not None:
return maybe_abort
if verbose:
kwargs['verbose'] = verbose
log.debug(str(kwargs))
try:
return render(pred, root, **kwargs)
except HTTPError:
return abort(404, 'Unknown ontology file.') # TODO 'Unknown git branch.'
@app.route(f'/{basename}/sparc/view/<tier1>', methods=['GET'])
@app.route(f'/{basename}/sparc/view/<tier1>/', methods=['GET'])
@app.route(f'/{basename}/sparc/view/<tier1>/<tier2>', methods=['GET'])
@app.route(f'/{basename}/sparc/view/<tier1>/<tier2>/', methods=['GET'])
def route_sparc_view_query(tier1, tier2=None):
journey = sparc_view
if tier1 not in journey:
return abort(404)
journey = journey[tier1]
if tier2 is not None:
if tier2 not in journey:
return abort(404)
journey = journey[tier2]
hyp_rows = hyperlink_tree(journey)
return hfn.htmldoc(
hfn.render_table(hyp_rows),
title = 'Terms for ' + (tier2 if tier2 is not None else tier1),
metas = ({'name':'date', 'content':time()},),
)
@app.route(f'/{basename}/sparc/view', methods=['GET'])
@app.route(f'/{basename}/sparc/view/', methods=['GET'])
def route_sparc_view():
hyp_rows = []
spaces = hfn.nbsp * 8
for tier1, tier2_on in sorted(sparc_view.items()):
url = url_for('route_sparc_view_query', tier1=tier1)
tier1_row = tier1.split(YML_DELIMITER)
tier1_row += tier2_on['CURIES']
tagged_tier1_row = tag_row(tier1_row, url)
hyp_rows.append(tagged_tier1_row)
if not tier2_on:
continue
# BUG: Will break what we want if more is added to spinal cord
if len(tier2_on.keys()) > 15:
continue
if tier1_row[0] == 'Nerve roots of spinal cord segments':
continue
for tier2, tier3_on in tier2_on.items():
if tier2 == 'CURIES':
continue
url = url_for('route_sparc_view_query', tier1=tier1, tier2=tier2)
tier2_row = tier2.split(YML_DELIMITER)
tier2_row += tier3_on['CURIES']
tagged_tier2_row = tag_row(row=tier2_row, url=url, tier_level=1)
if len(list(sparc_view[tier1_row[0]][tier2_row[0]].keys())) == 1:
tagged_tier2_row[0] = spaces+tier2_row[0]
hyp_rows.append(tagged_tier2_row)
return hfn.htmldoc(
hfn.render_table(hyp_rows),
title= 'Main Page Sparc',
styles= ["p {margin: 0px; padding: 0px;}"],
metas= ({'name':'date', 'content':time()},),
)
@app.route(f'/{basename}/sparc/index', methods=['GET'])
@app.route(f'/{basename}/sparc/index/', methods=['GET'])
def route_sparc_index():
hyp_rows = hyperlink_tree(sparc_view)
return hfn.htmldoc(
hfn.render_table(hyp_rows),
title = 'SPARC Anatomical terms index',
metas = ({'name':'date', 'content':time()},),
)
@app.route(f'/{basename}/sparc', methods=['GET'])
@app.route(f'/{basename}/sparc/', methods=['GET'])
def route_sparc():
# FIXME TODO route to compiled
p = Path('/var/www/ontology/trees/sparc/sawg.html')
if p.exists():
return send_from_directory(p.parent.as_posix(), p.name)
log.critical(f'{resources}/sawg.org has not been published')
return send_from_directory(resources.as_posix(), 'sawg.org')
#return hfn.htmldoc(
#atag(url_for('route_sparc_view'), 'Terms by region or atlas'), '<br>',
#atag(url_for('route_sparc_index'), 'Index'),
#title='SPARC Anatomical terms', styles=["p {margin: 0px; padding: 0px;}"],
#metas = ({'name':'date', 'content':time()},),
#)
return app
# for now hardcode
test_terms = [OntId('UBERON:0001759'),
OntId('UBERON:0000388'),
OntId('UBERON:0001629'),
OntId('UBERON:0001723'),
OntId('UBERON:0001737'),
OntId('UBERON:0001930'),
OntId('UBERON:0001989'),
OntId('UBERON:0001990'),
OntId('UBERON:0002024'),
OntId('UBERON:0002440'),
OntId('UBERON:0003126'),
OntId('UBERON:0003708'),
OntId('UBERON:0009050'),
OntId('UBERON:0011326'),
OntId('FMA:6240'),
OntId('FMA:6243'),
OntId('FMA:6474'),
OntId('FMA:6579'),
OntId('ILX:0738293'),
# # #
OntId('UBERON:0000388'),
OntId('UBERON:0000988'),
OntId('UBERON:0001103'),
OntId('UBERON:0001508'),
OntId('UBERON:0001629'),
OntId('UBERON:0001649'),
OntId('UBERON:0001650'),
OntId('UBERON:0001723'),
OntId('UBERON:0001737'),
OntId('UBERON:0001759'),
OntId('UBERON:0001884'),
OntId('UBERON:0001891'),
OntId('UBERON:0001896'),
OntId('UBERON:0001930'),
OntId('UBERON:0001990'),
OntId('UBERON:0002024'),
OntId('UBERON:0002126'),
OntId('UBERON:0002440'),
OntId('UBERON:0003126'),
OntId('UBERON:0003708'),
OntId('UBERON:0005807'),
OntId('UBERON:0006457'),
OntId('UBERON:0006469'),
OntId('UBERON:0006470'),
OntId('UBERON:0006488'),
OntId('UBERON:0006489'),
OntId('UBERON:0006490'),
OntId('UBERON:0006491'),
OntId('UBERON:0006492'),
OntId('UBERON:0006493'),
OntId('UBERON:0009009'),
OntId('UBERON:0009918'),
OntId('UBERON:0011326'),
OntId('FMA:67532'),
OntId('FMA:7643'),
OntId('ILX:0485722'),
OntId('ILX:0505839'),
OntId('ILX:0725956'),
OntId('ILX:0731873'),
OntId('ILX:0736966'),
OntId('ILX:0738279'),
OntId('ILX:0738290'),
OntId('ILX:0738291'),
OntId('ILX:0738292'),
OntId('ILX:0738293'),
OntId('ILX:0738308'),
OntId('ILX:0738309'),
OntId('ILX:0738312'),
OntId('ILX:0738313'),
OntId('ILX:0738315'),
OntId('ILX:0738319'),
OntId('ILX:0738324'),