JAL-2403 extract method refactoring of FeatureScoreModel + test
[jalview.git] / test / jalview / analysis / scoremodels / FeatureScoreModelTest.java
index 292b576..14254f7 100644 (file)
  */
 package jalview.analysis.scoremodels;
 
+import static org.testng.Assert.assertEquals;
+
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.JvOptionPane;
+import jalview.io.DataSourceType;
 import jalview.io.FileLoader;
-import jalview.io.FormatAdapter;
 
 import java.util.Arrays;
 
 import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class FeatureScoreModelTest
 {
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   public static String alntestFile = "FER1_MESCR/72-76 DVYIL\nFER1_SPIOL/71-75 DVYIL\nFER3_RAPSA/21-25 DVYVL\nFER1_MAIZE/73-77 DVYIL\n";
 
   int[] sf1 = new int[] { 74, 74, 73, 73, 23, 23, -1, -1 };
@@ -42,10 +54,21 @@ public class FeatureScoreModelTest
 
   int[] sf3 = new int[] { -1, -1, -1, -1, -1, -1, 76, 77 };
 
+  /**
+   * <pre>
+   * Load test alignment and add features to sequences: 
+   *      FER1_MESCR FER1_SPIOL FER3_RAPSA FER1_MAIZE 
+   *  sf1     X          X          X  
+   *  sf2                X                     X 
+   *  sf3                                      X
+   * </pre>
+   * 
+   * @return
+   */
   public AlignFrame getTestAlignmentFrame()
   {
     AlignFrame alf = new FileLoader(false).LoadFileWaitTillLoaded(
-            alntestFile, FormatAdapter.PASTE);
+            alntestFile, DataSourceType.PASTE);
     AlignmentI al = alf.getViewport().getAlignment();
     Assert.assertEquals(al.getHeight(), 4);
     Assert.assertEquals(al.getWidth(), 5);
@@ -87,6 +110,7 @@ public class FeatureScoreModelTest
     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
             .getAlignPanel()));
     alf.selectAllSequenceMenuItem_actionPerformed(null);
+
     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
             true));
     Assert.assertTrue(dm[0][2] == 0f,
@@ -150,7 +174,7 @@ public class FeatureScoreModelTest
     String alignment = "a CCCCCCGGGGGGCCCCCC\n" + "b CCCCCCGGGGGGCCCCCC\n"
             + "c CCCCCCGGGGGGCCCCCC\n";
     AlignFrame af = new jalview.io.FileLoader(false)
-            .LoadFileWaitTillLoaded(alignment, FormatAdapter.PASTE);
+            .LoadFileWaitTillLoaded(alignment, DataSourceType.PASTE);
     SequenceI aseq = af.getViewport().getAlignment().getSequenceAt(0);
     SequenceFeature sf = null;
     sf = new SequenceFeature("disulphide bond", "", 2, 5, Float.NaN, "");
@@ -180,4 +204,56 @@ public class FeatureScoreModelTest
             .size(), 0);
   }
 
+  @Test(groups = { "Functional" })
+  public void testFindDistances() throws Exception
+  {
+    String seqs = ">s1\nABCDE\n>seq2\nABCDE\n";
+    AlignFrame alf = new FileLoader().LoadFileWaitTillLoaded(seqs,
+            DataSourceType.PASTE);
+    SequenceI s1 = alf.getViewport().getAlignment().getSequenceAt(0);
+    SequenceI s2 = alf.getViewport().getAlignment().getSequenceAt(1);
+
+    /*
+     * set domain and variant features thus:
+     *     ----5
+     *  s1 ddd..
+     *  s1 .vvv.
+     *  s1 ..vvv    
+     *  s2 .ddd. 
+     *  s2 vv..v
+     *  The number of unshared feature types per column is
+     *     20120 (two features of the same type doesn't affect score)
+     *  giving an average (pairwise distance) of 5/5 or 1.0 
+     */
+    s1.addSequenceFeature(new SequenceFeature("domain", null, 1, 3, 0f,
+            null));
+    s1.addSequenceFeature(new SequenceFeature("variant", null, 2, 4, 0f,
+            null));
+    s1.addSequenceFeature(new SequenceFeature("variant", null, 3, 5, 0f,
+            null));
+    s2.addSequenceFeature(new SequenceFeature("domain", null, 2, 4, 0f,
+            null));
+    s2.addSequenceFeature(new SequenceFeature("variant", null, 1, 2, 0f,
+            null));
+    s2.addSequenceFeature(new SequenceFeature("variant", null, 5, 5, 0f,
+            null));
+    alf.setShowSeqFeatures(true);
+    alf.getFeatureRenderer().findAllFeatures(true);
+
+    FeatureScoreModel fsm = new FeatureScoreModel();
+    Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
+            .getAlignPanel()));
+    alf.selectAllSequenceMenuItem_actionPerformed(null);
+
+    float[][] distances = fsm.findDistances(alf.getViewport()
+            .getAlignmentView(true));
+    assertEquals(distances.length, 2);
+    assertEquals(distances[0][0], 0f);
+    assertEquals(distances[1][1], 0f);
+    // these left to fail pending resolution of
+    // JAL-2424 (dividing score by 6, not 5)
+    assertEquals(distances[0][1], 1f);
+    assertEquals(distances[1][0], 1f);
+  }
+
 }