0052dc229de21dd0ed04e61595034f3c6dd2bada
[jalview.git] / src / jalview / hmmer / HMMSearchThread.java
1 package jalview.hmmer;
2
3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.HiddenMarkovModel;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.gui.JvOptionPane;
11 import jalview.gui.Preferences;
12 import jalview.io.DataSourceType;
13 import jalview.io.FileParse;
14 import jalview.io.StockholmFile;
15 import jalview.util.MessageManager;
16 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
17 import jalview.ws.params.ArgumentI;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.List;
22
23 import javax.swing.JOptionPane;
24
25 public class HMMSearchThread implements Runnable
26 {
27   /**
28    * feature settings from view that job was associated with
29    */
30   protected FeatureRendererSettings featureSettings = null;
31
32   /**
33    * Object containing frequently used commands.
34    */
35   HMMERCommands cmds = new HMMERCommands();
36
37   AlignFrame af;
38
39   HiddenMarkovModel hmm;
40
41   boolean newFrame;
42
43   long barID;
44
45   SequenceI hmmSeq;
46
47   List<ArgumentI> params;
48
49   File hmmTemp = null;
50
51   File inputAlignmentTemp = null;
52
53   File inputTableTemp = null;
54
55   File databaseFile = null;
56
57   SequenceI[] seqs;
58
59
60   /**
61    * Constructor for the HMMSearchThread. If create new frame is set to true, a
62    * new frame will be created.
63    * 
64    * @param af
65    * @param createNewFrame
66    */
67   public HMMSearchThread(AlignFrame af, boolean createNewFrame,
68           List<ArgumentI> args)
69   {
70     this.af = af;
71     newFrame = createNewFrame;
72     featureSettings = af.getFeatureRenderer().getSettings();
73     params = args;
74   }
75
76   /**
77    * Runs the HMMSearchThread: the data on the alignment or group is exported,
78    * then the command is executed in the command line and then the data is
79    * imported and displayed in a new frame (if true).
80    */
81   @Override
82   public void run()
83   {
84     if (af.getSelectedHMM() == null)
85     {
86       JOptionPane.showMessageDialog(af,
87               MessageManager.getString("warn.no_selected_hmm"));
88       return;
89     }
90     else
91     {
92       hmm = af.getSelectedHMM();
93     }
94
95     hmmSeq = af.getSelectedHMMSequence();
96     barID = System.currentTimeMillis();
97     af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
98             barID);
99     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
100
101     try
102     {
103       createTemporaryFiles();
104     } catch (IOException e2)
105     {
106       e2.printStackTrace();
107     }
108     try
109     {
110       cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile());
111     } catch (IOException e1)
112     {
113       e1.printStackTrace();
114     }
115     try
116     {
117       boolean ran = runCommand();
118       if (!ran)
119       {
120         JvOptionPane.showInternalMessageDialog(af,
121                 MessageManager.getString("warn.hmmsearch_failed"));
122         return;
123       }
124     } catch (IOException | InterruptedException e)
125     {
126       e.printStackTrace();
127     }
128     try
129     {
130       importData();
131     } catch (IOException | InterruptedException e)
132     {
133       // TODO Auto-generated catch block
134       e.printStackTrace();
135     }
136
137     af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
138             barID);
139
140   }
141
142   /**
143    * Creates temporary files for exporting and importing the data.
144    * 
145    * @throws IOException
146    */
147   private void createTemporaryFiles() throws IOException
148   {
149     hmmTemp = File.createTempFile("hmm", ".hmm");
150     hmmTemp.deleteOnExit();
151     inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
152     inputAlignmentTemp.deleteOnExit();
153     inputTableTemp = File.createTempFile("buffer", ".sto");
154     inputTableTemp.deleteOnExit();
155     databaseFile = new File("D:/Thomas/uniref50.fasta");
156   }
157
158   /**
159    * Executes the hmmsearch command in the command line.
160    * 
161    * @return
162    * @throws IOException
163    * @throws InterruptedException
164    */
165   private boolean runCommand() throws IOException, InterruptedException
166   {
167     File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
168     if (!file.canExecute())
169     {
170       file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
171       {
172         if (!file.canExecute())
173         {
174           return false;
175         }
176       }
177     }
178     String command = cmds.HMMERFOLDER + "/hmmsearch -o "
179             + inputTableTemp.getAbsolutePath() + " -A "
180             + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE
181             + hmmTemp.getAbsolutePath() + cmds.SPACE
182             + databaseFile.getAbsolutePath();
183     return cmds.runCommand(command);
184   }
185
186   /**
187    * Imports the data from the temporary file to which the output of hmmsearch
188    * is directed.
189    */
190   private void importData() throws IOException, InterruptedException
191   {
192     StockholmFile file = new StockholmFile(new FileParse(
193             inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
194     seqs = file.getSeqsAsArray();
195
196     readTable();
197
198     SequenceI[] hmmAndSeqs = new SequenceI[seqs.length + 1];
199     AlignmentAnnotation[] list = hmmSeq.getAnnotation();
200     for (AlignmentAnnotation annot : list)
201     {
202       if ("HMM annotation".equals(annot.getCalcId()))
203       {
204         hmmSeq.removeAlignmentAnnotation(annot);
205       }
206     }
207     hmmSeq.setHasInfo(false);
208     hmmAndSeqs[0] = hmmSeq;
209     System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
210     AlignmentI alignment = new Alignment(hmmAndSeqs);
211     AlignFrame frame = new AlignFrame(alignment, 1, 1);
212     frame.setSelectedHMMSequence(hmmSeq);
213     frame.getViewport().initInformation();
214     HMMAlignThread hmmalign = new HMMAlignThread(frame, true);
215     hmmalign.hmmalignWaitTillComplete();
216     frame = null;
217     hmmTemp.delete();
218     inputAlignmentTemp.delete();
219     inputTableTemp.delete();
220   }
221
222   /**
223    * Runs hmmsearch, and waits for the results to be imported before continuing
224    */
225   public void hmmsearchWaitTillComplete()
226   {
227     Thread loader = new Thread(this);
228     loader.start();
229
230     while (loader.isAlive())
231     {
232       try
233       {
234         Thread.sleep(500);
235       } catch (Exception ex)
236       {
237       }
238     }
239
240   }
241
242   public void readTable() throws IOException
243   {
244
245     /*
246     BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
247     String line = "";
248     while (!line.startsWith("Query:"))
249     {
250       line = br.readLine();
251     }
252     for (int i = 0; i < 4; i++)
253     {
254       br.readLine();
255     }
256     
257     
258     int index = 0;
259     while (!"  ------ inclusion threshold ------".equals(line))
260     {
261       Scanner scanner = new Scanner(line);
262     
263       String str = scanner.next();
264       AlignmentAnnotation annots = new AlignmentAnnotation("", "", null);
265       annots.setScore(Double.parseDouble(str));
266       seqs[index].addAlignmentAnnotation(annots);
267     
268       scanner.close();
269     }
270     
271     br.close();
272     */
273
274   }
275
276 }