Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and copy Ontology IRI of parsed ontologies #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
jvendetti marked this conversation as resolved.
Show resolved Hide resolved

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());
}


jvendetti marked this conversation as resolved.
Show resolved Hide resolved
@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