JAL-2629 add multiple options for running hmmbuild on alignment/groups
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Fri, 25 Aug 2017 13:37:20 +0000 (14:37 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Fri, 25 Aug 2017 16:21:05 +0000 (17:21 +0100)
12 files changed:
resources/lang/Messages.properties
src/jalview/datamodel/AlignmentI.java
src/jalview/datamodel/AnnotatedCollectionI.java
src/jalview/datamodel/HiddenMarkovModel.java
src/jalview/datamodel/Sequence.java
src/jalview/datamodel/SequenceCollectionI.java
src/jalview/datamodel/SequenceGroup.java
src/jalview/gui/AlignFrame.java
src/jalview/hmmer/HMMBuildThread.java
src/jalview/hmmer/HMMERParamStore.java
src/jalview/io/FileLoader.java
src/jalview/schemes/HMMERColourScheme.java

index 574ac72..5e17deb 100644 (file)
@@ -1329,6 +1329,7 @@ label.invalid_hmmer_folder = The folder that you selected does not contain the n
 warn.no_selected_hmm = Please select a hidden Markov model sequence.
 label.select_hmm = Select HMM
 warn.no_sequence_data = No sequence data found.
+warn.empty_grp_or_alignment = An empty group or alignment was found.
 label.no_sequences_found = No matching sequences were found.
 label.hmmer = HMMER
 label.trim_termini = Trim Non-Matching Termini
@@ -1371,4 +1372,10 @@ label.use_reference = Use Reference Annotation
 label.use_reference_desc = If true, hmmbuild will keep all columns defined as a reference position by the reference annotation
 label.hmm_name = HMM Name
 label.hmm_name_desc = The name given to the HMM.
-warn.no_reference_annotation = No reference annotation found
\ No newline at end of file
+warn.no_reference_annotation = No reference annotation found.
+label.hmmbuild_for = Build HMM for
+label.hmmbuild_for_desc = Build an HMM for the selected sequence groups.
+label.alignment = Alignment
+label.groups_and_alignment = All groups and alignment
+label.groups = All groups
+label.selected_group = Selected group
index dd58b31..f4ee3a2 100755 (executable)
@@ -581,15 +581,6 @@ public interface AlignmentI extends AnnotatedCollectionI
   AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo);
 
   public void setHiddenColumns(HiddenColumns cols);
-  
-  /**
-   * Returns all HMM consensus sequences.
-   * 
-   * @param remove
-   *          If true, remove all HMM consensus sequences from the alignment.
-   * @return
-   */
-  public List<SequenceI> getHMMConsensusSequences(boolean remove);
 
   /**
    * Insert a sequence at a position in an alignment
index 927f3f4..878f22a 100644 (file)
@@ -61,5 +61,6 @@ public interface AnnotatedCollectionI extends SequenceCollectionI
    *         alignment, parent group).
    */
   AnnotatedCollectionI getContext();
+
   
 }
index 2f58d3f..a837b4f 100644 (file)
@@ -748,10 +748,22 @@ public class HiddenMarkovModel
    */
   public void setAlignmentColumn(int nodeIndex, int column)
   {
+    int currentCol = getNodeAlignmentColumn(nodeIndex);
+    nodeLookup.remove(currentCol);
     nodes.get(nodeIndex).setAlignmentColumn(column);
+    nodeLookup.put(column, nodeIndex);
   }
 
   /**
+   * Clears all data in the node lookup map
+   */
+  public void emptyNodeLookup()
+  {
+    nodeLookup = new HashMap<>();
+  }
+
+
+  /**
    * Sets the reference annotation at a given node.
    * 
    * @param nodeIndex
index 701ad7d..a673514 100755 (executable)
@@ -301,10 +301,15 @@ public class Sequence extends ASequence implements SequenceI
         this.addPDBId(new PDBEntry(pdb));
       }
     }
+    if (seq.isHMMConsensusSequence())
+    {
+      this.isHMMConsensusSequence = true;
+    }
     if (seq.getHMM() != null)
     {
       this.hmm = new HiddenMarkovModel(seq.getHMM());
     }
+
   }
 
   @Override
@@ -1545,18 +1550,14 @@ public class Sequence extends ASequence implements SequenceI
   {
     int node = 1;
     int column = 0;
+    hmm.emptyNodeLookup();
     for (char residue : sequence)
     {
       if (!Comparison.isGap(residue))
       {
         hmm.setAlignmentColumn(node, column);
-        hmm.getNodeLookup().put(column, node);
         node++;
       }
-      else
-      {
-        hmm.getNodeLookup().remove(column);
-      }
       column++;
     }
 
@@ -1578,21 +1579,6 @@ public class Sequence extends ASequence implements SequenceI
   public boolean hasHMMAnnotation()
   {
     return hasInfo;
-    /*
-    if (annotation == null)
-    {
-      return false;
-    }
-    
-    for (AlignmentAnnotation annot : annotation)
-    {
-      if (annot.label.contains("_HMM"))
-      {
-        return true;
-      }
-    }
-    return false;
-    */
   }
 
   @Override
