JAL-2629 multiple HMMs can now be dropped onto an alignment
[jalview.git] / src / jalview / hmmer / HMMERCommands.java
index 3981f58..3f50bbb 100644 (file)
@@ -1,7 +1,6 @@
 package jalview.hmmer;
 
 import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.SequenceI;
@@ -19,6 +18,7 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.util.List;
+import java.util.Map;
 
 import javax.swing.JOptionPane;
 
@@ -27,101 +27,122 @@ public class HMMERCommands
   // Path of hmmer binaries directory
   private static final String HMMERFOLDER = "H:/Documents/";
 
-  private static final String HMMALIGN = HMMERFOLDER + "hmmalign";
+  private static final String HMMALIGN = HMMERFOLDER + "hmmalign ";
 
-  private static final String HMMBUILD = HMMERFOLDER + "hmmbuild";
+  private static final String HMMBUILD = HMMERFOLDER + "hmmbuild ";
 
-  private static final String HMMSEARCH = HMMERFOLDER + "hmmsearch";
+  private static final String HMMSEARCH = HMMERFOLDER + "hmmsearch ";
 
   private static final String JALVIEWDIRECTORY = "C:/Users/TZVanaalten/git/jalview/";
 
-  private static final String HMMBUFFER = "src/jalview/hmmer/hmm_buffer.hmm";
+  private static final String HMMBUFFER = "src/jalview/hmmer/hmm_buffer.hmm ";
 
-  private static final String ALIGNMENTBUFFER = "src/jalview/hmmer/alignment_buffer.sto";
+  private static final String ALIGNMENTBUFFER = "src/jalview/hmmer/alignment_buffer.sto ";
 
-  private static final String ALIGNMENTINPUT = "src/jalview/hmmer/alignment_input.sto";
+  private static final String ALIGNMENTINPUT = "src/jalview/hmmer/alignment_input.sto ";
 
-  private static final String OUTPUTALIGNMENT = " -o " + JALVIEWDIRECTORY
+  private static final String OUTPUTALIGNMENT = "-o " + JALVIEWDIRECTORY
           + ALIGNMENTINPUT;
 
+  private static final String NAME = "-n ";
+
   private static final String SPACE = " ";
 
-  private static final String ALLCOL = " --allcol";
+  private static final String ALLCOL = "--allcol ";
 
-  private static final String TRIM = " --trim";
+  private static final String TRIM = "--trim ";
 
   public static void hmmBuild(AlignFrame af)
           throws IOException, InterruptedException
   {
-    HiddenMarkovModel hmm = null;
+
+    PrintWriter clearer = new PrintWriter(HMMBUFFER);
+    clearer.print("");
+    clearer.close();
     AlignmentI alignment = af.getViewport().getAlignment();
+    Map<Integer, SequenceI> seqs = alignment.getHMMConsensusSequences(true);
     exportAlignment(alignment);
-    final String command = HMMBUILD + SPACE + JALVIEWDIRECTORY + HMMBUFFER
-            + SPACE
+    final String command = HMMBUILD + NAME + af.getName() + SPACE
+            + JALVIEWDIRECTORY + HMMBUFFER
             + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
 
-    removeHMMData(alignment);
     runCommand(command);
 
     af.loadJalviewDataFile(HMMBUFFER, DataSourceType.FILE,
             FileFormat.HMMER3, null);
+    for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
+    {
+      SequenceI seq = entry.getValue();
+      Integer pos = entry.getKey();
+      addHMMConsensusSequence(af, seq, pos);
+    }
+    af.alignPanel.alignmentChanged();
   }
 
