def test_IRIMapping(self): """ This method test both addNewIRIMapping() and getNewOWLOntologyManager(), including updating of the mappings in previously created OOMs. """ ontIRI = IRI.create('http://some.ontology.iri/path') docIRI = IRI.create('http://some.doc.iri/path') oom1 = oom_man.getNewOWLOntologyManager() # Verify that the new OOM does not contain the IRI mapping. self.assertIsNone(oom_man.lookupDocumentIRI(oom1, ontIRI)) oom_man.addNewIRIMapping(ontIRI, docIRI) # Verify that the old OOM got the new mapping. self.assertTrue(oom_man.lookupDocumentIRI(oom1, ontIRI).equals(docIRI)) # Verify that a new OOM has the new mapping. oom2 = oom_man.getNewOWLOntologyManager() self.assertTrue(oom_man.lookupDocumentIRI(oom2, ontIRI).equals(docIRI)) # Attempting to add a mapping that already exists should not raise an # exception. oom_man.addNewIRIMapping(ontIRI, docIRI) # Attempting to add a conflicting mapping should raise an exception. with self.assertRaisesRegexp( RuntimeError, 'conflicting mapping of .* already exists'): oom_man.addNewIRIMapping(ontIRI, IRI.create('http://conflicting.iri'))
def test_mergeOntology(self): mergeiri_str = 'https://github.com/stuckyb/ontopilot/raw/master/python-src/test/test_data/ontology-import.owl' mergeIRI = IRI.create(mergeiri_str) mergeclassiri_str = 'http://purl.obolibrary.org/obo/OBITO_0001' mergeclassIRI = IRI.create(mergeclassiri_str) mergeclass = self.ont.df.getOWLClass(mergeclassIRI) # Verify that the source IRI is in the target ontology's imports list # and that the class defined in the source ontology is not in the # target ontology. self.assertTrue( self.owlont.getDirectImportsDocuments().contains(mergeIRI)) self.assertFalse( self.owlont.isDeclared(mergeclass, ImportsEnum.EXCLUDED)) # Merge the axioms from the source ontology. self.ont.mergeOntology(mergeiri_str) # Verify that the source IRI is *not* in the target ontology's imports # list and that the class defined in the source ontology *is* in the # target ontology. self.assertFalse( self.owlont.getDirectImportsDocuments().contains(mergeIRI)) self.assertTrue( self.owlont.isDeclared(mergeclass, ImportsEnum.EXCLUDED))
def test_excludeTypes(self): """ Tests the functionality of specifying classes to exclude from inferred class/type assertions. The tests in this method confirm that 1) inferred axioms about excluded classes are deleted; 2) explicit axioms about excluded classes are *not* deleted; 3) inferred axioms about non-excluded classes are not deleted; and 4) explicit axioms about non-excluded classes are not deleted. """ # Create a new class that is a subclass of OBTO:0010 and create an # individual of the new class. This is to test that inferred type # assertions that should *not* be excluded are preserved. newclass = self.ont.createNewClass('OBTO:0013') newclass.addSuperclass('OBTO:0010') newindv = self.ont.createNewIndividual('OBTO:8002') newindv.addType('OBTO:0013') self.iaa.loadExcludedTypes('test_data/excluded_types.csv') # Run the reasoner. inftypes = ['subclasses', 'types', 'disjoint classes'] self.iaa.addInferredAxioms(inftypes) #self.ont.saveOntology('test_inferred-2.owl') # Individual OBTO:8000 should only have OBTO:0011 as its type. # (OBTO:0011 is excluded in the CSV file, but in this case the type # assertion is not inferred so it should remain.) indv = self.ont.getExistingIndividual('OBTO:8000').getOWLAPIObj() axioms = self.owlont.getClassAssertionAxioms(indv) self.assertEqual(1, axioms.size()) typeclass = axioms.iterator().next().getClassExpression().asOWLClass() self.assertTrue(typeclass.getIRI().equals( IRI.create('http://purl.obolibrary.org/obo/OBTO_0011'))) # Individual OBTO:8001 should only have OBTO:0010 as its type. indv = self.ont.getExistingIndividual('OBTO:8001').getOWLAPIObj() axioms = self.owlont.getClassAssertionAxioms(indv) self.assertEqual(1, axioms.size()) typeclass = axioms.iterator().next().getClassExpression().asOWLClass() self.assertTrue(typeclass.getIRI().equals( IRI.create('http://purl.obolibrary.org/obo/OBTO_0010'))) # Individual OBTO:8002 should have OBTO:0013 (explicit) and OBTO:0010 # (inferred) as its types. expectedIRIs = { IRI.create('http://purl.obolibrary.org/obo/OBTO_0010'), IRI.create('http://purl.obolibrary.org/obo/OBTO_0013') } axioms = self.owlont.getClassAssertionAxioms(newindv.getOWLAPIObj()) self.assertEqual(2, axioms.size()) typeIRIs = set( [ax.getClassExpression().asOWLClass().getIRI() for ax in axioms]) self.assertEqual(expectedIRIs, typeIRIs)
def test_resolveIdentifier(self): expIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0001') # Test identifier inputs that include: plain label, OBO prefix label, # IRI prefix label, full IRI string, prefix IRI string, OBO ID, and OWL # API IRI object. testvals = [ "'test object property 1'", "OBTO:'test object property 1'", "obo:'test object property 1'", 'http://purl.obolibrary.org/obo/OBTO_0001', 'obo:OBTO_0001', 'OBTO:0001', IRI.create('http://purl.obolibrary.org/obo/OBTO_0001') ] for testval in testvals: self.assertTrue(expIRI.equals(self.ir.resolveIdentifier(testval)))
def expandIRI(self, iri): """ Expands an IRI string into a full IRI and returns a corresponding OWL API IRI object. Also accepts OWL API IRI objects, in which case they are returned unaltered. IRI strings can be either full IRIs, prefix IRIs (i.e. curies, such as "owl:Thing"), or relative IRIs (e.g., "term_name"). If the IRI string is a prefix IRI or relative IRI, it will be expanded using the prefixes or base defined in the ontology. If the string is not a prefix IRI or relative IRI, then it is assumed to be a full IRI. iri: The IRI to expand. Can be either a string or an OWL API IRI object. In the latter case, iri is returned as is. """ if isinstance(iri, basestring): # Verify that we have a valid IRI string. if rfc3987.match(iri, rule='IRI_reference') is None: raise RuntimeError('Invalid IRI string: "' + iri + '".') try: # If iri is not a prefix IRI, the OWL API will throw an # OWLRuntimeException. fullIRI = self.prefix_df.getIRI(iri) except OWLRuntimeException: fullIRI = IRI.create(iri) elif isinstance(iri, IRI): fullIRI = iri else: raise RuntimeError('Unsupported type for conversion to IRI.') return fullIRI
def test_addAnnotation(self): annotprop_iri = IRI.create('http://purl.obolibrary.org/obo/OBTO_0030') annot_txt = 'Test annotation text.' self.t_ent.addAnnotation(annotprop_iri, annot_txt) self._checkAnnotation(annotprop_iri, annot_txt)
def _test_make_imports(self, tppath): """ Tests the "make imports" build task. tppath: The path of the test OntoPilot project. """ args = [self.execpath, 'make', 'imports'] retval = subprocess.call(args, cwd=tppath) self.assertEqual(0, retval) # Check that the compiled import module exists. im_path = os.path.join(tppath, 'imports/bfo_test_import_module.owl') self.assertTrue(os.path.isfile(im_path)) im_ont = Ontology(im_path) owlont = im_ont.getOWLOntology() # Confirm that it contains the single imported class. self.assertIsNotNone(im_ont.getExistingClass("'continuant'")) self.assertEqual( 1, owlont.getClassesInSignature(Imports.EXCLUDED).size() ) # Confirm that it contains the proper source annotation. bfo_verIRI = IRI.create('http://purl.obolibrary.org/obo/bfo/2.0/bfo.owl') found_src_annot = False for ont_annot in owlont.getAnnotations(): if ont_annot.getProperty().getIRI().equals(Ontology.SOURCE_ANNOT_IRI): if ont_annot.getValue().asIRI().isPresent(): sourceIRI = ont_annot.getValue().asIRI().get() if sourceIRI.equals(bfo_verIRI): found_src_annot = True self.assertTrue(found_src_annot)
def _checkGenericAxioms(self, entity): """ Checks the values of all entity axioms that are shared among all entity types. """ # Check that the label is correct. self.assertEqual( [self.tr['Label']], entity.getAnnotationValues(LABEL_IRI) ) # Check the definition. self.assertEqual( [self.tr['Text definition']], entity.getAnnotationValues(entity.DEFINITION_IRI) ) # Check the comments. self.assertEqual( sorted(['The first comment.', 'The second; comment.']), sorted(entity.getAnnotationValues(COMMENT_IRI)) ) # Check the annotations. annotpropIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0030') self.assertEqual( sorted([ 'Annotation text 1.', 'Annotation text 2.', 'Annotation text 3.' ]), sorted(entity.getAnnotationValues(annotpropIRI)) )
def save_brain_as_ofn(brain, path): """ Wot it sez on' tin. brain = Brain object path = *FULL* path to file (relative paths not allowed). """ ofn = OWLFunctionalSyntaxOntologyFormat() brain.manager.saveOntology(brain.getOntology(), ofn, IRI.create(path))
def create_relation(s): if s == "part-of": istring = "http://purl.obolibrary.org/obo/BFO_0000050" elif s == "has-part": istring = "http://purl.obolibrary.org/obo/BFO_0000051" else: istring = "http://phenomebrowser.net/#"+s factory.getOWLObjectProperty(IRI.create(istring))
def get_sc_axioms_on_class(self, iri_string = '', sfid =''): """Returns an interable set of asserted superclasses of class.""" c = '' if sfid: c = self.bi_sfp.getEntity(sfid) else: iri = IRI.create(iri_string) c = self.factory.getOWLClass(iri) return c.getSuperClasses(self.ont)#?
def get_sc_axioms_on_class(self, iri_string='', sfid=''): """Returns an interable set of asserted superclasses of class.""" c = '' if sfid: c = self.bi_sfp.getEntity(sfid) else: iri = IRI.create(iri_string) c = self.factory.getOWLClass(iri) return c.getSuperClasses(self.ont) #?
def save_brain_as_ofn(brain, path): """ Wot it sez on' tin. brain = Brain object path = *FULL* path to file (relative paths not allowed). """ ofn = OWLFunctionalSyntaxOntologyFormat() brain.manager.saveOntology(brain.getOntology(), ofn, IRI.create("file://" + path))
def test_expandIRI(self): expIRI = IRI.create('http://www.w3.org/2000/01/rdf-schema#label') # Test full IRIs, prefix IRIs, and IRI objects. testvals = [ 'http://www.w3.org/2000/01/rdf-schema#label', 'rdfs:label', IRI.create('http://www.w3.org/2000/01/rdf-schema#label') ] for testval in testvals: self.assertTrue(expIRI.equals(self.ir.expandIRI(testval))) # Also test a relative IRI. expIRI = IRI.create( 'https://github.com/stuckyb/ontopilot/raw/master/python-src/test/test_data/ontology.owl#blah' ) self.assertTrue(expIRI.equals(self.ir.expandIRI('blah'))) # Make sure invalid IRI strings are detected. with self.assertRaisesRegexp(RuntimeError, 'Invalid IRI string'): self.ir.expandIRI('BL\nAH')
def test_addImport(self): importIRI = IRI.create('file:/local/path/ont.owl') # Verify that the import is not yet included in the ontology. self.assertFalse( self.owlont.getDirectImportsDocuments().contains(importIRI)) self.ont.addImport(importIRI, False) # Verify that the import declaration was added. self.assertTrue( self.owlont.getDirectImportsDocuments().contains(importIRI))
def test_lookupDocumentIRI(self): oom = OWLManager.createOWLOntologyManager() # Test two mappings to make sure multiple mappings work as expected. testvals = [{ 'ontIRI': IRI.create('http://some.ontology.iri/path'), 'docIRI': IRI.create('http://some.doc.iri/path') }, { 'ontIRI': IRI.create('http://2nd.ontology.iri/path'), 'docIRI': IRI.create('http://2nd.doc.iri/path') }] for testval in testvals: self.assertIsNone(oom_man.lookupDocumentIRI( oom, testval['ontIRI'])) oom.getIRIMappers().add( SimpleIRIMapper(testval['ontIRI'], testval['docIRI'])) self.assertTrue( oom_man.lookupDocumentIRI(oom, testval['ontIRI']).equals( testval['docIRI']))
def __init__(self, args, cfgfile_required=True, config=None): """ args: A "struct" of configuration options (typically, parsed command-line arguments). The only required member is 'config_file' (string). cfgfile_required (optional): Whether a config file is required. config (optional): An OntoConfig object. """ BuildTargetWithConfig.__init__(self, args, cfgfile_required, config) self.addDependency(BuildDirTarget(args, False, self.config)) # The string builddir is the path to a build directory where, at a # minimum, source ontologies can be cached. If we are doing an # out-of-source build, this will also be the location for the compiled # imports modules, specified by outputdir. For in-source builds, # outputdir will be the final destination for the imports modules. self.builddir = self.config.getBuildDir() if self.config.getDoInSourceBuilds(): self.outputdir = self.config.getImportsDir() else: self.outputdir = self.builddir self._checkFiles() self._readImportsSource() # Initialize the ImportModuleBuilder. self.mbuilder = ImportModuleBuilder(self.config.getImportsDevBaseIRI(), self.config.getImportModSuffix(), self.builddir, self.outputdir) # Update the IRI mappings for the import modules so that the local # files are loaded instead of the versions at the remote IRIs. for modinfo in self.getImportsInfo(): if modinfo.filename != '': doc_iristr = urlparse.urljoin( 'file://localhost', urllib.pathname2url(modinfo.filename)) oom_manager.addNewIRIMapping(IRI.create(modinfo.iristr), IRI.create(doc_iristr))
def get_types_for_ind(iri_string, ont): """iri_string: An iri string referencing an individual ont: An owlAPI ontology object for an ontology that includes the referenced individual. Returns and iterable set of class expressions """ # Could refactor to generic get owl entity fn. iri = IRI.create(iri_string) manager = OWLManager.createOWLOntologyManager() factory = manager.getOWLDataFactory() i = factory.getOWLNamedIndividual(iri) return i.getTypes(ont) #
def test_addEquivalentTo(self): equivclassIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0010') # Test a simple class expression that is only a class label. self.t_ent.addEquivalentTo("'test class 1'") # Check that the class has the correct equivalency relationship. found_eqclass = False for axiom in self.owlont.getEquivalentClassesAxioms(self.t_owlapiobj): for eqclass in axiom.getNamedClasses(): if eqclass.getIRI().equals(equivclassIRI): found_eqclass = True self.assertTrue(found_eqclass)
def test_resolveNonlabelIdentifier(self): # Test that a non-label identifier resolves as expected. self.assertEqual( 'http://purl.obolibrary.org/obo/OBTO_0001', str(self.ir.resolveNonlabelIdentifier('obo:OBTO_0001'))) # Test that an OWL API object works correctly. iriobj = IRI.create('http://purl.obolibrary.org/obo/OBTO_0001') self.assertEqual(str(iriobj), str(self.ir.resolveNonlabelIdentifier(iriobj))) # Test that a label throws an exception. with self.assertRaisesRegexp(RuntimeError, 'labels are not allowed'): self.ir.resolveNonlabelIdentifier("obo:'test object property 1'")
def test_addSuperproperty(self): newpropIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0032') newprop = self.test_ont.createNewAnnotationProperty(newpropIRI) self.t_ent.addSuperproperty('http://purl.obolibrary.org/obo/OBTO_0032') # Check that the property has the correct superproperty. found_prop = False for axiom in self.owlont.getSubAnnotationPropertyOfAxioms( self.t_owlapiobj): if axiom.getSuperProperty().getIRI().equals(newpropIRI): found_prop = True self.assertTrue(found_prop)
def test_addSubclass(self): subclassIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0010') # Test a simple class expression that is only a class label. self.t_ent.addSubclass("'test class 1'") # Check that the class has the correct subclass. found_subclass = False for axiom in self.owlont.getSubClassAxiomsForSuperClass( self.t_owlapiobj): if axiom.getSubClass().getIRI().equals(subclassIRI): found_subclass = True self.assertTrue(found_subclass)
def test_addDomain(self): classIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0010') self.t_ent.addDomain('http://purl.obolibrary.org/obo/OBTO_0010') # Check that the property has the correct domain. found_class = False for axiom in self.owlont.getDataPropertyDomainAxioms(self.t_owlapiobj): cl_exp = axiom.getDomain() if not (cl_exp.isAnonymous()): if cl_exp.asOWLClass().getIRI().equals(classIRI): found_class = True self.assertTrue(found_class)
def test_addInverse(self): newpropIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0003') newprop = self.test_ont.createNewObjectProperty(newpropIRI) self.t_ent.addInverse('http://purl.obolibrary.org/obo/OBTO_0003') # Check that the property has the correct inverse. found_prop = False for axiom in self.owlont.getInverseObjectPropertyAxioms( self.t_owlapiobj): for dprop in axiom.getProperties(): if dprop.getIRI().equals(newpropIRI): found_prop = True self.assertTrue(found_prop)
def test_addDisjointWith(self): classIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0010') # Test a simple class expression that is only a class label. self.t_ent.addDisjointWith("'test class 1'") # Check that the class has the correct disjointness relationship. found_class = False for axiom in self.owlont.getDisjointClassesAxioms(self.t_owlapiobj): for cl_exp in axiom.getClassExpressions(): if not (cl_exp.isAnonymous()): if cl_exp.asOWLClass().getIRI().equals(classIRI): found_class = True self.assertTrue(found_class)
def test_addEquivalentTo(self): newpropIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0022') newprop = self.test_ont.createNewDataProperty(newpropIRI) self.t_ent.addEquivalentTo('http://purl.obolibrary.org/obo/OBTO_0022') # Check that the property has the correct equivalency relationship. found_prop = False for axiom in self.owlont.getEquivalentDataPropertiesAxioms( self.t_owlapiobj): for dprop in axiom.getProperties(): if dprop.getIRI().equals(newpropIRI): found_prop = True self.assertTrue(found_prop)
def test_removeEntity(self): classobj = self.ont.getExistingClass(CLASS_IRI) self.assertIsNotNone(classobj) # First, delete the class but not its annotations. self.ont.removeEntity(classobj.getOWLAPIObj(), False) # Make sure the class has been deleted. self.assertIsNone(self.ont.getExistingClass(CLASS_IRI)) # Make sure annotations for the target entity have not been deleted. IRIobj = IRI.create(CLASS_IRI) annot_ax_set = self.owlont.getAnnotationAssertionAxioms(IRIobj) self.assertEqual(2, annot_ax_set.size()) # Run the deletion command again, this time deleting annotations. # Also, this time, use the _OntologyEntity object directly instead of # the OWL API object to make sure entity deletion works either way. self.ont.removeEntity(classobj, True) # Make sure annotations for the target entity have been deleted. IRIobj = IRI.create(CLASS_IRI) annot_ax_set = self.owlont.getAnnotationAssertionAxioms(IRIobj) self.assertTrue(annot_ax_set.isEmpty())
def test_addDisjointWith(self): newpropIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0003') newprop = self.test_ont.createNewObjectProperty(newpropIRI) self.t_ent.addDisjointWith('http://purl.obolibrary.org/obo/OBTO_0003') # Check that the property has the correct disjointness relationship. found_prop = False for axiom in self.owlont.getDisjointObjectPropertiesAxioms( self.t_owlapiobj): for dprop in axiom.getProperties(): if dprop.getIRI().equals(newpropIRI): found_prop = True self.assertTrue(found_prop)
def oboIDToIRI(oboID): """ Converts an OBO ID string (i.e., a string of the form "PO:0000003") to an IRI. """ oboID = oboID.strip() if not(isOboID(oboID)): raise OBOIdentifierError( 'The string "{0}" is not a valid OBO ID, so it cannot be ' 'converted to an OBO Foundry IRI.'.format(oboID) ) tIRI = IRI.create(OBO_BASE_IRI + oboID.replace(':', '_')) return tIRI
def test_addType(self): classIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0010') # Verify that the individual does not yet have any type information. axioms = self.owlont.getClassAssertionAxioms(self.t_owlapiobj) self.assertTrue(axioms.isEmpty()) # Add a type for the individual, using a simple class expression that # is only a class label. self.t_ent.addType("'test class 1'") # Check that the individual has the correct type. axioms = self.owlont.getClassAssertionAxioms(self.t_owlapiobj) self.assertEqual(1, axioms.size()) found_typeclass = False for axiom in axioms: classexp = axiom.getClassExpression() if not (classexp.isAnonymous()): if classexp.asOWLClass().getIRI().equals(classIRI): found_typeclass = True self.assertTrue(found_typeclass)
def __init__(self, ont='', file_path='', uri=''): self.manager = OWLManager.createOWLOntologyManager() if not ont: if file_path: self.ont = self.manager.loadOntologyFromOntologyDocument( File(file_path)) elif uri: self.ont = self.manager.loadOntologyFromIRI( IRI.create(uri)) # check this! else: warnings.warn("Constructor failed. Empty args") else: self.ont = ont self.factory = self.manager.getOWLDataFactory() self.simple_sfp = SimpleShortFormProvider() # .getShortForm(iri) self.simple_iri_sfp = SimpleIRIShortFormProvider() ontset = TreeSet() ontset.add(self.ont) #public BidirectionalShortFormProviderAdapter(OWLOntologyManager man, #java.util.Set<OWLOntology> ontologies, #ShortFormProvider shortFormProvider) # Providing the manager, means that this listens for changes. self.bi_sfp = BidirectionalShortFormProviderAdapter( self.manager, ontset, self.simple_sfp) # .getShortForm(iri); .getEntity()
def create_relation(s): return factory.getOWLObjectProperty(IRI.create("http://phenomebrowser.net/#" + s))
def prefixUrls(s): for prefix in PREFIX_MAP.keys(): if(s.startsWith(prefix)): s = s.replace(prefix, PREFIX_MAP[prefix]) return s file = "/home/mencella/borg/owl_files/swissprot.owl" gofile = "/home/mencella/borg/owl_files/go.owl" ncbifile = "/home/mencella/borg/owl_files/ncbitaxon.owl" manager = OWLManager.createOWLOntologyManager() factory = manager.getOWLDataFactory() print "Loading OWL files..." ontology = manager.loadOntologyFromOntologyDocument(IRI.create("file:" + file)) go = manager.loadOntologyFromOntologyDocument(IRI.create("file:" + gofile)) ncbi = manager.loadOntologyFromOntologyDocument(IRI.create("file:" + ncbifile)) print "... OWL files loaded." def create_relation(s): return factory.getOWLObjectProperty(IRI.create("http://phenomebrowser.net/#" + s)) def create_class(s): return factory.getOWLClass(IRI.create(s)) stats = { 'rBoxAxiomCount': 0, 'tBoxAxiomCount': 0,
# Choose file directories here input_directory = "/home/mencella/borg/swissprot_ttls/" output_directory = "/home/mencella/borg/swissprot.owl" go = "/home/mencella/borg/owl_files/go.owl" ncbi = "/home/mencella/borg/owl_files/ncbitaxon.owl" onturi = "http://aber-owl.net/uniprot.owl#" up = "http://purl.uniprot.org/core/" manager = OWLManager.createOWLOntologyManager() factory = manager.getOWLDataFactory() # Load GO and NCBI print "Loading ontologies..." manager.loadOntologyFromOntologyDocument(IRI.create("file:" + go)) manager.loadOntologyFromOntologyDocument(IRI.create("file:" + ncbi)) print "... ontologies loaded. Merging..." merger = OWLOntologyMerger(manager) ontology = merger.createMergedOntology(manager, IRI.create("http://aber-owl.net/uniprot.owl")) print "... merged." # This line for without GO or NCBITaxon # ontology = manager.createOntology(IRI.create("http://aber-owl.net/uniprot.owl")) # Imports # config = OWLOntologyLoaderConfiguration() # # importDeclaraton = factory.getOWLImportsDeclaration(IRI.create("http://purl.obolibrary.org/obo/go.owl")); # manager.applyChange(AddImport(ontology, importDeclaraton)) # manager.makeLoadImportRequest(importDeclaraton, config)
def get_ind_from_iri(self, iri_string): iri = IRI.create(iri_string) return self.factory.getOWLNamedIndividual(iri)
def get_axioms_ref_class(self, iri_string): """Returns all axioms referencing a class.""" iri = IRI.create(iri_string) c = self.factory.getOWLClass(iri) return c.getReferencingAxioms(self.ont)#?
def get_axioms_ref_class(self, iri_string): """Returns all axioms referencing a class.""" iri = IRI.create(iri_string) c = self.factory.getOWLClass(iri) return c.getReferencingAxioms(self.ont) #?
def create_class(s): factory.getOWLClass(IRI.create(s))
def test_addInferredAxioms(self): """ This does not attempt to exhaustively test every available type of inference. Instead, it only tests the most commonly used inference types when generating inferred axioms for an ontology: class hierarchy, individual types, and class disjointness. Furthermore, the tests implemented here are also designed to test important supporting algorithms, including identifying and removing duplicate axioms, finding and removing redundant subclass axioms, and removing trivial axioms. Actually generating the inferred axioms is the responsibility of OWL API objects, so this approach to testing should be reasonable. """ testclassIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0012') testclass = self.ont.df.getOWLClass(testclassIRI) parentIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0010') grandparentIRI = IRI.create( 'http://purl.obolibrary.org/obo/OBITO_0001') individualIRI = IRI.create(INDIVIDUAL_IRI) individual = self.ont.df.getOWLNamedIndividual(individualIRI) # Prior to running the reasoner, OBTO_0012 should only have OBITO_0001 # as an asserted superclass. axioms = self.owlont.getSubClassAxiomsForSubClass(testclass) self.assertEqual(1, axioms.size()) superclass = axioms.iterator().next().getSuperClass().asOWLClass() self.assertTrue(superclass.getIRI().equals(grandparentIRI)) # Individual 'test individual 2' should only have OBTO_0010 as its # type. axioms = self.owlont.getClassAssertionAxioms(individual) self.assertEqual(1, axioms.size()) typeclass = axioms.iterator().next().getClassExpression().asOWLClass() self.assertTrue(typeclass.getIRI().equals(parentIRI)) # Class OBTO_0012 should not have any disjointness axioms. self.assertTrue( self.owlont.getDisjointClassesAxioms(testclass).isEmpty()) # Run the reasoner. inftypes = ['subclasses', 'types', 'disjoint classes'] self.iaa.addInferredAxioms(inftypes) #self.ont.saveOntology('test_inferred.owl') # Make sure that there are no trivial axioms in the ontology (e.g., # axioms that involve owl:Thing). self.assertFalse( self.owlont.containsEntityInSignature(self.ont.df.getOWLThing())) # After running the reasoner and removing redundant "subclass of" # axioms, OBTO_0012 should only have OBTO_0010 as an asserted # superclass. axioms = self.owlont.getSubClassAxiomsForSubClass(testclass) self.assertEqual(1, axioms.size()) superclass = axioms.iterator().next().getSuperClass().asOWLClass() self.assertTrue(superclass.getIRI().equals(parentIRI)) # Individual 'test individual 2' should now have OBTO_0010, OBTO_0012, # and OBITO_0001 as its types. axioms = self.owlont.getClassAssertionAxioms(individual) self.assertEqual(3, axioms.size()) expected_typeiri_strs = { testclassIRI.toString(), parentIRI.toString(), grandparentIRI.toString() } typeiri_strs = set() for axiom in axioms: typeiri_strs.add( axiom.getClassExpression().asOWLClass().getIRI().toString()) self.assertEqual(expected_typeiri_strs, typeiri_strs) # Class OBTO_0012 should now be disjoint with OBTO_0011. disjointIRI = IRI.create('http://purl.obolibrary.org/obo/OBTO_0011') disjointclass = self.ont.df.getOWLClass(disjointIRI) axioms = self.owlont.getDisjointClassesAxioms(testclass) self.assertEqual(1, axioms.size()) self.assertTrue( axioms.iterator().next().containsEntityInSignature(disjointclass))
from java.io import FileReader import os from org.apache.jena.rdf.model import ModelFactory from org.apache.jena.vocabulary import RDF from org.semanticweb.owlapi.apibinding import OWLManager from org.semanticweb.owlapi.model import IRI from org.semanticweb.owlapi.vocab import OWLRDFVocabulary up = "http://purl.uniprot.org/core/" manager = OWLManager.createOWLOntologyManager() factory = manager.getOWLDataFactory() # mreader = JenaReader() ontology = manager.createOntology(IRI.create("http://aber-owl.net/uniprot.owl")) onturi = "http://aber-owl.net/uniprot.owl#" def create_relation(s): if s == "part-of": istring = "http://purl.obolibrary.org/obo/BFO_0000050" elif s == "has-part": istring = "http://purl.obolibrary.org/obo/BFO_0000051" else: istring = "http://phenomebrowser.net/#"+s factory.getOWLObjectProperty(IRI.create(istring)) def create_class(s): factory.getOWLClass(IRI.create(s)) def add_anno(resource, prop, cont):