index f681f11..2dd0ad3 100644 (file)
@@ -70,4 +70,13 @@ public interface SequenceCollectionI
    * @return
    */
   boolean isNucleotide();
+
+  /**
+   * Returns all HMM consensus sequences.
+   * 
+   * @param remove
+   *          If true, remove all HMM consensus sequences from the alignment.
+   * @return
+   */
+  List<SequenceI> getHMMConsensusSequences(boolean remove);
 }
index 854b39d..6db870c 100755 (executable)
@@ -1606,4 +1606,47 @@ public class SequenceGroup implements AnnotatedCollectionI
     normaliseSequenceLogo = state;
   }
 
+  /**
+   * Returns all HMM consensus sequences. This will not return real sequences
+   * with HMMs. If remove is set to true, the consensus sequences will be
+   * removed from the alignment.
+   */
+  @Override // TODO make this more efficient.
+  public List<SequenceI> getHMMConsensusSequences(boolean remove)
+  {
+    List<SequenceI> seqs = new ArrayList<>();
+    int position = 0;
+    int seqsRemoved = 0;
+    boolean endReached = false;
+
+    while (!endReached)
+    {
+      SequenceI seq = sequences.get(position);
+      if (seq.isHMMConsensusSequence())
+      {
+        if (remove)
+        {
+          sequences.remove(position);
+          seqsRemoved++;
+          seq.setPreviousPosition(seqsRemoved + position - 1);
+        }
+        else
+        {
+          position++;
+        }
+        seqs.add(seq);
+      }
+      else
+      {
+        position++;
+      }
+
+      if (position >= sequences.size())
+      {
+        endReached = true;
+      }
+    }
+    return seqs;
+  }
+
 }
index 043fd3d..126196f 100644 (file)
@@ -1105,7 +1105,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     {
       return;
     }
-    new Thread(new HMMBuildThread(this, null)).start();
+    new Thread(new HMMBuildThread(this, null))
+            .start();
     alignPanel.repaint();
 
   }
index 4260c34..faba183 100644 (file)
@@ -3,6 +3,7 @@ package jalview.hmmer;
 
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
@@ -11,8 +12,6 @@ import jalview.gui.AlignViewport;
 import jalview.gui.JvOptionPane;
 import jalview.gui.Preferences;
 import jalview.io.DataSourceType;
-import jalview.io.FileFormat;
-import jalview.io.FileLoader;
 import jalview.io.FileParse;
 import jalview.io.HMMFile;
 import jalview.util.MessageManager;
@@ -21,6 +20,7 @@ import jalview.ws.params.ArgumentI;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.swing.JOptionPane;
@@ -31,9 +31,9 @@ public class HMMBuildThread implements Runnable
   AlignFrame af;
 
   AlignViewport viewport;
+
   AlignmentI alignment;
   SequenceGroup group;
-
   List<ArgumentI> params;
 
 
