Search in sources :

Example 1 with DataIntegrityViolationException

use of uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException 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 2 with DataIntegrityViolationException

use of uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException in project goci by EBISPOT.

the class AssociationRenderlet method render.

@Override
public void render(RenderletNexus nexus, C context, E associationEntity) {
    getLog().trace("Association: " + associationEntity);
    if (!nexus.bandContextExists(context)) {
        nexus.setBandContext(context, sortBandsWithData(context));
    }
    try {
        BandInformation band = getBandInformation(context, associationEntity);
        if (!nexus.alreadyRendered(band)) {
            // there is no other association in this chromosomal band yet - render
            getLog().trace("This is the first association for band '" + band.getBandName() + "', " + "rendering new line");
            StringBuilder svg = new StringBuilder();
            String transform = getTransformation(band.getChromosome());
            svg.append("<g ").append("id='").append(getAssociationID(context, associationEntity)).append("' ").append("transform='").append(transform).append("' ").append("class='gwas-trait'>");
            // get rendered svg location of the cytogenetic band
            SVGArea location = nexus.getLocationOfRenderedEntity(band);
            if (location != null) {
                // starting x and y co-ords derived from cytogenetic band
                double x1 = location.getX();
                double y1 = location.getY() + (location.getHeight() / 2);
                double dotRadius = 0.35 * location.getWidth();
                // x2 and y2 mark the end of the horizontal line designating the snp location
                double x2 = location.getWidth();
                double y2 = 0;
                // x3 and y3 mark the end of the line - may be fanned to avoid overlaps
                // 0.75 * location.getWidth();
                double x3 = 2 * location.getWidth();
                double y3;
                // fanning algorithm - calculate diagonal part of the line, if necessary
                Map<BandInformation, BandInformation> previousBandMap = nexus.getBandContext(context);
                BandInformation previousBand = previousBandMap.get(band);
                if (previousBand != null && band.getChromosome().equals(previousBand.getChromosome())) {
                    SVGArea previousLocation = getLocationOfPreviousAssociation(nexus, context, associationEntity);
                    if (previousLocation != null) {
                        double previousY = previousLocation.getY() + previousLocation.getHeight();
                        // fan up or down?
                        if (band.getBandName().contains("p")) {
                            // p arm - we need to know how many traits are in this band
                            int traitCount = getNumberOfTraitsInSameBand(nexus, context, associationEntity);
                            int rowCount = ((traitCount - 1) / 20) + 2;
                            double blockSize = rowCount * dotRadius;
                            if (y1 + blockSize > previousY) {
                                // if blockSize takes us down so far it would overlap prevY, move up
                                y3 = previousY - (y1 + blockSize);
                            } else {
                                // otherwise, line can be horizontal
                                y3 = 0;
                            }
                        } else {
                            // q arm - we need to know how many traits were in the previous band (ie. the one above)
                            int traitCount = getNumberOfTraitsInPreviousBand(nexus, context, associationEntity);
                            int rowCount = ((traitCount - 1) / 20) + 2;
                            double blockSize = rowCount * dotRadius;
                            if (previousY + blockSize > y1) {
                                // if the previous blockSize takes us down so far it would overlap y, move down
                                y3 = (previousY + blockSize) - y1;
                            } else {
                                // otherwise, line can be horizontal
                                y3 = 0;
                            }
                        }
                    } else {
                        // no previous location, so line can be horizontal
                        y3 = 0;
                    }
                } else {
                    // no previous band, or isn't in the same chromosome, so line can be horizontal
                    y3 = 0;
                }
                StringBuilder d = new StringBuilder();
                d.append("m ");
                d.append(Double.toString(x1)).append(",").append(Double.toString(y1));
                d.append(" ");
                d.append(Double.toString(x2)).append(",").append(Double.toString(y2));
                d.append(" ");
                d.append(Double.toString(x3)).append(",").append(Double.toString(y3));
                svg.append("<path ").append("d='").append(d.toString()).append("' ").append("style='fill:none;stroke:#211c1d;stroke-width:1.1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none'").append(" />");
                svg.append("</g>");
                SVGArea currentArea = new SVGArea(x1, y1, x2 + x3, y2 + y3, transform, 0);
                RenderingEvent<E> event = new RenderingEvent<E>(associationEntity, svg.toString(), currentArea, this);
                nexus.renderingEventOccurred(event);
                // add band to renderedBands set
                nexus.setRenderedBand(band, currentArea);
            } else {
                getLog().error("Unable to render association '" + associationEntity + "' - " + "no location for band '" + band.getBandName() + "'");
            }
        } else {
            // we've already rendered the required association line, so we don't need to do it again
            // but we do need to log the rendering event for this association individual
            getLog().trace("Already rendered an association line to band '" + band.getBandName() + "', " + "logging secondary event for association '" + associationEntity + "'");
            SVGArea area = nexus.getRenderedBand(band);
            nexus.renderingEventOccurred(new RenderingEvent<E>(associationEntity, "", area, this));
        }
    } catch (DataIntegrityViolationException e) {
        getLog().error("Cannot render association '" + associationEntity + "'", e);
    }
}
Also used : BandInformation(uk.ac.ebi.spot.goci.pussycat.layout.BandInformation) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea) DataIntegrityViolationException(uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)

