Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[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.lang.reflect.Constructor;
11 import java.lang.reflect.InvocationTargetException;
12 import java.util.Iterator;
13
14 import org.testng.annotations.Test;
15
16 public class ScoreModelsTest
17 {
18   /**
19    * Verify that the singleton constructor successfully loads Jalview's built-in
20    * score models
21    */
22   @Test(groups = "Functional")
23   public void testConstructor()
24   {
25     Iterator<ScoreModelI> models = ScoreModels.getInstance().getModels()
26             .iterator();
27     assertTrue(models.hasNext());
28
29     /*
30      * models are served in order of addition
31      */
32     ScoreModelI sm = models.next();
33     assertTrue(sm instanceof SimilarityScoreModel);
34     assertTrue(sm instanceof PairwiseScoreModelI);
35     assertFalse(sm instanceof DistanceScoreModel);
36     assertEquals(sm.getName(), "BLOSUM62");
37     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('I', 'R'), -3f);
38
39     sm = models.next();
40     assertTrue(sm instanceof SimilarityScoreModel);
41     assertTrue(sm instanceof PairwiseScoreModelI);
42     assertFalse(sm instanceof DistanceScoreModel);
43     assertEquals(sm.getName(), "PAM250");
44     assertEquals(((PairwiseScoreModelI) sm).getPairwiseScore('R', 'C'), -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
108   @Test(groups = "Functional")
109   public void testInstantiate()
110           throws InstantiationException, IllegalAccessException,
111           NoSuchMethodException, SecurityException,
112           IllegalArgumentException, InvocationTargetException
113   {
114     Class<? extends Object> c = ScoreModels.class;
115     Constructor<? extends Object> con = c.getDeclaredConstructor();
116     con.setAccessible(true);
117     Object o = con.newInstance();
118     System.out.println(o);
119   }
120 }