• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 17302137193

28 Aug 2025 04:35PM UTC coverage: 11.965% (-0.4%) from 12.355%
17302137193

Pull #244

github

web-flow
Merge 4e969b0ee into 3323a35f1
Pull Request #244: Use vocabularies with latest version of `nanopub-java`

331 of 3840 branches covered (8.62%)

Branch coverage included in aggregate %.

943 of 6808 relevant lines covered (13.85%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/nanodash/LookupApis.java
1
package com.knowledgepixels.nanodash;
2

3
import com.beust.jcommander.Strings;
4
import com.github.openjson.JSONArray;
5
import com.github.openjson.JSONObject;
6
import com.knowledgepixels.nanodash.component.QueryParamField;
7
import org.apache.commons.io.IOUtils;
8
import org.apache.http.HttpHeaders;
9
import org.apache.http.HttpResponse;
10
import org.apache.http.NameValuePair;
11
import org.apache.http.client.methods.HttpGet;
12
import org.apache.http.client.methods.HttpPost;
13
import org.apache.http.client.utils.URLEncodedUtils;
14
import org.apache.http.impl.client.CloseableHttpClient;
15
import org.apache.http.impl.client.HttpClientBuilder;
16
import org.nanopub.extra.services.ApiResponse;
17
import org.nanopub.extra.services.ApiResponseEntry;
18

19
import java.io.InputStream;
20
import java.net.URLEncoder;
21
import java.nio.charset.StandardCharsets;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map;
25

26
/**
27
 * Utility class for APIs look up and parsing.
28
 */
29
public class LookupApis {
30

31
    private LookupApis() {
32
    }  // no instances allowed
33

34
    /**
35
     * Parses a JSON response from a grlc API for nanopublications and extracts URIs and labels.
36
     *
37
     * @param grlcJsonObject the JSON object containing the grlc API response
38
     * @param labelMap       a map to store URIs and their corresponding labels
39
     * @param values         a list to store the extracted URIs
40
     */
41
    public static void parseNanopubGrlcApi(JSONObject grlcJsonObject, Map<String, String> labelMap, List<String> values) {
42
        // Aimed to resolve Nanopub grlc API: http://grlc.nanopubs.lod.labs.vu.nl/api/local/local/find_signed_nanopubs_with_text?text=covid
43
        JSONArray resultsArray = grlcJsonObject.getJSONObject("results").getJSONArray("bindings");
×
44
        for (int i = 0; i < resultsArray.length(); i++) {
×
45
            JSONObject resultObject = resultsArray.getJSONObject(i);
×
46
            // Get the nanopub URI
47
            String uri = resultObject.getJSONObject("thing").getString("value");
×
48
            // Get the string which matched with the search term
49
            String label = resultObject.getJSONObject("label").getString("value");
×
50
            values.add(uri);
×
51
            labelMap.put(uri, label);
×
52
        }
53
    }
×
54

55
    /**
56
     * Fetches possible values from an API based on the provided search term.
57
     *
58
     * @param apiString  the API endpoint URL to query
59
     * @param searchterm the search term to use for querying the API
60
     * @param labelMap   a map to store URIs and their corresponding labels
61
     * @param values     a list to store the extracted URIs
62
     */
63
    public static void getPossibleValues(String apiString, String searchterm, Map<String, String> labelMap, List<String> values) {
64
        // TODO This method is a mess and needs some serious clean-up and structuring...
65
        try {
66
            if (apiString.startsWith("https://w3id.org/np/l/nanopub-query-1.1/api/") || apiString.startsWith("http://purl.org/nanopub/api/find_signed_things?")) {
×
67
                String queryName = "find-things";
×
68
                if (apiString.startsWith("https://w3id.org/np/l/nanopub-query-1.1/api/")) {
×
69
                    queryName = apiString.replace("https://w3id.org/np/l/nanopub-query-1.1/api/", "");
×
70
                    if (queryName.contains("?")) queryName = queryName.substring(0, queryName.indexOf("?"));
×
71
                }
72
                Map<String, String> params = new HashMap<>();
×
73
                if (apiString.contains("?")) {
×
74
                    List<NameValuePair> urlParams = URLEncodedUtils.parse(apiString.substring(apiString.indexOf("?") + 1), StandardCharsets.UTF_8);
×
75
                    for (NameValuePair p : urlParams) {
×
76
                        params.put(p.getName(), p.getValue());
×
77
                    }
×
78
                }
79
                String queryId = queryName;
×
80
                if (!queryName.contains("/")) {
×
81
                    queryId = QueryApiAccess.getQueryId(queryName);
×
82
                }
83
                GrlcQuery q = GrlcQuery.get(queryId);
×
84
                if (q.getEndpoint().stringValue().endsWith("/text")) {
×
85
                    searchterm = expandSearchTerm(searchterm);
×
86
                }
87
                String queryParamName = "query";
×
88
                if (q.getPlaceholdersList().size() == 1) {
×
89
                    queryParamName = QueryParamField.getParamName(q.getPlaceholdersList().get(0));
×
90
                }
91
                params.put(queryParamName, searchterm);
×
92
                ApiResponse result = QueryApiAccess.get(queryName, params);
×
93
                int count = 0;
×
94
                for (ApiResponseEntry r : result.getData()) {
×
95
                    String uri = r.get("thing");
×
96
                    values.add(uri);
×
97
                    String desc = r.get("description");
×
98
                    if (desc == null) desc = "";
×
99
                    if (desc.length() > 80) desc = desc.substring(0, 77) + "...";
×
100
                    if (!desc.isEmpty()) desc = " - " + desc;
×
101
                    labelMap.put(uri, r.get("label") + desc);
×
102
                    count++;
×
103
                    if (count > 9) return;
×
104
                }
×
105
                return;
×
106
            }
107

108
            if (apiString.startsWith("https://vodex.")) {
×
109
                searchterm = expandSearchTerm(searchterm);
×
110
            }
111
            String callUrl;
112
            if (apiString.contains(" ")) {
×
113
                callUrl = apiString.replaceAll(" ", URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString()));
×
114
            } else {
115
                callUrl = apiString + URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString());
×
116
            }
117
            HttpGet get = new HttpGet(callUrl);
×
118
            get.setHeader(HttpHeaders.ACCEPT, "application/json");
×
119
            String respString;
120
            try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
×
121
                HttpResponse resp = client.execute(get);
×
122
                if (resp.getStatusLine().getStatusCode() == 405) {
×
123
                    // Method not allowed, trying POST
124
                    HttpPost post = new HttpPost(apiString + URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString()));
×
125
                    resp = client.execute(post);
×
126
                }
127
                // TODO: support other content types (CSV, XML, ...)
128
                // System.err.println(resp.getHeaders("Content-Type")[0]);
129
                try (InputStream in = resp.getEntity().getContent()) {
×
130
                    respString = IOUtils.toString(in, StandardCharsets.UTF_8);
×
131
                }
132
            }
