Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / test / jalview / project / Jalview2xmlTests.java
index afc445e..489916e 100644 (file)
@@ -27,6 +27,23 @@ import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
+import java.awt.Color;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.swing.JInternalFrame;
+
+import org.testng.Assert;
+import org.testng.AssertJUnit;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
 import jalview.analysis.scoremodels.SimilarityParams;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
@@ -34,9 +51,14 @@ import jalview.api.FeatureColourI;
 import jalview.api.ViewStyleI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.GeneLocus;
+import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.HiddenSequences;
+import jalview.datamodel.Mapping;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.PDBEntry.Type;
+import jalview.datamodel.Sequence.DBModList;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
@@ -48,7 +70,6 @@ import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
 import jalview.gui.AlignmentPanel;
 import jalview.gui.Desktop;
-import jalview.gui.FeatureRenderer;
 import jalview.gui.JvOptionPane;
 import jalview.gui.PCAPanel;
 import jalview.gui.PopupMenu;
@@ -68,27 +89,20 @@ import jalview.schemes.RNAHelicesColour;
 import jalview.schemes.StrandColourScheme;
 import jalview.schemes.TCoffeeColourScheme;
 import jalview.structure.StructureImportSettings;
+import jalview.util.MapList;
 import jalview.util.matcher.Condition;
 import jalview.viewmodel.AlignmentViewport;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
 
-import java.awt.Color;
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.swing.JInternalFrame;
-
-import org.testng.Assert;
-import org.testng.AssertJUnit;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
+import junit.extensions.PA;
 @Test(singleThreaded = true)
 public class Jalview2xmlTests extends Jalview2xmlBase
 {
+  @AfterMethod(alwaysRun = true)
+  public void tearDown()
+  {
+    Desktop.getInstance().closeAll_actionPerformed(null);
+  }
 
   @Override
   @BeforeClass(alwaysRun = true)
@@ -115,7 +129,8 @@ public class Jalview2xmlTests extends Jalview2xmlBase
             af.getViewport()
                     .getGlobalColourScheme() instanceof RNAHelicesColour,
             "Couldn't apply RNA helices colourscheme");
-    assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
+    af.saveAlignment(tfile, FileFormat.Jalview);
+    assertTrue(af.isSaveAlignmentSuccessful(),
             "Failed to store as a project.");
     af.closeMenuItem_actionPerformed(true);
     af = null;
@@ -147,16 +162,18 @@ public class Jalview2xmlTests extends Jalview2xmlBase
             DataSourceType.FILE);
     assertNotNull(af, "Didn't read input file " + inFile);
     af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
-    assertSame(af.getViewport().getGlobalColourScheme().getClass(),
+    AlignViewportI viewport = af.getViewport();
+    assertSame(viewport.getGlobalColourScheme().getClass(),
             TCoffeeColourScheme.class, "Didn't set T-coffee colourscheme");
     assertNotNull(
-            ColourSchemeProperty.getColourScheme(
-                    af.getViewport().getAlignment(),
-                    af.getViewport().getGlobalColourScheme()
+            ColourSchemeProperty.getColourScheme(viewport,
+                    viewport.getAlignment(),
+                    viewport.getGlobalColourScheme()
                             .getSchemeName()),
             "Recognise T-Coffee score from string");
 
-    assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
+    af.saveAlignment(tfile, FileFormat.Jalview);
+    assertTrue(af.isSaveAlignmentSuccessful(),
             "Failed to store as a project.");
     af.closeMenuItem_actionPerformed(true);
     af = null;
@@ -202,7 +219,8 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
     af.alignPanel.alignmentChanged();
-    assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
+    af.saveAlignment(tfile, FileFormat.Jalview);
+    assertTrue(af.isSaveAlignmentSuccessful(),
             "Failed to store as a project.");
     af.closeMenuItem_actionPerformed(true);
     af = null;
@@ -267,7 +285,8 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
     assertNotNull(af, "Didn't read in the example file correctly.");
-    assertTrue(Desktop.getAlignFrames().length == 1 + origCount,
+    assertEquals(Desktop.getAlignFrames().length,
+            1 + origCount,
             "Didn't gather the views in the example file.");
 
   }
@@ -401,7 +420,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = { "Functional" }, enabled = true)
   public void testStoreAndRecoverExpandedviews() throws Exception
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
 
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
@@ -429,7 +448,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     {
       Assert.fail("Didn't save the expanded view state", e);
     }
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     if (Desktop.getAlignFrames() != null)
     {
       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
@@ -455,7 +474,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = { "Functional" })
   public void testStoreAndRecoverReferenceSeqSettings() throws Exception
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
     assertNotNull(af, "Didn't read in the example file correctly.");
@@ -494,7 +513,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     {
       Assert.fail("Didn't save the expanded view state", e);
     }
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     if (Desktop.getAlignFrames() != null)
     {
       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
@@ -588,7 +607,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = { "Functional" })
   public void testStoreAndRecoverGroupRepSeqs() throws Exception
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
             "examples/uniref50.fa", DataSourceType.FILE);
     assertNotNull(af, "Didn't read in the example file correctly.");
