user1298416
user1298416

Reputation: 341

OWL API - setting prefixes and ontology annotations (properties) of the new ontology

I work with an ontology, that has the following "header":

@prefix : <http://purl.obolibrary.org/obo/doid.owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix doid: <http://purl.obolibrary.org/obo/doid#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix obo: <http://purl.obolibrary.org/obo/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix fn: <http://www.w3.org/2005/xpath-functions#> .
@prefix sesame: <http://www.openrdf.org/schema/sesame#> .

obo:doid.owl a owl:Ontology ;
    oboInOwl:hasOBOFormatVersion "1.2"^^xsd:string ;
    oboInOwl:date "25:03:2016 16:27"^^xsd:string ;
    oboInOwl:auto-generated-by "OBO-Edit 2.3.1"^^xsd:string ;
    rdfs:comment "This work is licensed under a Creative Commons Attribution 3.0 Unported License http://creativecommons.org/licenses/by/3.0/."^^xsd:string ;
    oboInOwl:default-namespace "disease_ontology"^^xsd:string ;
    oboInOwl:saved-by "someone"^^xsd:string ;
    owl:versionIRI <http://purl.obolibrary.org/obo/doid/releases/2016-03-25/doid.owl> .

I'm merging it with another ontology. The merged file has the following header:

@prefix : <file:/Users/Downloads/merged.ttl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <file:/Users/Downloads/merged.ttl> .

<file:/Users/Downloads/merged.ttl> rdf:type owl:Ontology .

That's it. I would like to add back some of the properties, like oboInOwl:hasOBOFormatVersion, rdfs:comment, oboInOwl:default-namespace, oboInOwl:saved-by etc. How can I do this? Thank you.

Here is the merge code:

OWLOntology ontology2 = CalcDiff.getOntology2();

File output2 = new File("/ontology_management/DOID_MERGED/doid_do.ttl");

IRI mergedOntologyIRI = IRI.create(output2);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

OWLOntology ontology1 = manager.loadOntologyFromOntologyDocument(new 
File("/ontology_management/DOID_CURRENT/doid.ttl"));

OWLDocumentFormat format = manager.getOntologyFormat(ontology1);

// save the merged ontology in Turtle format
TurtleDocumentFormat newFormat = new TurtleDocumentFormat();

// will copy the prefixes over so that we have nicely abbreviated IRIs
// in the new ontology document
if (format.isPrefixOWLOntologyFormat()) {
      newFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
}

//Using HashSet to avoid duplicated entries
Set<OWLAxiom> axiomsall = new HashSet<OWLAxiom>();
Set<OWLImportsDeclaration> importsall = new HashSet<OWLImportsDeclaration>();

try {    
    ontology1.getAxioms().forEach(c -> {axiomsall.add(c);});
    ontology1.getImportsDeclarations().forEach(c -> {importsall.add(c);});
    ontology2.getAxioms().forEach(c -> {axiomsall.add(c);});
    ontology2.getImportsDeclarations().forEach(c -> {importsall.add(c);});

    mergedOntology = manager.createOntology(mergedOntologyIRI);

    manager.addAxioms(mergedOntology, axiomsall);

} catch (OWLOntologyCreationException e) {
    e.printStackTrace();
} 
//Adding the import declarations
for(OWLImportsDeclaration decl : importsall){
    manager.applyChange(new AddImport(mergedOntology, decl));
}

Ok, the code above got me to this point:

@prefix : <http://purl.obolibrary.org/obo/doid.owl#> .
@prefix fn: <http://www.w3.org/2005/xpath-functions#> .
@prefix obo: <http://purl.obolibrary.org/obo/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix doid: <http://purl.obolibrary.org/obo/doid#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sesame: <http://www.openrdf.org/schema/sesame#> .
@prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> .
@base <file:/ontology_management/DOID_MERGED/doid_do.ttl> .

<file:/ontology_management/DOID_MERGED/doid_do.ttl> rdf:type owl:Ontology .

So now I have all prefixes, but the Ontology's properties are not there (that is what I'm mostly interested in)

Upvotes: 0

Views: 945

Answers (1)

UninformedUser
UninformedUser

Reputation: 8465

What you mean isn't called "properties" but prefix declarations. And next time, it would be good to provide with some code how you load/save the ontology:

OWLOntologyFormat format = manager.getOntologyFormat(YOUR_FIRST_ONTOLOGY);

// save the merged ontology in RDF/XML format
RDFXMLDocumentFormat newFormat = new RDFXMLDocumentFormat();

// will copy the prefixes over so that we have nicely abbreviated IRIs
// in the new ontology document
if (format.isPrefixOWLOntologyFormat()) {
    newFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
}
manager.saveOntology(YOUR_MERGED_ONTOLOGY, newFormat, IRI.create(file.toURI()));

Copying ontology annotations:

manager.applyChanges(
                    ontology1.getAnnotations().stream()
                            .map(an -> new AddOntologyAnnotation(mergedOntology, an))
                            .collect(Collectors.toList()));

Regarding merging of ontologies, it's much easier to use the existing methods:

Set<OWLOntology> ontologies = ...

OWLOntology mergedOntology = manager.createOntology(mergedOntologyIRI, ontologies, false);

or just use the class org.semanticweb.owlapi.util.OWLOntologyMerger ...

Upvotes: 4

Related Questions