Skip to content

Commit c6f923a

Browse files
authored
Merge pull request #28 from ncbo/ontoportal-lirmm/bugfix/ontology-level-annotations
Ontoportal lirmm/bugfix/ontology level annotations
2 parents 419489d + ec9b6a7 commit c6f923a

File tree

17 files changed

+560101
-39
lines changed

17 files changed

+560101
-39
lines changed

build_install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mvn assembly:assembly -DskipTests && cp target/owlapi_wrapper-0.0.1-SNAPSHOT-jar-with-dependencies.jar ../ontologies_linked_data/bin/owlapi_wrapper.jar
1+
mvn assembly:assembly -DskipTests && cp target/owlapi_wrapper-0.0.1-SNAPSHOT-jar-with-dependencies.jar ../owlapi_wrapper.jar

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
<version>1.10.0</version>
154154
</dependency>
155155

156+
<dependency>
157+
<groupId>javax.xml.bind</groupId>
158+
<artifactId>jaxb-api</artifactId>
159+
<version>2.3.0</version>
160+
</dependency>
161+
156162
</dependencies>
157163

158164
<build>

src/main/java/org/stanford/ncbo/owlapi/wrapper/OntologyParser.java

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
public class OntologyParser {
3333
private final static Logger log = LoggerFactory.getLogger(OntologyParser.class.getName());
34+
public static final String USE_IMPORTS_IRI = "http://omv.ontoware.org/2005/05/ontology#useImports";
3435
public static final String VERSION_SUBJECT = "http://bioportal.bioontology.org/ontologies/versionSubject";
3536

3637
protected ParserInvocation parserInvocation = null;
@@ -169,6 +170,33 @@ private void addOntologyIRI(OWLOntology sourceOnt) {
169170
}
170171
}
171172

