Search in sources :

Example 1 with SparqlQueryException

use of uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException in project goci by EBISPOT.

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm, Object... args) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Map<String, Object> bindingMap = new HashMap<String, Object>();
    int i = 0;
    for (Object o : args) {
        String argName = "?_arg" + i++;
        sparql = sparql.replaceFirst("\\?\\?", argName);
        bindingMap.put(argName, o);
    }
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    for (String argName : bindingMap.keySet()) {
        Object argValue = bindingMap.get(argName);
        RDFNode arg;
        if (argValue instanceof URI) {
            arg = new ResourceImpl(argValue.toString());
        } else {
            arg = getLiteralNode(argValue);
        }
        initialBinding.add(argName, arg);
    }
    ParameterizedSparqlString queryString = new ParameterizedSparqlString(q1.toString(), initialBinding);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, queryString.asQuery(), false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Query(com.hp.hpl.jena.query.Query) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) URI(java.net.URI) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QuerySolutionMap(com.hp.hpl.jena.query.QuerySolutionMap) Graph(com.hp.hpl.jena.graph.Graph) ResourceImpl(com.hp.hpl.jena.rdf.model.impl.ResourceImpl) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) ResultSet(com.hp.hpl.jena.query.ResultSet) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 2 with SparqlQueryException

use of uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException in project goci by EBISPOT.

the class SparqlTemplate method label.

public String label(final URI entity) {
    String sparql = getPrefixString().concat("SELECT DISTINCT ?label WHERE { <" + entity.toString() + "> rdfs:label ?label . }");
    if (labelCache.containsKey(sparql)) {
        return labelCache.get(sparql);
    }
    String result = query(sparql, new ResultSetMapper<String>() {

        @Override
        public String mapResultSet(ResultSet rs) {
            String result = null;
            while (rs.hasNext()) {
                if (result != null) {
                    throw new SparqlQueryException(new DataIntegrityViolationException("More than one rdfs:label for' " + entity.toString() + "'"));
                }
                QuerySolution qs = rs.next();
                result = qs.getLiteral("label").getLexicalForm();
            }
            if (result == null) {
                result = "Annotation tbc";
            }
            return result;
        }
    });
    labelCache.put(sparql, result);
    return result;
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ResultSet(com.hp.hpl.jena.query.ResultSet) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) DataIntegrityViolationException(uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)

Example 3 with SparqlQueryException

use of uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException in project goci by EBISPOT.

the class SparqlPussycatSession method performRendering.

