Wiki source code of 2 Application Programming Interface (API)
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | == Programmatic access (API) == |
2 | |||
3 | The EBRAINS Knowledge Graph provides convenient tools and APIs for the implementation of queries into your scripts. | ||
4 | |||
![]() |
8.1 | 5 | To be able to access the Knowledge Graph (KG) programmatically, you only require an EBRAINS account. Register for an account here: [[https:~~/~~/ebrains.eu/register/>>https://ebrains.eu/register/]] and log in. |
![]() |
1.1 | 6 | |
![]() |
8.1 | 7 | === Writing Knowledge Graph queries === |
![]() |
1.1 | 8 | |
![]() |
6.3 | 9 | 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. |
10 | |||
11 | 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/".// | ||
12 | |||
13 | {{code language="python"}} | ||
14 | { | ||
15 | "@context": { | ||
16 | "@vocab": "https://core.kg.ebrains.eu/vocab/query/", | ||
17 | "query": "https://schema.hbp.eu/myQuery/", | ||
18 | "propertyName": { | ||
19 | "@id": "propertyName", | ||
20 | "@type": "@id" | ||
21 | }, | ||
22 | "path": { | ||
23 | "@id": "path", | ||
24 | "@type": "@id" | ||
25 | } | ||
26 | }, | ||
27 | "meta": { | ||
28 | "type": "https://openminds.ebrains.eu/core/DatasetVersion", | ||
29 | "responseVocab": "https://schema.hbp.eu/myQuery/" | ||
30 | }, | ||
31 | "structure": { | ||
32 | "propertyName": "query:DatasetVersionTitle", | ||
33 | "path": "https://openminds.ebrains.eu/vocab/shortName" | ||
34 | } | ||
35 | } | ||
36 | {{/code}} | ||
37 | |||
38 | |||
39 | ==== Query Builder ==== | ||
40 | |||
![]() |
1.1 | 41 | The query builder allows you to make, save, and reuse complex queries without any knowledge of graph query languages. |
42 | |||
43 | 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]] | ||
44 | |||
![]() |
6.3 | 45 | (% class="wikigeneratedid" id="HSelectacategory" %) |
46 | **Select a category** | ||
![]() |
1.1 | 47 | |
48 | 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. | ||
49 | |||
50 | [[image:QB1.png||alt="Step 1: Select a category" height="520" width="700"]] | ||
51 | |||
![]() |
6.3 | 52 | (% class="wikigeneratedid" id="HSelectanexperimentalapproach" %) |
53 | **Select an experimental approach** | ||
![]() |
1.1 | 54 | |
![]() |
6.4 | 55 | Similar to the faceted filters in the Knowledge Graph Search UI, we can refine our query by defining the experimental approach of our interest, for example electrophysiology. |
![]() |
1.1 | 56 | |
57 | 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). | ||
58 | |||
59 | [[image:QBstep2.png||alt="Step 2: Select modality" height="541" width="700"]] | ||
60 | |||
![]() |
6.3 | 61 | (% class="wikigeneratedid" id="HSelectspecies" %) |
62 | **Select species** | ||
![]() |
1.1 | 63 | |
![]() |
10.1 | 64 | We can also refine our query to only include dataset versions that use mice (mus musculus) in a similar way as the faceted filters in the Search UI. |
![]() |
1.1 | 65 | |
66 | [[image:QBstep3.png||alt="Step 3: Select species" height="543" width="700"]] | ||
67 | |||
![]() |
5.2 | 68 | |
![]() |
6.3 | 69 | (% class="wikigeneratedid" id="HAccessdetailedmetadata" %) |
70 | **Access detailed metadata** | ||
![]() |
1.1 | 71 | |
![]() |
5.2 | 72 | (% class="wikigeneratedid" id="H" %) |
![]() |
10.1 | 73 | The Knowledge Graph Search UI has visualised the basic metadata. In many cases, more metadata is stored in the KG than is readily accessible via the Search UI and we can use this to our advantage when refining our query. |
![]() |
1.1 | 74 | |
![]() |
10.1 | 75 | (% class="wikigeneratedid" %) |
76 | For example, we can filter datasets with data from male subjects by making this a required feature in the query. | ||
77 | |||
![]() |
6.2 | 78 | ==== [[image:QB4.png||alt="Step 4: Access detailed metadata" height="432" width="700"]] ==== |
![]() |
1.1 | 79 | |
![]() |
6.3 | 80 | ==== ==== |
![]() |
5.2 | 81 | |
82 | The above query in the Query Builder is written below: | ||
83 | |||
![]() |
1.1 | 84 | {{code language="python"}} |
![]() |
5.2 | 85 | { |
86 | "@context": { | ||
87 | "@vocab": "https://core.kg.ebrains.eu/vocab/query/", | ||
88 | "query": "https://schema.hbp.eu/myQuery/", | ||
89 | "propertyName": { | ||
90 | "@id": "propertyName", | ||
91 | "@type": "@id" | ||
92 | }, | ||
93 | "path": { | ||
94 | "@id": "path", | ||
95 | "@type": "@id" | ||
96 | } | ||
97 | }, | ||
98 | "meta": { | ||
99 | "type": "https://openminds.ebrains.eu/core/DatasetVersion", | ||
100 | "responseVocab": "https://schema.hbp.eu/myQuery/" | ||
101 | }, | ||
102 | "structure": [ | ||
103 | { | ||
104 | "propertyName": "query:experimentalApproach", | ||
105 | "path": "https://openminds.ebrains.eu/vocab/experimentalApproach", | ||
106 | "required": true, | ||
107 | "structure": { | ||
108 | "propertyName": "query:name", | ||
109 | "path": "https://openminds.ebrains.eu/vocab/name", | ||
110 | "required": true, | ||
111 | "filter": { | ||
112 | "op": "CONTAINS", | ||
113 | "value": "electrophysiology" | ||
114 | } | ||
115 | } | ||
116 | }, | ||
117 | { | ||
118 | "propertyName": "query:studiedSpecimen", | ||
119 | "path": "https://openminds.ebrains.eu/vocab/studiedSpecimen", | ||
120 | "structure": [ | ||
121 | { | ||
122 | "propertyName": "query:species", | ||
123 | "path": "https://openminds.ebrains.eu/vocab/species", | ||
124 | "required": true, | ||
125 | "structure": [ | ||
126 | { | ||
127 | "propertyName": "query:name", | ||
128 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
129 | }, | ||
130 | { | ||
131 | "propertyName": "query:species", | ||
132 | "path": "https://openminds.ebrains.eu/vocab/species", | ||
133 | "required": true, | ||
134 | "structure": { | ||
135 | "propertyName": "query:name", | ||
136 | "path": "https://openminds.ebrains.eu/vocab/name", | ||
137 | "required": true, | ||
138 | "filter": { | ||
139 | "op": "CONTAINS", | ||
140 | "value": "mus musculus" | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | ] | ||
145 | }, | ||
146 | { | ||
147 | "propertyName": "query:lookupLabel", | ||
148 | "path": "https://openminds.ebrains.eu/vocab/lookupLabel" | ||
149 | }, | ||
150 | { | ||
151 | "propertyName": "query:biologicalSex", | ||
152 | "path": "https://openminds.ebrains.eu/vocab/biologicalSex", | ||
153 | "structure": { | ||
154 | "propertyName": "query:name", | ||
155 | "path": "https://openminds.ebrains.eu/vocab/name", | ||
156 | "required": true, | ||
157 | "filter": { | ||
158 | "op": "CONTAINS", | ||
159 | "value": "male" | ||
160 | } | ||
161 | } | ||
162 | }, | ||
163 | { | ||
164 | "propertyName": "query:studiedState", | ||
165 | "path": "https://openminds.ebrains.eu/vocab/studiedState", | ||
166 | "structure": [ | ||
167 | { | ||
168 | "propertyName": "query:lookupLabel", | ||
169 | "path": "https://openminds.ebrains.eu/vocab/lookupLabel" | ||
170 | }, | ||
171 | { | ||
172 | "propertyName": "query:age", | ||
173 | "path": "https://openminds.ebrains.eu/vocab/age", | ||
174 | "structure": [ | ||
175 | { | ||
176 | "propertyName": "query:value", | ||
177 | "path": "https://openminds.ebrains.eu/vocab/value" | ||
178 | }, | ||
179 | { | ||
180 | "propertyName": "query:unit", | ||
181 | "path": "https://openminds.ebrains.eu/vocab/unit", | ||
182 | "structure": { | ||
183 | "propertyName": "query:name", | ||
184 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
185 | } | ||
186 | } | ||
187 | ] | ||
188 | }, | ||
189 | { | ||
190 | "propertyName": "query:ageCategory", | ||
191 | "path": "https://openminds.ebrains.eu/vocab/ageCategory", | ||
192 | "structure": { | ||
193 | "propertyName": "query:name", | ||
194 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
195 | } | ||
196 | }, | ||
197 | { | ||
198 | "propertyName": "query:attribute", | ||
199 | "path": "https://openminds.ebrains.eu/vocab/attribute", | ||
200 | "structure": { | ||
201 | "propertyName": "query:name", | ||
202 | "path": "https://openminds.ebrains.eu/vocab/name" | ||
203 | } | ||
204 | } | ||
205 | ] | ||
206 | } | ||
207 | ] | ||
208 | } | ||
209 | ] | ||
210 | } | ||
![]() |
1.1 | 211 | {{/code}} |
212 | |||
213 |