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.FileUtils;
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.Hashtable;
26 import java.util.List;
28 import javax.swing.JInternalFrame;
30 public class HMMAlign extends HmmerCommand
32 static final String HMMALIGN = "hmmalign";
34 static final String ARG_TRIM = "--trim";
36 private final AlignmentI dataset;
39 * Constructor for the HMMAlignThread
44 public HMMAlign(AlignFrame af, List<ArgumentI> args)
47 if (alignment.getDataset() != null)
49 dataset = alignment.getDataset();
58 * Runs the HMMAlignThread: the data on the alignment or group is exported,
59 * then the command is executed in the command line and then the data is
60 * imported and displayed in a new frame (if true). The command is executed
61 * for each segment of the alignment. Call this method directly to execute
62 * synchronously, or via start() in a new Thread for asynchronously.
67 HiddenMarkovModel hmm = af.getSelectedHMM();
70 System.err.println("Can't run hmmalign as no HMM profile selected");
74 long msgId = System.currentTimeMillis();
75 af.setProgressBar(MessageManager.getString("status.running_hmmalign"),
78 AlignmentView msa = af.gatherSequencesForAlignment();
79 SequenceI[][] subAlignments = msa.getVisibleContigs(alignment.getGapCharacter());
81 List<AlignmentOrder> allOrders = new ArrayList<>();
83 SequenceI[][] allResults = new SequenceI[subAlignments.length][];
85 for (SequenceI[] seqs : subAlignments)
87 Hashtable sequencesHash = stashSequences(seqs);
90 File modelFile = FileUtils.createTempFile("hmm", ".hmm");
91 File alignmentFile = FileUtils.createTempFile("output", ".sto");
92 File resultFile = FileUtils.createTempFile("input", ".sto");
94 exportStockholm(seqs, alignmentFile.getAbsoluteFile(), null);
95 exportHmm(hmm, modelFile.getAbsoluteFile());
97 boolean ran = runCommand(modelFile, alignmentFile, resultFile);
100 JvOptionPane.showInternalMessageDialog(af,
101 MessageManager.getString("warn.hmmalign_failed"));
105 SequenceI[] result = importData(resultFile, allOrders);
106 recoverSequences(sequencesHash, result);
107 allResults[job] = result;
109 alignmentFile.delete();
111 } catch (IOException e)
118 String title = "hmmalign to " + hmm.getConsensusSequence().getName();
119 displayResults(allResults, allOrders, msa, title);
121 af.setProgressBar("", msgId);
125 * Executes the hmmalign command and returns true if successful, false if an
129 * the HMM to align to
130 * @param alignmentFile
131 * the sequences to align
133 * the file to hold the results of alignment
135 * @throws IOException
137 private boolean runCommand(File modelFile, File alignmentFile,
138 File resultFile) throws IOException
140 String command = getCommandPath(HMMALIGN);
145 List<String> args = new ArrayList<>();
150 for (ArgumentI arg : params)
152 String name = arg.getName();
153 if (MessageManager.getString("label.trim_termini").equals(name))
160 args.add(resultFile.getAbsolutePath());
161 args.add(modelFile.getAbsolutePath());
162 args.add(alignmentFile.getAbsolutePath());
164 return runCommand(args);
168 * Imports the data from the file holding the output of hmmalign
172 * a list of alignment orders to add to
175 * @throws IOException
177 private SequenceI[] importData(File resultFile,
178 List<AlignmentOrder> allOrders) throws IOException
180 StockholmFile file = new StockholmFile(resultFile.getAbsolutePath(),
181 DataSourceType.FILE);
182 SequenceI[] result = file.getSeqsAsArray();
183 AlignmentOrder msaorder = new AlignmentOrder(result);
184 AlignmentSorter.recoverOrder(result);
185 allOrders.add(msaorder);
191 * Displays the results of all 'jobs' in a new frame
199 private void displayResults(SequenceI[][] allResults,
200 List<AlignmentOrder> allOrders, AlignmentView msa, String title)
202 AlignmentOrder[] arrOrders = allOrders
203 .toArray(new AlignmentOrder[allOrders.size()]);
204 Object[] newview = msa.getUpdatedView(allResults, arrOrders,
205 alignment.getGapCharacter());
206 SequenceI[] seqs = (SequenceI[]) newview[0];
207 HiddenColumns hidden = (HiddenColumns) newview[1];
208 Alignment al = new Alignment(seqs);
209 al.setProperty("Alignment Program", "hmmalign");
212 al.setDataset(dataset);
216 * hack to ensure hmm set on alignment
218 if (al.getSequenceAt(0).isHMMConsensusSequence())
220 al.setHmmConsensus(al.getSequenceAt(0));
223 displayInNewFrame(al, allOrders, hidden, title);
227 * Displays the results in a new frame
230 * The alignment containing the results
232 * The order of the sequences in the alignment on which the jobs were
235 * Hidden columns in the previous alignment
238 private void displayInNewFrame(AlignmentI al,
239 List<AlignmentOrder> alorders, HiddenColumns hidden, String title)
241 AlignFrame alignFrame = new AlignFrame(al, hidden, AlignFrame.DEFAULT_WIDTH,
242 AlignFrame.DEFAULT_HEIGHT);
243 alignFrame.setTitle(title);
245 FeatureRendererSettings featureSettings = af.getFeatureRenderer()
247 // initialise with same renderer settings as in parent alignframe.
248 alignFrame.getFeatureRenderer().transferSettings(featureSettings);
250 addSortByMenuItems(alignFrame, alorders);
252 // TODO: refactor retrieve and show as new splitFrame as Desktop method
255 * If alignment was requested from one half of a SplitFrame, show in a
256 * SplitFrame with the other pane similarly aligned.
258 AlignFrame requestedBy = this.af;
259 if (requestedBy != null && requestedBy.getSplitViewContainer() != null
260 && requestedBy.getSplitViewContainer()
261 .getComplement(requestedBy) != null)
263 AlignmentI complement = requestedBy.getSplitViewContainer()
264 .getComplement(requestedBy);
265 String complementTitle = requestedBy.getSplitViewContainer()
266 .getComplementTitle(requestedBy);
267 // becomes null if the alignment window was closed before the alignment
269 AlignmentI copyComplement = new Alignment(complement);
270 // todo should this be done by copy constructor?
271 copyComplement.setGapCharacter(complement.getGapCharacter());
272 // share the same dataset (and the mappings it holds)
273 copyComplement.setDataset(complement.getDataset());
274 copyComplement.alignAs(al);
275 if (copyComplement.getHeight() > 0)
277 alignFrame.setTitle(this.af.getTitle());
278 AlignFrame af2 = new AlignFrame(copyComplement,
279 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
280 af2.setTitle(complementTitle);
281 String linkedTitle = MessageManager
282 .getString("label.linked_view_title");
283 JInternalFrame splitFrame = new SplitFrame(
284 al.isNucleotide() ? alignFrame : af2, al.isNucleotide() ? af2 : alignFrame);
285 Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
291 * Not from SplitFrame, or failed to created a complementary alignment
293 Desktop.addInternalFrame(alignFrame, alignFrame.getTitle(), AlignFrame.DEFAULT_WIDTH,
294 AlignFrame.DEFAULT_HEIGHT);
298 * Adds sort order options to the AlignFrame menus
303 protected void addSortByMenuItems(AlignFrame alignFrame,
304 List<AlignmentOrder> alorders)
307 if (alorders.size() == 1)
309 alignFrame.addSortByOrderMenuItem("hmmalign" + " Ordering", alorders.get(0));
313 // construct a non-redundant ordering set
314 List<String> names = new ArrayList<>();
315 for (int i = 0, l = alorders.size(); i < l; i++)
317 String orderName = " Region " + i;
322 if (alorders.get(i).equals(alorders.get(j)))
326 orderName += "," + j;
334 if (i == 0 && j == 1)
340 names.add(orderName);
343 for (int i = 0, l = alorders.size(); i < l; i++)
345 alignFrame.addSortByOrderMenuItem("hmmalign" + (names.get(i)) + " Ordering",