@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; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || !(obj instanceof OntologyTerm)) { return false; } return StringUtils.equals(getId(), ((OntologyTerm) obj).getId()); } }
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 long getDistanceTo(final OntologyTerm other) { if (other == null) { return -1; } if (this.equals(other)) { return 0; } long distance = Integer.MAX_VALUE; Map<String, Integer> myLevelMap = new HashMap<String, Integer>(); myLevelMap.put(getId(), 0); Map<String, Integer> otherLevelMap = new HashMap<String, Integer>(); otherLevelMap.put(other.getId(), 0); Set<OntologyTerm> myCrtLevel = new HashSet<OntologyTerm>(); myCrtLevel.add(this); Set<OntologyTerm> otherCrtLevel = new HashSet<OntologyTerm>(); 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 == Integer.MAX_VALUE ? -1 : distance; }
/** * 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)); } }
@Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; BaseObject patientObject = doc.getXObject(Patient.CLASS_REFERENCE); if (patientObject != null && !StringUtils.equals("PatientTemplate", doc.getDocumentReference().getName())) { 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<String>(); for (String term : terms) { OntologyTerm properTerm = this.ontologyManager.resolveTerm(term); if (properTerm != null) { correctSet.add(properTerm.getId()); } else { correctSet.add(term); } } List<String> correctList = new LinkedList<String>(); correctList.addAll(correctSet); patientObject.set(field, correctList, context); } } } }