use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class TerminologyTree method getProximalPrimitiveParentIds.
/**
* <p><i>Non-api, tests only</i></p>
* <p>
* Returns the identifiers of the proximal primitive parents based on the given ancestor {@link Iterable}. This is the internal implementation of
* the proximal primitive algorithm. This method should not be part of the API of this or any other class. Use
* {@link #getProximalPrimitiveParents(String)} or {@link #getProximalPrimitiveParentIds(String)} instead.
* </p>
*
* @param ancestors
* @return
*/
/* package */
Set<String> getProximalPrimitiveParentIds(final Iterable<SnomedConcept> ancestors) {
final Set<String> proximalPrimitiveParentIds = newHashSet();
for (SnomedConcept ancestor : ancestors) {
if (ancestor.isPrimitive()) {
final String primitiveAncestorId = ancestor.getId();
if (proximalPrimitiveParentIds.isEmpty()) {
proximalPrimitiveParentIds.add(primitiveAncestorId);
} else {
boolean doAdd = true;
for (String id : newHashSet(proximalPrimitiveParentIds)) {
// if the current candidate is a subtype of any already visited nodes, then replace those nodes
if (isSubTypeOf(primitiveAncestorId, id)) {
proximalPrimitiveParentIds.remove(id);
proximalPrimitiveParentIds.add(primitiveAncestorId);
doAdd = false;
} else if (doAdd && isSuperTypeOf(primitiveAncestorId, id)) {
// do NOT add the node if it is a super type of any currently selected primitives
doAdd = false;
}
}
if (doAdd) {
proximalPrimitiveParentIds.add(primitiveAncestorId);
}
}
}
}
return proximalPrimitiveParentIds;
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class TreeBuilderImpl method build.
@Override
public TerminologyTree build(final ResourceURI resource, final Iterable<SnomedConcept> nodes, final String snomedDescriptionExpand) {
final Collection<SnomedConcept> topLevelConcepts = this.topLevelConcepts == null ? getDefaultTopLevelConcepts(resource, snomedDescriptionExpand) : this.topLevelConcepts;
final Map<String, SnomedConcept> treeItemsById = newHashMap();
// all matching concepts should be in the componentMap
treeItemsById.putAll(FluentIterable.from(nodes).uniqueIndex(IComponent::getId));
final Collection<String> requiredTopLevelConceptIds = topLevelConcepts.stream().map(IComponent::getId).collect(Collectors.toSet());
// compute subType and superType maps for the tree
final SetMultimap<String, String> superTypeMap = HashMultimap.create();
final SetMultimap<String, String> subTypeMap = HashMultimap.create();
for (SnomedConcept entry : nodes) {
final LongCollection parentIds = getParents(entry);
final LongCollection ancestorIds = getAncestors(entry);
if (parentIds != null) {
final Collection<String> parents = LongSets.toStringSet(parentIds);
final Collection<String> selectedParents = newHashSet();
// if the parent is not a match or TOP level
for (String parent : parents) {
if (treeItemsById.containsKey(parent) || requiredTopLevelConceptIds.contains(parent)) {
selectedParents.add(parent);
}
}
if (selectedParents.isEmpty()) {
findParentInAncestors(entry, treeItemsById, requiredTopLevelConceptIds, subTypeMap, superTypeMap);
} else {
for (String parent : selectedParents) {
subTypeMap.put(parent, entry.getId());
superTypeMap.put(entry.getId(), parent);
}
}
} else if (ancestorIds != null) {
findParentInAncestors(entry, treeItemsById, requiredTopLevelConceptIds, subTypeMap, superTypeMap);
} else {
// no parents or ancestors, root element
subTypeMap.put(null, entry.getId());
}
}
// add TOP levels
for (SnomedConcept entry : topLevelConcepts) {
if (!Concepts.ROOT_CONCEPT.equals(entry.getId()) && !treeItemsById.containsKey(entry.getId())) {
if (subTypeMap.containsKey(entry.getId())) {
treeItemsById.put(entry.getId(), entry);
}
}
}
for (SnomedConcept entry : topLevelConcepts) {
if (Concepts.ROOT_CONCEPT.equals(entry.getId())) {
// find all top level child and connect them with the root
for (SnomedConcept tl : topLevelConcepts) {
if (!Concepts.ROOT_CONCEPT.equals(tl.getId()) && treeItemsById.containsKey(tl.getId())) {
subTypeMap.put(entry.getId(), tl.getId());
superTypeMap.put(tl.getId(), entry.getId());
}
}
// only add root concept if the tree contains top level concepts
if (subTypeMap.containsKey(Concepts.ROOT_CONCEPT)) {
treeItemsById.put(entry.getId(), entry);
subTypeMap.put(null, entry.getId());
}
break;
}
}
// fetch all missing components to build the remaining part of the FULL tree
final Set<String> allRequiredComponents = newHashSet();
allRequiredComponents.addAll(superTypeMap.keySet());
allRequiredComponents.addAll(subTypeMap.keySet());
allRequiredComponents.removeAll(treeItemsById.keySet());
allRequiredComponents.remove(null);
// fetch required data for all unknown items
for (SnomedConcept entry : getComponents(resource, allRequiredComponents, snomedDescriptionExpand)) {
treeItemsById.put(entry.getId(), entry);
}
return new TerminologyTree(treeItemsById, subTypeMap, superTypeMap);
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class TreeBuilderImpl method getDefaultTopLevelConcepts.
private Collection<SnomedConcept> getDefaultTopLevelConcepts(final ResourceURI resource, final String snomedDescriptionExpand) {
final SnomedConcept root = SnomedRequests.prepareGetConcept(Concepts.ROOT_CONCEPT).setExpand(String.format("%2$s,%s(direct:true,expand(%2$s))", Trees.STATED_FORM.equals(getForm()) ? "statedDescendants" : "descendants", snomedDescriptionExpand)).setLocales(locales).build(resource).execute(getBus()).getSync();
final Collection<SnomedConcept> requiredTreeItemConcepts = newHashSet();
requiredTreeItemConcepts.add(root);
requiredTreeItemConcepts.addAll(root.getDescendants().getItems());
return requiredTreeItemConcepts;
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class SnomedConceptSearchRequestEvaluator method toConcept.
private Concept toConcept(ResourceURI codeSystem, SnomedConcept snomedConcept, String term, boolean requestedExpand) {
final Concept concept = toConcept(codeSystem, snomedConcept, snomedConcept.getIconId(), term, snomedConcept.getScore());
concept.setAlternativeTerms(FluentIterable.from(snomedConcept.getPreferredDescriptions()).transform(pd -> pd.getTerm()).toSortedSet(Comparator.naturalOrder()));
concept.setParentIds(snomedConcept.getParentIdsAsString());
concept.setAncestorIds(snomedConcept.getAncestorIdsAsString());
if (requestedExpand) {
concept.setInternalConcept(snomedConcept);
}
return concept;
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class SnomedSimpleTypeRefSetDSVExporter method writeValues.
private void writeValues(BufferedWriter writer, SnomedConcepts chunk) throws IOException {
List<String> dataRow = newArrayList();
for (SnomedConcept concept : chunk) {
dataRow.clear();
for (AbstractSnomedDsvExportItem exportItem : exportItems) {
switch(exportItem.getType()) {
case DESCRIPTION:
{
final ComponentIdSnomedDsvExportItem descriptionItem = (ComponentIdSnomedDsvExportItem) exportItem;
final String typeId = descriptionItem.getComponentId();
int occurrences = descriptionCount.get(typeId);
final Map<String, String> termsById = concept.getDescriptions().stream().filter(d -> typeId.equals(d.getTypeId())).collect(Collectors.toMap(SnomedDescription::getId, SnomedDescription::getTerm));
addCells(dataRow, occurrences, includeDescriptionId, termsById);
break;
}
case RELATIONSHIP:
{
final ComponentIdSnomedDsvExportItem relationshipItem = (ComponentIdSnomedDsvExportItem) exportItem;
for (Integer propertyGroup : propertyCountByGroup.keySet()) {
final String typeId = relationshipItem.getComponentId();
final Map<String, Integer> groupOccurrences = propertyCountByGroup.getOrDefault(propertyGroup, NO_OCCURRENCES);
final int occurrences = groupOccurrences.getOrDefault(typeId, 0);
concept.getRelationships().stream().filter(r -> typeId.equals(r.getTypeId()) && Objects.equals(r.getRelationshipGroup(), propertyGroup) && (Concepts.INFERRED_RELATIONSHIP.equals(r.getCharacteristicTypeId()) || Concepts.ADDITIONAL_RELATIONSHIP.equals(r.getCharacteristicTypeId()))).forEach(relationship -> {
if (relationship.hasValue()) {
addCells(dataRow, occurrences, includeRelationshipId, ImmutableMap.of(relationship.getValue(), ""));
} else {
addCells(dataRow, occurrences, includeRelationshipId, ImmutableMap.of(relationship.getDestinationId(), getPreferredTerm(relationship.getDestination())));
}
});
}
break;
}
case DATAYPE:
{
final DatatypeSnomedDsvExportItem datatypeItem = (DatatypeSnomedDsvExportItem) exportItem;
for (Integer propertyGroup : propertyCountByGroup.keySet()) {
Map<String, Integer> groupedOccurrences = propertyCountByGroup.getOrDefault(propertyGroup, NO_OCCURRENCES);
final String typeId = datatypeItem.getComponentId();
int occurrences = groupedOccurrences.getOrDefault(typeId, 0);
if (occurrences < 1) {
break;
}
final List<String> properties = concept.getMembers().stream().filter(m -> SnomedRefSetType.CONCRETE_DATA_TYPE.equals(m.type()) && m.isActive() && typeId.equals(m.getProperties().get(SnomedRf2Headers.FIELD_TYPE_ID)) && Objects.equals(m.getProperties().get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP), propertyGroup) && (Concepts.INFERRED_RELATIONSHIP.equals(m.getProperties().get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)) || Concepts.ADDITIONAL_RELATIONSHIP.equals(m.getProperties().get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)))).map(m -> m.getProperties().get(SnomedRf2Headers.FIELD_VALUE)).map(p -> {
if (datatypeItem.isBooleanDatatype()) {
return "1".equals(p) ? "Yes" : "No";
} else {
return p.toString();
}
}).sorted().collect(Collectors.toList());
for (String value : properties) {
dataRow.add(value);
occurrences--;
}
while (occurrences > 0) {
dataRow.add("");
occurrences--;
}
}
break;
}
case PREFERRED_TERM:
if (includeDescriptionId) {
dataRow.add(getPreferredTermId(concept));
dataRow.add(getPreferredTerm(concept));
} else {
dataRow.add(getPreferredTerm(concept));
}
break;
case CONCEPT_ID:
dataRow.add(concept.getId());
break;
case MODULE:
dataRow.add(concept.getModuleId());
break;
case EFFECTIVE_TIME:
dataRow.add(EffectiveTimes.format(concept.getEffectiveTime()));
break;
case STATUS_LABEL:
dataRow.add(concept.isActive() ? "Active" : "Inactive");
break;
case DEFINITION_STATUS:
dataRow.add(concept.getDefinitionStatusId());
break;
default:
break;
}
}
writer.write(joiner.join(dataRow));
writer.write(lineSeparator);
}
}
Aggregations