package jalview.hmmer; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.HiddenMarkovModel; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.io.HMMFile; import jalview.io.StockholmFile; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Hashtable; import java.util.List; /** * Contains multiple commands and methods frequently used to run hmmbuild, * hmmalign and hmmsearch * * @author TZVanaalten * */ public class HMMERCommands { // Path of hmmer binaries directory String HMMERFOLDER = "/Documents/"; public String JALVIEWDIRECTORY = System.getProperty("user.dir") + "/"; public final String HMMALIGN = "/hmmalign "; public final String HMMBUILD = "/hmmbuild "; public final String HMMSEARCH = "/hmmsearch "; public String OUTPUTALIGNMENT; public final String NAME = "-n "; public final String SPACE = " "; public final String ALLCOL = "--allcol "; public final String TRIM = "--trim "; public final String FORCEAMINO = "--amino "; public final String FORCEDNA = "--dna "; public final String FORCERNA = "--rna "; Hashtable hash = new Hashtable(); List hmmSeqs; /** * Uniquifies the sequences when exporting and stores their details in a * hashtable. * * @param seqs */ public void uniquifySequences(SequenceI[] seqs) { hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true); } /** * Recover the sequence data lost by uniquifying. * * @param seqs */ public void recoverSequenceNames(SequenceI[] seqs) { jalview.analysis.SeqsetUtils.deuniquify(hash, seqs); } /** * Runs a command in the command line. * * @param command * @throws IOException * @throws InterruptedException */ public boolean runCommand(String command) throws IOException, InterruptedException { try { final Process p = Runtime.getRuntime().exec(command); new Thread(new Runnable() { @Override public void run() { BufferedReader input = new BufferedReader( new InputStreamReader(p.getInputStream())); String line = null; try { while ((line = input.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }).start(); p.waitFor(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * Exports an alignment and/or HMM to the specified file. * * @param alignment * @throws IOException */ public void exportData(SequenceI[] seqs, File stoLocation, HiddenMarkovModel hmm, File hmmLocation) throws IOException { if (seqs != null) { StockholmFile file = new StockholmFile(new Alignment(seqs)); String output = file.print(seqs, false); PrintWriter writer = new PrintWriter(stoLocation); writer.println(output); writer.close(); } if (hmm != null) { HMMFile file = new HMMFile(hmm); PrintWriter writer = new PrintWriter(hmmLocation); writer.print(file.print()); writer.close(); } } /** * Adds any HMM sequences removed before submitting the alignment as a job * back into the alignment. * * @param af */ public void addHMMConsensusSequences(AlignFrame af) { AlignmentI al = af.getViewport().getAlignment(); if (hmmSeqs == null || hmmSeqs.size() < 1) { return; } for (SequenceI seq : hmmSeqs) { Integer position = seq.getPreviousPosition(); al.getSequences().add(position, seq); } af.getViewport().setAlignment(al); af.alignPanel.adjustAnnotationHeight(); af.getViewport().updateSequenceIdColours(); af.buildSortByAnnotationScoresMenu(); af.getViewport().initInformation(); } /** * Returns the list of HMM sequences removed * * @return */ public List getHmmSeqs() { return hmmSeqs; } /** * Sets the list of removed HMM sequences * * @param hmmSeqs */ public void setHmmSeqs(List hmmSeqs) { this.hmmSeqs = hmmSeqs; } }