6366 |
05 Dec 13 |
nicklas |
1 |
/* |
6366 |
05 Dec 13 |
nicklas |
$Id: TestJep.java 5320 2010-04-21 09:48:29Z nicklas $ |
6366 |
05 Dec 13 |
nicklas |
3 |
|
6366 |
05 Dec 13 |
nicklas |
Copyright (C) 2005 Nicklas Nordborg |
6366 |
05 Dec 13 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
6366 |
05 Dec 13 |
nicklas |
Copyright (C) 2007 Nicklas Nordborg |
6366 |
05 Dec 13 |
nicklas |
7 |
|
6366 |
05 Dec 13 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6366 |
05 Dec 13 |
nicklas |
Available at http://base.thep.lu.se/ |
6366 |
05 Dec 13 |
nicklas |
10 |
|
6366 |
05 Dec 13 |
nicklas |
BASE is free software; you can redistribute it and/or |
6366 |
05 Dec 13 |
nicklas |
modify it under the terms of the GNU General Public License |
6366 |
05 Dec 13 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6366 |
05 Dec 13 |
nicklas |
of the License, or (at your option) any later version. |
6366 |
05 Dec 13 |
nicklas |
15 |
|
6366 |
05 Dec 13 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6366 |
05 Dec 13 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6366 |
05 Dec 13 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6366 |
05 Dec 13 |
nicklas |
GNU General Public License for more details. |
6366 |
05 Dec 13 |
nicklas |
20 |
|
6366 |
05 Dec 13 |
nicklas |
You should have received a copy of the GNU General Public License |
6366 |
05 Dec 13 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6366 |
05 Dec 13 |
nicklas |
23 |
*/ |
6366 |
05 Dec 13 |
nicklas |
24 |
import net.sf.basedb.core.IntensityTransform; |
6366 |
05 Dec 13 |
nicklas |
25 |
import net.sf.basedb.util.ma.MACalculator; |
6366 |
05 Dec 13 |
nicklas |
26 |
|
6366 |
05 Dec 13 |
nicklas |
27 |
public class TestMACalculator |
6366 |
05 Dec 13 |
nicklas |
28 |
{ |
6366 |
05 Dec 13 |
nicklas |
29 |
|
6366 |
05 Dec 13 |
nicklas |
30 |
static boolean ok = true; |
6366 |
05 Dec 13 |
nicklas |
31 |
public static void main(String[] args) |
6366 |
05 Dec 13 |
nicklas |
32 |
{ |
6366 |
05 Dec 13 |
nicklas |
33 |
TestUtil.checkArgs(args); |
6366 |
05 Dec 13 |
nicklas |
34 |
TestUtil.begin(); |
6366 |
05 Dec 13 |
nicklas |
35 |
ok = test_all(); |
6366 |
05 Dec 13 |
nicklas |
36 |
TestUtil.stop(); |
6366 |
05 Dec 13 |
nicklas |
37 |
} |
6366 |
05 Dec 13 |
nicklas |
38 |
|
6366 |
05 Dec 13 |
nicklas |
39 |
static boolean test_all() |
6366 |
05 Dec 13 |
nicklas |
40 |
{ |
6366 |
05 Dec 13 |
nicklas |
41 |
write("++Testing MA calculators"); |
6366 |
05 Dec 13 |
nicklas |
42 |
|
6366 |
05 Dec 13 |
nicklas |
43 |
test_calculate(1, 1, 0, 0); |
6366 |
05 Dec 13 |
nicklas |
44 |
test_calculate(2, 1, 1, 0.15051499783199059760686944736225); |
6366 |
05 Dec 13 |
nicklas |
45 |
test_calculate(1, 2, -1, 0.15051499783199059760686944736225); |
6366 |
05 Dec 13 |
nicklas |
46 |
|
6366 |
05 Dec 13 |
nicklas |
47 |
double ch1 = Math.ceil(Math.random() * 100+10); |
6366 |
05 Dec 13 |
nicklas |
48 |
double ch2 = Math.ceil(Math.random() * 100+10); |
6366 |
05 Dec 13 |
nicklas |
49 |
double M = Math.log(ch1/ch2) / Math.log(2); |
6366 |
05 Dec 13 |
nicklas |
50 |
double A = Math.log10(ch1*ch2) / 2; |
6366 |
05 Dec 13 |
nicklas |
51 |
test_calculate(ch1, ch2, M, A); |
6366 |
05 Dec 13 |
nicklas |
52 |
test_calculate(ch2, ch1, -M, A); |
6366 |
05 Dec 13 |
nicklas |
53 |
|
6366 |
05 Dec 13 |
nicklas |
54 |
test_correct(1, 1, 0, 1, 1); |
6366 |
05 Dec 13 |
nicklas |
55 |
test_correct(2, 2, 1, 1, 4); |
6366 |
05 Dec 13 |
nicklas |
56 |
double factor = Math.random()-0.5; |
6366 |
05 Dec 13 |
nicklas |
57 |
|
6366 |
05 Dec 13 |
nicklas |
58 |
test_correct(ch1, ch2, factor, ch1 / Math.pow(2, factor) , ch2 * Math.pow(2, factor)); |
6366 |
05 Dec 13 |
nicklas |
59 |
|
6366 |
05 Dec 13 |
nicklas |
60 |
write("++Testing MA calculators "+(ok ? "OK" : "Failed")+"\n"); |
6366 |
05 Dec 13 |
nicklas |
61 |
return ok; |
6366 |
05 Dec 13 |
nicklas |
62 |
} |
6366 |
05 Dec 13 |
nicklas |
63 |
|
6366 |
05 Dec 13 |
nicklas |
64 |
static double log2(double v) |
6366 |
05 Dec 13 |
nicklas |
65 |
{ |
6366 |
05 Dec 13 |
nicklas |
66 |
return Math.log(v) / Math.log(2); |
6366 |
05 Dec 13 |
nicklas |
67 |
} |
6366 |
05 Dec 13 |
nicklas |
68 |
|
6366 |
05 Dec 13 |
nicklas |
69 |
static void test_calculate(double ch1, double ch2, double M, double A) |
6366 |
05 Dec 13 |
nicklas |
70 |
{ |
6366 |
05 Dec 13 |
nicklas |
71 |
String calcSummary = "MA calculation: ["+ch1+","+ch2+"]->["+M+","+A+"]"; |
6366 |
05 Dec 13 |
nicklas |
72 |
|
6366 |
05 Dec 13 |
nicklas |
73 |
for (IntensityTransform tf : IntensityTransform.values()) |
6366 |
05 Dec 13 |
nicklas |
74 |
{ |
6366 |
05 Dec 13 |
nicklas |
75 |
MACalculator mac = tf.getMACalculator(); |
6366 |
05 Dec 13 |
nicklas |
76 |
double[] MA = mac.MA(tf.transform(ch1), tf.transform(ch2)); |
6366 |
05 Dec 13 |
nicklas |
77 |
|
6366 |
05 Dec 13 |
nicklas |
78 |
if (!compareOk(MA[0], M) || !compareOk(MA[1], A)) |
6366 |
05 Dec 13 |
nicklas |
79 |
{ |
6366 |
05 Dec 13 |
nicklas |
80 |
write("--" + calcSummary +": FAILED "+mac.getClass().getSimpleName()+"["+MA[0]+","+MA[1]+"]"); |
6366 |
05 Dec 13 |
nicklas |
81 |
ok = false; |
6366 |
05 Dec 13 |
nicklas |
82 |
} |
6366 |
05 Dec 13 |
nicklas |
83 |
} |
6366 |
05 Dec 13 |
nicklas |
84 |
|
6366 |
05 Dec 13 |
nicklas |
85 |
if (ok) |
6366 |
05 Dec 13 |
nicklas |
86 |
{ |
6366 |
05 Dec 13 |
nicklas |
87 |
write("--" + calcSummary + ": OK"); |
6366 |
05 Dec 13 |
nicklas |
88 |
} |
6366 |
05 Dec 13 |
nicklas |
89 |
} |
6366 |
05 Dec 13 |
nicklas |
90 |
|
6366 |
05 Dec 13 |
nicklas |
91 |
static void test_correct(double ch1, double ch2, double factor, double ch1New, double ch2New) |
6366 |
05 Dec 13 |
nicklas |
92 |
{ |
6366 |
05 Dec 13 |
nicklas |
93 |
String calcSummary = "Correction calculation: ["+ch1+","+ch2+"]("+factor+")->["+ch1New+","+ch2New+"]"; |
6366 |
05 Dec 13 |
nicklas |
94 |
|
6366 |
05 Dec 13 |
nicklas |
95 |
for (IntensityTransform tf : IntensityTransform.values()) |
6366 |
05 Dec 13 |
nicklas |
96 |
{ |
6366 |
05 Dec 13 |
nicklas |
97 |
MACalculator mac = tf.getMACalculator(); |
6366 |
05 Dec 13 |
nicklas |
98 |
double[] c = mac.correct(tf.transform(ch1), tf.transform(ch2), factor); |
6366 |
05 Dec 13 |
nicklas |
99 |
|
6366 |
05 Dec 13 |
nicklas |
100 |
if (!compareOk(c[0], tf.transform(ch1New)) || !compareOk(c[1], tf.transform(ch2New))) |
6366 |
05 Dec 13 |
nicklas |
101 |
{ |
6366 |
05 Dec 13 |
nicklas |
102 |
write("--" + calcSummary +": FAILED "+mac.getClass().getSimpleName()+"["+c[0]+","+c[1]+"]"); |
6366 |
05 Dec 13 |
nicklas |
103 |
ok = false; |
6366 |
05 Dec 13 |
nicklas |
104 |
} |
6366 |
05 Dec 13 |
nicklas |
105 |
} |
6366 |
05 Dec 13 |
nicklas |
106 |
|
6366 |
05 Dec 13 |
nicklas |
107 |
if (ok) |
6366 |
05 Dec 13 |
nicklas |
108 |
{ |
6366 |
05 Dec 13 |
nicklas |
109 |
write("--" + calcSummary + ": OK"); |
6366 |
05 Dec 13 |
nicklas |
110 |
} |
6366 |
05 Dec 13 |
nicklas |
111 |
} |
6366 |
05 Dec 13 |
nicklas |
112 |
|
6366 |
05 Dec 13 |
nicklas |
113 |
|
6366 |
05 Dec 13 |
nicklas |
114 |
static boolean compareOk(double v1, double v2) |
6366 |
05 Dec 13 |
nicklas |
115 |
{ |
6366 |
05 Dec 13 |
nicklas |
116 |
return Math.abs(v1-v2) < 0.0000001; |
6366 |
05 Dec 13 |
nicklas |
117 |
} |
6366 |
05 Dec 13 |
nicklas |
118 |
|
6366 |
05 Dec 13 |
nicklas |
119 |
static void write(String message) |
6366 |
05 Dec 13 |
nicklas |
120 |
{ |
6366 |
05 Dec 13 |
nicklas |
121 |
System.out.println(message); |
6366 |
05 Dec 13 |
nicklas |
122 |
} |
6366 |
05 Dec 13 |
nicklas |
123 |
|
6366 |
05 Dec 13 |
nicklas |
124 |
|
6366 |
05 Dec 13 |
nicklas |
125 |
} |
6366 |
05 Dec 13 |
nicklas |
126 |
|