JAL-1934 conditional evaluation structure not needed
[jalview.git] / src / jalview / ws / ebi / hmmerClient.java
1 package jalview.ws.ebi;
2
3 import java.io.File;
4 import java.util.regex.Matcher;
5
6 import org.apache.axis.transport.http.HTTPConstants;
7 import org.apache.http.Header;
8 import org.apache.http.HttpResponse;
9 import org.apache.http.client.methods.HttpGet;
10 import org.apache.http.client.methods.HttpPost;
11 import org.apache.http.entity.StringEntity;
12 import org.apache.http.impl.client.DefaultHttpClient;
13 import org.apache.http.util.EntityUtils;
14 import org.json.JSONArray;
15 import org.json.JSONObject;
16
17 import compbio.util.FileUtil;
18
19 public class hmmerClient
20 {
21   /**
22    * URLs for ebi api
23    */
24   static String baseUrl = "http://www.ebi.ac.uk/Tools/hmmer",
25           jackH = "/search/jackhmmer", phmmer = "/search/phmmer",
26           hmmscan = "/search/hmmscan", hmmsearch = "/search/hmmsearch";
27
28   static String edseq = ">2abl_A mol:protein length:163  ABL TYROSINE KINASE\nMGPSENDPNLFVALYDFVASGDNTLSITKGEKLRVLGYNHNGEWCEAQTKNGQGWVPSNYITPVNSLEKHS\nWYHGPVSRNAAEYLLSSGINGSFLVRESESSPGQRSISLRYEGRVYHYRINTASDGKLYVSSESRFNTLAE\nLVHHHSTVADGLITTLHYPAP";
29
30   public static void main(String[] args)
31   {
32     String instr = edseq;
33     if (args.length > 0)
34     {
35       try
36       {
37         instr = FileUtil.readFileToString(new File(args[0]));
38       } catch (Exception f)
39       {
40         f.printStackTrace();
41         return;
42       }
43     }
44     String res = new hmmerClient().submitJackhmmerSearch(instr,
45             "jackhmmer", "pdb", 5);
46     if (res == null)
47     {
48       throw new Error("Failed.");
49     }
50     System.out.println("Result\n" + res);
51     return;
52   }
53   /**
54    * 
55    * @param input
56    *          - fasta or other formatted sequence or alignment
57    * @param algo
58    *          - jackhmmer
59    * @param db
60    *          - pdb, uniprot, etc.
61    * @param niter
62    *          number of iterations
63    * @return job id
64    */
65   String submitJackhmmerSearch(String input, String algo, String db,
66           int niter)
67   {
68     JSONObject inparam = new JSONObject();
69     HttpPost jackhp = new HttpPost(baseUrl + jackH);
70     String lastiter = null;
71     try
72     {
73       inparam.put("algo", algo);
74       inparam.put("seq", input);
75       inparam.put("seqdb", db);
76       inparam.put("iterations", niter);
77       // #Now POST the request and generate the search job.
78       // dumb json post service
79       jackhp.setHeader("content-type", "application/json");
80       jackhp.setEntity(new StringEntity(inparam.toString()));
81     } catch (Exception f)
82     {
83       f.printStackTrace();
84       return null;
85     }
86     HttpResponse r = null;
87     try
88     {
89       DefaultHttpClient httpCl = new DefaultHttpClient();
90
91       r = httpCl.execute(jackhp);
92
93     } catch (Exception x)
94     {
95       System.err.println("Submit failed.");
96       x.printStackTrace();
97     }
98     if (r.getStatusLine().getStatusCode() != 201)
99     {
100       throw new Error(r.toString());
101     }
102     // get uid for job
103     String jobid = null, redir = null;
104     try
105     {
106       JSONObject res = new JSONObject(EntityUtils.toString(r.getEntity()));
107       jobid = res.getString("job_id");
108
109       Header[] loc;
110       if ((loc = r.getHeaders(HTTPConstants.HEADER_LOCATION)) != null
111               && loc.length > 0)
112       {
113         if (loc.length > 1)
114         {
115           System.err
116                   .println("Ignoring additional "
117                           + (loc.length - 1)
118                           + " location(s) provided in response header ( next one is '"
119                           + loc[1].getValue() + "' )");
120         }
121         redir = loc[0].getValue();
122       }
123     } catch (Exception x)
124     {
125       System.err.println("job id extraction failed.");
126       x.printStackTrace();
127     }
128     int tries = 0;
129     boolean finished = false;
130     JSONObject jobstate = null;
131     do
132     {
133       try
134       {
135         DefaultHttpClient httpCl = new DefaultHttpClient();
136
137         HttpGet jackcheck = new HttpGet(redir);
138         jackcheck.setHeader("content-type", "application/json");
139         r = httpCl.execute(jackcheck);
140         switch (r.getStatusLine().getStatusCode())
141         {
142         case 200:
143           jobstate = new JSONObject(EntityUtils.toString(r.getEntity()));
144           String st = jobstate.getString("status");
145           if ("DONE".equals(st))
146           {
147             finished = true;
148           }
149           if ("ERROR".equals(st))
150           {
151             System.err.println("Error");
152             finished = true;
153           }
154           if ("PEND".equals(st) || "RUN".equals("st"))
155           {
156             JSONArray iters = jobstate.getJSONArray("result");
157             lastiter = iters.getJSONObject(iters.length() - 1)
158                     .getString("uuid");
159             if (lastiter.length() > 0)
160             {
161               java.util.regex.Pattern p = java.util.regex.Pattern
162                       .compile(".+(\\d+)");
163               Matcher m = p.matcher(lastiter);
164               if (m.matches())
165               {
166                 System.out.println("On iteration " + m.group(1));
167               }
168             }
169           }
170           break;
171
172         default:
173           tries++;
174           Thread.sleep(2000);
175         }
176       } catch (Exception q)
177       {
178         q.printStackTrace();
179         return null;
180       }
181     } while (!finished && tries < 50);
182
183     if (!finished)
184     {
185       System.err.println("Giving up with job " + jobid + " at " + redir);
186       return null;
187     }
188     // get results
189     // http://www.ebi.ac.uk/Tools/hmmer/download/60048B38-7CEC-11E5-A230-CED6D26C98AD.5/score?format=csv
190     // 1gri_A 1gri_A 217 jackhmmer - 163 4.7e-62 212.4 0.1 1 2 4.4e-46 2.1e-43
191     // 151.758316040039 0.04 11 151 3 139 1 150 0.94 GROWTH FACTOR BOUND PROTEIN
192     // 2 1cj1_J 1gri_B
193     // 1gri_A 1gri_A 217 jackhmmer - 163 4.7e-62 212.4 0.1 2 2 1.6e-17 7.9e-15
194     // 58.8796501159668 0.01 7 66 157 215 153 216 0.95 GROWTH FACTOR BOUND
195     // PROTEIN 2 1cj1_J 1gri_B
196     // 4h1o_A 4h1o_A 560 jackhmmer - 163 2.1e-57 197.3 0.0 1 2 7.5e-28 3.6e-25
197     // 92.4921493530273 0.00 65 161 20 122 17 124 0.95 Tyrosine-protein
198     // phosphatase non-receptor typ 4h1o_A
199     //
200     // 4h1o_A 4h1o_A 560 jackhmmer - 163 2.1e-57 197.3 0.0 2 2 7.6e-31 3.7e-28
201     // 102.219146728516 0.03 66 161 127 236 124 238 0.94 Tyrosine-protein
202     // phosphatase non-receptor typ 4h1o_A
203     //
204     // $ua->get( $rootUrl."/results/".$lastIteration->{uuid} . "/score"
205     return lastiter;
206     /*
207      *  * #Job should have finished, but we may have converged, so get the last
208      * job. my $results = $json->decode( $response->content ); my $lastIteration
209      * = pop( @{ $results->{result} } ); #Now fetch the results of the last
210      * iteration my $searchResult = $ua->get( $rootUrl."/results/" .
211      * $lastIteration->{uuid} . "/score", 'Accept' => 'application/json' );
212      * unless( $searchResult->status_line eq "200 OK"){ die
213      * "Failed to get search results\n"; }
214      * 
215      * #Decode the content of the full set of results $results = $json->decode(
216      * $searchResult->content ); print
217      * "Matched ".$results->{'results'}->{'stats'}->{'nincluded'}." sequences
218      * ($lastIteration->{uuid})!\n"; #Now do something more interesting with the
219      * results......
220      */
221   }
222 }