Mercurial > repos > mikel-egana-aranguren > oppl
comparison src/GalaxyOWLAPI.java @ 19:cc270db37d33 draft
Directories re-arranged
author | Mikel Egana Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu> |
---|---|
date | Sat, 06 Oct 2012 21:50:39 +0200 |
parents | OPPL/src/GalaxyOWLAPI.java@d3616fac4ca5 |
children |
comparison
equal
deleted
inserted
replaced
18:d3616fac4ca5 | 19:cc270db37d33 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package es.upm.fi.dia.oeg.oppl.galaxy; | |
5 | |
6 import java.io.BufferedWriter; | |
7 import java.io.File; | |
8 import java.io.IOException; | |
9 import java.io.OutputStreamWriter; | |
10 import java.util.ArrayList; | |
11 import java.util.List; | |
12 import java.util.Scanner; | |
13 import java.util.Set; | |
14 | |
15 import org.apache.log4j.Level; | |
16 import org.apache.log4j.Logger; | |
17 import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxEditorParser; | |
18 import org.coode.owlapi.obo.parser.OBOOntologyFormat; | |
19 import org.coode.parsers.BidirectionalShortFormProviderAdapter; | |
20 import org.semanticweb.HermiT.Reasoner; | |
21 import org.semanticweb.elk.owlapi.ElkReasonerFactory; | |
22 import org.semanticweb.owlapi.apibinding.OWLManager; | |
23 import org.semanticweb.owlapi.expression.OWLEntityChecker; | |
24 import org.semanticweb.owlapi.expression.ParserException; | |
25 import org.semanticweb.owlapi.expression.ShortFormEntityChecker; | |
26 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; | |
27 import org.semanticweb.owlapi.io.SystemOutDocumentTarget; | |
28 import org.semanticweb.owlapi.model.AddAxiom; | |
29 import org.semanticweb.owlapi.model.IRI; | |
30 import org.semanticweb.owlapi.model.OWLAnnotation; | |
31 import org.semanticweb.owlapi.model.OWLAnnotationProperty; | |
32 import org.semanticweb.owlapi.model.OWLAxiom; | |
33 import org.semanticweb.owlapi.model.OWLClass; | |
34 import org.semanticweb.owlapi.model.OWLClassExpression; | |
35 import org.semanticweb.owlapi.model.OWLDataFactory; | |
36 import org.semanticweb.owlapi.model.OWLLiteral; | |
37 import org.semanticweb.owlapi.model.OWLNamedIndividual; | |
38 import org.semanticweb.owlapi.model.OWLOntology; | |
39 import org.semanticweb.owlapi.model.OWLOntologyChange; | |
40 import org.semanticweb.owlapi.model.OWLOntologyCreationException; | |
41 import org.semanticweb.owlapi.model.OWLOntologyManager; | |
42 import org.semanticweb.owlapi.model.OWLOntologyStorageException; | |
43 import org.semanticweb.owlapi.reasoner.InferenceType; | |
44 import org.semanticweb.owlapi.reasoner.OWLReasoner; | |
45 import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; | |
46 import org.semanticweb.owlapi.util.BidirectionalShortFormProvider; | |
47 import org.semanticweb.owlapi.util.InferredAxiomGenerator; | |
48 import org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator; | |
49 import org.semanticweb.owlapi.util.InferredDisjointClassesAxiomGenerator; | |
50 import org.semanticweb.owlapi.util.InferredOntologyGenerator; | |
51 import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator; | |
52 import org.semanticweb.owlapi.util.InferredSubDataPropertyAxiomGenerator; | |
53 import org.semanticweb.owlapi.util.InferredSubObjectPropertyAxiomGenerator; | |
54 import org.semanticweb.owlapi.util.OWLEntityRenamer; | |
55 import org.semanticweb.owlapi.util.OWLOntologyImportsClosureSetProvider; | |
56 import org.semanticweb.owlapi.util.OWLOntologyMerger; | |
57 import org.semanticweb.owlapi.util.SimpleShortFormProvider; | |
58 import org.semanticweb.owlapi.vocab.OWL2Datatype; | |
59 import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; | |
60 import org.semanticweb.owlapi.reasoner.Node; | |
61 | |
62 import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory; | |
63 | |
64 import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; | |
65 | |
66 /** | |
67 * A wrapper of the OWL API | |
68 * | |
69 * @author Mikel Egana Aranguren | |
70 */ | |
71 public class GalaxyOWLAPI { | |
72 private OWLOntologyManager manager; | |
73 private OWLOntology ontology; | |
74 private OWLReasonerFactory reasonerFactory; | |
75 private OWLReasoner reasoner; | |
76 | |
77 public GalaxyOWLAPI (){ | |
78 manager = OWLManager.createOWLOntologyManager(); | |
79 } | |
80 public OWLOntologyManager getOWLManager(){ | |
81 return manager; | |
82 } | |
83 public OWLOntology getMainOntology(){ | |
84 return ontology; | |
85 } | |
86 public OWLReasoner getReasoner(){ | |
87 return reasoner; | |
88 } | |
89 | |
90 public void loadMainOntology(String OWLFilePath) throws OWLOntologyCreationException{ | |
91 File owl_file = new File(OWLFilePath); | |
92 ontology = manager.loadOntologyFromOntologyDocument(owl_file); | |
93 } | |
94 | |
95 // public void loadImportedOntology(IRI importedOntologyIRI) throws OWLOntologyCreationException{ | |
96 // manager.loadOntologyFromOntologyDocument(importedOntologyIRI); | |
97 // } | |
98 | |
99 // OWLLink: The problem is that Racer, for example, listens in 8080, the same port as Galaxy | |
100 // I have to change Racer settings and OWLLink settings | |
101 // OWLlinkHTTPXMLReasonerFactory factory = new OWLlinkHTTPXMLReasonerFactory(); | |
102 // reasoner = factory.createReasoner(OWL_ontology); | |
103 | |
104 public void setReasonerPellet (){ | |
105 reasonerFactory = new PelletReasonerFactory(); | |
106 reasoner = reasonerFactory.createReasoner(ontology); | |
107 } | |
108 public void setReasonerFaCT (){ | |
109 reasonerFactory = new FaCTPlusPlusReasonerFactory(); | |
110 reasoner = reasonerFactory.createReasoner(ontology); | |
111 } | |
112 public void setReasonerHermit (){ | |
113 reasonerFactory = new Reasoner.ReasonerFactory(); | |
114 reasoner = reasonerFactory.createReasoner(ontology); | |
115 } | |
116 public void setReasonerElk (){ | |
117 reasonerFactory = new ElkReasonerFactory(); | |
118 Logger.getLogger("org.semanticweb.elk").setLevel(Level.OFF); | |
119 reasoner = reasonerFactory.createReasoner(ontology); | |
120 } | |
121 public void disposeReasoner(){ | |
122 reasoner.dispose(); | |
123 } | |
124 public void merge (String ontologyIRI) throws OWLOntologyCreationException, OWLOntologyStorageException{ | |
125 OWLOntologyMerger merger = new OWLOntologyMerger(manager); | |
126 // OWLOntologyMerger merger = new OWLOntologyMerger(new OWLOntologyImportsClosureSetProvider(manager, ontology)); | |
127 IRI mergedOntologyIRI = IRI.create(ontologyIRI); | |
128 // OWLOntology merged_ontology = merger.createMergedOntology(manager, mergedOntologyIRI); | |
129 ontology = merger.createMergedOntology(manager, mergedOntologyIRI); | |
130 // manager.saveOntology(ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget()); | |
131 } | |
132 | |
133 public OWLClassExpression parseMOSClassExpression (String expr) throws ParserException{ | |
134 Set<OWLOntology> importsClosure = ontology.getImportsClosure(); | |
135 BidirectionalShortFormProvider bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(manager, importsClosure, new SimpleShortFormProvider()); | |
136 OWLEntityChecker entityChecker = new ShortFormEntityChecker(bidiShortFormProvider); | |
137 ManchesterOWLSyntaxEditorParser MOSparser = new ManchesterOWLSyntaxEditorParser(manager.getOWLDataFactory(), expr); | |
138 MOSparser.setOWLEntityChecker(entityChecker); | |
139 return MOSparser.parseClassExpression(); | |
140 } | |
141 | |
142 public Set<OWLNamedIndividual> getIndividuals (OWLClassExpression expr){ | |
143 return (reasoner.getInstances(expr, false)).getFlattened(); | |
144 } | |
145 public Set<OWLClass> getEquivalentClasses (OWLClassExpression expr){ | |
146 Node<OWLClass> equivalentClasses = reasoner.getEquivalentClasses(expr); | |
147 Set<OWLClass> result; | |
148 if (expr.isAnonymous()) { | |
149 result = equivalentClasses.getEntities(); | |
150 } | |
151 else { | |
152 result = equivalentClasses.getEntitiesMinus(expr.asOWLClass()); | |
153 } | |
154 return result; | |
155 } | |
156 | |
157 public Set<OWLClass> getDirectSuperClasses (OWLClassExpression expr){ | |
158 return (reasoner.getSuperClasses(expr, true)).getFlattened(); | |
159 } | |
160 | |
161 public Set<OWLClass> getAncestors (OWLClassExpression expr){ | |
162 return (reasoner.getSuperClasses(expr, false)).getFlattened(); | |
163 } | |
164 | |
165 public Set<OWLClass> getDirectSubClasses (OWLClassExpression expr){ | |
166 return (reasoner.getSubClasses(expr, true)).getFlattened(); | |
167 } | |
168 | |
169 public Set<OWLClass> getDescendants (OWLClassExpression expr){ | |
170 return (reasoner.getSubClasses(expr, false)).getFlattened(); | |
171 } | |
172 | |
173 // CLASS_ASSERTIONS Denotes the computation of the direct types of individuals for each individual in the signature of the imports closure of the root ontology. | |
174 | |
175 public void addCLASS_ASSERTIONS(){ | |
176 reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS); | |
177 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
178 gens.add(new InferredClassAssertionAxiomGenerator()); | |
179 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
180 iog.fillOntology(manager, ontology); | |
181 } | |
182 | |
183 // CLASS_HIERARCHY Denotes the computation of the class hierarchy. | |
184 | |
185 public void addCLASS_HIERARCHY(){ | |
186 reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); | |
187 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
188 gens.add(new InferredSubClassAxiomGenerator()); | |
189 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
190 iog.fillOntology(manager, ontology); | |
191 } | |
192 | |
193 // DATA_PROPERTY_ASSERTIONS Denotes the computation of relationships between individuals and data property values for each individual in the signature of the imports closure of the root ontology. | |
194 // public void addDATA_PROPERTY_ASSERTIONS(){ | |
195 // reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_ASSERTIONS); | |
196 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
197 // gens.add(new InferredDataPropertyAxiomGenerator()); | |
198 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
199 // iog.fillOntology(manager, ontology); | |
200 // } | |
201 | |
202 // DATA_PROPERTY_HIERARCHY Denotes the computation of the data property hierarchy. | |
203 | |
204 public void addDATA_PROPERTY_HIERARCHY(){ | |
205 reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_HIERARCHY); | |
206 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
207 gens.add(new InferredSubDataPropertyAxiomGenerator()); | |
208 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
209 iog.fillOntology(manager, ontology); | |
210 } | |
211 | |
212 // DIFFERENT_INDIVIDUALS Denotes the computation of sets of individuals that are different from each individual in the signature of the imports closure of the root ontology. | |
213 | |
214 // public void addDIFFERENT_INDIVIDUALS(){ | |
215 // reasoner.precomputeInferences(InferenceType.DIFFERENT_INDIVIDUALS); | |
216 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
217 // gens.add(new Inferred); | |
218 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
219 // iog.fillOntology(manager, ontology); | |
220 // } | |
221 | |
222 // DISJOINT_CLASSES Denotes the computation of sets of classes that are disjoint for each class in the signature of the imports closure of the root ontology. | |
223 | |
224 public void addDISJOINT_CLASSES(){ | |
225 reasoner.precomputeInferences(InferenceType.DISJOINT_CLASSES); | |
226 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
227 gens.add(new InferredDisjointClassesAxiomGenerator()); | |
228 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
229 iog.fillOntology(manager, ontology); | |
230 } | |
231 | |
232 // OBJECT_PROPERTY_ASSERTIONS Denotes the computation of relationships between individuals in the signature of the imports closure of the root ontology. | |
233 | |
234 // public void addOBJECT_PROPERTY_ASSERTIONS(){ | |
235 // reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_ASSERTIONS); | |
236 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
237 // gens.add(new InferredObjectPropertyAxiomGenerator<OWLAxiom>() { | |
238 // };()); | |
239 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
240 // iog.fillOntology(manager, ontology); | |
241 // } | |
242 | |
243 // OBJECT_PROPERTY_HIERARCHY Denotes the computation of the object property hierarchy. | |
244 | |
245 public void addOBJECT_PROPERTY_HIERARCHY(){ | |
246 reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_HIERARCHY); | |
247 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
248 gens.add(new InferredSubObjectPropertyAxiomGenerator()); | |
249 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
250 iog.fillOntology(manager, ontology); | |
251 } | |
252 | |
253 // SAME_INDIVIDUAL Denotes the computation of individuals that are interpreted as the same object for each individual in the imports closure of the root ontology. | |
254 | |
255 // public void addSAME_INDIVIDUAL(){ | |
256 // reasoner.precomputeInferences(InferenceType.SAME_INDIVIDUAL); | |
257 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); | |
258 // gens.add(new Infer); | |
259 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); | |
260 // iog.fillOntology(manager, ontology); | |
261 // } | |
262 | |
263 public void saveOntology (boolean OWL) throws OWLOntologyStorageException, IOException, OWLOntologyCreationException{ | |
264 // OWL format | |
265 if(OWL){ | |
266 manager.saveOntology(ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget()); | |
267 } | |
268 // OBO format | |
269 else{ | |
270 // OWL API generates bad OBO but OBOformat doesn't work either so I correct the OWL API problems (More predictable) in a temporary file | |
271 // Very inefficient but no time for another solution | |
272 | |
273 OWLDataFactory factory = manager.getOWLDataFactory(); | |
274 OWLEntityRenamer renamer = new OWLEntityRenamer (manager, manager.getOntologies()); | |
275 OWLAnnotationProperty label = factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI()); | |
276 for(OWLClass cls : ontology.getClassesInSignature()){ | |
277 // Remove annotations (OWL API generates bad OBO annotations) | |
278 | |
279 // Keep rdfs:label | |
280 String class_name = null; | |
281 for (OWLAnnotation annotation : cls.getAnnotations(ontology, label)){ | |
282 if (annotation.getValue() instanceof OWLLiteral) { | |
283 OWLLiteral val = (OWLLiteral) annotation.getValue(); | |
284 class_name = val.getLiteral(); | |
285 } | |
286 } | |
287 | |
288 // I have to remove all the annotations cause I don't know which ones are rendered properly | |
289 manager.removeAxioms(ontology,ontology.getAnnotationAssertionAxioms(cls.getIRI())); | |
290 | |
291 // Add rdfs:label again | |
292 OWLAnnotation labelAnno = factory.getOWLAnnotation(factory.getOWLAnnotationProperty( | |
293 OWLRDFVocabulary.RDFS_LABEL.getIRI()),factory.getOWLLiteral(class_name,OWL2Datatype.XSD_STRING)); | |
294 OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), labelAnno); | |
295 manager.applyChange(new AddAxiom(ontology, ax)); | |
296 | |
297 // Rename entities | |
298 String cls_IRI = cls.getIRI().toString(); | |
299 String cls_proper_IRI = cls_IRI.replace("_", ":"); | |
300 List<OWLOntologyChange> changes = renamer.changeIRI(cls, IRI.create(cls_proper_IRI)); | |
301 manager.applyChanges(changes); | |
302 } | |
303 | |
304 File file = new File("tmp.obo"); | |
305 manager.saveOntology(ontology, new OBOOntologyFormat(), IRI.create(file.toURI())); | |
306 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
307 Scanner input = new Scanner(file); | |
308 String buffer = ""; | |
309 | |
310 while(input.hasNext()){ | |
311 String nextLine = input.nextLine(); | |
312 if(nextLine.contains("[Term]") || nextLine.contains("[Typedef]")){ | |
313 if(buffer.isEmpty()){ | |
314 bw.write(buffer); | |
315 bw.newLine(); | |
316 } | |
317 } | |
318 if(!nextLine.contains("is_a: Thing") && !nextLine.contains("auto-generated-by:") && !nextLine.contains("id_space:") && !nextLine.contains("! ----")){ | |
319 if(!nextLine.isEmpty() && !buffer.contains("relationship:")){ | |
320 bw.write(nextLine); | |
321 bw.newLine(); | |
322 } | |
323 } | |
324 buffer = nextLine; | |
325 } | |
326 input.close(); | |
327 bw.close(); | |
328 file.delete(); | |
329 } | |
330 } | |
331 } |