JAL-2629 add hmmsearch class
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Tue, 22 Aug 2017 15:32:36 +0000 (16:32 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Tue, 22 Aug 2017 15:32:36 +0000 (16:32 +0100)
src/jalview/hmmer/HMMSearchThread.java [new file with mode: 0644]

diff --git a/src/jalview/hmmer/HMMSearchThread.java b/src/jalview/hmmer/HMMSearchThread.java
new file mode 100644 (file)
index 0000000..0052dc2
--- /dev/null
@@ -0,0 +1,276 @@
+package jalview.hmmer;
+
+import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenMarkovModel;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+import jalview.gui.JvOptionPane;
+import jalview.gui.Preferences;
+import jalview.io.DataSourceType;
+import jalview.io.FileParse;
+import jalview.io.StockholmFile;
+import jalview.util.MessageManager;
+import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
+import jalview.ws.params.ArgumentI;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import javax.swing.JOptionPane;
+
+public class HMMSearchThread implements Runnable
+{
+  /**
+   * feature settings from view that job was associated with
+   */
+  protected FeatureRendererSettings featureSettings = null;
+
+  /**
+   * Object containing frequently used commands.
+   */
+  HMMERCommands cmds = new HMMERCommands();
+
+  AlignFrame af;
+
+  HiddenMarkovModel hmm;
+
+  boolean newFrame;
+
+  long barID;
+
+  SequenceI hmmSeq;
+
+  List<ArgumentI> params;
+
+  File hmmTemp = null;
+
+  File inputAlignmentTemp = null;
+
+  File inputTableTemp = null;
+
+  File databaseFile = null;
+
+  SequenceI[] seqs;
+
+
+  /**
+   * Constructor for the HMMSearchThread. If create new frame is set to true, a
+   * new frame will be created.
+   * 
+   * @param af
+   * @param createNewFrame
+   */
+  public HMMSearchThread(AlignFrame af, boolean createNewFrame,
+          List<ArgumentI> args)
+  {
+    this.af = af;
+    newFrame = createNewFrame;
+    featureSettings = af.getFeatureRenderer().getSettings();
+    params = args;
+  }
+
+  /**
+   * Runs the HMMSearchThread: the data on the alignment or group is exported,
+   * then the command is executed in the command line and then the data is
+   * imported and displayed in a new frame (if true).
+   */
+  @Override
+  public void run()
+  {
+    if (af.getSelectedHMM() == null)
+    {
+      JOptionPane.showMessageDialog(af,
+              MessageManager.getString("warn.no_selected_hmm"));
+      return;
+    }
+    else
+    {
+      hmm = af.getSelectedHMM();
+    }
+
+    hmmSeq = af.getSelectedHMMSequence();
+    barID = System.currentTimeMillis();
+    af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
+            barID);
+    cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
+
+    try
+    {
+      createTemporaryFiles();
+    } catch (IOException e2)
+    {
+      e2.printStackTrace();
+    }
+    try
+    {
+      cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile());
+    } catch (IOException e1)
+    {
+      e1.printStackTrace();
+    }
+    try
+    {
+      boolean ran = runCommand();
+      if (!ran)
+      {
+        JvOptionPane.showInternalMessageDialog(af,
+                MessageManager.getString("warn.hmmsearch_failed"));
+        return;
+      }
+    } catch (IOException | InterruptedException e)
+    {
+      e.printStackTrace();
+    }
+    try
+    {
+      importData();
+    } catch (IOException | InterruptedException e)
+    {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    }
+
+    af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
+            barID);
+
+  }
+
+  /**
+   * Creates temporary files for exporting and importing the data.
+   * 
+   * @throws IOException
+   */
+  private void createTemporaryFiles() throws IOException
+  {
+    hmmTemp = File.createTempFile("hmm", ".hmm");
+    hmmTemp.deleteOnExit();
+    inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
+    inputAlignmentTemp.deleteOnExit();
+    inputTableTemp = File.createTempFile("buffer", ".sto");
+    inputTableTemp.deleteOnExit();
+    databaseFile = new File("D:/Thomas/uniref50.fasta");
+  }
+
+  /**
+   * Executes the hmmsearch command in the command line.
+   * 
+   * @return
+   * @throws IOException
+   * @throws InterruptedException
+   */
+  private boolean runCommand() throws IOException, InterruptedException
+  {
+    File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
+    if (!file.canExecute())
+    {
+      file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
+      {
+        if (!file.canExecute())
+        {
+          return false;
+        }
+      }
+    }
+    String command = cmds.HMMERFOLDER + "/hmmsearch -o "
+            + inputTableTemp.getAbsolutePath() + " -A "
+            + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE
+            + hmmTemp.getAbsolutePath() + cmds.SPACE
+            + databaseFile.getAbsolutePath();
+    return cmds.runCommand(command);
+  }
+
+  /**
+   * Imports the data from the temporary file to which the output of hmmsearch
+   * is directed.
+   */
+  private void importData() throws IOException, InterruptedException
+  {
+    StockholmFile file = new StockholmFile(new FileParse(
+            inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
+    seqs = file.getSeqsAsArray();
+
+    readTable();
+
+    SequenceI[] hmmAndSeqs = new SequenceI[seqs.length + 1];
+    AlignmentAnnotation[] list = hmmSeq.getAnnotation();
+    for (AlignmentAnnotation annot : list)
+    {
+      if ("HMM annotation".equals(annot.getCalcId()))
+      {
+        hmmSeq.removeAlignmentAnnotation(annot);
+      }
+    }
+    hmmSeq.setHasInfo(false);
+    hmmAndSeqs[0] = hmmSeq;
+    System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
+    AlignmentI alignment = new Alignment(hmmAndSeqs);
+    AlignFrame frame = new AlignFrame(alignment, 1, 1);
+    frame.setSelectedHMMSequence(hmmSeq);
+    frame.getViewport().initInformation();
+    HMMAlignThread hmmalign = new HMMAlignThread(frame, true);
+    hmmalign.hmmalignWaitTillComplete();
+    frame = null;
+    hmmTemp.delete();
+    inputAlignmentTemp.delete();
+    inputTableTemp.delete();
+  }
+
+  /**
+   * Runs hmmsearch, and waits for the results to be imported before continuing
+   */
+  public void hmmsearchWaitTillComplete()
+  {
+    Thread loader = new Thread(this);
+    loader.start();
+
+    while (loader.isAlive())
+    {
+      try
+      {
+        Thread.sleep(500);
+      } catch (Exception ex)
+      {
+      }
+    }
+
+  }
+
+  public void readTable() throws IOException
+  {
+
+    /*
+    BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
+    String line = "";
+    while (!line.startsWith("Query:"))
+    {
+      line = br.readLine();
+    }
+    for (int i = 0; i < 4; i++)
+    {
+      br.readLine();
+    }
+    
+    
+    int index = 0;
+    while (!"  ------ inclusion threshold ------".equals(line))
+    {
+      Scanner scanner = new Scanner(line);
+    
+      String str = scanner.next();
+      AlignmentAnnotation annots = new AlignmentAnnotation("", "", null);
+      annots.setScore(Double.parseDouble(str));
+      seqs[index].addAlignmentAnnotation(annots);
+    
+      scanner.close();
+    }
+    
+    br.close();
+    */
+
+  }
+
+}