JAL-3011 JAL-2719 JAL-1958 add posterior probability scores to score and value annota...
[jalview.git] / src / jalview / hmmer / HMMSearch.java
index a58b949..a3ac034 100644 (file)
@@ -4,6 +4,7 @@ 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;
@@ -195,6 +196,7 @@ public class HMMSearch extends HmmerCommand
     String seqScoreCutoff = null;
     String domScoreCutoff = null;
     databaseName = "Alignment";
+    boolean searchAlignment = false;
 
     if (params != null)
     {
@@ -206,6 +208,18 @@ public class HMMSearch extends HmmerCommand
         {
           seqsToReturn = Integer.parseInt(arg.getValue());
         }
+        else if (MessageManager.getString("action.search").equals(name))
+        {
+          searchAlignment = arg.getValue().equals(
+                  MessageManager.getString(HMMSearch.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(AUTO_ALIGN_SEQS_KEY)
                 .equals(name))
         {
@@ -280,8 +294,9 @@ public class HMMSearch extends HmmerCommand
       args.add(domScoreCutoff);
     }
 
-    if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
-            .equals(dbPath))
+//    if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
+//            .equals(dbPath))
+      if (searchAlignment)
     {
       /*
        * no external database specified for search, so
@@ -326,7 +341,23 @@ public class HMMSearch extends HmmerCommand
       StockholmFile file = new StockholmFile(new FileParse(
               inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
       seqs = file.getSeqsAsArray();
-
+      // look for PP cons and ref seq in alignment only annotation
+      AlignmentAnnotation modelpos = null, ppcons = null;
+      for (AlignmentAnnotation aa : file.getAnnotations())
+      {
+        if (aa.sequenceRef == null)
+        {
+          if (aa.label.equals("Reference Positions")) // RF feature type in
+                                                      // stockholm parser
+          {
+            modelpos = aa;
+          }
+          if (aa.label.equals("Posterior Probability"))
+          {
+            ppcons = aa;
+          }
+        }
+      }
       readTable(searchOutputFile);
 
       int seqCount = Math.min(seqs.length, seqsToReturn);
@@ -341,6 +372,14 @@ public class HMMSearch extends HmmerCommand
       else
       {
         AlignmentI al = new Alignment(hmmAndSeqs);
+        if (ppcons != null)
+        {
+          al.addAnnotation(ppcons);
+        }
+        if (modelpos != null)
+        {
+          al.addAnnotation(modelpos);
+        }
         AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
                 AlignFrame.DEFAULT_HEIGHT);
         String ttl = "hmmSearch of " + databaseName + " using "
@@ -416,13 +455,17 @@ public class HMMSearch extends HmmerCommand
             && !"".equals(line))
     {
       SequenceI seq = seqs[index];
+      AlignmentAnnotation pp = seq
+              .getAlignmentAnnotations("", "Posterior Probability")
+              .get(0);
       Scanner scanner = new Scanner(line);
       String str = scanner.next();
       addScoreAnnotation(str, seq, "hmmsearch E-value",
-              "Full sequence E-value");
+              "Full sequence E-value", pp);
       str = scanner.next();
       addScoreAnnotation(str, seq, "hmmsearch Score",
-              "Full sequence bit score");
+              "Full sequence bit score", pp);
+      seq.removeAlignmentAnnotation(pp);
       scanner.close();
       line = br.readLine();
       index++;
@@ -443,10 +486,38 @@ public class HMMSearch extends HmmerCommand
   protected void addScoreAnnotation(String value, SequenceI seq,
           String label, String description)
   {
+    addScoreAnnotation(value, seq, label, description, null);
+  }
+
+  /**
+   * 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 = new AlignmentAnnotation(label,
+      AlignmentAnnotation annot = null;
+      if (pp == null)
+      {
+        new AlignmentAnnotation(label,
               description, null);
+      }
+      else
+      {
+        annot = new AlignmentAnnotation(pp);
+        annot.label = label;
+        annot.description = description;
+      }
       annot.setCalcId(HMMSEARCH);
       double eValue = Double.parseDouble(value);
       annot.setScore(eValue);