Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package uk.ac.sanger.artemis.components.alignment;
import java.io.*;
import javax.swing.JTextArea;
/**
* Used to run an samtools process this reads stdout and
* stderr in separate threads.
*/
public class RunSamTools
{
/** running process */
private Process p;
/** standard out */
private StringBuffer stdout = new StringBuffer();
/** standard error */
private StringBuffer stderr = new StringBuffer();
private String initialIOError = null;
/** running directory */
//private File project;
/** process status */
private String status;
private StdoutHandler stdouth;
private StderrHandler stderrh;
private JTextArea textArea;
/**
* @param cmd command to run
* @param env environment
* @param project running directory
*/
public RunSamTools(String cmd[],
String[] envp, File project)
{
//this.project = project;
status = "0";
Runtime run = Runtime.getRuntime();
try
{
p = run.exec(cmd,envp,project);
// 2 threads to read in stdout & stderr buffers
// to prevent blocking
stdouth = new StdoutHandler(this);
stderrh = new StderrHandler(this);
stdouth.start();
stderrh.start();
}
catch(IOException ioe)
{
System.err.println("Error executing: "+
cmd[0]);
initialIOError = ioe.getMessage();
status = "1";
}
}
/**
* Read in the process stderr.
*/
private void readProcessStderr()
{
BufferedInputStream stderrStream = null;
BufferedReader stderrRead = null;
try
{
//String line;
stderrStream =
new BufferedInputStream(p.getErrorStream());
stderrRead =
new BufferedReader(new InputStreamReader(stderrStream));
char c[] = new char[100];
int nc = 0;
while((nc = stderrRead.read(c,0,100)) != -1)
stderr = stderr.append(new String(c,0,nc));
}
catch (IOException io)
{
System.err.println("RunEmbossApplication2: Error in "+
"collecting standard out");
}
finally
{
try
{
if(stderrStream!=null)
stderrStream.close();
}
catch(IOException ioe)
{
System.err.println("RunEmbossApplication2: Error closing stream");
}
try
{
if(stderrRead!=null)
stderrRead.close();
}
catch(IOException ioe)
{
System.err.println("RunEmbossApplication2: Error closing reader");
}
}
return;
}
/**
* Read in the process stdout.
*/
private void readProcessStdout()
{
BufferedInputStream stdoutStream = null;
BufferedReader stdoutRead = null;
try
{
//String line;
stdoutStream =
new BufferedInputStream(p.getInputStream());
stdoutRead =
new BufferedReader(new InputStreamReader(stdoutStream));
char c[] = new char[200];
int nc = 0;
String chunk;
while((nc = stdoutRead.read(c,0,200)) != -1)
{
chunk = new String(c,0,nc);
stdout = stdout.append(chunk);
if(textArea != null)
{
textArea.append(chunk);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
}
catch (IOException io)
{
System.err.println("RunEmbossApplication2: Error in "+
"collecting standard out");
}
finally
{
try
{
if(stdoutStream!=null)
stdoutStream.close();
}
catch(IOException ioe)
{
System.err.println("RunEmbossApplication2: Error closing stream");
}
try
{
if(stdoutRead!=null)
stdoutRead.close();
}
catch(IOException ioe)
{
System.err.println("RunEmbossApplication2: Error closing reader");
}
}
return;
}
/**
* This method can be called after the process has completed
* to write the stdout to the project directory.
*/
// private void writeStdout()
// {
// if(project != null)
// {
// PrintWriter out = null;
// String fname = "";
// try
// {
// fname = project.getCanonicalPath() +
// "/application_stdout";
// File so = new File(fname);
//
// if(!so.exists())
// so.createNewFile();
//
// out = new PrintWriter(new FileWriter(fname,true));
// out.println(stdout);
// }
// catch(IOException ioe)
// {
// System.err.println("RunEmbossApplication2: Error writing" +
// fname);
// }
// finally
// {
// if(out!=null)
// out.close();
// }
// }
// }
/**
* @return standard out
*/
public String getProcessStdout()
{
try
{
// make sure we hang around for stdout
while(stdouth.isAlive())
Thread.sleep(10);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
return stdout.toString();
}
/**
* @return stderr
*/
public String getProcessStderr()
{
try
{
// make sure we hang around for stdout
while(stderrh.isAlive())
Thread.sleep(10);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
return new String(stderr.toString().trim());
}
/**
* Wait for the process to end
*/
public void waitFor()
{
try
{
int exitVal = p.waitFor();
if(exitVal != 0)
System.out.println("Exit value:: "+exitVal);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
/**
* @return process
*/
public Process getProcess()
{
return p;
}
/**
* @return status
*/
public String getStatus()
{
return status;
}
class StdoutHandler extends Thread
{
RunSamTools rea;
protected StdoutHandler(RunSamTools rea)
{
this.rea = rea;
}
public void run()
{
rea.readProcessStdout();
}
}
class StderrHandler extends Thread
{
RunSamTools rea;
protected StderrHandler(RunSamTools rea)
{
this.rea = rea;
}
public void run()
{
rea.readProcessStderr();
}
}
public String getInitialIOError()
{
return initialIOError;
}
}