173+
/**
174+
* Get ontology imports and them to <ONTOLOGY_URI>
175+
* omv:useImports <import_URI>
176+
*
177+
* @param fact
178+
* @param sourceOnt
179+
*/
180+
private void addOntologyImports(OWLDataFactory fact, OWLOntology sourceOnt){
181+
Optional<IRI> sub = sourceOnt.getOntologyID().getOntologyIRI();
182+
183+
if(!sub.isPresent()){
184+
return;
185+
}
186+
187+
IRI ontologyIRI = sub.get();
188+
189+
// Get imports and add them as omv:useImports
190+
OWLAnnotationProperty useImportProp = fact.getOWLAnnotationProperty(IRI.create(USE_IMPORTS_IRI));
191+
for (OWLOntology imported : sourceOnt.getImports()) {
192+
if (!imported.getOntologyID().isAnonymous()) {
193+
log.info("useImports: " + imported.getOntologyID().getOntologyIRI().get());
194+
OWLAnnotationAssertionAxiom useImportAxiom = fact.getOWLAnnotationAssertionAxiom(useImportProp, ontologyIRI, imported.getOntologyID().getOntologyIRI().get());
195+
this.targetOwlManager.addAxiom(targetOwlOntology, useImportAxiom);
196+
}
197+
}
198+
}
199+
172200
/**
173201
* Copies ontology-level annotation axioms from the source ontology to the target ontology.
174202
* <p>
@@ -188,6 +216,7 @@ private void addGroundMetadata(IRI documentIRI, OWLDataFactory factory, OWLOntol
188216
}
189217

190218
for (OWLAnnotation annotation : sourceOntology.getAnnotations()) {
219+
191220
IRI subjectIRI = ontologyID.getOntologyIRI().get();
192221
OWLAnnotationProperty annotationProperty = annotation.getProperty();
193222
OWLAnnotationValue annotationValue = annotation.getValue();
@@ -212,6 +241,7 @@ private boolean buildOWLOntology(OWLOntology masterOntology, boolean isOBO) {
212241
try {
213242
targetOwlOntology = targetOwlManager.createOntology();
214243
addOntologyIRI(masterOntology);
244+
addOntologyImports(fact, masterOntology);
215245
} catch (OWLOntologyCreationException e) {
216246
log.error(e.getMessage());
217247
parserLog.addError(ParserError.OWL_CREATE_ONTOLOGY_EXCEPTION, "Error buildOWLOntology" + e.getMessage());
@@ -232,51 +262,52 @@ private boolean buildOWLOntology(OWLOntology masterOntology, boolean isOBO) {
232262

233263
boolean isPrefixedOWL = sourceOwlManager.getOntologyFormat(sourceOnt).isPrefixOWLOntologyFormat();
234264
log.info("isPrefixOWLOntologyFormat: {}", isPrefixedOWL);
235-
if (isPrefixedOWL == true && !isOBO) {
265+
if (isPrefixedOWL && !isOBO) {
236266
generateSKOSInOwl(allAxioms, fact, sourceOnt);
237267
}
238268
}
239269

240-
targetOwlManager.addAxioms(targetOwlOntology, allAxioms);
241-
for (OWLAnnotation ann : targetOwlOntology.getAnnotations()) {
270+
271+
targetOwlManager.addAxioms(targetOwlOntology, allAxioms);
272+
for (OWLAnnotation ann : targetOwlOntology.getAnnotations()) {
273+
AddOntologyAnnotation addAnn = new AddOntologyAnnotation(targetOwlOntology, ann);
274+
targetOwlManager.applyChange(addAnn);
275+
}
276+
277+
if (isOBO) {
278+
String oboVersion = parserInvocation.getOBOVersion();
279+
if (oboVersion != null) {
280+
log.info("Adding version: {}", oboVersion);
281+
OWLAnnotationProperty prop = fact.getOWLAnnotationProperty(IRI.create(OWLRDFVocabulary.OWL_VERSION_INFO.toString()));
282+
IRI versionSubjectIRI = IRI.create(VERSION_SUBJECT);
283+
OWLAnnotationAssertionAxiom annVersion = fact.getOWLAnnotationAssertionAxiom(prop, versionSubjectIRI, fact.getOWLLiteral(oboVersion));
284+
targetOwlManager.addAxiom(targetOwlOntology, annVersion);
285+
}
286+
}
287+
288+
addOntologyAnnotations(masterOntology);
289+
escapeXMLLiterals(targetOwlOntology);
290+
291+
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
292+
OWLReasoner reasoner = reasonerFactory.createReasoner(targetOwlOntology);
293+
InferredSubClassAxiomGenerator isc = new InferredSubClassAxiomGenerator();
294+
Set<OWLSubClassOfAxiom> subAxs = isc.createAxioms(targetOwlOntology.getOWLOntologyManager().getOWLDataFactory(), reasoner);
295+
targetOwlManager.addAxioms(targetOwlOntology, subAxs);
296+
deprecateBranch();
297+
298+
log.info("isOBO: {}", isOBO);
299+
if (isOBO) {
300+
replicateHierarchyAsTreeview(fact);
301+
}
302+
return true;
303+
}
304+
305+
private void addOntologyAnnotations(OWLOntology masterOntology){
306+
for (OWLAnnotation ann : masterOntology.getAnnotations()) {
242307
AddOntologyAnnotation addAnn = new AddOntologyAnnotation(targetOwlOntology, ann);
243308
targetOwlManager.applyChange(addAnn);
244309
}
245-
246-
if (isOBO) {
247-
String oboVersion = parserInvocation.getOBOVersion();
248-
if (oboVersion != null) {
249-
log.info("Adding version: {}", oboVersion);
250-
OWLAnnotationProperty prop = fact.getOWLAnnotationProperty(IRI.create(OWLRDFVocabulary.OWL_VERSION_INFO.toString()));
251-
IRI versionSubjectIRI = IRI.create(VERSION_SUBJECT);
252-
OWLAnnotationAssertionAxiom annVersion = fact.getOWLAnnotationAssertionAxiom(prop, versionSubjectIRI, fact.getOWLLiteral(oboVersion));
253-
targetOwlManager.addAxiom(targetOwlOntology, annVersion);
254-
}
255-
}
256-
257-
for (OWLOntology sourceOnt : sourceOwlManager.getOntologies()) {
258-
for (OWLAnnotation ann : sourceOnt.getAnnotations()) {
259-
AddOntologyAnnotation addAnn = new AddOntologyAnnotation(targetOwlOntology, ann);
260-
targetOwlManager.applyChange(addAnn);
261-
}
262-
}
263-
264-
escapeXMLLiterals(targetOwlOntology);
265-
266-
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
267-
OWLReasoner reasoner = reasonerFactory.createReasoner(targetOwlOntology);
268-
InferredSubClassAxiomGenerator isc = new InferredSubClassAxiomGenerator();
269-
Set<OWLSubClassOfAxiom> subAxs = isc.createAxioms(targetOwlOntology.getOWLOntologyManager().getOWLDataFactory(), reasoner);
270-
targetOwlManager.addAxioms(targetOwlOntology, subAxs);
271-
deprecateBranch();
272-
273-
log.info("isOBO: {}", isOBO);
274-
if (isOBO) {
275-
replicateHierarchyAsTreeview(fact);
276-
}
277-
return true;
278310
}
279-
280311
private void replicateHierarchyAsTreeview(OWLDataFactory fact) {
281312
Set<OWLAxiom> treeViewAxs = new HashSet<OWLAxiom>();
282313
for (OWLAxiom axiom : targetOwlOntology.getAxioms()) {
@@ -449,7 +480,7 @@ private void escapeXMLLiterals(OWLOntology target) {
449480
targetOwlManager.addAxiom(target, annAsse);
450481
Set<OWLAnnotationAssertionAxiom> del = new HashSet<OWLAnnotationAssertionAxiom>();
451482
del.add(ann);
452-
targetOwlManager.removeAxioms(target,del);
483+
targetOwlManager.removeAxioms(target,del);
453484
}
454485
}
455486
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.stanford.ncbo.owlapi.wrapper.preferred_labels;
2+
3+
import org.semanticweb.owlapi.model.OWLOntology;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.stanford.ncbo.owlapi.wrapper.ParserInvocation;
7+
import org.stanford.ncbo.owlapi.wrapper.metrics.OntologyMetrics;
8+
9+
public class PreferredLabels {
10+
private static final Logger logger = LoggerFactory.getLogger(OntologyMetrics.class);
11+
12+
private OWLOntology ontology;
13+
14+
private ParserInvocation parserInvocation;
15+
16+
public PreferredLabels(OWLOntology ontology, ParserInvocation parserInvocation) {
17+
this.ontology = ontology;
18+
this.parserInvocation = parserInvocation;
19+
}
20+
}

src/test/java/org/stanford/ncbo/owlapi/wrapper/OntologyParserTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ public class OntologyParserTest {
1818
public void setUp() throws Exception {
1919

2020
}
21+
22+
// Run this test: mvn -Dtest=OntologyParserTest#sifrTestParse test
23+
// parse_OntologyENVO_ReturnsTrue fonctionne pour comparer
24+
@Test
25+
public void sifrTestParse() throws Exception {
26+
ParserInvocation pi = new ParserInvocation("./src/test/resources/repo/input/sifr",
27+
"./src/test/resources/repo/output/sifr", "foodon.owl", true);
28+
// SKOS: anaee-france-thesaurus.rdf
29+
// OWL: AnimalDiseasesOntology.owl.xml
30+
OntologyParser parser = new OntologyParser(pi);
31+
parser.parse();
32+
//assertFalse(parser.parse());
33+
}
2134

2235
@Test
2336
public void getLocalOntologies_MultipleOntologies_Found() throws Exception {
@@ -165,6 +178,25 @@ public void parse_ImportSKOSCoreVocab_ShouldLoad() throws Exception {
165178
OWLClass skosConceptClass = factory.getOWLClass(skosConceptIRI);
166179
assertNotNull(skosConceptClass);
167180
}
181+
@Test
182+
public void parse_OntologyAnnotationCount_ReturnsTrue() throws Exception {
183+
String outputRepositoryFolder = "./src/test/resources/repo/output/cno";
184+
ParserInvocation pi = new ParserInvocation("./src/test/resources/repo/input/cno",
185+
outputRepositoryFolder, "cnov0_5.owl", true);
186+
assertTrue(pi.valid());
187+
188+
OntologyParser parser = new OntologyParser(pi);
189+
assertTrue(parser.parse());
190+
assertEquals(1, parser.getLocalOntologies().size());
191+
192+
OWLOntology targetOwlOntology = parser.getTargetOwlOntology();
193+
OWLOntology sourceOntology = parser.getParsedOntologies().stream().findFirst().orElse(null);
194+
195+
assertNotNull(sourceOntology);
196+
assertEquals(sourceOntology.getAnnotations().size(), targetOwlOntology.getAnnotations().size());
197+
198+
}
199+
168200

169201
@After
170202
public void tearDown() throws Exception {

0 commit comments

Comments
 (0)