Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-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 package uk.ac.babraham.FastQC.Graphs;
21
22 import java.awt.Color;
23 import java.awt.Graphics;
24
25 import javax.swing.JPanel;
26
27 public class QualityBoxPlot extends JPanel {
28
29 private double [] means;
30 private double [] medians;
31 private double [] lowest;
32 private double [] highest;
33 private double [] lowerQuartile;
34 private double [] upperQuartile;
35 private String [] xLabels;
36 private String graphTitle;
37 private double minY;
38 private double maxY;
39 private double yInterval;
40
41 private static final Color GOOD = new Color(195,230,195);
42 private static final Color BAD = new Color(230,220,195);
43 private static final Color UGLY = new Color(230,195,195);
44
45 private static final Color GOOD_DARK = new Color(175,230,175);
46 private static final Color BAD_DARK = new Color(230,215,175);
47 private static final Color UGLY_DARK = new Color(230,175,175);
48
49 public QualityBoxPlot (double [] means, double [] medians, double [] lowest, double [] highest, double [] lowerQuartile, double [] upperQuartile, double minY, double maxY, double yInterval, String [] xLabels, String graphTitle) {
50
51 this.means = means;
52 this.medians = medians;
53 this.lowest = lowest;
54 this.highest = highest;
55 this.lowerQuartile = lowerQuartile;
56 this.upperQuartile = upperQuartile;
57 this.minY = minY;
58 this.maxY = maxY;
59 this.yInterval = yInterval;
60 this.xLabels = xLabels;
61 this.graphTitle = graphTitle;
62 this.yInterval = yInterval;
63 }
64
65 public void paint (Graphics g) {
66 super.paint(g);
67
68 g.setColor(Color.WHITE);
69 g.fillRect(0, 0, getWidth(), getHeight());
70 g.setColor(Color.BLACK);
71
72 int lastY = 0;
73
74 double yStart;
75
76 if (minY % yInterval == 0) {
77 yStart = minY;
78 }
79 else {
80 yStart = yInterval * (((int)minY/yInterval)+1);
81 }
82
83 int xOffset = 0;
84
85 for (double i=yStart;i<=maxY;i+=yInterval) {
86 String label = ""+i;
87 label = label.replaceAll(".0$", "");
88 int width = g.getFontMetrics().stringWidth(label);
89 if (width > xOffset) {
90 xOffset = width;
91 }
92
93 g.drawString(label, 2, getY(i)+(g.getFontMetrics().getAscent()/2));
94 }
95
96 // Give the x axis a bit of breathing space
97 xOffset += 5;
98
99
100
101 g.setColor(Color.BLACK);
102
103
104 // Draw the graph title
105 int titleWidth = g.getFontMetrics().stringWidth(graphTitle);
106 g.drawString(graphTitle, (xOffset + ((getWidth()-(xOffset+10))/2)) - (titleWidth/2), 30);
107
108
109
110 // Work out the width of the x axis bins
111 int baseWidth = (getWidth()-(xOffset+10))/means.length;
112 if (baseWidth<1) baseWidth = 1;
113
114 // First draw faint boxes over alternating bases so you can see which is which
115
116 int lastXLabelEnd = 0;
117
118 for (int i=0;i<means.length;i++) { // Now draw some background colours which show good / bad quality
119 if (i%2 != 0) {
120 g.setColor(UGLY);
121 }
122 else {
123 g.setColor(UGLY_DARK);
124 }
125
126 g.fillRect(xOffset+(baseWidth*i), getY(20), baseWidth, getY(yStart)-getY(20));
127
128 if (i%2 != 0) {
129 g.setColor(BAD);
130 }
131 else {
132 g.setColor(BAD_DARK);
133 }
134
135 g.fillRect(xOffset+(baseWidth*i), getY(28), baseWidth, getY(20)-getY(28));
136
137 if (i%2 != 0) {
138 g.setColor(GOOD);
139 }
140 else {
141 g.setColor(GOOD_DARK);
142 }
143
144 g.fillRect(xOffset+(baseWidth*i), getY(maxY), baseWidth, getY(28)-getY(maxY));
145
146 g.setColor(Color.BLACK);
147 int baseNumberWidth = g.getFontMetrics().stringWidth(xLabels[i]);
148 int labelStart = ((baseWidth/2)+xOffset+(baseWidth*i))-(baseNumberWidth/2);
149
150 if (labelStart > lastXLabelEnd) {
151 g.drawString(xLabels[i], labelStart, getHeight()-25);
152 lastXLabelEnd = labelStart+g.getFontMetrics().stringWidth(xLabels[i])+5;
153 }
154 }
155
156 // Now draw the axes
157 g.drawLine(xOffset, getHeight()-40, getWidth()-10,getHeight()-40);
158 g.drawLine(xOffset, getHeight()-40, xOffset, 40);
159 g.drawString("Position in read (bp)", (getWidth()/2) - (g.getFontMetrics().stringWidth("Position in read (bp)")/2), getHeight()-5);
160
161 // Now draw the boxplots
162
163 for (int i=0;i<medians.length;i++) {
164
165 int boxBottomY = getY(lowerQuartile[i]);
166 int boxTopY = getY(upperQuartile[i]);
167 int lowerWhiskerY = getY(lowest[i]);
168 int upperWhiskerY = getY(highest[i]);
169 int medianY = getY(medians[i]);
170
171 // System.out.println("For base "+i+" values are BoxBottom="+lowerQuartile[i]+" boxTop="+upperQuartile[i]+" whiskerBottom="+lowest[i]+" whiskerTop="+highest[i]+" median="+medians[i]);
172 // System.out.println("For base "+i+" Yvalues are BoxBottom="+boxBottomY+" boxTop="+boxTopY+" whiskerBottom="+lowerWhiskerY+" whiskerTop="+upperWhiskerY+" median="+medianY);
173
174 // Draw the main box
175 g.setColor(new Color(240,240,0));
176 g.fillRect(xOffset+(baseWidth*i)+2, boxTopY, baseWidth-4, boxBottomY-boxTopY);
177 g.setColor(Color.BLACK);
178 g.drawRect(xOffset+(baseWidth*i)+2, boxTopY, baseWidth-4, boxBottomY-boxTopY);
179
180 // Draw the upper whisker
181 g.drawLine(xOffset+(baseWidth*i)+(baseWidth/2), upperWhiskerY, xOffset+(baseWidth*i)+(baseWidth/2), boxTopY);
182 g.drawLine(xOffset+(baseWidth*i)+2, upperWhiskerY, xOffset+(baseWidth*(i+1))-2, upperWhiskerY);
183
184 // Draw the lower whisker
185 g.drawLine(xOffset+(baseWidth*i)+(baseWidth/2), lowerWhiskerY, xOffset+(baseWidth*i)+(baseWidth/2), boxBottomY);
186 g.drawLine(xOffset+(baseWidth*i)+2, lowerWhiskerY, xOffset+(baseWidth*(i+1))-2, lowerWhiskerY);
187
188 // Draw the median line
189 g.setColor(new Color(200,0,0));
190 g.drawLine(xOffset+(baseWidth*i)+2, medianY, (xOffset+(baseWidth*(i+1)))-2,medianY);
191
192
193 }
194
195 // Now overlay the means
196 g.setColor(new Color(0,0,200));
197 lastY = getY(means[0]);
198 for (int i=1;i<means.length;i++) {
199 int thisY = getY(means[i]);
200 g.drawLine((baseWidth/2)+xOffset+(baseWidth*(i-1)), lastY, (baseWidth/2)+xOffset+(baseWidth*i), thisY);
201 lastY = thisY;
202 }
203
204 }
205
206 public int getY(double y) {
207 return (getHeight()-40) - (int)(((getHeight()-80)/(maxY-minY))*(y-minY));
208 }
209
210 }