ed6d4affa6901038ebaa49c133b6eaec1c3c3e47
[jalview.git] / src / jalview / ws / JPredThread.java
1 package jalview.ws;
2
3 import static java.lang.String.format;
4
5 import java.util.Hashtable;
6 import java.util.List;
7
8 import jalview.bin.Cache;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.AlignmentView;
12 import jalview.datamodel.HiddenColumns;
13 import jalview.datamodel.SequenceI;
14 import jalview.gui.AlignFrame;
15 import jalview.gui.Desktop;
16 import jalview.gui.WebserviceInfo;
17 import jalview.util.MessageManager;
18 import jalview.ws.api.CancellableI;
19 import jalview.ws.api.JPredMutlipleAlignmentServiceI;
20 import jalview.ws.gui.WsJob;
21 import jalview.ws.gui.WsJob.JobState;
22
23
24 class JPredJob extends WsJob
25 {
26   Hashtable<?, ?> sequenceInfo;
27   List<SequenceI> msf;
28   int[] delMap;
29   public AlignmentI alignment;
30   
31   public JPredJob(Hashtable<?, ?> sequenceInfo, SequenceI[] msf, int[] delMap)
32   {
33     this.sequenceInfo = sequenceInfo;
34     this.msf = List.of(msf);
35     this.delMap = delMap;
36   }
37   
38   @Override
39   public boolean hasValidInput()
40   {
41     return true;
42   }
43 }
44
45
46 public class JPredThread extends AWSThread implements WSClientI
47 {
48   
49   private JPredMutlipleAlignmentServiceI server;
50   private String title;
51   private Hashtable<?, ?> sequenceInfo;
52   private SequenceI[] msf;
53   private int[] delMap;  
54   
55   public JPredThread(WebserviceInfo wsInfo, String title,
56       JPredMutlipleAlignmentServiceI server, Hashtable<?, ?> sequenceInfo,
57       SequenceI[] msf, int[] delMap, AlignmentView view, AlignFrame frame,
58       String wsURL)
59   {
60     super(frame, wsInfo, view, wsURL);
61     this.server = server;
62     this.title = title;
63     this.sequenceInfo = sequenceInfo;
64     this.msf = msf;
65     this.delMap = delMap;
66     JPredJob job = new JPredJob(sequenceInfo, msf, delMap);
67     this.jobs = new JPredJob[] { job };
68   }
69
70   @Override
71   public boolean isCancellable()
72   {
73     return server instanceof CancellableI;
74   }
75
76   @Override
77   public boolean canMergeResults()
78   {
79     return false;
80   }
81
82   @Override
83   public void cancelJob()
84   {
85     // TODO Auto-generated method stub
86     
87   }
88
89   @Override
90   public void pollJob(AWsJob job_) throws Exception
91   {
92     var job = (JPredJob) job_;
93     server.updateStatus(job);
94     server.updateJobProgress(job);
95   }
96
97   @Override
98   public void StartJob(AWsJob job_)
99   {
100     if (!(job_ instanceof JPredJob))
101       throw new RuntimeException("Invalid job type");
102     var job = (JPredJob) job_;
103     if (job.isSubmitted())
104     {
105       return;
106     }
107     try {
108       try
109       {
110         var jobHandle = server.align(job.msf);
111         if (jobHandle != null)
112           job.setJobHandle(jobHandle);
113       } 
114       catch (Throwable th) {
115         if (!server.handleSubmitError(th, job, wsInfo)) {
116           throw th;
117         }
118       }
119       if (job.getJobId() != null) {
120         job.setSubmitted(true);
121         job.setSubjobComplete(false);
122         return;
123       }
124       else {
125         throw new Exception(MessageManager.formatMessage(
126                 "exception.web_service_returned_null_try_later",
127                 new String[]
128                 { WsUrl }));
129       }
130     }
131     catch (Throwable th)
132     {
133       // For unexpected errors
134       System.err.println(WebServiceName
135               + "Client: Failed to submit the sequences for alignment (probably a server side problem)\n"
136               + "When contacting Server:" + WsUrl + "\n");
137       th.printStackTrace(System.err);
138       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
139       wsInfo.setStatus(job.getJobnum(),
140               WebserviceInfo.STATE_STOPPED_SERVERERROR);
141       
142     }
143     finally
144     {
145       if (!job.isSubmitted())
146       {
147         job.setAllowedServerExceptions(0);
148         wsInfo.appendProgressText(job.getJobnum(), MessageManager.getString(
149                 "info.failed_to_submit_sequences_for_alignment"));
150       }
151     }
152   }
153
154   @Override
155   public void parseResult()
156   {
157     long progbar = (long) (Math.random() * ~(1L << 63));
158     wsInfo.setProgressBar(
159         MessageManager.getString("status.collecting_job_results"), progbar);
160     int results = 0;
161     var finalState = new JobStateSummary();
162     try
163     {
164       for (int i = 0; i < jobs.length; i++) {
165         final var job = (JPredJob) jobs[i];
166         finalState.updateJobPanelState(wsInfo, OutputHeader, job);
167         if (job.isFinished()) {
168           try {
169             server.updateJobProgress(job);
170           }
171           catch (Exception e) {
172             Cache.log.warn(format(
173                 "Exception when retrieving remaining Job progress data " +
174                 "for job %s on server %s", job.getJobId(), WsUrl));
175             e.printStackTrace();
176           }
177           // removed the waiting loop
178           Cache.log.debug(format("Job Execution file for job: %s " +
179               "on server %s%n%s", job.getJobId(), WsUrl, job.getStatus()));
180           try {
181             job.alignment = server.getResult(job.getJobHandle());
182           }
183           catch (Exception e) {
184             if (!server.handleCollectionException(e, job, wsInfo)) {
185               Cache.log.error("Could not get alignment for job.", e);
186               job.setState(JobState.SERVERERROR);
187             }
188           }
189         }
190         finalState.updateJobPanelState(wsInfo, OutputHeader, job);
191         if (job.isSubmitted() && job.isSubjobComplete() && job.hasResults()) {
192           results++;
193         }
194       }
195     }
196     catch (Exception e) {
197       Cache.log.error(
198           "Unexpected exception when processing results for " + title, e);
199       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
200     }
201     if (results > 0) {
202       wsInfo.showResultsNewFrame.addActionListener(
203           (evt) -> displayResults(true));
204       wsInfo.mergeResults.addActionListener(
205           (evt) -> displayResults(false));
206       wsInfo.setResultsReady();
207     }
208     else {
209       wsInfo.setFinishedNoResults();
210     }
211     updateGlobalStatus(finalState);
212     wsInfo.removeProgressBar(progbar);
213   }
214
215   
216
217   private void displayResults(boolean newWindow)
218   {
219     System.out.println("DISPLAYING THE RESULT");
220   }
221   
222 }