15 |
14 Oct 05 |
enell |
1 |
/* |
784 |
18 Sep 08 |
jari |
$Id$ |
784 |
18 Sep 08 |
jari |
3 |
|
784 |
18 Sep 08 |
jari |
Copyright (C) 2005 Johan Enell |
784 |
18 Sep 08 |
jari |
Copyright (C) 2008 Jari Häkkinen |
784 |
18 Sep 08 |
jari |
6 |
|
784 |
18 Sep 08 |
jari |
This file is part of the se.lu.onk.ReplicateError plug-in for |
784 |
18 Sep 08 |
jari |
BASE. Available at http://baseplugins.thep.lu.se/ and BASE web |
784 |
18 Sep 08 |
jari |
site is http://base.thep.lu.se |
784 |
18 Sep 08 |
jari |
10 |
|
784 |
18 Sep 08 |
jari |
This is free software; you can redistribute it and/or modify it |
784 |
18 Sep 08 |
jari |
under the terms of the GNU General Public License as published by |
784 |
18 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
784 |
18 Sep 08 |
jari |
(at your option) any later version. |
784 |
18 Sep 08 |
jari |
15 |
|
784 |
18 Sep 08 |
jari |
The software is distributed in the hope that it will be useful, but |
784 |
18 Sep 08 |
jari |
WITHOUT ANY WARRANTY; without even the implied warranty of |
784 |
18 Sep 08 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
784 |
18 Sep 08 |
jari |
General Public License for more details. |
784 |
18 Sep 08 |
jari |
20 |
|
784 |
18 Sep 08 |
jari |
You should have received a copy of the GNU General Public License |
784 |
18 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
784 |
18 Sep 08 |
jari |
23 |
*/ |
15 |
14 Oct 05 |
enell |
24 |
package replicateerror; |
15 |
14 Oct 05 |
enell |
25 |
|
15 |
14 Oct 05 |
enell |
26 |
import java.io.File; |
80 |
24 Mar 06 |
enell |
27 |
import java.io.FileNotFoundException; |
15 |
14 Oct 05 |
enell |
28 |
import java.io.IOException; |
20 |
19 Oct 05 |
enell |
29 |
import java.io.PrintStream; |
80 |
24 Mar 06 |
enell |
30 |
import java.util.ArrayList; |
80 |
24 Mar 06 |
enell |
31 |
import java.util.Collection; |
43 |
21 Nov 05 |
enell |
32 |
import java.util.Collections; |
80 |
24 Mar 06 |
enell |
33 |
import java.util.HashMap; |
80 |
24 Mar 06 |
enell |
34 |
import java.util.Iterator; |
43 |
21 Nov 05 |
enell |
35 |
import java.util.List; |
15 |
14 Oct 05 |
enell |
36 |
|
15 |
14 Oct 05 |
enell |
37 |
import replicateerror.AcrossAssays.ReplicateErrorAA; |
43 |
21 Nov 05 |
enell |
38 |
import replicateerror.WithinAssays.ReplicateErrorWA; |
15 |
14 Oct 05 |
enell |
39 |
|
15 |
14 Oct 05 |
enell |
40 |
import basefile.BASEFileReader; |
15 |
14 Oct 05 |
enell |
41 |
import basefile.BASEFileSection; |
15 |
14 Oct 05 |
enell |
42 |
import basefile.PluginException; |
15 |
14 Oct 05 |
enell |
43 |
|
15 |
14 Oct 05 |
enell |
44 |
public abstract class ReplicateError |
15 |
14 Oct 05 |
enell |
45 |
{ |
15 |
14 Oct 05 |
enell |
46 |
protected float sdLimit; |
15 |
14 Oct 05 |
enell |
47 |
|
15 |
14 Oct 05 |
enell |
48 |
protected boolean leaveSingletons; |
15 |
14 Oct 05 |
enell |
49 |
|
43 |
21 Nov 05 |
enell |
50 |
protected Center center; |
43 |
21 Nov 05 |
enell |
51 |
|
15 |
14 Oct 05 |
enell |
52 |
public static void main(String[] args) |
15 |
14 Oct 05 |
enell |
53 |
throws IOException, PluginException |
15 |
14 Oct 05 |
enell |
54 |
{ |
15 |
14 Oct 05 |
enell |
55 |
if (args.length < 1 && !args[0].matches("-va|-aa")) |
15 |
14 Oct 05 |
enell |
56 |
{ |
15 |
14 Oct 05 |
enell |
57 |
System.err.println("Usage: java ReplicateError {-va|-aa} [input-file]"); |
15 |
14 Oct 05 |
enell |
58 |
} |
15 |
14 Oct 05 |
enell |
59 |
|
15 |
14 Oct 05 |
enell |
60 |
BASEFileReader bfr; |
15 |
14 Oct 05 |
enell |
61 |
if (args.length == 2) |
15 |
14 Oct 05 |
enell |
62 |
{ |
15 |
14 Oct 05 |
enell |
63 |
bfr = new BASEFileReader(new File(args[1])); |
15 |
14 Oct 05 |
enell |
64 |
} |
15 |
14 Oct 05 |
enell |
65 |
else |
15 |
14 Oct 05 |
enell |
66 |
{ |
15 |
14 Oct 05 |
enell |
67 |
bfr = new BASEFileReader(new File("stdin.txt")); |
15 |
14 Oct 05 |
enell |
68 |
} |
20 |
19 Oct 05 |
enell |
69 |
|
20 |
19 Oct 05 |
enell |
70 |
System.setOut(new PrintStream("stdout.txt")); |
15 |
14 Oct 05 |
enell |
71 |
|
15 |
14 Oct 05 |
enell |
72 |
ReplicateError re = null; |
43 |
21 Nov 05 |
enell |
73 |
if (args[0].equalsIgnoreCase("-wa")) |
15 |
14 Oct 05 |
enell |
74 |
{ |
43 |
21 Nov 05 |
enell |
75 |
re = new ReplicateErrorWA(); |
15 |
14 Oct 05 |
enell |
76 |
} |
15 |
14 Oct 05 |
enell |
77 |
else if (args[0].equalsIgnoreCase("-aa")) |
15 |
14 Oct 05 |
enell |
78 |
{ |
15 |
14 Oct 05 |
enell |
79 |
re = new ReplicateErrorAA(); |
15 |
14 Oct 05 |
enell |
80 |
} |
15 |
14 Oct 05 |
enell |
81 |
|
15 |
14 Oct 05 |
enell |
82 |
BASEFileSection section = bfr.readSection(true); |
15 |
14 Oct 05 |
enell |
83 |
while (section != null) |
15 |
14 Oct 05 |
enell |
84 |
{ |
15 |
14 Oct 05 |
enell |
85 |
while (section != null) |
15 |
14 Oct 05 |
enell |
86 |
{ |
15 |
14 Oct 05 |
enell |
87 |
if (section.isType("settings")) |
15 |
14 Oct 05 |
enell |
88 |
{ |
15 |
14 Oct 05 |
enell |
89 |
re.extractSettings(section); |
15 |
14 Oct 05 |
enell |
90 |
} |
15 |
14 Oct 05 |
enell |
91 |
else if (section.isType("assays")) |
15 |
14 Oct 05 |
enell |
92 |
{ |
15 |
14 Oct 05 |
enell |
93 |
re.extractAssays(section, bfr); |
15 |
14 Oct 05 |
enell |
94 |
} |
15 |
14 Oct 05 |
enell |
95 |
else if (section.isType("spots")) |
15 |
14 Oct 05 |
enell |
96 |
{ |
15 |
14 Oct 05 |
enell |
97 |
re.extractSpots(section, bfr); |
15 |
14 Oct 05 |
enell |
98 |
} |
15 |
14 Oct 05 |
enell |
99 |
section = bfr.readSection(); |
15 |
14 Oct 05 |
enell |
100 |
} |
15 |
14 Oct 05 |
enell |
101 |
} |
15 |
14 Oct 05 |
enell |
102 |
} |
15 |
14 Oct 05 |
enell |
103 |
|
43 |
21 Nov 05 |
enell |
104 |
protected float standardDiviation(List<Float> l, float mean) |
43 |
21 Nov 05 |
enell |
105 |
{ |
43 |
21 Nov 05 |
enell |
106 |
double sd = 0; |
43 |
21 Nov 05 |
enell |
107 |
for (Float f : l) |
43 |
21 Nov 05 |
enell |
108 |
{ |
43 |
21 Nov 05 |
enell |
109 |
sd += Math.pow(f - mean, 2D); |
43 |
21 Nov 05 |
enell |
110 |
} |
43 |
21 Nov 05 |
enell |
111 |
sd = Math.sqrt(sd / l.size()); |
43 |
21 Nov 05 |
enell |
112 |
return (float)sd; |
43 |
21 Nov 05 |
enell |
113 |
} |
43 |
21 Nov 05 |
enell |
114 |
|
43 |
21 Nov 05 |
enell |
115 |
protected float mean(List<Float> l) |
43 |
21 Nov 05 |
enell |
116 |
{ |
43 |
21 Nov 05 |
enell |
117 |
float mean = 0; |
43 |
21 Nov 05 |
enell |
118 |
for (Float f : l) |
43 |
21 Nov 05 |
enell |
119 |
{ |
43 |
21 Nov 05 |
enell |
120 |
mean += f; |
43 |
21 Nov 05 |
enell |
121 |
} |
43 |
21 Nov 05 |
enell |
122 |
mean /= l.size(); |
43 |
21 Nov 05 |
enell |
123 |
return mean; |
43 |
21 Nov 05 |
enell |
124 |
} |
43 |
21 Nov 05 |
enell |
125 |
|
43 |
21 Nov 05 |
enell |
126 |
protected float median(List<Float> l) |
43 |
21 Nov 05 |
enell |
127 |
{ |
80 |
24 Mar 06 |
enell |
128 |
if (l.size() < 1) return Float.NaN; |
43 |
21 Nov 05 |
enell |
129 |
Collections.sort(l); |
43 |
21 Nov 05 |
enell |
130 |
float n = l.get(((l.size())/2)-1); |
43 |
21 Nov 05 |
enell |
131 |
float n1 = l.get(((l.size()+2)/2)-1); |
43 |
21 Nov 05 |
enell |
132 |
if (l.size() % 2 == 1) |
43 |
21 Nov 05 |
enell |
133 |
{ |
43 |
21 Nov 05 |
enell |
134 |
return n1; |
43 |
21 Nov 05 |
enell |
135 |
} |
43 |
21 Nov 05 |
enell |
136 |
return (n1 +n)/2f; |
43 |
21 Nov 05 |
enell |
137 |
} |
43 |
21 Nov 05 |
enell |
138 |
|
15 |
14 Oct 05 |
enell |
139 |
public void extractSettings(BASEFileSection section) throws PluginException |
15 |
14 Oct 05 |
enell |
140 |
{ |
45 |
22 Nov 05 |
enell |
141 |
System.out.println("BASEfile"); |
15 |
14 Oct 05 |
enell |
142 |
sdLimit = section.findFloatOpt("sdLimit"); |
15 |
14 Oct 05 |
enell |
143 |
leaveSingletons = section.findBooleanOpt("leaveSingletons"); |
43 |
21 Nov 05 |
enell |
144 |
center = Center.fromInt(section.findIntOpt("center")); |
15 |
14 Oct 05 |
enell |
145 |
} |
15 |
14 Oct 05 |
enell |
146 |
|
15 |
14 Oct 05 |
enell |
147 |
public abstract void extractAssays(BASEFileSection section, BASEFileReader bfr) throws PluginException; |
15 |
14 Oct 05 |
enell |
148 |
|
15 |
14 Oct 05 |
enell |
149 |
public abstract void extractSpots(BASEFileSection section, BASEFileReader bfr); |
43 |
21 Nov 05 |
enell |
150 |
|
43 |
21 Nov 05 |
enell |
151 |
protected enum Center |
43 |
21 Nov 05 |
enell |
152 |
{ |
43 |
21 Nov 05 |
enell |
153 |
median, mean; |
43 |
21 Nov 05 |
enell |
154 |
|
43 |
21 Nov 05 |
enell |
155 |
public static Center fromInt(int i) |
43 |
21 Nov 05 |
enell |
156 |
{ |
43 |
21 Nov 05 |
enell |
157 |
if (i == 0) |
43 |
21 Nov 05 |
enell |
158 |
{ |
43 |
21 Nov 05 |
enell |
159 |
return median; |
43 |
21 Nov 05 |
enell |
160 |
} |
43 |
21 Nov 05 |
enell |
161 |
else if (i == 1) |
43 |
21 Nov 05 |
enell |
162 |
{ |
43 |
21 Nov 05 |
enell |
163 |
return mean; |
43 |
21 Nov 05 |
enell |
164 |
} |
43 |
21 Nov 05 |
enell |
165 |
return null; |
43 |
21 Nov 05 |
enell |
166 |
} |
43 |
21 Nov 05 |
enell |
167 |
} |
80 |
24 Mar 06 |
enell |
168 |
|
80 |
24 Mar 06 |
enell |
169 |
protected void printDuplicates(Collection<Replicate> replicates, String filename) throws FileNotFoundException |
80 |
24 Mar 06 |
enell |
170 |
{ |
80 |
24 Mar 06 |
enell |
171 |
PrintStream out = new PrintStream(new File("data", filename)); |
80 |
24 Mar 06 |
enell |
172 |
for(Replicate r : replicates) |
80 |
24 Mar 06 |
enell |
173 |
{ |
80 |
24 Mar 06 |
enell |
174 |
Iterator<Spot[]> i = r.duplicateIterator(); |
80 |
24 Mar 06 |
enell |
175 |
while (i.hasNext()) |
80 |
24 Mar 06 |
enell |
176 |
{ |
80 |
24 Mar 06 |
enell |
177 |
Spot[] d = i.next(); |
80 |
24 Mar 06 |
enell |
178 |
out.println(d[0].getRatio()+"\t"+d[1].getRatio()); |
80 |
24 Mar 06 |
enell |
179 |
} |
80 |
24 Mar 06 |
enell |
180 |
} |
80 |
24 Mar 06 |
enell |
181 |
out.close(); |
80 |
24 Mar 06 |
enell |
182 |
} |
80 |
24 Mar 06 |
enell |
183 |
|
80 |
24 Mar 06 |
enell |
184 |
protected void printSpots(int assay, HashMap<Integer,Replicate> values) |
80 |
24 Mar 06 |
enell |
185 |
{ |
80 |
24 Mar 06 |
enell |
186 |
for (Integer reporter : values.keySet()) |
80 |
24 Mar 06 |
enell |
187 |
{ |
80 |
24 Mar 06 |
enell |
188 |
Replicate r = values.get(reporter); |
792 |
24 Sep 08 |
jari |
189 |
ArrayList<Spot> als = r.getAssaySpot(assay); |
792 |
24 Sep 08 |
jari |
190 |
for (Spot s : als) |
80 |
24 Mar 06 |
enell |
191 |
{ |
792 |
24 Sep 08 |
jari |
192 |
for (int i = 0; s != null && i < s.getPos().size(); i++) |
792 |
24 Sep 08 |
jari |
193 |
{ |
792 |
24 Sep 08 |
jari |
194 |
System.out.print(s.getPos(i)+"\t"+reporter+"\t"); |
792 |
24 Sep 08 |
jari |
195 |
float int1=s.getInt1(i); |
792 |
24 Sep 08 |
jari |
196 |
float int2=s.getInt2(i); |
792 |
24 Sep 08 |
jari |
197 |
if (!Float.isNaN(int1)) System.out.print(int1); |
792 |
24 Sep 08 |
jari |
198 |
System.out.print("\t"); |
792 |
24 Sep 08 |
jari |
199 |
if (!Float.isNaN(int2)) System.out.print(int2); |
792 |
24 Sep 08 |
jari |
200 |
System.out.println(); |
792 |
24 Sep 08 |
jari |
201 |
} |
80 |
24 Mar 06 |
enell |
202 |
} |
80 |
24 Mar 06 |
enell |
203 |
} |
80 |
24 Mar 06 |
enell |
204 |
} |
80 |
24 Mar 06 |
enell |
205 |
|
80 |
24 Mar 06 |
enell |
206 |
protected float[] caluculateABR(Collection<Replicate> replicates) |
80 |
24 Mar 06 |
enell |
207 |
{ |
80 |
24 Mar 06 |
enell |
//TODO compare with function in net.sf.basedb.plugins.LowessNormalization |
80 |
24 Mar 06 |
enell |
209 |
float[] result = new float[3]; // a b r |
80 |
24 Mar 06 |
enell |
210 |
|
80 |
24 Mar 06 |
enell |
211 |
float meanX = 0; |
80 |
24 Mar 06 |
enell |
212 |
float meanY = 0; |
80 |
24 Mar 06 |
enell |
213 |
float ssxx = 0; |
80 |
24 Mar 06 |
enell |
214 |
float ssyy = 0; |
80 |
24 Mar 06 |
enell |
215 |
float ssxy = 0; |
80 |
24 Mar 06 |
enell |
216 |
|
80 |
24 Mar 06 |
enell |
217 |
for (Replicate r : replicates) |
80 |
24 Mar 06 |
enell |
218 |
{ |
80 |
24 Mar 06 |
enell |
219 |
Iterator<Spot[]> i = r.duplicateIterator(); |
80 |
24 Mar 06 |
enell |
220 |
while(i.hasNext()) |
80 |
24 Mar 06 |
enell |
221 |
{ |
80 |
24 Mar 06 |
enell |
222 |
Spot[] d = i.next(); |
80 |
24 Mar 06 |
enell |
223 |
float m1 = d[0].getM(); |
80 |
24 Mar 06 |
enell |
224 |
float m2 = d[1].getM(); |
80 |
24 Mar 06 |
enell |
225 |
|
80 |
24 Mar 06 |
enell |
226 |
meanX += m1; |
80 |
24 Mar 06 |
enell |
227 |
meanY += m2; |
80 |
24 Mar 06 |
enell |
228 |
ssyy += Math.pow(m2, 2); |
80 |
24 Mar 06 |
enell |
229 |
ssxx += Math.pow(m1, 2); |
80 |
24 Mar 06 |
enell |
230 |
ssxy += m1 * m2; |
80 |
24 Mar 06 |
enell |
231 |
} |
80 |
24 Mar 06 |
enell |
232 |
} |
80 |
24 Mar 06 |
enell |
233 |
meanX /= replicates.size(); |
80 |
24 Mar 06 |
enell |
234 |
meanY /= replicates.size(); |
80 |
24 Mar 06 |
enell |
235 |
ssxx = ssxx - replicates.size() * meanX * meanX; |
80 |
24 Mar 06 |
enell |
236 |
ssyy = ssyy - replicates.size() * meanY * meanY; |
80 |
24 Mar 06 |
enell |
237 |
ssxy = ssxy - replicates.size() * meanX * meanY; |
80 |
24 Mar 06 |
enell |
238 |
|
80 |
24 Mar 06 |
enell |
239 |
result[1] = ssxy / ssxx; |
80 |
24 Mar 06 |
enell |
240 |
result[0] = meanY - result[1] * meanX; |
80 |
24 Mar 06 |
enell |
241 |
result[2] = (float) Math.sqrt((ssxy * ssxy) / (ssxx * ssyy)); |
80 |
24 Mar 06 |
enell |
242 |
|
80 |
24 Mar 06 |
enell |
243 |
return result; |
80 |
24 Mar 06 |
enell |
244 |
} |
80 |
24 Mar 06 |
enell |
245 |
|
791 |
24 Sep 08 |
jari |
246 |
protected void purgeUnusableReplicates(HashMap<Integer, Replicate> replicates, |
791 |
24 Sep 08 |
jari |
247 |
HashMap<Integer, Replicate> singletons) |
791 |
24 Sep 08 |
jari |
248 |
{ |
791 |
24 Sep 08 |
jari |
249 |
Iterator<Integer> i = replicates.keySet().iterator(); |
791 |
24 Sep 08 |
jari |
250 |
while (i.hasNext()) |
791 |
24 Sep 08 |
jari |
251 |
{ |
791 |
24 Sep 08 |
jari |
252 |
Integer reporter = i.next(); |
791 |
24 Sep 08 |
jari |
253 |
Replicate r = replicates.get(reporter); |
791 |
24 Sep 08 |
jari |
254 |
if (r.valid()) |
791 |
24 Sep 08 |
jari |
255 |
{ |
791 |
24 Sep 08 |
jari |
256 |
Iterator<Spot[]> dupI = r.duplicateIterator(); |
791 |
24 Sep 08 |
jari |
257 |
while (dupI.hasNext()) |
791 |
24 Sep 08 |
jari |
258 |
{ |
791 |
24 Sep 08 |
jari |
259 |
Spot[] d = dupI.next(); |
791 |
24 Sep 08 |
jari |
260 |
R.replica1Range(d[0].getM()); |
791 |
24 Sep 08 |
jari |
261 |
R.replica2Range(d[1].getM()); |
791 |
24 Sep 08 |
jari |
262 |
R.replicaMRange(Replicate.getReplicateM(d[0], d[1])); |
791 |
24 Sep 08 |
jari |
263 |
} |
791 |
24 Sep 08 |
jari |
264 |
} |
791 |
24 Sep 08 |
jari |
265 |
else |
791 |
24 Sep 08 |
jari |
266 |
{ |
791 |
24 Sep 08 |
jari |
267 |
i.remove(); |
791 |
24 Sep 08 |
jari |
268 |
if (leaveSingletons && r.size() == 1) singletons.put(reporter, r); |
791 |
24 Sep 08 |
jari |
269 |
} |
791 |
24 Sep 08 |
jari |
270 |
} |
791 |
24 Sep 08 |
jari |
271 |
} |
791 |
24 Sep 08 |
jari |
272 |
|
80 |
24 Mar 06 |
enell |
273 |
protected List<Float> getMList(Collection<Replicate> replicates) |
80 |
24 Mar 06 |
enell |
274 |
{ |
80 |
24 Mar 06 |
enell |
275 |
List<Float> l = new ArrayList<Float>(); |
80 |
24 Mar 06 |
enell |
276 |
for (Replicate r : replicates) |
80 |
24 Mar 06 |
enell |
277 |
{ |
80 |
24 Mar 06 |
enell |
278 |
Iterator<Spot[]> i = r.duplicateIterator(); |
80 |
24 Mar 06 |
enell |
279 |
while (i.hasNext()) |
80 |
24 Mar 06 |
enell |
280 |
{ |
80 |
24 Mar 06 |
enell |
281 |
Spot[] d = i.next(); |
101 |
05 May 06 |
enell |
282 |
Float m = Replicate.getReplicateM(d[0], d[1]); |
101 |
05 May 06 |
enell |
283 |
if (!m.isNaN()) |
101 |
05 May 06 |
enell |
284 |
{ |
101 |
05 May 06 |
enell |
285 |
l.add(m); |
101 |
05 May 06 |
enell |
286 |
} |
80 |
24 Mar 06 |
enell |
287 |
} |
80 |
24 Mar 06 |
enell |
288 |
} |
80 |
24 Mar 06 |
enell |
289 |
return l; |
80 |
24 Mar 06 |
enell |
290 |
} |
15 |
14 Oct 05 |
enell |
291 |
} |