5230 |
04 Feb 10 |
nicklas |
1 |
/* |
5230 |
04 Feb 10 |
nicklas |
$Id$ |
5230 |
04 Feb 10 |
nicklas |
3 |
|
5230 |
04 Feb 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5230 |
04 Feb 10 |
nicklas |
5 |
|
5230 |
04 Feb 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5230 |
04 Feb 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5230 |
04 Feb 10 |
nicklas |
8 |
|
5230 |
04 Feb 10 |
nicklas |
BASE is free software; you can redistribute it and/or modify it |
5230 |
04 Feb 10 |
nicklas |
under the terms of the GNU General Public License as published by |
5230 |
04 Feb 10 |
nicklas |
the Free Software Foundation; either version 3 of the License, or |
5230 |
04 Feb 10 |
nicklas |
at your option) any later version. |
5230 |
04 Feb 10 |
nicklas |
13 |
|
5230 |
04 Feb 10 |
nicklas |
BASE is distributed in the hope that it will be useful, but |
5230 |
04 Feb 10 |
nicklas |
WITHOUT ANY WARRANTY; without even the implied warranty of |
5230 |
04 Feb 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
5230 |
04 Feb 10 |
nicklas |
General Public License for more details. |
5230 |
04 Feb 10 |
nicklas |
18 |
|
5230 |
04 Feb 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5230 |
04 Feb 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5230 |
04 Feb 10 |
nicklas |
21 |
*/ |
5230 |
04 Feb 10 |
nicklas |
22 |
|
5230 |
04 Feb 10 |
nicklas |
23 |
import net.sf.basedb.core.BioAssay; |
5230 |
04 Feb 10 |
nicklas |
24 |
import net.sf.basedb.core.BioAssaySet; |
5230 |
04 Feb 10 |
nicklas |
25 |
import net.sf.basedb.core.DataFileType; |
5230 |
04 Feb 10 |
nicklas |
26 |
import net.sf.basedb.core.DbControl; |
5230 |
04 Feb 10 |
nicklas |
27 |
import net.sf.basedb.core.File; |
5230 |
04 Feb 10 |
nicklas |
28 |
import net.sf.basedb.core.FileStoreUtil; |
5230 |
04 Feb 10 |
nicklas |
29 |
import net.sf.basedb.core.Formula; |
5230 |
04 Feb 10 |
nicklas |
30 |
import net.sf.basedb.core.ItemQuery; |
5235 |
05 Feb 10 |
nicklas |
31 |
import net.sf.basedb.core.Transformation; |
5319 |
20 Apr 10 |
nicklas |
32 |
import net.sf.basedb.core.Type; |
5230 |
04 Feb 10 |
nicklas |
33 |
import net.sf.basedb.core.VirtualColumn; |
5230 |
04 Feb 10 |
nicklas |
34 |
import net.sf.basedb.core.query.Dynamic; |
5230 |
04 Feb 10 |
nicklas |
35 |
import net.sf.basedb.core.query.Expressions; |
5230 |
04 Feb 10 |
nicklas |
36 |
import net.sf.basedb.core.query.Hql; |
5230 |
04 Feb 10 |
nicklas |
37 |
import net.sf.basedb.core.query.Orders; |
5230 |
04 Feb 10 |
nicklas |
38 |
import net.sf.basedb.core.query.Restrictions; |
5230 |
04 Feb 10 |
nicklas |
39 |
import net.sf.basedb.util.ConsoleProgressReporter; |
5230 |
04 Feb 10 |
nicklas |
40 |
import net.sf.basedb.util.basefile.BaseFileWriter; |
5230 |
04 Feb 10 |
nicklas |
41 |
import net.sf.basedb.util.export.spotdata.BaseFileExporter; |
5230 |
04 Feb 10 |
nicklas |
42 |
import net.sf.basedb.util.export.spotdata.DynamicField; |
5230 |
04 Feb 10 |
nicklas |
43 |
import net.sf.basedb.util.export.spotdata.ExportableFieldFactory; |
5230 |
04 Feb 10 |
nicklas |
44 |
import net.sf.basedb.util.export.spotdata.MatrixBaseFileExporter; |
5230 |
04 Feb 10 |
nicklas |
45 |
import net.sf.basedb.util.export.spotdata.SerialBaseFileExporter; |
5374 |
03 Aug 10 |
nicklas |
46 |
import net.sf.basedb.util.importer.BaseFileWrapper; |
5235 |
05 Feb 10 |
nicklas |
47 |
import net.sf.basedb.util.importer.spotdata.BaseFileImporter; |
5230 |
04 Feb 10 |
nicklas |
48 |
|
5230 |
04 Feb 10 |
nicklas |
49 |
import java.io.OutputStreamWriter; |
5230 |
04 Feb 10 |
nicklas |
50 |
import java.util.ArrayList; |
5230 |
04 Feb 10 |
nicklas |
51 |
import java.util.List; |
5230 |
04 Feb 10 |
nicklas |
52 |
|
5230 |
04 Feb 10 |
nicklas |
53 |
|
5230 |
04 Feb 10 |
nicklas |
54 |
public class TestBaseFileExporterImporter |
5230 |
04 Feb 10 |
nicklas |
55 |
{ |
5230 |
04 Feb 10 |
nicklas |
56 |
static boolean ok = true; |
5230 |
04 Feb 10 |
nicklas |
57 |
|
5230 |
04 Feb 10 |
nicklas |
58 |
public static void main(String[] args) |
5230 |
04 Feb 10 |
nicklas |
59 |
{ |
5230 |
04 Feb 10 |
nicklas |
60 |
TestUtil.checkArgs(args); |
5230 |
04 Feb 10 |
nicklas |
61 |
TestUtil.begin(); |
5230 |
04 Feb 10 |
nicklas |
62 |
ok = test_all(); |
5230 |
04 Feb 10 |
nicklas |
63 |
TestUtil.stop(); |
5230 |
04 Feb 10 |
nicklas |
64 |
} |
5230 |
04 Feb 10 |
nicklas |
65 |
|
5230 |
04 Feb 10 |
nicklas |
66 |
static boolean test_all() |
5230 |
04 Feb 10 |
nicklas |
67 |
{ |
5230 |
04 Feb 10 |
nicklas |
68 |
boolean wasSetup = TestAnalyzeUtil.setupExperiment(); |
5230 |
04 Feb 10 |
nicklas |
69 |
write("++Testing BaseFileExporter and BaseFileImporter"); |
5230 |
04 Feb 10 |
nicklas |
70 |
try |
5230 |
04 Feb 10 |
nicklas |
71 |
{ |
5230 |
04 Feb 10 |
nicklas |
72 |
int experimentId = TestAnalyzeUtil.getItemId("experiment.genepix"); |
5230 |
04 Feb 10 |
nicklas |
73 |
int rootBasId = TestAnalyzeUtil.getItemId("bioassayset.root"); |
5230 |
04 Feb 10 |
nicklas |
74 |
if (experimentId == 0 || rootBasId == 0) |
5230 |
04 Feb 10 |
nicklas |
75 |
{ |
5230 |
04 Feb 10 |
nicklas |
76 |
ok = false; |
5230 |
04 Feb 10 |
nicklas |
77 |
write("Test experiment was not setup correctly."); |
5230 |
04 Feb 10 |
nicklas |
78 |
return ok; |
5230 |
04 Feb 10 |
nicklas |
79 |
} |
5230 |
04 Feb 10 |
nicklas |
80 |
|
5230 |
04 Feb 10 |
nicklas |
81 |
int fileSerialNoMerge = TestFile.test_create("serial.nomerge.txt", false, false); |
5230 |
04 Feb 10 |
nicklas |
82 |
int fileSerialMerge = TestFile.test_create("serial.merge.txt", false, false); |
5230 |
04 Feb 10 |
nicklas |
83 |
int fileMatrixNoMerge = TestFile.test_create("matrix.nomerge.txt", false, false); |
5230 |
04 Feb 10 |
nicklas |
84 |
int fileMatrixMerge = TestFile.test_create("matrix.merge.txt", false, false); |
5230 |
04 Feb 10 |
nicklas |
85 |
|
5230 |
04 Feb 10 |
nicklas |
86 |
int exportRoot = TestAnalyzeUtil.clone_bioassay_set(rootBasId, "BaseFileExporter"); |
5230 |
04 Feb 10 |
nicklas |
87 |
int exportNoMerge = TestAnalyzeUtil.clone_bioassay_set(exportRoot, "No merge"); |
5230 |
04 Feb 10 |
nicklas |
88 |
int exportMerge = TestAnalyzeUtil.clone_bioassay_set(exportRoot, "Merge"); |
5230 |
04 Feb 10 |
nicklas |
89 |
test_export(exportNoMerge, fileSerialNoMerge, false, false); |
5230 |
04 Feb 10 |
nicklas |
90 |
test_export(exportMerge, fileSerialMerge, false, true); |
5230 |
04 Feb 10 |
nicklas |
91 |
test_export(exportNoMerge, fileMatrixNoMerge, true, false); |
5230 |
04 Feb 10 |
nicklas |
92 |
test_export(exportMerge, fileMatrixMerge, true, true); |
5230 |
04 Feb 10 |
nicklas |
93 |
|
5235 |
05 Feb 10 |
nicklas |
94 |
test_import(exportNoMerge, fileSerialNoMerge, "Import serial BASEfile"); |
5235 |
05 Feb 10 |
nicklas |
95 |
test_import(exportNoMerge, fileMatrixNoMerge, "Import matrix BASEfile"); |
5235 |
05 Feb 10 |
nicklas |
96 |
test_import(exportMerge, fileSerialMerge, "Import merged serial BASEfile"); |
5235 |
05 Feb 10 |
nicklas |
97 |
test_import(exportMerge, fileMatrixMerge, "Import merged matrix BASEfile"); |
5235 |
05 Feb 10 |
nicklas |
98 |
|
5230 |
04 Feb 10 |
nicklas |
99 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
5230 |
04 Feb 10 |
nicklas |
100 |
TestAnalyzeUtil.test_delete_bioassayset(exportRoot); |
5230 |
04 Feb 10 |
nicklas |
101 |
|
5230 |
04 Feb 10 |
nicklas |
102 |
TestFile.test_delete(fileSerialNoMerge); |
5230 |
04 Feb 10 |
nicklas |
103 |
TestFile.test_delete(fileSerialMerge); |
5230 |
04 Feb 10 |
nicklas |
104 |
TestFile.test_delete(fileMatrixNoMerge); |
5230 |
04 Feb 10 |
nicklas |
105 |
TestFile.test_delete(fileMatrixMerge); |
5230 |
04 Feb 10 |
nicklas |
106 |
} |
5230 |
04 Feb 10 |
nicklas |
107 |
finally |
5230 |
04 Feb 10 |
nicklas |
108 |
{ |
5230 |
04 Feb 10 |
nicklas |
109 |
if (wasSetup) TestAnalyzeUtil.cleanupExperiment(); |
5230 |
04 Feb 10 |
nicklas |
110 |
write("++Testing BaseFileExporter and BaseFileImporter "+(ok ? "OK" : "Failed")+"\n"); |
5230 |
04 Feb 10 |
nicklas |
111 |
} |
5230 |
04 Feb 10 |
nicklas |
112 |
return ok; |
5230 |
04 Feb 10 |
nicklas |
113 |
} |
5230 |
04 Feb 10 |
nicklas |
114 |
|
5230 |
04 Feb 10 |
nicklas |
115 |
/** |
5230 |
04 Feb 10 |
nicklas |
The BioAssaySet thats beeing tested must be the one created by this class. |
5230 |
04 Feb 10 |
nicklas |
117 |
*/ |
5230 |
04 Feb 10 |
nicklas |
118 |
static void test_export(int bioAssaySetId, int fileId, |
5230 |
04 Feb 10 |
nicklas |
119 |
boolean matrix, boolean mergeReporters) |
5230 |
04 Feb 10 |
nicklas |
120 |
{ |
5235 |
05 Feb 10 |
nicklas |
121 |
if (bioAssaySetId == 0 || fileId == 0) return; |
5230 |
04 Feb 10 |
nicklas |
122 |
DbControl dc = null; |
5230 |
04 Feb 10 |
nicklas |
123 |
try |
5230 |
04 Feb 10 |
nicklas |
124 |
{ |
5230 |
04 Feb 10 |
nicklas |
125 |
dc = TestUtil.getDbControl(); |
5230 |
04 Feb 10 |
nicklas |
126 |
BioAssaySet bas = BioAssaySet.getById(dc, bioAssaySetId); |
5230 |
04 Feb 10 |
nicklas |
127 |
File file = File.getById(dc, fileId); |
5230 |
04 Feb 10 |
nicklas |
128 |
ItemQuery<BioAssay> query = bas.getBioAssays(); |
5230 |
04 Feb 10 |
nicklas |
129 |
query.order(Orders.asc(Hql.property("name"))); |
5230 |
04 Feb 10 |
nicklas |
130 |
query.order(Orders.asc(Hql.property("id"))); |
5230 |
04 Feb 10 |
nicklas |
131 |
List<BioAssay> assays = new ArrayList<BioAssay>(query.list(dc)); |
5230 |
04 Feb 10 |
nicklas |
132 |
|
5230 |
04 Feb 10 |
nicklas |
// Initialise exporter |
5230 |
04 Feb 10 |
nicklas |
134 |
BaseFileExporter bfe = matrix ? new MatrixBaseFileExporter() : new SerialBaseFileExporter(); |
5230 |
04 Feb 10 |
nicklas |
135 |
bfe.setDbControl(dc); |
5230 |
04 Feb 10 |
nicklas |
136 |
bfe.setSource(bas); |
5230 |
04 Feb 10 |
nicklas |
137 |
bfe.setAverageOnReporter(mergeReporters); |
5230 |
04 Feb 10 |
nicklas |
138 |
if (!TestUtil.getSilent()) bfe.setProgressReporter(new ConsoleProgressReporter()); |
5230 |
04 Feb 10 |
nicklas |
139 |
bfe.setWriter(new BaseFileWriter(new OutputStreamWriter(file.getUploadStream(false)))); |
5230 |
04 Feb 10 |
nicklas |
140 |
|
5230 |
04 Feb 10 |
nicklas |
// Parameters that go in the 'settings' section |
5230 |
04 Feb 10 |
nicklas |
142 |
bfe.setParameter("b", "some parameter"); |
5230 |
04 Feb 10 |
nicklas |
143 |
bfe.setParameter("a", "some other parameter"); |
5230 |
04 Feb 10 |
nicklas |
144 |
|
5230 |
04 Feb 10 |
nicklas |
// Configure the reporter fields to export |
5230 |
04 Feb 10 |
nicklas |
146 |
List<DynamicField> reporterFields = new ArrayList<DynamicField>(); |
5319 |
20 Apr 10 |
nicklas |
147 |
reporterFields.add(ExportableFieldFactory.simple(Dynamic.column(VirtualColumn.POSITION), "position", Type.INT, Formula.AverageMethod.NONE, null)); |
5319 |
20 Apr 10 |
nicklas |
148 |
reporterFields.add(ExportableFieldFactory.reporter("id", "reporter", Type.INT, Formula.AverageMethod.NONE, null)); |
5319 |
20 Apr 10 |
nicklas |
149 |
reporterFields.add(ExportableFieldFactory.reporter("name", null, Type.STRING, Formula.AverageMethod.NONE, null)); |
5230 |
04 Feb 10 |
nicklas |
150 |
bfe.addReporterFields(reporterFields); |
5230 |
04 Feb 10 |
nicklas |
151 |
|
5230 |
04 Feb 10 |
nicklas |
// Configure the spot fields to export |
5230 |
04 Feb 10 |
nicklas |
153 |
ItemQuery<Formula> ratio = Formula.getQuery(Formula.Type.COLUMN_EXPRESSION, |
5230 |
04 Feb 10 |
nicklas |
154 |
bas.getRawDataType(), bas.getIntensityTransform()); |
5230 |
04 Feb 10 |
nicklas |
155 |
ratio.restrict(Restrictions.eq(Hql.property("name"), Expressions.string("Ratio, ch1 / ch2"))); |
5230 |
04 Feb 10 |
nicklas |
156 |
if (ratio.count(dc) < 1) |
5230 |
04 Feb 10 |
nicklas |
157 |
{ |
5230 |
04 Feb 10 |
nicklas |
158 |
throw new Exception("Cant find formula 'Ratio, ch1 / ch2'"); |
5230 |
04 Feb 10 |
nicklas |
159 |
} |
5230 |
04 Feb 10 |
nicklas |
160 |
|
5230 |
04 Feb 10 |
nicklas |
161 |
List<DynamicField> spotFields = new ArrayList<DynamicField>(); |
5230 |
04 Feb 10 |
nicklas |
162 |
spotFields.add(ExportableFieldFactory.channel(1, "intensity1", null)); |
5235 |
05 Feb 10 |
nicklas |
163 |
spotFields.add(ExportableFieldFactory.channel(2, "intensity2", null)); |
5319 |
20 Apr 10 |
nicklas |
164 |
spotFields.add(ExportableFieldFactory.rawData("row", null, Type.INT, Formula.AverageMethod.NONE, null)); |
5319 |
20 Apr 10 |
nicklas |
165 |
spotFields.add(ExportableFieldFactory.rawData("flags", null, Type.INT, Formula.AverageMethod.NONE, null)); |
5230 |
04 Feb 10 |
nicklas |
166 |
spotFields.add(ExportableFieldFactory.formula(ratio.list(dc).get(0), null, null)); |
5230 |
04 Feb 10 |
nicklas |
167 |
bfe.addSpotFields(spotFields); |
5230 |
04 Feb 10 |
nicklas |
168 |
|
5230 |
04 Feb 10 |
nicklas |
169 |
bfe.doExport(); |
5230 |
04 Feb 10 |
nicklas |
170 |
FileStoreUtil.setDataFile(dc, bas, |
5230 |
04 Feb 10 |
nicklas |
171 |
matrix ? DataFileType.BASEFILE_MATRIX : DataFileType.BASEFILE_SERIAL, |
5230 |
04 Feb 10 |
nicklas |
172 |
file); |
5230 |
04 Feb 10 |
nicklas |
173 |
if (!TestUtil.getSilent()) write(""); |
5230 |
04 Feb 10 |
nicklas |
174 |
dc.commit(); |
5230 |
04 Feb 10 |
nicklas |
175 |
|
5230 |
04 Feb 10 |
nicklas |
176 |
write("--Exporting basefile [matrix:"+matrix+", mergeReporters:"+mergeReporters+"] OK"); |
5230 |
04 Feb 10 |
nicklas |
177 |
} |
5230 |
04 Feb 10 |
nicklas |
178 |
catch (Throwable ex) |
5230 |
04 Feb 10 |
nicklas |
179 |
{ |
5230 |
04 Feb 10 |
nicklas |
180 |
write("--Exporting basefile [matrix:"+matrix+", mergeReporters:"+mergeReporters+"] FAILED"); |
5230 |
04 Feb 10 |
nicklas |
181 |
ex.printStackTrace(); |
5230 |
04 Feb 10 |
nicklas |
182 |
ok = false; |
5230 |
04 Feb 10 |
nicklas |
183 |
} |
5230 |
04 Feb 10 |
nicklas |
184 |
finally |
5230 |
04 Feb 10 |
nicklas |
185 |
{ |
5230 |
04 Feb 10 |
nicklas |
186 |
if (dc != null) dc.close(); |
5230 |
04 Feb 10 |
nicklas |
187 |
} |
5230 |
04 Feb 10 |
nicklas |
188 |
} |
5230 |
04 Feb 10 |
nicklas |
189 |
|
5235 |
05 Feb 10 |
nicklas |
190 |
static void test_import(int bioAssaySetId, int fileId, |
5235 |
05 Feb 10 |
nicklas |
191 |
String childName) |
5235 |
05 Feb 10 |
nicklas |
192 |
{ |
5235 |
05 Feb 10 |
nicklas |
193 |
if (bioAssaySetId == 0 || fileId == 0) return; |
5235 |
05 Feb 10 |
nicklas |
194 |
DbControl dc = null; |
5235 |
05 Feb 10 |
nicklas |
195 |
try |
5235 |
05 Feb 10 |
nicklas |
196 |
{ |
5235 |
05 Feb 10 |
nicklas |
197 |
dc = TestUtil.getDbControl(); |
5235 |
05 Feb 10 |
nicklas |
198 |
BioAssaySet parent = BioAssaySet.getById(dc, bioAssaySetId); |
5235 |
05 Feb 10 |
nicklas |
199 |
File file = File.getById(dc, fileId); |
5235 |
05 Feb 10 |
nicklas |
200 |
|
5235 |
05 Feb 10 |
nicklas |
201 |
Transformation t = parent.newTransformation(null); |
5235 |
05 Feb 10 |
nicklas |
202 |
t.setName("Importing BASEfile: " + file.getName()); |
5235 |
05 Feb 10 |
nicklas |
203 |
dc.saveItem(t); |
5235 |
05 Feb 10 |
nicklas |
204 |
|
5235 |
05 Feb 10 |
nicklas |
205 |
BaseFileImporter importer = new BaseFileImporter(); |
5235 |
05 Feb 10 |
nicklas |
206 |
importer.setDbControl(dc); |
5374 |
03 Aug 10 |
nicklas |
207 |
importer.setSourceFileWrapper(new BaseFileWrapper(file)); |
5235 |
05 Feb 10 |
nicklas |
208 |
importer.setTransformation(t); |
5235 |
05 Feb 10 |
nicklas |
209 |
if (!TestUtil.getSilent()) importer.setProgressReporter(new ConsoleProgressReporter()); |
5235 |
05 Feb 10 |
nicklas |
210 |
BioAssaySet child = importer.doImport(); |
5235 |
05 Feb 10 |
nicklas |
211 |
child.setName(childName); |
5235 |
05 Feb 10 |
nicklas |
212 |
dc.commit(); |
5235 |
05 Feb 10 |
nicklas |
213 |
if (!TestUtil.getSilent()) write(""); |
5235 |
05 Feb 10 |
nicklas |
214 |
write("--Importing BASEfile for " + childName + " OK"); |
5235 |
05 Feb 10 |
nicklas |
215 |
} |
5235 |
05 Feb 10 |
nicklas |
216 |
catch (Throwable ex) |
5235 |
05 Feb 10 |
nicklas |
217 |
{ |
5235 |
05 Feb 10 |
nicklas |
218 |
write("--Importing BASEfile for " + childName + " FAILED"); |
5235 |
05 Feb 10 |
nicklas |
219 |
ex.printStackTrace(); |
5235 |
05 Feb 10 |
nicklas |
220 |
ok = false; |
5235 |
05 Feb 10 |
nicklas |
221 |
} |
5235 |
05 Feb 10 |
nicklas |
222 |
finally |
5235 |
05 Feb 10 |
nicklas |
223 |
{ |
5235 |
05 Feb 10 |
nicklas |
224 |
if (dc != null) dc.close(); |
5235 |
05 Feb 10 |
nicklas |
225 |
} |
5235 |
05 Feb 10 |
nicklas |
226 |
} |
5235 |
05 Feb 10 |
nicklas |
227 |
|
5230 |
04 Feb 10 |
nicklas |
228 |
|
5230 |
04 Feb 10 |
nicklas |
229 |
static void write(String message) |
5230 |
04 Feb 10 |
nicklas |
230 |
{ |
5230 |
04 Feb 10 |
nicklas |
231 |
System.out.println(message); |
5230 |
04 Feb 10 |
nicklas |
232 |
} |
5230 |
04 Feb 10 |
nicklas |
233 |
|
5231 |
04 Feb 10 |
nicklas |
234 |
|
5230 |
04 Feb 10 |
nicklas |
235 |
|
5230 |
04 Feb 10 |
nicklas |
236 |
} |