Search in sources :

Example 1 with MolecularModelJsonRenderer

use of org.geneontology.minerva.json.MolecularModelJsonRenderer 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 MolecularModelJsonRenderer

use of org.geneontology.minerva.json.MolecularModelJsonRenderer in project minerva by geneontology.

the class ModelARTHandler method addToModel.

private void addToModel(String modelId, ModelARTResult result) throws Exception {
    IRI modelIri = curieHandler.getIRI(modelId);
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology currentOntology = m3.getModelAbox(modelIri);
    OWLOntology storedOntology = m3.loadModelABox(modelIri, manager);
    // OWLOntology stored_ontology = man1.copyOntology(storedOntology, OntologyCopy.DEEP);
    ModelContainer storedMC = new ModelContainer(modelIri, null, storedOntology);
    final MolecularModelJsonRenderer storedRenderer = createModelRenderer(storedMC, go_lego, null, curieHandler);
    JsonModel jsonStoredModel = storedRenderer.renderModel();
    ModelContainer activeMC = new ModelContainer(modelIri, null, currentOntology);
    InferenceProvider inferenceProvider = ipc.create(activeMC);
    final MolecularModelJsonRenderer renderer = createModelRenderer(activeMC, go_lego, inferenceProvider, curieHandler);
    JsonModel jsonActiveModel = renderer.renderModel();
    result.storedModel = jsonStoredModel;
    result.activeModel = jsonActiveModel;
    result.diffModel = getDiff(jsonStoredModel, jsonActiveModel);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) JsonModel(org.geneontology.minerva.json.JsonModel) InferenceProvider(org.geneontology.minerva.json.InferenceProvider) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) ModelContainer(org.geneontology.minerva.ModelContainer) MolecularModelJsonRenderer(org.geneontology.minerva.json.MolecularModelJsonRenderer)

Example 3 with MolecularModelJsonRenderer

use of org.geneontology.minerva.json.MolecularModelJsonRenderer in project minerva by geneontology.

the class UndoAwareMolecularModelManagerTest method testUndoRedo.

@Test
public void testUndoRedo() throws Exception {
    OWLOntology tbox = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(this.getClass().getResourceAsStream("/go-mgi-signaling-test.obo"));
    m3 = new UndoAwareMolecularModelManager(tbox, curieHandler, "http://testmodel.geneontology.org/", folder.newFile().getAbsolutePath(), null, go_lego_journal_file, true);
    String userId = "test-user-id";
    ModelContainer model = m3.generateBlankModel(null);
    // GO:0001158 ! enhancer sequence-specific DNA binding
    OWLNamedIndividual bindingIdividual = m3.createIndividual(model.getModelId(), "GO:0001158", null, new UndoMetadata(userId));
    String bindingId = bindingIdividual.getIRI().toString();
    // BFO:0000066 GO:0005654 ! occurs_in nucleoplasm
    m3.addType(model.getModelId(), bindingId, "BFO:0000066", "GO:0005654", new UndoMetadata(userId));
    MolecularModelJsonRenderer renderer = new MolecularModelJsonRenderer(model, null, curieHandler);
    JsonOwlIndividual render1 = renderer.renderObject(bindingIdividual);
    assertEquals(2, render1.type.length);
    // check event count
    Pair<List<ChangeEvent>, List<ChangeEvent>> undoRedoEvents = m3.getUndoRedoEvents(model.getModelId());
    List<ChangeEvent> undoEvents = undoRedoEvents.getLeft();
    List<ChangeEvent> redoEvents = undoRedoEvents.getRight();
    assertEquals(0, redoEvents.size());
    assertEquals(2, undoEvents.size());
    // undo
    assertTrue(m3.undo(model, userId));
    JsonOwlIndividual render2 = renderer.renderObject(bindingIdividual);
    assertEquals(1, render2.type.length);
    // redo
    assertTrue(m3.redo(model, userId));
    JsonOwlIndividual render3 = renderer.renderObject(bindingIdividual);
    assertEquals(2, render3.type.length);
    // undo again
    assertTrue(m3.undo(model, userId));
    JsonOwlIndividual render4 = renderer.renderObject(bindingIdividual);
    assertEquals(1, render4.type.length);
    // add new type
    // GO:0001664 ! G-protein coupled receptor binding
    m3.addType(model.getModelId(), bindingId, "GO:0001664", new UndoMetadata(userId));
    // redo again, should fail
    assertFalse(m3.redo(model, userId));
    if (m3 != null) {
        m3.dispose();
    }
}
Also used : ChangeEvent(org.geneontology.minerva.UndoAwareMolecularModelManager.ChangeEvent) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) UndoMetadata(org.geneontology.minerva.UndoAwareMolecularModelManager.UndoMetadata) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) List(java.util.List) JsonOwlIndividual(org.geneontology.minerva.json.JsonOwlIndividual) MolecularModelJsonRenderer(org.geneontology.minerva.json.MolecularModelJsonRenderer) Test(org.junit.Test)

Aggregations

MolecularModelJsonRenderer (org.geneontology.minerva.json.MolecularModelJsonRenderer)3 InferenceProvider (org.geneontology.minerva.json.InferenceProvider)2 JsonModel (org.geneontology.minerva.json.JsonModel)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 List (java.util.List)1 ModelContainer (org.geneontology.minerva.ModelContainer)1 ChangeEvent (org.geneontology.minerva.UndoAwareMolecularModelManager.ChangeEvent)1 UndoMetadata (org.geneontology.minerva.UndoAwareMolecularModelManager.UndoMetadata)1 CurieHandler (org.geneontology.minerva.curie.CurieHandler)1 DefaultCurieHandler (org.geneontology.minerva.curie.DefaultCurieHandler)1 MappedCurieHandler (org.geneontology.minerva.curie.MappedCurieHandler)1 JsonOwlIndividual (org.geneontology.minerva.json.JsonOwlIndividual)1 Test (org.junit.Test)1 IRI (org.semanticweb.owlapi.model.IRI)1 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)1 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)1 ParserWrapper (owltools.io.ParserWrapper)1