JAL-2629 multiple HMMs can now be dropped onto an alignment
[jalview.git] / src / jalview / hmmer / HMMERCommands.java
1 package jalview.hmmer;
2
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;
14
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;
21 import java.util.Map;
22
23 import javax.swing.JOptionPane;
24
25 public class HMMERCommands
26 {
27   // Path of hmmer binaries directory
28   private static final String HMMERFOLDER = "H:/Documents/";
29
30   private static final String HMMALIGN = HMMERFOLDER + "hmmalign ";
31
32   private static final String HMMBUILD = HMMERFOLDER + "hmmbuild ";
33
34   private static final String HMMSEARCH = HMMERFOLDER + "hmmsearch ";
35
36   private static final String JALVIEWDIRECTORY = "C:/Users/TZVanaalten/git/jalview/";
37
38   private static final String HMMBUFFER = "src/jalview/hmmer/hmm_buffer.hmm ";
39
40   private static final String ALIGNMENTBUFFER = "src/jalview/hmmer/alignment_buffer.sto ";
41
42   private static final String ALIGNMENTINPUT = "src/jalview/hmmer/alignment_input.sto ";
43
44   private static final String OUTPUTALIGNMENT = "-o " + JALVIEWDIRECTORY
45           + ALIGNMENTINPUT;
46
47   private static final String NAME = "-n ";
48
49   private static final String SPACE = " ";
50
51   private static final String ALLCOL = "--allcol ";
52
53   private static final String TRIM = "--trim ";
54
55   public static void hmmBuild(AlignFrame af)
56           throws IOException, InterruptedException
57   {
58
59     PrintWriter clearer = new PrintWriter(HMMBUFFER);
60     clearer.print("");
61     clearer.close();
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;
68
69     runCommand(command);
70
71     af.loadJalviewDataFile(HMMBUFFER, DataSourceType.FILE,
72             FileFormat.HMMER3, null);
73     for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
74     {
75       SequenceI seq = entry.getValue();
76       Integer pos = entry.getKey();
77       addHMMConsensusSequence(af, seq, pos);
78     }
79     af.alignPanel.alignmentChanged();
80   }
81
82   public static void hmmAlign(AlignFrame af, boolean createNewFrame,
83           HiddenMarkovModel hmm)
84           throws IOException, InterruptedException
85   {
86
87     PrintWriter clearer = new PrintWriter(ALIGNMENTINPUT);
88     clearer.print("");
89     clearer.close();
90
91     AlignmentI al = af.getViewport().getAlignment();
92     Map<Integer, SequenceI> seqs = al.getHMMConsensusSequences(true);
93     int index = 0;
94     if (hmm == null)
95     {
96       JOptionPane.showMessageDialog(af,
97               MessageManager.getString("warn.null_hmm"));
98       return;
99     }
100     exportAlignment(al);
101
102     HMMFile file = new HMMFile(hmm);
103     file.exportFile(HMMBUFFER);
104
105     String command = HMMALIGN;
106     if (!hmm.getFileHeader().contains("HMMER3/f"))
107     {
108       command += ALLCOL;
109     }
110     command += TRIM + OUTPUTALIGNMENT + JALVIEWDIRECTORY + HMMBUFFER
111             + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
112
113     runCommand(command);
114
115     if (createNewFrame)
116     {
117       FileLoader loader = new FileLoader();
118       AlignFrame newFrame = loader.LoadFileWaitTillLoaded(ALIGNMENTINPUT,
119             DataSourceType.FILE);
120       for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
121       {
122         SequenceI seq = entry.getValue();
123         Integer pos = entry.getKey();
124         addHMMConsensusSequence(newFrame, seq, pos);
125       }
126       newFrame.alignPanel.alignmentChanged();
127     }
128     else
129     {
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())
134       {
135         SequenceI seq = entry.getValue();
136         Integer pos = entry.getKey();
137         addHMMConsensusSequence(af, seq, pos);
138       }
139
140     }
141
142
143   }
144
145
146   /**
147    * Runs a command in the terminal.
148    * 
149    * @param command
150    * @throws IOException
151    * @throws InterruptedException
152    */
153   private static void runCommand(String command)
154           throws IOException, InterruptedException
155   {
156     final Process p = Runtime.getRuntime().exec(command);
157
158     new Thread(new Runnable()
159     {
160       @Override
161       public void run()
162       {
163         BufferedReader input = new BufferedReader(
164                 new InputStreamReader(p.getInputStream()));
165         String line = null;
166
167         try
168         {
169           while ((line = input.readLine()) != null)
170           {
171             System.out.println(line);
172           }
173         } catch (IOException e)
174         {
175           e.printStackTrace();
176         }
177       }
178     }).start();
179
180     p.waitFor();
181   }
182
183   /**
184    * Exports an alignment to the buffer location in Jalview.
185    * 
186    * @param alignment
187    * @throws FileNotFoundException
188    */
189   private static void exportAlignment(AlignmentI alignment)
190           throws FileNotFoundException
191   {
192     List<SequenceI> list = alignment.getSequences();
193     SequenceI[] array = new SequenceI[list.size()];
194     list.toArray(array);
195
196     StockholmFile file = new StockholmFile(new Alignment(array));
197     file.setSeqs(array);
198     String output = file.print();
199     PrintWriter writer = new PrintWriter(ALIGNMENTBUFFER);
200     writer.println(output);
201     writer.close();
202   }
203
204   private static void addHMMConsensusSequence(AlignFrame af, SequenceI seq,
205           Integer position)
206   {
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();
213   }
214
215
216 }