@@ -663,7 +682,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     {
       Assert.fail("Didn't save the expanded view state", e);
     }
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     if (Desktop.getAlignFrames() != null)
     {
       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
@@ -708,7 +727,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = { "Functional" })
   public void testStoreAndRecoverPDBEntry() throws Exception
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     String exampleFile = "examples/3W5V.pdb";
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
             DataSourceType.FILE);
@@ -757,7 +776,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     {
       Assert.fail("Didn't save the state", e);
     }
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     if (Desktop.getAlignFrames() != null)
     {
       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
@@ -795,6 +814,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
               "Mismatch PDBEntry 'Type'");
       Assert.assertNotNull(recov.getFile(),
               "Recovered PDBEntry should have a non-null file entry");
+      Assert.assertEquals(recov.getFile().toLowerCase(Locale.ENGLISH).lastIndexOf("pdb"),recov.getFile().length()-3, "Recovered PDBEntry file should have PDB suffix");
     }
   }
 
@@ -808,16 +828,17 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = { "Functional" })
   public void testStoreAndRecoverColourThresholds() throws IOException
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
             "examples/uniref50.fa", DataSourceType.FILE);
 
-    AlignViewport av = af.getViewport();
+    AlignViewportI av = af.getViewport();
     AlignmentI al = av.getAlignment();
 
     /*
      * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
      */
+    av.setColourAppliesToAllGroups(false);
     af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
     assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
     af.abovePIDThreshold_actionPerformed(true);
@@ -836,13 +857,16 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     /*
      * create a group with Strand colouring, 30% Conservation
      * and 40% PID threshold
+     * (notice menu action applies to selection group even if mouse click
+     * is at a sequence not in the group)
      */
     SequenceGroup sg = new SequenceGroup();
     sg.addSequence(al.getSequenceAt(0), false);
     sg.setStartRes(15);
     sg.setEndRes(25);
     av.setSelectionGroup(sg);
-    PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
+    PopupMenu popupMenu = new PopupMenu(af.alignPanel, al.getSequenceAt(2),
+            null);
     popupMenu.changeColour_actionPerformed(
             JalviewColourScheme.Strand.toString());
     assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
@@ -867,7 +891,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
             ".jvp");
     tfile.deleteOnExit();
     new Jalview2XML(false).saveState(tfile);
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
             DataSourceType.FILE);
     Assert.assertNotNull(af, "Failed to reload project");
@@ -918,7 +942,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     /*
      * set colour schemes for features
      */
-    FeatureRenderer fr = af.getFeatureRenderer();
+    FeatureRendererModel fr = af.getFeatureRenderer();
     fr.findAllFeatures(true);
 
     // type1: red
