JAL-2629 add ability to build a HMM from a group
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Fri, 11 Aug 2017 15:09:39 +0000 (16:09 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Fri, 11 Aug 2017 15:12:08 +0000 (16:12 +0100)
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentI.java
src/jalview/datamodel/Sequence.java
src/jalview/datamodel/SequenceGroup.java
src/jalview/datamodel/SequenceI.java
src/jalview/hmmer/HMMBuildThread.java
src/jalview/hmmer/HMMERCommands.java
src/jalview/workers/InformationThread.java

index 8ca859f..38d0092 100755 (executable)
@@ -30,7 +30,6 @@ import jalview.util.MessageManager;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
@@ -1961,54 +1960,40 @@ public class Alignment implements AlignmentI
     hiddenCols = cols;
   }
 
+
   @Override
-  public Map<Integer, SequenceI> getHMMConsensusSequences(boolean remove)
+  public List<SequenceI> getHMMConsensusSequences(boolean remove)
   {
-    Map<Integer, SequenceI> seqs = new HashMap<>();
+    List<SequenceI> seqs = new ArrayList<>();
     int position = 0;
-    for (SequenceI seq : sequences)
+    int seqsRemoved = 0;
+    boolean endReached = false;
+
+    while (!endReached)
     {
+      SequenceI seq = sequences.get(position);
       if (seq.isHMMConsensusSequence())
       {
-        seqs.put(position, seq);
         if (remove)
         {
-          sequences.set(position, null);
+          sequences.remove(position);
+          seqsRemoved++;
+          seq.setPreviousPosition(seqsRemoved + position - 1);
         }
-
-      }
-      position++;
-    }
-    boolean endReached = false;
-    int pos = 0;
-    while (!endReached)
-    {
-      if (sequences.get(pos) == null)
-      {
-        sequences.remove(pos);
+        else
+        {
+          position++;
+        }
+        seqs.add(seq);
       }
       else
       {
-        pos++;
-      }
-      if (pos >= sequences.size())
-      {
-        endReached = true;
+        position++;
       }
-    }
-
-    return seqs;
-  }
 
-  @Override
-  public List<SequenceI> getHMMConsensusSequences()
-  {
-    List<SequenceI> seqs = new ArrayList<>();
-    for (SequenceI seq : sequences)
-    {
-      if (seq.isHMMConsensusSequence())
+      if (position >= sequences.size())
       {
-        seqs.add(seq);
+        endReached = true;
       }
     }
     return seqs;
index edd5b66..58df1b0 100755 (executable)
@@ -590,10 +590,8 @@ public interface AlignmentI extends AnnotatedCollectionI
   public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols);
 
   public void setHiddenColumns(HiddenColumns cols);
-
-  public Map<Integer, SequenceI> getHMMConsensusSequences(boolean remove);
   
-  public List<SequenceI> getHMMConsensusSequences();
+  public List<SequenceI> getHMMConsensusSequences(boolean remove);
   
 
   
index 54fa3b0..0da30ba 100755 (executable)
@@ -51,12 +51,16 @@ public class Sequence extends ASequence implements SequenceI
 
   private char[] sequence;
 
+  int previousPosition;
+
   String description;
 
   int start;
 
   int end;
 
+  boolean hasInfo;
+
   HiddenMarkovModel hmm;
 
   boolean isHMMConsensusSequence = false;
@@ -1529,6 +1533,43 @@ public class Sequence extends ASequence implements SequenceI
     this.isHMMConsensusSequence = isHMMConsensusSequence;
   }
 
+  @Override
+  public boolean hasHMMAnnotation()
+  {
+    return hasInfo;
+    /*
+    if (annotation == null)
+    {
+      return false;
+    }
+    
+    for (AlignmentAnnotation annot : annotation)
+    {
+      if (annot.label.contains("_HMM"))
+      {
+        return true;
+      }
+    }
+    return false;
+    */
+  }
+
+  @Override
+  public void setHasInfo(boolean status)
+  {
+    hasInfo = true;
+  }
+
+  @Override
+  public int getPreviousPosition()
+  {
+    return previousPosition;
+  }
 
+  @Override
+  public void setPreviousPosition(int previousPosition)
+  {
+    this.previousPosition = previousPosition;
+  }
 
 }
index eeff135..5cf07e2 100755 (executable)
@@ -51,8 +51,6 @@ public class SequenceGroup implements AnnotatedCollectionI
 
   boolean colourText = false;
 
