JAL-2629 fix for hmmbuild not loading a HMM onto the current frame
[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   @Override
70   public void run()
71   {
72     barID = System.currentTimeMillis();
73     if (af != null)
74     {
75     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
76             barID);
77     }
78     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
79     if (alignment == null && group == null)
80     {
81       JOptionPane.showMessageDialog(af,
82               MessageManager.getString("warn.no_sequence_data"));
83       return;
84     }
85     try
86     {
87       hmmTemp = File.createTempFile("hmm", ".hmm");
88       hmmTemp.deleteOnExit();
89       stoTemp = File.createTempFile("output", ".sto");
90       stoTemp.deleteOnExit();
91     } catch (IOException e1)
92     {
93       e1.printStackTrace();
94     }
95
96     try
97     {
98     try
99     {
100         SequenceI[] array;
101         List<SequenceI> seqs = alignment
102                 .getHMMConsensusSequences(true);
103         cmds.setHmmSeqs(seqs);
104         if (forGroup)
105         {
106           array = group.getSelectionAsNewSequences(alignment);
107         }
108         else
109         {
110           if (!alignment.isAligned())
111           {
112             alignment.padGaps();
113           }
114           array = alignment.getSequencesArray();
115         }
116         if (array.length < 1)
117         {
118           if (af != null)
119           {
120             JOptionPane.showMessageDialog(af,
121                   MessageManager.getString("warn.no_sequence_data"));
122           }
123           return;
124         }
125         SequenceI[] newArr = new SequenceI[array.length];
126         int index = 0;
127         for (SequenceI seq : array)
128         {
129           newArr[index] = new Sequence(seq);
130           index++;
131         }
132
133         cmds.uniquifySequences(newArr);
134         cmds.exportData(newArr, stoTemp, null, null);
135         jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array);
136
137     } catch (FileNotFoundException e)
138     {
139       // TODO Auto-generated catch block
140       e.printStackTrace();
141
142     }
143     try
144     {
145         boolean ran = runCommand();
146         if (!ran)
147         {
148           if (af != null)
149           {
150             JvOptionPane.showInternalMessageDialog(af,
151                   MessageManager.getString("warn.hmmbuild_failed"));
152           }
153           return;
154         }
155     } catch (IOException | InterruptedException e)
156     {
157       // TODO Auto-generated catch block
158       e.printStackTrace();
159     }
160     try
161     {
162
163       importData();
164     } catch (IOException | InterruptedException e)
165     {
166       // TODO Auto-generated catch block
167       e.printStackTrace();
168     }
169     } catch (Exception e)
170     {
171       e.printStackTrace();
172     } finally
173     {
174       if (af != null)
175       {
176         af.setProgressBar(
177                 MessageManager.getString("status.running_hmmbuild"),
178               barID);
179       }
180     }
181   }
182
183   
184
185   
186   private boolean runCommand() throws IOException, InterruptedException
187   {
188     File file = new File(cmds.HMMERFOLDER + "/hmmbuild");
189     if (!file.canExecute())
190     {
191       file = new File(cmds.HMMERFOLDER + "/hmmbuild.exe");
192       {
193         if (!file.canExecute())
194         {
195           return false;
196         }
197       }
198     }
199     String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME;
200     if (forGroup)
201     {
202       command += group.getName();
203     }
204     else
205     {
206       String name = null;
207       if (af != null)
208       {
209         name = af.getName();
210       }
211       if (name == null)
212       {
213         name = "Alignment";
214       }
215       command += name;
216     }
217     command += cmds.SPACE;
218     if (!alignment.isNucleotide())
219     {
220       command += cmds.FORCEAMINO; // TODO check for rna
221     }
222     else
223     {
224       command += cmds.FORCEDNA;
225     }
226
227     command += hmmTemp.getAbsolutePath()
228             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
229     return cmds.runCommand(command);
230   }
231   
232   private void importData() throws IOException, InterruptedException
233   {
234     if (af != null)
235     {
236       cmds.addHMMConsensusSequences(af);
237
238       FileLoader loader = new FileLoader();
239       loader.LoadFileOntoAlignmentWaitTillLoaded(viewport,
240               hmmTemp.getAbsolutePath(), DataSourceType.FILE,
241               FileFormat.HMMER3);
242     }
243     else
244     {
245       HMMFile file = new HMMFile(new FileParse(hmmTemp.getAbsolutePath(),
246               DataSourceType.FILE));
247       alignment.addSequence(file.getSeqsAsArray()[0]);
248     }
249     hmmTemp.delete();
250     stoTemp.delete();
251   }
252   
253   public void hmmbuildWaitTillComplete()
254   {
255     Thread loader = new Thread(this);
256     loader.start();
257
258     while (loader.isAlive())
259     {
260       try
261       {
262         Thread.sleep(500);
263       } catch (Exception ex)
264       {
265       }
266     }
267   }
268 }