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