From 293afc4e4e080e8d48cfa99760667b2b1dfe1da0 Mon Sep 17 00:00:00 2001 From: TZVanaalten Date: Fri, 23 Jun 2017 15:19:47 +0100 Subject: [PATCH] test HMMER colour scheme --- src/jalview/datamodel/HiddenMarkovModel.java | 4 +- src/jalview/schemes/HMMERColourScheme.java | 108 +++++++++++++++++++++++ test/jalview/io/HMMFileTest.java | 23 +++-- test/jalview/schemes/HMMERColourSchemeTest.java | 57 ++++++++++++ 4 files changed, 182 insertions(+), 10 deletions(-) create mode 100644 src/jalview/schemes/HMMERColourScheme.java create mode 100644 test/jalview/schemes/HMMERColourSchemeTest.java diff --git a/src/jalview/datamodel/HiddenMarkovModel.java b/src/jalview/datamodel/HiddenMarkovModel.java index e214373..5331f3d 100644 --- a/src/jalview/datamodel/HiddenMarkovModel.java +++ b/src/jalview/datamodel/HiddenMarkovModel.java @@ -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 index 0000000..b2d0742 --- /dev/null +++ b/src/jalview/schemes/HMMERColourScheme.java @@ -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 hiddenReps) + { + + collection.setHMM(hmm); + } + + @Override + public ColourSchemeI getInstance(AnnotatedCollectionI sg, + Map 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; + } + +} diff --git a/test/jalview/io/HMMFileTest.java b/test/jalview/io/HMMFileTest.java index 8ac2cf8..7350c04 100644 --- a/test/jalview/io/HMMFileTest.java +++ b/test/jalview/io/HMMFileTest.java @@ -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 index 0000000..44990c7 --- /dev/null +++ b/test/jalview/schemes/HMMERColourSchemeTest.java @@ -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); + + } + +} -- 1.7.10.2