@Override
public String performRendering(RenderletNexus renderletNexus, Filter... filters) throws PussycatSessionNotReadyException, NoRenderableDataException {
    // render
    if (!isRendering()) {
        setRendering(true);
        String associationQueryString = associationQueryMain;
        String traitQueryString = traitQueryMain;
        getLog().debug("Rendering SVG from SPARQL endpoint (filters = '" + filters + "')...");
        String associationPvalueFilters = "";
        String traitPvalueFilters = "";
        String associationDateFilters = "";
        String traitDateFilters = "";
        for (Filter filter : filters) {
            if (filter.getFilteredType().equals(Association.class)) {
                List<Double> values = filter.getFilteredValues();
                associationQueryString = associationQueryString.concat("?association gt:has_p_value ?pvalue .");
                traitQueryString = traitQueryString.concat("?association gt:has_p_value ?pvalue .");
                associationPvalueFilters = associationPvalueFilters.concat("  FILTER ( ?pvalue < ?? )").concat("  FILTER ( ?pvalue >= ?? )");
                traitPvalueFilters = traitPvalueFilters.concat("  FILTER ( ?pvalue < ?? )").concat("  FILTER ( ?pvalue >= ?? )");
            }
            if (filter.getFilteredType().equals(Publication.class)) {
                associationQueryString = associationQueryString.concat("?association ro:part_of ?study . ?study gt:has_publication_date ?date .");
                traitQueryString = traitQueryString.concat("?association ro:part_of ?study . ?study gt:has_publication_date ?date . ");
                associationDateFilters = associationDateFilters.concat("  FILTER ( ?date < ?? ) ").concat("  FILTER ( ?date >= ?? ) ");
                traitDateFilters = traitDateFilters.concat("  FILTER ( ?date < ?? ) ").concat("  FILTER ( ?date >= ?? ) ");
            }
        }
        associationQueryString = associationQueryString.concat(associationPvalueFilters).concat(associationDateFilters).concat(associationQueryBandFilter);
        traitQueryString = traitQueryString.concat(traitPvalueFilters).concat(traitDateFilters).concat(" }");
        System.out.println(associationQueryString);
        System.out.println(traitQueryString);
        try {
            getLog().debug("Querying SPARQL endpoint for GWAS data...");
            List<URI> chromosomes = loadChromosomes(getSparqlTemplate());
            getLog().debug("Acquired " + chromosomes.size() + " chromosomes to render");
            List<URI> associations = new ArrayList<URI>();
            associations.addAll(loadAssociations(getSparqlTemplate(), associationQueryString, renderletNexus.getRenderingContext()));
            List<URI> traits = new ArrayList<URI>();
            traits.addAll(loadTraits(getSparqlTemplate(), traitQueryString, renderletNexus.getRenderingContext()));
            getLog().debug("Acquired " + associations.size() + " associations and " + traits.size() + " to render");
            if (associations.size() == 0 && traits.size() == 0) {
                throw new NoRenderableDataException("No individuals available for rendering");
            }
            getLog().debug("GWAS data acquired, starting rendering...");
            // render chromosomes first
            for (URI chromosome : chromosomes) {
                dispatchRenderlet(renderletNexus, chromosome);
            }
            // then render individuals
            associations.parallelStream().forEach(a -> dispatchRenderlet(renderletNexus, a, SparqlAssociationRenderlet.class));
            traits.parallelStream().forEach(t -> dispatchRenderlet(renderletNexus, t, SparqlTraitRenderlet.class));
            getLog().debug("SVG rendering complete!");
            return renderletNexus.getSVG();
        } catch (SparqlQueryException e) {
            throw new RuntimeException("Failed to load data - cannot render SVG", e);
        } finally {
            getLog().debug("About to reset the renderlet nexus");
            setRendering(false);
            renderletNexus.reset();
        }
    } else {
        getLog().debug("The GWAS diagram is already being rendered");
        throw new PussycatSessionNotReadyException("The GWAS diagram is currently being rendered");
    }
}
Also used : SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ArrayList(java.util.ArrayList) PussycatSessionNotReadyException(uk.ac.ebi.spot.goci.pussycat.exception.PussycatSessionNotReadyException) URI(java.net.URI) SparqlAssociationRenderlet(uk.ac.ebi.spot.goci.sparql.pussycat.renderlet.SparqlAssociationRenderlet) NoRenderableDataException(uk.ac.ebi.spot.goci.pussycat.exception.NoRenderableDataException) Filter(uk.ac.ebi.spot.goci.pussycat.lang.Filter) SparqlTraitRenderlet(uk.ac.ebi.spot.goci.sparql.pussycat.renderlet.SparqlTraitRenderlet)

Example 4 with SparqlQueryException

use of uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException in project goci by EBISPOT.

the class SparqlTemplate method ask.

public boolean ask(String sparql) {
    sparql = getPrefixString().concat(sparql);
    if (askCache.containsKey(sparql)) {
        return askCache.get(sparql);
    }
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, q1, false);
        boolean result = execute.execAsk();
        askCache.put(sparql, result);
        return result;
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute ask '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Graph(com.hp.hpl.jena.graph.Graph) Query(com.hp.hpl.jena.query.Query) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Example 5 with SparqlQueryException

use of uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException in project goci by EBISPOT.

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, q1, false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Graph(com.hp.hpl.jena.graph.Graph) Query(com.hp.hpl.jena.query.Query) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Aggregations

SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)6 ResultSet (com.hp.hpl.jena.query.ResultSet)4 Graph (com.hp.hpl.jena.graph.Graph)3 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)3 Query (com.hp.hpl.jena.query.Query)3 QueryExecution (com.hp.hpl.jena.query.QueryExecution)3 URI (java.net.URI)3 LodeException (uk.ac.ebi.fgpt.lode.exception.LodeException)3 QuerySolution (com.hp.hpl.jena.query.QuerySolution)2 DataIntegrityViolationException (uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)2 QuerySolutionMap (com.hp.hpl.jena.query.QuerySolutionMap)1 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)1 ResourceImpl (com.hp.hpl.jena.rdf.model.impl.ResourceImpl)1 ArrayList (java.util.ArrayList)1 NoRenderableDataException (uk.ac.ebi.spot.goci.pussycat.exception.NoRenderableDataException)1 PussycatSessionNotReadyException (uk.ac.ebi.spot.goci.pussycat.exception.PussycatSessionNotReadyException)1 Filter (uk.ac.ebi.spot.goci.pussycat.lang.Filter)1 SparqlAssociationRenderlet (uk.ac.ebi.spot.goci.sparql.pussycat.renderlet.SparqlAssociationRenderlet)1 SparqlTraitRenderlet (uk.ac.ebi.spot.goci.sparql.pussycat.renderlet.SparqlTraitRenderlet)1