Example 3 with DataIntegrityViolationException

use of uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException in project goci by EBISPOT.

the class TraitRenderlet method render.

@Override
public void render(RenderletNexus nexus, C context, E trait) {
    getLog().trace("Trait: " + trait);
    try {
        // collect all the bands for which we need to render this trait, and list all associations in that band
        Map<E, Set<E>> bandToAssociationMap = new HashMap<E, Set<E>>();
        for (E association : getAssociationsForTrait(nexus, context, trait)) {
            try {
                // get the band for this association
                E band = getBandForAssociation(context, association);
                if (!bandToAssociationMap.containsKey(band)) {
                    bandToAssociationMap.put(band, new HashSet<E>());
                }
                bandToAssociationMap.get(band).add(association);
            } catch (DataIntegrityViolationException e) {
                getLog().error("Unable to render trait '" + trait + "' for association '" + association + ": " + e.getMessage());
            }
        }
        for (E band : bandToAssociationMap.keySet()) {
            try {
                // get the location of any traits previously rendered in this band
                List<SVGArea> locations = getLocationsOfOtherTraitsinBand(nexus, context, band);
                StringBuilder svg = new StringBuilder();
                svg.append("<circle ");
                // also grab the location of an association (should all be the same for the same band)
                E association = bandToAssociationMap.get(band).iterator().next();
                SVGArea associationLocation = nexus.getLocationOfRenderedEntity(association);
                if (associationLocation != null) {
                    if (associationLocation.getTransform() != null) {
                        svg.append("transform='").append(associationLocation.getTransform()).append("' ");
                    }
                    double alength = associationLocation.getWidth();
                    // 0.2 * alength;
                    double radius = 0.12 * alength;
                    double ax = associationLocation.getX();
                    double ay = associationLocation.getY();
                    double displacement = associationLocation.getHeight();
                    double cx, cy;
                    int position = getTraitPosition(nexus, context, trait, band, locations);
                    int horizontal = position % 20;
                    int vertical = position / 20;
                    if (position == 0) {
                        cx = ax + alength + radius;
                    } else {
                        if (vertical % 2 == 0) {
                            cx = ax + alength + (((2 * horizontal) + 1) * radius);
                        } else {
                            cx = ax + alength + (((2 * horizontal) + 2) * radius);
                        }
                    }
                    cy = ay + displacement + (vertical * radius);
                    svg.append("cx='").append(Double.toString(cx)).append("' ");
                    svg.append("cy='").append(Double.toString(cy)).append("' ");
                    svg.append("r='").append(Double.toString(radius)).append("' ");
                    String colour = getTraitColour(context, trait);
                    svg.append("fill='").append(colour).append("' ");
                    svg.append("stroke='black' ");
                    svg.append("stroke-width='0.5' ");
                    String traitName = getTraitLabel(context, trait).replace('"', '\'');
                    svg.append("gwasname=\"").append(traitName).append("\" ");
                    String traitAttribute = getTraitAttribute(context, trait);
                    getLog().trace("Setting CSS class for trait '" + trait + "' to " + traitAttribute);
                    svg.append("class='gwas-trait ").append(traitAttribute).append("' ");
                    svg.append("fading='false' ");
                    StringBuilder associationAttribute = new StringBuilder();
                    Iterator<E> associationIt = bandToAssociationMap.get(band).iterator();
                    while (associationIt.hasNext()) {
                        associationAttribute.append(getTraitAssociationAttribute(context, associationIt.next()));
                        if (associationIt.hasNext()) {
                            associationAttribute.append(",");
                        }
                    }
                    getLog().trace("Setting gwasassociation attribute for trait '" + trait + "' to " + associationAttribute.toString());
                    svg.append("gwasassociation='").append(associationAttribute.toString()).append("' ");
                    svg.append("priority='").append(vertical).append(// todo - remove this, just for debugging
                    "' ");
                    svg.append("/>");
                    SVGArea currentArea = new SVGArea(cx, cy, 2 * radius, 2 * radius, 0);
                    // this area is a conjunction of trait + band, so store as a List<OWLNamedIndividual> with 2 elements
                    RenderingEvent<List<E>> event = new RenderingEvent<List<E>>(Arrays.asList(trait, band), svg.toString(), currentArea, vertical, this);
                    nexus.renderingEventOccurred(event);
                } else {
                    getLog().error("Cannot render trait '" + trait + "' for association '" + association + "' - " + "failed to identify the location of this association. " + "This could be due to an earlier error rendering the association.");
                }
            } catch (DataIntegrityViolationException e) {
                getLog().error("Cannot render trait '" + trait + "' in band '" + band + "'");
            }
        }
    } catch (DataIntegrityViolationException e) {
        getLog().error("Cannot render trait '" + trait + "'", e);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) DataIntegrityViolationException(uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException) List(java.util.List) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea)

