JAL-2629 add ability to select hmmer binaries folder
[jalview.git] / src / jalview / gui / AlignFrame.java
index cce4e67..9b5681b 100644 (file)
@@ -63,6 +63,8 @@ import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.ColourMenuHelper.ColourChangeListener;
 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
+import jalview.hmmer.HMMAlignThread;
+import jalview.hmmer.HMMBuildThread;
 import jalview.io.AlignmentProperties;
 import jalview.io.AnnotationFile;
 import jalview.io.BioJsHTMLOutput;
@@ -87,9 +89,7 @@ import jalview.jbgui.GAlignFrame;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.ResidueColourScheme;
-import jalview.schemes.ResidueProperties;
 import jalview.schemes.TCoffeeColourScheme;
-import jalview.util.Comparison;
 import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.ViewportRanges;
@@ -125,10 +125,9 @@ import java.awt.event.MouseEvent;
 import java.awt.print.PageFormat;
 import java.awt.print.PrinterJob;
 import java.beans.PropertyChangeEvent;
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.URL;
 import java.util.ArrayList;
@@ -139,7 +138,6 @@ import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
-import java.util.Scanner;
 import java.util.Vector;
 
 import javax.swing.JCheckBoxMenuItem;
@@ -148,6 +146,7 @@ import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.swing.JScrollPane;
 import javax.swing.SwingUtilities;
 
@@ -167,6 +166,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   public static final int DEFAULT_HEIGHT = 500;
 
+  boolean autoAlignNewSequences;
+
+  boolean recurring;
+
   /*
    * The currently displayed panel (selected tabbed view if more than one)
    */
@@ -337,6 +340,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     init();
   }
 
+  public void setIsRecurring(boolean status)
+  {
+    recurring = status;
+  }
+
   /**
    * initalise the alignframe from the underlying viewport data and the
    * configurations
@@ -388,6 +396,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
     });
     buildColourMenu();
+    buildHMMERMenu();
 
     if (Desktop.desktop != null)
     {
@@ -494,6 +503,22 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   }
 
+  private void buildHMMERMenu()
+  {
+    hmmerMenu.removeAll();
+
+    hmmerMenu.add(changeHMMERLocation);
+    hmmerMenu.addSeparator();
+
+    hmmerMenu.add(autoAlignSeqs);
+    hmmerMenu.addSeparator();
+
+    hmmerMenu.add(hmmAlign);
+    hmmerMenu.add(hmmBuild);
+    hmmerMenu.add(hmmSearch);
+    
+  }
+
   /**
    * Change the filename and format for the alignment, and enable the 'reload'
    * button functionality.
@@ -789,6 +814,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       ap.av.updateConservation(ap);
       ap.av.updateConsensus(ap);
       ap.av.updateStrucConsensus(ap);
+      ap.av.updateInformation(ap);
     }
   }
 
@@ -1024,6 +1050,44 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   @Override
+  public void hmmBuild_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
+  {
+    new Thread(new HMMBuildThread(this)).start();
+    alignPanel.repaint();
+
+  }
+
+  @Override
+  public void hmmAlign_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
+  {
+    new Thread(new HMMAlignThread(this, true)).start();
+    alignPanel.repaint();
+  }
+
+  @Override
+  public void changeHMMERLocation_actionPerformed(ActionEvent e)
+  {
+    String location = JOptionPane.showInputDialog(
+            MessageManager.getString("label.enter_location"));
+    Cache.setProperty(Preferences.HMMER_PATH, location);
+  }
+
+  @Override
+  public void autoAlignSeqs_actionPerformed(boolean autoAlignSeqs)
+  {
+    autoAlignNewSequences = autoAlignSeqs;
+    alignPanel.repaint();
+  }
+
+  @Override
+  public void hmmSearch_actionPerformed(ActionEvent e)
+  {
+    alignPanel.repaint();
+  }
+
+  @Override
   public void reload_actionPerformed(ActionEvent e)
   {
     if (fileName != null)
@@ -1434,6 +1498,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   @Override
   public void associatedData_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     // Pick the tree file
     JalviewFileChooser chooser = new JalviewFileChooser(
@@ -1925,9 +1990,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param e
    *          DOCUMENT ME!
+   * @throws InterruptedException
+   * @throws IOException
    */
   @Override
   protected void pasteNew_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     paste(true);
   }
@@ -1937,9 +2005,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param e
    *          DOCUMENT ME!
+   * @throws InterruptedException
+   * @throws IOException
    */
   @Override
   protected void pasteThis_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     paste(false);
   }
@@ -1949,8 +2020,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param newAlignment
    *          true to paste to a new alignment, otherwise add to this.
+   * @throws InterruptedException
+   * @throws IOException
    */
-  void paste(boolean newAlignment)
+  void paste(boolean newAlignment) throws IOException, InterruptedException
   {
     boolean externalPaste = true;
     try
@@ -2278,7 +2351,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       System.out.println("Exception whilst pasting: " + ex);
       // could be anything being pasted in here
     }
-
   }
 
   @Override
@@ -4583,9 +4655,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param file
    *          either a filename or a URL string.
+   * @throws InterruptedException
+   * @throws IOException
    */
   public void loadJalviewDataFile(String file, DataSourceType sourceType,
           FileFormatI format, SequenceI assocSeq)