@@ -979,7 +1003,8 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     File tfile = File.createTempFile("JalviewTest", ".jvp");
     tfile.deleteOnExit();
     String filePath = tfile.getAbsolutePath();
-    assertTrue(af.saveAlignment(filePath, FileFormat.Jalview),
+    af.saveAlignment(filePath, FileFormat.Jalview);
+    assertTrue(af.isSaveAlignmentSuccessful(),
             "Failed to store as a project.");
 
     /*
@@ -1021,7 +1046,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     assertEquals(fr.getFeatureFilter("type2").toStableString(),
             "(Score LE 2.4) AND (Score GT 1.1)");
     assertEquals(fr.getFeatureFilter("type3").toStableString(),
-            "(AF Contains X) OR (CSQ:PolyPhen NE 0.0)");
+            "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
   }
 
   private void addFeature(SequenceI seq, String featureType, int score)
@@ -1053,6 +1078,59 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   }
 
   /**
+   * Load an HMM profile to an alignment, and confirm it is correctly restored
+   * when reloaded from project
+   * 
+   * @throws IOException
+   */
+  @Test(groups = { "Functional" })
+  public void testStoreAndRecoverHmmProfile() throws IOException
+  {
+    Desktop.getInstance().closeAll_actionPerformed(null);
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
+            "examples/uniref50.fa", DataSourceType.FILE);
+  
+    AlignViewportI av = af.getViewport();
+    AlignmentI al = av.getAlignment();
+
+    /*
+     * mimic drag and drop of hmm file on to alignment
+     */
+    AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
+            "examples/uniref50.hmm", DataSourceType.FILE);
+    al.insertSequenceAt(0,
+            af2.getViewport().getAlignment().getSequenceAt(0));
+
+    /*
+     * check it loaded in
+     */
+    SequenceI hmmSeq = al.getSequenceAt(0);
+    assertTrue(hmmSeq.hasHMMProfile());
+    HiddenMarkovModel hmm = hmmSeq.getHMM();
+    assertSame(hmm.getConsensusSequence(), hmmSeq);
+
+    /*
+     * save project, close windows, reload project, verify
+     */
+    File tfile = File.createTempFile("testStoreAndRecoverHmmProfile",
+            ".jvp");
+    tfile.deleteOnExit();
+    new Jalview2XML(false).saveState(tfile);
+    Desktop.getInstance().closeAll_actionPerformed(null);
+    af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
+            DataSourceType.FILE);
+    Assert.assertNotNull(af, "Failed to reload project");
+
+    hmmSeq = al.getSequenceAt(0);
+    assertTrue(hmmSeq.hasHMMProfile());
+    assertSame(hmm.getConsensusSequence(), hmmSeq);
+    Mapping mapToHmmConsensus = (Mapping) PA.getValue(hmm,
+            "mapToHmmConsensus");
+    assertNotNull(mapToHmmConsensus);
+    assertSame(mapToHmmConsensus.getTo(), hmmSeq.getDatasetSequence());
+  }
+
+  /**
    * pre 2.11 - jalview 2.10 erroneously created new dataset entries for each
    * view (JAL-3171) this test ensures we can import and merge those views
    */
@@ -1075,7 +1153,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = { "Functional" })
   public void testMergeDatasetsforManyViews() throws IOException
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
 
     // complex project - one dataset, several views on several alignments
     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
@@ -1119,7 +1197,7 @@ public class Jalview2xmlTests extends Jalview2xmlBase
   @Test(groups = "Functional")
   public void testPcaViewAssociation() throws IOException
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     final String PCAVIEWNAME = "With PCA";
     // create a new tempfile
     File tempfile = File.createTempFile("jvPCAviewAssoc", "jvp");
@@ -1153,10 +1231,10 @@ public class Jalview2xmlTests extends Jalview2xmlBase
     }
 
     // load again.
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
             tempfile.getCanonicalPath(), DataSourceType.FILE);
