JAL-2629 fix hmm on alignment being deleted upon running jackhmmer
[jalview.git] / src / jalview / hmmer / HMMSearch.java
index 6cda421..64802c7 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;
@@ -33,37 +34,6 @@ public class HMMSearch extends HmmerCommand
 {
   static final String HMMSEARCH = "hmmsearch";
 
-  /*
-   * constants for i18n lookup of passed parameter names
-   */
-  static final String DATABASE_KEY = "label.database";
-
-  static final String THIS_ALIGNMENT_KEY = "label.this_alignment";
-
-  static final String USE_ACCESSIONS_KEY = "label.use_accessions";
-
-  static final String AUTO_ALIGN_SEQS_KEY = "label.auto_align_seqs";
-
-  static final String NUMBER_OF_RESULTS_KEY = "label.number_of_results";
-
-  static final String TRIM_TERMINI_KEY = "label.trim_termini";
-
-  static final String REPORTING_CUTOFF_KEY = "label.reporting_cutoff";
-
-  static final String CUTOFF_NONE = "None";
-
-  static final String CUTOFF_SCORE = "Score";
-
-  static final String CUTOFF_EVALUE = "E-Value";
-
-  static final String SEQ_EVALUE_KEY = "label.seq_evalue";
-
-  static final String DOM_EVALUE_KEY = "label.dom_evalue";
-
-  static final String SEQ_SCORE_KEY = "label.seq_score";
-
-  static final String DOM_SCORE_KEY = "label.dom_score";
-
   boolean realign = false;
 
   boolean trim = false;
@@ -103,7 +73,7 @@ public class HMMSearch extends HmmerCommand
 
     SequenceI hmmSeq = hmm.getConsensusSequence();
     long msgId = System.currentTimeMillis();
-    af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
+    af.setProgressBar(MessageManager.getString("status.running_search"),
             msgId);
 
     try
@@ -180,9 +150,9 @@ public class HMMSearch extends HmmerCommand
           File hitsAlignmentFile, File hmmFile) throws IOException
   {
     args.add("-o");
-    args.add(getFilePath(searchOutputFile));
+    args.add(getFilePath(searchOutputFile, true));
     args.add("-A");
-    args.add(getFilePath(hitsAlignmentFile));
+    args.add(getFilePath(hitsAlignmentFile, true));
 
     boolean dbFound = false;
     String dbPath = "";
@@ -305,16 +275,12 @@ public class HMMSearch extends HmmerCommand
       databaseFile = FileUtils.createTempFile("database", ".sto");
       AlignmentI al = af.getViewport().getAlignment();
       AlignmentI copy = new Alignment(al);
-      List<SequenceI> hmms = copy.getHmmSequences();
-      for (SequenceI hmmSeq : hmms)
-      {
-        copy.deleteSequence(hmmSeq);
-      }
+      deleteHmmSequences(copy);
       exportStockholm(copy.getSequencesArray(), databaseFile, null);
     }
 
-    args.add(getFilePath(hmmFile));
-    args.add(getFilePath(databaseFile));
+    args.add(getFilePath(hmmFile, true));
+    args.add(getFilePath(databaseFile, true));
   }
 
   /**
@@ -340,14 +306,55 @@ 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);
       SequenceI[] hmmAndSeqs = new SequenceI[seqCount + 1];
+      hmmSeq = hmmSeq.deriveSequence(); // otherwise all bad things happen
       hmmAndSeqs[0] = hmmSeq;
       System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqCount);
-
+      if (modelpos != null)
+      {
+        // TODO need - get ungapped sequence method
+        hmmSeq.setSequence(
+                hmmSeq.getDatasetSequence().getSequenceAsString());
+        Annotation[] refpos = modelpos.annotations;
+        // insert gaps to match with refseq positions
+        int gc = 0, lcol = 0;
+        for (int c = 0; c < refpos.length; c++)
+        {
+          if (refpos[c] != null && ("x".equals(refpos[c].displayCharacter)))
+          {
+            if (gc > 0)
+            {
+              hmmSeq.insertCharAt(lcol + 1, gc, '-');
+            }
+            gc = 0;
+            lcol = c;
+          }
+          else
+          {
+            gc++;
+          }
+        }
+      }
       if (realign)
       {
         realignResults(hmmAndSeqs);
@@ -355,6 +362,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 "
@@ -420,10 +435,11 @@ public class HMMSearch extends HmmerCommand
     {
       line = br.readLine();
     }
-    for (int i = 0; i < 5; i++)
+    while (!line.contains("-------"))
     {
       line = br.readLine();
     }
+    line = br.readLine();
 
     int index = 0;
     while (!"  ------ inclusion threshold ------".equals(line)
@@ -431,12 +447,9 @@ public class HMMSearch extends HmmerCommand
     {
       SequenceI seq = seqs[index];
       Scanner scanner = new Scanner(line);
-      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");
+      String evalue = scanner.next();
+      String score = scanner.next();
+      addScoreAnnotations(evalue, score, seq);
       scanner.close();
       line = br.readLine();
       index++;
@@ -445,30 +458,35 @@ 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)
+
+  protected void addScoreAnnotations(String eValue, String bitScore,
+          SequenceI seq)
   {
+    String label = "Search Scores";
+    String description = "Full sequence bit score and E-Value";
+
     try
     {
       AlignmentAnnotation annot = new AlignmentAnnotation(label,
               description, null);
+
+      annot.label = label;
+      annot.description = description;
+
       annot.setCalcId(HMMSEARCH);
-      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);
     }
   }