Debian Codegraph @

uk/ac/babraham/FastQC/Graphs/TileGraph.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2013-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 package uk.ac.babraham.FastQC.Graphs;
22
23 import java.awt.BasicStroke;
24 import java.awt.Color;
25 import java.awt.Graphics;
26 import java.awt.Graphics2D;
27 import java.awt.RenderingHints;
28
29 import javax.swing.JPanel;
30
31 import uk.ac.babraham.FastQC.Modules.ModuleConfig;
32 import uk.ac.babraham.FastQC.Utilities.HotColdColourGradient;
33
34 public class TileGraph extends JPanel {
35
36 private String [] xLabels;
37 private int [] tiles;
38 private double [][]tileBaseMeans;
39 private HotColdColourGradient gradient = new HotColdColourGradient();
40
41 public TileGraph (String [] xLabels, int [] tiles, double [][] tileBaseMeans) {
42 this.xLabels = xLabels;
43 this.tiles = tiles;
44 this.tileBaseMeans = tileBaseMeans;
45
46 }
47
48
49 private int getY(double y) {
50 return (getHeight()-40) - (int)(((getHeight()-80)/(double)(tiles.length))*y);
51 }
52
53
54 public void paint (Graphics g) {
55 super.paint(g);
56
57 g.setColor(Color.WHITE);
58 g.fillRect(0, 0, getWidth(), getHeight());
59 g.setColor(Color.BLACK);
60
61 int lastY = 0;
62
63 int xOffset = 0;
64
65 for (int i=0;i<tiles.length;i++) {
66 String label = ""+tiles[i];
67 int width = g.getFontMetrics().stringWidth(label);
68 if (width > xOffset) {
69 xOffset = width;
70 }
71
72 int thisY = getY(i);
73 if (i>0 && thisY+g.getFontMetrics().getAscent() > lastY) continue;
74
75 g.drawString(label, 2, getY(i));
76 lastY = thisY;
77 }
78
79 // Give the x axis a bit of breathing space
80 xOffset += 5;
81
82 // Draw the graph title
83 String graphTitle = "Quality per tile";
84 int titleWidth = g.getFontMetrics().stringWidth(graphTitle);
85 g.drawString(graphTitle, (xOffset + ((getWidth()-(xOffset+10))/2)) - (titleWidth/2), 30);
86
87
88 // Now draw the axes
89 g.drawLine(xOffset, getHeight()-40, getWidth()-10,getHeight()-40);
90 g.drawLine(xOffset, getHeight()-40, xOffset, 40);
91
92 // Draw the xLabel under the xAxis
93 String xLabel = "Position in read (bp)";
94 g.drawString(xLabel, (getWidth()/2) - (g.getFontMetrics().stringWidth(xLabel)/2), getHeight()-5);
95
96
97 // Now draw the data points
98 int baseWidth = (getWidth()-(xOffset+10))/xLabels.length;
99 if (baseWidth<1) baseWidth=1;
100
101 // System.out.println("Base Width is "+baseWidth);
102
103 // First draw faint boxes over alternating bases so you can see which is which
104
105 // Let's find the longest label, and then work out how often we can draw labels
106
107 int lastXLabelEnd = 0;
108 g.setColor(Color.BLACK);
109
110 for (int base=0;base<xLabels.length;base++) {
111
112 String baseNumber = ""+xLabels[base];
113 int baseNumberWidth = g.getFontMetrics().stringWidth(baseNumber);
114 int baseNumberPosition = (baseWidth/2)+xOffset+(baseWidth*base)-(baseNumberWidth/2);
115
116 if (baseNumberPosition > lastXLabelEnd) {
117 g.drawString(baseNumber,baseNumberPosition, getHeight()-25);
118 lastXLabelEnd = baseNumberPosition+baseNumberWidth+5;
119 }
120 }
121
122 // Now draw the datasets
123
124 if (g instanceof Graphics2D) {
125 ((Graphics2D)g).setStroke(new BasicStroke(2));
126 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
127 }
128
129 for (int tile=0;tile<tiles.length;tile++) {
130 for (int base=0;base<xLabels.length;base++) {
131
132 g.setColor(getColour(tile,base));
133
134 int x=xOffset+(baseWidth*base);
135 int y=getY(tile+1);
136 g.fillRect(x, y, baseWidth, getY(tile)-getY(tile+1));
137
138 }
139
140 }
141
142
143 }
144
145 private Color getColour(int tile, int base) {
146 return gradient.getColor(0-tileBaseMeans[tile][base], 0, ModuleConfig.getParam("tile", "error"));
147 }
148
149
150 }