JAL-2719 parse bit score annotation from search results
[jalview.git] / src / jalview / hmmer / HMMSearch.java
index efc34ae..4c5bfb3 100644 (file)
@@ -4,10 +4,10 @@ import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Annotation;
 import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
 import jalview.gui.JvOptionPane;
 import jalview.io.DataSourceType;
 import jalview.io.FileParse;
@@ -72,6 +72,8 @@ public class HMMSearch extends HmmerCommand
 
   SequenceI[] seqs;
 
+  private String databaseName;
+
   /**
    * Constructor for the HMMSearchThread
    * 
@@ -99,7 +101,7 @@ public class HMMSearch extends HmmerCommand
       return;
     }
 
-    SequenceI hmmSeq = hmm.getConsensusSequence();// af.getSelectedHMMSequence();
+    SequenceI hmmSeq = hmm.getConsensusSequence();
     long msgId = System.currentTimeMillis();
     af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
             msgId);
@@ -173,6 +175,7 @@ public class HMMSearch extends HmmerCommand
     String domEvalueCutoff = null;
     String seqScoreCutoff = null;
     String domScoreCutoff = null;
+    databaseName = "Alignment";
 
     if (params != null)
     {
@@ -187,7 +190,7 @@ public class HMMSearch extends HmmerCommand
         else if (MessageManager.getString(AUTO_ALIGN_SEQS_KEY)
                 .equals(name))
         {
-          realign = true; // TODO: not used
+          realign = true;
         }
         else if (MessageManager.getString(USE_ACCESSIONS_KEY)
                 .equals(name))
@@ -235,6 +238,8 @@ public class HMMSearch extends HmmerCommand
           if (!MessageManager.getString(THIS_ALIGNMENT_KEY)
                   .equals(dbPath))
           {
+            int pos = dbPath.lastIndexOf(File.separator);
+            databaseName = dbPath.substring(pos + 1);
             databaseFile = new File(dbPath);
           }
         }
@@ -283,7 +288,7 @@ public class HMMSearch extends HmmerCommand
 
   /**
    * Imports the data from the temporary file to which the output of hmmsearch
-   * is directed.
+   * was directed. The results are optionally realigned using hmmalign.
    * 
    * @param hmmSeq
    */
@@ -312,27 +317,21 @@ public class HMMSearch extends HmmerCommand
       hmmAndSeqs[0] = hmmSeq;
       System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqCount);
 
-      /*
-       * and align the search results to the HMM profile
-       */
-      AlignmentI al = new Alignment(hmmAndSeqs);
-      AlignFrame frame = new AlignFrame(al, 1, 1);
-      List<ArgumentI> alignArgs = new ArrayList<>();
-      String defSeq = hmmSeq.getName();
-      List<String> options = Collections.singletonList(defSeq);
-      Option option = new Option(MessageManager.getString("label.use_hmm"),
-              "", true, defSeq, defSeq, options, null);
-      alignArgs.add(option);
-      if (trim)
+      if (realign)
+      {
+        realignResults(hmmAndSeqs);
+      }
+      else
       {
-        alignArgs.add(new BooleanOption(
-                MessageManager.getString(TRIM_TERMINI_KEY),
-                MessageManager.getString("label.trim_termini_desc"), true,
-                true, true, null));
+        AlignmentI al = new Alignment(hmmAndSeqs);
+        AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
+                AlignFrame.DEFAULT_HEIGHT);
+        String ttl = "hmmSearch of " + databaseName + " using "
+                + hmmSeq.getName();
+        Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
+                AlignFrame.DEFAULT_HEIGHT);
       }
-      HmmerCommand hmmalign = new HMMAlign(frame, alignArgs);
-      hmmalign.run();
-      frame = null;
+
       hmmTemp.delete();
       inputAlignmentTemp.delete();
       searchOutputFile.delete();
@@ -345,6 +344,43 @@ public class HMMSearch extends HmmerCommand
     }
   }
 
+  /**
+   * Realigns the given sequences using hmmalign, to the HMM profile sequence
+   * which is the first in the array, and opens the results in a new frame
+   * 
+   * @param hmmAndSeqs
+   */
+  protected void realignResults(SequenceI[] hmmAndSeqs)
+  {
+    /*
+     * and align the search results to the HMM profile
+     */
+    AlignmentI al = new Alignment(hmmAndSeqs);
+    AlignFrame frame = new AlignFrame(al, 1, 1);
+    List<ArgumentI> alignArgs = new ArrayList<>();
+    String alignTo = hmmAndSeqs[0].getName();
+    List<String> options = Collections.singletonList(alignTo);
+    Option option = new Option(MessageManager.getString("label.use_hmm"),
+            "", true, alignTo, alignTo, options, null);
+    alignArgs.add(option);
+    if (trim)
+    {
+      alignArgs.add(new BooleanOption(
+              MessageManager.getString(TRIM_TERMINI_KEY),
+              MessageManager.getString("label.trim_termini_desc"), true,
+              true, true, null));
+    }
+    HmmerCommand hmmalign = new HMMAlign(frame, alignArgs);
+    hmmalign.run();
+  }
+
+  /**
+   * Reads in the scores table output by hmmsearch and adds annotation to
+   * sequences for E-value and bit score
+   * 
+   * @param inputTableTemp
+   * @throws IOException
+   */
   void readTable(File inputTableTemp) throws IOException
   {
     BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
@@ -362,22 +398,14 @@ public class HMMSearch extends HmmerCommand
     while (!"  ------ inclusion threshold ------".equals(line)
             && !"".equals(line))
     {
+      SequenceI seq = seqs[index];
       Scanner scanner = new Scanner(line);
-
-      String str = scanner.next(); // full sequence eValue score
-      float eValue = Float.parseFloat(str);
-      int seqLength = seqs[index].getLength();
-      Annotation[] annots = new Annotation[seqLength];
-      for (int j = 0; j < seqLength; j++)
-      {
-        annots[j] = new Annotation(eValue);
-      }
-      AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
-              "Score", annots);
-      annot.setScore(Double.parseDouble(str));
-      annot.setSequenceRef(seqs[index]);
-      seqs[index].addAlignmentAnnotation(annot);
-
+      String str = scanner.next();
+      addScoreAnnotation(str, seq, "hmmsearch E-value",
+              "Full sequence E-value");
+      str = scanner.next();
+      addScoreAnnotation(str, seq, "hmmsearch Score",
+              "Full sequence bit score");
       scanner.close();
       line = br.readLine();
       index++;
@@ -386,4 +414,31 @@ public class HMMSearch extends HmmerCommand
     br.close();
   }
 
+  /**
+   * A helper method that adds one score-only (non-positional) annotation to a
+   * sequence
+   * 
+   * @param value
+   * @param seq
+   * @param label
+   * @param description
+   */
+  protected void addScoreAnnotation(String value, SequenceI seq,
+          String label, String description)
+  {
+    try
+    {
+      AlignmentAnnotation annot = new AlignmentAnnotation(label,
+              description, null);
+      annot.setCalcId(HMMSEARCH);
+      double eValue = Double.parseDouble(value);
+      annot.setScore(eValue);
+      annot.setSequenceRef(seq);
+      seq.addAlignmentAnnotation(annot);
+    } catch (NumberFormatException e)
+    {
+      System.err.println("Error parsing " + label + " from " + value);
+    }
+  }
+
 }