package gem;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Ozgun Babur
*/
public class PlotVector
{
static final int WIDTH = 600;
static final int HEIGHT = 600;
static final Map<String, Color> colorMap = new HashMap<String, Color>();
public static void main(String[] args) throws Throwable
{
generateHistoplot("result/GraphML/ABCA1|BC034824|1570279_at/trips.xls", "result/vectors.svg");
}
public static void generateHistoplot(String tripFile, String outFile) throws IOException
{
// Read the triplet data
List<Triplet> list = Triplet.readTrips(tripFile);
for (Triplet t : list)
{
t.backFromURLToIDs();
}
generateHistoplot(list, outFile);
}
public static void generateHistoplot(List<Triplet> trips, String outFile) throws IOException
{
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
paint(svgGenerator, trips);
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
boolean useCSS = true; // we want to use CSS style attributes
Writer out = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
svgGenerator.stream(out, useCSS);
}
public static void paint(Graphics2D g, List<Triplet> trips)
{
Point center = new Point(WIDTH / 2, HEIGHT / 2);
g.drawLine(center.x, 0, center.x, HEIGHT);
g.drawLine(0, center.y, WIDTH, center.y);
g.drawString("dF", WIDTH - 20, center.y - 2);
g.drawString("dM", center.x + 5, 10);
for (Triplet t : trips)
{
int[] n = Difference.calcTotals(t.cnt);
double[] p = Difference.calcProportions(t.cnt, n);
double aF = Difference.calcAlphaF(p);
double aM = Difference.calcAlphaM(p);
double bF = Difference.calcBetaF(p);
double bM = Difference.calcBetaM(p);
int x2 = (int) ((bF + 1) * center.x);
int y2 = HEIGHT - (int) ((bM + 1) * center.y);
g.setColor(colorMap.get(t.categ));
g.drawLine((int)((aF + 1) * center.x), HEIGHT - (int)((aM + 1) * center.y), x2, y2);
double gamma = Difference.calcGamma(p);
if (gamma > 0)
{
drawUpArrow(g, x2, y2);
g.drawString(Triplet.getGeneToSymbolMap().get(t.modulator), x2 - 5, y2 - 5);
}
else
{
drawDownArrow(g, x2, y2);
g.drawString(Triplet.getGeneToSymbolMap().get(t.modulator), x2 - 5, y2 + 5);
}
}
}
public static void drawUpArrow(Graphics2D g, int x, int y)
{
g.drawLine(x - 3, y, x, y);
g.drawLine(x, y, x, y + 3);
}
public static void drawDownArrow(Graphics2D g, int x, int y)
{
g.drawLine(x, y, x + 3, y);
g.drawLine(x, y - 3, x, y);
}
static
{
colorMap.put("Enhances Activation", new Color(33, 137, 33));
colorMap.put("Enhances Inhibition", new Color(106, 33, 137));
colorMap.put("Attenuates Activation", new Color(144, 238, 144));
colorMap.put("Attenuates Inhibition", new Color(210, 143, 238));
colorMap.put("Inverts Activation", new Color(253, 48, 48));
colorMap.put("Inverts Inhibition", new Color(253, 207, 47));
colorMap.put("MoA Insignificant", new Color(150, 150, 150));
colorMap.put("OR_ACTIVATION", new Color(100, 100, 200));
colorMap.put("OR_INHIBITION", new Color(100, 100, 200));
}
}