Skip to content

Commit

Permalink
support NCBI purl prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpoelen committed Oct 23, 2018
1 parent 49d10a9 commit ac2c999
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> PREFIXES = Arrays.asList(TaxonomyProvider.NCBI.getIdPrefix(),
TaxonomyProvider.NCBITaxon.getIdPrefix(),
"http://purl.obolibrary.org/obo/NCBITaxon_");

@Override
public Map<String, String> enrich(Map<String, String> properties) throws PropertyEnricherException {
// see http://www.ncbi.nlm.nih.gov/books/NBK25500/
Map<String, String> enriched = new HashMap<String, String>(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("<Taxon>")) {
Expand Down Expand Up @@ -80,7 +83,7 @@ private List<String> getPathElems(String fullHierarchy, String elementName, Stri

private void setPropertyToFirstValue(String propertyName, List<String> pathElems, Map<String, String> enriched) {
if (pathElems != null && pathElems.size() > 0) {
enriched.put(propertyName, pathElems.get(pathElems.size()-1));
enriched.put(propertyName, pathElems.get(pathElems.size() - 1));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> props = new HashMap<String, String>() {{
put(PropertyAndValueDictionary.EXTERNAL_ID, "http://purl.obolibrary.org/obo/NCBITaxon_9606");
}};
Map<String, String> 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 {
Expand Down

0 comments on commit ac2c999

Please sign in to comment.