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