diff --git a/nomer-taxon-resolver/src/main/java/org/eol/globi/taxon/NCBIService.java b/nomer-taxon-resolver/src/main/java/org/eol/globi/taxon/NCBIService.java index 4b5df85e..9f1f890b 100644 --- a/nomer-taxon-resolver/src/main/java/org/eol/globi/taxon/NCBIService.java +++ b/nomer-taxon-resolver/src/main/java/org/eol/globi/taxon/NCBIService.java @@ -20,24 +20,27 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Predicate; @PropertyEnricherInfo(name = "ncbi-taxon-id", description = "Lookup NCBI taxon by id with NCBI:* prefix.") public class NCBIService implements PropertyEnricher { + private static final List PREFIXES = Arrays.asList(TaxonomyProvider.NCBI.getIdPrefix(), + TaxonomyProvider.NCBITaxon.getIdPrefix(), + "http://purl.obolibrary.org/obo/NCBITaxon_"); + @Override public Map enrich(Map properties) throws PropertyEnricherException { // see http://www.ncbi.nlm.nih.gov/books/NBK25500/ Map enriched = new HashMap(properties); String externalId = properties.get(PropertyAndValueDictionary.EXTERNAL_ID); - String prefixAlt = TaxonomyProvider.NCBITaxon.getIdPrefix(); - if (StringUtils.startsWith(externalId, TaxonomyProvider.NCBI.getIdPrefix()) - || StringUtils.startsWith(externalId, prefixAlt)) { - String tsn = externalId - .replace(prefixAlt, "") - .replace(TaxonomyProvider.ID_PREFIX_NCBI, ""); + + if (PREFIXES.stream().anyMatch(x -> StringUtils.startsWith(externalId, x))) { + String tsn = PREFIXES.stream().reduce(externalId, (x, y) -> StringUtils.replace(x, y, "")); if (tsn.matches("\\d+")) { String fullHierarchy = getResponse("db=taxonomy&id=" + tsn); if (fullHierarchy.contains("")) { @@ -80,7 +83,7 @@ private List getPathElems(String fullHierarchy, String elementName, Stri private void setPropertyToFirstValue(String propertyName, List pathElems, Map enriched) { if (pathElems != null && pathElems.size() > 0) { - enriched.put(propertyName, pathElems.get(pathElems.size()-1)); + enriched.put(propertyName, pathElems.get(pathElems.size() - 1)); } } diff --git a/nomer-taxon-resolver/src/test/java/org/eol/globi/taxon/NCBIServiceTest.java b/nomer-taxon-resolver/src/test/java/org/eol/globi/taxon/NCBIServiceTest.java index b6319047..2dec6cc3 100644 --- a/nomer-taxon-resolver/src/test/java/org/eol/globi/taxon/NCBIServiceTest.java +++ b/nomer-taxon-resolver/src/test/java/org/eol/globi/taxon/NCBIServiceTest.java @@ -48,6 +48,17 @@ public void lookupPathByTaxonIdNCBITaxon() throws PropertyEnricherException { assertThat(enrich.get(PropertyAndValueDictionary.EXTERNAL_ID), is("NCBI:9606")); } + @Test + public void lookupPathByTaxonIdNCBIPurl() throws PropertyEnricherException { + PropertyEnricher enricher = new NCBIService(); + HashMap props = new HashMap() {{ + put(PropertyAndValueDictionary.EXTERNAL_ID, "http://purl.obolibrary.org/obo/NCBITaxon_9606"); + }}; + Map enrich = enricher.enrich(props); + assertThat(enrich.get(PropertyAndValueDictionary.NAME), is("Homo sapiens")); + assertThat(enrich.get(PropertyAndValueDictionary.EXTERNAL_ID), is("NCBI:9606")); + } + @Test public void parseInconsistentWithAltNCBI191217() throws PropertyEnricherException {