Search in sources :

Example 1 with Filter

use of uk.ac.ebi.spot.goci.pussycat.lang.Filter in project goci by EBISPOT.

the class PussycatGOCIController method renderAssociations.

@RequestMapping(value = "/gwasdiagram/associations")
@ResponseBody
public String renderAssociations(@RequestParam(value = "pvaluemin", required = false) String pvalueMin, @RequestParam(value = "pvaluemax", required = false) String pvalueMax, @RequestParam(value = "datemin", required = false) String dateMin, @RequestParam(value = "datemax", required = false) String dateMax, HttpSession session) throws PussycatSessionNotReadyException, NoRenderableDataException {
    getLog().debug("Received a new rendering request - " + "putting together the query from date '" + dateMin + "' to '" + dateMax + "' and from pvalue '" + pvalueMin + "' to '" + pvalueMax + "'");
    if (pvalueMin == "") {
        pvalueMin = null;
    }
    if (pvalueMax == "") {
        pvalueMax = null;
    }
    if (dateMin == "") {
        dateMin = null;
    }
    if (dateMax == "") {
        dateMax = null;
    }
    Filter pvalueFilter = null;
    Filter dateFilter = null;
    if (pvalueMin != null || pvalueMax != null) {
        pvalueFilter = setPvalueFilter(pvalueMin, pvalueMax);
        getRenderletNexus(session).setRenderingContext(pvalueFilter);
    }
    if (dateMin != null || dateMax != null) {
        dateFilter = setDateFilter(dateMin, dateMax);
        getRenderletNexus(session).setRenderingContext(dateFilter);
    }
    if (dateFilter == null && pvalueFilter == null) {
        return getPussycatSession(session).performRendering(getRenderletNexus(session));
    } else if (dateFilter == null && pvalueFilter != null) {
        return getPussycatSession(session).performRendering(getRenderletNexus(session), pvalueFilter);
    } else if (pvalueFilter == null && dateFilter != null) {
        return getPussycatSession(session).performRendering(getRenderletNexus(session), dateFilter);
    } else {
        return getPussycatSession(session).performRendering(getRenderletNexus(session), dateFilter, pvalueFilter);
    }
}
Also used : Filter(uk.ac.ebi.spot.goci.pussycat.lang.Filter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with Filter

use of uk.ac.ebi.spot.goci.pussycat.lang.Filter 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 3 with Filter

use of uk.ac.ebi.spot.goci.pussycat.lang.Filter 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 Filter

use of uk.ac.ebi.spot.goci.pussycat.lang.Filter in project goci by EBISPOT.

the class QueryManager method getAssociationForTraitAndBand.

public List<URI> getAssociationForTraitAndBand(SparqlTemplate sparqlTemplate, URI trait, URI bandIndividual, List<Filter> filters) {
    List<URI> results = new ArrayList<URI>();
    if (filters.size() == 0) {
        Object retrieved = checkCache("getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual);
        if (retrieved != null) {
            return (List<URI>) retrieved;
        }
        List<URI> queryResults = sparqlTemplate.query(SparqlQueries.ASSOCIATIONS_FOR_TRAIT_AND_BAND, new URIMapper("association"), trait, bandIndividual);
        // de-duplicate results; should be handled by        List<URI> results = new ArrayList<URI>();
        for (URI queryResult : queryResults) {
            if (!results.contains(queryResult)) {
                results.add(queryResult);
            }
        }
        return cache(results, "getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual);
    } else if (filters.size() == 1) {
        for (Filter filter : filters) {
            if (filter.getFilteredType().equals(Association.class)) {
                Object retrieved = checkCache("getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0));
                if (retrieved != null) {
                    return (List<URI>) retrieved;
                }
                results.addAll(sparqlTemplate.query(SparqlQueries.ASSOCIATIONS_FOR_TRAIT_AND_BAND_PVALUE_FILTER, new URIMapper("association"), trait, bandIndividual, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0)));
                return cache(results, "getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0));
            } else if (filter.getFilteredType().equals(Study.class)) {
                Object retrieved = checkCache("getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual, filter.getFilteredRange().to(), filter.getFilteredRange().from());
                if (retrieved != null) {
                    return (List<URI>) retrieved;
                }
                results.addAll(sparqlTemplate.query(SparqlQueries.ASSOCIATIONS_FOR_TRAIT_AND_BAND_DATE_FILTER, new URIMapper("association"), trait, bandIndividual, filter.getFilteredRange().to(), filter.getFilteredRange().from()));
                return cache(results, "getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual, 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();
            }
        }
        Object retrieved = checkCache("getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual, pval_max, pval_min, date_max, date_min);
        if (retrieved != null) {
            return (List<URI>) retrieved;
        }
        results.addAll(sparqlTemplate.query(SparqlQueries.ASSOCIATIONS_FOR_TRAIT_AND_BAND_PVALUE_DATE_FILTER, new URIMapper("association"), trait, bandIndividual, pval_max, pval_min, date_max, date_min));
        return cache(results, "getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual, pval_max, pval_min, date_max, date_min);
    }
    return cache(results, "getAssociationsForTraitInBand", sparqlTemplate, trait, bandIndividual);
}
Also used : Association(uk.ac.ebi.spot.goci.model.Association) Filter(uk.ac.ebi.spot.goci.pussycat.lang.Filter) URI(java.net.URI)

Example 5 with Filter

use of uk.ac.ebi.spot.goci.pussycat.lang.Filter in project goci by EBISPOT.

the class QueryManager method getTraitsOrderedByIdentificationDateForBand.

public List<URI> getTraitsOrderedByIdentificationDateForBand(SparqlTemplate sparqlTemplate, URI bandIndividual, List<Filter> filters) {
    List<URI> results = new ArrayList<URI>();
    if (filters.size() == 0) {
        Object retrieved = checkCache("getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual);
        if (retrieved != null) {
            return (List<URI>) retrieved;
        }
        List<URI> queryResults = sparqlTemplate.query(SparqlQueries.DATE_OF_TRAIT_ID_FOR_BAND, new URIMapper("trait"), bandIndividual);
        // de-duplicate results; should be handled by        List<URI> results = new ArrayList<URI>();
        for (URI queryResult : queryResults) {
            if (!results.contains(queryResult)) {
                results.add(queryResult);
            }
        }
        return cache(results, "getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual);
    } else if (filters.size() == 1) {
        for (Filter filter : filters) {
            if (filter.getFilteredType().equals(Association.class)) {
                Object retrieved = checkCache("getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0));
                if (retrieved != null) {
                    return (List<URI>) retrieved;
                }
                results.addAll(sparqlTemplate.query(SparqlQueries.DATE_OF_TRAIT_ID_FOR_BAND_PVALUE_FILTER, new URIMapper("trait"), bandIndividual, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0)));
                return cache(results, "getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual, filter.getFilteredValues().get(1), filter.getFilteredValues().get(0));
            } else if (filter.getFilteredType().equals(Study.class)) {
                Object retrieved = checkCache("getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual, filter.getFilteredRange().to(), filter.getFilteredRange().from());
                if (retrieved != null) {
                    return (List<URI>) retrieved;
                }
                results.addAll(sparqlTemplate.query(SparqlQueries.DATE_OF_TRAIT_ID_FOR_BAND_DATE_FILTER, new URIMapper("trait"), bandIndividual, filter.getFilteredRange().to(), filter.getFilteredRange().from()));
                return cache(results, "getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual, 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();
            }
        }
        Object retrieved = checkCache("getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual, pval_max, pval_min, date_max, date_min);
        if (retrieved != null) {
            return (List<URI>) retrieved;
        }
        results.addAll(sparqlTemplate.query(SparqlQueries.DATE_OF_TRAIT_ID_FOR_BAND_PVALUE_DATE_FILTER, new URIMapper("trait"), bandIndividual, pval_max, pval_min, date_max, date_min));
        return cache(results, "getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual, pval_max, pval_min, date_max, date_min);
    }
    return cache(results, "getTraitsOrderedByIdentificationDateForBand", sparqlTemplate, bandIndividual);
}
Also used : Association(uk.ac.ebi.spot.goci.model.Association) Filter(uk.ac.ebi.spot.goci.pussycat.lang.Filter) URI(java.net.URI)

Aggregations

Filter (uk.ac.ebi.spot.goci.pussycat.lang.Filter)13 Association (uk.ac.ebi.spot.goci.model.Association)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 URI (java.net.URI)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ArrayList (java.util.ArrayList)2 QuerySolution (com.hp.hpl.jena.query.QuerySolution)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Test (org.junit.Test)1 Publication (uk.ac.ebi.spot.goci.model.Publication)1 SingleNucleotidePolymorphism (uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism)1 NoRenderableDataException (uk.ac.ebi.spot.goci.pussycat.exception.NoRenderableDataException)1 PussycatSessionNotReadyException (uk.ac.ebi.spot.goci.pussycat.exception.PussycatSessionNotReadyException)1 RenderletNexus (uk.ac.ebi.spot.goci.pussycat.renderlet.RenderletNexus)1 SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)1 QuerySolutionMapper (uk.ac.ebi.spot.goci.sparql.pussycat.query.QuerySolutionMapper)1