3 import jalview.analysis.SeqsetUtils;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.Sequence;
6 import jalview.datamodel.SequenceGroup;
7 import jalview.datamodel.SequenceI;
8 import jalview.gui.AlignFrame;
9 import jalview.gui.AlignViewport;
10 import jalview.gui.JvOptionPane;
11 import jalview.io.DataSourceType;
12 import jalview.io.FileParse;
13 import jalview.io.HMMFile;
14 import jalview.util.MessageManager;
15 import jalview.ws.params.ArgumentI;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
23 import javax.swing.JOptionPane;
25 public class HMMBuildThread extends HmmerCommand implements Runnable
27 AlignViewport viewport;
29 boolean multiJob = false;
35 List<ArgumentI> params;
37 boolean forGroup = false;
46 * This is used for validation purposes. Do not use!
50 public HMMBuildThread(AlignmentI alignment)
52 this.alignment = alignment;
56 public HMMBuildThread(AlignFrame af, List<ArgumentI> args)
59 viewport = af.getViewport();
64 * Builds a HMM from an alignment, then imports and adds it to the alignment.
69 barID = System.currentTimeMillis();
72 af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
76 List<SequenceGroup> groups = new ArrayList<>();
79 for (ArgumentI arg : params)
81 String name = arg.getName();
82 if (MessageManager.getString("label.hmmbuild_for").equals(name))
84 String value = arg.getValue();
85 if ("Alignment".equals(value))
87 alignment = viewport.getAlignment();
90 else if ("All groups and alignment".equals(value))
92 alignment = viewport.getAlignment();
93 groups.addAll(viewport.getAlignment().getGroups());
94 if (groups.size() > 0)
100 else if ("All groups".equals(value))
103 groups = viewport.getAlignment().getGroups();
104 if (groups.size() > 0)
110 else if ("Selected group".equals(value))
113 groups.add(viewport.getSelectionGroup());
117 else if (MessageManager.getString("label.use_reference")
120 if (!af.getViewport().hasReferenceAnnotation())
125 MessageManager.getString("status.running_hmmbuild"),
128 JvOptionPane.showInternalMessageDialog(af, MessageManager
129 .getString("warn.no_reference_annotation"));
135 else if (viewport != null)
137 alignment = viewport.getAlignment();
140 if (alignment != null)
146 if (alignment == null)
148 alignment = viewport.getAlignment();
151 if (groups != null && groups.size() > 0)
153 for (SequenceGroup grp : groups)
163 af.setProgressBar("", barID);
167 private void runHMMBuild()
169 if (alignment == null && group == null)
171 JOptionPane.showMessageDialog(af,
172 MessageManager.getString("warn.no_sequence_data"));
176 hmmTemp = File.createTempFile("hmm", ".hmm");
177 hmmTemp.deleteOnExit();
178 stoTemp = File.createTempFile("output", ".sto");
179 stoTemp.deleteOnExit();
180 } catch (IOException e1)
182 e1.printStackTrace();
190 List<SequenceI> seqs = null;
193 seqs = group.getHMMConsensusSequences(true);
198 array = group.getSelectionAsNewSequences(alignment);
202 seqs = alignment.getHMMConsensusSequences(true);
203 if (!alignment.isAligned())
207 array = alignment.getSequencesArray();
210 if (array.length < 1)
214 JOptionPane.showMessageDialog(af,
215 MessageManager.getString("warn.no_sequence_data"));
219 SequenceI[] newArr = new SequenceI[array.length];
221 for (SequenceI seq : array)
223 newArr[index] = new Sequence(seq);
227 uniquifySequences(newArr);
231 exportData(newArr, stoTemp, null, null, group);
235 exportData(newArr, stoTemp, null, null, alignment);
238 SeqsetUtils.deuniquify(hash, array);
240 } catch (FileNotFoundException e)
242 // TODO Auto-generated catch block
248 boolean ran = runCommand();
253 } catch (IOException | InterruptedException e)
255 // TODO Auto-generated catch block
262 } catch (IOException | InterruptedException e)
264 // TODO Auto-generated catch block
267 } catch (Exception e)
274 * Executes the hmmbuild command in the command line.
277 * @throws IOException
278 * @throws InterruptedException
280 private boolean runCommand() throws IOException, InterruptedException
282 String binaryPath = getCommandRoot(HMMBUILD);
283 if (binaryPath == null)
287 String command = binaryPath + SPACE;
292 for (ArgumentI arg : params)
294 String argName = arg.getName();
298 name = arg.getValue();
301 case "Use Reference Annotation":
302 command += "--hand ";
309 if (forGroup && multiJob)
311 name = group.getName() + "_HMM";
314 if (name == null || "".equals(name))
318 if (af.getTitle().length() < 15)
320 name = af.getTitle();
323 if (name == null || "".equals(name))
330 command += "-n " + name.replace(' ', '_') + SPACE;
331 if (!alignment.isNucleotide())
333 command += FORCEAMINO; // TODO check for rna
340 command += hmmTemp.getAbsolutePath() + SPACE + stoTemp.getAbsolutePath()
342 return runCommand(command);
346 * Imports the .hmm file produced by hmmbuild.
348 * @throws IOException
349 * @throws InterruptedException
351 private void importData() throws IOException, InterruptedException
353 HMMFile file = new HMMFile(
354 new FileParse(hmmTemp.getAbsolutePath(), DataSourceType.FILE));
355 SequenceI[] seqs = file.getSeqsAsArray();
356 SequenceI seq = seqs[0];
357 seq.createDatasetSequence();
360 seq.insertCharAt(0, group.getStartRes(), '-');
361 seq.insertCharAt(group.getEndRes() + 1,
362 alignment.getWidth() - group.getEndRes() - 1, '-');
363 seq.updateHMMMapping();
364 SequenceI topSeq = group.getSequencesInOrder(alignment)[0];
365 int topIndex = alignment.findIndex(topSeq);
366 alignment.insertSequenceAt(topIndex, seq);
367 group.setSeqrep(seq);
368 group.addSequence(seq, false);
372 alignment.insertSequenceAt(0, seq);
375 if (viewport != null)
377 viewport.alignmentChanged(viewport.getAlignPanel());
378 viewport.getAlignPanel().adjustAnnotationHeight();
379 viewport.updateSequenceIdColours();
381 if (viewport.getAlignPanel().alignFrame.getSelectedHMM() == null)
383 viewport.getAlignPanel().alignFrame.setSelectedHMMSequence(seq);
391 * Runs hmmbuild, and waits for the results to be imported before continuing
393 public void hmmbuildWaitTillComplete()
395 Thread loader = new Thread(this);
398 while (loader.isAlive())
403 } catch (Exception ex)