1433 |
22 Mar 07 |
fredrik |
1 |
/* |
1433 |
22 Mar 07 |
fredrik |
$Id$ |
1433 |
22 Mar 07 |
fredrik |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Fredrik Levander, Gregory Vincic |
1433 |
22 Mar 07 |
fredrik |
5 |
|
1433 |
22 Mar 07 |
fredrik |
Files are copyright by their respective authors. The contributions to |
1433 |
22 Mar 07 |
fredrik |
files where copyright is not explicitly stated can be traced with the |
1433 |
22 Mar 07 |
fredrik |
source code revision system. |
1433 |
22 Mar 07 |
fredrik |
9 |
|
1433 |
22 Mar 07 |
fredrik |
This file is part of Proteios. |
1433 |
22 Mar 07 |
fredrik |
Available at http://www.proteios.org/ |
1433 |
22 Mar 07 |
fredrik |
12 |
|
1433 |
22 Mar 07 |
fredrik |
Proteios-2.x is free software; you can redistribute it and/or |
1433 |
22 Mar 07 |
fredrik |
modify it under the terms of the GNU General Public License |
1433 |
22 Mar 07 |
fredrik |
as published by the Free Software Foundation; either version 2 |
1433 |
22 Mar 07 |
fredrik |
of the License, or (at your option) any later version. |
1433 |
22 Mar 07 |
fredrik |
17 |
|
1433 |
22 Mar 07 |
fredrik |
Proteios is distributed in the hope that it will be useful, |
1433 |
22 Mar 07 |
fredrik |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1433 |
22 Mar 07 |
fredrik |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1433 |
22 Mar 07 |
fredrik |
GNU General Public License for more details. |
1433 |
22 Mar 07 |
fredrik |
22 |
|
1433 |
22 Mar 07 |
fredrik |
You should have received a copy of the GNU General Public License |
1433 |
22 Mar 07 |
fredrik |
along with this program; if not, write to the Free Software |
1433 |
22 Mar 07 |
fredrik |
Foundation, Inc., 59 Temple Place - Suite 330, |
1433 |
22 Mar 07 |
fredrik |
Boston, MA 02111-1307, USA. |
1433 |
22 Mar 07 |
fredrik |
27 |
*/ |
1433 |
22 Mar 07 |
fredrik |
28 |
package org.proteios.plugins; |
1433 |
22 Mar 07 |
fredrik |
29 |
|
1656 |
22 May 07 |
gregory |
30 |
import org.proteios.core.Annotator; |
1433 |
22 Mar 07 |
fredrik |
31 |
import org.proteios.core.BaseException; |
1433 |
22 Mar 07 |
fredrik |
32 |
import org.proteios.core.DataProcessingStep; |
1433 |
22 Mar 07 |
fredrik |
33 |
import org.proteios.core.DbControl; |
1433 |
22 Mar 07 |
fredrik |
34 |
import org.proteios.core.File; |
1433 |
22 Mar 07 |
fredrik |
35 |
import org.proteios.core.FileType; |
1433 |
22 Mar 07 |
fredrik |
36 |
import org.proteios.core.Hardware; |
1433 |
22 Mar 07 |
fredrik |
37 |
import org.proteios.core.HardwareType; |
1523 |
27 Apr 07 |
gregory |
38 |
import org.proteios.core.ItemFactory; |
1433 |
22 Mar 07 |
fredrik |
39 |
import org.proteios.core.ItemQuery; |
1433 |
22 Mar 07 |
fredrik |
40 |
import org.proteios.core.ItemResultIterator; |
1433 |
22 Mar 07 |
fredrik |
41 |
import org.proteios.core.Peak; |
1433 |
22 Mar 07 |
fredrik |
42 |
import org.proteios.core.PeakList; |
1433 |
22 Mar 07 |
fredrik |
43 |
import org.proteios.core.PeakListSet; |
1433 |
22 Mar 07 |
fredrik |
44 |
import org.proteios.core.ProgressReporter; |
1433 |
22 Mar 07 |
fredrik |
45 |
import org.proteios.core.SessionControl; |
1433 |
22 Mar 07 |
fredrik |
46 |
import org.proteios.core.Software; |
1433 |
22 Mar 07 |
fredrik |
47 |
import org.proteios.core.SoftwareType; |
1433 |
22 Mar 07 |
fredrik |
48 |
import org.proteios.core.SystemItems; |
1433 |
22 Mar 07 |
fredrik |
49 |
import org.proteios.core.Type; |
1433 |
22 Mar 07 |
fredrik |
50 |
import org.proteios.core.User; |
1433 |
22 Mar 07 |
fredrik |
51 |
import org.proteios.core.plugin.About; |
1433 |
22 Mar 07 |
fredrik |
52 |
import org.proteios.core.plugin.AboutImpl; |
1433 |
22 Mar 07 |
fredrik |
53 |
import org.proteios.core.plugin.AbstractPlugin; |
1433 |
22 Mar 07 |
fredrik |
54 |
import org.proteios.core.plugin.ParameterValues; |
1433 |
22 Mar 07 |
fredrik |
55 |
import org.proteios.core.plugin.Plugin; |
1433 |
22 Mar 07 |
fredrik |
56 |
import org.proteios.core.plugin.Request; |
1433 |
22 Mar 07 |
fredrik |
57 |
import org.proteios.core.plugin.Response; |
1433 |
22 Mar 07 |
fredrik |
58 |
import org.proteios.core.query.Expressions; |
1433 |
22 Mar 07 |
fredrik |
59 |
import org.proteios.core.query.Hql; |
1433 |
22 Mar 07 |
fredrik |
60 |
import org.proteios.core.query.Restrictions; |
1433 |
22 Mar 07 |
fredrik |
61 |
import org.proteios.io.FileTypeInterface; |
1433 |
22 Mar 07 |
fredrik |
62 |
import org.proteios.io.FileValidationInterface; |
3272 |
12 May 09 |
fredrik |
63 |
import org.proteios.io.PiumsPepfilReader; |
1433 |
22 Mar 07 |
fredrik |
64 |
import org.xml.sax.Attributes; |
1433 |
22 Mar 07 |
fredrik |
65 |
import org.xml.sax.SAXException; |
1433 |
22 Mar 07 |
fredrik |
66 |
import org.xml.sax.helpers.DefaultHandler; |
3272 |
12 May 09 |
fredrik |
67 |
|
1433 |
22 Mar 07 |
fredrik |
68 |
import java.io.IOException; |
1433 |
22 Mar 07 |
fredrik |
69 |
import java.io.InputStream; |
1433 |
22 Mar 07 |
fredrik |
70 |
import java.text.DateFormat; |
1433 |
22 Mar 07 |
fredrik |
71 |
import java.text.ParsePosition; |
1433 |
22 Mar 07 |
fredrik |
72 |
import java.text.SimpleDateFormat; |
1433 |
22 Mar 07 |
fredrik |
73 |
import java.util.Arrays; |
1433 |
22 Mar 07 |
fredrik |
74 |
import java.util.Date; |
3272 |
12 May 09 |
fredrik |
75 |
|
1433 |
22 Mar 07 |
fredrik |
76 |
import javax.xml.parsers.ParserConfigurationException; |
1433 |
22 Mar 07 |
fredrik |
77 |
import javax.xml.parsers.SAXParser; |
1433 |
22 Mar 07 |
fredrik |
78 |
import javax.xml.parsers.SAXParserFactory; |
3272 |
12 May 09 |
fredrik |
79 |
import javax.xml.stream.XMLStreamException; |
1433 |
22 Mar 07 |
fredrik |
80 |
|
1433 |
22 Mar 07 |
fredrik |
81 |
/** |
1433 |
22 Mar 07 |
fredrik |
* This import plugin imports a PiumsPepfil file. If a Hardware is given as a |
1433 |
22 Mar 07 |
fredrik |
* ParameterValue it will be added as the hardware type. |
1433 |
22 Mar 07 |
fredrik |
84 |
* |
1433 |
22 Mar 07 |
fredrik |
* @author fredrik |
1433 |
22 Mar 07 |
fredrik |
86 |
*/ |
1433 |
22 Mar 07 |
fredrik |
87 |
public class PiumsPepfilImportPlugin |
1433 |
22 Mar 07 |
fredrik |
88 |
extends AbstractPlugin |
1433 |
22 Mar 07 |
fredrik |
89 |
implements FileTypeInterface, FileValidationInterface |
1433 |
22 Mar 07 |
fredrik |
90 |
{ |
1433 |
22 Mar 07 |
fredrik |
91 |
static float MONOISOTOPIC_PROTON_MASS = (float) 1.00782; |
1433 |
22 Mar 07 |
fredrik |
92 |
private File resultsFile; |
1433 |
22 Mar 07 |
fredrik |
93 |
/** |
1433 |
22 Mar 07 |
fredrik |
* The hardware configuration to use for the import |
1433 |
22 Mar 07 |
fredrik |
95 |
*/ |
1433 |
22 Mar 07 |
fredrik |
96 |
private Hardware hardware; |
1433 |
22 Mar 07 |
fredrik |
97 |
/** |
1433 |
22 Mar 07 |
fredrik |
* logger to use |
1433 |
22 Mar 07 |
fredrik |
99 |
*/ |
1433 |
22 Mar 07 |
fredrik |
100 |
private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager |
1433 |
22 Mar 07 |
fredrik |
101 |
.getLogger("org.proteios.io"); |
1433 |
22 Mar 07 |
fredrik |
102 |
|
1433 |
22 Mar 07 |
fredrik |
103 |
|
1433 |
22 Mar 07 |
fredrik |
104 |
public PiumsPepfilImportPlugin() |
1433 |
22 Mar 07 |
fredrik |
105 |
{ |
1433 |
22 Mar 07 |
fredrik |
106 |
super(); |
1433 |
22 Mar 07 |
fredrik |
107 |
} |
1433 |
22 Mar 07 |
fredrik |
108 |
|
1433 |
22 Mar 07 |
fredrik |
109 |
|
1433 |
22 Mar 07 |
fredrik |
110 |
/** |
1433 |
22 Mar 07 |
fredrik |
* Constructor to use if FileValidationInterface is to used |
1433 |
22 Mar 07 |
fredrik |
112 |
* |
1433 |
22 Mar 07 |
fredrik |
* @param piumsFile The file to import |
1433 |
22 Mar 07 |
fredrik |
* @param sc The session control |
1433 |
22 Mar 07 |
fredrik |
115 |
*/ |
1433 |
22 Mar 07 |
fredrik |
116 |
public PiumsPepfilImportPlugin(File piumsFile, SessionControl sc) |
1433 |
22 Mar 07 |
fredrik |
117 |
{ |
1433 |
22 Mar 07 |
fredrik |
118 |
super(); |
1433 |
22 Mar 07 |
fredrik |
119 |
resultsFile = piumsFile; |
1433 |
22 Mar 07 |
fredrik |
120 |
this.sc = sc; |
1433 |
22 Mar 07 |
fredrik |
121 |
} |
1433 |
22 Mar 07 |
fredrik |
122 |
|
1433 |
22 Mar 07 |
fredrik |
123 |
|
1433 |
22 Mar 07 |
fredrik |
124 |
public MainType getMainType() |
1433 |
22 Mar 07 |
fredrik |
125 |
{ |
1433 |
22 Mar 07 |
fredrik |
126 |
return Plugin.MainType.IMPORT; |
1433 |
22 Mar 07 |
fredrik |
127 |
} |
1433 |
22 Mar 07 |
fredrik |
128 |
|
1433 |
22 Mar 07 |
fredrik |
129 |
|
1433 |
22 Mar 07 |
fredrik |
130 |
public String getFileType() |
1433 |
22 Mar 07 |
fredrik |
131 |
{ |
1433 |
22 Mar 07 |
fredrik |
132 |
return FileType.PIUMS_PEPFIL; |
1433 |
22 Mar 07 |
fredrik |
133 |
} |
1433 |
22 Mar 07 |
fredrik |
134 |
|
1433 |
22 Mar 07 |
fredrik |
135 |
|
1433 |
22 Mar 07 |
fredrik |
136 |
public About getAbout() |
1433 |
22 Mar 07 |
fredrik |
137 |
{ |
1433 |
22 Mar 07 |
fredrik |
138 |
return new AboutImpl("Piums Pepfil importer", |
1433 |
22 Mar 07 |
fredrik |
139 |
"PeakListSet import from Piums Pepfil", "1.0", |
1433 |
22 Mar 07 |
fredrik |
140 |
"2006, Protein Technology, Lund University", null, null, |
1433 |
22 Mar 07 |
fredrik |
141 |
"http://www.proteios.org "); |
1433 |
22 Mar 07 |
fredrik |
142 |
} |
1433 |
22 Mar 07 |
fredrik |
143 |
|
1433 |
22 Mar 07 |
fredrik |
144 |
|
2386 |
14 Nov 07 |
gregory |
145 |
@Override |
1433 |
22 Mar 07 |
fredrik |
146 |
public void init(SessionControl sc, ParameterValues configuration, |
1433 |
22 Mar 07 |
fredrik |
147 |
ParameterValues job) |
1433 |
22 Mar 07 |
fredrik |
148 |
throws BaseException |
1433 |
22 Mar 07 |
fredrik |
149 |
{ |
1433 |
22 Mar 07 |
fredrik |
150 |
super.init(sc, configuration, job); |
1433 |
22 Mar 07 |
fredrik |
151 |
} |
1433 |
22 Mar 07 |
fredrik |
152 |
|
1433 |
22 Mar 07 |
fredrik |
153 |
|
1433 |
22 Mar 07 |
fredrik |
154 |
public void run(Request request, Response response, |
1433 |
22 Mar 07 |
fredrik |
155 |
ProgressReporter progress) |
1433 |
22 Mar 07 |
fredrik |
156 |
{ |
1433 |
22 Mar 07 |
fredrik |
157 |
String command = request.getCommand(); |
1433 |
22 Mar 07 |
fredrik |
158 |
if (command.equals(Request.COMMAND_EXECUTE)) |
1433 |
22 Mar 07 |
fredrik |
159 |
{ |
1433 |
22 Mar 07 |
fredrik |
160 |
InputStream in = null; |
1433 |
22 Mar 07 |
fredrik |
161 |
try |
1433 |
22 Mar 07 |
fredrik |
162 |
{ |
1433 |
22 Mar 07 |
fredrik |
163 |
hardware = (Hardware) job.getValue("Hardware"); |
1433 |
22 Mar 07 |
fredrik |
164 |
File f = (File) job.getValue("file"); |
1433 |
22 Mar 07 |
fredrik |
165 |
resultsFile = f; |
1433 |
22 Mar 07 |
fredrik |
166 |
in = f.getDownloadStream(0); |
1433 |
22 Mar 07 |
fredrik |
167 |
if (valid(in)) |
1433 |
22 Mar 07 |
fredrik |
168 |
{ |
1433 |
22 Mar 07 |
fredrik |
169 |
in.close(); |
1433 |
22 Mar 07 |
fredrik |
170 |
in = f.getDownloadStream(0); |
1433 |
22 Mar 07 |
fredrik |
171 |
doImport(in, progress); |
1433 |
22 Mar 07 |
fredrik |
172 |
in.close(); |
1433 |
22 Mar 07 |
fredrik |
173 |
} |
1433 |
22 Mar 07 |
fredrik |
174 |
else |
1433 |
22 Mar 07 |
fredrik |
175 |
{ |
1433 |
22 Mar 07 |
fredrik |
176 |
throw new BaseException("Not valid PiumsPepfil file"); |
1433 |
22 Mar 07 |
fredrik |
177 |
} |
1433 |
22 Mar 07 |
fredrik |
178 |
response.setDone(getSuccessMessage()); |
1433 |
22 Mar 07 |
fredrik |
179 |
} |
1433 |
22 Mar 07 |
fredrik |
180 |
catch (Throwable ex) |
1433 |
22 Mar 07 |
fredrik |
181 |
{ |
1433 |
22 Mar 07 |
fredrik |
182 |
if (in != null) |
1433 |
22 Mar 07 |
fredrik |
183 |
{ |
1433 |
22 Mar 07 |
fredrik |
184 |
try |
1433 |
22 Mar 07 |
fredrik |
185 |
{ |
1433 |
22 Mar 07 |
fredrik |
186 |
in.close(); |
1433 |
22 Mar 07 |
fredrik |
187 |
} |
1433 |
22 Mar 07 |
fredrik |
188 |
catch (IOException iox) |
1433 |
22 Mar 07 |
fredrik |
189 |
{} |
1433 |
22 Mar 07 |
fredrik |
190 |
} |
1433 |
22 Mar 07 |
fredrik |
191 |
response.setError(ex.getMessage(), Arrays.asList(ex)); |
1433 |
22 Mar 07 |
fredrik |
192 |
} |
1433 |
22 Mar 07 |
fredrik |
193 |
} |
1433 |
22 Mar 07 |
fredrik |
194 |
else |
1433 |
22 Mar 07 |
fredrik |
195 |
{ |
1433 |
22 Mar 07 |
fredrik |
196 |
response.setError("Unknown command: " + command, null); |
1433 |
22 Mar 07 |
fredrik |
197 |
} |
1433 |
22 Mar 07 |
fredrik |
198 |
} |
1433 |
22 Mar 07 |
fredrik |
199 |
|
1433 |
22 Mar 07 |
fredrik |
200 |
|
1433 |
22 Mar 07 |
fredrik |
201 |
/** |
1433 |
22 Mar 07 |
fredrik |
* Called if the parsing was successful to let the subclass generate a |
1433 |
22 Mar 07 |
fredrik |
* simple message that is sent back to the core and user interface. The |
1433 |
22 Mar 07 |
fredrik |
* default implementation always return null. |
1433 |
22 Mar 07 |
fredrik |
205 |
* |
1433 |
22 Mar 07 |
fredrik |
* @return String with success message. |
1433 |
22 Mar 07 |
fredrik |
207 |
*/ |
1433 |
22 Mar 07 |
fredrik |
208 |
protected String getSuccessMessage() |
1433 |
22 Mar 07 |
fredrik |
209 |
{ |
1433 |
22 Mar 07 |
fredrik |
210 |
return "File imported"; |
1433 |
22 Mar 07 |
fredrik |
211 |
} |
1433 |
22 Mar 07 |
fredrik |
212 |
|
1433 |
22 Mar 07 |
fredrik |
213 |
|
1433 |
22 Mar 07 |
fredrik |
214 |
/** |
1433 |
22 Mar 07 |
fredrik |
* Read a Piums Pepfil file and add to database. |
1433 |
22 Mar 07 |
fredrik |
216 |
* |
1433 |
22 Mar 07 |
fredrik |
* @param instream Input stream to read from. |
1433 |
22 Mar 07 |
fredrik |
* @param progress Progress reporter to report progress to |
1433 |
22 Mar 07 |
fredrik |
219 |
*/ |
1433 |
22 Mar 07 |
fredrik |
220 |
public void doImport(InputStream instream, ProgressReporter progress) |
1433 |
22 Mar 07 |
fredrik |
221 |
throws BaseException |
1433 |
22 Mar 07 |
fredrik |
222 |
{ |
1433 |
22 Mar 07 |
fredrik |
// get a factory |
1433 |
22 Mar 07 |
fredrik |
224 |
SAXParserFactory spf = SAXParserFactory.newInstance(); |
1433 |
22 Mar 07 |
fredrik |
225 |
try |
1433 |
22 Mar 07 |
fredrik |
226 |
{ |
1433 |
22 Mar 07 |
fredrik |
// get a new instance of parser |
1433 |
22 Mar 07 |
fredrik |
228 |
SAXParser sp = spf.newSAXParser(); |
1433 |
22 Mar 07 |
fredrik |
229 |
PiumsPepfilHandler pmh = new PiumsPepfilHandler(); |
1433 |
22 Mar 07 |
fredrik |
230 |
pmh.progress = progress; |
1433 |
22 Mar 07 |
fredrik |
// parse the file and also register this class for call backs |
1433 |
22 Mar 07 |
fredrik |
232 |
sp.parse(instream, pmh); |
1433 |
22 Mar 07 |
fredrik |
233 |
} |
1433 |
22 Mar 07 |
fredrik |
234 |
catch (SAXException se) |
1433 |
22 Mar 07 |
fredrik |
235 |
{ |
1433 |
22 Mar 07 |
fredrik |
236 |
throw new BaseException(se); |
1433 |
22 Mar 07 |
fredrik |
237 |
} |
1433 |
22 Mar 07 |
fredrik |
238 |
catch (ParserConfigurationException pce) |
1433 |
22 Mar 07 |
fredrik |
239 |
{ |
1433 |
22 Mar 07 |
fredrik |
240 |
throw new BaseException(pce); |
1433 |
22 Mar 07 |
fredrik |
241 |
} |
1433 |
22 Mar 07 |
fredrik |
242 |
catch (IOException ie) |
1433 |
22 Mar 07 |
fredrik |
243 |
{ |
1433 |
22 Mar 07 |
fredrik |
244 |
throw new BaseException(ie); |
1433 |
22 Mar 07 |
fredrik |
245 |
} |
1433 |
22 Mar 07 |
fredrik |
246 |
} |
1433 |
22 Mar 07 |
fredrik |
247 |
|
1433 |
22 Mar 07 |
fredrik |
248 |
|
1433 |
22 Mar 07 |
fredrik |
249 |
/** |
1433 |
22 Mar 07 |
fredrik |
* Validate the entire file. |
1433 |
22 Mar 07 |
fredrik |
251 |
* |
1433 |
22 Mar 07 |
fredrik |
* @param instream input stream to validate. |
1433 |
22 Mar 07 |
fredrik |
* @return true if valid |
1433 |
22 Mar 07 |
fredrik |
254 |
*/ |
1433 |
22 Mar 07 |
fredrik |
255 |
private boolean valid(InputStream instream) |
1433 |
22 Mar 07 |
fredrik |
256 |
throws BaseException |
1433 |
22 Mar 07 |
fredrik |
257 |
{ |
3272 |
12 May 09 |
fredrik |
258 |
boolean retval = false; |
1433 |
22 Mar 07 |
fredrik |
259 |
try |
1433 |
22 Mar 07 |
fredrik |
260 |
{ |
3272 |
12 May 09 |
fredrik |
261 |
PiumsPepfilReader ppr = new PiumsPepfilReader(instream); |
3272 |
12 May 09 |
fredrik |
262 |
retval = ppr.valid(); |
1433 |
22 Mar 07 |
fredrik |
263 |
} |
1433 |
22 Mar 07 |
fredrik |
264 |
catch (SAXException e) |
1433 |
22 Mar 07 |
fredrik |
265 |
{ |
1433 |
22 Mar 07 |
fredrik |
266 |
log.warn(e); |
1433 |
22 Mar 07 |
fredrik |
267 |
throw new BaseException(e); |
1433 |
22 Mar 07 |
fredrik |
268 |
} |
3272 |
12 May 09 |
fredrik |
269 |
catch (XMLStreamException ie) |
1433 |
22 Mar 07 |
fredrik |
270 |
{ |
3272 |
12 May 09 |
fredrik |
271 |
log.warn(ie); |
1433 |
22 Mar 07 |
fredrik |
272 |
throw new BaseException(ie); |
1433 |
22 Mar 07 |
fredrik |
273 |
} |
3272 |
12 May 09 |
fredrik |
274 |
return retval; |
1433 |
22 Mar 07 |
fredrik |
275 |
} |
1433 |
22 Mar 07 |
fredrik |
276 |
|
1433 |
22 Mar 07 |
fredrik |
277 |
|
2386 |
14 Nov 07 |
gregory |
278 |
@Override |
1433 |
22 Mar 07 |
fredrik |
279 |
public void done() |
1433 |
22 Mar 07 |
fredrik |
280 |
{ |
1433 |
22 Mar 07 |
fredrik |
281 |
super.done(); |
1433 |
22 Mar 07 |
fredrik |
282 |
} |
1433 |
22 Mar 07 |
fredrik |
283 |
|
1433 |
22 Mar 07 |
fredrik |
284 |
|
1433 |
22 Mar 07 |
fredrik |
285 |
// ---------------------------------------------------------- |
1433 |
22 Mar 07 |
fredrik |
286 |
/* |
1433 |
22 Mar 07 |
fredrik |
* From the AutoDetectingImporter interface |
1433 |
22 Mar 07 |
fredrik |
288 |
* --------------------------------------------- |
1433 |
22 Mar 07 |
fredrik |
289 |
*/ |
1433 |
22 Mar 07 |
fredrik |
290 |
/* |
1433 |
22 Mar 07 |
fredrik |
* (non-Javadoc) @param in input stream to check for pkl start line @return |
1433 |
22 Mar 07 |
fredrik |
* true if input stream contains pkl start line, else false |
1433 |
22 Mar 07 |
fredrik |
293 |
*/ |
1433 |
22 Mar 07 |
fredrik |
294 |
public final boolean isImportable(InputStream in) |
1433 |
22 Mar 07 |
fredrik |
295 |
{ |
3272 |
12 May 09 |
fredrik |
296 |
PiumsPepfilReader ppr = new PiumsPepfilReader(in); |
3272 |
12 May 09 |
fredrik |
297 |
boolean retval = ppr.importable(); |
3272 |
12 May 09 |
fredrik |
298 |
return retval; |
1433 |
22 Mar 07 |
fredrik |
299 |
} |
1433 |
22 Mar 07 |
fredrik |
300 |
|
1433 |
22 Mar 07 |
fredrik |
301 |
|
1433 |
22 Mar 07 |
fredrik |
302 |
public boolean importable() |
1433 |
22 Mar 07 |
fredrik |
303 |
{ |
1433 |
22 Mar 07 |
fredrik |
304 |
return isImportable(resultsFile.getDownloadStream(0)); |
1433 |
22 Mar 07 |
fredrik |
305 |
} |
1433 |
22 Mar 07 |
fredrik |
306 |
|
1433 |
22 Mar 07 |
fredrik |
307 |
|
1433 |
22 Mar 07 |
fredrik |
308 |
public boolean valid() |
1433 |
22 Mar 07 |
fredrik |
309 |
{ |
2881 |
22 Sep 08 |
fredrik |
310 |
boolean retval = false; |
2881 |
22 Sep 08 |
fredrik |
311 |
try |
2881 |
22 Sep 08 |
fredrik |
312 |
{ |
2881 |
22 Sep 08 |
fredrik |
313 |
retval = valid(resultsFile.getDownloadStream(0)); |
2881 |
22 Sep 08 |
fredrik |
314 |
} |
2881 |
22 Sep 08 |
fredrik |
315 |
catch (BaseException e) |
2881 |
22 Sep 08 |
fredrik |
316 |
{} |
2881 |
22 Sep 08 |
fredrik |
317 |
return retval; |
1433 |
22 Mar 07 |
fredrik |
318 |
} |
1433 |
22 Mar 07 |
fredrik |
319 |
|
1433 |
22 Mar 07 |
fredrik |
320 |
public class PiumsPepfilHandler |
1433 |
22 Mar 07 |
fredrik |
321 |
extends DefaultHandler |
1433 |
22 Mar 07 |
fredrik |
322 |
{ |
1433 |
22 Mar 07 |
fredrik |
323 |
User owner; |
1433 |
22 Mar 07 |
fredrik |
324 |
private DbControl dc; |
1540 |
02 May 07 |
gregory |
325 |
private ItemFactory factory = null; |
1656 |
22 May 07 |
gregory |
326 |
private Annotator anna = null; |
1433 |
22 Mar 07 |
fredrik |
327 |
public ProgressReporter progress; |
1433 |
22 Mar 07 |
fredrik |
328 |
private Peak currentPeak; |
1433 |
22 Mar 07 |
fredrik |
329 |
private PeakList peaklist; |
1433 |
22 Mar 07 |
fredrik |
330 |
private DataProcessingStep dataProcessing; |
1433 |
22 Mar 07 |
fredrik |
331 |
private boolean peak = false; |
1433 |
22 Mar 07 |
fredrik |
332 |
private boolean subtract_hydrogen_mass = true; |
1433 |
22 Mar 07 |
fredrik |
333 |
private boolean peak_list = false; |
1433 |
22 Mar 07 |
fredrik |
334 |
private float total_intensity = 0; |
1523 |
27 Apr 07 |
gregory |
335 |
private float base_peak = 0; |
1433 |
22 Mar 07 |
fredrik |
// This stringbuffer is used to store all the char[]:s generated from |
1433 |
22 Mar 07 |
fredrik |
// characters() |
1433 |
22 Mar 07 |
fredrik |
338 |
private StringBuffer buffer = null; |
1433 |
22 Mar 07 |
fredrik |
339 |
|
1433 |
22 Mar 07 |
fredrik |
340 |
|
1433 |
22 Mar 07 |
fredrik |
// Event Handlers |
2386 |
14 Nov 07 |
gregory |
342 |
@Override |
1433 |
22 Mar 07 |
fredrik |
343 |
public void startElement(String uri, String localName, String qName, |
1433 |
22 Mar 07 |
fredrik |
344 |
Attributes attributes) |
1433 |
22 Mar 07 |
fredrik |
345 |
throws SAXException |
1433 |
22 Mar 07 |
fredrik |
346 |
{ |
1433 |
22 Mar 07 |
fredrik |
347 |
if (qName.equals("pepfil3")) |
1433 |
22 Mar 07 |
fredrik |
348 |
{ |
1433 |
22 Mar 07 |
fredrik |
349 |
dc = sc.newDbControl(); |
1540 |
02 May 07 |
gregory |
350 |
factory = new ItemFactory(dc); |
1656 |
22 May 07 |
gregory |
351 |
anna = new Annotator(factory); |
1433 |
22 Mar 07 |
fredrik |
352 |
owner = User.getById(dc, sc.getLoggedInUserId()); |
1433 |
22 Mar 07 |
fredrik |
353 |
String base_file = resultsFile.getName(); |
1433 |
22 Mar 07 |
fredrik |
354 |
if (base_file.indexOf('.') > 0) |
1433 |
22 Mar 07 |
fredrik |
355 |
{ |
1433 |
22 Mar 07 |
fredrik |
356 |
base_file = base_file.substring(0, base_file |
1433 |
22 Mar 07 |
fredrik |
357 |
.lastIndexOf('.')); |
1433 |
22 Mar 07 |
fredrik |
358 |
} |
1433 |
22 Mar 07 |
fredrik |
359 |
/* |
1433 |
22 Mar 07 |
fredrik |
* New PeakListSet item (mzData item) |
1433 |
22 Mar 07 |
fredrik |
361 |
*/ |
1546 |
02 May 07 |
gregory |
362 |
PeakListSet peakListSet = factory.create(PeakListSet.class); |
1433 |
22 Mar 07 |
fredrik |
363 |
peakListSet.setSourceFile(resultsFile); |
1433 |
22 Mar 07 |
fredrik |
364 |
/* |
1433 |
22 Mar 07 |
fredrik |
* Set PeakListSet name to name of pkl file. |
1433 |
22 Mar 07 |
fredrik |
366 |
*/ |
1433 |
22 Mar 07 |
fredrik |
367 |
peakListSet.setName(base_file); |
1433 |
22 Mar 07 |
fredrik |
368 |
peakListSet.setDescription("Imported Piums Pepfil"); |
1433 |
22 Mar 07 |
fredrik |
369 |
/* |
1433 |
22 Mar 07 |
fredrik |
* Check if hardware is given. Otherwise generate new |
1433 |
22 Mar 07 |
fredrik |
371 |
*/ |
1433 |
22 Mar 07 |
fredrik |
372 |
if (hardware == null) |
1433 |
22 Mar 07 |
fredrik |
373 |
{ |
1433 |
22 Mar 07 |
fredrik |
374 |
log |
1433 |
22 Mar 07 |
fredrik |
375 |
.info("PiumsPepfilImport: No hardware given, trying default MS"); |
1433 |
22 Mar 07 |
fredrik |
// This should be changed to mass spectrometer: |
1656 |
22 May 07 |
gregory |
377 |
HardwareType ht = factory.getById(HardwareType.class, |
1656 |
22 May 07 |
gregory |
378 |
SystemItems.getId(HardwareType.MASS_SPECTROMETER)); |
1656 |
22 May 07 |
gregory |
379 |
hardware = factory.create(Hardware.class); |
1656 |
22 May 07 |
gregory |
380 |
hardware.setHardwareType(ht); |
1433 |
22 Mar 07 |
fredrik |
381 |
hardware.setOwner(owner); |
1433 |
22 Mar 07 |
fredrik |
382 |
hardware.setName("Unknown"); |
1433 |
22 Mar 07 |
fredrik |
383 |
dc.saveItem(hardware); |
1433 |
22 Mar 07 |
fredrik |
384 |
} |
1433 |
22 Mar 07 |
fredrik |
385 |
peakListSet.setInstrument(hardware); |
1656 |
22 May 07 |
gregory |
386 |
anna.addAnnotation(peakListSet, "nameOfFile", Type.STRING, |
1656 |
22 May 07 |
gregory |
387 |
resultsFile.getName()); |
1656 |
22 May 07 |
gregory |
388 |
anna.addAnnotation(peakListSet, "fileType", Type.STRING, ""); |
1433 |
22 Mar 07 |
fredrik |
389 |
dc.saveItem(peakListSet); |
1523 |
27 Apr 07 |
gregory |
390 |
ItemFactory factory = new ItemFactory(dc); |
1523 |
27 Apr 07 |
gregory |
391 |
dataProcessing = factory.create(DataProcessingStep.class); |
1433 |
22 Mar 07 |
fredrik |
392 |
dataProcessing.setOwner(owner); |
1433 |
22 Mar 07 |
fredrik |
393 |
dc.saveItem(dataProcessing); |
1433 |
22 Mar 07 |
fredrik |
394 |
dataProcessing |
1433 |
22 Mar 07 |
fredrik |
395 |
.setDescription("Peak filtering and recalibration"); |
1433 |
22 Mar 07 |
fredrik |
396 |
peakListSet.addDataProcessingStep(dataProcessing); |
1542 |
02 May 07 |
gregory |
397 |
peaklist = factory.create(PeakList.class); |
1433 |
22 Mar 07 |
fredrik |
398 |
peaklist.setSpectrumId(1); |
1433 |
22 Mar 07 |
fredrik |
399 |
peaklist.setMzDoublePrecision(true); |
1433 |
22 Mar 07 |
fredrik |
400 |
peaklist.setPeakListSet(peakListSet); |
1433 |
22 Mar 07 |
fredrik |
401 |
dc.saveItem(peaklist); |
1433 |
22 Mar 07 |
fredrik |
402 |
} |
1433 |
22 Mar 07 |
fredrik |
403 |
else if (qName.equals("peak")) |
1433 |
22 Mar 07 |
fredrik |
404 |
{ |
1433 |
22 Mar 07 |
fredrik |
405 |
peak = true; |
1433 |
22 Mar 07 |
fredrik |
406 |
if (peak_list) |
1433 |
22 Mar 07 |
fredrik |
407 |
{ |
1540 |
02 May 07 |
gregory |
408 |
currentPeak = factory.create(Peak.class); |
1433 |
22 Mar 07 |
fredrik |
409 |
} |
1433 |
22 Mar 07 |
fredrik |
410 |
} |
1433 |
22 Mar 07 |
fredrik |
411 |
else if (qName.equals("peak_list")) |
1433 |
22 Mar 07 |
fredrik |
412 |
{ |
1433 |
22 Mar 07 |
fredrik |
413 |
peak_list = true; |
1433 |
22 Mar 07 |
fredrik |
414 |
} |
1433 |
22 Mar 07 |
fredrik |
415 |
else |
1433 |
22 Mar 07 |
fredrik |
416 |
{ |
1433 |
22 Mar 07 |
fredrik |
417 |
buffer = new StringBuffer(); |
1433 |
22 Mar 07 |
fredrik |
418 |
} |
1433 |
22 Mar 07 |
fredrik |
419 |
} |
1433 |
22 Mar 07 |
fredrik |
420 |
|
1433 |
22 Mar 07 |
fredrik |
421 |
|
2386 |
14 Nov 07 |
gregory |
422 |
@Override |
1433 |
22 Mar 07 |
fredrik |
423 |
public void characters(char[] ch, int start, int length) |
1433 |
22 Mar 07 |
fredrik |
424 |
throws SAXException |
1433 |
22 Mar 07 |
fredrik |
425 |
{ |
1433 |
22 Mar 07 |
fredrik |
426 |
if (buffer != null) |
1433 |
22 Mar 07 |
fredrik |
427 |
{ |
1433 |
22 Mar 07 |
fredrik |
428 |
buffer.append(ch, start, length); |
1433 |
22 Mar 07 |
fredrik |
429 |
} |
1433 |
22 Mar 07 |
fredrik |
430 |
} |
1433 |
22 Mar 07 |
fredrik |
431 |
|
1433 |
22 Mar 07 |
fredrik |
432 |
|
2386 |
14 Nov 07 |
gregory |
433 |
@Override |
1433 |
22 Mar 07 |
fredrik |
434 |
public void endElement(String uri, String localName, String qName) |
1433 |
22 Mar 07 |
fredrik |
435 |
throws SAXException |
1433 |
22 Mar 07 |
fredrik |
436 |
{ |
1433 |
22 Mar 07 |
fredrik |
437 |
if (qName.equals("peak")) |
1433 |
22 Mar 07 |
fredrik |
438 |
{ |
1433 |
22 Mar 07 |
fredrik |
439 |
peak = false; |
1433 |
22 Mar 07 |
fredrik |
440 |
if (peak_list) |
1433 |
22 Mar 07 |
fredrik |
441 |
{ |
1433 |
22 Mar 07 |
fredrik |
442 |
dc.saveItem(currentPeak); |
1433 |
22 Mar 07 |
fredrik |
443 |
peaklist.addPeak(currentPeak); |
1433 |
22 Mar 07 |
fredrik |
444 |
log.info("Added peak"); |
1433 |
22 Mar 07 |
fredrik |
445 |
} |
1433 |
22 Mar 07 |
fredrik |
446 |
} |
1433 |
22 Mar 07 |
fredrik |
447 |
else if (qName.equals("version")) |
1433 |
22 Mar 07 |
fredrik |
448 |
{ |
1433 |
22 Mar 07 |
fredrik |
449 |
Software soft = null; |
1433 |
22 Mar 07 |
fredrik |
450 |
log.info("Piums Pepfil import: Querying for software"); |
1433 |
22 Mar 07 |
fredrik |
451 |
ItemQuery<Software> query = Software.getQuery(); |
1433 |
22 Mar 07 |
fredrik |
452 |
query.restrict(Restrictions.eq(Hql.property("name"), |
1433 |
22 Mar 07 |
fredrik |
453 |
Expressions.parameter("name"))); |
1433 |
22 Mar 07 |
fredrik |
454 |
query.setParameter("name", "PIUMS Pepfil", null); |
1433 |
22 Mar 07 |
fredrik |
455 |
ItemResultIterator<Software> iter = query.iterate(dc); |
1433 |
22 Mar 07 |
fredrik |
456 |
while (iter.hasNext()) |
1433 |
22 Mar 07 |
fredrik |
457 |
{ |
1433 |
22 Mar 07 |
fredrik |
458 |
Software curr = iter.next(); |
1433 |
22 Mar 07 |
fredrik |
459 |
if (curr.getVersionString().equals(buffer.toString())) |
1433 |
22 Mar 07 |
fredrik |
460 |
{ |
1433 |
22 Mar 07 |
fredrik |
461 |
soft = curr; |
1433 |
22 Mar 07 |
fredrik |
462 |
} |
1433 |
22 Mar 07 |
fredrik |
463 |
} |
1433 |
22 Mar 07 |
fredrik |
464 |
iter.close(); |
1433 |
22 Mar 07 |
fredrik |
465 |
log.info("Piums Pepfil import: Query finished"); |
1433 |
22 Mar 07 |
fredrik |
466 |
if (soft == null) |
1433 |
22 Mar 07 |
fredrik |
467 |
{ |
1433 |
22 Mar 07 |
fredrik |
468 |
SoftwareType st = SoftwareType.getById(dc, SystemItems |
1433 |
22 Mar 07 |
fredrik |
469 |
.getId(SoftwareType.PROTEIN_IDENTIFICATION)); |
1656 |
22 May 07 |
gregory |
470 |
soft = factory.create(Software.class); |
1656 |
22 May 07 |
gregory |
471 |
soft.setSoftwareType(st); |
1433 |
22 Mar 07 |
fredrik |
472 |
soft.setName("PIUMS Pepfil"); |
1433 |
22 Mar 07 |
fredrik |
473 |
soft.setVersionString(buffer.toString()); |
1433 |
22 Mar 07 |
fredrik |
474 |
dc.saveItem(soft); |
1433 |
22 Mar 07 |
fredrik |
475 |
} |
1433 |
22 Mar 07 |
fredrik |
476 |
dataProcessing.setSoftware(soft); |
1433 |
22 Mar 07 |
fredrik |
477 |
} |
1433 |
22 Mar 07 |
fredrik |
478 |
else if (qName.equals("date_created")) |
1433 |
22 Mar 07 |
fredrik |
479 |
{ |
1433 |
22 Mar 07 |
fredrik |
480 |
String s = new String(buffer.toString()); |
1433 |
22 Mar 07 |
fredrik |
481 |
Date date; |
1433 |
22 Mar 07 |
fredrik |
482 |
DateFormat df_in = new SimpleDateFormat( |
1433 |
22 Mar 07 |
fredrik |
483 |
"HH:mm:ss, dd MMM, yyyy"); |
1433 |
22 Mar 07 |
fredrik |
484 |
date = df_in.parse(s, new ParsePosition(0)); |
1433 |
22 Mar 07 |
fredrik |
485 |
dataProcessing.setCompletionTime(date); |
1433 |
22 Mar 07 |
fredrik |
486 |
} |
1433 |
22 Mar 07 |
fredrik |
487 |
else if (qName.equals("subtract_hydrogen_mass")) |
1433 |
22 Mar 07 |
fredrik |
488 |
{ |
1433 |
22 Mar 07 |
fredrik |
489 |
subtract_hydrogen_mass = Boolean |
1433 |
22 Mar 07 |
fredrik |
490 |
.parseBoolean(buffer.toString()); |
1433 |
22 Mar 07 |
fredrik |
491 |
} |
1433 |
22 Mar 07 |
fredrik |
492 |
else if (peak) |
1433 |
22 Mar 07 |
fredrik |
493 |
{ |
1433 |
22 Mar 07 |
fredrik |
494 |
if (qName.equals("mass")) |
1433 |
22 Mar 07 |
fredrik |
495 |
{ |
1433 |
22 Mar 07 |
fredrik |
496 |
double mz = (Double.parseDouble(buffer.toString())); |
1433 |
22 Mar 07 |
fredrik |
497 |
if (subtract_hydrogen_mass) |
1433 |
22 Mar 07 |
fredrik |
498 |
mz += MONOISOTOPIC_PROTON_MASS; |
1433 |
22 Mar 07 |
fredrik |
499 |
currentPeak.setMassToChargeRatio(new Double(mz)); |
1433 |
22 Mar 07 |
fredrik |
500 |
} |
1433 |
22 Mar 07 |
fredrik |
501 |
else if (qName.equals("intensity")) |
1433 |
22 Mar 07 |
fredrik |
502 |
{ |
1433 |
22 Mar 07 |
fredrik |
503 |
currentPeak.setIntensity(Double.valueOf(buffer.toString())); |
1433 |
22 Mar 07 |
fredrik |
504 |
total_intensity += currentPeak.getIntensity().floatValue(); |
1523 |
27 Apr 07 |
gregory |
505 |
if (currentPeak.getIntensity().floatValue() > base_peak) |
1433 |
22 Mar 07 |
fredrik |
506 |
{ |
1523 |
27 Apr 07 |
gregory |
507 |
base_peak = currentPeak.getIntensity().floatValue(); |
1433 |
22 Mar 07 |
fredrik |
508 |
} |
1433 |
22 Mar 07 |
fredrik |
509 |
} |
1433 |
22 Mar 07 |
fredrik |
510 |
else if (!qName.equals("quality")) |
1433 |
22 Mar 07 |
fredrik |
511 |
{ |
1523 |
27 Apr 07 |
gregory |
// Within matched peaks all other values are floats |
1656 |
22 May 07 |
gregory |
513 |
anna.addAnnotation(currentPeak, qName, Type.FLOAT, Float |
1656 |
22 May 07 |
gregory |
514 |
.valueOf(buffer.toString())); |
1523 |
27 Apr 07 |
gregory |
515 |
log.info("Added peak annotation:" + buffer.toString()); |
1433 |
22 Mar 07 |
fredrik |
516 |
} |
1433 |
22 Mar 07 |
fredrik |
517 |
} |
1433 |
22 Mar 07 |
fredrik |
518 |
else if (qName.equals("pepfil3")) |
1433 |
22 Mar 07 |
fredrik |
// End of file. |
1433 |
22 Mar 07 |
fredrik |
520 |
{ |
1433 |
22 Mar 07 |
fredrik |
521 |
peaklist.setTotalIntensity(Float.valueOf(total_intensity)); |
1433 |
22 Mar 07 |
fredrik |
522 |
peaklist.setBasePeakIntensity(Float.valueOf(base_peak)); |
1433 |
22 Mar 07 |
fredrik |
523 |
log.info("PiumsPepfilImport end: dc.commit()"); |
1433 |
22 Mar 07 |
fredrik |
524 |
dc.commit(); |
1433 |
22 Mar 07 |
fredrik |
525 |
if (progress != null) |
1433 |
22 Mar 07 |
fredrik |
526 |
progress.display(100, "Piums Pepfil import done\n"); |
1433 |
22 Mar 07 |
fredrik |
527 |
} |
1433 |
22 Mar 07 |
fredrik |
528 |
} |
1433 |
22 Mar 07 |
fredrik |
529 |
} |
1433 |
22 Mar 07 |
fredrik |
530 |
} |