-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·90 lines (79 loc) · 2.81 KB
/
main.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
#!/usr/bin/env python3
import requests
import click
@click.command()
@click.argument('genus')
@click.argument('species')
@click.option('--endpoint', '-e', help='Endpoint url, defaults to `https://treatment.ld.plazi.org/sparql`')
def main(genus, species, endpoint):
sparqlEndoint = endpoint or 'https://treatment.ld.plazi.org/sparql'
# genus = 'Tyrannosaurus'
# species = 'rex'
query1 = f'''PREFIX dwc: <http://rs.tdwg.org/dwc/terms/>
PREFIX treat: <http://plazi.org/vocab/treatment#>
SELECT DISTINCT ?tc WHERE {{
?tc dwc:genus "{genus}";
dwc:species "{species}";
a <http://filteredpush.org/ontologies/oa/dwcFP#TaxonConcept>.
}}'''
print(f'Searching {sparqlEndoint} for {genus} {species}')
r = requests.get(sparqlEndoint,
params={'query': query1},
headers={'accept': 'application/sparql-results+json'})
if r.status_code == 200:
named = [binding['tc']['value']
for binding in r.json()['results']['bindings']]
print(f'Found {len(named)} species that are {genus} {species}')
for url in named:
synonyms(url, sparqlEndoint)
def synonyms(url, sparqlEndoint):
query = f'''PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX treat: <http://plazi.org/vocab/treatment#>
SELECT DISTINCT
?tc
?aug ?augd (group_concat(DISTINCT ?augc;separator="; ") as ?augcs)
?def ?defd (group_concat(DISTINCT ?defc;separator="; ") as ?defcs)
?dpr ?dprd (group_concat(DISTINCT ?dprc;separator="; ") as ?dprcs)
WHERE {{
<{url}> ((^treat:deprecates/(treat:augmentsTaxonConcept|treat:definesTaxonConcept))|
((^treat:augmentsTaxonConcept|^treat:definesTaxonConcept)/treat:deprecates))* ?tc .
OPTIONAL {{
?aug treat:augmentsTaxonConcept ?tc;
dc:creator ?augc.
OPTIONAL {{
?aug treat:publishedIn ?augp.
?augp dc:date ?augd.
}}
}}
OPTIONAL {{
?def treat:definesTaxonConcept ?tc;
dc:creator ?defc.
OPTIONAL {{
?def treat:publishedIn ?defp.
?defp dc:date ?defd.
}}
}}
OPTIONAL {{
?dpr treat:deprecates ?tc;
dc:creator ?dprc.
OPTIONAL {{
?dpr treat:publishedIn ?dprp.
?dprp dc:date ?dprd.
}}
}}
}}
GROUP BY ?tc ?aug ?augd ?def ?defd ?dpr ?dprd'''
# print(f'Searching {sparqlEndoint} for {url}')
r = requests.get(sparqlEndoint,
params={'query': query},
headers={'accept': 'application/sparql-results+json'})
if r.status_code == 200:
print(f'Synonyms of {url}')
synonyms = sorted(list(set([binding['tc']['value'] for binding in r.json()[
'results']['bindings'] if binding['tc']['value'] != url])))
if len(synonyms):
[print(' · ' + s) for s in synonyms]
else:
print(' × None found')
if __name__ == "__main__":
main()