@Test public void lookupTaxonLinksByWDEntry() throws IOException, URISyntaxException { List<Taxon> relatedTaxonIds = WikidataUtil.findRelatedTaxonIds("WD:Q15978631"); final String ids = relatedTaxonIds.stream().map(Taxon::getExternalId).sorted().collect(Collectors.joining("|")); assertThat(ids, is("EOL:327955|GBIF:2436436|INAT_TAXON:43584|IRMNG:10857762|ITIS:180092|MSW:12100795|NBN:NHMSYS0000376773|NCBI:9606|WD:Q15978631")); final String names = relatedTaxonIds.stream().map(Taxon::getName).distinct().collect(Collectors.joining("|")); assertThat(names, is("Homo sapiens")); }
public static List<Taxon> findRelatedTaxonIds(String externalId) throws IOException, URISyntaxException { List<Taxon> relatedIds = new ArrayList<>(); String sparql; final TaxonomyProvider taxonomyProvider = ExternalIdUtil.taxonomyProviderFor(externalId); if (taxonomyProvider != null) { final String taxonId = ExternalIdUtil.stripPrefix(taxonomyProvider, externalId); String whereClause = TaxonomyProvider.WIKIDATA.equals(taxonomyProvider) ? wdTaxonWhereClause(taxonId) : nonWdTaxonWhereClause(taxonomyProvider, taxonId); sparql = "SELECT ?wdTaxonId ?taxonScheme ?taxonId ?wdTaxonName WHERE {\n" + whereClause + " ?taxonSchemeEntity wikibase:directClaim ?taxonScheme .\n" + " ?taxonSchemeEntity wdt:P31 wd:Q42396390 .\n" + " OPTIONAL { ?wdTaxonId wdt:P225 ?wdTaxonName . }\n" + "}"; final String jsonString = executeQuery(sparql); JsonNode jsonNode = new ObjectMapper().readTree(jsonString); if (jsonNode.has("results")) { JsonNode results = jsonNode.get("results"); if (results.has("bindings")) { JsonNode bindings = results.get("bindings"); addWikidataTaxon(relatedIds, bindings); addLinkedTaxa(relatedIds, bindings); } } } return relatedIds; }
WikidataUtil { public static List<Taxon> findRelatedTaxonIds(String externalId) throws IOException, URISyntaxException { List<Taxon> relatedIds = new ArrayList<>(); String sparql; final TaxonomyProvider taxonomyProvider = ExternalIdUtil.taxonomyProviderFor(externalId); if (taxonomyProvider != null) { final String taxonId = ExternalIdUtil.stripPrefix(taxonomyProvider, externalId); String whereClause = TaxonomyProvider.WIKIDATA.equals(taxonomyProvider) ? wdTaxonWhereClause(taxonId) : nonWdTaxonWhereClause(taxonomyProvider, taxonId); sparql = "SELECT ?wdTaxonId ?taxonScheme ?taxonId ?wdTaxonName WHERE {\n" + whereClause + " ?taxonSchemeEntity wikibase:directClaim ?taxonScheme .\n" + " ?taxonSchemeEntity wdt:P31 wd:Q42396390 .\n" + " OPTIONAL { ?wdTaxonId wdt:P225 ?wdTaxonName . }\n" + "}"; final String jsonString = executeQuery(sparql); JsonNode jsonNode = new ObjectMapper().readTree(jsonString); if (jsonNode.has("results")) { JsonNode results = jsonNode.get("results"); if (results.has("bindings")) { JsonNode bindings = results.get("bindings"); addWikidataTaxon(relatedIds, bindings); addLinkedTaxa(relatedIds, bindings); } } } return relatedIds; } }
WikidataUtil { public static List<Taxon> findRelatedTaxonIds(String externalId) throws IOException, URISyntaxException { List<Taxon> relatedIds = new ArrayList<>(); String sparql; final TaxonomyProvider taxonomyProvider = ExternalIdUtil.taxonomyProviderFor(externalId); if (taxonomyProvider != null) { final String taxonId = ExternalIdUtil.stripPrefix(taxonomyProvider, externalId); String whereClause = TaxonomyProvider.WIKIDATA.equals(taxonomyProvider) ? wdTaxonWhereClause(taxonId) : nonWdTaxonWhereClause(taxonomyProvider, taxonId); sparql = "SELECT ?wdTaxonId ?taxonScheme ?taxonId ?wdTaxonName WHERE {\n" + whereClause + " ?taxonSchemeEntity wikibase:directClaim ?taxonScheme .\n" + " ?taxonSchemeEntity wdt:P31 wd:Q42396390 .\n" + " OPTIONAL { ?wdTaxonId wdt:P225 ?wdTaxonName . }\n" + "}"; final String jsonString = executeQuery(sparql); JsonNode jsonNode = new ObjectMapper().readTree(jsonString); if (jsonNode.has("results")) { JsonNode results = jsonNode.get("results"); if (results.has("bindings")) { JsonNode bindings = results.get("bindings"); addWikidataTaxon(relatedIds, bindings); addLinkedTaxa(relatedIds, bindings); } } } return relatedIds; }  }
WikidataUtil { public static List<Taxon> findRelatedTaxonIds(String externalId) throws IOException, URISyntaxException { List<Taxon> relatedIds = new ArrayList<>(); String sparql; final TaxonomyProvider taxonomyProvider = ExternalIdUtil.taxonomyProviderFor(externalId); if (taxonomyProvider != null) { final String taxonId = ExternalIdUtil.stripPrefix(taxonomyProvider, externalId); String whereClause = TaxonomyProvider.WIKIDATA.equals(taxonomyProvider) ? wdTaxonWhereClause(taxonId) : nonWdTaxonWhereClause(taxonomyProvider, taxonId); sparql = "SELECT ?wdTaxonId ?taxonScheme ?taxonId ?wdTaxonName WHERE {\n" + whereClause + " ?taxonSchemeEntity wikibase:directClaim ?taxonScheme .\n" + " ?taxonSchemeEntity wdt:P31 wd:Q42396390 .\n" + " OPTIONAL { ?wdTaxonId wdt:P225 ?wdTaxonName . }\n" + "}"; final String jsonString = executeQuery(sparql); JsonNode jsonNode = new ObjectMapper().readTree(jsonString); if (jsonNode.has("results")) { JsonNode results = jsonNode.get("results"); if (results.has("bindings")) { JsonNode bindings = results.get("bindings"); addWikidataTaxon(relatedIds, bindings); addLinkedTaxa(relatedIds, bindings); } } } return relatedIds; }  static String executeQuery(String sparql); static List<String> findTaxonIdProviders(); static List<Taxon> findRelatedTaxonIds(String externalId); static void addLinkedTaxa(List<Taxon> relatedIds, JsonNode bindings); static String nonWdTaxonWhereClause(TaxonomyProvider taxonomyProvider, String taxonId); static String wdTaxonWhereClause(String taxonId); static String createSparqlQuery(String externalId, String preferredLanguage); static boolean valueExists(JsonNode name); }
WikidataUtil { public static List<Taxon> findRelatedTaxonIds(String externalId) throws IOException, URISyntaxException { List<Taxon> relatedIds = new ArrayList<>(); String sparql; final TaxonomyProvider taxonomyProvider = ExternalIdUtil.taxonomyProviderFor(externalId); if (taxonomyProvider != null) { final String taxonId = ExternalIdUtil.stripPrefix(taxonomyProvider, externalId); String whereClause = TaxonomyProvider.WIKIDATA.equals(taxonomyProvider) ? wdTaxonWhereClause(taxonId) : nonWdTaxonWhereClause(taxonomyProvider, taxonId); sparql = "SELECT ?wdTaxonId ?taxonScheme ?taxonId ?wdTaxonName WHERE {\n" + whereClause + " ?taxonSchemeEntity wikibase:directClaim ?taxonScheme .\n" + " ?taxonSchemeEntity wdt:P31 wd:Q42396390 .\n" + " OPTIONAL { ?wdTaxonId wdt:P225 ?wdTaxonName . }\n" + "}"; final String jsonString = executeQuery(sparql); JsonNode jsonNode = new ObjectMapper().readTree(jsonString); if (jsonNode.has("results")) { JsonNode results = jsonNode.get("results"); if (results.has("bindings")) { JsonNode bindings = results.get("bindings"); addWikidataTaxon(relatedIds, bindings); addLinkedTaxa(relatedIds, bindings); } } } return relatedIds; }  static String executeQuery(String sparql); static List<String> findTaxonIdProviders(); static List<Taxon> findRelatedTaxonIds(String externalId); static void addLinkedTaxa(List<Taxon> relatedIds, JsonNode bindings); static String nonWdTaxonWhereClause(TaxonomyProvider taxonomyProvider, String taxonId); static String wdTaxonWhereClause(String taxonId); static String createSparqlQuery(String externalId, String preferredLanguage); static boolean valueExists(JsonNode name); static final Map<TaxonomyProvider, String> PROVIDER_TO_WIKIDATA; static final Map<String, TaxonomyProvider> WIKIDATA_TO_PROVIDER; }