3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.AlignmentOrder;
7 import jalview.datamodel.AlignmentView;
8 import jalview.datamodel.HiddenColumns;
9 import jalview.datamodel.HiddenMarkovModel;
10 import jalview.datamodel.SequenceI;
11 import jalview.gui.AlignFrame;
12 import jalview.gui.Desktop;
13 import jalview.gui.JvOptionPane;
14 import jalview.gui.Preferences;
15 import jalview.gui.SplitFrame;
16 import jalview.io.DataSourceType;
17 import jalview.io.StockholmFile;
18 import jalview.util.MessageManager;
19 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
20 import jalview.ws.params.ArgumentI;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
28 import javax.swing.JInternalFrame;
30 public class HMMAlignThread implements Runnable
34 * feature settings from view that job was associated with
36 protected FeatureRendererSettings featureSettings = null;
39 * Object containing frequently used commands.
41 HMMERCommands cmds = new HMMERCommands();
49 List<AlignmentOrder> orders;
53 HiddenMarkovModel hmm;
61 Map<Integer, SequenceI> hmmSeqs;
67 File inputTemp = null;
69 List<AlignmentOrder> allOrders;
71 SequenceI[][] allResults;
74 * Constructor for the HMMAlignThread. If create new frame is set to true, a
75 * new frame will be created.
78 * @param createNewFrame
80 public HMMAlignThread(AlignFrame af, boolean createNewFrame,
84 alignment = af.getViewport().getAlignment();
85 if (alignment.getDataset() != null)
87 dataset = alignment.getDataset();
89 newFrame = createNewFrame;
90 featureSettings = af.getFeatureRenderer().getSettings();
95 * Runs the HMMAlignThread: the data on the alignment or group is exported,
96 * then the command is executed in the command line and then the data is
97 * imported and displayed in a new frame (if true). The command is executed
98 * for each segemtn of the alignment.
104 hmm = af.getSelectedHMM();
106 barID = System.currentTimeMillis();
107 af.setProgressBar(MessageManager.getString("status.running_hmmalign"),
109 cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
111 SequenceI[][] subAlignments = msa.getVisibleContigs('-');
112 allOrders = new ArrayList<>();
113 allResults = new SequenceI[subAlignments.length][];
115 for (SequenceI[] seqs : subAlignments)
117 cmds.uniquifySequences(seqs);
120 createTemporaryFiles();
121 } catch (IOException e2)
123 e2.printStackTrace();
127 cmds.exportData(seqs, outTemp.getAbsoluteFile(), hmm,
128 hmmTemp.getAbsoluteFile());
129 } catch (IOException e1)
131 e1.printStackTrace();
135 boolean ran = runCommand();
138 JvOptionPane.showInternalMessageDialog(af,
139 MessageManager.getString("warn.hmmalign_failed"));
142 } catch (IOException | InterruptedException e)
149 } catch (IOException | InterruptedException e)
151 // TODO Auto-generated catch block
158 displayResults(newFrame);
160 af.setProgressBar(MessageManager.getString("status.running_hmmalign"),
166 * Creates temporary files for exporting and importing the data.
168 * @throws IOException
170 private void createTemporaryFiles() throws IOException
174 hmmTemp = File.createTempFile("hmm", ".hmm");
175 hmmTemp.deleteOnExit();
179 outTemp = File.createTempFile("output", ".sto");
180 outTemp.deleteOnExit();
182 inputTemp = File.createTempFile("input", ".sto");
183 inputTemp.deleteOnExit();
187 * Executes the hmmalign command in the command line.
190 * @throws IOException
191 * @throws InterruptedException
193 private boolean runCommand() throws IOException, InterruptedException
195 File file = new File(cmds.HMMERFOLDER + "/hmmalign");
196 if (!file.canExecute())
198 file = new File(cmds.HMMERFOLDER + "/hmmalign.exe");
200 if (!file.canExecute())
206 String command = cmds.HMMERFOLDER + cmds.HMMALIGN;
207 String version = Cache.getProperty("HMMER_VERSION");
208 if (!"3.1b2".equals(version))
210 command += cmds.ALLCOL;
214 for (ArgumentI arg : args)
216 String name = arg.getName();
219 case "Trim Non-Matching Termini":
224 command += " -o " + inputTemp.getAbsolutePath() + cmds.SPACE
225 + hmmTemp.getAbsolutePath() + cmds.SPACE
226 + outTemp.getAbsolutePath();
227 return cmds.runCommand(command);
231 * Imports the data from the temporary file to which the output of hmmalign is
232 * directed. this is used for an internal job.
235 * The index of the 'job' (or region of an alignment).
236 * @throws IOException
237 * @throws InterruptedException
239 private void importData(int index)
240 throws IOException, InterruptedException
242 StockholmFile file = new StockholmFile(inputTemp.getAbsolutePath(),
243 DataSourceType.FILE);
244 SequenceI[] result = file.getSeqsAsArray();
245 AlignmentOrder msaorder = new AlignmentOrder(result);
246 jalview.analysis.AlignmentSorter.recoverOrder(result);
247 jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, result);
248 allOrders.add(msaorder);
249 allResults[index] = result;
256 * Gathers the sequences in preparation for the alignment.
258 private void prepareAlignment()
260 // hmmSeqs = alignment.getHMMConsensusSequences(true);
261 msa = af.gatherSequencesForAlignment();
265 * Displays the results of all 'jobs'.
269 private void displayResults(boolean newFrame)
271 AlignmentOrder[] arrOrders = allOrders
272 .toArray(new AlignmentOrder[allOrders.size()]);
273 Object[] newview = msa.getUpdatedView(allResults,
275 SequenceI[] alignment = (SequenceI[]) newview[0];
276 HiddenColumns hidden = (HiddenColumns) newview[1];
277 Alignment al = new Alignment(alignment);
278 al.setProperty("Alignment Program", "hmmalign");
281 al.setDataset(dataset);
286 displayInNewFrame(al, allOrders, hidden);
291 * Displays the results in a new frame.
294 * The alignment containing the results.
296 * The order of the sequences in the alignment on which the jobs were
299 * Hidden columns in the previous alignment.
301 private void displayInNewFrame(AlignmentI al,
302 List<AlignmentOrder> alorders, HiddenColumns hidden)
304 AlignFrame af = new AlignFrame(al, hidden, AlignFrame.DEFAULT_WIDTH,
305 AlignFrame.DEFAULT_HEIGHT);
307 // initialise with same renderer settings as in parent alignframe.
308 af.getFeatureRenderer().transferSettings(this.featureSettings);
310 if (allOrders.size() > 0)
312 addSortByMenuItems(af, allOrders);
315 // TODO: refactor retrieve and show as new splitFrame as Desktop method
318 * If alignment was requested from one half of a SplitFrame, show in a
319 * SplitFrame with the other pane similarly aligned.
321 AlignFrame requestedBy = this.af;
322 if (requestedBy != null && requestedBy.getSplitViewContainer() != null
323 && requestedBy.getSplitViewContainer()
324 .getComplement(requestedBy) != null)
326 AlignmentI complement = requestedBy.getSplitViewContainer()
327 .getComplement(requestedBy);
328 String complementTitle = requestedBy.getSplitViewContainer()
329 .getComplementTitle(requestedBy);
330 // becomes null if the alignment window was closed before the alignment
332 AlignmentI copyComplement = new Alignment(complement);
333 // todo should this be done by copy constructor?
334 copyComplement.setGapCharacter(complement.getGapCharacter());
335 // share the same dataset (and the mappings it holds)
336 copyComplement.setDataset(complement.getDataset());
337 copyComplement.alignAs(al);
338 if (copyComplement.getHeight() > 0)
340 af.setTitle(this.af.getTitle());
341 AlignFrame af2 = new AlignFrame(copyComplement,
342 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
343 af2.setTitle(complementTitle);
344 String linkedTitle = MessageManager
345 .getString("label.linked_view_title");
346 JInternalFrame splitFrame = new SplitFrame(
347 al.isNucleotide() ? af : af2, al.isNucleotide() ? af2 : af);
348 Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
354 * Not from SplitFrame, or failed to created a complementary alignment
356 Desktop.addInternalFrame(af, af.getTitle(), AlignFrame.DEFAULT_WIDTH,
357 AlignFrame.DEFAULT_HEIGHT);
361 * Add sort order options to the AlignFrame menus.
366 protected void addSortByMenuItems(AlignFrame af,
367 List<AlignmentOrder> alorders)
370 if (alorders.size() == 1)
372 af.addSortByOrderMenuItem("hmmalign" + " Ordering", alorders.get(0));
376 // construct a non-redundant ordering set
377 List<String> names = new ArrayList<>();
378 for (int i = 0, l = alorders.size(); i < l; i++)
380 String orderName = " Region " + i;
385 if (alorders.get(i).equals(alorders.get(j)))
389 orderName += "," + j;
397 if (i == 0 && j == 1)
403 names.add(orderName);
406 for (int i = 0, l = alorders.size(); i < l; i++)
408 af.addSortByOrderMenuItem("hmmalign" + (names.get(i)) + " Ordering",
415 * Runs hmmalign, and waits for the results to be imported before continuing
417 public void hmmalignWaitTillComplete()
419 Thread loader = new Thread(this);
422 while (loader.isAlive())
427 } catch (Exception ex)