@@ -59,15 +59,8 @@ public class HMMBuildThread implements Runnable
   public HMMBuildThread(AlignFrame af, List<ArgumentI> args)
   {
     this.af = af;
-    if (af.getViewport().getSelectionGroup() != null)
-    {
-      group = af.getViewport().getSelectionGroup();
-      forGroup = true;
-    }
     viewport = af.getViewport();
-    alignment = viewport.getAlignment();
     params = args;
-
   }
 
   /**
@@ -83,102 +76,184 @@ public class HMMBuildThread implements Runnable
             barID);
     }
     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
-    if (alignment == null && group == null)
+
+    List<SequenceGroup> groups = new ArrayList<>();
+    if (params != null)
     {
-      JOptionPane.showMessageDialog(af,
-              MessageManager.getString("warn.no_sequence_data"));
-      return;
+      for (ArgumentI arg : params)
+      {
+        String name = arg.getName();
+        if (MessageManager.getString("label.hmmbuild_for").equals(name))
+        {
+          String value = arg.getValue();
+          if ("Alignment".equals(value))
+          {
+            alignment = viewport.getAlignment();
+            break;
+          }
+          else if ("All groups and alignment".equals(value))
+          {
+            alignment = viewport.getAlignment();
+            groups.addAll(viewport.getAlignment().getGroups());
+            break;
+          }
+          else if ("All groups".equals(value))
+          {
+            alignment = null;
+            groups = viewport.getAlignment().getGroups();
+            break;
+          }
+          else if ("Selected group".equals(value))
+          {
+            alignment = null;
+            groups.add(viewport.getSelectionGroup());
+            break;
+          }
+        }
+        else if (MessageManager.getString("label.use_reference")
+                .equals(name))
+        {
+          if (!af.getViewport().hasReferenceAnnotation())
+          {
+            if (af != null)
+            {
+              af.setProgressBar(
+                      MessageManager.getString("status.running_hmmbuild"),
+                      barID);
+            }
+            JvOptionPane.showInternalMessageDialog(af, MessageManager
+                    .getString("warn.no_reference_annotation"));
+            return;
+          }
+        }
+      }
     }
-    try
+
+    if (alignment != null)
     {
-      hmmTemp = File.createTempFile("hmm", ".hmm");
-      hmmTemp.deleteOnExit();
-      stoTemp = File.createTempFile("output", ".sto");
-      stoTemp.deleteOnExit();
-    } catch (IOException e1)
+      forGroup = false;
+      runHMMBuild();
+    }
+
+    if (alignment == null)
     {
-      e1.printStackTrace();
+      alignment = viewport.getAlignment();
     }
 
-    try
+    if (groups != null && groups.size() > 0)
+    {
+      for (SequenceGroup grp : groups)
+      {
+        group = grp;
+        forGroup = true;
+        runHMMBuild();
+      }
+    }
+
+
+    if (af != null)
     {
+      af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
+              barID);
+    }
+  }
+
+  private void runHMMBuild()
+  {
+    if (alignment == null && group == null)
+      {
+        JOptionPane.showMessageDialog(af,
+              MessageManager.getString("warn.no_sequence_data"));
+      }
+      try
+      {
+        hmmTemp = File.createTempFile("hmm", ".hmm");
+        hmmTemp.deleteOnExit();
+        stoTemp = File.createTempFile("output", ".sto");
+        stoTemp.deleteOnExit();
+      } catch (IOException e1)
+      {
+        e1.printStackTrace();
+      }
+
     try
     {
+      try
+      {
         SequenceI[] array;
-        List<SequenceI> seqs = alignment
-                .getHMMConsensusSequences(true);
-        cmds.setHmmSeqs(seqs);
+        List<SequenceI> seqs = null;
         if (forGroup)
         {
+          seqs = group.getHMMConsensusSequences(true);
+          if (seqs.size() > 0)
+          {
+            return;
+          }
           array = group.getSelectionAsNewSequences(alignment);
         }
         else
         {
+          seqs = alignment.getHMMConsensusSequences(true);
           if (!alignment.isAligned())
           {
             alignment.padGaps();
           }
           array = alignment.getSequencesArray();
         }
+
+          cmds.setHmmSeqs(seqs);
+
         if (array.length < 1)
-        {
+          {
           if (af != null)
           {
             JOptionPane.showMessageDialog(af,
-                  MessageManager.getString("warn.no_sequence_data"));
+                    MessageManager.getString("warn.no_sequence_data"));
           }
           return;
-        }
-        SequenceI[] newArr = new SequenceI[array.length];
-        int index = 0;
-        for (SequenceI seq : array)
-        {
-          newArr[index] = new Sequence(seq);
-          index++;
-        }
+          }
+          SequenceI[] newArr = new SequenceI[array.length];
+          int index = 0;
+          for (SequenceI seq : array)
+          {
+            newArr[index] = new Sequence(seq);
+            index++;
+          }
 
-        cmds.uniquifySequences(newArr);
-        cmds.exportData(newArr, stoTemp, null, null);
-        jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array);
+          cmds.uniquifySequences(newArr);
+          cmds.exportData(newArr, stoTemp, null, null);
+          jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array);
 
-    } catch (FileNotFoundException e)
-    {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
+      } catch (FileNotFoundException e)
+      {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
 
-    }
-    try
-    {
-        boolean ran = runCommand();
-        if (!ran)
-        {
-          return;
-        }
-    } catch (IOException | InterruptedException e)
-    {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    }
-    try
-    {
+      }
+      try
+      {
+          boolean ran = runCommand();
+          if (!ran)
+          {
+            return;
+          }
+      } catch (IOException | InterruptedException e)
+      {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      }
+      try
+      {
 
-      importData();
-    } catch (IOException | InterruptedException e)
-    {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    }
-    } catch (Exception e)
-    {
-      e.printStackTrace();
-    } finally
-    {
-      if (af != null)
+        importData();
+      } catch (IOException | InterruptedException e)
       {
-        af.setProgressBar(
-                MessageManager.getString("status.running_hmmbuild"),
-              barID);
+        // TODO Auto-generated catch block
+        e.printStackTrace();
       }
+      } catch (Exception e)
+      {
+        e.printStackTrace();
     }
   }
 
@@ -224,12 +299,6 @@ public class HMMBuildThread implements Runnable
           break;
         case "Use Reference Annotation":
           command += "--hand ";
-          if (!af.getViewport().hasReferenceAnnotation())
-          {
-            JvOptionPane.showInternalMessageDialog(af, MessageManager
-                    .getString("warn.no_reference_annotation"));
-            return false;
-          }
           break;
 
         }
@@ -237,7 +306,9 @@ public class HMMBuildThread implements Runnable
       }
     }
 
-    if (name == null || name == "" || name == " ")
+    name = name.trim();
+
+    if (name == null || "".equals(name))
     {
       if (forGroup)
       {
@@ -249,7 +320,7 @@ public class HMMBuildThread implements Runnable
         {
           name = af.getTitle();
         }
-        if (name == null || name == "" || name == " " || name == "  ")
+        if (name == null || "".equals(name))
         {
           name = "Alignment";
         }
@@ -279,20 +350,36 @@ public class HMMBuildThread implements Runnable
    */
   private void importData() throws IOException, InterruptedException
   {
-    if (af != null)
+      HMMFile file = new HMMFile(new FileParse(hmmTemp.getAbsolutePath(),
+              DataSourceType.FILE));
+    SequenceI[] seqs = file.getSeqsAsArray();
+    SequenceI seq = seqs[0];
+    seq.createDatasetSequence();
+    HiddenMarkovModel hmm = file.getHMM();
+    if (group != null)
     {
-      cmds.addHMMConsensusSequences(af);
-
-      FileLoader loader = new FileLoader();
-      loader.LoadFileOntoAlignmentWaitTillLoaded(viewport,
-              hmmTemp.getAbsolutePath(), DataSourceType.FILE,
-              FileFormat.HMMER3);
+      seq.insertCharAt(0, group.getStartRes(), '-');
+      seq.insertCharAt(group.getEndRes() + 1,
+              alignment.getWidth() - group.getEndRes(), '-');
+      seq.updateHMMMapping();
+      SequenceI topSeq = group.getSequencesInOrder(alignment)[0];
+      int topIndex = alignment.findIndex(topSeq);
+      alignment.insertSequenceAt(topIndex, seq);
+      group.setSeqrep(seq);
+      group.addSequence(seq, false);
     }
     else
     {
-      HMMFile file = new HMMFile(new FileParse(hmmTemp.getAbsolutePath(),
-              DataSourceType.FILE));
-      alignment.addSequence(file.getSeqsAsArray()[0]);
+      alignment.insertSequenceAt(0, seq);
+    }
+
+    viewport.alignmentChanged(viewport.getAlignPanel());
+    viewport.getAlignPanel().adjustAnnotationHeight();
+    viewport.updateSequenceIdColours();
+
+    if (viewport.getAlignPanel().alignFrame.getSelectedHMM() == null)
+    {
+      viewport.getAlignPanel().alignFrame.setSelectedHMMSequence(seq);
     }
     hmmTemp.delete();
     stoTemp.delete();
index b512ce1..11cde90 100644 (file)
@@ -6,7 +6,6 @@ import jalview.gui.Preferences;
 import jalview.util.MessageManager;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.ParamDatastoreI;
-import jalview.ws.params.ValueConstrainI;
 import jalview.ws.params.WsParamSetI;
 import jalview.ws.params.simple.BooleanOption;
 import jalview.ws.params.simple.IntegerParameter;
@@ -157,29 +156,17 @@ public class HMMERParamStore implements ParamDatastoreI
             MessageManager.getString("label.use_reference"),
             MessageManager.getString("label.use_reference_desc"), true,
             true, true, null));