-  public static void hmmAlign(AlignFrame af)
+  public static void hmmAlign(AlignFrame af, boolean createNewFrame,
+          HiddenMarkovModel hmm)
           throws IOException, InterruptedException
   {
+
+    PrintWriter clearer = new PrintWriter(ALIGNMENTINPUT);
+    clearer.print("");
+    clearer.close();
+
     AlignmentI al = af.getViewport().getAlignment();
-    List<SequenceI> seqs = al.getSequences();
-    HiddenMarkovModel hmm = null;
+    Map<Integer, SequenceI> seqs = al.getHMMConsensusSequences(true);
     int index = 0;
-    for (SequenceI seq : seqs)
-    {
-      if (seq.getHMM() != null)
-      {
-        hmm = seq.getHMM();
-        seqs.remove(index);
-        break;
-      }
-      index++;
-    }
     if (hmm == null)
     {
       JOptionPane.showMessageDialog(af,
               MessageManager.getString("warn.null_hmm"));
       return;
     }
-
     exportAlignment(al);
-    addHMM(af, hmm);
+
     HMMFile file = new HMMFile(hmm);
     file.exportFile(HMMBUFFER);
 
-    final String command = HMMALIGN + ALLCOL + TRIM
-            + OUTPUTALIGNMENT + SPACE + JALVIEWDIRECTORY + HMMBUFFER + SPACE
+    String command = HMMALIGN;
+    if (!hmm.getFileHeader().contains("HMMER3/f"))
+    {
+      command += ALLCOL;
+    }
+    command += TRIM + OUTPUTALIGNMENT + JALVIEWDIRECTORY + HMMBUFFER
             + JALVIEWDIRECTORY + ALIGNMENTBUFFER;
 
     runCommand(command);
-    FileLoader loader = new FileLoader();
-    AlignFrame newFrame = loader.LoadFileWaitTillLoaded(ALIGNMENTINPUT,
-            DataSourceType.FILE);
-    HiddenMarkovModel copy = new HiddenMarkovModel(hmm);
-    addHMM(newFrame, copy);
-
-    // addHMMData(newFrame, tempHMM);
-    
-    
-    
-    
 
-    //af.loadJalviewDataFile(ALIGNMENTINPUT, DataSourceType.FILE,
-          //  FileFormat.Stockholm, null);
-    //addHMMData(af, tempHMM);
+    if (createNewFrame)
+    {
+      FileLoader loader = new FileLoader();
+      AlignFrame newFrame = loader.LoadFileWaitTillLoaded(ALIGNMENTINPUT,
+            DataSourceType.FILE);
+      for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
+      {
+        SequenceI seq = entry.getValue();
+        Integer pos = entry.getKey();
+        addHMMConsensusSequence(newFrame, seq, pos);
+      }
+      newFrame.alignPanel.alignmentChanged();
+    }
+    else
+    {
+      af.getViewport().getAlignment().getSequences().clear();
+      af.loadJalviewDataFile(ALIGNMENTBUFFER, DataSourceType.FILE,
+              FileFormat.Stockholm, null);
+      for (Map.Entry<Integer, SequenceI> entry : seqs.entrySet())
+      {
+        SequenceI seq = entry.getValue();
+        Integer pos = entry.getKey();
+        addHMMConsensusSequence(af, seq, pos);
+      }
 
-   
+    }
 
 
   }
 
+
   /**
    * Runs a command in the terminal.
    * 
@@ -180,55 +201,16 @@ public class HMMERCommands
     writer.close();
   }
 
-  /**
-   * Removes the HMM sequence and information annotation from the alignment and
-   * stores it in a temporary container while the alignment is operated on.
-   * 
-   * @param alignment
-   * @return
-   */
-  private static SequenceI removeHMMData(AlignmentI alignment)
-  {
-    SequenceI seq = null;
-
-    List<SequenceI> list = alignment.getSequences();
-    for (int i = 0; i < list.size(); i++)
-    {
-      if (list.get(i).getName().contains("HMM CONSENSUS"))
-      {
-        seq = list.get(i);
-        list.remove(i);
-      }
-    }
-
-    AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
-    AlignmentAnnotation[] newAnnots = new AlignmentAnnotation[annots.length
-            - 1];
-    int index = 0;
-    for (AlignmentAnnotation annot : annots)
-    {
-      if (annot.label.contains("Information"))
-      {
-        alignment.deleteAnnotation(annot);
-      }
-    }
-
-
-    return seq;
-  }
-
-  private static void addHMM(AlignFrame af, HiddenMarkovModel hmm)
+  private static void addHMMConsensusSequence(AlignFrame af, SequenceI seq,
+          Integer position)
   {
-    hmm.mapToReferenceAnnotation(af);
-    hmm.initPlaceholder(af);
+    seq.getHMM().initHMMSequence(af, position);
     AlignmentI al = af.getViewport().getAlignment();
-    AlignmentAnnotation annot = hmm.createAnnotation(al.getWidth());
-    al.addAnnotation(annot);
     af.getViewport().setAlignment(al);
     af.alignPanel.adjustAnnotationHeight();
     af.getViewport().updateSequenceIdColours();
     af.buildSortByAnnotationScoresMenu();
-
   }
 
+
 }