JAL-2629 add documentation to previous work
[jalview.git] / src / jalview / hmmer / HMMBuildThread.java
1 package jalview.hmmer;
2
3
4 import jalview.bin.Cache;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.Sequence;
7 import jalview.datamodel.SequenceGroup;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.gui.AlignViewport;
11 import jalview.gui.JvOptionPane;
12 import jalview.gui.Preferences;
13 import jalview.io.DataSourceType;
14 import jalview.io.FileFormat;
15 import jalview.io.FileLoader;
16 import jalview.io.FileParse;
17 import jalview.io.HMMFile;
18 import jalview.util.MessageManager;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.util.List;
24
25 import javax.swing.JOptionPane;
26
27 public class HMMBuildThread implements Runnable
28 {
29   HMMERCommands cmds = new HMMERCommands();
30   AlignFrame af;
31
32   AlignViewport viewport;
33   AlignmentI alignment;
34   SequenceGroup group;
35
36
37   boolean forGroup = false;
38
39   File hmmTemp = null;
40
41   File stoTemp = null;
42
43   long barID;
44   
45   /**
46    * This is used for validation purposes. Do not use!
47    * 
48    * @param viewport
49    */
50   public HMMBuildThread(AlignmentI alignment)
51   {
52     this.alignment = alignment;
53     forGroup = false;
54   }
55
56   public HMMBuildThread(AlignFrame af)
57   {
58     this.af = af;
59     if (af.getViewport().getSelectionGroup() != null)
60     {
61       group = af.getViewport().getSelectionGroup();
62       forGroup = true;
63     }
64     viewport = af.getViewport();
65     alignment = viewport.getAlignment();
66
67   }
68
69   /**
70    * Builds a HMM from an alignment, then imports and adds it to the alignment.
71    */
72   @Override
73   public void run()
74   {
75     barID = System.currentTimeMillis();
76     if (af != null)
77     {
78     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
79             barID);
80     }
81     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
82     if (alignment == null && group == null)
83     {
84       JOptionPane.showMessageDialog(af,
85               MessageManager.getString("warn.no_sequence_data"));
86       return;
87     }
88     try
89     {
90       hmmTemp = File.createTempFile("hmm", ".hmm");
91       hmmTemp.deleteOnExit();
92       stoTemp = File.createTempFile("output", ".sto");
93       stoTemp.deleteOnExit();
94     } catch (IOException e1)
95     {
96       e1.printStackTrace();
97     }
98
99     try
100     {
101     try
102     {
103         SequenceI[] array;
104         List<SequenceI> seqs = alignment
105                 .getHMMConsensusSequences(true);
106         cmds.setHmmSeqs(seqs);
107         if (forGroup)
108         {
109           array = group.getSelectionAsNewSequences(alignment);
110         }
111         else
112         {
113           if (!alignment.isAligned())
114           {
115             alignment.padGaps();
116           }
117           array = alignment.getSequencesArray();
118         }
119         if (array.length < 1)
120         {
121           if (af != null)
122           {
123             JOptionPane.showMessageDialog(af,
124                   MessageManager.getString("warn.no_sequence_data"));
125           }
126           return;
127         }
128         SequenceI[] newArr = new SequenceI[array.length];
129         int index = 0;
130         for (SequenceI seq : array)
131         {
132           newArr[index] = new Sequence(seq);
133           index++;
134         }
135
136         cmds.uniquifySequences(newArr);
137         cmds.exportData(newArr, stoTemp, null, null);
138         jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array);
139
140     } catch (FileNotFoundException e)
141     {
142       // TODO Auto-generated catch block
143       e.printStackTrace();
144
145     }
146     try
147     {
148         boolean ran = runCommand();
149         if (!ran)
150         {
151           if (af != null)
152           {
153             JvOptionPane.showInternalMessageDialog(af,
154                   MessageManager.getString("warn.hmmbuild_failed"));
155           }
156           return;
157         }
158     } catch (IOException | InterruptedException e)
159     {
160       // TODO Auto-generated catch block
161       e.printStackTrace();
162     }
163     try
164     {
165
166       importData();
167     } catch (IOException | InterruptedException e)
168     {
169       // TODO Auto-generated catch block
170       e.printStackTrace();
171     }
172     } catch (Exception e)
173     {
174       e.printStackTrace();
175     } finally
176     {
177       if (af != null)
178       {
179         af.setProgressBar(
180                 MessageManager.getString("status.running_hmmbuild"),
181               barID);
182       }
183     }
184   }
185
186   
187
188   /**
189    * Executes the hmmbuild command in the command line.
190    * 
191    * @return
192    * @throws IOException
193    * @throws InterruptedException
194    */
195   private boolean runCommand() throws IOException, InterruptedException
196   {
197     File file = new File(cmds.HMMERFOLDER + "/hmmbuild");
198     if (!file.canExecute())
199     {
200       file = new File(cmds.HMMERFOLDER + "/hmmbuild.exe");
201       {
202         if (!file.canExecute())
203         {
204           return false;
205         }
206       }
207     }
208     String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME;
209     if (forGroup)
210     {
211       command += group.getName();
212     }
213     else
214     {
215       String name = null;
216       if (af != null)
217       {
218         name = af.getTitle();
219       }
220       if (name == null)
221       {
222         name = "Alignment";
223       }
224       command += name;
225     }
226     command += cmds.SPACE;
227     if (!alignment.isNucleotide())
228     {
229       command += cmds.FORCEAMINO; // TODO check for rna
230     }
231     else
232     {
233       command += cmds.FORCEDNA;
234     }
235
236     command += hmmTemp.getAbsolutePath()
237             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
238     return cmds.runCommand(command);
239   }
240   
241   /**
242    * Imports the .hmm file produced by hmmbuild.
243    * 
244    * @throws IOException
245    * @throws InterruptedException
246    */
247   private void importData() throws IOException, InterruptedException
248   {
249     if (af != null)
250     {
251       cmds.addHMMConsensusSequences(af);
252
253       FileLoader loader = new FileLoader();
254       loader.LoadFileOntoAlignmentWaitTillLoaded(viewport,
255               hmmTemp.getAbsolutePath(), DataSourceType.FILE,
256               FileFormat.HMMER3);
257     }
258     else
259     {
260       HMMFile file = new HMMFile(new FileParse(hmmTemp.getAbsolutePath(),
261               DataSourceType.FILE));
262       alignment.addSequence(file.getSeqsAsArray()[0]);
263     }
264     hmmTemp.delete();
265     stoTemp.delete();
266   }
267   
268   /**
269    * Runs hmmbuild, and waits for the results to be imported before continuing
270    */
271   public void hmmbuildWaitTillComplete()
272   {
273     Thread loader = new Thread(this);
274     loader.start();
275
276     while (loader.isAlive())
277     {
278       try
279       {
280         Thread.sleep(500);
281       } catch (Exception ex)
282       {
283       }
284     }
285   }
286 }