Search in sources :

Example 1 with ParserWrapper

use of owltools.io.ParserWrapper in project minerva by geneontology.

the class CommandLineInterface method owl2LegoJson.

/**
 * Convert a GO-CAM owl file to a minerva json structure
 * --owl-lego-to-json
 *
 * @param input
 * @param output
 * @param usePretty
 * @throws Exception
 */
public static void owl2LegoJson(String input, String output, boolean usePretty) throws Exception {
    // minimal inputs
    if (input == null) {
        System.err.println("No input model was configured.");
        System.exit(-1);
        return;
    }
    if (output == null) {
        System.err.println("No output file was configured.");
        System.exit(-1);
        return;
    }
    // configuration
    CurieHandler curieHandler = DefaultCurieHandler.getDefaultHandler();
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (usePretty) {
        gsonBuilder.setPrettyPrinting();
    }
    Gson gson = gsonBuilder.create();
    // process each model
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Loading model from file: " + input);
    }
    OWLOntology model = null;
    final JsonModel jsonModel;
    ParserWrapper pw = new ParserWrapper();
    try {
        // load model
        model = pw.parseOWL(IRI.create(new File(input).getCanonicalFile()));
        // TODO decide if we need reasoning
        InferenceProvider inferenceProvider = null;
        String modelId = null;
        Optional<IRI> ontologyIRI = model.getOntologyID().getOntologyIRI();
        if (ontologyIRI.isPresent()) {
            modelId = curieHandler.getCuri(ontologyIRI.get());
        }
        // render json
        final MolecularModelJsonRenderer renderer = new MolecularModelJsonRenderer(modelId, model, inferenceProvider, curieHandler);
        jsonModel = renderer.renderModel();
    } finally {
        if (model != null) {
            pw.getManager().removeOntology(model);
            model = null;
        }
    }
    // save as json string
    final String json = gson.toJson(jsonModel);
    final File outputFile = new File(output).getCanonicalFile();
    try (OutputStream outputStream = new FileOutputStream(outputFile)) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Saving json to file: " + outputFile);
        }
        IOUtils.write(json, outputStream);
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonModel(org.geneontology.minerva.json.JsonModel) Gson(com.google.gson.Gson) InferenceProvider(org.geneontology.minerva.json.InferenceProvider) DefaultCurieHandler(org.geneontology.minerva.curie.DefaultCurieHandler) MappedCurieHandler(org.geneontology.minerva.curie.MappedCurieHandler) CurieHandler(org.geneontology.minerva.curie.CurieHandler) ParserWrapper(owltools.io.ParserWrapper) MolecularModelJsonRenderer(org.geneontology.minerva.json.MolecularModelJsonRenderer)

Example 2 with ParserWrapper

use of owltools.io.ParserWrapper in project minerva by geneontology.

the class EcoMapperFactory method createEcoMapper.

/**
 * Create an instance of a {@link EcoMapper}. Uses a the manager to load ECO via the
 * PURL. Load mappings using the PURL.
 *
 * @param m
 * @return mapper pair
 * @throws OWLException
 * @throws IOException
 * @see EcoMapper#ECO_PURL
 * @see EcoMapper#ECO_MAPPING_PURL
 */
public static OntologyMapperPair<EcoMapper> createEcoMapper(OWLOntologyManager m) throws OWLException, IOException {
    ParserWrapper p = new ParserWrapper();
    p.setManager(m);
    return createEcoMapper(p);
}
Also used : ParserWrapper(owltools.io.ParserWrapper)

Example 3 with ParserWrapper

use of owltools.io.ParserWrapper in project ontologies by enanomapper.

the class FindLabelDuplicationWithinOneMergedOntology method testOverlap.

public OverlapResult testOverlap(String ontologyName) throws Exception {
    ParserWrapper pw = new ParserWrapper();
    OWLOntology owlOntology = pw.parseOWL(ontologyName);
    String iri = owlOntology.getOntologyID().getOntologyIRI().toString();
    String iriChanged = iri + "_merged.owl";
    System.out.println("IRI: " + iriChanged);
    owlOntology = OntologyHelper.loadAllImportedAxioms(owlOntology, iriChanged);
    OWLDataFactory fac = pw.getManager().getOWLDataFactory();
    OverlapResult result = new OverlapResult();
    for (OWLClass c : owlOntology.getClassesInSignature()) {
        Set<OWLAnnotation> annos = c.getAnnotations(owlOntology, fac.getRDFSLabel());
        for (OWLAnnotation anno : annos) {
            String annoVal = anno.getValue().toString().replaceAll("\"", "").replaceAll("@", "");
            String classIRI = c.toStringID();
            if (annoVal != null && !annoVal.isEmpty()) {
                result.allLabels.add(annoVal);
                if (!result.labelsToURIs.containsKey(annoVal)) {
                    List<String> newList = new ArrayList<String>();
                    newList.add(classIRI);
                    result.labelsToURIs.put(annoVal, newList);
                } else {
                    result.labelsToURIs.get(annoVal).add(classIRI);
                }
            }
        }
    }
    // start analytics
    for (String label : result.allLabels) {
        List<String> uris = result.labelsToURIs.get(label);
        if (uris.size() > 1) {
            // There is a duplicate
            result.labelsDuplicated.add(label);
        }
    }
    return result;
}
Also used : OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) ArrayList(java.util.ArrayList) ParserWrapper(owltools.io.ParserWrapper) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 4 with ParserWrapper

