Skip to content

Commit

Permalink
Merge pull request #17 from ontoportal-lirmm/pr/feature/add-ontology-…
Browse files Browse the repository at this point in the history
…iri-triple

Save and copy Ontology IRI of  parsed ontologies
  • Loading branch information
jvendetti authored Sep 2, 2022
2 parents b70dbcb + 9e8f08d commit 002e608
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/main/java/org/stanford/ncbo/owlapi/wrapper/OntologyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

public class OntologyParser {
private final static Logger log = LoggerFactory.getLogger(OntologyParser.class.getName());
public static final String VERSION_SUBJECT = "http://bioportal.bioontology.org/ontologies/versionSubject";

protected ParserInvocation parserInvocation = null;
private ParserLog parserLog = null;
Expand All @@ -53,6 +54,12 @@ public OntologyParser(ParserInvocation parserInvocation) throws OntologyParserEx
this.targetOwlManager = OWLManager.createOWLOntologyManager();
}


public OWLOntology getTargetOwlOntology() {
return targetOwlOntology;
}


public List<OntologyBean> getLocalOntologies() {
return ontologies;
}
Expand Down Expand Up @@ -135,6 +142,21 @@ private boolean isOBO(OWLOntology ontology) {
return isOBO;
}

/**
* Get the source ontology IRI and add it to the target ontology
*
* @param sourceOnt
*/
private void addOntologyIRI(OWLOntology sourceOnt) {
Optional<IRI> ontologyIRI = sourceOnt.getOntologyID().getOntologyIRI();
Optional<IRI> versionIRI = sourceOnt.getOntologyID().getVersionIRI();
if (ontologyIRI.isPresent()) {
OWLOntologyID newOntologyID = new OWLOntologyID(ontologyIRI, versionIRI);
SetOntologyID setOntologyID = new SetOntologyID(targetOwlOntology, newOntologyID);
this.targetOwlManager.applyChange(setOntologyID);
}
}

/**
* Copies ontology-level annotation axioms from the source ontology to the target ontology.
* <p>
Expand Down Expand Up @@ -162,21 +184,22 @@ private void addGroundMetadata(IRI documentIRI, OWLDataFactory factory, OWLOntol
targetOwlManager.addAxiom(targetOwlOntology, annotationAssertionAxiom);

if (isFile && (annotationProperty.toString().contains("versionInfo"))) {
IRI versionSubjectIRI = IRI.create("http://bioportal.bioontology.org/ontologies/versionSubject");
IRI versionSubjectIRI = IRI.create(VERSION_SUBJECT);
OWLAnnotationProperty versionAnnotationProperty = factory.getOWLAnnotationProperty(OWLRDFVocabulary.OWL_VERSION_INFO.getIRI());
OWLAnnotationAssertionAxiom versionAnnotationAssertionAxiom = factory.getOWLAnnotationAssertionAxiom(versionAnnotationProperty, versionSubjectIRI, annotationValue);
targetOwlManager.addAxiom(targetOwlOntology, versionAnnotationAssertionAxiom);
}
}
}

private boolean buildOWLOntology(boolean isOBO) {
private boolean buildOWLOntology(OWLOntology masterOntology, boolean isOBO) {

Set<OWLAxiom> allAxioms = new HashSet<OWLAxiom>();

OWLDataFactory fact = sourceOwlManager.getOWLDataFactory();
try {
targetOwlOntology = targetOwlManager.createOntology();
addOntologyIRI(masterOntology);
} catch (OWLOntologyCreationException e) {
log.error(e.getMessage());
parserLog.addError(ParserError.OWL_CREATE_ONTOLOGY_EXCEPTION, "Error buildOWLOntology" + e.getMessage());
Expand Down Expand Up @@ -213,7 +236,7 @@ private boolean buildOWLOntology(boolean isOBO) {
if (oboVersion != null) {
log.info("Adding version: {}", oboVersion);
OWLAnnotationProperty prop = fact.getOWLAnnotationProperty(IRI.create(OWLRDFVocabulary.OWL_VERSION_INFO.toString()));
IRI versionSubjectIRI = IRI.create("http://bioportal.bioontology.org/ontologies/versionSubject");
IRI versionSubjectIRI = IRI.create(VERSION_SUBJECT);
OWLAnnotationAssertionAxiom annVersion = fact.getOWLAnnotationAssertionAxiom(prop, versionSubjectIRI, fact.getOWLLiteral(oboVersion));
targetOwlManager.addAxiom(targetOwlOntology, annVersion);
}
Expand Down Expand Up @@ -525,7 +548,7 @@ private boolean internalParse() {

boolean isOBO = isOBO(ontology);

if (!buildOWLOntology(isOBO)) return false;
if (!buildOWLOntology(ontology, isOBO)) return false;

if (!serializeOntology()) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.semanticweb.owlapi.model.IRI;

import java.io.File;

Expand Down Expand Up @@ -125,6 +126,25 @@ public void parse_OntologyWithEmbeddedXML_ReturnsTrue() throws Exception {
assertTrue(parser.parse());
}


@Test
public void parse_DetectsOntologyIRI_ReturnsTrue() throws Exception {
String outputRepositoryFolder = "./src/test/resources/repo/output/cno";
ParserInvocation pi = new ParserInvocation("./src/test/resources/repo/input/cno",
outputRepositoryFolder, "cnov0_5.owl", true);
assertTrue(pi.valid());

OntologyParser parser = new OntologyParser(pi);
assertTrue(parser.parse());
assertEquals(1, parser.getLocalOntologies().size());

IRI targetIRI = parser.getTargetOwlOntology().getOntologyID().getOntologyIRI().orNull();
IRI sourceIRI = parser.getParsedOntologies().stream().findFirst().get().getOntologyID().getOntologyIRI().orNull();
assertNotNull(targetIRI);
assertEquals(sourceIRI, targetIRI);

}

@After
public void tearDown() throws Exception {

Expand Down

0 comments on commit 002e608

Please sign in to comment.