/** * Gets the {@link VocabularyTerm} associated with the provided {@code categoryId}. If {@code categoryId} refers to * a non-standard category, will set "phenotypic abnormality" ("HP:0000118") as the default. * * @param categoryId the ID of a category of interest * @return a {@link VocabularyTerm} associated with the provided {@code categoryId} or {@link VocabularyTerm} for * "HP:0000118" */ private VocabularyTerm getCategoryTerm(final String categoryId) { final VocabularyTerm categoryTerm = this.ontologyService.getTerm(categoryId); return categoryTerm != null ? categoryTerm : this.ontologyService.getTerm("HP:0000118"); }
@Override public Cache<VocabularyTerm> getTermCache(Vocabulary vocabulary) { if (!this.caches.containsKey(vocabulary.getIdentifier())) { try { initialize(vocabulary); } catch (InitializationException ex) { return null; } } return this.caches.get(vocabulary.getIdentifier()); }
/** * Suggest the terms that best match the user's input, in a specific sub-category of this vocabulary. * * @param input the text that the user entered * @param category the category of terms to search in, one of the {@link #getSupportedCategories() supported * categories} * @param maxResults the maximum number of terms to be returned * @param sort an optional sort parameter, in a format that depends on the actual engine that stores the vocabulary; * usually a property name followed by {@code asc} or {@code desc}; may be {@code null} * @param customFilter an optional custom filter query to further restrict which terms may be returned, in a format * that depends on the actual engine that stores the vocabulary; some vocabularies may not support a * filter query; may be {@code null} * @return a list of suggestions, possibly empty. * @since 1.4 */ default List<VocabularyTerm> search(String input, String category, int maxResults, String sort, String customFilter) { return search(input, maxResults, sort, customFilter); }
/** * How much information is captured by a feature? In other words, how many diseases are selected by a feature out of * the total selectable diseases. If a feature doesn't select any diseases at all, the information content of its * nearest represented ancestor is considered, with a slight boost for even more specificity. * * @param f the target feature to measure * @return the information content captured by this term */ private double informationContent(Feature f) { String toSearch = f.getId(); double ic = informationContent(this.omim.count(Collections.singletonMap(SEARCH_FOR, toSearch))); int i = 0; while (ic == 0 && ++i < 5) { VocabularyTerm term = this.hpo.getTerm(toSearch); if (term == null) { break; } Set<VocabularyTerm> parents = term.getParents(); if (parents.isEmpty()) { break; } toSearch = parents.iterator().next().getId(); ic = informationContent(this.omim.count(Collections.singletonMap(SEARCH_FOR, toSearch))); } return ic * (1 + i / 5); }
@Override public org.phenotips.vocabularies.rest.model.Vocabulary createVocabularyRepresentation(Vocabulary vocabulary) { org.phenotips.vocabularies.rest.model.Vocabulary result = new org.phenotips.vocabularies.rest.model.Vocabulary(); result .withIdentifier(vocabulary.getIdentifier()) .withName(vocabulary.getName()) .withAliases(vocabulary.getAliases()) .withSize(vocabulary.size()) .withVersion(vocabulary.getVersion()); try { result.withDefaultSourceLocation(vocabulary.getDefaultSourceLocation()); } catch (UnsupportedOperationException e) { // Don't do anything and leave source empty } return result; }
@Override public void initialize() throws InitializationException { this.totalTerms = this.omim.count(Collections.singletonMap(SEARCH_FOR, "HP:0000001")); }
/** * Constructs a map of vocabularies, where each vocabulary is also listed under each of its aliases. * * @return a map where the key is a vocabulary name or alias, and the value is the {@link Vocabulary} with that name * or alias */ private Map<String, Vocabulary> constructVocabulariesByAlias() { Map<String, Vocabulary> aliasVocabularies = new HashMap<>(); for (Vocabulary vocabulary : this.vocabularies.get().values()) { for (String alias : vocabulary.getAliases()) { aliasVocabularies.put(alias, vocabulary); } } return aliasVocabularies; }
/** * How much information is captured by a feature? In other words, how many diseases are selected by a feature out of * the total selectable diseases. If a feature doesn't select any diseases at all, the information content of its * nearest represented ancestor is considered, with a slight boost for even more specificity. * * @param f the target feature to measure * @return the information content captured by this term */ private double informationContent(Feature f) { String toSearch = f.getId(); double ic = informationContent(this.omim.count(Collections.singletonMap(SEARCH_FOR, toSearch))); int i = 0; while (ic == 0 && ++i < 5) { VocabularyTerm term = this.hpo.getTerm(toSearch); if (term == null) { break; } Set<VocabularyTerm> parents = term.getParents(); if (parents.isEmpty()) { break; } toSearch = parents.iterator().next().getId(); ic = informationContent(this.omim.count(Collections.singletonMap(SEARCH_FOR, toSearch))); } return ic * (1 + i / 5); }
@Override public org.phenotips.vocabularies.rest.model.Vocabulary createVocabularyRepresentation(Vocabulary vocabulary) { org.phenotips.vocabularies.rest.model.Vocabulary result = new org.phenotips.vocabularies.rest.model.Vocabulary(); result .withIdentifier(vocabulary.getIdentifier()) .withName(vocabulary.getName()) .withAliases(vocabulary.getAliases()) .withSize(vocabulary.size()) .withVersion(vocabulary.getVersion()); try { result.withDefaultSourceLocation(vocabulary.getDefaultSourceLocation()); } catch (UnsupportedOperationException e) { // Don't do anything and leave source empty } return result; }
@Override public void initialize() throws InitializationException { this.totalTerms = this.omim.count(Collections.singletonMap(SEARCH_FOR, "HP:0000001")); }
/** * Gets the {@link VocabularyTerm} associated with the provided {@code categoryId}. If {@code categoryId} refers to * a non-standard category, will set "phenotypic abnormality" ("HP:0000118") as the default. * * @param categoryId the ID of a category of interest * @return a {@link VocabularyTerm} associated with the provided {@code categoryId} or {@link VocabularyTerm} for * "HP:0000001" */ private VocabularyTerm getCategoryTerm(final String categoryId) { final VocabularyTerm categoryTerm = this.ontologyService.getTerm(categoryId); return categoryTerm != null ? categoryTerm : this.ontologyService.getTerm(ROOT_ID); }
@Override public SolrClient getSolrConnection(Vocabulary vocabulary) { if (!this.cores.containsKey(vocabulary.getIdentifier())) { try { initialize(vocabulary); } catch (InitializationException ex) { return null; } } return this.cores.get(vocabulary.getIdentifier()); }
/** * Performs a search for {@code input query string} using the provided set of {@code categorizedVocabularies}, and * returns the specified {@code maxResults number of results}, sorted by score (in descending order). * * @param input the input query string * @param maxResults the maximum number of results to return * @param category the vocabulary category * @param categorizedVocabularies the {@link Vocabulary} objects to search {@code input} in * @return an list of {@link VocabularyTerm} objects to search in */ private List<VocabularyTerm> search(@Nonnull final String input, final int maxResults, @Nonnull final String category, @Nonnull final Set<Vocabulary> categorizedVocabularies) { final List<VocabularyTerm> results = new ArrayList<>(); for (final Vocabulary vocabulary : categorizedVocabularies) { results.addAll(vocabulary.search(input, category, maxResults, null, null)); } sortTermsByScore(results); final int resultsSize = results.size(); final int rows = maxResults <= resultsSize ? maxResults : resultsSize; return results.subList(0, rows); }
private boolean hasDescendantsInOntology(String id) { if (!id.startsWith("HP:")) { return false; } Map<String, String> params = new HashMap<>(); params.put(INDEXED_PARENT_KEY, id); return (this.ontologyService.count(params) > 0); }
private VocabularyTerm getTerm(String gene) { // lazy-initialize HGNC if (this.hgnc == null) { this.hgnc = getHGNCVocabulary(); if (this.hgnc == null) { return null; } } return this.hgnc.getTerm(gene); }
@Override public boolean isVocabularySupported(Vocabulary vocabulary) { return getTargetVocabularyId().equals(vocabulary.getIdentifier()); }
@Override public Response suggest(String vocabularyId, String input, @DefaultValue("10") int maxResults, String sort, String customFilter) { if (StringUtils.isEmpty(input) || StringUtils.isEmpty(vocabularyId)) { throw new WebApplicationException(Response.Status.BAD_REQUEST); } Vocabulary vocabulary = this.vm.getVocabulary(vocabularyId); if (vocabulary == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } List<VocabularyTerm> termSuggestions = vocabulary.search(input, maxResults, sort, customFilter); JSONObject rep = new JSONObject(); JSONArray trms = new JSONArray(); for (VocabularyTerm term : termSuggestions) { JSONObject trm = term.toJSON(); trm.put("links", this.autolinker.get().forSecondaryResource(VocabularyTermResource.class, this.uriInfo) .build()); trms.put(trm); } rep.put("rows", trms); rep.put("links", this.autolinker.get().forResource(getClass(), this.uriInfo).build()); return Response.ok(rep, MediaType.APPLICATION_JSON_TYPE).build(); } }
private boolean hasDescendantsInOntology(String id) { if (!id.startsWith("HP:")) { return false; } Map<String, String> params = new HashMap<>(); params.put(INDEXED_PARENT_KEY, id); return (this.ontologyService.count(params) > 0); }