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 if (!alignment.isAligned())
67 Map<Integer, SequenceI> seqs = alignment.getHMMConsensusSequences(true);
68 exportAlignment(alignment);
69 final String command = HMMBUILD + NAME + af.getName() + SPACE
70 + JALVIEWDIRECTORY + HMMBUFFER
71 + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
75 af.loadJalviewDataFile(HMMBUFFER, DataSourceType.FILE,
76 FileFormat.HMMER3, null);
77 for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
79 SequenceI seq = entry.getValue();
80 Integer pos = entry.getKey();
81 addHMMConsensusSequence(af, seq, pos);
83 af.alignPanel.alignmentChanged();
86 public static void hmmAlign(AlignFrame af, boolean createNewFrame,
87 HiddenMarkovModel hmm)
88 throws IOException, InterruptedException
91 PrintWriter clearer = new PrintWriter(ALIGNMENTINPUT);
94 AlignmentI al = af.getViewport().getAlignment();
99 Map<Integer, SequenceI> seqs = al.getHMMConsensusSequences(true);
103 JOptionPane.showMessageDialog(af,
104 MessageManager.getString("warn.null_hmm"));
109 HMMFile file = new HMMFile(hmm);
110 file.exportFile(HMMBUFFER);
112 String command = HMMALIGN;
113 if (!hmm.getFileHeader().contains("HMMER3/f"))
117 command += TRIM + OUTPUTALIGNMENT + JALVIEWDIRECTORY + HMMBUFFER
118 + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
124 FileLoader loader = new FileLoader();
125 AlignFrame newFrame = loader.LoadFileWaitTillLoaded(ALIGNMENTINPUT,
126 DataSourceType.FILE);
127 for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
129 SequenceI seq = entry.getValue();
130 Integer pos = entry.getKey();
131 addHMMConsensusSequence(newFrame, seq, pos);
133 newFrame.alignPanel.alignmentChanged();
137 af.getViewport().getAlignment().getSequences().clear();
138 af.loadJalviewDataFile(ALIGNMENTBUFFER, DataSourceType.FILE,
139 FileFormat.Stockholm, null);
140 for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
142 SequenceI seq = entry.getValue();
143 Integer pos = entry.getKey();
144 addHMMConsensusSequence(af, seq, pos);
154 * Runs a command in the terminal.
157 * @throws IOException
158 * @throws InterruptedException
160 private static void runCommand(String command)
161 throws IOException, InterruptedException
163 final Process p = Runtime.getRuntime().exec(command);
165 new Thread(new Runnable()
170 BufferedReader input = new BufferedReader(
171 new InputStreamReader(p.getInputStream()));
176 while ((line = input.readLine()) != null)
178 System.out.println(line);
180 } catch (IOException e)
191 * Exports an alignment to the buffer location in Jalview.
194 * @throws FileNotFoundException
196 private static void exportAlignment(AlignmentI alignment)
197 throws FileNotFoundException
199 List<SequenceI> list = alignment.getSequences();
200 SequenceI[] array = new SequenceI[list.size()];
203 StockholmFile file = new StockholmFile(new Alignment(array));
205 String output = file.print();
206 PrintWriter writer = new PrintWriter(ALIGNMENTBUFFER);
207 writer.println(output);
211 private static void addHMMConsensusSequence(AlignFrame af, SequenceI seq,
214 seq.getHMM().initHMMSequence(af, position);
215 AlignmentI al = af.getViewport().getAlignment();
216 af.getViewport().setAlignment(al);
217 af.alignPanel.adjustAnnotationHeight();
218 af.getViewport().updateSequenceIdColours();
219 af.buildSortByAnnotationScoresMenu();