JAL-3954 Implement phmmer action and task as alignment actions
[jalview.git] / src / jalview / ws2 / actions / hmmer / PhmmerTask.java
1 package jalview.ws2.actions.hmmer;
2
3 import static jalview.util.Comparison.GapChars;
4
5 import java.io.IOException;
6 import java.util.List;
7
8 import jalview.analysis.AlignSeq;
9 import jalview.bin.Console;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.AlignmentView;
12 import jalview.datamodel.Sequence;
13 import jalview.ws.params.ArgumentI;
14 import jalview.ws2.actions.AbstractPollableTask;
15 import jalview.ws2.actions.BaseJob;
16 import jalview.ws2.actions.ServiceInputInvalidException;
17 import jalview.ws2.actions.api.TaskEventListener;
18 import jalview.ws2.api.Credentials;
19 import jalview.ws2.api.JobStatus;
20 import jalview.ws2.client.api.AlignmentWebServiceClientI;
21
22 class PhmmerTask extends AbstractPollableTask<BaseJob, AlignmentI>
23 {
24   private final AlignmentWebServiceClientI client;
25   private final AlignmentView view;
26
27   PhmmerTask(AlignmentWebServiceClientI client, List<ArgumentI> args,
28           Credentials credentials, AlignmentView view,
29           TaskEventListener<AlignmentI> eventListener)
30   {
31     super(client, args, credentials, eventListener);
32     this.client = client;
33     this.view = view;
34   }
35
36   @Override
37   protected List<BaseJob> prepare() throws ServiceInputInvalidException
38   {
39     Console.info("Preparing sequence for phmmer job");
40     var sequence = view.getVisibleAlignment('-').getSequenceAt(0);
41     var seq = new Sequence(sequence.getName(),
42             AlignSeq.extractGaps(GapChars, sequence.getSequenceAsString()));
43     var job = new BaseJob(List.of(seq))
44     {
45       @Override
46       public boolean isInputValid()
47       {
48         return true;
49       }
50     };
51     job.setStatus(JobStatus.READY);
52     return List.of(job);
53   }
54
55   @Override
56   protected AlignmentI done() throws IOException
57   {
58     var job = getSubJobs().get(0);
59     var jobId = job.getServerJob().getJobId();
60     var status = job.getStatus();
61     Console.info(String.format("phmmer finished job \"%s\" with status %s",
62             jobId, status));
63     return client.getAlignment(job.getServerJob());
64   }
65
66 }