test HMMER colour scheme
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Fri, 23 Jun 2017 14:19:47 +0000 (15:19 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Fri, 23 Jun 2017 14:19:47 +0000 (15:19 +0100)
src/jalview/datamodel/HiddenMarkovModel.java
src/jalview/schemes/HMMERColourScheme.java [new file with mode: 0644]
test/jalview/io/HMMFileTest.java
test/jalview/schemes/HMMERColourSchemeTest.java [new file with mode: 0644]

index e214373..5331f3d 100644 (file)
@@ -290,7 +290,7 @@ public class HiddenMarkovModel
     int symbolIndex;
     int nodeIndex;
     Double probability;
-    if (symbol == '-')
+    if (!symbolIndexLookup.containsKey(symbol))
     {
       return 0d;
     }
@@ -322,7 +322,7 @@ public class HiddenMarkovModel
     int symbolIndex;
     int nodeIndex;
     Double probability;
-    if (symbol == '-')
+    if (!symbolIndexLookup.containsKey(symbol))
     {
       return 0d;
     }
diff --git a/src/jalview/schemes/HMMERColourScheme.java b/src/jalview/schemes/HMMERColourScheme.java
new file mode 100644 (file)
index 0000000..b2d0742
--- /dev/null
@@ -0,0 +1,108 @@
+package jalview.schemes;
+
+import jalview.datamodel.AnnotatedCollectionI;
+import jalview.datamodel.HiddenMarkovModel;
+import jalview.datamodel.SequenceCollectionI;
+import jalview.datamodel.SequenceI;
+
+import java.awt.Color;
+import java.util.Map;
+
+public class HMMERColourScheme extends ResidueColourScheme
+{
+
+  AnnotatedCollectionI alignment;
+
+  HiddenMarkovModel hmm;
+  
+  boolean peptideSpecific;
+
+  boolean nucleotideSpecific;
+
+  public HMMERColourScheme(HiddenMarkovModel markov)
+  {
+    hmm = markov;
+  }
+  public HMMERColourScheme()
+  {
+
+  }
+
+  @Override
+  public Color findColour(char symbol, int position, SequenceI seq,
+          String consensusResidue, float pid)
+  {
+    if (hmm ==null)
+    {
+      return new Color(255, 255, 255);
+    }
+    return findColour(symbol, position);
+  }
+
+  public Color findColour(char symbol, int position)
+  {
+
+
+    Double probability;
+    probability = hmm.getMatchEmissionProbability(position, symbol);
+    // Double redModifier = Math.pow(probability, 0.9);
+    Double doubleGreenModifier;
+    float greenModifier;
+    if (probability < 0.5)
+    {
+      doubleGreenModifier = probability;
+      greenModifier = doubleGreenModifier.floatValue();
+    }
+    else
+    {
+      doubleGreenModifier = Math.pow(probability, 1 / 1.9);
+      greenModifier = doubleGreenModifier.floatValue();
+    }
+    // Double blueModifier = Math.pow(probability, 0.9);
+    return new Color(1f, 1f - greenModifier, 0.f);
+
+  }
+
+  @Override
+  public void alignmentChanged(AnnotatedCollectionI collection,
+          Map<SequenceI, SequenceCollectionI> hiddenReps)
+  {
+
+    collection.setHMM(hmm);
+  }
+
+  @Override
+  public ColourSchemeI getInstance(AnnotatedCollectionI sg,
+          Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+  {
+    
+    
+    
+    
+
+    HMMERColourScheme markov = new HMMERColourScheme(sg.getHMM());
+    return markov;
+  }
+
+  @Override
+  public boolean isApplicableTo(AnnotatedCollectionI ac)
+  {
+      return true;
+
+  }
+
+  @Override
+  public String getSchemeName()
+  {
+
+    return JalviewColourScheme.HMMER.name();
+  }
+
+  @Override
+  public boolean isSimple()
+  {
+    // TODO Auto-generated method stub
+    return false;
+  }
+
+}
index 8ac2cf8..7350c04 100644 (file)
@@ -21,13 +21,16 @@ public class HMMFileTest {
 
 
   HMMFile fn3 = new HMMFile(
-          new FileParse("H:/fn3.hmm", DataSourceType.FILE));
+          new FileParse("test/jalview/io/test_fn3_hmm.txt",
+                  DataSourceType.FILE));
 
   HMMFile pKinase = new HMMFile(
-          new FileParse("H:/Pkinase.hmm", DataSourceType.FILE));
+          new FileParse("test/jalview/io/test_PKinase_hmm.txt",
+                  DataSourceType.FILE));
 
   HMMFile made1 = new HMMFile(
-          new FileParse("H:/MADE1.hmm", DataSourceType.FILE));
+          new FileParse("test/jalview/io/test_MADE1_hmm.txt",
+                  DataSourceType.FILE));
 
   HMMFileTest() throws IOException
   {
@@ -145,7 +148,8 @@ public class HMMFileTest {
   @Test
   public void testParseFileProperties() throws IOException
   {
-    FileReader fr = new FileReader(new File("H:/fn3.hmm"));
+    FileReader fr = new FileReader(
+            new File("test/jalview/io/test_fn3_hmm.txt"));
     BufferedReader br = new BufferedReader(fr);
     fn3.parseFileProperties(br);
     fn3.parseModel(br); // this is for a later test
@@ -179,7 +183,8 @@ public class HMMFileTest {
     assertEquals(testHMM.getForward(), "-3.8341  0.71847");
   
   
-    FileReader fr3 = new FileReader(new File("H:/MADE1.hmm"));
+    FileReader fr3 = new FileReader(
+            new File("test/jalview/io/test_MADE1_hmm.txt"));
     BufferedReader br3 = new BufferedReader(fr3);
     made1.parseFileProperties(br3);
     testHMM = made1.getHMM();
@@ -264,7 +269,8 @@ public class HMMFileTest {
   @Test
   public void testParseModel() throws IOException
   {
-    FileReader fr = new FileReader(new File("H:/MADE1.hmm"));
+    FileReader fr = new FileReader(
+            new File("test/jalview/io/test_MADE1_hmm.txt"));
     BufferedReader br = new BufferedReader(fr);
     HiddenMarkovModel testHMM = new HiddenMarkovModel();
     for (int i = 0; i < 24; i++)
@@ -367,9 +373,10 @@ public class HMMFileTest {
   @Test(priority = 3)
   public void testExportFile() throws IOException
   {
-    fn3.exportFile("H:/WriteFileTest.txt");
+    fn3.exportFile("test/jalview/io/test_export_hmm.txt");
     HMMFile fn3Clone = new HMMFile(
-            new FileParse("H:/WriteFileTest.txt", DataSourceType.FILE));
+            new FileParse("test/jalview/io/test_export_hmm.txt",
+                    DataSourceType.FILE));
     fn3Clone.parse();
     HiddenMarkovModel fn3HMM = new HiddenMarkovModel();
     HiddenMarkovModel fn3CloneHMM = new HiddenMarkovModel();
diff --git a/test/jalview/schemes/HMMERColourSchemeTest.java b/test/jalview/schemes/HMMERColourSchemeTest.java
new file mode 100644 (file)
index 0000000..44990c7
--- /dev/null
@@ -0,0 +1,57 @@
+package jalview.schemes;
+
+import static org.testng.Assert.assertEquals;
+
+import jalview.io.DataSourceType;
+import jalview.io.FileParse;
+import jalview.io.HMMFile;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import org.testng.annotations.Test;
+
+public class HMMERColourSchemeTest {
+  HMMERColourScheme scheme = new HMMERColourScheme();
+
+  @Test
+  public void testFindColour() throws MalformedURLException, IOException
+  {
+
+    HMMFile file = new HMMFile(new FileParse(
+            "test/jalview/io/test_PKinase_hmm.txt", DataSourceType.FILE));
+    file.parse();
+    scheme.hmm = file.getHMM();
+
+    Color actual = scheme.findColour('A', 1);
+    Color expected = new Color(1f, 1f - 0.06253430455f, 0f);
+    assertEquals(actual, expected);
+
+    actual = scheme.findColour('Y', 3);
+    expected = new Color(1f, 1f - 0.02082023351f, 0f);
+    assertEquals(actual, expected);
+
+    actual = scheme.findColour('M', 109);
+    expected = new Color(1f, 1f, 0);
+    assertEquals(actual, expected);
+
+    actual = scheme.findColour('I', 128);
+    expected = new Color(1f, 1f - 0.15097062462f, 0);
+    assertEquals(actual, expected);
+
+    actual = scheme.findColour('D', 181);
+    expected = new Color(1f, 1f - 0.08686935114f, 0);
+    assertEquals(actual, expected);
+
+    actual = scheme.findColour('Z', 1);
+    expected = new Color(1f, 1f, 0);
+    assertEquals(actual, expected);
+
+    actual = scheme.findColour('A', 4000);
+    expected = new Color(1f, 1f, 0);
+    assertEquals(actual, expected);
+
+  }
+
+}