133
            // System.out.println(respString);
134

135
            if (apiString.startsWith("https://w3id.org/np/l/nanopub-query") || apiString.startsWith("https://grlc.") || apiString.contains("/sparql?")) {
×
136
                JSONArray resultsArray = new JSONObject(respString).getJSONObject("results").getJSONArray("bindings");
×
137
                for (int i = 0; i < resultsArray.length(); i++) {
×
138
                    JSONObject resultObject = resultsArray.getJSONObject(i);
×
139
                    // Get the nanopub URI
140
                    String uri = resultObject.getJSONObject("thing").getString("value");
×
141
                    // Get the string which matched with the search term
142
                    String label = resultObject.getJSONObject("label").getString("value");
×
143
                    values.add(uri);
×
144
                    labelMap.put(uri, label);
×
145
                }
146
            } else if (apiString.startsWith("https://www.ebi.ac.uk/ols/api/select")) {
×
147
                // Resolve EBI Ontology Lookup Service
148
                // e.g. https://www.ebi.ac.uk/ols/api/select?q=interacts%20with
149
                // response.docs.[].iri/label
150
                JSONArray responseArray = new JSONObject(respString).getJSONObject("response").getJSONArray("docs");
×
151
                for (int i = 0; i < responseArray.length(); i++) {
×
152
                    String uri = responseArray.getJSONObject(i).getString("iri");
×
153
                    String label = responseArray.getJSONObject(i).getString("label");
×
154
                    try {
155
                        label += " - " + responseArray.getJSONObject(i).getJSONArray("description").getString(0);
×
156
                    } catch (Exception ex) {
×
157
                    }
×
158
                    if (!values.contains(uri)) {
×
159
                        values.add(uri);
×
160
                        labelMap.put(uri, label);
×
161
                    }
162
                }
163
            } else if (apiString.startsWith("https://api.gbif.org/v1/species/suggest")) {
×
164
                JSONArray responseArray = new JSONArray(respString);
×
165
                for (int i = 0; i < responseArray.length(); i++) {
×
166
                    String uri = "https://www.gbif.org/species/" + responseArray.getJSONObject(i).getString("key");
×
167
                    String label = responseArray.getJSONObject(i).getString("scientificName");
×
168
                    if (!values.contains(uri)) {
×
169
                        values.add(uri);
×
170
                        labelMap.put(uri, label);
×
171
                    }
172
                }
173
            } else if (apiString.startsWith("https://api.catalogueoflife.org/dataset/3LR/nameusage/search")) {
×
174
                JSONArray responseArray = new JSONObject(respString).getJSONArray("result");
×
175
                for (int i = 0; i < responseArray.length(); i++) {
×
176
                    String uri = "https://www.catalogueoflife.org/data/taxon/" + responseArray.getJSONObject(i).getString("id");
×
177
                    String label = responseArray.getJSONObject(i).getJSONObject("usage").getString("label");
×
178
                    if (!values.contains(uri)) {
×
179
                        values.add(uri);
×
180
                        labelMap.put(uri, label);
×
181
                    }
182
                }
183
            } else if (apiString.startsWith("https://vodex.")) {
×
184
                // TODO This is just a test and needs to be improved
185
                JSONArray responseArray = new JSONObject(respString).getJSONObject("response").getJSONArray("docs");
×
186
                for (int i = 0; i < responseArray.length(); i++) {
×
187
                    String uri = responseArray.getJSONObject(i).getString("id");
×
188
                    String label = responseArray.getJSONObject(i).getJSONArray("label").get(0).toString();
×
189
                    if (!values.contains(uri)) {
×
190
                        values.add(uri);
×
191
                        labelMap.put(uri, label);
×
192
                    }
193
                }
194
            } else if (apiString.startsWith("https://api.ror.org/organizations")) {
×
195
                // TODO This is just a test and needs to be improved
196
                JSONArray responseArray = new JSONObject(respString).getJSONArray("items");
×
197
                for (int i = 0; i < responseArray.length(); i++) {
×
198
                    String uri = responseArray.getJSONObject(i).getString("id");
×
199
                    String label = responseArray.getJSONObject(i).getString("name");
×
200
                    if (!values.contains(uri)) {
×
201
                        values.add(uri);
×
202
                        labelMap.put(uri, label);
×
203
                    }
204
                }
205
            } else {
×
206
                // TODO: create parseJsonApi() ?
207
                boolean foundId = false;
×
208
                JSONObject json = new JSONObject(respString);
×
209
                for (String key : json.keySet()) {
×
210
                    if (values.size() > 9) break;
×
211
                    if (!(json.get(key) instanceof JSONArray)) continue;
×
212
                    JSONArray a = json.getJSONArray(key);
×
213
                    for (int i = 0; i < a.length(); i++) {
×
214
                        if (values.size() > 9) break;
×
215
                        if (!(a.get(i) instanceof JSONObject)) continue;
×
216
                        JSONObject o = a.getJSONObject(i);
×
217
                        String uri = null;
×
218
                        for (String s : new String[]{"@id", "concepturi", "uri"}) {
×
219
                            if (o.has(s)) {
×
220
                                uri = o.get(s).toString();
×
221
                                foundId = true;
×
222
                                break;
×
223
                            }
224
                        }
225
                        if (uri != null) {
×
226
                            values.add(uri);
×
227
                            String label = "";
×
228
                            for (String s : new String[]{"prefLabel", "label"}) {
×
229
                                if (o.has(s)) {
×
230
                                    label = o.get(s).toString().replaceAll(" - ", " -- ");
×
231
                                    break;
×
232
                                }
233
                            }
234
                            String desc = "";
×
235
                            for (String s : new String[]{"definition", "description"}) {
×
236
                                if (o.has(s)) {
×
237
                                    desc = o.get(s).toString();
×
238
                                    break;
×
239
                                }
240
                            }
241
                            if (!label.isEmpty() && !desc.isEmpty()) desc = " - " + desc;
×
242
                            labelMap.put(uri, label + desc);
×
243
                        }
244
                    }
245
                }
×
246
                if (foundId == false) {
×
247
                    // ID key not found, try to get results for following format
248
                    // {result1: ["label 1", "label 2"], result2: ["label 3", "label 4"]}
249
                    // Aims to resolve https://name-resolution-sri.renci.org/docs#
250

251
                    // TODO: It seems this is triggered too often and adds 'https://identifiers.org/search' when it
252
                    //       shouldn't. Manually filtering these out for now...
253
                    for (String key : json.keySet()) {
×
254
                        if (!(json.get(key) instanceof JSONArray)) continue;
×
255
                        if ("search".equals(key)) continue;
×
256
                        JSONArray labelArray = json.getJSONArray(key);
×
257
                        String uri = key;
×
258
                        String label = "";
×
259
                        String desc = "";
×
260
                        if (labelArray.length() > 0) label = labelArray.getString(0);
×
261
                        if (labelArray.length() > 1) desc = labelArray.getString(1);
×
262
                        if (desc.length() > 80) desc = desc.substring(0, 77) + "...";
×
263
                        if (!label.isEmpty() && !desc.isEmpty()) desc = " - " + desc;
×
264
                        // Quick fix to convert CURIE to URI, as Nanodash only accepts URIs here
265
                        if (!(uri.startsWith("http://") || uri.startsWith("https://"))) {
×
266
                            uri = "https://identifiers.org/" + uri;
×
267
                        }
268
                        values.add(uri);
×
269
                        labelMap.put(uri, label + desc);
×
270
                    }
×
271
                }
272
            }
