package jalview.hmmer; import jalview.bin.Cache; import jalview.datamodel.AlignmentI; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.AlignViewport; import jalview.gui.JvOptionPane; import jalview.gui.Preferences; import jalview.io.DataSourceType; import jalview.io.FileFormat; import jalview.io.FileLoader; import jalview.io.FileParse; import jalview.io.HMMFile; import jalview.util.MessageManager; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import javax.swing.JOptionPane; public class HMMBuildThread implements Runnable { HMMERCommands cmds = new HMMERCommands(); AlignFrame af; AlignViewport viewport; AlignmentI alignment; SequenceGroup group; boolean forGroup = false; File hmmTemp = null; File stoTemp = null; long barID; /** * This is used for validation purposes. Do not use! * * @param viewport */ public HMMBuildThread(AlignmentI alignment) { this.alignment = alignment; forGroup = false; } public HMMBuildThread(AlignFrame af) { this.af = af; if (af.getViewport().getSelectionGroup() != null) { group = af.getViewport().getSelectionGroup(); forGroup = true; } viewport = af.getViewport(); alignment = viewport.getAlignment(); } /** * Builds a HMM from an alignment, then imports and adds it to the alignment. */ @Override public void run() { barID = System.currentTimeMillis(); if (af != null) { af.setProgressBar(MessageManager.getString("status.running_hmmbuild"), barID); } cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH); if (alignment == null && group == null) { JOptionPane.showMessageDialog(af, MessageManager.getString("warn.no_sequence_data")); return; } try { hmmTemp = File.createTempFile("hmm", ".hmm"); hmmTemp.deleteOnExit(); stoTemp = File.createTempFile("output", ".sto"); stoTemp.deleteOnExit(); } catch (IOException e1) { e1.printStackTrace(); } try { try { SequenceI[] array; List seqs = alignment .getHMMConsensusSequences(true); cmds.setHmmSeqs(seqs); if (forGroup) { array = group.getSelectionAsNewSequences(alignment); } else { if (!alignment.isAligned()) { alignment.padGaps(); } array = alignment.getSequencesArray(); } if (array.length < 1) { if (af != null) { JOptionPane.showMessageDialog(af, MessageManager.getString("warn.no_sequence_data")); } return; } SequenceI[] newArr = new SequenceI[array.length]; int index = 0; for (SequenceI seq : array) { newArr[index] = new Sequence(seq); index++; } cmds.uniquifySequences(newArr); cmds.exportData(newArr, stoTemp, null, null); jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { boolean ran = runCommand(); if (!ran) { if (af != null) { JvOptionPane.showInternalMessageDialog(af, MessageManager.getString("warn.hmmbuild_failed")); } return; } } catch (IOException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { importData(); } catch (IOException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { if (af != null) { af.setProgressBar( MessageManager.getString("status.running_hmmbuild"), barID); } } } /** * Executes the hmmbuild command in the command line. * * @return * @throws IOException * @throws InterruptedException */ private boolean runCommand() throws IOException, InterruptedException { File file = new File(cmds.HMMERFOLDER + "/hmmbuild"); if (!file.canExecute()) { file = new File(cmds.HMMERFOLDER + "/hmmbuild.exe"); { if (!file.canExecute()) { return false; } } } String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME; if (forGroup) { command += group.getName(); } else { String name = null; if (af != null) { name = af.getTitle(); } if (name == null) { name = "Alignment"; } command += name; } command += cmds.SPACE; if (!alignment.isNucleotide()) { command += cmds.FORCEAMINO; // TODO check for rna } else { command += cmds.FORCEDNA; } command += hmmTemp.getAbsolutePath() + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE; return cmds.runCommand(command); } /** * Imports the .hmm file produced by hmmbuild. * * @throws IOException * @throws InterruptedException */ private void importData() throws IOException, InterruptedException { if (af != null) { cmds.addHMMConsensusSequences(af); FileLoader loader = new FileLoader(); loader.LoadFileOntoAlignmentWaitTillLoaded(viewport, hmmTemp.getAbsolutePath(), DataSourceType.FILE, FileFormat.HMMER3); } else { HMMFile file = new HMMFile(new FileParse(hmmTemp.getAbsolutePath(), DataSourceType.FILE)); alignment.addSequence(file.getSeqsAsArray()[0]); } hmmTemp.delete(); stoTemp.delete(); } /** * Runs hmmbuild, and waits for the results to be imported before continuing */ public void hmmbuildWaitTillComplete() { Thread loader = new Thread(this); loader.start(); while (loader.isAlive()) { try { Thread.sleep(500); } catch (Exception ex) { } } } }