JAL-1601 Create action and task for sec. str. pred. jobs
[jalview.git] / src / jalview / ws2 / actions / secstructpred / SecStructPredTask.java
1 package jalview.ws2.actions.secstructpred;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import jalview.analysis.AlignmentAnnotationUtils;
7 import jalview.api.AlignViewportI;
8 import jalview.bin.Console;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentAnnotation;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.AlignmentView;
13 import jalview.io.AlignFile;
14 import jalview.io.JPredFile;
15 import jalview.io.JnetAnnotationMaker;
16 import jalview.util.Comparison;
17 import jalview.ws.params.ArgumentI;
18 import jalview.ws2.actions.BaseJob;
19 import jalview.ws2.actions.BaseTask;
20 import jalview.ws2.actions.ServiceInputInvalidException;
21 import jalview.ws2.api.Credentials;
22 import jalview.ws2.api.JobStatus;
23 import jalview.ws2.client.api.SecStructPredWebServiceClientI;
24
25 public class SecStructPredTask extends BaseTask<BaseJob, AlignmentI>
26 {
27   private final SecStructPredWebServiceClientI client;
28
29   private final AlignmentView alignmentView;
30
31   private final AlignmentI currentView;
32
33   private final char gapChar;
34
35   SecStructPredTask(SecStructPredWebServiceClientI client, List<ArgumentI> args,
36       Credentials credentials, AlignViewportI viewport)
37   {
38     super(client, args, credentials);
39     this.client = client;
40     this.alignmentView = viewport.getAlignmentView(true);
41     this.currentView = viewport.getAlignment();
42     this.gapChar = viewport.getGapCharacter();
43   }
44
45   @Override
46   protected List<BaseJob> prepareJobs() throws ServiceInputInvalidException
47   {
48     AlignmentI alignment = alignmentView
49         .getVisibleAlignment(Comparison.GAP_DASH);
50     var job = new BaseJob(alignment.getSequences())
51     {
52       @Override
53       public boolean isInputValid()
54       {
55         return true;
56       }
57     };
58     job.setStatus(JobStatus.READY);
59     return List.of(job);
60   }
61
62   @Override
63   protected AlignmentI collectResult(List<BaseJob> jobs) throws IOException
64   {
65     var job = jobs.get(0); // There shouldn't be more than one job
66     var status = job.getStatus();
67     Console
68         .info(String
69             .format("sec str pred job \"%s\" finished with status %s",
70                 job.getServerJob().getJobId(), status));
71     if (status != JobStatus.COMPLETED)
72       return null;
73     JPredFile predictionFile = client.getPredictionFile(job.getServerJob());
74     AlignFile alignmentFile = client.getAlignmentFile(job.getServerJob());
75     // Object[] alnAndHiddenCols =
76     // alignmentView.getAlignmentAndHiddenColumns(gapChar);
77     // SequenceI[] sequences = (SequenceI[]) alnAndHiddenCols[0];
78     var sequences = alignmentFile.getSeqsAsArray();
79     // HiddenColumns hiddenCols = (HiddenColumns) alnAndHiddenCols[1];
80     var aln = new Alignment(sequences);
81     int firstSeq = 0;
82     aln.setDataset(currentView.getDataset());
83     try
84     {
85       JnetAnnotationMaker.add_annotation(predictionFile, aln, firstSeq, false);
86     } catch (Exception e)
87     {
88       throw new IOException(e);
89     }
90
91     for (AlignmentAnnotation alnAnnot : aln.getAlignmentAnnotation())
92     {
93       if (alnAnnot.sequenceRef != null)
94       {
95         AlignmentAnnotationUtils
96             .replaceAnnotationOnAlignmentWith(alnAnnot, alnAnnot.label,
97                 getClass().getSimpleName());
98       }
99     }
100     aln.setSeqrep(aln.getSequenceAt(firstSeq));
101     return aln;
102   }
103
104 }