JAL-2629 hmmbuild now runs on alignments of different lengths
[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     if (!alignment.isAligned())
64     {
65       alignment.padGaps();
66     }
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;
72
73     runCommand(command);
74
75     af.loadJalviewDataFile(HMMBUFFER, DataSourceType.FILE,
76             FileFormat.HMMER3, null);
77     for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
78     {
79       SequenceI seq = entry.getValue();
80       Integer pos = entry.getKey();
81       addHMMConsensusSequence(af, seq, pos);
82     }
83     af.alignPanel.alignmentChanged();
84   }
85
86   public static void hmmAlign(AlignFrame af, boolean createNewFrame,
87           HiddenMarkovModel hmm)
88           throws IOException, InterruptedException
89   {
90
91     PrintWriter clearer = new PrintWriter(ALIGNMENTINPUT);
92     clearer.print("");
93     clearer.close();
94     AlignmentI al = af.getViewport().getAlignment();
95     if (!al.isAligned())
96     {
97       al.padGaps();
98     }
99     Map<Integer, SequenceI> seqs = al.getHMMConsensusSequences(true);
100     int index = 0;
101     if (hmm == null)
102     {
103       JOptionPane.showMessageDialog(af,
104               MessageManager.getString("warn.null_hmm"));
105       return;
106     }
107     exportAlignment(al);
108
109     HMMFile file = new HMMFile(hmm);
110     file.exportFile(HMMBUFFER);
111
112     String command = HMMALIGN;
113     if (!hmm.getFileHeader().contains("HMMER3/f"))
114     {
115       command += ALLCOL;
116     }
117     command += TRIM + OUTPUTALIGNMENT + JALVIEWDIRECTORY + HMMBUFFER
118             + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
119
120     runCommand(command);
121
122     if (createNewFrame)
123     {
124       FileLoader loader = new FileLoader();
125       AlignFrame newFrame = loader.LoadFileWaitTillLoaded(ALIGNMENTINPUT,
126             DataSourceType.FILE);
127       for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
128       {
129         SequenceI seq = entry.getValue();
130         Integer pos = entry.getKey();
131         addHMMConsensusSequence(newFrame, seq, pos);
132       }
133       newFrame.alignPanel.alignmentChanged();
134     }
135     else
136     {
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())
141       {
142         SequenceI seq = entry.getValue();
143         Integer pos = entry.getKey();
144         addHMMConsensusSequence(af, seq, pos);
145       }
146
147     }
148
149
150   }
151
152
153   /**
154    * Runs a command in the terminal.
155    * 
156    * @param command
157    * @throws IOException
158    * @throws InterruptedException
159    */
160   private static void runCommand(String command)
161           throws IOException, InterruptedException
162   {
163     final Process p = Runtime.getRuntime().exec(command);
164
165     new Thread(new Runnable()
166     {
167       @Override
168       public void run()
169       {
170         BufferedReader input = new BufferedReader(
171                 new InputStreamReader(p.getInputStream()));
172         String line = null;
173
174         try
175         {
176           while ((line = input.readLine()) != null)
177           {
178             System.out.println(line);
179           }
180         } catch (IOException e)
181         {
182           e.printStackTrace();
183         }
184       }
185     }).start();
186
187     p.waitFor();
188   }
189
190   /**
191    * Exports an alignment to the buffer location in Jalview.
192    * 
193    * @param alignment
194    * @throws FileNotFoundException
195    */
196   private static void exportAlignment(AlignmentI alignment)
197           throws FileNotFoundException
198   {
199     List<SequenceI> list = alignment.getSequences();
200     SequenceI[] array = new SequenceI[list.size()];
201     list.toArray(array);
202
203     StockholmFile file = new StockholmFile(new Alignment(array));
204     file.setSeqs(array);
205     String output = file.print();
206     PrintWriter writer = new PrintWriter(ALIGNMENTBUFFER);
207     writer.println(output);
208     writer.close();
209   }
210
211   private static void addHMMConsensusSequence(AlignFrame af, SequenceI seq,
212           Integer position)
213   {
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();
220   }
221
222
223 }