187 |
13 Oct 06 |
enell |
1 |
/* |
187 |
13 Oct 06 |
enell |
$Id$ |
187 |
13 Oct 06 |
enell |
3 |
|
187 |
13 Oct 06 |
enell |
Copyright (C) 2006 Johan Enell |
187 |
13 Oct 06 |
enell |
5 |
|
187 |
13 Oct 06 |
enell |
This file is part of BASE - BioArray Software Environment. |
187 |
13 Oct 06 |
enell |
Available at http://base.thep.lu.se/ |
187 |
13 Oct 06 |
enell |
8 |
|
187 |
13 Oct 06 |
enell |
BASE is free software; you can redistribute it and/or modify it |
187 |
13 Oct 06 |
enell |
under the terms of the GNU General Public License as published by |
187 |
13 Oct 06 |
enell |
the Free Software Foundation; either version 2 of the License, or |
187 |
13 Oct 06 |
enell |
(at your option) any later version. |
187 |
13 Oct 06 |
enell |
13 |
|
187 |
13 Oct 06 |
enell |
BASE is distributed in the hope that it will be useful, but |
187 |
13 Oct 06 |
enell |
WITHOUT ANY WARRANTY; without even the implied warranty of |
187 |
13 Oct 06 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
187 |
13 Oct 06 |
enell |
General Public License for more details. |
187 |
13 Oct 06 |
enell |
18 |
|
187 |
13 Oct 06 |
enell |
You should have received a copy of the GNU General Public License |
187 |
13 Oct 06 |
enell |
along with this program; if not, write to the Free Software |
187 |
13 Oct 06 |
enell |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
187 |
13 Oct 06 |
enell |
02111-1307, USA. |
187 |
13 Oct 06 |
enell |
23 |
*/ |
187 |
13 Oct 06 |
enell |
24 |
package ztest; |
187 |
13 Oct 06 |
enell |
25 |
|
213 |
07 Dec 06 |
enell |
26 |
import org.jfree.data.xy.XYSeries; |
213 |
07 Dec 06 |
enell |
27 |
|
187 |
13 Oct 06 |
enell |
28 |
import java.io.File; |
187 |
13 Oct 06 |
enell |
29 |
import java.io.FileNotFoundException; |
187 |
13 Oct 06 |
enell |
30 |
import java.io.IOException; |
187 |
13 Oct 06 |
enell |
31 |
import java.io.PrintWriter; |
187 |
13 Oct 06 |
enell |
32 |
import java.util.Comparator; |
187 |
13 Oct 06 |
enell |
33 |
import java.util.List; |
187 |
13 Oct 06 |
enell |
34 |
|
187 |
13 Oct 06 |
enell |
35 |
import basefile.BASEFileException; |
187 |
13 Oct 06 |
enell |
36 |
import basefile.BASEFileReader; |
187 |
13 Oct 06 |
enell |
37 |
import basefile.BASEFileSpotSection; |
187 |
13 Oct 06 |
enell |
38 |
import basefile.BadFormatException; |
187 |
13 Oct 06 |
enell |
39 |
|
187 |
13 Oct 06 |
enell |
40 |
public class Start |
187 |
13 Oct 06 |
enell |
41 |
{ |
217 |
08 Dec 06 |
enell |
42 |
|
217 |
08 Dec 06 |
enell |
43 |
private static ZTest ztest = new ZTest(); |
187 |
13 Oct 06 |
enell |
44 |
|
187 |
13 Oct 06 |
enell |
45 |
/** |
187 |
13 Oct 06 |
enell |
* @param args |
187 |
13 Oct 06 |
enell |
47 |
*/ |
187 |
13 Oct 06 |
enell |
48 |
public static void main(String[] args) |
187 |
13 Oct 06 |
enell |
49 |
{ |
187 |
13 Oct 06 |
enell |
50 |
BASEFileReader bfr; |
187 |
13 Oct 06 |
enell |
51 |
try |
187 |
13 Oct 06 |
enell |
52 |
{ |
187 |
13 Oct 06 |
enell |
53 |
bfr = new BASEFileReader(new File(args.length == 1 ? args[0] : "stdin.txt")); |
187 |
13 Oct 06 |
enell |
54 |
BASEFileSpotSection<Reporter, Spot> bfss = bfr.readSpotSection(); |
187 |
13 Oct 06 |
enell |
55 |
|
187 |
13 Oct 06 |
enell |
56 |
List<String> columns = bfss.findFieldList("columns"); |
187 |
13 Oct 06 |
enell |
57 |
List<Integer> assays = bfss.findFieldIntList("assays"); |
187 |
13 Oct 06 |
enell |
58 |
List<String> assayFields = bfss.findFieldList("assayFields"); |
187 |
13 Oct 06 |
enell |
59 |
|
187 |
13 Oct 06 |
enell |
60 |
int mCol = assayFields.indexOf("l2ratio1_2"); |
221 |
20 Dec 06 |
enell |
61 |
int aCol = assayFields.indexOf("l10intgmean1_2"); |
187 |
13 Oct 06 |
enell |
62 |
int repCol = columns.indexOf("reporter"); |
207 |
04 Dec 06 |
enell |
63 |
int repidCol = columns.indexOf("reporterId"); |
207 |
04 Dec 06 |
enell |
64 |
int symCol = columns.indexOf("geneSymbol"); |
207 |
04 Dec 06 |
enell |
65 |
int llCol = columns.indexOf("locusLink"); |
187 |
13 Oct 06 |
enell |
66 |
int assayDataCol = columns.indexOf("assayData"); |
187 |
13 Oct 06 |
enell |
67 |
|
221 |
20 Dec 06 |
enell |
68 |
if (aCol == -1 || mCol == -1 || repCol == -1 || repidCol == -1 || symCol == -1 || llCol == -1 || assayDataCol == -1) |
187 |
13 Oct 06 |
enell |
69 |
{ |
187 |
13 Oct 06 |
enell |
70 |
throw new BASEFileException("Cant find the columns l2ratio1_2, reporter or assayData"); |
187 |
13 Oct 06 |
enell |
71 |
} |
187 |
13 Oct 06 |
enell |
72 |
mCol += assayDataCol; |
221 |
20 Dec 06 |
enell |
73 |
aCol += assayDataCol; |
187 |
13 Oct 06 |
enell |
74 |
|
187 |
13 Oct 06 |
enell |
75 |
int dataLength = columns.size() + assays.size() * assayFields.size() - 1; |
221 |
20 Dec 06 |
enell |
76 |
bfss.setDataMatrix(bfss.findIntOpt("count"), assays.size()); |
187 |
13 Oct 06 |
enell |
77 |
String[] data = bfr.readDataRow(dataLength); |
187 |
13 Oct 06 |
enell |
78 |
while (data != null) |
187 |
13 Oct 06 |
enell |
79 |
{ |
187 |
13 Oct 06 |
enell |
80 |
Spot[] spots = new Spot[assays.size()]; |
221 |
20 Dec 06 |
enell |
81 |
Double a = 0.0; |
221 |
20 Dec 06 |
enell |
82 |
int aCount = 0; |
187 |
13 Oct 06 |
enell |
83 |
for (int i = 0; i < assays.size(); i++) |
187 |
13 Oct 06 |
enell |
84 |
{ |
221 |
20 Dec 06 |
enell |
85 |
int mIndex = mCol + i * assayFields.size(); |
221 |
20 Dec 06 |
enell |
86 |
int aIndex = aCol + i * assayFields.size(); |
197 |
21 Nov 06 |
enell |
87 |
Double m = Double.NaN; |
221 |
20 Dec 06 |
enell |
88 |
if (!data[mIndex].equals("")) |
221 |
20 Dec 06 |
enell |
89 |
m = new Double(data[mIndex]); |
221 |
20 Dec 06 |
enell |
90 |
if (!data[aIndex].equals("")) |
187 |
13 Oct 06 |
enell |
91 |
{ |
221 |
20 Dec 06 |
enell |
92 |
a += new Double(data[aIndex]); |
221 |
20 Dec 06 |
enell |
93 |
aCount++; |
187 |
13 Oct 06 |
enell |
94 |
} |
187 |
13 Oct 06 |
enell |
95 |
spots[i] = new Spot(m); |
187 |
13 Oct 06 |
enell |
96 |
} |
221 |
20 Dec 06 |
enell |
97 |
Reporter r = new Reporter(new Integer(data[repCol]), data[repidCol], data[symCol], data[llCol], a/aCount); |
187 |
13 Oct 06 |
enell |
98 |
bfss.addData(r, spots); |
187 |
13 Oct 06 |
enell |
99 |
data = bfr.readDataRow(dataLength); |
187 |
13 Oct 06 |
enell |
100 |
} |
187 |
13 Oct 06 |
enell |
101 |
ztest.calculate(bfss); |
187 |
13 Oct 06 |
enell |
102 |
|
187 |
13 Oct 06 |
enell |
103 |
bfss.sortReporter(new Comparator<Reporter>() |
187 |
13 Oct 06 |
enell |
104 |
{ |
187 |
13 Oct 06 |
enell |
105 |
|
187 |
13 Oct 06 |
enell |
106 |
public int compare(Reporter r1, Reporter r2) |
187 |
13 Oct 06 |
enell |
107 |
{ |
187 |
13 Oct 06 |
enell |
108 |
if (r1.getP() < r2.getP()) return -1; |
187 |
13 Oct 06 |
enell |
109 |
else if (r1.getP() == r2.getP()) return 0; |
187 |
13 Oct 06 |
enell |
110 |
else return 1; |
187 |
13 Oct 06 |
enell |
111 |
} |
187 |
13 Oct 06 |
enell |
112 |
}); |
187 |
13 Oct 06 |
enell |
113 |
print(bfss); |
213 |
07 Dec 06 |
enell |
114 |
|
187 |
13 Oct 06 |
enell |
115 |
} |
187 |
13 Oct 06 |
enell |
116 |
catch (FileNotFoundException e) |
187 |
13 Oct 06 |
enell |
117 |
{ |
187 |
13 Oct 06 |
enell |
118 |
System.err.println("Can't find the file " + (args.length == 1 ? args[0] : "stdin.txt")); |
187 |
13 Oct 06 |
enell |
119 |
} |
187 |
13 Oct 06 |
enell |
120 |
catch (BadFormatException e) |
187 |
13 Oct 06 |
enell |
121 |
{ |
187 |
13 Oct 06 |
enell |
122 |
System.err.println((args.length == 1 ? args[0] : "stdin.txt") + " is not a basefile"); |
187 |
13 Oct 06 |
enell |
123 |
} |
187 |
13 Oct 06 |
enell |
124 |
catch (IOException e) |
187 |
13 Oct 06 |
enell |
125 |
{ |
187 |
13 Oct 06 |
enell |
126 |
System.err.println("An error when reading file " + (args.length == 1 ? args[0] : "stdin.txt")); |
187 |
13 Oct 06 |
enell |
127 |
e.printStackTrace(); |
187 |
13 Oct 06 |
enell |
128 |
} |
187 |
13 Oct 06 |
enell |
129 |
catch (BASEFileException e) |
187 |
13 Oct 06 |
enell |
130 |
{ |
187 |
13 Oct 06 |
enell |
131 |
e.printStackTrace(); |
187 |
13 Oct 06 |
enell |
132 |
} |
187 |
13 Oct 06 |
enell |
133 |
} |
187 |
13 Oct 06 |
enell |
134 |
|
187 |
13 Oct 06 |
enell |
135 |
private static void print(BASEFileSpotSection<Reporter, Spot> bfss) |
187 |
13 Oct 06 |
enell |
136 |
{ |
187 |
13 Oct 06 |
enell |
137 |
try |
187 |
13 Oct 06 |
enell |
138 |
{ |
213 |
07 Dec 06 |
enell |
139 |
XYSeries oSeries = new XYSeries("Observed"); |
213 |
07 Dec 06 |
enell |
140 |
XYSeries eSeries = new XYSeries("Expected"); |
214 |
07 Dec 06 |
enell |
141 |
XYSeries fdrSeries = new XYSeries("FDR"); |
219 |
18 Dec 06 |
enell |
142 |
Plot.HistogramDataset mHist = new Plot.HistogramDataset("M", 0.1); |
217 |
08 Dec 06 |
enell |
143 |
|
204 |
24 Nov 06 |
enell |
144 |
PrintWriter html = new PrintWriter(new File("index.html")); |
206 |
24 Nov 06 |
enell |
145 |
PrintWriter tab = new PrintWriter(new File("result.tsv")); |
187 |
13 Oct 06 |
enell |
146 |
int numOfReporters = bfss.getReporterSize(); |
220 |
19 Dec 06 |
enell |
147 |
|
220 |
19 Dec 06 |
enell |
148 |
Object[] tableRow = {"Reporter","GeneSymbol","LocusLink","AverageM","up/down","P","Rank","Expected","FDR","NumberOfValues"}; |
187 |
13 Oct 06 |
enell |
149 |
|
221 |
20 Dec 06 |
enell |
150 |
System.out.println("BASEfile"); |
221 |
20 Dec 06 |
enell |
151 |
System.out.println("section\tassays"); |
221 |
20 Dec 06 |
enell |
152 |
System.out.println("annotationColumns\t"); |
221 |
20 Dec 06 |
enell |
153 |
System.out.println("columns\tid\tname\tparents"); |
221 |
20 Dec 06 |
enell |
154 |
System.out.println("count\t1"); |
221 |
20 Dec 06 |
enell |
155 |
System.out.println("%"); |
221 |
20 Dec 06 |
enell |
156 |
System.out.println("1\tztest\t"+bfss.findStringOpt("assays").replaceAll("\t", "/")); |
221 |
20 Dec 06 |
enell |
157 |
System.out.println(); |
221 |
20 Dec 06 |
enell |
158 |
System.out.println("section\tspots"); |
221 |
20 Dec 06 |
enell |
159 |
System.out.println("channels\t2"); |
221 |
20 Dec 06 |
enell |
160 |
System.out.println("setExtraFloats\tJE_nbrOfElements\tJE_rank\tJE_Expected\tJE_fdr"); |
221 |
20 Dec 06 |
enell |
161 |
System.out.println("assayFields\tl2ratio1_2\tl10intgmean1_2\tJE_nbrOfElements\tJE_rank\tJE_Expected\tJE_fdr"); |
221 |
20 Dec 06 |
enell |
162 |
System.out.println("columns\tposition\treporter\tassayData"); |
221 |
20 Dec 06 |
enell |
163 |
System.out.println("assays\t1"); |
221 |
20 Dec 06 |
enell |
164 |
System.out.println("count\t"+bfss.getReporterSize()); |
221 |
20 Dec 06 |
enell |
165 |
System.out.println("%"); |
221 |
20 Dec 06 |
enell |
166 |
|
204 |
24 Nov 06 |
enell |
167 |
html.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">"); |
204 |
24 Nov 06 |
enell |
168 |
html.println("<html>"); |
204 |
24 Nov 06 |
enell |
169 |
html.println("<head>"); |
204 |
24 Nov 06 |
enell |
170 |
html.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />"); |
204 |
24 Nov 06 |
enell |
171 |
html.println("<title>BASE 1.2.16dev - Experiment Enell:Enell Plugin</title>"); |
204 |
24 Nov 06 |
enell |
172 |
html.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"base.css\" />"); |
204 |
24 Nov 06 |
enell |
173 |
html.println("</head>"); |
204 |
24 Nov 06 |
enell |
174 |
html.println("<table>"); |
220 |
19 Dec 06 |
enell |
175 |
html.printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", tableRow); |
221 |
20 Dec 06 |
enell |
176 |
|
220 |
19 Dec 06 |
enell |
177 |
tab.printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", tableRow); |
217 |
08 Dec 06 |
enell |
178 |
|
204 |
24 Nov 06 |
enell |
179 |
for (int i = 0; i < bfss.getReporterSize(); i++) |
187 |
13 Oct 06 |
enell |
180 |
{ |
197 |
21 Nov 06 |
enell |
181 |
int rank = i + 1; |
187 |
13 Oct 06 |
enell |
182 |
Reporter r = bfss.getReporter(i); |
206 |
24 Nov 06 |
enell |
183 |
double expected = r.getP() * numOfReporters; |
206 |
24 Nov 06 |
enell |
184 |
double fdr = (expected / rank); |
197 |
21 Nov 06 |
enell |
185 |
|
221 |
20 Dec 06 |
enell |
186 |
System.out.printf("%d\t%d\t", (-1*rank), r.getId()); |
221 |
20 Dec 06 |
enell |
187 |
System.out.printf("%f\t%f\t", r.getM(), r.getA()); |
221 |
20 Dec 06 |
enell |
188 |
System.out.printf("%d\t%d\t%f\t%f\n", r.getSize(), rank, expected, fdr); |
221 |
20 Dec 06 |
enell |
189 |
|
221 |
20 Dec 06 |
enell |
190 |
tableRow[0] = r.getReporter(); |
221 |
20 Dec 06 |
enell |
191 |
tableRow[1] = r.getSymbol(); |
221 |
20 Dec 06 |
enell |
192 |
tableRow[2] = r.getLocusLink(); |
221 |
20 Dec 06 |
enell |
193 |
tableRow[3] = r.getM(); |
221 |
20 Dec 06 |
enell |
194 |
tableRow[4] = r.getM() > 0 ? "+" : "-"; |
221 |
20 Dec 06 |
enell |
195 |
tableRow[5] = r.getP(); |
221 |
20 Dec 06 |
enell |
196 |
tableRow[6] = rank; |
221 |
20 Dec 06 |
enell |
197 |
tableRow[7] = expected; |
221 |
20 Dec 06 |
enell |
198 |
tableRow[8] = fdr; |
221 |
20 Dec 06 |
enell |
199 |
tableRow[9] = r.getSize(); |
221 |
20 Dec 06 |
enell |
200 |
|
207 |
04 Dec 06 |
enell |
201 |
if (i < 1000) |
207 |
04 Dec 06 |
enell |
202 |
{ |
207 |
04 Dec 06 |
enell |
203 |
html.printf( |
220 |
19 Dec 06 |
enell |
204 |
"<tr class=\"row" + (i % 2 + 1) + "\"><td>%s</td><td>%s</td><td>%s</td><td>%f</td>" + |
220 |
19 Dec 06 |
enell |
205 |
"<td>%s</td><td>%f</td><td>%d</td><td>%f</td><td>%.2f</td><td>%d</td></tr>\n", |
220 |
19 Dec 06 |
enell |
206 |
tableRow); |
207 |
04 Dec 06 |
enell |
207 |
} |
220 |
19 Dec 06 |
enell |
208 |
tab.printf("%s\t%s\t%s\t%f\t%s\t%f\t%d\t%f\t%f\t%d\n", tableRow); |
213 |
07 Dec 06 |
enell |
209 |
|
220 |
19 Dec 06 |
enell |
210 |
oSeries.add(rank, rank); |
220 |
19 Dec 06 |
enell |
211 |
eSeries.add(rank, expected); |
220 |
19 Dec 06 |
enell |
212 |
fdrSeries.add(rank, fdr); |
220 |
19 Dec 06 |
enell |
213 |
mHist.addObservation(r.getM()); |
187 |
13 Oct 06 |
enell |
214 |
} |
221 |
20 Dec 06 |
enell |
215 |
System.out.println(); |
221 |
20 Dec 06 |
enell |
216 |
|
204 |
24 Nov 06 |
enell |
217 |
html.println("</table>"); |
204 |
24 Nov 06 |
enell |
218 |
html.print("</html>"); |
204 |
24 Nov 06 |
enell |
219 |
html.close(); |
206 |
24 Nov 06 |
enell |
220 |
|
206 |
24 Nov 06 |
enell |
221 |
tab.close(); |
213 |
07 Dec 06 |
enell |
222 |
|
216 |
07 Dec 06 |
enell |
223 |
Plot.plotFDR(fdrSeries); |
216 |
07 Dec 06 |
enell |
224 |
Plot.plotOE(oSeries, eSeries); |
216 |
07 Dec 06 |
enell |
225 |
Plot.plotOE_FDR(oSeries, eSeries, fdrSeries); |
217 |
08 Dec 06 |
enell |
226 |
Plot.plotMHist(mHist, ztest.getTotalMean(), ztest.getTotalSD()); |
187 |
13 Oct 06 |
enell |
227 |
} |
213 |
07 Dec 06 |
enell |
228 |
catch (IOException e) |
213 |
07 Dec 06 |
enell |
229 |
{ |
213 |
07 Dec 06 |
enell |
230 |
e.printStackTrace(); |
213 |
07 Dec 06 |
enell |
231 |
} |
216 |
07 Dec 06 |
enell |
232 |
} |
187 |
13 Oct 06 |
enell |
233 |
} |