-    JInternalFrame[] frames = Desktop.instance.getAllFrames();
+    JInternalFrame[] frames = Desktop.getInstance().getAllFrames();
     // PCA and the tabbed alignment view should be the only two windows on the
     // desktop
     assertEquals(frames.length, 2,
@@ -1177,4 +1255,83 @@ public class Jalview2xmlTests extends Jalview2xmlBase
                     .getAlignViewport(),
             "Didn't restore correct view association for the PCA view");
   }
+
+  /**
+   * Test save and reload of DBRefEntry including GeneLocus in project
+   * 
+   * @throws Exception
+   */
+  @Test(groups = { "Functional" })
+  public void testStoreAndRecoverGeneLocus() throws Exception
+  {
+    Desktop.getInstance().closeAll_actionPerformed(null);
+    String seqData = ">P30419\nACDE\n>X1235\nGCCTGTGACGAA";
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(seqData,
+            DataSourceType.PASTE);
+    assertNotNull(af, "Didn't read in the example file correctly.");
+  
+    AlignmentViewPanel ap = Desktop.getAlignmentPanels(null)[0];
+    SequenceI pep = ap.getAlignment().getSequenceAt(0);
+    SequenceI cds = ap.getAlignment().getSequenceAt(1);
+
+    /*
+     * give 'protein' a dbref to self, a dbref with map to CDS,
+     * and a dbref with map to gene 'locus'
+     */
+    DBRefEntry dbref1 = new DBRefEntry("Uniprot", "1", "P30419", null);
+    pep.addDBRef(dbref1);
+    Mapping cdsmap = new Mapping(cds,
+            new MapList(new int[]
+            { 1, 4 }, new int[] { 1, 12 }, 1, 3));
+    DBRefEntry dbref2 = new DBRefEntry("EMBLCDS", "2", "X1235", cdsmap);
+    pep.addDBRef(dbref2);
+    Mapping locusmap = new Mapping(null,
+            new MapList(new int[]
+            { 1, 4 }, new int[] { 2674123, 2674135 }, 1, 3));
+    DBRefEntry dbref3 = new GeneLocus("human", "GRCh38", "5", locusmap);
+    pep.addDBRef(dbref3);
+
+    File tfile = File.createTempFile("testStoreAndRecoverGeneLocus",
+            ".jvp");
+    try
+    {
+      new Jalview2XML(false).saveState(tfile);
+    } catch (Throwable e)
+    {
+      Assert.fail("Didn't save the state", e);
+    }
+    Desktop.getInstance().closeAll_actionPerformed(null);
+  
+    new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
+            DataSourceType.FILE);
+    AlignmentViewPanel rap = Desktop.getAlignmentPanels(null)[0];
+    SequenceI rpep = rap.getAlignment().getSequenceAt(0);
+    DBModList<DBRefEntry> dbrefs = rpep.getDBRefs();
+    assertEquals(rpep.getName(), "P30419");
+    assertEquals(dbrefs.size(), 3);
+    DBRefEntry dbRef = dbrefs.get(0);
+    assertFalse(dbRef instanceof GeneLocus);
+    assertNull(dbRef.getMap());
+    assertEquals(dbRef, dbref1);
+
+    /*
+     * restored dbrefs with mapping have a different 'map to'
+     * sequence but otherwise match the original dbrefs
+     */
+    dbRef = dbrefs.get(1);
+    assertFalse(dbRef instanceof GeneLocus);
+    assertTrue(dbRef.equalRef(dbref2));
+    assertNotNull(dbRef.getMap());
+    SequenceI rcds = rap.getAlignment().getSequenceAt(1);
+    assertSame(dbRef.getMap().getTo(), rcds);
+    // compare MapList but not map.to
+    assertEquals(dbRef.getMap().getMap(), dbref2.getMap().getMap());
+
+    /*
+     * GeneLocus map.to is null so can compare Mapping objects
+     */
+    dbRef = dbrefs.get(2);
+    assertTrue(dbRef instanceof GeneLocus);
+    assertEquals(dbRef, dbref3);
+  }
 }