use of owltools.io.ParserWrapper in project ontologies by enanomapper.

the class FindLabelOverlapBetweenOntologies method testOverlap.

public OverlapResult testOverlap(String ontologyOne, String ontologyTwo, boolean getImportsClosure) throws Exception {
    ParserWrapper pw = new ParserWrapper();
    OWLOntology owlOntoOne = pw.parseOWL(ontologyOne);
    OWLOntology owlOntoTwo = pw.parseOWL(ontologyTwo);
    if (getImportsClosure) {
        String iriOne = owlOntoOne.getOntologyID().getOntologyIRI().toString();
        String iriTwo = owlOntoTwo.getOntologyID().getOntologyIRI().toString();
        String iriOneChanged = iriOne + "_merged.owl";
        String iriTwoChanged = iriTwo + "_merged.owl";
        System.out.println("ONE: " + iriOneChanged);
        System.out.println("TWO: " + iriTwoChanged);
        owlOntoOne = OntologyHelper.loadAllImportedAxioms(owlOntoOne, iriOneChanged);
        owlOntoTwo = OntologyHelper.loadAllImportedAxioms(owlOntoTwo, iriTwoChanged);
    }
    OWLDataFactory fac = pw.getManager().getOWLDataFactory();
    OverlapResult result = new OverlapResult();
    for (OWLClass c : owlOntoOne.getClassesInSignature()) {
        Set<OWLAnnotation> annos = c.getAnnotations(owlOntoOne, fac.getRDFSLabel());
        for (OWLAnnotation anno : annos) {
            String annoVal = anno.getValue().toString().replaceAll("\"", "").replaceAll("@", "");
            String classIRI = c.toStringID();
            if (annoVal != null && !annoVal.isEmpty()) {
                result.allLabelsOne.add(annoVal);
                result.labelsToURIsOne.put(annoVal, classIRI);
            }
        }
    }
    for (OWLClass c : owlOntoTwo.getClassesInSignature()) {
        Set<OWLAnnotation> annos = c.getAnnotations(owlOntoTwo, fac.getRDFSLabel());
        for (OWLAnnotation anno : annos) {
            String annoVal = anno.getValue().toString().replaceAll("\"", "").replaceAll("@", "");
            String classIRI = c.toStringID();
            if (annoVal != null && !annoVal.isEmpty()) {
                result.allLabelsTwo.add(annoVal);
                result.labelsToURIsTwo.put(annoVal, classIRI);
            }
        }
    }
    // start analytics
    for (String labelInOne : result.allLabelsOne) {
        if (result.allLabelsTwo.contains(labelInOne)) {
            if (result.labelsToURIsOne.get(labelInOne).equals(result.labelsToURIsTwo.get(labelInOne))) {
                result.labelsAndIdsShared.add(labelInOne);
            } else {
                result.labelsShared.add(labelInOne);
            }
        } else {
            result.labelsInOneNotTwo.add(labelInOne);
        }
    }
    for (String labelInTwo : result.allLabelsTwo) {
        if (!result.allLabelsOne.contains(labelInTwo)) {
            result.labelsInTwoNotOne.add(labelInTwo);
        }
    }
    return result;
}
Also used : OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) ParserWrapper(owltools.io.ParserWrapper) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 5 with ParserWrapper

use of owltools.io.ParserWrapper in project ontologies by enanomapper.

the class OntologyHelper method openOntologyFromURL.

public static OWLOntology openOntologyFromURL(String url) {
    ParserWrapper pw = new ParserWrapper();
    try {
        URL website = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) website.openConnection();
        InputStream is = openConnectionCheckRedirects(connection);
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null) response.append(inputLine);
        in.close();
        // create a temp file to store the content
        File temp = File.createTempFile("tempfile", url.lastIndexOf(".") > 0 ? url.substring(url.lastIndexOf(".")) : ".owl");
        // write it
        BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
        bw.write(response.toString());
        bw.close();
        OWLOntology o = pw.parse(temp.toString());
        return o;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) URL(java.net.URL) ParserException(org.semanticweb.owlapi.expression.ParserException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) HttpURLConnection(java.net.HttpURLConnection) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) BufferedReader(java.io.BufferedReader) ParserWrapper(owltools.io.ParserWrapper) File(java.io.File)

Aggregations

ParserWrapper (owltools.io.ParserWrapper)9 File (java.io.File)3 BeforeClass (org.junit.BeforeClass)3 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)3 URL (java.net.URL)2 MinervaOWLGraphWrapper (org.geneontology.minerva.MinervaOWLGraphWrapper)2 CurieHandler (org.geneontology.minerva.curie.CurieHandler)2 DefaultCurieHandler (org.geneontology.minerva.curie.DefaultCurieHandler)2 MappedCurieHandler (org.geneontology.minerva.curie.MappedCurieHandler)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 LocalDate (java.time.LocalDate)1 LocalTime (java.time.LocalTime)1