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 = getHmmProfile();
69 long msgId = System.currentTimeMillis();
70 af.setProgressBar(MessageManager.getString("status.running_hmmalign"),
73 AlignmentView msa = af.gatherSequencesForAlignment();
74 SequenceI[][] subAlignments = msa.getVisibleContigs(alignment.getGapCharacter());
76 List<AlignmentOrder> allOrders = new ArrayList<>();
78 SequenceI[][] allResults = new SequenceI[subAlignments.length][];
80 for (SequenceI[] seqs : subAlignments)
82 Hashtable sequencesHash = stashSequences(seqs);
85 File modelFile = FileUtils.createTempFile("hmm", ".hmm");
86 File alignmentFile = FileUtils.createTempFile("output", ".sto");
87 File resultFile = FileUtils.createTempFile("input", ".sto");
89 exportStockholm(seqs, alignmentFile.getAbsoluteFile(), null);
90 exportHmm(hmm, modelFile.getAbsoluteFile());
92 boolean ran = runCommand(modelFile, alignmentFile, resultFile);
95 JvOptionPane.showInternalMessageDialog(af, MessageManager
96 .formatMessage("warn.command_failed", "hmmalign"));
100 SequenceI[] result = importData(resultFile, allOrders);
101 recoverSequences(sequencesHash, result);
102 allResults[job] = result;
104 alignmentFile.delete();
106 } catch (IOException e)
113 String title = "hmmalign to " + hmm.getConsensusSequence().getName();
114 displayResults(allResults, allOrders, msa, title);
116 af.setProgressBar("", msgId);
120 * Executes the hmmalign command and returns true if successful, false if an
124 * the HMM to align to
125 * @param alignmentFile
126 * the sequences to align
128 * the file to hold the results of alignment
130 * @throws IOException
132 private boolean runCommand(File modelFile, File alignmentFile,
133 File resultFile) throws IOException
135 String command = getCommandPath(HMMALIGN);
140 List<String> args = new ArrayList<>();
145 for (ArgumentI arg : params)
147 String name = arg.getName();
148 if (MessageManager.getString("label.trim_termini").equals(name))
155 args.add(getFilePath(resultFile));
156 args.add(getFilePath(modelFile));
157 args.add(getFilePath(alignmentFile));
159 return runCommand(args);
163 * Imports the data from the file holding the output of hmmalign
167 * a list of alignment orders to add to
170 * @throws IOException
172 private SequenceI[] importData(File resultFile,
173 List<AlignmentOrder> allOrders) throws IOException
175 StockholmFile file = new StockholmFile(getFilePath(resultFile),
176 DataSourceType.FILE);
177 SequenceI[] result = file.getSeqsAsArray();
178 AlignmentOrder msaorder = new AlignmentOrder(result);
179 AlignmentSorter.recoverOrder(result);
180 allOrders.add(msaorder);
186 * Displays the results of all 'jobs' in a new frame
194 private void displayResults(SequenceI[][] allResults,
195 List<AlignmentOrder> allOrders, AlignmentView msa, String title)
197 AlignmentOrder[] arrOrders = allOrders
198 .toArray(new AlignmentOrder[allOrders.size()]);
199 Object[] newview = msa.getUpdatedView(allResults, arrOrders,
200 alignment.getGapCharacter());
201 SequenceI[] seqs = (SequenceI[]) newview[0];
202 HiddenColumns hidden = (HiddenColumns) newview[1];
203 Alignment al = new Alignment(seqs);
204 al.setProperty("Alignment Program", "hmmalign");
207 al.setDataset(dataset);
210 displayInNewFrame(al, allOrders, hidden, title);
214 * Displays the results in a new frame
217 * The alignment containing the results
219 * The order of the sequences in the alignment on which the jobs were
222 * Hidden columns in the previous alignment
225 private void displayInNewFrame(AlignmentI al,
226 List<AlignmentOrder> alorders, HiddenColumns hidden, String title)
228 AlignFrame alignFrame = new AlignFrame(al, hidden, AlignFrame.DEFAULT_WIDTH,
229 AlignFrame.DEFAULT_HEIGHT);
230 alignFrame.setTitle(title);
232 FeatureRendererSettings featureSettings = af.getFeatureRenderer()
234 // initialise with same renderer settings as in parent alignframe.
235 alignFrame.getFeatureRenderer().transferSettings(featureSettings);
237 addSortByMenuItems(alignFrame, alorders);
239 // TODO: refactor retrieve and show as new splitFrame as Desktop method
242 * If alignment was requested from one half of a SplitFrame, show in a
243 * SplitFrame with the other pane similarly aligned.
245 AlignFrame requestedBy = this.af;
246 if (requestedBy != null && requestedBy.getSplitViewContainer() != null
247 && requestedBy.getSplitViewContainer()
248 .getComplement(requestedBy) != null)
250 AlignmentI complement = requestedBy.getSplitViewContainer()
251 .getComplement(requestedBy);
252 String complementTitle = requestedBy.getSplitViewContainer()
253 .getComplementTitle(requestedBy);
254 // becomes null if the alignment window was closed before the alignment
256 AlignmentI copyComplement = new Alignment(complement);
257 // todo should this be done by copy constructor?
258 copyComplement.setGapCharacter(complement.getGapCharacter());
259 // share the same dataset (and the mappings it holds)
260 copyComplement.setDataset(complement.getDataset());
261 copyComplement.alignAs(al);
262 if (copyComplement.getHeight() > 0)
264 alignFrame.setTitle(this.af.getTitle());
265 AlignFrame af2 = new AlignFrame(copyComplement,
266 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
267 af2.setTitle(complementTitle);
268 String linkedTitle = MessageManager
269 .getString("label.linked_view_title");
270 JInternalFrame splitFrame = new SplitFrame(
271 al.isNucleotide() ? alignFrame : af2, al.isNucleotide() ? af2 : alignFrame);
272 Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
278 * Not from SplitFrame, or failed to created a complementary alignment
280 Desktop.addInternalFrame(alignFrame, alignFrame.getTitle(), AlignFrame.DEFAULT_WIDTH,
281 AlignFrame.DEFAULT_HEIGHT);
285 * Adds sort order options to the AlignFrame menus
290 protected void addSortByMenuItems(AlignFrame alignFrame,
291 List<AlignmentOrder> alorders)
294 if (alorders.size() == 1)
296 alignFrame.addSortByOrderMenuItem("hmmalign" + " Ordering", alorders.get(0));
300 // construct a non-redundant ordering set
301 List<String> names = new ArrayList<>();
302 for (int i = 0, l = alorders.size(); i < l; i++)
304 String orderName = " Region " + i;
309 if (alorders.get(i).equals(alorders.get(j)))
313 orderName += "," + j;
321 if (i == 0 && j == 1)
327 names.add(orderName);
330 for (int i = 0, l = alorders.size(); i < l; i++)
332 alignFrame.addSortByOrderMenuItem("hmmalign" + (names.get(i)) + " Ordering",