-  }
-
-  private class StringValueConstrain implements ValueConstrainI
-  {
-
-    @Override
-    public ValueType getType()
-    {
-      return ValueType.String;
-    }
 
-    @Override
-    public Number getMax()
-    {
-      return null;
-    }
-
-    @Override
-    public Number getMin()
-    {
-      return null;
-    }
+    Collection<String> options = new ArrayList<>();
+    options.add(MessageManager.getString("label.alignment"));
+    options.add(MessageManager.getString("label.groups_and_alignment"));
+    options.add(MessageManager.getString("label.groups"));
+    options.add(MessageManager.getString("label.selected_group"));
 
+    args.add(new Option(MessageManager.getString("label.hmmbuild_for"),
+            MessageManager.getString("label.hmmbuild_for_desc"), true,
+            MessageManager.getString("label.alignment"),
+            MessageManager.getString("label.alignment"), options, null));
   }
 
   @Override
index f3a4a1c..fd8429b 100755 (executable)
@@ -29,7 +29,6 @@ import jalview.bin.Jalview;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
@@ -428,30 +427,8 @@ public class FileLoader implements Runnable
                       .getSequenceAt(alignment.getAbsoluteHeight() - 1);
               seq.setIsHMMConsensusSequence(true);
               alignment.deleteSequence(alignment.getAbsoluteHeight() - 1);
