216 |
07 Dec 06 |
enell |
1 |
/* |
216 |
07 Dec 06 |
enell |
$Id$ |
216 |
07 Dec 06 |
enell |
3 |
|
216 |
07 Dec 06 |
enell |
Copyright (C) 2006 Johan Enell |
216 |
07 Dec 06 |
enell |
5 |
|
216 |
07 Dec 06 |
enell |
This file is part of BASE - BioArray Software Environment. |
216 |
07 Dec 06 |
enell |
Available at http://base.thep.lu.se/ |
216 |
07 Dec 06 |
enell |
8 |
|
216 |
07 Dec 06 |
enell |
BASE is free software; you can redistribute it and/or modify it |
216 |
07 Dec 06 |
enell |
under the terms of the GNU General Public License as published by |
216 |
07 Dec 06 |
enell |
the Free Software Foundation; either version 2 of the License, or |
216 |
07 Dec 06 |
enell |
(at your option) any later version. |
216 |
07 Dec 06 |
enell |
13 |
|
216 |
07 Dec 06 |
enell |
BASE is distributed in the hope that it will be useful, but |
216 |
07 Dec 06 |
enell |
WITHOUT ANY WARRANTY; without even the implied warranty of |
216 |
07 Dec 06 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
216 |
07 Dec 06 |
enell |
General Public License for more details. |
216 |
07 Dec 06 |
enell |
18 |
|
216 |
07 Dec 06 |
enell |
You should have received a copy of the GNU General Public License |
216 |
07 Dec 06 |
enell |
along with this program; if not, write to the Free Software |
216 |
07 Dec 06 |
enell |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
216 |
07 Dec 06 |
enell |
02111-1307, USA. |
216 |
07 Dec 06 |
enell |
23 |
*/ |
216 |
07 Dec 06 |
enell |
24 |
package ztest; |
216 |
07 Dec 06 |
enell |
25 |
|
216 |
07 Dec 06 |
enell |
26 |
import org.jfree.chart.ChartUtilities; |
216 |
07 Dec 06 |
enell |
27 |
import org.jfree.chart.JFreeChart; |
216 |
07 Dec 06 |
enell |
28 |
import org.jfree.chart.axis.LogarithmicAxis; |
216 |
07 Dec 06 |
enell |
29 |
import org.jfree.chart.axis.NumberAxis; |
219 |
18 Dec 06 |
enell |
30 |
import org.jfree.chart.plot.DatasetRenderingOrder; |
216 |
07 Dec 06 |
enell |
31 |
import org.jfree.chart.plot.SeriesRenderingOrder; |
216 |
07 Dec 06 |
enell |
32 |
import org.jfree.chart.plot.XYPlot; |
217 |
08 Dec 06 |
enell |
33 |
import org.jfree.chart.renderer.xy.XYBarRenderer; |
216 |
07 Dec 06 |
enell |
34 |
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; |
216 |
07 Dec 06 |
enell |
35 |
import org.jfree.data.RangeType; |
219 |
18 Dec 06 |
enell |
36 |
import org.jfree.data.function.Function2D; |
219 |
18 Dec 06 |
enell |
37 |
import org.jfree.data.function.NormalDistributionFunction2D; |
219 |
18 Dec 06 |
enell |
38 |
import org.jfree.data.general.DatasetUtilities; |
219 |
18 Dec 06 |
enell |
39 |
import org.jfree.data.statistics.SimpleHistogramBin; |
217 |
08 Dec 06 |
enell |
40 |
import org.jfree.data.statistics.SimpleHistogramDataset; |
219 |
18 Dec 06 |
enell |
41 |
import org.jfree.data.xy.XYDataset; |
216 |
07 Dec 06 |
enell |
42 |
import org.jfree.data.xy.XYSeries; |
216 |
07 Dec 06 |
enell |
43 |
import org.jfree.data.xy.XYSeriesCollection; |
216 |
07 Dec 06 |
enell |
44 |
|
216 |
07 Dec 06 |
enell |
45 |
import java.awt.Color; |
216 |
07 Dec 06 |
enell |
46 |
import java.io.File; |
216 |
07 Dec 06 |
enell |
47 |
import java.io.IOException; |
216 |
07 Dec 06 |
enell |
48 |
|
216 |
07 Dec 06 |
enell |
49 |
public class Plot |
216 |
07 Dec 06 |
enell |
50 |
{ |
217 |
08 Dec 06 |
enell |
51 |
public static class HistogramDataset extends SimpleHistogramDataset |
217 |
08 Dec 06 |
enell |
52 |
{ |
220 |
19 Dec 06 |
enell |
53 |
|
219 |
18 Dec 06 |
enell |
54 |
private double binSize; |
217 |
08 Dec 06 |
enell |
55 |
|
219 |
18 Dec 06 |
enell |
56 |
public HistogramDataset(Comparable key, double binSize) |
217 |
08 Dec 06 |
enell |
57 |
{ |
217 |
08 Dec 06 |
enell |
58 |
super(key); |
219 |
18 Dec 06 |
enell |
59 |
this.binSize = binSize; |
217 |
08 Dec 06 |
enell |
60 |
} |
217 |
08 Dec 06 |
enell |
61 |
|
217 |
08 Dec 06 |
enell |
62 |
@Override |
217 |
08 Dec 06 |
enell |
63 |
public void addObservation(double value, boolean notify) |
217 |
08 Dec 06 |
enell |
64 |
{ |
217 |
08 Dec 06 |
enell |
65 |
try |
217 |
08 Dec 06 |
enell |
66 |
{ |
217 |
08 Dec 06 |
enell |
67 |
super.addObservation(value, notify); |
217 |
08 Dec 06 |
enell |
68 |
} |
217 |
08 Dec 06 |
enell |
69 |
catch (RuntimeException e) |
217 |
08 Dec 06 |
enell |
70 |
{ |
219 |
18 Dec 06 |
enell |
71 |
newBin(value); |
217 |
08 Dec 06 |
enell |
72 |
super.addObservation(value, notify); |
217 |
08 Dec 06 |
enell |
73 |
} |
217 |
08 Dec 06 |
enell |
74 |
} |
219 |
18 Dec 06 |
enell |
75 |
|
219 |
18 Dec 06 |
enell |
76 |
private void newBin(double value) |
219 |
18 Dec 06 |
enell |
77 |
{ |
219 |
18 Dec 06 |
enell |
78 |
double precision = 1000000; |
219 |
18 Dec 06 |
enell |
79 |
int low = (int)(Math.floor(value/binSize) * binSize * precision); |
219 |
18 Dec 06 |
enell |
80 |
int high = low + (int)(binSize * precision); |
219 |
18 Dec 06 |
enell |
81 |
SimpleHistogramBin bin = new SimpleHistogramBin(low/precision, high/precision, true, false); |
219 |
18 Dec 06 |
enell |
82 |
super.addBin(bin); |
219 |
18 Dec 06 |
enell |
83 |
} |
219 |
18 Dec 06 |
enell |
84 |
|
219 |
18 Dec 06 |
enell |
85 |
public double getBinSize() |
219 |
18 Dec 06 |
enell |
86 |
{ |
219 |
18 Dec 06 |
enell |
87 |
return binSize; |
219 |
18 Dec 06 |
enell |
88 |
} |
217 |
08 Dec 06 |
enell |
89 |
} |
217 |
08 Dec 06 |
enell |
90 |
|
216 |
07 Dec 06 |
enell |
91 |
static void plotOE_FDR(XYSeries observed, XYSeries expected, XYSeries fdr) |
216 |
07 Dec 06 |
enell |
92 |
throws IOException |
216 |
07 Dec 06 |
enell |
93 |
{ |
216 |
07 Dec 06 |
enell |
94 |
NumberAxis domainAxis = new LogarithmicAxis("Rank"); // x axis |
216 |
07 Dec 06 |
enell |
95 |
domainAxis.setAutoRangeIncludesZero(false); |
216 |
07 Dec 06 |
enell |
96 |
domainAxis.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
97 |
NumberAxis rangeAxisFDR = new NumberAxis("FDR"); // y axis fdr |
216 |
07 Dec 06 |
enell |
98 |
rangeAxisFDR.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
99 |
rangeAxisFDR.setAutoRangeIncludesZero(true); |
217 |
08 Dec 06 |
enell |
100 |
NumberAxis rangeAxisOE = new NumberAxis("Observed / Expected"); // y axis oe |
216 |
07 Dec 06 |
enell |
101 |
rangeAxisOE.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
102 |
rangeAxisOE.setAutoRangeIncludesZero(true); |
216 |
07 Dec 06 |
enell |
103 |
|
216 |
07 Dec 06 |
enell |
104 |
XYSeriesCollection datasetOE = new XYSeriesCollection(); |
216 |
07 Dec 06 |
enell |
105 |
datasetOE.addSeries(observed); |
216 |
07 Dec 06 |
enell |
106 |
datasetOE.addSeries(expected); |
216 |
07 Dec 06 |
enell |
107 |
XYSeriesCollection datasetFDR = new XYSeriesCollection(); |
216 |
07 Dec 06 |
enell |
108 |
datasetFDR.addSeries(fdr); |
216 |
07 Dec 06 |
enell |
109 |
|
216 |
07 Dec 06 |
enell |
110 |
XYLineAndShapeRenderer oeRenderer = new XYLineAndShapeRenderer(true, false); |
216 |
07 Dec 06 |
enell |
111 |
oeRenderer.setSeriesPaint(0, Color.RED); |
216 |
07 Dec 06 |
enell |
112 |
oeRenderer.setSeriesPaint(1, Color.BLUE); |
216 |
07 Dec 06 |
enell |
113 |
XYLineAndShapeRenderer fdrRenderer = new XYLineAndShapeRenderer(true, false); |
216 |
07 Dec 06 |
enell |
114 |
fdrRenderer.setSeriesPaint(0, Color.BLACK); |
216 |
07 Dec 06 |
enell |
115 |
|
216 |
07 Dec 06 |
enell |
116 |
XYPlot plot = new XYPlot(); |
216 |
07 Dec 06 |
enell |
117 |
plot.setDomainAxis(domainAxis); |
216 |
07 Dec 06 |
enell |
118 |
|
216 |
07 Dec 06 |
enell |
119 |
plot.setDataset(0, datasetOE); |
216 |
07 Dec 06 |
enell |
120 |
plot.setRangeAxis(0, rangeAxisOE); |
216 |
07 Dec 06 |
enell |
121 |
plot.mapDatasetToRangeAxis(0, 0); |
216 |
07 Dec 06 |
enell |
122 |
plot.setRenderer(0, oeRenderer); |
216 |
07 Dec 06 |
enell |
123 |
|
216 |
07 Dec 06 |
enell |
124 |
plot.setDataset(1, datasetFDR); |
216 |
07 Dec 06 |
enell |
125 |
plot.setRangeAxis(1, rangeAxisFDR); |
216 |
07 Dec 06 |
enell |
126 |
plot.mapDatasetToRangeAxis(1, 1); |
216 |
07 Dec 06 |
enell |
127 |
plot.setRenderer(1, fdrRenderer); |
216 |
07 Dec 06 |
enell |
128 |
|
216 |
07 Dec 06 |
enell |
129 |
plot.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD); |
216 |
07 Dec 06 |
enell |
130 |
|
217 |
08 Dec 06 |
enell |
131 |
JFreeChart chart = new JFreeChart(plot); |
216 |
07 Dec 06 |
enell |
132 |
chart.setAntiAlias(false); |
216 |
07 Dec 06 |
enell |
133 |
|
216 |
07 Dec 06 |
enell |
134 |
ChartUtilities.saveChartAsPNG(new File("oe_fdrplot.png"), chart, 800, 600); |
216 |
07 Dec 06 |
enell |
135 |
} |
216 |
07 Dec 06 |
enell |
136 |
|
216 |
07 Dec 06 |
enell |
137 |
static void plotFDR(XYSeries fdr) |
216 |
07 Dec 06 |
enell |
138 |
throws IOException |
216 |
07 Dec 06 |
enell |
139 |
{ |
216 |
07 Dec 06 |
enell |
140 |
XYLineAndShapeRenderer fdrRenderer = new XYLineAndShapeRenderer(true, false); |
216 |
07 Dec 06 |
enell |
141 |
fdrRenderer.setSeriesPaint(0, Color.BLACK); |
216 |
07 Dec 06 |
enell |
142 |
|
216 |
07 Dec 06 |
enell |
143 |
NumberAxis domainAxis = new LogarithmicAxis("Rank"); // x axis |
216 |
07 Dec 06 |
enell |
144 |
domainAxis.setAutoRangeIncludesZero(false); |
216 |
07 Dec 06 |
enell |
145 |
domainAxis.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
146 |
NumberAxis rangeAxis = new NumberAxis("FDR"); // y axis |
216 |
07 Dec 06 |
enell |
147 |
rangeAxis.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
148 |
rangeAxis.setAutoRangeIncludesZero(true); |
216 |
07 Dec 06 |
enell |
149 |
XYPlot plot = new XYPlot(new XYSeriesCollection(fdr), domainAxis, rangeAxis, fdrRenderer); |
216 |
07 Dec 06 |
enell |
150 |
plot.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD); |
216 |
07 Dec 06 |
enell |
151 |
|
217 |
08 Dec 06 |
enell |
152 |
JFreeChart chart = new JFreeChart(plot); |
216 |
07 Dec 06 |
enell |
153 |
chart.setAntiAlias(false); |
216 |
07 Dec 06 |
enell |
154 |
chart.removeLegend(); |
216 |
07 Dec 06 |
enell |
155 |
|
216 |
07 Dec 06 |
enell |
156 |
ChartUtilities.saveChartAsPNG(new File("fdrplot.png"), chart, 800, 600); |
216 |
07 Dec 06 |
enell |
157 |
} |
220 |
19 Dec 06 |
enell |
158 |
|
216 |
07 Dec 06 |
enell |
159 |
|
216 |
07 Dec 06 |
enell |
160 |
static void plotOE(XYSeries observed, XYSeries expected) |
216 |
07 Dec 06 |
enell |
161 |
throws IOException |
216 |
07 Dec 06 |
enell |
162 |
{ |
216 |
07 Dec 06 |
enell |
163 |
XYSeriesCollection allSeries = new XYSeriesCollection(); |
216 |
07 Dec 06 |
enell |
164 |
allSeries.addSeries(observed); |
216 |
07 Dec 06 |
enell |
165 |
allSeries.addSeries(expected); |
216 |
07 Dec 06 |
enell |
166 |
|
216 |
07 Dec 06 |
enell |
167 |
XYLineAndShapeRenderer oeRenderer = new XYLineAndShapeRenderer(true, false); |
216 |
07 Dec 06 |
enell |
168 |
oeRenderer.setSeriesPaint(0, Color.RED); |
216 |
07 Dec 06 |
enell |
169 |
oeRenderer.setSeriesPaint(1, Color.BLUE); |
216 |
07 Dec 06 |
enell |
170 |
|
216 |
07 Dec 06 |
enell |
171 |
NumberAxis domainAxis = new LogarithmicAxis("Rank"); // x axis |
216 |
07 Dec 06 |
enell |
172 |
domainAxis.setAutoRangeIncludesZero(false); |
216 |
07 Dec 06 |
enell |
173 |
domainAxis.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
174 |
NumberAxis rangeAxis = new NumberAxis(); // y axis |
216 |
07 Dec 06 |
enell |
175 |
rangeAxis.setRangeType(RangeType.POSITIVE); |
216 |
07 Dec 06 |
enell |
176 |
rangeAxis.setAutoRangeIncludesZero(true); |
216 |
07 Dec 06 |
enell |
177 |
XYPlot plot = new XYPlot(allSeries, domainAxis, rangeAxis, oeRenderer); |
216 |
07 Dec 06 |
enell |
178 |
plot.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD); |
216 |
07 Dec 06 |
enell |
179 |
|
217 |
08 Dec 06 |
enell |
180 |
JFreeChart chart = new JFreeChart(plot); |
216 |
07 Dec 06 |
enell |
181 |
chart.setAntiAlias(false); |
216 |
07 Dec 06 |
enell |
182 |
|
216 |
07 Dec 06 |
enell |
183 |
ChartUtilities.saveChartAsPNG(new File("oeplot.png"), chart, 800, 600); |
216 |
07 Dec 06 |
enell |
184 |
} |
217 |
08 Dec 06 |
enell |
185 |
|
219 |
18 Dec 06 |
enell |
186 |
static void plotMHist(HistogramDataset m, double mean, double sd) |
217 |
08 Dec 06 |
enell |
187 |
throws IOException |
217 |
08 Dec 06 |
enell |
188 |
{ |
221 |
20 Dec 06 |
enell |
189 |
if (m.getItemCount(0) == 0) |
221 |
20 Dec 06 |
enell |
190 |
{ |
221 |
20 Dec 06 |
enell |
191 |
System.err.println("Can't print histogram. Dataset empty."); |
221 |
20 Dec 06 |
enell |
192 |
return; |
221 |
20 Dec 06 |
enell |
193 |
} |
221 |
20 Dec 06 |
enell |
194 |
|
217 |
08 Dec 06 |
enell |
195 |
NumberAxis domainAxis = new NumberAxis("M, log2(int1/int2)"); |
219 |
18 Dec 06 |
enell |
196 |
NumberAxis rangeAxis1 = new NumberAxis("Frequence"); |
219 |
18 Dec 06 |
enell |
197 |
NumberAxis rangeAxis2 = new NumberAxis("Normsdist"); |
219 |
18 Dec 06 |
enell |
198 |
rangeAxis1.setAutoRangeIncludesZero(true); |
217 |
08 Dec 06 |
enell |
199 |
|
217 |
08 Dec 06 |
enell |
200 |
XYBarRenderer hRenderer = new XYBarRenderer(); |
217 |
08 Dec 06 |
enell |
201 |
XYLineAndShapeRenderer ndRenderer = new XYLineAndShapeRenderer(true, false); |
217 |
08 Dec 06 |
enell |
202 |
|
217 |
08 Dec 06 |
enell |
203 |
XYPlot plot = new XYPlot(); |
217 |
08 Dec 06 |
enell |
204 |
plot.setDomainAxis(domainAxis); |
219 |
18 Dec 06 |
enell |
205 |
plot.setRangeAxis(0, rangeAxis1); |
219 |
18 Dec 06 |
enell |
206 |
plot.setRangeAxis(1, rangeAxis2); |
219 |
18 Dec 06 |
enell |
207 |
plot.mapDatasetToRangeAxis(0, 0); |
219 |
18 Dec 06 |
enell |
208 |
plot.mapDatasetToRangeAxis(1, 1); |
217 |
08 Dec 06 |
enell |
209 |
|
217 |
08 Dec 06 |
enell |
210 |
plot.setDataset(0, m); |
217 |
08 Dec 06 |
enell |
211 |
plot.setRenderer(0, hRenderer); |
217 |
08 Dec 06 |
enell |
212 |
|
219 |
18 Dec 06 |
enell |
213 |
double start = Math.floor(m.getStartXValue(0, 0)); |
219 |
18 Dec 06 |
enell |
214 |
double end = Math.ceil(m.getEndXValue(0, m.getItemCount(0) - 1)); |
219 |
18 Dec 06 |
enell |
215 |
Function2D dist = new NormalDistributionFunction2D(mean, sd); |
219 |
18 Dec 06 |
enell |
216 |
XYDataset normsdist = DatasetUtilities.sampleFunction2D(dist, start, end, (int)((end - start)/m.getBinSize()), "Normsdist"); |
219 |
18 Dec 06 |
enell |
217 |
plot.setDataset(1, normsdist); |
217 |
08 Dec 06 |
enell |
218 |
plot.setRenderer(1, ndRenderer); |
217 |
08 Dec 06 |
enell |
219 |
|
219 |
18 Dec 06 |
enell |
220 |
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); |
219 |
18 Dec 06 |
enell |
221 |
|
217 |
08 Dec 06 |
enell |
222 |
JFreeChart chart = new JFreeChart(plot); |
217 |
08 Dec 06 |
enell |
223 |
chart.setAntiAlias(false); |
217 |
08 Dec 06 |
enell |
224 |
|
217 |
08 Dec 06 |
enell |
225 |
ChartUtilities.saveChartAsPNG(new File("mhist.png"), chart, 800, 600); |
217 |
08 Dec 06 |
enell |
226 |
} |
216 |
07 Dec 06 |
enell |
227 |
} |