-  HiddenMarkovModel hmm;
-
   /**
    * True if the group is defined as a group on the alignment, false if it is
    * just a selection.
@@ -578,8 +576,12 @@ public class SequenceGroup implements AnnotatedCollectionI
               endRes + 1, showSequenceLogo);
       if (information != null)
       {
-        // _updateInformationRow(cnsns, sequences.size()); TODO don't know what
-        // to do here
+        HiddenMarkovModel hmm = information.sequenceRef.getHMM();
+
+        ProfilesI info = AAFrequency.calculateInformation(hmm,
+                (endRes + 1) - startRes, startRes, endRes + 1,
+                showHMMSequenceLogo, ignoreBelowBackground);
+        _updateInformationRow(info, sequences.size());
         upd = true;
       }
       if (consensus != null)
@@ -1170,12 +1172,12 @@ public class SequenceGroup implements AnnotatedCollectionI
     if (information == null)
     {
       information = new AlignmentAnnotation("", "", new Annotation[1], 0f,
-              100f, AlignmentAnnotation.BAR_GRAPH);
+              6.25f, AlignmentAnnotation.BAR_GRAPH);
       information.hasText = true;
-      information.autoCalculated = true;
+      information.autoCalculated = false;
       information.groupRef = this;
-      information.label = "Consensus for " + getName();
-      information.description = "Percent Identity";
+      information.label = getName() + "_HMM";
+      information.description = "Information content, measured in bits";
     }
     return information;
   }
index 243d688..271f25a 100755 (executable)
@@ -487,5 +487,12 @@ public interface SequenceI extends ASequenceI
 
   void setIsHMMConsensusSequence(boolean isHMMConsensusSequence);
 
+  boolean hasHMMAnnotation();
+
+  void setHasInfo(boolean status);
+
+  int getPreviousPosition();
+
+  void setPreviousPosition(int previousPosition);
 
 }
index c5e1816..fb904f9 100644 (file)
@@ -2,6 +2,7 @@ package jalview.hmmer;
 
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Preferences;
@@ -12,7 +13,9 @@ import jalview.util.MessageManager;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.Map;
+import java.util.List;
+
+import javax.swing.JOptionPane;
 
 public class HMMBuildThread implements Runnable
 {
@@ -20,28 +23,40 @@ public class HMMBuildThread implements Runnable
   AlignFrame af;
   AlignmentI alignment;
 
+  SequenceGroup group;
+
+  boolean canRun = true;
+
   File hmmTemp = null;
 
   File stoTemp = null;
 
   long barID;
-
-  Map<Integer, SequenceI> hmmSeqs;
   
   public HMMBuildThread(AlignFrame af)
   {
     this.af = af;
+    if (af.getViewport().getSelectionGroup() != null)
+    {
+      group = af.getViewport().getSelectionGroup();
+    }
     alignment = af.getViewport().getAlignment();
+
   }
 
   @Override
   public void run()
   {
     barID = System.currentTimeMillis();
-    System.out.println(System.getProperty("java.io.tmpdir"));
     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
             barID);
     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
+    if (alignment == null && group == null)
+    {
+      JOptionPane.showMessageDialog(af,
+              MessageManager.getString("label.no_sequence_data"));
+      return;
+    }
     try
     {
       hmmTemp = File.createTempFile("hmm", ".hmm");
@@ -57,12 +72,24 @@ public class HMMBuildThread implements Runnable
     {
     try
     {
-        hmmSeqs = alignment.getHMMConsensusSequences(true);
-        if (!alignment.isAligned())
+        List<SequenceI> seqs = alignment
+                .getHMMConsensusSequences(true);
+        cmds.setHmmSeqs(seqs);
+        if (group != null)
         {
-          alignment.padGaps();
+          SequenceI[] array = group.getSelectionAsNewSequences(alignment);
+
+          cmds.exportData(array, stoTemp, null, null);
+        }
+        else
+        {
+          if (!alignment.isAligned())
+          {
+            alignment.padGaps();
+          }
+          cmds.exportData(alignment.getSequencesArray(), stoTemp, null,
+                  null);
         }
-        cmds.exportData(alignment.getSequencesArray(), stoTemp, null, null);
     } catch (FileNotFoundException e)
     {
       // TODO Auto-generated catch block
@@ -88,7 +115,7 @@ public class HMMBuildThread implements Runnable
     }
     } catch (Exception e)
     {
-
+      e.printStackTrace();
     } finally
     {
       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
@@ -109,19 +136,17 @@ public class HMMBuildThread implements Runnable
   
   private void importData() throws IOException, InterruptedException
   {
+    cmds.addHMMConsensusSequences(af);
     af.loadJalviewDataFile(hmmTemp.getAbsolutePath(), DataSourceType.FILE,
             FileFormat.HMMER3, null);
-    for (Map.Entry<Integer, SequenceI> entry : hmmSeqs.entrySet())
-    {
-      SequenceI seq = entry.getValue();
-      Integer pos = entry.getKey();
-      cmds.addHMMConsensusSequence(af, seq, pos);
-    }
-    af.alignPanel.alignmentChanged();
     hmmTemp.delete();
     stoTemp.delete();
   }
   
+  public boolean canRun()
+  {
+    return canRun;
+  }
   
   
  
index daa61cb..3b5472a 100644 (file)
@@ -14,6 +14,7 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.util.Hashtable;
+import java.util.List;
 
 public class HMMERCommands
 {
@@ -47,6 +48,8 @@ public class HMMERCommands
 
   Hashtable hash = new Hashtable();
 
+  List<SequenceI> hmmSeqs;
+
   public void uniquifySequences(SequenceI[] seqs)
   {
     hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
@@ -121,16 +124,27 @@ public class HMMERCommands
     }
   }
 
-  public void addHMMConsensusSequence(AlignFrame af, SequenceI seq,
-          Integer position)
+  public void addHMMConsensusSequences(AlignFrame af)
   {
-    seq.getHMM().initHMMSequence(af, position);
     AlignmentI al = af.getViewport().getAlignment();
+    for (SequenceI seq : hmmSeqs)
+    {
+      Integer position = seq.getPreviousPosition();
+      al.getSequences().add(position, seq);
+    }
     af.getViewport().setAlignment(al);
     af.alignPanel.adjustAnnotationHeight();
     af.getViewport().updateSequenceIdColours();
     af.buildSortByAnnotationScoresMenu();
   }
 
+  public List<SequenceI> getHmmSeqs()
+  {
+    return hmmSeqs;
+  }
 
+  public void setHmmSeqs(List<SequenceI> hmmSeqs)
+  {
+    this.hmmSeqs = hmmSeqs;
+  }
 }
index 54e5198..67dd838 100644 (file)
@@ -116,7 +116,7 @@ public class InformationThread extends AlignCalcWorker
     {
 
       int width = alignment.getWidth();
-    List<SequenceI> hmmSeqs = alignment.getHMMConsensusSequences();
+    List<SequenceI> hmmSeqs = alignment.getHMMConsensusSequences(false);
     int index = 0;
     for (SequenceI seq : hmmSeqs)
     {