use of uk.ac.ebi.spot.goci.pussycat.layout.BandInformation 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);
}
}
use of uk.ac.ebi.spot.goci.pussycat.layout.BandInformation 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));
}
}
}
use of uk.ac.ebi.spot.goci.pussycat.layout.BandInformation 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");
}
}
use of uk.ac.ebi.spot.goci.pussycat.layout.BandInformation 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");
}
}
use of uk.ac.ebi.spot.goci.pussycat.layout.BandInformation in project goci by EBISPOT.
the class SparqlAssociationRenderlet method sortBandsWithData.
protected Map<BandInformation, BandInformation> sortBandsWithData(SparqlTemplate sparqlTemplate) {
Map<BandInformation, BandInformation> bandMap = new HashMap<BandInformation, BandInformation>();
// use the sparqlTemplate to get all individuals of type "cytogenic region"
getLog().trace("Retrieving all cytogenetic bands to sort into rendering order...");
List<BandInformation> bands = sparqlTemplate.query("SELECT DISTINCT ?band WHERE { ?bandUri a gt:CytogeneticRegion ; rdfs:label ?band . FILTER (STR(?band) != 'NR') .}", new QuerySolutionMapper<BandInformation>() {
@Override
public BandInformation mapQuerySolution(QuerySolution qs) {
return new BandInformation(qs.getLiteral("band").getLexicalForm());
}
});
getLog().trace("Got " + bands.size() + " bands, starting sorting...");
// now we've added all band info, sort the set of unique bands
Collections.sort(bands);
for (int i = 1; i < bands.size(); i++) {
BandInformation band = bands.get(i);
BandInformation previousBand = bands.get(i - 1);
bandMap.put(band, previousBand);
}
getLog().trace("Mapped " + bandMap.keySet().size() + " bands to their 'previous' band");
return bandMap;
}
Aggregations