Example 4 with DataIntegrityViolationException

use of uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException in project goci by EBISPOT.

the class SparqlTemplate method type.

public URI type(final URI entity) {
    String sparql = getPrefixString().concat("SELECT DISTINCT ?type WHERE { <" + entity.toString() + "> rdf:type ?type . " + "FILTER ( ?type != owl:Class ) . " + "FILTER ( ?type != owl:NamedIndividual ) }");
    if (typeCache.containsKey(sparql)) {
        return typeCache.get(sparql);
    }
    URI result = query(sparql, new ResultSetMapper<URI>() {

        @Override
        public URI mapResultSet(ResultSet rs) {
            URI result = null;
            while (rs.hasNext()) {
                if (result != null) {
                    throw new SparqlQueryException(new DataIntegrityViolationException("More than one non-owl rdf:type for' " + entity.toString() + "'"));
                }
                QuerySolution qs = rs.next();
                result = URI.create(qs.getResource("type").getURI());
            }
            return result;
        }
    });
    typeCache.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) URI(java.net.URI) DataIntegrityViolationException(uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)

Example 5 with DataIntegrityViolationException

use of uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException in project goci by EBISPOT.

the class SparqlAssociationRenderlet method getNumberOfTraitsInPreviousBand.

/**
 * For the given association, identifies the previous cytogenetic band to the one this association is located in,
 * then identifies the total number of traits located in that cytogenetic band and returns the count
 *
 * @param sparqlTemplate the sparqlTemplate
 * @param association    the association to identify co-located traits for
 * @return the number of traits in the same cytogenetic region as this association
 * @throws DataIntegrityViolationException
 */
protected int getNumberOfTraitsInPreviousBand(RenderletNexus nexus, SparqlTemplate sparqlTemplate, URI association) throws DataIntegrityViolationException {
    BandInformation band = getBandInformation(sparqlTemplate, association);
    if (band != null) {
        BandInformation current = band;
        BandInformation previousBand = null;
        boolean done = false;
        while (!done) {
            previousBand = getPreviousBandMap(nexus, sparqlTemplate).get(current);
            // now find the traits in the previous band
            Set<URI> previousBandAssociations = QueryManager.getCachingInstance().getAssociationsLocatedInCytogeneticBand(sparqlTemplate, previousBand.getBandName(), nexus.getRenderingContext());
            // get first not-null location for an association in the previous band
            for (URI previousBandAssociation : previousBandAssociations) {
                SVGArea prevLocation = nexus.getLocationOfRenderedEntity(previousBandAssociation);
                if (prevLocation != null) {
                    done = true;
                }
            }
            current = previousBand;
        }
        Set<URI> previousBandTraits = QueryManager.getCachingInstance().getTraitsLocatedInCytogeneticBand(sparqlTemplate, previousBand.getBandName(), nexus.getRenderingContext());
        return previousBandTraits.size();
    } else {
        throw new DataIntegrityViolationException("Unable to identify the cytogenetic region where association '" + association + "' is located");
    }
}
Also used : BandInformation(uk.ac.ebi.spot.goci.pussycat.layout.BandInformation) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea) URI(java.net.URI) DataIntegrityViolationException(uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)

Aggregations

DataIntegrityViolationException (uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)6 SVGArea (uk.ac.ebi.spot.goci.pussycat.layout.SVGArea)4 URI (java.net.URI)3 BandInformation (uk.ac.ebi.spot.goci.pussycat.layout.BandInformation)3 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)2 QuerySolution (com.hp.hpl.jena.query.QuerySolution)2 ResultSet (com.hp.hpl.jena.query.ResultSet)2 SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1