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
/* RunBlastAtNCBI
*
* created: 2009
*
* This file is part of Artemis
*
* Copyright(C) 2009 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
**/
package uk.ac.sanger.artemis.components;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import uk.ac.sanger.artemis.editor.BrowserControl;
/**
* Used to POST data to the NCBI URLAPI (qBLAST) web services.
*/
class RunBlastAtNCBI extends Thread
{
private String data;
public RunBlastAtNCBI(final String data)
{
this.data = data;
}
public void run()
{
try
{
// Send data
URL url = new URL("https://blast.ncbi.nlm.nih.gov/Blast.cgi");
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
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String urlResults = url + "?RID=";
String line;
String eta = "10";
while ((line = rd.readLine()) != null)
{
int index;
if ((index = line.indexOf("RID =")) > -1)
{
line = line.substring(index + 5).trim();
urlResults = urlResults.concat(line);
if (line.equals(""))
{
JOptionPane.showMessageDialog(null, line, "Blast search error",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
else if ((index = line.indexOf("RTOE =")) > -1)
eta = line.substring(index + 6).trim();
}
wr.close();
rd.close();
int waitTime = Integer.parseInt(eta);
ProgressBarFrame progress = new ProgressBarFrame(waitTime, "BLAST");
Thread.sleep(waitTime * 1000);
BrowserControl.displayURL(urlResults + "&CMD=Get&OLD_BLAST=false");
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
progress.dispose();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Utility for appending key - value pairs to the data used to POST
* @param data
* @param key
* @param value
* @return
* @throws UnsupportedEncodingException
*/
private static String addData(String data, String key, String value)
throws UnsupportedEncodingException
{
data += "&" +URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
return data;
}
/**
* Allow the user to define the blast options.
* @param programName
* @param residues
* @return
*/
public static String setData(String programName, String residues)
{
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JPanel dataPanel = new JPanel(layout);
int rows = 0;
c.gridx = 0;
c.gridy = rows;
c.anchor = GridBagConstraints.EAST;
JTextField textField = new JTextField("nr",14);
dataPanel.add(new JLabel("NCBI Database"), c);
c.gridx = 1;
dataPanel.add(textField, c);
c.gridx = 0;
c.gridy = ++rows;
JTextField hitField = new JTextField("500",14);
dataPanel.add(new JLabel("Number of hits to keep"), c);
c.gridx = 1;
dataPanel.add(hitField, c);
// Gap open and gap close costs
// Space separated float values
// ''5 2'' for nuc-nuc, ''11 1'' for proteins, non-affine for megablast
c.gridx = 0;
c.gridy = ++rows;
JTextField gapOpenField = new JTextField((programName.equals("blastn")) ? "5": "11",14);
dataPanel.add(new JLabel("Gap open cost"), c);
c.gridx = 1;
dataPanel.add(gapOpenField, c);
c.gridx = 0;
c.gridy = ++rows;
JTextField gapCloseField = new JTextField((programName.equals("blastn")) ? "2": "1",14);
dataPanel.add(new JLabel("Gap close cost"), c);
c.gridx = 1;
dataPanel.add(gapCloseField, c);
c.gridx = 0;
c.gridy = ++rows;
JTextField expectField = new JTextField("10.0",14);
dataPanel.add(new JLabel("Expect value"), c);
c.gridx = 1;
dataPanel.add(expectField, c);
c.gridx = 0;
c.gridy = ++rows;
JComboBox filterField = new JComboBox(new String[]{
"None", "Low Complexity", "Human Repeats", "Masked" });
dataPanel.add(new JLabel("Filter"), c);
c.gridx = 1;
c.anchor = GridBagConstraints.WEST;
dataPanel.add(filterField, c);
c.gridx = 0;
c.gridy = ++rows;
String serviceOptions[];
if(programName.equals("blastn"))
serviceOptions = new String[]{"plain", "megablast"};
else
serviceOptions = new String[]{"plain"}; // psi blast support?
JComboBox serviceField = new JComboBox(serviceOptions);
c.anchor = GridBagConstraints.EAST;
dataPanel.add(new JLabel("Blast service"), c);
c.gridx = 1;
c.anchor = GridBagConstraints.WEST;
dataPanel.add(serviceField, c);
int status = JOptionPane.showConfirmDialog(null, dataPanel,
"Options for "+programName, JOptionPane.OK_CANCEL_OPTION);
if(status != JOptionPane.OK_OPTION)
return null;
try
{
String data = URLEncoder.encode("CMD", "UTF-8") + "=" +
URLEncoder.encode("Put", "UTF-8");
data = addData(data, "QUERY", residues);
data = addData(data, "DATABASE", textField.getText());
data = addData(data, "HITLIST_SIZE", hitField.getText());
if(getFilterOption(filterField) != null)
data = addData(data, "FILTER", getFilterOption(filterField));
data = addData(data, "EXPECT", expectField.getText());
data = addData(data, "FORMAT_TYPE", "HTML");
data = addData(data, "PROGRAM", programName);
data = addData(data, "CLIENT", "web");
data = addData(data, "SERVICE", (String) serviceField.getSelectedItem());
if(((String) serviceField.getSelectedItem()).equals("megablast"))
data = addData(data, "MEGABLAST", "yes");
else
data = addData(data, "GAPCOSTS", gapOpenField.getText()+" "+gapCloseField.getText());
return data;
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return null;
}
/**
* Blast filter options.
* @param filterField
* @return
*/
private static String getFilterOption(JComboBox filterField)
{
String sel = (String) filterField.getSelectedItem();
if(sel.equals("None"))
return null;
else if(sel.equals("Low Complexity"))
return "L";
else if(sel.equals("Human Repeats"))
return "R";
else
return "m";
}
public static void main(String args[])
{
String data =
//BlastAtNCBI.setData("blastp", "ATHIEDLHNITSNQLYETYRTEKLSTSQLLLDSTVXTIDKNLSQHDQVLREDRLR");
RunBlastAtNCBI.setData("blastn", "aggctgttttccacagatttcacagtattggttcaaatggtcaaaaattgttttaaccagt");
RunBlastAtNCBI blast= new RunBlastAtNCBI(data);
blast.start();
}