JAL-2629 fix hmmsearch/jackhmmer being unable to search through DB file
[jalview.git] / src / jalview / hmmer / JackHMMER.java
index 85b97f9..4435b14 100644 (file)
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Scanner;
 
@@ -39,6 +40,10 @@ public class JackHMMER extends HmmerCommand
 
   private String databaseName;
 
+  boolean searchAlignment = true;
+
+  Hashtable sequencesHash;
+
   /**
    * Constructor for the JackhmmerThread
    * 
@@ -72,13 +77,14 @@ public class JackHMMER extends HmmerCommand
 
     try
     {
-      File seqFile = FileUtils.createTempFile("seq", ".fa");
+      File seqFile = FileUtils.createTempFile("seq", ".sto");
       File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment",
               ".sto");
       File searchOutputFile = FileUtils.createTempFile("searchOutput",
               ".txt");
 
-      exportSequence(seq, seqFile.getAbsoluteFile());
+      exportStockholm(new SequenceI[] { seq }, seqFile.getAbsoluteFile(),
+              null);
 
       boolean ran = runCommand(searchOutputFile, hitsAlignmentFile,
               seqFile);
@@ -159,7 +165,6 @@ public class JackHMMER extends HmmerCommand
     String seqScoreCutoff = null;
     String domScoreCutoff = null;
     databaseName = "Alignment";
-    boolean searchAlignment = false;
 
     if (params != null)
     {
@@ -167,26 +172,16 @@ public class JackHMMER extends HmmerCommand
       {
         String name = arg.getName();
 
-        if (MessageManager.getString("action.search").equals(name))
-        {
-          searchAlignment = arg.getValue().equals(
-                  MessageManager.getString(JackHMMER.THIS_ALIGNMENT_KEY));
-        }
-        else if (MessageManager.getString(DATABASE_KEY).equals(name))
-        {
-          dbPath = arg.getValue();
-          int pos = dbPath.lastIndexOf(File.separator);
-          databaseName = dbPath.substring(pos + 1);
-          databaseFile = new File(dbPath);
-        }
-        else if (MessageManager.getString(REPORTING_CUTOFF_KEY)
+        if (MessageManager.getString(REPORTING_CUTOFF_KEY)
                 .equals(name))
         {
-          if (CUTOFF_EVALUE.equals(arg.getValue()))
+          if (MessageManager.getString(CUTOFF_EVALUE)
+                  .equals(arg.getValue()))
           {
             useEvalueCutoff = true;
           }
-          else if (CUTOFF_SCORE.equals(arg.getValue()))
+          else if (MessageManager.getString(CUTOFF_SCORE)
+                  .equals(arg.getValue()))
           {
             useScoreCutoff = true;
           }
@@ -217,6 +212,7 @@ public class JackHMMER extends HmmerCommand
             databaseName = dbPath.substring(pos + 1);
             databaseFile = new File(dbPath);
           }
+          searchAlignment = false;
         }
       }
     }
@@ -244,9 +240,15 @@ public class JackHMMER extends HmmerCommand
        * no external database specified for search, so
        * export current alignment as 'database' to search
        */
-      databaseFile = FileUtils.createTempFile("database", ".fa");
+      databaseFile = FileUtils.createTempFile("database", ".sto");
       AlignmentI al = af.getViewport().getAlignment();
-      exportFasta(al.getSequencesArray(), databaseFile);
+      AlignmentI copy = new Alignment(al);
+
+      deleteHmmSequences(copy);
+
+      sequencesHash = stashSequences(copy.getSequencesArray());
+
+      exportStockholm(copy.getSequencesArray(), databaseFile, null);
     }
 
     args.add(getFilePath(seqFile, true));
@@ -274,6 +276,11 @@ public class JackHMMER extends HmmerCommand
               inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
       seqs = file.getSeqsAsArray();
 
+      if (searchAlignment)
+      {
+        recoverSequences(sequencesHash, seqs);
+      }
+
       readTable(searchOutputFile);
 
       int seqCount = Math.min(seqs.length, seqsToReturn);
@@ -325,22 +332,12 @@ public class JackHMMER extends HmmerCommand
             && !"".equals(line))
     {
       SequenceI seq = seqs[index];
-      AlignmentAnnotation pp = null;
-      if (seq.getAlignmentAnnotations("", "Posterior Probability")
-              .size() != 0)
-      {
-        pp = seq.getAlignmentAnnotations("", "Posterior Probability")
-                .get(0);
-      }
+
       Scanner scanner = new Scanner(line);
-      String str = scanner.next();
-      str = scanner.next();
-      addScoreAnnotation(str, seq, "jackhmmer E-value",
-              "Full sequence E-value", pp);
-      str = scanner.next();
-      addScoreAnnotation(str, seq, "jackhmmer Score",
-              "Full sequence bit score", pp);
-      seq.removeAlignmentAnnotation(pp);
+      String evalue = scanner.next();
+      evalue = scanner.next();
+      String score = scanner.next();
+      addScoreAnnotations(evalue, score, seq);
       scanner.close();
       line = br.readLine();
       index++;
@@ -349,58 +346,38 @@ public class JackHMMER 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)
+  protected void addScoreAnnotations(String eValue, String bitScore,
+          SequenceI seq)
   {
-    addScoreAnnotation(value, seq, label, description, null);
-  }
+    String label = "Search Scores";
+    String description = "Full sequence bit score and E-Value";
 
-  /**
-   * A helper method that adds one score-only (non-positional) annotation to a
-   * sequence
-   * 
-   * @param value
-   * @param seq
-   * @param label
-   * @param description
-   * @param pp
-   *                      existing posterior probability annotation - values
-   *                      copied to new annotation row
-   */
-  protected void addScoreAnnotation(String value, SequenceI seq,
-          String label, String description, AlignmentAnnotation pp)
-  {
     try
     {
-      AlignmentAnnotation annot = null;
-      if (pp == null)
-      {
-        annot = new AlignmentAnnotation(label, description, null);
-      }
-      else
-      {
-        annot = new AlignmentAnnotation(pp);
-        annot.label = label;
-        annot.description = description;
-      }
+      AlignmentAnnotation annot = new AlignmentAnnotation(label,
+              description, null);
+
+      annot.label = label;
+      annot.description = description;
+
       annot.setCalcId(JACKHMMER);
-      double eValue = Double.parseDouble(value);
-      annot.setScore(eValue);
+
+      double dEValue = Double.parseDouble(eValue);
+      annot.setEValue(dEValue);
+
+      double dBitScore = Double.parseDouble(bitScore);
+      annot.setBitScore(dBitScore);
+
       annot.setSequenceRef(seq);
       seq.addAlignmentAnnotation(annot);
+
     } catch (NumberFormatException e)
     {
-      System.err.println("Error parsing " + label + " from " + value);
+      System.err.println("Error parsing " + label + " from " + eValue
+              + " & " + bitScore);
     }
   }
 
+
+
 }