view OPPL/src/GalaxyOWLAPI.java @ 11:6ca67b155e32

Imports simplified, new tool for inference added
author Mikel Egaña Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Fri, 09 Mar 2012 16:15:27 +0100
parents
children 7e6604a5ee55
line wrap: on
line source

/**
 * 
 */
package es.upm.fi.dia.oeg.oppl.galaxy;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.coode.owlapi.obo.parser.OBOOntologyFormat;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyChange;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.reasoner.InferenceType;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.util.InferredAxiomGenerator;
import org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator;
import org.semanticweb.owlapi.util.InferredDisjointClassesAxiomGenerator;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator;
import org.semanticweb.owlapi.util.InferredSubDataPropertyAxiomGenerator;
import org.semanticweb.owlapi.util.InferredSubObjectPropertyAxiomGenerator;
import org.semanticweb.owlapi.util.OWLEntityRenamer;
import org.semanticweb.owlapi.util.OWLOntologyMerger;
import org.semanticweb.owlapi.vocab.OWL2Datatype;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory;

import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;

/**
 * A wrapper of the OWL API 
 * 
 * @author Mikel EgaƱa Aranguren
 */
public class GalaxyOWLAPI {
	private OWLOntologyManager manager;
	private OWLOntology ontology;
	private OWLReasonerFactory reasonerFactory;
	private OWLReasoner reasoner;
	
	public GalaxyOWLAPI (){
		manager = OWLManager.createOWLOntologyManager();
	}
	public OWLOntologyManager getOWLManager(){
		return manager;
	}
	public OWLOntology getMainOntology(){
		return ontology;
	}
	public OWLReasoner getReasoner(){
		return reasoner;
	}
	
	public void loadMainOntology(String OWLFilePath) throws OWLOntologyCreationException{
		File owl_file = new File(OWLFilePath);
		ontology = manager.loadOntologyFromOntologyDocument(owl_file);
	}
	
//	public void loadImportedOntology(IRI importedOntologyIRI) throws OWLOntologyCreationException{
//		manager.loadOntologyFromOntologyDocument(importedOntologyIRI);
//	}
	
	// OWLLink: The problem is that Racer, for example, listens in 8080, the same port as Galaxy
	// I have to change Racer settings and OWLLink settings
//		OWLlinkHTTPXMLReasonerFactory factory = new OWLlinkHTTPXMLReasonerFactory();
//		reasoner = factory.createReasoner(OWL_ontology);
	
	public void setReasonerPellet (){
		reasonerFactory = new PelletReasonerFactory(); 
		reasoner = reasonerFactory.createReasoner(ontology);
	}
	public void setReasonerFaCT (){
		reasonerFactory = new FaCTPlusPlusReasonerFactory();
		reasoner = reasonerFactory.createReasoner(ontology);
	}
	public void setReasonerHermit (){
		reasonerFactory = new Reasoner.ReasonerFactory();
		reasoner = reasonerFactory.createReasoner(ontology);
	}
	public void merge () throws OWLOntologyCreationException{
		OWLOntologyMerger merger = new OWLOntologyMerger(manager);
		IRI mergedOntologyIRI = IRI.create("http://oeg-upm.net/oppl_galaxy/merged.owl");
		ontology = merger.createMergedOntology(manager, mergedOntologyIRI);
	}

	//	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.	
	
	public void addCLASS_ASSERTIONS(){
		reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);
		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
		gens.add(new InferredClassAssertionAxiomGenerator());
		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
		iog.fillOntology(manager, ontology);
	}
	
	//	CLASS_HIERARCHY Denotes the computation of the class hierarchy.	
	
	public void addCLASS_HIERARCHY(){
		reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
		gens.add(new InferredSubClassAxiomGenerator());
		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
		iog.fillOntology(manager, ontology);
	}
	
	//	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.
//	public void addDATA_PROPERTY_ASSERTIONS(){
//		reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_ASSERTIONS);
//		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
//		gens.add(new InferredDataPropertyAxiomGenerator()); 
//		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
//		iog.fillOntology(manager, ontology);
//	}
	
	//	DATA_PROPERTY_HIERARCHY Denotes the computation of the data property hierarchy.
	
	public void addDATA_PROPERTY_HIERARCHY(){
		reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_HIERARCHY);
		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
		gens.add(new InferredSubDataPropertyAxiomGenerator());
		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
		iog.fillOntology(manager, ontology);
	}
	
	//	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.
	
