3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.HiddenMarkovModel;
6 import jalview.datamodel.SequenceI;
7 import jalview.gui.AlignFrame;
8 import jalview.io.DataSourceType;
9 import jalview.io.FileFormat;
10 import jalview.io.FileLoader;
11 import jalview.io.HMMFile;
12 import jalview.io.StockholmFile;
13 import jalview.util.MessageManager;
15 import java.io.BufferedReader;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.io.PrintWriter;
20 import java.util.List;
23 import javax.swing.JOptionPane;
25 public class HMMERCommands
27 // Path of hmmer binaries directory
28 private static final String HMMERFOLDER = "H:/Documents/";
30 private static final String HMMALIGN = HMMERFOLDER + "hmmalign ";
32 private static final String HMMBUILD = HMMERFOLDER + "hmmbuild ";
34 private static final String HMMSEARCH = HMMERFOLDER + "hmmsearch ";
36 private static final String JALVIEWDIRECTORY = "C:/Users/TZVanaalten/git/jalview/";
38 private static final String HMMBUFFER = "src/jalview/hmmer/hmm_buffer.hmm ";
40 private static final String ALIGNMENTBUFFER = "src/jalview/hmmer/alignment_buffer.sto ";
42 private static final String ALIGNMENTINPUT = "src/jalview/hmmer/alignment_input.sto ";
44 private static final String OUTPUTALIGNMENT = "-o " + JALVIEWDIRECTORY
47 private static final String NAME = "-n ";
49 private static final String SPACE = " ";
51 private static final String ALLCOL = "--allcol ";
53 private static final String TRIM = "--trim ";
55 public static void hmmBuild(AlignFrame af)
56 throws IOException, InterruptedException
59 PrintWriter clearer = new PrintWriter(HMMBUFFER);
62 AlignmentI alignment = af.getViewport().getAlignment();
63 Map<Integer, SequenceI> seqs = alignment.getHMMConsensusSequences(true);
64 exportAlignment(alignment);
65 final String command = HMMBUILD + NAME + af.getName() + SPACE
66 + JALVIEWDIRECTORY + HMMBUFFER
67 + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
71 af.loadJalviewDataFile(HMMBUFFER, DataSourceType.FILE,
72 FileFormat.HMMER3, null);
73 for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
75 SequenceI seq = entry.getValue();
76 Integer pos = entry.getKey();
77 addHMMConsensusSequence(af, seq, pos);
79 af.alignPanel.alignmentChanged();
82 public static void hmmAlign(AlignFrame af, boolean createNewFrame,
83 HiddenMarkovModel hmm)
84 throws IOException, InterruptedException
87 PrintWriter clearer = new PrintWriter(ALIGNMENTINPUT);
91 AlignmentI al = af.getViewport().getAlignment();
92 Map<Integer, SequenceI> seqs = al.getHMMConsensusSequences(true);
96 JOptionPane.showMessageDialog(af,
97 MessageManager.getString("warn.null_hmm"));
102 HMMFile file = new HMMFile(hmm);
103 file.exportFile(HMMBUFFER);
105 String command = HMMALIGN;
106 if (!hmm.getFileHeader().contains("HMMER3/f"))
110 command += TRIM + OUTPUTALIGNMENT + JALVIEWDIRECTORY + HMMBUFFER
111 + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
117 FileLoader loader = new FileLoader();
118 AlignFrame newFrame = loader.LoadFileWaitTillLoaded(ALIGNMENTINPUT,
119 DataSourceType.FILE);
120 for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
122 SequenceI seq = entry.getValue();
123 Integer pos = entry.getKey();
124 addHMMConsensusSequence(newFrame, seq, pos);
126 newFrame.alignPanel.alignmentChanged();
130 af.getViewport().getAlignment().getSequences().clear();
131 af.loadJalviewDataFile(ALIGNMENTBUFFER, DataSourceType.FILE,
132 FileFormat.Stockholm, null);
133 for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
135 SequenceI seq = entry.getValue();
136 Integer pos = entry.getKey();
137 addHMMConsensusSequence(af, seq, pos);
147 * Runs a command in the terminal.
150 * @throws IOException
151 * @throws InterruptedException
153 private static void runCommand(String command)
154 throws IOException, InterruptedException
156 final Process p = Runtime.getRuntime().exec(command);
158 new Thread(new Runnable()
163 BufferedReader input = new BufferedReader(
164 new InputStreamReader(p.getInputStream()));
169 while ((line = input.readLine()) != null)
171 System.out.println(line);
173 } catch (IOException e)
184 * Exports an alignment to the buffer location in Jalview.
187 * @throws FileNotFoundException
189 private static void exportAlignment(AlignmentI alignment)
190 throws FileNotFoundException
192 List<SequenceI> list = alignment.getSequences();
193 SequenceI[] array = new SequenceI[list.size()];
196 StockholmFile file = new StockholmFile(new Alignment(array));
198 String output = file.print();
199 PrintWriter writer = new PrintWriter(ALIGNMENTBUFFER);
200 writer.println(output);
204 private static void addHMMConsensusSequence(AlignFrame af, SequenceI seq,
207 seq.getHMM().initHMMSequence(af, position);
208 AlignmentI al = af.getViewport().getAlignment();
209 af.getViewport().setAlignment(al);
210 af.alignPanel.adjustAnnotationHeight();
211 af.getViewport().updateSequenceIdColours();
212 af.buildSortByAnnotationScoresMenu();