/** * Retrieve an ontology given its identifier. * * @param ontologyId the ontology identifier, which is also used as a prefix in every term identifier from that * ontology, for example {@code HP} or {@code MIM} * @return the requested ontology, or {@code null} if it doesn't exist or isn't available in the platform */ public OntologyService getOntology(String ontologyId) { return this.manager.getOntology(ontologyId); }
@Override public long getDistance(OntologyTerm fromTerm, OntologyTerm toTerm) { if (fromTerm == null || toTerm == null) { return -1; } return fromTerm.getDistanceTo(toTerm); }
/** Loads the terms from the ontology when needed. */ private synchronized void loadTerms() { if (this.terms == null) { this.terms = this.ontology.getTerms(this.identifiers); } } }
/** * 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<String>(); Set<String> sortedPhenotypes = new TreeSet<String>(); for (String phenotype : phenotypes) { sortedPhenotypes.add(phenotype); OntologyTerm phenotypeTerm = this.ontologyManager.resolveTerm(phenotype); if (phenotypeTerm != null) { for (OntologyTerm term : phenotypeTerm.getAncestorsAndSelf()) { extendedPhenotypes.add(term.getId()); } } else { extendedPhenotypes.add(phenotype); } } patientRecordObj.setDBStringListValue(extendedFieldName, new ArrayList<String>(extendedPhenotypes)); patientRecordObj.setDBStringListValue(baseFieldName, new ArrayList<String>(sortedPhenotypes)); } }
private long processAncestorsAtDistance(int localDistance, Set<OntologyTerm> sourceUnprocessedAncestors, Map<String, Integer> sourceDistanceMap, Map<String, Integer> targetDistanceMap) { long minDistance = Integer.MAX_VALUE; Set<OntologyTerm> nextLevel = new HashSet<OntologyTerm>(); for (OntologyTerm term : sourceUnprocessedAncestors) { for (OntologyTerm 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 OntologyTerm getTerm(String id) { OntologyTerm result = this.externalServicesAccess.getCache().get(id); if (result == null) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(CommonParams.Q, ID_FIELD_NAME + ':' + ClientUtils.escapeQueryChars(id)); SolrDocumentList allResults = this.search(params); if (allResults != null && !allResults.isEmpty()) { result = new SolrOntologyTerm(allResults.get(0), this); this.externalServicesAccess.getCache().set(id, result); } else { this.externalServicesAccess.getCache().set(id, EMPTY_MARKER); } } return (result == EMPTY_MARKER) ? null : result; }
/** * Retrieve a term from its owner ontology. For this to work properly, the term identifier must contain a known * ontology prefix. * * @param termId the term identifier, in the format {@code <ontology prefix>:<term id>}, for example * {@code HP:0002066} * @return the requested term, or {@code null} if the term doesn't exist in the ontology, or no matching ontology is * available */ public OntologyTerm resolveTerm(String termId) { return this.manager.resolveTerm(termId); }
Set<OntologyTerm> lookIn; if (match.getAncestors().contains(reference)) { toFind = reference; lookIn = match.getParents(); } else if (reference.getAncestors().contains(match)) { toFind = match; lookIn = reference.getParents(); } else { Set<OntologyTerm> next = new HashSet<OntologyTerm>(); for (OntologyTerm term : lookIn) { next.addAll(term.getParents());
@Override public void initialize() throws InitializationException { this.externalServicesAccess.initialize(this.getName()); }
@Override public OntologyTerm resolveTerm(String termId) { OntologyService ontology = getOntologyForTerm(termId); if (ontology != null) { return ontology.getTerm(termId); } return null; }
@Override public boolean contains(Object o) { if (String.class.isInstance(o)) { return this.identifiers.contains(o); } else if (OntologyTerm.class.isInstance(o)) { return this.identifiers.contains(((OntologyTerm) o).getId()); } return false; }
/** * Delete all the data in the Solr index. * * @return {@code 0} if the command was successful, {@code 1} otherwise */ protected int clear() { try { this.externalServicesAccess.getServer().deleteByQuery("*:*"); return 0; } catch (SolrServerException ex) { this.logger.error("SolrServerException while clearing the Solr index", ex); } catch (IOException ex) { this.logger.error("IOException while clearing the Solr index", ex); } return 1; }
@Override public void initialize() throws InitializationException { Map<String, OntologyService> newOntologiesMap = new HashMap<String, OntologyService>(); for (OntologyService ontology : this.ontologies.values()) { for (String alias : ontology.getAliases()) { newOntologiesMap.put(alias, ontology); } } this.ontologies = newOntologiesMap; }
/** * Modify this function to add more version types. * * @return Map of all the version types. Each entry becomes an object in the patient record. */ public Map<String, String> getOntologiesVersions() { Map<String, String> result = new HashMap<>(); for (Entry<String, OntologyService> ontology : this.ontologies.entrySet()) { String version = ontology.getValue().getVersion(); if (StringUtils.isNotBlank(version)) { result.put(ontology.getKey(), version); } } return result; } }
@Override public void initialize(String serverName) throws InitializationException { try { this.server = new EmbeddedSolrServer(this.cores.getContainer(), serverName); this.cache = this.cacheFactory.createNewLocalCache(new CacheConfiguration()); } catch (RuntimeException ex) { throw new InitializationException("Invalid URL specified for the Solr server: {}"); } catch (final CacheException ex) { throw new InitializationException("Cannot create cache: " + ex.getMessage()); } }
@Override public double getScore(Feature match, Feature reference) { if (match == null || reference == null) { return Double.NaN; } else if (StringUtils.equals(match.getId(), reference.getId())) { return 1; } OntologyTerm matchTerm = this.ontologyManager.resolveTerm(match.getId()); OntologyTerm referenceTerm = this.ontologyManager.resolveTerm(reference.getId()); if (matchTerm == null || referenceTerm == null) { return Double.NaN; } int distance = getDistance(matchTerm, referenceTerm); return distance <= 3 ? Math.pow(2.0, -distance) : 0; }
/** * Retrieve an ontology given its identifier. This is a shortcut for {@link #getOntology(String)} which allows * scripts to use the shorter {@code $services.ontologies.hpo} notation for accessing an ontology. * * @param ontologyId the ontology identifier, which is also used as a prefix in every term identifier from that * ontology, for example {@code HP} or {@code MIM} * @return the requested ontology, or {@code null} if it doesn't exist or isn't available in the platform */ public OntologyService get(String ontologyId) { return this.manager.getOntology(ontologyId); } }
/** * Get the number of entries that match a specific Lucene query. * * @param query a valid the Lucene query as string * @return the number of entries matching the query */ protected long count(String query) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(CommonParams.Q, query); params.set(CommonParams.START, "0"); params.set(CommonParams.ROWS, "0"); SolrDocumentList results; try { results = this.externalServicesAccess.getServer().query(params).getResults(); return results.getNumFound(); } catch (Exception ex) { this.logger.error("Failed to count ontology terms: {}", ex.getMessage(), ex); return 0; } }