97 |
20 Apr 06 |
enell |
1 |
/* |
782 |
18 Sep 08 |
jari |
$Id$ |
782 |
18 Sep 08 |
jari |
3 |
|
782 |
18 Sep 08 |
jari |
Copyright (C) 2006 Johan Enell |
782 |
18 Sep 08 |
jari |
5 |
|
782 |
18 Sep 08 |
jari |
This file is part of the se.lu.onk.BaseFile package, a utility |
782 |
18 Sep 08 |
jari |
package for reading files generated by BASE. The package is |
782 |
18 Sep 08 |
jari |
available at http://baseplugins.thep.lu.se/ and BASE web site is |
782 |
18 Sep 08 |
jari |
http://base.thep.lu.se |
782 |
18 Sep 08 |
jari |
10 |
|
782 |
18 Sep 08 |
jari |
This is free software; you can redistribute it and/or modify it |
782 |
18 Sep 08 |
jari |
under the terms of the GNU General Public License as published by |
782 |
18 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
782 |
18 Sep 08 |
jari |
(at your option) any later version. |
782 |
18 Sep 08 |
jari |
15 |
|
782 |
18 Sep 08 |
jari |
The software is distributed in the hope that it will be useful, but |
782 |
18 Sep 08 |
jari |
WITHOUT ANY WARRANTY; without even the implied warranty of |
782 |
18 Sep 08 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
782 |
18 Sep 08 |
jari |
General Public License for more details. |
782 |
18 Sep 08 |
jari |
20 |
|
782 |
18 Sep 08 |
jari |
You should have received a copy of the GNU General Public License |
782 |
18 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
782 |
18 Sep 08 |
jari |
23 |
*/ |
97 |
20 Apr 06 |
enell |
24 |
package basefile; |
97 |
20 Apr 06 |
enell |
25 |
|
97 |
20 Apr 06 |
enell |
26 |
import java.util.ArrayList; |
97 |
20 Apr 06 |
enell |
27 |
import java.util.Arrays; |
114 |
16 Jun 06 |
enell |
28 |
import java.util.Collections; |
103 |
01 Jun 06 |
enell |
29 |
import java.util.Comparator; |
103 |
01 Jun 06 |
enell |
30 |
import java.util.Iterator; |
97 |
20 Apr 06 |
enell |
31 |
import java.util.List; |
97 |
20 Apr 06 |
enell |
32 |
|
248 |
28 Mar 07 |
enell |
33 |
public class BASEFileSpotSection<R, S> extends BASEFileDataSection |
97 |
20 Apr 06 |
enell |
34 |
{ |
97 |
20 Apr 06 |
enell |
35 |
|
250 |
30 Mar 07 |
enell |
36 |
private final ArrayList<S> spotData = new ArrayList<S>(); |
250 |
30 Mar 07 |
enell |
37 |
private final ArrayList<R> reporterData = new ArrayList<R>(); |
250 |
30 Mar 07 |
enell |
38 |
|
250 |
30 Mar 07 |
enell |
39 |
public BASEFileSpotSection() |
250 |
30 Mar 07 |
enell |
40 |
{ |
250 |
30 Mar 07 |
enell |
41 |
super("spots"); |
265 |
27 Apr 07 |
enell |
42 |
this.setChannels(0); |
265 |
27 Apr 07 |
enell |
43 |
this.setAssayFields(); |
265 |
27 Apr 07 |
enell |
44 |
this.setAssays(); |
250 |
30 Mar 07 |
enell |
45 |
} |
250 |
30 Mar 07 |
enell |
46 |
|
248 |
28 Mar 07 |
enell |
47 |
public BASEFileSpotSection(BASEFileSection bfs) throws BASEFileException |
97 |
20 Apr 06 |
enell |
48 |
{ |
248 |
28 Mar 07 |
enell |
49 |
super(bfs); |
248 |
28 Mar 07 |
enell |
50 |
if (!bfs.isType("spots")) |
248 |
28 Mar 07 |
enell |
51 |
{ |
248 |
28 Mar 07 |
enell |
52 |
throw new BASEFileException("Section must be of type 'spots' to create an assay section."); |
248 |
28 Mar 07 |
enell |
53 |
} |
250 |
30 Mar 07 |
enell |
54 |
init(); |
97 |
20 Apr 06 |
enell |
55 |
} |
247 |
27 Mar 07 |
enell |
56 |
|
247 |
27 Mar 07 |
enell |
57 |
@Deprecated |
247 |
27 Mar 07 |
enell |
58 |
public BASEFileSpotSection(int height, int width) |
97 |
20 Apr 06 |
enell |
59 |
{ |
250 |
30 Mar 07 |
enell |
60 |
this(); |
97 |
20 Apr 06 |
enell |
61 |
this.height = height; |
97 |
20 Apr 06 |
enell |
62 |
this.width = width; |
97 |
20 Apr 06 |
enell |
63 |
} |
97 |
20 Apr 06 |
enell |
64 |
|
247 |
27 Mar 07 |
enell |
65 |
@Deprecated |
247 |
27 Mar 07 |
enell |
66 |
public final void setDataMatrix(int height, int width) |
247 |
27 Mar 07 |
enell |
67 |
{ |
248 |
28 Mar 07 |
enell |
68 |
this.height = height; |
248 |
28 Mar 07 |
enell |
69 |
this.width = width; |
248 |
28 Mar 07 |
enell |
70 |
this.spotData.ensureCapacity(height*width); |
248 |
28 Mar 07 |
enell |
71 |
this.reporterData.ensureCapacity(height); |
247 |
27 Mar 07 |
enell |
72 |
} |
247 |
27 Mar 07 |
enell |
73 |
|
103 |
01 Jun 06 |
enell |
74 |
public void sortSpot(Comparator<S> comparator) |
103 |
01 Jun 06 |
enell |
75 |
{ |
114 |
16 Jun 06 |
enell |
76 |
Collections.sort(spotData, comparator); |
103 |
01 Jun 06 |
enell |
77 |
} |
131 |
09 Aug 06 |
enell |
78 |
|
131 |
09 Aug 06 |
enell |
79 |
public void sortReporter(Comparator<R> comparator) |
131 |
09 Aug 06 |
enell |
80 |
{ |
131 |
09 Aug 06 |
enell |
81 |
Collections.sort(reporterData, comparator); |
131 |
09 Aug 06 |
enell |
82 |
} |
103 |
01 Jun 06 |
enell |
83 |
|
103 |
01 Jun 06 |
enell |
84 |
public Iterator<S> spotIterator() |
103 |
01 Jun 06 |
enell |
85 |
{ |
103 |
01 Jun 06 |
enell |
86 |
return spotData.iterator(); |
103 |
01 Jun 06 |
enell |
87 |
} |
250 |
30 Mar 07 |
enell |
88 |
|
250 |
30 Mar 07 |
enell |
89 |
@SuppressWarnings("unchecked") |
250 |
30 Mar 07 |
enell |
90 |
@Override |
250 |
30 Mar 07 |
enell |
91 |
public void addData(Object... data) |
250 |
30 Mar 07 |
enell |
92 |
throws BASEFileException |
250 |
30 Mar 07 |
enell |
93 |
{ |
250 |
30 Mar 07 |
enell |
94 |
super.addData(data); |
250 |
30 Mar 07 |
enell |
95 |
if (data.length < 2) |
250 |
30 Mar 07 |
enell |
96 |
{ |
250 |
30 Mar 07 |
enell |
97 |
throw new BASEFileException("Must have at least two objects to add"); |
250 |
30 Mar 07 |
enell |
98 |
} |
250 |
30 Mar 07 |
enell |
99 |
S[] spots = (S[]) new Object[data.length - 1]; |
250 |
30 Mar 07 |
enell |
100 |
System.arraycopy(data, 1, spots, 0, spots.length); |
250 |
30 Mar 07 |
enell |
101 |
reporterData.add((R) data[0]); |
250 |
30 Mar 07 |
enell |
102 |
spotData.addAll(Arrays.asList(spots)); |
250 |
30 Mar 07 |
enell |
103 |
} |
103 |
01 Jun 06 |
enell |
104 |
|
250 |
30 Mar 07 |
enell |
105 |
public void addData(R reporter, S... spots) throws BASEFileException |
97 |
20 Apr 06 |
enell |
106 |
{ |
250 |
30 Mar 07 |
enell |
107 |
Object[] data = new Object[spots.length + 1]; |
250 |
30 Mar 07 |
enell |
108 |
data[0] = reporter; |
250 |
30 Mar 07 |
enell |
109 |
System.arraycopy(spots, 0, data, 1, spots.length); |
250 |
30 Mar 07 |
enell |
110 |
addData(data); |
97 |
20 Apr 06 |
enell |
111 |
} |
97 |
20 Apr 06 |
enell |
112 |
|
97 |
20 Apr 06 |
enell |
113 |
public void set(int i, R reporter, S... spots) |
97 |
20 Apr 06 |
enell |
114 |
{ |
97 |
20 Apr 06 |
enell |
115 |
if (spotData == null || reporterData == null) return; |
97 |
20 Apr 06 |
enell |
116 |
if (spots.length != width) return; |
97 |
20 Apr 06 |
enell |
117 |
reporterData.set(i, reporter); |
97 |
20 Apr 06 |
enell |
118 |
i = i * width; |
97 |
20 Apr 06 |
enell |
119 |
for (S s : spots) |
97 |
20 Apr 06 |
enell |
120 |
{ |
97 |
20 Apr 06 |
enell |
121 |
spotData.set(i++, s); |
97 |
20 Apr 06 |
enell |
122 |
} |
97 |
20 Apr 06 |
enell |
123 |
} |
97 |
20 Apr 06 |
enell |
124 |
|
97 |
20 Apr 06 |
enell |
125 |
public S setSpot(R reporter, int i, S spot) |
97 |
20 Apr 06 |
enell |
126 |
{ |
97 |
20 Apr 06 |
enell |
127 |
if (spotData == null) return null; |
97 |
20 Apr 06 |
enell |
128 |
if (i >= width) return null; |
97 |
20 Apr 06 |
enell |
129 |
i = reporterData.indexOf(reporter) + i; |
97 |
20 Apr 06 |
enell |
130 |
return spotData.set(i, spot); |
97 |
20 Apr 06 |
enell |
131 |
} |
97 |
20 Apr 06 |
enell |
132 |
|
97 |
20 Apr 06 |
enell |
133 |
public R setReporter(int i, R reporter) |
97 |
20 Apr 06 |
enell |
134 |
{ |
97 |
20 Apr 06 |
enell |
135 |
if (reporterData == null) return null; |
97 |
20 Apr 06 |
enell |
136 |
return reporterData.set(i, reporter); |
97 |
20 Apr 06 |
enell |
137 |
} |
97 |
20 Apr 06 |
enell |
138 |
|
250 |
30 Mar 07 |
enell |
139 |
public List<Integer> getAssays() |
97 |
20 Apr 06 |
enell |
140 |
{ |
97 |
20 Apr 06 |
enell |
141 |
return this.findFieldIntList("assays"); |
97 |
20 Apr 06 |
enell |
142 |
} |
97 |
20 Apr 06 |
enell |
143 |
|
248 |
28 Mar 07 |
enell |
144 |
public int getAssaysColIndex(String col) throws BASEFileException |
248 |
28 Mar 07 |
enell |
145 |
{ |
248 |
28 Mar 07 |
enell |
146 |
return this.getColIndex("assays", col); |
248 |
28 Mar 07 |
enell |
147 |
} |
248 |
28 Mar 07 |
enell |
148 |
|
250 |
30 Mar 07 |
enell |
149 |
public List<String> getAssayFields() |
97 |
20 Apr 06 |
enell |
150 |
{ |
97 |
20 Apr 06 |
enell |
151 |
return this.findFieldList("assayFields"); |
97 |
20 Apr 06 |
enell |
152 |
} |
248 |
28 Mar 07 |
enell |
153 |
|
248 |
28 Mar 07 |
enell |
154 |
public int getAssayFieldsColIndex(String col) throws BASEFileException |
248 |
28 Mar 07 |
enell |
155 |
{ |
248 |
28 Mar 07 |
enell |
156 |
return this.getColIndex("assayFields", col); |
248 |
28 Mar 07 |
enell |
157 |
} |
97 |
20 Apr 06 |
enell |
158 |
|
97 |
20 Apr 06 |
enell |
159 |
public boolean isValid() |
97 |
20 Apr 06 |
enell |
160 |
{ |
250 |
30 Mar 07 |
enell |
161 |
if (getColumns() == null) return false; |
250 |
30 Mar 07 |
enell |
162 |
if (getAssayFields() == null) return false; |
250 |
30 Mar 07 |
enell |
163 |
if (getAssays() == null) return false; |
97 |
20 Apr 06 |
enell |
164 |
return true; |
97 |
20 Apr 06 |
enell |
165 |
} |
97 |
20 Apr 06 |
enell |
166 |
|
104 |
01 Jun 06 |
enell |
167 |
public R getReporter(int i) |
104 |
01 Jun 06 |
enell |
168 |
{ |
104 |
01 Jun 06 |
enell |
169 |
return reporterData.get(i); |
104 |
01 Jun 06 |
enell |
170 |
} |
104 |
01 Jun 06 |
enell |
171 |
|
104 |
01 Jun 06 |
enell |
172 |
public S getSpot(int i) |
104 |
01 Jun 06 |
enell |
173 |
{ |
104 |
01 Jun 06 |
enell |
174 |
return spotData.get(i); |
104 |
01 Jun 06 |
enell |
175 |
} |
253 |
10 Apr 07 |
enell |
176 |
|
253 |
10 Apr 07 |
enell |
177 |
public S getSpot(int x, int y) |
253 |
10 Apr 07 |
enell |
178 |
{ |
253 |
10 Apr 07 |
enell |
179 |
return getSpot(y * getWidth() + x); |
253 |
10 Apr 07 |
enell |
180 |
} |
136 |
10 Aug 06 |
enell |
181 |
|
113 |
14 Jun 06 |
enell |
182 |
public List<S> spotSubList(int fromIndex, int toIndex) |
113 |
14 Jun 06 |
enell |
183 |
{ |
113 |
14 Jun 06 |
enell |
184 |
return spotData.subList(fromIndex, toIndex); |
113 |
14 Jun 06 |
enell |
185 |
} |
103 |
01 Jun 06 |
enell |
186 |
|
136 |
10 Aug 06 |
enell |
187 |
public List<R> reporterSubList(int fromIndex, int toIndex) |
136 |
10 Aug 06 |
enell |
188 |
{ |
136 |
10 Aug 06 |
enell |
189 |
return reporterData.subList(fromIndex, toIndex); |
136 |
10 Aug 06 |
enell |
190 |
} |
136 |
10 Aug 06 |
enell |
191 |
|
114 |
16 Jun 06 |
enell |
192 |
public int getSpotSize() |
103 |
01 Jun 06 |
enell |
193 |
{ |
114 |
16 Jun 06 |
enell |
194 |
return spotData.size(); |
103 |
01 Jun 06 |
enell |
195 |
} |
136 |
10 Aug 06 |
enell |
196 |
|
136 |
10 Aug 06 |
enell |
197 |
public int getReporterSize() |
136 |
10 Aug 06 |
enell |
198 |
{ |
136 |
10 Aug 06 |
enell |
199 |
return reporterData.size(); |
136 |
10 Aug 06 |
enell |
200 |
} |
248 |
28 Mar 07 |
enell |
201 |
|
250 |
30 Mar 07 |
enell |
202 |
public final int getChannels() |
250 |
30 Mar 07 |
enell |
203 |
{ |
250 |
30 Mar 07 |
enell |
204 |
return findIntOpt("channels"); |
250 |
30 Mar 07 |
enell |
205 |
} |
250 |
30 Mar 07 |
enell |
206 |
|
250 |
30 Mar 07 |
enell |
207 |
public final String setChannels(int channels) |
250 |
30 Mar 07 |
enell |
208 |
{ |
250 |
30 Mar 07 |
enell |
209 |
return setHeader("channels", channels); |
250 |
30 Mar 07 |
enell |
210 |
} |
250 |
30 Mar 07 |
enell |
211 |
|
250 |
30 Mar 07 |
enell |
212 |
public final String setExtraFloats(Object... values) |
250 |
30 Mar 07 |
enell |
213 |
{ |
250 |
30 Mar 07 |
enell |
214 |
return setHeader("setExtraFloats", values); |
250 |
30 Mar 07 |
enell |
215 |
} |
250 |
30 Mar 07 |
enell |
216 |
|
250 |
30 Mar 07 |
enell |
217 |
public final String setAssayFields(Object... values) |
250 |
30 Mar 07 |
enell |
218 |
{ |
250 |
30 Mar 07 |
enell |
219 |
return setHeader("assayFields", values); |
250 |
30 Mar 07 |
enell |
220 |
} |
250 |
30 Mar 07 |
enell |
221 |
|
250 |
30 Mar 07 |
enell |
222 |
public final String setAssays(Integer... values) |
250 |
30 Mar 07 |
enell |
223 |
{ |
250 |
30 Mar 07 |
enell |
224 |
return setHeader("assays", (Object[]) values); |
250 |
30 Mar 07 |
enell |
225 |
} |
250 |
30 Mar 07 |
enell |
226 |
|
248 |
28 Mar 07 |
enell |
227 |
@Override |
248 |
28 Mar 07 |
enell |
228 |
public int getDataLineLength() |
248 |
28 Mar 07 |
enell |
229 |
{ |
250 |
30 Mar 07 |
enell |
230 |
int length = this.getColumns().size() -1; |
250 |
30 Mar 07 |
enell |
231 |
length += this.getAssayFields().size() * this.getAssays().size(); |
248 |
28 Mar 07 |
enell |
232 |
return length; |
248 |
28 Mar 07 |
enell |
233 |
} |
250 |
30 Mar 07 |
enell |
234 |
|
250 |
30 Mar 07 |
enell |
235 |
@Override |
250 |
30 Mar 07 |
enell |
236 |
public String getDataLine(int i) |
250 |
30 Mar 07 |
enell |
237 |
{ |
253 |
10 Apr 07 |
enell |
238 |
String line = getReporter(i).toString(); |
253 |
10 Apr 07 |
enell |
239 |
for (int j = 0; j < getWidth(); ++j) |
253 |
10 Apr 07 |
enell |
240 |
{ |
253 |
10 Apr 07 |
enell |
241 |
line += "\t"+getSpot(i, j); |
253 |
10 Apr 07 |
enell |
242 |
} |
253 |
10 Apr 07 |
enell |
243 |
return line; |
250 |
30 Mar 07 |
enell |
244 |
} |
250 |
30 Mar 07 |
enell |
245 |
|
250 |
30 Mar 07 |
enell |
246 |
@Override |
250 |
30 Mar 07 |
enell |
247 |
protected void init() throws BASEFileException |
250 |
30 Mar 07 |
enell |
248 |
{ |
250 |
30 Mar 07 |
enell |
249 |
super.init(); |
250 |
30 Mar 07 |
enell |
250 |
setHeight(getCount()); |
250 |
30 Mar 07 |
enell |
251 |
setWidth(getAssays().size()); |
250 |
30 Mar 07 |
enell |
252 |
spotData.ensureCapacity(getHeight()*getWidth()); |
250 |
30 Mar 07 |
enell |
253 |
reporterData.ensureCapacity(getHeight()); |
250 |
30 Mar 07 |
enell |
254 |
} |
250 |
30 Mar 07 |
enell |
255 |
|
250 |
30 Mar 07 |
enell |
256 |
@Override |
250 |
30 Mar 07 |
enell |
257 |
public void validate() throws BASEFileException |
250 |
30 Mar 07 |
enell |
258 |
{ |
250 |
30 Mar 07 |
enell |
259 |
super.validate(); |
250 |
30 Mar 07 |
enell |
260 |
if (getHeader("channels") == null) throw new BadSectionException("Data section must contain channels header"); |
250 |
30 Mar 07 |
enell |
261 |
if (getHeader("assays") == null) throw new BadSectionException("Data section must contain assays header"); |
250 |
30 Mar 07 |
enell |
262 |
if (getHeader("assayFields") == null) throw new BadSectionException("Data section must contain assayFields header"); |
250 |
30 Mar 07 |
enell |
263 |
if (getHeader("columns") == null) throw new BadSectionException("Data section must contain columns header"); |
250 |
30 Mar 07 |
enell |
264 |
if (getHeader("setExtraFloats") != null) |
250 |
30 Mar 07 |
enell |
265 |
{ |
250 |
30 Mar 07 |
enell |
266 |
List<String> assayFields = getAssayFields(); |
250 |
30 Mar 07 |
enell |
267 |
for (String s : findFieldList("setExtraFloats")) |
250 |
30 Mar 07 |
enell |
268 |
{ |
250 |
30 Mar 07 |
enell |
269 |
if (!assayFields.contains(s)) |
250 |
30 Mar 07 |
enell |
270 |
{ |
250 |
30 Mar 07 |
enell |
271 |
throw new BadSectionException("Header assayFields must contain "+s); |
250 |
30 Mar 07 |
enell |
272 |
} |
250 |
30 Mar 07 |
enell |
273 |
} |
250 |
30 Mar 07 |
enell |
274 |
} |
250 |
30 Mar 07 |
enell |
275 |
if (getReporterSize() != getHeight()) throw new BadSectionException("The size of the reporter list and height doesn't match"); |
250 |
30 Mar 07 |
enell |
276 |
if (getSpotSize() != getHeight()*getWidth()) throw new BadSectionException("The size of the spot list and height*width doesn't match"); |
250 |
30 Mar 07 |
enell |
277 |
} |
97 |
20 Apr 06 |
enell |
278 |
} |