JAL-2629 HMMER alignment background colour scheme is functional
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Mon, 28 Aug 2017 10:25:44 +0000 (11:25 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Mon, 28 Aug 2017 10:33:27 +0000 (11:33 +0100)
src/jalview/datamodel/ResidueCount.java
src/jalview/hmmer/HMMBuildThread.java
src/jalview/schemes/HMMERAlignmentColourScheme.java

index 74eb887..612969f 100644 (file)
@@ -640,4 +640,17 @@ public class ResidueCount
     sb.append("]");
     return sb.toString();
   }
+
+  public int getTotalCount()
+  {
+    int total = 0;
+    for (char symbol : this.getSymbolCounts().symbols)
+    {
+      if (!Comparison.isGap(symbol))
+      {
+        total += getCount(symbol);
+      }
+    }
+    return total;
+  }
 }
index b0e9d70..4e0fd08 100644 (file)
@@ -130,7 +130,10 @@ public class HMMBuildThread implements Runnable
     }
     else
     {
-      alignment = viewport.getAlignment();
+      if (viewport != null && alignment == null)
+      {
+        alignment = viewport.getAlignment();
+      }
     }
 
     if (alignment != null)
@@ -378,13 +381,16 @@ public class HMMBuildThread implements Runnable
       alignment.insertSequenceAt(0, seq);
     }
 
-    viewport.alignmentChanged(viewport.getAlignPanel());
-    viewport.getAlignPanel().adjustAnnotationHeight();
-    viewport.updateSequenceIdColours();
-
-    if (viewport.getAlignPanel().alignFrame.getSelectedHMM() == null)
+    if (viewport != null)
     {
-      viewport.getAlignPanel().alignFrame.setSelectedHMMSequence(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 47c065b..59c0317 100644 (file)
@@ -2,19 +2,22 @@ package jalview.schemes;
 
 import jalview.datamodel.AnnotatedCollectionI;
 import jalview.datamodel.HiddenMarkovModel;
+import jalview.datamodel.ResidueCount;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
 import jalview.util.ColorUtils;
 import jalview.util.Comparison;
 
 import java.awt.Color;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class HMMERAlignmentColourScheme extends ResidueColourScheme
 {
+  Map<Character, Double> backgroundFrequencies = new HashMap<>();
 
-  AnnotatedCollectionI alignment;
+  Double maxLLR;
 
   HiddenMarkovModel hmm;
 
@@ -65,17 +68,11 @@ public class HMMERAlignmentColourScheme extends ResidueColourScheme
     }
     Double prob;
     prob = hmm.getMatchEmissionProbability(position, symbol);
-    double freq = 0;
-    String alpha = hmm.getAlphabetType();
-    if (!ResidueProperties.backgroundFrequencies.get(alpha)
-            .containsKey(symbol))
+    Double freq = backgroundFrequencies.get(symbol);
+    if (freq == null)
     {
       return Color.white;
     }
-    else
-    {
-      freq = ResidueProperties.backgroundFrequencies.get(alpha).get(symbol);
-    }
     if (prob == 0)
     {
       return new Color(230, 0, 0);
@@ -86,7 +83,7 @@ public class HMMERAlignmentColourScheme extends ResidueColourScheme
     {
 
       colour = ColorUtils.getGraduatedColour(value.floatValue(), 0,
-              Color.WHITE, 4.52f, Color.blue);
+              Color.WHITE, maxLLR.floatValue(), Color.blue);
     }
     else if (value < 0)
     {
@@ -94,7 +91,6 @@ public class HMMERAlignmentColourScheme extends ResidueColourScheme
 
     }
     return colour;
-
   }
 
   @Override
@@ -111,6 +107,8 @@ public class HMMERAlignmentColourScheme extends ResidueColourScheme
       }
     }
 
+    count(collection);
+
   }
 
   @Override
@@ -127,7 +125,11 @@ public class HMMERAlignmentColourScheme extends ResidueColourScheme
         break;
       }
     }
-    HMMERColourScheme colour = new HMMERColourScheme(markov);
+
+    count(sg);
+
+    HMMERAlignmentColourScheme colour = new HMMERAlignmentColourScheme(
+            markov);
     return colour;
 
   }
@@ -152,5 +154,29 @@ public class HMMERAlignmentColourScheme extends ResidueColourScheme
     return false;
   }
 
+  public void count(AnnotatedCollectionI sg)
+  {
+    ResidueCount counts = new ResidueCount();
+    for (SequenceI seq : sg.getSequences())
+    {
+      for (int i = 0; i < seq.getLength(); i++)
+      {
+        if (!Comparison.isGap(seq.getCharAt(i)))
+        {
+          counts.add(seq.getCharAt(i));
+        }
+      }
+    }
+
+    int total = counts.getTotalCount();
+
+    for (char symbol : counts.getSymbolCounts().symbols)
+    {
+      double count = Double.valueOf(counts.getCount(symbol))
+              / Double.valueOf(total);
+      backgroundFrequencies.put(symbol, count);
+    }
+    maxLLR = Math.log(total);
+  }
 }