247 |
27 Mar 07 |
enell |
1 |
/* |
247 |
27 Mar 07 |
enell |
$Id$ |
247 |
27 Mar 07 |
enell |
3 |
|
247 |
27 Mar 07 |
enell |
Copyright (C) 2006 Johan Enell |
247 |
27 Mar 07 |
enell |
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 |
247 |
27 Mar 07 |
enell |
10 |
|
782 |
18 Sep 08 |
jari |
This is free software; you can redistribute it and/or modify it |
247 |
27 Mar 07 |
enell |
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 |
247 |
27 Mar 07 |
enell |
(at your option) any later version. |
247 |
27 Mar 07 |
enell |
15 |
|
782 |
18 Sep 08 |
jari |
The software is distributed in the hope that it will be useful, but |
247 |
27 Mar 07 |
enell |
WITHOUT ANY WARRANTY; without even the implied warranty of |
782 |
18 Sep 08 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
247 |
27 Mar 07 |
enell |
General Public License for more details. |
247 |
27 Mar 07 |
enell |
20 |
|
247 |
27 Mar 07 |
enell |
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/>. |
247 |
27 Mar 07 |
enell |
23 |
*/ |
247 |
27 Mar 07 |
enell |
24 |
package basefile; |
247 |
27 Mar 07 |
enell |
25 |
|
247 |
27 Mar 07 |
enell |
26 |
import java.util.ArrayList; |
247 |
27 Mar 07 |
enell |
27 |
import java.util.HashMap; |
247 |
27 Mar 07 |
enell |
28 |
import java.util.List; |
247 |
27 Mar 07 |
enell |
29 |
|
248 |
28 Mar 07 |
enell |
30 |
public class BASEFileAssaySection extends BASEFileDataSection |
247 |
27 Mar 07 |
enell |
31 |
{ |
250 |
30 Mar 07 |
enell |
32 |
private final ArrayList<String> data = new ArrayList<String>(); |
247 |
27 Mar 07 |
enell |
33 |
|
247 |
27 Mar 07 |
enell |
34 |
private final HashMap<Integer, Integer> idIndex = new HashMap<Integer, Integer>(); |
247 |
27 Mar 07 |
enell |
35 |
|
250 |
30 Mar 07 |
enell |
36 |
public BASEFileAssaySection() |
250 |
30 Mar 07 |
enell |
37 |
{ |
250 |
30 Mar 07 |
enell |
38 |
super("assays"); |
265 |
27 Apr 07 |
enell |
39 |
this.setAnnotationColumns(""); |
250 |
30 Mar 07 |
enell |
40 |
} |
250 |
30 Mar 07 |
enell |
41 |
|
247 |
27 Mar 07 |
enell |
42 |
public BASEFileAssaySection(BASEFileSection bfs) throws BASEFileException |
247 |
27 Mar 07 |
enell |
43 |
{ |
248 |
28 Mar 07 |
enell |
44 |
super(bfs); |
248 |
28 Mar 07 |
enell |
45 |
if (!bfs.isType("assays")) |
247 |
27 Mar 07 |
enell |
46 |
{ |
247 |
27 Mar 07 |
enell |
47 |
throw new BASEFileException("Section must be of type 'assays' to create an assay section."); |
247 |
27 Mar 07 |
enell |
48 |
} |
250 |
30 Mar 07 |
enell |
49 |
init(); |
247 |
27 Mar 07 |
enell |
50 |
} |
247 |
27 Mar 07 |
enell |
51 |
|
247 |
27 Mar 07 |
enell |
52 |
public final String getAssayName(int id) throws BASEFileException |
247 |
27 Mar 07 |
enell |
53 |
{ |
247 |
27 Mar 07 |
enell |
54 |
int nameIndex = getColumns().indexOf("name"); |
247 |
27 Mar 07 |
enell |
55 |
if (nameIndex == -1) |
247 |
27 Mar 07 |
enell |
56 |
{ |
247 |
27 Mar 07 |
enell |
57 |
throw new BASEFileException("Can't find column 'name' in assay section."); |
247 |
27 Mar 07 |
enell |
58 |
} |
247 |
27 Mar 07 |
enell |
59 |
Integer assay = idIndex.get(id); |
247 |
27 Mar 07 |
enell |
60 |
if (assay == null) |
247 |
27 Mar 07 |
enell |
61 |
{ |
247 |
27 Mar 07 |
enell |
62 |
throw new BASEFileException("Can't find assay with id " + id + " assay section."); |
247 |
27 Mar 07 |
enell |
63 |
} |
247 |
27 Mar 07 |
enell |
64 |
return data.get(assay + nameIndex); |
247 |
27 Mar 07 |
enell |
65 |
} |
247 |
27 Mar 07 |
enell |
66 |
|
247 |
27 Mar 07 |
enell |
67 |
public final List<Integer> getAssayIds() |
247 |
27 Mar 07 |
enell |
68 |
{ |
247 |
27 Mar 07 |
enell |
69 |
return new ArrayList<Integer>(idIndex.values()); |
247 |
27 Mar 07 |
enell |
70 |
} |
247 |
27 Mar 07 |
enell |
71 |
|
250 |
30 Mar 07 |
enell |
72 |
public final int getDataSize() |
250 |
30 Mar 07 |
enell |
73 |
{ |
250 |
30 Mar 07 |
enell |
74 |
return data.size(); |
250 |
30 Mar 07 |
enell |
75 |
} |
250 |
30 Mar 07 |
enell |
76 |
|
247 |
27 Mar 07 |
enell |
77 |
public final List<String> getAnnotationColumns() |
247 |
27 Mar 07 |
enell |
78 |
{ |
250 |
30 Mar 07 |
enell |
79 |
return findFieldList("annotationColumns"); |
247 |
27 Mar 07 |
enell |
80 |
} |
247 |
27 Mar 07 |
enell |
81 |
|
250 |
30 Mar 07 |
enell |
82 |
public final int getAnnotationColumnsColIndex(String col) throws BASEFileException |
250 |
30 Mar 07 |
enell |
83 |
{ |
250 |
30 Mar 07 |
enell |
84 |
return getColIndex("annotationColumns", col); |
250 |
30 Mar 07 |
enell |
85 |
} |
248 |
28 Mar 07 |
enell |
86 |
|
250 |
30 Mar 07 |
enell |
87 |
public final String setAnnotationColumns(Object... values) |
250 |
30 Mar 07 |
enell |
88 |
{ |
250 |
30 Mar 07 |
enell |
89 |
return setHeader("annotationColumns", values); |
250 |
30 Mar 07 |
enell |
90 |
} |
250 |
30 Mar 07 |
enell |
91 |
|
248 |
28 Mar 07 |
enell |
92 |
@Override |
248 |
28 Mar 07 |
enell |
93 |
public int getDataLineLength() |
248 |
28 Mar 07 |
enell |
94 |
{ |
248 |
28 Mar 07 |
enell |
95 |
return getColumns().size(); |
248 |
28 Mar 07 |
enell |
96 |
} |
250 |
30 Mar 07 |
enell |
97 |
|
250 |
30 Mar 07 |
enell |
98 |
@Override |
250 |
30 Mar 07 |
enell |
99 |
public String getDataLine(int i) |
250 |
30 Mar 07 |
enell |
100 |
{ |
250 |
30 Mar 07 |
enell |
101 |
int index = i * getWidth(); |
250 |
30 Mar 07 |
enell |
102 |
String line = data.get(index); |
250 |
30 Mar 07 |
enell |
103 |
for (int j = 1; j < getWidth(); ++j) |
250 |
30 Mar 07 |
enell |
104 |
{ |
250 |
30 Mar 07 |
enell |
105 |
line += "\t" + data.get(index + j); |
250 |
30 Mar 07 |
enell |
106 |
} |
250 |
30 Mar 07 |
enell |
107 |
return line; |
250 |
30 Mar 07 |
enell |
108 |
} |
250 |
30 Mar 07 |
enell |
109 |
|
250 |
30 Mar 07 |
enell |
110 |
@Override |
250 |
30 Mar 07 |
enell |
111 |
protected void init() throws BASEFileException |
250 |
30 Mar 07 |
enell |
112 |
{ |
250 |
30 Mar 07 |
enell |
113 |
super.init(); |
250 |
30 Mar 07 |
enell |
114 |
setHeight(getCount()); |
250 |
30 Mar 07 |
enell |
115 |
setWidth(getColumns().size()); |
250 |
30 Mar 07 |
enell |
116 |
data.ensureCapacity(getHeight()*getWidth()); |
250 |
30 Mar 07 |
enell |
117 |
} |
250 |
30 Mar 07 |
enell |
118 |
|
250 |
30 Mar 07 |
enell |
119 |
@Override |
250 |
30 Mar 07 |
enell |
120 |
public void addData(Object... data) |
250 |
30 Mar 07 |
enell |
121 |
throws BASEFileException |
250 |
30 Mar 07 |
enell |
122 |
{ |
250 |
30 Mar 07 |
enell |
123 |
super.addData(data); |
250 |
30 Mar 07 |
enell |
124 |
addData((String[]) data); |
250 |
30 Mar 07 |
enell |
125 |
} |
247 |
27 Mar 07 |
enell |
126 |
|
250 |
30 Mar 07 |
enell |
127 |
public void addData(String... data) throws BASEFileException |
250 |
30 Mar 07 |
enell |
128 |
{ |
250 |
30 Mar 07 |
enell |
129 |
super.addData((Object[]) data); |
250 |
30 Mar 07 |
enell |
130 |
if (data.length != getWidth()) |
250 |
30 Mar 07 |
enell |
131 |
{ |
250 |
30 Mar 07 |
enell |
132 |
throw new BASEFileException("The data line must contain "+getWidth()+" items"); |
250 |
30 Mar 07 |
enell |
133 |
} |
250 |
30 Mar 07 |
enell |
134 |
int idcol = getColumnsColIndex("id"); |
250 |
30 Mar 07 |
enell |
135 |
int namecol = getColumnsColIndex("name"); |
250 |
30 Mar 07 |
enell |
136 |
idIndex.put(new Integer(data[idcol]), this.data.size() + namecol - 1); |
250 |
30 Mar 07 |
enell |
137 |
for (String s : data) |
250 |
30 Mar 07 |
enell |
138 |
{ |
250 |
30 Mar 07 |
enell |
139 |
this.data.add(s); |
250 |
30 Mar 07 |
enell |
140 |
} |
250 |
30 Mar 07 |
enell |
141 |
} |
250 |
30 Mar 07 |
enell |
142 |
|
250 |
30 Mar 07 |
enell |
143 |
@Override |
250 |
30 Mar 07 |
enell |
144 |
public void validate() |
250 |
30 Mar 07 |
enell |
145 |
throws BASEFileException |
250 |
30 Mar 07 |
enell |
146 |
{ |
250 |
30 Mar 07 |
enell |
147 |
super.validate(); |
250 |
30 Mar 07 |
enell |
148 |
if (getHeader("annotationColumns") == null) throw new BadSectionException("Data section must contain annotationColumns header"); |
250 |
30 Mar 07 |
enell |
149 |
if (getHeader("columns") == null) throw new BadSectionException("Data section must contain columns header"); |
250 |
30 Mar 07 |
enell |
150 |
if (getDataSize() != getHeight() * getWidth()) throw new BadSectionException("The size of the data list is missmatch"); |
250 |
30 Mar 07 |
enell |
151 |
} |
247 |
27 Mar 07 |
enell |
152 |
} |