1 package jalview.ws.jws1;
3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.AlignmentView;
7 import jalview.datamodel.ColumnSelection;
8 import jalview.datamodel.SequenceI;
9 import jalview.io.FileParse;
10 import jalview.io.FormatAdapter;
11 import jalview.util.MessageManager;
13 import java.io.IOException;
14 import java.util.Hashtable;
15 import java.util.List;
19 * extraction of processing routines to allow mocking
24 public class JPredWSUtils
27 * Process data extracted from service result set to generate a JPred result
33 * - original input alignment
35 * - character to use for reconstructing alignment used for
40 * - true if a prediction based on existing MSA
43 * @param result_PredFile
44 * @param result_Aligfile
45 * @param full_alignment
46 * @return { Alignment, ColumnSelection }
49 public static Object[] processJnetResult(AlignmentI currentView,
51 char gapChar, Hashtable SequenceInfo,
52 boolean msaPred, int[] predMap, String result_PredFile,
53 String result_Aligfile, FileParse full_alignment)
58 ColumnSelection alcsel = null;
60 // the position of the query sequence in Alignment al
63 // the position of the original sequence in the array of
64 // Sequences in the input object that this job holds a
68 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt",
70 jalview.io.JPredFile prediction = new jalview.io.JPredFile(
71 result_PredFile, "Paste");
72 SequenceI[] preds = prediction.getSeqsAsArray();
73 jalview.bin.Cache.log.debug("Got prediction profile.");
75 if (msaPred && (result_Aligfile != null))
77 jalview.bin.Cache.log.debug("Getting associated alignment.");
78 // we ignore the returned alignment if we only predicted on a single
80 String format = new jalview.io.IdentifyFile().identify(
81 result_Aligfile, "Paste");
83 if (jalview.io.FormatAdapter.isValidFormat(format))
88 Object[] alandcolsel = input
89 .getAlignmentAndColumnSelection(gapChar);
90 sqs = (SequenceI[]) alandcolsel[0];
91 al = new Alignment(sqs);
92 alcsel = (ColumnSelection) alandcolsel[1];
96 al = new FormatAdapter().readFile(result_Aligfile, "Paste",
98 sqs = new SequenceI[al.getHeight()];
100 for (int i = 0, j = al.getHeight(); i < j; i++)
102 sqs[i] = al.getSequenceAt(i);
104 if (!jalview.analysis.SeqsetUtils.deuniquify(SequenceInfo, sqs))
106 throw (new Exception(
108 .getString("exception.couldnt_recover_sequence_properties_for_alignment")));
112 if (currentView.getDataset() != null)
114 al.setDataset(currentView.getDataset());
121 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
122 FirstSeq, false, predMap);
127 throw (new Exception(MessageManager.formatMessage(
128 "exception.unknown_format_for_file", new String[] { format,
129 result_Aligfile })));
134 AlignmentI fullAlignment = null;
137 // read full alignment if present.
138 if (!msaPred && full_alignment != null)
140 fullAlignment = new FormatAdapter().readFromFile(full_alignment,
143 } catch (IOException q)
148 if (fullAlignment != null)
155 al = new Alignment(preds);
156 FirstSeq = prediction.getQuerySeqPosition();
162 // map the prediction onto the query sequence, excluding positions
163 // corresponding to hidden regions in the original input.
165 SequenceI[] sqs = (SequenceI[]) input
166 .getAlignmentAndColumnSelection(gc)[0];
167 if (msaIndex >= sqs.length)
171 .getString("error.implementation_error_invalid_msa_index_for_job"));
173 if (fullAlignment == null)
176 // Uses RemoveGapsCommand
178 new jalview.commands.RemoveGapsCommand(
179 MessageManager.getString("label.remove_gaps"),
180 new SequenceI[] { sqs[msaIndex] }, currentView);
182 SequenceI profileseq = al.getSequenceAt(FirstSeq);
183 profileseq.setSequence(sqs[msaIndex].getSequenceAsString());
187 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
188 al.getSequenceAt(FirstSeq), SequenceInfo))
190 throw (new Exception(
192 .getString("exception.couldnt_recover_sequence_props_for_jnet_query")));
196 if (currentView.getDataset() != null)
198 al.setDataset(currentView.getDataset());
205 if (fullAlignment != null)
207 // map gapMap from positions in visible sequence to positions in
214 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
215 FirstSeq, true, predMap);
216 SequenceI profileseq = al.getSequenceAt(0); // this includes any gaps.
217 if (fullAlignment == null)
219 alignToProfileSeq(al, profileseq);
221 if (fullAlignment == null && predMap != null)
223 // Adjust input view for gaps
224 // propagate insertions into profile
225 alcsel = ColumnSelection.propagateInsertions(profileseq, al,
231 // transfer to dataset
232 for (AlignmentAnnotation alant : al.getAlignmentAnnotation())
234 if (alant.sequenceRef != null)
236 replaceAnnotationOnAlignmentWith(alant, alant.label,
237 "jalview.jws1.Jpred" + (msaPred ? "MSA" : ""),
242 return new Object[] { al, alcsel }; // , FirstSeq, noMsa};
246 * copied from JabawsCalcWorker
253 public static void replaceAnnotationOnAlignmentWith(
254 AlignmentAnnotation newAnnot, String typeName, String calcId,
257 SequenceI dsseq = aSeq.getDatasetSequence();
258 while (dsseq.getDatasetSequence() != null)
260 dsseq = dsseq.getDatasetSequence();
262 // look for same annotation on dataset and lift this one over
263 List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
265 if (dsan != null && dsan.size() > 0)
267 for (AlignmentAnnotation dssan : dsan)
269 dsseq.removeAlignmentAnnotation(dssan);
272 AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
273 dsseq.addAlignmentAnnotation(dssan);
274 dssan.adjustForAlignment();
278 * Given an alignment where all other sequences except profileseq are aligned
279 * to the ungapped profileseq, insert gaps in the other sequences to realign
280 * them with the residues in profileseq
285 public static void alignToProfileSeq(AlignmentI al, SequenceI profileseq)
287 char gc = al.getGapCharacter();
288 int[] gapMap = profileseq.gapMap();
289 insertGapsInto(al, gc, gapMap);
293 * Given an original sequence, and an alignment involving just the visible
294 * region insert gaps into the alignment and add in the missing residues from
295 * the original sequence
301 public static void insertHiddenResidues(AlignmentI al, char gc,
305 // orig: asdfPPPPPPPasdfPPPPasdf
307 // al: -----P-P-P---P---P----P---P-P--PP---P---
308 // s1: SSSSSSS-SS---S---SSSSSS---S-S--SSSSSSSSS
309 // s2: SSSSSSS-SSSSSSSSSSS----SSS-S-SSS-----SSS
313 // al: asdf-----P-P-P---P---P----P---Pasdf-P--PP---P---asdf
314 // s1: ....SSSSSSS-SS---S---SSSSSS---S....-S--SSSSSSSSS....
315 // s2: ....SSSSSSS-SSSSSSSSSSS----SSS-....S-SSS-----SSS....
318 SequenceI predseq = al.getSequenceAt(0);
319 int predIdx = 0; // next column of prediction to preserve
320 // positions in original and prediction sequence
321 int lp = origseq.getStart(), predPos = predseq.getStart();
322 for (int r = 0; r < predMap.length; r++)
324 // also need to keep track of trimmed prediction sequence numbering
325 if (predMap[r] - lp > 1)
327 // hidden region insert from origseq
328 String insert = origseq.getSequenceAsString(
329 origseq.findIndex(lp) - 1,
330 origseq.findIndex(predMap[r]) - 1);
332 insertGapsAt(al, gc, alseq.length(), insert.length());
335 // Now update prediction sequence for next position.
337 int predIdxNext = predseq.findIndex(predPos + 1) - 1;
338 if (predIdxNext <= predIdx)
340 predIdxNext = predseq.getLength();
342 // just add in next segment of predseq
343 String predsert = predseq.getSequenceAsString(predIdx, predIdxNext);
345 predIdx = predIdxNext;
351 if (lp < origseq.getEnd())
353 String insert = origseq.getSequenceAsString(
354 origseq.findIndex(lp) - 1, origseq.getLength());
355 insertGapsAt(al, gc, alseq.length(), insert.length());
358 // then add in origseq data.
359 predseq.setSequence(alseq);
362 public static void insertGapsInto(AlignmentI al, char gc, int[] gapMap)
364 // insert gaps into profile
365 for (int lp = 0, r = 0; r < gapMap.length; r++)
367 if (gapMap[r] - lp > 1)
369 insertGapsAt(al, gc, gapMap[r], gapMap[r]-lp);
375 private static void insertGapsAt(AlignmentI al, char gc, int i, int lp)
378 StringBuffer sb = new StringBuffer();
379 for (int s = 0, ns = lp; s < ns; s++)
383 for (int s = 1, ns = al.getHeight(); s < ns; s++)
385 String sq = al.getSequenceAt(s).getSequenceAsString();
386 int diff = i - sq.length();
391 while ((diff = i - sq.length()) > 0)
394 + ((diff >= sb.length()) ? sb.toString() : sb.substring(
397 al.getSequenceAt(s).setSequence(sq);
401 al.getSequenceAt(s).setSequence(
402 sq.substring(0, i) + sb.toString() + sq.substring(i));