/** * Retrieve a term from its owner vocabulary. For this to work properly, the term identifier must contain a known * vocabulary prefix. * * @param termId the term identifier, in the format {@code <vocabulary prefix>:<term id>}, for example * {@code HP:0002066} * @return the requested term, or {@code null} if the term doesn't exist in the vocabulary, or no matching * vocabulary is available */ public VocabularyTerm resolveTerm(String termId) { return this.manager.resolveTerm(termId); }
private void addTerms(String item, List<VocabularyTerm> holder) { if (StringUtils.isNotBlank(item)) { VocabularyTerm term = this.vocabularyManager.resolveTerm(item); if (term != null) { holder.add(term); } } } }
/** * Retrieve a term from its owner vocabulary. For this to work properly, the term identifier must contain a known * vocabulary prefix. * * @param termId the term identifier, in the format {@code <vocabulary prefix>:<term id>}, for example * {@code HP:0002066} * @return the requested term, or {@code null} if the term doesn't exist in the vocabulary, or no matching * vocabulary is available * @deprecated use {@link VocabularyScriptService#resolveTerm(String) $services.vocabularies.resolveTerm} instead */ @Deprecated public VocabularyTerm resolveTerm(String termId) { return this.manager.resolveTerm(termId); }
private void addTerms(String item, List<VocabularyTerm> holder) { if (StringUtils.isNotBlank(item)) { VocabularyTerm term = this.vocabularyManager.resolveTerm(item); if (term != null) { holder.add(term); } } } }
/** * Builds a set of {@link VocabularyTerm} objects from a collection of term ID strings. * * @param termIds a collection of term IDs as strings; all IDs must be prefixed with the vocabulary identifier * @return a set of {@link VocabularyTerm} objects corresponding with the provided term IDs */ private Set<VocabularyTerm> buildTermsFromIDs(@Nonnull final Collection<String> termIds) { final Set<VocabularyTerm> terms = new HashSet<>(); for (final String termId : termIds) { if (StringUtils.isBlank(termId)) { continue; } final VocabularyTerm term = this.vocabularyManager.resolveTerm(termId); if (term == null) { this.logger.warn("A term with id: {} could not be found in existing vocabularies, and will be ignored.", termId); continue; } terms.add(term); } return Collections.unmodifiableSet(terms); }
@Override public Response resolveTerm(String termId) { VocabularyTerm term = this.vm.resolveTerm(termId); if (term == null) { return Response.status(Response.Status.NOT_FOUND).build(); } JSONObject rep = term.toJSON(); rep.put("links", this.autolinker.get().forResource(getClass(), this.uriInfo) .withExtraParameters("vocabulary-id", term.getVocabulary().getIdentifier()).build()); return Response.ok(rep, MediaType.APPLICATION_JSON_TYPE).build(); } }
/** * 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; }
/** * 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; }
/** * Converts and adds {@code features} to sets of present {@code retrievedPresentTerms terms} and absent * {@code retrievedAbsentTerms terms}. * * @param features a collection of {@link Feature} objects * @param retrievedPresentTerms a set of present {@link VocabularyTerm} objects * @param retrievedAbsentTerms a set of absent {@link VocabularyTerm} objects */ private void addTermsFromFeatures( @Nonnull final Collection<? extends Feature> features, @Nonnull final Set<VocabularyTerm> retrievedPresentTerms, @Nonnull final Set<VocabularyTerm> retrievedAbsentTerms) { for (final Feature feature : features) { final VocabularyTerm term = this.vocabularyManager.resolveTerm(feature.getValue()); if (term != null) { if (feature.isPresent()) { retrievedPresentTerms.add(term); } else { retrievedAbsentTerms.add(term); } } } } }
@Override public Response resolveTerm(String termId) { VocabularyTerm term = this.vm.resolveTerm(termId); if (term == null) { return Response.status(Response.Status.NOT_FOUND).build(); } JSONObject rep = term.toJSON(); rep.put("links", this.autolinker.get().forResource(getClass(), this.uriInfo) .withExtraParameters("vocabulary-id", term.getVocabulary().getIdentifier()).build()); return Response.ok(rep, MediaType.APPLICATION_JSON_TYPE).build(); } }
@Override public PatientData<List<VocabularyTerm>> readJSON(JSONObject json) { try { Map<String, List<VocabularyTerm>> result = new HashMap<>(); for (String property : this.getProperties()) { JSONArray elements = json.optJSONArray(property); if (elements != null) { List<VocabularyTerm> propertyTerms = new LinkedList<>(); Iterator<Object> elementsIterator = elements.iterator(); while (elementsIterator.hasNext()) { JSONObject element = (JSONObject) elementsIterator.next(); String termId = element.optString(ID_NAME); if (termId != null) { VocabularyTerm term = this.vocabularyManager.resolveTerm(termId); propertyTerms.add(term); } } result.put(property, propertyTerms); } else { result.put(property, null); } } return new DictionaryPatientData<>(DATA_NAME, result); } catch (Exception ex) { // must be in a wrong format } return null; }
FormField(String value, String title, String hint, String metaData, boolean expandable, boolean yesSelected, boolean noSelected) { super(title); this.value = value; this.hint = hint; this.metaData = metaData; this.expandable = expandable; this.selection = new boolean[2]; this.selection[YES] = yesSelected; this.selection[NO] = noSelected; try { VocabularyManager vm = ComponentManagerRegistry.getContextComponentManager().getInstance(VocabularyManager.class); this.term = vm.resolveTerm(value); } catch (ComponentLookupException ex) { this.term = null; } }
FormField(String value, String title, String hint, String metaData, boolean expandable, boolean yesSelected, boolean noSelected) { super(title); this.value = value; this.hint = hint; this.metaData = metaData; this.expandable = expandable; this.selection = new boolean[2]; this.selection[YES] = yesSelected; this.selection[NO] = noSelected; try { VocabularyManager vm = ComponentManagerRegistry.getContextComponentManager().getInstance(VocabularyManager.class); this.term = vm.resolveTerm(value); } catch (ComponentLookupException ex) { this.term = null; } }
@Override public List<TermAnnotation> annotate(String text) throws AnnotationException { final List<Annotation> biolarkAnnotations = this.biolark.annotatePlain(text, false); final List<TermAnnotation> finalAnnotations = new LinkedList<TermAnnotation>(); // Resolve each termID against the ontology for (final Annotation biolarkAnnotation : biolarkAnnotations) { final String termId = FilenameUtils.getBaseName(biolarkAnnotation.getUri()).replace('_', ':'); final VocabularyTerm term = this.vocabularies.resolveTerm(termId); if (term != null) { final long start = biolarkAnnotation.getStartOffset(); final long end = biolarkAnnotation.getEndOffset(); finalAnnotations.add(new TermAnnotation(start, end, term)); } } return finalAnnotations; } }
/** * Fills in an extended phenotype field using the base phenotype field. * * @param baseFieldName the name of the field holding the basic list of phenotypes * @param extendedFieldName the name of the target field that will hold the extended list of phenotypes * @param patientRecordObj the object to update */ private void updateField(String baseFieldName, String extendedFieldName, BaseObject patientRecordObj) { @SuppressWarnings("unchecked") List<String> phenotypes = patientRecordObj.getListValue(baseFieldName); Set<String> extendedPhenotypes = new TreeSet<>(); Set<String> sortedPhenotypes = new TreeSet<>(); for (String phenotype : phenotypes) { sortedPhenotypes.add(phenotype); VocabularyTerm phenotypeTerm = this.vocabularyManager.resolveTerm(phenotype); if (phenotypeTerm != null) { for (VocabularyTerm term : phenotypeTerm.getAncestorsAndSelf()) { extendedPhenotypes.add(term.getId()); } } else { extendedPhenotypes.add(phenotype); } } patientRecordObj.setDBStringListValue(extendedFieldName, new ArrayList<>(extendedPhenotypes)); patientRecordObj.setDBStringListValue(baseFieldName, new ArrayList<>(sortedPhenotypes)); } }
@Override public List<TermAnnotation> annotate(String text) throws AnnotationException { List<RESTWrapper.RESTAnnotation> annotations = wrapper.annotate(text); List<TermAnnotation> retval = new ArrayList<>(annotations.size()); for (RESTWrapper.RESTAnnotation annotation : annotations) { String termId = annotation.getToken().getId().replace("hpo:", "").replace("_", ":"); VocabularyTerm term = this.vocabularies.resolveTerm(termId); if (term != null) { long start = annotation.getStart(); long end = annotation.getEnd(); retval.add(new TermAnnotation(start, end, term)); } } return retval; } }
@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() { 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 void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; BaseObject patientObject = doc.getXObject(Patient.CLASS_REFERENCE); if (patientObject != null) { XWikiContext context = (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); for (String field : this.fieldsToFix) { DBStringListProperty currentTermList = (DBStringListProperty) patientObject.getField(field); if (currentTermList == null) { continue; } List<String> terms = currentTermList.getList(); Set<String> correctSet = new LinkedHashSet<>(); for (String term : terms) { VocabularyTerm properTerm = this.vocabularyManager.resolveTerm(term); if (properTerm != null) { correctSet.add(properTerm.getId()); } else { correctSet.add(term); } } List<String> correctList = new LinkedList<>(); correctList.addAll(correctSet); patientObject.set(field, correctList, context); } } } }
@Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; BaseObject patientObject = doc.getXObject(Patient.CLASS_REFERENCE); if (patientObject != null) { XWikiContext context = (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); for (String field : this.fieldsToFix) { DBStringListProperty currentTermList = (DBStringListProperty) patientObject.getField(field); if (currentTermList == null) { continue; } List<String> terms = currentTermList.getList(); Set<String> correctSet = new LinkedHashSet<>(); for (String term : terms) { VocabularyTerm properTerm = this.vocabularyManager.resolveTerm(term); if (properTerm != null) { correctSet.add(properTerm.getId()); } else { correctSet.add(term); } } List<String> correctList = new LinkedList<>(); correctList.addAll(correctSet); patientObject.set(field, correctList, context); } } } }