4e8575dfe258a7d8a76263c163157421ba8ab36e
[jalview.git] / test / jalview / analysis / scoremodels / ScoreModelsTest.java
1 package jalview.analysis.scoremodels;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertTrue;
6
7 import jalview.api.analysis.PairwiseScoreModelI;
8 import jalview.api.analysis.ScoreModelI;
9
10 import java.util.Iterator;
11
12 import org.testng.annotations.Test;
13
14 public class ScoreModelsTest
15 {
16   /**
17    * Verify that the singleton constructor successfully loads Jalview's built-in
18    * score models
19    */
20   @Test(groups = "Functional")
21   public void testConstructor()
22   {
23     Iterator<ScoreModelI> models = ScoreModels.getInstance().getModels()
24             .iterator();
25     assertTrue(models.hasNext());
26
27     /*
28      * models are served in order of addition
29      */
30     ScoreModelI sm = models.next();
31     assertTrue(sm instanceof SimilarityScoreModel);
32     assertTrue(sm instanceof PairwiseScoreModelI);
33     assertFalse(sm instanceof DistanceScoreModel);
34     assertEquals(sm.getName(), "BLOSUM62");
35     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('I', 'R'),
36             -3f);
37
38     sm = models.next();
39     assertTrue(sm instanceof SimilarityScoreModel);
40     assertTrue(sm instanceof PairwiseScoreModelI);
41     assertFalse(sm instanceof DistanceScoreModel);
42     assertEquals(sm.getName(), "PAM250");
43     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('R', 'C'),
44             -4f);
45
46     sm = models.next();
47     assertTrue(sm instanceof SimilarityScoreModel);
48     assertTrue(sm instanceof PairwiseScoreModelI);
49     assertFalse(sm instanceof DistanceScoreModel);
50     assertEquals(sm.getName(), "DNA");
51     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('c', 'x'), 1f);
52
53     sm = models.next();
54     assertTrue(sm instanceof SimilarityScoreModel);
55     assertTrue(sm instanceof PairwiseScoreModelI);
56     assertFalse(sm instanceof DistanceScoreModel);
57     assertEquals(sm.getName(), "PID");
58     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('R', 'C'), 0f);
59     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('R', 'r'), 1f);
60
61     sm = models.next();
62     assertFalse(sm instanceof SimilarityScoreModel);
63     assertFalse(sm instanceof PairwiseScoreModelI);
64     assertTrue(sm instanceof DistanceScoreModel);
65     assertEquals(sm.getName(), "Sequence Feature Similarity");
66   }
67
68   /**
69    * 'Test' that prints out score matrices in tab-delimited format. This test is
70    * intentionally not assigned to any group so would not be run as part of a
71    * suite. It makes no assertions and is just provided as a utility method for
72    * printing out matrices. Relocated here from ScoreMatrixPrinter.
73    */
74   @Test(groups = "none")
75   public void printAllMatrices_tabDelimited()
76   {
77     printAllMatrices(false);
78   }
79
80   /**
81    * 'Test' that prints out score matrices in html format. This test is
82    * intentionally not assigned to any group so would not be run as part of a
83    * suite. It makes no assertions and is just provided as a utility method for
84    * printing out matrices. Relocated here from ScoreMatrixPrinter.
85    */
86   @Test(groups = "none")
87   public void printAllMatrices_asHtml()
88   {
89     printAllMatrices(true);
90   }
91
92   /**
93    * Print all registered ScoreMatrix as plain or html tables
94    * 
95    * @param asHtml
96    */
97   protected void printAllMatrices(boolean asHtml)
98   {
99     for (ScoreModelI sm : ScoreModels.getInstance().getModels())
100     {
101       if (sm instanceof ScoreMatrix)
102       {
103         System.out.println(((ScoreMatrix) sm).outputMatrix(asHtml));
104       }
105     }
106   }
107 }