Search in sources :

Example 1 with JsonModel

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

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

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

the class ARTHandlerTest method testGetModel.

@Test
public final void testGetModel() throws URISyntaxException, IOException, OWLOntologyStorageException, OWLOntologyCreationException, RepositoryException, UnknownIdentifierException {
    // get a hold of a test model
    String mid = "5fbeae9c00000008";
    final String modelId = "http://model.geneontology.org/" + mid;
    M3BatchResponse response = BatchTestTools.getModel(handler, modelId, false);
    ModelARTResult result = getStoredModel(mid);
    // Repeat it after
    response = BatchTestTools.getModel(handler, modelId, false);
    assertFalse("Model should not be modified", response.data.modifiedFlag);
    assertTrue("Active Model should be the same as stored model", equalJsonSize(result.getActiveModel(), result.getStoredModel()));
    // Set up m3Batch response to compare active models
    JsonModel m3JsonModel = m3ResponseToJsonModel(response);
    assertTrue("Active Model should be the same as m3Batch active model", equalJsonSize(result.getActiveModel(), m3JsonModel));
}
Also used : ModelARTResult(org.geneontology.minerva.server.handler.ModelARTHandler.ModelARTResult) JsonModel(org.geneontology.minerva.json.JsonModel)

Example 4 with JsonModel

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

the class ARTHandlerTest method testStoredModelModified.

@Test
public final void testStoredModelModified() throws URISyntaxException, IOException, OWLOntologyStorageException, OWLOntologyCreationException, RepositoryException, UnknownIdentifierException {
    // get a hold of a test model
    String mid = "5fbeae9c00000008";
    final String modelId = "http://model.geneontology.org/" + mid;
    M3Request r = BatchTestTools.addIndividual(modelId, "GO:0003674");
    List<M3Request> batch = Collections.singletonList(r);
    M3BatchResponse response = handler.m3Batch("test-user", Collections.emptySet(), "test-intention", "foo-packet-id", batch.toArray(new M3Request[batch.size()]), false, true);
    // Get the model
    response = BatchTestTools.getModel(handler, modelId, false);
    ModelARTResult result = getStoredModel(mid);
    // Repeat
    response = BatchTestTools.getModel(handler, modelId, false);
    int activeIndividualCount = result.getActiveModel().individuals.length;
    int storedIndividualCount = result.getStoredModel().individuals.length;
    assertTrue("Model should be modified", response.data.modifiedFlag);
    assertFalse("Active model should not be the same as stored model", equalJsonSize(result.getActiveModel(), result.getStoredModel()));
    // Since we added one individual
    assertTrue("Active individuals should be one more stored individual count", activeIndividualCount == storedIndividualCount + 1);
    // Set up m3Batch response to compare active models
    JsonModel m3JsonModel = m3ResponseToJsonModel(response);
    assertTrue("Active Model should be the same as m3Batch active model", equalJsonSize(result.getActiveModel(), m3JsonModel));
    // Store the model
    response = storeModel(modelId);
    result = getStoredModel(mid);
    assertFalse("Model should not be modified", response.data.modifiedFlag);
    assertTrue("Active model should be the same as stored model", equalJsonSize(result.getActiveModel(), result.getStoredModel()));
    // Set up m3Batch response to compare active models
    m3JsonModel = m3ResponseToJsonModel(response);
    assertTrue("Active Model should be the same as m3Batch active model", equalJsonSize(result.getActiveModel(), m3JsonModel));
    // After store active should be equal to stored
    storedIndividualCount = result.getStoredModel().individuals.length;
    assertTrue("Active individuals should be one more stored individual count", activeIndividualCount == storedIndividualCount);
    // Get the model
    response = BatchTestTools.getModel(handler, modelId, false);
    assertFalse("Model should not be modified", response.data.modifiedFlag);
    // Set up m3Batch response to compare active models
    m3JsonModel = m3ResponseToJsonModel(response);
    assertTrue("Active Model should be the same as m3Batch active model", equalJsonSize(result.getActiveModel(), m3JsonModel));
}
Also used : ModelARTResult(org.geneontology.minerva.server.handler.ModelARTHandler.ModelARTResult) JsonModel(org.geneontology.minerva.json.JsonModel)

Example 5 with JsonModel

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

the class ARTHandlerTest method testStoredModelReset.

@Test
public final void testStoredModelReset() throws URISyntaxException, IOException, OWLOntologyStorageException, OWLOntologyCreationException, RepositoryException, UnknownIdentifierException {
    // get a hold of a test model
    String mid = "5fbeae9c00000008";
    final String modelId = "http://model.geneontology.org/" + mid;
    M3Request r = BatchTestTools.addIndividual(modelId, "GO:0003674");
    List<M3Request> batch = Collections.singletonList(r);
    M3BatchResponse response = handler.m3Batch("test-user", Collections.emptySet(), "test-intention", "foo-packet-id", batch.toArray(new M3Request[batch.size()]), false, true);
    // Reset the model
    response = resetModel(modelId);
    // Get the model
    response = BatchTestTools.getModel(handler, modelId, false);
    ModelARTResult result = getStoredModel(mid);
    assertFalse("Model should not be modified", response.data.modifiedFlag);
    assertTrue("Active Model should be the same as stored model", equalJsonSize(result.getActiveModel(), result.getStoredModel()));
    // Set up m3Batch response to compare active models
    JsonModel m3JsonModel = m3ResponseToJsonModel(response);
    assertTrue("Active Model should be the same as m3Batch active model", equalJsonSize(result.getActiveModel(), m3JsonModel));
}
Also used : ModelARTResult(org.geneontology.minerva.server.handler.ModelARTHandler.ModelARTResult) JsonModel(org.geneontology.minerva.json.JsonModel)

Aggregations

JsonModel (org.geneontology.minerva.json.JsonModel)6 ModelARTResult (org.geneontology.minerva.server.handler.ModelARTHandler.ModelARTResult)3 InferenceProvider (org.geneontology.minerva.json.InferenceProvider)2 MolecularModelJsonRenderer (org.geneontology.minerva.json.MolecularModelJsonRenderer)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ModelContainer (org.geneontology.minerva.ModelContainer)1 CurieHandler (org.geneontology.minerva.curie.CurieHandler)1 DefaultCurieHandler (org.geneontology.minerva.curie.DefaultCurieHandler)1 MappedCurieHandler (org.geneontology.minerva.curie.MappedCurieHandler)1 IRI (org.semanticweb.owlapi.model.IRI)1 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)1 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)1 ParserWrapper (owltools.io.ParserWrapper)1