-              SequenceGroup sg = viewport.getSelectionGroup();
-              if (sg != null)
-              {
-                seq.insertCharAt(0, sg.getStartRes(), '-');
-                seq.insertCharAt(sg.getEndRes() + 1,
-                        alignment.getWidth() - sg.getEndRes(), '-');
-                SequenceI topSeq = sg.getSequencesInOrder(alignment)[0];
-                int topIndex = alignment.findIndex(topSeq);
-                alignment.insertSequenceAt(topIndex, seq);
-                sg.setSeqrep(seq);
-                viewport.getSelectionGroup().addSequence(seq, false);
-              }
-              else
-              {
-                alignment.insertSequenceAt(0, seq);
-              }
               viewport.getAlignPanel().adjustAnnotationHeight();
               viewport.updateSequenceIdColours();
-              if (viewport.getAlignPanel().alignFrame
-                      .getSelectedHMM() == null)
-              {
-                viewport.getAlignPanel().alignFrame
-                        .setSelectedHMMSequence(seq);
-              }
 
             }
           }
index f3691f0..61eb693 100644 (file)
@@ -61,7 +61,7 @@ public class HMMERColourScheme extends ResidueColourScheme
     }
     if (Character.isLowerCase(symbol))
     {
-      return new Color(230, 0, 0);
+      symbol = Character.toUpperCase(symbol);
     }
     Double prob;
     prob = hmm.getMatchEmissionProbability(position, symbol);
@@ -108,7 +108,7 @@ public class HMMERColourScheme extends ResidueColourScheme
          List<SequenceI> seqs = collection.getSequences();
            for (SequenceI seq : seqs)
            {
-             if (seq.getHMM() != null)
+      if (seq.isHMMConsensusSequence())
              {
                hmm = seq.getHMM();
                break;