comparison OPPL/Tool.java @ 8:40adbcb2a7cc

Added merge function and FaCT++ support. Improved OBO render
author Mikel Egaña Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Tue, 11 Oct 2011 11:09:52 +0200
parents 756f1f5798bf
children 14bfd77f8520
comparison
equal deleted inserted replaced
7:756f1f5798bf 8:40adbcb2a7cc
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package es.upm.fi.dia.oeg.oppl.galaxy; 18 package es.upm.fi.dia.oeg.oppl.galaxy;
19 19
20 import java.io.BufferedWriter;
20 import java.io.File; 21 import java.io.File;
21 import java.io.FileNotFoundException; 22 //import java.io.PrintWriter;
23 //import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26 import java.net.URISyntaxException;
22 import java.util.ArrayList; 27 import java.util.ArrayList;
23 import java.util.List; 28 import java.util.List;
24 import java.util.Scanner; 29 import java.util.Scanner;
25 import java.util.logging.Level; 30 //import java.util.logging.Level;
26 import java.util.logging.Logger; 31 import java.util.logging.Logger;
27 32
28 import org.coode.oppl.ChangeExtractor; 33 import org.coode.oppl.ChangeExtractor;
29 import org.coode.oppl.OPPLParser; 34 import org.coode.oppl.OPPLParser;
30 import org.coode.oppl.OPPLScript; 35 import org.coode.oppl.OPPLScript;
31 import org.coode.oppl.ParserFactory; 36 import org.coode.oppl.ParserFactory;
32 import org.coode.oppl.exceptions.QuickFailRuntimeExceptionHandler; 37 import org.coode.oppl.exceptions.QuickFailRuntimeExceptionHandler;
33 import org.coode.oppl.log.Logging; 38 //import org.coode.oppl.log.Logging;
34 import org.coode.owlapi.obo.parser.OBOOntologyFormat; 39 import org.coode.owlapi.obo.parser.OBOOntologyFormat;
35 import org.coode.parsers.ErrorListener; 40 import org.coode.parsers.ErrorListener;
36 import org.coode.parsers.LoggerErrorListener; 41 import org.coode.parsers.LoggerErrorListener;
42 //import org.obolibrary.obo2owl.Owl2Obo;
43 //import org.obolibrary.oboformat.model.OBODoc;
44 //import org.obolibrary.oboformat.writer.OBOFormatWriter;
37 import org.semanticweb.owlapi.apibinding.OWLManager; 45 import org.semanticweb.owlapi.apibinding.OWLManager;
38 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; 46 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
39 import org.semanticweb.owlapi.io.SystemOutDocumentTarget; 47 import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
48 import org.semanticweb.owlapi.model.AddAxiom;
40 import org.semanticweb.owlapi.model.IRI; 49 import org.semanticweb.owlapi.model.IRI;
50 import org.semanticweb.owlapi.model.OWLAnnotation;
51 import org.semanticweb.owlapi.model.OWLAnnotationProperty;
41 import org.semanticweb.owlapi.model.OWLAxiom; 52 import org.semanticweb.owlapi.model.OWLAxiom;
42 import org.semanticweb.owlapi.model.OWLAxiomChange; 53 import org.semanticweb.owlapi.model.OWLAxiomChange;
54 import org.semanticweb.owlapi.model.OWLClass;
43 import org.semanticweb.owlapi.model.OWLDataFactory; 55 import org.semanticweb.owlapi.model.OWLDataFactory;
56 import org.semanticweb.owlapi.model.OWLLiteral;
57 //import org.semanticweb.owlapi.model.OWLObjectProperty;
44 import org.semanticweb.owlapi.model.OWLOntology; 58 import org.semanticweb.owlapi.model.OWLOntology;
59 import org.semanticweb.owlapi.model.OWLOntologyChange;
45 import org.semanticweb.owlapi.model.OWLOntologyCreationException; 60 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
46 import org.semanticweb.owlapi.model.OWLOntologyIRIMapper; 61 import org.semanticweb.owlapi.model.OWLOntologyIRIMapper;
47 import org.semanticweb.owlapi.model.OWLOntologyManager; 62 import org.semanticweb.owlapi.model.OWLOntologyManager;
48 import org.semanticweb.owlapi.model.OWLOntologyStorageException; 63 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
64 //import org.semanticweb.owlapi.owllink.OWLlinkHTTPXMLReasonerFactory;
49 import org.semanticweb.owlapi.reasoner.InferenceType; 65 import org.semanticweb.owlapi.reasoner.InferenceType;
50 import org.semanticweb.owlapi.reasoner.OWLReasoner; 66 import org.semanticweb.owlapi.reasoner.OWLReasoner;
51 import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; 67 import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
52 import org.semanticweb.owlapi.util.InferredAxiomGenerator; 68 import org.semanticweb.owlapi.util.InferredAxiomGenerator;
53 import org.semanticweb.owlapi.util.InferredOntologyGenerator; 69 import org.semanticweb.owlapi.util.InferredOntologyGenerator;
54 import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator; 70 import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator;
71 import org.semanticweb.owlapi.util.OWLEntityRenamer;
72 import org.semanticweb.owlapi.util.OWLOntologyMerger;
55 import org.semanticweb.owlapi.util.SimpleIRIMapper; 73 import org.semanticweb.owlapi.util.SimpleIRIMapper;
74 import org.semanticweb.owlapi.vocab.OWL2Datatype;
75 import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
76
77 import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory;
56 78
57 import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; 79 import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
58 80
59 import org.semanticweb.HermiT.Reasoner; 81 import org.semanticweb.HermiT.Reasoner;
60 82
62 * @author Mikel Egaña Aranguren 84 * @author Mikel Egaña Aranguren
63 */ 85 */
64 public class Tool { 86 public class Tool {
65 87
66 /** 88 /**
67 * @throws FileNotFoundException
68 * @throws OWLOntologyCreationException 89 * @throws OWLOntologyCreationException
69 * @throws OWLOntologyStorageException 90 * @throws OWLOntologyStorageException
91 * @throws IOException
92 * @throws URISyntaxException
70 */ 93 */
71 public static void main(String[] args) throws FileNotFoundException, OWLOntologyCreationException, OWLOntologyStorageException { 94 public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException, IOException, URISyntaxException {
72 // Get the arguments from command-line 95 // Get the arguments from command-line
73 String OWLFilePath = args [0]; 96 String OWLFilePath = args [0];
74 String OPPL_script_file = args [1]; 97 String OPPL_script_file = args [1];
75 String Output_format = args [2]; // OWL|OBO 98 String Output_format = args [2]; // OWL|OBO
76 String Add_inferred = args [3]; // Add_inferred|Whatever 99 String Add_inferred = args [3]; // AddInferred|Whatever
77 String imports_file_path = args [4]; // Flat tab delimited file: URI Document URI 100 String imports_file_path = args [4]; // NoImports|Flat tab delimited file: URI Document URI
78 String reasoner_type = args [5]; // Pellet|HermiT 101 String reasoner_type = args [5]; // Pellet|FaCTPlusPlus|HermiT
102 String merge_URI = args [6]; // NoMerge|URI for merged ontology
79 103
80 // Load the main ontology 104 // Load the main ontology
81 File owl_file = new File(OWLFilePath); 105 File owl_file = new File(OWLFilePath);
82 OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); 106 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
83 107
95 manager.addIRIMapper(iriMapper); 119 manager.addIRIMapper(iriMapper);
96 } 120 }
97 } 121 }
98 imports_input.close(); 122 imports_input.close();
99 } 123 }
100 OWLDataFactory factory = manager.getOWLDataFactory();
101 OWLOntology OWL_ontology = manager.loadOntologyFromOntologyDocument(owl_file); 124 OWLOntology OWL_ontology = manager.loadOntologyFromOntologyDocument(owl_file);
102 125
103 // Reasoner 126 // Reasoner
104 OWLReasonerFactory reasonerFactory = null; 127 OWLReasonerFactory reasonerFactory = null;
105 OWLReasoner reasoner = null; 128 OWLReasoner reasoner = null;
129
130 // Pellet
106 if(reasoner_type.equals("Pellet")){ 131 if(reasoner_type.equals("Pellet")){
107 reasonerFactory = new PelletReasonerFactory(); 132 reasonerFactory = new PelletReasonerFactory();
108 reasoner = reasonerFactory.createReasoner(OWL_ontology); 133 reasoner = reasonerFactory.createReasoner(OWL_ontology);
109 } 134 }
135 // FaCTPlusPlus
136 else if (reasoner_type.equals("FaCTPlusPlus")){
137 reasonerFactory = new FaCTPlusPlusReasonerFactory();
138 reasoner = reasonerFactory.createReasoner(OWL_ontology);
139 }
140 // HermiT
110 else{ 141 else{
111 reasonerFactory = new Reasoner.ReasonerFactory(); 142 reasonerFactory = new Reasoner.ReasonerFactory();
112 reasoner = reasonerFactory.createReasoner(OWL_ontology); 143 reasoner = reasonerFactory.createReasoner(OWL_ontology);
113 } 144 }
145
146 // OWLLink: The problem is that Racer, for example, listens in 8080, the same port as Galaxy
147 // I have to change Racer settings and OWLLink settings
148 // OWLlinkHTTPXMLReasonerFactory factory = new OWLlinkHTTPXMLReasonerFactory();
149 // reasoner = factory.createReasoner(OWL_ontology);
114 150
115 // Load the flat file with script in memory 151 // Load the flat file with script in memory
116 String OPPL_script_source = ""; 152 String OPPL_script_source = "";
117 File file = new File(OPPL_script_file); 153 File file = new File(OPPL_script_file);
118 Scanner input = new Scanner(file); 154 Scanner input = new Scanner(file);
134 ChangeExtractor extractor = new ChangeExtractor(new QuickFailRuntimeExceptionHandler(), true); 170 ChangeExtractor extractor = new ChangeExtractor(new QuickFailRuntimeExceptionHandler(), true);
135 List<OWLAxiomChange> changes = extractor.visit(OPPLscript); 171 List<OWLAxiomChange> changes = extractor.visit(OPPLscript);
136 manager.applyChanges(changes); 172 manager.applyChanges(changes);
137 173
138 // Add the inferred axioms as asserted axioms to the original ontology 174 // Add the inferred axioms as asserted axioms to the original ontology
139 if(Add_inferred.equals("Add_inferred")){ 175 if(Add_inferred.equals("AddInferred")){
140 reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); 176 reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
141 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); 177 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
142 gens.add(new InferredSubClassAxiomGenerator()); 178 gens.add(new InferredSubClassAxiomGenerator());
143 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); 179 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
144 iog.fillOntology(manager, OWL_ontology); 180 iog.fillOntology(manager, OWL_ontology);
145 } 181 }
146 182
147 // Print the ontology to the standard output so other galaxy tools can operate on the output 183 // Print the ontology to the standard output so other galaxy tools can operate on it
148 if(Output_format.equals("OBO")){ 184
149 manager.saveOntology(OWL_ontology, new OBOOntologyFormat(), new SystemOutDocumentTarget()); 185 // Do not merge ontologies
150 } 186 if(merge_URI.equals("NoMerge")){
187 saveOntology(manager, OWL_ontology, Output_format);
188 }
189 // Merge ontologies
190 else{
191 OWLOntologyMerger merger = new OWLOntologyMerger(manager);
192 IRI mergedOntologyIRI = IRI.create(merge_URI);
193 OWLOntology merged = merger.createMergedOntology(manager, mergedOntologyIRI);
194 saveOntology(manager, merged, Output_format);
195 }
196 }
197
198 private static void saveOntology (OWLOntologyManager manager, OWLOntology ontology, String Format) throws OWLOntologyStorageException, IOException, URISyntaxException {
199
200 // OBO format
201 if(Format.equals("OBO")){
202
203 // OWL API generates bad OBO but OBOformat doesn't work either so I correct the OWL API problems (More predictable) in a temporary file
204 // Very inefficient but no time for another solution
205
206 OWLDataFactory factory = manager.getOWLDataFactory();
207
208 OWLEntityRenamer renamer = new OWLEntityRenamer (manager, manager.getOntologies());
209 OWLAnnotationProperty label = factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
210
211 for(OWLClass cls : ontology.getClassesInSignature()){
212 // Remove annotations (OWL API generates bad OBO annotations)
213
214 // Keep rdfs:label
215 String class_name = null;
216 for (OWLAnnotation annotation : cls.getAnnotations(ontology, label)) {
217 if (annotation.getValue() instanceof OWLLiteral) {
218 OWLLiteral val = (OWLLiteral) annotation.getValue();
219 class_name = val.getLiteral();
220 }
221 }
222
223 // I have to remove all the annotations cause I don't know which ones are rendered properly
224 manager.removeAxioms(ontology,ontology.getAnnotationAssertionAxioms(cls.getIRI()));
225
226 // Add rdfs:label again
227 OWLAnnotation labelAnno = factory.getOWLAnnotation(factory.getOWLAnnotationProperty(
228 OWLRDFVocabulary.RDFS_LABEL.getIRI()),factory.getOWLLiteral(class_name,OWL2Datatype.XSD_STRING));
229 OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), labelAnno);
230 manager.applyChange(new AddAxiom(ontology, ax));
231
232 // Rename entities
233 String cls_IRI = cls.getIRI().toString();
234 String cls_proper_IRI = cls_IRI.replace("_", ":");
235 List<OWLOntologyChange> changes = renamer.changeIRI(cls, IRI.create(cls_proper_IRI));
236 manager.applyChanges(changes);
237 }
238
239 File file = new File("tmp.obo");
240 manager.saveOntology(ontology, new OBOOntologyFormat(), IRI.create(file.toURI()));
241 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
242 Scanner input = new Scanner(file);
243 String buffer = "";
244
245 while(input.hasNext()){
246 String nextLine = input.nextLine();
247 if(nextLine.contains("[Term]") || nextLine.contains("[Typedef]")){
248 if(buffer.isEmpty()){
249 bw.write(buffer);
250 bw.newLine();
251 }
252 }
253 if(!nextLine.contains("is_a: Thing") && !nextLine.contains("auto-generated-by:") && !nextLine.contains("id_space:") && !nextLine.contains("! ----")){
254 if(!nextLine.isEmpty() && !buffer.contains("relationship:")){
255 bw.write(nextLine);
256 bw.newLine();
257 }
258 }
259 buffer = nextLine;
260 }
261 input.close();
262 bw.close();
263 file.delete();
264 }
265
266 //OWL format
151 else{ 267 else{
152 manager.saveOntology(OWL_ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget()); 268 manager.saveOntology(ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget());
153 } 269 }
154 } 270 }
155 } 271 }