@Override public String getId() { return this.term.getId(); }
private List<String> termsToXWikiFormat(List<VocabularyTerm> terms) { List<String> ids = new LinkedList<>(); for (VocabularyTerm term : terms) { ids.add(term.getId()); } return ids; }
@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; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } TermAnnotation other = (TermAnnotation) obj; if (this.mEndPos != other.mEndPos) { return false; } if (this.mStartPos != other.mStartPos) { return false; } if (this.mTerm == null) { if (other.mTerm != null) { return false; } } else if (!this.mTerm.getId().equals(other.mTerm.getId())) { return false; } return true; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof VocabularyTerm)) { return false; } return StringUtils.equals(getId(), ((VocabularyTerm) obj).getId()); }
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; }
/** * 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(); } }
/** * 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; }
/** * 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); }); } }
/** * 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 Set<Disorder> convertDisorders(JSONObject json) { try { if (json.has(ApiConfiguration.JSON_DISORDERS)) { Set<Disorder> disorderSet = new HashSet<>(); JSONArray disorderJson = (JSONArray) json.get(ApiConfiguration.JSON_DISORDERS); for (Object jsonDisorderUncast : disorderJson) { JSONObject jsonDisorder = (JSONObject) jsonDisorderUncast; String id = jsonDisorder.getString(ApiConfiguration.JSON_DISORDER_ID).toUpperCase(); if (!this.disorderTerm.matcher(id).matches()) { LOGGER.error("Patient feature parser: ignoring unsupported term with ID [{}]", id); continue; } // resolve the given disease identifier to a MIM ontology disease ID VocabularyTerm term = MIM_VOCABULARY.getTerm(id); id = (term != null) ? term.getId() : id; Disorder disorder = new RemotePatientDisorder(id, null); disorderSet.add(disorder); } return disorderSet; } } catch (Exception ex) { LOGGER.error("Error converting disorders: {}", ex); } return null; }
/** * 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; }
@Override public long getDistanceTo(final VocabularyTerm other) { if (other == null) { return -1; } if (this.equals(other)) { return 0; } long distance = Long.MAX_VALUE; Map<String, Integer> myLevelMap = new HashMap<>(); myLevelMap.put(getId(), 0); Map<String, Integer> otherLevelMap = new HashMap<>(); otherLevelMap.put(other.getId(), 0); Set<VocabularyTerm> myCrtLevel = new HashSet<>(); myCrtLevel.add(this); Set<VocabularyTerm> otherCrtLevel = new HashSet<>(); otherCrtLevel.add(other); for (int l = 1; l <= distance && (!myCrtLevel.isEmpty() || !otherCrtLevel.isEmpty()); ++l) { distance = Math.min(distance, processAncestorsAtDistance(l, myCrtLevel, myLevelMap, otherLevelMap)); distance = Math.min(distance, processAncestorsAtDistance(l, otherCrtLevel, otherLevelMap, myLevelMap)); } return distance == Long.MAX_VALUE ? -1 : distance; }
@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; }
/** * 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; }
@Override public VocabularyInputTerm setParents(Set<VocabularyTerm> parents) { if (!isNull()) { SolrInputField field = new SolrInputField(PARENTS_KEY); for (VocabularyTerm parent : parents) { field.addValue(parent.getId(), 1.0f); } ((SolrInputDocument) this.doc).put(PARENTS_KEY, field); } return this; }
/** * 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 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; } }
protected String generateSelection(final String[] fieldNames) { String selectionMarker = isSelected(YES) ? "yes-selected" : isSelected(NO) ? "no-selected" : null; String termSourceMarker = ""; if (this.term == null || StringUtils.isEmpty(this.term.getId())) { String customTitle = ""; try { TranslationManager tm = ComponentManagerRegistry.getContextComponentManager().getInstance(TranslationManager.class); customTitle = tm.translate("phenotips.patientSheetCode.termSuggest.nonStandardPhenotype"); } catch (ComponentLookupException ex) { // Will not happen, and if it does, it doesn't matter, the tooltip is not that critical } termSourceMarker = "<span class='fa fa-exclamation-triangle fa-fw' title='" + customTitle + "'> </span>"; } String selectionPrefix = isSelected(NO) ? "NO " : ""; return (selectionMarker != null) ? ("<div class='value-checked " + selectionMarker + "'>" + termSourceMarker + selectionPrefix + XMLUtils.escapeElementContent(this.title) + this.metaData + "</div>") : ""; }
protected String generateSelection(final String[] fieldNames) { String selectionMarker = isSelected(YES) ? "yes-selected" : isSelected(NO) ? "no-selected" : null; String termSourceMarker = ""; if (this.term == null || StringUtils.isEmpty(this.term.getId())) { String customTitle = ""; try { TranslationManager tm = ComponentManagerRegistry.getContextComponentManager().getInstance(TranslationManager.class); customTitle = tm.translate("phenotips.patientSheetCode.termSuggest.nonStandardPhenotype"); } catch (ComponentLookupException ex) { // Will not happen, and if it does, it doesn't matter, the tooltip is not that critical } termSourceMarker = "<span class='fa fa-exclamation-triangle fa-fw' title='" + customTitle + "'> </span>"; } String selectionPrefix = isSelected(NO) ? "NO " : ""; return (selectionMarker != null) ? ("<div class='value-checked " + selectionMarker + "'>" + termSourceMarker + selectionPrefix + XMLUtils.escapeElementContent(this.title) + this.metaData + "</div>") : ""; }