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.JvOptionPane; import jalview.gui.Preferences; import jalview.io.DataSourceType; import jalview.io.FileFormat; import jalview.io.FileLoader; 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; AlignmentI alignment; SequenceGroup group; boolean forGroup = false; boolean canRun = true; File hmmTemp = null; File stoTemp = null; long barID; public HMMBuildThread(AlignFrame af) { this.af = af; if (af.getViewport().getSelectionGroup() != null) { group = af.getViewport().getSelectionGroup(); forGroup = true; } alignment = af.getViewport().getAlignment(); } @Override public void run() { barID = System.currentTimeMillis(); 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("label.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(); } 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) { 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 { af.setProgressBar(MessageManager.getString("status.running_hmmbuild"), barID); } } 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 = af.getName(); 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); } private void importData() throws IOException, InterruptedException { cmds.addHMMConsensusSequences(af); FileLoader loader = new FileLoader(); loader.LoadFileOntoAlignmentWaitTillLoaded(af.getViewport(), hmmTemp.getAbsolutePath(), DataSourceType.FILE, FileFormat.HMMER3); hmmTemp.delete(); stoTemp.delete(); } public boolean canRun() { return canRun; } }