Search in sources :

Example 1 with OWLClassExpression

use of org.semanticweb.owlapi.model.OWLClassExpression in project goci by EBISPOT.

the class KBLoader method quantifyKnowledgeBase.

public Map<IRI, Integer> quantifyKnowledgeBase(URL efoLocation, URL gwasSchemaLocation, URL kbLocation) throws OWLOntologyCreationException, URISyntaxException {
    Map<IRI, Integer> results = new HashMap<IRI, Integer>();
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    // do iri mapping
    URI efoURI = efoLocation.toURI();
    URI gwasSchemaURI = gwasSchemaLocation.toURI();
    URI kbURI = kbLocation.toURI();
    getLog().info("Mapping EFO to " + efoURI);
    manager.addIRIMapper(new SimpleIRIMapper(IRI.create(OntologyConstants.EFO_ONTOLOGY_SCHEMA_IRI), IRI.create(efoURI)));
    getLog().info("Mapping GWAS schema to " + gwasSchemaURI);
    manager.addIRIMapper(new SimpleIRIMapper(IRI.create(OntologyConstants.GWAS_ONTOLOGY_SCHEMA_IRI), IRI.create(gwasSchemaURI)));
    System.out.println("Loading knowledge base " + kbURI);
    getLog().info("Loading knowledge base " + kbURI);
    // load the knowledgebase
    OWLOntology kb = manager.loadOntology(IRI.create(kbURI));
    System.out.println("Processing knowledge base");
    getLog().info("Processing knowledge base");
    // retrieve all individuals
    Set<OWLNamedIndividual> inds = kb.getIndividualsInSignature();
    System.out.println("The knowledge base contains " + inds.size() + " individuals");
    getLog().info("The knowledge base contains " + inds.size() + " individuals");
    for (OWLNamedIndividual ind : inds) {
        // for each individual, check if it is an association
        boolean isAssociation = false;
        Set<OWLClassExpression> types = ind.getTypes(kb);
        for (OWLClassExpression type : types) {
            if (type.asOWLClass().getIRI().toString().equals(OntologyConstants.TRAIT_ASSOCIATION_CLASS_IRI)) {
                isAssociation = true;
                break;
            }
        }
        if (isAssociation) {
            // get the IRI of the trait class (from EFO) this individual is associated with
            Set<IRI> traitClasses = getTraitClass(kb, ind);
            for (IRI traitClass : traitClasses) {
                // skip SNPs
                if (traitClass.toString().equals(OntologyConstants.SNP_CLASS_IRI)) {
                    continue;
                }
                // increment count
                if (results.containsKey(traitClass)) {
                    int count = results.get(traitClass) + 1;
                    results.put(traitClass, count);
                } else {
                    results.put(traitClass, 1);
                }
            }
        }
    }
    return results;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) SimpleIRIMapper(org.semanticweb.owlapi.util.SimpleIRIMapper) HashMap(java.util.HashMap) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) URI(java.net.URI) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 2 with OWLClassExpression

use of org.semanticweb.owlapi.model.OWLClassExpression in project goci by EBISPOT.

the class KBLoader method getTraitClass.

