3 import jalview.analysis.AlignmentSorter;
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.SplitFrame;
15 import jalview.io.DataSourceType;
16 import jalview.io.StockholmFile;
17 import jalview.util.MessageManager;
18 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
19 import jalview.ws.params.ArgumentI;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.swing.JInternalFrame;
28 public class HMMAlignThread extends HmmerCommand implements Runnable
30 static final String HMMALIGN = "hmmalign";
32 static final String ARG_TRIM = "--trim";
35 * feature settings from view that job was associated with
37 protected FeatureRendererSettings featureSettings = null;
43 List<AlignmentOrder> orders;
47 HiddenMarkovModel hmm;
49 SequenceI[][] allResults;
52 * Constructor for the HMMAlignThread
57 public HMMAlignThread(AlignFrame af, List<ArgumentI> args)
60 alignment = af.getViewport().getAlignment();
61 if (alignment.getDataset() != null)
63 dataset = alignment.getDataset();
65 featureSettings = af.getFeatureRenderer().getSettings();
69 * Runs the HMMAlignThread: the data on the alignment or group is exported,
70 * then the command is executed in the command line and then the data is
71 * imported and displayed in a new frame (if true). The command is executed
72 * for each segemtn of the alignment.
77 hmm = af.getSelectedHMM();
79 long msgId = System.currentTimeMillis();
80 af.setProgressBar(MessageManager.getString("status.running_hmmalign"),
83 SequenceI[][] subAlignments = msa.getVisibleContigs('-');
85 List<AlignmentOrder> allOrders = new ArrayList<>();
86 allResults = new SequenceI[subAlignments.length][];
88 for (SequenceI[] seqs : subAlignments)
93 File modelFile = createTempFile("hmm", ".hmm");
94 File alignmentFile = createTempFile("output", ".sto");
95 File resultFile = createTempFile("input", ".sto");
97 exportStockholm(seqs, alignmentFile.getAbsoluteFile(), null);
98 exportHmm(hmm, modelFile.getAbsoluteFile());
100 boolean ran = runCommand(modelFile, alignmentFile, resultFile);
103 JvOptionPane.showInternalMessageDialog(af,
104 MessageManager.getString("warn.hmmalign_failed"));
108 SequenceI[] result = importData(resultFile, allOrders);
109 allResults[job] = result;
111 alignmentFile.delete();
113 } catch (IOException e)
120 displayResults(allOrders);
122 af.setProgressBar("", msgId);
126 * Executes the hmmalign command and returns true if successful, false if an
130 * the HMM to align to
131 * @param alignmentFile
132 * the sequences to align
134 * the file to hold the results of alignment
136 * @throws IOException
138 private boolean runCommand(File modelFile, File alignmentFile,
142 String command = getCommandPath(HMMALIGN);
147 List<String> args = new ArrayList<>();
152 for (ArgumentI arg : params)
154 String name = arg.getName();
155 if (MessageManager.getString("label.trim_termini").equals(name))
162 args.add(resultFile.getAbsolutePath());
163 args.add(modelFile.getAbsolutePath());
164 args.add(alignmentFile.getAbsolutePath());
166 return runCommand(args);
170 * Imports the data from the file holding the output of hmmalign
174 * a list of alignment orders to add to
177 * @throws IOException
179 private SequenceI[] importData(File resultFile,
180 List<AlignmentOrder> allOrders) throws IOException
182 StockholmFile file = new StockholmFile(resultFile.getAbsolutePath(),
183 DataSourceType.FILE);
184 SequenceI[] result = file.getSeqsAsArray();
185 AlignmentOrder msaorder = new AlignmentOrder(result);
186 AlignmentSorter.recoverOrder(result);
187 recoverSequences(result);
188 allOrders.add(msaorder);
194 * Gathers the sequences in preparation for the alignment.
196 private void prepareAlignment()
198 msa = af.gatherSequencesForAlignment();
202 * Displays the results of all 'jobs' in a new frame
206 private void displayResults(List<AlignmentOrder> allOrders)
208 AlignmentOrder[] arrOrders = allOrders
209 .toArray(new AlignmentOrder[allOrders.size()]);
210 Object[] newview = msa.getUpdatedView(allResults, arrOrders, '-');
211 SequenceI[] seqs = (SequenceI[]) newview[0];
212 HiddenColumns hidden = (HiddenColumns) newview[1];
213 Alignment al = new Alignment(seqs);
214 al.setProperty("Alignment Program", "hmmalign");
217 al.setDataset(dataset);
220 displayInNewFrame(al, allOrders, hidden);
224 * Displays the results in a new frame
227 * The alignment containing the results
229 * The order of the sequences in the alignment on which the jobs were
232 * Hidden columns in the previous alignment
234 private void displayInNewFrame(AlignmentI al,
235 List<AlignmentOrder> alorders, HiddenColumns hidden)
237 AlignFrame alignFrame = new AlignFrame(al, hidden, AlignFrame.DEFAULT_WIDTH,
238 AlignFrame.DEFAULT_HEIGHT);
240 // initialise with same renderer settings as in parent alignframe.
241 alignFrame.getFeatureRenderer().transferSettings(this.featureSettings);
243 addSortByMenuItems(alignFrame, alorders);
245 // TODO: refactor retrieve and show as new splitFrame as Desktop method
248 * If alignment was requested from one half of a SplitFrame, show in a
249 * SplitFrame with the other pane similarly aligned.
251 AlignFrame requestedBy = this.af;
252 if (requestedBy != null && requestedBy.getSplitViewContainer() != null
253 && requestedBy.getSplitViewContainer()
254 .getComplement(requestedBy) != null)
256 AlignmentI complement = requestedBy.getSplitViewContainer()
257 .getComplement(requestedBy);
258 String complementTitle = requestedBy.getSplitViewContainer()
259 .getComplementTitle(requestedBy);
260 // becomes null if the alignment window was closed before the alignment
262 AlignmentI copyComplement = new Alignment(complement);
263 // todo should this be done by copy constructor?
264 copyComplement.setGapCharacter(complement.getGapCharacter());
265 // share the same dataset (and the mappings it holds)
266 copyComplement.setDataset(complement.getDataset());
267 copyComplement.alignAs(al);
268 if (copyComplement.getHeight() > 0)
270 alignFrame.setTitle(this.af.getTitle());
271 AlignFrame af2 = new AlignFrame(copyComplement,
272 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
273 af2.setTitle(complementTitle);
274 String linkedTitle = MessageManager
275 .getString("label.linked_view_title");
276 JInternalFrame splitFrame = new SplitFrame(
277 al.isNucleotide() ? alignFrame : af2, al.isNucleotide() ? af2 : alignFrame);
278 Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
284 * Not from SplitFrame, or failed to created a complementary alignment
286 Desktop.addInternalFrame(alignFrame, alignFrame.getTitle(), AlignFrame.DEFAULT_WIDTH,
287 AlignFrame.DEFAULT_HEIGHT);
291 * Adds sort order options to the AlignFrame menus
296 protected void addSortByMenuItems(AlignFrame alignFrame,
297 List<AlignmentOrder> alorders)
300 if (alorders.size() == 1)
302 alignFrame.addSortByOrderMenuItem("hmmalign" + " Ordering", alorders.get(0));
306 // construct a non-redundant ordering set
307 List<String> names = new ArrayList<>();
308 for (int i = 0, l = alorders.size(); i < l; i++)
310 String orderName = " Region " + i;
315 if (alorders.get(i).equals(alorders.get(j)))
319 orderName += "," + j;
327 if (i == 0 && j == 1)
333 names.add(orderName);
336 for (int i = 0, l = alorders.size(); i < l; i++)
338 alignFrame.addSortByOrderMenuItem("hmmalign" + (names.get(i)) + " Ordering",
345 * Runs hmmalign, and waits for the results to be imported before continuing
347 public void hmmalignWaitTillComplete()
349 Thread loader = new Thread(this);
352 while (loader.isAlive())
357 } catch (Exception ex)