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 private final AlignmentI dataset;
37 * Constructor for the HMMAlignThread
42 public HMMAlign(AlignFrame af, List<ArgumentI> args)
45 if (alignment.getDataset() != null)
47 dataset = alignment.getDataset();
56 * Runs the HMMAlignThread: the data on the alignment or group is exported,
57 * then the command is executed in the command line and then the data is
58 * imported and displayed in a new frame (if true). The command is executed
59 * for each segment of the alignment. Call this method directly to execute
60 * synchronously, or via start() in a new Thread for asynchronously.
65 HiddenMarkovModel hmm = getHmmProfile();
67 long msgId = System.currentTimeMillis();
68 af.setProgressBar(MessageManager.getString("status.running_hmmalign"),
71 // ensure alignments are the same length
74 AlignmentView msa = af.gatherSequencesForAlignment();
75 SequenceI[][] subAlignments = msa.getVisibleContigs(alignment.getGapCharacter());
77 List<AlignmentOrder> allOrders = new ArrayList<>();
79 SequenceI[][] allResults = new SequenceI[subAlignments.length][];
81 for (SequenceI[] seqs : subAlignments)
83 Hashtable sequencesHash = stashSequences(seqs);
86 File modelFile = FileUtils.createTempFile("hmm", ".hmm");
87 File alignmentFile = FileUtils.createTempFile("output", ".sto");
88 File resultFile = FileUtils.createTempFile("input", ".sto");
90 exportStockholm(seqs, alignmentFile.getAbsoluteFile(), null);
91 exportHmm(hmm, modelFile.getAbsoluteFile());
93 boolean ran = runCommand(modelFile, alignmentFile, resultFile);
96 JvOptionPane.showInternalMessageDialog(af, MessageManager
97 .formatMessage("warn.command_failed", "hmmalign"));
101 SequenceI[] result = importData(resultFile, allOrders);
102 recoverSequences(sequencesHash, result);
103 allResults[job] = result;
105 alignmentFile.delete();
107 } catch (IOException e)
114 String title = "hmmalign to " + hmm.getConsensusSequence().getName();
115 displayResults(allResults, allOrders, msa, title);
117 af.setProgressBar("", msgId);
121 * Executes the hmmalign command and returns true if successful, false if an
125 * the HMM to align to
126 * @param alignmentFile
127 * the sequences to align
129 * the file to hold the results of alignment
131 * @throws IOException
133 private boolean runCommand(File modelFile, File alignmentFile,
134 File resultFile) throws IOException
136 String command = getCommandPath(HMMALIGN);
141 List<String> args = new ArrayList<>();
146 for (ArgumentI arg : params)
148 String name = arg.getName();
149 if (MessageManager.getString("label.trim_termini").equals(name))
156 args.add(getFilePath(resultFile, true));
157 args.add(getFilePath(modelFile, true));
158 args.add(getFilePath(alignmentFile, true));
160 return runCommand(args);
164 * Imports the data from the file holding the output of hmmalign
168 * a list of alignment orders to add to
171 * @throws IOException
173 private SequenceI[] importData(File resultFile,
174 List<AlignmentOrder> allOrders) throws IOException
176 StockholmFile file = new StockholmFile(getFilePath(resultFile, false),
177 DataSourceType.FILE);
178 SequenceI[] result = file.getSeqsAsArray();
179 AlignmentOrder msaorder = new AlignmentOrder(result);
180 AlignmentSorter.recoverOrder(result);
181 allOrders.add(msaorder);
187 * Displays the results of all 'jobs' in a new frame
195 private void displayResults(SequenceI[][] allResults,
196 List<AlignmentOrder> allOrders, AlignmentView msa, String title)
198 AlignmentOrder[] arrOrders = allOrders
199 .toArray(new AlignmentOrder[allOrders.size()]);
200 Object[] newview = msa.getUpdatedView(allResults, arrOrders,
201 alignment.getGapCharacter());
202 SequenceI[] seqs = (SequenceI[]) newview[0];
203 HiddenColumns hidden = (HiddenColumns) newview[1];
204 Alignment al = new Alignment(seqs);
205 al.setProperty("Alignment Program", "hmmalign");
208 al.setDataset(dataset);
211 displayInNewFrame(al, allOrders, hidden, title);
215 * Displays the results in a new frame
218 * The alignment containing the results
220 * The order of the sequences in the alignment on which the jobs were
223 * Hidden columns in the previous alignment
226 private void displayInNewFrame(AlignmentI al,
227 List<AlignmentOrder> alorders, HiddenColumns hidden, String title)
229 AlignFrame alignFrame = new AlignFrame(al, hidden, AlignFrame.DEFAULT_WIDTH,
230 AlignFrame.DEFAULT_HEIGHT);
231 alignFrame.setTitle(title);
233 FeatureRendererSettings featureSettings = af.getFeatureRenderer()
235 // initialise with same renderer settings as in parent alignframe.
236 alignFrame.getFeatureRenderer().transferSettings(featureSettings);
238 addSortByMenuItems(alignFrame, alorders);
240 // TODO: refactor retrieve and show as new splitFrame as Desktop method
243 * If alignment was requested from one half of a SplitFrame, show in a
244 * SplitFrame with the other pane similarly aligned.
246 AlignFrame requestedBy = this.af;
247 if (requestedBy != null && requestedBy.getSplitViewContainer() != null
248 && requestedBy.getSplitViewContainer()
249 .getComplement(requestedBy) != null)
251 AlignmentI complement = requestedBy.getSplitViewContainer()
252 .getComplement(requestedBy);
253 String complementTitle = requestedBy.getSplitViewContainer()
254 .getComplementTitle(requestedBy);
255 // becomes null if the alignment window was closed before the alignment
257 AlignmentI copyComplement = new Alignment(complement);
258 // todo should this be done by copy constructor?
259 copyComplement.setGapCharacter(complement.getGapCharacter());
260 // share the same dataset (and the mappings it holds)
261 copyComplement.setDataset(complement.getDataset());
262 copyComplement.alignAs(al);
263 if (copyComplement.getHeight() > 0)
265 alignFrame.setTitle(this.af.getTitle());
266 AlignFrame af2 = new AlignFrame(copyComplement,
267 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
268 af2.setTitle(complementTitle);
269 String linkedTitle = MessageManager
270 .getString("label.linked_view_title");
271 JInternalFrame splitFrame = new SplitFrame(
272 al.isNucleotide() ? alignFrame : af2, al.isNucleotide() ? af2 : alignFrame);
273 Desktop.addInternalFrame(splitFrame, linkedTitle, -1, -1);
279 * Not from SplitFrame, or failed to created a complementary alignment
281 Desktop.addInternalFrame(alignFrame, alignFrame.getTitle(), AlignFrame.DEFAULT_WIDTH,
282 AlignFrame.DEFAULT_HEIGHT);
286 * Adds sort order options to the AlignFrame menus
291 protected void addSortByMenuItems(AlignFrame alignFrame,
292 List<AlignmentOrder> alorders)
295 if (alorders.size() == 1)
297 alignFrame.addSortByOrderMenuItem("hmmalign" + " Ordering", alorders.get(0));
301 // construct a non-redundant ordering set
302 List<String> names = new ArrayList<>();
303 for (int i = 0, l = alorders.size(); i < l; i++)
305 String orderName = " Region " + i;
310 if (alorders.get(i).equals(alorders.get(j)))
314 orderName += "," + j;
322 if (i == 0 && j == 1)
328 names.add(orderName);
331 for (int i = 0, l = alorders.size(); i < l; i++)
333 alignFrame.addSortByOrderMenuItem("hmmalign" + (names.get(i)) + " Ordering",