private Set<IRI> getTraitClass(OWLOntology ontology, OWLNamedIndividual individual) {
    OWLOntologyManager manager = ontology.getOWLOntologyManager();
    OWLDataFactory dataFactory = manager.getOWLDataFactory();
    Set<IRI> results = new HashSet<IRI>();
    // get all individuals related to this one by "is_about"
    OWLObjectProperty has_object = dataFactory.getOWLObjectProperty(IRI.create(OntologyConstants.HAS_OBJECT_IRI));
    Set<OWLIndividual> relatedInds = individual.getObjectPropertyValues(has_object, ontology);
    // for each related individual, get all types
    for (OWLIndividual related : relatedInds) {
        Set<OWLClassExpression> types = related.getTypes(ontology);
        for (OWLClassExpression type : types) {
            results.add(type.asOWLClass().getIRI());
        }
    }
    return results;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 3 with OWLClassExpression

use of org.semanticweb.owlapi.model.OWLClassExpression in project stanbol by apache.

the class OntologyNetworkConfigurationUtils method getScopes.

/**
     * To get all the instances of Scope in this configuration
     * 
     * @param config
     * @return
     */
public static String[] getScopes(OWLOntology config) {
    Set<OWLIndividual> scopes = cScope.getIndividuals(config);
    List<String> result = new ArrayList<String>();
    for (OWLIndividual iScope : scopes) {
        for (OWLClassExpression sce : iScope.getTypes(config)) {
            if (sce.containsConjunct(cScope)) {
                if (iScope.isNamed()) {
                    result.add(((OWLNamedIndividual) iScope).getIRI().toString());
                }
            }
        }
    }
    return result.toArray(EMPTY_IRI_ARRAY);
}
Also used : ArrayList(java.util.ArrayList) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 4 with OWLClassExpression

use of org.semanticweb.owlapi.model.OWLClassExpression in project stanbol by apache.

the class RegistryManagerImpl method createModel.

@Override
public Set<Registry> createModel(Set<OWLOntology> registryOntologies) {
    Set<Registry> results = new HashSet<Registry>();
    // Reset population
    population.clear();
    // Build the transitive imports closure of the union.
    Set<OWLOntology> closure = new HashSet<OWLOntology>();
    for (OWLOntology rego : registryOntologies) closure.addAll(rego.getOWLOntologyManager().getImportsClosure(rego));
    /*
         * For each value in this map, index 0 is the score of the library class, while 1 is the score of the
         * ontology class.
         */
    final Map<IRI, int[]> candidateTypes = new HashMap<IRI, int[]>();
    /*
         * Scans class assertions and object property values and tries to determine the type of each
         * individual it finds.
         */
    OWLAxiomVisitor scanner = new OWLAxiomVisitorAdapter() {

        /*
             * For a given identifier, returns the array of integers whose value determine the likelihood if
             * the corresponding entity being a library or an ontology. If no such array exists, it is
             * created.
             */
        private int[] checkScores(IRI key) {
            int[] scores;
            if (candidateTypes.containsKey(key))
                scores = candidateTypes.get(key);
            else {
                scores = new int[] { 0, 0 };
                candidateTypes.put(key, scores);
            }
            return scores;
        }

        @Override
        public void visit(OWLAnnotationAssertionAxiom axiom) {
            /*
                 * Works like object property assertions, in case hasOntology and isOntologyOf are not
                 * detected to be object properties (e.g. due to a failure to load codolight or the registry
                 * metamodel).
                 */
            OWLAnnotationProperty prop = axiom.getProperty();
            if (hasOntologyAnn.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its library score.
                OWLObject ind = axiom.getSubject();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[0]++;
                }
                // The axiom object gets a +1 in its ontology score.
                ind = axiom.getValue();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[1]++;
                }
            } else if (isOntologyOfAnn.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its ontology score.
                OWLObject ind = axiom.getSubject();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[1]++;
                }
                // The axiom object gets a +1 in its library score.
                ind = axiom.getValue();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[0]++;
                }
            }
        }

        @Override
        public void visit(OWLClassAssertionAxiom axiom) {
            OWLIndividual ind = axiom.getIndividual();
            // Do not accept anonymous registry items.
            if (ind.isAnonymous())
                return;
            IRI iri = ind.asOWLNamedIndividual().getIRI();
            int[] scores = checkScores(iri);
            OWLClassExpression type = axiom.getClassExpression();
            // If the type is stated to be a library, increase its library score.
            if (cRegistryLibrary.equals(type)) {
                scores[0]++;
            } else // If the type is stated to be an ontology, increase its ontology score.
            if (cOntology.equals(type)) {
                scores[1]++;
            }
        }

        @Override
        public void visit(OWLObjectPropertyAssertionAxiom axiom) {
            OWLObjectPropertyExpression prop = axiom.getProperty();
            if (hasOntology.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its library score.
                OWLIndividual ind = axiom.getSubject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[0]++;
                }
                // The axiom object gets a +1 in its ontology score.
                ind = axiom.getObject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[1]++;
                }
            } else if (isOntologyOf.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its ontology score.
                OWLIndividual ind = axiom.getSubject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[1]++;
                }
                // The axiom object gets a +1 in its library score.
                ind = axiom.getObject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[0]++;
                }
            }
        }
    };
    // First pass to determine the types.
    for (OWLOntology o : closure) for (OWLAxiom ax : o.getAxioms()) ax.accept(scanner);
    // Then populate on the registry
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (IRI iri : candidateTypes.keySet()) {
        int[] scores = candidateTypes.get(iri);
        if (scores != null && (scores[0] > 0 || scores[1] > 0)) {
            if (scores[0] > 0 && scores[1] == 0)
                population.put(iri, riFactory.createLibrary(df.getOWLNamedIndividual(iri)));
            else if (scores[0] == 0 && scores[1] > 0)
                population.put(iri, riFactory.createRegistryOntology(df.getOWLNamedIndividual(iri)));
        }
    // else log.warn("Unable to determine type for registry item {}", iri);
    }
    for (OWLOntology oReg : registryOntologies) {
        try {
            results.add(populateRegistry(oReg));
        } catch (RegistryContentException e) {
            log.error("An error occurred while populating an ontology registry.", e);
        }
    }
    return results;
}
Also used : OWLObject(org.semanticweb.owlapi.model.OWLObject) IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAxiomVisitor(org.semanticweb.owlapi.model.OWLAxiomVisitor) HashMap(java.util.HashMap) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLAxiomVisitorAdapter(org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLObjectPropertyExpression(org.semanticweb.owlapi.model.OWLObjectPropertyExpression) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 5 with OWLClassExpression

use of org.semanticweb.owlapi.model.OWLClassExpression in project stanbol by apache.

the class TestClerezzaProvider method testVersionIRISplit.

@Test
public void testVersionIRISplit() throws Exception {
    // Check the first version
    InputStream data = getClass().getResourceAsStream(fn1);
    OWLOntologyID key1 = ontologyProvider.loadInStore(data, RDF_XML, true);
    assertNotNull(key1);
    assertFalse(key1.isAnonymous());
    // Check the second version
    data = getClass().getResourceAsStream(fn2);
    OWLOntologyID key2 = ontologyProvider.loadInStore(data, RDF_XML, true);
    assertNotNull(key2);
    assertFalse(key2.isAnonymous());
    // Must be 2 different graphs
    assertFalse(key1.equals(key2));
    assertEquals(2, ontologyProvider.listPrimaryKeys().size());
    // Ontologies must not be tainting each other
    // Don't use keys any more here. They're not the real public keys!
    Set<OWLOntology> oAll = new HashSet<OWLOntology>();
    for (OWLOntologyID key : ontologyProvider.listPrimaryKeys()) {
        log.info("Found public key {}", key);
        oAll.add(ontologyProvider.getStoredOntology(key, OWLOntology.class, true));
    }
    Iterator<OWLOntology> it = oAll.iterator();
    OWLOntology o1 = it.next();
    OWLOntology o2 = it.next();
    for (OWLNamedIndividual i : o1.getIndividualsInSignature()) {
        Set<OWLClassExpression> tAll = i.getTypes(oAll), t1 = i.getTypes(o1), t2 = i.getTypes(o2);
        // Should be obvious from the OWL API
        assertTrue(tAll.containsAll(t1));
        // Should be obvious from the OWL API
        assertTrue(tAll.containsAll(t2));
        assertFalse(t1.containsAll(t2));
        assertFalse(t2.containsAll(t1));
    }
}
Also used : InputStream(java.io.InputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

OWLClassExpression (org.semanticweb.owlapi.model.OWLClassExpression)6 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)4 HashSet (java.util.HashSet)3 IRI (org.semanticweb.owlapi.model.IRI)3 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)3 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)3 HashMap (java.util.HashMap)2 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)2 OWLAxiomVisitor (org.semanticweb.owlapi.model.OWLAxiomVisitor)2 OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)2 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)2 OWLObjectPropertyAssertionAxiom (org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom)2 OWLObjectPropertyExpression (org.semanticweb.owlapi.model.OWLObjectPropertyExpression)2 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)2 OWLAxiomVisitorAdapter (org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter)2 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)1 Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)1