+          throws IOException, InterruptedException
   {
     try
     {
@@ -4666,156 +4741,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           }
           else if (FileFormat.HMMER3.equals(format))
           {
-            HMMFile hmmFile = new HMMFile(new FileParse(file, sourceType)); // TODO
-                                                                            // need
-                                                                            // to
-                                                                            // follow
-                                                                            // standard
-                                                                            // pipeline
-            hmmFile.parse();
+            HMMFile hmmFile = new HMMFile(new FileParse(file, sourceType));
             HiddenMarkovModel hmm = hmmFile.getHMM();
-            AlignmentAnnotation annotation = hmm.createAnnotation(
-                    getViewport().getAlignment().getWidth());
-            getViewport().getAlignment().addAnnotation(annotation);
-            annotation.setHMM(hmm);
-            isAnnotation = true;
-
-            BufferedReader input = new BufferedReader(new FileReader(
-                    "H:/Desktop/Distributions/BadAlignment-8.csv"));
-            String line = input.readLine();
-
-            while (!("".equals(line)) && line != null)
-            {
-              Scanner scanner = new Scanner(line);
-              if (scanner.hasNext())
-              {
-                scanner.useDelimiter(",");
-                String value = scanner.next();
-
-                distribution.put(value, scanner.nextFloat());
-                line = input.readLine();
-              }
-            }
-
-
-            AlignmentI alignment = getViewport().getAlignment();
-            Integer alpha = 0;
-            final int AMINO = 0;
-            final int DNA = 1;
-            if ("amino".equals(hmm.getAlphabetType()))
-            {
-              alpha = AMINO;
-            }
-            else if ("DNA".equals(hmm.getAlphabetType()))
-            {
-              alpha = DNA;
-            }
-            
-
-            int size = 0;
-
-            for (int l = 1; l < hmm.getLength() + 1; l++)
-            {
-              for (int n = 0; n < alignment.getHeight(); n++)
-              {
-
-                char character = alignment.getSequenceAt(n)
-                        .getCharAt(hmm.getNodeAlignmentColumn(l));
-                character = Character.toUpperCase(character);
-
-                boolean containedN;
-                boolean containedA;
-
-                containedN = ResidueProperties.nucleotideBackgroundFrequencies
-                        .containsKey(character);
-                containedA = ResidueProperties.aminoBackgroundFrequencies
-                        .containsKey(character);
-
-                if (!Comparison.isGap(character)
-                        && ((alpha == DNA && containedN)
-                                || (alpha == AMINO && containedA)))
-                {
-                  size++;
-                }
-
-              }
-            }
-            
-            for (int l = 1; l < hmm.getLength() + 1; l++)
-            {
-              for (int n = 0; n < alignment.getHeight(); n++)
-              {
-                Double prob;
-                char character;
-                character = alignment.getSequenceAt(n)
-                        .getCharAt(hmm.getNodeAlignmentColumn(l));
-                character = Character.toUpperCase(character);
-                boolean containedN;
-                boolean containedA;
-
-                containedN = ResidueProperties.nucleotideBackgroundFrequencies
-                        .containsKey(character);
-                containedA = ResidueProperties.aminoBackgroundFrequencies
-                        .containsKey(character);
-
-                if (!Comparison.isGap(character)
-                        && ((alpha == DNA && containedN)
-                                || (alpha == AMINO && containedA)))
-                {
-                  prob = hmm.getMatchEmissionProbability(
-                          hmm.getNodeAlignmentColumn(l), character);
-                  if (prob == 0d)
-                  {
-                    System.out.println("?");
-                  }
-
-                  double freq = 0;
-                  if (alpha == AMINO)
-                  {
-                    freq = ResidueProperties.aminoBackgroundFrequencies
-                            .get(character);
-                  }
-                  if (alpha == DNA)
-                  {
-                    freq = ResidueProperties.nucleotideBackgroundFrequencies
-                            .get(character);
-                  }
-                    Double doubleValue = Math.log(prob / freq);
-
-                      String value = String.format("%.1f", doubleValue);
-                      if ("-0.0".equals(value))
-                      {
-                        value = "0.0";
-                      }
-                        if (distribution.containsKey(value))
-                        {
-                        float prev = distribution.get(value);
-                        prev = prev + (10000f / size);
-                          distribution.put(value, prev);
-                        }
-                        else
-                        {
-                    distribution.put(value, 10000f / size);
-                        }
-
-
-                }
-            
-              }
-            }
-            
-            PrintWriter writer = new PrintWriter(
-                    new File(
-                            "H:/Desktop/Distributions/BadAlignment-8.csv"));
-            for (Map.Entry<String, Float> entry : distribution
-                    .entrySet())
-            {
-              writer.println(entry.getKey() + "," + entry.getValue());
-            
-            }
-            
-            writer.close();
+            SequenceI hmmSeq = hmm.initHMMSequence(this, 0);
+            getViewport().initInformation(hmmSeq);
+            getViewport().updateInformation(alignPanel);
 
+            isAnnotation = true;
 
           }
           else if (FileFormat.Jnet.equals(format))
@@ -4842,12 +4774,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           else
           {
             new FileLoader().LoadFile(viewport, file, sourceType, format);
+            if (autoAlignNewSequences && !recurring)
+            {
+              new Thread(new HMMAlignThread(this, false)).start();
+            }
           }
         }
       }
       if (isAnnotation)
       {
-
         alignPanel.adjustAnnotationHeight();
         viewport.updateSequenceIdColours();
         buildSortByAnnotationScoresMenu();
@@ -5827,4 +5762,6 @@ class PrintThread extends Thread
       }
     }
   }
+
+
 }