1 package jalview.ws2.actions.alignment;
3 import static java.lang.String.format;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
13 import jalview.analysis.AlignmentSorter;
14 import jalview.analysis.SeqsetUtils;
15 import jalview.analysis.SeqsetUtils.SequenceInfo;
16 import jalview.api.AlignViewportI;
17 import jalview.bin.Cache;
18 import jalview.datamodel.AlignedCodonFrame;
19 import jalview.datamodel.Alignment;
20 import jalview.datamodel.AlignmentI;
21 import jalview.datamodel.AlignmentOrder;
22 import jalview.datamodel.AlignmentView;
23 import jalview.datamodel.HiddenColumns;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceI;
26 import jalview.ws.params.ArgumentI;
27 import jalview.ws2.actions.AbstractPollableTask;
28 import jalview.ws2.actions.ServiceInputInvalidException;
29 import jalview.ws2.actions.api.TaskEventListener;
30 import jalview.ws2.api.Credentials;
31 import jalview.ws2.api.JobStatus;
32 import jalview.ws2.client.api.AlignmentWebServiceClientI;
35 * Implementation of an abstract pollable task used by alignment service
41 class AlignmentTask extends AbstractPollableTask<AlignmentJob, AlignmentResult>
43 /* task parameters set in the constructor */
44 private final AlignmentWebServiceClientI client;
46 private final AlignmentAction action;
48 private final AlignmentView msa; // a.k.a. input
50 private final AlignViewportI viewport;
52 private final boolean submitGaps;
54 private final AlignmentI currentView;
56 private final AlignmentI dataset;
58 private final char gapChar;
60 private final List<AlignedCodonFrame> codonFrame = new ArrayList<>();
62 AlignmentTask(AlignmentWebServiceClientI client, AlignmentAction action,
63 List<ArgumentI> args, Credentials credentials,
64 AlignmentView msa, AlignViewportI viewport, boolean submitGaps,
65 TaskEventListener<AlignmentResult> eventListener)
67 super(client, args, credentials, eventListener);
71 this.viewport = viewport;
72 this.submitGaps = submitGaps;
73 this.currentView = viewport.getAlignment();
74 this.dataset = viewport.getAlignment().getDataset();
75 this.gapChar = viewport.getGapCharacter();
76 List<AlignedCodonFrame> cf = viewport.getAlignment().getCodonFrames();
78 this.codonFrame.addAll(cf);
82 protected List<AlignmentJob> prepare() throws ServiceInputInvalidException
84 Cache.log.info(format("starting alignment service %s:%s",
85 client.getClientName(), action.getName()));
86 SequenceI[][] conmsa = msa.getVisibleContigs(gapChar);
89 throw new ServiceInputInvalidException("no visible contigs for alignment");
91 List<AlignmentJob> jobs = new ArrayList<>(conmsa.length);
92 boolean validInput = false;
93 for (int i = 0; i < conmsa.length; i++)
95 AlignmentJob job = AlignmentJob.create(conmsa[i], 2, submitGaps);
96 validInput |= job.isInputValid(); // at least one input is valid
97 job.setStatus(job.isInputValid() ? JobStatus.READY : JobStatus.INVALID);
103 throw new ServiceInputInvalidException("no valid sequences for alignment");
109 protected AlignmentResult done() throws IOException
111 IOException lastIOE = null;
112 for (AlignmentJob job : jobs)
114 if (job.isInputValid() && job.getStatus() == JobStatus.COMPLETED &&
119 job.setAlignmentResult(client.getAlignment(job.getServerJob()));
120 } catch (IOException e)
127 throw lastIOE; // do not proceed unless all results has been retrieved
129 List<AlignmentOrder> alorders = new ArrayList<>();
130 SequenceI[][] results = new SequenceI[jobs.size()][];
131 AlignmentOrder[] orders = new AlignmentOrder[jobs.size()];
132 for (int i = 0; i < jobs.size(); i++)
134 /* alternative implementation of MsaWSJob#getAlignment */
135 AlignmentJob job = jobs.get(i);
136 if (!job.hasResult())
138 AlignmentI alignment = job.getAlignmentResult();
139 int alnSize = alignment.getSequences().size();
140 char gapChar = alnSize > 0 ? alignment.getGapCharacter() : '-';
141 List<SequenceI> emptySeqs = job.getEmptySequences();
142 List<SequenceI> alnSeqs = new ArrayList<>(alnSize);
143 // create copies of all sequences involved
144 for (SequenceI seq : alignment.getSequences())
146 alnSeqs.add(new Sequence(seq));
148 for (SequenceI seq : emptySeqs)
150 alnSeqs.add(new Sequence(seq));
152 // find the width of the longest sequence
154 for (var seq: alnSeqs)
155 width = Integer.max(width, seq.getLength());
156 // make a sequence of gaps only to cut/paste
159 char[] gaps = new char[width];
160 Arrays.fill(gaps, gapChar);
161 gapSeq = new String(gaps);
163 for (var seq: alnSeqs)
165 if (seq.getLength() < width)
167 // pad sequences shorter than the target width with gaps
168 seq.setSequence(seq.getSequenceAsString()
169 + gapSeq.substring(seq.getLength()));
172 SequenceI[] result = alnSeqs.toArray(new SequenceI[0]);
173 AlignmentOrder msaOrder = new AlignmentOrder(result);
174 AlignmentSorter.recoverOrder(result);
175 Map<String, SequenceInfo> names = new HashMap<>(job.getNames());
176 SeqsetUtils.deuniquify(names, result);
178 alorders.add(msaOrder);
180 orders[i] = msaOrder;
182 Object[] newView = msa.getUpdatedView(results, orders, gapChar);
183 // free references to original data
184 for (int i = 0; i < jobs.size(); i++)
189 SequenceI[] alignment = (SequenceI[]) newView[0];
190 HiddenColumns hidden = (HiddenColumns) newView[1];
191 Alignment aln = new Alignment(alignment);
192 aln.setProperty("Alignment Program", action.getName());
194 aln.setDataset(dataset);
196 propagateDatasetMappings(aln);
197 return new AlignmentResult(aln, alorders, hidden);
201 * Conserve dataset references to sequence objects returned from web services.
202 * Propagate AlignedCodonFrame data from {@code codonFrame} to {@code aln}.
203 * TODO: Refactor to datamodel
205 private void propagateDatasetMappings(AlignmentI aln)
207 if (codonFrame != null)
209 SequenceI[] alignment = aln.getSequencesArray();
210 for (final SequenceI seq : alignment)
212 for (AlignedCodonFrame acf : codonFrame)
214 if (acf != null && acf.involvesSequence(seq))
216 aln.addCodonFrame(acf);