/** * */ package oboannotator.ontology; import java.io.IOException; import java.net.URI; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import oboannotator.util.AppConfig; import oboannotator.util.Util; import oboannotator.util.XmlEndec; import org.semanticweb.owl.apibinding.OWLManager; import org.semanticweb.owl.model.OWLAnnotation; import org.semanticweb.owl.model.OWLClass; import org.semanticweb.owl.model.OWLEntity; import org.semanticweb.owl.model.OWLOntology; import org.semanticweb.owl.model.OWLOntologyCreationException; import org.semanticweb.owl.model.OWLOntologyManager; /** * Author: Ahmed Abdeen Hamed
* Marine Biological Laboratory
* Biology of Aging Group
* Date: 11-Jan-2007

*/ public class OBO_toLexiconConverter { public static final String ENVO_ONT_URI="http://www.berkeleybop.org/ontologies/obo-all/envo/envo.obo"; public static final String GAZ_ONT_URI="http://obo.cvs.sourceforge.net/*checkout*/obo/obo/ontology/environmental/gaz.obo"; public static final String RESOURCES_OUTPUT_LOCATION="src/oboannotator/resources/dict/"; public static final String ENVO_OBO_DICT="envo_dictionary.xml"; public static final String GAZ_OBO_DICT="gaz_dictionary.xml"; public static final String TEST_DICT="test_dicnary.xml"; //public static final String DICTIONARY_EXTENSION=".xml"; public static void compileDictionaryFromOwlURI(String ontologyURI, String dictionaryName, String outputDir, String addUpdateFlag){ //Pattern synonymPattern = Pattern.compile("\\\"([^\\\"]*)\\\"\\s([A-Z]*)\\s"); try { // A simple example of how to load and save an ontology // We first need to obtain a copy of an OWLOntologyManager, which, as the // name suggests, manages a set of ontologies. An ontology is unique within // an ontology manager. To load multiple copies of an ontology, multiple managers // would have to be used. OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); // We load an ontology from a physical URI - in this case we'll load the pizza // ontology. URI physicalURI = URI.create(ontologyURI); // Now ask the manager to load the ontology OWLOntology ontology = manager.loadOntologyFromPhysicalURI(physicalURI); StringBuffer buffer = new StringBuffer(""); StringBuffer tokenBuffer = new StringBuffer(""); StringBuffer variantBuffer = new StringBuffer(""); buffer.append("\n"); buffer.append(" \n"); boolean closeToken = false; for(OWLClass cls : ontology.getReferencedClasses()) { //System.out.println("Class : " + cls); //System.out.println("Class URI: " + cls.getURI()); Set annotations = cls.getAnnotations(ontology); for (OWLAnnotation annotation: annotations){ // String uriAnnotationName = OntoUtil.extractAnnotationNameFromURI(annotation.getAnnotationURI().toString()); if("label".equalsIgnoreCase(uriAnnotationName)){ closeToken = true; //tokenBuffer = new StringBuffer(""); tokenBuffer.append(" \n"); tokenBuffer.append(" \n"); } if("comment".equalsIgnoreCase(uriAnnotationName)){ String temp = OntoUtil.cleanupAnnotationFromCarretString(annotation.getAnnotationValue().toString()); temp = OntoUtil.extractTextFromQuotedText(temp); temp = OntoUtil.extractSynonymFromOwlComment(temp); StringBuffer words = new StringBuffer(""); if(temp!=null){ variantBuffer.append(" \n"); } } } //System.out.println("--------------------------------------------"); buffer.append(tokenBuffer.toString()); buffer.append(variantBuffer.toString()); //buffer.append() if(closeToken){ buffer.append(" \n\n"); } tokenBuffer = new StringBuffer(""); variantBuffer = new StringBuffer(""); closeToken=false; } buffer.append(" \n\n"); try { //writeStringToFile(String stringToBeWritten, String filePath, String fileName) //System.out.println("PRINT PLEASE: " + outputDir + dictionaryName); if(addUpdateFlag.equals(AppConfig.ADD_NEW_DICTION)){ Util.writeStringToFile(buffer.toString(), outputDir, dictionaryName+AppConfig.XML_EXTENSION); } else if(addUpdateFlag.equals(AppConfig.UPDATE_DICTION)){ //System.out.println("OBO_toLexiconConvertor class : " + dictionaryName); Util.writeStringToFile(buffer.toString(), outputDir, dictionaryName); } } catch (IOException e) { e.printStackTrace(); } } catch (OWLOntologyCreationException e) { System.out.println("The ontology could not be created: " + e.getMessage()); } } public static void compileDictionaryFromOboUri(String ontologyURI, String dictionaryName, String outputDir, String addUpdateFlag){ Pattern synonymPattern = Pattern.compile("\\\"([^\\\"]*)\\\"\\s([A-Z]*)\\s"); try { // A simple example of how to load and save an ontology // We first need to obtain a copy of an OWLOntologyManager, which, as the // name suggests, manages a set of ontologies. An ontology is unique within // an ontology manager. To load multiple copies of an ontology, multiple managers // would have to be used. OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); // We load an ontology from a physical URI - in this case we'll load the pizza // ontology. URI physicalURI = URI.create(ontologyURI); // Now ask the manager to load the ontology OWLOntology ontology = manager.loadOntologyFromPhysicalURI(physicalURI); StringBuffer buffer = new StringBuffer(""); StringBuffer tokenBuffer = new StringBuffer(""); StringBuffer variantBuffer = new StringBuffer(""); buffer.append("\n"); buffer.append(" \n"); for(OWLEntity entity: ontology.getReferencedEntities()){ for(OWLAnnotation annot: entity.getAnnotations(ontology)){ if("label".equalsIgnoreCase(annot.getAnnotationURI().getFragment())){ //tokenBuffer = new StringBuffer(""); tokenBuffer.append(" \n"); tokenBuffer.append(" \n"); } if("synonym".equalsIgnoreCase(annot.getAnnotationURI().getFragment())){ Matcher synonymMatcher = synonymPattern.matcher(annot.getAnnotationValue().toString()); while (synonymMatcher.find()) { //if ("RELATED".equalsIgnoreCase(synonymMatcher.group(2).toString())){ if ("RELATED".equalsIgnoreCase(synonymMatcher.group(1).toString())){ variantBuffer.append(" \n"); }else{ variantBuffer.append(" \n"); } } } } buffer.append(tokenBuffer.toString()); buffer.append(variantBuffer.toString()); buffer.append(" \n\n"); tokenBuffer = new StringBuffer(""); variantBuffer = new StringBuffer(""); } buffer.append(" \n\n"); try { //writeStringToFile(String stringToBeWritten, String filePath, String fileName) //System.out.println("PRINT PLEASE: " + outputDir + dictionaryName); if(addUpdateFlag.equals(AppConfig.ADD_NEW_DICTION)){ Util.writeStringToFile(buffer.toString(), outputDir, dictionaryName+AppConfig.XML_EXTENSION); }else if(addUpdateFlag.equals(AppConfig.UPDATE_DICTION)){ Util.writeStringToFile(buffer.toString(), outputDir, dictionaryName); } } catch (IOException e) { e.printStackTrace(); } } catch (OWLOntologyCreationException e) { System.out.println("The ontology could not be created: " + e.getMessage()); } } public static void main(String[] args) { OBO_toLexiconConverter.compileDictionaryFromOwlURI("https://msi-workgroups.svn.sourceforge.net/svnroot/msi-workgroups/ontology/NMR.owl", "nmrOWL", "/Users/ahmedabdeenhamed/Test/", AppConfig.ADD_NEW_DICTION); } }