103 |
01 Jun 06 |
enell |
1 |
/* |
103 |
01 Jun 06 |
enell |
$Id$ |
103 |
01 Jun 06 |
enell |
3 |
|
103 |
01 Jun 06 |
enell |
Copyright (C) 2006 Johan Enell |
103 |
01 Jun 06 |
enell |
5 |
|
103 |
01 Jun 06 |
enell |
This file is part of BASE - BioArray Software Environment. |
103 |
01 Jun 06 |
enell |
Available at http://base.thep.lu.se/ |
103 |
01 Jun 06 |
enell |
8 |
|
103 |
01 Jun 06 |
enell |
BASE is free software; you can redistribute it and/or modify it |
103 |
01 Jun 06 |
enell |
under the terms of the GNU General Public License as published by |
103 |
01 Jun 06 |
enell |
the Free Software Foundation; either version 2 of the License, or |
103 |
01 Jun 06 |
enell |
(at your option) any later version. |
103 |
01 Jun 06 |
enell |
13 |
|
103 |
01 Jun 06 |
enell |
BASE is distributed in the hope that it will be useful, but |
103 |
01 Jun 06 |
enell |
WITHOUT ANY WARRANTY; without even the implied warranty of |
103 |
01 Jun 06 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
103 |
01 Jun 06 |
enell |
General Public License for more details. |
103 |
01 Jun 06 |
enell |
18 |
|
103 |
01 Jun 06 |
enell |
You should have received a copy of the GNU General Public License |
103 |
01 Jun 06 |
enell |
along with this program; if not, write to the Free Software |
103 |
01 Jun 06 |
enell |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
103 |
01 Jun 06 |
enell |
02111-1307, USA. |
103 |
01 Jun 06 |
enell |
23 |
*/ |
103 |
01 Jun 06 |
enell |
24 |
package medianratio; |
103 |
01 Jun 06 |
enell |
25 |
|
103 |
01 Jun 06 |
enell |
26 |
import java.util.ArrayList; |
103 |
01 Jun 06 |
enell |
27 |
import java.util.Collections; |
103 |
01 Jun 06 |
enell |
28 |
import java.util.Comparator; |
103 |
01 Jun 06 |
enell |
29 |
import java.util.HashMap; |
103 |
01 Jun 06 |
enell |
30 |
import java.util.Iterator; |
103 |
01 Jun 06 |
enell |
31 |
import java.util.List; |
103 |
01 Jun 06 |
enell |
32 |
|
103 |
01 Jun 06 |
enell |
33 |
import basefile.BASEFileSpotSection; |
103 |
01 Jun 06 |
enell |
34 |
|
103 |
01 Jun 06 |
enell |
35 |
public class Normalizer |
103 |
01 Jun 06 |
enell |
36 |
{ |
105 |
02 Jun 06 |
enell |
37 |
private final Float heighExclude; |
103 |
01 Jun 06 |
enell |
38 |
|
105 |
02 Jun 06 |
enell |
39 |
private final Float lowExclude; |
103 |
01 Jun 06 |
enell |
40 |
|
105 |
02 Jun 06 |
enell |
41 |
private final Float minIntensity; |
103 |
01 Jun 06 |
enell |
42 |
|
105 |
02 Jun 06 |
enell |
43 |
private final Integer blockGroup; |
103 |
01 Jun 06 |
enell |
44 |
|
105 |
02 Jun 06 |
enell |
45 |
private final Float median; |
105 |
02 Jun 06 |
enell |
46 |
|
105 |
02 Jun 06 |
enell |
47 |
public Normalizer(Float heighExclude, Float lowExclude, Float minIntensity, Integer blockGroup, Float median) |
103 |
01 Jun 06 |
enell |
48 |
{ |
104 |
01 Jun 06 |
enell |
49 |
this.heighExclude = heighExclude / 100f; |
104 |
01 Jun 06 |
enell |
50 |
this.lowExclude = lowExclude / 100f; |
103 |
01 Jun 06 |
enell |
51 |
this.minIntensity = minIntensity; |
103 |
01 Jun 06 |
enell |
52 |
this.blockGroup = blockGroup; |
105 |
02 Jun 06 |
enell |
53 |
this.median = median; |
103 |
01 Jun 06 |
enell |
54 |
} |
103 |
01 Jun 06 |
enell |
55 |
|
103 |
01 Jun 06 |
enell |
56 |
public void normalize(BASEFileSpotSection<Reporter, Spot> bfss) |
103 |
01 Jun 06 |
enell |
57 |
{ |
103 |
01 Jun 06 |
enell |
58 |
HashMap<Integer, List<Spot>> blockGroups = groupSpots(bfss); |
103 |
01 Jun 06 |
enell |
59 |
Iterator<List<Spot>> i = blockGroups.values().iterator(); |
103 |
01 Jun 06 |
enell |
60 |
while (i.hasNext()) |
103 |
01 Jun 06 |
enell |
61 |
{ |
103 |
01 Jun 06 |
enell |
62 |
List<Spot> v = i.next(); |
105 |
02 Jun 06 |
enell |
63 |
double med_ratio; |
105 |
02 Jun 06 |
enell |
64 |
if (median == null) |
105 |
02 Jun 06 |
enell |
65 |
{ |
105 |
02 Jun 06 |
enell |
66 |
float[] m = median(v); |
105 |
02 Jun 06 |
enell |
67 |
med_ratio = Math.sqrt(m[0] * m[1]); |
105 |
02 Jun 06 |
enell |
68 |
} |
105 |
02 Jun 06 |
enell |
69 |
else |
105 |
02 Jun 06 |
enell |
70 |
{ |
105 |
02 Jun 06 |
enell |
71 |
med_ratio = median; |
105 |
02 Jun 06 |
enell |
72 |
} |
103 |
01 Jun 06 |
enell |
73 |
double sqrt_med_ratio = Math.sqrt(med_ratio); |
103 |
01 Jun 06 |
enell |
74 |
double inv_sqrt_med_ratio = 1f / sqrt_med_ratio; |
105 |
02 Jun 06 |
enell |
75 |
|
103 |
01 Jun 06 |
enell |
76 |
for (Spot s : v) |
103 |
01 Jun 06 |
enell |
77 |
{ |
103 |
01 Jun 06 |
enell |
78 |
s.setInt1((float) (s.getInt1() * inv_sqrt_med_ratio)); |
103 |
01 Jun 06 |
enell |
79 |
s.setInt2((float) (s.getInt2() * sqrt_med_ratio)); |
103 |
01 Jun 06 |
enell |
80 |
} |
103 |
01 Jun 06 |
enell |
81 |
} |
103 |
01 Jun 06 |
enell |
82 |
} |
103 |
01 Jun 06 |
enell |
83 |
|
103 |
01 Jun 06 |
enell |
84 |
private float[] median(List<Spot> v) |
103 |
01 Jun 06 |
enell |
85 |
{ |
103 |
01 Jun 06 |
enell |
86 |
List<Spot> vin = new ArrayList<Spot>(); |
103 |
01 Jun 06 |
enell |
87 |
|
103 |
01 Jun 06 |
enell |
88 |
for (int i = 0; i < v.size(); i++) |
103 |
01 Jun 06 |
enell |
89 |
{ |
103 |
01 Jun 06 |
enell |
90 |
Spot s = v.get(i); |
103 |
01 Jun 06 |
enell |
91 |
if (s.getInt1() >= minIntensity || s.getInt2() >= minIntensity) |
103 |
01 Jun 06 |
enell |
92 |
{ |
103 |
01 Jun 06 |
enell |
93 |
vin.add(s); |
103 |
01 Jun 06 |
enell |
94 |
} |
103 |
01 Jun 06 |
enell |
95 |
} |
105 |
02 Jun 06 |
enell |
96 |
|
103 |
01 Jun 06 |
enell |
97 |
Collections.sort(vin, new Comparator<Spot>() |
103 |
01 Jun 06 |
enell |
98 |
{ |
103 |
01 Jun 06 |
enell |
99 |
public int compare(Spot s1, Spot s2) |
103 |
01 Jun 06 |
enell |
100 |
{ |
103 |
01 Jun 06 |
enell |
101 |
if (s1.getRatio() < s2.getRatio()) return -1; |
103 |
01 Jun 06 |
enell |
102 |
else if (s1.getRatio() == s2.getRatio()) return 0; |
103 |
01 Jun 06 |
enell |
103 |
else return 1; |
103 |
01 Jun 06 |
enell |
104 |
} |
103 |
01 Jun 06 |
enell |
105 |
}); |
103 |
01 Jun 06 |
enell |
106 |
|
103 |
01 Jun 06 |
enell |
107 |
int low = Math.round(vin.size() * lowExclude); |
103 |
01 Jun 06 |
enell |
108 |
int high = Math.round(vin.size() - vin.size() * heighExclude); |
103 |
01 Jun 06 |
enell |
109 |
|
103 |
01 Jun 06 |
enell |
110 |
vin = new ArrayList<Spot>(vin.subList(low, high)); |
103 |
01 Jun 06 |
enell |
111 |
|
103 |
01 Jun 06 |
enell |
112 |
int mid = vin.size() / 2; |
103 |
01 Jun 06 |
enell |
113 |
|
103 |
01 Jun 06 |
enell |
114 |
float[] ret = new float[2]; |
103 |
01 Jun 06 |
enell |
115 |
if (vin.size() % 2 == 1) |
103 |
01 Jun 06 |
enell |
116 |
{ |
103 |
01 Jun 06 |
enell |
117 |
ret[0] = ret[1] = vin.get(mid).getRatio(); |
103 |
01 Jun 06 |
enell |
118 |
} |
103 |
01 Jun 06 |
enell |
119 |
else |
103 |
01 Jun 06 |
enell |
120 |
{ |
103 |
01 Jun 06 |
enell |
121 |
ret[0] = vin.get(mid).getRatio(); |
103 |
01 Jun 06 |
enell |
122 |
ret[1] = vin.get(mid - 1).getRatio(); |
103 |
01 Jun 06 |
enell |
123 |
} |
103 |
01 Jun 06 |
enell |
124 |
|
103 |
01 Jun 06 |
enell |
125 |
return ret; |
103 |
01 Jun 06 |
enell |
126 |
} |
103 |
01 Jun 06 |
enell |
127 |
|
103 |
01 Jun 06 |
enell |
128 |
private HashMap<Integer, List<Spot>> groupSpots(BASEFileSpotSection<Reporter, Spot> bfss) |
103 |
01 Jun 06 |
enell |
129 |
{ |
103 |
01 Jun 06 |
enell |
130 |
HashMap<Integer, List<Spot>> blockGroups = new HashMap<Integer, List<Spot>>(); |
103 |
01 Jun 06 |
enell |
131 |
Iterator<Spot> i = bfss.spotIterator(); |
103 |
01 Jun 06 |
enell |
132 |
while (i.hasNext()) |
103 |
01 Jun 06 |
enell |
133 |
{ |
103 |
01 Jun 06 |
enell |
134 |
Spot s = i.next(); |
103 |
01 Jun 06 |
enell |
135 |
int block = (int) Math.ceil(s.getBlock() / (float) blockGroup); |
103 |
01 Jun 06 |
enell |
136 |
List<Spot> v; |
103 |
01 Jun 06 |
enell |
137 |
if (blockGroups.containsKey(block)) |
103 |
01 Jun 06 |
enell |
138 |
{ |
103 |
01 Jun 06 |
enell |
139 |
v = blockGroups.get(block); |
103 |
01 Jun 06 |
enell |
140 |
} |
103 |
01 Jun 06 |
enell |
141 |
else |
103 |
01 Jun 06 |
enell |
142 |
{ |
103 |
01 Jun 06 |
enell |
143 |
v = new ArrayList<Spot>(); |
103 |
01 Jun 06 |
enell |
144 |
blockGroups.put(block, v); |
103 |
01 Jun 06 |
enell |
145 |
} |
103 |
01 Jun 06 |
enell |
146 |
v.add(s); |
103 |
01 Jun 06 |
enell |
147 |
} |
103 |
01 Jun 06 |
enell |
148 |
return blockGroups; |
103 |
01 Jun 06 |
enell |
149 |
} |
103 |
01 Jun 06 |
enell |
150 |
} |