Search in sources :

Example 1 with SVGArea

use of uk.ac.ebi.spot.goci.pussycat.layout.SVGArea in project goci by EBISPOT.

the class ChromosomeRenderlet method render.

public void render(RenderletNexus nexus, C context, E entity) {
    int position = getPosition();
    int height = SVGCanvas.canvasHeight;
    int width = SVGCanvas.canvasWidth;
    double chromWidth = (double) width / 12;
    double chromHeight = (double) height / 2;
    double xCoordinate;
    double yCoordinate = 0;
    if (position < 12) {
        xCoordinate = position * chromWidth;
    } else {
        xCoordinate = (position - 12) * chromWidth;
        yCoordinate = (double) height / 2;
    }
    InputStream in = null;
    try {
        in = getSVGFile().openStream();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document chromSVG = db.parse(in);
        Element root = chromSVG.getDocumentElement();
        Element g = (Element) root.getElementsByTagName("g").item(0).cloneNode(true);
        setChromBands(g, nexus);
        StringBuilder builder = new StringBuilder();
        builder.append("translate(");
        builder.append(Double.toString(xCoordinate));
        builder.append(",");
        builder.append(Double.toString(yCoordinate));
        builder.append(")");
        g.setAttribute("transform", builder.toString());
        g.setAttribute("gwasname", getName());
        g.removeAttribute("title");
        SVGArea currentArea = new SVGArea(xCoordinate, yCoordinate, chromWidth, chromHeight, 0);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(g), new StreamResult(buffer));
        String str = buffer.toString();
        RenderingEvent<E> event = new RenderingEvent<E>(entity, str, currentArea, this);
        nexus.renderingEventOccurred(event);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
    } catch (SAXException e) {
        throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
    } catch (IOException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // tried our best!
            }
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) RenderingEvent(uk.ac.ebi.spot.goci.pussycat.renderlet.RenderingEvent) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea) TransformerException(javax.xml.transform.TransformerException)

Example 2 with SVGArea

use of uk.ac.ebi.spot.goci.pussycat.layout.SVGArea in project goci by EBISPOT.

the class SparqlAssociationRenderlet method getLocationOfPreviousAssociation.

protected SVGArea getLocationOfPreviousAssociation(RenderletNexus nexus, SparqlTemplate sparqlTemplate, URI association) throws DataIntegrityViolationException {
    BandInformation band = getBandInformation(sparqlTemplate, association);
    if (band != null) {
        BandInformation current = band;
        boolean done = false;
        while (!done) {
            BandInformation previousBand = getPreviousBandMap(nexus, sparqlTemplate).get(current);
            if (previousBand == null) {
                done = true;
                return null;
            }
            if (!previousBand.getChromosome().equals(current.getChromosome())) {
                done = true;
                return null;
            }
            // 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;
                    return prevLocation;
                }
            }
            // if we get to here, no associations are located in the previous region so return null
            getLog().trace("Unable to identify any associations in the previous cytogenetic region '" + previousBand.getBandName() + "'");
            current = previousBand;
        }
        return null;
    } 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)

Example 3 with SVGArea

use of uk.ac.ebi.spot.goci.pussycat.layout.SVGArea 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)

Example 4 with SVGArea

use of uk.ac.ebi.spot.goci.pussycat.layout.SVGArea in project goci by EBISPOT.

the class SparqlTraitRenderlet method getLocationsOfOtherTraitsinBand.

protected List<SVGArea> getLocationsOfOtherTraitsinBand(RenderletNexus nexus, SparqlTemplate sparqlTemplate, URI band) throws DataIntegrityViolationException {
    Set<URI> allTraits = QueryManager.getCachingInstance().getTraitsLocatedInCytogeneticBand(sparqlTemplate, band, nexus.getRenderingContext());
    getLog().trace("Identified " + allTraits.size() + " traits in band '" + band + "'");
    List<SVGArea> locations = new ArrayList<SVGArea>();
    // fetch the location of all trait + band pairs that have been rendered so far
    for (URI nextTrait : allTraits) {
        SVGArea location = nexus.getLocationOfRenderedEntity(Arrays.asList(nextTrait, band));
        if (location != null) {
            locations.add(location);
        }
    }
    // now sort
    Collections.sort(locations, new Comparator<SVGArea>() {

        @Override
        public int compare(SVGArea a1, SVGArea a2) {
            Double comp;
            double dY = a2.getY() - a1.getY();
            if (dY == 0) {
                double dX = a2.getX() - a1.getX();
                comp = dX > 0 ? Math.ceil(dX) : Math.floor(dX);
            } else {
                comp = dY > 0 ? Math.ceil(dY) : Math.floor(dY);
            }
            return comp.intValue();
        }
    });
    getLog().trace("Sorted locations for " + allTraits.size() + " traits - " + locations.size() + " have been rendered");
    return locations;
}
Also used : ArrayList(java.util.ArrayList) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea) URI(java.net.URI)

Example 5 with SVGArea

use of uk.ac.ebi.spot.goci.pussycat.layout.SVGArea 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) / 10) + 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) / 10) + 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)

Aggregations

SVGArea (uk.ac.ebi.spot.goci.pussycat.layout.SVGArea)7 DataIntegrityViolationException (uk.ac.ebi.spot.goci.pussycat.exception.DataIntegrityViolationException)4 BandInformation (uk.ac.ebi.spot.goci.pussycat.layout.BandInformation)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)2 Element (org.w3c.dom.Element)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 StringTokenizer (java.util.StringTokenizer)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1