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.util.Hashtable;
14 import java.util.List;
18 * extraction of processing routines to allow mocking
23 public class JPredWSUtils
26 * Process data extracted from service result set to generate a JPred result
32 * - original input alignment
34 * - character to use for reconstructing alignment used for
39 * - true if a prediction based on existing MSA
42 * @param result_PredFile
43 * @param result_Aligfile
44 * @param full_alignment
45 * @return { Alignment, ColumnSelection }
48 public static Object[] processJnetResult(AlignmentI currentView,
50 char gapChar, Hashtable SequenceInfo,
51 boolean msaPred, int[] predMap, String result_PredFile,
52 String result_Aligfile, FileParse full_alignment)
57 ColumnSelection alcsel = null;
59 // the position of the query sequence in Alignment al
62 // the position of the original sequence in the array of
63 // Sequences in the input object that this job holds a
67 // JPredFile prediction = new JPredFile("C:/JalviewX/files/jpred.txt",
69 jalview.io.JPredFile prediction = new jalview.io.JPredFile(
70 result_PredFile, "Paste");
71 SequenceI[] preds = prediction.getSeqsAsArray();
72 jalview.bin.Cache.log.debug("Got prediction profile.");
74 if (msaPred && (result_Aligfile != null))
76 jalview.bin.Cache.log.debug("Getting associated alignment.");
77 // we ignore the returned alignment if we only predicted on a single
79 String format = new jalview.io.IdentifyFile().identify(
80 result_Aligfile, "Paste");
82 if (jalview.io.FormatAdapter.isValidFormat(format))
87 Object[] alandcolsel = input
88 .getAlignmentAndColumnSelection(gapChar);
89 sqs = (SequenceI[]) alandcolsel[0];
90 al = new Alignment(sqs);
91 alcsel = (ColumnSelection) alandcolsel[1];
95 al = new FormatAdapter().readFile(result_Aligfile, "Paste",
97 sqs = new SequenceI[al.getHeight()];
99 for (int i = 0, j = al.getHeight(); i < j; i++)
101 sqs[i] = al.getSequenceAt(i);
103 if (!jalview.analysis.SeqsetUtils.deuniquify(SequenceInfo, sqs))
105 throw (new Exception(
107 .getString("exception.couldnt_recover_sequence_properties_for_alignment")));
111 if (currentView.getDataset() != null)
113 al.setDataset(currentView.getDataset());
120 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
121 FirstSeq, false, predMap);
126 throw (new Exception(MessageManager.formatMessage(
127 "exception.unknown_format_for_file", new String[] { format,
128 result_Aligfile })));
133 AlignmentI fullAlignment = null;
136 // read full alignment if present.
137 if (full_alignment != null)
139 fullAlignment = new FormatAdapter().readFromFile(full_alignment,
142 } catch (Exception q)
147 if (fullAlignment != null)
154 al = new Alignment(preds);
155 FirstSeq = prediction.getQuerySeqPosition();
161 // map the prediction onto the query sequence, excluding positions
162 // corresponding to hidden regions in the original input.
164 SequenceI[] sqs = (SequenceI[]) input
165 .getAlignmentAndColumnSelection(gc)[0];
166 if (msaIndex >= sqs.length)
170 .getString("error.implementation_error_invalid_msa_index_for_job"));
172 if (fullAlignment == null)
175 // Uses RemoveGapsCommand
177 new jalview.commands.RemoveGapsCommand(
178 MessageManager.getString("label.remove_gaps"),
179 new SequenceI[] { sqs[msaIndex] }, currentView);
181 SequenceI profileseq = al.getSequenceAt(FirstSeq);
182 profileseq.setSequence(sqs[msaIndex].getSequenceAsString());
186 if (!jalview.analysis.SeqsetUtils.SeqCharacterUnhash(
187 al.getSequenceAt(FirstSeq), SequenceInfo))
189 throw (new Exception(
191 .getString("exception.couldnt_recover_sequence_props_for_jnet_query")));
195 if (currentView.getDataset() != null)
197 al.setDataset(currentView.getDataset());
204 jalview.io.JnetAnnotationMaker.add_annotation(prediction, al,
205 FirstSeq, true, predMap);
206 SequenceI profileseq = al.getSequenceAt(0); // this includes any gaps.
207 if (fullAlignment == null)
209 alignToProfileSeq(al, profileseq);
211 if (fullAlignment == null && predMap != null)
213 // Adjust input view for gaps
214 // propagate insertions into profile
215 alcsel = ColumnSelection.propagateInsertions(profileseq, al,
221 // transfer to dataset
222 for (AlignmentAnnotation alant : al.getAlignmentAnnotation())
224 if (alant.sequenceRef != null)
226 replaceAnnotationOnAlignmentWith(alant, alant.label,
227 "jalview.jws1.Jpred" + (msaPred ? "MSA" : ""),
232 return new Object[] { al, alcsel }; // , FirstSeq, noMsa};
236 * copied from JabawsCalcWorker
243 public static void replaceAnnotationOnAlignmentWith(
244 AlignmentAnnotation newAnnot, String typeName, String calcId,
247 SequenceI dsseq = aSeq.getDatasetSequence();
248 while (dsseq.getDatasetSequence() != null)
250 dsseq = dsseq.getDatasetSequence();
252 // look for same annotation on dataset and lift this one over
253 List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
255 if (dsan != null && dsan.size() > 0)
257 for (AlignmentAnnotation dssan : dsan)
259 dsseq.removeAlignmentAnnotation(dssan);
262 AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
263 dsseq.addAlignmentAnnotation(dssan);
264 dssan.adjustForAlignment();
268 * Given an alignment where all other sequences except profileseq are aligned
269 * to the ungapped profileseq, insert gaps in the other sequences to realign
270 * them with the residues in profileseq
275 public static void alignToProfileSeq(AlignmentI al, SequenceI profileseq)
277 char gc = al.getGapCharacter();
278 int[] gapMap = profileseq.gapMap();
279 // insert gaps into profile
280 for (int lp = 0, r = 0; r < gapMap.length; r++)
282 if (gapMap[r] - lp > 1)
284 StringBuffer sb = new StringBuffer();
285 for (int s = 0, ns = gapMap[r] - lp; s < ns; s++)
289 for (int s = 1, ns = al.getHeight(); s < ns; s++)
291 String sq = al.getSequenceAt(s).getSequenceAsString();
292 int diff = gapMap[r] - sq.length();
297 while ((diff = gapMap[r] - sq.length()) > 0)
300 + ((diff >= sb.length()) ? sb.toString() : sb
301 .substring(0, diff));
303 al.getSequenceAt(s).setSequence(sq);
307 al.getSequenceAt(s).setSequence(
308 sq.substring(0, gapMap[r]) + sb.toString()
309 + sq.substring(gapMap[r]));