139 |
10 Aug 06 |
enell |
1 |
/* |
139 |
10 Aug 06 |
enell |
$Id$ |
139 |
10 Aug 06 |
enell |
3 |
|
139 |
10 Aug 06 |
enell |
Copyright (C) 2006 Johan Enell |
139 |
10 Aug 06 |
enell |
5 |
|
139 |
10 Aug 06 |
enell |
This file is part of BASE - BioArray Software Environment. |
139 |
10 Aug 06 |
enell |
Available at http://base.thep.lu.se/ |
139 |
10 Aug 06 |
enell |
8 |
|
139 |
10 Aug 06 |
enell |
BASE is free software; you can redistribute it and/or modify it |
139 |
10 Aug 06 |
enell |
under the terms of the GNU General Public License as published by |
139 |
10 Aug 06 |
enell |
the Free Software Foundation; either version 2 of the License, or |
139 |
10 Aug 06 |
enell |
(at your option) any later version. |
139 |
10 Aug 06 |
enell |
13 |
|
139 |
10 Aug 06 |
enell |
BASE is distributed in the hope that it will be useful, but |
139 |
10 Aug 06 |
enell |
WITHOUT ANY WARRANTY; without even the implied warranty of |
139 |
10 Aug 06 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
139 |
10 Aug 06 |
enell |
General Public License for more details. |
139 |
10 Aug 06 |
enell |
18 |
|
139 |
10 Aug 06 |
enell |
You should have received a copy of the GNU General Public License |
139 |
10 Aug 06 |
enell |
along with this program; if not, write to the Free Software |
139 |
10 Aug 06 |
enell |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
139 |
10 Aug 06 |
enell |
02111-1307, USA. |
139 |
10 Aug 06 |
enell |
23 |
*/ |
139 |
10 Aug 06 |
enell |
24 |
package smooth; |
139 |
10 Aug 06 |
enell |
25 |
|
222 |
22 Dec 06 |
enell |
26 |
import org.jfree.chart.ChartUtilities; |
222 |
22 Dec 06 |
enell |
27 |
import org.jfree.chart.JFreeChart; |
222 |
22 Dec 06 |
enell |
28 |
import org.jfree.chart.axis.CategoryAxis; |
222 |
22 Dec 06 |
enell |
29 |
import org.jfree.chart.axis.NumberAxis; |
222 |
22 Dec 06 |
enell |
30 |
import org.jfree.chart.plot.CategoryPlot; |
227 |
17 Jan 07 |
enell |
31 |
import org.jfree.chart.plot.PlotRenderingInfo; |
222 |
22 Dec 06 |
enell |
32 |
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer; |
227 |
17 Jan 07 |
enell |
33 |
import org.jfree.chart.renderer.category.CategoryItemRendererState; |
222 |
22 Dec 06 |
enell |
34 |
import org.jfree.data.statistics.BoxAndWhiskerItem; |
222 |
22 Dec 06 |
enell |
35 |
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset; |
222 |
22 Dec 06 |
enell |
36 |
|
139 |
10 Aug 06 |
enell |
37 |
import basefile.BASEFileException; |
139 |
10 Aug 06 |
enell |
38 |
import basefile.BASEFileReader; |
139 |
10 Aug 06 |
enell |
39 |
import basefile.BASEFileSection; |
139 |
10 Aug 06 |
enell |
40 |
import basefile.BASEFileSpotSection; |
139 |
10 Aug 06 |
enell |
41 |
|
227 |
17 Jan 07 |
enell |
42 |
import java.awt.Graphics2D; |
227 |
17 Jan 07 |
enell |
43 |
import java.awt.geom.Rectangle2D; |
139 |
10 Aug 06 |
enell |
44 |
import java.io.File; |
222 |
22 Dec 06 |
enell |
45 |
import java.io.IOException; |
139 |
10 Aug 06 |
enell |
46 |
import java.io.PrintStream; |
222 |
22 Dec 06 |
enell |
47 |
import java.util.HashMap; |
139 |
10 Aug 06 |
enell |
48 |
import java.util.List; |
222 |
22 Dec 06 |
enell |
49 |
import java.util.Map; |
139 |
10 Aug 06 |
enell |
50 |
|
139 |
10 Aug 06 |
enell |
51 |
/** |
139 |
10 Aug 06 |
enell |
* This is the main class for the smooth plugin. |
139 |
10 Aug 06 |
enell |
53 |
* |
139 |
10 Aug 06 |
enell |
* @author Johan Enell, johan.enell@med.lu.se, Dept Oncology, Lund University |
139 |
10 Aug 06 |
enell |
55 |
*/ |
139 |
10 Aug 06 |
enell |
56 |
public class SmoothMain |
139 |
10 Aug 06 |
enell |
57 |
{ |
139 |
10 Aug 06 |
enell |
58 |
|
227 |
17 Jan 07 |
enell |
59 |
private static String wType = ""; |
227 |
17 Jan 07 |
enell |
60 |
|
139 |
10 Aug 06 |
enell |
61 |
/** |
139 |
10 Aug 06 |
enell |
* The main method will parse the basefile. The first section must be named |
139 |
10 Aug 06 |
enell |
* "settings" and the requierd parameters will be extracted and validated |
139 |
10 Aug 06 |
enell |
* and a new smooth object will be constructed from these. Every following |
139 |
10 Aug 06 |
enell |
* spot section will be computed using the smoother and written to the file |
139 |
10 Aug 06 |
enell |
* stdout.txt. |
139 |
10 Aug 06 |
enell |
67 |
* |
139 |
10 Aug 06 |
enell |
* @param args either empty or the name of the basefile |
139 |
10 Aug 06 |
enell |
69 |
*/ |
139 |
10 Aug 06 |
enell |
70 |
public static void main(String[] args) |
139 |
10 Aug 06 |
enell |
71 |
{ |
139 |
10 Aug 06 |
enell |
72 |
try |
139 |
10 Aug 06 |
enell |
73 |
{ |
139 |
10 Aug 06 |
enell |
74 |
BASEFileReader bfr; |
139 |
10 Aug 06 |
enell |
75 |
if (args.length == 1) bfr = new BASEFileReader(new File(args[0])); |
139 |
10 Aug 06 |
enell |
76 |
else bfr = new BASEFileReader(new File("stdin.txt")); |
139 |
10 Aug 06 |
enell |
77 |
System.setOut(new PrintStream("stdout.txt")); |
139 |
10 Aug 06 |
enell |
78 |
|
139 |
10 Aug 06 |
enell |
79 |
BASEFileSection bfs = bfr.readSection(true); |
713 |
29 May 08 |
jari |
80 |
if (!bfs.isType("smooth settings")) |
139 |
10 Aug 06 |
enell |
81 |
{ |
139 |
10 Aug 06 |
enell |
82 |
throw new BASEFileException("Can't find section settings"); |
139 |
10 Aug 06 |
enell |
83 |
} |
139 |
10 Aug 06 |
enell |
84 |
|
227 |
17 Jan 07 |
enell |
85 |
wType = bfs.findStringOpt("w_type"); |
139 |
10 Aug 06 |
enell |
86 |
String average = bfs.findStringOpt("average"); |
139 |
10 Aug 06 |
enell |
87 |
int wSize = bfs.findIntOpt("w_size"); |
139 |
10 Aug 06 |
enell |
88 |
float w = bfs.findFloatOpt("w"); |
139 |
10 Aug 06 |
enell |
89 |
|
139 |
10 Aug 06 |
enell |
90 |
if (wSize <= 0) |
139 |
10 Aug 06 |
enell |
91 |
{ |
139 |
10 Aug 06 |
enell |
92 |
System.err.println("Can't do anything with windowsize " + wSize); |
139 |
10 Aug 06 |
enell |
93 |
System.exit(-1); |
139 |
10 Aug 06 |
enell |
94 |
} |
139 |
10 Aug 06 |
enell |
95 |
if (w <= 0 || w > 1) |
139 |
10 Aug 06 |
enell |
96 |
{ |
139 |
10 Aug 06 |
enell |
97 |
System.err.println("Weight must be greater than 0 and less or equal with 1"); |
139 |
10 Aug 06 |
enell |
98 |
System.exit(-1); |
139 |
10 Aug 06 |
enell |
99 |
} |
222 |
22 Dec 06 |
enell |
100 |
|
222 |
22 Dec 06 |
enell |
101 |
bfs = bfr.readSection(); |
222 |
22 Dec 06 |
enell |
102 |
if (!bfs.isType("assays")) |
222 |
22 Dec 06 |
enell |
103 |
{ |
222 |
22 Dec 06 |
enell |
104 |
throw new BASEFileException("Can't find section assays"); |
222 |
22 Dec 06 |
enell |
105 |
} |
222 |
22 Dec 06 |
enell |
106 |
Map<Integer, String> assays = getAssays(bfs, bfr); |
222 |
22 Dec 06 |
enell |
107 |
|
139 |
10 Aug 06 |
enell |
108 |
System.out.println("BASEfile"); |
139 |
10 Aug 06 |
enell |
109 |
|
139 |
10 Aug 06 |
enell |
110 |
Smooth smooth = new Smooth(wType, average, wSize, w); |
139 |
10 Aug 06 |
enell |
111 |
BASEFileSpotSection<Reporter, Spot> bfss = bfr.readSpotSection(); |
139 |
10 Aug 06 |
enell |
112 |
while (bfss != null) |
139 |
10 Aug 06 |
enell |
113 |
{ |
139 |
10 Aug 06 |
enell |
114 |
List<String> columns = bfss.findFieldList("columns"); |
139 |
10 Aug 06 |
enell |
115 |
List<String> assayFields = bfss.findFieldList("assayFields"); |
139 |
10 Aug 06 |
enell |
116 |
|
139 |
10 Aug 06 |
enell |
117 |
int posCol = columns.indexOf("position"); |
139 |
10 Aug 06 |
enell |
118 |
int repCol = columns.indexOf("reporter"); |
139 |
10 Aug 06 |
enell |
119 |
int chrCol = columns.indexOf("chromosome"); |
139 |
10 Aug 06 |
enell |
120 |
int startCol = columns.indexOf("startPosition"); |
139 |
10 Aug 06 |
enell |
121 |
int endCol = columns.indexOf("endPosition"); |
139 |
10 Aug 06 |
enell |
122 |
int dataCol = columns.indexOf("assayData"); |
139 |
10 Aug 06 |
enell |
123 |
int i1Col = assayFields.indexOf("intensity1") + dataCol; |
139 |
10 Aug 06 |
enell |
124 |
int i2Col = assayFields.indexOf("intensity2") + dataCol; |
139 |
10 Aug 06 |
enell |
125 |
|
139 |
10 Aug 06 |
enell |
126 |
bfss.setDataMatrix(bfss.findIntOpt("count"), 1); |
139 |
10 Aug 06 |
enell |
127 |
|
139 |
10 Aug 06 |
enell |
128 |
String[] data = bfr.readDataRow(); |
139 |
10 Aug 06 |
enell |
129 |
while (data != null) |
139 |
10 Aug 06 |
enell |
130 |
{ |
139 |
10 Aug 06 |
enell |
131 |
try |
139 |
10 Aug 06 |
enell |
132 |
{ |
139 |
10 Aug 06 |
enell |
133 |
Spot s = new Spot(data[i1Col], data[i2Col]); |
139 |
10 Aug 06 |
enell |
134 |
Reporter r = new Reporter(data[posCol], data[repCol], data[chrCol], data[startCol], data[endCol], s); |
139 |
10 Aug 06 |
enell |
135 |
bfss.addData(r, s); |
139 |
10 Aug 06 |
enell |
136 |
} |
222 |
22 Dec 06 |
enell |
137 |
catch (Exception e) |
139 |
10 Aug 06 |
enell |
138 |
{} |
139 |
10 Aug 06 |
enell |
139 |
data = bfr.readDataRow(7); |
139 |
10 Aug 06 |
enell |
140 |
} |
139 |
10 Aug 06 |
enell |
141 |
|
139 |
10 Aug 06 |
enell |
142 |
smooth.compute(bfss); |
139 |
10 Aug 06 |
enell |
143 |
|
139 |
10 Aug 06 |
enell |
144 |
bfss.setHeader("columns", "position", "reporter", "assayData"); |
139 |
10 Aug 06 |
enell |
145 |
bfss.setHeader("assayFields", "l2ratio1_2", "l10intgmean1_2", "JE_smoothdiff"); |
139 |
10 Aug 06 |
enell |
146 |
bfss.setHeader("setExtraFloats", "JE_smoothdiff"); |
139 |
10 Aug 06 |
enell |
147 |
System.out.println(bfss); |
139 |
10 Aug 06 |
enell |
148 |
for (int i = 0; i < bfss.getReporterSize(); i++) |
139 |
10 Aug 06 |
enell |
149 |
{ |
139 |
10 Aug 06 |
enell |
150 |
Reporter r = bfss.getReporter(i); |
139 |
10 Aug 06 |
enell |
151 |
System.out.println(r + "\t" + r.getSpot()); |
139 |
10 Aug 06 |
enell |
152 |
} |
139 |
10 Aug 06 |
enell |
153 |
System.out.println(); |
139 |
10 Aug 06 |
enell |
154 |
bfss = bfr.readSpotSection(); |
139 |
10 Aug 06 |
enell |
155 |
} |
139 |
10 Aug 06 |
enell |
156 |
|
222 |
22 Dec 06 |
enell |
157 |
plot(assays, smooth); |
222 |
22 Dec 06 |
enell |
158 |
|
139 |
10 Aug 06 |
enell |
159 |
} |
139 |
10 Aug 06 |
enell |
160 |
catch (OutOfMemoryError oome) |
139 |
10 Aug 06 |
enell |
161 |
{ |
222 |
22 Dec 06 |
enell |
162 |
System.err.println("No more memory in the java virtual machine. Start the application with more memory."); |
139 |
10 Aug 06 |
enell |
163 |
System.exit(-1); |
139 |
10 Aug 06 |
enell |
164 |
} |
139 |
10 Aug 06 |
enell |
165 |
catch (Exception e) |
139 |
10 Aug 06 |
enell |
166 |
{ |
139 |
10 Aug 06 |
enell |
167 |
e.printStackTrace(); |
139 |
10 Aug 06 |
enell |
168 |
System.exit(-1); |
139 |
10 Aug 06 |
enell |
169 |
} |
139 |
10 Aug 06 |
enell |
170 |
} |
139 |
10 Aug 06 |
enell |
171 |
|
222 |
22 Dec 06 |
enell |
172 |
private static void plot(Map<Integer, String> assays, Smooth smooth) throws IOException |
222 |
22 Dec 06 |
enell |
173 |
{ |
222 |
22 Dec 06 |
enell |
174 |
DefaultBoxAndWhiskerCategoryDataset data = new DefaultBoxAndWhiskerCategoryDataset(); |
222 |
22 Dec 06 |
enell |
175 |
for (Integer assayId : assays.keySet()) |
222 |
22 Dec 06 |
enell |
176 |
{ |
222 |
22 Dec 06 |
enell |
177 |
BoxAndWhiskerItem bw = smooth.getBoxAndWhiskerItem(assayId); |
227 |
17 Jan 07 |
enell |
178 |
data.add(bw, "Window distibution", assays.get(assayId)); |
222 |
22 Dec 06 |
enell |
179 |
} |
222 |
22 Dec 06 |
enell |
180 |
|
227 |
17 Jan 07 |
enell |
181 |
CategoryAxis xAxis = new CategoryAxis("Sample"); |
227 |
17 Jan 07 |
enell |
182 |
NumberAxis yAxis = new NumberAxis("Window size (" + (wType.equals("cl") ? "bp" : "clones") + ")"); |
227 |
17 Jan 07 |
enell |
183 |
yAxis.setAutoRangeIncludesZero(false); |
222 |
22 Dec 06 |
enell |
184 |
|
227 |
17 Jan 07 |
enell |
185 |
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer() |
227 |
17 Jan 07 |
enell |
186 |
{ |
227 |
17 Jan 07 |
enell |
187 |
@Override |
227 |
17 Jan 07 |
enell |
188 |
public CategoryItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) |
227 |
17 Jan 07 |
enell |
189 |
{ |
227 |
17 Jan 07 |
enell |
190 |
CategoryItemRendererState state = super.initialise(g2, dataArea, plot, rendererIndex, info); |
227 |
17 Jan 07 |
enell |
191 |
state.setBarWidth(20); |
227 |
17 Jan 07 |
enell |
192 |
return state; |
227 |
17 Jan 07 |
enell |
193 |
} |
227 |
17 Jan 07 |
enell |
194 |
}; |
227 |
17 Jan 07 |
enell |
195 |
CategoryPlot plot = new CategoryPlot(data, xAxis, yAxis, renderer); |
222 |
22 Dec 06 |
enell |
196 |
|
227 |
17 Jan 07 |
enell |
197 |
|
222 |
22 Dec 06 |
enell |
198 |
JFreeChart chart = new JFreeChart(plot); |
227 |
17 Jan 07 |
enell |
199 |
chart.removeLegend(); |
222 |
22 Dec 06 |
enell |
200 |
chart.setAntiAlias(false); |
222 |
22 Dec 06 |
enell |
201 |
|
227 |
17 Jan 07 |
enell |
202 |
int w = 150 + 40 * assays.size(); |
227 |
17 Jan 07 |
enell |
203 |
ChartUtilities.saveChartAsPNG(new File("windowsize.png"), chart, w, 600); |
222 |
22 Dec 06 |
enell |
204 |
} |
222 |
22 Dec 06 |
enell |
205 |
|
222 |
22 Dec 06 |
enell |
206 |
private static Map<Integer, String> getAssays(BASEFileSection bfs, BASEFileReader bfr) throws IOException |
222 |
22 Dec 06 |
enell |
207 |
{ |
222 |
22 Dec 06 |
enell |
208 |
List<String> columns = bfs.findFieldList("columns"); |
222 |
22 Dec 06 |
enell |
209 |
int idCol = columns.indexOf("id"); |
222 |
22 Dec 06 |
enell |
210 |
int nameCol = columns.indexOf("name"); |
222 |
22 Dec 06 |
enell |
211 |
|
222 |
22 Dec 06 |
enell |
212 |
Map<Integer, String> assays = new HashMap<Integer, String>(); |
222 |
22 Dec 06 |
enell |
213 |
String[] data = bfr.readDataRow(); |
222 |
22 Dec 06 |
enell |
214 |
while (data != null) |
222 |
22 Dec 06 |
enell |
215 |
{ |
222 |
22 Dec 06 |
enell |
216 |
assays.put(new Integer(data[idCol]), data[nameCol]); |
222 |
22 Dec 06 |
enell |
217 |
data = bfr.readDataRow(); |
222 |
22 Dec 06 |
enell |
218 |
} |
222 |
22 Dec 06 |
enell |
219 |
return assays; |
222 |
22 Dec 06 |
enell |
220 |
} |
222 |
22 Dec 06 |
enell |
221 |
|
139 |
10 Aug 06 |
enell |
222 |
} |