123 |
12 Jul 06 |
jari |
1 |
/* |
123 |
12 Jul 06 |
jari |
$Id$ |
123 |
12 Jul 06 |
jari |
3 |
|
348 |
27 Jun 07 |
jari |
Copyright (C) 2006 Jari Häkkinen |
348 |
27 Jun 07 |
jari |
Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
123 |
12 Jul 06 |
jari |
6 |
|
123 |
12 Jul 06 |
jari |
This file is part of BASEPlugIns - Non-core plug-ins for BASE, |
123 |
12 Jul 06 |
jari |
BioArray Software Environment. |
123 |
12 Jul 06 |
jari |
BASEPlugIns is available at http://baseplugins.thep.lu.se/ |
123 |
12 Jul 06 |
jari |
BASE is available at http://base.thep.lu.se/ |
123 |
12 Jul 06 |
jari |
11 |
|
123 |
12 Jul 06 |
jari |
BASEPlugIns is free software; you can redistribute it and/or modify |
123 |
12 Jul 06 |
jari |
it under the terms of the GNU General Public License as published by |
123 |
12 Jul 06 |
jari |
the Free Software Foundation; either version 2 of the License, or |
123 |
12 Jul 06 |
jari |
(at your option) any later version. |
123 |
12 Jul 06 |
jari |
16 |
|
123 |
12 Jul 06 |
jari |
BASEPlugIns is distributed in the hope that it will be useful, but |
123 |
12 Jul 06 |
jari |
WITHOUT ANY WARRANTY; without even the implied warranty of |
123 |
12 Jul 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
123 |
12 Jul 06 |
jari |
General Public License for more details. |
123 |
12 Jul 06 |
jari |
21 |
|
123 |
12 Jul 06 |
jari |
You should have received a copy of the GNU General Public License |
123 |
12 Jul 06 |
jari |
along with this program; if not, write to the Free Software |
123 |
12 Jul 06 |
jari |
Foundation, Inc., 59 Temple Place - Suite 330, |
123 |
12 Jul 06 |
jari |
Boston, MA 02111-1307, USA. |
123 |
12 Jul 06 |
jari |
26 |
*/ |
123 |
12 Jul 06 |
jari |
27 |
|
123 |
12 Jul 06 |
jari |
28 |
package se.lu.thep.affymetrix; |
123 |
12 Jul 06 |
jari |
29 |
|
311 |
28 May 07 |
peter |
30 |
import se.lu.thep.affymetrix.AbstractExternalBinaryPlugin; |
311 |
28 May 07 |
peter |
31 |
|
123 |
12 Jul 06 |
jari |
32 |
import net.sf.basedb.core.Affymetrix; |
123 |
12 Jul 06 |
jari |
33 |
import net.sf.basedb.core.ArrayDesign; |
123 |
12 Jul 06 |
jari |
34 |
import net.sf.basedb.core.BaseException; |
123 |
12 Jul 06 |
jari |
35 |
import net.sf.basedb.core.BioAssay; |
123 |
12 Jul 06 |
jari |
36 |
import net.sf.basedb.core.BioAssaySet; |
123 |
12 Jul 06 |
jari |
37 |
import net.sf.basedb.core.DbControl; |
123 |
12 Jul 06 |
jari |
38 |
import net.sf.basedb.core.Experiment; |
123 |
12 Jul 06 |
jari |
39 |
import net.sf.basedb.core.File; |
123 |
12 Jul 06 |
jari |
40 |
import net.sf.basedb.core.Item; |
123 |
12 Jul 06 |
jari |
41 |
import net.sf.basedb.core.ItemParameterType; |
123 |
12 Jul 06 |
jari |
42 |
import net.sf.basedb.core.Job; |
153 |
10 Aug 06 |
jari |
43 |
import net.sf.basedb.core.PluginDefinition; |
123 |
12 Jul 06 |
jari |
44 |
import net.sf.basedb.core.PluginParameter; |
123 |
12 Jul 06 |
jari |
45 |
import net.sf.basedb.core.PositionBatcher; |
123 |
12 Jul 06 |
jari |
46 |
import net.sf.basedb.core.ProgressReporter; |
123 |
12 Jul 06 |
jari |
47 |
import net.sf.basedb.core.RawBioAssay; |
129 |
08 Aug 06 |
jari |
48 |
import net.sf.basedb.core.ReporterBatcher; |
123 |
12 Jul 06 |
jari |
49 |
import net.sf.basedb.core.RequestInformation; |
123 |
12 Jul 06 |
jari |
50 |
import net.sf.basedb.core.SpotBatcher; |
123 |
12 Jul 06 |
jari |
51 |
import net.sf.basedb.core.StringParameterType; |
123 |
12 Jul 06 |
jari |
52 |
import net.sf.basedb.core.Transformation; |
123 |
12 Jul 06 |
jari |
53 |
|
123 |
12 Jul 06 |
jari |
54 |
import net.sf.basedb.core.plugin.About; |
123 |
12 Jul 06 |
jari |
55 |
import net.sf.basedb.core.plugin.AboutImpl; |
123 |
12 Jul 06 |
jari |
56 |
import net.sf.basedb.core.plugin.GuiContext; |
123 |
12 Jul 06 |
jari |
57 |
|
123 |
12 Jul 06 |
jari |
58 |
import net.sf.basedb.util.parser.FlatFileParser; |
123 |
12 Jul 06 |
jari |
59 |
|
123 |
12 Jul 06 |
jari |
60 |
import java.io.IOException; |
123 |
12 Jul 06 |
jari |
61 |
|
123 |
12 Jul 06 |
jari |
62 |
import java.util.Collections; |
123 |
12 Jul 06 |
jari |
63 |
import java.util.HashMap; |
123 |
12 Jul 06 |
jari |
64 |
import java.util.List; |
129 |
08 Aug 06 |
jari |
65 |
import java.util.Map; |
123 |
12 Jul 06 |
jari |
66 |
import java.util.Set; |
123 |
12 Jul 06 |
jari |
67 |
|
123 |
12 Jul 06 |
jari |
68 |
import java.util.regex.Pattern; |
123 |
12 Jul 06 |
jari |
69 |
|
123 |
12 Jul 06 |
jari |
70 |
/** |
123 |
12 Jul 06 |
jari |
This plugin provides means to calculate the fold changes for a root bioassayset |
123 |
12 Jul 06 |
jari |
using RMAExpress. It needs the following parameters: |
123 |
12 Jul 06 |
jari |
73 |
|
123 |
12 Jul 06 |
jari |
<pre> |
123 |
12 Jul 06 |
jari |
String : name; The name of the new bioassayset. |
123 |
12 Jul 06 |
jari |
Experiment : experiment; The experiment we are working on. |
123 |
12 Jul 06 |
jari |
List>RawBioAssay< : rawBioAssays; The raw bioassays to create bioassays for |
123 |
12 Jul 06 |
jari |
(must be part of the experiment). |
123 |
12 Jul 06 |
jari |
</pre> |
123 |
12 Jul 06 |
jari |
80 |
|
123 |
12 Jul 06 |
jari |
@author Jari |
134 |
09 Aug 06 |
jari |
@version MAKESUBSTOFVERSIONNUMBER |
126 |
20 Jul 06 |
jari |
@base.modified $Date$ |
123 |
12 Jul 06 |
jari |
84 |
*/ |
123 |
12 Jul 06 |
jari |
85 |
|
123 |
12 Jul 06 |
jari |
86 |
public class RMAExpress |
311 |
28 May 07 |
peter |
87 |
extends AbstractExternalBinaryPlugin |
123 |
12 Jul 06 |
jari |
88 |
{ |
123 |
12 Jul 06 |
jari |
89 |
|
123 |
12 Jul 06 |
jari |
90 |
public RMAExpress() {} |
123 |
12 Jul 06 |
jari |
91 |
|
123 |
12 Jul 06 |
jari |
92 |
public About getAbout() { return about; } |
123 |
12 Jul 06 |
jari |
93 |
|
123 |
12 Jul 06 |
jari |
94 |
|
153 |
10 Aug 06 |
jari |
95 |
public String getRMAExpressConsolePath() |
153 |
10 Aug 06 |
jari |
96 |
{ |
153 |
10 Aug 06 |
jari |
97 |
if (RMAExpressConsolePath == null) { |
153 |
10 Aug 06 |
jari |
98 |
DbControl dc = sc.newDbControl(); |
343 |
25 Jun 07 |
jari |
99 |
try { |
343 |
25 Jun 07 |
jari |
100 |
String jarpath=PluginDefinition.getByClassName(dc, |
153 |
10 Aug 06 |
jari |
101 |
"se.lu.thep.affymetrix.RMAExpress").getJarPath(); |
343 |
25 Jun 07 |
jari |
102 |
int pos = jarpath.length(); |
343 |
25 Jun 07 |
jari |
103 |
while ((pos>0) && (jarpath.charAt(pos-1)!=java.io.File.separatorChar)) |
343 |
25 Jun 07 |
jari |
104 |
--pos; |
343 |
25 Jun 07 |
jari |
105 |
RMAExpressConsolePath=jarpath.substring(0,pos); |
343 |
25 Jun 07 |
jari |
106 |
} |
343 |
25 Jun 07 |
jari |
107 |
finally { |
343 |
25 Jun 07 |
jari |
108 |
if (dc != null) |
343 |
25 Jun 07 |
jari |
109 |
dc.close(); |
343 |
25 Jun 07 |
jari |
110 |
} |
153 |
10 Aug 06 |
jari |
// Hard coded file name and the binary must reside in the same |
153 |
10 Aug 06 |
jari |
// directory as the plug-in jar. The idea behind this is that |
153 |
10 Aug 06 |
jari |
// only the BASE administrator is allowed to change files in the |
342 |
23 Jun 07 |
jari |
// BASE application directory structure. |
153 |
10 Aug 06 |
jari |
115 |
RMAExpressConsolePath+="RMAExpressConsole"; |
153 |
10 Aug 06 |
jari |
116 |
} |
153 |
10 Aug 06 |
jari |
117 |
return RMAExpressConsolePath; |
153 |
10 Aug 06 |
jari |
118 |
} |
153 |
10 Aug 06 |
jari |
119 |
|
153 |
10 Aug 06 |
jari |
120 |
|
311 |
28 May 07 |
peter |
121 |
protected void runBinary(List<RawBioAssay> sources, |
311 |
28 May 07 |
peter |
122 |
ProgressReporter progress) |
123 |
12 Jul 06 |
jari |
123 |
throws InterruptedException, IOException |
123 |
12 Jul 06 |
jari |
124 |
{ |
123 |
12 Jul 06 |
jari |
// Are filenames used here unique? There are many possible |
123 |
12 Jul 06 |
jari |
// clashes, maybe the implementation should use system generated |
123 |
12 Jul 06 |
jari |
// unique filenames? |
123 |
12 Jul 06 |
jari |
128 |
String filelistname="inputfiles.txt"; |
123 |
12 Jul 06 |
jari |
129 |
java.io.PrintWriter filelist = |
123 |
12 Jul 06 |
jari |
130 |
new java.io.PrintWriter(new java.io.File(getExecDirectory(),filelistname)); |
123 |
12 Jul 06 |
jari |
131 |
File cdf=Affymetrix.getCdfFile(sources.get(0).getArrayDesign()); |
133 |
09 Aug 06 |
jari |
132 |
progress.display(percentDone,"Downloading CDF file '" +cdf.getName()+ "'."); |
123 |
12 Jul 06 |
jari |
133 |
filelist.println(cdf.getName()); |
123 |
12 Jul 06 |
jari |
134 |
download(cdf); |
123 |
12 Jul 06 |
jari |
135 |
for (RawBioAssay rba : sources) { |
123 |
12 Jul 06 |
jari |
136 |
File cel=Affymetrix.getCelFile(rba); |
133 |
09 Aug 06 |
jari |
137 |
progress.display(percentDone,"Downloading CEL file '"+cel.getName()+"'."); |
123 |
12 Jul 06 |
jari |
138 |
filelist.println(cel.getName()); |
123 |
12 Jul 06 |
jari |
139 |
download(cel); |
123 |
12 Jul 06 |
jari |
140 |
} |
123 |
12 Jul 06 |
jari |
141 |
filelist.close(); |
123 |
12 Jul 06 |
jari |
142 |
String optionname="option.txt"; |
123 |
12 Jul 06 |
jari |
143 |
String resultname="./rma.txt"; |
123 |
12 Jul 06 |
jari |
144 |
java.io.PrintWriter option = |
123 |
12 Jul 06 |
jari |
145 |
new java.io.PrintWriter(new java.io.File(getExecDirectory(), optionname)); |
123 |
12 Jul 06 |
jari |
146 |
option.println("1\n"+resultname); |
123 |
12 Jul 06 |
jari |
147 |
option.close(); |
133 |
09 Aug 06 |
jari |
148 |
percentDone+=1; |
133 |
09 Aug 06 |
jari |
149 |
progress.display(percentDone,"Running RMAExpress."); |
153 |
10 Aug 06 |
jari |
150 |
Process p = Runtime.getRuntime().exec(getRMAExpressConsolePath() + " " + |
153 |
10 Aug 06 |
jari |
151 |
filelistname + " " + optionname, null, |
153 |
10 Aug 06 |
jari |
152 |
getExecDirectory()); |
123 |
12 Jul 06 |
jari |
153 |
p.waitFor(); |
123 |
12 Jul 06 |
jari |
154 |
int status=p.exitValue(); |
123 |
12 Jul 06 |
jari |
155 |
if (status>0) |
123 |
12 Jul 06 |
jari |
156 |
throw new IOException("Unexpected exit of RMAExpress sub-process. " + |
123 |
12 Jul 06 |
jari |
157 |
"Return value: " + status); |
123 |
12 Jul 06 |
jari |
158 |
} |
123 |
12 Jul 06 |
jari |
159 |
|
123 |
12 Jul 06 |
jari |
160 |
|
311 |
28 May 07 |
peter |
161 |
protected void storeResult(DbControl dc, Experiment experiment, |
311 |
28 May 07 |
peter |
162 |
List<RawBioAssay> sources, String name, |
311 |
28 May 07 |
peter |
163 |
ProgressReporter progress) |
123 |
12 Jul 06 |
jari |
164 |
{ |
123 |
12 Jul 06 |
jari |
165 |
try { |
123 |
12 Jul 06 |
jari |
166 |
FlatFileParser ffp=new FlatFileParser(); |
123 |
12 Jul 06 |
jari |
167 |
ffp.setInputStream(new java.io.FileInputStream |
290 |
23 May 07 |
jari |
168 |
(getExecDirectory()+java.io.File.separator+"rma.txt"), |
290 |
23 May 07 |
jari |
169 |
null); |
123 |
12 Jul 06 |
jari |
170 |
ffp.setDataHeaderRegexp(Pattern.compile("Probesets\\t.*")); |
123 |
12 Jul 06 |
jari |
171 |
ffp.setDataSplitterRegexp(Pattern.compile("\\t")); |
123 |
12 Jul 06 |
jari |
172 |
ffp.setMinDataColumns(sources.size()+1); |
123 |
12 Jul 06 |
jari |
173 |
ffp.setMaxDataColumns(sources.size()+1); |
123 |
12 Jul 06 |
jari |
174 |
ffp.parseHeaders(); |
123 |
12 Jul 06 |
jari |
175 |
|
133 |
09 Aug 06 |
jari |
176 |
Transformation t=experiment.newTransformation(Job.getById(dc,job.getId()), |
133 |
09 Aug 06 |
jari |
177 |
sources); |
123 |
12 Jul 06 |
jari |
178 |
t.setName("RMAExpress"); |
129 |
08 Aug 06 |
jari |
179 |
BioAssaySet root = t.newProduct("new", "new", false); |
123 |
12 Jul 06 |
jari |
180 |
root.setName(name); |
123 |
12 Jul 06 |
jari |
181 |
dc.saveItem(t); |
123 |
12 Jul 06 |
jari |
182 |
dc.saveItem(root); |
123 |
12 Jul 06 |
jari |
183 |
|
129 |
08 Aug 06 |
jari |
184 |
HashMap<Integer,Short> cubecolumn=new HashMap<Integer,Short>(); |
123 |
12 Jul 06 |
jari |
185 |
for (RawBioAssay rba : sources) { |
123 |
12 Jul 06 |
jari |
186 |
BioAssay ba = root.newRootBioAssay(Collections.singleton(rba)); |
123 |
12 Jul 06 |
jari |
187 |
ba.setName(rba.getName()); |
123 |
12 Jul 06 |
jari |
188 |
dc.saveItem(ba); |
123 |
12 Jul 06 |
jari |
189 |
cubecolumn.put |
123 |
12 Jul 06 |
jari |
190 |
(ffp.getColumnHeaderIndex(Affymetrix.getCelFile(rba).getName()), |
129 |
08 Aug 06 |
jari |
191 |
ba.getDataCubeColumnNo()); |
123 |
12 Jul 06 |
jari |
192 |
} |
123 |
12 Jul 06 |
jari |
193 |
|
191 |
31 Oct 06 |
jari |
194 |
int position=1; // Use 1 for consistency with BASE usage if position |
129 |
08 Aug 06 |
jari |
195 |
ReporterBatcher rbatcher = ReporterBatcher.getNew(dc); |
129 |
08 Aug 06 |
jari |
196 |
PositionBatcher pbatcher = root.getPositionBatcher(); |
129 |
08 Aug 06 |
jari |
197 |
SpotBatcher sbatcher = root.getSpotBatcher(); |
133 |
09 Aug 06 |
jari |
// The progress should be inside the loop, but for now we do not |
133 |
09 Aug 06 |
jari |
// know how many data entries there is to store (currently the |
133 |
09 Aug 06 |
jari |
// number of "spots" is not stored in the database for |
133 |
09 Aug 06 |
jari |
// Affymetrix). |
133 |
09 Aug 06 |
jari |
202 |
percentDone=95; |
133 |
09 Aug 06 |
jari |
203 |
progress.display(percentDone,"Importing result."); |
123 |
12 Jul 06 |
jari |
204 |
while (ffp.hasMoreData()) { |
123 |
12 Jul 06 |
jari |
205 |
FlatFileParser.Data data = ffp.nextData(); |
129 |
08 Aug 06 |
jari |
206 |
for (Map.Entry<Integer,Short> entry : cubecolumn.entrySet()) |
129 |
08 Aug 06 |
jari |
207 |
sbatcher.insert(entry.getValue().shortValue(), position, |
129 |
08 Aug 06 |
jari |
208 |
Float.parseFloat(data.get(entry.getKey()))); |
123 |
12 Jul 06 |
jari |
// The reporter id is assumed to be the first column |
129 |
08 Aug 06 |
jari |
210 |
pbatcher.insert(position,rbatcher.getByExternalId(data.get(0))); |
123 |
12 Jul 06 |
jari |
211 |
++position; |
123 |
12 Jul 06 |
jari |
212 |
} |
133 |
09 Aug 06 |
jari |
213 |
rbatcher.close(); |
133 |
09 Aug 06 |
jari |
214 |
pbatcher.close(); |
123 |
12 Jul 06 |
jari |
215 |
sbatcher.close(); |
123 |
12 Jul 06 |
jari |
216 |
} |
123 |
12 Jul 06 |
jari |
217 |
catch(Throwable e) { |
218 |
13 Dec 06 |
jari |
218 |
throw new BaseException("Unable to import root bioassay.",e); |
123 |
12 Jul 06 |
jari |
219 |
} |
123 |
12 Jul 06 |
jari |
220 |
} |
123 |
12 Jul 06 |
jari |
221 |
|
123 |
12 Jul 06 |
jari |
222 |
|
123 |
12 Jul 06 |
jari |
223 |
private static final About about = new AboutImpl |
123 |
12 Jul 06 |
jari |
224 |
("RMAExpress plug-in", "Computes gene expression summary values for " + |
123 |
12 Jul 06 |
jari |
225 |
"Affymetrix Genechip data using the Robust Multichip Average " + |
301 |
24 May 07 |
jari |
226 |
"expression summary. Results are expressed in log2 scale.", |
134 |
09 Aug 06 |
jari |
227 |
"Version: MAKESUBSTOFVERSIONNUMBER $Revision$ " + |
134 |
09 Aug 06 |
jari |
228 |
"$Date$", |
297 |
23 May 07 |
jari |
229 |
"2006, 2007 Jari Hakkinen, " + |
125 |
20 Jul 06 |
jari |
230 |
"Department of Theoretical Physics, Lund University", |
165 |
29 Aug 06 |
jari |
231 |
null, null, |
299 |
24 May 07 |
jari |
232 |
"http://baseplugins.thep.lu.se/wiki/se.lu.thep.affymetrix" ); |
123 |
12 Jul 06 |
jari |
233 |
private RequestInformation configureJob = null; |
123 |
12 Jul 06 |
jari |
234 |
private RequestInformation configurePlugin = null; |
123 |
12 Jul 06 |
jari |
235 |
private ItemParameterType<Experiment> experimentType; |
123 |
12 Jul 06 |
jari |
236 |
/** |
123 |
12 Jul 06 |
jari |
This is the directory where the plug-in will generate its output. |
123 |
12 Jul 06 |
jari |
238 |
*/ |
123 |
12 Jul 06 |
jari |
239 |
private java.io.File execDirectory; |
123 |
12 Jul 06 |
jari |
240 |
private PluginParameter<Experiment> experimentParameter; |
123 |
12 Jul 06 |
jari |
241 |
private static final Set<GuiContext> guiContexts = Collections.singleton |
123 |
12 Jul 06 |
jari |
242 |
(new GuiContext(Item.BIOASSAYSET,GuiContext.Type.LIST)); |
123 |
12 Jul 06 |
jari |
243 |
private static final StringParameterType nameType = |
123 |
12 Jul 06 |
jari |
244 |
new StringParameterType(BioAssaySet.MAX_NAME_LENGTH, "New bioassayset", |
123 |
12 Jul 06 |
jari |
245 |
true); |
123 |
12 Jul 06 |
jari |
246 |
private static final PluginParameter<String> nameParameter = |
123 |
12 Jul 06 |
jari |
247 |
new PluginParameter<String>("name", "Bioassay set name", |
123 |
12 Jul 06 |
jari |
248 |
"The name of the root bioassayset", nameType); |
133 |
09 Aug 06 |
jari |
249 |
private int percentDone=0; |
123 |
12 Jul 06 |
jari |
250 |
private ItemParameterType<RawBioAssay> rawBioAssaysType; |
123 |
12 Jul 06 |
jari |
251 |
private PluginParameter<RawBioAssay> rawBioAssaysParameter; |
153 |
10 Aug 06 |
jari |
252 |
private String RMAExpressConsolePath; |
123 |
12 Jul 06 |
jari |
253 |
} |