Search in sources :

Example 11 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphOntologyManager method getAllSubClasses.

public Set<String> getAllSubClasses(String uri) throws IOException {
    Set<String> supers = new HashSet<String>();
    try {
        BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
        try {
            String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "SELECT ?sub " + "WHERE { " + "?sub rdfs:subClassOf* <" + uri + "> . " + "} ";
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            while (result.hasNext()) {
                BindingSet binding = result.next();
                Value v = binding.getValue("sub");
                // ignore anonymous sub classes
                if (v instanceof URI) {
                    String superclass = binding.getValue("sub").stringValue();
                    supers.add(superclass);
                }
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return supers;
}
Also used : RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) URI(org.openrdf.model.URI) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Value(org.openrdf.model.Value)

Example 12 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphOntologyManager method getTaxaByGenes.

public Set<String> getTaxaByGenes(Set<String> genes) throws IOException {
    String expansion = "VALUES ?gene { ";
    for (String gene : genes) {
        expansion += "<" + gene + "> \n";
    }
    expansion += " } . \n";
    Set<String> taxa = new HashSet<String>();
    try {
        BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
        try {
            String query = "select distinct ?taxon  \n" + "where { \n" + expansion + "  ?gene rdfs:subClassOf ?taxon_restriction .\n" + "  ?taxon_restriction owl:onProperty <http://purl.obolibrary.org/obo/RO_0002162> .\n" + "  ?taxon_restriction owl:someValuesFrom ?taxon \n" + "\n" + "}";
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            while (result.hasNext()) {
                BindingSet binding = result.next();
                Value v = binding.getValue("taxon");
                // ignore anonymous sub classes
                if (v instanceof URI) {
                    String taxon = binding.getValue("taxon").stringValue();
                    taxa.add(taxon);
                }
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return taxa;
}
Also used : RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) URI(org.openrdf.model.URI) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Value(org.openrdf.model.Value)

Example 13 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphOntologyManager method getAllTaxaWithGenes.

public Set<String> getAllTaxaWithGenes() throws IOException {
    Set<String> taxa = new HashSet<String>();
    try {
        BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
        try {
            String query = "select distinct ?taxon  \n" + "where { \n" + "  ?gene rdfs:subClassOf ?taxon_restriction .\n" + "  ?taxon_restriction owl:onProperty <http://purl.obolibrary.org/obo/RO_0002162> .\n" + "  ?taxon_restriction owl:someValuesFrom ?taxon \n" + "\n" + "}";
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            while (result.hasNext()) {
                BindingSet binding = result.next();
                Value v = binding.getValue("taxon");
                // ignore anonymous sub classes
                if (v instanceof URI) {
                    String taxon = binding.getValue("taxon").stringValue();
                    taxa.add(taxon);
                }
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return taxa;
}
Also used : RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) URI(org.openrdf.model.URI) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Value(org.openrdf.model.Value)

Example 14 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class CommandLineInterface method sparqlUpdate.

/**
 * Updates the journal with the provided update sparql statement.
 * cli parameter --sparql-update
 *
 * @param journalFilePath
 * @param updateFile
 * @throws OWLOntologyCreationException
 * @throws IOException
 * @throws RepositoryException
 * @throws MalformedQueryException
 * @throws UpdateExecutionException
 */
public static void sparqlUpdate(String journalFilePath, String updateFile) throws OWLOntologyCreationException, IOException, RepositoryException, MalformedQueryException, UpdateExecutionException {
    // minimal inputs
    if (journalFilePath == null) {
        System.err.println("No journal file was configured.");
        System.exit(-1);
        return;
    }
    if (updateFile == null) {
        System.err.println("No update file was configured.");
        System.exit(-1);
        return;
    }
    String update = FileUtils.readFileToString(new File(updateFile), StandardCharsets.UTF_8);
    Properties properties = new Properties();
    properties.load(CommandLineInterface.class.getResourceAsStream("/org/geneontology/minerva/blazegraph.properties"));
    properties.setProperty(com.bigdata.journal.Options.FILE, journalFilePath);
    BigdataSail sail = new BigdataSail(properties);
    BigdataSailRepository repository = new BigdataSailRepository(sail);
    repository.initialize();
    BigdataSailRepositoryConnection conn = repository.getUnisolatedConnection();
    BlazegraphMutationCounter counter = new BlazegraphMutationCounter();
    conn.addChangeLog(counter);
    conn.prepareUpdate(QueryLanguage.SPARQL, update).execute();
    int changes = counter.mutationCount();
    conn.removeChangeLog(counter);
    System.out.println("\nApplied " + changes + " changes");
    conn.close();
}
Also used : BigdataSailRepository(com.bigdata.rdf.sail.BigdataSailRepository) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) BigdataSail(com.bigdata.rdf.sail.BigdataSail) BlazegraphMutationCounter(org.geneontology.minerva.util.BlazegraphMutationCounter)

Example 15 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class ReplaceObsoleteReferencesCommand method applySPARQLUpdate.

private static void applySPARQLUpdate(BigdataSailRepository repository, String update, Optional<IChangeLog> changeLog) throws RepositoryException, UpdateExecutionException, MalformedQueryException {
    BigdataSailRepositoryConnection connection = repository.getUnisolatedConnection();
    changeLog.ifPresent(connection::addChangeLog);
    try {
        connection.begin();
        try {
            connection.prepareUpdate(QueryLanguage.SPARQL, update).execute();
        } catch (UpdateExecutionException | RepositoryException | MalformedQueryException e) {
            connection.rollback();
            throw e;
        }
    } finally {
        connection.close();
    }
    changeLog.ifPresent(connection::removeChangeLog);
}
Also used : UpdateExecutionException(org.openrdf.query.UpdateExecutionException) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) MalformedQueryException(org.openrdf.query.MalformedQueryException) RepositoryException(org.openrdf.repository.RepositoryException)

Aggregations

BigdataSailRepositoryConnection (com.bigdata.rdf.sail.BigdataSailRepositoryConnection)28 RepositoryException (org.openrdf.repository.RepositoryException)25 IOException (java.io.IOException)15 Value (org.openrdf.model.Value)12 URI (org.openrdf.model.URI)9 URIImpl (org.openrdf.model.impl.URIImpl)9 UnknownIdentifierException (org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException)4 BlazegraphMutationCounter (org.geneontology.minerva.util.BlazegraphMutationCounter)4 StatementCollector (org.openrdf.rio.helpers.StatementCollector)4 RioMemoryTripleSource (org.semanticweb.owlapi.rio.RioMemoryTripleSource)4 RioRenderer (org.semanticweb.owlapi.rio.RioRenderer)4 BigdataSail (com.bigdata.rdf.sail.BigdataSail)3 BigdataSailRepository (com.bigdata.rdf.sail.BigdataSailRepository)3 java.util (java.util)3 Options (com.bigdata.journal.Options)2 Iterations (info.aduna.iteration.Iterations)2 java.io (java.io)2 Entry (java.util.Map.Entry)2 Collectors (java.util.stream.Collectors)2 FileUtils (org.apache.commons.io.FileUtils)2