Wiki source code of Programming Interface Application (API)
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | == Programmatic access (API) == | ||
2 | |||
3 | The EBRAINS Knowledge Graph provides convenient tools and APIs for the implementation of queries into your scripts. | ||
4 | |||
5 | === Request permission === | ||
6 | |||
7 | To be able to access the Knowledge Graph (KG) programmatically, you require: | ||
8 | |||
9 | 1. An EBRAINS account. Register for an account here: [[https:~~/~~/ebrains.eu/register/>>https://ebrains.eu/register/]] | ||
10 | 1. Register and request credentials for your KG client by emailing support@ebrains.eu | ||
11 | |||
12 | === Writting Knowledge Graph queries === | ||
13 | |||
14 | A query for the Knowledge Graph is defined declaratively in JSON-LD and always executed against a specific data structure (e.g. a "Dataset version") allowing the graph to be traversed. | ||
15 | |||
16 | In the snippet below, we can see that we first define the semantical context for the query with //"@context",// which belongs to the namespace "//https:~/~/schema.hbp.eu/graphQuery/".// | ||
17 | |||
18 | {{code language="python"}} | ||
19 | { | ||
20 | "@context": { | ||
21 | "@vocab": "https://core.kg.ebrains.eu/vocab/query/", | ||
22 | "query": "https://schema.hbp.eu/myQuery/", | ||
23 | "propertyName": { | ||
24 | "@id": "propertyName", | ||
25 | "@type": "@id" | ||
26 | }, | ||
27 | "path": { | ||
28 | "@id": "path", | ||
29 | "@type": "@id" | ||
30 | } | ||
31 | }, | ||
32 | "meta": { | ||
33 | "type": "https://openminds.ebrains.eu/core/DatasetVersion", | ||
34 | "responseVocab": "https://schema.hbp.eu/myQuery/" | ||
35 | }, | ||
36 | "structure": { | ||
37 | "propertyName": "query:DatasetVersionTitle", | ||
38 | "path": "https://openminds.ebrains.eu/vocab/shortName" | ||
39 | } | ||
40 | } | ||
41 | {{/code}} | ||
42 | |||
43 | |||
44 | ==== Query Builder ==== | ||
45 | |||
46 | The query builder allows you to make, save, and reuse complex queries without any knowledge of graph query languages. | ||
47 | |||
48 | Go to the Query Builder: [[https:~~/~~/query.kg.ebrains.eu>>https://query.kg.ebrains.eu/#state=6a41dd5b-3cf9-4263-8a65-83d41f27677a&session_state=638c33f5-4551-4d70-8b1d-a5c900aa62d0&code=300f25d8-caac-4576-9a21-195ccc93fd31.638c33f5-4551-4d70-8b1d-a5c900aa62d0.4214dba9-b6d0-4331-9837-3e75a10cda14]] | ||
49 | |||
50 | (% class="wikigeneratedid" id="HSelectacategory" %) | ||
51 | **Select a category** | ||
52 | |||
53 | A query for the Knowledge Graph is always executed against a specific data structure (e.g. a "Dataset version"). First select the data structure for your query. | ||
54 | |||
55 | [[image:QB1.png||alt="Step 1: Select a category" height="520" width="700"]] | ||
56 | |||
57 | (% class="wikigeneratedid" id="HSelectanexperimentalapproach" %) | ||
58 | **Select an experimental approach** | ||
59 | |||
60 | Similar to the facetted filters in the Knowledge Graph Search UI, we can refine our query by defining the experimental approach of our interest, for example electrophysiology. | ||
61 | |||
62 | To ensure that all dataset versions with electrophysiology are queried, and none of the dataset versions without electrophysiology, we make this property required and we apply a filter to the label we are searching for (i.e. electrophysiology). | ||
63 | |||
64 | [[image:QBstep2.png||alt="Step 2: Select modality" height="541" width="700"]] | ||
65 | |||
66 | (% class="wikigeneratedid" id="HSelectspecies" %) | ||
67 | **Select species** | ||
68 | |||
69 | Similarly, we can refine our query for mice only. | ||
70 | |||
71 | [[image:QBstep3.png||alt="Step 3: Select species" height="543" width="700"]] | ||
72 | |||
73 | |||
74 | (% class="wikigeneratedid" id="HAccessdetailedmetadata" %) | ||
75 | **Access detailed metadata** | ||
76 | |||
77 | (% class="wikigeneratedid" id="H" %) | ||
78 | Following | ||
79 | |||
80 | ==== [[image:QB4.png||alt="Step 4: Access detailed metadata" height="432" width="700"]] ==== | ||
81 | |||
82 | ==== ==== | ||
83 | |||
84 | The above query in the Query Builder is written below: | ||
85 | |||
86 | {{code language="python"}} | ||
87 | { | ||
88 | "@context": { | ||
89 | "@vocab": "https://core.kg.ebrains.eu/vocab/query/", | ||
90 | "query": "https://schema.hbp.eu/myQuery/", | ||
91 | "propertyName": { | ||
92 | "@id": "propertyName", | ||
93 | "@type": "@id" | ||
94 | }, | ||
95 | "path": { | ||
96 | "@id": "path", | ||
97 | "@type": "@id" | ||
98 | } | ||
99 | }, | ||
100 | "meta": { | ||
101 | "type": "https://openminds.ebrains.eu/core/DatasetVersion", | ||
102 | "responseVocab": "https://schema.hbp.eu/myQuery/" | ||
103 | }, | ||
104 | "structure": [ | ||
105 | { | ||
106 | "propertyName": "query:experimentalApproach", | ||
107 | "path": "https://openminds.ebrains.eu/vocab/experimentalApproach", | ||
108 | "required": true, | ||
109 | "structure": { | ||
110 | "propertyName": "query:name", | ||
111 | "path": "https://openminds.ebrains.eu/vocab/name", | ||
112 | "required": true, | ||
113 | "filter": { | ||
114 | "op": "CONTAINS", | ||
115 | "value": "electrophysiology" | ||
116 | } | ||
117 | } | ||
118 | }, | ||
119 | { | ||
120 | "propertyName": "query:studiedSpecimen", | ||
121 | "path": "https://openminds.ebrains.eu/vocab/studiedSpecimen", | ||
122 | "structure": [ | ||
123 | { | ||
124 | "propertyName": "query:species", | ||
125 | "path": "https://openminds.ebrains.eu/vocab/species", | ||
126 | "required": true, | ||
127 | "structure": [ | ||
128 | { | ||
129 | "propertyName": "query:name", | ||
130 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
131 | }, | ||
132 | { | ||
133 | "propertyName": "query:species", | ||
134 | "path": "https://openminds.ebrains.eu/vocab/species", | ||
135 | "required": true, | ||
136 | "structure": { | ||
137 | "propertyName": "query:name", | ||
138 | "path": "https://openminds.ebrains.eu/vocab/name", | ||
139 | "required": true, | ||
140 | "filter": { | ||
141 | "op": "CONTAINS", | ||
142 | "value": "mus musculus" | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | ] | ||
147 | }, | ||
148 | { | ||
149 | "propertyName": "query:lookupLabel", | ||
150 | "path": "https://openminds.ebrains.eu/vocab/lookupLabel" | ||
151 | }, | ||
152 | { | ||
153 | "propertyName": "query:biologicalSex", | ||
154 | "path": "https://openminds.ebrains.eu/vocab/biologicalSex", | ||
155 | "structure": { | ||
156 | "propertyName": "query:name", | ||
157 | "path": "https://openminds.ebrains.eu/vocab/name", | ||
158 | "required": true, | ||
159 | "filter": { | ||
160 | "op": "CONTAINS", | ||
161 | "value": "male" | ||
162 | } | ||
163 | } | ||
164 | }, | ||
165 | { | ||
166 | "propertyName": "query:studiedState", | ||
167 | "path": "https://openminds.ebrains.eu/vocab/studiedState", | ||
168 | "structure": [ | ||
169 | { | ||
170 | "propertyName": "query:lookupLabel", | ||
171 | "path": "https://openminds.ebrains.eu/vocab/lookupLabel" | ||
172 | }, | ||
173 | { | ||
174 | "propertyName": "query:age", | ||
175 | "path": "https://openminds.ebrains.eu/vocab/age", | ||
176 | "structure": [ | ||
177 | { | ||
178 | "propertyName": "query:value", | ||
179 | "path": "https://openminds.ebrains.eu/vocab/value" | ||
180 | }, | ||
181 | { | ||
182 | "propertyName": "query:unit", | ||
183 | "path": "https://openminds.ebrains.eu/vocab/unit", | ||
184 | "structure": { | ||
185 | "propertyName": "query:name", | ||
186 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
187 | } | ||
188 | } | ||
189 | ] | ||
190 | }, | ||
191 | { | ||
192 | "propertyName": "query:ageCategory", | ||
193 | "path": "https://openminds.ebrains.eu/vocab/ageCategory", | ||
194 | "structure": { | ||
195 | "propertyName": "query:name", | ||
196 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
197 | } | ||
198 | }, | ||
199 | { | ||
200 | "propertyName": "query:attribute", | ||
201 | "path": "https://openminds.ebrains.eu/vocab/attribute", | ||
202 | "structure": { | ||
203 | "propertyName": "query:name", | ||
204 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
205 | } | ||
206 | } | ||
207 | ] | ||
208 | } | ||
209 | ] | ||
210 | } | ||
211 | ] | ||
212 | } | ||
213 | {{/code}} | ||
214 | |||
215 |