//	public void addDIFFERENT_INDIVIDUALS(){
//		reasoner.precomputeInferences(InferenceType.DIFFERENT_INDIVIDUALS);
//		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
//		gens.add(new Inferred);
//		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
//		iog.fillOntology(manager, ontology);
//	}
	
	//	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.
	
	public void addDISJOINT_CLASSES(){
		reasoner.precomputeInferences(InferenceType.DISJOINT_CLASSES);
		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
		gens.add(new InferredDisjointClassesAxiomGenerator());
		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
		iog.fillOntology(manager, ontology);
	}
	
	//	OBJECT_PROPERTY_ASSERTIONS Denotes the computation of relationships between individuals in the signature of the imports closure of the root ontology.
	
//	public void addOBJECT_PROPERTY_ASSERTIONS(){
//		reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_ASSERTIONS);
//		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
//		gens.add(new InferredObjectPropertyAxiomGenerator<OWLAxiom>() {
//		};());
//		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
//		iog.fillOntology(manager, ontology);
//	}
	
	//	OBJECT_PROPERTY_HIERARCHY Denotes the computation of the object property hierarchy.
	
	public void addOBJECT_PROPERTY_HIERARCHY(){
		reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_HIERARCHY);
		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
		gens.add(new InferredSubObjectPropertyAxiomGenerator());
		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
		iog.fillOntology(manager, ontology);
	}
	
	//	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.
	
//	public void addSAME_INDIVIDUAL(){
//		reasoner.precomputeInferences(InferenceType.SAME_INDIVIDUAL);
//		List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
//		gens.add(new Infer);
//		InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
//		iog.fillOntology(manager, ontology);
//	}
	
	public void saveOntology (boolean OWL) throws OWLOntologyStorageException, IOException, OWLOntologyCreationException{
		// OWL format
		if(OWL){
			manager.saveOntology(ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget());
		}
		// OBO format
		else{
			// OWL API generates bad OBO but OBOformat doesn't work either so I correct the OWL API problems (More predictable) in a temporary file
			// Very inefficient but no time for another solution
						
			OWLDataFactory factory = manager.getOWLDataFactory();
			OWLEntityRenamer renamer = new OWLEntityRenamer (manager, manager.getOntologies());
			OWLAnnotationProperty label = factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
			for(OWLClass cls : ontology.getClassesInSignature()){
			// Remove annotations (OWL API generates bad OBO annotations)
							
			// Keep rdfs:label
			String class_name = null;
			for (OWLAnnotation annotation : cls.getAnnotations(ontology, label)){
				if (annotation.getValue() instanceof OWLLiteral) {
					OWLLiteral val = (OWLLiteral) annotation.getValue();
					class_name = val.getLiteral();
				}
			}
										
//			I have to remove all the annotations cause I don't know which ones are rendered properly
			manager.removeAxioms(ontology,ontology.getAnnotationAssertionAxioms(cls.getIRI()));
							
			// Add rdfs:label again
			OWLAnnotation labelAnno = factory.getOWLAnnotation(factory.getOWLAnnotationProperty(
			OWLRDFVocabulary.RDFS_LABEL.getIRI()),factory.getOWLLiteral(class_name,OWL2Datatype.XSD_STRING));
			OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), labelAnno);
			manager.applyChange(new AddAxiom(ontology, ax));
							
			// Rename entities
			String cls_IRI = cls.getIRI().toString();
			String cls_proper_IRI = cls_IRI.replace("_", ":");
			List<OWLOntologyChange> changes = renamer.changeIRI(cls, IRI.create(cls_proper_IRI));
			manager.applyChanges(changes);
			}
						
			File file = new File("tmp.obo");
			manager.saveOntology(ontology, new OBOOntologyFormat(), IRI.create(file.toURI()));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));		
			Scanner input = new Scanner(file);
			String buffer = "";

			while(input.hasNext()){
				String nextLine = input.nextLine();
				if(nextLine.contains("[Term]") || nextLine.contains("[Typedef]")){
					if(buffer.isEmpty()){
						bw.write(buffer);
						bw.newLine();
					}
				}
				if(!nextLine.contains("is_a: Thing") && !nextLine.contains("auto-generated-by:") && !nextLine.contains("id_space:") && !nextLine.contains("! ----")){
					if(!nextLine.isEmpty() && !buffer.contains("relationship:")){
						bw.write(nextLine);
						bw.newLine();
					}
				}
				buffer = nextLine;
			}
			input.close();
			bw.close();
			file.delete();
		}
	}	
}