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();
}
}
}
}
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;
}
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");
}
}
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();
}
}
}
}
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();
}
}
}
}
Aggregations