Search in sources :

Example 6 with SVGArea

use of uk.ac.ebi.spot.goci.pussycat.layout.SVGArea 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 % 10;
                    int vertical = position / 10;
                    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);
                    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 7 with SVGArea

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

the class ChromosomeRenderlet method setChromBands.

protected void setChromBands(Element g, RenderletNexus nexus) {
    NodeList paths = g.getElementsByTagName("path");
    for (int i = 0; i < paths.getLength(); i++) {
        Element path = (Element) paths.item(i);
        String id = path.getAttribute("id");
        String d = path.getAttribute("d");
        if (!id.contains("centre") && !id.contains("outline") && !id.contains("satellite") && !id.contains("-o") && !id.contains("centromere")) {
            StringTokenizer tokenizer = new StringTokenizer(d);
            ArrayList<String> pathElements = new ArrayList<String>();
            while (tokenizer.hasMoreTokens()) {
                pathElements.add(tokenizer.nextToken());
            }
            //bottom left corner of the chromosomal band
            String[] xy = pathElements.get(1).split(",");
            double x = Double.parseDouble(xy[0]);
            double y = Double.parseDouble(xy[1]);
            double width;
            double height = 0;
            //special case for top- and bottom-most bands
            if (pathElements.contains("c")) {
                //top band: starts bottom left -> br -> arc
                if (id.contains("p")) {
                    double original = y;
                    String[] w = pathElements.get(2).split(",");
                    width = Math.abs(Double.parseDouble((w[0])));
                    for (int k = 3; k < pathElements.size() - 1; k++) {
                        String element = pathElements.get(k);
                        if (element.contains("c")) {
                            while (!pathElements.get(k + 1).contains("l") && !pathElements.get(k + 1).contains("z")) {
                                k = k + 3;
                                String point = pathElements.get(k);
                                String[] coords = point.split(",");
                                double temp = Double.parseDouble(coords[1]);
                                if (temp < 0) {
                                    y = y + temp;
                                }
                            }
                        } else if (element.contains("l")) {
                            k = k + 1;
                            String point = pathElements.get(k);
                            String[] coords = point.split(",");
                            double temp = Double.parseDouble(coords[1]);
                            if (temp < 0) {
                                y = y + temp;
                            }
                        } else {
                            String point = pathElements.get(k);
                            String[] coords = point.split(",");
                            double temp = Double.parseDouble(coords[1]);
                            if (temp < 0) {
                                y = y + temp;
                            }
                        }
                        height = original - y;
                    }
                } else //bottom band: starts top left -> arc -> tr
                {
                    int last = pathElements.size() - 2;
                    String[] w = pathElements.get(last).split(",");
                    width = Math.abs(Double.parseDouble(w[0]));
                    height = 0;
                    for (int j = 3; j < pathElements.size() - 1; j++) {
                        String element = pathElements.get(j);
                        if (element.contains("c")) {
                            while (!pathElements.get(j + 1).contains("l")) {
                                j = j + 3;
                                String point = pathElements.get(j);
                                String[] coords = point.split(",");
                                double temp = Double.parseDouble(coords[1]);
                                if (temp > 0) {
                                    height = height + temp;
                                }
                            }
                        } else if (element.contains("l")) {
                            j = j + 1;
                            String point = pathElements.get(j);
                            String[] coords = point.split(",");
                            double temp = Double.parseDouble(coords[1]);
                            if (temp > 0) {
                                height = height + temp;
                            }
                        } else {
                            String point = pathElements.get(j);
                            String[] coords = point.split(",");
                            double temp = Double.parseDouble(coords[1]);
                            if (temp > 0) {
                                height = height + temp;
                            }
                        }
                    }
                }
            } else //all the other bands
            {
                //width of the band
                String[] w = pathElements.get(2).split(",");
                width = Math.abs(Double.parseDouble((w[0])));
                //height of the band
                String[] h = pathElements.get(3).split(",");
                height = Math.abs(Double.parseDouble((h[1])));
                y = y - height;
            }
            // SVG area for the chromosomal bands gives the x&y coordinates for its top left corner, and its width and height
            SVGArea band = new SVGArea(x, y, width, height, 0);
            String chromName = getName().split(" ")[1];
            BandInformation info = new BandInformation(id, chromName);
            nexus.renderingEventOccurred(new RenderingEvent<BandInformation>(info, "", band, this));
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) BandInformation(uk.ac.ebi.spot.goci.pussycat.layout.BandInformation) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea)

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