Search in sources :

Example 66 with SnomedConcept

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;
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 67 with SnomedConcept

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);
}
Also used : LongCollection(com.b2international.collections.longs.LongCollection) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 68 with SnomedConcept

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;
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 69 with SnomedConcept

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;
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Concept(com.b2international.snowowl.core.domain.Concept)

Example 70 with SnomedConcept

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);
    }
}
Also used : EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) java.util(java.util) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) ComponentIdSnomedDsvExportItem(com.b2international.snowowl.snomed.datastore.internal.rf2.ComponentIdSnomedDsvExportItem) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Charsets(com.google.common.base.Charsets) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Files(java.nio.file.Files) ImmutableMap(com.google.common.collect.ImmutableMap) Optional.ofNullable(java.util.Optional.ofNullable) BufferedWriter(java.io.BufferedWriter) Dates(com.b2international.snowowl.core.date.Dates) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) AbstractSnomedDsvExportItem(com.b2international.snowowl.snomed.datastore.internal.rf2.AbstractSnomedDsvExportItem) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedRefSetDSVExportModel(com.b2international.snowowl.snomed.datastore.internal.rf2.SnomedRefSetDSVExportModel) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) DatatypeSnomedDsvExportItem(com.b2international.snowowl.snomed.datastore.internal.rf2.DatatypeSnomedDsvExportItem) Ordering(com.google.common.collect.Ordering) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) SearchResourceRequestIterator(com.b2international.snowowl.core.request.SearchResourceRequestIterator) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) Joiner(com.google.common.base.Joiner) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractSnomedDsvExportItem(com.b2international.snowowl.snomed.datastore.internal.rf2.AbstractSnomedDsvExportItem) DatatypeSnomedDsvExportItem(com.b2international.snowowl.snomed.datastore.internal.rf2.DatatypeSnomedDsvExportItem) ComponentIdSnomedDsvExportItem(com.b2international.snowowl.snomed.datastore.internal.rf2.ComponentIdSnomedDsvExportItem)

Aggregations

SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)90 Test (org.junit.Test)42 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)17 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)15 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)15 LocalDate (java.time.LocalDate)15 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)12 SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)12 Options (com.b2international.commons.options.Options)10 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)10 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)9 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)8 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)8 CodeSystemRestRequests.createCodeSystem (com.b2international.snowowl.test.commons.codesystem.CodeSystemRestRequests.createCodeSystem)8 List (java.util.List)8 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 Json (com.b2international.commons.json.Json)7 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)7 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)5