226 |
09 Jan 07 |
mbayer |
1 |
/* |
239 |
20 Feb 07 |
mbayer |
Copyright (C) Authors contributing to this file. |
226 |
09 Jan 07 |
mbayer |
3 |
|
239 |
20 Feb 07 |
mbayer |
This program is free software; you can redistribute it and/or modify |
239 |
20 Feb 07 |
mbayer |
it under the terms of the GNU General Public License as published by |
239 |
20 Feb 07 |
mbayer |
the Free Software Foundation; either version 2 of the License, or |
239 |
20 Feb 07 |
mbayer |
(at your option) any later version. |
226 |
09 Jan 07 |
mbayer |
8 |
|
239 |
20 Feb 07 |
mbayer |
This program is distributed in the hope that it will be useful, |
239 |
20 Feb 07 |
mbayer |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
239 |
20 Feb 07 |
mbayer |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
239 |
20 Feb 07 |
mbayer |
GNU General Public License for more details. |
226 |
09 Jan 07 |
mbayer |
13 |
|
239 |
20 Feb 07 |
mbayer |
You should have received a copy of the GNU General Public License along |
239 |
20 Feb 07 |
mbayer |
with this program; if not, write to the Free Software Foundation, Inc., |
239 |
20 Feb 07 |
mbayer |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
226 |
09 Jan 07 |
mbayer |
17 |
|
239 |
20 Feb 07 |
mbayer |
See also http://www.gnu.org/licenses/gpl.txt. |
231 |
14 Feb 07 |
mbayer |
19 |
*/ |
226 |
09 Jan 07 |
mbayer |
20 |
package sbrn.base; |
226 |
09 Jan 07 |
mbayer |
21 |
|
239 |
20 Feb 07 |
mbayer |
22 |
import java.io.IOException; |
239 |
20 Feb 07 |
mbayer |
23 |
import java.io.InputStream; |
239 |
20 Feb 07 |
mbayer |
24 |
import java.net.InetAddress; |
239 |
20 Feb 07 |
mbayer |
25 |
import java.net.UnknownHostException; |
239 |
20 Feb 07 |
mbayer |
26 |
import java.text.SimpleDateFormat; |
239 |
20 Feb 07 |
mbayer |
27 |
import java.util.Date; |
239 |
20 Feb 07 |
mbayer |
28 |
import java.util.Enumeration; |
239 |
20 Feb 07 |
mbayer |
29 |
import java.util.List; |
239 |
20 Feb 07 |
mbayer |
30 |
import java.util.Locale; |
239 |
20 Feb 07 |
mbayer |
31 |
import java.util.zip.ZipEntry; |
239 |
20 Feb 07 |
mbayer |
32 |
import java.util.zip.ZipFile; |
239 |
20 Feb 07 |
mbayer |
33 |
import java.util.zip.ZipInputStream; |
239 |
20 Feb 07 |
mbayer |
34 |
|
231 |
14 Feb 07 |
mbayer |
35 |
import net.sf.basedb.core.Affymetrix; |
231 |
14 Feb 07 |
mbayer |
36 |
import net.sf.basedb.core.ArrayDesign; |
226 |
09 Jan 07 |
mbayer |
37 |
import net.sf.basedb.core.BaseException; |
226 |
09 Jan 07 |
mbayer |
38 |
import net.sf.basedb.core.DbControl; |
226 |
09 Jan 07 |
mbayer |
39 |
import net.sf.basedb.core.Directory; |
226 |
09 Jan 07 |
mbayer |
40 |
import net.sf.basedb.core.Experiment; |
226 |
09 Jan 07 |
mbayer |
41 |
import net.sf.basedb.core.File; |
226 |
09 Jan 07 |
mbayer |
42 |
import net.sf.basedb.core.FileParameterType; |
226 |
09 Jan 07 |
mbayer |
43 |
import net.sf.basedb.core.ItemParameterType; |
226 |
09 Jan 07 |
mbayer |
44 |
import net.sf.basedb.core.ItemQuery; |
226 |
09 Jan 07 |
mbayer |
45 |
import net.sf.basedb.core.ItemResultList; |
226 |
09 Jan 07 |
mbayer |
46 |
import net.sf.basedb.core.Job; |
239 |
20 Feb 07 |
mbayer |
47 |
import net.sf.basedb.core.Location; |
226 |
09 Jan 07 |
mbayer |
48 |
import net.sf.basedb.core.Permission; |
226 |
09 Jan 07 |
mbayer |
49 |
import net.sf.basedb.core.PluginConfiguration; |
226 |
09 Jan 07 |
mbayer |
50 |
import net.sf.basedb.core.PluginDefinition; |
226 |
09 Jan 07 |
mbayer |
51 |
import net.sf.basedb.core.PluginExecutionRequest; |
226 |
09 Jan 07 |
mbayer |
52 |
import net.sf.basedb.core.PluginResponse; |
226 |
09 Jan 07 |
mbayer |
53 |
import net.sf.basedb.core.ProgressReporter; |
239 |
20 Feb 07 |
mbayer |
54 |
import net.sf.basedb.core.Quota; |
239 |
20 Feb 07 |
mbayer |
55 |
import net.sf.basedb.core.QuotaType; |
226 |
09 Jan 07 |
mbayer |
56 |
import net.sf.basedb.core.RawBioAssay; |
226 |
09 Jan 07 |
mbayer |
57 |
import net.sf.basedb.core.RawDataType; |
226 |
09 Jan 07 |
mbayer |
58 |
import net.sf.basedb.core.SessionControl; |
226 |
09 Jan 07 |
mbayer |
59 |
import net.sf.basedb.core.SimpleProgressReporter; |
226 |
09 Jan 07 |
mbayer |
60 |
import net.sf.basedb.core.StringParameterType; |
239 |
20 Feb 07 |
mbayer |
61 |
import net.sf.basedb.core.SystemItems; |
226 |
09 Jan 07 |
mbayer |
62 |
import net.sf.basedb.core.User; |
226 |
09 Jan 07 |
mbayer |
63 |
import net.sf.basedb.core.plugin.Response; |
226 |
09 Jan 07 |
mbayer |
64 |
import net.sf.basedb.core.query.Expressions; |
226 |
09 Jan 07 |
mbayer |
65 |
import net.sf.basedb.core.query.Hql; |
226 |
09 Jan 07 |
mbayer |
66 |
import net.sf.basedb.core.query.Restrictions; |
226 |
09 Jan 07 |
mbayer |
67 |
import net.sf.basedb.util.zip.FileUnpacker; |
226 |
09 Jan 07 |
mbayer |
68 |
|
226 |
09 Jan 07 |
mbayer |
69 |
/** |
239 |
20 Feb 07 |
mbayer |
* Imports raw data from a batch of flat files into BASE2. The data files must |
239 |
20 Feb 07 |
mbayer |
* be packaged as a zip file and conform to the same data format. It is assumed |
239 |
20 Feb 07 |
mbayer |
* that this code is executed after an Experiment object has been created |
239 |
20 Feb 07 |
mbayer |
* already and that it is of the same data type as the data that are being |
239 |
20 Feb 07 |
mbayer |
* imported. Currently only supports the creation of RawBioAssay objects as part |
239 |
20 Feb 07 |
mbayer |
* of the import but could be extended to create other objects as needed. |
239 |
20 Feb 07 |
mbayer |
76 |
* |
239 |
20 Feb 07 |
mbayer |
* @author Micha Bayer - Plant Bioinformatics Group, Scottish Crop Research |
239 |
20 Feb 07 |
mbayer |
* Institute email: sbrn@scri.ac.uk, web http://www.scri.ac.uk |
226 |
09 Jan 07 |
mbayer |
79 |
*/ |
226 |
09 Jan 07 |
mbayer |
80 |
public class BatchDataImport |
226 |
09 Jan 07 |
mbayer |
81 |
{ |
239 |
20 Feb 07 |
mbayer |
// ======================================vars============================= |
226 |
09 Jan 07 |
mbayer |
83 |
|
239 |
20 Feb 07 |
mbayer |
// BASE client infrastructure |
239 |
20 Feb 07 |
mbayer |
85 |
private DbControl dc = null; |
239 |
20 Feb 07 |
mbayer |
86 |
|
226 |
09 Jan 07 |
mbayer |
87 |
private SessionControl sc = null; |
226 |
09 Jan 07 |
mbayer |
88 |
|
239 |
20 Feb 07 |
mbayer |
// the directory the files for import are located in |
226 |
09 Jan 07 |
mbayer |
90 |
private Directory dir = null; |
226 |
09 Jan 07 |
mbayer |
91 |
|
239 |
20 Feb 07 |
mbayer |
// the name of the zip file minus the extension |
226 |
09 Jan 07 |
mbayer |
93 |
private String fileName = null; |
226 |
09 Jan 07 |
mbayer |
94 |
|
239 |
20 Feb 07 |
mbayer |
// the zip file we want to import |
226 |
09 Jan 07 |
mbayer |
96 |
private File zipFile = null; |
226 |
09 Jan 07 |
mbayer |
97 |
|
239 |
20 Feb 07 |
mbayer |
// the Experiment that this import will be attached to |
226 |
09 Jan 07 |
mbayer |
99 |
private Experiment experiment = null; |
231 |
14 Feb 07 |
mbayer |
100 |
|
239 |
20 Feb 07 |
mbayer |
// the progress reporter for the overall batch import |
226 |
09 Jan 07 |
mbayer |
102 |
private ProgressReporter progress = null; |
231 |
14 Feb 07 |
mbayer |
103 |
|
239 |
20 Feb 07 |
mbayer |
// the percentage complete of the currently executing file import |
239 |
20 Feb 07 |
mbayer |
// (subjob) |
226 |
09 Jan 07 |
mbayer |
106 |
private double currentJobPercentComplete = 0; |
231 |
14 Feb 07 |
mbayer |
107 |
|
239 |
20 Feb 07 |
mbayer |
// the currently executing file import job |
226 |
09 Jan 07 |
mbayer |
109 |
private Job currentJob = null; |
226 |
09 Jan 07 |
mbayer |
110 |
|
239 |
20 Feb 07 |
mbayer |
// the cdf file for the array desing for Affy data |
231 |
14 Feb 07 |
mbayer |
112 |
private File cdfFile = null; |
231 |
14 Feb 07 |
mbayer |
113 |
|
239 |
20 Feb 07 |
mbayer |
// ================================c'tor================================= |
226 |
09 Jan 07 |
mbayer |
115 |
|
239 |
20 Feb 07 |
mbayer |
116 |
public BatchDataImport(SessionControl sc, File zipFile, Experiment experiment, ProgressReporter progress) |
226 |
09 Jan 07 |
mbayer |
117 |
{ |
226 |
09 Jan 07 |
mbayer |
118 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
119 |
this.sc = sc; |
226 |
09 Jan 07 |
mbayer |
120 |
this.zipFile = zipFile; |
226 |
09 Jan 07 |
mbayer |
121 |
this.experiment = experiment; |
226 |
09 Jan 07 |
mbayer |
122 |
this.progress = progress; |
226 |
09 Jan 07 |
mbayer |
123 |
} |
226 |
09 Jan 07 |
mbayer |
124 |
|
239 |
20 Feb 07 |
mbayer |
// ================================methods================================== |
226 |
09 Jan 07 |
mbayer |
126 |
|
226 |
09 Jan 07 |
mbayer |
127 |
/** |
239 |
20 Feb 07 |
mbayer |
* Unpacks the zip file in a new subdirectory of the directory where the zip file is located. |
239 |
20 Feb 07 |
mbayer |
* The new dir is named after the name of the zip file (minus the extension). |
226 |
09 Jan 07 |
mbayer |
130 |
*/ |
231 |
14 Feb 07 |
mbayer |
131 |
public boolean unpackZipFile() throws Exception |
226 |
09 Jan 07 |
mbayer |
132 |
{ |
239 |
20 Feb 07 |
mbayer |
133 |
|
226 |
09 Jan 07 |
mbayer |
134 |
int numUnpackedFiles = 0; |
239 |
20 Feb 07 |
mbayer |
135 |
boolean unpackSuccess = false; |
226 |
09 Jan 07 |
mbayer |
136 |
|
226 |
09 Jan 07 |
mbayer |
137 |
try |
226 |
09 Jan 07 |
mbayer |
138 |
{ |
239 |
20 Feb 07 |
mbayer |
// figure out where the zip file is located |
239 |
20 Feb 07 |
mbayer |
140 |
Directory zipFileDir = zipFile.getDirectory(); |
239 |
20 Feb 07 |
mbayer |
141 |
System.out.println("zipFile is located in dir " + zipFileDir.toString()); |
226 |
09 Jan 07 |
mbayer |
142 |
|
239 |
20 Feb 07 |
mbayer |
// make a new subdir in that dir |
239 |
20 Feb 07 |
mbayer |
// the zip file should then be unzipped in here |
239 |
20 Feb 07 |
mbayer |
// give it the same name as that of the zip file plus a |
239 |
20 Feb 07 |
mbayer |
// timestamp |
231 |
14 Feb 07 |
mbayer |
147 |
String timeStamp = getTime("ddMMyy_HHmmss"); |
239 |
20 Feb 07 |
mbayer |
148 |
Directory subDir = Directory.getNew(dc, zipFileDir); |
226 |
09 Jan 07 |
mbayer |
149 |
fileName = zipFile.getName(); |
239 |
20 Feb 07 |
mbayer |
150 |
String fileNameNoExt = fileName.substring(0, fileName.indexOf(".")); |
239 |
20 Feb 07 |
mbayer |
151 |
System.out.println("new subDir = " + fileNameNoExt + timeStamp); |
239 |
20 Feb 07 |
mbayer |
152 |
subDir.setName(fileNameNoExt + timeStamp); |
226 |
09 Jan 07 |
mbayer |
153 |
|
239 |
20 Feb 07 |
mbayer |
// save the new subDir |
239 |
20 Feb 07 |
mbayer |
155 |
dc.saveItem(subDir); |
226 |
09 Jan 07 |
mbayer |
156 |
dir = subDir; |
239 |
20 Feb 07 |
mbayer |
157 |
|
239 |
20 Feb 07 |
mbayer |
//check the size of the zip file content |
239 |
20 Feb 07 |
mbayer |
//if unzipping this file we must not execeed our disk quota |
239 |
20 Feb 07 |
mbayer |
160 |
InputStream inStream = zipFile.getDownloadStream(0); |
239 |
20 Feb 07 |
mbayer |
161 |
long zipFileSize = getZipfileEntriesSize(inStream); |
239 |
20 Feb 07 |
mbayer |
162 |
|
239 |
20 Feb 07 |
mbayer |
//get the user's current disk usage and quota |
239 |
20 Feb 07 |
mbayer |
164 |
int userID = sc.getLoggedInUserId(); |
239 |
20 Feb 07 |
mbayer |
165 |
User user = User.getById(dc, userID); |
239 |
20 Feb 07 |
mbayer |
166 |
Quota quota = user.getQuota(); |
239 |
20 Feb 07 |
mbayer |
167 |
QuotaType totalQuotaType = QuotaType.getById(dc, SystemItems.getId(QuotaType.TOTAL)); |
239 |
20 Feb 07 |
mbayer |
//unlimited storage (e.g. for user root) will return -1 here so need to check for this further down |
239 |
20 Feb 07 |
mbayer |
169 |
long quotaValue = quota.getQuotaValue(totalQuotaType, Location.PRIMARY); |
239 |
20 Feb 07 |
mbayer |
170 |
long currentDiskUsage = user.getDiskUsage(totalQuotaType, Location.PRIMARY); |
239 |
20 Feb 07 |
mbayer |
171 |
|
239 |
20 Feb 07 |
mbayer |
//check whether remaining disk space and zip file size are compatible |
239 |
20 Feb 07 |
mbayer |
173 |
long remainingSpace = quotaValue - currentDiskUsage; |
239 |
20 Feb 07 |
mbayer |
174 |
System.out.println("quotaValue for user " + user.toString() + " = " + quotaValue); |
239 |
20 Feb 07 |
mbayer |
175 |
System.out.println("currentDiskUsage = " + currentDiskUsage); |
239 |
20 Feb 07 |
mbayer |
176 |
System.out.println("remainingSpace = " + remainingSpace); |
239 |
20 Feb 07 |
mbayer |
177 |
System.out.println("zipFileSize = " + zipFileSize); |
239 |
20 Feb 07 |
mbayer |
178 |
if(remainingSpace < zipFileSize && quotaValue!=-1L) |
239 |
20 Feb 07 |
mbayer |
179 |
{ |
239 |
20 Feb 07 |
mbayer |
180 |
throw new Exception("insufficient disk space remaining for unzipping of file -- please contact your BASE administrator or remove some files"); |
239 |
20 Feb 07 |
mbayer |
181 |
} |
226 |
09 Jan 07 |
mbayer |
182 |
|
239 |
20 Feb 07 |
mbayer |
// now unzip the zip file into the new subdir |
226 |
09 Jan 07 |
mbayer |
184 |
if (zipFile != null) |
239 |
20 Feb 07 |
mbayer |
185 |
{ |
226 |
09 Jan 07 |
mbayer |
186 |
PluginDefinition plugin = PluginDefinition.getByClassName(dc, "net.sf.basedb.plugins.ZipFileUnpacker"); |
239 |
20 Feb 07 |
mbayer |
187 |
FileUnpacker unpacker = plugin.newInstance(net.sf.basedb.plugins.ZipFileUnpacker.class, null, sc, null, null); |
226 |
09 Jan 07 |
mbayer |
188 |
InputStream uploadStream = zipFile.getDownloadStream(0); |
226 |
09 Jan 07 |
mbayer |
189 |
if (unpacker != null) |
226 |
09 Jan 07 |
mbayer |
190 |
{ |
226 |
09 Jan 07 |
mbayer |
191 |
System.out.println("unpacking"); |
226 |
09 Jan 07 |
mbayer |
192 |
numUnpackedFiles = unpacker.unpack(dc, subDir, uploadStream, false, null); |
239 |
20 Feb 07 |
mbayer |
193 |
} |
226 |
09 Jan 07 |
mbayer |
194 |
uploadStream.close(); |
226 |
09 Jan 07 |
mbayer |
195 |
} |
226 |
09 Jan 07 |
mbayer |
196 |
|
239 |
20 Feb 07 |
mbayer |
// commit the changes |
226 |
09 Jan 07 |
mbayer |
198 |
dc.commit(); |
226 |
09 Jan 07 |
mbayer |
199 |
|
239 |
20 Feb 07 |
mbayer |
// can't check for all the files so we have to assume |
239 |
20 Feb 07 |
mbayer |
// that if any files were unpacked ok, they all worked |
239 |
20 Feb 07 |
mbayer |
202 |
if (numUnpackedFiles > 0) |
226 |
09 Jan 07 |
mbayer |
203 |
{ |
226 |
09 Jan 07 |
mbayer |
204 |
unpackSuccess = true; |
226 |
09 Jan 07 |
mbayer |
205 |
} |
226 |
09 Jan 07 |
mbayer |
206 |
System.out.println("numUnpackedFiles = " + numUnpackedFiles); |
226 |
09 Jan 07 |
mbayer |
207 |
} |
226 |
09 Jan 07 |
mbayer |
208 |
catch (Exception e) |
226 |
09 Jan 07 |
mbayer |
209 |
{ |
226 |
09 Jan 07 |
mbayer |
210 |
e.printStackTrace(); |
239 |
20 Feb 07 |
mbayer |
211 |
throw new Exception("zip file unpacking failed: " + e.getMessage()); |
226 |
09 Jan 07 |
mbayer |
212 |
} |
239 |
20 Feb 07 |
mbayer |
213 |
finally |
239 |
20 Feb 07 |
mbayer |
214 |
{ |
239 |
20 Feb 07 |
mbayer |
215 |
if (dc != null) |
239 |
20 Feb 07 |
mbayer |
216 |
dc.close(); |
239 |
20 Feb 07 |
mbayer |
217 |
} |
226 |
09 Jan 07 |
mbayer |
218 |
return unpackSuccess; |
226 |
09 Jan 07 |
mbayer |
219 |
} |
226 |
09 Jan 07 |
mbayer |
220 |
|
239 |
20 Feb 07 |
mbayer |
221 |
// ----------------------------------------------------------------------------------------------------------------------------------------------- |
226 |
09 Jan 07 |
mbayer |
222 |
|
226 |
09 Jan 07 |
mbayer |
223 |
/** |
239 |
20 Feb 07 |
mbayer |
* Imports the data from a batch of text files into new RawBioAssay |
239 |
20 Feb 07 |
mbayer |
* objects (one per file), then creates a new experiment with all of |
239 |
20 Feb 07 |
mbayer |
* these RBAs attached. |
239 |
20 Feb 07 |
mbayer |
227 |
* |
239 |
20 Feb 07 |
mbayer |
* @return boolean success -- true if everything worked as defined |
226 |
09 Jan 07 |
mbayer |
229 |
*/ |
226 |
09 Jan 07 |
mbayer |
230 |
public boolean importData() throws Exception |
226 |
09 Jan 07 |
mbayer |
231 |
{ |
226 |
09 Jan 07 |
mbayer |
232 |
boolean importSuccess = false; |
226 |
09 Jan 07 |
mbayer |
233 |
boolean unpackSuccess = unpackZipFile(); |
226 |
09 Jan 07 |
mbayer |
234 |
|
226 |
09 Jan 07 |
mbayer |
235 |
System.out.println("BatchDataImport importData"); |
226 |
09 Jan 07 |
mbayer |
236 |
System.out.println("dir = " + dir.getName()); |
226 |
09 Jan 07 |
mbayer |
237 |
|
239 |
20 Feb 07 |
mbayer |
// get the raw data type from the experiment |
239 |
20 Feb 07 |
mbayer |
239 |
RawDataType rawDataType = experiment.getRawDataType(); |
231 |
14 Feb 07 |
mbayer |
240 |
System.out.println("rawDataType = " + rawDataType.getName()); |
231 |
14 Feb 07 |
mbayer |
241 |
|
226 |
09 Jan 07 |
mbayer |
242 |
ItemQuery<File> fileQuery = null; |
239 |
20 Feb 07 |
mbayer |
243 |
ItemResultList<File> fileList = null; |
226 |
09 Jan 07 |
mbayer |
244 |
|
239 |
20 Feb 07 |
mbayer |
245 |
if (dc == null || !dc.isConnected()) |
239 |
20 Feb 07 |
mbayer |
246 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
247 |
|
226 |
09 Jan 07 |
mbayer |
248 |
try |
239 |
20 Feb 07 |
mbayer |
249 |
{ |
239 |
20 Feb 07 |
mbayer |
// list all files in dir -- this should be what we |
239 |
20 Feb 07 |
mbayer |
// unzipped from the zip file |
226 |
09 Jan 07 |
mbayer |
252 |
fileQuery = dir.getFiles(); |
239 |
20 Feb 07 |
mbayer |
253 |
fileList = fileQuery.list(dc); |
231 |
14 Feb 07 |
mbayer |
254 |
|
239 |
20 Feb 07 |
mbayer |
255 |
File[] dataFiles = null; |
239 |
20 Feb 07 |
mbayer |
256 |
if (fileQuery != null) |
226 |
09 Jan 07 |
mbayer |
257 |
{ |
239 |
20 Feb 07 |
mbayer |
// for Affy data the zip file uploaded should |
239 |
20 Feb 07 |
mbayer |
// contain the cdf file for the chip and all the |
239 |
20 Feb 07 |
mbayer |
// cel files |
239 |
20 Feb 07 |
mbayer |
// need to separate these out |
231 |
14 Feb 07 |
mbayer |
262 |
if (rawDataType.getName().equalsIgnoreCase("affymetrix")) |
231 |
14 Feb 07 |
mbayer |
263 |
{ |
231 |
14 Feb 07 |
mbayer |
264 |
|
239 |
20 Feb 07 |
mbayer |
// iterate over all the files and check |
239 |
20 Feb 07 |
mbayer |
// their extensions |
239 |
20 Feb 07 |
mbayer |
267 |
int dataFileCount = 0; |
239 |
20 Feb 07 |
mbayer |
// initiate a shorter array for the data |
239 |
20 Feb 07 |
mbayer |
// files since this should not include |
239 |
20 Feb 07 |
mbayer |
// the cdf file |
239 |
20 Feb 07 |
mbayer |
271 |
dataFiles = new File[fileList.size() - 1]; |
231 |
14 Feb 07 |
mbayer |
272 |
System.out.println("instantiating dataFiles array of length " + dataFiles.length); |
239 |
20 Feb 07 |
mbayer |
273 |
|
231 |
14 Feb 07 |
mbayer |
274 |
System.out.println("affy data -- iterating over file names"); |
231 |
14 Feb 07 |
mbayer |
275 |
for (int i = 0; i < fileList.size(); i++) |
231 |
14 Feb 07 |
mbayer |
276 |
{ |
239 |
20 Feb 07 |
mbayer |
// get the current file and |
239 |
20 Feb 07 |
mbayer |
// check its extension |
231 |
14 Feb 07 |
mbayer |
279 |
File file = fileList.get(i); |
231 |
14 Feb 07 |
mbayer |
280 |
String fileName = file.getName(); |
239 |
20 Feb 07 |
mbayer |
281 |
String extension = fileName.substring(fileName.lastIndexOf(".") + 1); |
239 |
20 Feb 07 |
mbayer |
282 |
System.out.println("file name = " + fileName + ", extension = " + extension); |
231 |
14 Feb 07 |
mbayer |
283 |
|
239 |
20 Feb 07 |
mbayer |
// if it's the cdf file, point |
239 |
20 Feb 07 |
mbayer |
// the global var for this at it |
239 |
20 Feb 07 |
mbayer |
286 |
if (extension.equalsIgnoreCase("cdf")) |
231 |
14 Feb 07 |
mbayer |
287 |
{ |
231 |
14 Feb 07 |
mbayer |
288 |
System.out.println("cdf file found"); |
231 |
14 Feb 07 |
mbayer |
289 |
cdfFile = file; |
239 |
20 Feb 07 |
mbayer |
290 |
System.out.println("cdf file = " + cdfFile); |
231 |
14 Feb 07 |
mbayer |
291 |
} |
239 |
20 Feb 07 |
mbayer |
// if it's a cel file, add it to |
239 |
20 Feb 07 |
mbayer |
// the dataFiles array |
239 |
20 Feb 07 |
mbayer |
294 |
else if (extension.equalsIgnoreCase("cel")) |
231 |
14 Feb 07 |
mbayer |
295 |
{ |
239 |
20 Feb 07 |
mbayer |
296 |
System.out.println("setting dataFiles[" + dataFileCount + "] to file " + file.getName()); |
239 |
20 Feb 07 |
mbayer |
297 |
dataFiles[dataFileCount] = file; |
239 |
20 Feb 07 |
mbayer |
298 |
dataFileCount++; |
231 |
14 Feb 07 |
mbayer |
299 |
} |
239 |
20 Feb 07 |
mbayer |
// if it is neither a cel file |
239 |
20 Feb 07 |
mbayer |
// nor a cdf file then the user |
239 |
20 Feb 07 |
mbayer |
// must have defined the |
239 |
20 Feb 07 |
mbayer |
// Experiment as |
239 |
20 Feb 07 |
mbayer |
// being Affy but the data they |
239 |
20 Feb 07 |
mbayer |
// are importing is not Affy |
239 |
20 Feb 07 |
mbayer |
// in that case we need to throw |
239 |
20 Feb 07 |
mbayer |
// an Exception and abort the |
239 |
20 Feb 07 |
mbayer |
// whole import |
234 |
14 Feb 07 |
mbayer |
309 |
else |
234 |
14 Feb 07 |
mbayer |
310 |
{ |
234 |
14 Feb 07 |
mbayer |
311 |
throw new Exception("data file is not Affymetrix cel file but Experiment was defined as Affymetrix data type"); |
234 |
14 Feb 07 |
mbayer |
312 |
} |
231 |
14 Feb 07 |
mbayer |
313 |
} |
226 |
09 Jan 07 |
mbayer |
314 |
} |
239 |
20 Feb 07 |
mbayer |
315 |
else |
239 |
20 Feb 07 |
mbayer |
// non-Affy data types |
231 |
14 Feb 07 |
mbayer |
317 |
{ |
239 |
20 Feb 07 |
mbayer |
// in this case all the files in the zip |
239 |
20 Feb 07 |
mbayer |
// file should be data files |
239 |
20 Feb 07 |
mbayer |
320 |
dataFiles = new File[fileList.size()]; |
231 |
14 Feb 07 |
mbayer |
321 |
System.out.println("instantiating dataFiles array of length " + dataFiles.length); |
231 |
14 Feb 07 |
mbayer |
322 |
|
239 |
20 Feb 07 |
mbayer |
// put the files from the list into the |
239 |
20 Feb 07 |
mbayer |
// array for homogeneity's sake |
231 |
14 Feb 07 |
mbayer |
325 |
fileList.toArray(dataFiles); |
231 |
14 Feb 07 |
mbayer |
326 |
} |
231 |
14 Feb 07 |
mbayer |
327 |
|
239 |
20 Feb 07 |
mbayer |
// list all the data files |
239 |
20 Feb 07 |
mbayer |
329 |
System.out.println("data files: "); |
239 |
20 Feb 07 |
mbayer |
330 |
for (int i = 0; i < dataFiles.length; i++) |
239 |
20 Feb 07 |
mbayer |
331 |
{ |
231 |
14 Feb 07 |
mbayer |
332 |
System.out.println(dataFiles[i].getName()); |
231 |
14 Feb 07 |
mbayer |
333 |
} |
226 |
09 Jan 07 |
mbayer |
334 |
} |
226 |
09 Jan 07 |
mbayer |
335 |
|
239 |
20 Feb 07 |
mbayer |
// earmark this one file for autodetecting the file |
239 |
20 Feb 07 |
mbayer |
// format |
239 |
20 Feb 07 |
mbayer |
// all files must have same format |
226 |
09 Jan 07 |
mbayer |
339 |
File exampleFile = fileList.get(0); |
226 |
09 Jan 07 |
mbayer |
340 |
System.out.println("exampleFile = " + exampleFile.getName()); |
239 |
20 Feb 07 |
mbayer |
341 |
|
239 |
20 Feb 07 |
mbayer |
// check we don't have a discrepancy between the raw |
239 |
20 Feb 07 |
mbayer |
// data type and the data files here |
239 |
20 Feb 07 |
mbayer |
// if users have specified a non-affy data type and the |
239 |
20 Feb 07 |
mbayer |
// data is affy we need to abort the import with an |
239 |
20 Feb 07 |
mbayer |
// appropriate error |
239 |
20 Feb 07 |
mbayer |
// get the current file and check its extension |
234 |
14 Feb 07 |
mbayer |
348 |
String exampleFileName = exampleFile.getName(); |
239 |
20 Feb 07 |
mbayer |
349 |
String exampleFileExtension = exampleFileName.substring(exampleFileName.lastIndexOf(".") + 1); |
239 |
20 Feb 07 |
mbayer |
350 |
if ((exampleFileExtension.equalsIgnoreCase("cdf") || exampleFileExtension.equalsIgnoreCase("cel")) && !rawDataType.getName().equalsIgnoreCase("affymetrix")) |
234 |
14 Feb 07 |
mbayer |
351 |
{ |
234 |
14 Feb 07 |
mbayer |
352 |
throw new Exception("Affymetrix data found but Experiment data type was specified as non-Affy"); |
234 |
14 Feb 07 |
mbayer |
353 |
} |
226 |
09 Jan 07 |
mbayer |
354 |
|
239 |
20 Feb 07 |
mbayer |
// for non-Affy data types, find a suitable plugin |
239 |
20 Feb 07 |
mbayer |
// configuration |
231 |
14 Feb 07 |
mbayer |
357 |
PluginConfiguration importConfig = null; |
231 |
14 Feb 07 |
mbayer |
358 |
if (!rawDataType.getName().equalsIgnoreCase("affymetrix")) |
231 |
14 Feb 07 |
mbayer |
359 |
{ |
239 |
20 Feb 07 |
mbayer |
// get the appropriate plugin config |
231 |
14 Feb 07 |
mbayer |
361 |
PluginConfigDetector detector = new PluginConfigDetector(sc); |
239 |
20 Feb 07 |
mbayer |
362 |
importConfig = detector.detectPluginConfig(exampleFile); |
231 |
14 Feb 07 |
mbayer |
363 |
System.out.println("PluginConfiguration importConfig = " + importConfig); |
239 |
20 Feb 07 |
mbayer |
364 |
|
239 |
20 Feb 07 |
mbayer |
// now need to figure out the raw data type to |
239 |
20 Feb 07 |
mbayer |
// make sure it matches that of the Experiment |
239 |
20 Feb 07 |
mbayer |
// we are importing into |
239 |
20 Feb 07 |
mbayer |
// this can be done by looking at the plugin |
239 |
20 Feb 07 |
mbayer |
// config which should have it stored in it |
234 |
14 Feb 07 |
mbayer |
370 |
List rdtVal = importConfig.getParameterValues("rawDataType"); |
239 |
20 Feb 07 |
mbayer |
371 |
String rawDataTypeStr = (String) rdtVal.get(0); |
239 |
20 Feb 07 |
mbayer |
372 |
System.out.println("rawDataType detected from files = " + rawDataTypeStr); |
239 |
20 Feb 07 |
mbayer |
// compare this to the raw data type of the |
239 |
20 Feb 07 |
mbayer |
// experiment |
239 |
20 Feb 07 |
mbayer |
375 |
if (!rawDataType.getName().equalsIgnoreCase(rawDataTypeStr)) |
234 |
14 Feb 07 |
mbayer |
376 |
{ |
234 |
14 Feb 07 |
mbayer |
377 |
throw new Exception("raw data types of experiment and data files do not match"); |
234 |
14 Feb 07 |
mbayer |
378 |
} |
239 |
20 Feb 07 |
mbayer |
379 |
|
231 |
14 Feb 07 |
mbayer |
380 |
} |
226 |
09 Jan 07 |
mbayer |
381 |
|
231 |
14 Feb 07 |
mbayer |
// for each data file |
239 |
20 Feb 07 |
mbayer |
// create a new RawBioAssay |
239 |
20 Feb 07 |
mbayer |
384 |
RawBioAssay[] rawBioAssays = new RawBioAssay[dataFiles.length]; |
231 |
14 Feb 07 |
mbayer |
385 |
System.out.println("dataFiles.length = " + dataFiles.length); |
231 |
14 Feb 07 |
mbayer |
386 |
for (int i = 0; i < dataFiles.length; i++) |
226 |
09 Jan 07 |
mbayer |
387 |
{ |
239 |
20 Feb 07 |
mbayer |
388 |
if (dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
389 |
dc = sc.newDbControl(); |
231 |
14 Feb 07 |
mbayer |
390 |
System.out.println("current data file is " + dataFiles[i].getName()); |
239 |
20 Feb 07 |
mbayer |
391 |
RawBioAssay rba = createRawBioAssay(dc, "raw bioassay " + dataFiles[i].getName(), rawDataType); |
231 |
14 Feb 07 |
mbayer |
392 |
rawBioAssays[i] = rba; |
239 |
20 Feb 07 |
mbayer |
393 |
} |
226 |
09 Jan 07 |
mbayer |
394 |
|
239 |
20 Feb 07 |
mbayer |
// write all this to the database |
226 |
09 Jan 07 |
mbayer |
396 |
dc.commit(); |
226 |
09 Jan 07 |
mbayer |
397 |
|
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
399 |
if (dc.isClosed()) |
239 |
20 Feb 07 |
mbayer |
400 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
401 |
|
239 |
20 Feb 07 |
mbayer |
// monitor the job progress |
239 |
20 Feb 07 |
mbayer |
403 |
ProgressMonitorThread pm = new ProgressMonitorThread(progress, rawBioAssays.length, this); |
226 |
09 Jan 07 |
mbayer |
404 |
pm.start(); |
231 |
14 Feb 07 |
mbayer |
405 |
|
239 |
20 Feb 07 |
mbayer |
// import the data for each RawBioAssay |
239 |
20 Feb 07 |
mbayer |
// only do this for non-Affy data types |
231 |
14 Feb 07 |
mbayer |
408 |
if (!rawDataType.getName().equalsIgnoreCase("affymetrix")) |
226 |
09 Jan 07 |
mbayer |
409 |
{ |
239 |
20 Feb 07 |
mbayer |
410 |
for (int i = 0; i < rawBioAssays.length; i++) |
231 |
14 Feb 07 |
mbayer |
411 |
{ |
231 |
14 Feb 07 |
mbayer |
412 |
SimpleProgressReporter pr = new SimpleProgressReporter(null); |
231 |
14 Feb 07 |
mbayer |
413 |
pm.setPr(pr); |
231 |
14 Feb 07 |
mbayer |
414 |
importRawData(importConfig, dataFiles[i], rawBioAssays[i], pr); |
231 |
14 Feb 07 |
mbayer |
415 |
pm.setCount(i); |
231 |
14 Feb 07 |
mbayer |
416 |
} |
239 |
20 Feb 07 |
mbayer |
417 |
} |
239 |
20 Feb 07 |
mbayer |
418 |
else |
239 |
20 Feb 07 |
mbayer |
// Affy data |
231 |
14 Feb 07 |
mbayer |
420 |
{ |
231 |
14 Feb 07 |
mbayer |
421 |
System.out.println("Affymetrix experiment -- attaching cel files to raw bioassays"); |
231 |
14 Feb 07 |
mbayer |
422 |
|
239 |
20 Feb 07 |
mbayer |
// make a new ArrayDesign for this affy chip |
231 |
14 Feb 07 |
mbayer |
424 |
ArrayDesign arrayDesign = ArrayDesign.getNew(dc, true); |
231 |
14 Feb 07 |
mbayer |
425 |
arrayDesign.setName(cdfFile.getName()); |
231 |
14 Feb 07 |
mbayer |
426 |
dc.saveItem(arrayDesign); |
231 |
14 Feb 07 |
mbayer |
427 |
dc.commit(); |
239 |
20 Feb 07 |
mbayer |
// ArrayDesign arrayDesign = |
239 |
20 Feb 07 |
mbayer |
// ArrayDesign.getById(dc, 7); |
231 |
14 Feb 07 |
mbayer |
430 |
System.out.println("using arrayDesign object " + arrayDesign.getName()); |
231 |
14 Feb 07 |
mbayer |
431 |
|
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
433 |
if (dc.isClosed()) |
239 |
20 Feb 07 |
mbayer |
434 |
dc = sc.newDbControl(); |
239 |
20 Feb 07 |
mbayer |
435 |
|
239 |
20 Feb 07 |
mbayer |
// check the cdf file exists -- this should have |
239 |
20 Feb 07 |
mbayer |
// been included in the zip file |
239 |
20 Feb 07 |
mbayer |
// else fail the whole import |
239 |
20 Feb 07 |
mbayer |
439 |
if (cdfFile == null) |
231 |
14 Feb 07 |
mbayer |
440 |
{ |
231 |
14 Feb 07 |
mbayer |
441 |
throw new Exception("no cdf file found for Affy data -- check that the zip file included this"); |
231 |
14 Feb 07 |
mbayer |
442 |
} |
231 |
14 Feb 07 |
mbayer |
443 |
|
239 |
20 Feb 07 |
mbayer |
// now set the cdf file on the array design |
231 |
14 Feb 07 |
mbayer |
445 |
System.out.println("setting cdf file on the array design object"); |
231 |
14 Feb 07 |
mbayer |
446 |
dc.reattachItem(arrayDesign); |
231 |
14 Feb 07 |
mbayer |
447 |
dc.reattachItem(cdfFile); |
231 |
14 Feb 07 |
mbayer |
448 |
Affymetrix.setCdfFile(arrayDesign, cdfFile); |
231 |
14 Feb 07 |
mbayer |
449 |
|
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
451 |
if (dc.isClosed()) |
239 |
20 Feb 07 |
mbayer |
452 |
dc = sc.newDbControl(); |
239 |
20 Feb 07 |
mbayer |
453 |
|
231 |
14 Feb 07 |
mbayer |
454 |
System.out.println("adding cel files to raw bioassays"); |
239 |
20 Feb 07 |
mbayer |
455 |
for (int i = 0; i < rawBioAssays.length; i++) |
239 |
20 Feb 07 |
mbayer |
456 |
{ |
231 |
14 Feb 07 |
mbayer |
457 |
RawBioAssay rba = RawBioAssay.getById(dc, rawBioAssays[i].getId()); |
239 |
20 Feb 07 |
mbayer |
458 |
|
231 |
14 Feb 07 |
mbayer |
459 |
System.out.println("iteration " + i); |
231 |
14 Feb 07 |
mbayer |
460 |
System.out.println("rba = " + rba.getName()); |
231 |
14 Feb 07 |
mbayer |
461 |
System.out.println("file = " + dataFiles[i].getName()); |
239 |
20 Feb 07 |
mbayer |
462 |
|
239 |
20 Feb 07 |
mbayer |
// must set the array design |
231 |
14 Feb 07 |
mbayer |
464 |
rba.setArrayDesign(arrayDesign); |
231 |
14 Feb 07 |
mbayer |
465 |
|
239 |
20 Feb 07 |
mbayer |
// need to attach the cel file to the |
239 |
20 Feb 07 |
mbayer |
// bioassay object |
239 |
20 Feb 07 |
mbayer |
468 |
Affymetrix.setCelFile(rba, dataFiles[i]); |
231 |
14 Feb 07 |
mbayer |
469 |
} |
226 |
09 Jan 07 |
mbayer |
470 |
} |
239 |
20 Feb 07 |
mbayer |
471 |
|
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
473 |
if (dc.isClosed()) |
239 |
20 Feb 07 |
mbayer |
474 |
dc = sc.newDbControl(); |
239 |
20 Feb 07 |
mbayer |
// commit the changes to the database |
231 |
14 Feb 07 |
mbayer |
476 |
dc.commit(); |
226 |
09 Jan 07 |
mbayer |
477 |
|
239 |
20 Feb 07 |
mbayer |
// now attach all the new RawBioAssay objects to the |
239 |
20 Feb 07 |
mbayer |
// current Experiment item |
239 |
20 Feb 07 |
mbayer |
// this is the Experiment the user will be running the |
239 |
20 Feb 07 |
mbayer |
// import from |
239 |
20 Feb 07 |
mbayer |
482 |
configureExperiment(dc, rawDataType, rawBioAssays); |
231 |
14 Feb 07 |
mbayer |
483 |
|
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
485 |
if (dc.isClosed()) |
239 |
20 Feb 07 |
mbayer |
486 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
487 |
|
239 |
20 Feb 07 |
mbayer |
// check whether the import worked |
239 |
20 Feb 07 |
mbayer |
// if item 0 in the list of RawBioAssay objects can be |
239 |
20 Feb 07 |
mbayer |
// retrieved, we assume it all worked |
226 |
09 Jan 07 |
mbayer |
491 |
ItemQuery<RawBioAssay> query = RawBioAssay.getQuery(); |
231 |
14 Feb 07 |
mbayer |
492 |
query.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(rawBioAssays[0].getName()))); |
226 |
09 Jan 07 |
mbayer |
493 |
RawBioAssay testRba = query.list(dc).get(0); |
239 |
20 Feb 07 |
mbayer |
494 |
if (testRba != null) |
226 |
09 Jan 07 |
mbayer |
495 |
{ |
226 |
09 Jan 07 |
mbayer |
496 |
importSuccess = true; |
226 |
09 Jan 07 |
mbayer |
497 |
} |
226 |
09 Jan 07 |
mbayer |
498 |
|
226 |
09 Jan 07 |
mbayer |
499 |
} |
226 |
09 Jan 07 |
mbayer |
500 |
catch (Exception e) |
226 |
09 Jan 07 |
mbayer |
501 |
{ |
226 |
09 Jan 07 |
mbayer |
502 |
e.printStackTrace(); |
231 |
14 Feb 07 |
mbayer |
503 |
throw new Exception(e); |
226 |
09 Jan 07 |
mbayer |
504 |
} |
226 |
09 Jan 07 |
mbayer |
505 |
finally |
226 |
09 Jan 07 |
mbayer |
506 |
{ |
239 |
20 Feb 07 |
mbayer |
507 |
if (dc != null) |
226 |
09 Jan 07 |
mbayer |
508 |
dc.close(); |
226 |
09 Jan 07 |
mbayer |
509 |
} |
226 |
09 Jan 07 |
mbayer |
510 |
|
226 |
09 Jan 07 |
mbayer |
511 |
return importSuccess; |
226 |
09 Jan 07 |
mbayer |
512 |
} |
226 |
09 Jan 07 |
mbayer |
513 |
|
226 |
09 Jan 07 |
mbayer |
514 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
226 |
09 Jan 07 |
mbayer |
515 |
|
226 |
09 Jan 07 |
mbayer |
516 |
/** |
239 |
20 Feb 07 |
mbayer |
* Import features. |
226 |
09 Jan 07 |
mbayer |
518 |
*/ |
239 |
20 Feb 07 |
mbayer |
519 |
private void importRawData(PluginConfiguration config, File file, RawBioAssay rba, SimpleProgressReporter pr) throws Exception |
226 |
09 Jan 07 |
mbayer |
520 |
{ |
239 |
20 Feb 07 |
mbayer |
521 |
System.out.println("importing raw data from file: " + file.getName()); |
226 |
09 Jan 07 |
mbayer |
522 |
Job job = null; |
226 |
09 Jan 07 |
mbayer |
523 |
try |
226 |
09 Jan 07 |
mbayer |
524 |
{ |
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
526 |
if (dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
527 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
528 |
|
226 |
09 Jan 07 |
mbayer |
529 |
config = PluginConfiguration.getById(dc, config.getId()); |
226 |
09 Jan 07 |
mbayer |
530 |
job = Job.getNew(dc, config.getPluginDefinition(), config); |
226 |
09 Jan 07 |
mbayer |
531 |
job.setName("Importing data to raw bioassay: " + rba.getName()); |
226 |
09 Jan 07 |
mbayer |
532 |
job.setParameterValue("file", new FileParameterType(), file); |
226 |
09 Jan 07 |
mbayer |
533 |
job.setParameterValue("rawBioAssay", new ItemParameterType<RawBioAssay>(RawBioAssay.class, null), rba); |
226 |
09 Jan 07 |
mbayer |
534 |
job.setParameterValue("missingReporterError", new StringParameterType(), "skip"); |
226 |
09 Jan 07 |
mbayer |
535 |
job.setParameterValue("featureMismatchError", new StringParameterType(), "skip"); |
231 |
14 Feb 07 |
mbayer |
536 |
|
226 |
09 Jan 07 |
mbayer |
537 |
currentJob = job; |
231 |
14 Feb 07 |
mbayer |
538 |
|
226 |
09 Jan 07 |
mbayer |
539 |
dc.saveItem(job); |
226 |
09 Jan 07 |
mbayer |
540 |
dc.commit(); |
239 |
20 Feb 07 |
mbayer |
541 |
executeJob(job, pr); |
226 |
09 Jan 07 |
mbayer |
542 |
} |
239 |
20 Feb 07 |
mbayer |
543 |
catch (Exception x) |
226 |
09 Jan 07 |
mbayer |
544 |
{ |
226 |
09 Jan 07 |
mbayer |
545 |
x.printStackTrace(); |
226 |
09 Jan 07 |
mbayer |
546 |
} |
226 |
09 Jan 07 |
mbayer |
547 |
finally |
226 |
09 Jan 07 |
mbayer |
548 |
{ |
239 |
20 Feb 07 |
mbayer |
549 |
if (!dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
550 |
dc.close(); |
226 |
09 Jan 07 |
mbayer |
551 |
} |
226 |
09 Jan 07 |
mbayer |
552 |
} |
226 |
09 Jan 07 |
mbayer |
553 |
|
226 |
09 Jan 07 |
mbayer |
554 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
226 |
09 Jan 07 |
mbayer |
555 |
|
226 |
09 Jan 07 |
mbayer |
556 |
/** |
239 |
20 Feb 07 |
mbayer |
* Execute a job. |
239 |
20 Feb 07 |
mbayer |
558 |
* |
239 |
20 Feb 07 |
mbayer |
* @param job |
226 |
09 Jan 07 |
mbayer |
560 |
*/ |
239 |
20 Feb 07 |
mbayer |
561 |
private void executeJob(Job job, SimpleProgressReporter pr) |
226 |
09 Jan 07 |
mbayer |
562 |
{ |
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
564 |
if (dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
565 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
566 |
|
226 |
09 Jan 07 |
mbayer |
567 |
String localhost = ""; |
226 |
09 Jan 07 |
mbayer |
568 |
try |
226 |
09 Jan 07 |
mbayer |
569 |
{ |
239 |
20 Feb 07 |
mbayer |
570 |
localhost = InetAddress.getLocalHost().toString(); |
226 |
09 Jan 07 |
mbayer |
571 |
} |
226 |
09 Jan 07 |
mbayer |
572 |
catch (UnknownHostException e) |
226 |
09 Jan 07 |
mbayer |
573 |
{ |
226 |
09 Jan 07 |
mbayer |
574 |
e.printStackTrace(); |
226 |
09 Jan 07 |
mbayer |
575 |
} |
226 |
09 Jan 07 |
mbayer |
576 |
|
239 |
20 Feb 07 |
mbayer |
577 |
System.out.println("Executing job: " + job.getName()); |
226 |
09 Jan 07 |
mbayer |
578 |
job = Job.getById(dc, job.getId()); |
231 |
14 Feb 07 |
mbayer |
579 |
|
226 |
09 Jan 07 |
mbayer |
580 |
PluginExecutionRequest request = job.execute(pr, localhost); |
226 |
09 Jan 07 |
mbayer |
581 |
dc.commit(); |
226 |
09 Jan 07 |
mbayer |
582 |
|
239 |
20 Feb 07 |
mbayer |
// execute the job request |
226 |
09 Jan 07 |
mbayer |
584 |
PluginResponse response = request.invoke(); |
231 |
14 Feb 07 |
mbayer |
585 |
|
239 |
20 Feb 07 |
mbayer |
// check the job ended sucessfully |
226 |
09 Jan 07 |
mbayer |
587 |
if (response.getStatus() == Response.Status.ERROR) |
226 |
09 Jan 07 |
mbayer |
588 |
{ |
226 |
09 Jan 07 |
mbayer |
589 |
throw new BaseException(response.getMessage(), response.getErrorList().get(0)); |
226 |
09 Jan 07 |
mbayer |
590 |
} |
239 |
20 Feb 07 |
mbayer |
591 |
System.out.println("Job ended successfully: " + response.getMessage()); |
226 |
09 Jan 07 |
mbayer |
592 |
|
239 |
20 Feb 07 |
mbayer |
593 |
if (!dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
594 |
dc.close(); |
226 |
09 Jan 07 |
mbayer |
595 |
} |
226 |
09 Jan 07 |
mbayer |
596 |
|
226 |
09 Jan 07 |
mbayer |
597 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
226 |
09 Jan 07 |
mbayer |
598 |
|
226 |
09 Jan 07 |
mbayer |
599 |
/** |
239 |
20 Feb 07 |
mbayer |
* Adds the raw bioassays to our experiment. |
226 |
09 Jan 07 |
mbayer |
601 |
*/ |
239 |
20 Feb 07 |
mbayer |
602 |
private void configureExperiment(DbControl dc, RawDataType rawDataType, RawBioAssay[] rawBioAssays) throws Exception |
226 |
09 Jan 07 |
mbayer |
603 |
{ |
226 |
09 Jan 07 |
mbayer |
604 |
try |
226 |
09 Jan 07 |
mbayer |
605 |
{ |
239 |
20 Feb 07 |
mbayer |
// make sure we have a working dbcontrol object |
239 |
20 Feb 07 |
mbayer |
607 |
if (dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
608 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
609 |
|
226 |
09 Jan 07 |
mbayer |
610 |
System.out.println("Configuring experiment " + experiment); |
226 |
09 Jan 07 |
mbayer |
611 |
|
239 |
20 Feb 07 |
mbayer |
// make sure the raw data type is set to the correct |
239 |
20 Feb 07 |
mbayer |
// value |
239 |
20 Feb 07 |
mbayer |
// rather than relying on the one specified by the user, |
239 |
20 Feb 07 |
mbayer |
// we want to use the one that we have |
239 |
20 Feb 07 |
mbayer |
// actually detected by looking at the data to be |
239 |
20 Feb 07 |
mbayer |
// imported (see above) |
239 |
20 Feb 07 |
mbayer |
// check what the user has set is correct -- if not, we |
239 |
20 Feb 07 |
mbayer |
// need to fail the whole run |
226 |
09 Jan 07 |
mbayer |
620 |
RawDataType actualRdt = experiment.getRawDataType(); |
226 |
09 Jan 07 |
mbayer |
621 |
if (!actualRdt.getName().equals(rawDataType.getName())) |
226 |
09 Jan 07 |
mbayer |
622 |
{ |
239 |
20 Feb 07 |
mbayer |
623 |
throw new BaseException("Import failed -- the raw data type of the selected experiment and that of the " + "data to be imported don't match. Please change your data type to " + rawDataType.getName() + " and try again."); |
226 |
09 Jan 07 |
mbayer |
624 |
} |
226 |
09 Jan 07 |
mbayer |
625 |
|
239 |
20 Feb 07 |
mbayer |
// now attach the raw bioassays |
239 |
20 Feb 07 |
mbayer |
// tried to do this directly on the Experiment object |
239 |
20 Feb 07 |
mbayer |
// passed into this class -- that fails |
239 |
20 Feb 07 |
mbayer |
// the following is a workaround -- just make a fresh |
239 |
20 Feb 07 |
mbayer |
// Experiment object from the ID of the one passed in |
226 |
09 Jan 07 |
mbayer |
631 |
int expID = experiment.getId(); |
226 |
09 Jan 07 |
mbayer |
632 |
Experiment exp = Experiment.getById(dc, expID); |
231 |
14 Feb 07 |
mbayer |
633 |
|
226 |
09 Jan 07 |
mbayer |
634 |
for (RawBioAssay rba : rawBioAssays) |
226 |
09 Jan 07 |
mbayer |
635 |
{ |
226 |
09 Jan 07 |
mbayer |
636 |
rba = RawBioAssay.getById(dc, rba.getId()); |
226 |
09 Jan 07 |
mbayer |
637 |
System.out.println("adding raw bioassay to experiment : " + rba); |
226 |
09 Jan 07 |
mbayer |
638 |
exp.addRawBioAssay(rba); |
226 |
09 Jan 07 |
mbayer |
639 |
} |
226 |
09 Jan 07 |
mbayer |
640 |
|
239 |
20 Feb 07 |
mbayer |
// housekeeping |
226 |
09 Jan 07 |
mbayer |
642 |
dc.commit(); |
239 |
20 Feb 07 |
mbayer |
643 |
if (!dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
644 |
dc.close(); |
226 |
09 Jan 07 |
mbayer |
645 |
} |
226 |
09 Jan 07 |
mbayer |
646 |
catch (Exception e) |
226 |
09 Jan 07 |
mbayer |
647 |
{ |
226 |
09 Jan 07 |
mbayer |
648 |
e.printStackTrace(); |
226 |
09 Jan 07 |
mbayer |
649 |
throw new Exception("configuration of existing experiment failed"); |
226 |
09 Jan 07 |
mbayer |
650 |
} |
226 |
09 Jan 07 |
mbayer |
651 |
} |
226 |
09 Jan 07 |
mbayer |
652 |
|
226 |
09 Jan 07 |
mbayer |
653 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
226 |
09 Jan 07 |
mbayer |
654 |
|
226 |
09 Jan 07 |
mbayer |
655 |
/** |
239 |
20 Feb 07 |
mbayer |
* Create a raw bioassay. |
226 |
09 Jan 07 |
mbayer |
657 |
*/ |
239 |
20 Feb 07 |
mbayer |
658 |
private RawBioAssay createRawBioAssay(DbControl dc, String name, RawDataType rawDataType) |
226 |
09 Jan 07 |
mbayer |
659 |
{ |
239 |
20 Feb 07 |
mbayer |
660 |
if (dc.isClosed()) |
226 |
09 Jan 07 |
mbayer |
661 |
dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
662 |
|
239 |
20 Feb 07 |
mbayer |
663 |
System.out.println("Creating raw bioassay: " + name); |
226 |
09 Jan 07 |
mbayer |
664 |
RawBioAssay rba = RawBioAssay.getNew(dc, rawDataType); |
226 |
09 Jan 07 |
mbayer |
665 |
rba.setName(name); |
226 |
09 Jan 07 |
mbayer |
666 |
dc.saveItem(rba); |
226 |
09 Jan 07 |
mbayer |
667 |
return rba; |
226 |
09 Jan 07 |
mbayer |
668 |
} |
226 |
09 Jan 07 |
mbayer |
669 |
|
239 |
20 Feb 07 |
mbayer |
670 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
231 |
14 Feb 07 |
mbayer |
671 |
|
231 |
14 Feb 07 |
mbayer |
672 |
private String getTime(String pattern) |
231 |
14 Feb 07 |
mbayer |
673 |
{ |
231 |
14 Feb 07 |
mbayer |
674 |
SimpleDateFormat formatter; |
231 |
14 Feb 07 |
mbayer |
675 |
Locale currentLocale = Locale.getDefault(); |
231 |
14 Feb 07 |
mbayer |
676 |
formatter = new SimpleDateFormat(pattern, currentLocale); |
239 |
20 Feb 07 |
mbayer |
677 |
String timeStamp = formatter.format(new Date()); |
231 |
14 Feb 07 |
mbayer |
678 |
return timeStamp; |
231 |
14 Feb 07 |
mbayer |
679 |
} |
231 |
14 Feb 07 |
mbayer |
680 |
|
239 |
20 Feb 07 |
mbayer |
681 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
231 |
14 Feb 07 |
mbayer |
682 |
|
226 |
09 Jan 07 |
mbayer |
683 |
public Job getCurrentJob() |
226 |
09 Jan 07 |
mbayer |
684 |
{ |
226 |
09 Jan 07 |
mbayer |
685 |
return currentJob; |
226 |
09 Jan 07 |
mbayer |
686 |
} |
226 |
09 Jan 07 |
mbayer |
687 |
|
239 |
20 Feb 07 |
mbayer |
688 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
239 |
20 Feb 07 |
mbayer |
689 |
|
239 |
20 Feb 07 |
mbayer |
690 |
/** |
239 |
20 Feb 07 |
mbayer |
* Checks the total size of a given zip file (uncompressed) |
239 |
20 Feb 07 |
mbayer |
* @param InputStream fileInputStream -- an input stream from the zip file |
239 |
20 Feb 07 |
mbayer |
* @return long - the total size of the zip file uncompressed, in bytes |
239 |
20 Feb 07 |
mbayer |
694 |
* |
239 |
20 Feb 07 |
mbayer |
695 |
*/ |
239 |
20 Feb 07 |
mbayer |
696 |
public static long getZipfileEntriesSize(InputStream fileInputStream) |
239 |
20 Feb 07 |
mbayer |
697 |
{ |
239 |
20 Feb 07 |
mbayer |
698 |
long totalSize =0; |
239 |
20 Feb 07 |
mbayer |
699 |
try |
239 |
20 Feb 07 |
mbayer |
700 |
{ |
239 |
20 Feb 07 |
mbayer |
701 |
ZipInputStream zipStream = new ZipInputStream(fileInputStream); |
239 |
20 Feb 07 |
mbayer |
702 |
ZipEntry entry = zipStream.getNextEntry(); |
239 |
20 Feb 07 |
mbayer |
703 |
while(entry!=null) |
239 |
20 Feb 07 |
mbayer |
704 |
{ |
239 |
20 Feb 07 |
mbayer |
705 |
long size = entry.getSize(); |
239 |
20 Feb 07 |
mbayer |
706 |
totalSize += size; |
239 |
20 Feb 07 |
mbayer |
707 |
entry = zipStream.getNextEntry(); |
239 |
20 Feb 07 |
mbayer |
708 |
} |
239 |
20 Feb 07 |
mbayer |
709 |
} |
239 |
20 Feb 07 |
mbayer |
710 |
catch (IOException ioe) |
239 |
20 Feb 07 |
mbayer |
711 |
{ |
239 |
20 Feb 07 |
mbayer |
712 |
ioe.printStackTrace(); |
239 |
20 Feb 07 |
mbayer |
713 |
} |
239 |
20 Feb 07 |
mbayer |
714 |
return totalSize; |
239 |
20 Feb 07 |
mbayer |
715 |
} |
239 |
20 Feb 07 |
mbayer |
716 |
|
239 |
20 Feb 07 |
mbayer |
717 |
// ------------------------------------------------------------------------------------------------------------------------------------------- |
226 |
09 Jan 07 |
mbayer |
718 |
|
226 |
09 Jan 07 |
mbayer |
719 |
}// end class |