Search in sources :

Example 1 with RenderingEvent

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

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)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 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 SAXException (org.xml.sax.SAXException)1 SVGArea (uk.ac.ebi.spot.goci.pussycat.layout.SVGArea)1 RenderingEvent (uk.ac.ebi.spot.goci.pussycat.renderlet.RenderingEvent)1