273
        } catch (Exception ex) {
×
274
            ex.printStackTrace();
×
275
        }
×
276
    }
×
277

278
    private static String expandSearchTerm(String searchTerm) {
279
        String expanded = "";
×
280
        boolean insideQuotes = false;
×
281
        searchTerm = searchTerm.replaceAll("\\s+", " ").trim();
×
282
        for (char c : searchTerm.toCharArray()) {
×
283
            if (c == '\n') {
×
284
                continue;
×
285
            } else if (c == '"') {
×
286
                expanded += '"';
×
287
                insideQuotes = !insideQuotes;
×
288
            } else if (c == ' ') {
×
289
                if (insideQuotes) {
×
290
                    expanded += ' ';
×
291
                } else {
292
                    expanded += '\n';
×
293
                }
294
            } else if (("" + c).matches("\\w") || c == '-' || c == '_') {
×
295
                expanded += c;
×
296
            } else {
297
                if (insideQuotes) {
×
298
                    expanded += ' ';
×
299
                } else {
300
                    expanded += '\n';
×
301
                }
302
            }
303
        }
304
        String extra = "*";
×
305
        expanded = expanded.replaceAll("\\n+", "\n").replaceAll("\"", "\\\\\\\"").trim();
×
306
        if (expanded.endsWith("\"") || insideQuotes) extra = "";
×
307
        return "( " + Strings.join(" AND ", expanded.split("\n")) + extra + " )";
×
308
    }
309

310
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc