Search in sources :

Example 6 with QuerySolution

use of com.hp.hpl.jena.query.QuerySolution in project goci by EBISPOT.

the class SparqlPussycatSession method loadAssociations.

private List<URI> loadAssociations(SparqlTemplate sparqlTemplate, String queryString, List<Filter> filters) {
    List<AssociationLocation> associationLocations = null;
    if (filters.size() == 0) {
        associationLocations = sparqlTemplate.query(queryString, /*had to add this line in to exclude "NR" bands as they break the AssociationLocation bit below
                                                                                    and can't be rendered anyway*/
        new QuerySolutionMapper<AssociationLocation>() {

            @Override
            public AssociationLocation mapQuerySolution(QuerySolution qs) {
                URI association = URI.create(qs.getResource("association").getURI());
                String bandName = qs.getLiteral("band").getLexicalForm();
                return new AssociationLocation(association, bandName);
            }
        });
    } else if (filters.size() == 1) {
        for (Filter filter : filters) {
            if (filter.getFilteredType().equals(Association.class)) {
                associationLocations = sparqlTemplate.query(queryString, /*had to add this line in to exclude "NR" bands as they break the AssociationLocation bit below
                                                                                    and can't be rendered anyway*/
                new QuerySolutionMapper<AssociationLocation>() {

                    @Override
                    public AssociationLocation mapQuerySolution(QuerySolution qs) {
                        URI association = URI.create(qs.getResource("association").getURI());
                        String bandName = qs.getLiteral("band").getLexicalForm();
                        return new AssociationLocation(association, bandName);
                    }
                }, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0));
            } else if (filter.getFilteredType().equals(Study.class)) {
                associationLocations = sparqlTemplate.query(queryString, /*had to add this line in to exclude "NR" bands as they break the AssociationLocation bit below
                                                                                    and can't be rendered anyway*/
                new QuerySolutionMapper<AssociationLocation>() {

                    @Override
                    public AssociationLocation mapQuerySolution(QuerySolution qs) {
                        URI association = URI.create(qs.getResource("association").getURI());
                        String bandName = qs.getLiteral("band").getLexicalForm();
                        return new AssociationLocation(association, bandName);
                    }
                }, filter.getFilteredRange().to(), filter.getFilteredRange().from());
            }
        }
    } else {
        Object pval_min = null, pval_max = null, date_min = null, date_max = null;
        for (Filter filter : filters) {
            if (filter.getFilteredType().equals(Association.class)) {
                pval_min = filter.getFilteredValues().get(0);
                pval_max = filter.getFilteredValues().get(1);
            } else if (filter.getFilteredType().equals(Publication.class)) {
                date_min = filter.getFilteredRange().from();
                date_max = filter.getFilteredRange().to();
            }
        }
        associationLocations = sparqlTemplate.query(queryString, /*had to add this line in to exclude "NR" bands as they break the AssociationLocation bit below
                                                                                    and can't be rendered anyway*/
        new QuerySolutionMapper<AssociationLocation>() {

            @Override
            public AssociationLocation mapQuerySolution(QuerySolution qs) {
                URI association = URI.create(qs.getResource("association").getURI());
                String bandName = qs.getLiteral("band").getLexicalForm();
                return new AssociationLocation(association, bandName);
            }
        }, pval_max, pval_min, date_max, date_min);
    }
    Collections.sort(associationLocations);
    List<URI> associations = new ArrayList<URI>();
    for (AssociationLocation al : associationLocations) {
        associations.add(al.getAssociation());
    }
    return associations;
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) QuerySolutionMapper(uk.ac.ebi.spot.goci.sparql.pussycat.query.QuerySolutionMapper) Association(uk.ac.ebi.spot.goci.model.Association) QuerySolution(com.hp.hpl.jena.query.QuerySolution) Filter(uk.ac.ebi.spot.goci.pussycat.lang.Filter)

Example 7 with QuerySolution

use of com.hp.hpl.jena.query.QuerySolution 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 8 with QuerySolution

use of com.hp.hpl.jena.query.QuerySolution in project nextprot-api by calipho-sib.

the class DiffBaseTest method getCountForSparql.

private int getCountForSparql() {
    long t0 = System.currentTimeMillis();
    String query = sparqlDictionary.getSparqlWithPrefixes(qName);
    QueryExecution qExec = sparqlService.queryExecution(query);
    com.hp.hpl.jena.query.ResultSet rs = qExec.execSelect();
    QuerySolution qs = rs.next();
    Literal lit = qs.getLiteral("cnt");
    int count = lit.getInt();
    timeSPARQL = (int) (System.currentTimeMillis() - t0);
    countSPARQL = count;
    return countSPARQL;
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) Literal(com.hp.hpl.jena.rdf.model.Literal) QueryExecution(com.hp.hpl.jena.query.QueryExecution) SparqlEndpoint(org.nextprot.api.rdf.service.SparqlEndpoint)

Example 9 with QuerySolution

use of com.hp.hpl.jena.query.QuerySolution in project nextprot-api by calipho-sib.

the class RdfHelpServiceImpl method getTripleInfoList.

@Override
public List<TripleInfo> getTripleInfoList(String rdfType) {
    String queryBase = sparqlDictionary.getSparqlWithPrefixes("typepred");
    Set<TripleInfo> tripleList = new TreeSet<TripleInfo>();
    try {
        String query = sparqlDictionary.getSparqlOnly("prefix");
        query += queryBase.replace(":SomeRdfType", rdfType);
        QueryExecution qExec = sparqlService.queryExecution(query);
        ResultSet rs = qExec.execSelect();
        while (rs.hasNext()) {
            QuerySolution sol = rs.next();
            TripleInfo ti = new TripleInfo();
            String pred = (String) getDataFromSolutionVar(sol, "pred");
            String sspl = (String) getDataFromSolutionVar(sol, "subjSample");
            String ospl = (String) getDataFromSolutionVar(sol, "objSample", true);
            String spl = sspl + " " + pred + " " + ospl + " .";
            ti.setTripleSample(spl);
            ti.setPredicate(pred);
            ti.setSubjectType((String) getDataFromSolutionVar(sol, "subjType"));
            String objectType = (String) getDataFromSolutionVar(sol, "objType");
            if (objectType.length() == 0) {
                objectType = getObjectTypeFromSample(sol, "objSample");
                ti.setLiteralType(true);
            }
            ti.setObjectType(objectType);
            ti.setTripleCount(Integer.valueOf((String) getDataFromSolutionVar(sol, "objCount")));
            LOGGER.info(ti);
            tripleList.add(ti);
        }
        qExec.close();
    } catch (Exception e) {
        incrementErrors();
        System.err.println("Error with " + rdfType);
        e.printStackTrace();
        LOGGER.error("Error with " + rdfType, e);
    }
    return new ArrayList<TripleInfo>(tripleList);
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeSet(java.util.TreeSet) ResultSet(com.hp.hpl.jena.query.ResultSet) ArrayList(java.util.ArrayList) TripleInfo(org.nextprot.api.rdf.domain.TripleInfo) QueryExecution(com.hp.hpl.jena.query.QueryExecution) NextProtException(org.nextprot.api.commons.exception.NextProtException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with QuerySolution

use of com.hp.hpl.jena.query.QuerySolution in project eol-globi-data by jhpoelen.

the class StudyImporterForSaproxylic method toInteractions.

public void toInteractions(ResultSet results) throws StudyImporterException {
    final InteractionListener listener = new InteractionListenerImpl(nodeFactory, getGeoNamesService(), getLogger());
    while (results.hasNext()) {
        QuerySolution next = results.next();
        Iterator<String> nameIter = next.varNames();
        Map<String, String> props = new TreeMap<>();
        while (nameIter.hasNext()) {
            String key = nameIter.next();
            RDFNode rdfNode = next.get(key);
            if (rdfNode.isURIResource()) {
                props.put(key, next.getResource(key).getURI());
            } else {
                props.put(key, next.getLiteral(key).getString());
            }
        }
        props.put(StudyImporterForTSV.STUDY_SOURCE_CITATION, getDataset().getCitation());
        listener.newLink(props);
    }
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeMap(java.util.TreeMap) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Aggregations

QuerySolution (com.hp.hpl.jena.query.QuerySolution)14 ResultSet (com.hp.hpl.jena.query.ResultSet)10 QueryExecution (com.hp.hpl.jena.query.QueryExecution)9 Query (com.hp.hpl.jena.query.Query)4 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)3 Model (com.hp.hpl.jena.rdf.model.Model)3 HashMap (java.util.HashMap)3 TreeSet (java.util.TreeSet)3 Literal (com.hp.hpl.jena.rdf.model.Literal)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Study (org.eol.globi.domain.Study)2 Test (org.junit.Test)2 DataIntegrityViolationException (uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)2 SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)2 TreeNode (ca.corefacility.bioinformatics.irida.util.TreeNode)1 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1