| 1 |
|
package jalview.ws.ebi; |
| 2 |
|
|
| 3 |
|
import jalview.datamodel.AlignmentI; |
| 4 |
|
import jalview.io.AppletFormatAdapter; |
| 5 |
|
import jalview.io.DataSourceType; |
| 6 |
|
import jalview.io.FileFormat; |
| 7 |
|
import jalview.io.FileParse; |
| 8 |
|
|
| 9 |
|
import java.io.File; |
| 10 |
|
import java.io.IOException; |
| 11 |
|
import java.util.regex.Matcher; |
| 12 |
|
|
| 13 |
|
import org.apache.axis.transport.http.HTTPConstants; |
| 14 |
|
import org.apache.http.Header; |
| 15 |
|
import org.apache.http.HttpResponse; |
| 16 |
|
import org.apache.http.client.methods.HttpGet; |
| 17 |
|
import org.apache.http.client.methods.HttpPost; |
| 18 |
|
import org.apache.http.entity.StringEntity; |
| 19 |
|
import org.apache.http.impl.client.DefaultHttpClient; |
| 20 |
|
import org.apache.http.util.EntityUtils; |
| 21 |
|
import org.json.JSONArray; |
| 22 |
|
import org.json.JSONObject; |
| 23 |
|
|
| 24 |
|
import compbio.util.FileUtil; |
| 25 |
|
|
| |
|
| 0% |
Uncovered Elements: 117 (117) |
Complexity: 26 |
Complexity Density: 0.3 |
|
| 26 |
|
public class hmmerClient |
| 27 |
|
{ |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
static String baseUrl = "http://www.ebi.ac.uk/Tools/hmmer", |
| 32 |
|
jackH = "/search/jackhmmer", phmmer = "/search/phmmer", |
| 33 |
|
hmmscan = "/search/hmmscan", hmmsearch = "/search/hmmsearch"; |
| 34 |
|
|
| 35 |
|
static String edseq = ">2abl_A mol:protein length:163 ABL TYROSINE KINASE\nMGPSENDPNLFVALYDFVASGDNTLSITKGEKLRVLGYNHNGEWCEAQTKNGQGWVPSNYITPVNSLEKHS\nWYHGPVSRNAAEYLLSSGINGSFLVRESESSPGQRSISLRYEGRVYHYRINTASDGKLYVSSESRFNTLAE\nLVHHHSTVADGLITTLHYPAP"; |
| 36 |
|
|
| |
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 37 |
0 |
public static void main(String[] args)... |
| 38 |
|
{ |
| 39 |
0 |
String instr = edseq; |
| 40 |
0 |
if (args.length > 0) |
| 41 |
|
{ |
| 42 |
0 |
try |
| 43 |
|
{ |
| 44 |
0 |
instr = FileUtil.readFileToString(new File(args[0])); |
| 45 |
|
} catch (Exception f) |
| 46 |
|
{ |
| 47 |
0 |
f.printStackTrace(); |
| 48 |
0 |
return; |
| 49 |
|
} |
| 50 |
|
} |
| 51 |
0 |
String res = new hmmerClient().submitJackhmmerSearch(instr, |
| 52 |
|
"jackhmmer", "pdb", 5); |
| 53 |
0 |
if (res == null) |
| 54 |
|
{ |
| 55 |
0 |
throw new Error("Failed."); |
| 56 |
|
} |
| 57 |
0 |
System.out.println("Result\n" + res); |
| 58 |
0 |
return; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@param |
| 64 |
|
|
| 65 |
|
@param |
| 66 |
|
|
| 67 |
|
@param |
| 68 |
|
|
| 69 |
|
@param |
| 70 |
|
|
| 71 |
|
@return |
| 72 |
|
|
| |
|
| 0% |
Uncovered Elements: 87 (87) |
Complexity: 19 |
Complexity Density: 0.28 |
|
| 73 |
0 |
String submitJackhmmerSearch(String input, String algo, String db,... |
| 74 |
|
int niter) |
| 75 |
|
{ |
| 76 |
0 |
JSONObject inparam = new JSONObject(); |
| 77 |
0 |
HttpPost jackhp = new HttpPost(baseUrl + jackH); |
| 78 |
0 |
String lastiter = null; |
| 79 |
0 |
try |
| 80 |
|
{ |
| 81 |
0 |
inparam.put("algo", algo); |
| 82 |
0 |
inparam.put("seq", input); |
| 83 |
0 |
inparam.put("seqdb", db); |
| 84 |
0 |
inparam.put("iterations", niter); |
| 85 |
|
|
| 86 |
|
|
| 87 |
0 |
jackhp.setHeader("content-type", "application/json"); |
| 88 |
0 |
jackhp.setEntity(new StringEntity(inparam.toString())); |
| 89 |
|
} catch (Exception f) |
| 90 |
|
{ |
| 91 |
0 |
f.printStackTrace(); |
| 92 |
0 |
return null; |
| 93 |
|
} |
| 94 |
0 |
HttpResponse r = null; |
| 95 |
0 |
try |
| 96 |
|
{ |
| 97 |
0 |
DefaultHttpClient httpCl = new DefaultHttpClient(); |
| 98 |
|
|
| 99 |
0 |
r = httpCl.execute(jackhp); |
| 100 |
|
|
| 101 |
|
} catch (Exception x) |
| 102 |
|
{ |
| 103 |
0 |
System.err.println("Submit failed."); |
| 104 |
0 |
x.printStackTrace(); |
| 105 |
|
} |
| 106 |
0 |
if (r.getStatusLine().getStatusCode() != 201) |
| 107 |
|
{ |
| 108 |
0 |
throw new Error(r.toString()); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
0 |
String jobid = null, redir = null; |
| 112 |
0 |
try |
| 113 |
|
{ |
| 114 |
0 |
JSONObject res = new JSONObject(EntityUtils.toString(r.getEntity())); |
| 115 |
0 |
jobid = res.getString("job_id"); |
| 116 |
|
|
| 117 |
0 |
Header[] loc; |
| 118 |
0 |
if ((loc = r.getHeaders(HTTPConstants.HEADER_LOCATION)) != null |
| 119 |
|
&& loc.length > 0) |
| 120 |
|
{ |
| 121 |
0 |
if (loc.length > 1) |
| 122 |
|
{ |
| 123 |
0 |
System.err |
| 124 |
|
.println("Ignoring additional " |
| 125 |
|
+ (loc.length - 1) |
| 126 |
|
+ " location(s) provided in response header ( next one is '" |
| 127 |
|
+ loc[1].getValue() + "' )"); |
| 128 |
|
} |
| 129 |
0 |
redir = loc[0].getValue(); |
| 130 |
|
} |
| 131 |
|
} catch (Exception x) |
| 132 |
|
{ |
| 133 |
0 |
System.err.println("job id extraction failed."); |
| 134 |
0 |
x.printStackTrace(); |
| 135 |
|
} |
| 136 |
0 |
int tries = 0; |
| 137 |
0 |
boolean finished = false; |
| 138 |
0 |
JSONObject jobstate = null; |
| 139 |
0 |
do |
| 140 |
|
{ |
| 141 |
0 |
try |
| 142 |
|
{ |
| 143 |
0 |
DefaultHttpClient httpCl = new DefaultHttpClient(); |
| 144 |
|
|
| 145 |
0 |
HttpGet jackcheck = new HttpGet(redir); |
| 146 |
0 |
jackcheck.setHeader("content-type", "application/json"); |
| 147 |
0 |
r = httpCl.execute(jackcheck); |
| 148 |
0 |
switch (r.getStatusLine().getStatusCode()) |
| 149 |
|
{ |
| 150 |
0 |
case 200: |
| 151 |
0 |
jobstate = new JSONObject(EntityUtils.toString(r.getEntity())); |
| 152 |
0 |
String st = jobstate.getString("status"); |
| 153 |
0 |
if ("DONE".equals(st)) |
| 154 |
|
{ |
| 155 |
0 |
finished = true; |
| 156 |
|
} |
| 157 |
0 |
if ("ERROR".equals(st)) |
| 158 |
|
{ |
| 159 |
0 |
System.err.println("Error"); |
| 160 |
0 |
finished = true; |
| 161 |
|
} |
| 162 |
0 |
if ("PEND".equals(st) || "RUN".equals("st")) |
| 163 |
|
{ |
| 164 |
0 |
JSONArray iters = jobstate.getJSONArray("result"); |
| 165 |
0 |
lastiter = iters.getJSONObject(iters.length() - 1).getString( |
| 166 |
|
"uuid"); |
| 167 |
0 |
if (lastiter.length() > 0) |
| 168 |
|
{ |
| 169 |
0 |
java.util.regex.Pattern p = java.util.regex.Pattern |
| 170 |
|
.compile(".+(\\d+)"); |
| 171 |
0 |
Matcher m = p.matcher(lastiter); |
| 172 |
0 |
if (m.matches()) |
| 173 |
|
{ |
| 174 |
0 |
System.out.println("On iteration " + m.group(1)); |
| 175 |
|
} |
| 176 |
|
} |
| 177 |
|
} |
| 178 |
0 |
break; |
| 179 |
|
|
| 180 |
0 |
default: |
| 181 |
0 |
tries++; |
| 182 |
0 |
Thread.sleep(2000); |
| 183 |
|
} |
| 184 |
|
} catch (Exception q) |
| 185 |
|
{ |
| 186 |
0 |
q.printStackTrace(); |
| 187 |
0 |
return null; |
| 188 |
|
} |
| 189 |
0 |
} while (!finished && tries < 50); |
| 190 |
|
|
| 191 |
0 |
if (!finished) |
| 192 |
|
{ |
| 193 |
0 |
System.err.println("Giving up with job " + jobid + " at " + redir); |
| 194 |
0 |
return null; |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
0 |
return lastiter; |
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
@param |
| 235 |
|
@param |
| 236 |
|
@return |
| 237 |
|
|
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 238 |
0 |
AlignmentI retrieveJackhmmerResult(String jobid, AlignmentI dataset)... |
| 239 |
|
throws OutOfMemoryError, IOException |
| 240 |
|
{ |
| 241 |
0 |
AlignmentI searchResult = null; |
| 242 |
|
|
| 243 |
|
|
| 244 |
|
|
| 245 |
0 |
searchResult = new AppletFormatAdapter().readFile(baseUrl |
| 246 |
|
+ "/download/" + jobid + "/score?format=afa&t=.gz", |
| 247 |
|
DataSourceType.URL, FileFormat.Fasta); |
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
0 |
FileParse jsonsource = new FileParse(baseUrl + "/download/" + jobid |
| 255 |
|
+ "/score?format=json", DataSourceType.URL); |
| 256 |
0 |
if (!jsonsource.isValid()) |
| 257 |
|
{ |
| 258 |
0 |
throw new IOException("Couldn't access scores for Jackhammer results"); |
| 259 |
|
} |
| 260 |
0 |
readJackhmmerScores(searchResult, jsonsource); |
| 261 |
0 |
return searchResult; |
| 262 |
|
} |
| 263 |
|
|
| 264 |
|
|
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
private static String[] _hmmsearchcols = new String[] { "acc", "name", "" }; |
| 276 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 277 |
0 |
private void readJackhmmerScores(AlignmentI searchResult,... |
| 278 |
|
FileParse jsonsource) throws IOException, OutOfMemoryError |
| 279 |
|
{ |
| 280 |
0 |
HmmerJSONProcessor hjp = new HmmerJSONProcessor(searchResult); |
| 281 |
0 |
hjp.parseFrom(jsonsource); |
| 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 |
|
} |
| 318 |
|
|
| 319 |
|
} |