Search in sources :

Example 1 with SimpleConfiguration

use of org.semanticweb.owlapi.reasoner.SimpleConfiguration in project goci by EBISPOT.

the class DefaultGWASOWLPublisher method publishGWASDataInferredView.

public OWLReasoner publishGWASDataInferredView(OWLOntology ontology) throws OWLConversionException {
    getLog().debug("Loading any missing imports...");
    StringBuilder loadedOntologies = new StringBuilder();
    int n = 1;
    for (OWLOntology o : ontology.getOWLOntologyManager().getOntologies()) {
        loadedOntologies.append("\t").append(n++).append(") ").append(o.getOntologyID().getOntologyIRI()).append("\n");
    }
    getLog().debug("Imports collected: the following ontologies have been loaded in this session:\n" + loadedOntologies.toString());
    getLog().info("Classifying ontology from " + ontology.getOntologyID().getOntologyIRI());
    getLog().debug("Creating reasoner... ");
    OWLReasonerFactory factory = new Reasoner.ReasonerFactory();
    ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
    OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
    OWLReasoner reasoner = factory.createReasoner(ontology, config);
    getLog().debug("Precomputing inferences...");
    reasoner.precomputeInferences();
    getLog().debug("Checking ontology consistency...");
    reasoner.isConsistent();
    getLog().debug("Checking for unsatisfiable classes...");
    if (reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() > 0) {
        throw new OWLConversionException("Once classified, unsatisfiable classes were detected");
    } else {
        getLog().info("Reasoning complete! ");
        return reasoner;
    }
}
Also used : OWLConversionException(uk.ac.ebi.spot.goci.exception.OWLConversionException) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) ConsoleProgressMonitor(org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory)

Example 2 with SimpleConfiguration

use of org.semanticweb.owlapi.reasoner.SimpleConfiguration in project goci by EBISPOT.

the class IRITreeBuilder method buildIRITree.

// OntologyLoader ontologyLoader;
// 
// @Autowired
// public IRITreeBuilder(OntologyLoader ontologyLoader){
// this.ontologyLoader = ontologyLoader;
// }
public IRITree buildIRITree(URL efoLocation) throws URISyntaxException, OWLOntologyCreationException {
    // load efo
    getLog().info("Loading efo...");
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology efo = manager.loadOntology(IRI.create(efoLocation));
    owlNothingIRI = manager.getOWLDataFactory().getOWLNothing().getIRI().toString();
    // create a reasoner over efo
    getLog().info("Reasoning over efo...");
    OWLReasonerFactory factory = new Reasoner.ReasonerFactory();
    ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
    OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
    OWLReasoner reasoner = factory.createReasoner(efo, config);
    getLog().info("Precomputing inferences...");
    reasoner.precomputeInferences();
    getLog().info("Checking ontology consistency...");
    reasoner.isConsistent();
    // get 'top' class
    OWLClass topClass = reasoner.getTopClassNode().getRepresentativeElement();
    getLog().info("Reasoner 'top class' element is " + topClass.getIRI());
    IRITree tree = new IRITree();
    // do one level deep manually - should only be experimental factor
    IRINode rootNode = null;
    OWLClass efClass = null;
    NodeSet<OWLClass> subclasses = reasoner.getSubClasses(topClass, true);
    for (Node<OWLClass> node : subclasses) {
        OWLClass cls = node.getRepresentativeElement();
        getLog().debug("Next child of " + topClass + " is " + cls);
        if (cls.getIRI().toString().equals(OntologyConstants.EXPERIMENTAL_FACTOR_CLASS_IRI)) {
            efClass = cls;
            rootNode = new IRINode(cls.getIRI(), getClassLabel(efo, cls));
        }
    }
    if (rootNode != null) {
        getLog().info("Building tree... walking ontology from " + rootNode.getLabel() + " down...");
        tree.setRootNode(rootNode);
        recurse(reasoner, efo, efClass, rootNode);
        getLog().info("...Tree build complete!");
    } else {
        throw new RuntimeException("Could not find Experimental Factor as a child of OWL:Thing");
    }
    return tree;
}
Also used : OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) ConsoleProgressMonitor(org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory)

Example 3 with SimpleConfiguration

use of org.semanticweb.owlapi.reasoner.SimpleConfiguration in project goci by EBISPOT.

the class ReasonedOntologyLoader method indexOntology.

