/** * Annotates a block of values of a heatmap.plus SVG file by giving them an id and row/column * attributes. A heatmap.plus can contain up to three 'blocks': row annotations, column * annotations and the matrix. * * @param nRow number of rows of this block * @param nCol number of columns of this block * @param blockId value of the id attribute to give elements in this block * @throws XMLStreamException */ private void annotateHeatMapBlock( int nRow, int nCol, String blockId, XMLEventWriter writer, XMLEventReader reader) throws XMLStreamException { int counter = 0; int nPath = nRow * nCol; int currentRow = nRow; // elements drawn from bottom to top, so start counting at last row int currentCol = 1; while (counter < nPath) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(PATH)) { // make a new start element with the same attributes plus the extra annotations @SuppressWarnings("unchecked") Iterator<Attribute> attributes = event.asStartElement().getAttributes(); StartElement newSe = eventFactory.createStartElement(new QName(PATH), attributes, null); writer.add(newSe); writer.add(eventFactory.createAttribute(ID, blockId)); writer.add(eventFactory.createAttribute(new QName("row"), Integer.toString(currentRow))); writer.add(eventFactory.createAttribute(new QName("col"), Integer.toString(currentCol))); currentRow--; if (currentRow == 0) { // finished one column, reset currentRow and increment currentCol currentRow = nRow; currentCol++; } counter++; } else { // write the rest untouched writer.add(event); } } }
@Override public Scenario parse(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException { OutputStream output = new ByteArrayOutputStream(); JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).prettyPrint(false).build(); try { /* Create source (XML). */ XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(inputStream); // XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); // Source source = new StAXSource(reader); /* Create result (JSON). */ XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output); // XMLStreamWriter writer = new JsonXMLOutputFactory(config).createXMLStreamWriter(output); // Result result = new StAXResult(writer); /* * Copy events from reader to writer. */ writer.add(reader); /* Copy source to result via "identity transform". */ // TransformerFactory.newInstance().newTransformer().transform(source, result); } catch (XMLStreamException e) { e.printStackTrace(); } finally { /* As per StAX specification, XMLStreamReader/Writer.close() doesn't close the underlying stream. */ output.close(); inputStream.close(); } /* try { json = xmlToJson(inputStream); } catch (JSONException e) { e.printStackTrace(); } String jsonString = json.toString(); System.out.println(jsonString); GenericDocument genericDocument = new GenericDocument(); genericDocument.setJson(jsonString); */ GenericDocument genericDocument = new GenericDocument(); String json = output.toString(); genericDocument.setJson(json); return genericDocument; }
/** * Can annotate SVG heatmap.plus charts made by R. Reads and writes using StAX, adding row and col * attributes to <path> elements corresponding to data points in the heatmap. All indexes can be * calculated using nRow, nCol, nRowAnnotations and nColAnnotations. * * @param chart * @throws FactoryConfigurationError * @throws XMLStreamException * @throws FileNotFoundException */ public void annotateHeatMap(HeatMapChart chart) throws XMLStreamException, FactoryConfigurationError, FileNotFoundException { XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new FileInputStream(inFile)); OutputStream os = new FileOutputStream(outFile); XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(os); // get values from HeatMapChart int nRow = chart.getData().getRowTargets().size(); int nCol = chart.getData().getColumnTargets().size(); // TODO get from HeatMapChart: int nRowAnnotations = 0; int nColAnnotations = 0; // skip the headers and <def> bit until we reach <g id=""> while (true) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement()) { StartElement se = event.asStartElement(); if (se.getName().getLocalPart().equals(G) && se.getAttributeByName(ID) != null) { LOG.info("<g id=\"\"> reached"); writer.add(event); break; } } writer.add(event); } // annotation begins here // ROW ANNOTATIONS if (nRowAnnotations > 0) { LOG.info("parsing " + nRowAnnotations + " row annotations"); annotateHeatMapBlock(nRow, nRowAnnotations, "rowAnnotation", writer, reader); } // COLUMN ANNOTATIONS if (nColAnnotations > 0) { LOG.info("parsing " + nColAnnotations + " col annotations"); annotateHeatMapBlock(nColAnnotations, nCol, "colAnnotatation", writer, reader); } // MATRIX ANNOTATIONS LOG.info("parsing " + (nRow * nCol) + " matrix values"); annotateHeatMapBlock(nRow, nCol, "matrix", writer, reader); // COLUMN NAMES LOG.info("parsing " + nCol + " column names"); int counter = 0; while (counter < nCol) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(G)) { @SuppressWarnings("unchecked") Iterator<Attribute> attributes = event.asStartElement().getAttributes(); StartElement newSe = eventFactory.createStartElement(new QName(G), attributes, null); writer.add(newSe); writer.add(eventFactory.createAttribute(ID, "colName")); writer.add(eventFactory.createAttribute(new QName("col"), Integer.toString(counter + 1))); counter++; } else { writer.add(event); } } // ROW NAMES LOG.info("parsing " + nRow + " row names"); counter = 0; while (counter < nRow) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(G)) { @SuppressWarnings("unchecked") Iterator<Attribute> attributes = event.asStartElement().getAttributes(); StartElement newSe = eventFactory.createStartElement(new QName(G), attributes, null); writer.add(newSe); writer.add(eventFactory.createAttribute(ID, "rowName")); writer.add( eventFactory.createAttribute(new QName("row"), Integer.toString(nRow - counter))); counter++; } else { writer.add(event); } } // finish rest of file while (reader.hasNext()) { XMLEvent event = (XMLEvent) reader.next(); if (event.isEndElement()) { // close the <g id=""> tag, right before the </svg> end element if (event.asEndElement().getName().getLocalPart().equals("svg")) { EndElement newEe = eventFactory.createEndElement(new QName(G), null); writer.add(newEe); } } writer.add(event); } writer.close(); }