/** * Creates an empty sorted set, where {@link VocabularyTerm} objects are sorted by the natural order of their name. * * @return an empty sorted set */ private Set<VocabularyTerm> newSortedSet() { return new TreeSet<>((o1, o2) -> { final String name1 = o1.getName() != null ? o1.getName() : o1.getId(); final String name2 = o2.getName() != null ? o2.getName() : o2.getId(); return name1.compareTo(name2); }); } }
/** * Returns a list of {@link VocabularyTerm genes}, given an {@link VocabularyTerm HPO term}. * * @param term an HPO {@link VocabularyTerm} * @return a list of {@link VocabularyTerm genes} associated with the provided {@code term}, or an empty list */ private List<String> getGeneDataFromTerm(@Nonnull final VocabularyTerm term) { @SuppressWarnings("unchecked") final List<String> geneList = (List<String>) term.get(ASSOCIATED_GENES); return CollectionUtils.isNotEmpty(geneList) ? geneList : Collections.emptyList(); }
/** * Get the HPO IDs of the specified phenotype and all its ancestors. * * @param id the HPO identifier to search for, in the {@code HP:1234567} format * @return the full set of ancestors-or-self IDs, or an empty set if the requested ID was not found in the index */ public Set<String> getAllAncestorsAndSelfIDs(final String id) { Set<String> parents = new HashSet<>(); VocabularyTerm crt = this.hpo.getTerm(id); Set<VocabularyTerm> ancenstors = crt.getAncestorsAndSelf(); for (VocabularyTerm term : ancenstors) { parents.add(term.getId()); } return parents; } }
private long processAncestorsAtDistance(int localDistance, Set<VocabularyTerm> sourceUnprocessedAncestors, Map<String, Integer> sourceDistanceMap, Map<String, Integer> targetDistanceMap) { long minDistance = Long.MAX_VALUE; Set<VocabularyTerm> nextLevel = new HashSet<>(); for (VocabularyTerm term : sourceUnprocessedAncestors) { for (VocabularyTerm parent : term.getParents()) { if (sourceDistanceMap.containsKey(parent.getId())) { continue; } if (targetDistanceMap.containsKey(parent.getId())) { minDistance = Math.min(minDistance, targetDistanceMap.get(parent.getId()) + localDistance); } nextLevel.add(parent); sourceDistanceMap.put(parent.getId(), localDistance); } } sourceUnprocessedAncestors.clear(); sourceUnprocessedAncestors.addAll(nextLevel); return minDistance; }
@Override public String getId() { return this.term.getId(); }
@Override public VocabularyTermSummary createVocabularyTermRepresentation(VocabularyTerm term) { VocabularyTermSummary rep = new VocabularyTermSummary(); rep.withId(term.getId()); rep.withName(term.getName()); JSONObject jsonObject = term.toJSON(); String symbolKey = "symbol"; if (jsonObject != null && jsonObject.opt(symbolKey) != null) { rep.withSymbol(jsonObject.get(symbolKey).toString()); } rep.withDescription(term.getDescription()); return rep; } }
/** * Builds a {@link JSONArray} of JSON representations of term {@link VocabularyTerm} objects. * * @return a {@link JSONArray} of term {@link VocabularyTerm} objects */ private JSONArray buildPhenotypesJSON() { final JSONArray json = new JSONArray(); for (final VocabularyTerm term : getTerms()) { final JSONObject termSummary = new JSONObject(); termSummary.put(ID, term.getId()); termSummary.putOpt(NAME, term.getName()); termSummary.putOpt(NAME_TRANSLATED, term.getTranslatedName()); json.put(termSummary); } return json; }
/** * Returns a set with {@code termId} and the IDs of its ancestor terms. * * @param termId the term ID * @return a set with {@code termId} and the IDs of all its ancestor terms */ private Collection<String> getSelfAndAncestorTermIds(@Nonnull final String termId) { // The collection that will contain termId and the IDs of its ancestors. final Collection<String> ancestors = new HashSet<>(); ancestors.add(termId); // Find the term in the vocabulary, and if it exists, retrieve its ancestors, if any. final VocabularyTerm vocabularyTerm = this.vocabularyManager.resolveTerm(termId); if (vocabularyTerm != null) { for (VocabularyTerm ancestor : vocabularyTerm.getAncestors()) { ancestors.add(ancestor.getId()); } } else { this.logger.warn("Could not find term with ID: {} in indexed vocabularies.", termId); } return ancestors; }
protected String generateFormField(String[] fieldNames) { if (fieldNames[NO] != null) { return String.format( "<div class='%s%s'><span class='yes-no-picker'>%s%s%s</span> <span title='%s'>%s</span></div>", DEFAULT_CSS_CLASS, this.expandable ? EXPANDABLE_CSS_CLASS : "", generateCheckbox("none", this.value, "", (!isSelected(YES) && !isSelected(NO)), "na", "NA"), generateCheckbox(fieldNames[YES], this.value, this.hint, isSelected(YES), "yes", "Y"), generateCheckbox(fieldNames[NO], this.value, this.hint, isSelected(NO), "no", "N"), this.term == null ? this.title + "\n(custom term)" : (this.term.getName() + (StringUtils.isNotBlank(this.term.getDescription()) ? "\n" + StringEscapeUtils.escapeXml10(this.term.getDescription()) : "")), generateLabel(fieldNames[YES] + '_' + this.value, "yes-no-picker-label", this.title)); } else { return generateCheckbox(fieldNames[YES], this.value, this.hint, isSelected(YES), DEFAULT_CSS_CLASS + (this.expandable ? EXPANDABLE_CSS_CLASS : ""), this.title); } }
@Override public String getName() { if (this.name != null) { return this.name; } try { VocabularyManager vm = ComponentManagerRegistry.getContextComponentManager().getInstance(VocabularyManager.class); VocabularyTerm term = vm.resolveTerm(this.id); if (term != null && StringUtils.isNotEmpty(term.getName())) { this.name = term.getName(); return this.name; } } catch (ComponentLookupException ex) { // Shouldn't happen } return this.id; }
@Override public String getName() { return this.term.getTranslatedName(); }
private List<String> termsToXWikiFormat(List<VocabularyTerm> terms) { List<String> ids = new LinkedList<>(); for (VocabularyTerm term : terms) { ids.add(term.getId()); } return ids; }
@Override public JSONObject toJSON() { JSONObject json = new JSONObject(); for (Map.Entry<String, ? extends Object> field : getEntrySet()) { addAsCorrectType(json, field.getKey(), field.getValue()); } json.put(TRANSLATED_NAME_KEY, getTranslatedName()); json.put(TRANSLATED_DESCRIPTION_KEY, getTranslatedDescription()); if (this.parents != null && !this.parents.isEmpty()) { JSONArray parentsJson = new JSONArray(); for (VocabularyTerm parent : this.parents) { JSONObject parentJSON = new JSONObject(); parentJSON.put(ID_KEY, parent.getId()); parentJSON.put(NAME_KEY, parent.getName()); parentJSON.put(TRANSLATED_NAME_KEY, parent.getTranslatedName()); parentsJson.put(parentJSON); } json.put("parents", parentsJson); } return json; }
/** * 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); }
/** * Returns a set with {@code termId} and the IDs of its ancestor terms. * * @param termId the term ID * @return a set with {@code termId} and the IDs of all its ancestor terms */ private Collection<String> getSelfAndAncestorTermIds(@Nonnull final String termId) { // The collection that will contain termId and the IDs of its ancestors. final Collection<String> ancestors = new HashSet<>(); ancestors.add(termId); // Find the term in the vocabulary, and if it exists, retrieve its ancestors, if any. final VocabularyTerm vocabularyTerm = this.vocabularyManager.resolveTerm(termId); if (vocabularyTerm != null) { for (VocabularyTerm ancestor : vocabularyTerm.getAncestors()) { ancestors.add(ancestor.getId()); } } else { this.logger.warn("Could not find term with ID: {} in indexed vocabularies.", termId); } return ancestors; }
protected String generateFormField(String[] fieldNames) { if (fieldNames[NO] != null) { return String.format( "<div class='%s%s'><span class='yes-no-picker'>%s%s%s</span> <span title='%s'>%s</span></div>", DEFAULT_CSS_CLASS, this.expandable ? EXPANDABLE_CSS_CLASS : "", generateCheckbox("none", this.value, "", (!isSelected(YES) && !isSelected(NO)), "na", "NA"), generateCheckbox(fieldNames[YES], this.value, this.hint, isSelected(YES), "yes", "Y"), generateCheckbox(fieldNames[NO], this.value, this.hint, isSelected(NO), "no", "N"), this.term == null ? this.title + "\n(custom term)" : (this.term.getName() + (StringUtils.isNotBlank(this.term.getDescription()) ? "\n" + StringEscapeUtils.escapeXml10(this.term.getDescription()) : "")), generateLabel(fieldNames[YES] + '_' + this.value, "yes-no-picker-label", this.title)); } else { return generateCheckbox(fieldNames[YES], this.value, this.hint, isSelected(YES), DEFAULT_CSS_CLASS + (this.expandable ? EXPANDABLE_CSS_CLASS : ""), this.title); } }
@Override public String getName() { if (this.name != null) { return this.name; } try { VocabularyManager vm = ComponentManagerRegistry.getContextComponentManager().getInstance(VocabularyManager.class); VocabularyTerm term = vm.resolveTerm(this.id); if (term != null && StringUtils.isNotEmpty(term.getName())) { this.name = term.getName(); return this.name; } } catch (ComponentLookupException ex) { // Shouldn't happen } return this.id; }
private String getLabelFromOntology(String id) { if (!id.startsWith("HP:")) { return id; } VocabularyTerm phObj = this.ontologyService.getTerm(id); if (phObj != null) { return phObj.getTranslatedName(); } return id; }
/** * Returns the {@link VocabularyTerm#getName() term name} if it is specified, otherwise returns * {@link VocabularyTerm#getId()}. * * @param term the {@link VocabularyTerm} of interest * @return the name of the term if specified, the ID otherwise */ private String getTermName(@Nonnull final VocabularyTerm term) { final String name = term.getName(); return StringUtils.isNotBlank(name) ? name : term.getId(); } }
@Override public boolean contains(Object o) { if (String.class.isInstance(o)) { return this.identifiers.contains(o); } else if (VocabularyTerm.class.isInstance(o)) { return this.identifiers.contains(((VocabularyTerm) o).getId()); } return false; }