protected OWLOntology indexOntology(OWLOntology ontology) throws OWLOntologyCreationException {
    getLog().debug("Trying to create a reasoner over ontology '" + getOntologyURI() + "'");
    OWLReasonerFactory factory = new Reasoner.ReasonerFactory();
    ReasonerProgressMonitor progressMonitor = new LoggingReasonerProgressMonitor(getLog());
    OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
    this.reasoner = factory.createReasoner(ontology, config);
    getLog().debug("Precomputing inferences...");
    reasoner.precomputeInferences();
    getLog().debug("Checking ontology consistency...");
    reasoner.isConsistent();
    getLog().debug("Checking for unsatisfiable classes...");
    if (reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() > 0) {
        throw new OWLOntologyCreationException("Once classified, unsatisfiable classes were detected in '" + getOntologyIRI() + "'");
    } else {
        getLog().debug("Reasoning complete! ");
    }
    Set<OWLClass> allClasses = ontology.getClassesInSignature();
    removeExcludedClasses(ontology, allClasses, superclass -> reasoner.getSubClasses(superclass, false).getFlattened());
    int labelCount = 0;
    int labelledClassCount = 0;
    int synonymCount = 0;
    int synonymedClassCount = 0;
    getLog().debug("Loading " + allClasses.size() + " classes...");
    for (OWLClass ontologyClass : allClasses) {
        IRI clsIri = ontologyClass.getIRI();
        // get IRI fragment/path
        Optional<String> accession = evaluateAccessionValue(ontology, ontologyClass);
        if (accession.isPresent()) {
            addClassAccession(clsIri, accession.get());
        }
        // get label annotations
        Optional<String> label = evaluateLabelAnnotationValue(ontology, ontologyClass);
        if (label.isPresent()) {
            addClassLabel(clsIri, label.get());
            labelledClassCount++;
            labelCount++;
        }
        // get all synonym annotations
        getLog().trace("Loading synonyms of " + clsIri.toString() + "...");
        Set<String> synonyms = evaluateSynonymAnnotationValues(ontology, ontologyClass);
        if (!synonyms.isEmpty()) {
            addSynonyms(clsIri, synonyms);
            synonymCount += synonyms.size();
            synonymedClassCount++;
        }
        // get parent labels
        getLog().trace("Loading parents of " + clsIri.toString() + "...");
        Set<OWLClass> parents = reasoner.getSuperClasses(ontologyClass, false).getFlattened();
        // only add type if the parent isn't excluded
        Set<String> parentLabelSet = parents.stream().filter(allClasses::contains).peek(parent -> getLog().trace("Next parent of " + label + ": " + parent)).map(parent -> evaluateLabelAnnotationValue(ontology, parent)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
        // always also add current class to the parents
        label.ifPresent(parentLabelSet::add);
        addClassParentLabels(clsIri, parentLabelSet);
        // get child labels
        getLog().trace("Loading children of " + clsIri.toString() + "...");
        Set<OWLClass> children = reasoner.getSubClasses(ontologyClass, false).getFlattened();
        // only add type if the child isn't excluded
        Set<String> childLabelSet = children.stream().filter(allClasses::contains).peek(child -> getLog().trace("Next child of " + label + ": " + child)).map(child -> evaluateLabelAnnotationValue(ontology, child)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
        // always also add current class to the parents
        label.ifPresent(childLabelSet::add);
        addClassChildLabels(clsIri, childLabelSet);
    }
    getLog().debug("Successfully indexed " + labelCount + " labels on " + labelledClassCount + " classes and " + synonymCount + " synonyms on " + synonymedClassCount + " classes!");
    return ontology;
}
Also used : Logger(org.slf4j.Logger) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) Reasoner(org.semanticweb.HermiT.Reasoner) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) Set(java.util.Set) Collectors(java.util.stream.Collectors) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) ReasonerProgressMonitor(org.semanticweb.owlapi.reasoner.ReasonerProgressMonitor) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Component(org.springframework.stereotype.Component) IRI(org.semanticweb.owlapi.model.IRI) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) Optional(java.util.Optional) OWLClass(org.semanticweb.owlapi.model.OWLClass) IRI(org.semanticweb.owlapi.model.IRI) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) Optional(java.util.Optional) ReasonerProgressMonitor(org.semanticweb.owlapi.reasoner.ReasonerProgressMonitor) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory)

Aggregations

OWLOntology (org.semanticweb.owlapi.model.OWLOntology)3 OWLReasoner (org.semanticweb.owlapi.reasoner.OWLReasoner)3 OWLReasonerConfiguration (org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration)3 OWLReasonerFactory (org.semanticweb.owlapi.reasoner.OWLReasonerFactory)3 SimpleConfiguration (org.semanticweb.owlapi.reasoner.SimpleConfiguration)3 OWLClass (org.semanticweb.owlapi.model.OWLClass)2 ConsoleProgressMonitor (org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor)2 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Reasoner (org.semanticweb.HermiT.Reasoner)1 IRI (org.semanticweb.owlapi.model.IRI)1 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)1 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)1 ReasonerProgressMonitor (org.semanticweb.owlapi.reasoner.ReasonerProgressMonitor)1 Logger (org.slf4j.Logger)1 Component (org.springframework.stereotype.Component)1 OWLConversionException (uk.ac.ebi.spot.goci.exception.OWLConversionException)1